Function actix_http::body::to_bytes_limited
source · pub async fn to_bytes_limited<B: MessageBody>(
body: B,
limit: usize,
) -> Result<Result<Bytes, B::Error>, BodyLimitExceeded>
Expand description
Collects the bytes produced by body
, up to limit
bytes.
If a chunk read from poll_next
causes the total number of bytes read to exceed limit
, an
Err(BodyLimitExceeded)
is returned.
Any errors produced by the body stream are returned immediately as Ok(Err(B::Error))
.
§Examples
use actix_http::body::{self, to_bytes_limited};
use bytes::Bytes;
let body = body::None::new();
let bytes = to_bytes_limited(body, 10).await.unwrap().unwrap();
assert!(bytes.is_empty());
let body = Bytes::from_static(b"123");
let bytes = to_bytes_limited(body, 10).await.unwrap().unwrap();
assert_eq!(bytes, "123");
let body = Bytes::from_static(b"123");
assert!(to_bytes_limited(body, 2).await.is_err());