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
use std::collections::HashMap;
pub struct Request {
parsed_body: Option<String>,
headers: HashMap<String, String>,
status_line: Vec<String>,
body: Vec<u8>,
}
#[derive(Clone, Debug)]
pub enum BodyType {
ASCII(String),
Bytes(Vec<u8>),
}
impl Request {
pub(crate) fn new(
raw_body: Vec<u8>,
raw_headers: Vec<String>,
status_line: Vec<String>,
) -> Request {
let raw_body_clone = raw_body.clone();
let ascii_body = match std::str::from_utf8(&raw_body_clone) {
Ok(s) => Some(s),
Err(_) => {
log::info!("Not an ASCII body");
None
}
};
let mut headers: HashMap<String, String> = HashMap::new();
log::trace!("Headers: {:#?}", raw_headers);
for i in raw_headers.iter() {
let mut iter = i.split_whitespace();
let key = iter.next().unwrap();
let value = iter.next().unwrap();
headers.insert(key.to_string(), value.to_string());
}
log::info!("Request headers: {:?}", headers);
if ascii_body.is_none() {
Request {
parsed_body: None,
body: raw_body,
headers,
status_line,
}
} else {
Request {
body: raw_body,
parsed_body: Some(ascii_body.unwrap().to_string()),
headers,
status_line,
}
}
}
pub fn get_raw_body(&self) -> Vec<u8> {
self.body.clone()
}
pub fn get_parsed_body(&self) -> Option<String> {
self.parsed_body.clone()
}
pub fn get_headers(&self) -> HashMap<String, String> {
self.headers.clone()
}
pub fn get_status_line(&self) -> Vec<String> {
self.status_line.clone()
}
}