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
///! Module [`Header`]
use crate::http::CRLF;
#[allow(unused_imports)]
use napi_derive::napi;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{
    fmt,
    io::{Error, ErrorKind, Result},
    str,
};

#[cfg_attr(feature = "napi", napi(object))]
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct Header {
    pub name: String,
    pub value: String,
}

#[cfg_attr(feature = "napi", napi(object))]
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct Headers {
    pub raw: String,
    pub list: Vec<Header>,
}

impl fmt::Display for Header {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}: {}", self.name, self.value)
    }
}

impl Headers {
    pub fn new(prefix: &str, list: Vec<Header>) -> Self {
        let postfix = Headers::to_string(list);
        let raw = format!(
            "{}{CRLF}{postfix}",
            Regex::new(r"\s*$")
                .unwrap()
                .replace_all(prefix, "")
                .to_string()
        );
        Headers::from_string(raw)
    }

    /// Parse headers
    pub fn from_string(raw: String) -> Self {
        let mut res: Headers = Headers {
            raw: raw.clone(),
            list: vec![],
        };
        let heads = raw.split(CRLF);
        for h in heads {
            let reg_name = Regex::new(r"^.+: ").unwrap();
            let capts_name = reg_name.captures(h);
            if let None = capts_name {
                continue;
            }
            let capts_name = capts_name.unwrap();
            let name = capts_name
                .get(0)
                .unwrap()
                .as_str()
                .to_string()
                .replace(": ", "");

            let reg_value = Regex::new(r": *.*$").unwrap();
            let capts_value = reg_value.captures(h);
            if let None = capts_value {
                res.list.push(Header {
                    name,
                    value: "".to_string(),
                });
                continue;
            }
            let capts_value = capts_value.unwrap();
            let value = capts_value
                .get(0)
                .unwrap()
                .as_str()
                .to_string()
                .replace(": ", "");
            res.list.push(Header {
                name,
                value: value.to_lowercase(),
            });
        }
        res
    }

    /// Create string of headers from list
    pub fn to_string(list: Vec<Header>) -> String {
        let mut result = "".to_string();
        for h in list {
            result = format!(
                "{result}{}: {}{CRLF}",
                h.name.to_lowercase(),
                h.value.to_lowercase()
            );
        }
        result = format!("{result}{CRLF}");
        result
    }

    /// Create headers from bytes
    pub fn from_bytes(heads: &Vec<u8>) -> Result<Self> {
        let res = str::from_utf8(heads);
        if let Err(err) = res {
            return Err(Error::new(ErrorKind::InvalidInput, err));
        }
        let res = res.unwrap().to_string();
        Ok(Headers::from_string(res))
    }

    /// For change request headers host to host of target
    pub fn change_header(&self, name: &str, value: &str) -> Result<Self> {
        let mut new_list: Vec<Header> = vec![];
        let mut check = false;
        for h in self.list.clone() {
            if name.to_lowercase() == h.name.as_str().to_lowercase() {
                new_list.push(Header {
                    name: name.to_string(),
                    value: value.to_string(),
                });
                check = true;
            } else {
                new_list.push(Header {
                    name: h.name.to_string(),
                    value: h.value.to_string(),
                });
            }
        }
        if !check {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                "Changed header is missing",
            ));
        }

        let prefix = Headers::get_headers_prefix(&self.raw)?;
        let new_h = Headers::new(prefix.as_str(), new_list);

        let new_headers = Headers {
            list: new_h.list,
            raw: new_h.raw,
        };

        Ok(new_headers)
    }

    pub fn add_header(&self, name: &str, value: &str) -> Result<Self> {
        let mut new_list = self.list.to_owned();
        new_list.push(Header {
            name: name.to_string(),
            value: value.to_string(),
        });

        let prefix = Headers::get_headers_prefix(&self.raw)?;
        let new_h = Headers::new(prefix.as_str(), new_list);

        let new_headers = Headers {
            list: new_h.list,
            raw: new_h.raw,
        };

        Ok(new_headers)
    }

    /// Parse content length from request headers
    pub fn get_content_length(raw: &String) -> Option<u32> {
        let low = Regex::new(r"(c|C)ontent-(l|L)ength:\s*\d+")
            .unwrap()
            .captures(&raw);

        #[allow(unused_assignments)]
        let mut check: Option<&str> = None;
        if let Some(v) = low {
            let low = v.get(0).unwrap();
            check = Some(low.as_str());
        }

        if let None = check {
            return None;
        }

        let cont_len = check.unwrap();

        let num = Regex::new(r"\d+").unwrap().captures(cont_len);
        if let None = num {
            return None;
        }
        let capts = num.unwrap();
        let num = capts.get(0);
        let num_str = num.unwrap().as_str();
        let num = num_str.parse::<u32>();
        if let Err(e) = num {
            println!("Failed parse content lenght from str: {}: {}", num_str, e);
            return None;
        }
        Some(num.unwrap())
    }

    /// Get url from raw headers
    pub fn get_url(raw: &String) -> String {
        let reg = Regex::new(r"\/[a-zA-Z0-9_\-\/]*").unwrap();
        let capts = reg.captures(raw.as_str());
        if let None = capts {
            return "/".to_string();
        }
        let capts = capts.unwrap();
        let url = capts.get(0).unwrap().as_str();
        url.to_string()
    }

    // Get protocol from raw headers
    pub fn get_protocol(raw: &String) -> String {
        let reg = Regex::new(r"HTTPS?\/\d+\.\d+").unwrap();
        let capts = reg.captures(raw.as_str());
        if let None = capts {
            return "OPTIONS".to_string();
        }
        let capts = capts.unwrap();
        let protocol = capts.get(0).unwrap().as_str();
        protocol.to_string()
    }

    // Get request prefix
    fn get_headers_prefix(raw: &String) -> Result<String> {
        let reg = Regex::new(format!(r".+{CRLF}").as_str()).unwrap();
        let capts = reg.captures(raw.as_str());
        if let None = capts {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                "Headers prefix didn't find",
            ));
        }
        let capts = capts.unwrap();
        let result = capts.get(0).unwrap().as_str();
        Ok(result.to_string())
    }

    /// Get method from raw headers
    pub fn get_method(raw: &String) -> String {
        let reg = Regex::new(r"\w+").unwrap();
        let capts = reg.captures(raw.as_str());
        if let None = capts {
            return "OPTIONS".to_string();
        }
        let capts = capts.unwrap();
        let method = capts.get(0).unwrap().as_str();
        method.to_string()
    }
}