1use bytes::Buf;
2
3use super::HttpBody;
4use crate::common::buf::BufList;
5
6/// Aggregate the data buffers from a body asynchronously.
7///
8/// The returned `impl Buf` groups the `Buf`s from the `HttpBody` without
9/// copying them. This is ideal if you don't require a contiguous buffer.
10///
11/// # Note
12///
13/// Care needs to be taken if the remote is untrusted. The function doesn't implement any length
14/// checks and an malicious peer might make it consume arbitrary amounts of memory. Checking the
15/// `Content-Length` is a possibility, but it is not strictly mandated to be present.
16#[cfg_attr(
17 feature = "deprecated",
18 deprecated(
19 note = "This function has been replaced by a method on the `hyper::body::HttpBody` trait. Use `.collect().await?.aggregate()` instead."
20 )
21)]
22#[cfg_attr(feature = "deprecated", allow(deprecated))]
23pub async fn aggregate<T>(body: T) -> Result<impl Buf, T::Error>
24where
25 T: HttpBody,
26{
27 let mut bufs: BufList<::Data> = BufList::new();
28
29 futures_util::pin_mut!(body);
30 while let Some(buf: Result<::Data, …>) = body.data().await {
31 let buf: ::Data = buf?;
32 if buf.has_remaining() {
33 bufs.push(buf);
34 }
35 }
36
37 Ok(bufs)
38}
39