From 236fc84be1fa5fae35a200a52e895599267e0077 Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Tue, 29 Oct 2024 22:49:05 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20clarify=20inline=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spiderweb/middleware/gzip.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spiderweb/middleware/gzip.py b/spiderweb/middleware/gzip.py index 1f956a1..6589c7c 100644 --- a/spiderweb/middleware/gzip.py +++ b/spiderweb/middleware/gzip.py @@ -17,9 +17,13 @@ class GzipMiddleware(SpiderwebMiddleware): def post_process( self, request: Request, response: HttpResponse, rendered_response: str ) -> str: - - # right status, length > 500, instance string (because FileResponse returns list of bytes, - # not already compressed, and client accepts gzip + # Only actually compress the response if the following attributes are true: + # + # * The response status code is a 2xx success code + # * The response length is at least 500 bytes + # * The response is not already compressed (e.g. it's not an image) + # * The request accepts gzip encoding + # * The response is not a streaming response if ( not (200 <= response.status_code < 300) or len(rendered_response) < self.minimum_length