Skip to content

feat(gzip): add option to combine default and custom compression decisions#135

Open
kekexiaoai wants to merge 1 commit intogin-contrib:masterfrom
kekexiaoai:master
Open

feat(gzip): add option to combine default and custom compression decisions#135
kekexiaoai wants to merge 1 commit intogin-contrib:masterfrom
kekexiaoai:master

Conversation

@kekexiaoai
Copy link

@kekexiaoai kekexiaoai commented Dec 10, 2025

…sions

This patch introduces a new option WithCombineDefaultAndCustom to the gin-contrib/gzip middleware, allowing developers to control how customShouldCompressFn interacts with the default shouldCompress logic.

Previously, when a custom compression decision function was provided, the default compression rules (Accept-Encoding, excluded paths, excluded extensions, Upgrade connections, etc.) were completely bypassed. This made it impossible to extend the default behavior without fully re-implementing it externally.

Changes introduced:

  • Add combineDefaultAndCustom flag to the gzipHandler config.
  • Update compression decision logic:
    • When combineDefaultAndCustom = true, compression is enabled only if: customShouldCompressFn(c) && shouldCompress(c.Request)
    • When combineDefaultAndCustom = false (default behavior), customShouldCompressFn fully overrides default logic.
  • Add new option: gzip.WithCombineDefaultAndCustom()
  • Preserve full backward compatibility.
  • Update handler logic to use the new combined decision flow.

This enhancement allows developers to:

  • Extend default compression behavior safely.
  • Add additional custom filters without losing built-in protections.
  • Support advanced use cases such as conditionally enabling gzip only on specific cluster nodes (e.g. leader-only compression) while still respecting default checks.

@kekexiaoai kekexiaoai marked this pull request as draft December 10, 2025 02:51
@kekexiaoai kekexiaoai marked this pull request as ready for review December 10, 2025 02:54
…sions

This patch introduces a new option `WithCombineDefaultAndCustom` to the
gin-contrib/gzip middleware, allowing developers to control how
`customShouldCompressFn` interacts with the default `shouldCompress` logic.

Previously, when a custom compression decision function was provided,
the default compression rules (Accept-Encoding, excluded paths,
excluded extensions, Upgrade connections, etc.) were completely bypassed.
This made it impossible to extend the default behavior without fully
re-implementing it externally.

Changes introduced:

- Add `combineDefaultAndCustom` flag to the gzipHandler config.
- Update compression decision logic:
    - When `combineDefaultAndCustom = true`, compression is enabled only if:
          customShouldCompressFn(c) && shouldCompress(c.Request)
    - When `combineDefaultAndCustom = false` (default behavior),
      customShouldCompressFn fully overrides default logic.
- Add new option:
      `gzip.WithCombineDefaultAndCustom()`
- Preserve full backward compatibility.
- Update handler logic to use the new combined decision flow.

This enhancement allows developers to:
- Extend default compression behavior safely.
- Add additional custom filters without losing built-in protections.
- Support advanced use cases such as conditionally enabling gzip only
  on specific cluster nodes (e.g. leader-only compression) while still
  respecting default checks.
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.

1 participant