Skip to content

fix(middleware): properly parse Accept-Encoding with quality values#1123

Open
happysnaker wants to merge 1 commit into
go-chi:masterfrom
happysnaker:fix-compress-accept-encoding
Open

fix(middleware): properly parse Accept-Encoding with quality values#1123
happysnaker wants to merge 1 commit into
go-chi:masterfrom
happysnaker:fix-compress-accept-encoding

Conversation

@happysnaker

Copy link
Copy Markdown
Contributor

Summary

Fixes #1069

The compress middleware currently fails to properly parse headers that include quality (q) values, such as:

Accept-Encoding: gzip;q=1.0, identity;q=0.5, *;q=0

The existing parsing logic does not strip quality values before matching, causing the middleware to not recognize valid encodings.

Changes

  • Updated the Accept-Encoding parsing in the compress middleware to strip quality values (;q=...) before matching against supported encodings
  • Ensures proper handling of wildcard (*) in the presence of quality values

Testing

  • Existing tests pass
  • Manual verification with curl against a chi server using various Accept-Encoding headers with quality values

Fix two issues in matchAcceptEncoding:
1. Substring matching: 'strings.Contains(v, encoding)' caused false
   positives (e.g. 'br' matched encoding 'b', 'bgzip' matched 'gzip').
   Now uses exact EqualFold comparison after stripping params.

2. q=0 ignored: 'gzip;q=0' was treated as acceptable per RFC 9110
   Section 12.5.3, q=0 means the encoding is not acceptable.
   Now checks for exact q=0 / q=0.0 / q=0.00 / q=0.000 and skips.

Also uses EqualFold for case-insensitive matching per RFC 9110.

Fixes go-chi#1069

Signed-off-by: happysnaker <73147033+happysnaker@users.noreply.github.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.

Compress middleware: matchAcceptEncoding uses substring match and ignores q=0

1 participant