From 02007252e81ca61b27f6a45b202f1c3c6d19576a Mon Sep 17 00:00:00 2001 From: Nametkin Date: Thu, 13 May 2021 17:20:40 +0300 Subject: [PATCH 1/5] add proxy_response_headers attr --- Lib/http/client.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Lib/http/client.py b/Lib/http/client.py index 0f5cd35247ae82..989359c51fa1d6 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -858,6 +858,7 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, self._tunnel_host = None self._tunnel_port = None self._tunnel_headers = {} + self._proxy_response_headers = None (self.host, self.port) = self._get_hostport(host, port) @@ -943,21 +944,11 @@ def _tunnel(self): response = self.response_class(self.sock, method=self._method) (version, code, message) = response._read_status() + self._proxy_response_headers = parse_headers(response.fp) + 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()) def connect(self): """Connect to the host and port specified in __init__.""" From 7d8cefc7fc94f7c98b837c60fd700a908ead3098 Mon Sep 17 00:00:00 2001 From: Nametkin Date: Thu, 13 May 2021 17:33:28 +0300 Subject: [PATCH 2/5] restore debug messages --- Lib/http/client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/http/client.py b/Lib/http/client.py index 989359c51fa1d6..fea5a7cf40457e 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -946,6 +946,10 @@ def _tunnel(self): self._proxy_response_headers = parse_headers(response.fp) + if self.debuglevel > 0: + for hdr, val in self._proxy_response_headers.items(): + print("header:", hdr + ":", val) + if code != http.HTTPStatus.OK: self.close() raise OSError(f"Tunnel connection failed: {code} {message.strip()}") From 367893d4b62d21f742256edbbea204a62a508c78 Mon Sep 17 00:00:00 2001 From: Nametkin Date: Sat, 15 May 2021 22:14:47 +0300 Subject: [PATCH 3/5] add_test --- Lib/test/test_httplib.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index b4f4e2b14351a6..983d2e05035dd4 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -2390,6 +2390,19 @@ def test_tunnel_debuglog(self): lines = output.getvalue().splitlines() self.assertIn('header: {}'.format(expected_header), lines) + def test_proxy_response_headers(self): + expected_header = ('X-Dummy', '1') + response_text = 'HTTP/1.0 200 OK\r\n{}: {}\r\n\r\n'.format( + *expected_header + ) + + self.conn._create_connection = self._create_connection(response_text) + self.conn.set_tunnel('destination.com') + + self.conn.request('PUT', '/', '') + headers = self.conn._proxy_response_headers + self.assertIn(expected_header, headers.items()) + if __name__ == '__main__': unittest.main(verbosity=2) From 48d91190884576ade1fedc586528bfbfba6f179a Mon Sep 17 00:00:00 2001 From: Nametkin Date: Sun, 16 May 2021 13:39:24 +0300 Subject: [PATCH 4/5] refactoring test --- Lib/test/test_httplib.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 983d2e05035dd4..00e3803f6e3282 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -2392,8 +2392,9 @@ def test_tunnel_debuglog(self): def test_proxy_response_headers(self): expected_header = ('X-Dummy', '1') - response_text = 'HTTP/1.0 200 OK\r\n{}: {}\r\n\r\n'.format( - *expected_header + response_text = ( + 'HTTP/1.0 200 OK\r\n' + '{0}\r\n\r\n'.format(':'.join(expected_header)) ) self.conn._create_connection = self._create_connection(response_text) From ec7f5778bc1ba782a9c549e0c4c67fa4b1a7645e Mon Sep 17 00:00:00 2001 From: Nametkin Date: Sun, 16 May 2021 14:29:30 +0300 Subject: [PATCH 5/5] add record to NEWS --- .../next/Library/2021-05-16-14-28-30.bpo-24964.Oa5Ie_.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-05-16-14-28-30.bpo-24964.Oa5Ie_.rst diff --git a/Misc/NEWS.d/next/Library/2021-05-16-14-28-30.bpo-24964.Oa5Ie_.rst b/Misc/NEWS.d/next/Library/2021-05-16-14-28-30.bpo-24964.Oa5Ie_.rst new file mode 100644 index 00000000000000..ba113673b7fbe5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-16-14-28-30.bpo-24964.Oa5Ie_.rst @@ -0,0 +1,3 @@ +Added attribute '_proxy_response_headers' to HTTPConnection class. This +attribute contains the headers of the proxy server response to the CONNECT +request.