Skip to content

Commit 2ed08e8

Browse files
committed
Rm redundant decompression
1 parent b6cf0a5 commit 2ed08e8

File tree

1 file changed

+1
-26
lines changed

1 file changed

+1
-26
lines changed

src/stac_auth_proxy/utils/middleware.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
"""Utilities for middleware response handling."""
22

3-
import gzip
43
import json
54
import re
6-
import zlib
75
from abc import ABC, abstractmethod
86
from typing import Any, Optional
97

10-
import brotli
118
from starlette.datastructures import Headers, MutableHeaders
129
from starlette.requests import Request
1310
from starlette.types import ASGIApp, Message, Receive, Scope, Send
1411

15-
# TODO: Consider using a single middleware to handle all compression/decompression
16-
ENCODING_HANDLERS = {
17-
"gzip": gzip,
18-
"deflate": zlib,
19-
"br": brotli,
20-
}
21-
2212

2313
class JsonResponseMiddleware(ABC):
2414
"""Base class for middleware that transforms JSON response bodies."""
@@ -89,32 +79,17 @@ async def process_message(message: Message) -> None:
8979
body += message["body"]
9080

9181
# Skip body chunks until all chunks have been received
92-
if message["more_body"]:
82+
if message.get("more_body"):
9383
return
9484

95-
# Handle compression/decompression
9685
headers = MutableHeaders(scope=start_message)
97-
content_encoding = headers.get("content-encoding", "").lower()
98-
handler = None
99-
if content_encoding:
100-
handler = ENCODING_HANDLERS.get(content_encoding)
101-
assert handler, f"Unsupported content encoding: {content_encoding}"
102-
body = (
103-
handler.decompress(body)
104-
if content_encoding != "deflate"
105-
else handler.decompress(body, -zlib.MAX_WBITS)
106-
)
10786

10887
# Transform the JSON body
10988
if body:
11089
data = json.loads(body)
11190
transformed = self.transform_json(data)
11291
body = json.dumps(transformed).encode()
11392

114-
# Re-compress if necessary
115-
if handler:
116-
body = handler.compress(body)
117-
11893
# Update content-length header
11994
headers["content-length"] = str(len(body))
12095
assert start_message, "Expected start_message to be set"

0 commit comments

Comments
 (0)