77from anyio import CapacityLimiter
88from anyio .lowlevel import RunVar
99from fastapi import APIRouter , HTTPException , Request , Response , status
10+ from fastapi .responses import StreamingResponse
1011from fastapi .staticfiles import StaticFiles
1112from starlette .middleware .base import BaseHTTPMiddleware , RequestResponseEndpoint
1213from starlette .routing import NoMatchFound
@@ -70,7 +71,7 @@ async def dispatch(
7071 return await call_next (request )
7172
7273 toolbar = DebugToolbar (request , call_next , self .settings )
73- response = await toolbar .process_request (request )
74+ response = t . cast ( StreamingResponse , await toolbar .process_request (request ) )
7475 content_type = response .headers .get ("Content-Type" , "" )
7576 is_html = content_type .startswith ("text/html" )
7677
@@ -84,9 +85,12 @@ async def dispatch(
8485 toolbar .generate_server_timing_header (response )
8586
8687 if is_html :
87- async for body in response .body_iterator : # type: ignore[attr-defined]
88- if not isinstance (body , bytes ):
89- body = body .encode (response .charset )
88+ body = b""
89+
90+ async for chunk in response .body_iterator :
91+ if not isinstance (chunk , bytes ):
92+ chunk = chunk .encode (response .charset )
93+ body += chunk
9094
9195 decoded = body .decode (response .charset )
9296 pattern = re .escape (self .settings .INSERT_BEFORE )
@@ -100,7 +104,7 @@ async def dispatch(
100104 async def stream () -> t .AsyncGenerator [bytes , None ]:
101105 yield body
102106
103- response .body_iterator = stream () # type: ignore[attr-defined]
107+ response .body_iterator = stream ()
104108 else :
105109 data = parse .quote (json .dumps (toolbar .refresh ()))
106110 response .set_cookie (key = "dtRefresh" , value = data )
0 commit comments