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 4 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
8 changes: 3 additions & 5 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.chars().all(|c| c.is_ascii_digit()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace chars() with as_bytes() here and lower, as it expects for ASCII range only, which is probably faster, than walking over utf8 code points.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, very good point!

return true;
}

Expand Down Expand Up @@ -298,10 +298,8 @@ 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)
}),
10 => input.chars().all(|c| c.is_ascii_digit()),
16 => input.chars().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
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