Skip to content

Commit

Permalink
feat(wireless configuration): Fixed empty request causing crash
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchapp authored Sep 26, 2022
2 parents 9491adc + bd12cc9 commit 9e369aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 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
14 changes: 9 additions & 5 deletions firmware/lib/webpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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''' <input type="text" id="{key}" name="{key}" placeholder="{value}"><br><br>'''
return template.render_template(load_webpage("webpages/default.html"), {"render": render})
render += f""" <input type="text" id="{key}" name="{key}" placeholder="{value}"><br><br>"""
return template.render_template(
load_webpage("webpages/default.html"), {"render": render}
)


def config_route(*args, **kwargs):
Expand All @@ -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()

34 changes: 17 additions & 17 deletions firmware/lib/webrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ 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'
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 9e369aa

Please sign in to comment.