Skip to content

Commit 2aff77f

Browse files
Lxxyxtargos
authored andcommitted
url: fix url.format with ipv6 hostname
Fixes: #36654 PR-URL: #36665 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yash Ladha <yash@yashladha.in>
1 parent 535bbc2 commit 2aff77f

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎lib/url.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
ObjectCreate,
2727
ObjectKeys,
2828
SafeSet,
29+
StringPrototypeCharCodeAt,
2930
}=primordials;
3031

3132
const{ toASCII }=require('internal/idna');
@@ -156,6 +157,14 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
156157
returnurlObject;
157158
}
158159

160+
functionisIpv6Hostname(hostname){
161+
return(
162+
StringPrototypeCharCodeAt(hostname,0)===CHAR_LEFT_SQUARE_BRACKET&&
163+
StringPrototypeCharCodeAt(hostname,hostname.length-1)===
164+
CHAR_RIGHT_SQUARE_BRACKET
165+
);
166+
}
167+
159168
Url.prototype.parse=functionparse(url,parseQueryString,slashesDenoteHost){
160169
validateString(url,'url');
161170

@@ -364,8 +373,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
364373

365374
// If hostname begins with [ and ends with ]
366375
// assume that it's an IPv6 address.
367-
constipv6Hostname=hostname.charCodeAt(0)===CHAR_LEFT_SQUARE_BRACKET&&
368-
hostname.charCodeAt(hostname.length-1)===CHAR_RIGHT_SQUARE_BRACKET;
376+
constipv6Hostname=isIpv6Hostname(hostname);
369377

370378
// validate a little.
371379
if(!ipv6Hostname){
@@ -590,7 +598,7 @@ Url.prototype.format = function format() {
590598
host=auth+this.host;
591599
}elseif(this.hostname){
592600
host=auth+(
593-
this.hostname.includes(':') ?
601+
this.hostname.includes(':')&&!isIpv6Hostname(this.hostname)?
594602
'['+this.hostname+']' :
595603
this.hostname
596604
);

‎test/parallel/test-url-format.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ const formatTests = {
148148
host: '[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:61616',
149149
pathname: '/s/stopButton'
150150
},
151+
'http://[::]/': {
152+
href: 'http://[::]/',
153+
protocol: 'http:',
154+
hostname: '[::]',
155+
pathname: '/'
156+
},
151157

152158
// Encode context-specific delimiters in path and query, but do not touch
153159
// other non-delimiter chars like `%`.

0 commit comments

Comments
 (0)