@@ -122,6 +122,7 @@ assert!(css_url.serialize() == "http://servo.github.io/rust-url/main.css".to_str
122
122
#![ cfg_attr( feature="heap_size" , plugin( heapsize_plugin) ) ]
123
123
124
124
extern crate rustc_serialize;
125
+ extern crate uuid;
125
126
126
127
#[ macro_use]
127
128
extern crate matches;
@@ -148,6 +149,8 @@ use percent_encoding::{percent_encode, lossy_utf8_percent_decode, DEFAULT_ENCODE
148
149
use format:: { PathFormatter , UserInfoFormatter , UrlNoFragmentFormatter } ;
149
150
use encoding:: EncodingOverride ;
150
151
152
+ use uuid:: Uuid ;
153
+
151
154
mod encoding;
152
155
mod host;
153
156
mod parser;
@@ -193,6 +196,20 @@ pub struct Url {
193
196
pub fragment : Option < String > ,
194
197
}
195
198
199
+ /// Opaque identifier for URLs that have file or other schemes
200
+ #[ derive( PartialEq , Eq , Clone , Debug ) ]
201
+ pub struct OpaqueOrigin ( Uuid ) ;
202
+
203
+ /// The origin of the URL
204
+ #[ derive( PartialEq , Eq , Clone , Debug ) ]
205
+ pub enum Origin {
206
+ /// A globally unique identifier
207
+ UID ( OpaqueOrigin ) ,
208
+
209
+ /// Consists of the URL's scheme, host and port
210
+ Tuple ( String , Host , u16 )
211
+ }
212
+
196
213
/// The components of the URL whose representation depends on where the scheme is *relative*.
197
214
#[ derive( PartialEq , Eq , Clone , Debug , Hash , PartialOrd , Ord ) ]
198
215
#[ cfg_attr( feature="heap_size" , derive( HeapSizeOf ) ) ]
@@ -570,6 +587,26 @@ impl Url {
570
587
self . to_string ( )
571
588
}
572
589
590
+ // Return the origin of this URL (https://url.spec.whatwg.org/#origin)
591
+ pub fn origin ( & self ) -> Origin {
592
+ match & * self . scheme {
593
+ "blob" => {
594
+ let result = Url :: parse ( self . non_relative_scheme_data ( ) . unwrap ( ) ) ;
595
+ match result {
596
+ Ok ( ref url) => url. origin ( ) ,
597
+ Err ( _) => Origin :: UID ( OpaqueOrigin ( Uuid :: new_v4 ( ) ) )
598
+ }
599
+ } ,
600
+ "ftp" | "gopher" | "http" | "https" | "ws" | "wss" => {
601
+ Origin :: Tuple ( self . scheme . clone ( ) , self . host ( ) . unwrap ( ) . clone ( ) ,
602
+ self . port_or_default ( ) . unwrap ( ) )
603
+ } ,
604
+ // TODO: Figure out what to do if the scheme is a file
605
+ "file" => Origin :: UID ( OpaqueOrigin ( Uuid :: new_v4 ( ) ) ) ,
606
+ _ => Origin :: UID ( OpaqueOrigin ( Uuid :: new_v4 ( ) ) )
607
+ }
608
+ }
609
+
573
610
/// Return the serialization of this URL, without the fragment identifier, as a string
574
611
pub fn serialize_no_fragment ( & self ) -> String {
575
612
UrlNoFragmentFormatter { url : self } . to_string ( )
0 commit comments