Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@malcolmt malcolmt authored
View
16 django/core/servers/basehttp.py
@@ -398,8 +398,20 @@ def write(self, data):
self.bytes_sent += len(data)
# XXX check Content-Length and truncate if too many bytes written?
- self._write(data)
- self._flush()
+
+ # If data is too large, socket will choke, so write chunks no larger
+ # than 32MB at a time.
+ length = len(data)
+ if length > 33554432:
+ offset = 0
+ while offset < length:
+ chunk_size = min(33554432, length)
+ self._write(data[offset:offset+chunk_size])
+ self._flush()
+ offset += chunk_size
+ else:
+ self._write(data)
+ self._flush()
def sendfile(self):
"""Platform-specific file transmission
View
3  django/views/static.py
@@ -59,10 +59,11 @@ def serve(request, path, document_root=None, show_indexes=False):
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
return HttpResponseNotModified()
- mimetype = mimetypes.guess_type(fullpath)[0]
+ mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
contents = open(fullpath, 'rb').read()
response = HttpResponse(contents, mimetype=mimetype)
response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
+ response["Content-Length"] = len(contents)
return response
DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
View
1  tests/regressiontests/views/media/file.unknown
@@ -0,0 +1 @@
+An unknown file extension.
View
8 tests/regressiontests/views/tests/static.py
@@ -13,11 +13,15 @@ def test_serve(self):
response = self.client.get('/views/site_media/%s' % filename)
file = open(path.join(media_dir, filename))
self.assertEquals(file.read(), response.content)
+ self.assertEquals(len(response.content), int(response['Content-Length']))
+
+ def test_unknown_mime_type(self):
+ response = self.client.get('/views/site_media/file.unknown')
+ self.assertEquals('application/octet-stream', response['Content-Type'])
def test_copes_with_empty_path_component(self):
file_name = 'file.txt'
response = self.client.get('/views/site_media//%s' % file_name)
file = open(path.join(media_dir, file_name))
self.assertEquals(file.read(), response.content)
-
-
+
Please sign in to comment.
Something went wrong with that request. Please try again.