diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 20 | 
1 files changed, 15 insertions, 5 deletions
| @@ -8,6 +8,7 @@ import ( | |||
| 8 | "io/ioutil" | 8 | "io/ioutil" | 
| 9 | "log" | 9 | "log" | 
| 10 | "net/http" | 10 | "net/http" | 
| 11 | "net/url" | ||
| 11 | "strings" | 12 | "strings" | 
| 12 | 13 | ||
| 13 | "github.com/prologic/go-gopher" | 14 | "github.com/prologic/go-gopher" | 
| @@ -17,6 +18,8 @@ var ( | |||
| 17 | bind = flag.String("bind", ":80", "[int]:port to bind to") | 18 | bind = flag.String("bind", ":80", "[int]:port to bind to") | 
| 18 | host = flag.String("host", "localhost", "host to proxy to") | 19 | host = flag.String("host", "localhost", "host to proxy to") | 
| 19 | port = flag.Int("port", 70, "port to proxy to") | 20 | port = flag.Int("port", 70, "port to proxy to") | 
| 21 | |||
| 22 | tpl *template.Template | ||
| 20 | ) | 23 | ) | 
| 21 | 24 | ||
| 22 | type tplRow struct { | 25 | type tplRow struct { | 
| @@ -44,7 +47,9 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir | |||
| 44 | } else { | 47 | } else { | 
| 45 | tr.Link = template.URL( | 48 | tr.Link = template.URL( | 
| 46 | fmt.Sprintf( | 49 | fmt.Sprintf( | 
| 47 | "%s%s", string(byte(x.Type)), x.Selector, | 50 | "/%s%s", | 
| 51 | string(byte(x.Type)), | ||
| 52 | url.QueryEscape(x.Selector), | ||
| 48 | ), | 53 | ), | 
| 49 | ) | 54 | ) | 
| 50 | } | 55 | } | 
| @@ -55,13 +60,20 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir | |||
| 55 | return tpl.Execute(w, struct { | 60 | return tpl.Execute(w, struct { | 
| 56 | Title string | 61 | Title string | 
| 57 | Lines []tplRow | 62 | Lines []tplRow | 
| 58 | }{"XXX", out}) | 63 | }{*host, out}) | 
| 59 | } | 64 | } | 
| 60 | 65 | ||
| 61 | func proxy(w http.ResponseWriter, req *http.Request) { | 66 | func proxy(w http.ResponseWriter, req *http.Request) { | 
| 62 | path := strings.TrimPrefix(req.URL.Path, "/") | 67 | path := strings.TrimPrefix(req.URL.Path, "/") | 
| 63 | 68 | ||
| 64 | res, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path)) | 69 | res, err := gopher.Get( | 
| 70 | fmt.Sprintf( | ||
| 71 | "gopher://%s:%d/%s", | ||
| 72 | *host, | ||
| 73 | *port, | ||
| 74 | url.QueryEscape(path), | ||
| 75 | ), | ||
| 76 | ) | ||
| 65 | if err != nil { | 77 | if err != nil { | 
| 66 | io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) | 78 | io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) | 
| 67 | return | 79 | return | 
| @@ -77,8 +89,6 @@ func proxy(w http.ResponseWriter, req *http.Request) { | |||
| 77 | } | 89 | } | 
| 78 | } | 90 | } | 
| 79 | 91 | ||
| 80 | var tpl *template.Template | ||
| 81 | |||
| 82 | func main() { | 92 | func main() { | 
| 83 | flag.Parse() | 93 | flag.Parse() | 
| 84 | 94 | ||
