http: trim off brackets from IPv6 addresses with string operations

This is simpler than using regular expressions.

PR-URL: https://github.com/nodejs/node/pull/59420
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Krishnadas PC 2025-08-20 17:04:26 +05:30 committed by GitHub
parent ebbdea29ac
commit 6c215fb746
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -112,7 +112,9 @@ class ProxyConfig {
const { host, hostname, port, protocol, username, password } = new URL(proxyUrl);
this.href = proxyUrl; // Full URL of the proxy server.
this.host = host; // Full host including port, e.g. 'localhost:8080'.
this.hostname = hostname.replace(/^\[|\]$/g, ''); // Trim off the brackets from IPv6 addresses.
// Trim off the brackets from IPv6 addresses. As it's parsed from a valid URL, an opening
// "[" Must already have a matching "]" at the end.
this.hostname = hostname[0] === '[' ? hostname.slice(1, -1) : hostname;
this.port = port ? NumberParseInt(port, 10) : (protocol === 'https:' ? 443 : 80);
this.protocol = protocol; // Protocol of the proxy server, e.g. 'http:' or 'https:'.