about summary refs log tree commit diff
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
parente5df54da3967255edd3c945075389afbf81374e6 (diff)
downloadhttpy-d2ee7f153e4d21879e12fe25ec2128f8e6466afb.tar.gz
httpy-d2ee7f153e4d21879e12fe25ec2128f8e6466afb.zip
Add simple response.html page
-rw-r--r--main.py17
-rw-r--r--www/err/response.html10
-rw-r--r--www/index.html78
3 files changed, 65 insertions, 40 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):
diff --git a/www/err/response.html b/www/err/response.html
new file mode 100644
index 0000000..70753e2
--- /dev/null
+++ b/www/err/response.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en" style="color: white;background: black;margin: 0;padding: 0">
+<head>
+    <title> {status_code} </title>
+</head>
+<body style="text-align: center">
+    <header style="font-size: 100px; padding: 10vh 0 0"> {status_code} </header>
+    <p style="padding: 10px;"> <button style="font-size: 20px;padding: 10px" onclick="window.location.href='/';"> back to main page </button> </p>
+</body>
+</html>
\ No newline at end of file
diff --git a/www/index.html b/www/index.html
index 6ba0e5b..bdb132f 100644
--- a/www/index.html
+++ b/www/index.html
@@ -1,47 +1,47 @@
 <!DOCTYPE html>
 <html lang="en">
-    <head>
-        <title> The YES's Page </title>
-        <link rel="stylesheet" href="css/styles.css">
-    </head>
-    <body>
+<head>
+    <title> The YES's Page </title>
+    <link rel="stylesheet" href="css/styles.css">
+</head>
+<body>
     <header> <b> Welcome To The Mighty HTML Page </b> </header>
-        <div id="cool-div">
-            <section style="padding-top: 10px">
-                <h1> What is this? </h1>
-                <p> > This is an HTML page! </p>
-                <p> > It exists! </p>
-            </section>
-            <section>
-                <h1> Why does this exist? </h1>
-                <p> > Funny haha </p>
-                <p> > I'm learning how to webpage from nothing </p>
-            </section>
-            <section>
-                <h1> What is it running? </h1>
-                <p> > This server is run by the shitty python code I wrote </p>
-                <p> > Server does not use flask or any other similar python web frameworks </p>
-                <p> > It primarily uses standard python libraries, with 2 libraries being an exception </p>
-                <p> > <i> aiofiles </i> - for asynchronous file I/O </p>
-            </section>
-            <section>
-                <h1> Where can I see the shitty python code you wrote? </h1>
-                <p> > You can check the shitty python code I wrote at my github </p>
-                <p> > here's comically large button to help you navigate to there </p>
-                <p style="padding-top: 12px;text-align: center">
-                    <button id="git-butt" class="btn btn-success" onclick=" window.open('https://github.com/UltraQbik/httpy','_blank')"> OPEN GITHUB NOW </button>
-                </p>
-            </section>
-            <section>
-                <h1> Useless button section? </h1>
-                <p style="padding-top: 12px">
-                    <button id="cool-butt"> Useless button section. </button>
-                </p>
-            </section>
-        </div>
+    <div id="cool-div">
+        <section style="padding-top: 10px">
+            <h1> What is this? </h1>
+            <p> > This is an HTML page! </p>
+            <p> > It exists! </p>
+        </section>
+        <section>
+            <h1> Why does this exist? </h1>
+            <p> > Funny haha </p>
+            <p> > I'm learning how to webpage from nothing </p>
+        </section>
+        <section>
+            <h1> What is it running? </h1>
+            <p> > This server is run by the shitty python code I wrote </p>
+            <p> > Server does not use flask or any other similar python web frameworks </p>
+            <p> > It primarily uses standard python libraries, with 2 libraries being an exception </p>
+            <p> > <i> aiofiles </i> - for asynchronous file I/O </p>
+        </section>
+        <section>
+            <h1> Where can I see the shitty python code you wrote? </h1>
+            <p> > You can check the shitty python code I wrote at my GitHub </p>
+            <p> > here's comically large button to help you navigate to there </p>
+            <p style="padding-top: 12px;text-align: center">
+                <button id="git-butt" class="btn btn-success" onclick="window.open('https://github.com/UltraQbik/httpy','_blank')"> OPEN GITHUB NOW </button>
+            </p>
+        </section>
+        <section>
+            <h1> Useless button section? </h1>
+            <p style="padding-top: 12px">
+                <button id="cool-butt"> Useless button section. </button>
+            </p>
+        </section>
+    </div>
     <footer>
         <p> Hehe <i>foot</i>er </p>
         <p> P.S. CSS is a mess :< </p>
     </footer>
-    </body>
+</body>
 </html>