summaryrefslogtreecommitdiffstats
path: root/Lib/httplib.py
diff options
context:
space:
mode:
authorSenthil Kumaran <orsenthil@gmail.com>2009-07-26 12:36:08 (GMT)
committerSenthil Kumaran <orsenthil@gmail.com>2009-07-26 12:36:08 (GMT)
commit308681c405177dd893384bb039854c08c88e852c (patch)
tree8299437012b95b64095856315257dc792586b347 /Lib/httplib.py
parent47ccf0cbbaa4797d9d32c51c70d51a711a8d5fe0 (diff)
downloadcpython-308681c405177dd893384bb039854c08c88e852c.zip
cpython-308681c405177dd893384bb039854c08c88e852c.tar.gz
cpython-308681c405177dd893384bb039854c08c88e852c.tar.bz2
Backporting the changes made in revision 72880 as fix for Issue1424152.
Diffstat (limited to 'Lib/httplib.py')
-rw-r--r--Lib/httplib.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/httplib.py b/Lib/httplib.py
index 20a9204..1f584ef 100644
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -652,11 +652,18 @@ 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):
+ """ Sets up the host and the port for the HTTP CONNECT Tunnelling."""
+ self._tunnel_host = host
+ self._tunnel_port = port
+
def _set_hostport(self, host, port):
if port is None:
i = host.rfind(':')
@@ -677,11 +684,30 @@ class HTTPConnection:
def set_debuglevel(self, level):
self.debuglevel = level
+ def _tunnel(self):
+ self._set_hostport(self._tunnel_host, self._tunnel_port)
+ self.send("CONNECT %s:%d HTTP/1.0\r\n\r\n" % (self.host, self.port))
+ 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 == '\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."""
if self.sock:
@@ -1070,6 +1096,9 @@ else:
"Connect to a host on a given (SSL) port."
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)
__all__.append("HTTPSConnection")