From 946f4f86eb48deb17b0ad8d4698d1435b1963c84 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 28 Dec 2025 03:25:33 +0000 Subject: [PATCH 1/2] Initial plan From 411f86400ecfb4e90d70ccbd73829dfa482d3fe3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 28 Dec 2025 03:29:33 +0000 Subject: [PATCH 2/2] refactor: rename flag to track decompression success explicitly The `is_gzipped` variable was ambiguous - it initially tracked whether the header indicated gzip encoding, but then was repurposed to track whether decompression succeeded. This made the code harder to understand. Renamed to `decompressed` to explicitly indicate it tracks whether we successfully decompressed the body. This makes the logic clearer: - If decompression succeeds and UTF-8 decoding fails, return the decompressed body with no content-encoding header - If decompression fails and UTF-8 decoding fails, return the original body with the original content-encoding header Addresses feedback: https://github.com/JacobCoffee/debug-toolbar/pull/24#discussion_r2649450723 Co-authored-by: JacobCoffee <45884264+JacobCoffee@users.noreply.github.com> --- src/debug_toolbar/litestar/middleware.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/debug_toolbar/litestar/middleware.py b/src/debug_toolbar/litestar/middleware.py index 546facf..a17cbf1 100644 --- a/src/debug_toolbar/litestar/middleware.py +++ b/src/debug_toolbar/litestar/middleware.py @@ -493,13 +493,15 @@ def _inject_toolbar(self, body: bytes, context: RequestContext, content_encoding import gzip # Handle gzip-compressed responses - is_gzipped = content_encoding.lower() == "gzip" - if is_gzipped: + # Track whether we successfully decompressed the body + decompressed = False + if content_encoding.lower() == "gzip": try: body = gzip.decompress(body) + decompressed = True except (gzip.BadGzipFile, OSError): # Not valid gzip, try to decode as-is - is_gzipped = False + pass try: html = body.decode("utf-8") @@ -507,7 +509,7 @@ def _inject_toolbar(self, body: bytes, context: RequestContext, content_encoding # Can't decode. If we successfully decompressed gzip, return the # decompressed body with no content-encoding. Otherwise, return # the body as-is with the original encoding. - if is_gzipped: + if decompressed: return body, "" return body, content_encoding