Skip to content

Commit

Permalink
Content-Type for compressed StaticFileHandler file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aebrahim committed Jul 30, 2015
1 parent 3a881d9 commit 1ea6e91
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,19 @@ def get_content_type(self):
.. 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"""
Expand Down

0 comments on commit 1ea6e91

Please sign in to comment.