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
use crate::{header::Header, http::CRLF};
use regex::Regex;
use serde::Serialize;
use std::str;

#[derive(Debug, Serialize)]
pub struct Request {
    pub protocol: String,
    pub method: String,
    pub status: u16,
    pub status_text: String,
    pub content_length: usize,
    pub headers_raw: String,
    pub headers: Vec<Header>,
}

impl Request {
    pub fn new(buffer: Vec<u8>) -> Self {
        let raw = stringify(&buffer);
        Request::from_string(raw)
    }

    pub fn from_string(raw: String) -> Self {
        let mut content_length: usize = 0;
        let content_length_op = get_content_length(&raw);
        if let Some(val) = content_length_op {
            content_length = val;
        }
        Request {
            protocol: get_protocol(&raw),
            status: get_status(&raw),
            status_text: get_status_text(&raw),
            method: get_method(&raw),
            content_length,
            headers_raw: raw.clone(),
            headers: parse(raw),
        }
    }

    pub fn change_host(&mut self, target: &str) {
        let raw = change_host(self.headers_raw.clone(), target);
        self.headers_raw = raw.clone();
        self.headers = parse(raw);
    }
}

/// Parse headers
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 });
    }
    res
}

/// Stringify headers
fn stringify(heads: &Vec<u8>) -> String {
    let s = str::from_utf8(heads);
    match s {
        Ok(val) => val.to_string(),
        Err(err) => {
            println!("Failed to stringify headers: {:?}", err);
            "".to_string()
        }
    }
}

/// For change request headers host to host of target
fn change_host(heads: String, target: &str) -> String {
    let reg = Regex::new(r"Host: *.*\r\n").unwrap();
    let capts = reg.captures(heads.as_str());
    if let None = capts {
        return heads;
    }
    let capts = capts.unwrap();
    let old_host = capts.get(0).unwrap().as_str();
    heads.replace(old_host, format!("Host: {}\r\n", target).as_str())
}

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

    #[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::<usize>();
    if let Err(e) = num {
        println!("Failed parse content lenght from str: {}: {}", num_str, e);
        return None;
    }
    Some(num.unwrap())
}

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

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

fn get_status(raw: &String) -> u16 {
    let reg = Regex::new(r"\d{3}").unwrap();
    let capts = reg.captures(raw.as_str());
    let mut status: u16 = 500;
    if let None = capts {
        return status;
    }
    let capts = capts.unwrap();
    let status_r = capts.get(0).unwrap().as_str().parse::<u16>();
    if let Ok(val) = status_r {
        status = val;
    }
    status
}

fn get_status_text(raw: &String) -> String {
    let reg = Regex::new(r"\d{3}\s+\w+").unwrap();
    let capts = reg.captures(raw.as_str());
    let mut status_text: String = "Internal Server Error".to_string();
    if let None = capts {
        return status_text;
    }
    let capts = capts.unwrap();
    status_text = capts.get(0).unwrap().as_str().to_string();
    Regex::new(r"^\d{3}\s+")
        .unwrap()
        .replace_all(&status_text, "")
        .to_string()
}