diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index 7146aced6..76304b389 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -37,6 +37,21 @@ from glanceclient import exc from glanceclient.common import utils +try: + from eventlet import patcher + # Handle case where we are running in a monkey patched environment + if patcher.is_monkey_patched('socket'): + from eventlet.green.httplib import HTTPSConnection + from eventlet.green.OpenSSL.SSL import GreenConnection as Connection + from eventlet.greenio import GreenSocket + # TODO(mclaren): A getsockopt workaround: see 'getsockopt' doc string + GreenSocket.getsockopt = utils.getsockopt + else: + raise ImportError +except ImportError: + from httplib import HTTPSConnection + from OpenSSL.SSL import Connection as Connection + LOG = logging.getLogger(__name__) USER_AGENT = 'python-glanceclient' @@ -256,7 +271,7 @@ class OpenSSLConnectionDelegator(object): a delegator must be used. """ def __init__(self, *args, **kwargs): - self.connection = OpenSSL.SSL.Connection(*args, **kwargs) + self.connection = Connection(*args, **kwargs) def __getattr__(self, name): return getattr(self.connection, name) @@ -265,7 +280,7 @@ def makefile(self, *args, **kwargs): return socket._fileobject(self.connection, *args, **kwargs) -class VerifiedHTTPSConnection(httplib.HTTPSConnection): +class VerifiedHTTPSConnection(HTTPSConnection): """ Extended HTTPSConnection which uses the OpenSSL library for enhanced SSL support. @@ -275,9 +290,9 @@ class VerifiedHTTPSConnection(httplib.HTTPSConnection): def __init__(self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True): - httplib.HTTPSConnection.__init__(self, host, port, - key_file=key_file, - cert_file=cert_file) + HTTPSConnection.__init__(self, host, port, + key_file=key_file, + cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index 08e047b67..30dcd58b0 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -263,3 +263,14 @@ def ensure_str(text, incoming=None, return text.encode(encoding, errors) return text + + +def getsockopt(self, *args, **kwargs): + """ + A function which allows us to monkey patch eventlet's + GreenSocket, adding a required 'getsockopt' method. + TODO: (mclaren) we can remove this once the eventlet fix + (https://bitbucket.org/eventlet/eventlet/commits/609f230) + lands in mainstream packages. + """ + return self.fd.getsockopt(*args, **kwargs)