Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/debug_toolbar/litestar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,21 +493,23 @@ 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")
except UnicodeDecodeError:
# 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

Expand Down