diff options
author | Andrew Gaffney <andrew@agaffney.org> | 2019-08-05 10:46:47 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-05 10:46:47 -0500 |
commit | 32a8d8ae2a227f807f46f59ce3144e6e18d22a13 (patch) | |
tree | de3c93537cfb2b5777990ec11eba7c85e31b20c5 /lib/ansible/module_utils/urls.py | |
parent | df6b8d2a4a91e59d40bfb7c2b8b80bad48cd4585 (diff) | |
download | ansible-32a8d8ae2a227f807f46f59ce3144e6e18d22a13.tar.gz |
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.
Diffstat (limited to 'lib/ansible/module_utils/urls.py')
-rw-r--r-- | lib/ansible/module_utils/urls.py | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 041d0829f9..af1cfa8c7c 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): |