Skip to content

gzip middleware - #42

Merged
yogthos merged 6 commits into
masterfrom
gzip
Dec 3, 2025
Merged

gzip middleware#42
yogthos merged 6 commits into
masterfrom
gzip

Conversation

@yogthos

@yogthos yogthos commented Dec 3, 2025

Copy link
Copy Markdown
Member

No description provided.

@rutchkiwi

Copy link
Copy Markdown
Contributor

that was fast! Thanks a lot for this.

I had a look through it now. I'm having quite a hard time understanding things once inside the undertow java world. Things I noticed:

  1. gzip-deflate-level - It looks to me like it doesn't actually set the compression level. It looks to me like it's setting a "priority", which as I understand it is something to do with picking which compression to use, in case both deflate and gzip is used - like in the headers gzip;q=0.8, deflate;q=0.9. Which, if I'm correct, means that it should likely not be exposed to the users of the library. (source: https://stackoverflow.com/questions/28295752/compressing-undertow-server-responses)
  2. This compresses all responses. It'd be nice to skip small ones? I believe the addEncodingHandler has another arity that lets you pass in a predicate for when to apply the handler. How that predicate works I'm not sure. AI thinks it should be like (Predicates/parse "max-content-size[1]") but haven't verified that.
  3. In a similar vein, it'd be nice to skip gzipping for jpegs etc, that are already compressed. But on the other hand I don't think any of the ring middlewares for gzipping handles this either, so could definitely be skipped. I don't know how you'd implement such a check.

@rutchkiwi

Copy link
Copy Markdown
Contributor

Digging a bit more I think a predicate like this should do the trick:

(Predicates/parse "max-content-size(1) and regex(pattern='text/.*|application/(json|javascript|xml|.*\\\\+xml)|image/svg.*', value=%{o,Content-Type})")
  • 1KB limit (IMO not worth having this user configurable)
  • Compressed: application/json, text/html, text/css, text/csv, text/xml, application/xml, application/*+xml, application/javascript, image/svg+xml
  • Not compressed: image/jpeg, image/png, video/*, application/pdf etc

@yogthos

yogthos commented Dec 3, 2025

Copy link
Copy Markdown
Member Author

Yeah, looks like I misread the gzip-deflate-level bit, just pushed up an update that should do the trick and handle the predicates.

@rutchkiwi rutchkiwi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this with my actual webserver and everything works great as far as I can tell. In fact, it works greater than the ring gzip middlewares I've seen around in that images are skipped.
I only noticed a leftover doc line from previous commit.

Thanks a lot for this, you are the best @yogthos :)

Comment thread README.md Outdated
@yogthos
yogthos merged commit 895ee6e into master Dec 3, 2025
1 check passed
@yogthos

yogthos commented Dec 3, 2025

Copy link
Copy Markdown
Member Author

Awesome, glad to hear everything works, and great find that the support natively exists in Undertow. Just pushed out 1.4.3 with the update. 🎉

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