diff --git a/Cargo.toml b/Cargo.toml index a7d64ac5..edbfcae7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "url" -version = "0.5.1" +version = "0.6.0" authors = [ "Simon Sapin " ] description = "URL library for Rust, based on the WHATWG URL Standard" @@ -33,6 +33,5 @@ version = "0.6.1" optional = true [dependencies] -uuid = "0.1.17" rustc-serialize = "0.3" matches = "0.1" diff --git a/src/lib.rs b/src/lib.rs index f03c8082..d06a9cbe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,6 @@ assert!(css_url.serialize() == "http://servo.github.io/rust-url/main.css".to_str #![cfg_attr(feature="heap_size", plugin(heapsize_plugin))] extern crate rustc_serialize; -extern crate uuid; #[macro_use] extern crate matches; @@ -151,8 +150,6 @@ use percent_encoding::{percent_encode, lossy_utf8_percent_decode, DEFAULT_ENCODE use format::{PathFormatter, UserInfoFormatter, UrlNoFragmentFormatter}; use encoding::EncodingOverride; -use uuid::Uuid; - mod encoding; mod host; mod parser; @@ -194,20 +191,28 @@ pub struct Url { pub fragment: Option, } -/// Opaque identifier for URLs that have file or other schemes -#[derive(PartialEq, Eq, Clone, Debug)] -pub struct OpaqueOrigin(Uuid); - /// The origin of the URL -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(Eq, Clone, Debug)] +#[cfg_attr(feature="heap_size", derive(HeapSizeOf))] pub enum Origin { /// A globally unique identifier - UID(OpaqueOrigin), - + Opaque, /// Consists of the URL's scheme, host and port Tuple(String, Host, u16) } + +impl PartialEq for Origin { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (&Origin::Opaque, _) | (_, &Origin::Opaque) => false, + (&Origin::Tuple(ref scheme1, ref host1, ref port1), + &Origin::Tuple(ref scheme2, ref host2, ref port2)) => scheme1 == scheme2 + && host1 == host2 && port1 == port2, + } + } +} + /// The components of the URL whose representation depends on where the scheme is *relative*. #[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)] #[cfg_attr(feature="heap_size", derive(HeapSizeOf))] @@ -631,7 +636,7 @@ impl Url { let result = Url::parse(self.non_relative_scheme_data().unwrap()); match result { Ok(ref url) => url.origin(), - Err(_) => Origin::UID(OpaqueOrigin(Uuid::new_v4())) + Err(_) => Origin::Opaque, } }, "ftp" | "gopher" | "http" | "https" | "ws" | "wss" => { @@ -639,8 +644,8 @@ impl Url { self.port_or_default().unwrap()) }, // TODO: Figure out what to do if the scheme is a file - "file" => Origin::UID(OpaqueOrigin(Uuid::new_v4())), - _ => Origin::UID(OpaqueOrigin(Uuid::new_v4())) + "file" => Origin::Opaque, + _ => Origin::Opaque, } } diff --git a/tests/origin.rs b/tests/origin.rs new file mode 100644 index 00000000..07efcd82 --- /dev/null +++ b/tests/origin.rs @@ -0,0 +1,18 @@ +extern crate url; +use url::{Url, Origin}; + +#[test] +fn test_origin_eq() { + let a = Url::parse("http://example.org").unwrap(); + let b = Url::parse("http://mozilla.org").unwrap(); + assert!(a.origin() != b.origin()); + assert!(a.origin() == a.origin()); + let c = Url::parse("file:///home/user/foobar/Documents/letter.odf").unwrap(); + let d = Url::parse("file:///home/user/foobar/Images/holiday.png").unwrap(); + let c_origin = c.origin(); + assert!(c.origin() != d.origin()); + assert!(c.origin() != c.origin()); + assert!(c_origin != c_origin); + assert!(c_origin != c_origin.clone()); + assert!(Origin::Opaque != Origin::Opaque); +}