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
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use actix_http::{error::PayloadError, Payload};
use bytes::{Bytes, BytesMut};
use futures_core::{ready, Stream};
use pin_project_lite::pin_project;

pin_project! {
    pub(crate) struct ReadBody<S> {
        #[pin]
        pub(crate) stream: Payload<S>,
        pub(crate) buf: BytesMut,
        pub(crate) limit: usize,
    }
}

impl<S> ReadBody<S> {
    pub(crate) fn new(stream: Payload<S>, limit: usize) -> Self {
        Self {
            stream,
            buf: BytesMut::new(),
            limit,
        }
    }
}

impl<S> Future for ReadBody<S>
where
    S: Stream<Item = Result<Bytes, PayloadError>>,
{
    type Output = Result<Bytes, PayloadError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();

        while let Some(chunk) = ready!(this.stream.as_mut().poll_next(cx)?) {
            if (this.buf.len() + chunk.len()) > *this.limit {
                return Poll::Ready(Err(PayloadError::Overflow));
            }

            this.buf.extend_from_slice(&chunk);
        }

        Poll::Ready(Ok(this.buf.split().freeze()))
    }
}

#[cfg(test)]
mod tests {
    use static_assertions::assert_impl_all;

    use super::*;
    use crate::any_body::AnyBody;

    assert_impl_all!(ReadBody<()>: Unpin);
    assert_impl_all!(ReadBody<AnyBody>: Unpin);
}