Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup Jetty GzipHandler programmatically

I'm playing with Jetty GzipHandler and it seems to work rather strangely: It only compresses already compressed files.

My whole setup is

GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setHandler(myHandler);
server.setHandler(gzipHandler);

The browser (Chromium) always sends a header containing

Accept-Encoding:gzip,deflate,sdch

so according to the documentation

GZIP Handler This handler will gzip the content of a response if:

  • The filter is mapped to a matching path
  • The response status code is >=200 and <300
  • The content length is unknown or more than the minGzipSize initParameter or the minGzipSize is 0 (default)
  • The content-type is in the comma separated list of mimeTypes set in the mimeTypes initParameter or if no mimeTypes are defined the content-type is not "application/gzip"
  • No content-encoding is specified by the resource

it should work for both. I'm only unsure about the path part, but without having specified any, I'd expect it to work for both or neither.

I used window.location.reload(true) to force reload. The headers are rather long so, I'm linking them: css and png.

I've tried to set some properties, but without any success. Should I ever find jetty-servlets-9.1.3.v20140225-sources.jar, I'll debug it. The question is: Why does GzipHandler decide to compress only compressed files? It's perfectly deterministic: jpg and png get compressed (no matter how small) and no other files do.

Update

Via setMimeTypes I could exclude the images. I debugged it and I'm still having no clue, why other static resources never get compressed. I double checked that myHandler treats them all uniformly (they all get served directly from a precomputed Map<String, byte[]>).

like image 392
maaartinus Avatar asked Jul 02 '14 01:07

maaartinus


1 Answers

Here is how I configured the GzipHandler in Jetty 9.3.7:

GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setIncludedMimeTypes("text/html", "text/plain", "text/xml", 
            "text/css", "application/javascript", "text/javascript");
gzipHandler.setHandler(myHandler);
handlerList.addHandler(gzipHandler);

In this case, myHandler was an instance of ResourceHandler. By default, the Gzip Handler only gzips responses to GET requests with a response code in the 200 range.

like image 125
Max Avatar answered Oct 14 '22 06:10

Max