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 <junie@jetbrains.com>
This commit is contained in:
Tobias Gesellchen
2026-03-07 12:58:01 +01:00
co-authored by Junie
parent cf82feca06
commit fc5de2bbc7
2 changed files with 23 additions and 27 deletions
+14 -16
View File
@@ -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 {
+9 -11
View File
@@ -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