From ee33f52736b355724718b8d123063d248fe20cd0 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 13 Apr 2019 16:35:25 -0700 Subject: [PATCH] make extractor config type explicit --- CHANGES.md | 2 ++ actix-files/src/lib.rs | 1 + actix-multipart/src/extractor.rs | 1 + actix-session/CHANGES.md | 4 ++++ actix-session/src/lib.rs | 1 + src/data.rs | 2 ++ src/extract.rs | 17 +++++++++++++++++ src/middleware/identity.rs | 1 + src/request.rs | 1 + src/route.rs | 8 +++++--- src/types/form.rs | 7 +++++-- src/types/json.rs | 15 +++++++++------ src/types/path.rs | 1 + src/types/payload.rs | 13 ++++++++++--- src/types/query.rs | 1 + tests/test_server.rs | 4 +--- 16 files changed, 62 insertions(+), 17 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f7693e66e..45ff6b387 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,8 @@ * Removed `Decompress` middleware. Bytes, String, Json, Form extractors automatically decompress payload. +* Make extractor config type explicit. Add `FromRequest::Config` associated type. + ## [1.0.0-alpha.5] - 2019-04-12 diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index fd7ac3f64..89eead562 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -546,6 +546,7 @@ impl PathBufWrp { impl FromRequest for PathBufWrp { type Error = UriSegmentError; type Future = Result; + type Config = (); 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 1f2f15c63..7274ed092 100644 --- a/actix-multipart/src/extractor.rs +++ b/actix-multipart/src/extractor.rs @@ -33,6 +33,7 @@ use crate::server::Multipart; impl FromRequest for Multipart { type Error = Error; type Future = Result; + type Config = (); #[inline] fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { diff --git a/actix-session/CHANGES.md b/actix-session/CHANGES.md index 305fa561e..ce2c2d637 100644 --- a/actix-session/CHANGES.md +++ b/actix-session/CHANGES.md @@ -1,5 +1,9 @@ # Changes +## [0.1.0-alpha.6] - 2019-04-xx + +* Update actix-web + ## [0.1.0-alpha.4] - 2019-04-08 * Update actix-web diff --git a/actix-session/src/lib.rs b/actix-session/src/lib.rs index 8db875238..b82029647 100644 --- a/actix-session/src/lib.rs +++ b/actix-session/src/lib.rs @@ -175,6 +175,7 @@ impl Session { impl FromRequest for Session { type Error = Error; type Future = Result; + type Config = (); #[inline] fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { diff --git a/src/data.rs b/src/data.rs index d178d779a..e0eb8fa9d 100644 --- a/src/data.rs +++ b/src/data.rs @@ -89,6 +89,7 @@ impl Clone for Data { } impl FromRequest for Data { + type Config = (); type Error = Error; type Future = Result; @@ -233,6 +234,7 @@ impl Clone for RouteData { } impl FromRequest for RouteData { + type Config = (); type Error = Error; type Future = Result; diff --git a/src/extract.rs b/src/extract.rs index 3f20f3e3f..9023ea49a 100644 --- a/src/extract.rs +++ b/src/extract.rs @@ -17,6 +17,9 @@ pub trait FromRequest: Sized { /// Future that resolves to a Self type Future: IntoFuture; + /// Configuration for this extractor + type Config: Default + 'static; + /// Convert request to a Self fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future; @@ -26,6 +29,14 @@ pub trait FromRequest: Sized { fn extract(req: &HttpRequest) -> Self::Future { Self::from_request(req, &mut Payload::None) } + + /// Create and configure config instance. + fn configure(f: F) -> Self::Config + where + F: FnOnce(Self::Config) -> Self::Config, + { + f(Self::Config::default()) + } } /// Optionally extract a field from the request @@ -48,6 +59,7 @@ pub trait FromRequest: Sized { /// impl FromRequest for Thing { /// type Error = Error; /// type Future = Result; +/// type Config = (); /// /// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { /// if rand::random() { @@ -80,6 +92,7 @@ where T: FromRequest, T::Future: 'static, { + type Config = T::Config; type Error = Error; type Future = Box, Error = Error>>; @@ -119,6 +132,7 @@ where /// impl FromRequest for Thing { /// type Error = Error; /// type Future = Result; +/// type Config = (); /// /// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { /// if rand::random() { @@ -149,6 +163,7 @@ where T::Future: 'static, T::Error: 'static, { + type Config = T::Config; type Error = Error; type Future = Box, Error = Error>>; @@ -167,6 +182,7 @@ where #[doc(hidden)] impl FromRequest for () { + type Config = (); type Error = Error; type Future = Result<(), Error>; @@ -183,6 +199,7 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => { { type Error = Error; type Future = $fut_type<$($T),+>; + type Config = ($($T::Config),+); fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { $fut_type { diff --git a/src/middleware/identity.rs b/src/middleware/identity.rs index 8dd2ddb80..5bc3f923c 100644 --- a/src/middleware/identity.rs +++ b/src/middleware/identity.rs @@ -141,6 +141,7 @@ struct IdentityItem { /// # fn main() {} /// ``` impl FromRequest for Identity { + type Config = (); type Error = Error; type Future = Result; diff --git a/src/request.rs b/src/request.rs index 93ac954f2..082d36b61 100644 --- a/src/request.rs +++ b/src/request.rs @@ -266,6 +266,7 @@ impl Drop for HttpRequest { /// } /// ``` impl FromRequest for HttpRequest { + type Config = (); type Error = Error; type Future = Result; diff --git a/src/route.rs b/src/route.rs index eb911b307..b1c7b2ab4 100644 --- a/src/route.rs +++ b/src/route.rs @@ -292,7 +292,7 @@ impl Route { /// configuration or specific state available via `RouteData` extractor. /// /// ```rust - /// use actix_web::{web, App}; + /// use actix_web::{web, App, FromRequest}; /// /// /// extract text data from request /// fn index(body: String) -> String { @@ -304,13 +304,15 @@ impl Route { /// web::resource("/index.html").route( /// web::get() /// // limit size of the payload - /// .data(web::PayloadConfig::new(4096)) + /// .data(String::configure(|cfg| { + /// cfg.limit(4096) + /// })) /// // register handler /// .to(index) /// )); /// } /// ``` - pub fn data(mut self, data: C) -> Self { + pub fn data(mut self, data: T) -> Self { if self.data.is_none() { self.data = Some(Extensions::new()); } diff --git a/src/types/form.rs b/src/types/form.rs index c2e8c63bc..249f33b3a 100644 --- a/src/types/form.rs +++ b/src/types/form.rs @@ -73,6 +73,7 @@ impl FromRequest for Form where T: DeserializeOwned + 'static, { + type Config = FormConfig; type Error = Error; type Future = Box>; @@ -115,7 +116,7 @@ impl fmt::Display for Form { /// /// ```rust /// #[macro_use] extern crate serde_derive; -/// use actix_web::{web, App, Result}; +/// use actix_web::{web, App, FromRequest, Result}; /// /// #[derive(Deserialize)] /// struct FormData { @@ -133,7 +134,9 @@ impl fmt::Display for Form { /// web::resource("/index.html") /// .route(web::get() /// // change `Form` extractor configuration -/// .data(web::FormConfig::default().limit(4097)) +/// .data( +/// web::Form::::configure(|cfg| cfg.limit(4097)) +/// ) /// .to(index)) /// ); /// } diff --git a/src/types/json.rs b/src/types/json.rs index d59136225..3543975ae 100644 --- a/src/types/json.rs +++ b/src/types/json.rs @@ -168,6 +168,7 @@ impl FromRequest for Json where T: DeserializeOwned + 'static, { + type Config = JsonConfig; type Error = Error; type Future = Box>; @@ -205,7 +206,7 @@ where /// /// ```rust /// #[macro_use] extern crate serde_derive; -/// use actix_web::{error, web, App, HttpResponse}; +/// use actix_web::{error, web, App, FromRequest, HttpResponse}; /// /// #[derive(Deserialize)] /// struct Info { @@ -222,11 +223,13 @@ where /// web::resource("/index.html").route( /// web::post().data( /// // change json extractor configuration -/// web::JsonConfig::default().limit(4096) -/// .error_handler(|err, req| { // <- create custom error response -/// error::InternalError::from_response( -/// err, HttpResponse::Conflict().finish()).into() -/// })) +/// web::Json::::configure(|cfg| { +/// cfg.limit(4096) +/// .error_handler(|err, req| { // <- create custom error response +/// error::InternalError::from_response( +/// err, HttpResponse::Conflict().finish()).into() +/// }) +/// })) /// .to(index)) /// ); /// } diff --git a/src/types/path.rs b/src/types/path.rs index 47ec1f562..13a35d5ea 100644 --- a/src/types/path.rs +++ b/src/types/path.rs @@ -156,6 +156,7 @@ impl FromRequest for Path where T: de::DeserializeOwned, { + type Config = (); type Error = Error; type Future = Result; diff --git a/src/types/payload.rs b/src/types/payload.rs index 3dac828cb..ca4b5de6b 100644 --- a/src/types/payload.rs +++ b/src/types/payload.rs @@ -86,6 +86,7 @@ impl Stream for Payload { /// } /// ``` impl FromRequest for Payload { + type Config = PayloadConfig; type Error = Error; type Future = Result; @@ -121,6 +122,7 @@ impl FromRequest for Payload { /// } /// ``` impl FromRequest for Bytes { + type Config = PayloadConfig; type Error = Error; type Future = Either>, FutureResult>; @@ -156,7 +158,7 @@ impl FromRequest for Bytes { /// ## Example /// /// ```rust -/// use actix_web::{web, App}; +/// use actix_web::{web, App, FromRequest}; /// /// /// extract text data from request /// fn index(text: String) -> String { @@ -167,12 +169,15 @@ impl FromRequest for Bytes { /// let app = App::new().service( /// web::resource("/index.html").route( /// web::get() -/// .data(web::PayloadConfig::new(4096)) // <- limit size of the payload +/// .data(String::configure(|cfg| { // <- limit size of the payload +/// cfg.limit(4096) +/// })) /// .to(index)) // <- register handler with extractor params /// ); /// } /// ``` impl FromRequest for String { + type Config = PayloadConfig; type Error = Error; type Future = Either>, FutureResult>; @@ -228,7 +233,9 @@ pub struct PayloadConfig { impl PayloadConfig { /// Create `PayloadConfig` instance and set max size of payload. pub fn new(limit: usize) -> Self { - Self::default().limit(limit) + let mut cfg = Self::default(); + cfg.limit = limit; + cfg } /// Change max size of payload. By default max size is 256Kb diff --git a/src/types/query.rs b/src/types/query.rs index 0467ddee4..596254be5 100644 --- a/src/types/query.rs +++ b/src/types/query.rs @@ -115,6 +115,7 @@ impl FromRequest for Query where T: de::DeserializeOwned, { + type Config = (); type Error = Error; type Future = Result; diff --git a/tests/test_server.rs b/tests/test_server.rs index 3ec20bced..718aa7d4f 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -16,10 +16,8 @@ use flate2::Compression; use futures::stream::once; use rand::{distributions::Alphanumeric, Rng}; -use actix_web::{http, test, web, App, HttpResponse, HttpServer}; - -#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))] use actix_web::middleware::{BodyEncoding, Compress}; +use actix_web::{http, test, web, App, HttpResponse, HttpServer}; const STR: &str = "Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \