commit 4a66bb398ab80870134cc0619d2490fcf62ed47c Author: sidekek Date: Fri Oct 1 12:18:30 2021 -0400 init diff --git a/__pycache__/response.cpython-39.pyc b/__pycache__/response.cpython-39.pyc new file mode 100644 index 0000000..de92817 Binary files /dev/null and b/__pycache__/response.cpython-39.pyc differ diff --git a/body.html b/body.html new file mode 100644 index 0000000..1354ae2 --- /dev/null +++ b/body.html @@ -0,0 +1 @@ +< BLACK_MAN> yes, hello. black man here \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..21267c8 --- /dev/null +++ b/readme.txt @@ -0,0 +1,3 @@ +/technically/ standards-compliant http 1.1 server + +spec'd to RFC 2616 \ No newline at end of file diff --git a/response.py b/response.py new file mode 100644 index 0000000..c946b2a --- /dev/null +++ b/response.py @@ -0,0 +1,49 @@ +class status: + all_valid = { # https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html + 200: "OK",# add the rest l8r + 403: "Forbidden", + 404: "Not Found", + 500: "Internal Server Error", + 505: "HTTP Version not supported" + } + def __init__(self): + pass + @staticmethod + def add_status(status_line, code): + status_line.append(str(code)+" "+status.all_valid[code]) +class headers: + def __init__(self): + self.headers_dict = {} + def as_str(self): + ret = "" + for h in self.headers_dict: + ret += h+": "+str(self.headers_dict[h]) + ret += "\r\n" + return ret +class response: + def __init__(self): + self.status_line = [] + self.headers_obj = headers() + self.body = "" + def text(self): + return " ".join(self.status_line)+"\r\n"+self.headers_obj.as_str()+"\r\n"+self.body + @staticmethod + def build(query): + r = response() + + r.status_line.append("HTTP/1.1") + # check if query is heckin cute % valid/returnable + # always 200 for static testing + status.add_status(r.status_line, 200) + + f = open(query, "r") + r.body = f.read() + f.close() + + # manual for now + r.headers_obj.headers_dict["Server"] = "shttpile" + r.headers_obj.headers_dict["Content-Type"] = "text/html" + r.headers_obj.headers_dict["Content-Length"] = len(r.body.encode("ascii")) + r.headers_obj.headers_dict["X-Clacks-Overhead"] = "GNU Terry Pratchett, Aaron Swartz, Norm Macdonald" + + return r diff --git a/server.py b/server.py new file mode 100644 index 0000000..61177dd --- /dev/null +++ b/server.py @@ -0,0 +1,13 @@ +import socket +import response +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(("0.0.0.0", 1337)) +s.listen() +while True: + c, c_add = s.accept() + print(c_add) + print(c.recv(1024).decode("ascii")) + r = response.response.build("body.html") + c.sendall(r.text().encode("ascii")) + c.close() +s.close()