Django

Code

Changeset 6939

Show
Ignore:
Timestamp:
12/17/07 05:46:48 (7 months ago)
Author:
mtredinnick
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/servers/basehttp.py

    r6780 r6939  
    399399 
    400400        # 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() 
    403415 
    404416    def sendfile(self): 
  • django/trunk/django/views/static.py

    r6768 r6939  
    6060                              statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]): 
    6161        return HttpResponseNotModified() 
    62     mimetype = mimetypes.guess_type(fullpath)[0] 
     62    mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream' 
    6363    contents = open(fullpath, 'rb').read() 
    6464    response = HttpResponse(contents, mimetype=mimetype) 
    6565    response["Last-Modified"] = http_date(statobj[stat.ST_MTIME]) 
     66    response["Content-Length"] = len(contents) 
    6667    return response 
    6768 
  • django/trunk/tests/regressiontests/views/tests/static.py

    r6731 r6939  
    1414            file = open(path.join(media_dir, filename)) 
    1515            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']) 
    1621 
    1722    def test_copes_with_empty_path_component(self): 
     
    2025        file = open(path.join(media_dir, file_name)) 
    2126        self.assertEquals(file.read(), response.content) 
    22          
    23          
     27