Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5596 -- Changed the static view for the development server so …

…that Django doesn't crash if somebody tries to serve a 200MB file. Patch from eibaan.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6939 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b279b75c876e819034e6c56f3bf3b0c4b9843636 1 parent 24cb41b
Malcolm Tredinnick authored
16  django/core/servers/basehttp.py
@@ -398,8 +398,20 @@ def write(self, data):
398 398
             self.bytes_sent += len(data)
399 399
 
400 400
         # XXX check Content-Length and truncate if too many bytes written?
401  
-        self._write(data)
402  
-        self._flush()
  401
+
  402
+        # If data is too large, socket will choke, so write chunks no larger
  403
+        # than 32MB at a time.
  404
+        length = len(data)
  405
+        if length > 33554432:
  406
+            offset = 0
  407
+            while offset < length:
  408
+                chunk_size = min(33554432, length)
  409
+                self._write(data[offset:offset+chunk_size])
  410
+                self._flush()
  411
+                offset += chunk_size
  412
+        else:
  413
+            self._write(data)
  414
+            self._flush()
403 415
 
404 416
     def sendfile(self):
405 417
         """Platform-specific file transmission
3  django/views/static.py
@@ -59,10 +59,11 @@ def serve(request, path, document_root=None, show_indexes=False):
59 59
     if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
60 60
                               statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
61 61
         return HttpResponseNotModified()
62  
-    mimetype = mimetypes.guess_type(fullpath)[0]
  62
+    mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
63 63
     contents = open(fullpath, 'rb').read()
64 64
     response = HttpResponse(contents, mimetype=mimetype)
65 65
     response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
  66
+    response["Content-Length"] = len(contents)
66 67
     return response
67 68
 
68 69
 DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
1  tests/regressiontests/views/media/file.unknown
... ...
@@ -0,0 +1 @@
  1
+An unknown file extension.
8  tests/regressiontests/views/tests/static.py
@@ -13,11 +13,15 @@ def test_serve(self):
13 13
             response = self.client.get('/views/site_media/%s' % filename)
14 14
             file = open(path.join(media_dir, filename))
15 15
             self.assertEquals(file.read(), response.content)
  16
+            self.assertEquals(len(response.content), int(response['Content-Length']))
  17
+
  18
+    def test_unknown_mime_type(self):
  19
+        response = self.client.get('/views/site_media/file.unknown')
  20
+        self.assertEquals('application/octet-stream', response['Content-Type'])
16 21
 
17 22
     def test_copes_with_empty_path_component(self):
18 23
         file_name = 'file.txt'
19 24
         response = self.client.get('/views/site_media//%s' % file_name)
20 25
         file = open(path.join(media_dir, file_name))
21 26
         self.assertEquals(file.read(), response.content)
22  
-        
23  
-        
  27
+

0 notes on commit b279b75

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