1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

move Payload to inner http request

This commit is contained in:
Nikolay Kim 2019-05-22 11:49:27 -07:00
parent 7746e785c1
commit d3e807f6e9
5 changed files with 39 additions and 33 deletions

View File

@ -215,19 +215,21 @@ where
inner.path.get_mut().update(&head.uri); inner.path.get_mut().update(&head.uri);
inner.path.reset(); inner.path.reset();
inner.head = head; inner.head = head;
inner.payload = payload;
inner.app_data = self.data.clone(); inner.app_data = self.data.clone();
req req
} else { } else {
HttpRequest::new( HttpRequest::new(
Path::new(Url::new(head.uri.clone())), Path::new(Url::new(head.uri.clone())),
head, head,
payload,
self.rmap.clone(), self.rmap.clone(),
self.config.clone(), self.config.clone(),
self.data.clone(), self.data.clone(),
self.pool, self.pool,
) )
}; };
self.service.call(ServiceRequest::from_parts(req, payload)) self.service.call(ServiceRequest::new(req))
} }
} }

View File

@ -1,7 +1,7 @@
use std::convert::Infallible; use std::convert::Infallible;
use std::marker::PhantomData; use std::marker::PhantomData;
use actix_http::{Error, Payload, Response}; use actix_http::{Error, Response};
use actix_service::{NewService, Service}; use actix_service::{NewService, Service};
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{try_ready, Async, Future, IntoFuture, Poll}; use futures::{try_ready, Async, Future, IntoFuture, Poll};
@ -331,15 +331,15 @@ where
ExtractResponse { ExtractResponse {
fut, fut,
req,
fut_s: None, fut_s: None,
req: Some((req, payload)),
service: self.service.clone(), service: self.service.clone(),
} }
} }
} }
pub struct ExtractResponse<T: FromRequest, S: Service> { pub struct ExtractResponse<T: FromRequest, S: Service> {
req: Option<(HttpRequest, Payload)>, req: HttpRequest,
service: S, service: S,
fut: <T::Future as IntoFuture>::Future, fut: <T::Future as IntoFuture>::Future,
fut_s: Option<S::Future>, fut_s: Option<S::Future>,
@ -362,12 +362,11 @@ where
} }
let item = try_ready!(self.fut.poll().map_err(|e| { let item = try_ready!(self.fut.poll().map_err(|e| {
let (req, payload) = self.req.take().unwrap(); let req = ServiceRequest::new(self.req.clone());
let req = ServiceRequest::from_parts(req, payload);
(e.into(), req) (e.into(), req)
})); }));
self.fut_s = Some(self.service.call((item, self.req.take().unwrap().0))); self.fut_s = Some(self.service.call((item, self.req.clone())));
self.poll() self.poll()
} }
} }

View File

@ -20,6 +20,7 @@ pub struct HttpRequest(pub(crate) Rc<HttpRequestInner>);
pub(crate) struct HttpRequestInner { pub(crate) struct HttpRequestInner {
pub(crate) head: Message<RequestHead>, pub(crate) head: Message<RequestHead>,
pub(crate) path: Path<Url>, pub(crate) path: Path<Url>,
pub(crate) payload: Payload,
pub(crate) app_data: Rc<Extensions>, pub(crate) app_data: Rc<Extensions>,
rmap: Rc<ResourceMap>, rmap: Rc<ResourceMap>,
config: AppConfig, config: AppConfig,
@ -31,6 +32,7 @@ impl HttpRequest {
pub(crate) fn new( pub(crate) fn new(
path: Path<Url>, path: Path<Url>,
head: Message<RequestHead>, head: Message<RequestHead>,
payload: Payload,
rmap: Rc<ResourceMap>, rmap: Rc<ResourceMap>,
config: AppConfig, config: AppConfig,
app_data: Rc<Extensions>, app_data: Rc<Extensions>,
@ -39,6 +41,7 @@ impl HttpRequest {
HttpRequest(Rc::new(HttpRequestInner { HttpRequest(Rc::new(HttpRequestInner {
head, head,
path, path,
payload,
rmap, rmap,
config, config,
app_data, app_data,

View File

@ -50,45 +50,46 @@ where
} }
} }
pub struct ServiceRequest { /// An service http request
req: HttpRequest, ///
payload: Payload, /// ServiceRequest allows mutable access to request's internal structures
} pub struct ServiceRequest(HttpRequest);
impl ServiceRequest { impl ServiceRequest {
/// Construct service request from parts /// Construct service request
pub(crate) fn from_parts(req: HttpRequest, payload: Payload) -> Self { pub(crate) fn new(req: HttpRequest) -> Self {
ServiceRequest { req, payload } ServiceRequest(req)
} }
/// Deconstruct request into parts /// Deconstruct request into parts
pub fn into_parts(self) -> (HttpRequest, Payload) { pub fn into_parts(mut self) -> (HttpRequest, Payload) {
(self.req, self.payload) let pl = Rc::get_mut(&mut (self.0).0).unwrap().payload.take();
(self.0, pl)
} }
/// Create service response /// Create service response
#[inline] #[inline]
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> { pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
ServiceResponse::new(self.req, res.into()) ServiceResponse::new(self.0, res.into())
} }
/// Create service response for error /// Create service response for error
#[inline] #[inline]
pub fn error_response<B, E: Into<Error>>(self, err: E) -> ServiceResponse<B> { pub fn error_response<B, E: Into<Error>>(self, err: E) -> ServiceResponse<B> {
let res: Response = err.into().into(); let res: Response = err.into().into();
ServiceResponse::new(self.req, res.into_body()) ServiceResponse::new(self.0, res.into_body())
} }
/// This method returns reference to the request head /// This method returns reference to the request head
#[inline] #[inline]
pub fn head(&self) -> &RequestHead { pub fn head(&self) -> &RequestHead {
&self.req.head() &self.0.head()
} }
/// This method returns reference to the request head /// This method returns reference to the request head
#[inline] #[inline]
pub fn head_mut(&mut self) -> &mut RequestHead { pub fn head_mut(&mut self) -> &mut RequestHead {
self.req.head_mut() self.0.head_mut()
} }
/// Request's uri. /// Request's uri.
@ -164,24 +165,24 @@ impl ServiceRequest {
/// access the matched value for that segment. /// access the matched value for that segment.
#[inline] #[inline]
pub fn match_info(&self) -> &Path<Url> { pub fn match_info(&self) -> &Path<Url> {
self.req.match_info() self.0.match_info()
} }
#[inline] #[inline]
pub fn match_info_mut(&mut self) -> &mut Path<Url> { pub fn match_info_mut(&mut self) -> &mut Path<Url> {
self.req.match_info_mut() self.0.match_info_mut()
} }
/// Service configuration /// Service configuration
#[inline] #[inline]
pub fn app_config(&self) -> &AppConfig { pub fn app_config(&self) -> &AppConfig {
self.req.app_config() self.0.app_config()
} }
/// Get an application data stored with `App::data()` method during /// Get an application data stored with `App::data()` method during
/// application configuration. /// application configuration.
pub fn app_data<T: 'static>(&self) -> Option<Data<T>> { pub fn app_data<T: 'static>(&self) -> Option<Data<T>> {
if let Some(st) = self.req.0.app_data.get::<Data<T>>() { if let Some(st) = (self.0).0.app_data.get::<Data<T>>() {
Some(st.clone()) Some(st.clone())
} else { } else {
None None
@ -191,7 +192,7 @@ impl ServiceRequest {
#[doc(hidden)] #[doc(hidden)]
/// Set new app data container /// Set new app data container
pub fn set_data_container(&mut self, extensions: Rc<Extensions>) { pub fn set_data_container(&mut self, extensions: Rc<Extensions>) {
Rc::get_mut(&mut self.req.0).unwrap().app_data = extensions; Rc::get_mut(&mut (self.0).0).unwrap().app_data = extensions;
} }
} }
@ -213,18 +214,18 @@ impl HttpMessage for ServiceRequest {
/// Request extensions /// Request extensions
#[inline] #[inline]
fn extensions(&self) -> Ref<Extensions> { fn extensions(&self) -> Ref<Extensions> {
self.req.extensions() self.0.extensions()
} }
/// Mutable reference to a the request's extensions /// Mutable reference to a the request's extensions
#[inline] #[inline]
fn extensions_mut(&self) -> RefMut<Extensions> { fn extensions_mut(&self) -> RefMut<Extensions> {
self.req.extensions_mut() self.0.extensions_mut()
} }
#[inline] #[inline]
fn take_payload(&mut self) -> Payload<Self::Stream> { fn take_payload(&mut self) -> Payload<Self::Stream> {
std::mem::replace(&mut self.payload, Payload::None) Rc::get_mut(&mut (self.0).0).unwrap().payload.take()
} }
} }

View File

@ -512,16 +512,15 @@ impl TestRequest {
let (head, payload) = self.req.finish().into_parts(); let (head, payload) = self.req.finish().into_parts();
self.path.get_mut().update(&head.uri); self.path.get_mut().update(&head.uri);
let req = HttpRequest::new( ServiceRequest::new(HttpRequest::new(
self.path, self.path,
head, head,
payload,
Rc::new(self.rmap), Rc::new(self.rmap),
AppConfig::new(self.config), AppConfig::new(self.config),
Rc::new(self.app_data), Rc::new(self.app_data),
HttpRequestPool::create(), HttpRequestPool::create(),
); ))
ServiceRequest::from_parts(req, payload)
} }
/// Complete request creation and generate `ServiceResponse` instance /// Complete request creation and generate `ServiceResponse` instance
@ -531,12 +530,13 @@ impl TestRequest {
/// Complete request creation and generate `HttpRequest` instance /// Complete request creation and generate `HttpRequest` instance
pub fn to_http_request(mut self) -> HttpRequest { pub fn to_http_request(mut self) -> HttpRequest {
let (head, _) = self.req.finish().into_parts(); let (head, payload) = self.req.finish().into_parts();
self.path.get_mut().update(&head.uri); self.path.get_mut().update(&head.uri);
HttpRequest::new( HttpRequest::new(
self.path, self.path,
head, head,
payload,
Rc::new(self.rmap), Rc::new(self.rmap),
AppConfig::new(self.config), AppConfig::new(self.config),
Rc::new(self.app_data), Rc::new(self.app_data),
@ -552,6 +552,7 @@ impl TestRequest {
let req = HttpRequest::new( let req = HttpRequest::new(
self.path, self.path,
head, head,
Payload::None,
Rc::new(self.rmap), Rc::new(self.rmap),
AppConfig::new(self.config), AppConfig::new(self.config),
Rc::new(self.app_data), Rc::new(self.app_data),