Skip to content

Commit

Permalink
[1.2.X] Fixed #7535 -- Correctly set Content-Encoding header in stati…
Browse files Browse the repository at this point in the history
…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
jezdez committed Sep 26, 2010
1 parent 5f4fe3c commit c37add7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion django/views/static.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ def serve(request, path, document_root=None, show_indexes=False):
raise Http404('"%s" does not exist' % fullpath) raise Http404('"%s" does not exist' % fullpath)
# Respect the If-Modified-Since header. # Respect the If-Modified-Since header.
statobj = os.stat(fullpath) 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'), if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]): statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
return HttpResponseNotModified(mimetype=mimetype) return HttpResponseNotModified(mimetype=mimetype)
contents = open(fullpath, 'rb').read() contents = open(fullpath, 'rb').read()
response = HttpResponse(contents, mimetype=mimetype) response = HttpResponse(contents, mimetype=mimetype)
response["Last-Modified"] = http_date(statobj[stat.ST_MTIME]) response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
response["Content-Length"] = len(contents) response["Content-Length"] = len(contents)
if encoding:
response["Content-Encoding"] = encoding
return response return response


DEFAULT_DIRECTORY_INDEX_TEMPLATE = """ DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
Expand Down
Binary file added tests/regressiontests/views/media/file.txt.gz
Binary file not shown.
8 changes: 5 additions & 3 deletions tests/regressiontests/views/tests/static.py
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
import mimetypes
from os import path from os import path


from django.test import TestCase from django.test import TestCase
Expand All @@ -8,12 +9,13 @@ class StaticTests(TestCase):


def test_serve(self): def test_serve(self):
"The static view can serve static media" "The static view can serve static media"
media_files = ['file.txt',] media_files = ['file.txt', 'file.txt.gz']
for filename in media_files: for filename in media_files:
response = self.client.get('/views/site_media/%s' % filename) response = self.client.get('/views/site_media/%s' % filename)
file = open(path.join(media_dir, filename)) file_path = path.join(media_dir, filename)
self.assertEquals(file.read(), response.content) self.assertEquals(open(file_path).read(), response.content)
self.assertEquals(len(response.content), int(response['Content-Length'])) 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): def test_unknown_mime_type(self):
response = self.client.get('/views/site_media/file.unknown') response = self.client.get('/views/site_media/file.unknown')
Expand Down

0 comments on commit c37add7

Please sign in to comment.