From 1ea6e91bd9d21972247af53fa2361597573859c8 Mon Sep 17 00:00:00 2001 From: Ali Ebrahim Date: Thu, 30 Jul 2015 13:57:27 -0700 Subject: [PATCH] Content-Type for compressed StaticFileHandler file The python mimetypes module used by StaticFileHandler will recognize compression (gzip, bz2, etc.) as the file encoding, and will give the mime type for the uncompressed file. This commit will fix this behavior, so a gzip file will end up as application/gzip. Additionally, unknown file types (or known file types compressed with anything other than gzip) are served as application/octet-stream. --- tornado/web.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tornado/web.py b/tornado/web.py index f237d78f0..b2fa6086e 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -2495,7 +2495,19 @@ class StaticFileHandler(RequestHandler): .. versionadded:: 3.1 """ mime_type, encoding = mimetypes.guess_type(self.absolute_path) - return mime_type + # per RFC 6713, use the appropriate type for a gzip compressed file + if encoding == "gzip": + return "application/gzip" + # As of 2015-07-21 there is no bzip2 encoding defined at + # http://www.iana.org/assignments/media-types/media-types.xhtml + # So for that (and any other encoding), use octet-stream. + elif encoding is not None: + return "application/octet-stream" + elif mime_type is not None: + return mime_type + # if mime_type not detected, use application/octet-stream + else: + return "application/octet-stream" def set_extra_headers(self, path): """For subclass to add extra headers to the response""" -- 2.47.2