Ticket #7894: file_wrapper_response.diff

File file_wrapper_response.diff, 2.0 KB (added by graham.carlyle@…, 7 years ago)

Provides the HttpResponseFileWrapper and support for the wsgi file_wrapper speedup as well as a fix to achieve this is the simple_server

  • django/http/__init__.py

     
    424424    def __init__(self, *args, **kwargs):
    425425        HttpResponse.__init__(self, *args, **kwargs)
    426426
     427class HttpResponseFileWrapper(HttpResponse):
     428    def __init__(self, filelike, block_size=8192, **kwargs):
     429        default_reader = iter(lambda: filelike.read(block_size), '')
     430        HttpResponse.__init__(self, content=default_reader, **kwargs)
     431        self.filelike = filelike
     432        self.block_size = block_size
     433
    427434# A backwards compatible alias for HttpRequest.get_host.
    428435def get_host(request):
    429436    return request.get_host()
  • django/core/servers/basehttp.py

     
    312312        in the event loop to iterate over the data, and to call
    313313        'self.close()' once the response is finished.
    314314        """
    315         if not self.result_is_file() and not self.sendfile():
     315        if not self.result_is_file() or not self.sendfile():
    316316            for data in self.result:
    317317                self.write(data)
    318318            self.finish_content()
  • django/core/handlers/wsgi.py

     
    232232        for c in response.cookies.values():
    233233            response_headers.append(('Set-Cookie', str(c.output(header=''))))
    234234        start_response(status, response_headers)
    235         return response
     235        if (isinstance(response, http.HttpResponseFileWrapper) and
     236            'wsgi.file_wrapper'in environ):
     237            return environ['wsgi.file_wrapper'](response.filelike, response.block_size)
     238        else:
     239            return response
    236240
Back to Top