Skip to content
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
13 changes: 6 additions & 7 deletions faup/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ userinfo = ${ username ~ (":" ~ password)? ~ "@" }

// this rule is used to check if the hostname is actually
// a valid ipv4 address as hostname rule matches any ipv4
ipv4 = ${ SOI ~ (ASCII_DIGIT{1, 3} ~ "."){3} ~ ASCII_DIGIT{1, 3} ~ EOI }
zone_id = ${ "%" ~ (!"]" ~ ANY)* }
host = ${ ("[" ~ ipv6 ~ zone_id? ~ "]") | hostname }
checked_host = _{ SOI ~ (ipv6 | hostname) ~ EOI }
domain_part = ${ (!(":" | "?" | "/" | "#" | "." | WHITE_SPACE) ~ ANY)+ }
tld = ${ (!(":" | "?" | "/" | "#" | "." | WHITE_SPACE) ~ ANY)+ }
hostname = ${ (((domain_part ~ ".")+ ~ tld) | domain_part) }
ipv4 = ${ SOI ~ (ASCII_DIGIT{1, 3} ~ "."){3} ~ ASCII_DIGIT{1, 3} ~ EOI }
zone_id = ${ "%" ~ (!"]" ~ ANY)* }
host = ${ ("[" ~ ipv6 ~ zone_id? ~ "]") | hostname }
checked_host = _{ SOI ~ (ipv6 | hostname) ~ EOI }
hostname_part = ${ (!(":" | "?" | "/" | "#" | "." | WHITE_SPACE) ~ ANY)+ }
hostname = ${ (hostname_part ~ "."?)+ }

ipv6 = ${ (ASCII_HEX_DIGIT{,4}? ~ ":"){1, 7} ~ ASCII_HEX_DIGIT{,4} }

Expand Down
37 changes: 37 additions & 0 deletions faup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ impl<'url> Hostname<'url> {
}

fn from_str(hostname: &'url str) -> Self {
let hostname = hostname.trim_end_matches(".");
let suffix = suffix(hostname);

let domain = if let Some(suffix) = suffix.as_ref() {
Expand Down Expand Up @@ -1483,6 +1484,7 @@ mod tests {
"https://alex:adore-la-quiche@avec-des-œufs.be#et-des-lardons",
"https://%40lex:adore:la:quiche@%61vec-des-œufs.be/../../..some/directory/traversal/../#et-des-lardons",
"https://44.129.205.92.host.secureserver.net",
"http://shops.myshopify.com./",
];

for url in test_urls {
Expand Down Expand Up @@ -1892,4 +1894,39 @@ mod tests {
.inspect_err(|e| println!("{e}"))
.unwrap();
}

/// Test URLs with trailing dots in hostname
#[test]
fn test_url_trailing_dot() {
// URL with trailing dot in hostname
let url = Url::parse("http://shops.myshopify.com./").unwrap();
assert_eq!(url.host().unwrap().to_string(), "shops.myshopify.com");

// URL with trailing dot and path
let url = Url::parse("http://shops.myshopify.com./path").unwrap();
assert_eq!(url.host().unwrap().to_string(), "shops.myshopify.com");
assert_eq!(url.path(), Some("/path"));

// URL with trailing dot and query
let url = Url::parse("http://shops.myshopify.com.?query=value").unwrap();
assert_eq!(url.host().unwrap().to_string(), "shops.myshopify.com");
assert_eq!(url.query(), Some("query=value"));

// URL with trailing dot and fragment
let url = Url::parse("http://shops.myshopify.com.#fragment").unwrap();
assert_eq!(url.host().unwrap().to_string(), "shops.myshopify.com");
assert_eq!(url.fragment(), Some("fragment"));

// URL with trailing dot and port
let url = Url::parse("http://shops.myshopify.com.:8080").unwrap();
assert_eq!(url.host().unwrap().to_string(), "shops.myshopify.com");
assert_eq!(url.port(), Some(8080));

// URL with trailing dot and userinfo
let url = Url::parse("http://user:pass@shops.myshopify.com./").unwrap();
assert_eq!(url.host().unwrap().to_string(), "shops.myshopify.com");
let userinfo = url.userinfo().unwrap();
assert_eq!(userinfo.username(), "user");
assert_eq!(userinfo.password(), Some("pass"));
}
}
Loading