Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ef989ab

Browse files
nametkinorsenthil
authored andcommittedMay 8, 2023
pythongh-69152: Add _proxy_response_headers attribute to HTTPConnection (python#26152)
Add _proxy_response_headers attribute to HTTPConnection (python#26152) --------- Co-authored-by: Senthil Kumaran <senthil@python.org>
1 parent 21fb990 commit ef989ab

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed
 

‎Lib/http/client.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
858858
self._tunnel_host = None
859859
self._tunnel_port = None
860860
self._tunnel_headers = {}
861+
self._proxy_response_headers = None
861862

862863
(self.host, self.port) = self._get_hostport(host, port)
863864

@@ -944,21 +945,16 @@ def _tunnel(self):
944945
try:
945946
(version, code, message) = response._read_status()
946947

948+
self._proxy_response_headers = parse_headers(response.fp)
949+
950+
if self.debuglevel > 0:
951+
for hdr, val in self._proxy_response_headers.items():
952+
print("header:", hdr + ":", val)
953+
947954
if code != http.HTTPStatus.OK:
948955
self.close()
949956
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
959957

960-
if self.debuglevel > 0:
961-
print('header:', line.decode())
962958
finally:
963959
response.close()
964960

‎Lib/test/test_httplib.py

+14
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,20 @@ def test_tunnel_debuglog(self):
23902390
lines = output.getvalue().splitlines()
23912391
self.assertIn('header: {}'.format(expected_header), lines)
23922392

2393+
def test_proxy_response_headers(self):
2394+
expected_header = ('X-Dummy', '1')
2395+
response_text = (
2396+
'HTTP/1.0 200 OK\r\n'
2397+
'{0}\r\n\r\n'.format(':'.join(expected_header))
2398+
)
2399+
2400+
self.conn._create_connection = self._create_connection(response_text)
2401+
self.conn.set_tunnel('destination.com')
2402+
2403+
self.conn.request('PUT', '/', '')
2404+
headers = self.conn._proxy_response_headers
2405+
self.assertIn(expected_header, headers.items())
2406+
23932407
def test_tunnel_leak(self):
23942408
sock = None
23952409

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added attribute '_proxy_response_headers' to HTTPConnection class. This
2+
attribute contains the headers of the proxy server response to the CONNECT
3+
request.

0 commit comments

Comments
 (0)
Please sign in to comment.