mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 10:33:54 +00:00
fix(URL): IPv6 hostname support (#5766)
This commit is contained in:
parent
1120dfe3f2
commit
408edbb065
2 changed files with 32 additions and 3 deletions
|
@ -39,7 +39,7 @@ const MAX_PORT = 2 ** 16 - 1;
|
|||
// (LHS). e.g.
|
||||
// takePattern("https://deno.land:80", /^([a-z]+):[/]{2}/)
|
||||
// = ["http", "deno.land:80"]
|
||||
// takePattern("deno.land:80", /^([^:]+):)
|
||||
// takePattern("deno.land:80", /^(\[[0-9a-fA-F.:]{2,}\]|[^:]+)/)
|
||||
// = ["deno.land", "80"]
|
||||
function takePattern(string: string, pattern: RegExp): [string, string] {
|
||||
let capture = "";
|
||||
|
@ -80,7 +80,10 @@ function parse(url: string, isBase = true): URLParts | undefined {
|
|||
parts.username = encodeUserinfo(parts.username);
|
||||
[parts.password] = takePattern(restAuthentication, /^:(.*)/);
|
||||
parts.password = encodeUserinfo(parts.password);
|
||||
[parts.hostname, restAuthority] = takePattern(restAuthority, /^([^:]+)/);
|
||||
[parts.hostname, restAuthority] = takePattern(
|
||||
restAuthority,
|
||||
/^(\[[0-9a-fA-F.:]{2,}\]|[^:]+)/
|
||||
);
|
||||
[parts.port] = takePattern(restAuthority, /^:(.*)/);
|
||||
if (!isValidPort(parts.port)) {
|
||||
return undefined;
|
||||
|
@ -92,7 +95,11 @@ function parse(url: string, isBase = true): URLParts | undefined {
|
|||
parts.port = "";
|
||||
}
|
||||
try {
|
||||
parts.hostname = encodeHostname(parts.hostname).toLowerCase();
|
||||
const IPv6re = /^\[[0-9a-fA-F.:]{2,}\]$/;
|
||||
if (!IPv6re.test(parts.hostname)) {
|
||||
parts.hostname = encodeHostname(parts.hostname); // Non-IPv6 URLs
|
||||
}
|
||||
parts.hostname = parts.hostname.toLowerCase();
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue