summaryrefslogtreecommitdiff
path: root/spider/src/request.rs
blob: fa32bbd9bd971717d1794bb4648de9ef9675eeee (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
use std::collections::HashMap;
use std::fmt;

use crate::http_method::HttpMethod;

pub struct Request {
    pub method: HttpMethod,
    path: String,
    http_version: String,
    headers: HashMap<String, String>,
    body: Vec<u8>,
}

impl Request {
    pub fn new( method: HttpMethod
              , path: String
              , http_version: String
              , headers: HashMap<String, String>
              , body: Vec<u8> ) -> Request {
        return Request {
            method,
            path,
            http_version,
            headers,
            body,
        };
    }

    // TODO(andrew): add some error handling.
    pub fn from(data: &[u8]) -> Option<Request> {
        let mut lines: Vec<String> = Vec::new();
        let mut line = String::new();
        let mut idx = 0;
        let mut char_count = 0;

        // Parsing headers until first empty line (char_count == 0).
        for c in data {
            let c = *c as char;
            line.push(c);
            idx += 1;

            if c == '\n' {
                lines.push(line);
                if char_count == 0 { break; }
                else { line = String::new(); }
            }
            if c != '\r' && c != '\n' {
                char_count += 1;
            }
        }

        let body = data[idx..].to_vec();

        let mut headers: HashMap<String, String> = HashMap::new();
        for line in &lines[1..] {
            let line = line
                .trim_end_matches("\r\n")
                .split(": ")
                .collect::<Vec<&str>>();
            headers.insert(String::from(line[0]), line[1..].join(" "));
        }

        let first_line = lines[0]
            .trim_end_matches("\r\n")
            .split(" ")
            .collect::<Vec<&str>>();

        let method = String::from(first_line[0]);
        match HttpMethod::parse(method) {
            Some(method) =>
                return Some(Request::new(
                    method,
                    String::from(first_line[1]),
                    String::from(first_line[2]),
                    headers,
                    body )),
            None => return None
        };
    }
}

impl fmt::Display for Request {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        return write!(f, "Request({}, {})", self.method, self.path);
    }
}