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
This commit is contained in:
Malcolm Tredinnick 2007-12-17 11:46:48 +00:00
parent 24cb41b8c7
commit b279b75c87
4 changed files with 23 additions and 5 deletions

View File

@ -398,6 +398,18 @@ class ServerHandler(object):
self.bytes_sent += len(data) self.bytes_sent += len(data)
# XXX check Content-Length and truncate if too many bytes written? # XXX check Content-Length and truncate if too many bytes written?
# 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._write(data)
self._flush() self._flush()

View File

@ -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'), if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]): statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
return HttpResponseNotModified() return HttpResponseNotModified()
mimetype = mimetypes.guess_type(fullpath)[0] mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
contents = open(fullpath, 'rb').read() contents = open(fullpath, 'rb').read()
response = HttpResponse(contents, mimetype=mimetype) response = HttpResponse(contents, mimetype=mimetype)
response["Last-Modified"] = http_date(statobj[stat.ST_MTIME]) response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
response["Content-Length"] = len(contents)
return response return response
DEFAULT_DIRECTORY_INDEX_TEMPLATE = """ DEFAULT_DIRECTORY_INDEX_TEMPLATE = """

View File

@ -0,0 +1 @@
An unknown file extension.

View File

@ -13,6 +13,11 @@ class StaticTests(TestCase):
response = self.client.get('/views/site_media/%s' % filename) response = self.client.get('/views/site_media/%s' % filename)
file = open(path.join(media_dir, filename)) file = open(path.join(media_dir, filename))
self.assertEquals(file.read(), response.content) 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): def test_copes_with_empty_path_component(self):
file_name = 'file.txt' file_name = 'file.txt'
@ -20,4 +25,3 @@ class StaticTests(TestCase):
file = open(path.join(media_dir, file_name)) file = open(path.join(media_dir, file_name))
self.assertEquals(file.read(), response.content) self.assertEquals(file.read(), response.content)