Skip to content

Commit

Permalink
fix: h2 without body (#2258)
Browse files Browse the repository at this point in the history
* fix: h2 without body

* refactor: close writable on non-expected body requests
  • Loading branch information
metcoder95 authored Sep 10, 2023
1 parent 9fa8224 commit e39a632
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1761,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, { endStream: false, signal })
stream = session.request(headers, { endStream: shouldEndStream, signal })

stream.once('continue', writeBodyH2)
} else {
/** @type {import('node:http2').ClientHttp2Stream} */
stream = session.request(headers, { endStream: false, signal })
stream = session.request(headers, {
endStream: shouldEndStream,
signal
})
writeBodyH2()
}

Expand Down
4 changes: 3 additions & 1 deletion lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e39a632

Please sign in to comment.