Skip to content

Commit

Permalink
[3.7] No Content-Length header for 1xx 204 (#5119) (#5120)
Browse files Browse the repository at this point in the history
Backports the following commits to 3.7:
 - No Content-Length header for 1xx 204 (#5119)

Co-authored-by: Dmitry Erlikh <derlih@gmail.com>
  • Loading branch information
aio-libs-github-bot[bot] and derlih committed Oct 24, 2020
1 parent 77f17c7 commit 0353589
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/4901.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Server doesn't send Content-Length for 1xx or 204
4 changes: 4 additions & 0 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ async def _prepare_headers(self) -> None:
del headers[hdrs.CONTENT_LENGTH]
else:
keep_alive = False
# HTTP 1.1: https://tools.ietf.org/html/rfc7230#section-3.3.2
# HTTP 1.0: https://tools.ietf.org/html/rfc1945#section-10.4
elif version >= HttpVersion11 and self.status in (100, 101, 102, 103, 204):
del headers[hdrs.CONTENT_LENGTH]

headers.setdefault(hdrs.CONTENT_TYPE, "application/octet-stream")
headers.setdefault(hdrs.DATE, rfc822_formatted_time())
Expand Down
18 changes: 18 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import aiohttp
from aiohttp import FormData, HttpVersion10, HttpVersion11, TraceConfig, multipart, web
from aiohttp.hdrs import CONTENT_LENGTH, TRANSFER_ENCODING
from aiohttp.test_utils import make_mocked_coro

try:
Expand Down Expand Up @@ -1948,3 +1949,20 @@ async def handler(request):
resp = await client.post("/", data=b"data")
assert resp.status == 200
assert await resp.text() == "data (2, 4)"


@pytest.mark.parametrize(
"status", [101, 204],
)
async def test_response_101_204_no_content_length_http11(
status, aiohttp_client
) -> None:
async def handler(_):
return web.Response(status=status)

app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app, version="1.1")
resp = await client.get("/")
assert CONTENT_LENGTH not in resp.headers
assert TRANSFER_ENCODING not in resp.headers

0 comments on commit 0353589

Please sign in to comment.