httpserver: Fix circular dependency when servers.http.keepAlive is true.

This commit is contained in:
Valentin Lorentz 2020-05-09 22:17:28 +02:00
parent fd702a6a92
commit 5c5cad5851
1 changed files with 56 additions and 54 deletions

View File

@ -164,58 +164,6 @@ def get_template(filename):
with open(path + '.example', 'r') as fd:
return fd.read()
class RealSupyHTTPServer(HTTPServer):
# TODO: make this configurable
timeout = 0.5
running = False
def __init__(self, address, protocol, callback):
self.protocol = protocol
if protocol == 4:
self.address_family = socket.AF_INET
elif protocol == 6:
self.address_family = socket.AF_INET6
else:
raise AssertionError(protocol)
HTTPServer.__init__(self, address, callback)
self.callbacks = DEFAULT_CALLBACKS
def server_bind(self):
if self.protocol == 6:
v = conf.supybot.servers.http.singleStack()
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, v)
HTTPServer.server_bind(self)
def hook(self, subdir, callback):
if subdir in self.callbacks:
log.warning(('The HTTP subdirectory `%s` was already hooked but '
'has been claimed by another plugin (or maybe you '
'reloaded the plugin and it didn\'t properly unhook. '
'Forced unhook.') % subdir)
self.callbacks[subdir] = callback
callback.doHook(self, subdir)
def unhook(self, subdir):
callback = self.callbacks.pop(subdir, None)
if callback:
callback.doUnhook(self)
return callback
def __str__(self):
return 'server at %s %i' % self.server_address[0:2]
class TestSupyHTTPServer(RealSupyHTTPServer):
def __init__(self, *args, **kwargs):
self.callbacks = {}
def serve_forever(self, *args, **kwargs):
pass
def shutdown(self, *args, **kwargs):
pass
if world.testing:
SupyHTTPServer = TestSupyHTTPServer
else:
SupyHTTPServer = RealSupyHTTPServer
class SupyHTTPRequestHandler(BaseHTTPRequestHandler):
def do_X(self, callbackMethod, *args, **kwargs):
if self.path == '/':
@ -452,6 +400,62 @@ class SupyWellKnown(SupyHTTPServerCallback):
handler.send_response(404)
self.end_headers()
DEFAULT_CALLBACKS = {'.well-known': SupyWellKnown()}
class RealSupyHTTPServer(HTTPServer):
# TODO: make this configurable
timeout = 0.5
running = False
def __init__(self, address, protocol, callback):
self.protocol = protocol
if protocol == 4:
self.address_family = socket.AF_INET
elif protocol == 6:
self.address_family = socket.AF_INET6
else:
raise AssertionError(protocol)
HTTPServer.__init__(self, address, callback)
self.callbacks = DEFAULT_CALLBACKS.copy()
def server_bind(self):
if self.protocol == 6:
v = conf.supybot.servers.http.singleStack()
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, v)
HTTPServer.server_bind(self)
def hook(self, subdir, callback):
if subdir in self.callbacks:
log.warning(('The HTTP subdirectory `%s` was already hooked but '
'has been claimed by another plugin (or maybe you '
'reloaded the plugin and it didn\'t properly unhook. '
'Forced unhook.') % subdir)
self.callbacks[subdir] = callback
callback.doHook(self, subdir)
def unhook(self, subdir):
callback = self.callbacks.pop(subdir, None)
if callback:
callback.doUnhook(self)
return callback
def __str__(self):
return 'server at %s %i' % self.server_address[0:2]
class TestSupyHTTPServer(RealSupyHTTPServer):
def __init__(self, *args, **kwargs):
self.callbacks = {}
def serve_forever(self, *args, **kwargs):
pass
def shutdown(self, *args, **kwargs):
pass
if world.testing:
SupyHTTPServer = TestSupyHTTPServer
else:
SupyHTTPServer = RealSupyHTTPServer
http_servers = []
def startServer():
@ -499,5 +503,3 @@ def unhook(subdir):
and not configGroup.keepAlive():
server.shutdown()
http_servers.remove(server)
DEFAULT_CALLBACKS = {'.well-known': SupyWellKnown()}