cataclysm/http/
response.rs1use std::collections::HashMap;
2use crate::Error;
3
4pub 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 const CONTINUE: (u32, &'static str) = (100, "Continue");
35 const SWITCHING_PROTOCOLS: (u32, &'static str) = (101, "Switching Protocols");
36
37 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 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 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 pub fn r#continue() -> Response{ Response::CONTINUE.into() }
63 pub fn switching_protocols() -> Response{ Response::SWITCHING_PROTOCOLS.into() }
65
66 pub fn ok() -> Response { Response::OK.into() }
68 pub fn created() -> Response { Response::CREATED.into() }
70 pub fn accepted() -> Response { Response::ACCEPTED.into() }
72 pub fn non_authoritative_information() -> Response { Response::NON_AUTHORITATIVE_INFORMATION.into() }
74 pub fn no_content() -> Response { Response::NO_CONTENT.into() }
76 pub fn reset_content() -> Response { Response::RESET_CONTENT.into() }
78 pub fn partial_content() -> Response { Response::PARTIAL_CONTENT.into() }
80
81 pub fn bad_request() -> Response { Response::BAD_REQUEST.into() }
83 pub fn unauthorized() -> Response { Response::UNAUTHORIZED.into() }
85 pub fn payment_required() -> Response { Response::PAYMENT_REQUIRED.into() }
87 pub fn forbidden() -> Response { Response::FORBIDDEN.into() }
89 pub fn not_found() -> Response { Response::NOT_FOUND.into() }
91
92 pub fn internal_server_error() -> Response { Response::INTERNAL_SERVER_ERROR.into() }
94 pub fn not_implemented() -> Response { Response::NOT_IMPLEMENTED.into() }
96 pub fn bad_gateway() -> Response { Response::BAD_GATEWAY.into() }
98 pub fn service_unavailable() -> Response { Response::SERVICE_UNAVAILABLE.into() }
100
101 pub fn new() -> Response {
103 Response::OK.into()
104 }
105
106 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 pub fn body<T: AsRef<[u8]>>(mut self, body: T) -> Response {
114 self.content = Vec::from(body.as_ref());
115 self
116 }
117
118 pub fn status_code(&self) -> u32 {
120 self.status.0
121 }
122
123 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 response += "\r\n";
135 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 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 let mut content: Vec<_> = source.drain((split_index - 1)..).collect();
159 content.drain(..4);
161 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 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}