From a23ad13fd8028fdf0aa58e8f68368d82c679e03c Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Sat, 10 Jul 2021 11:28:08 -0700 Subject: [PATCH] Fix connection issue affecting some sites --- CHANGELOG | 6 ++- pkg/gmitohtml/convert.go | 81 +++++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3e4e65e..c178838 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ -1.0.4: +1.0.5: - Add option ConvertImages (thanks to @f) +- Fix connection issue affecting some sites + +1.0.4: +- Migrate to code.rocketnine.space 1.0.3: - Add hostname option diff --git a/pkg/gmitohtml/convert.go b/pkg/gmitohtml/convert.go index 6f4c53a..bb27075 100644 --- a/pkg/gmitohtml/convert.go +++ b/pkg/gmitohtml/convert.go @@ -22,45 +22,50 @@ var assetLock sync.Mutex var imageExtensions = []string{"png", "jpg", "jpeg", "gif", "svg", "webp"} func rewriteURL(u string, loc *url.URL) string { - if daemonAddress != "" { - scheme := "gemini" - if strings.HasPrefix(loc.Path, "/file/") { - scheme = "file" - } - - if strings.HasPrefix(u, "file://") { - if !allowFileAccess { - return "http://" + daemonAddress + "/?FileAccessNotAllowed" - } - return "http://" + daemonAddress + "/file/" + u[7:] - } - - offset := 0 - if strings.HasPrefix(u, "gemini://") { - offset = 9 - } - firstSlash := strings.IndexRune(u[offset:], '/') - if firstSlash != -1 { - u = strings.ToLower(u[:firstSlash+offset]) + u[firstSlash+offset:] - } - - if strings.HasPrefix(u, "gemini://") { - return "http://" + daemonAddress + "/gemini/" + u[9:] - } else if strings.Contains(u, "://") { - return u - } else if loc != nil && len(u) > 0 && !strings.HasPrefix(u, "//") { - if u[0] != '/' { - if loc.Path[len(loc.Path)-1] == '/' { - u = path.Join("/", loc.Path, u) - } else { - u = path.Join("/", path.Dir(loc.Path), u) - } - } - return "http://" + daemonAddress + "/" + scheme + "/" + strings.ToLower(loc.Host) + u - } - return "http://" + daemonAddress + "/" + scheme + "/" + u + if daemonAddress == "" { + return u } - return u + + if loc.Path == "" { + loc.Path = "/" + } + + scheme := "gemini" + if strings.HasPrefix(loc.Path, "/file/") { + scheme = "file" + } + + if strings.HasPrefix(u, "file://") { + if !allowFileAccess { + return "http://" + daemonAddress + "/?FileAccessNotAllowed" + } + return "http://" + daemonAddress + "/file/" + u[7:] + } + + offset := 0 + if strings.HasPrefix(u, "gemini://") { + offset = 9 + } + firstSlash := strings.IndexRune(u[offset:], '/') + if firstSlash != -1 { + u = strings.ToLower(u[:firstSlash+offset]) + u[firstSlash+offset:] + } + + if strings.HasPrefix(u, "gemini://") { + return "http://" + daemonAddress + "/gemini/" + u[9:] + } else if strings.Contains(u, "://") { + return u + } else if loc != nil && len(u) > 0 && !strings.HasPrefix(u, "//") { + if u[0] != '/' { + if loc.Path[len(loc.Path)-1] == '/' { + u = path.Join("/", loc.Path, u) + } else { + u = path.Join("/", path.Dir(loc.Path), u) + } + } + return "http://" + daemonAddress + "/" + scheme + "/" + strings.ToLower(loc.Host) + u + } + return "http://" + daemonAddress + "/" + scheme + "/" + u } func newPage() []byte {