From 089964a5ebdcb82f5adee26d15c7a307890c345f Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 6 Jul 2019 13:03:49 +0200 Subject: [PATCH] query parameter support for the http module --- owrx/controllers.py | 6 +++--- owrx/http.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/owrx/controllers.py b/owrx/controllers.py index c4f917a..f979891 100644 --- a/owrx/controllers.py +++ b/owrx/controllers.py @@ -13,9 +13,9 @@ import logging logger = logging.getLogger(__name__) class Controller(object): - def __init__(self, handler, matches): + def __init__(self, handler, request): self.handler = handler - self.matches = matches + self.request = request def send_response(self, content, code = 200, content_type = "text/html", last_modified: datetime = None, max_age = None): self.handler.send_response(code) if content_type is not None: @@ -69,7 +69,7 @@ class AssetsController(Controller): except FileNotFoundError: self.send_response("file not found", code = 404) def handle_request(self): - filename = self.matches.group(1) + filename = self.request.matches.group(1) self.serve_file(filename) class IndexController(AssetsController): diff --git a/owrx/http.py b/owrx/http.py index 7e1f578..ce821b9 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -1,6 +1,7 @@ from owrx.controllers import StatusController, IndexController, AssetsController, WebSocketController, MapController, FeatureController, ApiController from http.server import BaseHTTPRequestHandler import re +from urllib.parse import urlparse, parse_qs import logging logger = logging.getLogger(__name__) @@ -12,6 +13,11 @@ class RequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.router.route(self) +class Request(object): + def __init__(self, query = None, matches = None): + self.query = query + self.matches = matches + class Router(object): mappings = [ {"route": "/", "controller": IndexController}, @@ -36,10 +42,13 @@ class Router(object): if matches: return (m["controller"], matches) def route(self, handler): - res = self.find_controller(handler.path) + url = urlparse(handler.path) + res = self.find_controller(url.path) if res is not None: (controller, matches) = res - logger.debug("path: {0}, controller: {1}, matches: {2}".format(handler.path, controller, matches)) - controller(handler, matches).handle_request() + query = parse_qs(url.query) + logger.debug("path: {0}, controller: {1}, query: {2}, matches: {3}".format(handler.path, controller, query, matches)) + request = Request(query, matches) + controller(handler, request).handle_request() else: handler.send_error(404, "Not Found", "The page you requested could not be found.")