From 7445d6c830d50a92e3ce6093122c2c64e8a8704e Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Wed, 25 Nov 2020 21:59:12 -0800 Subject: [PATCH] Always use lowercase hostname in requests --- README.md | 2 ++ pkg/gmitohtml/convert.go | 24 +++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3c19aa3..c4cb521 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ conversion tool and daemon [**Download gmitohtml**](https://gmitohtml.rocketnine.space/download/?sort=name&order=desc) +gmitohtml is available on Android as [Xenia](https://gitlab.com/tslocum/xenia). + ## Compile gmitohtml is written in [Go](https://golang.org). Run the following command to diff --git a/pkg/gmitohtml/convert.go b/pkg/gmitohtml/convert.go index 9e7049b..36c1f24 100644 --- a/pkg/gmitohtml/convert.go +++ b/pkg/gmitohtml/convert.go @@ -26,25 +26,35 @@ func rewriteURL(u string, loc *url.URL) string { scheme = "file" } - if strings.HasPrefix(u, "gemini://") { - return "http://" + daemonAddress + "/gemini/" + u[9:] - } else if strings.HasPrefix(u, "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, "//") { - newPath := u if u[0] != '/' { if loc.Path[len(loc.Path)-1] == '/' { - newPath = path.Join("/", loc.Path, u) + u = path.Join("/", loc.Path, u) } else { - newPath = path.Join("/", path.Dir(loc.Path), u) + u = path.Join("/", path.Dir(loc.Path), u) } } - return "http://" + daemonAddress + "/" + scheme + "/" + loc.Host + newPath + return "http://" + daemonAddress + "/" + scheme + "/" + strings.ToLower(loc.Host) + u } return "http://" + daemonAddress + "/" + scheme + "/" + u }