Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.2.X] Fixed #7535 -- Correctly set Content-Encoding header in stati…

…c files serving view. Thanks for the report and patch, Kevin Hunter.

Backport from trunk (r13868).

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13869 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c37add7c473e8893af17a39745525364bf79f3dc 1 parent 5f4fe3c
Jannis Leidel jezdez authored
5 django/views/static.py
View
@@ -56,7 +56,8 @@ def serve(request, path, document_root=None, show_indexes=False):
raise Http404('"%s" does not exist' % fullpath)
# Respect the If-Modified-Since header.
statobj = os.stat(fullpath)
- mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
+ mimetype, encoding = mimetypes.guess_type(fullpath)
+ mimetype = mimetype or 'application/octet-stream'
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
return HttpResponseNotModified(mimetype=mimetype)
@@ -64,6 +65,8 @@ def serve(request, path, document_root=None, show_indexes=False):
response = HttpResponse(contents, mimetype=mimetype)
response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
response["Content-Length"] = len(contents)
+ if encoding:
+ response["Content-Encoding"] = encoding
return response
DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
BIN  tests/regressiontests/views/media/file.txt.gz
View
Binary file not shown
8 tests/regressiontests/views/tests/static.py
View
@@ -1,3 +1,4 @@
+import mimetypes
from os import path
from django.test import TestCase
@@ -8,12 +9,13 @@ class StaticTests(TestCase):
def test_serve(self):
"The static view can serve static media"
- media_files = ['file.txt',]
+ media_files = ['file.txt', 'file.txt.gz']
for filename in media_files:
response = self.client.get('/views/site_media/%s' % filename)
- file = open(path.join(media_dir, filename))
- self.assertEquals(file.read(), response.content)
+ file_path = path.join(media_dir, filename)
+ self.assertEquals(open(file_path).read(), response.content)
self.assertEquals(len(response.content), int(response['Content-Length']))
+ self.assertEquals(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None))
def test_unknown_mime_type(self):
response = self.client.get('/views/site_media/file.unknown')
Please sign in to comment.
Something went wrong with that request. Please try again.