Add ImmutableOrigin to allow for serializing origins

This commit is contained in:
Connor Brewster 2017-01-20 13:21:23 -06:00 committed by Alan Jeffrey
parent 4f7e422054
commit bfd7b950ad
21 changed files with 270 additions and 152 deletions

View file

@ -10,11 +10,14 @@ name = "servo_url"
path = "lib.rs"
[features]
servo = ["heapsize", "heapsize_derive", "serde", "url/heap_size", "url_serde"]
servo = ["heapsize", "heapsize_derive", "serde", "serde_derive", "uuid/serde", "url/heap_size", "url_serde"]
[dependencies]
heapsize = {version = "0.3.0", optional = true}
heapsize_derive = {version = "0.1", optional = true}
serde = {version = "0.9", optional = true}
serde_derive = {version = "0.9", optional = true}
servo_rand = {path = "../rand"}
url = "1.2"
url_serde = {version = "0.1", optional = true}
url_serde = {version = "0.1.3", optional = true}
uuid = {version = "0.4.0", features = ["v4"]}

View file

@ -7,19 +7,26 @@
#![crate_name = "servo_url"]
#![crate_type = "rlib"]
#[cfg(feature = "servo")] extern crate heapsize;
#[cfg(feature = "servo")] #[macro_use] extern crate heapsize;
#[cfg(feature = "servo")] #[macro_use] extern crate heapsize_derive;
#[cfg(feature = "servo")] extern crate serde;
#[cfg(feature = "servo")] #[macro_use] extern crate serde_derive;
#[cfg(feature = "servo")] extern crate url_serde;
extern crate servo_rand;
extern crate url;
extern crate uuid;
pub mod origin;
pub use origin::{OpaqueOrigin, ImmutableOrigin, MutableOrigin};
use std::fmt;
use std::net::IpAddr;
use std::ops::{Range, RangeFrom, RangeTo, RangeFull, Index};
use std::path::Path;
use std::sync::Arc;
use url::{Url, Origin, Position};
use url::{Url, Position};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
@ -68,8 +75,8 @@ impl ServoUrl {
self.0.path()
}
pub fn origin(&self) -> Origin {
self.0.origin()
pub fn origin(&self) -> ImmutableOrigin {
ImmutableOrigin::new(self.0.origin())
}
pub fn scheme(&self) -> &str {

172
components/url/origin.rs Normal file
View file

@ -0,0 +1,172 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use servo_rand;
use servo_rand::Rng;
use std::cell::RefCell;
use std::rc::Rc;
use url::{Host, Origin};
#[cfg(feature = "servo")] use url_serde;
use uuid::Uuid;
/// The origin of an URL
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub enum ImmutableOrigin {
/// A globally unique identifier
Opaque(OpaqueOrigin),
/// Consists of the URL's scheme, host and port
Tuple(
String,
#[cfg_attr(feature = "servo",
serde(deserialize_with = "url_serde::deserialize", serialize_with = "url_serde::serialize"))]
Host,
u16,
)
}
impl ImmutableOrigin {
pub fn new(origin: Origin) -> ImmutableOrigin {
match origin {
Origin::Opaque(_) => ImmutableOrigin::new_opaque(),
Origin::Tuple(scheme, host, port) => ImmutableOrigin::Tuple(scheme, host, port),
}
}
pub fn same_origin(&self, other: &MutableOrigin) -> bool {
self == other.immutable()
}
pub fn same_origin_domain(&self, other: &MutableOrigin) -> bool {
!other.has_domain() && self == other.immutable()
}
/// Creates a new opaque origin that is only equal to itself.
pub fn new_opaque() -> ImmutableOrigin {
ImmutableOrigin::Opaque(OpaqueOrigin(servo_rand::thread_rng().gen()))
}
pub fn scheme(&self) -> Option<&str> {
match *self {
ImmutableOrigin::Opaque(_) => None,
ImmutableOrigin::Tuple(ref scheme, _, _) => Some(&**scheme),
}
}
pub fn host(&self) -> Option<&Host> {
match *self {
ImmutableOrigin::Opaque(_) => None,
ImmutableOrigin::Tuple(_, ref host, _) => Some(host),
}
}
pub fn port(&self) -> Option<u16> {
match *self {
ImmutableOrigin::Opaque(_) => None,
ImmutableOrigin::Tuple(_, _, port) => Some(port),
}
}
pub fn into_url_origin(self) -> Origin {
match self {
ImmutableOrigin::Opaque(_) => Origin::new_opaque(),
ImmutableOrigin::Tuple(scheme, host, port) => Origin::Tuple(scheme, host, port),
}
}
/// Return whether this origin is a (scheme, host, port) tuple
/// (as opposed to an opaque origin).
pub fn is_tuple(&self) -> bool {
match *self {
ImmutableOrigin::Opaque(..) => false,
ImmutableOrigin::Tuple(..) => true,
}
}
/// https://html.spec.whatwg.org/multipage/#ascii-serialisation-of-an-origin
pub fn ascii_serialization(&self) -> String {
self.clone().into_url_origin().ascii_serialization()
}
/// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
pub fn unicode_serialization(&self) -> String {
self.clone().into_url_origin().unicode_serialization()
}
}
/// Opaque identifier for URLs that have file or other schemes
#[derive(Eq, PartialEq, Clone, Debug)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
pub struct OpaqueOrigin(Uuid);
#[cfg(feature = "servo")]
known_heap_size!(0, OpaqueOrigin);
/// A representation of an [origin](https://html.spec.whatwg.org/multipage/#origin-2).
#[derive(Clone, Debug)]
pub struct MutableOrigin(Rc<(ImmutableOrigin, RefCell<Option<Host>>)>);
#[cfg(feature = "servo")]
known_heap_size!(0, MutableOrigin);
impl MutableOrigin {
pub fn new(origin: ImmutableOrigin) -> MutableOrigin {
MutableOrigin(Rc::new((origin, RefCell::new(None))))
}
pub fn immutable(&self) -> &ImmutableOrigin {
&(self.0).0
}
pub fn is_tuple(&self) -> bool {
self.immutable().is_tuple()
}
pub fn scheme(&self) -> Option<&str> {
self.immutable().scheme()
}
pub fn host(&self) -> Option<&Host> {
self.immutable().host()
}
pub fn port(&self) -> Option<u16> {
self.immutable().port()
}
pub fn same_origin(&self, other: &MutableOrigin) -> bool {
self.immutable() == other.immutable()
}
pub fn same_origin_domain(&self, other: &MutableOrigin) -> bool {
if let Some(ref self_domain) = *(self.0).1.borrow() {
if let Some(ref other_domain) = *(other.0).1.borrow() {
self_domain == other_domain &&
self.immutable().scheme() == other.immutable().scheme()
} else {
false
}
} else {
self.immutable().same_origin_domain(other)
}
}
pub fn domain(&self) -> Option<Host> {
(self.0).1.borrow().clone()
}
pub fn set_domain(&self, domain: Host) {
*(self.0).1.borrow_mut() = Some(domain);
}
pub fn has_domain(&self) -> bool {
(self.0).1.borrow().is_some()
}
pub fn effective_domain(&self) -> Option<Host> {
self.immutable().host()
.map(|host| self.domain().unwrap_or_else(|| host.clone()))
}
}