From f6b4958148c7f26de0e11fa075aed690d1785b1a Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Wed, 13 Jan 2021 02:54:51 +0100 Subject: [PATCH] Serve static content via websocket --- graphit_controlpi/main.py | 2 +- graphit_controlpi/websocket.py | 35 +++++++++++++++++++++++++++++++--- setup.py | 2 +- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/graphit_controlpi/main.py b/graphit_controlpi/main.py index 32ebf10..1b01b7a 100644 --- a/graphit_controlpi/main.py +++ b/graphit_controlpi/main.py @@ -12,7 +12,7 @@ async def setup(): with open(sys.argv[1]) as json_data: conf = json.load(json_data) pins = await process_configuration(conf, out_queue) - await setup_websocket(pins, out_queue) + await setup_websocket(pins, out_queue, sys.argv[2]) if __name__ == '__main__': diff --git a/graphit_controlpi/websocket.py b/graphit_controlpi/websocket.py index 440994f..9d0ee8e 100644 --- a/graphit_controlpi/websocket.py +++ b/graphit_controlpi/websocket.py @@ -3,6 +3,7 @@ import socket import json import asyncio import websockets +from http import HTTPStatus async def process_command(command, pins, out_queue): @@ -50,10 +51,36 @@ async def handler(pins, out_queue, websocket, path): task.cancel() +async def process_request(sever_root, path, request_headers): + if 'Upgrade' in request_headers: + return + if path == '/': + path = '/index.html' + response_headers = [ + ('Server', 'graphit_controlpi websocket server'), + ('Connection', 'close'), + ] + full_path = os.path.realpath(os.path.join(sever_root, path[1:])) + if os.path.commonpath((sever_root, full_path)) != sever_root or \ + not os.path.exists(full_path) or not os.path.isfile(full_path): + return HTTPStatus.NOT_FOUND, [], b'404 NOT FOUND' + mime_type = 'application/octet-stream' + extension = full_path.split(".")[-1] + if extension == 'html': + mime_type = 'text/html' + elif extension == 'js': + mime_type = 'text/javascript' + elif extension == 'css': + mime_type = 'text/css' + response_headers.append(('Content-Type', mime_type)) + body = open(full_path, 'rb').read() + response_headers.append(('Content-Length', str(len(body)))) + return HTTPStatus.OK, response_headers, body + + def get_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: - # doesn't even have to be reachable s.connect(('10.255.255.255', 1)) ip = s.getsockname()[0] except Exception: @@ -63,8 +90,10 @@ def get_ip(): return ip -async def setup_websocket(pins, out_queue): +async def setup_websocket(pins, out_queue, server_root): parameterised_handler = functools.partial(handler, pins, out_queue) + parameterised_process_request = functools.partial(process_request, server_root) hostname = get_ip() - await websockets.serve(parameterised_handler, hostname, 80) + await websockets.serve(parameterised_handler, hostname, 80, + process_request=parameterised_process_request) print(f"Serving on ws://{hostname}:80") diff --git a/setup.py b/setup.py index cf9e697..6f4a328 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as readme_file: setuptools.setup( name="graphit-controlpi", - version="0.2.0", + version="0.2.1", author="Graph-IT GmbH", author_email="info@graph-it.com", description="Main Module for Machine Control Pi", -- 2.34.1