Skip to content

Preserve upstream Content-Encoding in gzip middleware - #3237

Closed
Twiink wants to merge 1 commit into
labstack:masterfrom
Twiink:fix/preserve-proxy-content-encoding
Closed

Twiink wants to merge 1 commit into
labstack:masterfrom
Twiink:fix/preserve-proxy-content-encoding

Conversation

@Twiink

@Twiink Twiink commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

With Gzip wrapping Proxy, an upstream Content-Encoding: gzip response is compressed again while still advertised as a single gzip stream. An upstream deflate response is also recompressed and relabeled. A client decoding once receives compressed bytes instead of the original payload.

Bypass gzip when a response already has Content-Encoding, before this middleware starts writing its own compressed stream. Forward the original status, headers, bytes and flushes, and discard the unused compressor during cleanup. This preserves Content-Length and encoded HEAD metadata as well.

Validation: real loopback upstream + Echo v5 proxy + client with automatic decompression disabled, covering gzip/deflate with three MinLength settings. Four cases fail on unchanged master and two buffered controls pass; all six pass with the fix. Four additional handler cases cover explicit/implicit headers, flushing and header-only responses. Existing gzip cases and the complete race suite pass, as do vet, staticcheck, golint and formatting. The full suite used command-local empty proxy variables so its IPv6 loopback requests bypass this machine's outbound proxy.

Related #2788: this reproduces the reported behavior on current v5 master f275a6b. The flush-only gzip completion fix in #3208 is independent and unchanged.

vishr added a commit that referenced this pull request Nov 20, 2026
…responses in Gzip (#3239)

Accept-Encoding is now parsed per RFC 9110: gzip;q=0 and malformed
weights refuse gzip, the wildcard is honored, x-gzip is treated as gzip,
and substrings such as "notgzip" no longer match.

Responses that already carry a Content-Encoding (for example from a
reverse proxy) are passed through instead of being compressed twice.
Content-Length is now removed only when the body is actually compressed
or when an error response replaces an unwritten body.

Also fixes Write reporting more bytes than it was given when the
MinLength threshold is crossed, a truncated gzip stream when a handler
only flushes, and error responses keeping a stale Content-Encoding.

Fixes #3232
Fixes #2788
Supersedes #3234, #3237

Co-authored-by: Yshuro <83331159+Yshuro@users.noreply.github.com>
Co-authored-by: Twiink <154496350+Twiink@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vishr

vishr commented Nov 20, 2026

Copy link
Copy Markdown
Member

Thank you @Twiink for this fix and for confirming that #2788 still happens on current v5. The fix landed in #3239 together with the Accept-Encoding fix for #3232. It also covers Content-Encoding: identity, error responses, and Content-Length on uncompressed responses. You are credited as co-author. Closing in favor of that PR.

@vishr vishr closed this Nov 20, 2026
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.

2 participants