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
use std::collections::HashMap;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum HTTPMethod {
    CONNECT,
    DELETE,
    GET,
    HEAD,
    OPTIONS,
    PATCH,
    POST,
    PUT,
    TRACE
}

pub struct HTTPRequest {
    method: HTTPMethod,
    uri: String,
    headers: HashMap<String, String>,
    queries: HashMap<String, String>,
    body: Vec<u8>
}

impl HTTPRequest {
    pub fn new(
        method: HTTPMethod,
        uri: &str,
        body: &[u8]
    ) -> Self {
        Self {
            method,
            uri: uri.to_string(),
            headers: HashMap::new(),
            queries: HashMap::new(),
            body: body.to_vec()
        }
    }

    /// Adds a header to the request, if the header already exists, it replaces
    /// it
    pub fn add_header(&mut self, key: &str, value: &str) {
        self.headers.insert(key.to_string(), value.to_string());
    }

    // Adds a query to the request, if the query already exists, it replaces it
    pub fn add_query(&mut self, key: &str, value: &str) {
        self.queries.insert(key.to_string(), value.to_string());
    }

    pub fn get_method(&self) -> HTTPMethod {
        self.method
    }

    pub fn get_uri(&self) -> String {
        self.uri.clone()
    }

    pub fn get_body(&self) -> Vec<u8> {
        self.body[..].to_vec() // just to ensure deep copy
    }

    pub fn get_header(&self, key: &str) -> Option<String> {
        self.headers.get(key).map(|val| {(*val).clone()})
    }

    pub fn get_query(&self, key: &str) -> Option<String> {
        self.queries.get(key).map(|val| {(*val).clone()})
    }
}