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
use crate::utils::get_vec::get_vec;
#[derive(Clone, Debug)]
pub struct Request {
/// Get Client Address/IP with Port
///
/// # Example
///
/// ```
/// use oxidy::{Server, Context, Returns, route};
///
/// async fn route(c: Context) -> Returns {
/// println!("Path: {}", c.request.address);
/// (c, None)
/// }
///
/// let mut app = Server::new();
/// app.add(route!("get /", route));
/// ```
pub address: String,
/// Get Request Header (RAW)
///
/// # Example
///
/// ```
/// use oxidy::{Server, Context, Returns, route};
///
/// async fn route(c: Context) -> Returns {
/// println!("Path: {}", c.request.header);
/// (c, None)
/// }
///
/// let mut app = Server::new();
/// app.add(route!("get /", route));
/// ```
pub header: String,
/*
* Store / Cache
*/
pub(crate) header_store: Vec<(String, String)>,
pub(crate) param_store: Vec<(String, String)>,
pub(crate) query_store: Vec<(String, String)>,
pub method: String,
pub url: String,
pub path: String,
pub query: String,
pub http_version: f64,
}
impl Request {
/// Get Request Header
///
/// # Example
///
/// ```
/// use oxidy::{Server, Context, Returns, route};
///
/// async fn route(mut c: Context) -> Returns {
/// let host: Option<String> = c.request.header("host").await;
/// match host {
/// Some(h) => println!("Host is: {}", h),
/// None => println!("Host not found"),
/// }
/// (c, None)
/// }
///
/// let mut app = Server::new();
/// app.add(route!("get /", route));
/// ```
pub async fn header(&mut self, key: &str) -> Option<String> {
if !self.header_store.is_empty() {
return get_vec(&self.header_store, key.to_owned()).await;
}
let mut found_value: Option<String> = None;
let mut headers: Vec<(String, String)> = Vec::new();
let mut h: Vec<&str> = self.header.lines().collect();
h.remove(0);
h.into_iter().for_each(|ln: &str| {
let mut ln_split: Vec<String> =
ln.split_whitespace().map(|s: &str| s.to_owned()).collect();
if ln_split.is_empty() {
return;
}
/*
* Key
*/
let k: String = ln_split[0].trim().replace(':', "");
/*
* Filter Key
*/
let k: Vec<String> = k.split_whitespace().map(|s: &str| s.to_owned()).collect();
let k: String = k.join("");
if k.is_empty() {
return;
}
/*
* Value
*/
let mut v: String = String::new();
ln_split.remove(0);
if !ln_split.is_empty() {
v = ln_split.join(" ");
}
if k == key {
found_value = Some(v.clone());
}
headers.push((k, v));
});
self.header_store = headers;
found_value
}
/// Get Request Parameter
///
/// # Example
///
/// ```
/// use oxidy::{Server, Context, Returns, route};
///
/// async fn route(mut c: Context) -> Returns {
/// let user: String = c.request.param("user").await;
/// c.response.body = format!("Username: {}", user);
/// (c, None)
/// }
///
/// let mut app = Server::new();
/// app.add(route!("get /:user", route));
/// ```
pub async fn param(&self, key: &str) -> String {
let v: Option<String> = get_vec(&self.param_store, key.to_owned()).await;
match v {
Some(x) => x,
None => String::new(),
}
}
/// Get Request Query
///
/// # Example
///
/// ```
/// use oxidy::{Server, Context, Returns, route};
///
/// async fn route(mut c: Context) -> Returns {
/// let user: Option<String> = c.request.query("user").await;
/// match user {
/// Some(u) => c.response.body = format!("Username: {}", u),
/// None => {
/// c.response.body = format!("Username not found");
/// c.response.status = 404;
/// },
/// }
/// (c, None)
/// }
///
/// let mut app = Server::new();
///
/// /* Requested URL: /user?user=John */
/// app.add(route!("get /", route));
/// ```
pub async fn query(&mut self, key: &str) -> Option<String> {
if !self.query_store.is_empty() {
return get_vec(&self.query_store, key.to_owned()).await;
}
let mut found_value: Option<String> = None;
let query: String = self.query.clone();
if query.is_empty() {
return found_value;
}
let mut query_str: Vec<(String, String)> = Vec::new();
let query_split: Vec<String> = query.split('&').map(|s: &str| s.to_owned()).collect();
query_split.iter().for_each(|q: &String| {
let mut kv: Vec<String> = q.split('=').map(|s: &str| s.to_owned()).collect();
if kv.get(0).is_none() || kv[0].is_empty() {
return;
}
let k: String = kv[0].to_owned();
let mut v: String = String::new();
kv.remove(0);
if !kv.is_empty() {
v = kv.join("=");
}
if k == key {
found_value = Some(v.clone());
}
query_str.push((k, v));
});
self.query_store = query_str;
found_value
}
}