1
1
use hyper:: header:: { ACCESS_CONTROL_ALLOW_HEADERS , ACCESS_CONTROL_REQUEST_HEADERS } ;
2
2
use hyper:: { Body , HeaderMap , Method , Request , Response , StatusCode } ;
3
3
4
- pub enum CorsResult {
4
+ pub enum Cors {
5
5
Preflight ( Response < Body > ) ,
6
6
Simple ( HeaderMap ) ,
7
- None ,
8
- }
9
-
10
- impl CorsResult {
11
- pub fn is_none ( & self ) -> bool {
12
- match self {
13
- Self :: None => true ,
14
- _ => false ,
15
- }
16
- }
17
7
}
18
8
19
9
/// Cross-Origin Resource Sharing request handler
20
10
/// Allows all origins and methods and supports Preflighted `OPTIONS` requests.
21
11
/// On Simple CORS requests sets initial `HeaderMap` to be used in the routes handlers.
22
12
/// Otherwise returns a `OPTION` Response.
23
- pub ( crate ) fn cors ( req : & Request < Body > ) -> CorsResult {
13
+ pub ( crate ) fn cors ( req : & Request < Body > ) -> Option < Cors > {
24
14
use hyper:: header:: {
25
15
HeaderValue , ACCESS_CONTROL_ALLOW_METHODS , ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_LENGTH ,
26
16
ORIGIN ,
@@ -62,11 +52,11 @@ pub(crate) fn cors(req: &Request<Body>) -> CorsResult {
62
52
// set the headers of the Request
63
53
* response. headers_mut ( ) = headers;
64
54
65
- CorsResult :: Preflight ( response)
55
+ Some ( Cors :: Preflight ( response) )
66
56
} else if !headers. is_empty ( ) {
67
- CorsResult :: Simple ( headers)
57
+ Some ( Cors :: Simple ( headers) )
68
58
} else {
69
- CorsResult :: None
59
+ None
70
60
}
71
61
}
72
62
@@ -87,7 +77,7 @@ mod test {
87
77
. unwrap ( ) ;
88
78
89
79
let allowed_origin_headers = match cors ( & cors_req) {
90
- CorsResult :: Simple ( headers) => headers,
80
+ Some ( Cors :: Simple ( headers) ) => headers,
91
81
_ => panic ! ( "Simple CORS headers were expected" ) ,
92
82
} ;
93
83
assert_eq ! (
@@ -120,7 +110,7 @@ mod test {
120
110
. unwrap ( ) ;
121
111
122
112
let response = match cors ( & cors_req) {
123
- CorsResult :: Preflight ( headers) => headers,
113
+ Some ( Cors :: Preflight ( headers) ) => headers,
124
114
_ => panic ! ( "Preflight CORS response was expected" ) ,
125
115
} ;
126
116
0 commit comments