about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUltraQbik <no1skill@yandex.ru>2024-08-27 19:26:12 +0300
committerUltraQbik <no1skill@yandex.ru>2024-08-27 19:26:12 +0300
commit873946002d347b5db6ba6dec8722c0b792e7d584 (patch)
treea726e879095c03999339c479f4c60b94a7de1bec
parentfdda4950d518e4ddae8a548f8ff6c8b0d4cba41a (diff)
downloadhttpy-873946002d347b5db6ba6dec8722c0b792e7d584.tar.gz
httpy-873946002d347b5db6ba6dec8722c0b792e7d584.zip
Bug fix
Fixed bug with file data stream fetching, as when file reach the end they return `b''` and not `None`

Bug was causing threads to run forever, effectively completely halting the website from functioning
-rw-r--r--main.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/main.py b/main.py
index 408fbe4..73fdc17 100644
--- a/main.py
+++ b/main.py
@@ -21,7 +21,11 @@ from src.file_man import PATH_MAP
 usocket = socket.socket | ssl.SSLSocket
 
 # logging
-logging.basicConfig(filename="runtime.log", encoding="utf-8", level=logging.INFO)
+logging.basicConfig(
+    filename="runtime.log",
+    encoding="utf-8",
+    datefmt="%H:%M:%S",
+    level=logging.INFO)
 
 
 class HTTPServer:
@@ -93,6 +97,7 @@ class HTTPServer:
             # accept new client
             try:
                 client = self._accept()
+                client.setblocking(True)  # ensures that client sockets are blocking
             except ssl.SSLError:
                 continue
             except OSError as e:
@@ -118,7 +123,6 @@ class HTTPServer:
         :param client: client ssl socket
         """
 
-        client.setblocking(True)  # in ssl it's the default, in plain sockets it's not
         self.semaphore.acquire()
         try:
             request = self._recv_request(client)
@@ -133,8 +137,8 @@ class HTTPServer:
 
         # Remove self from thread list and close the connection
         self.client_threads.remove(threading.current_thread())
-        self.semaphore.release()
         client.close()
+        self.semaphore.release()
 
     def _client_request_handler(self, client: usocket, request: Request):
         """
@@ -247,7 +251,7 @@ class HTTPServer:
 
         if path in self.path_map:
             with open(self.path_map[path]["path"], "rb") as file:
-                while (msg := file.read(BUFFER_LENGTH)) is not None:
+                while msg := file.read(BUFFER_LENGTH):
                     yield msg
                 raise StopIteration
         yield None