mirror of https://github.com/python/cpython.git
Backporting the changes made in revision 72880 as fix for Issue1424152.
This commit is contained in:
parent
47ccf0cbba
commit
308681c405
|
@ -652,11 +652,18 @@ def __init__(self, host, port=None, strict=None,
|
||||||
self.__response = None
|
self.__response = None
|
||||||
self.__state = _CS_IDLE
|
self.__state = _CS_IDLE
|
||||||
self._method = None
|
self._method = None
|
||||||
|
self._tunnel_host = None
|
||||||
|
self._tunnel_port = None
|
||||||
|
|
||||||
self._set_hostport(host, port)
|
self._set_hostport(host, port)
|
||||||
if strict is not None:
|
if strict is not None:
|
||||||
self.strict = strict
|
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):
|
def _set_hostport(self, host, port):
|
||||||
if port is None:
|
if port is None:
|
||||||
i = host.rfind(':')
|
i = host.rfind(':')
|
||||||
|
@ -677,11 +684,30 @@ def _set_hostport(self, host, port):
|
||||||
def set_debuglevel(self, level):
|
def set_debuglevel(self, level):
|
||||||
self.debuglevel = 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):
|
def connect(self):
|
||||||
"""Connect to the host and port specified in __init__."""
|
"""Connect to the host and port specified in __init__."""
|
||||||
self.sock = socket.create_connection((self.host,self.port),
|
self.sock = socket.create_connection((self.host,self.port),
|
||||||
self.timeout)
|
self.timeout)
|
||||||
|
|
||||||
|
if self._tunnel_host:
|
||||||
|
self._tunnel()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Close the connection to the HTTP server."""
|
"""Close the connection to the HTTP server."""
|
||||||
if self.sock:
|
if self.sock:
|
||||||
|
@ -1070,6 +1096,9 @@ def connect(self):
|
||||||
"Connect to a host on a given (SSL) port."
|
"Connect to a host on a given (SSL) port."
|
||||||
|
|
||||||
sock = socket.create_connection((self.host, self.port), self.timeout)
|
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)
|
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
|
||||||
|
|
||||||
__all__.append("HTTPSConnection")
|
__all__.append("HTTPSConnection")
|
||||||
|
|
|
@ -943,6 +943,21 @@ def test_proxy(self):
|
||||||
self.assertEqual([(handlers[0], "http_open")],
|
self.assertEqual([(handlers[0], "http_open")],
|
||||||
[tup[0:2] for tup in o.calls])
|
[tup[0:2] for tup in o.calls])
|
||||||
|
|
||||||
|
def test_proxy_https(self):
|
||||||
|
o = OpenerDirector()
|
||||||
|
ph = urllib2.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='"'):
|
def test_basic_auth(self, quote_char='"'):
|
||||||
opener = OpenerDirector()
|
opener = OpenerDirector()
|
||||||
password_manager = MockPasswordManager()
|
password_manager = MockPasswordManager()
|
||||||
|
|
|
@ -192,6 +192,7 @@ def __init__(self, url, data=None, headers={},
|
||||||
# self.__r_type is what's left after doing the splittype
|
# self.__r_type is what's left after doing the splittype
|
||||||
self.host = None
|
self.host = None
|
||||||
self.port = None
|
self.port = None
|
||||||
|
self._tunnel_host = None
|
||||||
self.data = data
|
self.data = data
|
||||||
self.headers = {}
|
self.headers = {}
|
||||||
for key, value in headers.items():
|
for key, value in headers.items():
|
||||||
|
@ -252,8 +253,13 @@ def get_selector(self):
|
||||||
return self.__r_host
|
return self.__r_host
|
||||||
|
|
||||||
def set_proxy(self, host, type):
|
def set_proxy(self, host, type):
|
||||||
self.host, self.type = host, type
|
if self.type == 'https' and not self._tunnel_host:
|
||||||
self.__r_host = self.__original
|
self._tunnel_host = self.host
|
||||||
|
else:
|
||||||
|
self.type = type
|
||||||
|
self.__r_host = self.__original
|
||||||
|
|
||||||
|
self.host = host
|
||||||
|
|
||||||
def has_proxy(self):
|
def has_proxy(self):
|
||||||
return self.__r_host == self.__original
|
return self.__r_host == self.__original
|
||||||
|
@ -700,7 +706,7 @@ def proxy_open(self, req, proxy, type):
|
||||||
req.add_header('Proxy-authorization', 'Basic ' + creds)
|
req.add_header('Proxy-authorization', 'Basic ' + creds)
|
||||||
hostport = unquote(hostport)
|
hostport = unquote(hostport)
|
||||||
req.set_proxy(hostport, proxy_type)
|
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
|
# let other handlers take care of it
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
@ -1098,6 +1104,10 @@ def do_open(self, http_class, req):
|
||||||
headers["Connection"] = "close"
|
headers["Connection"] = "close"
|
||||||
headers = dict(
|
headers = dict(
|
||||||
(name.title(), val) for name, val in headers.items())
|
(name.title(), val) for name, val in headers.items())
|
||||||
|
|
||||||
|
if req._tunnel_host:
|
||||||
|
h._set_tunnel(req._tunnel_host)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
h.request(req.get_method(), req.get_selector(), req.data, headers)
|
h.request(req.get_method(), req.get_selector(), req.data, headers)
|
||||||
r = h.getresponse()
|
r = h.getresponse()
|
||||||
|
|
|
@ -67,6 +67,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #1424152: Fix for httplib, urllib2 to support SSL while working through
|
||||||
|
proxy. Original patch by Christopher Li, changes made by Senthil Kumaran.
|
||||||
|
|
||||||
- Issues #5155, 5313, 5331: multiprocessing.Process._bootstrap was
|
- Issues #5155, 5313, 5331: multiprocessing.Process._bootstrap was
|
||||||
unconditionally calling "os.close(sys.stdin.fileno())" resulting in file
|
unconditionally calling "os.close(sys.stdin.fileno())" resulting in file
|
||||||
descriptor errors
|
descriptor errors
|
||||||
|
|
Loading…
Reference in New Issue