Skip to content

Commit

Permalink
Fix issues with empty requests crashing program
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchapp committed Sep 26, 2022
1 parent 88c9a50 commit bd12cc9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion firmware/lib/request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

class Request:
def __init__(self, request):
self.raw = request
Expand All @@ -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:
Expand Down
29 changes: 15 additions & 14 deletions firmware/lib/webrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit bd12cc9

Please sign in to comment.