Fixed #27346 -- Stopped setting the Content-Length header in ConditionalGetMiddleware.

This commit is contained in:
Adam Malinowski 2016-11-05 12:39:46 +01:00 committed by Tim Graham
parent c7dddc01d6
commit 37809b891e
5 changed files with 7 additions and 35 deletions

View File

@ -9,6 +9,7 @@ answer newbie questions, and generally made Django that much better:
Aaron Swartz <http://www.aaronsw.com/>
Aaron T. Myers <atmyers@gmail.com>
Adam Johnson <https://github.com/adamchainz>
Adam Malinowski <http://adammalinowski.co.uk>
Adam Vandenberg
Adrian Holovaty <adrian@holovaty.com>
Adrien Lemaire <lemaire.adrien@gmail.com>

View File

@ -11,13 +11,8 @@ class ConditionalGetMiddleware(MiddlewareMixin):
Last-Modified header, and the request has If-None-Match or
If-Modified-Since, the response is replaced by an HttpNotModified. An ETag
header is added if needed.
Also sets the Content-Length response-header.
"""
def process_response(self, request, response):
if not response.streaming and not response.has_header('Content-Length'):
response['Content-Length'] = str(len(response.content))
# It's too late to prevent an unsafe request with a 412 response, and
# for a HEAD request, the response body is always empty so computing
# an accurate ETag isn't possible.

View File

@ -181,12 +181,10 @@ header, the middleware adds one if needed. If the response has a ``ETag`` or
``If-Modified-Since``, the response is replaced by an
:class:`~django.http.HttpResponseNotModified`.
Also sets ``Content-Length`` response-header.
.. versionchanged:: 1.11
In older versions, the middleware set the ``Date`` header and didn't set
the ``ETag`` header.
In older versions, the middleware set the ``Content-Length`` and ``Date``
headers and didn't set the ``ETag`` header.
Locale middleware
-----------------

View File

@ -579,8 +579,10 @@ Miscellaneous
* In the admin templates, ``<p class="help">`` is replaced with a ``<div>`` tag
to allow including lists inside help text.
* ``ConditionalGetMiddleware`` no longer sets the ``Date`` header as Web
servers set that header.
* :class:`~django.middleware.http.ConditionalGetMiddleware` no longer sets the
``Date`` header as Web servers set that header. It also no longer sets the
``Content-Length`` header as this is now done by
:class:`~django.middleware.common.CommonMiddleware`.
* :meth:`~django.apps.AppConfig.get_model` and
:meth:`~django.apps.AppConfig.get_models` now raise

View File

@ -478,30 +478,6 @@ class ConditionalGetMiddlewareTest(SimpleTestCase):
self.req = RequestFactory().get('/')
self.resp = self.client.get(self.req.path_info)
# Tests for the Content-Length header
def test_content_length_header_added(self):
content_length = len(self.resp.content)
# Already set by CommonMiddleware, remove it to check that
# ConditionalGetMiddleware readds it.
del self.resp['Content-Length']
self.assertNotIn('Content-Length', self.resp)
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
self.assertIn('Content-Length', self.resp)
self.assertEqual(int(self.resp['Content-Length']), content_length)
def test_content_length_header_not_added(self):
resp = StreamingHttpResponse('content')
self.assertNotIn('Content-Length', resp)
resp = ConditionalGetMiddleware().process_response(self.req, resp)
self.assertNotIn('Content-Length', resp)
def test_content_length_header_not_changed(self):
bad_content_length = len(self.resp.content) + 10
self.resp['Content-Length'] = bad_content_length
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
self.assertEqual(int(self.resp['Content-Length']), bad_content_length)
# Tests for the ETag header
def test_middleware_calculates_etag(self):