mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
gh-66897: Upgrade HTTP CONNECT to protocol HTTP/1.1 (#8305)
* bpo-22708: Upgrade HTTP CONNECT to protocol HTTP/1.1 (GH-NNNN) Use protocol HTTP/1.1 when sending HTTP CONNECT tunnelling requests; generate Host: headers if one is not already provided (required by HTTP/1.1), convert IDN domains to punycode in HTTP CONNECT requests. * Refactor tests to pass under -bb (fix ByteWarnings); missed some lines >80. * Use consistent 'tunnelling' spelling in Lib/http/client.py * Lib/test/test_httplib: Remove remnant of obsoleted test. * Use dict.copy() not copy.copy() * fix version changed * Update Lib/http/client.py Co-authored-by: bgehman <bgehman@users.noreply.github.com> * Switch to for/else: syntax, as suggested * Don't use for: else: * Sure, fine, w/e * Oops * 1nm to the left --------- Co-authored-by: Éric <merwok@netwok.org> Co-authored-by: bgehman <bgehman@users.noreply.github.com> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
This commit is contained in:
parent
a62ff97075
commit
1a8f862e32
5 changed files with 167 additions and 21 deletions
|
|
@ -2187,11 +2187,12 @@ def test_getting_header_defaultint(self):
|
|||
class TunnelTests(TestCase):
|
||||
def setUp(self):
|
||||
response_text = (
|
||||
'HTTP/1.0 200 OK\r\n\r\n' # Reply to CONNECT
|
||||
'HTTP/1.1 200 OK\r\n\r\n' # Reply to CONNECT
|
||||
'HTTP/1.1 200 OK\r\n' # Reply to HEAD
|
||||
'Content-Length: 42\r\n\r\n'
|
||||
)
|
||||
self.host = 'proxy.com'
|
||||
self.port = client.HTTP_PORT
|
||||
self.conn = client.HTTPConnection(self.host)
|
||||
self.conn._create_connection = self._create_connection(response_text)
|
||||
|
||||
|
|
@ -2203,15 +2204,45 @@ def create_connection(address, timeout=None, source_address=None):
|
|||
return FakeSocket(response_text, host=address[0], port=address[1])
|
||||
return create_connection
|
||||
|
||||
def test_set_tunnel_host_port_headers(self):
|
||||
def test_set_tunnel_host_port_headers_add_host_missing(self):
|
||||
tunnel_host = 'destination.com'
|
||||
tunnel_port = 8888
|
||||
tunnel_headers = {'User-Agent': 'Mozilla/5.0 (compatible, MSIE 11)'}
|
||||
tunnel_headers_after = tunnel_headers.copy()
|
||||
tunnel_headers_after['Host'] = '%s:%d' % (tunnel_host, tunnel_port)
|
||||
self.conn.set_tunnel(tunnel_host, port=tunnel_port,
|
||||
headers=tunnel_headers)
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertEqual(self.conn._tunnel_host, tunnel_host)
|
||||
self.assertEqual(self.conn._tunnel_port, tunnel_port)
|
||||
self.assertEqual(self.conn._tunnel_headers, tunnel_headers_after)
|
||||
|
||||
def test_set_tunnel_host_port_headers_set_host_identical(self):
|
||||
tunnel_host = 'destination.com'
|
||||
tunnel_port = 8888
|
||||
tunnel_headers = {'User-Agent': 'Mozilla/5.0 (compatible, MSIE 11)',
|
||||
'Host': '%s:%d' % (tunnel_host, tunnel_port)}
|
||||
self.conn.set_tunnel(tunnel_host, port=tunnel_port,
|
||||
headers=tunnel_headers)
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertEqual(self.conn._tunnel_host, tunnel_host)
|
||||
self.assertEqual(self.conn._tunnel_port, tunnel_port)
|
||||
self.assertEqual(self.conn._tunnel_headers, tunnel_headers)
|
||||
|
||||
def test_set_tunnel_host_port_headers_set_host_different(self):
|
||||
tunnel_host = 'destination.com'
|
||||
tunnel_port = 8888
|
||||
tunnel_headers = {'User-Agent': 'Mozilla/5.0 (compatible, MSIE 11)',
|
||||
'Host': '%s:%d' % ('example.com', 4200)}
|
||||
self.conn.set_tunnel(tunnel_host, port=tunnel_port,
|
||||
headers=tunnel_headers)
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertEqual(self.conn._tunnel_host, tunnel_host)
|
||||
self.assertEqual(self.conn._tunnel_port, tunnel_port)
|
||||
self.assertEqual(self.conn._tunnel_headers, tunnel_headers)
|
||||
|
|
@ -2223,17 +2254,96 @@ def test_disallow_set_tunnel_after_connect(self):
|
|||
'destination.com')
|
||||
|
||||
def test_connect_with_tunnel(self):
|
||||
self.conn.set_tunnel('destination.com')
|
||||
d = {
|
||||
b'host': b'destination.com',
|
||||
b'port': client.HTTP_PORT,
|
||||
}
|
||||
self.conn.set_tunnel(d[b'host'].decode('ascii'))
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertIn(b'CONNECT %(host)s:%(port)d HTTP/1.1\r\n'
|
||||
b'Host: %(host)s:%(port)d\r\n\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
self.assertIn(b'HEAD / HTTP/1.1\r\nHost: %(host)s\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
|
||||
def test_connect_with_tunnel_with_default_port(self):
|
||||
d = {
|
||||
b'host': b'destination.com',
|
||||
b'port': client.HTTP_PORT,
|
||||
}
|
||||
self.conn.set_tunnel(d[b'host'].decode('ascii'), port=d[b'port'])
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertIn(b'CONNECT %(host)s:%(port)d HTTP/1.1\r\n'
|
||||
b'Host: %(host)s:%(port)d\r\n\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
self.assertIn(b'HEAD / HTTP/1.1\r\nHost: %(host)s\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
|
||||
def test_connect_with_tunnel_with_nonstandard_port(self):
|
||||
d = {
|
||||
b'host': b'destination.com',
|
||||
b'port': 8888,
|
||||
}
|
||||
self.conn.set_tunnel(d[b'host'].decode('ascii'), port=d[b'port'])
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertIn(b'CONNECT %(host)s:%(port)d HTTP/1.1\r\n'
|
||||
b'Host: %(host)s:%(port)d\r\n\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
self.assertIn(b'HEAD / HTTP/1.1\r\nHost: %(host)s:%(port)d\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
|
||||
# This request is not RFC-valid, but it's been possible with the library
|
||||
# for years, so don't break it unexpectedly... This also tests
|
||||
# case-insensitivity when injecting Host: headers if they're missing.
|
||||
def test_connect_with_tunnel_with_different_host_header(self):
|
||||
d = {
|
||||
b'host': b'destination.com',
|
||||
b'tunnel_host_header': b'example.com:9876',
|
||||
b'port': client.HTTP_PORT,
|
||||
}
|
||||
self.conn.set_tunnel(
|
||||
d[b'host'].decode('ascii'),
|
||||
headers={'HOST': d[b'tunnel_host_header'].decode('ascii')})
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertIn(b'CONNECT %(host)s:%(port)d HTTP/1.1\r\n'
|
||||
b'HOST: %(tunnel_host_header)s\r\n\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
self.assertIn(b'HEAD / HTTP/1.1\r\nHost: %(host)s\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
|
||||
def test_connect_with_tunnel_different_host(self):
|
||||
d = {
|
||||
b'host': b'destination.com',
|
||||
b'port': client.HTTP_PORT,
|
||||
}
|
||||
self.conn.set_tunnel(d[b'host'].decode('ascii'))
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertIn(b'CONNECT %(host)s:%(port)d HTTP/1.1\r\n'
|
||||
b'Host: %(host)s:%(port)d\r\n\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
self.assertIn(b'HEAD / HTTP/1.1\r\nHost: %(host)s\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
|
||||
def test_connect_with_tunnel_idna(self):
|
||||
dest = '\u03b4\u03c0\u03b8.gr'
|
||||
dest_port = b'%s:%d' % (dest.encode('idna'), client.HTTP_PORT)
|
||||
expected = b'CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n' % (
|
||||
dest_port, dest_port)
|
||||
self.conn.set_tunnel(dest)
|
||||
self.conn.request('HEAD', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
|
||||
self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
|
||||
# issue22095
|
||||
self.assertNotIn(b'Host: destination.com:None', self.conn.sock.data)
|
||||
self.assertIn(b'Host: destination.com', self.conn.sock.data)
|
||||
|
||||
# This test should be removed when CONNECT gets the HTTP/1.1 blessing
|
||||
self.assertNotIn(b'Host: proxy.com', self.conn.sock.data)
|
||||
self.assertIn(expected, self.conn.sock.data)
|
||||
|
||||
def test_tunnel_connect_single_send_connection_setup(self):
|
||||
"""Regresstion test for https://bugs.python.org/issue43332."""
|
||||
|
|
@ -2253,12 +2363,19 @@ def test_tunnel_connect_single_send_connection_setup(self):
|
|||
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')
|
||||
|
||||
def test_connect_put_request(self):
|
||||
self.conn.set_tunnel('destination.com')
|
||||
d = {
|
||||
b'host': b'destination.com',
|
||||
b'port': client.HTTP_PORT,
|
||||
}
|
||||
self.conn.set_tunnel(d[b'host'].decode('ascii'))
|
||||
self.conn.request('PUT', '/', '')
|
||||
self.assertEqual(self.conn.sock.host, self.host)
|
||||
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
|
||||
self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
|
||||
self.assertIn(b'Host: destination.com', self.conn.sock.data)
|
||||
self.assertEqual(self.conn.sock.port, self.port)
|
||||
self.assertIn(b'CONNECT %(host)s:%(port)d HTTP/1.1\r\n'
|
||||
b'Host: %(host)s:%(port)d\r\n\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
self.assertIn(b'PUT / HTTP/1.1\r\nHost: %(host)s\r\n' % d,
|
||||
self.conn.sock.data)
|
||||
|
||||
def test_tunnel_debuglog(self):
|
||||
expected_header = 'X-Dummy: 1'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue