diff options
author | Thomas Grainger <tagrain@gmail.com> | 2023-05-02 03:59:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-02 03:59:42 (GMT) |
commit | 9de0cf20fa0485e327e57cc0864c7476da85cfad (patch) | |
tree | a6f4c32b5d1bebbebecd337b63f4c2c91bea30c2 /Lib/http | |
parent | 690df4c16ca4f0054d27a6148da9e6af809a2658 (diff) | |
download | cpython-9de0cf20fa0485e327e57cc0864c7476da85cfad.zip cpython-9de0cf20fa0485e327e57cc0864c7476da85cfad.tar.gz cpython-9de0cf20fa0485e327e57cc0864c7476da85cfad.tar.bz2 |
GH-103472: close response in HTTPConnection._tunnel (#103473)
Avoid a potential `ResourceWarning` in `http.client.HTTPConnection`
by closing the proxy / tunnel's CONNECT response explicitly.
---------
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/http')
-rw-r--r-- | Lib/http/client.py | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/Lib/http/client.py b/Lib/http/client.py index 0f5cd35..74f7bcb 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -941,23 +941,26 @@ class HTTPConnection: del headers response = self.response_class(self.sock, method=self._method) - (version, code, message) = response._read_status() + try: + (version, code, message) = response._read_status() - if code != http.HTTPStatus.OK: - self.close() - raise OSError(f"Tunnel connection failed: {code} {message.strip()}") - while True: - line = response.fp.readline(_MAXLINE + 1) - if len(line) > _MAXLINE: - raise LineTooLong("header line") - if not line: - # for sites which EOF without sending a trailer - break - if line in (b'\r\n', b'\n', b''): - break + if code != http.HTTPStatus.OK: + self.close() + raise OSError(f"Tunnel connection failed: {code} {message.strip()}") + while True: + line = response.fp.readline(_MAXLINE + 1) + if len(line) > _MAXLINE: + raise LineTooLong("header line") + if not line: + # for sites which EOF without sending a trailer + break + if line in (b'\r\n', b'\n', b''): + break - if self.debuglevel > 0: - print('header:', line.decode()) + if self.debuglevel > 0: + print('header:', line.decode()) + finally: + response.close() def connect(self): """Connect to the host and port specified in __init__.""" |