mirror of https://github.com/fafhrd91/actix-web
rename to_bytes_limited
This commit is contained in:
parent
0fd301ba14
commit
e5287bde38
|
@ -22,4 +22,4 @@ pub(crate) use self::message_body::MessageBodyMapErr;
|
||||||
pub use self::none::None;
|
pub use self::none::None;
|
||||||
pub use self::size::BodySize;
|
pub use self::size::BodySize;
|
||||||
pub use self::sized_stream::SizedStream;
|
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};
|
||||||
|
|
|
@ -11,7 +11,7 @@ use super::{BodySize, MessageBody};
|
||||||
///
|
///
|
||||||
/// Any errors produced by the body stream are returned immediately.
|
/// 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
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -30,12 +30,12 @@ use super::{BodySize, MessageBody};
|
||||||
/// # });
|
/// # });
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn to_bytes<B: MessageBody>(body: B) -> Result<Bytes, B::Error> {
|
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
|
.await
|
||||||
.expect("body should never overflow usize::MAX")
|
.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)]
|
#[derive(Debug)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct BodyLimitExceeded;
|
pub struct BodyLimitExceeded;
|
||||||
|
@ -50,23 +50,23 @@ pub struct BodyLimitExceeded;
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_http::body::{self, to_bytes_limit};
|
/// use actix_http::body::{self, to_bytes_limited};
|
||||||
/// use bytes::Bytes;
|
/// use bytes::Bytes;
|
||||||
///
|
///
|
||||||
/// # actix_rt::System::new().block_on(async {
|
/// # actix_rt::System::new().block_on(async {
|
||||||
/// let body = body::None::new();
|
/// 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());
|
/// assert!(bytes.is_empty());
|
||||||
///
|
///
|
||||||
/// let body = Bytes::from_static(b"123");
|
/// 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");
|
/// assert_eq!(bytes, "123");
|
||||||
///
|
///
|
||||||
/// let body = Bytes::from_static(b"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,
|
body: B,
|
||||||
limit: usize,
|
limit: usize,
|
||||||
) -> Result<Result<Bytes, B::Error>, BodyLimitExceeded> {
|
) -> Result<Result<Bytes, B::Error>, BodyLimitExceeded> {
|
||||||
|
@ -146,38 +146,42 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn to_body_limit_complete() {
|
async fn to_bytes_limited_complete() {
|
||||||
let bytes = to_bytes_limit((), 0).await.unwrap().unwrap();
|
let bytes = to_bytes_limited((), 0).await.unwrap().unwrap();
|
||||||
assert!(bytes.is_empty());
|
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!(bytes.is_empty());
|
||||||
|
|
||||||
assert!(to_bytes_limit(Bytes::from_static(b"12"), 0).await.is_err());
|
assert!(to_bytes_limited(Bytes::from_static(b"12"), 0)
|
||||||
assert!(to_bytes_limit(Bytes::from_static(b"12"), 1).await.is_err());
|
.await
|
||||||
assert!(to_bytes_limit(Bytes::from_static(b"12"), 2).await.is_ok());
|
.is_err());
|
||||||
assert!(to_bytes_limit(Bytes::from_static(b"12"), 3).await.is_ok());
|
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]
|
#[actix_rt::test]
|
||||||
async fn to_body_limit_streams() {
|
async fn to_bytes_limited_streams() {
|
||||||
// hinting a larger body fails
|
// hinting a larger body fails
|
||||||
let body = SizedStream::new(8, stream::empty().map(Ok::<_, Error>));
|
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
|
// hinting a smaller body is okay
|
||||||
let body = SizedStream::new(3, stream::empty().map(Ok::<_, Error>));
|
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
|
// hinting a smaller body then returning a larger one fails
|
||||||
let stream = stream::iter(vec![Bytes::from_static(b"1234")]).map(Ok::<_, Error>);
|
let stream = stream::iter(vec![Bytes::from_static(b"1234")]).map(Ok::<_, Error>);
|
||||||
let body = SizedStream::new(3, stream);
|
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")])
|
let stream = stream::iter(vec![Bytes::from_static(b"123"), Bytes::from_static(b"abc")])
|
||||||
.map(Ok::<_, Error>);
|
.map(Ok::<_, Error>);
|
||||||
let body = BodyStream::new(stream);
|
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]
|
#[actix_rt::test]
|
||||||
|
@ -185,6 +189,6 @@ mod tests {
|
||||||
let err_stream = stream::once(async { Err(io::Error::new(io::ErrorKind::Other, "")) });
|
let err_stream = stream::once(async { Err(io::Error::new(io::ErrorKind::Other, "")) });
|
||||||
let body = SizedStream::new(8, err_stream);
|
let body = SizedStream::new(8, err_stream);
|
||||||
// not too big, but propagates error from body 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue