2021-02-15 12:20:43 +00:00
|
|
|
use std::{
|
2021-06-17 17:57:58 +01:00
|
|
|
error::Error as StdError,
|
2021-02-15 12:20:43 +00:00
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures_core::{ready, Stream};
|
2021-04-13 11:16:12 +01:00
|
|
|
use pin_project_lite::pin_project;
|
2021-02-15 12:20:43 +00:00
|
|
|
|
|
|
|
use super::{BodySize, MessageBody};
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
pin_project! {
|
|
|
|
/// Known sized streaming response wrapper.
|
|
|
|
///
|
2021-06-17 17:57:58 +01:00
|
|
|
/// This body implementation should be used if total size of stream is known. Data is sent as-is
|
|
|
|
/// without using chunked transfer encoding.
|
2021-04-13 11:16:12 +01:00
|
|
|
pub struct SizedStream<S> {
|
|
|
|
size: u64,
|
|
|
|
#[pin]
|
|
|
|
stream: S,
|
|
|
|
}
|
2021-02-15 12:20:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 17:57:58 +01:00
|
|
|
impl<S, E> SizedStream<S>
|
2021-02-15 12:20:43 +00:00
|
|
|
where
|
2021-06-17 17:57:58 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>>,
|
|
|
|
E: Into<Box<dyn StdError>> + 'static,
|
2021-02-15 12:20:43 +00:00
|
|
|
{
|
2021-12-11 16:05:08 +00:00
|
|
|
#[inline]
|
2021-02-15 12:20:43 +00:00
|
|
|
pub fn new(size: u64, stream: S) -> Self {
|
|
|
|
SizedStream { size, stream }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
// TODO: from_infallible method
|
|
|
|
|
2021-06-17 17:57:58 +01:00
|
|
|
impl<S, E> MessageBody for SizedStream<S>
|
2021-02-15 12:20:43 +00:00
|
|
|
where
|
2021-06-17 17:57:58 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>>,
|
|
|
|
E: Into<Box<dyn StdError>> + 'static,
|
2021-02-15 12:20:43 +00:00
|
|
|
{
|
2021-06-17 17:57:58 +01:00
|
|
|
type Error = E;
|
2021-05-05 18:36:02 +01:00
|
|
|
|
2021-12-11 16:05:08 +00:00
|
|
|
#[inline]
|
2021-02-15 12:20:43 +00:00
|
|
|
fn size(&self) -> BodySize {
|
2023-01-01 20:56:27 +00:00
|
|
|
BodySize::Sized(self.size)
|
2021-02-15 12:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to pull out the next value of the underlying [`Stream`].
|
|
|
|
///
|
|
|
|
/// Empty values are skipped to prevent [`SizedStream`]'s transmission being
|
|
|
|
/// ended on a zero-length chunk, but rather proceed until the underlying
|
|
|
|
/// [`Stream`] ends.
|
|
|
|
fn poll_next(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
2021-05-05 18:36:02 +01:00
|
|
|
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
2021-02-15 12:20:43 +00:00
|
|
|
loop {
|
2021-04-13 11:16:12 +01:00
|
|
|
let stream = self.as_mut().project().stream;
|
2021-02-15 12:20:43 +00:00
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
let chunk = match ready!(stream.poll_next(cx)) {
|
2021-02-15 12:20:43 +00:00
|
|
|
Some(Ok(ref bytes)) if bytes.is_empty() => continue,
|
|
|
|
val => val,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Poll::Ready(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 13:34:22 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-17 17:57:58 +01:00
|
|
|
use std::convert::Infallible;
|
|
|
|
|
2021-04-13 13:34:22 +01:00
|
|
|
use actix_rt::pin;
|
|
|
|
use actix_utils::future::poll_fn;
|
|
|
|
use futures_util::stream;
|
2022-02-08 13:37:01 +00:00
|
|
|
use static_assertions::{assert_impl_all, assert_not_impl_any};
|
2021-04-13 13:34:22 +01:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::body::to_bytes;
|
|
|
|
|
2021-11-16 21:41:35 +00:00
|
|
|
assert_impl_all!(SizedStream<stream::Empty<Result<Bytes, crate::Error>>>: MessageBody);
|
|
|
|
assert_impl_all!(SizedStream<stream::Empty<Result<Bytes, &'static str>>>: MessageBody);
|
|
|
|
assert_impl_all!(SizedStream<stream::Repeat<Result<Bytes, &'static str>>>: MessageBody);
|
|
|
|
assert_impl_all!(SizedStream<stream::Empty<Result<Bytes, Infallible>>>: MessageBody);
|
|
|
|
assert_impl_all!(SizedStream<stream::Repeat<Result<Bytes, Infallible>>>: MessageBody);
|
|
|
|
|
2022-02-08 13:37:01 +00:00
|
|
|
assert_not_impl_any!(SizedStream<stream::Empty<Bytes>>: MessageBody);
|
|
|
|
assert_not_impl_any!(SizedStream<stream::Repeat<Bytes>>: MessageBody);
|
2021-11-16 21:41:35 +00:00
|
|
|
// crate::Error is not Clone
|
2022-02-08 13:37:01 +00:00
|
|
|
assert_not_impl_any!(SizedStream<stream::Repeat<Result<Bytes, crate::Error>>>: MessageBody);
|
2021-11-16 21:41:35 +00:00
|
|
|
|
2021-04-13 13:34:22 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn skips_empty_chunks() {
|
|
|
|
let body = SizedStream::new(
|
|
|
|
2,
|
2021-06-17 17:57:58 +01:00
|
|
|
stream::iter(
|
|
|
|
["1", "", "2"]
|
|
|
|
.iter()
|
|
|
|
.map(|&v| Ok::<_, Infallible>(Bytes::from(v))),
|
|
|
|
),
|
2021-04-13 13:34:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
pin!(body);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
poll_fn(|cx| body.as_mut().poll_next(cx))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.ok(),
|
|
|
|
Some(Bytes::from("1")),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
poll_fn(|cx| body.as_mut().poll_next(cx))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.ok(),
|
|
|
|
Some(Bytes::from("2")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn read_to_bytes() {
|
|
|
|
let body = SizedStream::new(
|
|
|
|
2,
|
2021-06-17 17:57:58 +01:00
|
|
|
stream::iter(
|
|
|
|
["1", "", "2"]
|
|
|
|
.iter()
|
|
|
|
.map(|&v| Ok::<_, Infallible>(Bytes::from(v))),
|
|
|
|
),
|
2021-04-13 13:34:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(to_bytes(body).await.ok(), Some(Bytes::from("12")));
|
|
|
|
}
|
2021-11-16 21:41:35 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn stream_string_error() {
|
|
|
|
// `&'static str` does not impl `Error`
|
|
|
|
// but it does impl `Into<Box<dyn Error>>`
|
|
|
|
|
|
|
|
let body = SizedStream::new(0, stream::once(async { Err("stringy error") }));
|
|
|
|
assert_eq!(to_bytes(body).await, Ok(Bytes::new()));
|
|
|
|
|
|
|
|
let body = SizedStream::new(1, stream::once(async { Err("stringy error") }));
|
|
|
|
assert!(matches!(to_bytes(body).await, Err("stringy error")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn stream_boxed_error() {
|
|
|
|
// `Box<dyn Error>` does not impl `Error`
|
|
|
|
// but it does impl `Into<Box<dyn Error>>`
|
|
|
|
|
|
|
|
let body = SizedStream::new(
|
|
|
|
0,
|
|
|
|
stream::once(async { Err(Box::<dyn StdError>::from("stringy error")) }),
|
|
|
|
);
|
|
|
|
assert_eq!(to_bytes(body).await.unwrap(), Bytes::new());
|
|
|
|
|
|
|
|
let body = SizedStream::new(
|
|
|
|
1,
|
|
|
|
stream::once(async { Err(Box::<dyn StdError>::from("stringy error")) }),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_bytes(body).await.unwrap_err().to_string(),
|
|
|
|
"stringy error"
|
|
|
|
);
|
|
|
|
}
|
2021-04-13 13:34:22 +01:00
|
|
|
}
|