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
///! Module [`Header`]
use crate::http::CRLF;
#[allow(unused_imports)]
use napi_derive::napi;
use regex::Regex;
use serde::Serialize;
use std::fmt;

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

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

impl Header {
    /// Parse headers
    pub fn parse(heads: String) -> Vec<Header> {
        let mut res: Vec<Header> = vec![];
        let heads = heads.split(CRLF);
        for h in heads {
            // TODO check it reg
            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()
                // FIXME
                .replace(": ", "");

            let reg_value = Regex::new(r": *.*$").unwrap();
            let capts_value = reg_value.captures(h);
            if let None = capts_value {
                res.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.push(Header {
                name,
                value: value.to_lowercase(),
            });
        }
        res
    }
}