Skip to content

Commit

Permalink
refactor(#3023): Pass headers as array instead (#3025)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 authored Apr 1, 2024
1 parent 7fb8232 commit d7f10e1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 30 deletions.
3 changes: 0 additions & 3 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ function bufferToLowerCasedHeaderName (value) {
* @returns {Record<string, string | string[]>}
*/
function parseHeaders (headers, obj) {
// For H2 support
if (!Array.isArray(headers)) return headers

if (obj === undefined) obj = {}
for (let i = 0; i < headers.length; i += 2) {
const key = headerNameToString(headers[i])
Expand Down
16 changes: 15 additions & 1 deletion lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ const {
}
} = http2

function parseH2Headers (headers) {
// set-cookie is always an array. Duplicates are added to the array.
// For duplicate cookie headers, the values are joined together with '; '.
headers = Object.entries(headers).flat(2)

const result = []

for (const header of headers) {
result.push(Buffer.from(header))
}

return result
}

async function connectH2 (client, socket) {
client[kSocket] = socket

Expand Down Expand Up @@ -391,7 +405,7 @@ function writeH2 (client, request) {
const { [HTTP2_HEADER_STATUS]: statusCode, ...realHeaders } = headers
request.onResponseStarted()

if (request.onHeaders(Number(statusCode), realHeaders, stream.resume.bind(stream), '') === false) {
if (request.onHeaders(Number(statusCode), parseH2Headers(realHeaders), stream.resume.bind(stream), '') === false) {
stream.pause()
}

Expand Down
23 changes: 0 additions & 23 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2141,29 +2141,6 @@ async function httpNetworkFetch (
codings = contentEncoding.toLowerCase().split(',').map((x) => x.trim())
}
location = headersList.get('location', true)
} else {
const keys = Object.keys(rawHeaders)
for (let i = 0; i < keys.length; ++i) {
// The header names are already in lowercase.
const key = keys[i]
const value = rawHeaders[key]
if (key === 'set-cookie') {
for (let j = 0; j < value.length; ++j) {
headersList.append(key, value[j], true)
}
} else {
headersList.append(key, value, true)
}
}
// For H2, The header names are already in lowercase,
// so we can avoid the `HeadersList#get` call here.
const contentEncoding = rawHeaders['content-encoding']
if (contentEncoding) {
// https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1
// "All content-coding values are case-insensitive..."
codings = contentEncoding.toLowerCase().split(',').map((x) => x.trim()).reverse()
}
location = rawHeaders.location
}

this.body = new Readable({ read: resume })
Expand Down
3 changes: 2 additions & 1 deletion test/fetch/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('[Fetch] Issue#2311', async (t) => {
res.end(body)
})

const { strictEqual } = tspl(t, { plan: 1 })
const { strictEqual } = tspl(t, { plan: 2 })

server.listen()
await once(server, 'listening')
Expand Down Expand Up @@ -65,6 +65,7 @@ test('[Fetch] Issue#2311', async (t) => {
t.after(closeClientAndServerAsPromise(client, server))

strictEqual(responseBody, expectedBody)
strictEqual(response.headers.get('x-custom-h2'), 'foo')
})

test('[Fetch] Simple GET with h2', async (t) => {
Expand Down
6 changes: 4 additions & 2 deletions test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,10 @@ test('Should handle h2 request with body (string or buffer) - dispatch', async t
},
onHeaders (statusCode, headers) {
t.strictEqual(statusCode, 200)
t.strictEqual(headers['content-type'], 'text/plain; charset=utf-8')
t.strictEqual(headers['x-custom-h2'], 'foo')
t.strictEqual(headers[0].toString('utf-8'), 'content-type')
t.strictEqual(headers[1].toString('utf-8'), 'text/plain; charset=utf-8')
t.strictEqual(headers[2].toString('utf-8'), 'x-custom-h2')
t.strictEqual(headers[3].toString('utf-8'), 'foo')
},
onData (chunk) {
response.push(chunk)
Expand Down

0 comments on commit d7f10e1

Please sign in to comment.