Skip to content

Commit

Permalink
feat: pass rawHeaders (nodejs#1240)
Browse files Browse the repository at this point in the history
* Pass raw headers

* tests

* types

* CR

Co-authored-by: Moshe Atlow <moshea@testim.io>
  • Loading branch information
2 people authored and KhafraDev committed Jun 23, 2022
1 parent 22e3a6d commit 158ca94
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 19 deletions.
8 changes: 5 additions & 3 deletions lib/api/api-connect.js
Expand Up @@ -15,7 +15,7 @@ class ConnectHandler extends AsyncResource {
throw new InvalidArgumentError('invalid callback')
}

const { signal, opaque } = opts
const { signal, opaque, responseHeaders } = opts

if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
Expand All @@ -24,6 +24,7 @@ class ConnectHandler extends AsyncResource {
super('UNDICI_CONNECT')

this.opaque = opaque || null
this.responseHeaders = responseHeaders || null
this.callback = callback
this.abort = null

Expand All @@ -43,15 +44,16 @@ class ConnectHandler extends AsyncResource {
throw new SocketError('bad connect', null)
}

onUpgrade (statusCode, headers, socket) {
onUpgrade (statusCode, rawHeaders, socket) {
const { callback, opaque, context } = this

removeSignal(this)

this.callback = null
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
this.runInAsyncScope(callback, null, null, {
statusCode,
headers: util.parseHeaders(headers),
headers,
socket,
opaque,
context
Expand Down
11 changes: 7 additions & 4 deletions lib/api/api-pipeline.js
Expand Up @@ -69,7 +69,7 @@ class PipelineHandler extends AsyncResource {
throw new InvalidArgumentError('invalid handler')
}

const { signal, method, opaque, onInfo } = opts
const { signal, method, opaque, onInfo, responseHeaders } = opts

if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
Expand All @@ -86,6 +86,7 @@ class PipelineHandler extends AsyncResource {
super('UNDICI_PIPELINE')

this.opaque = opaque || null
this.responseHeaders = responseHeaders || null
this.handler = handler
this.abort = null
this.context = null
Expand Down Expand Up @@ -156,12 +157,13 @@ class PipelineHandler extends AsyncResource {
this.context = context
}

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

if (statusCode < 200) {
if (this.onInfo) {
this.onInfo({ statusCode, headers: util.parseHeaders(headers) })
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
this.onInfo({ statusCode, headers })
}
return
}
Expand All @@ -171,9 +173,10 @@ class PipelineHandler extends AsyncResource {
let body
try {
this.handler = null
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
body = this.runInAsyncScope(handler, null, {
statusCode,
headers: util.parseHeaders(headers),
headers,
opaque,
body: this.res,
context
Expand Down
13 changes: 8 additions & 5 deletions lib/api/api-request.js
Expand Up @@ -15,7 +15,7 @@ class RequestHandler extends AsyncResource {
throw new InvalidArgumentError('invalid opts')
}

const { signal, method, opaque, body, onInfo } = opts
const { signal, method, opaque, body, onInfo, responseHeaders } = opts

try {
if (typeof callback !== 'function') {
Expand All @@ -42,6 +42,7 @@ class RequestHandler extends AsyncResource {
throw err
}

this.responseHeaders = responseHeaders || null
this.opaque = opaque || null
this.callback = callback
this.res = null
Expand Down Expand Up @@ -69,25 +70,27 @@ class RequestHandler extends AsyncResource {
this.context = context
}

onHeaders (statusCode, headers, resume) {
onHeaders (statusCode, rawHeaders, resume) {
const { callback, opaque, abort, context } = this

if (statusCode < 200) {
if (this.onInfo) {
this.onInfo({ statusCode, headers: util.parseHeaders(headers) })
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
this.onInfo({ statusCode, headers })
}
return
}

const parsedHeaders = util.parseHeaders(headers)
const parsedHeaders = util.parseHeaders(rawHeaders)
const body = new Readable(resume, abort, parsedHeaders['content-type'])

this.callback = null
this.res = body
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)

this.runInAsyncScope(callback, null, null, {
statusCode,
headers: parsedHeaders,
headers,
trailers: this.trailers,
opaque,
body,
Expand Down
11 changes: 7 additions & 4 deletions lib/api/api-stream.js
Expand Up @@ -16,7 +16,7 @@ class StreamHandler extends AsyncResource {
throw new InvalidArgumentError('invalid opts')
}

const { signal, method, opaque, body, onInfo } = opts
const { signal, method, opaque, body, onInfo, responseHeaders } = opts

try {
if (typeof callback !== 'function') {
Expand Down Expand Up @@ -47,6 +47,7 @@ class StreamHandler extends AsyncResource {
throw err
}

this.responseHeaders = responseHeaders || null
this.opaque = opaque || null
this.factory = factory
this.callback = callback
Expand Down Expand Up @@ -75,20 +76,22 @@ class StreamHandler extends AsyncResource {
this.context = context
}

onHeaders (statusCode, headers, resume) {
onHeaders (statusCode, rawHeaders, resume) {
const { factory, opaque, context } = this

if (statusCode < 200) {
if (this.onInfo) {
this.onInfo({ statusCode, headers: util.parseHeaders(headers) })
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
this.onInfo({ statusCode, headers })
}
return
}

this.factory = null
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
const res = this.runInAsyncScope(factory, null, {
statusCode,
headers: util.parseHeaders(headers),
headers,
opaque,
context
})
Expand Down
8 changes: 5 additions & 3 deletions lib/api/api-upgrade.js
Expand Up @@ -16,14 +16,15 @@ class UpgradeHandler extends AsyncResource {
throw new InvalidArgumentError('invalid callback')
}

const { signal, opaque } = opts
const { signal, opaque, responseHeaders } = opts

if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
}

super('UNDICI_UPGRADE')

this.responseHeaders = responseHeaders || null
this.opaque = opaque || null
this.callback = callback
this.abort = null
Expand All @@ -45,16 +46,17 @@ class UpgradeHandler extends AsyncResource {
throw new SocketError('bad upgrade', null)
}

onUpgrade (statusCode, headers, socket) {
onUpgrade (statusCode, rawHeaders, socket) {
const { callback, opaque, context } = this

assert.strictEqual(statusCode, 101)

removeSignal(this)

this.callback = null
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
this.runInAsyncScope(callback, null, null, {
headers: util.parseHeaders(headers),
headers,
socket,
opaque,
context
Expand Down
5 changes: 5 additions & 0 deletions lib/core/util.js
Expand Up @@ -200,6 +200,10 @@ function parseHeaders (headers, obj = {}) {
return obj
}

function parseRawHeaders (headers) {
return headers.map(header => header.toString());
}

function isBuffer (buffer) {
// See, https://github.com/mcollina/undici/pull/319
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
Expand Down Expand Up @@ -339,6 +343,7 @@ module.exports = {
isIterable,
isAsyncIterable,
isDestroyed,
parseRawHeaders,
parseHeaders,
parseKeepAliveTimeout,
destroy,
Expand Down
37 changes: 37 additions & 0 deletions test/client-request.js
Expand Up @@ -537,6 +537,43 @@ test('request onInfo callback headers parsing', async (t) => {
t.pass()
})


test('request raw responseHeaders', async (t) => {
t.plan(4)
const infos = []

const server = net.createServer((socket) => {
const lines = [
'HTTP/1.1 103 Early Hints',
'Link: </style.css>; rel=preload; as=style',
'',
'HTTP/1.1 200 OK',
'Date: Sat, 09 Oct 2010 14:28:02 GMT',
'Connection: close',
'',
'the body'
]
socket.end(lines.join('\r\n'))
})
t.teardown(server.close.bind(server))

await promisify(server.listen.bind(server))(0)

const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.close.bind(client))

const { headers } = await client.request({
path: '/',
method: 'GET',
responseHeaders: 'raw',
onInfo: (x) => { infos.push(x) }
})
t.equal(infos.length, 1)
t.same(infos[0].headers, ['Link', '</style.css>; rel=preload; as=style'])
t.same(headers, ['Date', 'Sat, 09 Oct 2010 14:28:02 GMT', 'Connection', 'close'])
t.pass()
})

test('request formData', { skip: nodeMajor < 16 }, (t) => {
t.plan(1)

Expand Down
13 changes: 13 additions & 0 deletions test/util.js
Expand Up @@ -81,3 +81,16 @@ test('validateHandler', (t) => {
onUpgrade: 'null'
}, 'CONNECT', () => {}), InvalidArgumentError, 'invalid onUpgrade method')
})

test('parseHeaders', (t) => {
t.plan(4)
t.same(util.parseHeaders(['key', 'value']), { key: 'value' })
t.same(util.parseHeaders([Buffer.from('key'), Buffer.from('value')]), { key: 'value' })
t.same(util.parseHeaders(['Key', 'Value']), { key: 'Value' })
t.same(util.parseHeaders(['Key', 'value', 'key', 'Value']), { key: ['value', 'Value'] })
})

test('parseRawHeaders', (t) => {
t.plan(1)
t.same(util.parseRawHeaders(['key', 'value', Buffer.from('key'), Buffer.from('value')]), ['key', 'value', 'key', 'value'])
})
6 changes: 6 additions & 0 deletions types/dispatcher.d.ts
Expand Up @@ -64,6 +64,8 @@ declare namespace Dispatcher {
opaque?: unknown;
/** Default: 0 */
maxRedirections?: number;
/** Default: `null` */
responseHeader?: 'raw' | null;
}
export interface RequestOptions extends DispatchOptions {
/** Default: `null` */
Expand All @@ -74,6 +76,8 @@ declare namespace Dispatcher {
maxRedirections?: number;
/** Default: `null` */
onInfo?: (info: {statusCode: number, headers: Record<string, string | string[]>}) => void;
/** Default: `null` */
responseHeader?: 'raw' | null;
}
export interface PipelineOptions extends RequestOptions {
/** `true` if the `handler` will return an object stream. Default: `false` */
Expand All @@ -91,6 +95,8 @@ declare namespace Dispatcher {
signal?: AbortSignal | EventEmitter | null;
/** Default: 0 */
maxRedirections?: number;
/** Default: `null` */
responseHeader?: 'raw' | null;
}
export interface ConnectData {
statusCode: number;
Expand Down

0 comments on commit 158ca94

Please sign in to comment.