Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: fix url.parse() for @hostname #42136

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
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
23 changes: 15 additions & 8 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,29 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
rest = rest.slice(proto.length);
}

// Figure out if it's got a host
// user@server is *always* interpreted as a hostname, and url
// Figure out if it's got a host.
// user@server is *always* interpreted as a hostname, and URL
// resolution will treat //foo/bar as host=foo,path=bar because that's
// how the browser resolves relative URLs.
// how the browser resolves relative URLs. http:@example.com is treated
// the same as http://example.com.
let slashes;
let at;
if (slashesDenoteHost || proto || hostPattern.test(rest)) {
slashes = rest.charCodeAt(0) === CHAR_FORWARD_SLASH &&
rest.charCodeAt(1) === CHAR_FORWARD_SLASH;
if (slashes && !(proto && hostlessProtocol.has(lowerProto))) {
rest = rest.slice(2);
this.slashes = true;
rest.charCodeAt(1) === CHAR_FORWARD_SLASH;
at = rest.charCodeAt(0) === CHAR_AT;
if (!(proto && hostlessProtocol.has(lowerProto))) {
if (slashes) {
rest = rest.slice(2);
this.slashes = true;
} else if (at) {
rest = rest.slice(1);
}
}
}

if (!hostlessProtocol.has(lowerProto) &&
(slashes || (proto && !slashedProtocol.has(proto)))) {
(slashes || at || (proto && !slashedProtocol.has(proto)))) {

// there's a hostname.
// the first instance of /, ?, ;, or # ends the host.
Expand Down
17 changes: 16 additions & 1 deletion test/parallel/test-url-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,22 @@ const parseTests = {
query: null,
pathname: '/everybody',
path: '/everybody',
href: '//[email protected]/everybody#to-the-limit'
href: '//[email protected]/everybody#to-the-limit',
},

'http:@localhost': {
protocol: 'http:',
slashes: null,
auth: null,
host: 'localhost',
port: null,
hostname: 'localhost',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'http://localhost/',
},
};

Expand Down