Skip to content

Commit a4633be

Browse files
qsantosRicardo Monteiro
and
Ricardo Monteiro
authored
No colon when setting empty password (servo#825)
* set empty password tests * Fix empty password should not keep colon * Add test for setting empty query * Address clippy::manual_is_ascii_check * No point in decoding UTF-8 to check ASCII * Remove obsolete is_ascii_hex_digit --------- Co-authored-by: Ricardo Monteiro <[email protected]>
1 parent 1092960 commit a4633be

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

url/src/host.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn ends_in_a_number(input: &str) -> bool {
269269
} else {
270270
last
271271
};
272-
if !last.is_empty() && last.chars().all(|c| ('0'..='9').contains(&c)) {
272+
if !last.is_empty() && last.as_bytes().iter().all(|c| c.is_ascii_digit()) {
273273
return true;
274274
}
275275

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

299299
let valid_number = match r {
300-
8 => input.chars().all(|c| ('0'..='7').contains(&c)),
301-
10 => input.chars().all(|c| ('0'..='9').contains(&c)),
302-
16 => input.chars().all(|c| {
303-
('0'..='9').contains(&c) || ('a'..='f').contains(&c) || ('A'..='F').contains(&c)
304-
}),
300+
8 => input.as_bytes().iter().all(|c| (b'0'..=b'7').contains(c)),
301+
10 => input.as_bytes().iter().all(|c| c.is_ascii_digit()),
302+
16 => input.as_bytes().iter().all(|c| c.is_ascii_hexdigit()),
305303
_ => false,
306304
};
307305
if !valid_number {

url/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,8 @@ impl Url {
20692069
if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
20702070
return Err(());
20712071
}
2072-
if let Some(password) = password {
2072+
let password = password.unwrap_or_default();
2073+
if !password.is_empty() {
20732074
let host_and_after = self.slice(self.host_start..).to_owned();
20742075
self.serialization.truncate(self.username_end as usize);
20752076
self.serialization.push(':');

url/src/parser.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ impl<'a> Parser<'a> {
15171517
if c == '%' {
15181518
let mut input = input.clone();
15191519
if !matches!((input.next(), input.next()), (Some(a), Some(b))
1520-
if is_ascii_hex_digit(a) && is_ascii_hex_digit(b))
1520+
if a.is_ascii_hexdigit() && b.is_ascii_hexdigit())
15211521
{
15221522
vfn(SyntaxViolation::PercentDecode)
15231523
}
@@ -1528,11 +1528,6 @@ impl<'a> Parser<'a> {
15281528
}
15291529
}
15301530

1531-
#[inline]
1532-
fn is_ascii_hex_digit(c: char) -> bool {
1533-
matches!(c, 'a'..='f' | 'A'..='F' | '0'..='9')
1534-
}
1535-
15361531
// Non URL code points:
15371532
// U+0000 to U+0020 (space)
15381533
// " # % < > [ \ ] ^ ` { | }

url/tests/unit.rs

+35
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ fn test_set_empty_host() {
6464
assert_eq!(base.as_str(), "file://foo/share/foo/bar");
6565
}
6666

67+
#[test]
68+
fn test_set_empty_username_and_password() {
69+
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();
70+
base.set_username("").unwrap();
71+
assert_eq!(base.as_str(), "moz://:bar@servo/baz");
72+
73+
base.set_password(Some("")).unwrap();
74+
assert_eq!(base.as_str(), "moz://servo/baz");
75+
76+
base.set_password(None).unwrap();
77+
assert_eq!(base.as_str(), "moz://servo/baz");
78+
}
79+
80+
#[test]
81+
fn test_set_empty_password() {
82+
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();
83+
84+
base.set_password(Some("")).unwrap();
85+
assert_eq!(base.as_str(), "moz://foo@servo/baz");
86+
87+
base.set_password(None).unwrap();
88+
assert_eq!(base.as_str(), "moz://foo@servo/baz");
89+
}
90+
6791
#[test]
6892
fn test_set_empty_hostname() {
6993
use url::quirks;
@@ -82,6 +106,17 @@ fn test_set_empty_hostname() {
82106
assert_eq!(base.as_str(), "moz:///baz");
83107
}
84108

109+
#[test]
110+
fn test_set_empty_query() {
111+
let mut base: Url = "moz://example.com/path?query".parse().unwrap();
112+
113+
base.set_query(Some(""));
114+
assert_eq!(base.as_str(), "moz://example.com/path?");
115+
116+
base.set_query(None);
117+
assert_eq!(base.as_str(), "moz://example.com/path");
118+
}
119+
85120
macro_rules! assert_from_file_path {
86121
($path: expr) => {
87122
assert_from_file_path!($path, $path)

0 commit comments

Comments
 (0)