Skip to content

Commit 86952ce

Browse files
committed
pythonGH-103472: close response in HTTPConnection._tunnel
1 parent 52f96d3 commit 86952ce

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Lib/http/client.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -941,23 +941,26 @@ def _tunnel(self):
941941
del headers
942942

943943
response = self.response_class(self.sock, method=self._method)
944-
(version, code, message) = response._read_status()
944+
try:
945+
(version, code, message) = response._read_status()
945946

946-
if code != http.HTTPStatus.OK:
947-
self.close()
948-
raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
949-
while True:
950-
line = response.fp.readline(_MAXLINE + 1)
951-
if len(line) > _MAXLINE:
952-
raise LineTooLong("header line")
953-
if not line:
954-
# for sites which EOF without sending a trailer
955-
break
956-
if line in (b'\r\n', b'\n', b''):
957-
break
947+
if code != http.HTTPStatus.OK:
948+
self.close()
949+
raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
950+
while True:
951+
line = response.fp.readline(_MAXLINE + 1)
952+
if len(line) > _MAXLINE:
953+
raise LineTooLong("header line")
954+
if not line:
955+
# for sites which EOF without sending a trailer
956+
break
957+
if line in (b'\r\n', b'\n', b''):
958+
break
958959

959-
if self.debuglevel > 0:
960-
print('header:', line.decode())
960+
if self.debuglevel > 0:
961+
print('header:', line.decode())
962+
finally:
963+
response.close()
961964

962965
def connect(self):
963966
"""Connect to the host and port specified in __init__."""

0 commit comments

Comments
 (0)