Skip to content

Commit

Permalink
pythongh-69152: Add _proxy_response_headers attribute to HTTPConnecti…
Browse files Browse the repository at this point in the history
…on (python#26152)

Add _proxy_response_headers attribute to HTTPConnection (python#26152)

---------

Co-authored-by: Senthil Kumaran <[email protected]>
  • Loading branch information
2 people authored and jbower-fb committed May 8, 2023
1 parent 21fb990 commit ef989ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
18 changes: 7 additions & 11 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -944,21 +945,16 @@ def _tunnel(self):
try:
(version, code, message) = response._read_status()

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()}")
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())
finally:
response.close()

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2390,6 +2390,20 @@ 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'
'{0}\r\n\r\n'.format(':'.join(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())

def test_tunnel_leak(self):
sock = None

Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ef989ab

Please sign in to comment.