Skip to content

Commit a3cd772

Browse files
author
bors-servo
committed
Auto merge of #111 - KiChjang:url-origin, r=SimonSapin
Implemented the origin method (fixes #54) Fixes #54, but still need some clarafication as to how to generate a GUID for schemes such as blob, file and others. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/111) <!-- Reviewable:end -->
2 parents f8a1344 + 08ea09c commit a3cd772

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ version = "*"
3333
optional = true
3434

3535
[dependencies]
36+
uuid = "0.1.17"
3637
rustc-serialize = "0.3"
3738
matches = "0.1"

src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ assert!(css_url.serialize() == "http://servo.github.io/rust-url/main.css".to_str
122122
#![cfg_attr(feature="heap_size", plugin(heapsize_plugin))]
123123

124124
extern crate rustc_serialize;
125+
extern crate uuid;
125126

126127
#[macro_use]
127128
extern crate matches;
@@ -148,6 +149,8 @@ use percent_encoding::{percent_encode, lossy_utf8_percent_decode, DEFAULT_ENCODE
148149
use format::{PathFormatter, UserInfoFormatter, UrlNoFragmentFormatter};
149150
use encoding::EncodingOverride;
150151

152+
use uuid::Uuid;
153+
151154
mod encoding;
152155
mod host;
153156
mod parser;
@@ -193,6 +196,20 @@ pub struct Url {
193196
pub fragment: Option<String>,
194197
}
195198

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+
196213
/// The components of the URL whose representation depends on where the scheme is *relative*.
197214
#[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
198215
#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
@@ -570,6 +587,26 @@ impl Url {
570587
self.to_string()
571588
}
572589

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+
573610
/// Return the serialization of this URL, without the fragment identifier, as a string
574611
pub fn serialize_no_fragment(&self) -> String {
575612
UrlNoFragmentFormatter{ url: self }.to_string()

0 commit comments

Comments
 (0)