git » gofer » commit bc3868c

http: Fix dir and file schemes when target is relative

author Alberto Bertogli
2020-05-05 20:33:48 UTC
committer Alberto Bertogli
2020-05-06 00:53:59 UTC
parent 577838d4b2251b6007d61fc0dae5304f37f27fde

http: Fix dir and file schemes when target is relative

When the target is a relative path, the URL parser will put it
in Opaque, not Path (for reasonable reasons), causing it not to
work properly.

This patch fixes that issue by adjusting the served path accordingly.

proxy/http.go +17 -9

diff --git a/proxy/http.go b/proxy/http.go
index 890db2a..ee0bec0 100644
--- a/proxy/http.go
+++ b/proxy/http.go
@@ -172,14 +172,24 @@ func adjustPath(req string, from string, to string) string {
 	return dst
 }
 
+func pathOrOpaque(u url.URL) string {
+	if u.Path != "" {
+		return u.Path
+	}
+
+	// This happens for relative paths, which are fine in this context.
+	return u.Opaque
+}
+
 func makeDir(from string, to url.URL) http.Handler {
 	from = stripDomain(from)
+	path := pathOrOpaque(to)
 
-	fs := http.FileServer(http.Dir(to.Path))
+	fs := http.FileServer(http.Dir(path))
 	return WithLogging("http:dir",
 		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 			tr, _ := trace.FromContext(r.Context())
-			tr.Printf("serving dir root %q", to.Path)
+			tr.Printf("serving dir root %q", path)
 
 			r.URL.Path = strings.TrimPrefix(r.URL.Path, from)
 			if r.URL.Path == "" || r.URL.Path[0] != '/' {
@@ -192,22 +202,20 @@ func makeDir(from string, to url.URL) http.Handler {
 }
 
 func makeStatic(from string, to url.URL) http.Handler {
+	path := pathOrOpaque(to)
+
 	return WithLogging("http:static",
 		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 			tr, _ := trace.FromContext(r.Context())
-			tr.Printf("statically serving %q", to.Path)
-			http.ServeFile(w, r, to.Path)
+			tr.Printf("statically serving %q", path)
+			http.ServeFile(w, r, path)
 		}),
 	)
 }
 
 func makeCGI(from string, to url.URL) http.Handler {
 	from = stripDomain(from)
-	path := to.Path
-	if path == "" {
-		// This happens for relative paths, which are fine in this context.
-		path = to.Opaque
-	}
+	path := pathOrOpaque(to)
 	args := queryToArgs(to.RawQuery)
 
 	return WithLogging("http:cgi",