|
|
@@ -6,8 +6,11 @@
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
// except according to those terms.
|
|
|
|
|
|
-use Url;
|
|
|
use host::Host;
|
|
|
+use idna::domain_to_unicode;
|
|
|
+use parser::default_port;
|
|
|
+use std::sync::Arc;
|
|
|
+use Url;
|
|
|
|
|
|
impl Url {
|
|
|
/// Return the origin of this URL (https://url.spec.whatwg.org/#origin)
|
|
|
@@ -46,7 +49,42 @@ pub enum Origin {
|
|
|
impl Origin {
|
|
|
/// Creates a new opaque origin that is only equal to itself.
|
|
|
pub fn new_opaque() -> Origin {
|
|
|
- Origin::Opaque(OpaqueOrigin(Box::new(0)))
|
|
|
+ Origin::Opaque(OpaqueOrigin(Arc::new(0)))
|
|
|
+ }
|
|
|
+
|
|
|
+ /// https://html.spec.whatwg.org/multipage/#ascii-serialisation-of-an-origin
|
|
|
+ pub fn ascii_serialization(&self) -> String {
|
|
|
+ match *self {
|
|
|
+ Origin::Opaque(_) => "null".to_owned(),
|
|
|
+ Origin::Tuple(ref scheme, ref host, port) => {
|
|
|
+ if default_port(scheme) == Some(port) {
|
|
|
+ format!("{}://{}", scheme, host)
|
|
|
+ } else {
|
|
|
+ format!("{}://{}:{}", scheme, host, port)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
|
|
|
+ pub fn unicode_serialization(&self) -> String {
|
|
|
+ match *self {
|
|
|
+ Origin::Opaque(_) => "null".to_owned(),
|
|
|
+ Origin::Tuple(ref scheme, ref host, port) => {
|
|
|
+ let host = match *host {
|
|
|
+ Host::Domain(ref domain) => {
|
|
|
+ let (domain, _errors) = domain_to_unicode(domain);
|
|
|
+ Host::Domain(domain)
|
|
|
+ }
|
|
|
+ _ => host.clone()
|
|
|
+ };
|
|
|
+ if default_port(scheme) == Some(port) {
|
|
|
+ format!("{}://{}", scheme, host)
|
|
|
+ } else {
|
|
|
+ format!("{}://{}:{}", scheme, host, port)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|