diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2009-07-26 12:39:47 (GMT) |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2009-07-26 12:39:47 (GMT) |
commit | 0ac1f83079ee90f56bc7eef92643980a911bd355 (patch) | |
tree | 2ce2674de89d07f83132467448a9af38c0f4d29d /Lib/http | |
parent | 36613e08c3bec90e975eef9bdd3c9f2082a68616 (diff) | |
download | cpython-0ac1f83079ee90f56bc7eef92643980a911bd355.zip cpython-0ac1f83079ee90f56bc7eef92643980a911bd355.tar.gz cpython-0ac1f83079ee90f56bc7eef92643980a911bd355.tar.bz2 |
Backport the changes made in revision 74198, fixing the issue 1424152
Diffstat (limited to 'Lib/http')
-rw-r--r-- | Lib/http/client.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/http/client.py b/Lib/http/client.py index cef942f..f73cd9e 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -644,11 +644,17 @@ class HTTPConnection: self.__response = None self.__state = _CS_IDLE self._method = None + self._tunnel_host = None + self._tunnel_port = None self._set_hostport(host, port) if strict is not None: self.strict = strict + def _set_tunnel(self, host, port=None): + self._tunnel_host = host + self._tunnel_port = port + def _set_hostport(self, host, port): if port is None: i = host.rfind(':') @@ -669,10 +675,29 @@ class HTTPConnection: def set_debuglevel(self, level): self.debuglevel = level + def _tunnel(self): + self._set_hostport(self._tunnel_host, self._tunnel_port) + connect_str = "CONNECT %s:%d HTTP/1.0\r\n\r\n" %(self.host, self.port) + connect_bytes = connect_str.encode("ascii") + self.send(connect_bytes) + response = self.response_class(self.sock, strict = self.strict, + method= self._method) + (version, code, message) = response._read_status() + if code != 200: + self.close() + raise socket.error("Tunnel connection failed: %d %s" % (code, + message.strip())) + while True: + line = response.fp.readline() + if line == b'\r\n': + break + def connect(self): """Connect to the host and port specified in __init__.""" self.sock = socket.create_connection((self.host,self.port), self.timeout) + if self._tunnel_host: + self._tunnel() def close(self): """Close the connection to the HTTP server.""" @@ -1008,6 +1033,11 @@ else: sock = socket.create_connection((self.host, self.port), self.timeout) + + if self._tunnel_host: + self.sock = sock + self._tunnel() + self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file) |