kalgan 0.9.1

A web framework for Rust programing language.
Documentation
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Module for the request object passed to the handler.

use log::warn;
use kalgan_router::Route;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use urlencoding::decode;

#[derive(Serialize, Deserialize, Debug, Clone)]
/// The struct that contains all the data of the file attached to the request.
pub struct File<'a> {
    pub filename: String,
    pub content_type: String,
    pub content: &'a [u8],
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// The struct that contains all the data sent by the browser.
pub struct Request<'a> {
    method: String,
    uri: String,
    protocol: String,
    cookies: HashMap<String, String>,
    host: String,
    user_agent: String,
    input: HashMap<String, String>,
    referer: String,
    #[serde(borrow)]
    files: HashMap<String, File<'a>>,
    raw: String,
    pub middleware: HashMap<String, String>,
    pub route: Option<Route>,
}
impl<'a> Request<'a> {
    /// Creates and returns an instance of the ´Request´ struct with the data sent by the browser.
    pub fn new(buffer: &[u8]) -> Option<Request> {
        let raw = String::from_utf8_lossy(&buffer[..]).to_string();
        let first_line = raw.split("\r\n").next()?;
        let mut parameters = first_line.split(" ");
        Some(Request {
            method: parameters.next()?.to_string(),
            uri: Request::parse_url_encoding(&parameters.next()?.trim().to_string()),
            protocol: parameters.next()?.to_string(),
            cookies: Request::parse_cookies(&raw),
            host: Request::parse_host(&raw),
            user_agent: Request::parse_user_agent(&raw),
            input: Request::parse_input(&raw),
            referer: Request::parse_referer(&raw),
            files: Request::parse_files(&raw, buffer),
            raw: raw,
            middleware: HashMap::new(),
            route: None,
        })
    }
    /// Returns the http method of the request.
    /// # Examples
    /// ```
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let request = Request::mock().mock_set_method("POST".to_string());
    /// let method: &String = request.get_method();
    /// # assert_eq!(method, &"POST".to_string())
    /// ```
    pub fn get_method(&self) -> &String {
        &self.method
    }
    /// Returns the uri of the request.
    /// # Examples
    /// ```
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let request = Request::mock().mock_set_uri("/home".to_string());
    /// let uri: &String = request.get_uri();
    /// # assert_eq!(uri, &"/home".to_string())
    /// ```
    pub fn get_uri(&self) -> &String {
        &self.uri
    }
    /// Returns the http protocol of the request.
    /// # Examples
    /// ```
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let request = Request::mock().mock_set_protocol("HTTP/1.1".to_string());
    /// let protocol: &String = request.get_protocol();
    /// # assert_eq!(protocol, &"HTTP/1.1".to_string())
    /// ```
    pub fn get_protocol(&self) -> &String {
        &self.protocol
    }
    /// Returns the collection of cookies of the request.
    /// # Examples
    /// ```
    /// use std::collections::HashMap;
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    /// # let mut cookies_right = HashMap::new();
    /// # cookies_right.insert("key".to_string(), "value".to_string());
    /// # let request = Request::mock().mock_set_cookies(cookies_right.clone());
    /// let cookies: &HashMap<String, String> = request.get_cookies();
    /// # assert_eq!(cookies["key"], cookies_right["key"])
    /// ```
    pub fn get_cookies(&self) -> &HashMap<String, String> {
        &self.cookies
    }
    /// Returns the host field of the request.
    /// # Examples
    /// ```
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let request = Request::mock().mock_set_host("dev.foobar.com".to_string());
    /// let host: &String = request.get_host();
    /// # assert_eq!(host, &"dev.foobar.com".to_string())
    /// ```
    pub fn get_host(&self) -> &String {
        &self.host
    }
    /// Returns the user agent of the request.
    /// # Examples
    /// ```
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let request = Request::mock().mock_set_user_agent("Mozilla/5.0".to_string());
    /// let user_agent: &String = request.get_user_agent();
    /// # assert_eq!(user_agent, &"Mozilla/5.0".to_string())
    /// ```
    pub fn get_user_agent(&self) -> &String {
        &self.user_agent
    }
    /// Returns the input data collection of the request.
    /// # Examples
    /// ```
    /// use std::collections::HashMap;
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let mut input_right = HashMap::new();
    /// # input_right.insert("key".to_string(), "value".to_string());
    /// # let request = Request::mock().mock_set_input(input_right.clone());
    /// let input: &HashMap<String, String> = request.get_input();
    /// # assert_eq!(input["key"], input_right["key"])
    /// ```
    pub fn get_input(&self) -> &HashMap<String, String> {
        &self.input
    }
    /// Returns the referer field of the request.
    /// # Examples
    /// ```
    /// use std::collections::HashMap;
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let request = Request::mock().mock_set_referer("/home".to_string());
    /// let referer: &String = request.get_referer();
    /// # assert_eq!(referer, &"/home".to_string());
    /// ```
    pub fn get_referer(&self) -> &String {
        &self.referer
    }
    /// Returns the collection of files attached to the request.
    /// # Examples
    /// ```
    /// use std::collections::HashMap;
    /// use kalgan::http::request::Request;
    /// use kalgan::http::request::File;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let mut files_right = HashMap::new();
    /// # files_right.insert("key".to_string(), File {
    /// #     filename: "test".to_string(),
    /// #     content_type: "text/png".to_string(),
    /// #     content: &[0, 0, 0, 1]
    /// # });
    /// # let request = Request::mock().mock_set_files(files_right.clone());
    /// let files: &HashMap<String, File> = request.get_files();
    /// # assert_eq!(files["key"].filename, files_right["key"].filename);
    /// ```
    pub fn get_files(&self) -> &HashMap<String, File<'a>> {
        &self.files
    }
    /// Returns the raw value of the request.
    /// # Examples
    /// ```
    /// use std::collections::HashMap;
    /// use kalgan::http::request::Request;
    /// # use kalgan::http::request::Mock;
    ///
    /// # let request = Request::mock().mock_set_raw("foobar".to_string());
    /// let raw: &String = request.get_raw();
    /// # assert_eq!(raw, &"foobar".to_string());
    /// ```
    pub fn get_raw(&self) -> &String {
        &self.raw
    }
    /// Parses and returns the collection of cookies of the request.
    fn parse_cookies(request: &str) -> HashMap<String, String> {
        let mut cookies: HashMap<String, String> = HashMap::new();
        let cookies_collection = match regex::Regex::new(r#"(?i)Cookie:.*"#).unwrap().find(request) {
            Some(x) => regex::Regex::new(r"(?i)Cookie:").unwrap().replace_all(x.as_str(), "").to_string().replace(" ", ""),
            None => "".to_string(),
        };
        let collection: Vec<&str> = cookies_collection.split(";").collect();
        for cookie in collection {
            if cookie.contains("=") {
                let pos = cookie.find("=").unwrap();
                let re = regex::Regex::new(r"\s+").unwrap();
                let cookie_without_spaces = re.replace_all(&cookie, " ").trim().to_string();
                cookies.insert(
                    cookie_without_spaces[..pos].to_string(),
                    cookie_without_spaces[pos + 1..].to_string(),
                );
            }
        }
        cookies
    }
    /// Parses and returns the host field of the request.
    fn parse_host(request: &str) -> String {
        match regex::Regex::new(r#"(?i)Host:.*"#).unwrap().find(request) {
            Some(x) => {
                let mut string = regex::Regex::new(r"(?i)Host:").unwrap().replace_all(x.as_str(), "").to_string().replace(" ", "");
                let len = string.trim_end_matches(&['\r', '\n'][..]).len();
                string.truncate(len);
                Request::parse_url_encoding(&string)
            }
            None => "".to_string(),
        }
    }
    /// Parses and returns the user agent of the request.
    fn parse_user_agent(request: &str) -> String {
        match regex::Regex::new(r#"(?i)User-Agent:.*"#).unwrap().find(request) {
            Some(x) => {
                let mut string = regex::Regex::new(r"(?i)User-Agent:").unwrap().replace_all(x.as_str(), "").to_string().replace(" ", "");
                let len = string.trim_end_matches(&['\r', '\n'][..]).len();
                string.truncate(len);
                string
            }
            None => "".to_string(),
        }
    }
    /// Parses and returns the input data collection of the request.
    fn parse_input(request: &str) -> HashMap<String, String> {
        let mut input: HashMap<String, String> = HashMap::new();
        match regex::Regex::new(r#"(?i)Content-Type: multipart/form-data;"#)
            .unwrap()
            .find(request)
        {
            Some(_x) => {
                for mat in regex::Regex::new(r#"(?i)Content-Disposition: form-data; name=.*"#)
                    .unwrap()
                    .find_iter(request)
                {
                    let mut key = regex::Regex::new(r"(?i)Content-Disposition: form-data; name=").unwrap()
                        .replace_all(mat.as_str(), "").to_string()
                        .replace(" ", "")
                        .replace("\"", "");
                    let len = key.trim_end_matches(&['\r', '\n'][..]).len();
                    key.truncate(len);
                    if !key.contains("filename=") {
                        let request_clone = request[mat.end()..].to_string().clone();
                        let lines: Vec<&str> = request_clone.split("\r\n").collect();
                        input.insert(
                            Request::parse_url_encoding(&key),
                            Request::parse_url_encoding(&lines[1].to_string()),
                        );
                    }
                }
            }
            None => {
                let lines: Vec<&str> = request.split("\r\n").collect();
                let len = lines.len();
                if lines[len - 2].trim() == "" {
                    let parameters: Vec<&str> = lines[len - 1].split("&").collect();
                    for parameter in parameters {
                        if parameter.contains("=") {
                            let param: Vec<&str> = parameter.split("=").collect();
                            input.insert(
                                Request::parse_url_encoding(&param[0].to_string()),
                                Request::parse_url_encoding(
                                    &param[1].to_string().replace("+", " "),
                                ),
                            );
                        }
                    }
                }
            }
        }
        input
    }
    /// Parses and returns the referer field of the request.
    fn parse_referer(request: &str) -> String {
        match regex::Regex::new(r#"(?i)Referer:.*"#).unwrap().find(request) {
            Some(x) => {
                let mut string = regex::Regex::new(r"(?i)Referer:").unwrap().replace_all(x.as_str(), "").to_string().replace(" ", "");
                let len = string.trim_end_matches(&['\r', '\n'][..]).len();
                string.truncate(len);
                string
            }
            None => "".to_string(),
        }
    }
    /// Parses and returns the uri of the request.
    fn parse_url_encoding(url_encoded_string: &String) -> String {
        match decode(&url_encoded_string) {
            Ok(s) => s.to_string(),
            Err(e) => {
                warn!("{}", e);
                url_encoded_string.to_string()
            }
        }
    }
    /// Parses and returns the collection of files attached to the request.
    fn parse_files<'b>(request: &str, buffer: &'b [u8]) -> HashMap<String, File<'b>> {
        let mut files: HashMap<String, File> = HashMap::new();
        match regex::Regex::new(r#"(?i)Content-Type: multipart/form-data;.*?\r\n"#)
            .unwrap()
            .find(request)
        {
            Some(x) => {
                let param: Vec<&str> = request[x.start()..x.end()]
                    .trim_end_matches(&['\r', '\n'][..])
                    .split("; boundary=")
                    .collect();
                let boundary = &param[1];
                let mut prev = 0;
                for mat in regex::Regex::new(
                    format!(
                        r#"(?i)Content-Disposition: form-data; name=(?s:.*?){}"#,
                        boundary
                    )
                        .as_str(),
                )
                    .unwrap()
                    .find_iter(request)
                {
                    let request_clone = request[mat.start()..mat.end()].to_string().clone();
                    let lines: Vec<&str> = request_clone
                        .trim_start_matches(&['\r', '\n'][..])
                        .split("\r\n")
                        .collect();
                    let first_line = lines[0].to_string();
                    if first_line.contains("filename=") && !first_line.contains("filename=\"\"") {
                        let params: Vec<&str> = first_line.split("filename=").collect();
                        let mut content_type = regex::Regex::new(r"(?i)Content-Type:").unwrap()
                            .replace_all(lines[1], "").to_string()
                            .replace(" ", "")
                            .replace("\"", "");
                        let len = content_type.trim_end_matches(&['\r', '\n'][..]).len();
                        content_type.truncate(len);
                        let x1 = request_clone.find(&lines[3].to_string()).unwrap();
                        let x2 = request_clone
                            .find(&lines[lines.len() - 1].to_string())
                            .unwrap();
                        let content = request_clone[x1..x2].trim_end_matches(&['\r', '\n'][..]);
                        let n1 = if prev == 0 {
                            request.to_string().find(content).unwrap()
                        } else {
                            let mut new_n1 = prev.clone();
                            loop {
                                if String::from_utf8_lossy(&buffer[new_n1..new_n1 + 4])
                                    .to_string()
                                    .find("\r\n\r\n")
                                    == Some(0)
                                {
                                    break new_n1 + 4;
                                }
                                new_n1 = new_n1 + 1;
                            }
                        };
                        let mut n2 = n1.clone();
                        loop {
                            n2 = n2 + 1;
                            let test = String::from_utf8_lossy(&buffer[n2 - boundary.len()..n2])
                                .to_string();
                            if test.contains(boundary) {
                                prev = n2.clone();
                                n2 = n2 - 4 - boundary.len();
                                break;
                            }
                        }
                        files.insert(
                            kalgan_string::strip(
                                kalgan_string::strip_right(
                                    regex::Regex::new(r"(?i)Content-Disposition: form-data; name=")
                                        .unwrap().replace_all(params[0], "").trim(),
                                    ';',
                                ),
                                '"',
                            )
                                .to_string(),
                            File {
                                filename: params[1].replace(" ", "").replace("\"", ""),
                                content_type: content_type,
                                content: &buffer[n1..n2],
                            },
                        );
                    }
                }
            }
            None => (),
        }
        files
    }
}
#[cfg(feature = "test")]
/// Describes all the methods to set the `Request` fields to be used in testing.
pub trait Mock<'a> {
    /// Creates the `Request` object with empty fields to be used in testing and returns the instance.
    fn mock() -> Self;
    /// Sets the http method of the request and returns the instance.
    fn mock_set_method(self, method: String) -> Self;
    /// Sets the uri of the request and returns the instance.
    fn mock_set_uri(self, uri: String) -> Self;
    /// Sets the protocol of the request and returns the instance.
    fn mock_set_protocol(self, protocol: String) -> Self;
    /// Sets the collection of cookies of the request and returns the instance.
    fn mock_set_cookies(self, cookies: HashMap<String, String>) -> Self;
    /// Sets the host field of the request and returns the instance.
    fn mock_set_host(self, host: String) -> Self;
    /// Sets the user agent of the request and returns the instance.
    fn mock_set_user_agent(self, user_agent: String) -> Self;
    /// Sets the input data collection of the request and returns the instance.
    fn mock_set_input(self, input: HashMap<String, String>) -> Self;
    /// Sets the referer field the request and returns the instance.
    fn mock_set_referer(self, referer: String) -> Self;
    /// Sets the collection of files attached to the request and returns the instance.
    fn mock_set_files(self, files: HashMap<String, File<'a>>) -> Self;
    /// Sets the raw value of the request and returns the instance.
    fn mock_set_raw(self, raw: String) -> Self;
}
#[cfg(feature = "test")]
impl<'a> Mock<'a> for Request<'a> {
    fn mock() -> Self {
        Request {
            method: "".to_string(),
            uri: "".to_string(),
            protocol: "".to_string(),
            cookies: HashMap::new(),
            host: "".to_string(),
            user_agent: "".to_string(),
            input: HashMap::new(),
            referer: "".to_string(),
            files: HashMap::new(),
            raw: "".to_string(),
            middleware: HashMap::new(),
            route: None,
        }
    }
    fn mock_set_method(mut self, method: String) -> Self {
        self.method = method;
        self
    }
    fn mock_set_uri(mut self, uri: String) -> Self {
        self.uri = uri;
        self
    }
    fn mock_set_protocol(mut self, protocol: String) -> Self {
        self.protocol = protocol;
        self
    }
    fn mock_set_cookies(mut self, cookies: HashMap<String, String>) -> Self {
        self.cookies = cookies;
        self
    }
    fn mock_set_host(mut self, host: String) -> Self {
        self.host = host;
        self
    }
    fn mock_set_user_agent(mut self, user_agent: String) -> Self {
        self.user_agent = user_agent;
        self
    }
    fn mock_set_input(mut self, input: HashMap<String, String>) -> Self {
        self.input = input;
        self
    }
    fn mock_set_referer(mut self, referer: String) -> Self {
        self.referer = referer;
        self
    }
    fn mock_set_files(mut self, files: HashMap<String, File<'a>>) -> Self {
        self.files = files;
        self
    }
    fn mock_set_raw(mut self, raw: String) -> Self {
        self.raw = raw;
        self
    }
}