aboutsummaryrefslogtreecommitdiffstats
path: root/gopherproxy.go
diff options
context:
space:
mode:
Diffstat (limited to 'gopherproxy.go')
-rw-r--r--gopherproxy.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/gopherproxy.go b/gopherproxy.go
index 4a2255f..8c85448 100644
--- a/gopherproxy.go
+++ b/gopherproxy.go
@@ -45,7 +45,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str
45 } else { 45 } else {
46 hostport = fmt.Sprintf("%s:%d", x.Host, x.Port) 46 hostport = fmt.Sprintf("%s:%d", x.Host, x.Port)
47 } 47 }
48 path := url.QueryEscape(x.Selector) 48 path := url.PathEscape(x.Selector)
49 path = strings.Replace(path, "%2F", "/", -1) 49 path = strings.Replace(path, "%2F", "/", -1)
50 tr.Link = template.URL( 50 tr.Link = template.URL(
51 fmt.Sprintf( 51 fmt.Sprintf(
@@ -73,25 +73,35 @@ func MakeGopherProxyHandler(tpl *template.Template, uri string) Handler {
73 return func(w http.ResponseWriter, req *http.Request) { 73 return func(w http.ResponseWriter, req *http.Request) {
74 parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/") 74 parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/")
75 hostport := parts[0] 75 hostport := parts[0]
76 path := strings.Join(parts[1:], "/")
77 76
78 if len(hostport) == 0 { 77 if len(hostport) == 0 {
79 http.Redirect(w, req, "/"+uri, http.StatusFound) 78 http.Redirect(w, req, "/"+uri, http.StatusFound)
80 return 79 return
81 } 80 }
82 81
82 var qs string
83
84 path := strings.Join(parts[1:], "/")
85
86 if req.URL.RawQuery != "" {
87 qs = fmt.Sprintf("?%s", url.QueryEscape(req.URL.RawQuery))
88 }
89
83 uri, err := url.QueryUnescape(path) 90 uri, err := url.QueryUnescape(path)
84 if err != nil { 91 if err != nil {
85 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) 92 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
86 return 93 return
87 } 94 }
95
88 res, err := gopher.Get( 96 res, err := gopher.Get(
89 fmt.Sprintf( 97 fmt.Sprintf(
90 "gopher://%s/%s", 98 "gopher://%s/%s%s",
91 hostport, 99 hostport,
92 uri, 100 uri,
101 qs,
93 ), 102 ),
94 ) 103 )
104
95 if err != nil { 105 if err != nil {
96 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) 106 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
97 return 107 return