Ticket #2504: get_content_length.diff

File get_content_length.diff, 3.2 KB (added by Fraser Nevett, 17 years ago)
  • django/http/__init__.py

     
    307307
    308308    content = property(_get_content, _set_content)
    309309
     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
    310321    def __iter__(self):
    311322        self._iterator = iter(self._container)
    312323        return self
  • django/middleware/http.py

     
    1313    def process_response(self, request, response):
    1414        response['Date'] = formatdate()[:26] + "GMT"
    1515        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)
    1719
    1820        if response.has_header('ETag'):
    1921            if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None)
  • tests/regressiontests/httpwrappers/tests.py

     
    391391>>> q.getlist('foo')
    392392[u'bar', u'\ufffd']
    393393
     394
     395>>> r = HttpResponse()
     396>>> r.get_content_length()
     3970
     398
     399>>> r = HttpResponse('foo')
     400>>> r.get_content_length()
     4013
     402
     403>>> r = HttpResponse('foo')
     404>>> r.write('bar')
     405>>> r.get_content_length()
     4066
     407
     408>>> r = HttpResponse(['foo', 'bar', 'foobar'])
     409>>> r.get_content_length()
     41012
     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
     420foofoofoofoofoofoofoofoofoofoo
     421
    394422"""
    395423
    396 from django.http import QueryDict
     424from django.http import QueryDict, HttpResponse
    397425
    398426if __name__ == "__main__":
    399427    import doctest
  • docs/request_response.txt

     
    431431``write(content)``, ``flush()`` and ``tell()``
    432432    These methods make an ``HttpResponse`` instance a file-like object.
    433433
     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
    434440.. _HTTP Status code: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10
    435441
    436442HttpResponse subclasses
Back to Top