the redirect handler does not correctly honor relative paths in the location header
Bug Description
As outlined in RFC 7231, the Location header can feature full URLs, absolute paths, and relative paths. Undici treats relative paths like absolute paths.
Reproducible By
I noticed this while trying to use the Wikipedia API, which returns relative location headers. Here's example code:
const undici = require('undici');
(async () => {
const res = await undici.request('https://en.wikipedia.org/api/rest_v1/page/summary/bean', { maxRedirections: 10 });
console.log(res.context.history.map(item => item.href));
const { headers } = await undici.request('https://en.wikipedia.org/api/rest_v1/page/summary/bean', { maxRedirections: 0 });
console.log(headers['location']);
})();
Expected Behavior
> GET /a/b/c > Host: example.com > < HTTP/1.1 301 Moved Permanently < location: d
Should redirect to example.com/a/b/d, but undici will redirect to example.com/d.
(in the particular case above, it should return https://en.wikipedia.org/api/rest_v1/page/summary/Bean instead of https://en.wikipedia.org/Bean).
Logs & Screenshots
For the code example:
[
'https://en.wikipedia.org/api/rest_v1/page/summary/bean',
'https://en.wikipedia.org/Bean'
]
Bean
Environment
node: 16.15.1 undici: ^5.6.0
Additional context
I suspect the line where this happens is this one. Here, only the origin is passed into the URL constructor, discarding any previous path and therefore breaking for relative paths.
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.