Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15531 -- Partially reverted [15701] due to compatibility issue…

…s with middlewares that modify content of responses. Thanks for the report, schinckel. Refs #15281.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15703 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6b95aa6fb549b3834b7feefc6fbe92f8a50da411 1 parent ec19322
Jannis Leidel authored March 02, 2011
25  django/core/servers/basehttp.py
@@ -19,7 +19,6 @@
19 19
 from django.utils.http import http_date
20 20
 from django.utils._os import safe_join
21 21
 from django.views import static
22  
-from django.views.static import FileWrapper # for backwards compatibility, #15281
23 22
 
24 23
 from django.contrib.staticfiles import handlers
25 24
 
@@ -33,6 +32,30 @@
33 32
 class WSGIServerException(Exception):
34 33
     pass
35 34
 
  35
+class FileWrapper(object):
  36
+    """Wrapper to convert file-like objects to iterables"""
  37
+
  38
+    def __init__(self, filelike, blksize=8192):
  39
+        self.filelike = filelike
  40
+        self.blksize = blksize
  41
+        if hasattr(filelike,'close'):
  42
+            self.close = filelike.close
  43
+
  44
+    def __getitem__(self,key):
  45
+        data = self.filelike.read(self.blksize)
  46
+        if data:
  47
+            return data
  48
+        raise IndexError
  49
+
  50
+    def __iter__(self):
  51
+        return self
  52
+
  53
+    def next(self):
  54
+        data = self.filelike.read(self.blksize)
  55
+        if data:
  56
+            return data
  57
+        raise StopIteration
  58
+
36 59
 # Regular expression that matches `special' characters in parameters, the
37 60
 # existence of which force quoting of the parameter value.
38 61
 tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')
33  django/views/static.py
@@ -9,39 +9,10 @@
9 9
 import re
10 10
 import urllib
11 11
 
12  
-from django.template import loader
13 12
 from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified
14  
-
15  
-from django.template import Template, Context, TemplateDoesNotExist
  13
+from django.template import loader, Template, Context, TemplateDoesNotExist
16 14
 from django.utils.http import http_date, parse_http_date
17 15
 
18  
-
19  
-class FileWrapper(object):
20  
-    """
21  
-    Wrapper to convert file-like objects to iterables
22  
-    """
23  
-    def __init__(self, filelike, blksize=8192):
24  
-        self.filelike = filelike
25  
-        self.blksize = blksize
26  
-        if hasattr(filelike,'close'):
27  
-            self.close = filelike.close
28  
-
29  
-    def __getitem__(self,key):
30  
-        data = self.filelike.read(self.blksize)
31  
-        if data:
32  
-            return data
33  
-        raise IndexError
34  
-
35  
-    def __iter__(self):
36  
-        return self
37  
-
38  
-    def next(self):
39  
-        data = self.filelike.read(self.blksize)
40  
-        if data:
41  
-            return data
42  
-        raise StopIteration
43  
-
44  
-
45 16
 def serve(request, path, document_root=None, show_indexes=False):
46 17
     """
47 18
     Serve static files below a given point in the directory structure.
@@ -85,7 +56,7 @@ def serve(request, path, document_root=None, show_indexes=False):
85 56
     if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
86 57
                               statobj.st_mtime, statobj.st_size):
87 58
         return HttpResponseNotModified(mimetype=mimetype)
88  
-    response = HttpResponse(FileWrapper(open(fullpath, 'rb')), mimetype=mimetype)
  59
+    response = HttpResponse(open(fullpath, 'rb').read(), mimetype=mimetype)
89 60
     response["Last-Modified"] = http_date(statobj.st_mtime)
90 61
     response["Content-Length"] = statobj.st_size
91 62
     if encoding:
24  tests/regressiontests/views/tests/static.py
@@ -26,18 +26,10 @@ def test_serve(self):
26 26
         for filename in media_files:
27 27
             response = self.client.get('/views/%s/%s' % (self.prefix, filename))
28 28
             file_path = path.join(media_dir, filename)
29  
-            content = str(response.content)
30  
-            self.assertEquals(open(file_path).read(), content)
31  
-            self.assertEquals(len(content), int(response['Content-Length']))
  29
+            self.assertEquals(open(file_path).read(), response.content)
  30
+            self.assertEquals(len(response.content), int(response['Content-Length']))
32 31
             self.assertEquals(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None))
33 32
 
34  
-    def test_serve_does_not_buffer_files(self):
35  
-        "The static view uses an iterator - see #15281"
36  
-        response = self.client.get('/views/%s/file.txt' % self.prefix)
37  
-        # response objects can't be used as file-like objects when they are
38  
-        # initialized with an iterator
39  
-        self.assertRaises(Exception, response.write, 'more text')
40  
-
41 33
     def test_unknown_mime_type(self):
42 34
         response = self.client.get('/views/%s/file.unknown' % self.prefix)
43 35
         self.assertEquals('application/octet-stream', response['Content-Type'])
@@ -76,9 +68,9 @@ def test_invalid_if_modified_since(self):
76 68
         response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
77 69
                                    HTTP_IF_MODIFIED_SINCE=invalid_date)
78 70
         file = open(path.join(media_dir, file_name))
79  
-        content = str(response.content)
80  
-        self.assertEquals(file.read(), content)
81  
-        self.assertEquals(len(content), int(response['Content-Length']))
  71
+        self.assertEquals(file.read(), response.content)
  72
+        self.assertEquals(len(response.content),
  73
+                          int(response['Content-Length']))
82 74
 
83 75
     def test_invalid_if_modified_since2(self):
84 76
         """Handle even more bogus If-Modified-Since values gracefully
@@ -90,10 +82,10 @@ def test_invalid_if_modified_since2(self):
90 82
         invalid_date = ': 1291108438, Wed, 20 Oct 2010 14:05:00 GMT'
91 83
         response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
92 84
                                    HTTP_IF_MODIFIED_SINCE=invalid_date)
93  
-        content = str(response.content)
94 85
         file = open(path.join(media_dir, file_name))
95  
-        self.assertEquals(file.read(), content)
96  
-        self.assertEquals(len(content), int(response['Content-Length']))
  86
+        self.assertEquals(file.read(), response.content)
  87
+        self.assertEquals(len(response.content),
  88
+                          int(response['Content-Length']))
97 89
 
98 90
 
99 91
 class StaticHelperTest(StaticTests):

0 notes on commit 6b95aa6

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