Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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 authored September 26, 2010
5  django/views/static.py
@@ -56,7 +56,8 @@ def serve(request, path, document_root=None, show_indexes=False):
56 56
         raise Http404('"%s" does not exist' % fullpath)
57 57
     # Respect the If-Modified-Since header.
58 58
     statobj = os.stat(fullpath)
59  
-    mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
  59
+    mimetype, encoding = mimetypes.guess_type(fullpath)
  60
+    mimetype = mimetype or 'application/octet-stream'
60 61
     if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
61 62
                               statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
62 63
         return HttpResponseNotModified(mimetype=mimetype)
@@ -64,6 +65,8 @@ def serve(request, path, document_root=None, show_indexes=False):
64 65
     response = HttpResponse(contents, mimetype=mimetype)
65 66
     response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
66 67
     response["Content-Length"] = len(contents)
  68
+    if encoding:
  69
+        response["Content-Encoding"] = encoding
67 70
     return response
68 71
 
69 72
 DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
BIN  tests/regressiontests/views/media/file.txt.gz
Binary file not shown
8  tests/regressiontests/views/tests/static.py
... ...
@@ -1,3 +1,4 @@
  1
+import mimetypes
1 2
 from os import path
2 3
 
3 4
 from django.test import TestCase
@@ -8,12 +9,13 @@ class StaticTests(TestCase):
8 9
 
9 10
     def test_serve(self):
10 11
         "The static view can serve static media"
11  
-        media_files = ['file.txt',]
  12
+        media_files = ['file.txt', 'file.txt.gz']
12 13
         for filename in media_files:
13 14
             response = self.client.get('/views/site_media/%s' % filename)
14  
-            file = open(path.join(media_dir, filename))
15  
-            self.assertEquals(file.read(), response.content)
  15
+            file_path = path.join(media_dir, filename)
  16
+            self.assertEquals(open(file_path).read(), response.content)
16 17
             self.assertEquals(len(response.content), int(response['Content-Length']))
  18
+            self.assertEquals(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None))
17 19
 
18 20
     def test_unknown_mime_type(self):
19 21
         response = self.client.get('/views/site_media/file.unknown')

0 notes on commit c37add7

Please sign in to comment.
Something went wrong with that request. Please try again.