From 649a301cf9e8790c15917ec280abfe10c28235b9 Mon Sep 17 00:00:00 2001 From: Matt Arnold Date: Sat, 6 Sep 2025 08:21:18 -0400 Subject: [PATCH] this is almost ready --- http.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/http.py b/http.py index 97bfef6..0fac446 100644 --- a/http.py +++ b/http.py @@ -11,7 +11,7 @@ from io import StringIO from email.utils import formatdate Fork = False -# The next post in this series,which now has the name +# The next post in this series, which now has the name # __The Network Programing Purgatorio__ by the way. # Was originally going to be about how to use TLS # But it turns out just kinda faking your http implementation @@ -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,14 +121,17 @@ 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) - buf.write(f"HTTP/1.1 {self.status}\r\n ") + buf.write(f"HTTP/1.1 {self.status}\r\n") length = len(self["body"].getvalue()) 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"