query parameter support for the http module
This commit is contained in:
parent
31b8dd4fd5
commit
089964a5eb
@ -13,9 +13,9 @@ import logging
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Controller(object):
|
class Controller(object):
|
||||||
def __init__(self, handler, matches):
|
def __init__(self, handler, request):
|
||||||
self.handler = handler
|
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):
|
def send_response(self, content, code = 200, content_type = "text/html", last_modified: datetime = None, max_age = None):
|
||||||
self.handler.send_response(code)
|
self.handler.send_response(code)
|
||||||
if content_type is not None:
|
if content_type is not None:
|
||||||
@ -69,7 +69,7 @@ class AssetsController(Controller):
|
|||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
self.send_response("file not found", code = 404)
|
self.send_response("file not found", code = 404)
|
||||||
def handle_request(self):
|
def handle_request(self):
|
||||||
filename = self.matches.group(1)
|
filename = self.request.matches.group(1)
|
||||||
self.serve_file(filename)
|
self.serve_file(filename)
|
||||||
|
|
||||||
class IndexController(AssetsController):
|
class IndexController(AssetsController):
|
||||||
|
15
owrx/http.py
15
owrx/http.py
@ -1,6 +1,7 @@
|
|||||||
from owrx.controllers import StatusController, IndexController, AssetsController, WebSocketController, MapController, FeatureController, ApiController
|
from owrx.controllers import StatusController, IndexController, AssetsController, WebSocketController, MapController, FeatureController, ApiController
|
||||||
from http.server import BaseHTTPRequestHandler
|
from http.server import BaseHTTPRequestHandler
|
||||||
import re
|
import re
|
||||||
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -12,6 +13,11 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
self.router.route(self)
|
self.router.route(self)
|
||||||
|
|
||||||
|
class Request(object):
|
||||||
|
def __init__(self, query = None, matches = None):
|
||||||
|
self.query = query
|
||||||
|
self.matches = matches
|
||||||
|
|
||||||
class Router(object):
|
class Router(object):
|
||||||
mappings = [
|
mappings = [
|
||||||
{"route": "/", "controller": IndexController},
|
{"route": "/", "controller": IndexController},
|
||||||
@ -36,10 +42,13 @@ class Router(object):
|
|||||||
if matches:
|
if matches:
|
||||||
return (m["controller"], matches)
|
return (m["controller"], matches)
|
||||||
def route(self, handler):
|
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:
|
if res is not None:
|
||||||
(controller, matches) = res
|
(controller, matches) = res
|
||||||
logger.debug("path: {0}, controller: {1}, matches: {2}".format(handler.path, controller, matches))
|
query = parse_qs(url.query)
|
||||||
controller(handler, matches).handle_request()
|
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:
|
else:
|
||||||
handler.send_error(404, "Not Found", "The page you requested could not be found.")
|
handler.send_error(404, "Not Found", "The page you requested could not be found.")
|
||||||
|
Loading…
Reference in New Issue
Block a user