about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUltraQbik <no1skill@yandex.ru>2024-08-24 21:28:54 +0300
committerUltraQbik <no1skill@yandex.ru>2024-08-24 21:28:54 +0300
commit6fc286392d71e7e1351b5ffcf4e265c3ca475f01 (patch)
tree235c988532a960d74579d2cd2df50434653976f9
parent1e29c48239f65946369c9f5275689a2420d320bb (diff)
downloadhttpy-6fc286392d71e7e1351b5ffcf4e265c3ca475f01.tar.gz
httpy-6fc286392d71e7e1351b5ffcf4e265c3ca475f01.zip
Change client handling
Previously _client_thread was checking for new data from client, which is very much useless, because the client will not send any data back twice.

This commit removes the useless while loop that was there, as well as the thread that was started

So currently, it's 1 thread per 1 request, which is how it should probably be
-rw-r--r--main.py30
1 files changed, 13 insertions, 17 deletions
diff --git a/main.py b/main.py
index c2b9ba1..a2d3310 100644
--- a/main.py
+++ b/main.py
@@ -132,21 +132,14 @@ class HTTPServer:
         :param client: client ssl socket
         """
 
-        # client.settimeout(5)
-        while not self.stop_event.is_set():
-            try:
-                # get client's request
-                request = self._recv_request(client)
-                if request is None:
-                    break
-
-                threading.Thread(target=self._client_request_handler, args=[client, request], daemon=True).start()
-            except TimeoutError:
-                print("Client timeout")
-                break
-            except Exception as e:
-                print(e)
-                break
+        try:
+            request = self._recv_request(client)
+            if request is not None:
+                self._client_request_handler(client, request)
+        except TimeoutError:
+            print("Client timeout")
+        except Exception as e:
+            print(e)
 
         # close the connection once stop even was set or an error occurred
         client.close()
@@ -175,8 +168,8 @@ class HTTPServer:
         #         response.headers["Content-Encoding"] = "br"
         #     elif "gzip" in supported_compressions:
         #         response.headers["Content-Encoding"] = "gzip"
-        if response.headers.get("Content-Length") is None:
-            response.headers["Content-Length"] = len(response.data)
+        # if response.headers.get("Content-Length") is None:
+        #     response.headers["Content-Length"] = len(response.data)
         if response.headers.get("Connection") is None:
             response.headers["Connection"] = "close"
 
@@ -191,6 +184,9 @@ class HTTPServer:
         for packet in response.get_data_stream():
             client.sendall(packet)
 
+            if self.stop_event.is_set():
+                break
+
     def _handle_get(self, client: ssl.SSLSocket, request: Request) -> Response:
         """
         Handles GET / HEAD requests from a client