Skip to content

Instantly share code, notes, and snippets.

@fisherds
Created December 16, 2024 19:45
Show Gist options
  • Save fisherds/cda89a51e621275d9caa32a1cabe0f3e to your computer and use it in GitHub Desktop.
Save fisherds/cda89a51e621275d9caa32a1cabe0f3e to your computer and use it in GitHub Desktop.
second http server TODOs
# Done: get the Request Method
request_method = request_line.split(' ')[0]
file_name = "." + request_line.split(' ')[1]
file_extension = file_name.split(".")[2]
# Done: read all Request Headers into a dictionary.
headers = {}
while True:
header_line = reader_from_browser.readline().decode("utf-8")
# print("header_line = ", header_line)
if(header_line == '\r\n'):
break
header_pair = header_line.split(": ")
headers[header_pair[0]] = header_pair[1]
# print(headers)
# if POST:
if request_method == "POST":
# print("This is a POST!")
post_body = reader_from_browser.read(int(headers["Content-Length"]))
print("Raw POST body:", post_body)
# DONE: 2. Decode the Request Body.
post_lines = post_body.decode("utf-8").split("\r\n")
# DONE: 3. Convert the Request Body into a dictionary.
form_fields = {}
for line in post_lines:
if(line == ""):
continue
print(" line:", line)
post_body_pair = line.split("=")
form_fields[post_body_pair[0]] = post_body_pair[1]
print("Form fields:", form_fields)
# TODO: 4. Decide what to do with the data.
if(form_fields["secret_passcode"] == "abc123"):
print("Secret passcode entered successfully")
file_name = "./secret.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment