From a686096c262b706b1367abc87436ae9dc47dbe2a Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sun, 26 Sep 2010 15:10:41 +0000 Subject: [PATCH] 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 --- django/views/static.py | 5 ++++- tests/regressiontests/views/media/file.txt.gz | Bin 0 -> 51 bytes tests/regressiontests/views/tests/static.py | 8 +++++--- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 tests/regressiontests/views/media/file.txt.gz diff --git a/django/views/static.py b/django/views/static.py index d9117f282e..a2990f1d1d 100644 --- a/django/views/static.py +++ b/django/views/static.py @@ -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 = """ diff --git a/tests/regressiontests/views/media/file.txt.gz b/tests/regressiontests/views/media/file.txt.gz new file mode 100644 index 0000000000000000000000000000000000000000..0ee7d18311e16da167d42c12dab43af90b2e8abd GIT binary patch literal 51 zcmb2|=HO6B*yP2)oR*oBs#j7`!ccrB*ki578Ql|}p(lKOPx^#-oIc}um6;(Sci%EG H1_lNI)m##& literal 0 HcmV?d00001 diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py index d7e87d19d2..fdd31b4660 100644 --- a/tests/regressiontests/views/tests/static.py +++ b/tests/regressiontests/views/tests/static.py @@ -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')