Code

Ticket #2131: django-ticket-2131-http-response-send-file.2-django1.0.2final-v1.1.diff

File django-ticket-2131-http-response-send-file.2-django1.0.2final-v1.1.diff, 3.4 KB (added by mizatservercave, 5 years ago)

Supersedes previous patches for Django 1.0.2 final.

Line 
1diff -u django/core/handlers/modpython.py django/core/handlers/modpython.py
2--- django/core/handlers/modpython.py   2008-11-19 00:44:20.000000000 -0500
3+++ django/core/handlers/modpython.py   2009-02-18 12:54:35.000000000 -0500
4@@ -209,6 +209,9 @@
5 
6         # Convert our custom HttpResponse object back into the mod_python req.
7         req.content_type = response['Content-Type']
8+        if hasattr(response, 'sendfile_filename'):
9+            req.sendfile(response.sendfile_filename)
10+            return 0
11         for key, value in response.items():
12             if key != 'content-type':
13                 req.headers_out[str(key)] = str(value)
14diff -u django/core/handlers/wsgi.py django/core/handlers/wsgi.py
15--- django/core/handlers/wsgi.py        2008-11-19 00:44:20.000000000 -0500
16+++ django/core/handlers/wsgi.py        2009-02-18 11:29:33.000000000 -0500
17@@ -250,9 +250,20 @@
18         except KeyError:
19             status_text = 'UNKNOWN STATUS CODE'
20         status = '%s %s' % (response.status_code, status_text)
21+
22+        if hasattr(response, 'sendfile_filename') and response.has_header('Content-Length'):
23+            del response['Content-Length']
24         response_headers = [(str(k), str(v)) for k, v in response.items()]
25         for c in response.cookies.values():
26             response_headers.append(('Set-Cookie', str(c.output(header=''))))
27         start_response(status, response_headers)
28+        if hasattr(response, 'sendfile_filename'):
29+            filelike = open(response.sendfile_filename, 'rb')
30+            block_size = response.block_size
31+            if 'wsgi.file_wrapper' in environ:
32+                return environ['wsgi.file_wrapper'](filelike, block_size)
33+            else:
34+                return iter(lambda: filelike.read(block_size), '')
35+
36         return response
37 
38diff -u django/core/servers/basehttp.py django/core/servers/basehttp.py
39--- django/core/servers/basehttp.py     2008-11-19 00:44:20.000000000 -0500
40+++ django/core/servers/basehttp.py     2009-02-18 11:29:33.000000000 -0500
41@@ -313,10 +313,9 @@
42         in the event loop to iterate over the data, and to call
43         'self.close()' once the response is finished.
44         """
45-        if not self.result_is_file() and not self.sendfile():
46-            for data in self.result:
47-                self.write(data)
48-            self.finish_content()
49+        for data in self.result:
50+            self.write(data)
51+        self.finish_content()
52         self.close()
53 
54     def get_scheme(self):
55diff -u django/http/__init__.py django/http/__init__.py
56--- django/http/__init__.py     2008-11-19 00:44:05.000000000 -0500
57+++ django/http/__init__.py     2009-02-18 11:29:34.000000000 -0500
58@@ -393,6 +393,19 @@
59             raise Exception("This %s instance cannot tell its position" % self.__class__)
60         return sum([len(chunk) for chunk in self._container])
61 
62+class HttpResponseSendFile(HttpResponse):
63+    def __init__(self, path_to_file, content_type=None):
64+        HttpResponse.__init__(self)
65+        if not content_type:
66+            from mimetypes import guess_type
67+            content_type = guess_type(path_to_file)[0]
68+            if content_type == None: # probably windows w/o proper mimetype lookup
69+                content_type = "application/octet-stream"
70+        self['Content-Type'] = content_type
71+        self.sendfile_filename = path_to_file
72+        self.block_size = 8192
73+        self.status_code = 200
74+
75 class HttpResponseRedirect(HttpResponse):
76     status_code = 302
77