Ticket #7581: 7581-Streaming-HttpResponse-r16445.diff
File 7581-Streaming-HttpResponse-r16445.diff, 6.6 KB (added by , 13 years ago) |
---|
-
django/http/__init__.py
549 549 status_code = 200 550 550 551 551 def __init__(self, content='', mimetype=None, status=None, 552 content_type=None ):552 content_type=None, stream_content=False): 553 553 # _headers is a mapping of the lower-case name to the original case of 554 554 # the header (required for working with legacy systems) and the header 555 555 # value. Both the name of the header and its value are ASCII strings. … … 560 560 if not content_type: 561 561 content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, 562 562 self._charset) 563 if not isinstance(content, basestring) and hasattr(content, '__iter__'): 564 self._container = content 565 self._is_string = False 566 else: 567 self._container = [content] 568 self._is_string = True 563 self._stream_content = stream_content 564 self.content = content 569 565 self.cookies = SimpleCookie() 570 566 if status: 571 567 self.status_code = status … … 663 659 self.set_cookie(key, max_age=0, path=path, domain=domain, 664 660 expires='Thu, 01-Jan-1970 00:00:00 GMT') 665 661 662 def _consume_content(self): 663 if not self._is_string: 664 self._container = [''.join(self._container)] 665 self._is_string = True 666 666 667 def _get_content(self): 668 self._consume_content() 667 669 if self.has_header('Content-Encoding'): 668 670 return ''.join(self._container) 669 671 return smart_str(''.join(self._container), self._charset) 670 672 671 673 def _set_content(self, value): 672 self._container = [value] 673 self._is_string = True 674 if not isinstance(value, basestring) and hasattr(value, '__iter__'): 675 self._container = value 676 self._is_string = False 677 if not self._stream_content: 678 self._consume_content() 679 else: 680 self._container = [value] 681 self._is_string = True 674 682 675 683 content = property(_get_content, _set_content) 676 684 685 def _get_content_generator(self): 686 if not self._is_string: 687 return (smart_str(item, self._charset) for item in self._container) 688 689 content_generator = property(_get_content_generator) 690 677 691 def __iter__(self): 678 692 self._iterator = iter(self._container) 679 693 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"' % hashlib.md5(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.cookies 119 response = http.HttpResponseNotModified() 120 response.cookies = cookies 121 else: 122 response['ETag'] = etag 117 try: 118 if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag: 119 cookies = response.cookies 120 response = http.HttpResponseNotModified() 121 response.cookies = cookies 122 else: 123 response['ETag'] = etag 124 except NameError: 125 pass 123 126 124 127 return response 125 128 -
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'):