diff options
| author | UltraQbik <no1skill@yandex.ru> | 2024-08-27 15:44:19 +0300 |
|---|---|---|
| committer | UltraQbik <no1skill@yandex.ru> | 2024-08-27 15:44:19 +0300 |
| commit | fdda4950d518e4ddae8a548f8ff6c8b0d4cba41a (patch) | |
| tree | 8471fb2b1f657b848507425d74b12bbb8ac018f2 | |
| parent | 3263cfdee15391995532545df84b8e4144684879 (diff) | |
| download | httpy-fdda4950d518e4ddae8a548f8ff6c8b0d4cba41a.tar.gz httpy-fdda4950d518e4ddae8a548f8ff6c8b0d4cba41a.zip | |
wut?
| -rw-r--r-- | main.py | 27 | ||||
| -rw-r--r-- | requirements.txt | 3 | ||||
| -rw-r--r-- | src/file_man.py | 10 | ||||
| -rw-r--r-- | src/minimizer.py | 65 | ||||
| -rw-r--r-- | www/projects.html | 6 |
5 files changed, 26 insertions, 85 deletions
diff --git a/main.py b/main.py index 9098210..408fbe4 100644 --- a/main.py +++ b/main.py @@ -91,7 +91,16 @@ class HTTPServer: # listen and respond handler while not self.stop_event.is_set(): # accept new client - client = self._accept() + try: + client = self._accept() + except ssl.SSLError: + continue + except OSError as e: + logging.info(f"Client dropped due to: {e}") + continue + except Exception: + logging.warning(f"ignoring exception:\n{traceback.format_exc()}") + continue if client is None: continue @@ -109,12 +118,13 @@ 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) if request is not None: self._client_request_handler(client, request) - except ssl.SSLEOFError: + except ssl.SSLError: pass except OSError as e: logging.info(f"request dropped due to: {e}") @@ -152,8 +162,6 @@ class HTTPServer: # send message client.sendall(message) for packet in response.get_data_stream(): - if packet is None: - break client.sendall(packet) # check for stop event @@ -217,14 +225,6 @@ class HTTPServer: return self.sock.accept()[0] except BlockingIOError: time.sleep(0.005) - except ssl.SSLEOFError: - break - except OSError as e: - logging.info(f"Client dropped due to: {e}") - break - except Exception: - logging.warning(f"ignoring exception:\n{traceback.format_exc()}") - break return None def fetch_file_headers(self, path: str) -> dict[str, Any] | None: @@ -247,8 +247,9 @@ class HTTPServer: if path in self.path_map: with open(self.path_map[path]["path"], "rb") as file: - while (msg := file.read(BUFFER_LENGTH)): + while (msg := file.read(BUFFER_LENGTH)) is not None: yield msg + raise StopIteration yield None diff --git a/requirements.txt b/requirements.txt index 12d9786..96cf00a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -Brotli~=1.1.0 \ No newline at end of file +Brotli==1.1.0 +htmlmin==0.1.12 diff --git a/src/file_man.py b/src/file_man.py index 3371dd2..1759338 100644 --- a/src/file_man.py +++ b/src/file_man.py @@ -80,7 +80,7 @@ def compress_path_map(path_map: dict[str, dict[str, Any]], path_prefix: str = "c """ import brotli - from src.minimizer import minimize_html + import htmlmin if not os.path.exists(path_prefix): os.mkdir(path_prefix) for val in path_map.values(): @@ -95,8 +95,12 @@ def compress_path_map(path_map: dict[str, dict[str, Any]], path_prefix: str = "c with open(filepath, "wb") as comp: with open(val["path"], "rb") as file: comp.write( - brotli.compress(minimize_html(file.read())) - ) + brotli.compress(htmlmin.minify( + file.read().decode("utf-8"), + remove_comments=True, + remove_empty_space=True, + remove_all_empty_space=True, + reduce_boolean_attributes=True).encode("utf-8"))) else: with open(filepath, "wb") as comp: br = brotli.Compressor() diff --git a/src/minimizer.py b/src/minimizer.py deleted file mode 100644 index af1c015..0000000 --- a/src/minimizer.py +++ /dev/null @@ -1,65 +0,0 @@ -import re -# import htmlmin - - -def minimize_html(html: bytes) -> bytes: - """ - Minimizes HTML files. - Slightly better than htmlmin for my files, - but maybe I break something in process and I don't notice - """ - - html = bytearray(html) - - # remove newlines - html = (html - .replace(b'\r', b'') - .replace(b'\n', b'')) - - # remove double spaces - size = len(html) - while True: - html = html.replace(b' ', b'') - - # if nothing changes -> break - if size == len(html): - break - size = len(html) - - # simplify '> <' to '><' - html = html.replace(b'> <', b'><') - - # remove unnecessary quotes - index = 0 - for tag in re.findall(r"<.*?>", html.decode("utf8")): - index = html.find(tag.encode("utf8"), index) - processed = (tag - .replace("\"", "") - .replace(": ", ":") - .replace("; ", ";")) - if len(processed) < len(tag): - html[index:index+len(tag)] = (html[index:index+len(tag)] - .replace(tag.encode("utf8"), processed.encode("utf8"), 1)) - - return html - - -def test(): - with open("../www/about.html", "rb") as file: - original = file.read() - - processed = minimize_html(original) - - print(f"Original : {len(original)}\n" - f"Processed: {len(processed)}\n" - f"Rate : {(1 - len(processed) / len(original)) * 100:.2f}%", end="\n\n") - - # processed = htmlmin.minify(original.decode("utf8"), True, True, True, True, True) - # - # print(f"Original : {len(original)}\n" - # f"Processed: {len(processed)}\n" - # f"Rate : {(1 - len(processed) / len(original)) * 100:.2f}%") - - -if __name__ == '__main__': - test() diff --git a/www/projects.html b/www/projects.html index b253051..cd96435 100644 --- a/www/projects.html +++ b/www/projects.html @@ -12,7 +12,7 @@ </header> <div id="section-div"> <section> - <h1 style="padding-bottom: 10px"> <a href="https://scratch.mit.edu/projects/725766366/" target="_blank">Qubes3D (scratch)</a> </h1> + <h1 style="padding-bottom: 10px"> <a href="https://turbowarp.org/725766366?fps=60&turbo&hqpen" target="_blank">Qubes3D</a></h1> <img src="images/Qubes3D_0.webp" alt="Scratch RTX" width="500px"> <img src="images/Qubes3D_1.webp" alt="Scratch RTX" width="500px"> <p> My first big Ray Tracing project in Scratch! </p> @@ -20,7 +20,7 @@ <p> Can't tell exactly how long this project took, as there is no GitHub for scratch, but probably a couple of weeks to around a month</p> </section> <section> - <h1 style="padding-bottom: 10px"> <a href="https://scratch.mit.edu/projects/884304276" target="_blank">Ray Tracing in Scratch</a> </h1> + <h1 style="padding-bottom: 10px"> <a href="https://turbowarp.org/884304276?turbo&hqpen" target="_blank">Ray Tracing in Scratch</a></h1> <img src="images/ScratchRTX_0.webp" alt="Scratch RTX" width="500px"> <img src="images/ScratchRTX_1.webp" alt="Scratch RTX" width="500px"> <p> This is the Ray Tracing project that I am more or less proud of </p> @@ -29,7 +29,7 @@ <p> I am planning on creating a better version of this project, but for now that's that </p> </section> <section> - <h1 style="padding-bottom: 10px"> <a href="https://scratch.mit.edu/projects/926537124/" target="_blank">Ray Casting Tutorial</a> </h1> + <h1 style="padding-bottom: 10px"> <a href="https://turbowarp.org/1059299067?interpolate&hqpen" target="_blank">Ray Casting Tutorial</a></h1> <img src="images/RayCastingTutor_0.webp" alt="Scratch RTX" width="500px"> <img src="images/RayCastingTutor_1.webp" alt="Scratch RTX" width="500px"> <img src="images/RayCastingTutor_2.webp" alt="Scratch RTX" width="500px"> |