diff options
| author | Michael Merickel <mmerickel@users.noreply.github.com> | 2016-11-15 19:46:52 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-11-15 19:46:52 -0600 |
| commit | f5769191f4cd9e88cc8b4ce3b4a9f428204a26d6 (patch) | |
| tree | 1848dc9258f2e2fec888b0bad867f1836ed45787 | |
| parent | d1e8b222e11926132a72e2468707950206cc87dd (diff) | |
| parent | 734f241a961b6b1e4b2a43c0c4b5a4d972b65b80 (diff) | |
| download | pyramid-f5769191f4cd9e88cc8b4ce3b4a9f428204a26d6.tar.gz pyramid-f5769191f4cd9e88cc8b4ce3b4a9f428204a26d6.tar.bz2 pyramid-f5769191f4cd9e88cc8b4ce3b4a9f428204a26d6.zip | |
Merge pull request #2810 from davisagli/fix-static-content-encoding
Avoid setting Content-Encoding header for static view responses.
| -rw-r--r-- | CHANGES.txt | 6 | ||||
| -rw-r--r-- | pyramid/response.py | 25 | ||||
| -rw-r--r-- | pyramid/static.py | 12 | ||||
| -rw-r--r-- | pyramid/tests/test_static.py | 4 |
4 files changed, 33 insertions, 14 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 434557f89..1d69471f1 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -97,6 +97,12 @@ Bug Fixes from previous orders have executed. See https://github.com/Pylons/pyramid/pull/2757 +- Fix static view to avoid setting the ``Content-Encoding`` response header + to an encoding guessed using Python's ``mimetypes`` module. + This was causing clients to decode the content of gzipped files + when downloading them. + See https://github.com/Pylons/pyramid/pull/2810 + Deprecations ------------ diff --git a/pyramid/response.py b/pyramid/response.py index 892e5dfff..1d9daae7d 100644 --- a/pyramid/response.py +++ b/pyramid/response.py @@ -54,16 +54,7 @@ class FileResponse(Response): def __init__(self, path, request=None, cache_max_age=None, content_type=None, content_encoding=None): if content_type is None: - content_type, content_encoding = mimetypes.guess_type( - path, - strict=False - ) - if content_type is None: - content_type = 'application/octet-stream' - # str-ifying content_type is a workaround for a bug in Python 2.7.7 - # on Windows where mimetypes.guess_type returns unicode for the - # content_type. - content_type = str(content_type) + content_type, content_encoding = _guess_type(path) super(FileResponse, self).__init__( conditional_response=True, content_type=content_type, @@ -180,3 +171,17 @@ def _get_response_factory(registry): ) return response_factory + + +def _guess_type(path): + content_type, content_encoding = mimetypes.guess_type( + path, + strict=False + ) + if content_type is None: + content_type = 'application/octet-stream' + # str-ifying content_type is a workaround for a bug in Python 2.7.7 + # on Windows where mimetypes.guess_type returns unicode for the + # content_type. + content_type = str(content_type) + return content_type, content_encoding diff --git a/pyramid/static.py b/pyramid/static.py index 0965be95c..31e500e70 100644 --- a/pyramid/static.py +++ b/pyramid/static.py @@ -32,7 +32,12 @@ from pyramid.httpexceptions import ( ) from pyramid.path import caller_package -from pyramid.response import FileResponse + +from pyramid.response import ( + _guess_type, + FileResponse, +) + from pyramid.traversal import traversal_path_info slash = text_('/') @@ -134,7 +139,10 @@ class static_view(object): if not exists(filepath): raise HTTPNotFound(request.url) - return FileResponse(filepath, request, self.cache_max_age) + content_type, content_encoding = _guess_type(filepath) + return FileResponse( + filepath, request, self.cache_max_age, + content_type, content_encoding=None) def add_slash_redirect(self, request): url = request.path_url + '/' diff --git a/pyramid/tests/test_static.py b/pyramid/tests/test_static.py index 2ca86bc44..2b200d72b 100644 --- a/pyramid/tests/test_static.py +++ b/pyramid/tests/test_static.py @@ -186,14 +186,14 @@ class Test_static_view_use_subpath_False(unittest.TestCase): from pyramid.httpexceptions import HTTPNotFound self.assertRaises(HTTPNotFound, inst, context, request) - def test_resource_with_content_encoding(self): + def test_gz_resource_no_content_encoding(self): inst = self._makeOne('pyramid.tests:fixtures/static') request = self._makeRequest({'PATH_INFO':'/arcs.svg.tgz'}) context = DummyContext() response = inst(context, request) self.assertEqual(response.status, '200 OK') self.assertEqual(response.content_type, 'application/x-tar') - self.assertEqual(response.content_encoding, 'gzip') + self.assertEqual(response.content_encoding, None) response.app_iter.close() def test_resource_no_content_encoding(self): |
