Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Sep 3, 2021
1 parent 5193d07 commit 5228306
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/canonicalize_and_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ pub fn canonicalize_hostname(value: &str) -> Result<String, ParseError> {
Ok(url::quirks::hostname(&url).to_string())
}

// Ref: https://wicg.github.io/urlpattern/#canonicalize-an-ipv6-hostname
pub fn canonicalize_ipv6_hostname(value: &str) -> Result<String, ParseError> {
let valid_ipv6 = value
.chars()
.all(|c| c.is_ascii_hexdigit() || matches!(c, '[' | ']' | ':'));
if !valid_ipv6 {
Err(ParseError::SomeRandomOtherError)
} else {
Ok(value.to_ascii_lowercase())
}
}

// Ref: https://wicg.github.io/urlpattern/#canonicalize-a-port
pub fn canonicalize_port(
value: &str,
Expand Down
34 changes: 29 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ pub struct UrlPattern {
}

impl UrlPattern {
// Ref: https://wicg.github.io/urlpattern/#dom-urlpattern-urlpattern
/// Parse a [URLPatternInput] and optionally a base url into a [UrlPattern].
pub fn parse(
input: URLPatternInput,
Expand Down Expand Up @@ -258,6 +259,23 @@ impl UrlPattern {
canonicalize_and_process::canonicalize_protocol,
Default::default(),
)?;

let hostname = if hostname_pattern_is_ipv6_address(
&processed_init.hostname.clone().unwrap(),
) {
Component::compile(
processed_init.hostname.as_deref(),
canonicalize_and_process::canonicalize_ipv6_hostname,
parser::Options::hostname(),
)?
} else {
Component::compile(
processed_init.hostname.as_deref(),
canonicalize_and_process::canonicalize_hostname,
parser::Options::hostname(),
)?
};

let pathname = if protocol.protocol_component_matches_special_scheme() {
Component::compile(
processed_init.pathname.as_deref(),
Expand All @@ -284,11 +302,7 @@ impl UrlPattern {
canonicalize_and_process::canonicalize_password,
Default::default(),
)?,
hostname: Component::compile(
processed_init.hostname.as_deref(),
canonicalize_and_process::canonicalize_hostname,
parser::Options::hostname(),
)?,
hostname,
port: Component::compile(
processed_init.port.as_deref(),
|port| {
Expand Down Expand Up @@ -484,6 +498,16 @@ impl UrlPattern {
}
}

// Ref: https://wicg.github.io/urlpattern/#hostname-pattern-is-an-ipv6-address
fn hostname_pattern_is_ipv6_address(input: &str) -> bool {
// TODO: code point length
if input.len() < 2 {
return false;
}

input.starts_with('[') || input.starts_with("{[") || input.starts_with("\\[")
}

// Ref: https://wicg.github.io/urlpattern/#dictdef-urlpatternresult
// TODO: doc
#[derive(Debug, Deserialize, Serialize)]
Expand Down

0 comments on commit 5228306

Please sign in to comment.