Skip to content

Commit 6dd8f07

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 1c8d928 commit 6dd8f07

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
@@ -1263,7 +1263,7 @@ def test_negative_content_length(self):
12631263
def test_malformed_content_length(self):
12641264
# RFC 9112: Content-Length = 1*DIGIT. Values that int() accepts but
12651265
# the grammar forbids must not be used to frame the body.
1266-
for value in ('+5', '5_0'):
1266+
for value in ('+5', '5_0', '5 0'):
12671267
with self.subTest(value=value):
12681268
sock = FakeSocket(
12691269
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\nHello\r\n' % value)
@@ -1273,6 +1273,22 @@ def test_malformed_content_length(self):
12731273
self.assertEqual(resp.read(), b'Hello\r\n')
12741274
resp.close()
12751275

1276+
def test_content_length_with_whitespace(self):
1277+
# RFC 9112, section 5.1: optional whitespace (SP / HTAB) around
1278+
# the field value is not part of the value, so it must not
1279+
# prevent the Content-Length from being used to frame the body.
1280+
for value in ('7 ', '7\t', '7 \t ', ' 7 '):
1281+
with self.subTest(value=value):
1282+
sock = FakeSocket(
1283+
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\n'
1284+
'Hello\r\nextra' % value)
1285+
resp = client.HTTPResponse(sock, method="GET")
1286+
resp.begin()
1287+
self.assertEqual(resp.length, 7)
1288+
self.assertEqual(resp.read(), b'Hello\r\n')
1289+
self.assertTrue(resp.isclosed())
1290+
resp.close()
1291+
12761292
def test_malformed_chunk_size(self):
12771293
# RFC 9112: chunk-size = 1*HEXDIG. Reject sizes that int(_, 16) accepts
12781294
# 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)