From bd12cc93a89ae80bc564eb7b0b2d6a967ab34ee9 Mon Sep 17 00:00:00 2001 From: Michael Schappacher Date: Sun, 25 Sep 2022 22:10:33 -0400 Subject: [PATCH] Fix issues with empty requests crashing program --- firmware/lib/request.py | 2 +- firmware/lib/webrouter.py | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/firmware/lib/request.py b/firmware/lib/request.py index 631f898..17c18eb 100644 --- a/firmware/lib/request.py +++ b/firmware/lib/request.py @@ -1,3 +1,4 @@ + class Request: def __init__(self, request): self.raw = request @@ -10,7 +11,6 @@ def __init__(self, request): self.parse_request() def parse_request(self): - print(self.raw) try: request, self.body = self.raw.split("\r\n\r\n") except ValueError: diff --git a/firmware/lib/webrouter.py b/firmware/lib/webrouter.py index 2ebfce4..ae43503 100644 --- a/firmware/lib/webrouter.py +++ b/firmware/lib/webrouter.py @@ -46,17 +46,18 @@ def serve(self): while True: client = self.socket.accept()[0] request = client.recv(1024) - request = Request(request.decode("utf-8")) - path = request.path - header = "HTTP/1.1 200 OK\n" - mimetype = self.determine_mimetype(path) - if path in self.routes.keys(): - webpage = self.routes[path](request=request) - elif path in self.static_files: - with open(f"{self.static_location}/{path}", "rb") as static: - webpage = static.read() - else: - webpage = self.routes["default"](request=request) - header += f"Content-Type: {mimetype}\n\n" - client.send(webpage) - client.close() + if request: + request = Request(request.decode("utf-8")) + path = request.path + header = "HTTP/1.1 200 OK\n" + mimetype = self.determine_mimetype(path) + if path in self.routes.keys(): + webpage = self.routes[path](request=request) + elif path in self.static_files: + with open(f"{self.static_location}/{path}", "rb") as static: + webpage = static.read() + else: + webpage = self.routes["default"](request=request) + header += f"Content-Type: {mimetype}\n\n" + client.sendall(webpage) + client.close()