Code

Ticket #7894: file_wrapper_response.diff

File file_wrapper_response.diff, 2.0 KB (added by graham.carlyle@…, 6 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

Line 
1Index: django/http/__init__.py
2===================================================================
3--- django/http/__init__.py     (revision 8053)
4+++ django/http/__init__.py     (working copy)
5@@ -424,6 +424,13 @@
6     def __init__(self, *args, **kwargs):
7         HttpResponse.__init__(self, *args, **kwargs)
8 
9+class HttpResponseFileWrapper(HttpResponse):
10+    def __init__(self, filelike, block_size=8192, **kwargs):
11+        default_reader = iter(lambda: filelike.read(block_size), '')
12+        HttpResponse.__init__(self, content=default_reader, **kwargs)
13+        self.filelike = filelike
14+        self.block_size = block_size
15+
16 # A backwards compatible alias for HttpRequest.get_host.
17 def get_host(request):
18     return request.get_host()
19Index: django/core/servers/basehttp.py
20===================================================================
21--- django/core/servers/basehttp.py     (revision 8053)
22+++ django/core/servers/basehttp.py     (working copy)
23@@ -312,7 +312,7 @@
24         in the event loop to iterate over the data, and to call
25         'self.close()' once the response is finished.
26         """
27-        if not self.result_is_file() and not self.sendfile():
28+        if not self.result_is_file() or not self.sendfile():
29             for data in self.result:
30                 self.write(data)
31             self.finish_content()
32Index: django/core/handlers/wsgi.py
33===================================================================
34--- django/core/handlers/wsgi.py        (revision 8053)
35+++ django/core/handlers/wsgi.py        (working copy)
36@@ -232,5 +232,9 @@
37         for c in response.cookies.values():
38             response_headers.append(('Set-Cookie', str(c.output(header=''))))
39         start_response(status, response_headers)
40-        return response
41+        if (isinstance(response, http.HttpResponseFileWrapper) and
42+            'wsgi.file_wrapper'in environ):
43+            return environ['wsgi.file_wrapper'](response.filelike, response.block_size)
44+        else:
45+            return response
46