mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
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.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// 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
|
||||
}
|
||||
+3
-4
@@ -16,7 +16,6 @@ import (
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/textproto"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -434,10 +433,10 @@ func HandleForward(h *config.WebForward, w http.ResponseWriter, r *http.Request,
|
||||
if h.StripPath {
|
||||
u := *r.URL
|
||||
u.Path = r.URL.Path[len(path):]
|
||||
if !strings.HasPrefix(u.Path, "/") {
|
||||
if !strings.HasPrefix(u.Path, "/") && u.Path != "" {
|
||||
u.Path = "/" + u.Path
|
||||
}
|
||||
u.RawPath = ""
|
||||
u.RawPath = u.EscapedPath()
|
||||
r.URL = &u
|
||||
}
|
||||
|
||||
@@ -484,7 +483,7 @@ func HandleForward(h *config.WebForward, w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
|
||||
// ReverseProxy will append any remaining path to the configured target URL.
|
||||
proxy := httputil.NewSingleHostReverseProxy(h.TargetURL)
|
||||
proxy := newSingleHostReverseProxy(h.TargetURL)
|
||||
proxy.FlushInterval = time.Duration(-1) // Flush after each write.
|
||||
proxy.ErrorLog = golog.New(mlog.LogWriter(mlog.New("net/http/httputil", nil).WithContext(r.Context()), mlog.LevelDebug, "reverseproxy error"), "", 0)
|
||||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
|
||||
Reference in New Issue
Block a user