o0Ignition0o пре 6 година
родитељ
комит
446484009e
4 измењених фајлова са 41 додато и 13 уклоњено
  1. 6 6
      src/lib.rs
  2. 1 1
      src/parser.rs
  3. 5 6
      src/quirks.rs
  4. 29 0
      tests/unit.rs

+ 6 - 6
src/lib.rs

@@ -2058,12 +2058,12 @@ impl Url {
         let new_scheme_type = SchemeType::from(&parser.serialization);
         let old_scheme_type = SchemeType::from(self.scheme());
         // If url’s scheme is a special scheme and buffer is not a special scheme, then return.
-        if new_scheme_type.is_special() && !old_scheme_type.is_special() ||
+        if (new_scheme_type.is_special() && !old_scheme_type.is_special()) ||
             // If url’s scheme is not a special scheme and buffer is a special scheme, then return.
-            !new_scheme_type.is_special() && old_scheme_type.is_special() ||
+            (!new_scheme_type.is_special() && old_scheme_type.is_special()) ||
             // If url includes credentials or has a non-null port, and buffer is "file", then return.
             // If url’s scheme is "file" and its host is an empty host or null, then return.
-            new_scheme_type.is_file() && self.has_authority()
+            (new_scheme_type.is_file() && self.has_authority())
         {
             return Err(());
         }
@@ -2095,8 +2095,8 @@ impl Url {
 
         // Update the port so it can be removed
         // If it is the scheme's default
-        // We don't mind it silently failing
-        // If there was no port in the first place
+        // we don't mind it silently failing
+        // if there was no port in the first place
         let previous_port = self.port();
         let _ = self.set_port(previous_port);
 
@@ -2575,7 +2575,7 @@ fn file_url_segments_to_pathbuf(
     }
     // A windows drive letter must end with a slash.
     if bytes.len() > 2 {
-        if matches!(bytes[bytes.len() -2], b'a'..=b'z' | b'A'..=b'Z')
+        if matches!(bytes[bytes.len() - 2], b'a'..=b'z' | b'A'..=b'Z')
             && matches!(bytes[bytes.len() - 1], b':' | b'|')
         {
             bytes.push(b'/');

+ 1 - 1
src/parser.rs

@@ -908,7 +908,7 @@ impl<'a> Parser<'a> {
                 // url is special and c is U+005C (\)
                 // If @ flag is set and buffer is the empty string, validation error, return failure.
                 if let (Some(c), _) = remaining.split_first() {
-                    if c == '/' || c == '?' || c == '#' || scheme_type.is_special() && c == '\\' {
+                    if c == '/' || c == '?' || c == '#' || (scheme_type.is_special() && c == '\\') {
                         return Err(ParseError::EmptyHost);
                     }
                 }

+ 5 - 6
src/quirks.rs

@@ -157,8 +157,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
     if url.cannot_be_a_base() {
         return Err(());
     }
-    // Host parsing rules are strict,
-    // We don't want to trim the input
+    // Host parsing rules are strict we don't want to trim the input
     let input = Input::no_trim(new_hostname);
     let scheme_type = SchemeType::from(url.scheme());
     if let Ok((host, _remaining)) = Parser::parse_host(input, scheme_type) {
@@ -168,7 +167,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
                 if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
                     // Port with an empty host
                     ||!port(&url).is_empty()
-                    // Empty host with includes credentials
+                    // Empty host that includes credentials
                     || !url.username().is_empty()
                     || !url.password().unwrap_or(&"").is_empty()
                 {
@@ -224,9 +223,9 @@ pub fn set_pathname(url: &mut Url, new_pathname: &str) {
         return;
     }
     if Some('/') == new_pathname.chars().nth(0)
-        || SchemeType::from(url.scheme()).is_special()
-        // \ is a segment delimiter for 'special' URLs"
-        && Some('\\') == new_pathname.chars().nth(0)
+        || (SchemeType::from(url.scheme()).is_special()
+            // \ is a segment delimiter for 'special' URLs"
+            && Some('\\') == new_pathname.chars().nth(0))
     {
         url.set_path(new_pathname)
     } else {

+ 29 - 0
tests/unit.rs

@@ -37,6 +37,35 @@ fn test_relative_empty() {
     assert_eq!(url.as_str(), "sc://%C3%B1");
 }
 
+#[test]
+fn test_set_empty_host() {
+    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_host(None).unwrap();
+    assert_eq!(base.as_str(), "moz:/baz");
+    base.set_host(Some("servo")).unwrap();
+    assert_eq!(base.as_str(), "moz://servo/baz");
+}
+
+#[test]
+fn test_set_empty_hostname() {
+    use url::quirks;
+    let mut base: Url = "moz://foo@servo/baz".parse().unwrap();
+    assert!(
+        quirks::set_hostname(&mut base, "").is_err(),
+        "setting an empty hostname to a url with a username should fail"
+    );
+    base = "moz://:pass@servo/baz".parse().unwrap();
+    assert!(
+        quirks::set_hostname(&mut base, "").is_err(),
+        "setting an empty hostname to a url with a password should fail"
+    );
+    base = "moz://servo/baz".parse().unwrap();
+    quirks::set_hostname(&mut base, "").unwrap();
+    assert_eq!(base.as_str(), "moz:///baz");
+}
+
 macro_rules! assert_from_file_path {
     ($path: expr) => {
         assert_from_file_path!($path, $path)