Skip to content

Commit

Permalink
Fixed #5596 -- Changed the static view for the development server so …
Browse files Browse the repository at this point in the history
…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
malcolmt committed Dec 17, 2007
1 parent 24cb41b commit b279b75
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
16 changes: 14 additions & 2 deletions django/core/servers/basehttp.py
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion django/views/static.py
Expand Up @@ -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 = """
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/views/media/file.unknown
@@ -0,0 +1 @@
An unknown file extension.
8 changes: 6 additions & 2 deletions tests/regressiontests/views/tests/static.py
Expand Up @@ -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)



0 comments on commit b279b75

Please sign in to comment.