1pub mod header {
2 pub struct Header {
3 key: &'static str,
4 value: &'static str,
5 }
6
7 impl Header {
8 pub fn to_string(&self) -> String {
9 format!("{}: {}", &self.key, &self.value)
10 }
11
12 pub fn new(key: &'static str, value: &'static str) -> Header {
13 Header {
14 key: key.trim(),
15 value: value.trim(),
16 }
17 }
18 }
19
20 pub struct Headers {
21 headers: Vec<Header>,
22 }
23
24 impl Headers {
25
26 pub fn new() -> Headers {
27 Headers { headers: vec![] }
28 }
29
30 pub fn append(&mut self, header: Header) {
31 self.headers.push(header);
32 }
33
34 pub fn has(&self, key: &'static str) -> bool {
35 for header in &self.headers {
36 if key.trim().to_lowercase().eq(&header.key.to_lowercase()) {
37 return true;
38 }
39 }
40
41 false
42 }
43
44 pub fn get(&self, key: &'static str) -> Option<&Header> {
45 for header in &self.headers {
46 if key.trim().to_lowercase().eq(&header.key.to_lowercase()) {
47 return Some(header);
48 }
49 }
50
51 None
52 }
53
54 pub fn finalize(&self) -> String {
55 let mut final_string = String::new();
56
57 for header in &self.headers {
58 final_string += &header.to_string();
59 final_string += "\r\n\r\n";
60 }
61
62 final_string
63 }
64 }
65}
66
67
68#[cfg(test)]
69mod tests {
70
71 #[test]
72 fn to_string() {
73 use super::header::Header;
74
75 let header = Header::new("Content-Type", "application/json");
76
77 assert_eq!(header.to_string(), String::from("Content-Type: application/json"));
78 }
79
80 #[test]
81 fn has() {
82 use super::header::Header;
83 use super::header::Headers;
84
85 let mut headers = Headers::new();
86 headers.append(Header::new("Content-Type", "Application/Json"));
87 headers.append(Header::new("Content-Type", "Application/Json"));
88
89 assert_eq!(true, headers.has("content-type"));
90 }
91}