From 0105bbb0d56f4ca845df1b73cd2a0b8e94a968ae Mon Sep 17 00:00:00 2001 From: James Mills Date: Fri, 23 Sep 2016 00:02:59 +1000 Subject: [PATCH] Add query support --- main.go | 20 +++++++++++++++----- template.go | 14 +++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 6815b0b..32ae98c 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "net/http" + "net/url" "strings" "github.com/prologic/go-gopher" @@ -17,6 +18,8 @@ var ( bind = flag.String("bind", ":80", "[int]:port to bind to") host = flag.String("host", "localhost", "host to proxy to") port = flag.Int("port", 70, "port to proxy to") + + tpl *template.Template ) type tplRow struct { @@ -44,7 +47,9 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir } else { tr.Link = template.URL( fmt.Sprintf( - "%s%s", string(byte(x.Type)), x.Selector, + "/%s%s", + string(byte(x.Type)), + url.QueryEscape(x.Selector), ), ) } @@ -55,13 +60,20 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir return tpl.Execute(w, struct { Title string Lines []tplRow - }{"XXX", out}) + }{*host, out}) } func proxy(w http.ResponseWriter, req *http.Request) { path := strings.TrimPrefix(req.URL.Path, "/") - res, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path)) + res, err := gopher.Get( + fmt.Sprintf( + "gopher://%s:%d/%s", + *host, + *port, + url.QueryEscape(path), + ), + ) if err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) return @@ -77,8 +89,6 @@ func proxy(w http.ResponseWriter, req *http.Request) { } } -var tpl *template.Template - func main() { flag.Parse() diff --git a/template.go b/template.go index 7c13451..c0a79a4 100644 --- a/template.go +++ b/template.go @@ -8,7 +8,19 @@ var tpltext = `
-{{range .Lines}} {{if .Link}}({{.Type}}) {{.Text}}{{else}}      {{.Text}}{{end}}
+{{range .Lines}} {{if .Link}}({{.Type}}) {{.Text}}{{else}}      {{.Text}}{{end}}
 {{end}}
+ + `