Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
9588718c
Kaydet (Commit)
9588718c
authored
Haz 18, 2016
tarafından
Claude Paroz
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #5897 -- Added the Content-Length response header in CommonMiddleware
Thanks Tim Graham for the review.
üst
ca77b509
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
44 additions
and
5 deletions
+44
-5
common.py
django/middleware/common.py
+4
-0
middleware.txt
docs/ref/middleware.txt
+6
-0
1.11.txt
docs/releases/1.11.txt
+3
-0
tests.py
tests/middleware/tests.py
+24
-0
test_settings.py
tests/project_template/test_settings.py
+1
-0
tests.py
tests/wsgi/tests.py
+6
-5
No files found.
django/middleware/common.py
Dosyayı görüntüle @
9588718c
...
...
@@ -123,6 +123,10 @@ class CommonMiddleware(MiddlewareMixin):
etag
=
unquote_etag
(
response
[
'ETag'
]),
response
=
response
,
)
# Add the Content-Length header to non-streaming responses if not
# already set.
if
not
response
.
streaming
and
not
response
.
has_header
(
'Content-Length'
):
response
[
'Content-Length'
]
=
str
(
len
(
response
.
content
))
return
response
...
...
docs/ref/middleware.txt
Dosyayı görüntüle @
9588718c
...
...
@@ -66,6 +66,12 @@ Adds a few conveniences for perfectionists:
for each request by MD5-hashing the page content, and it'll take care of
sending ``Not Modified`` responses, if appropriate.
* Sets the ``Content-Length`` header for non-streaming responses.
.. versionchanged: 1.11
Older versions didn't set the ``Content-Length`` header.
.. attribute:: CommonMiddleware.response_redirect_class
Defaults to :class:`~django.http.HttpResponsePermanentRedirect`. Subclass
...
...
docs/releases/1.11.txt
Dosyayı görüntüle @
9588718c
...
...
@@ -197,6 +197,9 @@ Requests and Responses
* Added :meth:`QueryDict.fromkeys() <django.http.QueryDict.fromkeys>`.
* :class:`~django.middleware.common.CommonMiddleware` now sets the
``Content-Length`` response header for non-streaming responses.
Serialization
~~~~~~~~~~~~~
...
...
tests/middleware/tests.py
Dosyayı görüntüle @
9588718c
...
...
@@ -285,6 +285,27 @@ class CommonMiddlewareTest(SimpleTestCase):
second_res
=
CommonMiddleware
()
.
process_response
(
second_req
,
HttpResponse
(
'content'
))
self
.
assertEqual
(
second_res
.
status_code
,
304
)
# Tests for the Content-Length header
def
test_content_length_header_added
(
self
):
response
=
HttpResponse
(
'content'
)
self
.
assertNotIn
(
'Content-Length'
,
response
)
response
=
CommonMiddleware
()
.
process_response
(
HttpRequest
(),
response
)
self
.
assertEqual
(
int
(
response
[
'Content-Length'
]),
len
(
response
.
content
))
def
test_content_length_header_not_added_for_streaming_response
(
self
):
response
=
StreamingHttpResponse
(
'content'
)
self
.
assertNotIn
(
'Content-Length'
,
response
)
response
=
CommonMiddleware
()
.
process_response
(
HttpRequest
(),
response
)
self
.
assertNotIn
(
'Content-Length'
,
response
)
def
test_content_length_header_not_changed
(
self
):
response
=
HttpResponse
()
bad_content_length
=
len
(
response
.
content
)
+
10
response
[
'Content-Length'
]
=
bad_content_length
response
=
CommonMiddleware
()
.
process_response
(
HttpRequest
(),
response
)
self
.
assertEqual
(
int
(
response
[
'Content-Length'
]),
bad_content_length
)
# Other tests
@override_settings
(
DISALLOWED_USER_AGENTS
=
[
re
.
compile
(
r'foo'
)])
...
...
@@ -445,6 +466,9 @@ class ConditionalGetMiddlewareTest(SimpleTestCase):
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
)
...
...
tests/project_template/test_settings.py
Dosyayı görüntüle @
9588718c
...
...
@@ -41,6 +41,7 @@ class TestStartProjectSettings(TestCase):
response
=
self
.
client
.
get
(
'/empty/'
)
headers
=
sorted
(
response
.
serialize_headers
()
.
split
(
b
'
\r\n
'
))
self
.
assertEqual
(
headers
,
[
b
'Content-Length: 0'
,
b
'Content-Type: text/html; charset=utf-8'
,
b
'X-Frame-Options: SAMEORIGIN'
,
])
tests/wsgi/tests.py
Dosyayı görüntüle @
9588718c
...
...
@@ -41,11 +41,12 @@ class WSGITest(SimpleTestCase):
self
.
assertEqual
(
response_data
[
"status"
],
"200 OK"
)
self
.
assertEqual
(
response_data
[
"headers"
],
[(
'Content-Type'
,
'text/html; charset=utf-8'
)])
self
.
assertEqual
(
bytes
(
response
),
b
"Content-Type: text/html; charset=utf-8
\r\n\r\n
Hello World!"
)
set
(
response_data
[
"headers"
]),
{(
'Content-Length'
,
'12'
),
(
'Content-Type'
,
'text/html; charset=utf-8'
)})
self
.
assertTrue
(
bytes
(
response
)
in
[
b
"Content-Length: 12
\r\n
Content-Type: text/html; charset=utf-8
\r\n\r\n
Hello World!"
,
b
"Content-Type: text/html; charset=utf-8
\r\n
Content-Length: 12
\r\n\r\n
Hello World!"
])
def
test_file_wrapper
(
self
):
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment