whitenoise icon indicating copy to clipboard operation
whitenoise copied to clipboard

Improve Accept-Encoding parsing

Open adamchainz opened this issue 3 years ago • 3 comments

Currently invalid values like gzip brotli spam still pass, and valid values like gzip;q=0 ("don't send me gzip") are intepreted as "send me gzip". Also the "no encoding" case relies on matching r"" within the string (?).

The header should be parsed correctly. Logic can be borrowed from Django's request.accepts().

adamchainz avatar Feb 04 '22 00:02 adamchainz

This is a very valid point. Also along the same lines, if multiple encodings are available (e.g. gzip and brotli), whitenoise does not choose sensibly between them. Chrome for example sends Accept-Encoding: gzip, deflate, br which means all of those encodings are equally acceptable, and yet whitenoise treats it as "gzip by preference" (because it's earlier in the list), so despite the good work put in to make it support brotli, in practice the brotli versions will never be used - which is a shame given brotli is up to one hundred times better than gzip! Given whitenoise has the file sizes immediately available to it, it would seem only sensible for it to choose the smallest file from amongst the equally-acceptable encodings.

jribbens avatar Oct 25 '22 17:10 jribbens

whitenoise treats it as "gzip by preference" (because it's earlier in the list)

I'm pretty sure that's not true. It prefers the smallest response size: https://github.com/evansd/whitenoise/blob/main/src/whitenoise/responders.py#L194

evansd avatar Oct 25 '22 17:10 evansd

Hmm, that's not the behaviour I was seeing yesterday! Perhaps nginx is interfering somehow...

jribbens avatar Oct 26 '22 09:10 jribbens