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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
pub use hyper::{self, header::HeaderValue, Body, Method, StatusCode};
#[derive(Debug)]
pub struct Response {
pub code: StatusCode,
pub content_type: HeaderValue,
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: HeaderValue::from_static("application/json; charset=utf-8"),
content: response.into(),
}
}
pub fn internal_error<T: Into<String>>(msg: T) -> Self {
Response {
code: StatusCode::INTERNAL_SERVER_ERROR,
content_type: plain_text(),
content: format!("Internal Server Error: {}", msg.into()),
}
}
pub fn service_unavailable<T: Into<String>>(msg: T) -> Self {
Response {
code: StatusCode::SERVICE_UNAVAILABLE,
content_type: HeaderValue::from_static("application/json; charset=utf-8"),
content: msg.into(),
}
}
pub fn host_not_allowed() -> Self {
Response {
code: StatusCode::FORBIDDEN,
content_type: plain_text(),
content: "Provided Host header is not whitelisted.\n".to_owned(),
}
}
pub fn unsupported_content_type() -> Self {
Response {
code: StatusCode::UNSUPPORTED_MEDIA_TYPE,
content_type: plain_text(),
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::METHOD_NOT_ALLOWED,
content_type: plain_text(),
content: "Used HTTP Method is not allowed. POST or OPTIONS is required\n".to_owned(),
}
}
pub fn invalid_allow_origin() -> Self {
Response {
code: StatusCode::FORBIDDEN,
content_type: plain_text(),
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(),
}
}
pub fn invalid_allow_headers() -> Self {
Response {
code: StatusCode::FORBIDDEN,
content_type: plain_text(),
content: "Requested headers are not allowed for CORS. CORS headers would not be sent and any side-effects were cancelled as well.\n".to_owned(),
}
}
pub fn bad_request<S: Into<String>>(msg: S) -> Self {
Response {
code: StatusCode::BAD_REQUEST,
content_type: plain_text(),
content: msg.into(),
}
}
pub fn too_large<S: Into<String>>(msg: S) -> Self {
Response {
code: StatusCode::PAYLOAD_TOO_LARGE,
content_type: plain_text(),
content: msg.into(),
}
}
}
fn plain_text() -> HeaderValue {
HeaderValue::from_static("text/plain; charset=utf-8")
}
impl From<Response> for hyper::Response<Body> {
fn from(res: Response) -> hyper::Response<Body> {
hyper::Response::builder()
.status(res.code)
.header("content-type", res.content_type)
.body(res.content.into())
.expect("Unable to parse response body for type conversion")
}
}