openwebrx-clone/owrx/http.py

115 lines
3.8 KiB
Python
Raw Normal View History

from owrx.controllers.status import StatusController
from owrx.controllers.template import (
IndexController,
MapController,
FeatureController
)
from owrx.controllers.assets import (
OwrxAssetsController,
AprsSymbolsController
)
from owrx.controllers.websocket import WebSocketController
from owrx.controllers.api import ApiController
from owrx.controllers.metrics import MetricsController
2020-02-23 18:23:18 +00:00
from owrx.controllers.settings import SettingsController
from owrx.controllers.session import SessionController
2019-05-03 20:59:24 +00:00
from http.server import BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import re
2020-02-23 16:53:02 +00:00
from abc import ABC, abstractmethod
2019-05-03 20:59:24 +00:00
2019-05-10 19:50:58 +00:00
import logging
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-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-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)
class Request(object):
2020-02-23 16:53:02 +00:00
def __init__(self, url):
self.path = url.path
self.query = parse_qs(url.query)
self.matches = None
def setMatches(self, matches):
self.matches = matches
2020-02-23 16:53:02 +00:00
class Route(ABC):
def __init__(self, controller, controllerOptions = None):
2020-02-23 16:53:02 +00:00
self.controller = controller
self.controllerOptions = controllerOptions if controllerOptions is not None else {}
2020-02-23 16:53:02 +00:00
@abstractmethod
def matches(self, request):
pass
class StaticRoute(Route):
def __init__(self, route, controller, controllerOptions = None):
2020-02-23 16:53:02 +00:00
self.route = route
super().__init__(controller, controllerOptions)
2020-02-23 16:53:02 +00:00
def matches(self, request):
return request.path == self.route
class RegexRoute(Route):
def __init__(self, regex, controller, controllerOptions = None):
2020-02-23 16:53:02 +00:00
self.regex = re.compile(regex)
super().__init__(controller, controllerOptions)
2020-02-23 16:53:02 +00:00
def matches(self, request):
matches = self.regex.match(request.path)
# this is probably not the cleanest way to do it...
request.setMatches(matches)
return matches is not None
2019-05-03 20:59:24 +00:00
class Router(object):
2020-02-23 16:53:02 +00:00
def __init__(self):
self.routes = [
StaticRoute("/", IndexController),
StaticRoute("/status", StatusController),
StaticRoute("/status.json", StatusController, {"action": "jsonAction"}),
2020-02-23 16:53:02 +00:00
RegexRoute("/static/(.+)", OwrxAssetsController),
RegexRoute("/aprs-symbols/(.+)", AprsSymbolsController),
StaticRoute("/ws/", WebSocketController),
RegexRoute("(/favicon.ico)", OwrxAssetsController),
# backwards compatibility for the sdr.hu portal
RegexRoute("(/gfx/openwebrx-avatar.png)", OwrxAssetsController),
StaticRoute("/map", MapController),
StaticRoute("/features", FeatureController),
StaticRoute("/api/features", ApiController),
StaticRoute("/metrics", MetricsController),
2020-02-23 18:23:18 +00:00
StaticRoute("/settings", SettingsController),
StaticRoute("/login", SessionController, {"action": "loginAction"}),
StaticRoute("/logout", SessionController, {"action": "logoutAction"}),
2020-02-23 16:53:02 +00:00
]
def find_route(self, request):
for r in self.routes:
if r.matches(request):
return r
2019-05-03 20:59:24 +00:00
def route(self, handler):
url = urlparse(handler.path)
2020-02-23 16:53:02 +00:00
request = Request(url)
route = self.find_route(request)
if route is not None:
controller = route.controller
controller(handler, request, route.controllerOptions).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.")