2019-07-21 17:40:28 +00:00
|
|
|
from owrx.controllers import (
|
|
|
|
StatusController,
|
|
|
|
IndexController,
|
2019-09-18 15:22:35 +00:00
|
|
|
OwrxAssetsController,
|
2019-07-21 17:40:28 +00:00
|
|
|
WebSocketController,
|
|
|
|
MapController,
|
|
|
|
FeatureController,
|
|
|
|
ApiController,
|
2019-08-04 16:36:03 +00:00
|
|
|
MetricsController,
|
2019-09-18 15:22:35 +00:00
|
|
|
AprsSymbolsController,
|
2019-07-21 17:40:28 +00:00
|
|
|
)
|
2019-05-03 20:59:24 +00:00
|
|
|
from http.server import BaseHTTPRequestHandler
|
2019-07-06 11:03:49 +00:00
|
|
|
from urllib.parse import urlparse, parse_qs
|
2019-09-21 11:49:37 +00:00
|
|
|
import re
|
2019-05-03 20:59:24 +00:00
|
|
|
|
2019-05-10 19:50:58 +00:00
|
|
|
import logging
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 19:50:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-09-22 18:51:33 +00:00
|
|
|
logger.setLevel(logging.INFO)
|
2019-05-10 19:50:58 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-03 20:59:24 +00:00
|
|
|
class RequestHandler(BaseHTTPRequestHandler):
|
|
|
|
def __init__(self, request, client_address, server):
|
|
|
|
self.router = Router()
|
|
|
|
super().__init__(request, client_address, server)
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-09-22 18:51:33 +00:00
|
|
|
def log_message(self, format, *args):
|
2019-09-23 01:15:24 +00:00
|
|
|
logger.debug("%s - - [%s] %s", self.address_string(), self.log_date_time_string(), format % args)
|
2019-09-22 18:51:33 +00:00
|
|
|
|
2019-05-03 20:59:24 +00:00
|
|
|
def do_GET(self):
|
|
|
|
self.router.route(self)
|
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-07-06 11:03:49 +00:00
|
|
|
class Request(object):
|
2019-07-21 17:40:28 +00:00
|
|
|
def __init__(self, query=None, matches=None):
|
2019-07-06 11:03:49 +00:00
|
|
|
self.query = query
|
|
|
|
self.matches = matches
|
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-03 20:59:24 +00:00
|
|
|
class Router(object):
|
|
|
|
mappings = [
|
|
|
|
{"route": "/", "controller": IndexController},
|
|
|
|
{"route": "/status", "controller": StatusController},
|
2019-09-18 15:22:35 +00:00
|
|
|
{"regex": "/static/(.+)", "controller": OwrxAssetsController},
|
|
|
|
{"regex": "/aprs-symbols/(.+)", "controller": AprsSymbolsController},
|
2019-05-09 14:10:58 +00:00
|
|
|
{"route": "/ws/", "controller": WebSocketController},
|
2019-09-18 15:22:35 +00:00
|
|
|
{"regex": "(/favicon.ico)", "controller": OwrxAssetsController},
|
2019-06-07 18:23:31 +00:00
|
|
|
# backwards compatibility for the sdr.hu portal
|
2019-09-18 15:22:35 +00:00
|
|
|
{"regex": "/(gfx/openwebrx-avatar.png)", "controller": OwrxAssetsController},
|
2019-07-05 17:30:24 +00:00
|
|
|
{"route": "/map", "controller": MapController},
|
|
|
|
{"route": "/features", "controller": FeatureController},
|
2019-07-21 17:40:28 +00:00
|
|
|
{"route": "/api/features", "controller": ApiController},
|
2019-08-04 16:36:03 +00:00
|
|
|
{"route": "/metrics", "controller": MetricsController},
|
2019-05-03 20:59:24 +00:00
|
|
|
]
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-03 20:59:24 +00:00
|
|
|
def find_controller(self, path):
|
|
|
|
for m in Router.mappings:
|
|
|
|
if "route" in m:
|
|
|
|
if m["route"] == path:
|
|
|
|
return (m["controller"], None)
|
|
|
|
if "regex" in m:
|
|
|
|
regex = re.compile(m["regex"])
|
|
|
|
matches = regex.match(path)
|
|
|
|
if matches:
|
|
|
|
return (m["controller"], matches)
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-03 20:59:24 +00:00
|
|
|
def route(self, handler):
|
2019-07-06 11:03:49 +00:00
|
|
|
url = urlparse(handler.path)
|
|
|
|
res = self.find_controller(url.path)
|
2019-05-03 20:59:24 +00:00
|
|
|
if res is not None:
|
|
|
|
(controller, matches) = res
|
2019-07-06 11:03:49 +00:00
|
|
|
query = parse_qs(url.query)
|
|
|
|
request = Request(query, matches)
|
|
|
|
controller(handler, request).handle_request()
|
2019-05-03 20:59:24 +00:00
|
|
|
else:
|
|
|
|
handler.send_error(404, "Not Found", "The page you requested could not be found.")
|