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

use crate::{Method, Url};

/// A server request.
/// Parses the raw request string into a more usable format.
#[derive(Debug, Clone)]
pub struct Request {
    pub ip: SocketAddr,
    /// Raw URL string.
    /// Use `Request::parse_url()` to get a parsed version of the URL
    pub url: String,
    pub method: Method,
    pub body: String,
    pub headers: HashMap<String, String>,
}

impl Request {
    pub fn new(text: String, ip: SocketAddr) -> Self {
        let mut lines = text.lines();

        let first_line = lines.next().unwrap();
        let mut parts = first_line.split_whitespace();

        let method = Method::from(parts.next().unwrap().to_string());
        let url = parts.next().unwrap().into();

        let mut headers = HashMap::new();
        let mut in_body = false;
        let mut body = String::new();

        for line in lines {
            if line.is_empty() {
                in_body = true;
                continue;
            } else if in_body {
                body.push_str(line);
                continue;
            }

            let mut parts = line.splitn(2, ':');
            let key = parts.next().unwrap().into();
            let value = parts.next().unwrap().trim().into();

            headers.insert(key, value);
        }

        Self {
            ip,
            url,
            method,
            body,
            headers,
        }
    }

    pub fn get_header(&self, key: &str) -> Option<&str> {
        self.headers.get(key).map(|s| s.as_str())
    }

    pub fn get_header_or(&self, key: &str, default: &'static str) -> &str {
        self.get_header(key).unwrap_or(default)
    }

    pub fn set_header<T, K>(&mut self, key: T, value: K)
    where
        T: Into<String>,
        K: Into<String>,
    {
        self.headers.insert(key.into(), value.into());
    }

    /// Get a parsed version of the URL
    pub fn parse_url(&self) -> Url {
        self.url.as_str().into()
    }
}