Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -646,6 +646,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