From 4f0d4983ca16dd476bb8d5442659677641c21f0a Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Fri, 14 May 2021 18:36:30 +0200 Subject: [PATCH] introduce http timeout; single router instance --- owrx/http.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/owrx/http.py b/owrx/http.py index 643c89a..050e5ef 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -34,27 +34,6 @@ logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) -class RequestHandler(BaseHTTPRequestHandler): - def __init__(self, request, client_address, server): - self.router = Router() - super().__init__(request, client_address, server) - - def log_message(self, format, *args): - logger.debug("%s - - [%s] %s", self.address_string(), self.log_date_time_string(), format % args) - - def do_GET(self): - self.router.route(self, self._build_request("GET")) - - def do_POST(self): - self.router.route(self, self._build_request("POST")) - - def do_DELETE(self): - self.router.route(self, self._build_request("DELETE")) - - def _build_request(self, method): - return Request(self.path, method, self.headers) - - class Request(object): def __init__(self, url, method, headers): parsed_url = urlparse(url) @@ -195,3 +174,23 @@ class Router(object): controller(handler, request, route.controllerOptions).handle_request() else: handler.send_error(404, "Not Found", "The page you requested could not be found.") + + +class RequestHandler(BaseHTTPRequestHandler): + timeout = 30 + router = Router() + + def log_message(self, format, *args): + logger.debug("%s - - [%s] %s", self.address_string(), self.log_date_time_string(), format % args) + + def do_GET(self): + self.router.route(self, self._build_request("GET")) + + def do_POST(self): + self.router.route(self, self._build_request("POST")) + + def do_DELETE(self): + self.router.route(self, self._build_request("DELETE")) + + def _build_request(self, method): + return Request(self.path, method, self.headers)