Ticket #2504: 2504_http_response_and_conditional_middleware.patch
File 2504_http_response_and_conditional_middleware.patch, 4.2 KB (added by , 17 years ago) |
---|
-
django/http/__init__.py
18 18 class Http404(Exception): 19 19 pass 20 20 21 class UnknownSize(Exception): 22 pass 23 21 24 class HttpRequest(object): 22 25 "A basic HTTP request" 23 26 … … 314 317 315 318 def _get_content(self): 316 319 if self.has_header('Content-Encoding'): 317 return ''.join(self._container) 318 return smart_str(''.join(self._container), self._charset) 320 content = ''.join(self._container) 321 else: 322 content = smart_str(''.join(self._container), self._charset) 323 # cache generated output 324 self.content = content 325 return content 319 326 320 327 def _set_content(self, value): 321 328 self._container = [value] 322 329 self._is_string = True 323 330 331 def get_content_length(self): 332 # try to determin the length of the file 333 # 334 if isinstance(self._container, file): 335 if os.path.exists(self._container.name): 336 return os.path.getsize(self._container.name) 337 else: 338 raise UnknownSize, "size of file can't be determined" 339 return len(self.content) 340 341 324 342 content = property(_get_content, _set_content) 325 343 326 344 def __iter__(self): -
django/middleware/http.py
11 11 def process_response(self, request, response): 12 12 response['Date'] = http_date() 13 13 if not response.has_header('Content-Length'): 14 response['Content-Length'] = str(len(response.content)) 14 try: 15 clength = response.get_content_length() 16 response['Content-Length'] = str(clength) 17 except http.UnknownSize: 18 # FIXME we could switch to chunked transfer encoding if 19 # self.META['SERVER_PROTOCOL'] == "HTTP/1.1" 20 # don't sending a Content-Length is legal and EOF 21 # of tcp connection determins end of stream 22 pass 15 23 16 24 if response.has_header('ETag'): 17 25 if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None) -
tests/regressiontests/httpwrappers/tests.py
391 391 >>> q.getlist('foo') 392 392 [u'bar', u'\ufffd'] 393 393 394 >>> r = HttpResponse() 395 >>> r.get_content_length() 396 0 397 398 >>> r = HttpResponse('foo') 399 >>> r.get_content_length() 400 3 401 402 >>> r = HttpResponse('foo') 403 >>> r.write('bar') 404 >>> r.get_content_length() 405 6 406 407 >>> r = HttpResponse(['foo', 'bar', 'foobar']) 408 >>> r.get_content_length() 409 12 410 411 >>> r = HttpResponse(file(__file__)) 412 >>> assert r.get_content_length() > 0 413 414 >>> import os, os.path 415 >>> if os.path.isfile("/bin/echo"): 416 ... fp = os.popen2("echo") 417 ... r = HttpResponse(fp[1]) 418 ... r.get_content_length() 419 Traceback (most recent call last): 420 ... 421 UnknownSize: size of file can't be determined 422 423 >>> def ten_foos(): 424 ... for i in range(10): yield 'foo' 425 426 >>> r = HttpResponse(ten_foos()) 427 >>> r.get_content_length() 428 30 429 >>> print r.content 430 foofoofoofoofoofoofoofoofoofoo 431 394 432 """ 395 433 396 from django.http import QueryDict 434 from django.http import QueryDict, HttpResponse 397 435 398 436 if __name__ == "__main__": 399 437 import doctest -
docs/request_response.txt
431 431 ``write(content)``, ``flush()`` and ``tell()`` 432 432 These methods make an ``HttpResponse`` instance a file-like object. 433 433 434 ``get_content_length`` 435 **(New in Django development version)** 436 Returns the length of the content. 437 If the content of HttpResponse is a Filepointer to a file, the filesize is returned. 438 It the Filepointer is a pipe, django.http.UnknownSize exception is raised. 439 434 440 .. _HTTP Status code: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10 435 441 436 442 HttpResponse subclasses