mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Conditionally create the CustomHTTPSConnection class only if we have the required baseclasses.
Fixes #11918
This commit is contained in:
parent
72a0654b81
commit
33863eb653
1 changed files with 35 additions and 31 deletions
|
@ -310,8 +310,11 @@ class NoSSLError(SSLValidationError):
|
||||||
"""Needed to connect to an HTTPS url but no ssl library available to verify the certificate"""
|
"""Needed to connect to an HTTPS url but no ssl library available to verify the certificate"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Some environments (Google Compute Engine's CoreOS deploys) do not compile
|
||||||
class CustomHTTPSConnection(httplib.HTTPSConnection):
|
# against openssl and thus do not have any HTTPS support.
|
||||||
|
CustomHTTPSConnection = CustomHTTPSHandler = None
|
||||||
|
if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib2, 'HTTPSHandler'):
|
||||||
|
class CustomHTTPSConnection(httplib.HTTPSConnection):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
httplib.HTTPSConnection.__init__(self, *args, **kwargs)
|
httplib.HTTPSConnection.__init__(self, *args, **kwargs)
|
||||||
if HAS_SSLCONTEXT:
|
if HAS_SSLCONTEXT:
|
||||||
|
@ -340,7 +343,7 @@ class CustomHTTPSConnection(httplib.HTTPSConnection):
|
||||||
else:
|
else:
|
||||||
self.sock = ssl.wrap_socket(sock, keyfile=self.key_file, certfile=self.cert_file, ssl_version=PROTOCOL)
|
self.sock = ssl.wrap_socket(sock, keyfile=self.key_file, certfile=self.cert_file, ssl_version=PROTOCOL)
|
||||||
|
|
||||||
class CustomHTTPSHandler(urllib2.HTTPSHandler):
|
class CustomHTTPSHandler(urllib2.HTTPSHandler):
|
||||||
|
|
||||||
def https_open(self, req):
|
def https_open(self, req):
|
||||||
return self.do_open(CustomHTTPSConnection, req)
|
return self.do_open(CustomHTTPSConnection, req)
|
||||||
|
@ -673,8 +676,9 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True,
|
||||||
handlers.append(proxyhandler)
|
handlers.append(proxyhandler)
|
||||||
|
|
||||||
# pre-2.6 versions of python cannot use the custom https
|
# pre-2.6 versions of python cannot use the custom https
|
||||||
# handler, since the socket class is lacking this method
|
# handler, since the socket class is lacking create_connection.
|
||||||
if hasattr(socket, 'create_connection'):
|
# Some python builds lack HTTPS support.
|
||||||
|
if hasattr(socket, 'create_connection') and CustomHTTPSHandler:
|
||||||
handlers.append(CustomHTTPSHandler)
|
handlers.append(CustomHTTPSHandler)
|
||||||
|
|
||||||
opener = urllib2.build_opener(*handlers)
|
opener = urllib2.build_opener(*handlers)
|
||||||
|
|
Loading…
Reference in a new issue