1use core::pin::pin;
4
5use xitca_http::body::BodyExt;
6
7use crate::{body::BodyStream, service::pipeline::PipelineE};
8
9pub async fn collect_body<B>(body: B) -> Result<Vec<u8>, B::Error>
11where
12 B: BodyStream,
13{
14 let mut body = pin!(body);
15
16 let mut res = Vec::new();
17
18 while let Some(frame) = body.as_mut().data().await {
19 res.extend_from_slice(frame?.as_ref());
20 }
21
22 Ok(res)
23}
24
25pub type CollectStringError<E> = PipelineE<std::string::FromUtf8Error, E>;
26
27pub async fn collect_string_body<B>(body: B) -> Result<String, CollectStringError<B::Error>>
29where
30 B: BodyStream,
31{
32 let body = collect_body(body).await.map_err(CollectStringError::Second)?;
33 String::from_utf8(body).map_err(CollectStringError::First)
34}