changeset 810:3905bc24eb17

server: proper options handling
author Goffi <goffi@goffi.org>
date Fri, 18 Dec 2015 21:29:24 +0100
parents c500bdb0c216
children b1826adbeeff
files src/server/server.py src/twisted/plugins/libervia_server.py
diffstat 2 files changed, 26 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/server/server.py	Fri Dec 18 19:37:13 2015 +0100
+++ b/src/server/server.py	Fri Dec 18 21:29:24 2015 +0100
@@ -790,7 +790,7 @@
 
     def _loginAccount(self, request, new_account_domain):
         """Try to authenticate the user with the request information.
-        
+
         @param request: request of the register form
         @param new_account_domain (unicode): host corresponding to the local domain
         @return: a constant indicating the state:
@@ -991,13 +991,13 @@
 
     def __getSecurityWarning(self):
         """@return: a security warning message, or None if the connection is secure"""
-        if self.request.URLPath().scheme == 'https' or not self.sat_host.security_warning:
+        if self.request.URLPath().scheme == 'https' or not self.sat_host.options['security_warning']:
             return None
         text = "<p>" + D_("You are about to connect to an unsecure service.") + "</p><p>&nbsp;</p><p>"
 
-        if self.sat_host.connection_type == 'both':
-            new_port = (':%s' % self.sat_host.port_https_ext) if self.sat_host.port_https_ext != HTTPS_PORT else ''
-            url = "https://%s" % self.request.URLPath().netloc.replace(':%s' % self.sat_host.port, new_port)
+        if self.sat_host.options['connection_type'] == 'both':
+            new_port = (':%s' % self.sat_host.options['port_https_ext']) if self.sat_host.options['port_https_ext'] != HTTPS_PORT else ''
+            url = "https://%s" % self.request.URLPath().netloc.replace(':%s' % self.sat_host.options['port'], new_port)
             text += D_('Please read our %(faq_prefix)ssecurity notice%(faq_suffix)s regarding HTTPS') % {'faq_prefix': '<a href="http://salut-a-toi.org/faq.html#https" target="#">', 'faq_suffix': '</a>'}
             text += "</p><p>" + D_('and use the secure version of this website:')
             text += '</p><p>&nbsp;</p><p align="center"><a href="%(url)s">%(url)s</a>' % {'url': url}
@@ -1225,20 +1225,17 @@
 class Libervia(service.Service):
 
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, options):
+        self.options = options
         self.initialised = defer.Deferred()
 
-        # options managing
-        for opt in OPT_PARAMETERS_BOTH + OPT_PARAMETERS_CFG:
-            opt_name = opt[0]
-            setattr(self, opt_name, kwargs.get(opt_name, opt[2]))
-        if not self.port_https_ext:
-            self.port_https_ext = self.port_https
-        if self.data_dir == DATA_DIR_DEFAULT:
-            coerceDataDir(self.data_dir)  # this is not done when using the default value
+        if not self.options['port_https_ext']:
+            self.options['port_https_ext'] = self.options['port_https']
+        if self.options['data_dir'] == DATA_DIR_DEFAULT:
+            coerceDataDir(self.options['data_dir'])  # this is not done when using the default value
 
-        self.html_dir = os.path.join(self.data_dir, C.HTML_DIR)
-        self.themes_dir = os.path.join(self.data_dir, C.THEMES_DIR)
+        self.html_dir = os.path.join(self.options['data_dir'], C.HTML_DIR)
+        self.themes_dir = os.path.join(self.options['data_dir'], C.THEMES_DIR)
 
         self._cleanup = []
 
@@ -1339,7 +1336,7 @@
 
         def initOk(dummy):
             if not self.bridge.isConnected(C.SERVICE_PROFILE):
-                self.bridge.asyncConnect(C.SERVICE_PROFILE, self.passphrase,
+                self.bridge.asyncConnect(C.SERVICE_PROFILE, self.options['passphrase'],
                                          callback=self._startService, errback=eb)
             else:
                 self._startService()
@@ -1351,25 +1348,25 @@
         @raise IOError: the certificate file doesn't exist
         @raise OpenSSL.crypto.Error: the certificate file is invalid
         """
-        if self.connection_type in ('https', 'both'):
-            if not ssl_available:
-                raise(ImportError(_("Python module pyOpenSSL is not installed!")))
+        if self.options['connection_type'] in ('https', 'both'):
+            if ssl is None:
+                raise ImportError(u"Python module pyOpenSSL is not installed!")
             try:
-                with open(os.path.expanduser(self.ssl_certificate)) as keyAndCert:
+                with open(os.path.expanduser(self.options['ssl_certificate'])) as keyAndCert:
                     try:
                         cert = ssl.PrivateCertificate.loadPEM(keyAndCert.read())
                     except OpenSSL.crypto.Error as e:
-                        log.error(_(u"The file '%s' must contain both private and public parts of the certificate") % self.ssl_certificate)
+                        log.error(_(u"The file '%s' must contain both private and public parts of the certificate") % self.options['ssl_certificate'])
                         raise e
             except IOError as e:
-                log.error(_(u"The file '%s' doesn't exist") % self.ssl_certificate)
+                log.error(_(u"The file '%s' doesn't exist") % self.options['ssl_certificate'])
                 raise e
-            reactor.listenSSL(self.port_https, self.site, cert.options())
-        if self.connection_type in ('http', 'both'):
-            if self.connection_type == 'both' and self.redirect_to_https:
-                reactor.listenTCP(self.port, server.Site(RedirectToHTTPS(self.port, self.port_https_ext)))
+            reactor.listenSSL(self.options['port_https'], self.site, cert.options())
+        if self.options['connection_type'] in ('http', 'both'):
+            if self.options['connection_type'] == 'both' and self.options['redirect_to_https']:
+                reactor.listenTCP(self.options['port'], server.Site(RedirectToHTTPS(self.options['port'], self.options['port_https_ext'])))
             else:
-                reactor.listenTCP(self.port, self.site)
+                reactor.listenTCP(self.options['port'], self.site)
 
     def stopService(self):
         log.info(_("launching cleaning methods"))
--- a/src/twisted/plugins/libervia_server.py	Fri Dec 18 19:37:13 2015 +0100
+++ b/src/twisted/plugins/libervia_server.py	Fri Dec 18 21:29:24 2015 +0100
@@ -130,7 +130,7 @@
     def makeService(self, options):
         initialise(options.parent)
         from libervia.server import server
-        return server.Libervia(**dict(options))  # get rid of the usage.Option overload
+        return server.Libervia(options)
 
 
 # affectation to some variable is necessary for twisted introspection to work