diff --git a/agent/pkg/handler/proxy_http.go b/agent/pkg/handler/proxy_http.go index d840d456..79fa2c60 100644 --- a/agent/pkg/handler/proxy_http.go +++ b/agent/pkg/handler/proxy_http.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/http/httputil" + "strings" "time" "github.com/yusing/godoxy/agent/pkg/agent" @@ -43,10 +44,22 @@ func ProxyHTTP(w http.ResponseWriter, r *http.Request) { return } - r.URL.Scheme = "" - r.URL.Host = "" - r.URL.Path = r.URL.Path[agent.HTTPProxyURLPrefixLen:] // strip the {API_BASE}/proxy/http prefix - r.RequestURI = r.URL.String() + // Strip the {API_BASE}/proxy/http prefix while preserving URL escaping. + // + // NOTE: `r.URL.Path` is decoded. If we rewrite it without keeping `RawPath` + // in sync, Go may re-escape the path (e.g. turning "%5B" into "%255B"), + // which breaks urls with percent-encoded characters, like Next.js static chunk URLs. + prefix := agent.APIEndpointBase + agent.EndpointProxyHTTP + r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix) + if r.URL.RawPath != "" { + if after, ok := strings.CutPrefix(r.URL.RawPath, prefix); ok { + r.URL.RawPath = after + } else { + // RawPath is no longer a valid encoding for Path; force Go to re-derive it. + r.URL.RawPath = "" + } + } + r.RequestURI = "" rp := &httputil.ReverseProxy{ Director: func(r *http.Request) {