From 578dbb1ffd36a29b275702793da0f3bd7bda93d8 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 1 Jan 2017 16:37:11 -0800 Subject: [PATCH] Only compress sequences of 2 or more zero segments for IPV6 hosts (fixes #266) --- src/host.rs | 9 +++++- tests/urltestdata.json | 63 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/host.rs b/src/host.rs index 4da5848c..ed67c527 100644 --- a/src/host.rs +++ b/src/host.rs @@ -206,6 +206,7 @@ fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter) -> fmt::Result { Ok(()) } +// https://url.spec.whatwg.org/#concept-ipv6-serializer step 2 and 3 fn longest_zero_sequence(pieces: &[u16; 8]) -> (isize, isize) { let mut longest = -1; let mut longest_length = -1; @@ -232,7 +233,13 @@ fn longest_zero_sequence(pieces: &[u16; 8]) -> (isize, isize) { } } 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 diff --git a/tests/urltestdata.json b/tests/urltestdata.json index d92dc8b7..36c32088 100644 --- a/tests/urltestdata.json +++ b/tests/urltestdata.json @@ -4374,17 +4374,68 @@ "searchParams": "", "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", - "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:", "username": "", "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": "", "pathname": "/", "search": "",