Code

Ticket #1569: 1569.m-r.diff

File 1569.m-r.diff, 5.1 KB (added by Maniac <Maniac@…>, 8 years ago)

Patch for magic-removal branch

Line 
1Index: django/http/__init__.py
2===================================================================
3--- django/http/__init__.py     (revision 2604)
4+++ django/http/__init__.py     (working copy)
5@@ -2,6 +2,7 @@
6 from pprint import pformat
7 from urllib import urlencode
8 from django.utils.datastructures import MultiValueDict
9+from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET
10 
11 try:
12     # The mod_python version is more efficient, so try importing it first.
13@@ -146,13 +147,17 @@
14         cookiedict[key] = c.get(key).value
15     return cookiedict
16 
17-class HttpResponse:
18+class HttpResponse(object):
19     "A basic HTTP response, with content and dictionary-accessed headers"
20     def __init__(self, content='', mimetype=None):
21         if not mimetype:
22-            from django.conf import settings
23-            mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET)
24-        self.content = content
25+            mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET)
26+        if hasattr(content,'__iter__'):
27+            self.iterator = content
28+            self._is_string = False
29+        else:
30+            self.iterator = [content]
31+            self._is_string = True
32         self.headers = {'Content-Type':mimetype}
33         self.cookies = SimpleCookie()
34         self.status_code = 200
35@@ -196,25 +201,32 @@
36         except KeyError:
37             pass
38 
39-    def get_content_as_string(self, encoding):
40-        """
41-        Returns the content as a string, encoding it from a Unicode object if
42-        necessary.
43-        """
44-        if isinstance(self.content, unicode):
45-            return self.content.encode(encoding)
46-        return self.content
47+    def _get_content(self):
48+        content = ''.join(self.iterator)
49+        if isinstance(content, unicode):
50+            content = content.encode(DEFAULT_CHARSET)
51+        return content
52 
53+    def _set_content(self, value):
54+        self.iterator = [value]
55+        self._is_string = True
56+
57+    content = property(_get_content,_set_content)
58+
59     # The remaining methods partially implement the file-like object interface.
60     # See http://docs.python.org/lib/bltin-file-objects.html
61     def write(self, content):
62-        self.content += content
63+        if not self._is_string:
64+            raise Exception, "This %s instance is not writable"%self.__class__
65+        self.iterator.append(content)
66 
67     def flush(self):
68         pass
69 
70     def tell(self):
71-        return len(self.content)
72+        if not self._is_string:
73+            raise Exception, "This %s instance cannot tell its position"%self.__class__
74+        return sum(len(chunk) for chunk in self.iterator)
75 
76 class HttpResponseRedirect(HttpResponse):
77     def __init__(self, redirect_to):
78Index: django/core/handlers/wsgi.py
79===================================================================
80--- django/core/handlers/wsgi.py        (revision 2604)
81+++ django/core/handlers/wsgi.py        (working copy)
82@@ -159,6 +159,5 @@
83         response_headers = response.headers.items()
84         for c in response.cookies.values():
85             response_headers.append(('Set-Cookie', c.output(header='')))
86-        output = [response.get_content_as_string(settings.DEFAULT_CHARSET)]
87         start_response(status, response_headers)
88-        return output
89+        return response.iterator
90Index: django/core/handlers/modpython.py
91===================================================================
92--- django/core/handlers/modpython.py   (revision 2604)
93+++ django/core/handlers/modpython.py   (working copy)
94@@ -149,7 +149,8 @@
95     for c in http_response.cookies.values():
96         mod_python_req.headers_out.add('Set-Cookie', c.output(header=''))
97     mod_python_req.status = http_response.status_code
98-    mod_python_req.write(http_response.get_content_as_string(settings.DEFAULT_CHARSET))
99+    for chunk in http_response.iterator:
100+        mod_python_req.write(chunk)
101 
102 def handler(req):
103     # mod_python hooks into this function.
104Index: django/utils/cache.py
105===================================================================
106--- django/utils/cache.py       (revision 2604)
107+++ django/utils/cache.py       (working copy)
108@@ -74,7 +74,7 @@
109         cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
110     now = datetime.datetime.utcnow()
111     if not response.has_header('ETag'):
112-        response['ETag'] = md5.new(response.get_content_as_string('utf8')).hexdigest()
113+        response['ETag'] = md5.new(response.content).hexdigest()
114     if not response.has_header('Last-Modified'):
115         response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
116     if not response.has_header('Expires'):
117Index: django/middleware/common.py
118===================================================================
119--- django/middleware/common.py (revision 2604)
120+++ django/middleware/common.py (working copy)
121@@ -68,7 +68,7 @@
122 
123         # Use ETags, if requested.
124         if settings.USE_ETAGS:
125-            etag = md5.new(response.get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest()
126+            etag = md5.new(response.content).hexdigest()
127             if request.META.get('HTTP_IF_NONE_MATCH') == etag:
128                 response = http.HttpResponseNotModified()
129             else: