From cd8943a3f9e0cbdfe02bdfcdc0393946c9265731 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Mon, 11 Nov 2019 22:04:20 +0100 Subject: Fix: Correctly handle URIs where the type is not separated by a slash --- gopherproxy.go | 13 ++++++++++++- template.go | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gopherproxy.go b/gopherproxy.go index 8b33c05..d98ef7c 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -132,7 +132,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass RawText string Lines []Item Error bool - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true}) + }{"", hostport, assetList, fmt.Sprintf("Error: %s", err), nil, true}) return } @@ -346,6 +346,17 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, vipsconcurrency i var inputStr = fmt.Sprintf("%v", input) return re.ReplaceAllString(inputStr, output) }, + "trimLeftChar": func(s string) string { + for i := range s { + if i > 0 { + return s[i:] + } + } + return s[:0] + }, + "hasPrefix": func(s string, prefix string) bool { + return strings.HasPrefix(s, prefix) + }, } tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) diff --git a/template.go b/template.go index f58349e..7aee61a 100644 --- a/template.go +++ b/template.go @@ -43,6 +43,11 @@ var tpltext = ` {{ . }} {{- else -}} {{- $href = printf "%s/1" $href -}} + {{- $part = $part | trimLeftChar -}} + {{- if not (eq $part "") -}} + {{- $href = printf "%s/%s" $href $part -}} + /{{ $part }} + {{- end -}} {{- end -}} {{- end -}} {{- if ne (len $uriParts) 0 -}} -- cgit v1.2.3-70-g09d2