]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Content-Type for compressed StaticFileHandler file 1468/head
authorAli Ebrahim <aebrahim@ucsd.edu>
Thu, 30 Jul 2015 20:57:27 +0000 (13:57 -0700)
committerAli Ebrahim <aebrahim@ucsd.edu>
Thu, 30 Jul 2015 20:57:27 +0000 (13:57 -0700)
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

index f237d78f027dc1e12c3ee1fc8b84f0800ddfb659..b2fa6086e6050281507b45f63e4bfe091ca34f21 100644 (file)
@@ -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"""