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

No colon when setting empty password #825

Merged
merged 6 commits into from
Mar 17, 2023
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
10 changes: 4 additions & 6 deletions url/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn ends_in_a_number(input: &str) -> bool {
} else {
last
};
if !last.is_empty() && last.chars().all(|c| ('0'..='9').contains(&c)) {
if !last.is_empty() && last.as_bytes().iter().all(|c| c.is_ascii_digit()) {
return true;
}

Expand Down Expand Up @@ -297,11 +297,9 @@ fn parse_ipv4number(mut input: &str) -> Result<Option<u32>, ()> {
}

let valid_number = match r {
8 => input.chars().all(|c| ('0'..='7').contains(&c)),
10 => input.chars().all(|c| ('0'..='9').contains(&c)),
16 => input.chars().all(|c| {
('0'..='9').contains(&c) || ('a'..='f').contains(&c) || ('A'..='F').contains(&c)
}),
8 => input.as_bytes().iter().all(|c| (b'0'..=b'7').contains(c)),
10 => input.as_bytes().iter().all(|c| c.is_ascii_digit()),
16 => input.as_bytes().iter().all(|c| c.is_ascii_hexdigit()),
_ => false,
};
if !valid_number {
Expand Down
3 changes: 2 additions & 1 deletion url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,8 @@ impl Url {
if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
return Err(());
}
if let Some(password) = password {
let password = password.unwrap_or_default();
if !password.is_empty() {
let host_and_after = self.slice(self.host_start..).to_owned();
self.serialization.truncate(self.username_end as usize);
self.serialization.push(':');
Expand Down
7 changes: 1 addition & 6 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ impl<'a> Parser<'a> {
if c == '%' {
let mut input = input.clone();
if !matches!((input.next(), input.next()), (Some(a), Some(b))
if is_ascii_hex_digit(a) && is_ascii_hex_digit(b))
if a.is_ascii_hexdigit() && b.is_ascii_hexdigit())
{
vfn(SyntaxViolation::PercentDecode)
}
Expand All @@ -1528,11 +1528,6 @@ impl<'a> Parser<'a> {
}
}

#[inline]
fn is_ascii_hex_digit(c: char) -> bool {
matches!(c, 'a'..='f' | 'A'..='F' | '0'..='9')
}

// Non URL code points:
// U+0000 to U+0020 (space)
// " # % < > [ \ ] ^ ` { | }
Expand Down
35 changes: 35 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ fn test_set_empty_host() {
assert_eq!(base.as_str(), "file://foo/share/foo/bar");
}

#[test]
fn test_set_empty_username_and_password() {
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();
base.set_username("").unwrap();
assert_eq!(base.as_str(), "moz://:bar@servo/baz");

base.set_password(Some("")).unwrap();
assert_eq!(base.as_str(), "moz://servo/baz");

base.set_password(None).unwrap();
assert_eq!(base.as_str(), "moz://servo/baz");
}

#[test]
fn test_set_empty_password() {
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();

base.set_password(Some("")).unwrap();
assert_eq!(base.as_str(), "moz://foo@servo/baz");

base.set_password(None).unwrap();
assert_eq!(base.as_str(), "moz://foo@servo/baz");
}

#[test]
fn test_set_empty_hostname() {
use url::quirks;
Expand All @@ -82,6 +106,17 @@ fn test_set_empty_hostname() {
assert_eq!(base.as_str(), "moz:///baz");
}

#[test]
fn test_set_empty_query() {
let mut base: Url = "moz://example.com/path?query".parse().unwrap();

base.set_query(Some(""));
assert_eq!(base.as_str(), "moz://example.com/path?");

base.set_query(None);
assert_eq!(base.as_str(), "moz://example.com/path");
}

macro_rules! assert_from_file_path {
($path: expr) => {
assert_from_file_path!($path, $path)
Expand Down