From 746cb21a03363587a06fc9ae004adaa0855ef31c Mon Sep 17 00:00:00 2001 From: UltraQbik Date: Sat, 24 Aug 2024 21:56:02 +0300 Subject: zlib and gzip don't seem to work normally --- main.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index a2d3310..33dbcd4 100644 --- a/main.py +++ b/main.py @@ -5,10 +5,9 @@ The mighty silly webserver written in python for no good reason import os import ssl -# import zlib import time import socket -# import brotli +import brotli import signal import threading from src import APIv1 @@ -162,14 +161,12 @@ class HTTPServer: response = Response(data, STATUS_CODE_NOT_FOUND) # process header data - # if response.headers.get("Content-Encoding") is None and response.compress: - # supported_compressions = [x.strip() for x in getattr(request, "Accept-Encoding", "").split(",")] - # if "br" in supported_compressions: - # 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) + compressor = None + if response.headers.get("Content-Encoding") is None and response.compress: + supported_compressions = [x.strip() for x in getattr(request, "Accept-Encoding", "").split(",")] + if "br" in supported_compressions: + response.headers["Content-Encoding"] = "br" + compressor = brotli.Compressor() if response.headers.get("Connection") is None: response.headers["Connection"] = "close" @@ -182,8 +179,15 @@ class HTTPServer: # send message client.sendall(message) for packet in response.get_data_stream(): - client.sendall(packet) + data = packet + if response.headers.get("Content-Encoding") == "br": + # Docs for brotli (and other compressions) are bad, so that may not be the correct way of doing it + compressor.process(data) + data = compressor.flush() + client.sendall(data) + + # check for stop event if self.stop_event.is_set(): break -- cgit 1.4.1