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
35 changes: 19 additions & 16 deletions gradio/brotli_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ def __init__(
else:
self.excluded_handlers = []

self.compressible_extensions = {
".html",
".htm",
".js",
".css",
".json",
".md",
".txt",
".csv",
".tsv",
".xml",
".svg",
}

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if (
self._is_handler_excluded(scope)
Expand Down Expand Up @@ -100,22 +114,9 @@ def _is_handler_excluded(self, scope: Scope) -> bool:
def _is_compressible_file_type(self, scope: Scope) -> bool:
"""Check if the requested file has a compressible file extension."""
path = scope.get("path", "")
compressible_extensions = {
".html",
".htm",
".js",
".css",
".json",
".md",
".txt",
".csv",
".tsv",
".xml",
".svg",
}
if "." in path:
extension = "." + path.split(".")[-1].lower()
return extension in compressible_extensions
extension = "." + path.rpartition(".")[-1].lower()
return extension in self.compressible_extensions

return False

Expand Down Expand Up @@ -147,7 +148,9 @@ def __init__(
)
self.br_buffer = io.BytesIO()

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: # noqa
async def __call__(
self, scope: Scope, receive: Receive, send: Send
) -> None: # noqa
self.send = send
await self.app(scope, receive, self.send_with_brotli)

Expand Down