Skip to content

Migrate httputil.ReverseProxy from Director to Rewrite#3603

Draft
zackverham wants to merge 3 commits intomainfrom
chore/migrate-director-to-rewrite
Draft

Migrate httputil.ReverseProxy from Director to Rewrite#3603
zackverham wants to merge 3 commits intomainfrom
chore/migrate-director-to-rewrite

Conversation

@zackverham
Copy link
Collaborator

@zackverham zackverham commented Feb 23, 2026

Summary

This PR fixes a go deprecation that was introduced by go 1.26. Interestingly, it looks like there was never any test coverage for the proxy package, so this PR also adds test coverage we were missing.


  • Migrates from the deprecated Director field to the newer Rewrite function in httputil.ReverseProxy
  • Director was deprecated in Go 1.26
  • The Rewrite function provides cleaner separation between inbound and outbound requests via the ProxyRequest type
  • Adds comprehensive test coverage for the proxy package (previously had none)

Changes

  • Replace Director field with Rewrite function
  • Remove baseProxy intermediate proxy (no longer needed)
  • Store targetURL as *url.URL instead of string
  • Use pr.SetURL() for URL rewriting instead of calling base Director
  • Inline stripSourcePrefix logic into rewrite() and proxyURL()

Key Differences

Aspect Director (old) Rewrite (new)
Signature func(req *http.Request) func(pr *httputil.ProxyRequest)
Request access Single req (clone) pr.In (original), pr.Out (to modify)
URL rewriting Call base Director Use pr.SetURL(targetURL)

Test Coverage Added

Added 17 tests covering all proxy functionality:

Test Description
TestBasicProxy Verifies requests are forwarded and responses returned
TestPathStripping Verifies sourcePath is removed from request paths
TestPathStrippingToRoot Verifies empty path becomes "/"
TestQueryStringPreserved Verifies query parameters pass through
TestCookiesStripped Verifies cookies are not forwarded (security)
TestHostHeaderSet Verifies Host header is set to target
TestRefererRewritten Verifies Referer header is rewritten
TestRefererEmpty Handles missing Referer gracefully
TestLocationHeaderRewritten Verifies redirect locations are rewritten
TestLocationHeaderRelativeUnchanged Relative redirects pass through unchanged
TestErrorHandler Verifies 502 on backend errors
TestPostRequestBody Verifies POST bodies are forwarded
TestProxyURL Tests the proxyURL() helper
TestProxyURLWithQueryString Tests query string handling in helper
TestProxyURLToRoot Tests root path mapping
TestProxyURLInvalidURL Tests error handling for invalid URLs
TestOtherHeadersPreserved Verifies non-cookie headers pass through

Test Plan

  • Code compiles successfully
  • Full test suite passes
  • New proxy tests pass (17/17)
  • Manual testing of proxy functionality

Closes #3572

🤖 Generated with Claude Code

zackverham and others added 3 commits February 23, 2026 13:26
Director was deprecated in Go 1.26. This migrates to the newer Rewrite
function which provides cleaner separation between inbound and outbound
requests via the ProxyRequest type.

Changes:
- Replace Director field with Rewrite function
- Remove baseProxy intermediate proxy (no longer needed)
- Store targetURL as *url.URL instead of string
- Use pr.SetURL() for URL rewriting instead of calling base Director
- Inline stripSourcePrefix logic into rewrite() and proxyURL()

Closes #3572

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comprehensive test suite covering:
- Basic proxying functionality
- Path stripping (sourcePath removal)
- Query string preservation
- Cookie stripping (security)
- Host header setting
- Referer header rewriting
- Location header rewriting for redirects
- Error handling (502 Bad Gateway)
- POST request body forwarding
- proxyURL helper function

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chore: Director deprecated in Go 1.26, migration to Rewrite tracked separately

1 participant