Skip to content

Commit cdba30d

Browse files
tyranronJohnTitor
andcommitted
Skip empty chucks for BodyStream and SizedStream (actix#1308)
* Skip empty chucks for BodyStream and SizedStream when streaming response (actix#1267) * Fix tests to fail on previous implementation Co-authored-by: Yuki Okushi <[email protected]>
1 parent 74dcc73 commit cdba30d

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
### Changed
66

7-
* Use `sha-1` crate instead of unmaintained `sha1` crate
7+
* Use `sha-1` crate instead of unmaintained `sha1` crate
8+
9+
* Skip empty chunks when returning response from a `Stream` #1308
810

911
## [2.0.0] - 2019-12-25
1012

actix-http/src/body.rs

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{fmt, mem};
55

66
use bytes::{Bytes, BytesMut};
77
use futures_core::Stream;
8+
use futures_util::ready;
89
use pin_project::{pin_project, project};
910

1011
use crate::error::Error;
@@ -389,12 +390,19 @@ where
389390
BodySize::Stream
390391
}
391392

393+
/// Attempts to pull out the next value of the underlying [`Stream`].
394+
///
395+
/// Empty values are skipped to prevent [`BodyStream`]'s transmission being
396+
/// ended on a zero-length chunk, but rather proceed until the underlying
397+
/// [`Stream`] ends.
392398
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> {
393-
unsafe { Pin::new_unchecked(self) }
394-
.project()
395-
.stream
396-
.poll_next(cx)
397-
.map(|res| res.map(|res| res.map_err(std::convert::Into::into)))
399+
let mut stream = unsafe { Pin::new_unchecked(self) }.project().stream;
400+
loop {
401+
return Poll::Ready(match ready!(stream.as_mut().poll_next(cx)) {
402+
Some(Ok(ref bytes)) if bytes.is_empty() => continue,
403+
opt => opt.map(|res| res.map_err(Into::into)),
404+
});
405+
}
398406
}
399407
}
400408

@@ -424,17 +432,26 @@ where
424432
BodySize::Sized64(self.size)
425433
}
426434

435+
/// Attempts to pull out the next value of the underlying [`Stream`].
436+
///
437+
/// Empty values are skipped to prevent [`SizedStream`]'s transmission being
438+
/// ended on a zero-length chunk, but rather proceed until the underlying
439+
/// [`Stream`] ends.
427440
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> {
428-
unsafe { Pin::new_unchecked(self) }
429-
.project()
430-
.stream
431-
.poll_next(cx)
441+
let mut stream = unsafe { Pin::new_unchecked(self) }.project().stream;
442+
loop {
443+
return Poll::Ready(match ready!(stream.as_mut().poll_next(cx)) {
444+
Some(Ok(ref bytes)) if bytes.is_empty() => continue,
445+
val => val,
446+
});
447+
}
432448
}
433449
}
434450

435451
#[cfg(test)]
436452
mod tests {
437453
use super::*;
454+
use futures::stream;
438455
use futures_util::future::poll_fn;
439456

440457
impl Body {
@@ -589,4 +606,45 @@ mod tests {
589606
BodySize::Sized(25)
590607
);
591608
}
609+
610+
mod body_stream {
611+
use super::*;
612+
613+
#[actix_rt::test]
614+
async fn skips_empty_chunks() {
615+
let mut body = BodyStream::new(stream::iter(
616+
["1", "", "2"]
617+
.iter()
618+
.map(|&v| Ok(Bytes::from(v)) as Result<Bytes, ()>),
619+
));
620+
assert_eq!(
621+
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
622+
Some(Bytes::from("1")),
623+
);
624+
assert_eq!(
625+
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
626+
Some(Bytes::from("2")),
627+
);
628+
}
629+
}
630+
631+
mod sized_stream {
632+
use super::*;
633+
634+
#[actix_rt::test]
635+
async fn skips_empty_chunks() {
636+
let mut body = SizedStream::new(
637+
2,
638+
stream::iter(["1", "", "2"].iter().map(|&v| Ok(Bytes::from(v)))),
639+
);
640+
assert_eq!(
641+
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
642+
Some(Bytes::from("1")),
643+
);
644+
assert_eq!(
645+
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
646+
Some(Bytes::from("2")),
647+
);
648+
}
649+
}
592650
}

0 commit comments

Comments
 (0)