Code

Ticket #2131: http-response-send-file.diff

File http-response-send-file.diff, 3.0 KB (added by ymasuda[at]ethercube.com, 8 years ago)

diff implements HttpResponseSendFile

Line 
1Index: django/http/__init__.py
2===================================================================
3--- django/http/__init__.py     (revision 2967)
4+++ django/http/__init__.py     (working copy)
5@@ -238,6 +238,17 @@
6             raise Exception, "This %s instance cannot tell its position" % self.__class__
7         return sum([len(chunk) for chunk in self._iterator])
8 
9+class HttpResponseSendFile(HttpResponse):
10+    def __init__(self, path_to_file, content_type=None):
11+        HttpResponse.__init__(self)
12+        if not content_type:
13+            from mimetime import guess_type
14+            content_type = guess_type(path_to_file)
15+        self['Content-Type'] = content_type
16+        self.sendfile_filename = path_to_file
17+        self.block_size = 8192
18+        self.status_code = 200
19+
20 class HttpResponseRedirect(HttpResponse):
21     def __init__(self, redirect_to):
22         HttpResponse.__init__(self)
23Index: django/core/servers/basehttp.py
24===================================================================
25--- django/core/servers/basehttp.py     (revision 2967)
26+++ django/core/servers/basehttp.py     (working copy)
27@@ -307,10 +307,9 @@
28         in the event loop to iterate over the data, and to call
29         'self.close()' once the response is finished.
30         """
31-        if not self.result_is_file() and not self.sendfile():
32-            for data in self.result:
33-                self.write(data)
34-            self.finish_content()
35+        for data in self.result:
36+            self.write(data)
37+        self.finish_content()
38         self.close()
39 
40     def get_scheme(self):
41Index: django/core/handlers/wsgi.py
42===================================================================
43--- django/core/handlers/wsgi.py        (revision 2967)
44+++ django/core/handlers/wsgi.py        (working copy)
45@@ -160,4 +160,11 @@
46         for c in response.cookies.values():
47             response_headers.append(('Set-Cookie', c.output(header='')))
48         start_response(status, response_headers)
49+        if hasattr(response, 'sendfile_filename'):
50+            filelike = open(response.sendfile_filename, 'rb')
51+            block_size = response.block_size
52+            if 'wsgi.file_wrapper' in environ:
53+                return environ['wsgi.file_wrapper'](filelike, block_size)
54+            else:
55+                return iter(lambda: filelike.read(block_size), '')
56         return response.iterator
57Index: django/core/handlers/modpython.py
58===================================================================
59--- django/core/handlers/modpython.py   (revision 2967)
60+++ django/core/handlers/modpython.py   (working copy)
61@@ -143,6 +143,9 @@
62     "Populates the mod_python request object with an HttpResponse"
63     from django.conf import settings
64     mod_python_req.content_type = http_response['Content-Type']
65+    if hasattr(http_response, 'sendfile_filename'):
66+        mod_python_req.sendfile(http_response.sendfile_filename)
67+        return
68     for key, value in http_response.headers.items():
69         if key != 'Content-Type':
70             mod_python_req.headers_out[key] = value