File no-WantReadError.patch of Package python-glanceclient

From 2f33f5f283d921ce1db595a89c36eefb8b8fcd29 Mon Sep 17 00:00:00 2001
From: Stuart McLaren <stuart.mclaren@hp.com>
Date: Wed, 20 Mar 2013 18:00:39 +0000
Subject: [PATCH] Prevent WantReadError when using https

If the glance client is instantiated when the socket module has been
monkey patched requests to the server can yield a WantReadError because
the socket has been set to non-blocking but this is not being handled
correctly. When this is the case use eventlet's GreenConnection which
handles non-blocking sockets correctly.

Also, for now, add a required getsockopt method to GreenConnection.
This can be removed once the eventlet fix
(https://bitbucket.org/eventlet/eventlet/commits/609f230) lands.

Fixes bug 1157864.

Change-Id: I187b69f75b8bcfe16facd41e69b1cd0490dae605
---
 glanceclient/common/http.py  | 25 ++++++++++++++++++++-----
 glanceclient/common/utils.py | 11 +++++++++++
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py
index 7146ace..76304b3 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 08e047b..30dcd58 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)
-- 
1.8.1.6
openSUSE Build Service is sponsored by