Ticket #2504: get_content_length.diff
File get_content_length.diff, 3.2 KB (added by , 17 years ago) |
---|
-
django/http/__init__.py
307 307 308 308 content = property(_get_content, _set_content) 309 309 310 def get_content_length(self): 311 if isinstance(self._container, file): 312 if os.path.exists(self._container.name): 313 return os.path.getsize(self._container.name) 314 else: 315 return None 316 elif isinstance(self._container, (list, tuple)): 317 return len(self.content) 318 else: 319 return None 320 310 321 def __iter__(self): 311 322 self._iterator = iter(self._container) 312 323 return self -
django/middleware/http.py
13 13 def process_response(self, request, response): 14 14 response['Date'] = formatdate()[:26] + "GMT" 15 15 if not response.has_header('Content-Length'): 16 response['Content-Length'] = str(len(response.content)) 16 content_length = response.get_content_length() 17 if content_length is not None: 18 response['Content-Length'] = str(content_length) 17 19 18 20 if response.has_header('ETag'): 19 21 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 395 >>> r = HttpResponse() 396 >>> r.get_content_length() 397 0 398 399 >>> r = HttpResponse('foo') 400 >>> r.get_content_length() 401 3 402 403 >>> r = HttpResponse('foo') 404 >>> r.write('bar') 405 >>> r.get_content_length() 406 6 407 408 >>> r = HttpResponse(['foo', 'bar', 'foobar']) 409 >>> r.get_content_length() 410 12 411 412 >>> r = HttpResponse(file(__file__)) 413 >>> assert r.get_content_length() > 0 414 415 >>> def ten_foos(): 416 ... for i in range(10): yield 'foo' 417 >>> r = HttpResponse(ten_foos()) 418 >>> assert r.get_content_length() is None 419 >>> print r.content 420 foofoofoofoofoofoofoofoofoofoo 421 394 422 """ 395 423 396 from django.http import QueryDict 424 from django.http import QueryDict, HttpResponse 397 425 398 426 if __name__ == "__main__": 399 427 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 Calculates the current length of ``content``. If a file was passed into 436 ``__init__``, its system filesize is returned. If a non-fixed size iterator 437 was passed into ``__init__``, then ``None`` will be returned because the size 438 of ``content`` cannot necessarily be determined. 439 434 440 .. _HTTP Status code: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10 435 441 436 442 HttpResponse subclasses