From a9b201c1312fa5579b50f7fd75c2bef38db787d2 Mon Sep 17 00:00:00 2001 From: vo-va Date: Tue, 21 Mar 2017 00:18:38 +0600 Subject: [PATCH] Fix http protocol version in CONNECT method, support IDN Original author of this patch demianbrecht@gmail.com --- Lib/http/client.py | 7 ++++--- Lib/test/test_httplib.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Lib/http/client.py b/Lib/http/client.py index 0234199dfa9207..ffd35d7a82ad5e 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -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) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 68f6946a3a12b6..f57d4c7124597b 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -1877,6 +1877,16 @@ def test_connect_with_tunnel(self): # This test should be removed when CONNECT gets the HTTP/1.1 blessing 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') % ( + 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', '/', '')