Skip to content

Commit

Permalink
Merge pull request tornadoweb#1468 from aebrahim/gzip_header
Browse files Browse the repository at this point in the history
fix Content-Type for gzip in StaticFileHandler
  • Loading branch information
bdarnell committed Jul 31, 2015
2 parents 8943ee0 + 1ea6e91 commit d370183
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
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 d370183

Please sign in to comment.