Code

Ticket #1569: 1569.trunk.2.diff

File 1569.trunk.2.diff, 4.9 KB (added by Maniac <Maniac@…>, 8 years ago)

More correct patch for trunk

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