Skip to content

Commit

Permalink
Merge pull request #488 from KeepSafe/forbid_chunked_on_http10
Browse files Browse the repository at this point in the history
Fix #476: Raise RuntimeError when chunked enabled and HTTP is 1.0
  • Loading branch information
asvetlov committed Sep 4, 2015
2 parents e85a85d + 9412aa4 commit 9175145
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
CIMultiDict,
MultiDictProxy,
MultiDict)
from .protocol import Response as ResponseImpl, HttpVersion10
from .protocol import Response as ResponseImpl, HttpVersion10, HttpVersion11
from .streams import EOF_MARKER


Expand Down Expand Up @@ -647,6 +647,10 @@ def start(self, request):
self._start_compression(request)

if self._chunked:
if request.version != HttpVersion11:
raise RuntimeError("Using chunked encoding is forbidden "
"for HTTP/{0.major}.{0.minor}".format(
request.version))
resp_impl.enable_chunked_encoding()
if self._chunk_size:
resp_impl.add_chunking_filter(self._chunk_size)
Expand Down
5 changes: 5 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ StreamResponse

.. versionadded:: 0.14

.. warning:: chunked encoding can be enabled for ``HTTP/1.1`` only.

Setting up both :attr:`content_length` and chunked
encoding is mutually exclusive.

.. seealso:: :attr:`chunked`

.. attribute:: headers
Expand Down
15 changes: 13 additions & 2 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def setUp(self):
def tearDown(self):
self.loop.close()

def make_request(self, method, path, headers=CIMultiDict()):
message = RawRequestMessage(method, path, HttpVersion11, headers,
def make_request(self, method, path, headers=CIMultiDict(),
version=HttpVersion11):
message = RawRequestMessage(method, path, version, headers,
False, False)
return self.request_from_message(message)

Expand Down Expand Up @@ -183,6 +184,16 @@ def test_chunk_size(self, ResponseImpl):
msg.add_chunking_filter.assert_called_with(8192)
self.assertIsNotNone(msg.filter)

def test_chunked_encoding_forbidden_for_http_10(self):
req = self.make_request('GET', '/', version=HttpVersion10)
resp = StreamResponse()
resp.enable_chunked_encoding()

with self.assertRaisesRegex(
RuntimeError,
"Using chunked encoding is forbidden for HTTP/1.0"):
resp.start(req)

@mock.patch('aiohttp.web_reqrep.ResponseImpl')
def test_compression_no_accept(self, ResponseImpl):
req = self.make_request('GET', '/')
Expand Down

0 comments on commit 9175145

Please sign in to comment.