about summary refs log tree commit diff
path: root/main.py
diff options
context:
space:
mode:
authorUltraQbik <no1skill@yandex.ru>2024-08-21 00:54:27 +0300
committerUltraQbik <no1skill@yandex.ru>2024-08-21 00:54:27 +0300
commitd2ee7f153e4d21879e12fe25ec2128f8e6466afb (patch)
treeb5b71fc0160de4bba050bc9ef778bda11d25f6a2 /main.py
parente5df54da3967255edd3c945075389afbf81374e6 (diff)
downloadhttpy-d2ee7f153e4d21879e12fe25ec2128f8e6466afb.tar.gz
httpy-d2ee7f153e4d21879e12fe25ec2128f8e6466afb.zip
Add simple response.html page
Diffstat (limited to 'main.py')
-rw-r--r--main.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/main.py b/main.py
index 491f85e..f99fae8 100644
--- a/main.py
+++ b/main.py
@@ -21,6 +21,11 @@ PATH_MAP = {
     "/css/styles.css":          {"path": "css/styles.css"},
 }
 
+# internal path map
+I_PATH_MAP = {
+    "/err/response.html":       {"path": "www/err/response.html"}
+}
+
 
 def get_response_code(code: int) -> bytes:
     match code:
@@ -200,8 +205,18 @@ class HTTPServer:
             # send 200 response with the file to the client
             HTTPServer._send(client, 200, data, headers)
         else:
+            # in case of error, return error page
+            async with aiofiles.open(I_PATH_MAP["/err/response.html"]["path"], "r") as f:
+                data = await f.read()
+
+            # status code
+            status_code = 404
+
+            # format error response
+            data = data.format(status_code=get_response_code(status_code).decode("ascii"))
+
             # send 404 response to the client
-            HTTPServer._send(client, 404)
+            HTTPServer._send(client, status_code, data.encode("ascii"))
 
     @staticmethod
     def _send(client: socket.socket, response: int, data: bytes = None, headers: dict[str, str] = None):