Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
fix(sse): strip stale Content-Encoding/Content-Length on non-stream forward (#2264)
Gleb Peregud955049186f22
diff --git a/open-sse/handlers/chatCore.ts b/open-sse/handlers/chatCore.tsindex 93eb0186cc..2c6c6b0a37 100644--- a/open-sse/handlers/chatCore.ts+++ b/open-sse/handlers/chatCore.ts@@ -704,6 +704,34 @@ function readStreamChunkWithTimeout(});}+/**+ * Strip hop-by-hop headers that describe the upstream wire encoding.+ *+ * `readNonStreamingResponseBody` reads (and, for compressed responses, also+ * decompresses via fetch's auto-decoder) the full upstream body into a JS+ * string before we re-emit it to the client. Once that happens, the original+ * `Content-Encoding`, `Content-Length`, and `Transfer-Encoding` all describe+ * a payload that no longer exists:+ *+ * - `Content-Length` is the *compressed* byte count, so clients honoring it+ * read only the first N bytes of the decompressed JSON and surface+ * "Unterminated string in JSON at position …" parse failures (observed+ * on gzipped Gemini responses).+ * - `Content-Encoding` advertises a compression we have already undone.+ * - `Transfer-Encoding` is hop-by-hop per RFC 7230 §6.1 and must not be+ * forwarded across a buffering proxy — its presence alongside a+ * re-emitted body is undefined behavior.+ *+ * Deleting all three lets the response framework set a fresh, correct+ * `Content-Length` (or fall back to `Transfer-Encoding: chunked`) for the+ * payload we are actually sending.+ */+export function stripStaleForwardingHeaders(headers: Headers): void {+ headers.delete("content-encoding");+ headers.delete("content-length");+ headers.delete("transfer-encoding");+}+async function readNonStreamingResponseBody(response: Response,contentType: string,@@ -3238,6 +3266,7 @@ export async function handleChatCore({const status = rawResult.response.status;const statusText = rawResult.response.statusText;const headers = new Headers(rawResult.response.headers);+ stripStaleForwardingHeaders(headers);const contentType = (headers.get("content-type") || "").toLowerCase();const payload = await readNonStreamingResponseBody(rawResult.response,diff --git a/tests/unit/chatcore-strip-stale-headers.test.ts b/tests/unit/chatcore-strip-stale-headers.test.tsnew file mode 100644index 0000000000..65b2cc71b6--- /dev/null+++ b/tests/unit/chatcore-strip-stale-headers.test.ts@@ -0,0 +1,48 @@+import test from "node:test";+import assert from "node:assert/strict";++test("stripStaleForwardingHeaders removes content-encoding/content-length/transfer-encoding", async () => {+ const { stripStaleForwardingHeaders } = await import("../../open-sse/handlers/chatCore.ts");++ const headers = new Headers({+ "content-type": "application/json",+ "content-encoding": "gzip",+ "content-length": "1234",+ "transfer-encoding": "chunked",+ "x-request-id": "abc",+ });++ stripStaleForwardingHeaders(headers);++ assert.equal(headers.get("content-encoding"), null);+ assert.equal(headers.get("content-length"), null);+ assert.equal(headers.get("transfer-encoding"), null);+ // Unrelated headers must be preserved so downstream behavior (content-type+ // negotiation, request tracing) keeps working.+ assert.equal(headers.get("content-type"), "application/json");+ assert.equal(headers.get("x-request-id"), "abc");+});++test("stripStaleForwardingHeaders is case-insensitive", async () => {+ const { stripStaleForwardingHeaders } = await import("../../open-sse/handlers/chatCore.ts");++ const headers = new Headers();+ headers.set("Content-Encoding", "gzip");+ headers.set("Content-Length", "999");+ headers.set("Transfer-Encoding", "chunked");++ stripStaleForwardingHeaders(headers);++ assert.equal(headers.get("content-encoding"), null);+ assert.equal(headers.get("content-length"), null);+ assert.equal(headers.get("transfer-encoding"), null);+});++test("stripStaleForwardingHeaders is a no-op when headers are absent", async () => {+ const { stripStaleForwardingHeaders } = await import("../../open-sse/handlers/chatCore.ts");++ const headers = new Headers({ "content-type": "application/json" });+ stripStaleForwardingHeaders(headers);++ assert.equal(headers.get("content-type"), "application/json");+});