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