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

use super::status::Status;

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

/// HTTP headers
#[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() -> Self {
        Headers::from_string("".to_string())
    }

    // Create request headers
    pub fn new_request(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)
    }

    /// Create response headers
    pub fn new_response(status: &Status, list: Vec<Header>) -> Self {
        let postfix = Headers::to_string(list);
        let prefix = status.to_full_string();
        let raw = format!("{prefix}{CRLF}{postfix}");
        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))
    }

    fn change_header(&self, name: &str, value: &str) -> (Vec<Header>, bool) {
        let mut check = false;
        let mut new_list: Vec<Header> = vec![];
        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(),
                });
            }
        }
        (new_list, check)
    }

    /// Set new header or change old one
    pub fn set_header(&self, name: &str, value: &str) -> Result<Self> {
        let (mut new_list, check) = self.change_header(name, value);
        if !check {
            new_list.push(Header {
                name: name.to_string(),
                value: value.to_string(),
            });
        }

        let new_h = match self.is_response() {
            true => {
                let status = Headers::get_status(&self.raw)?;
                Headers::new_response(&status, new_list)
            }
            false => {
                let prefix = Headers::get_headers_prefix(&self.raw)?;
                Headers::new_request(prefix.as_str(), new_list)
            }
        };

        Ok(Headers {
            list: new_h.list,
            raw: new_h.raw,
        })
    }

    /// 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_status(raw: &String) -> Result<Status> {
        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();

        let result = Regex::new(format!(r"^HTTPS?\/\d+\.\d+ ").as_str())
            .unwrap()
            .replace_all(result, "")
            .to_string();

        let reg = Regex::new(format!(r"^\d+").as_str()).unwrap();
        let capts = reg.captures(result.as_str());
        if let None = capts {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                "Headers prefix didn't find",
            ));
        }
        let capts = capts.unwrap();
        let code = capts.get(0).unwrap().as_str().parse::<u16>();
        if let Err(err) = code {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!("Failed parse status code {:?}", err),
            ));
        }
        let code = code.unwrap();

        let text = Regex::new(format!(r"^\d+ ").as_str())
            .unwrap()
            .replace_all(result.as_str(), "")
            .to_string();

        Ok(Status { code, text })
    }

    /// 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()
    }

    // 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();
        let result = Regex::new(format!(r"{CRLF}+$").as_str())
            .unwrap()
            .replace_all(result, "");
        Ok(result.to_string())
    }

    fn is_response(&self) -> bool {
        let reg = Regex::new(r"^HTTPS?/\d+\.\d+").unwrap();
        let capts = reg.captures(self.raw.as_str());
        if let None = capts {
            return false;
        }
        true
    }
}