Index: Misc/NEWS =================================================================== --- Misc/NEWS (revision 66111) +++ Misc/NEWS (working copy) @@ -63,6 +63,8 @@ Extension Modules ----------------- +- Issue #1424152: Changes in urllib2 to work with HTTPS over (Squid) Proxy + - Issue #3643: Added a few more checks to _testcapi to prevent segfaults by exploitation of poor argument checking. Index: Lib/http/client.py =================================================================== --- Lib/http/client.py (revision 66111) +++ Lib/http/client.py (working copy) @@ -616,11 +616,17 @@ 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(':') @@ -641,10 +647,24 @@ 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.""" @@ -950,6 +970,10 @@ "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) Index: Lib/urllib/request.py =================================================================== --- Lib/urllib/request.py (revision 66111) +++ Lib/urllib/request.py (working copy) @@ -168,6 +168,7 @@ # self.__r_type is what's left after doing the splittype self.host = None self.port = None + self._tunnel_host = None self.data = data self.headers = {} for key, value in headers.items(): @@ -228,9 +229,14 @@ return self.__r_host def set_proxy(self, host, type): - self.host, self.type = host, type - self.__r_host = self.__original + if self.type == 'https' and not self._tunnel_host: + self._tunnel_host = self.host + else: + self.type = type + self.__r_host = self.__original + self.host = host + def has_proxy(self): return self.__r_host == self.__original @@ -676,7 +682,7 @@ req.add_header('Proxy-authorization', 'Basic ' + creds) hostport = unquote(hostport) req.set_proxy(hostport, proxy_type) - if orig_type == proxy_type: + if orig_type == proxy_type or orig_type == 'https': # let other handlers take care of it return None else: @@ -1064,6 +1070,9 @@ headers["Connection"] = "close" headers = dict( (name.title(), val) for name, val in headers.items()) + + if req._tunnel_host: + h.set_tunnel(req._tunnel_host) try: h.request(req.get_method(), req.get_selector(), req.data, headers) r = h.getresponse() Index: Lib/test/test_urllib2.py =================================================================== --- Lib/test/test_urllib2.py (revision 66111) +++ Lib/test/test_urllib2.py (working copy) @@ -938,6 +938,23 @@ self.assertEqual([(handlers[0], "http_open")], [tup[0:2] for tup in o.calls]) + def test_proxy_https(self): + o = OpenerDirector() + ph = urllib.request.ProxyHandler(dict(https="proxy.example.com:3128")) + o.add_handler(ph) + meth_spec = [ + [("https_open", "return response")] + ] + handlers = add_ordered_mock_handlers(o, meth_spec) + + req = Request("https://www.example.com/") + self.assertEqual(req.get_host(), "www.example.com") + r = o.open(req) + self.assertEqual(req.get_host(), "proxy.example.com:3128") + + self.assertEqual([(handlers[0], "https_open")], + [tup[0:2] for tup in o.calls]) + def test_basic_auth(self, quote_char='"'): opener = OpenerDirector() password_manager = MockPasswordManager()