Skip to content

Normalize headers in interceptors #3860

@DTrombett

Description

@DTrombett

This would solve...

Some interceptors assume the request headers are necessarily objects:

headers: {
...opts.headers,
'if-modified-since': new Date(result.cachedAt).toUTCString(),
etag: result.etag
}
headers: {
host: origin.hostname,
...origDispatchOpts.headers
}
This creates problems when headers are not objects or when they get converted to an array, like in the redirect interceptor:
const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers)
for (const [key, value] of entries) {
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
ret.push(key, value)
}
}
Using the redirect handler with the dns one, in fact, will completely break headers (see example below)

The implementation should look like...

IMHO, the interceptors should convert the request headers to an object before using them, considering that it makes them easy to manipulate and most users will provide them already as objects, so no conversion is needed.

I have also considered...

An alternative could be to fix the interceptors to handle the headers based on the possible types, but this would make very difficult to implement some logics, like checking the value of some headers (which is needed in the cache interceptor)

Additional context

Using redirect and dns interceptors:

createServer((req, res) => {
	if (req.url?.endsWith("/redirect")) {
		console.log(req.headers);
		exit();
	}
	res.setHeader("location", "/redirect");
	res.statusCode = 302;
	res.end();
}).listen(8008);

request("http://localhost:8008", {
	headers: { "user-agent": "something", hello: "world" },
	dispatcher: new Agent().compose(
		interceptors.dns(),
		interceptors.redirect({ maxRedirections: 1 }),
	),
});
// This logs
// {
//   '0': 'user-agent',
//   '1': 'something',
//   '2': 'hello',
//   '3': 'world',
//   host: 'localhost',
//   connection: 'keep-alive'
// }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions