Skip to content

Commit 71ef268

Browse files
committed
fix: pass empty chunks while waiting for entirety of body
1 parent ab01eca commit 71ef268

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/stac_auth_proxy/middleware/UpdateOpenApiMiddleware.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44

55
from starlette.types import ASGIApp, Message, Receive, Scope, Send
6+
from starlette.requests import Request
67

78
from ..config import EndpointMethods
89
from ..utils.requests import dict_to_bytes
@@ -21,20 +22,30 @@ class OpenApiMiddleware:
2122

2223
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
2324
"""Add the OpenAPI spec to the response."""
24-
if scope["type"] != "http":
25+
if scope["type"] != "http" or Request(scope).url.path != self.openapi_spec_path:
2526
return await self.app(scope, receive, send)
2627

28+
total_body = b""
29+
2730
async def augment_oidc_spec(message: Message):
2831
if message["type"] != "http.response.body":
2932
return await send(message)
3033

3134
# TODO: Make more robust to handle non-JSON responses
32-
body = json.loads(message["body"])
35+
36+
nonlocal total_body
37+
38+
total_body += message["body"]
39+
40+
# Pass empty body chunks until all chunks have been received
41+
if message["more_body"]:
42+
return await send({**message, "body": b""})
3343

3444
await send(
3545
{
3646
"type": "http.response.body",
37-
"body": dict_to_bytes(self.augment_spec(body)),
47+
"body": dict_to_bytes(self.augment_spec(json.loads(total_body))),
48+
"more_body": False,
3849
}
3950
)
4051

0 commit comments

Comments
 (0)