From f0755a891d9ac1c1650449f590f091e4c1e1a880 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Fri, 8 Sep 2023 23:50:22 +0200 Subject: [PATCH 1/2] fix: h2 without body --- lib/client.js | 5 +++-- lib/core/request.js | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index ebc9a6afc11..72ff9a4d098 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1687,6 +1687,7 @@ function writeH2 (client, session, request) { // we are already connected, streams are pending, first request // will create a new stream. We trigger a request to create the stream and wait until // `ready` event is triggered + // We disabled endStream to allow the user to write to the stream stream = session.request(headers, { endStream: false, signal }) if (stream.id && !stream.pending) { @@ -1766,12 +1767,12 @@ function writeH2 (client, session, request) { /** * @type {import('node:http2').ClientHttp2Stream} */ - stream = session.request(headers, { endStream: false, signal }) + stream = session.request(headers, { signal }) stream.once('continue', writeBodyH2) } else { /** @type {import('node:http2').ClientHttp2Stream} */ - stream = session.request(headers, { endStream: false, signal }) + stream = session.request(headers, { signal }) writeBodyH2() } diff --git a/lib/core/request.js b/lib/core/request.js index 3ddc7fdbe32..e3b0c7b9dbf 100644 --- a/lib/core/request.js +++ b/lib/core/request.js @@ -320,11 +320,13 @@ class Request { static [kHTTP2CopyHeaders] (raw) { const rawHeaders = raw.split('\r\n') - const headers = {} + for (const header of rawHeaders) { const [key, value] = header.split(': ') + if (value == null || value.length === 0) continue + if (headers[key]) headers[key] += `,${value}` else headers[key] = value } From dfa6fcfa2f5eb06a8487fbab70bdbc41d77366db Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Sat, 9 Sep 2023 00:04:51 +0200 Subject: [PATCH 2/2] refactor: close writable on non-expected body requests --- lib/client.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 72ff9a4d098..50d5ca34ae3 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1762,17 +1762,21 @@ function writeH2 (client, session, request) { session.ref() + const shouldEndStream = method === 'GET' || method === 'HEAD' if (expectContinue) { headers[HTTP2_HEADER_EXPECT] = '100-continue' /** * @type {import('node:http2').ClientHttp2Stream} */ - stream = session.request(headers, { signal }) + stream = session.request(headers, { endStream: shouldEndStream, signal }) stream.once('continue', writeBodyH2) } else { /** @type {import('node:http2').ClientHttp2Stream} */ - stream = session.request(headers, { signal }) + stream = session.request(headers, { + endStream: shouldEndStream, + signal + }) writeBodyH2() }