Skip to content

fix(middleware): match gzip Content-Encoding case-insensitively#3056

Open
sonnemusk wants to merge 2 commits into
labstack:masterfrom
sonnemusk:fix/decompress-case-insensitive-encoding
Open

fix(middleware): match gzip Content-Encoding case-insensitively#3056
sonnemusk wants to merge 2 commits into
labstack:masterfrom
sonnemusk:fix/decompress-case-insensitive-encoding

Conversation

@sonnemusk

Copy link
Copy Markdown

Summary

Decompress middleware only accepted exact Content-Encoding: gzip. Values like Gzip or GZIP were skipped.

Change

  • Compare with strings.EqualFold after trim.
  • Add regression tests for common case variants.

Test plan

  • go test ./middleware -run TestDecompress

Proxies and clients may send Content-Encoding: Gzip or GZIP. Exact string
compare skipped decompression. Trim and EqualFold against "gzip".
@aldas

aldas commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

I am not agains this change but please give us real world example then gzip has spaces or different case. are we dealing theoretical problem, fix for some custom client or some widespread problem

@sonnemusk

Copy link
Copy Markdown
Author

This is primarily RFC compliance, not a custom client quirk.

RFC 9110 §8.4.1 (and earlier HTTP RFCs) define content codings as case-insensitive. So Content-Encoding: Gzip / GZIP are valid and should decompress the same as gzip.

Real-world sources of non-lowercase values:

  • Some language HTTP clients / libraries canonicalize header names but leave coding tokens as the caller typed them.
  • Reverse proxies and gateways that rewrite or re-emit Content-Encoding without lowercasing.
  • Integration tests and generated clients often send Gzip.

The TrimSpace part is minor (malformed-but-common whitespace); the important bit is EqualFold for case-insensitivity required by the HTTP semantics. Happy to drop the trim if you only want the case fix.

Drop TrimSpace so the change is strictly RFC 9110 case-insensitivity
for content codings, per review discussion.
@sonnemusk

Copy link
Copy Markdown
Author

Follow-up: I dropped the TrimSpace part so this PR is only case-insensitivity via strings.EqualFold, matching RFC 9110 content-coding rules.

Concrete cases this fixes:

  • clients/libraries that send Content-Encoding: Gzip or GZIP (valid per HTTP; previously skipped decompression and left a gzip body for handlers to read as plain text)
  • e.g. some Java/OkHttp and generated clients historically emit mixed-case coding tokens

Happy to adjust further if you want a different approach.

@sonnemusk
sonnemusk force-pushed the fix/decompress-case-insensitive-encoding branch from c4b0fdd to c7d2c83 Compare July 22, 2026 07:56
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