Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Content-Type for gzip in StaticFileHandler #1468

Merged
merged 2 commits into from Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions MANIFEST.in
Expand Up @@ -8,6 +8,9 @@ include tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo
include tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po
include tornado/test/options_test.cfg
include tornado/test/static/robots.txt
include tornado/test/static/sample.xml
include tornado/test/static/sample.xml.gz
include tornado/test/static/sample.xml.bz2
include tornado/test/static/dir/index.html
include tornado/test/static_foo.txt
include tornado/test/templates/utf8.html
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Expand Up @@ -146,6 +146,9 @@ def build_extension(self, ext):
"gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po",
"options_test.cfg",
"static/robots.txt",
"static/sample.xml",
"static/sample.xml.gz",
"static/sample.xml.bz2",
"static/dir/index.html",
"static_foo.txt",
"templates/utf8.html",
Expand Down
23 changes: 23 additions & 0 deletions tornado/test/static/sample.xml
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
Binary file added tornado/test/static/sample.xml.bz2
Binary file not shown.
Binary file added tornado/test/static/sample.xml.gz
Binary file not shown.
13 changes: 13 additions & 0 deletions tornado/test/web_test.py
Expand Up @@ -978,6 +978,19 @@ def test_static_files(self):

response = self.fetch('/static/robots.txt')
self.assertTrue(b"Disallow: /" in response.body)
self.assertEqual(response.headers.get("Content-Type"), "text/plain")

def test_static_compressed_files(self):
response = self.fetch("/static/sample.xml.gz")
self.assertEqual(response.headers.get("Content-Type"),
"application/gzip")
response = self.fetch("/static/sample.xml.bz2")
self.assertEqual(response.headers.get("Content-Type"),
"application/octet-stream")
# make sure the uncompressed file still has the correct type
response = self.fetch("/static/sample.xml")
self.assertTrue(response.headers.get("Content-Type")
in set(("text/xml", "application/xml")))

def test_static_url(self):
response = self.fetch("/static_url/robots.txt")
Expand Down
14 changes: 13 additions & 1 deletion tornado/web.py
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