From 5870db4815c1103161384924f4f1826986955b8b Mon Sep 17 00:00:00 2001 From: BroneKot <13498924+BroneKot@users.noreply.github.com> Date: Fri, 22 Nov 2019 17:26:02 +0300 Subject: [PATCH 1/2] Adding the ability to change the compression level. By default, python gzip.GzipFile compresslevel is 9. This commit saves the behavior of the standard library, but allows you to override compresslevel. High values are not recommended for streaming compression. --- starlette/middleware/gzip.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/starlette/middleware/gzip.py b/starlette/middleware/gzip.py index bb634e36d..091316534 100644 --- a/starlette/middleware/gzip.py +++ b/starlette/middleware/gzip.py @@ -6,29 +6,31 @@ class GZipMiddleware: - def __init__(self, app: ASGIApp, minimum_size: int = 500) -> None: + def __init__(self, app: ASGIApp, minimum_size: int = 500, compresslevel: int = 9) -> None: self.app = app self.minimum_size = minimum_size + self.compresslevel = compresslevel async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if scope["type"] == "http": headers = Headers(scope=scope) if "gzip" in headers.get("Accept-Encoding", ""): - responder = GZipResponder(self.app, self.minimum_size) + responder = GZipResponder(self.app, self.minimum_size, self.compresslevel) await responder(scope, receive, send) return await self.app(scope, receive, send) class GZipResponder: - def __init__(self, app: ASGIApp, minimum_size: int) -> None: + def __init__(self, app: ASGIApp, minimum_size: int, compresslevel: int) -> None: self.app = app self.minimum_size = minimum_size + self.compresslevel = compresslevel self.send = unattached_send # type: Send self.initial_message = {} # type: Message self.started = False self.gzip_buffer = io.BytesIO() - self.gzip_file = gzip.GzipFile(mode="wb", fileobj=self.gzip_buffer) + self.gzip_file = gzip.GzipFile(mode="wb", fileobj=self.gzip_buffer, compresslevel=self.compresslevel) async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: self.send = send From 1128338132a3b7ff8428a3cf08752c592c2d635a Mon Sep 17 00:00:00 2001 From: BroneKot <13498924+BroneKot@users.noreply.github.com> Date: Fri, 22 Nov 2019 21:43:40 +0300 Subject: [PATCH 2/2] lint --- starlette/middleware/gzip.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/starlette/middleware/gzip.py b/starlette/middleware/gzip.py index 091316534..13ce92bbb 100644 --- a/starlette/middleware/gzip.py +++ b/starlette/middleware/gzip.py @@ -6,7 +6,9 @@ class GZipMiddleware: - def __init__(self, app: ASGIApp, minimum_size: int = 500, compresslevel: int = 9) -> None: + def __init__( + self, app: ASGIApp, minimum_size: int = 500, compresslevel: int = 9 + ) -> None: self.app = app self.minimum_size = minimum_size self.compresslevel = compresslevel @@ -15,7 +17,9 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if scope["type"] == "http": headers = Headers(scope=scope) if "gzip" in headers.get("Accept-Encoding", ""): - responder = GZipResponder(self.app, self.minimum_size, self.compresslevel) + responder = GZipResponder( + self.app, self.minimum_size, self.compresslevel + ) await responder(scope, receive, send) return await self.app(scope, receive, send) @@ -30,7 +34,9 @@ def __init__(self, app: ASGIApp, minimum_size: int, compresslevel: int) -> None: self.initial_message = {} # type: Message self.started = False self.gzip_buffer = io.BytesIO() - self.gzip_file = gzip.GzipFile(mode="wb", fileobj=self.gzip_buffer, compresslevel=self.compresslevel) + self.gzip_file = gzip.GzipFile( + mode="wb", fileobj=self.gzip_buffer, compresslevel=self.compresslevel + ) async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: self.send = send