diff --git a/filesystem.py b/filesystem.py index 4e6b172..f1179bc 100644 --- a/filesystem.py +++ b/filesystem.py @@ -124,7 +124,21 @@ class HttpResponse(AccessDict): return self.body.write(bytes(stuff, "utf-8")) return self.body.write(stuff) - # Foreshadowing (n): A literary device in which an author ... + def binary_io(self): + if not self.binmode: + str(self) + hbuf = StringIO() + bbuf = BytesIO() + hbuf.write(f"HTTP/1.1 {self.status}" + CRLF) + length = len(self["body"].getvalue()) + for k, v in self["headers"].items(): + hbuf.write(f"{k}: {v}\r\n") + if "Content-Length" not in self["headers"]: + hbuf.write(f"Content-Length: {length}\r\n") + hbuf.write(CRLF) # Per RFC 9112 + bbuf.write(self.body.getvalue()) + return (hbuf.getvalue(), bbuf.getvalue()) + def __str__(self): buf = StringIO() buf.write(f"HTTP/1.1 {self.status}" + CRLF) @@ -440,8 +454,13 @@ def client_handler(sock): continue # gevent.sleep(0.25) # this is a somewhat magical value, see Part II - default = str(server_response) + CRLF - sock.send(default.encode("utf-8")) + if server_response.binmode: + headers, body = server_response.binary_io() + sock.send(headers.encode("utf-8")) + sock.send(body) + else: + default = str(server_response) + CRLF + sock.send(default.encode("utf-8")) # sock.shutdown(socket.SHUT_RDWR) # we do a more graceful exit here by # shutting down the socket, makes things faster for TLS # may have an effect on client response time to but i didn't notice it. diff --git a/htdocs/index.html b/htdocs/index.html index d794bb7..1da78ec 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -9,6 +9,7 @@

Page title

+ No Strangers

Hello! this is an example page using Xess

When in doubt, create another framework
diff --git a/htdocs/rick.jpg b/htdocs/rick.jpg new file mode 100644 index 0000000..8a9f261 Binary files /dev/null and b/htdocs/rick.jpg differ