NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.93k stars 1.69k forks source link

Pre-encoded text responses are encoded again (unnecessarily) #463

Open mike10004 opened 6 years ago

mike10004 commented 6 years ago

The server automatically gzips outgoing response data streams if the response content type is text and the client will accept it. If the response data is already encoded, this results in the server encoding the data again.

This behavior should be avoided because it is unlikely that there would be any benefit to gzipping a data stream already compressed with gzip or any other compression method.

Expected behavior: a response containing a Content-Encoding header with a value other than identity should be served without gzipping the content.

Actual behavior: The response content is always gzipped if the Content-Type is text/* (and the gzipUsage setting is DEFAULT or ALWAYS).

I encountered this in org.nanohttpd:nanohttpd:2.3.1, but the issue exists in the latest version too, as far as I can tell from examining the current code at Response.java.

The resolution I recommend is to modify the useGzipWhenAccepted method to return false if the response contains a Content-Encoding header whose value is anything other than identity, regardless of what the gzipUsage setting is. I'd be glad to submit a pull request if this resolution is acceptable.

LordFokas commented 6 years ago

If you want to contribute this yourself, I'll let you know when to start. Currently we're in the middle of a very long freeze and I might find myself having to code most of 3.0.0 again and from scratch.

mike10004 commented 6 years ago

Sure, let me know whenever.