Files
mail/http/reverseproxy.go
Mechiel Lukkien 043c9fb22d 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.
2025-11-10 14:43:55 +01:00

66 lines
1.7 KiB
Go

// Code in this file is based on net/http/httputil/reverseproxy.go from the Go
// standard library, and covered by licenses/golang.org/x/sys/LICENSE.
package http
import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func newSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
director := func(req *http.Request) {
rewriteRequestURL(req, target)
}
return &httputil.ReverseProxy{Director: director}
}
func rewriteRequestURL(req *http.Request, target *url.URL) {
targetQuery := target.RawQuery
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
}
// Like in httputil, but we won't add a trailing slash if the original request did
// not have one.
func joinURLPath(a, b *url.URL) (path, rawpath string) {
if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), ""
}
// Same as singleJoiningSlash, but uses EscapedPath to determine
// whether a slash should be added
apath := a.EscapedPath()
bpath := b.EscapedPath()
aslash := strings.HasSuffix(apath, "/")
bslash := strings.HasPrefix(bpath, "/")
switch {
case aslash && bslash:
return a.Path + b.Path[1:], apath + bpath[1:]
case !aslash && !bslash && bpath != "":
return a.Path + "/" + b.Path, apath + "/" + bpath
}
return a.Path + b.Path, apath + bpath
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash && b != "":
return a + "/" + b
}
return a + b
}