Fixed #7535 -- Correctly set Content-Encoding header in static files serving view. Thanks for the report and patch, Kevin Hunter.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13868 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-09-26 15:10:41 +00:00
parent e2ff11c6a3
commit a686096c26
3 changed files with 9 additions and 4 deletions

View File

@ -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 = """

Binary file not shown.

View File

@ -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')