From b9057508d9b79138f0297b688512523cce4f4309 Mon Sep 17 00:00:00 2001 From: sloum Date: Mon, 18 May 2020 20:10:02 -0700 Subject: [PATCH] Reworks how relative URLs are handled for gemini --- client.go | 19 ++++++++++++++----- gemini/gemini.go | 30 +++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 5322c08..b74e2c0 100644 --- a/client.go +++ b/client.go @@ -977,8 +977,10 @@ func (c *client) handleGemini(u Url) { go saveConfig() switch capsule.Status { case 1: + // Query c.search("", u.Full, capsule.Content) case 2: + // Success if capsule.MimeMaj == "text" || (c.Options["showimages"] == "true" && capsule.MimeMaj == "image") { pg := MakePage(u, capsule.Content, capsule.Links) pg.FileType = capsule.MimeMaj @@ -996,14 +998,21 @@ func (c *client) handleGemini(u Url) { c.saveFileFromData(capsule.Content, filename) } case 3: - c.SetMessage(fmt.Sprintf("Follow redirect (y/n): %s?", capsule.Content), false) - c.DrawMessage() - ch := cui.Getch() - if ch == 'y' || ch == 'Y' { + // Redirect + lowerRedirect := strings.ToLower(capsule.Content) + lowerOriginal := strings.ToLower(u.Full) + if strings.Replace(lowerRedirect, lowerOriginal, "", 1) == "/" { c.Visit(capsule.Content) } else { - c.SetMessage("Redirect aborted", false) + c.SetMessage(fmt.Sprintf("Follow redirect (y/n): %s?", capsule.Content), false) c.DrawMessage() + ch := cui.Getch() + if ch == 'y' || ch == 'Y' { + c.Visit(capsule.Content) + } else { + c.SetMessage("Redirect aborted", false) + c.DrawMessage() + } } } } diff --git a/gemini/gemini.go b/gemini/gemini.go index e13cb59..e90fe98 100644 --- a/gemini/gemini.go +++ b/gemini/gemini.go @@ -381,18 +381,38 @@ func handleRelativeUrl(u, root, current string) string { if len(u) < 1 { return u } + currentIsDir := (current[len(current)-1] == '/') if u[0] == '/' { return fmt.Sprintf("%s%s", root, u) + } else if strings.HasPrefix(u, "../") { + currentDir := strings.LastIndex(current, "/") + if currentIsDir { + upOne := strings.LastIndex(current[:currentDir], "/") + dirRoot := current[:upOne] + return dirRoot + u[2:] + } + return current[:currentDir] + u[2:] + } + + if strings.HasPrefix(u, "./") { + if len(u) == 2 { + return current + } + u = u[2:] + } + + if currentIsDir { + indPrevDir := strings.LastIndex(current[:len(current)-1], "/") + if indPrevDir < 9 { + return current + u + } + return current[:indPrevDir+1] + u } ind := strings.LastIndex(current, "/") - if ind < 10 { - return fmt.Sprintf("%s/%s", root, u) - } - current = current[:ind+1] - return fmt.Sprintf("%s%s", current, u) + return current + u } func hashCert(cert []byte) string {