volo-http 0.5.5

HTTP framework implementation of volo.
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! HTTP Body implementation for [`http_body::Body`]
//!
//! See [`Body`] for more details.

use std::{
    convert::Infallible,
    error::Error,
    fmt,
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use bytes::Bytes;
use faststr::FastStr;
use futures_util::stream::Stream;
use http_body::{Frame, SizeHint};
use http_body_util::{BodyExt, Full, StreamBody, combinators::BoxBody};
use hyper::body::Incoming;
use linkedbytes::{LinkedBytes, Node};
use pin_project::pin_project;
#[cfg(feature = "json")]
use serde::de::DeserializeOwned;

use crate::error::BoxError;

// The `futures_util::stream::BoxStream` does not have `Sync`
type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + Sync + 'a>>;

/// An implementation for [`http_body::Body`].
#[pin_project]
pub struct Body {
    #[pin]
    repr: BodyRepr,
}

#[pin_project(project = BodyProj)]
enum BodyRepr {
    /// Complete [`Bytes`], with a certain size and content
    Full(#[pin] Full<Bytes>),
    /// Wrapper of [`Incoming`], it usually appears in request of server or response of client.
    ///
    /// Althrough [`Incoming`] implements [`http_body::Body`], the type is so commonly used, we
    /// wrap it here as [`BodyRepr::Hyper`] to avoid cost of [`Box`] with dynamic dispatch.
    Hyper(#[pin] Incoming),
    /// Boxed stream with `Item = Result<Frame<Bytes>, BoxError>`
    Stream(#[pin] StreamBody<BoxStream<'static, Result<Frame<Bytes>, BoxError>>>),
    /// Boxed [`http_body::Body`]
    Body(#[pin] BoxBody<Bytes, BoxError>),
}

impl Default for Body {
    fn default() -> Self {
        Body::empty()
    }
}

impl Body {
    /// Create an empty body.
    pub fn empty() -> Self {
        Self {
            repr: BodyRepr::Full(Full::new(Bytes::new())),
        }
    }

    /// Create a body by [`Incoming`].
    ///
    /// Compared to [`Body::from_body`], this function avoids overhead of allocating by [`Box`]
    /// and dynamic dispatch by [`dyn http_body::Body`][http_body::Body].
    pub fn from_incoming(incoming: Incoming) -> Self {
        Self {
            repr: BodyRepr::Hyper(incoming),
        }
    }

    /// Create a body by a [`Stream`] with `Item = Result<Frame<Bytes>, BoxError>`.
    pub fn from_stream<S>(stream: S) -> Self
    where
        S: Stream<Item = Result<Frame<Bytes>, BoxError>> + Send + Sync + 'static,
    {
        Self {
            repr: BodyRepr::Stream(StreamBody::new(Box::pin(stream))),
        }
    }

    /// Create a body by another [`http_body::Body`] instance.
    pub fn from_body<B>(body: B) -> Self
    where
        B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
        B::Error: Into<BoxError>,
    {
        Self {
            repr: BodyRepr::Body(BoxBody::new(body.map_err(Into::into))),
        }
    }
}

impl http_body::Body for Body {
    type Data = Bytes;
    type Error = BoxError;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        match self.project().repr.project() {
            BodyProj::Full(full) => http_body::Body::poll_frame(full, cx).map_err(BoxError::from),
            BodyProj::Hyper(incoming) => {
                http_body::Body::poll_frame(incoming, cx).map_err(BoxError::from)
            }
            BodyProj::Stream(stream) => http_body::Body::poll_frame(stream, cx),
            BodyProj::Body(body) => http_body::Body::poll_frame(body, cx),
        }
    }

    fn is_end_stream(&self) -> bool {
        match &self.repr {
            BodyRepr::Full(full) => http_body::Body::is_end_stream(full),
            BodyRepr::Hyper(incoming) => http_body::Body::is_end_stream(incoming),
            BodyRepr::Stream(stream) => http_body::Body::is_end_stream(stream),
            BodyRepr::Body(body) => http_body::Body::is_end_stream(body),
        }
    }

    fn size_hint(&self) -> SizeHint {
        match &self.repr {
            BodyRepr::Full(full) => http_body::Body::size_hint(full),
            BodyRepr::Hyper(incoming) => http_body::Body::size_hint(incoming),
            BodyRepr::Stream(stream) => http_body::Body::size_hint(stream),
            BodyRepr::Body(body) => http_body::Body::size_hint(body),
        }
    }
}

impl fmt::Debug for Body {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.repr {
            BodyRepr::Full(_) => f.write_str("Body::Full"),
            BodyRepr::Hyper(_) => f.write_str("Body::Hyper"),
            BodyRepr::Stream(_) => f.write_str("Body::Stream"),
            BodyRepr::Body(_) => f.write_str("Body::Body"),
        }
    }
}

mod sealed {
    pub trait SealedBody
    where
        Self: http_body::Body + Sized + Send,
        Self::Data: Send,
    {
    }

    impl<T> SealedBody for T
    where
        T: http_body::Body + Send,
        T::Data: Send,
    {
    }
}

/// An extend trait for [`http_body::Body`] that can converting a body to other types
pub trait BodyConversion: sealed::SealedBody
where
    <Self as http_body::Body>::Data: Send,
{
    /// Consume a body and convert it into [`Bytes`].
    fn into_bytes(self) -> impl Future<Output = Result<Bytes, BodyConvertError>> + Send {
        async {
            Ok(self
                .collect()
                .await
                .map_err(|_| BodyConvertError::BodyCollectionError)?
                .to_bytes())
        }
    }

    /// Consume a body and convert it into [`Vec<u8>`].
    fn into_vec(self) -> impl Future<Output = Result<Vec<u8>, BodyConvertError>> + Send {
        async { Ok(self.into_bytes().await?.into()) }
    }

    /// Consume a body and convert it into [`String`].
    fn into_string(self) -> impl Future<Output = Result<String, BodyConvertError>> + Send {
        async {
            let vec = self.into_vec().await?;

            // SAFETY: The `Vec<u8>` is checked by `simdutf8` and it is a valid `String`
            let _ =
                simdutf8::basic::from_utf8(&vec).map_err(|_| BodyConvertError::StringUtf8Error)?;
            Ok(unsafe { String::from_utf8_unchecked(vec) })
        }
    }

    /// Consume a body and convert it into [`String`].
    ///
    /// # Safety
    ///
    /// It is up to the caller to guarantee that the value really is valid. Using this when the
    /// content is invalid causes immediate undefined behavior.
    unsafe fn into_string_unchecked(
        self,
    ) -> impl Future<Output = Result<String, BodyConvertError>> + Send {
        async {
            let vec = self.into_vec().await?;

            Ok(unsafe { String::from_utf8_unchecked(vec) })
        }
    }

    /// Consume a body and convert it into [`FastStr`].
    fn into_faststr(self) -> impl Future<Output = Result<FastStr, BodyConvertError>> + Send {
        async {
            let bytes = self.into_bytes().await?;

            // SAFETY: The `Vec<u8>` is checked by `simdutf8` and it is a valid `String`
            let _ = simdutf8::basic::from_utf8(&bytes)
                .map_err(|_| BodyConvertError::StringUtf8Error)?;
            Ok(unsafe { FastStr::from_bytes_unchecked(bytes) })
        }
    }

    /// Consume a body and convert it into [`FastStr`].
    ///
    /// # Safety
    ///
    /// It is up to the caller to guarantee that the value really is valid. Using this when the
    /// content is invalid causes immediate undefined behavior.
    unsafe fn into_faststr_unchecked(
        self,
    ) -> impl Future<Output = Result<FastStr, BodyConvertError>> + Send {
        async {
            let bytes = self.into_bytes().await?;

            Ok(unsafe { FastStr::from_bytes_unchecked(bytes) })
        }
    }

    /// Consume a body and convert it into an instance with [`DeserializeOwned`].
    #[cfg(feature = "json")]
    fn into_json<T>(self) -> impl Future<Output = Result<T, BodyConvertError>> + Send
    where
        T: DeserializeOwned,
    {
        async {
            let bytes = self.into_bytes().await?;
            crate::utils::json::deserialize(&bytes).map_err(BodyConvertError::JsonDeserializeError)
        }
    }
}

impl<T> BodyConversion for T
where
    T: sealed::SealedBody,
    <T as http_body::Body>::Data: Send,
{
}

/// General error for polling [`http_body::Body`] or converting the [`Bytes`] just polled.
#[derive(Debug)]
pub enum BodyConvertError {
    /// Failed to collect the body
    BodyCollectionError,
    /// The body is not a valid utf-8 string
    StringUtf8Error,
    /// Failed to deserialize the json
    #[cfg(feature = "json")]
    JsonDeserializeError(crate::utils::json::Error),
}

impl fmt::Display for BodyConvertError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BodyCollectionError => f.write_str("failed to collect body"),
            Self::StringUtf8Error => f.write_str("body is not a valid string"),
            #[cfg(feature = "json")]
            Self::JsonDeserializeError(e) => write!(f, "failed to deserialize body: {e}"),
        }
    }
}

impl Error for BodyConvertError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            #[cfg(feature = "json")]
            Self::JsonDeserializeError(e) => Some(e),
            _ => None,
        }
    }
}

impl From<()> for Body {
    fn from(_: ()) -> Self {
        Self::empty()
    }
}

impl From<&'static str> for Body {
    fn from(value: &'static str) -> Self {
        Self {
            repr: BodyRepr::Full(Full::new(Bytes::from_static(value.as_bytes()))),
        }
    }
}

impl From<Vec<u8>> for Body {
    fn from(value: Vec<u8>) -> Self {
        Self {
            repr: BodyRepr::Full(Full::new(Bytes::from(value))),
        }
    }
}

impl From<Bytes> for Body {
    fn from(value: Bytes) -> Self {
        Self {
            repr: BodyRepr::Full(Full::new(value)),
        }
    }
}

impl From<FastStr> for Body {
    fn from(value: FastStr) -> Self {
        Self {
            repr: BodyRepr::Full(Full::new(value.into_bytes())),
        }
    }
}

impl From<String> for Body {
    fn from(value: String) -> Self {
        Self {
            repr: BodyRepr::Full(Full::new(Bytes::from(value))),
        }
    }
}

struct LinkedBytesBody<I> {
    inner: I,
}

impl<I> http_body::Body for LinkedBytesBody<I>
where
    I: Iterator<Item = Node> + Unpin,
{
    type Data = Bytes;
    type Error = Infallible;

    fn poll_frame(
        self: Pin<&mut Self>,
        _: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        let this = self.get_mut();
        let Some(node) = this.inner.next() else {
            return Poll::Ready(None);
        };
        let bytes = match node {
            Node::Bytes(bytes) => bytes,
            Node::BytesMut(bytesmut) => bytesmut.freeze(),
            Node::FastStr(faststr) => faststr.into_bytes(),
        };
        Poll::Ready(Some(Ok(Frame::data(bytes))))
    }

    fn is_end_stream(&self) -> bool {
        false
    }

    fn size_hint(&self) -> SizeHint {
        let (lower, upper) = self.inner.size_hint();
        let mut size_hint = SizeHint::new();
        size_hint.set_lower(lower as u64);
        if let Some(upper) = upper {
            size_hint.set_upper(upper as u64);
        }
        size_hint
    }
}

impl From<LinkedBytes> for Body {
    fn from(value: LinkedBytes) -> Self {
        Body::from_body(LinkedBytesBody {
            inner: value.into_iter_list(),
        })
    }
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use faststr::FastStr;
    use linkedbytes::LinkedBytes;

    use super::Body;
    use crate::body::BodyConversion;

    #[tokio::test]
    async fn test_from_linked_bytes() {
        let mut bytes = LinkedBytes::new();
        bytes.insert(Bytes::from_static(b"Hello, "));
        bytes.insert_faststr(FastStr::new("world!"));
        let body = Body::from(bytes);
        assert_eq!(body.into_string().await.unwrap(), "Hello, world!");
    }
}