From 7302899e79306c4d06943c3d1569e3958ffe70e5 Mon Sep 17 00:00:00 2001 From: ddelange <14880945+ddelange@users.noreply.github.com> Date: Fri, 9 Dec 2022 19:32:41 +0100 Subject: [PATCH 1/3] Make brotli configurable by environment variable --- django_brotli/middleware.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/django_brotli/middleware.py b/django_brotli/middleware.py index d7edd3b..652d471 100644 --- a/django_brotli/middleware.py +++ b/django_brotli/middleware.py @@ -1,6 +1,7 @@ # -*- encoding: utf-8 -*- # ! python3 +import os import re import brotli @@ -14,10 +15,16 @@ RE_ACCEPT_ENCODING_BROTLI = re.compile(r"\bbr\b") MIN_LEN_FOR_RESPONSE_TO_PROCESS = 200 +BROTLI_MODE = getattr(brotli, os.environ.get("BROTLI_MODE", "MODE_GENERIC")) +BROTLI_QUALITY = int(os.environ.get("BROTLI_QUALITY", 5)) __all__ = ["BrotliMiddleware"] +def compress(obj): + return brotli.compress(obj, BROTLI_MODE, BRODLI_QUALITY) + + # noinspection PyClassHasNoInit class BrotliMiddleware(MiddlewareMixin): """ @@ -56,7 +63,7 @@ def process_response( # we won't know the compressed size until we stream it. del response["Content-Length"] else: - compressed_content = brotli.compress(response.content) + compressed_content = compress(response.content) # Return the compressed content only if it's actually shorter. if len(compressed_content) >= len(response.content): @@ -75,7 +82,7 @@ def process_response( def compress_stream(self, streaming_content): streaming_content = [line.decode("utf-8") for line in list(streaming_content)] streaming_content = "".join(streaming_content).encode() - streaming_content = map(lambda x: x, [brotli.compress(streaming_content)]) + streaming_content = map(lambda x: x, [compress(streaming_content)]) return streaming_content From a9d8e3d6785c39a69ea5170ec34bf8bc96d3d54a Mon Sep 17 00:00:00 2001 From: ddelange <14880945+ddelange@users.noreply.github.com> Date: Fri, 9 Dec 2022 20:40:51 +0100 Subject: [PATCH 2/3] Typo --- django_brotli/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_brotli/middleware.py b/django_brotli/middleware.py index 652d471..4616685 100644 --- a/django_brotli/middleware.py +++ b/django_brotli/middleware.py @@ -22,7 +22,7 @@ def compress(obj): - return brotli.compress(obj, BROTLI_MODE, BRODLI_QUALITY) + return brotli.compress(obj, BROTLI_MODE, BROTLI_QUALITY) # noinspection PyClassHasNoInit From 57aa2df9b51334deb0d2961ccd64851983277a92 Mon Sep 17 00:00:00 2001 From: ddelange <14880945+ddelange@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:01:22 +0100 Subject: [PATCH 3/3] Set default quality to 4 --- django_brotli/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_brotli/middleware.py b/django_brotli/middleware.py index 4616685..c08bf20 100644 --- a/django_brotli/middleware.py +++ b/django_brotli/middleware.py @@ -16,7 +16,7 @@ RE_ACCEPT_ENCODING_BROTLI = re.compile(r"\bbr\b") MIN_LEN_FOR_RESPONSE_TO_PROCESS = 200 BROTLI_MODE = getattr(brotli, os.environ.get("BROTLI_MODE", "MODE_GENERIC")) -BROTLI_QUALITY = int(os.environ.get("BROTLI_QUALITY", 5)) +BROTLI_QUALITY = int(os.environ.get("BROTLI_QUALITY", 4)) __all__ = ["BrotliMiddleware"]