simplified main dispatcher

This commit is contained in:
Hay1tsme 2023-02-18 23:12:40 -05:00
parent 9a43303880
commit 655d9dc530
3 changed files with 14 additions and 32 deletions

View File

@ -92,7 +92,7 @@ class AllnetServlet:
self.uri_registry[code] = (uri, host) self.uri_registry[code] = (uri, host)
self.logger.info(f"Allnet serving {len(self.uri_registry)} games on port {core_cfg.allnet.port}") self.logger.info(f"Allnet serving {len(self.uri_registry)} games on port {core_cfg.allnet.port}")
def handle_poweron(self, request: Request): def handle_poweron(self, request: Request, _: Dict):
request_ip = request.getClientAddress().host request_ip = request.getClientAddress().host
try: try:
req = AllnetPowerOnRequest(self.allnet_req_to_dict(request.content.getvalue())) req = AllnetPowerOnRequest(self.allnet_req_to_dict(request.content.getvalue()))
@ -148,7 +148,7 @@ class AllnetServlet:
return self.dict_to_http_form_string([vars(resp)]) return self.dict_to_http_form_string([vars(resp)])
def handle_dlorder(self, request: Request): def handle_dlorder(self, request: Request, _: Dict):
request_ip = request.getClientAddress().host request_ip = request.getClientAddress().host
try: try:
req = AllnetDownloadOrderRequest(self.billing_req_to_dict(request.content.getvalue())) req = AllnetDownloadOrderRequest(self.billing_req_to_dict(request.content.getvalue()))
@ -168,7 +168,7 @@ class AllnetServlet:
else: # TODO: Actual dlorder response else: # TODO: Actual dlorder response
return self.dict_to_http_form_string(vars(resp)) return self.dict_to_http_form_string(vars(resp))
def handle_billing_request(self, request: Request): def handle_billing_request(self, request: Request, _: Dict):
req_dict = self.billing_req_to_dict(request.content.getvalue()) req_dict = self.billing_req_to_dict(request.content.getvalue())
request_ip = request.getClientAddress() request_ip = request.getClientAddress()
if req_dict is None: if req_dict is None:

View File

@ -28,7 +28,7 @@ class MuchaServlet:
self.logger.setLevel(logging.INFO) self.logger.setLevel(logging.INFO)
coloredlogs.install(level=logging.INFO, logger=self.logger, fmt=log_fmt_str) coloredlogs.install(level=logging.INFO, logger=self.logger, fmt=log_fmt_str)
def handle_boardauth(self, request: Request) -> bytes: def handle_boardauth(self, request: Request, _: Dict) -> bytes:
req_dict = self.mucha_preprocess(request.content.getvalue()) req_dict = self.mucha_preprocess(request.content.getvalue())
if req_dict is None: if req_dict is None:
self.logger.error(f"Error processing mucha request {request.content.getvalue()}") self.logger.error(f"Error processing mucha request {request.content.getvalue()}")
@ -41,7 +41,7 @@ class MuchaServlet:
return self.mucha_postprocess(vars(resp)) return self.mucha_postprocess(vars(resp))
def handle_updatecheck(self, request: Request) -> bytes: def handle_updatecheck(self, request: Request, _: Dict) -> bytes:
req_dict = self.mucha_preprocess(request.content.getvalue()) req_dict = self.mucha_preprocess(request.content.getvalue())
if req_dict is None: if req_dict is None:
self.logger.error(f"Error processing mucha request {request.content.getvalue()}") self.logger.error(f"Error processing mucha request {request.content.getvalue()}")

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
from typing import Dict
import yaml import yaml
from os import path, mkdir, access, W_OK from os import path, mkdir, access, W_OK
from core import * from core import *
@ -36,44 +37,25 @@ class HttpDispatcher(resource.Resource):
if test is None: if test is None:
return b"" return b""
controller = getattr(self, test["controller"], None) return self.dispatch(test, request)
if controller is None:
return b""
handler = getattr(controller, test["action"], None)
if handler is None:
return b""
url_vars = test
url_vars.pop("controller")
url_vars.pop("action")
if len(url_vars) > 0:
ret = handler(request, url_vars)
else:
ret = handler(request)
if type(ret) == str:
return ret.encode()
elif type(ret) == bytes:
return ret
else:
return b""
def render_POST(self, request: Request) -> bytes: def render_POST(self, request: Request) -> bytes:
test = self.map_post.match(request.uri.decode()) test = self.map_post.match(request.uri.decode())
if test is None: if test is None:
return b"" return b""
controller = getattr(self, test["controller"], None) return self.dispatch(test, request)
def dispatch(self, matcher: Dict, request: Request) -> bytes:
controller = getattr(self, matcher["controller"], None)
if controller is None: if controller is None:
return b"" return b""
handler = getattr(controller, test["action"], None) handler = getattr(controller, matcher["action"], None)
if handler is None: if handler is None:
return b"" return b""
url_vars = test url_vars = matcher
url_vars.pop("controller") url_vars.pop("controller")
url_vars.pop("action") url_vars.pop("action")
ret = handler(request, url_vars) ret = handler(request, url_vars)