Hello,

The attached patch was made against svn revision 889.  It adds
support for https connections in `supervisor.xmlrpc`, along with related
tests in `supervisor.tests.test_xmlrpc`.

Thanks for considering!
~Dan
From d654dfe7f76549e263971ec5f92e854b437fa6a3 Mon Sep 17 00:00:00 2001
From: Dan Buch <[email protected]>
Date: Fri, 25 Sep 2009 20:48:15 -0400
Subject: [PATCH] supervisor/xmlrpc.py supervisor/tests/test_xmlrpc.py: added support for https:// transport protocol

---
 src/supervisor/tests/test_xmlrpc.py |   20 ++++++++++++++++++++
 src/supervisor/xmlrpc.py            |   32 ++++++++++++++++++++++----------
 2 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/src/supervisor/tests/test_xmlrpc.py b/src/supervisor/tests/test_xmlrpc.py
index 96c4448..f4489d9 100644
--- a/src/supervisor/tests/test_xmlrpc.py
+++ b/src/supervisor/tests/test_xmlrpc.py
@@ -162,6 +162,16 @@ class TesstSupervisorTransport(unittest.TestCase):
         self.assertEqual(conn.host, '127.0.0.1')
         self.assertEqual(conn.port, 9001)
 
+    def test__get_connection_https_9001(self):
+        import httplib
+        if not hasattr(httplib, 'HTTPSConnection'):
+            return
+        transport = self._makeOne('user', 'pass', 'https://127.0.0.1:9001/')
+        conn = transport._get_connection()
+        self.failUnless(isinstance(conn, httplib.HTTPSConnection))
+        self.assertEqual(conn.host, '127.0.0.1')
+        self.assertEqual(conn.port, 9001)
+
     def test__get_connection_http_80(self):
         import httplib
         transport = self._makeOne('user', 'pass', 'http://127.0.0.1/')
@@ -170,6 +180,16 @@ class TesstSupervisorTransport(unittest.TestCase):
         self.assertEqual(conn.host, '127.0.0.1')
         self.assertEqual(conn.port, 80)
 
+    def test__get_connection_https_443(self):
+        import httplib
+        if not hasattr(httplib, 'HTTPSConnection'):
+            return
+        transport = self._makeOne('user', 'pass', 'https://127.0.0.1/')
+        conn = transport._get_connection()
+        self.failUnless(isinstance(conn, httplib.HTTPSConnection))
+        self.assertEqual(conn.host, '127.0.0.1')
+        self.assertEqual(conn.port, 443)
+
     def test_request_non_200_response(self):
         import xmlrpclib
         transport = self._makeOne('user', 'pass', 'http://127.0.0.1/')
diff --git a/src/supervisor/xmlrpc.py b/src/supervisor/xmlrpc.py
index a604ea7..b5b1e45 100644
--- a/src/supervisor/xmlrpc.py
+++ b/src/supervisor/xmlrpc.py
@@ -422,16 +422,13 @@ class SupervisorTransport(xmlrpclib.Transport):
         self.verbose = False
         self.serverurl = serverurl
         if serverurl.startswith('http://'):
-            type, uri = urllib.splittype(serverurl)
-            host, path = urllib.splithost(uri)
-            host, port = urllib.splitport(host)
-            if port is None:
-                port = 80
-            else:
-                port = int(port)
-            def get_connection(host=host, port=port):
-                return httplib.HTTPConnection(host, port)
-            self._get_connection = get_connection
+            self._set_http_conn_getter(httplib.HTTPConnection)
+        elif serverurl.startswith('https://'):
+            if not hasattr(httplib, 'HTTPSConnection'):
+                raise ValueError('https protocol selected but the interpreter '
+                                 'at %s does not have ssl support'
+                                 % sys.executable)
+            self._set_http_conn_getter(httplib.HTTPSConnection)
         elif serverurl.startswith('unix://'):
             def get_connection(serverurl=serverurl):
                 # we use 'localhost' here because domain names must be
@@ -443,6 +440,21 @@ class SupervisorTransport(xmlrpclib.Transport):
         else:
             raise ValueError('Unknown protocol for serverurl %s' % serverurl)
 
+    def _set_http_conn_getter(self, http_conn_cls):
+        type, uri = urllib.splittype(self.serverurl)
+        host, path = urllib.splithost(uri)
+        host, port = urllib.splitport(host)
+        if port is None:
+            if http_conn_cls.__name__.lower().startswith('https'):
+                port = 443
+            else:
+                port = 80
+        else:
+            port = int(port)
+        def get_connection(host=host, port=port):
+            return http_conn_cls(host, port)
+        self._get_connection = get_connection
+
     def request(self, host, handler, request_body, verbose=0):
         if not self.connection:
             self.connection = self._get_connection()
-- 
1.6.0.4


From f8f7cb245b16e879f3815b47a2e4753c1eebe9f6 Mon Sep 17 00:00:00 2001
From: Dan Buch <[email protected]>
Date: Fri, 25 Sep 2009 20:52:33 -0400
Subject: [PATCH] supervisor/tests/test_xmlrpc.py: adding test to assert ValueError is raised when https:// schema specified and ssl is not available

---
 src/supervisor/tests/test_xmlrpc.py |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/src/supervisor/tests/test_xmlrpc.py b/src/supervisor/tests/test_xmlrpc.py
index f4489d9..aba0faf 100644
--- a/src/supervisor/tests/test_xmlrpc.py
+++ b/src/supervisor/tests/test_xmlrpc.py
@@ -190,6 +190,14 @@ class TesstSupervisorTransport(unittest.TestCase):
         self.assertEqual(conn.host, '127.0.0.1')
         self.assertEqual(conn.port, 443)
 
+    def test_https_schema_fails_when_httplib_lacks_ssl(self):
+        import httplib
+        https_cls = httplib.HTTPSConnection
+        httplib.__dict__.pop('HTTPSConnection')
+        self.assertRaises(ValueError, self._makeOne, 'user', 'pass',
+                          'https://127.0.0.1/')
+        httplib.HTTPSConnection = https_cls
+
     def test_request_non_200_response(self):
         import xmlrpclib
         transport = self._makeOne('user', 'pass', 'http://127.0.0.1/')
-- 
1.6.0.4

_______________________________________________
Supervisor-users mailing list
[email protected]
http://lists.supervisord.org/mailman/listinfo/supervisor-users

Reply via email to