Skip to content

bpo-22708: Fix http protocol version in CONNECT method, support IDN #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,10 @@ def set_debuglevel(self, level):
self.debuglevel = level

def _tunnel(self):
connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (self._tunnel_host,
self._tunnel_port)
connect_bytes = connect_str.encode("ascii")
connect_bytes = b'CONNECT %s:%d %s\r\n' % (
self._tunnel_host.encode('idna'),
self._tunnel_port,
self._http_vsn_str.encode('ascii'))
self.send(connect_bytes)
for header, value in self._tunnel_headers.items():
header_str = "%s: %s\r\n" % (header, value)
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,16 @@ def test_connect_with_tunnel(self):
# This test should be removed when CONNECT gets the HTTP/1.1 blessing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is now redundant and can be removed, but I don't see a reason to remove the test itself.

self.assertNotIn(b'Host: proxy.com', self.conn.sock.data)

def test_connect_with_tunnel_idna(self):
dest = '\u03b4\u03c0\u03b8.gr'
expected = 'CONNECT %s:%d HTTP/1.1\r\n'.encode('ascii') % (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not use b'CONNECT %s:%d HTTP/1.1\r\n' instead? We already know that "HTTP/1.1", dest.encode('idna') and client.HTTP_PORT don't contain any non-ASCII chars so there is no need to encode it ASCII.

dest.encode('idna'), client.HTTP_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(expected, self.conn.sock.data)

def test_connect_put_request(self):
self.conn.set_tunnel('destination.com')
self.conn.request('PUT', '/', '')
Expand Down