From 32a8d8ae2a227f807f46f59ce3144e6e18d22a13 Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Mon, 5 Aug 2019 10:46:47 -0500 Subject: [PATCH] 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. --- lib/ansible/module_utils/urls.py | 51 +++++++++++++++++--------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 041d0829f99..af1cfa8c7ce 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -397,6 +397,7 @@ class NoSSLError(SSLValidationError): CustomHTTPSConnection = None CustomHTTPSHandler = None HTTPSClientAuthHandler = None +UnixHTTPSConnection = None if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler'): class CustomHTTPSConnection(httplib.HTTPSConnection): 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 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 -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 - - -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() + class UnixHTTPSConnection(httplib.HTTPSConnection): + def __init__(self, unix_socket): + self._unix_socket = unix_socket - def __call__(self, *args, **kwargs): - httplib.HTTPSConnection.__init__(self, *args, **kwargs) - return self + def connect(self): + # This method exists simply to ensure we monkeypatch + # 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):