Ticket #7581: 7581-Streaming-HttpResponse-r11381.diff
File 7581-Streaming-HttpResponse-r11381.diff, 7.2 KB (added by , 15 years ago) |
---|
-
django/http/__init__.py
272 272 status_code = 200 273 273 274 274 def __init__(self, content='', mimetype=None, status=None, 275 content_type=None ):275 content_type=None, stream_content=False): 276 276 from django.conf import settings 277 277 self._charset = settings.DEFAULT_CHARSET 278 278 if mimetype: … … 280 280 if not content_type: 281 281 content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, 282 282 settings.DEFAULT_CHARSET) 283 if not isinstance(content, basestring) and hasattr(content, '__iter__'): 284 self._container = content 285 self._is_string = False 286 else: 287 self._container = [content] 288 self._is_string = True 283 self._stream_content = stream_content 284 self.content = content 289 285 self.cookies = SimpleCookie() 290 286 if status: 291 287 self.status_code = status … … 359 355 self.set_cookie(key, max_age=0, path=path, domain=domain, 360 356 expires='Thu, 01-Jan-1970 00:00:00 GMT') 361 357 358 def _consume_content(self): 359 if not self._is_string: 360 self._container = [''.join(self._container)] 361 self._is_string = True 362 362 363 def _get_content(self): 364 self._consume_content() 363 365 if self.has_header('Content-Encoding'): 364 366 return ''.join(self._container) 365 367 return smart_str(''.join(self._container), self._charset) 366 368 367 369 def _set_content(self, value): 368 self._container = [value] 369 self._is_string = True 370 if not isinstance(value, basestring) and hasattr(value, '__iter__'): 371 self._container = value 372 self._is_string = False 373 if not self._stream_content: 374 self._consume_content() 375 else: 376 self._container = [value] 377 self._is_string = True 370 378 371 379 content = property(_get_content, _set_content) 372 380 381 def _get_content_generator(self): 382 if not self._is_string: 383 return self._container 384 385 content_generator = property(_get_content_generator) 386 373 387 def __iter__(self): 374 388 self._iterator = iter(self._container) 375 389 return self -
django/contrib/csrf/middleware.py
101 101 "' /></div>") 102 102 103 103 # Modify any POST forms 104 response.content = _POST_FORM_RE.sub(add_csrf_field, response.content) 104 if response.content_generator: 105 response.content = (_POST_FORM_RE.sub(add_csrf_field, chunk) for chunk in response.content_generator) 106 else: 107 response.content = _POST_FORM_RE.sub(add_csrf_field, response.content) 105 108 return response 106 109 107 110 class CsrfMiddleware(CsrfViewMiddleware, CsrfResponseMiddleware): -
django/utils/text.py
176 176 zfile.close() 177 177 return zbuf.getvalue() 178 178 179 # WARNING - be aware that compress_sequence does not achieve the same 180 # level of compression as compress_string 181 def compress_sequence(sequence): 182 import cStringIO, gzip 183 zbuf = cStringIO.StringIO() 184 zfile = gzip.GzipFile(mode='wb', compresslevel=6, fileobj=zbuf) 185 yield zbuf.getvalue() 186 for item in sequence: 187 position = zbuf.tell() 188 zfile.write(item) 189 zfile.flush() 190 zbuf.seek(position) 191 yield zbuf.read() 192 position = zbuf.tell() 193 zfile.close() 194 zbuf.seek(position) 195 yield zbuf.read() 196 179 197 ustring_re = re.compile(u"([\u0080-\uffff])") 180 198 181 199 def javascript_quote(s, quote_double_quotes=False): -
django/middleware/common.py
100 100 if settings.USE_ETAGS: 101 101 if response.has_header('ETag'): 102 102 etag = response['ETag'] 103 el se:103 elif not response.content_generator: 104 104 etag = '"%s"' % md5_constructor(response.content).hexdigest() 105 if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag: 106 cookies = response.cookies 107 response = http.HttpResponseNotModified() 108 response.cookies = cookies 109 else: 110 response['ETag'] = etag 105 try: 106 if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag: 107 cookies = response.cookies 108 response = http.HttpResponseNotModified() 109 response.cookies = cookies 110 else: 111 response['ETag'] = etag 112 except NameError: 113 pass 111 114 112 115 return response 113 116 -
django/middleware/gzip.py
1 1 import re 2 2 3 from django.utils.text import compress_s tring3 from django.utils.text import compress_sequence, compress_string 4 4 from django.utils.cache import patch_vary_headers 5 5 6 6 re_accepts_gzip = re.compile(r'\bgzip\b') … … 13 13 """ 14 14 def process_response(self, request, response): 15 15 # It's not worth compressing non-OK or really short responses. 16 if response.status_code != 200 or len(response.content) < 200:16 if response.status_code != 200 or (not response.content_generator and len(response.content) < 200): 17 17 return response 18 18 19 19 patch_vary_headers(response, ('Accept-Encoding',)) … … 32 32 if not re_accepts_gzip.search(ae): 33 33 return response 34 34 35 response.content = compress_string(response.content) 35 if response.content_generator: 36 response.content = compress_sequence(response.content_generator) 37 del response['Content-Length'] 38 else: 39 response.content = compress_string(response.content) 40 response['Content-Length'] = str(len(response.content)) 36 41 response['Content-Encoding'] = 'gzip' 37 response['Content-Length'] = str(len(response.content))38 42 return response -
django/middleware/http.py
11 11 """ 12 12 def process_response(self, request, response): 13 13 response['Date'] = http_date() 14 if not response.has_header('Content-Length') :14 if not response.has_header('Content-Length') and not response.content_generator: 15 15 response['Content-Length'] = str(len(response.content)) 16 16 17 17 if response.has_header('ETag'):