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
use hyper::server;
pub use hyper::{header, Method, StatusCode};
#[derive(Debug)]
pub struct Response {
pub code: StatusCode,
pub content_type: header::ContentType,
pub content: String,
}
impl Response {
pub fn empty() -> Self {
Self::ok(String::new())
}
pub fn ok<T: Into<String>>(response: T) -> Self {
Response {
code: StatusCode::Ok,
content_type: header::ContentType::json(),
content: response.into(),
}
}
pub fn internal_error() -> Self {
Response {
code: StatusCode::Forbidden,
content_type: header::ContentType::plaintext(),
content: "Provided Host header is not whitelisted.\n".to_owned(),
}
}
pub fn host_not_allowed() -> Self {
Response {
code: StatusCode::Forbidden,
content_type: header::ContentType::plaintext(),
content: "Provided Host header is not whitelisted.\n".to_owned(),
}
}
pub fn unsupported_content_type() -> Self {
Response {
code: StatusCode::UnsupportedMediaType,
content_type: header::ContentType::plaintext(),
content: "Supplied content type is not allowed. Content-Type: application/json is required\n".to_owned(),
}
}
pub fn method_not_allowed() -> Self {
Response {
code: StatusCode::MethodNotAllowed,
content_type: header::ContentType::plaintext(),
content: "Used HTTP Method is not allowed. POST or OPTIONS is required\n".to_owned(),
}
}
pub fn invalid_cors() -> Self {
Response {
code: StatusCode::Forbidden,
content_type: header::ContentType::plaintext(),
content: "Origin of the request is not whitelisted. CORS headers would not be sent and any side-effects were cancelled as well.\n".to_owned(),
}
}
}
impl Into<server::Response> for Response {
fn into(self) -> server::Response {
server::Response::new()
.with_status(self.code)
.with_header(self.content_type)
.with_body(self.content)
}
}