Don't overwrite attributes of classes

This commit is contained in:
Unrud 2018-08-28 16:19:49 +02:00
parent 6c12b13ec1
commit c08754cf92

View File

@ -205,31 +205,37 @@ def serve(configuration):
servers = {} servers = {}
if configuration.getboolean("server", "ssl"): if configuration.getboolean("server", "ssl"):
server_class = ParallelHTTPSServer server_class = ParallelHTTPSServer
server_class.certificate = configuration.get("server", "certificate") else:
server_class.key = configuration.get("server", "key") server_class = ParallelHTTPServer
server_class.certificate_authority = configuration.get(
class ServerCopy(server_class):
"""Copy, avoids overriding the original class attributes."""
ServerCopy.client_timeout = configuration.getint("server", "timeout")
ServerCopy.max_connections = configuration.getint(
"server", "max_connections")
if configuration.getboolean("server", "ssl"):
ServerCopy.certificate = configuration.get("server", "certificate")
ServerCopy.key = configuration.get("server", "key")
ServerCopy.certificate_authority = configuration.get(
"server", "certificate_authority") "server", "certificate_authority")
server_class.ciphers = configuration.get("server", "ciphers") ServerCopy.ciphers = configuration.get("server", "ciphers")
server_class.protocol = getattr( ServerCopy.protocol = getattr(
ssl, configuration.get("server", "protocol"), ssl.PROTOCOL_SSLv23) ssl, configuration.get("server", "protocol"), ssl.PROTOCOL_SSLv23)
# Test if the SSL files can be read # Test if the SSL files can be read
for name in ["certificate", "key"] + ( for name in ["certificate", "key"] + (
["certificate_authority"] ["certificate_authority"]
if server_class.certificate_authority else []): if ServerCopy.certificate_authority else []):
filename = getattr(server_class, name) filename = getattr(ServerCopy, name)
try: try:
open(filename, "r").close() open(filename, "r").close()
except OSError as e: except OSError as e:
raise RuntimeError("Failed to read SSL %s %r: %s" % raise RuntimeError("Failed to read SSL %s %r: %s" %
(name, filename, e)) from e (name, filename, e)) from e
else:
server_class = ParallelHTTPServer
server_class.client_timeout = configuration.getint("server", "timeout")
server_class.max_connections = configuration.getint(
"server", "max_connections")
class RequestHandlerCopy(RequestHandler):
"""Copy, avoids overriding the original class attributes."""
if not configuration.getboolean("server", "dns_lookup"): if not configuration.getboolean("server", "dns_lookup"):
RequestHandler.address_string = lambda self: self.client_address[0] RequestHandlerCopy.address_string = lambda self: self.client_address[0]
shutdown_program = False shutdown_program = False
@ -243,7 +249,7 @@ def serve(configuration):
application = Application(configuration) application = Application(configuration)
try: try:
server = wsgiref.simple_server.make_server( server = wsgiref.simple_server.make_server(
address, port, application, server_class, RequestHandler) address, port, application, ServerCopy, RequestHandlerCopy)
except OSError as e: except OSError as e:
raise RuntimeError( raise RuntimeError(
"Failed to start server %r: %s" % (host, e)) from e "Failed to start server %r: %s" % (host, e)) from e