Repository navigation
fix(middleware): honor Accept-Encoding q-values and preserve encoded responses in Gzip - #3239
Merged
Merged
Conversation
…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>
This was referenced Nov 20, 2026
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.
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)
gzip;q=0refuses gzip, and malformed or out-of-range weights also refuse gzip.*is honored. An explicitgzipentry wins over*, and a refusal wins over duplicate allowances.x-gzipis treated asgzip, matched case-insensitively.notgzipno longer match.Already-encoded responses (#2788)
Content-Encodingother thanidentity, such as one from a reverse proxy, now passes through unchanged instead of being compressed again.Content-Lengthis 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
Writereturned the size of the whole buffer instead oflen(b)when the MinLength threshold was crossed, soio.Copyfailed withinvalid write result.Flushgot a gzip stream with no end.Content-Encoding(any coding, any case) orContent-Length.Flushin passthrough mode now marksecho.Responseas committed, so the error handler can no longer write a plain body under a gzip header.Behavior change
A handler that sets
Content-Encoding: gzipitself and then writes plain bytes now sends those bytes unchanged. Before, the middleware compressed them. SettingContent-Encodingnow 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