Skip to content

Commit 9175145

Browse files
committed
Merge pull request #488 from KeepSafe/forbid_chunked_on_http10
Fix #476: Raise RuntimeError when chunked enabled and HTTP is 1.0
2 parents e85a85d + 9412aa4 commit 9175145

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

aiohttp/web_reqrep.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
CIMultiDict,
2525
MultiDictProxy,
2626
MultiDict)
27-
from .protocol import Response as ResponseImpl, HttpVersion10
27+
from .protocol import Response as ResponseImpl, HttpVersion10, HttpVersion11
2828
from .streams import EOF_MARKER
2929

3030

@@ -647,6 +647,10 @@ def start(self, request):
647647
self._start_compression(request)
648648

649649
if self._chunked:
650+
if request.version != HttpVersion11:
651+
raise RuntimeError("Using chunked encoding is forbidden "
652+
"for HTTP/{0.major}.{0.minor}".format(
653+
request.version))
650654
resp_impl.enable_chunked_encoding()
651655
if self._chunk_size:
652656
resp_impl.add_chunking_filter(self._chunk_size)

docs/web_reference.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ StreamResponse
429429

430430
.. versionadded:: 0.14
431431

432+
.. warning:: chunked encoding can be enabled for ``HTTP/1.1`` only.
433+
434+
Setting up both :attr:`content_length` and chunked
435+
encoding is mutually exclusive.
436+
432437
.. seealso:: :attr:`chunked`
433438

434439
.. attribute:: headers

tests/test_web_response.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def setUp(self):
1818
def tearDown(self):
1919
self.loop.close()
2020

21-
def make_request(self, method, path, headers=CIMultiDict()):
22-
message = RawRequestMessage(method, path, HttpVersion11, headers,
21+
def make_request(self, method, path, headers=CIMultiDict(),
22+
version=HttpVersion11):
23+
message = RawRequestMessage(method, path, version, headers,
2324
False, False)
2425
return self.request_from_message(message)
2526

@@ -183,6 +184,16 @@ def test_chunk_size(self, ResponseImpl):
183184
msg.add_chunking_filter.assert_called_with(8192)
184185
self.assertIsNotNone(msg.filter)
185186

187+
def test_chunked_encoding_forbidden_for_http_10(self):
188+
req = self.make_request('GET', '/', version=HttpVersion10)
189+
resp = StreamResponse()
190+
resp.enable_chunked_encoding()
191+
192+
with self.assertRaisesRegex(
193+
RuntimeError,
194+
"Using chunked encoding is forbidden for HTTP/1.0"):
195+
resp.start(req)
196+
186197
@mock.patch('aiohttp.web_reqrep.ResponseImpl')
187198
def test_compression_no_accept(self, ResponseImpl):
188199
req = self.make_request('GET', '/')

0 commit comments

Comments
 (0)