Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Origin #200

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -24,7 +24,7 @@ pub fn url_origin(url: &Url) -> Origin {
},
"ftp" | "gopher" | "http" | "https" | "ws" | "wss" => {
Origin::Tuple(scheme.to_owned(), url.host().unwrap().to_owned(),
url.port_or_known_default().unwrap())
url.port_or_known_default().unwrap(), None)
},
// TODO: Figure out what to do if the scheme is a file
"file" => Origin::new_opaque(),
@@ -39,8 +39,9 @@ pub enum Origin {
/// A globally unique identifier
Opaque(OpaqueOrigin),

/// Consists of the URL's scheme, host and port
Tuple(String, Host<String>, u16)
/// Consists of the URL's scheme, host and port, and an optional domain
/// https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-tuple

This comment has been minimized.

@Manishearth

Manishearth May 27, 2016

Member

I suspect the html spec origin might diverge from the URL spec origin, this crate is URL spec only

Tuple(String, Host<String>, u16, Option<String>)
}


@@ -61,7 +62,7 @@ impl Origin {
pub fn ascii_serialization(&self) -> String {
match *self {
Origin::Opaque(_) => "null".to_owned(),
Origin::Tuple(ref scheme, ref host, port) => {
Origin::Tuple(ref scheme, ref host, port, _) => {
if default_port(scheme) == Some(port) {
format!("{}://{}", scheme, host)
} else {
@@ -75,7 +76,7 @@ impl Origin {
pub fn unicode_serialization(&self) -> String {
match *self {
Origin::Opaque(_) => "null".to_owned(),
Origin::Tuple(ref scheme, ref host, port) => {
Origin::Tuple(ref scheme, ref host, port, _) => {
let host = match *host {
Host::Domain(ref domain) => {
let (domain, _errors) = domain_to_unicode(domain);
@@ -91,6 +92,27 @@ impl Origin {
}
}
}

/// https://html.spec.whatwg.org/multipage/browsers.html#same-origin
pub fn same_origin(&self, other: &Origin) -> bool {
self == other
}

/// https://html.spec.whatwg.org/multipage/browsers.html#same-origin
pub fn same_origin_domain(&self, other: &Origin) -> bool {
match (self, other) {
(&Origin::Opaque(ref o1), &Origin::Opaque(ref o2)) => o1 == o2,
(&Origin::Tuple(ref s1, _, _, ref d1),
&Origin::Tuple(ref s2, _, _, ref d2)) => {
match (d1, d2) {
(&Some(ref d1), &Some(ref d2)) => s1 == s2 && d1 == d2,
(&None, &None) => self == other,
_ => false,
}
}
_ => false,
}
}
}

/// Opaque identifier for URLs that have file or other schemes
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.