Skip to content

Commit 755e323

Browse files
authored
gh-161861: preserve http continue timeout after rejected request (#161862)
move the continue timeout assignment after the connection state check so a rejected putrequest call cannot modify the active request.
1 parent 2702684 commit 755e323

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎Lib/http/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,6 @@ def putrequest(self, method, url, skip_host=False,
12681268
"""
12691269
if continue_timeout <= 0.0:
12701270
raise ValueError("continue_timeout must be greater than 0")
1271-
self._continue_timeout = continue_timeout
12721271

12731272
# if a prior response has been completed, then forget about it.
12741273
if self.__response and self.__response.isclosed():
@@ -1294,6 +1293,7 @@ def putrequest(self, method, url, skip_host=False,
12941293
#
12951294
if self.__state == _CS_IDLE:
12961295
self.__state = _CS_REQ_STARTED
1296+
self._continue_timeout = continue_timeout
12971297
else:
12981298
raise CannotSendRequest(self.__state)
12991299

‎Lib/test/test_httplib.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,20 @@ def test_putrequest_continue_timeout(self):
25872587
self.assertIn(b'Body content', conn.sock.data)
25882588
self.assertEqual(conn.getresponse().code, 200)
25892589

2590+
def test_failed_putrequest_preserves_continue_timeout(self):
2591+
conn = FakeSocketHTTPConnection(
2592+
b'HTTP/1.1 100 Continue\r\n'
2593+
b'\r\n'
2594+
)
2595+
conn._select = mock.Mock(wraps=conn.fake_select)
2596+
conn.putrequest('PUT', '/', continue_timeout=0.25)
2597+
with self.assertRaises(client.CannotSendRequest):
2598+
conn.putrequest('GET', '/', continue_timeout=10)
2599+
conn.putheader('Expect', '100-continue')
2600+
conn.putheader('Content-Length', '12')
2601+
conn.endheaders(b'Body content')
2602+
conn._select.assert_called_once_with([conn.sock], [], [], 0.25)
2603+
25902604
def test_invalid_continue_timeout(self):
25912605
conn = FakeSocketHTTPConnection(b'')
25922606
for timeout in (0, -1):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Preserve the active request's ``continue_timeout`` when a second
2+
:meth:`~http.client.HTTPConnection.putrequest` call is rejected.

0 commit comments

Comments
 (0)