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! {
|
|
|
|
/// Streaming response wrapper.
|
|
|
|
///
|
|
|
|
/// Response does not contain `Content-Length` header and appropriate transfer encoding is used.
|
|
|
|
pub struct BodyStream<S> {
|
|
|
|
#[pin]
|
|
|
|
stream: S,
|
|
|
|
}
|
2021-02-15 12:20:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
// TODO: from_infallible method
|
|
|
|
|
2021-02-15 12:20:43 +00:00
|
|
|
impl<S, E> BodyStream<S>
|
|
|
|
where
|
2021-04-13 11:16:12 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>>,
|
2021-06-17 17:57:58 +01:00
|
|
|
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(stream: S) -> Self {
|
|
|
|
BodyStream { stream }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, E> MessageBody for BodyStream<S>
|
|
|
|
where
|
2021-04-13 11:16:12 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>>,
|
2021-06-17 17:57:58 +01:00
|
|
|
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 {
|
|
|
|
BodySize::Stream
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to pull out the next value of the underlying [`Stream`].
|
|
|
|
///
|
2023-07-22 02:02:29 +01:00
|
|
|
/// Empty values are skipped to prevent [`BodyStream`]'s transmission being ended on a
|
|
|
|
/// zero-length chunk, but rather proceed until the underlying [`Stream`] ends.
|
2021-02-15 12:20:43 +00:00
|
|
|
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,
|
2021-06-17 17:57:58 +01:00
|
|
|
opt => opt,
|
2021-02-15 12:20:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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, time::Duration};
|
|
|
|
|
|
|
|
use actix_rt::{
|
|
|
|
pin,
|
|
|
|
time::{sleep, Sleep},
|
|
|
|
};
|
2021-04-13 13:34:22 +01:00
|
|
|
use actix_utils::future::poll_fn;
|
2025-02-10 01:27:56 +00:00
|
|
|
use derive_more::{Display, Error};
|
2021-06-17 17:57:58 +01:00
|
|
|
use futures_core::ready;
|
|
|
|
use futures_util::{stream, FutureExt as _};
|
2021-12-04 19:40:47 +00:00
|
|
|
use pin_project_lite::pin_project;
|
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!(BodyStream<stream::Empty<Result<Bytes, crate::Error>>>: MessageBody);
|
|
|
|
assert_impl_all!(BodyStream<stream::Empty<Result<Bytes, &'static str>>>: MessageBody);
|
|
|
|
assert_impl_all!(BodyStream<stream::Repeat<Result<Bytes, &'static str>>>: MessageBody);
|
|
|
|
assert_impl_all!(BodyStream<stream::Empty<Result<Bytes, Infallible>>>: MessageBody);
|
|
|
|
assert_impl_all!(BodyStream<stream::Repeat<Result<Bytes, Infallible>>>: MessageBody);
|
|
|
|
|
2022-02-08 13:37:01 +00:00
|
|
|
assert_not_impl_any!(BodyStream<stream::Empty<Bytes>>: MessageBody);
|
|
|
|
assert_not_impl_any!(BodyStream<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!(BodyStream<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 = BodyStream::new(stream::iter(
|
|
|
|
["1", "", "2"]
|
|
|
|
.iter()
|
2021-06-17 17:57:58 +01:00
|
|
|
.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 = BodyStream::new(stream::iter(
|
|
|
|
["1", "", "2"]
|
|
|
|
.iter()
|
2021-06-17 17:57:58 +01:00
|
|
|
.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-06-17 17:57:58 +01:00
|
|
|
#[derive(Debug, Display, Error)]
|
2024-08-18 22:17:03 +08:00
|
|
|
#[display("stream error")]
|
2021-06-17 17:57:58 +01:00
|
|
|
struct StreamErr;
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn stream_immediate_error() {
|
|
|
|
let body = BodyStream::new(stream::once(async { Err(StreamErr) }));
|
|
|
|
assert!(matches!(to_bytes(body).await, Err(StreamErr)));
|
|
|
|
}
|
|
|
|
|
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 = BodyStream::new(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 = BodyStream::new(stream::once(async {
|
|
|
|
Err(Box::<dyn StdError>::from("stringy error"))
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
to_bytes(body).await.unwrap_err().to_string(),
|
|
|
|
"stringy error"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:57:58 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn stream_delayed_error() {
|
2021-12-08 06:01:11 +00:00
|
|
|
let body = BodyStream::new(stream::iter(vec![Ok(Bytes::from("1")), Err(StreamErr)]));
|
2021-06-17 17:57:58 +01:00
|
|
|
assert!(matches!(to_bytes(body).await, Err(StreamErr)));
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
pin_project! {
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[project = TimeDelayStreamProj]
|
|
|
|
enum TimeDelayStream {
|
|
|
|
Start,
|
|
|
|
Sleep { delay: Pin<Box<Sleep>> },
|
|
|
|
Done,
|
|
|
|
}
|
2021-06-17 17:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for TimeDelayStream {
|
|
|
|
type Item = Result<Bytes, StreamErr>;
|
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
) -> Poll<Option<Self::Item>> {
|
|
|
|
match self.as_mut().get_mut() {
|
|
|
|
TimeDelayStream::Start => {
|
|
|
|
let sleep = sleep(Duration::from_millis(1));
|
2021-12-04 19:40:47 +00:00
|
|
|
self.as_mut().set(TimeDelayStream::Sleep {
|
|
|
|
delay: Box::pin(sleep),
|
|
|
|
});
|
2021-06-17 17:57:58 +01:00
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
TimeDelayStream::Sleep { ref mut delay } => {
|
2021-06-17 17:57:58 +01:00
|
|
|
ready!(delay.poll_unpin(cx));
|
|
|
|
self.set(TimeDelayStream::Done);
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeDelayStream::Done => Poll::Ready(Some(Err(StreamErr))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let body = BodyStream::new(TimeDelayStream::Start);
|
|
|
|
assert!(matches!(to_bytes(body).await, Err(StreamErr)));
|
|
|
|
}
|
2021-04-13 13:34:22 +01:00
|
|
|
}
|