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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::collections::HashMap;
use std::str;
use hyper::{Body, Response, Request, StatusCode};
use thruster_core::context::Context;
use thruster_core::request::RequestWithParams;
use thruster_middleware::query_params::HasQueryParams;
pub struct HyperRequest(pub Request<Body>);
impl RequestWithParams for HyperRequest {
fn set_params(&mut self, params: HashMap<String, String>) {
let extensions = self.0
.extensions_mut();
extensions.insert(params);
}
}
impl Default for HyperRequest {
fn default() -> Self {
HyperRequest(Request::default())
}
}
pub enum SameSite {
Strict,
Lax
}
pub struct CookieOptions {
pub domain: String,
pub path: String,
pub expires: u64,
pub http_only: bool,
pub max_age: u64,
pub secure: bool,
pub signed: bool,
pub same_site: SameSite
}
impl CookieOptions {
pub fn default() -> CookieOptions {
CookieOptions {
domain: "".to_owned(),
path: "/".to_owned(),
expires: 0,
http_only: false,
max_age: 0,
secure: false,
signed: false,
same_site: SameSite::Strict
}
}
}
pub fn generate_context(request: HyperRequest) -> BasicHyperContext {
let mut ctx = BasicHyperContext::new();
ctx.request = request;
ctx
}
#[derive(Default)]
pub struct BasicHyperContext {
pub body: Body,
pub query_params: HashMap<String, String>,
pub request: HyperRequest,
pub status: u16,
pub headers: HashMap<String, String>
}
impl BasicHyperContext {
pub fn new() -> BasicHyperContext {
let mut req = Request::builder()
.body(Body::empty()).unwrap();
let param_map: HashMap<String, String> = HashMap::new();
req.extensions_mut().insert(param_map);
BasicHyperContext {
body: Body::empty(),
query_params: HashMap::new(),
request: HyperRequest(req),
headers: HashMap::new(),
status: 200
}
}
pub fn param(&mut self, key: &str) -> Option<&String> {
let params = self.request.0
.extensions_mut()
.get_mut::<HashMap<String, String>>()
.unwrap();
params.get(key)
}
pub fn set(&mut self, key: &str, value: &str) {
self.headers.insert(key.to_owned(), value.to_owned());
}
pub fn remove(&mut self, key: &str) {
self.headers.remove(key);
}
pub fn status(&mut self, code: u16) {
self.status = code;
}
pub fn content_type(&mut self, c_type: &str) {
self.set("Content-Type", c_type);
}
pub fn redirect(&mut self, destination: &str) {
self.status(302);
self.set("Location", destination);
}
pub fn cookie(&mut self, name: &str, value: &str, options: &CookieOptions) {
let cookie_value = match self.headers.get("Set-Cookie") {
Some(val) => format!("{}, {}", val, self.cookify_options(name, value, &options)),
None => self.cookify_options(name, value, &options)
};
self.set("Set-Cookie", &cookie_value);
}
fn cookify_options(&self, name: &str, value: &str, options: &CookieOptions) -> String {
let mut pieces = vec![format!("Path={}", options.path)];
if options.expires > 0 {
pieces.push(format!("Expires={}", options.expires));
}
if options.max_age > 0 {
pieces.push(format!("Max-Age={}", options.max_age));
}
if !options.domain.is_empty() {
pieces.push(format!("Domain={}", options.domain));
}
if options.secure {
pieces.push("Secure".to_owned());
}
if options.http_only {
pieces.push("HttpOnly".to_owned());
}
match options.same_site {
SameSite::Strict => pieces.push("SameSite=Strict".to_owned()),
SameSite::Lax => pieces.push("SameSite=Lax".to_owned())
};
format!("{}={}; {}", name, value, pieces.join(", "))
}
}
impl Context for BasicHyperContext {
type Response = Response<Body>;
fn get_response(self) -> Self::Response {
let mut response_builder = Response::builder();
for (key, val) in self.headers {
let key: &str = &key;
let val: &str = &val;
response_builder.header(key, val);
}
response_builder.status(StatusCode::from_u16(self.status).unwrap());
response_builder.body(self.body).unwrap()
}
fn set_body(&mut self, body: Vec<u8>) {
self.body = Body::from(body);
}
}
impl HasQueryParams for BasicHyperContext {
fn set_query_params(&mut self, query_params: HashMap<String, String>) {
self.query_params = query_params;
}
fn route(&self) -> &str {
match self.request.0.uri().path_and_query() {
Some(val) => val.as_str(),
None => self.request.0.uri().path()
}
}
}