aboutsummaryrefslogtreecommitdiff
path: root/src/request.rs
blob: 5d2b33fb3952bd0ef0b06537650c98e087549c20 (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
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
use std::collections::HashMap;

use crate::http::*;

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

impl Request {
    fn new(method: Endpoint, 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 {
            endpoint: method,
            headers,
            body,
        }
    }

    pub fn get_tag(&self, key: &str) -> Option<&String> {
        self.headers.as_ref().unwrap().0.get(&key.to_string())
    }

    pub fn endpoint(&self) -> &Endpoint {
        &self.endpoint
    }

    pub fn headers(&self) -> &Option<Headers> {
        &self.headers
    }

    pub fn body(&self) -> &Option<String> {
        &self.body
    }
}

impl From<Vec<&str>> for Request {
    fn from(value: Vec<&str>) -> Self {
        match &value[..] {
            [request_line, headers @ .., body] => {
                let (method, headers, body) =
                    (Endpoint::from(*request_line), Headers::from(headers), body);
                if let Some(content_length) = headers.0.get("Content-Length") {
                    let content_length = content_length
                        .parse::<usize>()
                        .expect("Content-Length should be parsable to usize");
                    Request::new(method, headers, (body[0..content_length]).to_string())
                } else {
                    Request::new(method, headers, (*body).to_string())
                }
            }
            _ => {
                unreachable!();
            }
        }
    }
}

impl<'a> Into<String> for Request {
    fn into(self) -> String {
        let method = String::from(self.endpoint);
        let (method, endpoint) = method.split_once(" ").unwrap();
        let status_line = format!("{} {} HTTP/1.1", method, endpoint);
        let headers = self
            .headers
            .unwrap_or(Headers(HashMap::new()))
            .0
            .iter()
            .map(|(key, value)| format!("{key}: {value}\r\n"))
            .collect::<String>();
        let body = self.body.unwrap_or("".to_string());
        format!("{status_line}\r\n{headers}\r\n{body}")
    }
}

impl Into<String> for &Request {
    fn into(self) -> String {
        let method = String::from(self.endpoint.clone());
        let (method, endpoint) = method.split_once(" ").unwrap();
        let status_line = format!("{} {} HTTP/1.1", method, endpoint);
        let headers = self
            .headers()
            .clone()
            .unwrap_or(Headers(HashMap::new()))
            .0
            .iter()
            .map(|(key, value)| format!("{key}: {value}\r\n"))
            .collect::<String>();
        let body = self.body.clone().unwrap_or("".to_string());
        format!("{status_line}\r\n{headers}\r\n{body}")
    }
}