Skip to content

Commit 3eb41e7

Browse files
committed
fix: 🐛 request.url getter fails when using bare IPv6 hosts ([#4556](#4556))
1 parent 303571b commit 3eb41e7

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/request.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ exports = module.exports = internals.Request = class {
183183
this._normalizePath(url, options);
184184
return url;
185185
}
186+
else {
187+
188+
const hostname = this._formatIpv6Host(this.info.host || this._core.info.host);
189+
const hostPort = this.info.host ? hostname : `${hostname}:${this._core.info.port}`;
186190

187-
this._url = new Url.URL(`${this._core.info.protocol}://${this.info.host || `${this._core.info.host}:${this._core.info.port}`}${source}`);
191+
this._url = new Url.URL(`${this._core.info.protocol}://${hostPort}${source}`);
192+
}
188193
}
189194
else {
190195

@@ -201,6 +206,25 @@ exports = module.exports = internals.Request = class {
201206
return this._url;
202207
}
203208

209+
_isBareIpv6(host) {
210+
211+
// If it's already bracketed, it's not a 'bare' IPv6 we need to wrap.
212+
213+
if (host.startsWith('[') && host.endsWith(']')) {
214+
return false;
215+
}
216+
217+
// An IPv6 address must contain at least two colons.
218+
219+
const colonCount = (host.match(/:/g) || []).length;
220+
return colonCount >= 2;
221+
}
222+
223+
_formatIpv6Host(host) {
224+
225+
return this._isBareIpv6(host) ? `[${host}]`: host;
226+
}
227+
204228
_normalizePath(url, options) {
205229

206230
let path = this._core.router.normalize(url.pathname);

0 commit comments

Comments
 (0)