Skip to content

Commit 023a40e

Browse files
committed
webserver: don't force adding a trailing slash when forwarding
if the incoming request didn't have a trailing slash, and the forward target url didn't either, we would add a trailing slash nonetheless, which is not what i would expect to happen. it also made it impossible to serve a forward a single non-directory endpoint.
1 parent 29f5e86 commit 023a40e

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

http/reverseproxy.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Code in this file is based on net/http/httputil/reverseproxy.go from the Go
2+
// standard library, and covered by licenses/golang.org/x/sys/LICENSE.
3+
4+
package http
5+
6+
import (
7+
"net/http"
8+
"net/http/httputil"
9+
"net/url"
10+
"strings"
11+
)
12+
13+
func newSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
14+
director := func(req *http.Request) {
15+
rewriteRequestURL(req, target)
16+
}
17+
return &httputil.ReverseProxy{Director: director}
18+
}
19+
20+
func rewriteRequestURL(req *http.Request, target *url.URL) {
21+
targetQuery := target.RawQuery
22+
req.URL.Scheme = target.Scheme
23+
req.URL.Host = target.Host
24+
req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
25+
if targetQuery == "" || req.URL.RawQuery == "" {
26+
req.URL.RawQuery = targetQuery + req.URL.RawQuery
27+
} else {
28+
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
29+
}
30+
}
31+
32+
// Like in httputil, but we won't add a trailing slash if the original request did
33+
// not have one.
34+
func joinURLPath(a, b *url.URL) (path, rawpath string) {
35+
if a.RawPath == "" && b.RawPath == "" {
36+
return singleJoiningSlash(a.Path, b.Path), ""
37+
}
38+
// Same as singleJoiningSlash, but uses EscapedPath to determine
39+
// whether a slash should be added
40+
apath := a.EscapedPath()
41+
bpath := b.EscapedPath()
42+
43+
aslash := strings.HasSuffix(apath, "/")
44+
bslash := strings.HasPrefix(bpath, "/")
45+
46+
switch {
47+
case aslash && bslash:
48+
return a.Path + b.Path[1:], apath + bpath[1:]
49+
case !aslash && !bslash && bpath != "":
50+
return a.Path + "/" + b.Path, apath + "/" + bpath
51+
}
52+
return a.Path + b.Path, apath + bpath
53+
}
54+
55+
func singleJoiningSlash(a, b string) string {
56+
aslash := strings.HasSuffix(a, "/")
57+
bslash := strings.HasPrefix(b, "/")
58+
switch {
59+
case aslash && bslash:
60+
return a + b[1:]
61+
case !aslash && !bslash && b != "":
62+
return a + "/" + b
63+
}
64+
return a + b
65+
}

http/webserver.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"log/slog"
1717
"net"
1818
"net/http"
19-
"net/http/httputil"
2019
"net/textproto"
2120
"net/url"
2221
"os"
@@ -434,10 +433,10 @@ func HandleForward(h *config.WebForward, w http.ResponseWriter, r *http.Request,
434433
if h.StripPath {
435434
u := *r.URL
436435
u.Path = r.URL.Path[len(path):]
437-
if !strings.HasPrefix(u.Path, "/") {
436+
if !strings.HasPrefix(u.Path, "/") && u.Path != "" {
438437
u.Path = "/" + u.Path
439438
}
440-
u.RawPath = ""
439+
u.RawPath = u.EscapedPath()
441440
r.URL = &u
442441
}
443442

@@ -484,7 +483,7 @@ func HandleForward(h *config.WebForward, w http.ResponseWriter, r *http.Request,
484483
}
485484

486485
// ReverseProxy will append any remaining path to the configured target URL.
487-
proxy := httputil.NewSingleHostReverseProxy(h.TargetURL)
486+
proxy := newSingleHostReverseProxy(h.TargetURL)
488487
proxy.FlushInterval = time.Duration(-1) // Flush after each write.
489488
proxy.ErrorLog = golog.New(mlog.LogWriter(mlog.New("net/http/httputil", nil).WithContext(r.Context()), mlog.LevelDebug, "reverseproxy error"), "", 0)
490489
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {

0 commit comments

Comments
 (0)