Ticket #5596: static.diff

File static.diff, 2.8 KB (added by eibaan, 17 years ago)

chunk large files, make application/octet-stream the default

  • django/core/servers/basehttp.py

     
    399399            self.bytes_sent += len(data)
    400400
    401401        # XXX check Content-Length and truncate if too many bytes written?
     402
     403        # if data is too large, socket will choke, so transfer smaller chunks
     404        length = len(data)
     405        if length > 33554432:
     406            offset = 0
     407            while length > 0:
     408                chunk_size = min(33554432, length)
     409                self._write(data[offset:offset+chunk_size])
     410                self._flush()
     411                offset += chunk_size
     412                length -= chunk_size
     413            return
    402414        self._write(data)
    403415        self._flush()
    404416
  • django/views/static.py

     
    5151    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
    5252                              statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
    5353        return HttpResponseNotModified()
    54     mimetype = mimetypes.guess_type(fullpath)[0]
     54    mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
    5555    contents = open(fullpath, 'rb').read()
    5656    response = HttpResponse(contents, mimetype=mimetype)
    5757    response["Last-Modified"] = rfc822.formatdate(statobj[stat.ST_MTIME])
     58    response["Content-Length"] = len(contents)
    5859    return response
    5960
    6061DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
  • tests/regressiontests/views/media/file.unknown

     
     1An unknown file extension.
  • tests/regressiontests/views/tests/static.py

     
    1212        for filename in media_files:
    1313            response = self.client.get('/views/site_media/%s' % filename)
    1414            file = open(path.join(media_dir, filename))
    15             self.assertEquals(file.read(), response.content)
    16  No newline at end of file
     15            self.assertEquals(file.read(), response.content)
     16            self.assertEquals(len(response.content), 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'])
Back to Top