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 };

/// This trait is implemented by anything that represents the incoming request type.
/// Only one argument implementing this can be asked for in a given handler. The type
/// that implements this is used to determine the input type expected by the handler
/// for the sake of generating API information.
#[async_trait]
pub trait HandlerBody: Sized {
    /// Given a request containing arbitrary bytes, this function needs to return an
    /// instance of the type that this trait is implemented on (typically by deserializing
    /// it from the bytes provided), or else it should return an error describing what
    /// went wrong.
    async fn handler_body(req: Request<&mut dyn AsyncReadBody>) -> Result<Self,ApiError>;
    /// Which HTTP method is required for this Body to be valid. By default, if a body
    /// is present in the handler we'll expect the method to be POST. Implement this function
    /// to override that.
    fn handler_method() -> Method { Method::POST }
}

/// A simple trait that makes it a little more ergonomic in some cases to extract the body
/// out of our various types like [`FromJson`] and [`FromBinary`]. Useful when we combine
/// types like [`Capped`] so that we can avoid multiple layers of unwrapping.
pub trait IntoBody {
    /// The body type that will be extracted. Whatever we extract should itself implement
    /// [`trait@ApiBody`] to correspond to the generated type information.
    type Target: ApiBody;

    /// Extract the body of the request with this, unwrapping as many layers of nesting as
    /// needed.
    fn into_body(self) -> Self::Target;
}

/// If the last argument to a handler is this, we'll assume
/// that the user needs to provide JSON that decodes to `T`.
/// Notably, `T` needs to implement `ApiBody` with the
/// Deserialize option.
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())
        }

        // Stream our body into a vector of bytes:
        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
            })?;

        // Assume JSON and parse:
        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
    }
}

/// If the last argument to a handler is this, we'll assume
/// that the user can provide arbitrary binary data, and
/// we'll make that data available within the handler as bytes.
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
    }
}


/// This wraps anything implementing [`HandlerBody`] and puts a type level cap on the size
/// that the request body is allowed to be before this is rejected. This works best when the
/// request body is streamed, as it will stop the streaming once said limit is reached.
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()
    }
}