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
/**
 * An HTTP Response
 */
pub struct Response {
    version: String,
    status_code: usize,
    status_message: String,
    headers: Vec<String>,
    body: Vec<u8>
}

impl Response {
    /**
     * Creates a new HTTP Response
     */
    pub fn new(version: &str, status_code: usize, status_message: &str, body: &[u8]) -> Self {
        Self {
            version: version.to_string(),
            status_code,
            status_message: status_message.to_string(),
            headers: vec![],
            body: body.to_vec()
        }
    }

    /**
     * Creates an Ok Response (code 200)
     */
    pub fn create_200(version: &str, body: &[u8]) -> Self {
        Self::new(version, 200, "Ok", body)
    }

    /**
     * Creates a Not Found Response (code 404)
     */
    pub fn create_404(version: &str, body: &[u8]) -> Self {
        Self::new(version, 404, "Not Found", body)
    }

    /**
     * Adds a header to the response
     */
    pub fn add_header(&mut self, header: &str) {
        self.headers.push(header.to_string());
    }

    /**
     * Parses the response into a vector of bytes
     */
    pub fn parse(&self) -> Vec<u8> {
        let mut response_bytes = vec![];

        response_bytes.extend_from_slice(self.version.as_bytes());
        response_bytes.push(b' ');
        response_bytes.extend_from_slice(self.status_code.to_string().as_bytes());
        response_bytes.push(b' ');
        response_bytes.extend_from_slice(self.status_message.as_bytes());
        response_bytes.extend_from_slice(b"\r\n");
        for header in &self.headers {
            response_bytes.extend_from_slice(header.as_bytes());
            response_bytes.extend_from_slice(b"\r\n");
        }
        response_bytes.extend_from_slice(b"\r\n");
        response_bytes.extend_from_slice(&self.body);

        response_bytes
    }
}