Merge pull request 'Reworks how relative URLs are handled for gemini' (#153) from pathing-fixes into release-2.3.0

This commit is contained in:
Sloom Sloum Sluom IV 2020-05-19 10:36:48 -04:00
commit c34226e4c1
2 changed files with 39 additions and 10 deletions

View File

@ -977,8 +977,10 @@ func (c *client) handleGemini(u Url) {
go saveConfig() go saveConfig()
switch capsule.Status { switch capsule.Status {
case 1: case 1:
// Query
c.search("", u.Full, capsule.Content) c.search("", u.Full, capsule.Content)
case 2: case 2:
// Success
if capsule.MimeMaj == "text" || (c.Options["showimages"] == "true" && capsule.MimeMaj == "image") { if capsule.MimeMaj == "text" || (c.Options["showimages"] == "true" && capsule.MimeMaj == "image") {
pg := MakePage(u, capsule.Content, capsule.Links) pg := MakePage(u, capsule.Content, capsule.Links)
pg.FileType = capsule.MimeMaj pg.FileType = capsule.MimeMaj
@ -996,14 +998,21 @@ func (c *client) handleGemini(u Url) {
c.saveFileFromData(capsule.Content, filename) c.saveFileFromData(capsule.Content, filename)
} }
case 3: case 3:
c.SetMessage(fmt.Sprintf("Follow redirect? (y/n): %s", capsule.Content), false) // Redirect
c.DrawMessage() lowerRedirect := strings.ToLower(capsule.Content)
ch := cui.Getch() lowerOriginal := strings.ToLower(u.Full)
if ch == 'y' || ch == 'Y' { if strings.Replace(lowerRedirect, lowerOriginal, "", 1) == "/" {
c.Visit(capsule.Content) c.Visit(capsule.Content)
} else { } else {
c.SetMessage("Redirect aborted", false) c.SetMessage(fmt.Sprintf("Follow redirect (y/n): %s?", capsule.Content), false)
c.DrawMessage() c.DrawMessage()
ch := cui.Getch()
if ch == 'y' || ch == 'Y' {
c.Visit(capsule.Content)
} else {
c.SetMessage("Redirect aborted", false)
c.DrawMessage()
}
} }
} }
} }

View File

@ -409,18 +409,38 @@ func handleRelativeUrl(u, root, current string) string {
if len(u) < 1 { if len(u) < 1 {
return u return u
} }
currentIsDir := (current[len(current)-1] == '/')
if u[0] == '/' { if u[0] == '/' {
return fmt.Sprintf("%s%s", root, u) 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, "/") ind := strings.LastIndex(current, "/")
if ind < 10 {
return fmt.Sprintf("%s/%s", root, u)
}
current = current[:ind+1] current = current[:ind+1]
return fmt.Sprintf("%s%s", current, u) return current + u
} }
func hashCert(cert []byte) string { func hashCert(cert []byte) string {