Open jmlrt opened 2 years ago
The file is not decompressed by bottle, but if the file-name looks stream-compressed (e.g. foo.txt.gz
), bottle sets the Content-Encoding
header in addition to Content-Type
and most HTTP clients will decompress the body automatically. Not sure if that was a good idea in hindsight. HTTP is a mess sometimes. If you want to serve gzip files as is, you must explicitly set the mimetype parameter to bypass mime-guessing.
Hi @defnull, thanks for the quick answer and explanation.
bottle sets the Content-Encoding header in addition to Content-Type and most HTTP clients will decompress the body automatically.
Indeed requests
is uncompressing the file while wget isn't which made me confused a lot 😄.
If you want to serve gzip files as is, you must explicitly set the mimetype parameter to bypass mime-guessing.
To be able to serve gzip and non-gzip files in the same route without Content-Encoding
, detecting files mimetype and passing it to static_file
seems fine:
from bottle import route, run, static_file
import mimetypes
@route("/static/<filename:path>")
def send_static(filename):
mimetype = mimetypes.guess_type(filepath)
return static_file(filename, root=".", mimetype=mimetype)
Do you think it could make sense to add a new param to static_file
to be able to disable setting the Content-Encoding
?
Hmm the more I think about it, the more I'd say this is a bug in Bottle, not just a misguided feature. Not sure how to fix this in a backwards compatible way, though. That's something for the next release.
The idea would be to disable the flawed Content-Encoding
logic as it currently exists in Bottle, and add a gzip
parameter. If gzip == "static"
is true, bottle would automatically look for and send a *.gz
version of the requested file if it exists and the browser supports it. Otherwise, it would just serve the normal file as usual. This is what nginx does with the gzip_static
module.
When using
static_file
withoutmimetype
to serve a file compressed with gzip, bottle seems to uncompress it on the fly.How to reproduce it:
create a gzipped text file:
serve the file with and without
mimetype
:in a second shell, download the file using both endpoint and check its type: