Skip to content

Commit d714e8f

Browse files
danilablagorodniy37encukou
authored andcommitted
gh-150751: Ignore optional whitespace around Content-Length in http.client (GH-159156)
gh-150752 started validating the Content-Length value against the RFC 9112 grammar (1*DIGIT), but the compat32 header parser keeps trailing SP/HTAB in the field value, so "Content-Length: 5 " is now treated as a missing header. HTTPResponse then assumes that the connection will close and read() blocks until the server closes it, which never happens on a keep-alive connection. RFC 9112, section 5.1 says that optional whitespace around a field value is excluded by parsers. Strip SP/HTAB before the grammar check; values such as "+5", "5_0" and "5 0" are still rejected.
1 parent 7a4b8ae commit d714e8f

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎Lib/http/client.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ def begin(self):
388388
# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
389389
self.length = None
390390
length = self.headers.get("content-length")
391+
if length is not None:
392+
# RFC 9112, section 5.1: optional whitespace (SP / HTAB) around
393+
# the field value is not part of the value.
394+
length = length.strip(' \t')
391395
if length and not self.chunked and _is_legal_content_length(length):
392396
self.length = int(length)
393397
else:

‎Lib/test/test_httplib.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ def test_negative_content_length(self):
12661266
def test_malformed_content_length(self):
12671267
# RFC 9112: Content-Length = 1*DIGIT. Values that int() accepts but
12681268
# the grammar forbids must not be used to frame the body.
1269-
for value in ('+5', '5_0'):
1269+
for value in ('+5', '5_0', '5 0'):
12701270
with self.subTest(value=value):
12711271
sock = FakeSocket(
12721272
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\nHello\r\n' % value)
@@ -1276,6 +1276,22 @@ def test_malformed_content_length(self):
12761276
self.assertEqual(resp.read(), b'Hello\r\n')
12771277
resp.close()
12781278

1279+
def test_content_length_with_whitespace(self):
1280+
# RFC 9112, section 5.1: optional whitespace (SP / HTAB) around
1281+
# the field value is not part of the value, so it must not
1282+
# prevent the Content-Length from being used to frame the body.
1283+
for value in ('7 ', '7\t', '7 \t ', ' 7 '):
1284+
with self.subTest(value=value):
1285+
sock = FakeSocket(
1286+
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\n'
1287+
'Hello\r\nextra' % value)
1288+
resp = client.HTTPResponse(sock, method="GET")
1289+
resp.begin()
1290+
self.assertEqual(resp.length, 7)
1291+
self.assertEqual(resp.read(), b'Hello\r\n')
1292+
self.assertTrue(resp.isclosed())
1293+
resp.close()
1294+
12791295
def test_malformed_chunk_size(self):
12801296
# RFC 9112: chunk-size = 1*HEXDIG. Reject sizes that int(_, 16) accepts
12811297
# but the grammar forbids (a sign, an "0x" prefix, underscores or
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:mod:`http.client` now ignores optional whitespace around the
2+
``Content-Length`` header value when validating it, as required by
3+
:rfc:`9112`. Previously a value with trailing whitespace (for example
4+
``Content-Length: 5`` followed by a space) was treated as missing and the
5+
response body was read until the connection was closed.

0 commit comments

Comments
 (0)