rename to_bytes_limited

This commit is contained in:
Rob Ede 2023-03-12 16:54:21 +00:00
parent 0fd301ba14
commit e5287bde38
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 26 additions and 22 deletions

View File

@ -22,4 +22,4 @@ pub(crate) use self::message_body::MessageBodyMapErr;
pub use self::none::None;
pub use self::size::BodySize;
pub use self::sized_stream::SizedStream;
pub use self::utils::{to_bytes, to_bytes_limit, BodyLimitExceeded};
pub use self::utils::{to_bytes, to_bytes_limited, BodyLimitExceeded};

View File

@ -11,7 +11,7 @@ use super::{BodySize, MessageBody};
///
/// Any errors produced by the body stream are returned immediately.
///
/// Consider using [`to_bytes_limit`] instead to protect against memory exhaustion.
/// Consider using [`to_bytes_limited`] instead to protect against memory exhaustion.
///
/// # Examples
///
@ -30,12 +30,12 @@ use super::{BodySize, MessageBody};
/// # });
/// ```
pub async fn to_bytes<B: MessageBody>(body: B) -> Result<Bytes, B::Error> {
to_bytes_limit(body, usize::MAX)
to_bytes_limited(body, usize::MAX)
.await
.expect("body should never overflow usize::MAX")
}
/// Error type returned from [`to_bytes_limit`] when body produced exceeds limit.
/// Error type returned from [`to_bytes_limited`] when body produced exceeds limit.
#[derive(Debug)]
#[non_exhaustive]
pub struct BodyLimitExceeded;
@ -50,23 +50,23 @@ pub struct BodyLimitExceeded;
/// # Examples
///
/// ```
/// use actix_http::body::{self, to_bytes_limit};
/// use actix_http::body::{self, to_bytes_limited};
/// use bytes::Bytes;
///
/// # actix_rt::System::new().block_on(async {
/// let body = body::None::new();
/// let bytes = to_bytes_limit(body, 10).await.unwrap().unwrap();
/// 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_limit(body, 10).await.unwrap().unwrap();
/// let bytes = to_bytes_limited(body, 10).await.unwrap().unwrap();
/// assert_eq!(bytes, "123");
///
/// let body = Bytes::from_static(b"123");
/// assert!(to_bytes_limit(body, 2).await.is_err());
/// assert!(to_bytes_limited(body, 2).await.is_err());
/// # });
/// ```
pub async fn to_bytes_limit<B: MessageBody>(
pub async fn to_bytes_limited<B: MessageBody>(
body: B,
limit: usize,
) -> Result<Result<Bytes, B::Error>, BodyLimitExceeded> {
@ -146,38 +146,42 @@ mod tests {
}
#[actix_rt::test]
async fn to_body_limit_complete() {
let bytes = to_bytes_limit((), 0).await.unwrap().unwrap();
async fn to_bytes_limited_complete() {
let bytes = to_bytes_limited((), 0).await.unwrap().unwrap();
assert!(bytes.is_empty());
let bytes = to_bytes_limit((), 1).await.unwrap().unwrap();
let bytes = to_bytes_limited((), 1).await.unwrap().unwrap();
assert!(bytes.is_empty());
assert!(to_bytes_limit(Bytes::from_static(b"12"), 0).await.is_err());
assert!(to_bytes_limit(Bytes::from_static(b"12"), 1).await.is_err());
assert!(to_bytes_limit(Bytes::from_static(b"12"), 2).await.is_ok());
assert!(to_bytes_limit(Bytes::from_static(b"12"), 3).await.is_ok());
assert!(to_bytes_limited(Bytes::from_static(b"12"), 0)
.await
.is_err());
assert!(to_bytes_limited(Bytes::from_static(b"12"), 1)
.await
.is_err());
assert!(to_bytes_limited(Bytes::from_static(b"12"), 2).await.is_ok());
assert!(to_bytes_limited(Bytes::from_static(b"12"), 3).await.is_ok());
}
#[actix_rt::test]
async fn to_body_limit_streams() {
async fn to_bytes_limited_streams() {
// hinting a larger body fails
let body = SizedStream::new(8, stream::empty().map(Ok::<_, Error>));
assert!(to_bytes_limit(body, 3).await.is_err());
assert!(to_bytes_limited(body, 3).await.is_err());
// hinting a smaller body is okay
let body = SizedStream::new(3, stream::empty().map(Ok::<_, Error>));
assert!(to_bytes_limit(body, 3).await.unwrap().unwrap().is_empty());
assert!(to_bytes_limited(body, 3).await.unwrap().unwrap().is_empty());
// hinting a smaller body then returning a larger one fails
let stream = stream::iter(vec![Bytes::from_static(b"1234")]).map(Ok::<_, Error>);
let body = SizedStream::new(3, stream);
assert!(to_bytes_limit(body, 3).await.is_err());
assert!(to_bytes_limited(body, 3).await.is_err());
let stream = stream::iter(vec![Bytes::from_static(b"123"), Bytes::from_static(b"abc")])
.map(Ok::<_, Error>);
let body = BodyStream::new(stream);
assert!(to_bytes_limit(body, 3).await.is_err());
assert!(to_bytes_limited(body, 3).await.is_err());
}
#[actix_rt::test]
@ -185,6 +189,6 @@ mod tests {
let err_stream = stream::once(async { Err(io::Error::new(io::ErrorKind::Other, "")) });
let body = SizedStream::new(8, err_stream);
// not too big, but propagates error from body stream
assert!(to_bytes_limit(body, 10).await.unwrap().is_err());
assert!(to_bytes_limited(body, 10).await.unwrap().is_err());
}
}