1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::*;
impl Default for CorsConfig {
/// Builds the configuration the cross origin plugin uses when none is
/// given.
///
/// # Returns
///
/// - `Self` - The default configuration.
fn default() -> Self {
Self {
allowed_origins: vec![WILDCARD_ANY.to_owned()],
allowed_methods: vec![ALL_METHODS.to_owned()],
allowed_headers: vec![WILDCARD_ANY.to_owned()],
max_age: None,
allow_credentials: false,
}
}
}
impl CorsConfig {
/// The rules the configuration works from.
///
/// # Returns
///
/// - `CorsPolicy` - The rules of the configuration.
fn policy(&self) -> CorsPolicy {
CorsPolicy {
allowed_origins: self.get_allowed_origins().clone(),
allowed_methods: self.get_allowed_methods().clone(),
allowed_headers: self.get_allowed_headers().clone(),
max_age: self.get_max_age(),
allow_credentials: self.get_allow_credentials(),
}
}
/// The origin that is written back, when the origin is allowed.
///
/// A wildcard answers with the wildcard itself, a list answers with the
/// origin that was asked for.
///
/// # Arguments
///
/// - `&str` - The origin the caller asked with.
///
/// # Returns
///
/// - `Option<String>` - The origin to write back, when the origin is
/// allowed.
pub fn allowed_origin(&self, origin: &str) -> Option<String> {
self.policy().allowed_origin(origin)
}
}
impl CorsPlugin {
/// Stores the configuration the plugin works from.
///
/// # Arguments
///
/// - `CorsConfig` - The configuration to store.
///
/// # Returns
///
/// - `Result<(), String>` - Nothing, or the reason the configuration was
/// refused.
pub fn init(config: CorsConfig) -> Result<(), String> {
let policy: CorsPolicy = config.policy();
CONFIG
.set(config)
.map_err(|_: CorsConfig| MESSAGE_ALREADY_SET.to_owned())?;
POLICY
.set(policy)
.map_err(|_: CorsPolicy| MESSAGE_ALREADY_SET.to_owned())
}
/// The configuration the plugin works from.
///
/// # Returns
///
/// - `Option<&'static CorsConfig>` - The configuration, when it was set.
pub fn get() -> Option<&'static CorsConfig> {
CONFIG.get()
}
}
impl ServerHook for CorsMiddleware {
/// Builds the middleware.
///
/// # Arguments
///
/// - `&mut Stream` - The stream the request arrived on.
/// - `&mut Context` - The context of the request.
///
/// # Returns
///
/// - `Self` - The middleware.
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
/// Writes the cross origin headers of an allowed origin.
///
/// A request without an origin, and a request from an origin that is not
/// allowed, are left alone.
///
/// # Arguments
///
/// - `Self` - The middleware.
/// - `&mut Stream` - The stream the request arrived on.
/// - `&mut Context` - The context of the request.
///
/// # Returns
///
/// - `Status` - Whether the pipeline continues.
async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status {
let policy: &CorsPolicy = match POLICY.get() {
Some(policy) => policy,
None => return Status::Continue,
};
let origin: String = match ctx.get_request().try_get_header(ORIGIN) {
Some(values) => match values.front() {
Some(value) => value.clone(),
None => return Status::Continue,
},
None => return Status::Continue,
};
let headers: Vec<(&'static str, String)> = match policy.headers(&origin) {
Some(headers) => headers,
None => return Status::Continue,
};
let response: &mut Response = ctx.get_mut_response();
for (name, value) in headers {
response.set_header(name, value);
}
Status::Continue
}
}