Merge pull request 'Fixes gemini relative links to work for all relative link types' (#168) from fix-relative-links into release-2.3.1

Since this has been reviewed as functional I am merging into the release branch. There will be another opportunity to review, if need be, for the release branch when a PR is made into `develop`.
This commit is contained in:
Sloom Sloum Sluom IV 2020-05-28 00:50:13 -04:00
commit 7a0506853a
1 changed files with 13 additions and 38 deletions

View File

@ -6,6 +6,7 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/url"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -343,8 +344,7 @@ func Visit(host, port, resource string, td *TofuDigest) (Capsule, error) {
resource = "/" resource = "/"
} }
currentUrl := fmt.Sprintf("gemini://%s:%s%s", host, port, resource) currentUrl := fmt.Sprintf("gemini://%s:%s%s", host, port, resource)
rootUrl := fmt.Sprintf("gemini://%s:%s", host, port) capsule.Content, capsule.Links = parseGemini(body, currentUrl)
capsule.Content, capsule.Links = parseGemini(body, rootUrl, currentUrl)
} else { } else {
capsule.Content = body capsule.Content = body
} }
@ -365,7 +365,7 @@ func Visit(host, port, resource string, td *TofuDigest) (Capsule, error) {
} }
} }
func parseGemini(b, rootUrl, currentUrl string) (string, []string) { func parseGemini(b, currentUrl string) (string, []string) {
splitContent := strings.Split(b, "\n") splitContent := strings.Split(b, "\n")
links := make([]string, 0, 10) links := make([]string, 0, 10)
@ -399,7 +399,7 @@ func parseGemini(b, rootUrl, currentUrl string) (string, []string) {
} }
if strings.Index(link, "://") < 0 { if strings.Index(link, "://") < 0 {
link = handleRelativeUrl(link, rootUrl, currentUrl) link, _ = handleRelativeUrl(link, currentUrl)
} }
links = append(links, link) links = append(links, link)
@ -417,42 +417,17 @@ func parseGemini(b, rootUrl, currentUrl string) (string, []string) {
return strings.Join(splitContent[:outputIndex], "\n"), links return strings.Join(splitContent[:outputIndex], "\n"), links
} }
func handleRelativeUrl(u, root, current string) string { // handleRelativeUrl provides link completion
if len(u) < 1 { func handleRelativeUrl(relLink, current string) (string, error) {
return u base, err := url.Parse(current)
if err != nil {
return relLink, err
} }
currentIsDir := (current[len(current)-1] == '/') rel, err := url.Parse(relLink)
if err != nil {
if u[0] == '/' { return relLink, err
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:]
} }
return base.ResolveReference(rel).String(), nil
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, "/")
current = current[:ind+1]
return current + u
} }
func hashCert(cert []byte) string { func hashCert(cert []byte) string {