DNS interceptor breaks headers of type `[string, string][]`
Bug Description
Unable to use headers of type [string, string][], using Record<string, string> works (see below).
Reproducible By
import http from "node:http";
import { Agent, interceptors, request } from "undici";
const port = process.env.PORT ?? 3001;
const { cache, dns } = interceptors;
const good = new Agent().compose(cache());
const fail = new Agent().compose(cache(), dns());
http
.createServer((req, res) => {
console.log(req.headers);
res.end();
})
.listen(port, async () => {
const headers = [["foo", "bar"]];
await request(`http://localhost:${port}`, {
dispatcher: good,
headers,
});
await request(`http://localhost:${port}`, {
dispatcher: fail,
headers,
});
const headersAsRecord = { foo: "bar" };
await request(`http://localhost:${port}`, {
dispatcher: fail,
headers: headersAsRecord,
});
});
node index.js
{ host: 'localhost:3001', connection: 'keep-alive', foo: 'bar' }
{ '0': 'foo, bar', host: 'localhost:3001', connection: 'keep-alive' }
{ host: 'localhost:3001', connection: 'keep-alive', foo: 'bar' }
The problem seems to be here: https://github.com/nodejs/undici/blob/main/lib/interceptor/dns.js#L416
Maybe I should switch all my code to use IncomingHttpHeaders (which still allows multiple values)
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>
Which type do you recommend for headers?
I agree with you.. there should be proper handling of headers. I think we have some functions which takes care of headers being processed properly. we just need to use them in the interceptor.
I agree with you.. there should be proper handling of headers. I think we have some functions which takes care of headers being processed properly. we just need to use them in the interceptor.
Or just normalize the headers into one normal view and use this structure everywhere after the request, in each interceptor to check the type and format of the headers so-so
https://github.com/nodejs/undici/issues/4336
We dont normalize headers for performance reasons.
In any case, the headers are normalized when sending requests.
https://github.com/nodejs/undici/blob/aec609b0fc595b9d99079dfce0bb0010f7a9ca7f/lib/core/request.js#L165
@Uzlopak could you elaborate? If the overhead isnt too bad I would be +1 for closing #4336 as well. If not we can just make changes to the dns interceptor
Let's make adjustments to the dns interceptor; it should handle the headers in any way this is possibly set, not break them
We found another constellation that can lead to this problem. If you use dns in combination with redirect you can get the same issues if a redirect happens during the request regardless of the provided header format. This seems to happen due to a header cleanup inside of the RedirectHandler logic.
I opened a fix for this here: https://github.com/nodejs/undici/pull/4863\n\nIt keeps DNS host-header behavior but preserves tuple/iterable header inputs by normalizing them before redispatch, and includes a regression test in .
Small correction to my previous comment (shell escaped it badly):\n\nThe PR is https://github.com/nodejs/undici/pull/4863.\n\nIt preserves tuple/iterable header inputs while still setting the original DNS host header, and adds a regression test in dns interceptor tests.