Move definition of UnixHTTPSConnection behind guard (#60049)

This allows module_utils/urls.py to not immediately fall over when run
against a python without SSL support.
pull/60122/head
Andrew Gaffney 5 years ago committed by GitHub
parent df6b8d2a4a
commit 32a8d8ae2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -397,6 +397,7 @@ class NoSSLError(SSLValidationError):
CustomHTTPSConnection = None CustomHTTPSConnection = None
CustomHTTPSHandler = None CustomHTTPSHandler = None
HTTPSClientAuthHandler = None HTTPSClientAuthHandler = None
UnixHTTPSConnection = None
if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler'): if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler'):
class CustomHTTPSConnection(httplib.HTTPSConnection): class CustomHTTPSConnection(httplib.HTTPSConnection):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -478,32 +479,34 @@ if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler
return UnixHTTPSConnection(self._unix_socket)(host, **kwargs) return UnixHTTPSConnection(self._unix_socket)(host, **kwargs)
return httplib.HTTPSConnection(host, **kwargs) return httplib.HTTPSConnection(host, **kwargs)
@contextmanager
def unix_socket_patch_httpconnection_connect():
'''Monkey patch ``httplib.HTTPConnection.connect`` to be ``UnixHTTPConnection.connect``
so that when calling ``super(UnixHTTPSConnection, self).connect()`` we get the
correct behavior of creating self.sock for the unix socket
'''
_connect = httplib.HTTPConnection.connect
httplib.HTTPConnection.connect = UnixHTTPConnection.connect
yield
httplib.HTTPConnection.connect = _connect
@contextmanager class UnixHTTPSConnection(httplib.HTTPSConnection):
def unix_socket_patch_httpconnection_connect(): def __init__(self, unix_socket):
'''Monkey patch ``httplib.HTTPConnection.connect`` to be ``UnixHTTPConnection.connect`` self._unix_socket = unix_socket
so that when calling ``super(UnixHTTPSConnection, self).connect()`` we get the
correct behavior of creating self.sock for the unix socket
'''
_connect = httplib.HTTPConnection.connect
httplib.HTTPConnection.connect = UnixHTTPConnection.connect
yield
httplib.HTTPConnection.connect = _connect
class UnixHTTPSConnection(httplib.HTTPSConnection):
def __init__(self, unix_socket):
self._unix_socket = unix_socket
def connect(self):
# This method exists simply to ensure we monkeypatch
# httplib.HTTPConnection.connect to call UnixHTTPConnection.connect
with unix_socket_patch_httpconnection_connect():
super(UnixHTTPSConnection, self).connect()
def __call__(self, *args, **kwargs): def connect(self):
httplib.HTTPSConnection.__init__(self, *args, **kwargs) # This method exists simply to ensure we monkeypatch
return self # httplib.HTTPConnection.connect to call UnixHTTPConnection.connect
with unix_socket_patch_httpconnection_connect():
# Disable pylint check for the super() call. It complains about UnixHTTPSConnection
# being a NoneType because of the initial definition above, but it won't actually
# be a NoneType when this code runs
# pylint: disable=bad-super-call
super(UnixHTTPSConnection, self).connect()
def __call__(self, *args, **kwargs):
httplib.HTTPSConnection.__init__(self, *args, **kwargs)
return self
class UnixHTTPConnection(httplib.HTTPConnection): class UnixHTTPConnection(httplib.HTTPConnection):

Loading…
Cancel
Save