diff mercurial/hgweb/server.py @ 12784:763be3cd084a

hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later pyOpenSSL apparently doesn't work for Python 2.7 and isn't very actively maintained. The built-in ssl module seems like a long-term winner, so we now use that with Python 2.6 and higher.
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 20 Oct 2010 20:19:34 +0200
parents 191d0fd5c2fd
children 076bbbf0ba86
line wrap: on
line diff
--- a/mercurial/hgweb/server.py	Wed Oct 20 20:19:32 2010 +0200
+++ b/mercurial/hgweb/server.py	Wed Oct 20 20:19:34 2010 +0200
@@ -214,6 +214,26 @@
             self.close_connection = True
             pass
 
+class _httprequesthandlerssl(_httprequesthandler):
+    """HTTPS handler based on Pythons ssl module (introduced in 2.6)"""
+
+    url_scheme = 'https'
+
+    @staticmethod
+    def preparehttpserver(httpserver, ssl_cert):
+        try:
+            import ssl
+            ssl.wrap_socket
+        except ImportError:
+            raise util.Abort(_("SSL support is unavailable"))
+        httpserver.socket = ssl.wrap_socket(httpserver.socket, server_side=True,
+            certfile=ssl_cert, ssl_version=ssl.PROTOCOL_SSLv3)
+
+    def setup(self):
+        self.connection = self.request
+        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
+        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
+
 try:
     from threading import activeCount
     _mixin = SocketServer.ThreadingMixIn
@@ -265,7 +285,10 @@
 def create_server(ui, app):
 
     if ui.config('web', 'certificate'):
-        handler = _httprequesthandleropenssl
+        if sys.version_info >= (2, 6):
+            handler = _httprequesthandlerssl
+        else:
+            handler = _httprequesthandleropenssl
     else:
         handler = _httprequesthandler