Skip to content

Commit

Permalink
Fix stuck when using http2 POST Buffer (#2336)
Browse files Browse the repository at this point in the history
* fix: πŸ› call stream.end when POST Buffer

* test: βœ… add test POST Buffer for http2

* test: βœ… remove debug log
  • Loading branch information
binsee authored Oct 11, 2023
1 parent 3a9f481 commit 06380f6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
52 changes: 51 additions & 1 deletion test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 06380f6

Please sign in to comment.