nodejs / nodejs/undici

Normalize headers in interceptors

Open
#3,860 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

Description

This would solve...

Some interceptors assume the request headers are necessarily objects: https://github.com/nodejs/undici/blob/6551919a3e492746806604cec2d241385c391f35/lib/interceptor/cache.js#L238-L242 https://github.com/nodejs/undici/blob/6551919a3e492746806604cec2d241385c391f35/lib/interceptor/dns.js#L360-L363 This creates problems when headers are not objects or when they get converted to an array, like in the redirect interceptor: https://github.com/nodejs/undici/blob/6551919a3e492746806604cec2d241385c391f35/lib/handler/redirect-handler.js#L231-L236 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'
// }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the header handling around the referenced lines in lib/interceptor/cache.js, lib/interceptor/dns.js, and lib/handler/redirect-handler.js. Reproduce the redirect-plus-DNS example first, then verify that the combined flow preserves request headers in a usable object form without numeric entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.