Skip to content

Commit 9a1b068

Browse files
committed
Shorten the grammar comment and only rstrip the chunk-size line
1 parent ae7724a commit 9a1b068

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

‎Lib/http/client.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,8 @@
158158
# to prevent http header injection.
159159
_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
160160

161-
# RFC 9112: Content-Length = 1*DIGIT and chunk-size = 1*HEXDIG. int() is more
162-
# permissive (it accepts a leading sign, underscores, surrounding whitespace
163-
# and, in base 16, an "0x" prefix and non-ASCII digits), so the body-framing
164-
# values are matched against the grammar before being passed to int().
161+
# RFC 9112: Content-Length = 1*DIGIT and chunk-size = 1*HEXDIG.
162+
# int() is more permissive, so we match against the grammar before calling it.
165163
_is_legal_content_length = re.compile(r'[0-9]+').fullmatch
166164
_is_legal_chunk_size = re.compile(rb'[0-9a-fA-F]+').fullmatch
167165

@@ -551,7 +549,7 @@ def _read_next_chunk_size(self):
551549
i = line.find(b";")
552550
if i >= 0:
553551
line = line[:i] # strip chunk-extensions
554-
line = line.strip()
552+
line = line.rstrip()
555553
try:
556554
if not _is_legal_chunk_size(line):
557555
raise ValueError("invalid chunk size")

‎Lib/test/test_httplib.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,9 +1334,10 @@ def test_malformed_content_length(self):
13341334

13351335
def test_malformed_chunk_size(self):
13361336
# RFC 9112: chunk-size = 1*HEXDIG. Reject sizes that int(_, 16) accepts
1337-
# but the grammar forbids (a sign, an "0x" prefix or underscores).
1337+
# but the grammar forbids (a sign, an "0x" prefix, underscores or
1338+
# leading whitespace).
13381339
start = 'HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n'
1339-
for size in ('-5', '+5', '0x5', '1_f'):
1340+
for size in ('-5', '+5', '0x5', '1_f', ' 5'):
13401341
with self.subTest(size=size):
13411342
sock = FakeSocket(start + '%s\r\nHELLO\r\n0\r\n\r\n' % size)
13421343
resp = client.HTTPResponse(sock, method="GET")

0 commit comments

Comments
 (0)