-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add brotli and zstd decompression support for toolbar injection #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -485,30 +485,50 @@ def _inject_toolbar(self, body: bytes, context: RequestContext, content_encoding | |||||
| Args: | ||||||
| body: The original response body (may be compressed). | ||||||
| context: The request context with collected data. | ||||||
| content_encoding: The content-encoding header value (e.g., "gzip"). | ||||||
| content_encoding: The content-encoding header value (e.g., "gzip", "br", "zstd"). | ||||||
|
|
||||||
| Returns: | ||||||
| Tuple of (modified body, content_encoding to use). | ||||||
| If gzip was decompressed, returns uncompressed body with empty encoding. | ||||||
| If compression was handled, returns uncompressed body with empty encoding. | ||||||
| """ | ||||||
| # Handle gzip-compressed responses | ||||||
| # Track whether we successfully decompressed the body | ||||||
| decompressed = False | ||||||
| encodings = [e.strip() for e in content_encoding.lower().split(",")] if content_encoding else [] | ||||||
|
|
||||||
| if "gzip" in encodings: | ||||||
| try: | ||||||
| body = gzip.decompress(body) | ||||||
| decompressed = True | ||||||
| except gzip.BadGzipFile: | ||||||
| # Not valid gzip, try to decode as-is | ||||||
| pass | ||||||
| logger.debug("Invalid gzip data, attempting to decode as-is") | ||||||
|
|
||||||
| elif "br" in encodings: | ||||||
| try: | ||||||
| import brotli # type: ignore[import-untyped] | ||||||
|
|
||||||
| body = brotli.decompress(body) | ||||||
| decompressed = True | ||||||
| except ImportError: | ||||||
| logger.debug("Brotli not installed, skipping toolbar injection for br-encoded response") | ||||||
| return body, content_encoding | ||||||
| except Exception: | ||||||
| logger.debug("Invalid brotli data, attempting to decode as-is") | ||||||
|
Comment on lines
+504
to
+514
|
||||||
|
|
||||||
| elif "zstd" in encodings: | ||||||
| try: | ||||||
| import zstandard # type: ignore[import-untyped] | ||||||
|
|
||||||
| dctx = zstandard.ZstdDecompressor() | ||||||
| body = dctx.decompress(body) | ||||||
| decompressed = True | ||||||
|
Comment on lines
+505
to
+522
|
||||||
| except ImportError: | ||||||
| logger.debug("zstandard not installed, skipping toolbar injection for zstd-encoded response") | ||||||
| return body, content_encoding | ||||||
| except Exception: | ||||||
|
||||||
| except Exception: | |
| except zstandard.ZstdError: |
Copilot
AI
Jan 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new zstd decompression support lacks test coverage. The existing TestGzipCompression class demonstrates comprehensive testing patterns (valid compression, invalid data, UTF-8 decoding failures, case-insensitive headers) that should be replicated for zstd. Consider adding similar test cases for zstd-encoded responses to ensure the decompression logic works correctly and handles edge cases appropriately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The broad exception handler catches all exceptions from brotli decompression. This can mask legitimate programming errors or unexpected issues. Consider catching specific exceptions like brotli.error (if available) to handle only expected decompression failures, while allowing unexpected errors to propagate appropriately.