Skip to content

Commit 4114800

Browse files
committed
fix: Remove content encoding header when body is actually not gzipped
1 parent a52db64 commit 4114800

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/ai/backend/web/proxy.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,29 @@ async def web_handler(
221221
async with api_request.fetch() as up_resp:
222222
down_resp = web.StreamResponse()
223223
down_resp.set_status(up_resp.status, up_resp.reason)
224-
down_resp.headers.update(up_resp.headers)
224+
225+
final_headers = dict(up_resp.headers)
226+
227+
# figure out if the response body is gzipped by checking the first chunk
228+
first_chunk = await up_resp.read(8192)
229+
if first_chunk:
230+
body_is_actually_gzipped = first_chunk.startswith(b"\x1f\x8b")
231+
header_says_gzipped = (
232+
"gzip" in final_headers.get("Content-Encoding", "").lower()
233+
)
234+
# If reponse header says gzipped but the body is not actually gzipped,
235+
# remove the header to prevent confusion.
236+
if header_says_gzipped and not body_is_actually_gzipped:
237+
del final_headers["Content-Encoding"]
238+
225239
# We already have configured CORS handlers and the API server
226240
# also provides those headers. Just let them as-is.
241+
down_resp.headers.update(final_headers)
227242
await down_resp.prepare(request)
243+
244+
if first_chunk:
245+
await down_resp.write(first_chunk)
246+
228247
while True:
229248
chunk = await up_resp.read(8192)
230249
if not chunk:

0 commit comments

Comments
 (0)