From 274a3a9c0757452bd96df659df985103e4cef098 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 May 2021 11:59:46 +0100 Subject: [PATCH] keep future impl for response --- actix-http/CHANGES.md | 5 +- actix-http/src/h2/dispatcher.rs | 2 +- actix-http/src/message.rs | 8 +++ actix-http/src/response.rs | 81 ++++++++++++++++-------------- actix-http/src/response_builder.rs | 2 +- src/response/response.rs | 38 ++++++-------- src/scope.rs | 2 +- src/test.rs | 3 +- 8 files changed, 75 insertions(+), 66 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 171914122..51598288a 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -9,6 +9,7 @@ ### Changed * The `MessageBody` trait now has an associated `Error` type. [#2183] +* Places in `Response` where `ResponseBody` was received or returned now simply use `B`. [#2201] * `header` mod is now public. [#2171] * `uri` mod is now public. [#2171] * Update `language-tags` to `0.3`. @@ -17,12 +18,12 @@ ### Removed * Stop re-exporting `http` crate's `HeaderMap` types in addition to ours. [#2171] * Down-casting for `MessageBody` types. [#2183] -* `error::Result` alias. [#????] +* `error::Result` alias. [#2201] [#2171]: https://github.com/actix/actix-web/pull/2171 [#2183]: https://github.com/actix/actix-web/pull/2183 [#2196]: https://github.com/actix/actix-web/pull/2196 -[#????]: https://github.com/actix/actix-web/pull/???? +[#2201]: https://github.com/actix/actix-web/pull/2201 ## 3.0.0-beta.6 - 2021-04-17 diff --git a/actix-http/src/h2/dispatcher.rs b/actix-http/src/h2/dispatcher.rs index 4a17f4f1a..5be172aaf 100644 --- a/actix-http/src/h2/dispatcher.rs +++ b/actix-http/src/h2/dispatcher.rs @@ -346,7 +346,7 @@ where ServiceResponseStateProj::SendErrorPayload(ref mut stream, ref mut body) => { // TODO: de-dupe impl with SendPayload - + loop { match this.buffer { Some(ref mut buffer) => match ready!(stream.poll_capacity(cx)) { diff --git a/actix-http/src/message.rs b/actix-http/src/message.rs index 3659213ce..5ce40ee8e 100644 --- a/actix-http/src/message.rs +++ b/actix-http/src/message.rs @@ -389,6 +389,14 @@ impl BoxedResponseHead { pub fn new(status: StatusCode) -> Self { RESPONSE_POOL.with(|p| p.get_message(status)) } + + // used in: impl Future for Response + #[allow(dead_code)] + pub(crate) fn take(&mut self) -> Self { + BoxedResponseHead { + head: self.head.take(), + } + } } impl std::ops::Deref for BoxedResponseHead { diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index ac49c9d13..d9a32845f 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -19,7 +19,7 @@ use crate::{ /// An HTTP response. pub struct Response { pub(crate) head: BoxedResponseHead, - pub(crate) body: B, + pub(crate) body: Option, pub(crate) error: Option, } @@ -29,7 +29,7 @@ impl Response { pub fn new(status: StatusCode) -> Response { Response { head: BoxedResponseHead::new(status), - body: Body::Empty, + body: Some(Body::Empty), error: None, } } @@ -79,19 +79,6 @@ impl Response { resp.error = Some(error); resp } - - // /// Convert response to response with body - // pub fn into_body(self) -> Response { - // let b = match self.body { - // ResponseBody::Body(b) => b, - // ResponseBody::Other(b) => b, - // }; - // Response { - // head: self.head, - // error: self.error, - // body: ResponseBody::Other(b), - // } - // } } impl Response { @@ -100,7 +87,7 @@ impl Response { pub fn with_body(status: StatusCode, body: B) -> Response { Response { head: BoxedResponseHead::new(status), - body, + body: Some(body), error: None, } } @@ -173,14 +160,14 @@ impl Response { /// Get body of this response #[inline] pub fn body(&self) -> &B { - &self.body + self.body.as_ref().unwrap() } /// Set a body pub fn set_body(self, body: B2) -> Response { Response { head: self.head, - body, + body: Some(body), error: None, } } @@ -190,10 +177,10 @@ impl Response { ( Response { head: self.head, - body: (), + body: Some(()), error: self.error, }, - self.body, + self.body.unwrap(), ) } @@ -201,7 +188,7 @@ impl Response { pub fn drop_body(self) -> Response<()> { Response { head: self.head, - body: (), + body: Some(()), error: None, } } @@ -211,10 +198,10 @@ impl Response { ( Response { head: self.head, - body, + body: Some(body), error: self.error, }, - self.body, + self.body.unwrap(), ) } @@ -223,11 +210,11 @@ impl Response { where F: FnOnce(&mut ResponseHead, B) -> B2, { - let body = f(&mut self.head, self.body); + let body = f(&mut self.head, self.body.unwrap()); Response { - body, head: self.head, + body: Some(body), error: self.error, } } @@ -239,7 +226,7 @@ impl Response { /// Extract response body pub fn into_body(self) -> B { - self.body + self.body.unwrap() } } @@ -260,23 +247,41 @@ where for (key, val) in self.head.headers.iter() { let _ = writeln!(f, " {:?}: {:?}", key, val); } - let _ = writeln!(f, " body: {:?}", self.body.size()); + let _ = writeln!(f, " body: {:?}", self.body.as_ref().unwrap().size()); res } } -// TODO: document why this is needed -// impl Future for Response { -// type Output = Result, Infallible>; +impl Default for Response { + #[inline] + fn default() -> Response { + Response::with_body(StatusCode::default(), B::default()) + } +} -// fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { -// Poll::Ready(Ok(Response { -// head: self.head.take(), -// body: self.body.take_body(), -// error: self.error.take(), -// })) -// } -// } +mod fut { + use std::{ + convert::Infallible, + future::Future, + pin::Pin, + task::{Context, Poll}, + }; + + use super::*; + + // TODO: document why this is needed + impl Future for Response { + type Output = Result, Infallible>; + + fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { + Poll::Ready(Ok(Response { + head: self.head.take(), + body: self.body.take(), + error: self.error.take(), + })) + } + } +} /// Helper converters impl>, E: Into> From> for Response { diff --git a/actix-http/src/response_builder.rs b/actix-http/src/response_builder.rs index 5d94f2eb7..8568c23bd 100644 --- a/actix-http/src/response_builder.rs +++ b/actix-http/src/response_builder.rs @@ -251,7 +251,7 @@ impl ResponseBuilder { Response { head: response, - body, + body: Some(body), error: None, } } diff --git a/src/response/response.rs b/src/response/response.rs index 5d129ba67..a7a76e79a 100644 --- a/src/response/response.rs +++ b/src/response/response.rs @@ -2,7 +2,6 @@ use std::{ cell::{Ref, RefMut}, fmt, future::Future, - mem, pin::Pin, task::{Context, Poll}, }; @@ -12,6 +11,7 @@ use actix_http::{ http::{header::HeaderMap, StatusCode}, Extensions, Response, ResponseHead, }; +use futures_core::ready; #[cfg(feature = "cookies")] use { @@ -56,14 +56,6 @@ impl HttpResponse { error: Some(error), } } - - // /// Convert response to response with body - // pub fn into_body(self) -> HttpResponse { - // HttpResponse { - // res: self.res.into_body(), - // error: self.error, - // } - // } } impl HttpResponse { @@ -241,7 +233,7 @@ impl HttpResponse { // pub fn take_body(&mut self) -> ResponseBody { // self.res.take_body() // } - + /// Extract response body pub fn into_body(self) -> B { self.res.into_body() @@ -279,26 +271,28 @@ impl From> for Response { // TODO: expose cause somewhere? // if let Some(err) = res.error { - // eprintln!("impl From> for Response let Some(err)"); - // return Response::from_error(err).into_body(); + // return Response::from_error(err); // } res.res } } -impl Future for HttpResponse { - type Output = Result, Error>; +impl Future for HttpResponse { + type Output = Result, Error>; - fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { - // if let Some(err) = self.error.take() { - // return Poll::Ready(Ok(Response::from_error(err).into_body())); - // } + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + if let Some(err) = self.error.take() { + return Poll::Ready(Err(err)); + } - Poll::Ready(Ok(mem::replace( - &mut self.res, - Response::new(StatusCode::default()), - ))) + let res = &mut self.res; + actix_rt::pin!(res); + + match ready!(res.poll(cx)) { + Ok(val) => Poll::Ready(Ok(val)), + Err(err) => Poll::Ready(Err(err.into())), + } } } diff --git a/src/scope.rs b/src/scope.rs index 4bbf33cf6..412c01d95 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -578,7 +578,7 @@ mod tests { use actix_utils::future::ok; use bytes::Bytes; - use crate::dev::{Body}; + use crate::dev::Body; use crate::http::{header, HeaderValue, Method, StatusCode}; use crate::middleware::DefaultHeaders; use crate::service::ServiceRequest; diff --git a/src/test.rs b/src/test.rs index 102f07a37..de97dc8aa 100644 --- a/src/test.rs +++ b/src/test.rs @@ -4,9 +4,10 @@ use std::{net::SocketAddr, rc::Rc}; pub use actix_http::test::TestBuffer; use actix_http::{ + body, http::{header::IntoHeaderPair, Method, StatusCode, Uri, Version}, test::TestRequest as HttpTestRequest, - Extensions, Request, body + Extensions, Request, }; use actix_router::{Path, ResourceDef, Url}; use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};