Patchwork [3,of,3] hgweb: pass ui into preparehttpserver

login
register
mail settings
Submitter Gregory Szorc
Date July 12, 2016, 10:32 p.m.
Message ID <a4a5217e826490e3e372.1468362747@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/15813/
State Superseded
Headers show

Comments

Gregory Szorc - July 12, 2016, 10:32 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1468360640 25200
#      Tue Jul 12 14:57:20 2016 -0700
# Node ID a4a5217e826490e3e37206133dc35a2b090668fd
# Parent  a9a5f1ca8f96a1292cf90f55e666733285f7b0f0
hgweb: pass ui into preparehttpserver

Upcoming patches will need it so they can use sslutil.wrapsocket().

Patch

diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py
--- a/mercurial/hgweb/server.py
+++ b/mercurial/hgweb/server.py
@@ -53,17 +53,17 @@  class _error_logger(object):
         for msg in seq:
             self.handler.log_error("HG error:  %s", msg)
 
 class _httprequesthandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
     url_scheme = 'http'
 
     @staticmethod
-    def preparehttpserver(httpserver, ssl_cert):
+    def preparehttpserver(httpserver, ui):
         """Prepare .socket of new HTTPServer instance"""
         pass
 
     def __init__(self, *args, **kargs):
         self.protocol_version = 'HTTP/1.1'
         BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args, **kargs)
 
     def _log_any(self, fp, format, *args):
@@ -217,25 +217,27 @@  class _httprequesthandler(BaseHTTPServer
             self.wfile.flush()
 
 class _httprequesthandlerssl(_httprequesthandler):
     """HTTPS handler based on Python's ssl module"""
 
     url_scheme = 'https'
 
     @staticmethod
-    def preparehttpserver(httpserver, ssl_cert):
+    def preparehttpserver(httpserver, ui):
         try:
             import ssl
             ssl.wrap_socket
         except ImportError:
             raise error.Abort(_("SSL support is unavailable"))
+
+        certfile = ui.config('web', 'certificate')
         httpserver.socket = ssl.wrap_socket(
             httpserver.socket, server_side=True,
-            certfile=ssl_cert, ssl_version=ssl.PROTOCOL_TLSv1)
+            certfile=certfile, ssl_version=ssl.PROTOCOL_TLSv1)
 
     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:
     import threading
@@ -259,17 +261,17 @@  class MercurialHTTPServer(object, _mixin
     if os.name == 'nt':
         allow_reuse_address = 0
 
     def __init__(self, ui, app, addr, handler, **kwargs):
         BaseHTTPServer.HTTPServer.__init__(self, addr, handler, **kwargs)
         self.daemon_threads = True
         self.application = app
 
-        handler.preparehttpserver(self, ui.config('web', 'certificate'))
+        handler.preparehttpserver(self, ui)
 
         prefix = ui.config('web', 'prefix', '')
         if prefix:
             prefix = '/' + prefix.strip('/')
         self.prefix = prefix
 
         alog = openlog(ui.config('web', 'accesslog', '-'), sys.stdout)
         elog = openlog(ui.config('web', 'errorlog', '-'), sys.stderr)
diff --git a/tests/test-https.t b/tests/test-https.t
--- a/tests/test-https.t
+++ b/tests/test-https.t
@@ -399,22 +399,23 @@  Test https with cert problems through pr
 
 Start patched hgweb that requires client certificates:
 
   $ cat << EOT > reqclientcert.py
   > import ssl
   > from mercurial.hgweb import server
   > class _httprequesthandlersslclientcert(server._httprequesthandlerssl):
   >     @staticmethod
-  >     def preparehttpserver(httpserver, ssl_cert):
+  >     def preparehttpserver(httpserver, ui):
+  >         certfile = ui.config('web', 'certificate')
   >         sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
   >         sslcontext.verify_mode = ssl.CERT_REQUIRED
-  >         sslcontext.load_cert_chain(ssl_cert)
+  >         sslcontext.load_cert_chain(certfile)
   >         # verify clients by server certificate
-  >         sslcontext.load_verify_locations(ssl_cert)
+  >         sslcontext.load_verify_locations(certfile)
   >         httpserver.socket = sslcontext.wrap_socket(httpserver.socket,
   >                                                    server_side=True)
   > server._httprequesthandlerssl = _httprequesthandlersslclientcert
   > EOT
   $ cd test
   $ hg serve -p $HGPORT -d --pid-file=../hg0.pid --certificate=$PRIV \
   > --config extensions.reqclientcert=../reqclientcert.py
   $ cat ../hg0.pid >> $DAEMON_PIDS