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

feat: Introduce rawHeaders #2435

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/api/Dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo
* **onConnect** `(abort: () => void, context: object) => void` - Invoked before request is dispatched on socket. May be invoked multiple times when a request is retried when the request at the head of the pipeline fails.
* **onError** `(error: Error) => void` - Invoked when an error has occurred. May not throw.
* **onUpgrade** `(statusCode: number, headers: Buffer[], socket: Duplex) => void` (optional) - Invoked when request is upgraded. Required if `DispatchOptions.upgrade` is defined or `DispatchOptions.method === 'CONNECT'`.
* **onHeaders** `(statusCode: number, headers: Buffer[], resume: () => void, statusText: string) => boolean` - Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. Not required for `upgrade` requests.
* **onHeaders** `(statusCode: number, headers: Buffer[], resume: () => void, statusTextOrRawHeaders: string) => boolean` - Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. If h2, it is rawHeaders instead of statusText, if http/1.1 it remains as string. Not required for `upgrade` requests.
* **onData** `(chunk: Buffer) => boolean` - Invoked when response payload data is received. Not required for `upgrade` requests.
* **onComplete** `(trailers: Buffer[]) => void` - Invoked when response payload and trailers have been received and the request has completed. Not required for `upgrade` requests.
* **onBodySent** `(chunk: string | Buffer | Uint8Array) => void` - Invoked when a body chunk is sent to the server. Not required. For a stream or iterable body this will be invoked for every chunk. For other body types, it will be invoked once after the body is sent.
Expand Down Expand Up @@ -385,6 +385,7 @@ Extends: [`RequestOptions`](#parameter-requestoptions)

* **statusCode** `number`
* **headers** `Record<string, string | string[] | undefined>`
* **rawHeaders** `Record<string, string | string[] | number | undefined>` - Almost the same as **headers**, but in the case of h2 there is a pseudo-headers.
* **opaque** `unknown`
* **body** `stream.Readable`
* **context** `object`
Expand Down Expand Up @@ -479,6 +480,7 @@ The `RequestOptions.method` property should not be value `'CONNECT'`.

* **statusCode** `number`
* **headers** `Record<string, string | string[]>` - Note that all header keys are lower-cased, e. g. `content-type`.
* **rawHeaders** `Record<string, string | string[] | number | undefined>` - Almost the same as **headers**, but in the case of h2 there is a pseudo-headers.
* **body** `stream.Readable` which also implements [the body mixin from the Fetch Standard](https://fetch.spec.whatwg.org/#body-mixin).
* **trailers** `Record<string, string>` - This object starts out
as empty and will be mutated to contain trailers after `body` has emitted `'end'`.
Expand Down Expand Up @@ -646,6 +648,7 @@ Returns: `void | Promise<StreamData>` - Only returns a `Promise` if no `callback

* **statusCode** `number`
* **headers** `Record<string, string | string[] | undefined>`
* **rawHeaders** `Record<string, string | string[] | number | undefined>`- Almost the same as **headers**, but in the case of h2 there is a pseudo-headers.
* **opaque** `unknown`
* **onInfo** `({statusCode: number, headers: Record<string, string | string[]>}) => void | null` (optional) - Default: `null` - Callback collecting all the info headers (HTTP 100-199) received.

Expand Down
9 changes: 5 additions & 4 deletions lib/api/api-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ class PipelineHandler extends AsyncResource {
this.context = context
}

onHeaders (statusCode, rawHeaders, resume) {
onHeaders (statusCode, headersList, resume, statusMessageOrRawHeaders) {
const { opaque, handler, context } = this

if (statusCode < 200) {
if (this.onInfo) {
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
this.onInfo({ statusCode, headers })
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(headersList) : util.parseHeaders(headersList)
this.onInfo({ statusCode, headers, rawHeaders: typeof statusMessageOrRawHeaders === 'string' ? headers : statusMessageOrRawHeaders })
}
return
}
Expand All @@ -173,10 +173,11 @@ class PipelineHandler extends AsyncResource {
let body
try {
this.handler = null
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(headersList) : util.parseHeaders(headersList)
body = this.runInAsyncScope(handler, null, {
statusCode,
headers,
rawHeaders: typeof statusMessageOrRawHeaders === 'string' ? headers : statusMessageOrRawHeaders,
opaque,
body: this.res,
context
Expand Down
11 changes: 6 additions & 5 deletions lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ class RequestHandler extends AsyncResource {
this.context = context
}

onHeaders (statusCode, rawHeaders, resume, statusMessage) {
onHeaders (statusCode, headersList, resume, statusMessageOrRawHeaders) {
const { callback, opaque, abort, context, responseHeaders, highWaterMark } = this

const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
const headers = responseHeaders === 'raw' ? util.parseRawHeaders(headersList) : util.parseHeaders(headersList)

if (statusCode < 200) {
if (this.onInfo) {
this.onInfo({ statusCode, headers })
this.onInfo({ statusCode, headers, rawHeaders: typeof statusMessageOrRawHeaders === 'string' ? headers : statusMessageOrRawHeaders })
}
return
}

const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(headersList) : headers
const contentType = parsedHeaders['content-type']
const body = new Readable({ resume, abort, contentType, highWaterMark })

Expand All @@ -98,12 +98,13 @@ class RequestHandler extends AsyncResource {
if (callback !== null) {
if (this.throwOnError && statusCode >= 400) {
this.runInAsyncScope(getResolveErrorBodyCallback, null,
{ callback, body, contentType, statusCode, statusMessage, headers }
{ callback, body, contentType, statusCode, statusMessage: typeof statusMessageOrRawHeaders === 'string' ? statusMessageOrRawHeaders : '', headers }
)
} else {
this.runInAsyncScope(callback, null, null, {
statusCode,
headers,
rawHeaders: typeof statusMessageOrRawHeaders === 'string' ? headers : statusMessageOrRawHeaders,
trailers: this.trailers,
opaque,
body,
Expand Down
11 changes: 6 additions & 5 deletions lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ class StreamHandler extends AsyncResource {
this.context = context
}

onHeaders (statusCode, rawHeaders, resume, statusMessage) {
onHeaders (statusCode, headersList, resume, statusMessageOrRawHeaders) {
const { factory, opaque, context, callback, responseHeaders } = this

const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
const headers = responseHeaders === 'raw' ? util.parseRawHeaders(headersList) : util.parseHeaders(headersList)

if (statusCode < 200) {
if (this.onInfo) {
this.onInfo({ statusCode, headers })
this.onInfo({ statusCode, headers, rawHeaders: typeof statusMessageOrRawHeaders === 'string' ? headers : statusMessageOrRawHeaders })
}
return
}
Expand All @@ -95,13 +95,13 @@ class StreamHandler extends AsyncResource {
let res

if (this.throwOnError && statusCode >= 400) {
const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(headersList) : headers
const contentType = parsedHeaders['content-type']
res = new PassThrough()

this.callback = null
this.runInAsyncScope(getResolveErrorBodyCallback, null,
{ callback, body: res, contentType, statusCode, statusMessage, headers }
{ callback, body: res, contentType, statusCode, statusMessage: typeof statusMessageOrRawHeaders === 'string' ? statusMessageOrRawHeaders : '', headers }
)
} else {
if (factory === null) {
Expand All @@ -111,6 +111,7 @@ class StreamHandler extends AsyncResource {
res = this.runInAsyncScope(factory, null, {
statusCode,
headers,
rawHeaders: typeof statusMessageOrRawHeaders === 'string' ? headers : statusMessageOrRawHeaders,
opaque,
context
})
Expand Down
7 changes: 2 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ function writeH2 (client, session, request) {
return false
}

/** @type {import('node:http2').ClientHttp2Stream} */
let stream
const h2State = client[kHTTP2SessionState]

Expand Down Expand Up @@ -1777,14 +1778,10 @@ function writeH2 (client, session, request) {
const shouldEndStream = method === 'GET' || method === 'HEAD'
if (expectContinue) {
headers[HTTP2_HEADER_EXPECT] = '100-continue'
/**
* @type {import('node:http2').ClientHttp2Stream}
*/
stream = session.request(headers, { endStream: shouldEndStream, signal })

stream.once('continue', writeBodyH2)
} else {
/** @type {import('node:http2').ClientHttp2Stream} */
stream = session.request(headers, {
endStream: shouldEndStream,
signal
Expand All @@ -1796,7 +1793,7 @@ function writeH2 (client, session, request) {
++h2State.openStreams

stream.once('response', headers => {
if (request.onHeaders(Number(headers[HTTP2_HEADER_STATUS]), headers, stream.resume.bind(stream), '') === false) {
if (request.onHeaders(Number(headers[HTTP2_HEADER_STATUS]), util.omitPseudoHeaders(headers), stream.resume.bind(stream), headers) === false) {
stream.pause()
}
})
Expand Down
17 changes: 17 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,22 @@ function parseRawHeaders (headers) {
return ret
}

/**
* @param {Record<string, string | string[] | number | undefined>} headers
*/
function omitPseudoHeaders (headers) {
/** @type {Record<string, string | string[] | number | undefined>} */
const ret = {}
const keys = Object.keys(headers)
for (let i = 0; i < keys.length; ++i) {
const key = keys[i]
if (key.charCodeAt(0) !== 0x3a) {
ret[key] = headers[key]
}
}
return ret
}

function isBuffer (buffer) {
// See, https://github.com/mcollina/undici/pull/319
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
Expand Down Expand Up @@ -491,6 +507,7 @@ module.exports = {
isDestroyed,
parseRawHeaders,
parseHeaders,
omitPseudoHeaders,
parseKeepAliveTimeout,
destroy,
bodyLength,
Expand Down
7 changes: 4 additions & 3 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ async function httpNetworkFetch (
}
},

onHeaders (status, headersList, resume, statusText) {
onHeaders (status, headersList, resume, statusTextOrRawHeaders) {
if (status < 200) {
return
}
Expand Down Expand Up @@ -2006,7 +2006,8 @@ async function httpNetworkFetch (
}
} else {
const keys = Object.keys(headersList)
for (const key of keys) {
for (let i = 0; i < keys.length; ++i) {
const key = keys[i]
const val = headersList[key]
if (key.toLowerCase() === 'content-encoding') {
// https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1
Expand Down Expand Up @@ -2054,7 +2055,7 @@ async function httpNetworkFetch (

resolve({
status,
statusText,
statusText: typeof statusTextOrRawHeaders === 'string' ? statusTextOrRawHeaders : '',
headersList: headers[kHeadersList],
body: decoders.length
? pipeline(this.body, ...decoders, () => { })
Expand Down
28 changes: 28 additions & 0 deletions test/client-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,3 +1040,31 @@ test('pipeline abort after headers', (t) => {
})
})
})

test('rawHeaders must be equal to headers', (t) => {
t.plan(2)
const server = createServer(async (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain', 'x-powered-by': 'NodeJS' })
res.end()
})

t.teardown(server.close.bind(server))

server.listen(0, async () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

client.pipeline({
path: '/',
method: 'GET'
}, ({ body, headers, rawHeaders }) => {
t.equal(rawHeaders, headers)
return body
}).end()
.on('data', () => {})
.on('end', () => {})
.on('close', () => {
t.pass()
})
})
})
23 changes: 23 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,26 @@ test('request post body DataView', (t) => {
t.pass()
})
})

test('rawHeaders must be equal to headers', (t) => {
t.plan(1)
const server = createServer(async (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain', 'x-powered-by': 'NodeJS' })
res.end()
})

t.teardown(server.close.bind(server))

server.listen(0, async () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

const res = await client.request({
path: '/',
method: 'GET'
})

await res.body.text()
t.equal(res.rawHeaders, res.headers)
})
})
25 changes: 25 additions & 0 deletions test/client-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,28 @@ test('stream legacy needDrain', (t) => {
})
})
})

test('rawHeaders must be equal to headers', (t) => {
t.plan(2)
const server = createServer(async (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain', 'x-powered-by': 'NodeJS' })
res.end()
})

t.teardown(server.close.bind(server))

server.listen(0, async () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

client.stream({
path: '/',
method: 'GET',
opaque: []
}, (data) => {
t.equal(data.rawHeaders, data.headers)
}, () => {
t.pass()
})
})
})
42 changes: 40 additions & 2 deletions test/fetch/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const { Readable } = require('node:stream')
const { test, plan } = require('tap')
const pem = require('https-pem')

const { Client, fetch } = require('../..')
const { Client, fetch, Headers } = require('../..')

const nodeVersion = Number(process.version.split('v')[1].split('.')[0])

plan(6)
plan(7)

test('[Fetch] Issue#2311', async t => {
const expectedBody = 'hello from client!'
Expand Down Expand Up @@ -375,3 +375,41 @@ test(
t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody)
}
)

test('Issue#2415', async (t) => {
t.plan(1)
const server = createSecureServer(pem)

server.on('stream', async (stream, headers) => {
stream.respond({
':status': 200
})
stream.end('test')
})

server.listen()
await once(server, 'listening')

const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
},
allowH2: true
})

const response = await fetch(
`https://localhost:${server.address().port}/`,
// Needs to be passed to disable the reject unauthorized
{
method: 'GET',
dispatcher: client
}
)

await response.text()

t.teardown(server.close.bind(server))
t.teardown(client.close.bind(client))

t.doesNotThrow(() => new Headers(response.headers))
})
Loading