1use crate::host::Host;
10use crate::parser::default_port;
11use crate::Url;
12use alloc::borrow::ToOwned;
13use alloc::format;
14use alloc::string::String;
15use core::sync::atomic::{AtomicUsize, Ordering};
16
17pub fn url_origin(url: &Url) -> Origin {
18 let scheme = url.scheme();
19 match scheme {
20 "blob" => {
21 let result = Url::parse(url.path());
22 match result {
23 Ok(ref url) => url_origin(url),
24 Err(_) => Origin::new_opaque(),
25 }
26 }
27 "ftp" | "http" | "https" | "ws" | "wss" => Origin::Tuple(
28 scheme.to_owned(),
29 url.host().unwrap().to_owned(),
30 url.port_or_known_default().unwrap(),
31 ),
32 "file" => Origin::new_opaque(),
34 _ => Origin::new_opaque(),
35 }
36}
37
38)]
56pub enum Origin {
57 Opaque(OpaqueOrigin),
59
60 Tuple(String, Host<String>, u16),
62}
63
64impl Origin {
65 pub fn new_opaque() -> Self {
67 static COUNTER: AtomicUsize = AtomicUsize::new(0);
68 Self::Opaque(OpaqueOrigin(COUNTER.fetch_add(1, Ordering::SeqCst)))
69 }
70
71 pub fn is_tuple(&self) -> bool {
74 #[allow(non_exhaustive_omitted_patterns)] match *self {
Self::Tuple(..) => true,
_ => false,
}matches!(*self, Self::Tuple(..))
75 }
76
77 pub fn ascii_serialization(&self) -> String {
79 match *self {
80 Self::Opaque(_) => "null".to_owned(),
81 Self::Tuple(ref scheme, ref host, port) => {
82 if default_port(scheme) == Some(port) {
83 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}://{1}", scheme, host))
})format!("{scheme}://{host}")
84 } else {
85 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}://{1}:{2}", scheme, host,
port))
})format!("{scheme}://{host}:{port}")
86 }
87 }
88 }
89 }
90
91 pub fn unicode_serialization(&self) -> String {
93 match *self {
94 Self::Opaque(_) => "null".to_owned(),
95 Self::Tuple(ref scheme, ref host, port) => {
96 let host = match *host {
97 Host::Domain(ref domain) => {
98 let (domain, _errors) = idna::domain_to_unicode(domain);
99 Host::Domain(domain)
100 }
101 _ => host.clone(),
102 };
103 if default_port(scheme) == Some(port) {
104 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}://{1}", scheme, host))
})format!("{scheme}://{host}")
105 } else {
106 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}://{1}:{2}", scheme, host,
port))
})format!("{scheme}://{host}:{port}")
107 }
108 }
109 }
110 }
111}
112
113)]
115pub struct OpaqueOrigin(usize);