2021-02-15 12:20:43 +00:00
|
|
|
use std::{
|
|
|
|
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 crate::error::Error;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, E> BodyStream<S>
|
|
|
|
where
|
2021-04-13 11:16:12 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>>,
|
2021-02-15 12:20:43 +00:00
|
|
|
E: Into<Error>,
|
|
|
|
{
|
|
|
|
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-02-15 12:20:43 +00:00
|
|
|
E: Into<Error>,
|
|
|
|
{
|
2021-05-05 18:36:02 +01:00
|
|
|
type Error = Error;
|
|
|
|
|
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`].
|
|
|
|
///
|
|
|
|
/// Empty values are skipped to prevent [`BodyStream`]'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,
|
|
|
|
opt => opt.map(|res| res.map_err(Into::into)),
|
|
|
|
};
|
|
|
|
|
|
|
|
return Poll::Ready(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 13:34:22 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use actix_rt::pin;
|
|
|
|
use actix_utils::future::poll_fn;
|
|
|
|
use futures_util::stream;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::body::to_bytes;
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn skips_empty_chunks() {
|
|
|
|
let body = BodyStream::new(stream::iter(
|
|
|
|
["1", "", "2"]
|
|
|
|
.iter()
|
|
|
|
.map(|&v| Ok(Bytes::from(v)) as Result<Bytes, ()>),
|
|
|
|
));
|
|
|
|
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()
|
|
|
|
.map(|&v| Ok(Bytes::from(v)) as Result<Bytes, ()>),
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(to_bytes(body).await.ok(), Some(Bytes::from("12")));
|
|
|
|
}
|
|
|
|
}
|