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 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..0152dacf9ca 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,19 @@ 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. Remove + // these skips once https://github.com/ShogunPanda/milo/issues/63 is resolved. + 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 +561,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 +590,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 +629,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() +}