Commits

__russ__ committed 6d2e045

fixed get_ssl_adapter_class to work for builtin ssl in python 2.6+

  • Participants
  • Parent commits 8cf614a

Comments (0)

Files changed (1)

cheroot/test/__init__.py

-"""Regression test suite for Cheroot.
-
-Run 'nosetests -s test/' to exercise all tests.
-
-The '-s' flag instructs nose to output stdout messages, wihch is crucial to
-the 'interactive' mode of webtest.py. If you run these tests without the '-s'
-flag, don't be surprised if the test seems to hang: it's waiting for your
-interactive input.
-"""
-
-import os
 import sys
 
-def newexit():
-    os._exit(1)
+from cheroot._compat import basestring, py3k
 
-def setup():
-    # We want to monkey patch sys.exit so that we can get some
-    # information about where exit is being called.
-    newexit._old = sys.exit
-    sys.exit = newexit
+DEFAULT_BUFFER_SIZE = -1
 
-def teardown():
-    try:
-        sys.exit = sys.exit._old
-    except AttributeError:
-        sys.exit = sys._exit
+
+class SSLAdapter(object):
+    """Base class for SSL driver library adapters.
+    
+    Required methods:
+    
+        * ``wrap(sock) -> (wrapped socket, ssl environ dict)``
+        * ``makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE) -> socket file object``
+    """
+    
+    def __init__(self, certificate, private_key, certificate_chain=None):
+        self.certificate = certificate
+        self.private_key = private_key
+        self.certificate_chain = certificate_chain
+    
+    def wrap(self, sock):
+        raise NotImplemented
+    
+    def makefile(self, sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE):
+        raise NotImplemented
+
+
+
+# These may either be SSLAdapter subclasses or the string names
+# of such classes (in which case they will be lazily loaded).
+ssl_adapters = {
+    'builtin': 'cheroot.ssllib.ssl_builtin.BuiltinSSLAdapter',
+    'pyopenssl': 'cheroot.ssllib.ssl_pyopenssl.pyOpenSSLAdapter',
+    }
+
+def get_ssl_adapter_class(name=None):
+    """Return an SSL adapter class for the given name."""
+    if name is None:
+        if py3k or sys.version_info >= (2, 6):
+            name = 'builtin'
+        else:
+            name = 'pyopenssl'
+
+    adapter = ssl_adapters[name.lower()]
+    if isinstance(adapter, basestring):
+        last_dot = adapter.rfind(".")
+        attr_name = adapter[last_dot + 1:]
+        mod_path = adapter[:last_dot]
+        
+        try:
+            mod = sys.modules[mod_path]
+            if mod is None:
+                raise KeyError()
+        except KeyError:
+            # The last [''] is important.
+            mod = __import__(mod_path, globals(), locals(), [''])
+        
+        # Let an AttributeError propagate outward.
+        try:
+            adapter = getattr(mod, attr_name)
+        except AttributeError:
+            raise AttributeError("'%s' object has no attribute '%s'"
+                                 % (mod_path, attr_name))
+    
+    return adapter
+