about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUltraQbik <no1skill@yandex.ru>2024-08-22 03:08:57 +0300
committerUltraQbik <no1skill@yandex.ru>2024-08-22 03:08:57 +0300
commitc6c3f47b04a7ccf301c76cdd8ab21bee9209be30 (patch)
tree788cb939172825164f309390aef2dc5ce12462ca
parenteedb57f893986091887d1a889d6752ea4b06ba2a (diff)
downloadhttpy-c6c3f47b04a7ccf301c76cdd8ab21bee9209be30.tar.gz
httpy-c6c3f47b04a7ccf301c76cdd8ab21bee9209be30.zip
Add brotli as a better compression
-rw-r--r--main.py39
1 files changed, 29 insertions, 10 deletions
diff --git a/main.py b/main.py
index f22b957..a4a6d58 100644
--- a/main.py
+++ b/main.py
@@ -7,6 +7,7 @@ import ssl
 import gzip
 import time
 import socket
+import brotli
 import signal
 import asyncio
 import aiofiles
@@ -16,12 +17,24 @@ from src.minimizer import minimize_html
 
 # path mapping
 PATH_MAP = {
-    "/":                        {"path": "www/index.html"},
-    "/index.html":              {"path": "www/index.html"},
-    "/robots.txt":              {"path": "www/robots.txt"},
-    "/favicon.ico":             {"path": "www/favicon.ico"},
-    "/css/styles.css":          {"path": "css/styles.css"},
-    "/about":                   {"path": "www/about.html"},
+    "/":
+        {"path": "www/index.html",
+         "compress": True},
+    "/index.html":
+        {"path": "www/index.html",
+         "compress": True},
+    "/robots.txt":
+        {"path": "www/robots.txt",
+         "compress": False},
+    "/favicon.ico":
+        {"path": "www/favicon.ico",
+         "compress": False},
+    "/css/styles.css":
+        {"path": "css/styles.css",
+         "compress": True},
+    "/about":
+        {"path": "www/about.html",
+         "compress": True},
 }
 
 # internal path map
@@ -173,9 +186,13 @@ class HTTPServer:
             if PATH_MAP[request.path]["path"][-4:] == "html":
                 data = minimize_html(data)
 
-            # add gzip compression header (if supported)
+            # add brotli compression header (if supported)
             headers = {}
-            if "gzip" in compressions:
+            if "br" in compressions:
+                headers["Content-Encoding"] = "br"
+
+            # else add gzip compression (if supported)
+            elif "gzip" in compressions:
                 headers["Content-Encoding"] = "gzip"
 
             # send 200 response with the file to the client
@@ -213,8 +230,10 @@ class HTTPServer:
             headers = dict()
 
         # check for 'content-encoding' header
-        if headers.get("Content-Encoding") == "gzip":
-            # if present -> compress data
+        if headers.get("Content-Encoding") == "br":
+            data = brotli.compress(data)
+
+        elif headers.get("Content-Encoding") == "gzip":
             data = gzip.compress(data)
 
         # format headers