From 875833ebb9092b04b270558eae6b09cdb87c406f Mon Sep 17 00:00:00 2001 From: binsee <5285894+binsee@users.noreply.github.com> Date: Wed, 11 Oct 2023 03:01:54 -0500 Subject: [PATCH] Fix stuck when using http2 POST Buffer (#2336) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 🐛 call stream.end when POST Buffer * test: ✅ add test POST Buffer for http2 * test: ✅ remove debug log --- lib/client.js | 1 + test/http2.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index be00382d0d0..065fb563380 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1862,6 +1862,7 @@ function writeH2 (client, session, request) { stream.cork() stream.write(body) stream.uncork() + stream.end() request.onBodySent(body) request.onRequestSent() } else if (util.isBlobLike(body)) { diff --git a/test/http2.js b/test/http2.js index 46f2ff6755d..7be79b021d1 100644 --- a/test/http2.js +++ b/test/http2.js @@ -17,7 +17,7 @@ const isGreaterThanv20 = gte(process.version.slice(1), '20.0.0') // https://github.com/nodejs/node/pull/41735 const hasPseudoHeadersOrderFix = gte(process.version.slice(1), '16.14.1') -plan(20) +plan(21) test('Should support H2 connection', async t => { const body = [] @@ -114,6 +114,56 @@ test('Should support H2 connection (headers as array)', async t => { t.equal(Buffer.concat(body).toString('utf8'), 'hello h2!') }) +test('Should support H2 connection(POST Buffer)', async t => { + const server = createSecureServer({ ...pem, allowHTTP1: false }) + + server.on('stream', async (stream, headers, _flags, rawHeaders) => { + t.equal(headers[':method'], 'POST') + const reqData = [] + stream.on('data', chunk => reqData.push(chunk.toString())) + await once(stream, 'end') + t.equal(reqData.join(''), 'hello!') + stream.respond({ + 'content-type': 'text/plain; charset=utf-8', + 'x-custom-h2': 'hello', + ':status': 200 + }) + stream.end('hello h2!') + }) + + server.listen(0) + await once(server, 'listening') + + const client = new Client(`https://localhost:${server.address().port}`, { + connect: { + rejectUnauthorized: false + }, + allowH2: true + }) + + t.plan(6) + t.teardown(server.close.bind(server)) + t.teardown(client.close.bind(client)) + + const sendBody = 'hello!' + const body = [] + const response = await client.request({ + path: '/', + method: 'POST', + body: sendBody + }) + + response.body.on('data', chunk => { + body.push(chunk) + }) + + await once(response.body, 'end') + t.equal(response.statusCode, 200) + t.equal(response.headers['content-type'], 'text/plain; charset=utf-8') + t.equal(response.headers['x-custom-h2'], 'hello') + t.equal(Buffer.concat(body).toString('utf8'), 'hello h2!') +}) + test('Should support H2 GOAWAY (server-side)', async t => { const body = [] const server = createSecureServer(pem)