Skip to content

fix(middleware): respect Accept-Encoding q-values and encoding tokens in Gzip (#3232) - #3234

Closed
Yshuro wants to merge 1 commit into
labstack:masterfrom
Yshuro:fix/gzip-accept-encoding-qvalues
Closed

Yshuro wants to merge 1 commit into
labstack:masterfrom
Yshuro:fix/gzip-accept-encoding-qvalues

Conversation

@Yshuro

@Yshuro Yshuro commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #3232.

What

The Gzip middleware previously determined whether to compress responses using a substring match:

if strings.Contains(c.Request().Header.Get(echo.HeaderAcceptEncoding), gzipScheme) {

This caused two issues:

  1. gzip;q=0 was compressed even though the client explicitly disallowed gzip.
  2. Partial matches such as custom-gzip or other-gzip triggered compression because they contain the substring gzip.

Fix

Replaced the substring check with token-aware parsing conforming to RFC 9110 Section 12.5.3:

  • Parses comma-separated encoding tokens and their optional quality parameters (q=...).
  • Compares encoding names case-insensitively (gzip, GZIP).
  • Honors q=0 as disallowing compression, while q > 0 allows it.
  • Supports the wildcard * fallback, with explicit gzip preferences taking precedence (e.g. *;q=1, gzip;q=0 disallows gzip, *;q=0, gzip;q=1 allows gzip).
  • Supports multiple Accept-Encoding header fields via req.Header.Values(echo.HeaderAcceptEncoding).
  • Preserves the existing Vary: Accept-Encoding response header in all cases.

Tests

Added TestGzip_AcceptEncodingQuality covering 17 subtest scenarios and TestAcceptsGzip_EdgeCases. Full suite passes:

ok  github.com/labstack/echo/v5           0.864s
ok  github.com/labstack/echo/v5/echotest  0.414s
ok  github.com/labstack/echo/v5/middleware 1.337s

…abstack#3232)

Signed-off-by: Sami Chergui <83331159+Yshuro@users.noreply.github.com>
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 @Yshuro for this fix to #3232. Review found a few gaps: malformed weights such as gzip;q= still enabled gzip, x-gzip was no longer matched, and when gzip appeared twice the last entry won. The fix landed in #3239 together with the fix for #2788 in the same middleware, and 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.

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

2 participants