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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::ops::{Deref, DerefMut};
use http::{ Request, method::Method };
use serde::{ de::DeserializeOwned };
use crate::api::{ ApiBody, ApiBodyInfo, ApiError };
use crate::handler::request::{ AsyncReadBody, CappedAsyncRead };
use async_trait::async_trait;
use futures::{ AsyncReadExt };
#[async_trait]
pub trait HandlerBody: Sized {
async fn handler_body(req: Request<&mut dyn AsyncReadBody>) -> Result<Self,ApiError>;
fn handler_method() -> Method { Method::POST }
}
pub trait IntoBody {
type Target: ApiBody;
fn into_body(self) -> Self::Target;
}
pub struct FromJson<T: ApiBody>(pub T);
#[async_trait]
impl <T: DeserializeOwned + ApiBody> HandlerBody for FromJson<T> {
async fn handler_body(req: Request<&mut dyn AsyncReadBody>) -> Result<Self,ApiError> {
let content_type = req.headers()
.get(http::header::CONTENT_TYPE)
.ok_or_else(content_type_not_json_err)?;
let content_type_is_json = content_type
.to_str()
.map(|s| s.to_ascii_lowercase() == "application/json")
.unwrap_or(false);
if !content_type_is_json {
return Err(content_type_not_json_err())
}
let mut body = vec![];
req.into_body().read_to_end(&mut body).await
.map_err(|e| ApiError {
code: 400,
internal_message: e.to_string(),
external_message: e.to_string(),
value: None
})?;
let json = serde_json::from_slice(&body)
.map_err(|e| ApiError {
code: 400,
internal_message: e.to_string(),
external_message: e.to_string(),
value: None
})?;
Ok(FromJson(json))
}
}
impl <T> ApiBody for FromJson<T> where T: ApiBody {
fn api_body_info() -> ApiBodyInfo {
T::api_body_info()
}
}
impl <T: ApiBody> Deref for FromJson<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl <T: ApiBody> DerefMut for FromJson<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl <T: ApiBody> IntoBody for FromJson<T> {
type Target = T;
fn into_body(self) -> Self::Target {
self.0
}
}
fn content_type_not_json_err() -> ApiError {
ApiError {
code: 415,
internal_message: "Content-Type must be application/json".to_string(),
external_message: "Content-Type must be application/json".to_string(),
value: None
}
}
pub struct FromBinary(pub Vec<u8>);
#[async_trait]
impl HandlerBody for FromBinary {
async fn handler_body(req: Request<&mut dyn AsyncReadBody>) -> Result<Self,ApiError> {
let mut body = vec![];
req.into_body().read_to_end(&mut body).await
.map_err(|e| ApiError {
code: 400,
internal_message: e.to_string(),
external_message: e.to_string(),
value: None
})?;
Ok(FromBinary(body))
}
}
impl ApiBody for FromBinary {
fn api_body_info() -> ApiBodyInfo {
ApiBodyInfo {
description: "Binary data".to_owned(),
ty: crate::api::ApiBodyType::Binary
}
}
}
impl Deref for FromBinary {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for FromBinary {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<FromBinary> for Vec<u8> {
fn from(b: FromBinary) -> Self {
b.0
}
}
impl IntoBody for FromBinary {
type Target = Vec<u8>;
fn into_body(self) -> Self::Target {
self.0
}
}
pub struct Capped<T: ApiBody + HandlerBody, const MAX_BYTES: usize>(pub T);
#[async_trait]
impl <T: ApiBody + HandlerBody, const MAX_BYTES: usize> HandlerBody for Capped<T, MAX_BYTES> {
async fn handler_body<'a>(req: Request<&'a mut dyn AsyncReadBody>) -> Result<Self,ApiError> {
let (parts, body) = req.into_parts();
let mut body = CappedAsyncRead::<_, MAX_BYTES>::new(body);
let req = Request::from_parts(parts, &mut body as &mut dyn AsyncReadBody);
T::handler_body(req).await.map(|res| Capped(res))
}
}
impl <T: ApiBody + HandlerBody, const MAX_BYTES: usize> ApiBody for Capped<T, MAX_BYTES> {
fn api_body_info() -> ApiBodyInfo {
T::api_body_info()
}
}
impl <T: ApiBody + HandlerBody, const MAX_BYTES: usize> Deref for Capped<T, MAX_BYTES> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl <T: ApiBody + HandlerBody, const MAX_BYTES: usize> DerefMut for Capped<T, MAX_BYTES> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl <T: ApiBody + HandlerBody + IntoBody, const MAX_BYTES: usize> IntoBody for Capped<T, MAX_BYTES> {
type Target = T::Target;
fn into_body(self) -> Self::Target {
self.0.into_body()
}
}