fix: strip Accept-Encoding from proxied requests#5
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts request header forwarding in the proxy so that Accept-Encoding is not forwarded upstream, allowing Go’s http.Transport to manage gzip transparently and ensuring the proxy always reads uncompressed JSON for parsing/rewriting/usage tracking.
Changes:
- Strip
Accept-EncodingincopyRequestHeadersto enable automatic gzip decompression on the proxy→upstream hop.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func copyRequestHeaders(dst, src http.Header) { | ||
| for key, values := range src { | ||
| if strings.EqualFold(key, "Authorization") || strings.EqualFold(key, "Host") { | ||
| if strings.EqualFold(key, "Authorization") || strings.EqualFold(key, "Host") || strings.EqualFold(key, "Accept-Encoding") { |
| func copyRequestHeaders(dst, src http.Header) { | ||
| for key, values := range src { | ||
| if strings.EqualFold(key, "Authorization") || strings.EqualFold(key, "Host") { | ||
| if strings.EqualFold(key, "Authorization") || strings.EqualFold(key, "Host") || strings.EqualFold(key, "Accept-Encoding") { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Strip
Accept-Encodingfrom forwarded request headers so Go'shttp.Transportmanages gzip compression transparently for the proxy→upstream leg.Problem: When clients send
Accept-Encoding: gzip, the proxy forwards it to the upstream. The upstream responds with a compressed body, but the proxy's JSON parsing, model rewriting, and usage tracking all expect uncompressed bytes — causing silent failures.Fix: Skip
Accept-EncodingincopyRequestHeaders. Go'shttp.Transportthen adds its ownAccept-Encoding: gzip, decompresses the response body automatically, and stripsContent-Encoding/Content-Lengthbefore the proxy reads it. The upstream still gets compressed responses (bandwinth efficient), but the proxy always operates on plain JSON.Works for both streaming (SSE) and non-streaming responses since gzip decompression is incremental.