From 18349a7a00d778738381ef3b1c1d2e30be879327 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Sat, 16 Nov 2019 10:42:19 +0100 Subject: Display proper HTML title --- gopherproxy.go | 71 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 27 deletions(-) (limited to 'gopherproxy.go') diff --git a/gopherproxy.go b/gopherproxy.go index 65c8f89..751cebf 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -50,25 +50,25 @@ type AssetList struct { PropFontW2 string } -func resolveURI(uri string, baseUrl *url.URL) (resolvedUri string) { +func resolveURI(uri string, baseURL *url.URL) (resolvedURI string) { if strings.HasPrefix(uri, "//") { - resolvedUri = "/gemini/" + strings.TrimPrefix(uri, "//") + resolvedURI = "/gemini/" + strings.TrimPrefix(uri, "//") } else if strings.HasPrefix(uri, "gemini://") { - resolvedUri = "/gemini/" + strings.TrimPrefix(uri, "gemini://") + resolvedURI = "/gemini/" + strings.TrimPrefix(uri, "gemini://") } else if strings.HasPrefix(uri, "gopher://") { - resolvedUri = "/gopher/" + strings.TrimPrefix(uri, "gopher://") + resolvedURI = "/gopher/" + strings.TrimPrefix(uri, "gopher://") } else { url, err := url.Parse(uri) if err != nil { return "" } - adjustedUrl := baseUrl.ResolveReference(url) - if adjustedUrl.Scheme == "gemini" { - resolvedUri = "/gemini/" + adjustedUrl.Host + adjustedUrl.Path - } else if adjustedUrl.Scheme == "gopher" { - resolvedUri = "/gopher/" + adjustedUrl.Host + adjustedUrl.Path + adjustedURI := baseURL.ResolveReference(url) + if adjustedURI.Scheme == "gemini" { + resolvedURI = "/gemini/" + adjustedURI.Host + adjustedURI.Path + } else if adjustedURI.Scheme == "gopher" { + resolvedURI = "/gopher/" + adjustedURI.Host + adjustedURI.Path } else { - resolvedUri = adjustedUrl.String() + resolvedURI = adjustedURI.String() } } @@ -123,7 +123,11 @@ func renderGopherDirectory(w http.ResponseWriter, tpl *template.Template, assetL } if title == "" { - title = hostport + if uri != "" { + title = fmt.Sprintf("%s/%s", hostport, uri) + } else { + title = hostport + } } return tpl.Execute(w, struct { @@ -138,7 +142,7 @@ func renderGopherDirectory(w http.ResponseWriter, tpl *template.Template, assetL } func parseGeminiDocument(body *bytes.Buffer, uri string, hostport string) (items []Item) { - baseUrl, err := url.Parse(fmt.Sprintf( + baseURL, err := url.Parse(fmt.Sprintf( "gemini://%s/%s", hostport, uri, @@ -160,7 +164,7 @@ func parseGeminiDocument(body *bytes.Buffer, uri string, hostport string) (items linkMatch := GeminiLinkPattern.FindStringSubmatch(line) if len(linkMatch) != 0 && linkMatch[0] != "" { item.Type = ITEM_TYPE_GEMINI_LINK - item.Link = template.URL(resolveURI(linkMatch[1], baseUrl)) + item.Link = template.URL(resolveURI(linkMatch[1], baseURL)) item.Text = linkMatch[2] if item.Text == "" { item.Text = linkMatch[1] @@ -201,6 +205,8 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass return } + title := hostport + var qs string if req.URL.RawQuery != "" { @@ -217,10 +223,14 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{"", hostport, assetList, fmt.Sprintf("Error: %s", err), nil, true, "gopher"}) + }{title, hostport, assetList, fmt.Sprintf("Error: %s", err), nil, true, "gopher"}) return } + if uri != "" { + title = fmt.Sprintf("%s/%s", hostport, uri) + } + res, err := gopher.Get( fmt.Sprintf( "gopher://%s/%s%s", @@ -239,15 +249,14 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gopher"}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gopher"}) return } if res.Body != nil { if len(parts) < 2 { io.Copy(w, res.Body) - } else if strings.HasPrefix(parts[1], "0") && !strings.HasSuffix(uri, ".xml") && !strings.HasSuffix(uri, ".asc") { //strings.HasSuffix(uri, ".txt") || strings.HasSuffix(uri, ".md") { - // handle .txt files + } else if strings.HasPrefix(parts[1], "0") && !strings.HasSuffix(uri, ".xml") && !strings.HasSuffix(uri, ".asc") { buf := new(bytes.Buffer) buf.ReadFrom(res.Body) tpl.Execute(w, struct { @@ -258,7 +267,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, buf.String(), nil, false, "gopher"}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, buf.String(), nil, false, "gopher"}) } else if strings.HasPrefix(parts[1], "T") { _, _, err = vips.NewTransform(). Load(res.Body). @@ -280,8 +289,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gopher"}) - return + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gopher"}) } } } @@ -304,6 +312,8 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass return } + title := hostport + var qs string if req.URL.RawQuery != "" { @@ -320,10 +330,14 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{"", hostport, assetList, fmt.Sprintf("Error: %s", err), nil, true, "gemini"}) + }{title, hostport, assetList, fmt.Sprintf("Error: %s", err), nil, true, "gemini"}) return } + if uri != "" { + title = fmt.Sprintf("%s/%s", hostport, uri) + } + res, err := GeminiGet( fmt.Sprintf( "gemini://%s/%s%s", @@ -342,12 +356,12 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gemini"}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gemini"}) return } if int(res.Header.Status/10) == 3 { - baseUrl, err := url.Parse(fmt.Sprintf( + baseURL, err := url.Parse(fmt.Sprintf( "gemini://%s/%s", hostport, uri, @@ -361,11 +375,11 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gemini"}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true, "gemini"}) return } - http.Redirect(w, req, resolveURI(res.Header.Meta, baseUrl), http.StatusFound) + http.Redirect(w, req, resolveURI(res.Header.Meta, baseURL), http.StatusFound) return } @@ -378,7 +392,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error %d: %s", res.Header.Status, res.Header.Meta), nil, true, "gemini"}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error %d: %s", res.Header.Status, res.Header.Meta), nil, true, "gemini"}) return } @@ -417,7 +431,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass Lines []Item Error bool Protocol string - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, rawText, items, false, "gemini"}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, rawText, items, false, "gemini"}) } else { io.Copy(w, res.Body) } @@ -594,6 +608,9 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, vipsconcurrency i "hasPrefix": func(s string, prefix string) bool { return strings.HasPrefix(s, prefix) }, + "title": func(s string) string { + return strings.Title(s) + }, } tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) -- cgit v1.2.3-54-g00ecf