Skip to content

Commit 4f47a71

Browse files
committed
fix: 🐛 request.url getter fails when using HTTP2 and IPv6
1 parent 303571b commit 4f47a71

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/request.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,12 @@ exports = module.exports = internals.Request = class {
183183
this._normalizePath(url, options);
184184
return url;
185185
}
186+
else {
187+
188+
const host = this.info.host || this._formatIpv6Host(this._core.info.host, this._core.info.port);
186189

187-
this._url = new Url.URL(`${this._core.info.protocol}://${this.info.host || `${this._core.info.host}:${this._core.info.port}`}${source}`);
190+
this._url = new Url.URL(`${this._core.info.protocol}://${host}${source}`);
191+
}
188192
}
189193
else {
190194

@@ -201,6 +205,25 @@ exports = module.exports = internals.Request = class {
201205
return this._url;
202206
}
203207

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

206229
let path = this._core.router.normalize(url.pathname);
@@ -624,7 +647,7 @@ internals.Info = class {
624647
this._request = request;
625648

626649
const req = request.raw.req;
627-
const host = req.headers.host ? req.headers.host.trim() : '';
650+
const host = (req.headers.host || req.headers[':authority'] || '').trim();
628651
const received = Date.now();
629652

630653
this.received = received;

0 commit comments

Comments
 (0)