Merge branch 'master' into master

This commit is contained in:
kevinpoitra 2020-01-28 05:56:12 -05:00 committed by GitHub
commit 6d575e726c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 159 additions and 67 deletions

View File

@ -7,6 +7,8 @@
* Use `sha-1` crate instead of unmaintained `sha1` crate * Use `sha-1` crate instead of unmaintained `sha1` crate
* Skip empty chunks when returning response from a `Stream` #1308
* Update the `time` dependency to 0.2.3 * Update the `time` dependency to 0.2.3
## [2.0.0] - 2019-12-25 ## [2.0.0] - 2019-12-25

View File

@ -5,6 +5,7 @@ use std::{fmt, mem};
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use futures_core::Stream; use futures_core::Stream;
use futures_util::ready;
use pin_project::{pin_project, project}; use pin_project::{pin_project, project};
use crate::error::Error; use crate::error::Error;
@ -389,12 +390,19 @@ where
BodySize::Stream 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, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> { fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> {
unsafe { Pin::new_unchecked(self) } let mut stream = unsafe { Pin::new_unchecked(self) }.project().stream;
.project() loop {
.stream return Poll::Ready(match ready!(stream.as_mut().poll_next(cx)) {
.poll_next(cx) Some(Ok(ref bytes)) if bytes.is_empty() => continue,
.map(|res| res.map(|res| res.map_err(std::convert::Into::into))) opt => opt.map(|res| res.map_err(Into::into)),
});
}
} }
} }
@ -424,17 +432,26 @@ where
BodySize::Sized64(self.size) BodySize::Sized64(self.size)
} }
/// 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, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> { fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> {
unsafe { Pin::new_unchecked(self) } let mut stream = unsafe { Pin::new_unchecked(self) }.project().stream;
.project() loop {
.stream return Poll::Ready(match ready!(stream.as_mut().poll_next(cx)) {
.poll_next(cx) Some(Ok(ref bytes)) if bytes.is_empty() => continue,
val => val,
});
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use futures::stream;
use futures_util::future::poll_fn; use futures_util::future::poll_fn;
impl Body { impl Body {
@ -589,4 +606,45 @@ mod tests {
BodySize::Sized(25) BodySize::Sized(25)
); );
} }
mod body_stream {
use super::*;
#[actix_rt::test]
async fn skips_empty_chunks() {
let mut body = BodyStream::new(stream::iter(
["1", "", "2"]
.iter()
.map(|&v| Ok(Bytes::from(v)) as Result<Bytes, ()>),
));
assert_eq!(
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
Some(Bytes::from("1")),
);
assert_eq!(
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
Some(Bytes::from("2")),
);
}
}
mod sized_stream {
use super::*;
#[actix_rt::test]
async fn skips_empty_chunks() {
let mut body = SizedStream::new(
2,
stream::iter(["1", "", "2"].iter().map(|&v| Ok(Bytes::from(v)))),
);
assert_eq!(
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
Some(Bytes::from("1")),
);
assert_eq!(
poll_fn(|cx| body.poll_next(cx)).await.unwrap().ok(),
Some(Bytes::from("2")),
);
}
}
} }

View File

@ -488,10 +488,12 @@ where
} }
} }
#[pin_project::pin_project(PinnedDrop)]
struct OpenWaitingConnection<F, Io> struct OpenWaitingConnection<F, Io>
where where
Io: AsyncRead + AsyncWrite + Unpin + 'static, Io: AsyncRead + AsyncWrite + Unpin + 'static,
{ {
#[pin]
fut: F, fut: F,
key: Key, key: Key,
h2: Option< h2: Option<
@ -525,12 +527,13 @@ where
} }
} }
impl<F, Io> Drop for OpenWaitingConnection<F, Io> #[pin_project::pinned_drop]
impl<F, Io> PinnedDrop for OpenWaitingConnection<F, Io>
where where
Io: AsyncRead + AsyncWrite + Unpin + 'static, Io: AsyncRead + AsyncWrite + Unpin + 'static,
{ {
fn drop(&mut self) { fn drop(self: Pin<&mut Self>) {
if let Some(inner) = self.inner.take() { if let Some(inner) = self.project().inner.take() {
let mut inner = inner.as_ref().borrow_mut(); let mut inner = inner.as_ref().borrow_mut();
inner.release(); inner.release();
inner.check_availibility(); inner.check_availibility();
@ -545,8 +548,8 @@ where
{ {
type Output = (); type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() }; let this = self.as_mut().project();
if let Some(ref mut h2) = this.h2 { if let Some(ref mut h2) = this.h2 {
return match Pin::new(h2).poll(cx) { return match Pin::new(h2).poll(cx) {
@ -571,7 +574,7 @@ where
}; };
} }
match unsafe { Pin::new_unchecked(&mut this.fut) }.poll(cx) { match this.fut.poll(cx) {
Poll::Ready(Err(err)) => { Poll::Ready(Err(err)) => {
let _ = this.inner.take(); let _ = this.inner.take();
if let Some(rx) = this.rx.take() { if let Some(rx) = this.rx.take() {
@ -589,8 +592,8 @@ where
))); )));
Poll::Ready(()) Poll::Ready(())
} else { } else {
this.h2 = Some(handshake(io).boxed_local()); *this.h2 = Some(handshake(io).boxed_local());
unsafe { Pin::new_unchecked(this) }.poll(cx) self.poll(cx)
} }
} }
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,

View File

@ -158,14 +158,16 @@ where
#[pin_project::pin_project] #[pin_project::pin_project]
struct ServiceResponse<F, I, E, B> { struct ServiceResponse<F, I, E, B> {
#[pin]
state: ServiceResponseState<F, B>, state: ServiceResponseState<F, B>,
config: ServiceConfig, config: ServiceConfig,
buffer: Option<Bytes>, buffer: Option<Bytes>,
_t: PhantomData<(I, E)>, _t: PhantomData<(I, E)>,
} }
#[pin_project::pin_project]
enum ServiceResponseState<F, B> { enum ServiceResponseState<F, B> {
ServiceCall(F, Option<SendResponse<Bytes>>), ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
SendPayload(SendStream<Bytes>, ResponseBody<B>), SendPayload(SendStream<Bytes>, ResponseBody<B>),
} }
@ -247,12 +249,14 @@ where
{ {
type Output = (); type Output = ();
#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project(); let mut this = self.as_mut().project();
match this.state { #[project]
ServiceResponseState::ServiceCall(ref mut call, ref mut send) => { match this.state.project() {
match unsafe { Pin::new_unchecked(call) }.poll(cx) { ServiceResponseState::ServiceCall(call, send) => {
match call.poll(cx) {
Poll::Ready(Ok(res)) => { Poll::Ready(Ok(res)) => {
let (res, body) = res.into().replace_body(()); let (res, body) = res.into().replace_body(());
@ -273,8 +277,7 @@ where
if size.is_eof() { if size.is_eof() {
Poll::Ready(()) Poll::Ready(())
} else { } else {
*this.state = this.state.set(ServiceResponseState::SendPayload(stream, body));
ServiceResponseState::SendPayload(stream, body);
self.poll(cx) self.poll(cx)
} }
} }
@ -300,10 +303,10 @@ where
if size.is_eof() { if size.is_eof() {
Poll::Ready(()) Poll::Ready(())
} else { } else {
*this.state = ServiceResponseState::SendPayload( this.state.set(ServiceResponseState::SendPayload(
stream, stream,
body.into_body(), body.into_body(),
); ));
self.poll(cx) self.poll(cx)
} }
} }

View File

@ -193,6 +193,30 @@ impl FromRequest for () {
macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => { macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
// This module is a trick to get around the inability of
// `macro_rules!` macros to make new idents. We want to make
// a new `FutWrapper` struct for each distinct invocation of
// this macro. Ideally, we would name it something like
// `FutWrapper_$fut_type`, but this can't be done in a macro_rules
// macro.
//
// Instead, we put everything in a module named `$fut_type`, thus allowing
// us to use the name `FutWrapper` without worrying about conflicts.
// This macro only exists to generate trait impls for tuples - these
// are inherently global, so users don't have to care about this
// weird trick.
#[allow(non_snake_case)]
mod $fut_type {
// Bring everything into scope, so we don't need
// redundant imports
use super::*;
/// A helper struct to allow us to pin-project through
/// to individual fields
#[pin_project::pin_project]
struct FutWrapper<$($T: FromRequest),+>($(#[pin] $T::Future),+);
/// FromRequest implementation for tuple /// FromRequest implementation for tuple
#[doc(hidden)] #[doc(hidden)]
#[allow(unused_parens)] #[allow(unused_parens)]
@ -205,7 +229,7 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
$fut_type { $fut_type {
items: <($(Option<$T>,)+)>::default(), items: <($(Option<$T>,)+)>::default(),
futs: ($($T::from_request(req, payload),)+), futs: FutWrapper($($T::from_request(req, payload),)+),
} }
} }
} }
@ -214,7 +238,8 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
#[pin_project::pin_project] #[pin_project::pin_project]
pub struct $fut_type<$($T: FromRequest),+> { pub struct $fut_type<$($T: FromRequest),+> {
items: ($(Option<$T>,)+), items: ($(Option<$T>,)+),
futs: ($($T::Future,)+), #[pin]
futs: FutWrapper<$($T,)+>,
} }
impl<$($T: FromRequest),+> Future for $fut_type<$($T),+> impl<$($T: FromRequest),+> Future for $fut_type<$($T),+>
@ -222,12 +247,12 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
type Output = Result<($($T,)+), Error>; type Output = Result<($($T,)+), Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let mut this = self.project();
let mut ready = true; let mut ready = true;
$( $(
if this.items.$n.is_none() { if this.items.$n.is_none() {
match unsafe { Pin::new_unchecked(&mut this.futs.$n) }.poll(cx) { match this.futs.as_mut().project().$n.poll(cx) {
Poll::Ready(Ok(item)) => { Poll::Ready(Ok(item)) => {
this.items.$n = Some(item); this.items.$n = Some(item);
} }
@ -246,6 +271,7 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
} }
} }
} }
}
}); });
#[rustfmt::skip] #[rustfmt::skip]