Skip to content
Open
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
7 changes: 5 additions & 2 deletions brotli_asgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ async def send_with_brotli(self, message: Message) -> None:
# Don't apply Brotli to small outgoing responses.
await self.send(self.initial_message)
await self.send(message)
headers = MutableHeaders(raw=self.initial_message["headers"])
if "br" in headers.get("Content-Encoding", ""):
Copy link
Copy Markdown

@keul keul Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhysrevans3 I'm wondering: is OK here to check if there is the br compression explicitly, or should the middleware to prevent compressing again if there is any other compression? ("Content-Encoding" in headers)?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid point. Because the main idea of this library is to be a drop-in replacement for the GZipMiddleware shipped with Starlette, I need to review the current state of the GZipMiddleware

https://github.com/Kludex/starlette/blob/main/starlette/middleware/gzip.py

# Don't apply Brotli twice.
await self.send(self.initial_message)
await self.send(message)
elif not more_body:
# Standard Brotli response.
body = self._process(body) + self.br_file.finish()
headers = MutableHeaders(raw=self.initial_message["headers"])
headers["Content-Encoding"] = "br"
headers["Content-Length"] = str(len(body))
headers.add_vary_header("Accept-Encoding")
Expand All @@ -159,7 +163,6 @@ async def send_with_brotli(self, message: Message) -> None:
await self.send(message)
else:
# Initial body in streaming Brotli response.
headers = MutableHeaders(raw=self.initial_message["headers"])
headers["Content-Encoding"] = "br"
headers.add_vary_header("Accept-Encoding")
del headers["Content-Length"]
Expand Down