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