Basic HTTP server implemented as a process_request hook.
(connection, request)
| 74 | |
| 75 | |
| 76 | async def serve_html(connection, request): |
| 77 | """Basic HTTP server implemented as a process_request hook.""" |
| 78 | user = get_query_param(request.path, "user") |
| 79 | path = urllib.parse.urlparse(request.path).path |
| 80 | if path == "/": |
| 81 | if user is None: |
| 82 | page = "index.html" |
| 83 | else: |
| 84 | page = "test.html" |
| 85 | else: |
| 86 | page = path[1:] |
| 87 | |
| 88 | try: |
| 89 | template = pathlib.Path(__file__).with_name(page) |
| 90 | except ValueError: |
| 91 | pass |
| 92 | else: |
| 93 | if template.is_file(): |
| 94 | body = template.read_bytes() |
| 95 | if user is not None: |
| 96 | token = create_token(user) |
| 97 | body = body.replace(b"TOKEN", token.encode()) |
| 98 | headers = Headers( |
| 99 | { |
| 100 | "Date": email.utils.formatdate(usegmt=True), |
| 101 | "Connection": "close", |
| 102 | "Content-Length": str(len(body)), |
| 103 | "Content-Type": CONTENT_TYPES[template.suffix], |
| 104 | } |
| 105 | ) |
| 106 | return Response(200, "OK", headers, body) |
| 107 | |
| 108 | return connection.respond(http.HTTPStatus.NOT_FOUND, "Not found\n") |
| 109 | |
| 110 | |
| 111 | async def first_message_handler(websocket): |
nothing calls this directly
no test coverage detected
searching dependent graphs…