From: Ali Ebrahim Date: Thu, 30 Jul 2015 20:57:27 +0000 (-0700) Subject: Content-Type for compressed StaticFileHandler file X-Git-Tag: v4.3.0b1~68^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ea6e91bd9d21972247af53fa2361597573859c8;p=thirdparty%2Ftornado.git 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. --- 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"""