From fc5de2bbc70a81a3fc17f306a72540619331933d Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Sat, 7 Mar 2026 12:48:54 +0100 Subject: [PATCH] refactor: replace deprecated httputil.ReverseProxy.Director with Rewrite - Update pkg/service/handlers/handlers_proxy.go and mirror_middleware.go to use the modern httputil.ReverseProxy.Rewrite hook (available since Go 1.20). - Fix SA1019 staticcheck warnings triggered by Go 1.26 deprecation notice. - Refactor proxy initialization to avoid NewSingleHostReverseProxy to prevent conflicts between Director and Rewrite hooks. - Standardize request modification using ProxyRequest.SetURL and ProxyRequest.Out. Co-authored-by: Junie --- pkg/service/handlers/handlers_proxy.go | 30 +++++++++++------------ pkg/service/handlers/mirror_middleware.go | 20 +++++++-------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/pkg/service/handlers/handlers_proxy.go b/pkg/service/handlers/handlers_proxy.go index 24792f1..543786e 100644 --- a/pkg/service/handlers/handlers_proxy.go +++ b/pkg/service/handlers/handlers_proxy.go @@ -55,23 +55,21 @@ func (s *Server) ServeProxy(target *url.URL) http.HandlerFunc { r.Body = io.NopCloser(bytes.NewBuffer(reqBody)) } - rp := httputil.NewSingleHostReverseProxy(target) - rp.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } + rp := &httputil.ReverseProxy{ + Rewrite: func(pr *httputil.ProxyRequest) { + pr.SetURL(target) + pr.Out.Host = target.Host + // If target has a path, we should probably append or replace. + // For Bose upstream, it's usually just the domain. + if target.Path != "" && target.Path != "/" { + pr.Out.URL.Path = target.Path + } - // Update director to set the correct host and path - originalDirector := rp.Director - rp.Director = func(req *http.Request) { - originalDirector(req) - req.Host = target.Host - // If target has a path, we should probably append or replace. - // For Bose upstream, it's usually just the domain. - if target.Path != "" && target.Path != "/" { - req.URL.Path = target.Path - } - - lp.LogRequest(req) + lp.LogRequest(pr.Out) + }, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }, } rp.ModifyResponse = func(res *http.Response) error { diff --git a/pkg/service/handlers/mirror_middleware.go b/pkg/service/handlers/mirror_middleware.go index 3065935..8265e8c 100644 --- a/pkg/service/handlers/mirror_middleware.go +++ b/pkg/service/handlers/mirror_middleware.go @@ -257,17 +257,15 @@ func (s *Server) performMirror(r *http.Request) *mirrorResponseRecorder { } // Create a proxy that doesn't write to the original ResponseWriter - proxy := httputil.NewSingleHostReverseProxy(target) - proxy.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - - // Record the mirrored request - originalDirector := proxy.Director - proxy.Director = func(req *http.Request) { - originalDirector(req) - req.Host = target.Host - req.Header.Set("X-Mirror-Request", "true") + proxy := &httputil.ReverseProxy{ + Rewrite: func(pr *httputil.ProxyRequest) { + pr.SetURL(target) + pr.Out.Host = target.Host + pr.Out.Header.Set("X-Mirror-Request", "true") + }, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }, } // Capture response for parity check and recording