77// except according to those terms.
88
99use crate :: host:: Host ;
10- use crate :: parser:: default_port;
1110use crate :: Url ;
1211use alloc:: borrow:: ToOwned ;
1312use alloc:: format;
1413use alloc:: string:: String ;
1514use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
1615
16+ /// Get the origin from a URL according to the specification:
17+ /// <https://url.spec.whatwg.org/#origin>
1718pub fn url_origin ( url : & Url ) -> Origin {
1819 let scheme = url. scheme ( ) ;
1920 match scheme {
21+ // > "blob"
22+ // > 1. If url’s blob URL entry is non-null, then return url’s blob URL entry’s
23+ // > environment’s origin.
24+ // > 2. Let pathURL be the result of parsing the result of URL path serializing url.
25+ // > 3. If pathURL is failure, then return a new opaque origin.
26+ // > 4. If pathURL’s scheme is "http", "https", or "file", then return pathURL’s origin.
27+ // > 5. Return a new opaque origin.
2028 "blob" => {
2129 let result = Url :: parse ( url. path ( ) ) ;
2230 match result {
2331 Ok ( ref url) => url_origin ( url) ,
2432 Err ( _) => Origin :: new_opaque ( ) ,
2533 }
2634 }
35+ // > "ftp" "http" "https" "ws" "wss": Return the tuple origin (url’s scheme, url’s host,
36+ // > url’s port, null).
37+ //
2738 "ftp" | "http" | "https" | "ws" | "wss" => Origin :: Tuple (
2839 scheme. to_owned ( ) ,
2940 url. host ( ) . unwrap ( ) . to_owned ( ) ,
30- url. port_or_known_default ( ) . unwrap ( ) ,
41+ url. port ( ) ,
3142 ) ,
43+ // > "file": Unfortunate as it is, this is left as an exercise to the reader. When in
44+ // > doubt, return a new opaque origin.
45+ //
3246 // TODO: Figure out what to do if the scheme is a file
3347 "file" => Origin :: new_opaque ( ) ,
48+ // > Otherwise: Return a new opaque origin.
3449 _ => Origin :: new_opaque ( ) ,
3550 }
3651}
@@ -58,7 +73,7 @@ pub enum Origin {
5873 Opaque ( OpaqueOrigin ) ,
5974
6075 /// Consists of the URL's scheme, host and port
61- Tuple ( String , Host < String > , u16 ) ,
76+ Tuple ( String , Host < String > , Option < u16 > ) ,
6277}
6378
6479impl Origin {
@@ -78,12 +93,11 @@ impl Origin {
7893 pub fn ascii_serialization ( & self ) -> String {
7994 match * self {
8095 Origin :: Opaque ( _) => "null" . to_owned ( ) ,
81- Origin :: Tuple ( ref scheme, ref host, port) => {
82- if default_port ( scheme) == Some ( port) {
83- format ! ( "{}://{}" , scheme, host)
84- } else {
85- format ! ( "{}://{}:{}" , scheme, host, port)
86- }
96+ Origin :: Tuple ( ref scheme, ref host, Some ( port) ) => {
97+ format ! ( "{}://{}:{}" , scheme, host, port)
98+ }
99+ Origin :: Tuple ( ref scheme, ref host, _) => {
100+ format ! ( "{}://{}" , scheme, host)
87101 }
88102 }
89103 }
@@ -100,10 +114,9 @@ impl Origin {
100114 }
101115 _ => host. clone ( ) ,
102116 } ;
103- if default_port ( scheme) == Some ( port) {
104- format ! ( "{}://{}" , scheme, host)
105- } else {
106- format ! ( "{}://{}:{}" , scheme, host, port)
117+ match port {
118+ Some ( port) => format ! ( "{}://{}:{}" , scheme, host, port) ,
119+ None => format ! ( "{}://{}" , scheme, host) ,
107120 }
108121 }
109122 }
0 commit comments