diff --git a/CHANGES/7772.bugfix b/CHANGES/7772.bugfix new file mode 100644 index 00000000000..9fbd29237ee --- /dev/null +++ b/CHANGES/7772.bugfix @@ -0,0 +1 @@ +Fix CONNECT always being treated as having an empty body diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 27bcc641734..eaf6b3c3457 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -1069,7 +1069,7 @@ def method_must_be_empty_body(method: str) -> bool: """Check if a method must return an empty body.""" # https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.1 # https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.2 - return method.upper() in (hdrs.METH_CONNECT, hdrs.METH_HEAD) + return method.upper() == hdrs.METH_HEAD def status_code_must_be_empty_body(code: int) -> bool: diff --git a/tests/test_helpers.py b/tests/test_helpers.py index ae178ae15ec..65ac54f9f99 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -17,7 +17,11 @@ from yarl import URL from aiohttp import helpers -from aiohttp.helpers import is_expected_content_type, parse_http_date +from aiohttp.helpers import ( + is_expected_content_type, + method_must_be_empty_body, + parse_http_date, +) IS_PYPY = platform.python_implementation() == "PyPy" @@ -1071,3 +1075,10 @@ def test_read_basicauth_from_empty_netrc(): LookupError, match="No entry for example.com found in the `.netrc` file." ): helpers.basicauth_from_netrc(netrc_obj, "example.com") + + +def test_method_must_be_empty_body(): + """Test that HEAD is the only method that unequivocally must have an empty body.""" + assert method_must_be_empty_body("HEAD") is True + # CONNECT is only empty on a successful response + assert method_must_be_empty_body("CONNECT") is False