about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUltraQbik <no1skill@yandex.ru>2024-08-25 03:13:45 +0300
committerUltraQbik <no1skill@yandex.ru>2024-08-25 03:13:45 +0300
commit619f5707b95b3ff13047f6114e8b89ef665b1100 (patch)
treed507a6671e758a6399584f276eda83ef29240082
parent93317bcd56a39fc70020143d6f4e6a615a45111f (diff)
downloadhttpy-619f5707b95b3ff13047f6114e8b89ef665b1100.tar.gz
httpy-619f5707b95b3ff13047f6114e8b89ef665b1100.zip
Add UTF-8 support
Probably useful to have it
-rw-r--r--main.py12
-rw-r--r--src/request.py6
-rw-r--r--src/status_code.py2
-rw-r--r--www/about.html1
-rw-r--r--www/index.html1
-rw-r--r--www/projects.html1
-rw-r--r--www/test.html1
7 files changed, 15 insertions, 9 deletions
diff --git a/main.py b/main.py
index 599fd85..c7c2f81 100644
--- a/main.py
+++ b/main.py
@@ -28,6 +28,8 @@ path_map = {
     "/test":                {"path": "www/test.html"},
     "/projects":            {"path": "www/projects.html"},
     "/images/*":            {"path": "www/images"},
+    "/js-test":             {"path": "www/js-test.html"},
+    "/scripts/*":           {"path": "www/scripts"},
 }
 
 # API
@@ -163,8 +165,8 @@ class HTTPServer:
             # case "POST":  # Not Implemented
             #     response = self._handle_post(client, request)
             case _:
-                with open(I_PATH_MAP["/err/response"]["path"], "r", encoding="ascii") as file:
-                    data = file.read().format(status_code=str(STATUS_CODE_NOT_FOUND)).encode("ascii")
+                with open(I_PATH_MAP["/err/response"]["path"], "r", encoding="utf8") as file:
+                    data = file.read().format(status_code=str(STATUS_CODE_NOT_FOUND)).encode("utf8")
                 response = Response(data, STATUS_CODE_NOT_FOUND)
 
         # process header data
@@ -180,7 +182,7 @@ class HTTPServer:
         # generate basic message
         message = b'HTTP/1.1 ' + response.status.__bytes__() + b'\r\n'
         for key, value in response.headers.items():
-            message += f"{key}: {value}\r\n".encode("ascii")
+            message += f"{key}: {value}\r\n".encode("utf8")
         message += b'\r\n'
 
         # send message
@@ -235,9 +237,9 @@ class HTTPServer:
             return APIv1.api_call(client, request)
 
         else:  # assume browser
-            with open(I_PATH_MAP["/err/response"]["path"], "r", encoding="ascii") as file:
+            with open(I_PATH_MAP["/err/response"]["path"], "r", encoding="utf8") as file:
                 data = file.read()
-            data = data.format(status_code=str(STATUS_CODE_NOT_FOUND)).encode("ascii")
+            data = data.format(status_code=str(STATUS_CODE_NOT_FOUND)).encode("utf8")
             return Response(data, STATUS_CODE_NOT_FOUND)
 
     def _handle_post(self, client: ssl.SSLSocket, request: Request) -> Response:
diff --git a/src/request.py b/src/request.py
index 6c5b9d5..8f5a6a8 100644
--- a/src/request.py
+++ b/src/request.py
@@ -25,8 +25,8 @@ class Request:
         request = Request()
 
         # change type and path
-        request.type = raw_request[:raw_request.find(b' ')].decode("ascii")
-        raw_path = raw_request[len(request.type)+1:raw_request.find(b' ', len(request.type)+1)].decode("ascii")
+        request.type = raw_request[:raw_request.find(b' ')].decode("utf8")
+        raw_path = raw_request[len(request.type)+1:raw_request.find(b' ', len(request.type)+1)].decode("utf8")
 
         # remove path args from path
         request.path = raw_path.split("?")[0]
@@ -47,7 +47,7 @@ class Request:
 
         # decode headers
         for raw_header in raw_request.split(b'\r\n'):
-            if len(pair := raw_header.decode("ascii").split(":")) == 2:
+            if len(pair := raw_header.decode("utf8").split(":")) == 2:
                 key, val = pair
                 val = val.strip()
 
diff --git a/src/status_code.py b/src/status_code.py
index a63712c..79a2011 100644
--- a/src/status_code.py
+++ b/src/status_code.py
@@ -8,7 +8,7 @@ class StatusCode:
         self._message: str = message
 
     def __bytes__(self):
-        return f"{self._code} {self._message}".encode("ascii")
+        return f"{self._code} {self._message}".encode("utf8")
 
     def __str__(self):
         return f"{self._code} {self._message}"
diff --git a/www/about.html b/www/about.html
index aa0b57e..b9fde96 100644
--- a/www/about.html
+++ b/www/about.html
@@ -2,6 +2,7 @@
 <html lang="en">
 <head>
     <title> The YES's Page </title>
+    <meta charset="UTF-8">
     <link rel="stylesheet" href="css/styles.css">
 </head>
 <body>
diff --git a/www/index.html b/www/index.html
index b40895f..79ff93c 100644
--- a/www/index.html
+++ b/www/index.html
@@ -2,6 +2,7 @@
 <html lang="en">
 <head>
     <title> The YES's Page </title>
+    <meta charset="UTF-8">
     <link rel="stylesheet" href="css/styles.css">
 </head>
 <body>
diff --git a/www/projects.html b/www/projects.html
index 95be2f4..be48254 100644
--- a/www/projects.html
+++ b/www/projects.html
@@ -2,6 +2,7 @@
 <html lang="en">
 <head>
     <title> The YES's Page </title>
+    <meta charset="UTF-8">
     <link rel="stylesheet" href="css/styles.css">
 </head>
 <body>
diff --git a/www/test.html b/www/test.html
index b3f38f3..9f0d12d 100644
--- a/www/test.html
+++ b/www/test.html
@@ -2,6 +2,7 @@
 <html lang="en">
 <head>
     <title> The YES's Page </title>
+    <meta charset="UTF-8">
     <link rel="stylesheet" href="css/styles.css">
 </head>
 <body>