From 45b1fc07c7be5ff571653d23449461726d9e8c7a Mon Sep 17 00:00:00 2001 From: epslkslsksndnsjs-lab Date: Wed, 19 Aug 2026 12:39:43 +0800 Subject: [PATCH] fix(http bridge): let undici own Content-Length for buffered request bodies Resolves #134. On Node.js >= 24.17, undici rejects a manually supplied Content-Length header for a buffered request body (invalid content-length header), which the bridge surfaced as a 502 for buffered request bodies. The bridge no longer sets Content-Length manually on the buffered request paths (content-length-complete and chunked-complete); undici computes an accurate Content-Length from the buffered body automatically. The parsed Content-Length is still normalized during request parsing. --- host/src/qemu/http.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/host/src/qemu/http.ts b/host/src/qemu/http.ts index 550960fc..b24ee47d 100644 --- a/host/src/qemu/http.ts +++ b/host/src/qemu/http.ts @@ -731,18 +731,18 @@ export async function handleHttpDataWithWriter( url: baseRequest.url, headers: { ...baseRequest.headers, - "content-length": body.length.toString(), }, body: body.length > 0 ? body : null, }; - request.headers = { ...request.headers }; - delete request.headers["transfer-encoding"]; - if (request.body) { - request.headers["content-length"] = request.body.length.toString(); - } else { - delete request.headers["content-length"]; - } + request.headers = { ...request.headers }; + delete request.headers["transfer-encoding"]; + // Let undici compute Content-Length from the buffered body. Node >= 24.17's + // undici rejects a manually supplied Content-Length for a buffered body + // ("invalid content-length header"), which surfaced as a 502 for buffered + // request bodies (issue #134). undici sets an accurate Content-Length + // automatically for buffered/string bodies. + delete request.headers["content-length"]; httpSession.processing = true; let releaseHttpConcurrency: (() => void) | null = null; @@ -1015,11 +1015,12 @@ export async function handleHttpDataWithWriter( // Normalize framing headers for fetch. request.headers = { ...request.headers }; delete request.headers["transfer-encoding"]; - if (request.body) { - request.headers["content-length"] = request.body.length.toString(); - } else { - delete request.headers["content-length"]; - } + // Let undici compute Content-Length from the buffered body. Node >= 24.17's + // undici rejects a manually supplied Content-Length for a buffered body + // ("invalid content-length header"), which surfaced as a 502 for buffered + // request bodies (issue #134). undici sets an accurate Content-Length + // automatically for buffered/string bodies. + delete request.headers["content-length"]; httpSession.processing = true; let releaseHttpConcurrency: (() => void) | null = null;