From 96d22665dc8da9b553620d1f7fa40715019ec46f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 13 Nov 2026 12:01:50 +0100 Subject: [PATCH 1/3] fix(milo): port llhttp fixes for paused EOF, 304 framing and upgrades Fixes made to the llhttp path did not reach the experimental Milo parser, and CI did not run the tests with Milo enabled: - On socket end, finish() now resumes a parser paused by backpressure, completes a body whose completion was deferred, and parses what is still buffered before signalling EOF, as llhttp does since #5474. Before, a complete response paused by backpressure failed with 'Invalid EOF state'. - A 204/304 with a non-zero Content-Length is delivered without a body and the connection is not reused, as llhttp does since #5864, instead of failing with ResponseContentLengthMismatchError. - Upgrades pass statusText to onRequestUpgrade, and an upgrade handler error now errors the request, as llhttp does since #5761, instead of only destroying the socket and leaving the request without a terminal event. Milo itself rejects Content-Length on 204 responses and Transfer-Encoding on 204/304 responses (RFC 9110 section 8.6, RFC 9112 section 6.1). Skip the tests that expect llhttp's lenient handling when Milo is enabled, using a helper that reads UNDICI_USE_MILO the way the parser does. test/issue-4389.js checked only for 'true' and ran its HTTP/1.0 test with UNDICI_USE_MILO=1. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sfj4nEWQ4ovycNDjbvXcRs Signed-off-by: Matteo Collina --- lib/dispatcher/parser-h1.js | 53 +++++++++++++++++++++++++++++++++++-- test/content-length.js | 20 +++++++++++--- test/issue-4389.js | 3 ++- test/utils/milo.js | 10 +++++++ 4 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 test/utils/milo.js diff --git a/lib/dispatcher/parser-h1.js b/lib/dispatcher/parser-h1.js index b9f7c15f124..727f865461b 100644 --- a/lib/dispatcher/parser-h1.js +++ b/lib/dispatcher/parser-h1.js @@ -516,6 +516,42 @@ class H1Parser { return null } + // The peer closed the connection. If the parser was paused by + // backpressure, finish parsing before signalling EOF, as the llhttp + // parser does, otherwise a complete body that is waiting to be completed + // is reported as a truncated message. Backpressure is advisory here: the + // response keeps buffering delivered bytes, so resume across pauses and + // drain whatever is still buffered on the socket. + if (this.paused) { + let data + do { + this.milo.resume(this.parser) + this.paused = false + + if (this.directBodyCompletePending) { + this.completeDirectBody() + } + + if (this.paused || !this.parser) { + data = EMPTY_BUF + } else { + // Parse even when nothing is buffered: a response suspended after + // its headers, such as a bodiless 204/304, completes that way. + data = this.socket.read() || EMPTY_BUF + this.execute(data) + } + } while (this.paused && data.length > 0) + + if (!this.parser) { + return null + } + + if (this.paused) { + this.milo.resume(this.parser) + this.paused = false + } + } + if (this.directBodyRemaining > 0) { return new ResponseContentLengthMismatchError() } @@ -827,6 +863,18 @@ class H1Parser { this.contentLength = bodyKind === BODY_KIND_CONTENT_LENGTH ? contentLength : -1 + // Responses to HEAD and 204/304 responses never carry content. A + // non-zero Content-Length on a 204/304 means the server may also have + // sent content, which would then be parsed as the next response on + // this connection. Accept the response, but do not reuse the + // connection. Milo itself rejects Transfer-Encoding on these. + if (request.method === 'HEAD' || statusCode === 204 || statusCode === 304) { + if (request.method !== 'HEAD' && this.contentLength > 0) { + this.shouldKeepAlive = false + } + this.contentLength = -1 + } + if (this.statusCode >= 200) { const bodyTimeout = request.bodyTimeout != null @@ -1027,7 +1075,7 @@ class H1Parser { } onUpgrade (head) { - const { upgrade, client, socket, headers, statusCode } = this + const { upgrade, client, socket, headers, statusCode, statusText } = this assert(upgrade) assert(client[kSocket] === socket) @@ -1063,8 +1111,9 @@ class H1Parser { ) try { - request.onRequestUpgrade(statusCode, headers, socket) + request.onRequestUpgrade(statusCode, headers, socket, statusText) } catch (err) { + util.errorRequest(client, request, err) util.destroy(socket, err) } diff --git a/test/content-length.js b/test/content-length.js index 774bb522bd6..52344fd60c7 100644 --- a/test/content-length.js +++ b/test/content-length.js @@ -9,6 +9,7 @@ const net = require('node:net') const { once } = require('node:events') const { Readable } = require('node:stream') const { maybeWrapStream, consts } = require('./utils/async-iterators') +const { miloEnabled } = require('./utils/milo') test('request invalid content-length', async (t) => { t = tspl(t, { plan: 7 }) @@ -515,7 +516,18 @@ function dispatchPaused (client) { } for (const statusCode of [204, 304]) { - test(`response ${statusCode} with content-length is not a mismatch but closes the connection`, async (t) => { + // Milo follows RFC 9110 strictly and fails the response instead. + const miloSkip = (framing) => { + if (!miloEnabled) { + return false + } + if (framing === 'transfer-encoding') { + return 'Milo rejects Transfer-Encoding on 204 and 304 responses' + } + return statusCode === 204 && 'Milo rejects Content-Length on 204 responses' + } + + test(`response ${statusCode} with content-length is not a mismatch but closes the connection`, { skip: miloSkip('content-length') }, async (t) => { let connections = 0 const server = createRawServer(t, (socket) => { connections++ @@ -548,7 +560,7 @@ for (const statusCode of [204, 304]) { assert.ok(disconnects.every((code) => code === 'UND_ERR_INFO')) }) - test(`response ${statusCode} with content-length: 0 keeps the connection`, async (t) => { + test(`response ${statusCode} with content-length: 0 keeps the connection`, { skip: miloSkip('content-length') }, async (t) => { let connections = 0 const server = createRawServer(t, (socket) => { connections++ @@ -577,7 +589,7 @@ for (const statusCode of [204, 304]) { for (const framing of ['content-length', 'transfer-encoding']) { for (const blocking of [true, false]) { - test(`response ${statusCode} with ${framing} does not let its content become a pipelined response (blocking: ${blocking})`, async (t) => { + test(`response ${statusCode} with ${framing} does not let its content become a pipelined response (blocking: ${blocking})`, { skip: miloSkip(framing) }, async (t) => { const forged = 'HTTP/1.1 200 OK\r\ncontent-length: 6\r\n\r\nFORGED' let connections = 0 const server = createRawServer(t, (socket) => { @@ -616,7 +628,7 @@ for (const statusCode of [204, 304]) { } } - test(`response ${statusCode} with content-length completes when the connection closes while paused`, async (t) => { + test(`response ${statusCode} with content-length completes when the connection closes while paused`, { skip: miloSkip('content-length') }, async (t) => { const server = createRawServer(t, (socket) => { socket.once('data', () => { socket.end(`HTTP/1.1 ${statusCode} X\r\ncontent-length: 100\r\nconnection: close\r\n\r\n`) diff --git a/test/issue-4389.js b/test/issue-4389.js index e2d1f902de8..1def7e954c9 100644 --- a/test/issue-4389.js +++ b/test/issue-4389.js @@ -5,6 +5,7 @@ const { once } = require('node:events') const { createServer: createHttpServer } = require('node:http') const { createServer: createSocketServer } = require('node:net') const { fetch, Agent } = require('..') +const { miloEnabled } = require('./utils/milo') describe('Interceptor should not break multi-value headers', () => { test('Interceptor should not break Set-Cookie', async (t) => { @@ -29,7 +30,7 @@ describe('Interceptor should not break multi-value headers', () => { test('Interceptor should not break other multi-value header', { // Milo intentionally does not support HTTP/1.0 responses. - skip: process.env.UNDICI_USE_MILO === 'true' && 'Milo does not support HTTP/1.0 responses' + skip: miloEnabled && 'Milo does not support HTTP/1.0 responses' }, async (t) => { const server = createSocketServer((socket) => { socket.write('HTTP/1.0 204 No Content\r\n') diff --git a/test/utils/milo.js b/test/utils/milo.js new file mode 100644 index 00000000000..8baad0c0f7c --- /dev/null +++ b/test/utils/milo.js @@ -0,0 +1,10 @@ +'use strict' + +const { getMiloEnabled } = require('../../lib/dispatcher/parser-h1.js') + +// Whether the HTTP/1.1 clients in this process use the experimental Milo +// parser, as decided by UNDICI_USE_MILO. Use it to skip tests for behaviors +// that Milo intentionally does not support. +module.exports = { + miloEnabled: getMiloEnabled() +} From a6ec467d5212bf03cc7d96c16584644eb102451f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 13 Nov 2026 12:01:56 +0100 Subject: [PATCH 2/3] ci: run the test suite with the Milo parser Add a milo input to the reusable Node.js workflow, passed to every test step as UNDICI_USE_MILO, and a test-with-milo job that runs the suite with Milo on ubuntu-latest for every tested Node.js version, like the WASM SIMD job. Automerge waits for it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sfj4nEWQ4ovycNDjbvXcRs Signed-off-by: Matteo Collina --- .github/workflows/ci.yml | 16 ++++++++++++++++ .github/workflows/nodejs.yml | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 38011b28b88..70742e84bc6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,6 +83,21 @@ jobs: no-wasm-simd: '1' secrets: inherit + test-with-milo: + name: Test with Node.js ${{ matrix.node-version }} on ${{ matrix.runs-on }} with Milo + strategy: + fail-fast: false + max-parallel: 0 + matrix: + node-version: ['22', '24', '25', '26'] + runs-on: ['ubuntu-latest'] + uses: ./.github/workflows/nodejs.yml + with: + node-version: ${{ matrix.node-version }} + runs-on: ${{ matrix.runs-on }} + milo: '1' + secrets: inherit + test-without-intl: name: Test with Node.js ${{ matrix.node-version }} compiled --without-intl strategy: @@ -315,6 +330,7 @@ jobs: - test - test-types - test-with-no-wasm-simd + - test-with-milo - test-without-intl - test-fuzzing - lint diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 8c3ac444bdd..de2a5cd5dc1 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -17,6 +17,10 @@ on: type: string required: false default: '' + milo: + type: string + required: false + default: '' permissions: contents: read @@ -85,6 +89,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test node-test run: npm run test:node-test @@ -93,6 +98,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test fetch run: npm run test:fetch @@ -101,6 +107,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test node-fetch run: npm run test:node-fetch @@ -109,6 +116,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test cache run: npm run test:cache @@ -117,6 +125,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test cache-interceptor with sqlite run: npm run test:cache-interceptor @@ -126,6 +135,7 @@ jobs: NODE_OPTIONS: --experimental-sqlite NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test cache-tests run: npm run test:cache-tests @@ -134,6 +144,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test cookies run: npm run test:cookies @@ -142,6 +153,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test interceptors run: npm run test:interceptors @@ -150,6 +162,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test eventsource run: npm run test:eventsource @@ -158,6 +171,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test infra run: npm run test:infra @@ -166,6 +180,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test subresource-integrity run: npm run test:subresource-integrity @@ -174,6 +189,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test websocket run: npm run test:websocket @@ -182,6 +198,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test jest run: npm run test:jest @@ -190,6 +207,7 @@ jobs: CI: true NODE_V8_COVERAGE: '' UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Test wpt run: npm run test:wpt @@ -198,6 +216,7 @@ jobs: CI: true NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }} UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }} + UNDICI_USE_MILO: ${{ inputs.milo }} - name: Coverage summary if: inputs.codecov == true From 4db6845cd2df319ade7b48070fc10c4e8d5bd303 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 13 Nov 2026 12:54:56 +0100 Subject: [PATCH 3/3] test: link the Milo issue from the 204/304 framing skips Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sfj4nEWQ4ovycNDjbvXcRs Signed-off-by: Matteo Collina --- test/content-length.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/content-length.js b/test/content-length.js index 52344fd60c7..0152dacf9ca 100644 --- a/test/content-length.js +++ b/test/content-length.js @@ -516,7 +516,8 @@ function dispatchPaused (client) { } for (const statusCode of [204, 304]) { - // Milo follows RFC 9110 strictly and fails the response instead. + // Milo follows RFC 9110 strictly and fails the response instead. Remove + // these skips once https://github.com/ShogunPanda/milo/issues/63 is resolved. const miloSkip = (framing) => { if (!miloEnabled) { return false