From 88c9a506abeb70008bc52e081cf5929735e370a3 Mon Sep 17 00:00:00 2001 From: Michael Schappacher Date: Sun, 25 Sep 2022 21:29:29 -0400 Subject: [PATCH 1/2] Reformatted --- firmware/lib/webpage.py | 14 +++++++++----- firmware/lib/webrouter.py | 7 +++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/firmware/lib/webpage.py b/firmware/lib/webpage.py index f717874..49fcd7d 100644 --- a/firmware/lib/webpage.py +++ b/firmware/lib/webpage.py @@ -15,12 +15,15 @@ def default_route(*args, **kwargs): {"ssid": "SSID:"}, {"password": "Password:"}, {"babybuddy": "BabyBuddy URL:"}, - {"babyauth": "BabyBuddy API Key"}] + {"babyauth": "BabyBuddy API Key"}, + ] render = "" for option in options: for key, value in option.items(): - render += f'''

''' - return template.render_template(load_webpage("webpages/default.html"), {"render": render}) + render += f"""

""" + return template.render_template( + load_webpage("webpages/default.html"), {"render": render} + ) def config_route(*args, **kwargs): @@ -37,7 +40,8 @@ def config_route(*args, **kwargs): if request.query_strings.get("babybuddy"): secret_json["BASE_URL"] = request.query_strings.get("babybuddy") if request.query_strings.get("babyauth"): - secret_json["AUTHORIZATION"]["Authorization"] = request.query_strings.get("babyauth") + secret_json["AUTHORIZATION"][ + "Authorization" + ] = request.query_strings.get("babyauth") secret.write(json.dumps(secret_json)) machine.reset() - diff --git a/firmware/lib/webrouter.py b/firmware/lib/webrouter.py index 66d8bdd..2ebfce4 100644 --- a/firmware/lib/webrouter.py +++ b/firmware/lib/webrouter.py @@ -37,19 +37,18 @@ def open_socket(ip, port): @staticmethod def determine_mimetype(path): mimetype = "text/html" - if path.endswith('.js'): + if path.endswith(".js"): mimetype = "text/javascript" - if path.endswith('.css'): + if path.endswith(".css"): mimetype = "text/css" - 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' + header = "HTTP/1.1 200 OK\n" mimetype = self.determine_mimetype(path) if path in self.routes.keys(): webpage = self.routes[path](request=request) From bd12cc93a89ae80bc564eb7b0b2d6a967ab34ee9 Mon Sep 17 00:00:00 2001 From: Michael Schappacher Date: Sun, 25 Sep 2022 22:10:33 -0400 Subject: [PATCH 2/2] 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()