Ticket #7581: streaming_response.diff
File streaming_response.diff, 6.8 KB (added by , 16 years ago) |
---|
-
django/http/__init__.py
268 268 if not content_type: 269 269 content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, 270 270 settings.DEFAULT_CHARSET) 271 if not isinstance(content, basestring) and hasattr(content, '__iter__'): 272 self._container = content 273 self._is_string = False 274 else: 275 self._container = [content] 276 self._is_string = True 271 self.content = content 277 272 self.cookies = SimpleCookie() 278 273 if status: 279 274 self.status_code = status … … 345 340 expires='Thu, 01-Jan-1970 00:00:00 GMT') 346 341 347 342 def _get_content(self): 343 if not self._is_string: 344 self._container = [''.join(self._container)] 345 self._is_string = True 348 346 if self.has_header('Content-Encoding'): 349 347 return ''.join(self._container) 350 348 return smart_str(''.join(self._container), self._charset) 351 349 352 350 def _set_content(self, value): 353 self._container = [value] 354 self._is_string = True 351 if not isinstance(value, basestring) and hasattr(value, '__iter__'): 352 self._container = value 353 self._is_string = False 354 else: 355 self._container = [value] 356 self._is_string = True 355 357 356 358 content = property(_get_content, _set_content) 357 359 360 def _get_content_generator(self): 361 if not self._is_string: 362 return self._container 363 364 content_generator = property(_get_content_generator) 365 358 366 def __iter__(self): 359 367 self._iterator = iter(self._container) 360 368 return self -
django/http/utils.py
31 31 if request.method == 'HEAD': 32 32 response.content = '' 33 33 return response 34 35 def set_content_length(request, response): 36 """ 37 Ensures that we always have a Content-Length header, required by some 38 handlers (WSGI), which don't permit chunked transfer encoding. 39 """ 40 if not 'Content-Length' in response: 41 response['Content-Length'] = str(len(response.content)) 42 return response -
django/core/handlers/wsgi.py
182 182 class WSGIHandler(BaseHandler): 183 183 initLock = Lock() 184 184 request_class = WSGIRequest 185 response_fixes = [http.fix_location_header, 186 http.conditional_content_removal, 187 http.set_content_length] 185 188 186 189 def __call__(self, environ, start_response): 187 190 from django.conf import settings -
django/utils/text.py
175 175 zfile.close() 176 176 return zbuf.getvalue() 177 177 178 def compress_sequence(sequence): 179 import cStringIO, gzip 180 zbuf = cStringIO.StringIO() 181 zfile = gzip.GzipFile(mode='wb', compresslevel=6, fileobj=zbuf) 182 yield zbuf.getvalue() 183 for item in sequence: 184 position = zbuf.tell() 185 zfile.write(item) 186 zfile.flush() 187 zbuf.seek(position) 188 yield zbuf.read() 189 178 190 ustring_re = re.compile(u"([\u0080-\uffff])") 179 191 180 192 def javascript_quote(s, quote_double_quotes=False): -
django/middleware/common.py
107 107 if settings.USE_ETAGS: 108 108 if response.has_header('ETag'): 109 109 etag = response['ETag'] 110 el se:110 elif not response.content_generator: 111 111 etag = '"%s"' % md5.new(response.content).hexdigest() 112 if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag: 113 cookies = response.cookies 114 response = http.HttpResponseNotModified() 115 response.cookies = cookies 116 else: 117 response['ETag'] = etag 112 try: 113 if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag: 114 cookies = response.cookies 115 response = http.HttpResponseNotModified() 116 response.cookies = cookies 117 else: 118 response['ETag'] = etag 119 except NameError: 120 pass 118 121 119 122 return response 120 123 -
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',)) … … 33 33 if not re_accepts_gzip.search(ae): 34 34 return response 35 35 36 response.content = compress_string(response.content) 36 if response.content_generator: 37 response.content = compress_sequence(response.content_generator) 38 del response['Content-Length'] 39 else: 40 response.content = compress_string(response.content) 41 response['Content-Length'] = str(len(response.content)) 37 42 response['Content-Encoding'] = 'gzip' 38 response['Content-Length'] = str(len(response.content))39 43 return response -
django/middleware/http.py
10 10 """ 11 11 def process_response(self, request, response): 12 12 response['Date'] = http_date() 13 if not response.has_header('Content-Length') :13 if not response.has_header('Content-Length') and not response.content_generator: 14 14 response['Content-Length'] = str(len(response.content)) 15 15 16 16 if response.has_header('ETag'):