cataclysm/http/
response.rs

1use std::collections::HashMap;
2use crate::Error;
3
4/// Contains the data of an http response
5pub struct Response {
6    protocol: String,
7    pub(crate) status: (u32, String),
8    pub(crate) headers: HashMap<String, Vec<String>>,
9    pub content: Vec<u8>
10}
11
12impl<A: Into<Response>, B: Into<Response>> Into<Response> for Result<A, B> {
13    fn into(self) -> Response {
14        match self {
15            Ok(v) => v.into(),
16            Err(e) => e.into()
17        }
18    }
19}
20
21impl<A: Into<String>> From<(u32, A)> for Response {
22    fn from(source: (u32, A)) -> Response {
23        Response {
24            protocol: "HTTP/1.1".into(),
25            status: (source.0, source.1.into()),
26            headers: HashMap::new(),
27            content: Vec::new()
28        }
29    }
30}
31
32impl Response {
33    // Informational
34    const CONTINUE: (u32, &'static str) = (100, "Continue");
35    const SWITCHING_PROTOCOLS: (u32, &'static str) = (101, "Switching Protocols");
36
37    // Successful responses
38    const OK: (u32, &'static str) = (200, "OK");
39    const CREATED: (u32, &'static str) = (201, "Created");
40    const ACCEPTED: (u32, &'static str) = (202, "Accepted");
41    const NON_AUTHORITATIVE_INFORMATION: (u32, &'static str) = (203, "Non-Authoritative Information");
42    const NO_CONTENT: (u32, &'static str) = (204, "No Content");
43    const RESET_CONTENT: (u32, &'static str) = (205, "Reset Content");
44    const PARTIAL_CONTENT: (u32, &'static str) = (206, "Partial Content");
45
46    // Redirection Messages
47
48    // Client error responses
49    const BAD_REQUEST: (u32, &'static str) = (400, "Bad Request");
50    const UNAUTHORIZED: (u32, &'static str) = (401, "Unauthorized");
51    const PAYMENT_REQUIRED: (u32, &'static str) = (402, "Payment Required");
52    const FORBIDDEN: (u32, &'static str) = (403, "Forbidden");
53    const NOT_FOUND: (u32, &'static str) = (404, "Not Found");
54
55    // Server error responses
56    const INTERNAL_SERVER_ERROR: (u32, &'static str) = (500, "Internal Server Error");
57    const NOT_IMPLEMENTED: (u32, &'static str) = (501, "Not Implemented");
58    const BAD_GATEWAY: (u32, &'static str) = (502, "Bad Gateway");
59    const SERVICE_UNAVAILABLE: (u32, &'static str) = (503, "Service Unavailable");
60
61    /// Creates an Continue response, with a 100 status code
62    pub fn r#continue() -> Response{ Response::CONTINUE.into() }
63    /// Creates an Switching Protocols response, with a 101 status code
64    pub fn switching_protocols() -> Response{ Response::SWITCHING_PROTOCOLS.into() }
65
66    /// Creates an Ok response, with a 200 status code
67    pub fn ok() -> Response { Response::OK.into() }
68    /// Creates a Created response, with a 201 status code
69    pub fn created() -> Response { Response::CREATED.into() }
70    /// Creates an Accepted response, with a 202 status code
71    pub fn accepted() -> Response { Response::ACCEPTED.into() }
72    /// Creates a Non-Authoritative Information response, with a 203 status code
73    pub fn non_authoritative_information() -> Response { Response::NON_AUTHORITATIVE_INFORMATION.into() }
74    /// Creates a No Content response, with a 204 status code
75    pub fn no_content() -> Response { Response::NO_CONTENT.into() }
76    /// Creates a Reset Content response, with a 205 status code
77    pub fn reset_content() -> Response { Response::RESET_CONTENT.into() }
78    /// Creates a Partial Content response, with a 206 status code
79    pub fn partial_content() -> Response { Response::PARTIAL_CONTENT.into() }
80
81    /// Creates a Bad Request response, with a 400 status code
82    pub fn bad_request() -> Response { Response::BAD_REQUEST.into() }
83    /// Creates an Unauthorized response, with a 401 status code
84    pub fn unauthorized() -> Response { Response::UNAUTHORIZED.into() }
85    /// Creates a Payment Required response, with a 402 status code
86    pub fn payment_required() -> Response { Response::PAYMENT_REQUIRED.into() }
87    /// Creates a Forbidden response, with a 403 status code
88    pub fn forbidden() -> Response { Response::FORBIDDEN.into() }
89    /// Creates a Not Found response, with a 404 status code
90    pub fn not_found() -> Response { Response::NOT_FOUND.into() }
91
92    /// Creates an Internal Server Error response, with a 500 status code
93    pub fn internal_server_error() -> Response { Response::INTERNAL_SERVER_ERROR.into() }
94    /// Creates a Not Implemented response, with a 501 status code
95    pub fn not_implemented() -> Response { Response::NOT_IMPLEMENTED.into() }
96    /// Creates a Bad Gateway response, with a 502 status code
97    pub fn bad_gateway() -> Response { Response::BAD_GATEWAY.into() }
98    /// Creates a Service Unavailable response, with a 503 status code
99    pub fn service_unavailable() -> Response { Response::SERVICE_UNAVAILABLE.into() }
100
101    /// Creates a new response, with defaut response status 200, and a text/html content type
102    pub fn new() -> Response {
103        Response::OK.into()
104    }
105
106    /// Inserts a header into the response
107    pub fn header<A: Into<String>, B: Into<String>>(mut self, key: A, value: B) -> Response {
108        self.headers.entry(key.into()).or_insert_with(|| Vec::new()).push(value.into());
109        self
110    }
111
112    /// Inserts a body in the response
113    pub fn body<T: AsRef<[u8]>>(mut self, body: T) -> Response {
114        self.content = Vec::from(body.as_ref());
115        self
116    }
117
118    /// Returns the status code contained in the response
119    pub fn status_code(&self) -> u32 {
120        self.status.0
121    }
122
123    /// Serializes the response to be sent to the client
124    pub(crate) fn serialize(&mut self) -> Vec<u8> {
125        let mut response = format!("{} {} {}\r\n", self.protocol, self.status.0, self.status.1);
126
127        self.headers.entry("Content-Length".to_string()).or_insert_with(|| Vec::new()).push(format!("{}", self.content.len()));
128        for (header_name, headers) in &self.headers {
129            for header in headers {
130                response += &format!("{}: {}\r\n", header_name, header);
131            }
132        }
133        // We finalize the headers
134        response += "\r\n";
135        // And now add the body, if any
136        let mut response = response.into_bytes();
137        response.extend_from_slice(&self.content);
138        response
139    }
140
141    pub(crate) fn parse<A: Into<Vec<u8>>>(bytes: A) -> Result<Response, Error> {
142        let mut source: Vec<u8> = bytes.into();
143
144        // http call should have at least 3 bytes. For sure
145        let (one, two) = (source.iter(), source.iter().skip(2));
146
147        let mut split_index = None;
148        for (idx, (a, b)) in one.zip(two).enumerate() {
149            if a==b && b==&b'\n' && idx > 0 && source[idx-1] == b'\r' && source[idx+1] == b'\r' {
150                split_index = Some(idx);
151                break;
152            }
153        }
154
155        let split_index = split_index.ok_or(Error::Parse(format!("no end of header was found")))?;
156
157        // The minus one is a safe operation, due to the upper for loop
158        let mut content: Vec<_> = source.drain((split_index - 1)..).collect();
159        // We have to remove the `\r\n\r\n` that is at the beginning of the remaining bytes
160        content.drain(..4);
161        // The request header needs to be a string
162        let response_string = String::from_utf8(source).map_err(|e| Error::Parse(format!("{}", e)))?;
163
164        let mut lines = response_string.split("\r\n");
165        let first_line = lines.next().ok_or(Error::Parse("response has no first line".into()))?;
166        let tokens = first_line.split(" ").collect::<Vec<_>>();
167        let (protocol, code, status_text) = if tokens.len() < 3 {
168            return Err(Error::Parse("responses's first has incorrect format".into()));
169        } else {
170            (
171                tokens[0].to_string(),
172                tokens[1].parse::<u32>().map_err(|e| Error::custom(format!("{}", e)))?,
173                tokens[2..].join(" ")
174            )
175        };
176        // We parse the remaining headers
177        let mut headers = HashMap::new();
178        for line in lines {
179            let idx = line.find(":").ok_or(Error::Parse(format!("corrupted header missing colon")))?;
180            let (key, value) = line.split_at(idx);
181            let (key, value) = (key.to_string(), value.trim_start_matches(": ").trim_end().to_string());
182            headers.entry(key).or_insert_with(|| Vec::new()).push(value);
183        }
184
185        Ok(Response {
186            protocol,
187            status: (code, status_text),
188            headers,
189            content
190        })
191    }
192}