diff --git a/CHANGES.md b/CHANGES.md index b7e0d7423..3c619eee4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,8 @@ ### Changed +* `FromRequest` trait refactoring + * Move multipart support to actix-multipart crate diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index e2fa06e12..6820d3622 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -10,8 +10,8 @@ use std::{cmp, io}; use actix_service::boxed::{self, BoxedNewService, BoxedService}; use actix_service::{IntoNewService, NewService, Service}; use actix_web::dev::{ - HttpServiceFactory, Payload, ResourceDef, ServiceConfig, ServiceFromRequest, - ServiceRequest, ServiceResponse, + HttpServiceFactory, Payload, ResourceDef, ServiceConfig, ServiceRequest, + ServiceResponse, }; use actix_web::error::{BlockingError, Error, ErrorInternalServerError}; use actix_web::http::header::DispositionType; @@ -551,8 +551,8 @@ impl
FromRequest
for PathBufWrp {
type Error = UriSegmentError;
type Future = Result ) -> Self::Future {
- PathBufWrp::get_pathbuf(req.request().match_info().path())
+ fn from_request(req: &HttpRequest, _: &mut Payload ) -> Self::Future {
+ PathBufWrp::get_pathbuf(req.match_info().path())
}
}
diff --git a/actix-multipart/src/extractor.rs b/actix-multipart/src/extractor.rs
index 18c26c6fb..94eb4c305 100644
--- a/actix-multipart/src/extractor.rs
+++ b/actix-multipart/src/extractor.rs
@@ -2,10 +2,8 @@
use bytes::Bytes;
use futures::Stream;
-use actix_web::dev::ServiceFromRequest;
use actix_web::error::{Error, PayloadError};
-use actix_web::FromRequest;
-use actix_web::HttpMessage;
+use actix_web::{dev::Payload, FromRequest, HttpRequest};
use crate::server::Multipart;
@@ -50,8 +48,7 @@ where
type Future = Result ) -> Self::Future {
- let pl = req.take_payload();
- Ok(Multipart::new(req.headers(), pl))
+ fn from_request(req: &HttpRequest, payload: &mut Payload ) -> Self::Future {
+ Ok(Multipart::new(req.headers(), payload.take()))
}
}
diff --git a/actix-session/src/lib.rs b/actix-session/src/lib.rs
index 0cd1b9ed8..4b7ae2fde 100644
--- a/actix-session/src/lib.rs
+++ b/actix-session/src/lib.rs
@@ -45,8 +45,8 @@
use std::cell::RefCell;
use std::rc::Rc;
-use actix_web::dev::{ServiceFromRequest, ServiceRequest, ServiceResponse};
-use actix_web::{Error, FromRequest, HttpMessage};
+use actix_web::dev::{Extensions, Payload, ServiceRequest, ServiceResponse};
+use actix_web::{Error, FromRequest, HttpMessage, HttpRequest};
use hashbrown::HashMap;
use serde::de::DeserializeOwned;
use serde::Serialize;
@@ -123,7 +123,7 @@ impl Session {
data: impl Iterator ,
) {
- let session = Session::get_session(req);
+ let session = Session::get_session(&mut *req.extensions_mut());
let mut inner = session.0.borrow_mut();
inner.state.extend(data);
}
@@ -144,12 +144,12 @@ impl Session {
}
}
- fn get_session FromRequest for Session {
type Future = Result ) -> Self::Future {
- Ok(Session::get_session(req))
+ fn from_request(req: &HttpRequest, _: &mut Payload ) -> Self::Future {
+ Ok(Session::get_session(&mut *req.extensions_mut()))
}
}
@@ -196,7 +196,7 @@ mod tests {
vec![("key".to_string(), "\"value\"".to_string())].into_iter(),
&mut req,
);
- let session = Session::get_session(&mut req);
+ let session = Session::get_session(&mut *req.extensions_mut());
let res = session.get:: for Data ) -> Self::Future {
- if let Some(st) = req.request().config().extensions().get::>() {
+ fn from_request(req: &HttpRequest, _: &mut Payload ) -> Self::Future {
+ if let Some(st) = req.app_config().extensions().get::>() {
Ok(st.clone())
} else {
Err(ErrorInternalServerError(
@@ -230,7 +231,7 @@ impl for RouteData ) -> Self::Future {
+ fn from_request(req: &HttpRequest, _: &mut Payload ) -> Self::Future {
if let Some(st) = req.route_data:: : Sized {
type Future: IntoFuture ) -> Self::Future;
+ fn from_request(req: &HttpRequest, payload: &mut Payload ) -> Self::Future;
+
+ /// Convert request to a Self
+ ///
+ /// This method uses `Payload::None` as payload stream.
+ fn extract(req: &HttpRequest) -> Self::Future {
+ Self::from_request(req, &mut Payload::None)
+ }
}
/// Optionally extract a field from the request
@@ -28,7 +36,7 @@ pub trait FromRequest : Sized {
///
/// ```rust
/// # #[macro_use] extern crate serde_derive;
-/// use actix_web::{web, dev, App, Error, FromRequest};
+/// use actix_web::{web, dev, App, Error, HttpRequest, FromRequest};
/// use actix_web::error::ErrorBadRequest;
/// use rand;
///
@@ -41,7 +49,7 @@ pub trait FromRequest : Sized {
/// type Error = Error;
/// type Future = Result ) -> Self::Future {
+/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload ) -> Self::Future {
/// if rand::random() {
/// Ok(Thing { name: "thingy".into() })
/// } else {
@@ -76,14 +84,18 @@ where
type Future = Box ) -> Self::Future {
- Box::new(T::from_request(req).into_future().then(|r| match r {
- Ok(v) => future::ok(Some(v)),
- Err(e) => {
- log::debug!("Error for Option ) -> Self::Future {
+ Box::new(
+ T::from_request(req, payload)
+ .into_future()
+ .then(|r| match r {
+ Ok(v) => future::ok(Some(v)),
+ Err(e) => {
+ log::debug!("Error for Option ) -> Self::Future {
+/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload ) -> Self::Future {
/// if rand::random() {
/// Ok(Thing { name: "thingy".into() })
/// } else {
@@ -141,11 +153,15 @@ where
type Future = Box ) -> Self::Future {
- Box::new(T::from_request(req).into_future().then(|res| match res {
- Ok(v) => ok(Ok(v)),
- Err(e) => ok(Err(e)),
- }))
+ fn from_request(req: &HttpRequest, payload: &mut Payload ) -> Self::Future {
+ Box::new(
+ T::from_request(req, payload)
+ .into_future()
+ .then(|res| match res {
+ Ok(v) => ok(Ok(v)),
+ Err(e) => ok(Err(e)),
+ }),
+ )
}
}
@@ -154,7 +170,7 @@ impl FromRequest for () {
type Error = Error;
type Future = Result<(), Error>;
- fn from_request(_req: &mut ServiceFromRequest ) -> Self::Future {
+ fn from_request(_: &HttpRequest, _: &mut Payload ) -> Self::Future {
Ok(())
}
}
@@ -168,10 +184,10 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
type Error = Error;
type Future = $fut_type ;
- fn from_request(req: &mut ServiceFromRequest ) -> Self::Future {
+ fn from_request(req: &HttpRequest, payload: &mut Payload ) -> Self::Future {
$fut_type {
items: <($(Option<$T>,)+)>::default(),
- futs: ($($T::from_request(req).into_future(),)+),
+ futs: ($($T::from_request(req, payload).into_future(),)+),
}
}
}
@@ -247,25 +263,25 @@ mod tests {
#[test]
fn test_option() {
- let mut req = TestRequest::with_header(
+ let (req, mut pl) = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.route_data(FormConfig::default().limit(4096))
- .to_from();
+ .to_http_parts();
- let r = block_on(Option::