Parcourir la source

Auto merge of #267 - servo:ipv6-compress, r=SimonSapin

Only compress sequences of 2 or more zero segments for IPV6 hosts

(fixes #266)

cc @valenting

r? @SimonSapin

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/267)
<!-- Reviewable:end -->
bors-servo il y a 9 ans
Parent
commit
b418ab2c96
2 fichiers modifiés avec 65 ajouts et 7 suppressions
  1. 8 1
      src/host.rs
  2. 57 6
      tests/urltestdata.json

+ 8 - 1
src/host.rs

@@ -206,6 +206,7 @@ fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter) -> fmt::Result {
     Ok(())
     Ok(())
 }
 }
 
 
+// https://url.spec.whatwg.org/#concept-ipv6-serializer step 2 and 3
 fn longest_zero_sequence(pieces: &[u16; 8]) -> (isize, isize) {
 fn longest_zero_sequence(pieces: &[u16; 8]) -> (isize, isize) {
     let mut longest = -1;
     let mut longest = -1;
     let mut longest_length = -1;
     let mut longest_length = -1;
@@ -232,7 +233,13 @@ fn longest_zero_sequence(pieces: &[u16; 8]) -> (isize, isize) {
         }
         }
     }
     }
     finish_sequence!(8);
     finish_sequence!(8);
-    (longest, longest + longest_length)
+    // https://url.spec.whatwg.org/#concept-ipv6-serializer
+    // step 3: ignore lone zeroes
+    if longest_length < 2 {
+        (-1, -2)
+    } else {
+        (longest, longest + longest_length)
+    }
 }
 }
 
 
 /// https://url.spec.whatwg.org/#ipv4-number-parser
 /// https://url.spec.whatwg.org/#ipv4-number-parser

+ 57 - 6
tests/urltestdata.json

@@ -4374,17 +4374,68 @@
     "searchParams": "",
     "searchParams": "",
     "hash": "#foo%08bar"
     "hash": "#foo%08bar"
   },
   },
-  "# IPv6 compression",
+  "# IPv6 compression and serialization",
   {
   {
-    "input": "http://[fe80:cd00::cde:1257:0:211e:729c]/",
+    "input": "http://[fe80:cd00::1257:0:211e:729c]/",
     "base": "about:blank",
     "base": "about:blank",
-    "href": "http://[fe80:cd00::cde:1257:0:211e:729c]/",
-    "origin": "http://[fe80:cd00::cde:1257:0:211e:729c]",
+    "href": "http://[fe80:cd00::1257:0:211e:729c]/",
+    "origin": "http://[fe80:cd00::1257:0:211e:729c]",
     "protocol": "http:",
     "protocol": "http:",
     "username": "",
     "username": "",
     "password": "",
     "password": "",
-    "host": "[fe80:cd00::cde:1257:0:211e:729c]",
-    "hostname": "[fe80:cd00::cde:1257:0:211e:729c]",
+    "host": "[fe80:cd00::1257:0:211e:729c]",
+    "hostname": "[fe80:cd00::1257:0:211e:729c]",
+    "port": "",
+    "pathname": "/",
+    "search": "",
+    "searchParams": "",
+    "hash": ""
+  },
+  "# IPv6 compression and serialization: Compress sequences of two or more zeroes",
+  {
+    "input": "http://[fe80:cd00:0:0:1257:0:211e:729c]/",
+    "base": "about:blank",
+    "href": "http://[fe80:cd00::1257:0:211e:729c]/",
+    "origin": "http://[fe80:cd00::1257:0:211e:729c]",
+    "protocol": "http:",
+    "username": "",
+    "password": "",
+    "host": "[fe80:cd00::1257:0:211e:729c]",
+    "hostname": "[fe80:cd00::1257:0:211e:729c]",
+    "port": "",
+    "pathname": "/",
+    "search": "",
+    "searchParams": "",
+    "hash": ""
+  },
+  "# IPv6 compression and serialization: Compress longest sequence of zeroes",
+  {
+    "input": "http://[fe80:0:0:1257:0:0:0:cd00]/",
+    "base": "about:blank",
+    "href": "http://[fe80:0:0:1257::cd00]/",
+    "origin": "http://[fe80:0:0:1257::cd00]",
+    "protocol": "http:",
+    "username": "",
+    "password": "",
+    "host": "[fe80:0:0:1257::cd00]",
+    "hostname": "[fe80:0:0:1257::cd00]",
+    "port": "",
+    "pathname": "/",
+    "search": "",
+    "searchParams": "",
+    "hash": ""
+  },
+  "# IPv6 compression and serialization: Do not compress lone zeroes",
+  {
+    "input": "http://[fe80:cd00:0:cde:1257:0:211e:729c]/",
+    "base": "about:blank",
+    "href": "http://[fe80:cd00:0:cde:1257:0:211e:729c]/",
+    "origin": "http://[fe80:cd00:0:cde:1257:0:211e:729c]",
+    "protocol": "http:",
+    "username": "",
+    "password": "",
+    "host": "[fe80:cd00:0:cde:1257:0:211e:729c]",
+    "hostname": "[fe80:cd00:0:cde:1257:0:211e:729c]",
     "port": "",
     "port": "",
     "pathname": "/",
     "pathname": "/",
     "search": "",
     "search": "",