Ticket #7581: 7581-Streaming-HttpResponse-1.3.1.diff
File 7581-Streaming-HttpResponse-1.3.1.diff, 6.6 KB (added by , 13 years ago) |
---|
-
django/http/__init__.py
485 485 status_code = 200 486 486 487 487 def __init__(self, content='', mimetype=None, status=None, 488 content_type=None ):488 content_type=None, stream_content=False): 489 489 # _headers is a mapping of the lower-case name to the original case of 490 490 # the header (required for working with legacy systems) and the header 491 491 # value. Both the name of the header and its value are ASCII strings. … … 496 496 if not content_type: 497 497 content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, 498 498 self._charset) 499 if not isinstance(content, basestring) and hasattr(content, '__iter__'): 500 self._container = content 501 self._is_string = False 502 else: 503 self._container = [content] 504 self._is_string = True 499 self._stream_content = stream_content 500 self.content = content 505 501 self.cookies = SimpleCookie() 506 502 if status: 507 503 self.status_code = status … … 595 591 self.set_cookie(key, max_age=0, path=path, domain=domain, 596 592 expires='Thu, 01-Jan-1970 00:00:00 GMT') 597 593 594 def _consume_content(self): 595 if not self._is_string: 596 self._container = [''.join(self._container)] 597 self._is_string = True 598 598 599 def _get_content(self): 600 self._consume_content() 599 601 if self.has_header('Content-Encoding'): 600 602 return ''.join(self._container) 601 603 return smart_str(''.join(self._container), self._charset) 602 604 603 605 def _set_content(self, value): 604 self._container = [value] 605 self._is_string = True 606 if not isinstance(value, basestring) and hasattr(value, '__iter__'): 607 self._container = value 608 self._is_string = False 609 if not self._stream_content: 610 self._consume_content() 611 else: 612 self._container = [value] 613 self._is_string = True 606 614 607 615 content = property(_get_content, _set_content) 608 616 617 def _get_content_generator(self): 618 if not self._is_string: 619 return (smart_str(item, self._charset) for item in self._container) 620 621 content_generator = property(_get_content_generator) 622 609 623 def __iter__(self): 610 624 self._iterator = iter(self._container) 611 625 return self -
django/utils/text.py
187 187 zfile.close() 188 188 return zbuf.getvalue() 189 189 190 # WARNING - be aware that compress_sequence does not achieve the same 191 # level of compression as compress_string 192 def compress_sequence(sequence): 193 import cStringIO, gzip 194 zbuf = cStringIO.StringIO() 195 zfile = gzip.GzipFile(mode='wb', compresslevel=6, fileobj=zbuf) 196 yield zbuf.getvalue() 197 for item in sequence: 198 position = zbuf.tell() 199 zfile.write(item) 200 zfile.flush() 201 zbuf.seek(position) 202 yield zbuf.read() 203 position = zbuf.tell() 204 zfile.close() 205 zbuf.seek(position) 206 yield zbuf.read() 207 190 208 ustring_re = re.compile(u"([\u0080-\uffff])") 191 209 192 210 def javascript_quote(s, quote_double_quotes=False): -
django/middleware/common.py
112 112 if settings.USE_ETAGS: 113 113 if response.has_header('ETag'): 114 114 etag = response['ETag'] 115 el se:115 elif not response.content_generator: 116 116 etag = '"%s"' % md5_constructor(response.content).hexdigest() 117 if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag:118 cookies = response.cookies119 response = http.HttpResponseNotModified()120 response.cookies = cookies121 else:122 response['ETag'] = etag123 117 118 try: 119 if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag: 120 cookies = response.cookies 121 response = http.HttpResponseNotModified() 122 response.cookies = cookies 123 else: 124 response['ETag'] = etag 125 except NameError: 126 pass 127 124 128 return response 125 129 126 130 def _is_ignorable_404(uri): -
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'):