actually use those shiny new config settings

This commit is contained in:
Joe Kaufeld 2024-10-29 23:50:53 -04:00
parent 95f9479aa9
commit 972225d8bc

View file

@ -1,6 +1,7 @@
""" """
Source code inspiration: https://github.com/colour-science/flask-compress/blob/master/flask_compress/flask_compress.py Source code inspiration: https://github.com/colour-science/flask-compress/blob/master/flask_compress/flask_compress.py
""" """
from spiderweb.exceptions import ConfigError from spiderweb.exceptions import ConfigError
from spiderweb.middleware import SpiderwebMiddleware from spiderweb.middleware import SpiderwebMiddleware
from spiderweb.server_checks import ServerCheck from spiderweb.server_checks import ServerCheck
@ -28,9 +29,9 @@ class CheckValidGzipMinimumLength(ServerCheck):
INVALID_GZIP_MINIMUM_LENGTH = "`gzip_minimum_length` must be a positive integer." INVALID_GZIP_MINIMUM_LENGTH = "`gzip_minimum_length` must be a positive integer."
def check(self): def check(self):
if not isinstance(self.server.gzip_minimum_length, int): if not isinstance(self.server.gzip_minimum_response_length, int):
raise ConfigError(self.INVALID_GZIP_MINIMUM_LENGTH) raise ConfigError(self.INVALID_GZIP_MINIMUM_LENGTH)
if self.server.gzip_minimum_length < 1: if self.server.gzip_minimum_response_length < 1:
raise ConfigError(self.INVALID_GZIP_MINIMUM_LENGTH) raise ConfigError(self.INVALID_GZIP_MINIMUM_LENGTH)
@ -39,7 +40,6 @@ class GzipMiddleware(SpiderwebMiddleware):
checks = [CheckValidGzipCompressionLevel, CheckValidGzipMinimumLength] checks = [CheckValidGzipCompressionLevel, CheckValidGzipMinimumLength]
algorithm = "gzip" algorithm = "gzip"
minimum_length = 500
def post_process( def post_process(
self, request: Request, response: HttpResponse, rendered_response: str self, request: Request, response: HttpResponse, rendered_response: str
@ -54,15 +54,17 @@ class GzipMiddleware(SpiderwebMiddleware):
# - The request accepts gzip encoding # - The request accepts gzip encoding
if ( if (
not (200 <= response.status_code < 300) not (200 <= response.status_code < 300)
or len(rendered_response) < self.minimum_length or len(rendered_response) < self.server.gzip_minimum_response_length
or not isinstance(rendered_response, str) or not isinstance(rendered_response, str)
or self.algorithm in response.headers.get("Content-Encoding", "") or self.algorithm in response.headers.get("Content-Encoding", "")
or self.algorithm not in request.headers.get("Accept-Encoding", "") or self.algorithm not in request.headers.get("Accept-Encoding", "")
): ):
return rendered_response return rendered_response
zipped = gzip.compress(rendered_response.encode("UTF-8"), compresslevel=6) zipped = gzip.compress(
rendered_response.encode("UTF-8"),
compresslevel=self.server.gzip_compression_level,
)
response.headers["Content-Encoding"] = self.algorithm response.headers["Content-Encoding"] = self.algorithm
response.headers["Content-Length"] = str(len(zipped)) response.headers["Content-Length"] = str(len(zipped))
return zipped return zipped