this is almost ready

This commit is contained in:
Matt Arnold 2025-09-06 08:21:18 -04:00
parent 30a9e0bedd
commit 649a301cf9

View File

@ -38,6 +38,9 @@ Fork = False
# client's code in a public project.
# Thus cut 'n' paste.
CRLF = "\r\n"
LF = "\n"
class AccessDict(dict):
def __init__(self, *args, **kwargs):
@ -83,6 +86,8 @@ class HttpRequest(AccessDict):
self["headers"] = headers
self["body"] = StringIO(body)
self["path"] = path
if "Host" not in self["headers"]:
self["headers"]["Host"] = "localhost"
def read(self, seek):
return self["body"].read(seek)
@ -92,6 +97,7 @@ class HttpRequest(AccessDict):
buf.write(f"{self.method} {self.path} HTTP/1.1")
for k, v in self["headers"].items():
buf.write(f"{k}: {v}\r\n")
buf.write(CRLF + CRLF)
buf.write(self["body"].getvalue() + "\r\n")
return buf.getvalue() + "\r\n"
@ -115,6 +121,7 @@ class HttpResponse(AccessDict):
def write(self, stuff):
return self.body.write(stuff)
# Foreshadowing (n): A literary device in which an author ...
def __str__(self):
buf = StringIO()
print(self.headers)
@ -123,6 +130,8 @@ class HttpResponse(AccessDict):
for k, v in self["headers"].items():
buf.write(f"{k}: {v}\r\n")
buf.write(f"Content-Length: {length}\r\n")
buf.write(CRLF + CRLF) # Per RFC 9112
buf.write(self["body"].getvalue() + "\r\n")
return buf.getvalue() + "\r\n"