Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/http-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
): void {
const { proxy } = this;
const protocol = opts.secureEndpoint ? 'https:' : 'http:';
const hostname = req.getHeader('host') || 'localhost';

let hostname = req.getHeader('host');
// If host header is not set or available in the processed headers, construct it from opts.host and opts.port
if (!hostname) {
const host = opts.host || 'localhost';
const port = opts.port || '';

// Wrap IPv6 addresses in brackets for proper hostname format
const formattedHost = net.isIPv6(host) ? `[${host}]` : host;
hostname = port ? `${formattedHost}:${port}` : formattedHost;
}

const base = `${protocol}//${hostname}`;
const url = new URL(req.path, base);
if (opts.port !== 80) {
Expand Down