aboutsummaryrefslogtreecommitdiff
path: root/src/request.rs
blob: 8728e79906996c14b1f333825ed15cedfb74117b (plain)
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
use crate::http_types::*;

#[derive(Debug)]
pub struct Request {
    pub method: HTTPMethod,
    pub headers: Option<Headers>,
    body: Option<String>,
}

impl Request {
    fn new(method: HTTPMethod, headers: Headers, body: String) -> Self {
        let headers = if headers.0.len() == 0 {
            None
        } else {
            Some(headers)
        };
        let body = if body.is_empty() { None } else { Some(body) };
        Request {
            method,
            headers,
            body,
        }
    }
}

impl From<&str> for Request {
    fn from(val: &str) -> Self {
        let request: Vec<&str> = val.split("\r\n").collect();
        match &request[..] {
            [request_line, headers @ .., body] => {
                let (method, headers, body) = (
                    HTTPMethod::from(*request_line),
                    Headers::from(headers),
                    body.to_string(),
                );
                Request::new(method, headers, body)
            }
            _ => {
                unreachable!();
            }
        }
    }
}