this is almost ready
This commit is contained in:
parent
30a9e0bedd
commit
649a301cf9
13
http.py
13
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"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user