Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(#3023): Pass headers as array instead #3025

Merged
merged 2 commits into from Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/core/util.js
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
6 changes: 5 additions & 1 deletion lib/dispatcher/client-h2.js
Expand Up @@ -54,6 +54,10 @@ const {
}
} = http2

function parseH2Headers (headers) {
return Object.entries(headers).flat()
}

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

Expand Down Expand Up @@ -391,7 +395,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
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
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
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], 'content-type')
t.strictEqual(headers[1], 'text/plain; charset=utf-8')
t.strictEqual(headers[2], 'x-custom-h2')
t.strictEqual(headers[3], 'foo')
},
onData (chunk) {
response.push(chunk)
Expand Down