7
7
// except according to those terms.
8
8
9
9
use crate :: host:: Host ;
10
- use crate :: parser:: default_port;
11
10
use crate :: Url ;
12
11
use alloc:: borrow:: ToOwned ;
13
12
use alloc:: format;
14
13
use alloc:: string:: String ;
15
14
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
16
15
16
+ /// Get the origin from a URL according to the specification:
17
+ /// <https://url.spec.whatwg.org/#origin>
17
18
pub fn url_origin ( url : & Url ) -> Origin {
18
19
let scheme = url. scheme ( ) ;
19
20
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.
20
28
"blob" => {
21
29
let result = Url :: parse ( url. path ( ) ) ;
22
30
match result {
23
31
Ok ( ref url) => url_origin ( url) ,
24
32
Err ( _) => Origin :: new_opaque ( ) ,
25
33
}
26
34
}
35
+ // > "ftp" "http" "https" "ws" "wss": Return the tuple origin (url’s scheme, url’s host,
36
+ // > url’s port, null).
37
+ //
27
38
"ftp" | "http" | "https" | "ws" | "wss" => Origin :: Tuple (
28
39
scheme. to_owned ( ) ,
29
40
url. host ( ) . unwrap ( ) . to_owned ( ) ,
30
- url. port_or_known_default ( ) . unwrap ( ) ,
41
+ url. port ( ) ,
31
42
) ,
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
+ //
32
46
// TODO: Figure out what to do if the scheme is a file
33
47
"file" => Origin :: new_opaque ( ) ,
48
+ // > Otherwise: Return a new opaque origin.
34
49
_ => Origin :: new_opaque ( ) ,
35
50
}
36
51
}
@@ -58,7 +73,7 @@ pub enum Origin {
58
73
Opaque ( OpaqueOrigin ) ,
59
74
60
75
/// Consists of the URL's scheme, host and port
61
- Tuple ( String , Host < String > , u16 ) ,
76
+ Tuple ( String , Host < String > , Option < u16 > ) ,
62
77
}
63
78
64
79
impl Origin {
@@ -78,12 +93,11 @@ impl Origin {
78
93
pub fn ascii_serialization ( & self ) -> String {
79
94
match * self {
80
95
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)
87
101
}
88
102
}
89
103
}
@@ -100,10 +114,9 @@ impl Origin {
100
114
}
101
115
_ => host. clone ( ) ,
102
116
} ;
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) ,
107
120
}
108
121
}
109
122
}
0 commit comments