]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-69152: Add _proxy_response_headers attribute to HTTPConnection (#26152)
authorAlexey Namyotkin <62434915+nametkin@users.noreply.github.com>
Fri, 5 May 2023 18:52:24 +0000 (21:52 +0300)
committerGitHub <noreply@github.com>
Fri, 5 May 2023 18:52:24 +0000 (18:52 +0000)
Add _proxy_response_headers attribute to HTTPConnection (#26152)

---------

Co-authored-by: Senthil Kumaran <senthil@python.org>
Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS.d/next/Library/2021-05-16-14-28-30.bpo-24964.Oa5Ie_.rst [new file with mode: 0644]

index 74f7bcb68fb6bcaa075cde51acfd81a8b8375136..50f2b4680769c8e280020312e22b0809c3d12997 100644 (file)
@@ -858,6 +858,7 @@ class HTTPConnection:
         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)
 
@@ -944,21 +945,16 @@ class HTTPConnection:
         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()
 
index 37f77fe0a320c701da6038ad228cd661696e7ea0..4b1d355f550b49bdb584ca1e98033ddb37d1e8c8 100644 (file)
@@ -2390,6 +2390,20 @@ class TunnelTests(TestCase):
         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
 
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 (file)
index 0000000..ba11367
--- /dev/null
@@ -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.