Skip to content

fix(middleware): honor Accept-Encoding q-values and preserve encoded responses in Gzip - #3239

Merged
vishr merged 1 commit into
masterfrom
fix/gzip-accept-and-passthrough
Nov 20, 2026
Merged

vishr merged 1 commit into
masterfrom
fix/gzip-accept-and-passthrough

Conversation

@vishr

@vishr vishr commented Nov 20, 2026

Copy link
Copy Markdown
Member

Fixes #3232 and #2788. Supersedes #3234 and #3237. Thanks to @Yshuro and @Twiink, credited as co-authors. #3237's reproduction confirmed that #2788 still happens on current v5.

Accept-Encoding (#3232)

  • The header is parsed per RFC 9110. gzip;q=0 refuses gzip, and malformed or out-of-range weights also refuse gzip.
  • * is honored. An explicit gzip entry wins over *, and a refusal wins over duplicate allowances.
  • x-gzip is treated as gzip, matched case-insensitively.
  • Substrings such as notgzip no longer match.

Already-encoded responses (#2788)

  • A response that already has a Content-Encoding other than identity, such as one from a reverse proxy, now passes through unchanged instead of being compressed again.
  • Content-Length is removed only when gzip actually changes the body, or when an error response replaces a body that was never written.

Other fixes in the same code

  • Write returned the size of the whole buffer instead of len(b) when the MinLength threshold was crossed, so io.Copy failed with invalid write result.
  • A handler that only calls Flush got a gzip stream with no end.
  • Error responses could keep a stale Content-Encoding (any coding, any case) or Content-Length.
  • A Flush in passthrough mode now marks echo.Response as committed, so the error handler can no longer write a plain body under a gzip header.

Behavior change

A handler that sets Content-Encoding: gzip itself and then writes plain bytes now sends those bytes unchanged. Before, the middleware compressed them. Setting Content-Encoding now means the body is already encoded.

Testing

go test -race ./... passes. New tests cover each item above. Each new test was checked to fail on the code before its fix.

🤖 Generated with Claude Code

…responses in Gzip

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>
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.

Gzip middleware ignores Accept-Encoding quality values and matches partial encoding names

1 participant