1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

rename App::register_data to App::app_data and HttpRequest::app_data returns Option<&T> instead of Option<&Data<T>>

This commit is contained in:
Nikolay Kim 2019-12-20 17:13:09 +06:00
parent 20248daeda
commit c877840c07
15 changed files with 114 additions and 84 deletions

View File

@ -8,6 +8,10 @@
* Allow to set `peer_addr` for TestRequest #1074 * Allow to set `peer_addr` for TestRequest #1074
* Rename `App::register_data()` to `App::app_data()`
* `HttpRequest::app_data<T>()` returns `Option<&T>` instead of `Option<&Data<T>>`
### Fixed ### Fixed
* Fix `AppConfig::secure()` is always false. #1202 * Fix `AppConfig::secure()` is always false. #1202

View File

@ -1,5 +1,10 @@
## 2.0.0 ## 2.0.0
* `App::register_data()` renamed to `App::app_data()` and accepts any type `T: 'static`.
Stored data is available via `HttpRequest::app_data()` method at runtime.
* Extractor configuration must be registered with `App::app_data()` instead of `App::data()`
* Sync handlers has been removed. `.to_async()` method has been renamed to `.to()` * Sync handlers has been removed. `.to_async()` method has been renamed to `.to()`
replace `fn` with `async fn` to convert sync handler to async replace `fn` with `async fn` to convert sync handler to async

View File

@ -5,6 +5,7 @@ use std::marker::PhantomData;
use std::rc::Rc; use std::rc::Rc;
use actix_http::body::{Body, MessageBody}; use actix_http::body::{Body, MessageBody};
use actix_http::Extensions;
use actix_service::boxed::{self, BoxServiceFactory}; use actix_service::boxed::{self, BoxServiceFactory};
use actix_service::{ use actix_service::{
apply, apply_fn_factory, IntoServiceFactory, ServiceFactory, Transform, apply, apply_fn_factory, IntoServiceFactory, ServiceFactory, Transform,
@ -37,6 +38,7 @@ pub struct App<T, B> {
data: Vec<Box<dyn DataFactory>>, data: Vec<Box<dyn DataFactory>>,
data_factories: Vec<FnDataFactory>, data_factories: Vec<FnDataFactory>,
external: Vec<ResourceDef>, external: Vec<ResourceDef>,
extensions: Extensions,
_t: PhantomData<B>, _t: PhantomData<B>,
} }
@ -52,6 +54,7 @@ impl App<AppEntry, Body> {
default: None, default: None,
factory_ref: fref, factory_ref: fref,
external: Vec::new(), external: Vec::new(),
extensions: Extensions::new(),
_t: PhantomData, _t: PhantomData,
} }
} }
@ -133,10 +136,15 @@ where
self self
} }
/// Set application data. Application data could be accessed /// Set application level arbitrary data item.
/// by using `Data<T>` extractor where `T` is data type. ///
pub fn register_data<U: 'static>(mut self, data: Data<U>) -> Self { /// Application data stored with `App::app_data()` method is available
self.data.push(Box::new(data)); /// via `HttpRequest::app_data()` method at runtime.
///
/// This method could be used for storing `Data<T>` as well, in that case
/// data could be accessed by using `Data<T>` extractor.
pub fn app_data<U: 'static>(mut self, ext: U) -> Self {
self.extensions.insert(ext);
self self
} }
@ -370,6 +378,7 @@ where
default: self.default, default: self.default,
factory_ref: self.factory_ref, factory_ref: self.factory_ref,
external: self.external, external: self.external,
extensions: self.extensions,
_t: PhantomData, _t: PhantomData,
} }
} }
@ -431,6 +440,7 @@ where
default: self.default, default: self.default,
factory_ref: self.factory_ref, factory_ref: self.factory_ref,
external: self.external, external: self.external,
extensions: self.extensions,
_t: PhantomData, _t: PhantomData,
} }
} }
@ -456,6 +466,7 @@ where
external: RefCell::new(self.external), external: RefCell::new(self.external),
default: self.default, default: self.default,
factory_ref: self.factory_ref, factory_ref: self.factory_ref,
extensions: RefCell::new(Some(self.extensions)),
} }
} }
} }
@ -539,6 +550,20 @@ mod tests {
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
} }
#[actix_rt::test]
async fn test_extension() {
let mut srv = init_service(App::new().app_data(10usize).service(
web::resource("/").to(|req: HttpRequest| {
assert_eq!(*req.app_data::<usize>().unwrap(), 10);
HttpResponse::Ok()
}),
))
.await;
let req = TestRequest::default().to_request();
let resp = srv.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[actix_rt::test] #[actix_rt::test]
async fn test_wrap() { async fn test_wrap() {
let mut srv = init_service( let mut srv = init_service(

View File

@ -39,6 +39,7 @@ where
>, >,
{ {
pub(crate) endpoint: T, pub(crate) endpoint: T,
pub(crate) extensions: RefCell<Option<Extensions>>,
pub(crate) data: Rc<Vec<Box<dyn DataFactory>>>, pub(crate) data: Rc<Vec<Box<dyn DataFactory>>>,
pub(crate) data_factories: Rc<Vec<FnDataFactory>>, pub(crate) data_factories: Rc<Vec<FnDataFactory>>,
pub(crate) services: Rc<RefCell<Vec<Box<dyn AppServiceFactory>>>>, pub(crate) services: Rc<RefCell<Vec<Box<dyn AppServiceFactory>>>>,
@ -114,6 +115,12 @@ where
data: self.data.clone(), data: self.data.clone(),
data_factories: Vec::new(), data_factories: Vec::new(),
data_factories_fut: self.data_factories.iter().map(|f| f()).collect(), data_factories_fut: self.data_factories.iter().map(|f| f()).collect(),
extensions: Some(
self.extensions
.borrow_mut()
.take()
.unwrap_or_else(Extensions::new),
),
config, config,
rmap, rmap,
_t: PhantomData, _t: PhantomData,
@ -134,6 +141,7 @@ where
data: Rc<Vec<Box<dyn DataFactory>>>, data: Rc<Vec<Box<dyn DataFactory>>>,
data_factories: Vec<Box<dyn DataFactory>>, data_factories: Vec<Box<dyn DataFactory>>,
data_factories_fut: Vec<LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>, data_factories_fut: Vec<LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>,
extensions: Option<Extensions>,
_t: PhantomData<B>, _t: PhantomData<B>,
} }
@ -172,7 +180,7 @@ where
if this.endpoint.is_some() && this.data_factories_fut.is_empty() { if this.endpoint.is_some() && this.data_factories_fut.is_empty() {
// create app data container // create app data container
let mut data = Extensions::new(); let mut data = this.extensions.take().unwrap();
for f in this.data.iter() { for f in this.data.iter() {
f.create(&mut data); f.create(&mut data);
} }

View File

@ -56,7 +56,7 @@ pub(crate) trait DataFactory {
/// ///
/// let app = App::new() /// let app = App::new()
/// // Store `MyData` in application storage. /// // Store `MyData` in application storage.
/// .register_data(data.clone()) /// .app_data(data.clone())
/// .service( /// .service(
/// web::resource("/index.html").route( /// web::resource("/index.html").route(
/// web::get().to(index))); /// web::get().to(index)));
@ -107,8 +107,8 @@ impl<T: 'static> FromRequest for Data<T> {
#[inline] #[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
if let Some(st) = req.get_app_data::<T>() { if let Some(st) = req.app_data::<Data<T>>() {
ok(st) ok(st.clone())
} else { } else {
log::debug!( log::debug!(
"Failed to construct App-level Data extractor. \ "Failed to construct App-level Data extractor. \
@ -165,9 +165,9 @@ mod tests {
} }
#[actix_rt::test] #[actix_rt::test]
async fn test_register_data_extractor() { async fn test_app_data_extractor() {
let mut srv = let mut srv =
init_service(App::new().register_data(Data::new(10usize)).service( init_service(App::new().app_data(Data::new(10usize)).service(
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()), web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
)) ))
.await; .await;
@ -177,7 +177,7 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
let mut srv = let mut srv =
init_service(App::new().register_data(Data::new(10u32)).service( init_service(App::new().app_data(Data::new(10u32)).service(
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()), web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
)) ))
.await; .await;

View File

@ -8,7 +8,6 @@ use actix_router::{Path, Url};
use futures::future::{ok, Ready}; use futures::future::{ok, Ready};
use crate::config::AppConfig; use crate::config::AppConfig;
use crate::data::Data;
use crate::error::UrlGenerationError; use crate::error::UrlGenerationError;
use crate::extract::FromRequest; use crate::extract::FromRequest;
use crate::info::ConnectionInfo; use crate::info::ConnectionInfo;
@ -207,25 +206,15 @@ impl HttpRequest {
&self.0.config &self.0.config
} }
/// Get an application data stored with `App::data()` method during /// Get an application data stored with `App::extension()` method during
/// application configuration. /// application configuration.
pub fn app_data<T: 'static>(&self) -> Option<&T> { pub fn app_data<T: 'static>(&self) -> Option<&T> {
if let Some(st) = self.0.app_data.get::<Data<T>>() { if let Some(st) = self.0.app_data.get::<T>() {
Some(&st) Some(&st)
} else { } else {
None None
} }
} }
/// Get an application data stored with `App::data()` method during
/// application configuration.
pub fn get_app_data<T: 'static>(&self) -> Option<Data<T>> {
if let Some(st) = self.0.app_data.get::<Data<T>>() {
Some(st.clone())
} else {
None
}
}
} }
impl HttpMessage for HttpRequest { impl HttpMessage for HttpRequest {
@ -467,8 +456,8 @@ mod tests {
} }
#[actix_rt::test] #[actix_rt::test]
async fn test_app_data() { async fn test_data() {
let mut srv = init_service(App::new().data(10usize).service( let mut srv = init_service(App::new().app_data(10usize).service(
web::resource("/").to(|req: HttpRequest| { web::resource("/").to(|req: HttpRequest| {
if req.app_data::<usize>().is_some() { if req.app_data::<usize>().is_some() {
HttpResponse::Ok() HttpResponse::Ok()
@ -483,7 +472,7 @@ mod tests {
let resp = call_service(&mut srv, req).await; let resp = call_service(&mut srv, req).await;
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
let mut srv = init_service(App::new().data(10u32).service( let mut srv = init_service(App::new().app_data(10u32).service(
web::resource("/").to(|req: HttpRequest| { web::resource("/").to(|req: HttpRequest| {
if req.app_data::<usize>().is_some() { if req.app_data::<usize>().is_some() {
HttpResponse::Ok() HttpResponse::Ok()

View File

@ -192,16 +192,13 @@ where
/// } /// }
/// ``` /// ```
pub fn data<U: 'static>(self, data: U) -> Self { pub fn data<U: 'static>(self, data: U) -> Self {
self.register_data(Data::new(data)) self.app_data(Data::new(data))
} }
/// Set or override application data. /// Set or override application data.
/// ///
/// This method has the same effect as [`Resource::data`](#method.data), /// This method overrides data stored with [`App::app_data()`](#method.app_data)
/// except that instead of taking a value of some type `T`, it expects a pub fn app_data<U: 'static>(mut self, data: U) -> Self {
/// value of type `Data<T>`. Use a `Data<T>` extractor to retrieve its
/// value.
pub fn register_data<U: 'static>(mut self, data: Data<U>) -> Self {
if self.data.is_none() { if self.data.is_none() {
self.data = Some(Extensions::new()); self.data = Some(Extensions::new());
} }
@ -754,11 +751,11 @@ mod tests {
App::new() App::new()
.data(1.0f64) .data(1.0f64)
.data(1usize) .data(1usize)
.register_data(web::Data::new('-')) .app_data(web::Data::new('-'))
.service( .service(
web::resource("/test") web::resource("/test")
.data(10usize) .data(10usize)
.register_data(web::Data::new('*')) .app_data(web::Data::new('*'))
.guard(guard::Get()) .guard(guard::Get())
.to( .to(
|data1: web::Data<usize>, |data1: web::Data<usize>,

View File

@ -148,15 +148,13 @@ where
/// } /// }
/// ``` /// ```
pub fn data<U: 'static>(self, data: U) -> Self { pub fn data<U: 'static>(self, data: U) -> Self {
self.register_data(Data::new(data)) self.app_data(Data::new(data))
} }
/// Set or override application data. /// Set or override application data.
/// ///
/// This method has the same effect as [`Scope::data`](#method.data), except /// This method overrides data stored with [`App::app_data()`](#method.app_data)
/// that instead of taking a value of some type `T`, it expects a value of pub fn app_data<U: 'static>(mut self, data: U) -> Self {
/// type `Data<T>`. Use a `Data<T>` extractor to retrieve its value.
pub fn register_data<U: 'static>(mut self, data: Data<U>) -> Self {
if self.data.is_none() { if self.data.is_none() {
self.data = Some(Extensions::new()); self.data = Some(Extensions::new());
} }
@ -1122,12 +1120,9 @@ mod tests {
} }
#[actix_rt::test] #[actix_rt::test]
async fn test_override_register_data() { async fn test_override_app_data() {
let mut srv = init_service( let mut srv = init_service(App::new().app_data(web::Data::new(1usize)).service(
App::new().register_data(web::Data::new(1usize)).service( web::scope("app").app_data(web::Data::new(10usize)).route(
web::scope("app")
.register_data(web::Data::new(10usize))
.route(
"/t", "/t",
web::get().to(|data: web::Data<usize>| { web::get().to(|data: web::Data<usize>| {
assert_eq!(*data, 10); assert_eq!(*data, 10);
@ -1135,8 +1130,7 @@ mod tests {
HttpResponse::Ok() HttpResponse::Ok()
}), }),
), ),
), ))
)
.await; .await;
let req = TestRequest::with_uri("/app/t").to_request(); let req = TestRequest::with_uri("/app/t").to_request();

View File

@ -451,6 +451,13 @@ impl TestRequest {
self self
} }
/// Set application data. This is equivalent of `App::app_data()` method
/// for testing purpose.
pub fn app_data<T: 'static>(mut self, data: T) -> Self {
self.app_data.insert(data);
self
}
#[cfg(test)] #[cfg(test)]
/// Set request config /// Set request config
pub(crate) fn rmap(mut self, rmap: ResourceMap) -> Self { pub(crate) fn rmap(mut self, rmap: ResourceMap) -> Self {
@ -964,6 +971,7 @@ mod tests {
.set(header::Date(SystemTime::now().into())) .set(header::Date(SystemTime::now().into()))
.param("test", "123") .param("test", "123")
.data(10u32) .data(10u32)
.app_data(20u64)
.peer_addr("127.0.0.1:8081".parse().unwrap()) .peer_addr("127.0.0.1:8081".parse().unwrap())
.to_http_request(); .to_http_request();
assert!(req.headers().contains_key(header::CONTENT_TYPE)); assert!(req.headers().contains_key(header::CONTENT_TYPE));
@ -974,14 +982,13 @@ mod tests {
); );
assert_eq!(&req.match_info()["test"], "123"); assert_eq!(&req.match_info()["test"], "123");
assert_eq!(req.version(), Version::HTTP_2); assert_eq!(req.version(), Version::HTTP_2);
let data = req.get_app_data::<u32>().unwrap(); let data = req.app_data::<Data<u32>>().unwrap();
assert!(req.get_app_data::<u64>().is_none()); assert!(req.app_data::<Data<u64>>().is_none());
assert_eq!(*data, 10);
assert_eq!(*data.get_ref(), 10); assert_eq!(*data.get_ref(), 10);
assert!(req.app_data::<u64>().is_none()); assert!(req.app_data::<u32>().is_none());
let data = req.app_data::<u32>().unwrap(); let data = req.app_data::<u64>().unwrap();
assert_eq!(*data, 10); assert_eq!(*data, 20);
} }
#[actix_rt::test] #[actix_rt::test]

View File

@ -190,7 +190,7 @@ impl<T: Serialize> Responder for Form<T> {
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html") /// web::resource("/index.html")
/// // change `Form` extractor configuration /// // change `Form` extractor configuration
/// .data( /// .app_data(
/// web::Form::<FormData>::configure(|cfg| cfg.limit(4097)) /// web::Form::<FormData>::configure(|cfg| cfg.limit(4097))
/// ) /// )
/// .route(web::get().to(index)) /// .route(web::get().to(index))

View File

@ -224,7 +224,8 @@ where
/// ///
/// fn main() { /// fn main() {
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html").data( /// web::resource("/index.html")
/// .app_data(
/// // change json extractor configuration /// // change json extractor configuration
/// web::Json::<Info>::configure(|cfg| { /// web::Json::<Info>::configure(|cfg| {
/// cfg.limit(4096) /// cfg.limit(4096)
@ -462,7 +463,7 @@ mod tests {
header::HeaderValue::from_static("16"), header::HeaderValue::from_static("16"),
) )
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}")) .set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
.data(JsonConfig::default().limit(10).error_handler(|err, _| { .app_data(JsonConfig::default().limit(10).error_handler(|err, _| {
let msg = MyObject { let msg = MyObject {
name: "invalid request".to_string(), name: "invalid request".to_string(),
}; };
@ -514,7 +515,7 @@ mod tests {
header::HeaderValue::from_static("16"), header::HeaderValue::from_static("16"),
) )
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}")) .set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
.data(JsonConfig::default().limit(10)) .app_data(JsonConfig::default().limit(10))
.to_http_parts(); .to_http_parts();
let s = Json::<MyObject>::from_request(&req, &mut pl).await; let s = Json::<MyObject>::from_request(&req, &mut pl).await;
@ -531,7 +532,7 @@ mod tests {
header::HeaderValue::from_static("16"), header::HeaderValue::from_static("16"),
) )
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}")) .set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
.data( .app_data(
JsonConfig::default() JsonConfig::default()
.limit(10) .limit(10)
.error_handler(|_, _| JsonPayloadError::ContentType.into()), .error_handler(|_, _| JsonPayloadError::ContentType.into()),
@ -604,7 +605,7 @@ mod tests {
header::HeaderValue::from_static("16"), header::HeaderValue::from_static("16"),
) )
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}")) .set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
.data(JsonConfig::default().limit(4096)) .app_data(JsonConfig::default().limit(4096))
.to_http_parts(); .to_http_parts();
let s = Json::<MyObject>::from_request(&req, &mut pl).await; let s = Json::<MyObject>::from_request(&req, &mut pl).await;
@ -622,7 +623,7 @@ mod tests {
header::HeaderValue::from_static("16"), header::HeaderValue::from_static("16"),
) )
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}")) .set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
.data(JsonConfig::default().content_type(|mime: mime::Mime| { .app_data(JsonConfig::default().content_type(|mime: mime::Mime| {
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
})) }))
.to_http_parts(); .to_http_parts();
@ -642,7 +643,7 @@ mod tests {
header::HeaderValue::from_static("16"), header::HeaderValue::from_static("16"),
) )
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}")) .set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
.data(JsonConfig::default().content_type(|mime: mime::Mime| { .app_data(JsonConfig::default().content_type(|mime: mime::Mime| {
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
})) }))
.to_http_parts(); .to_http_parts();

View File

@ -212,7 +212,7 @@ where
/// fn main() { /// fn main() {
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/messages/{folder}") /// web::resource("/messages/{folder}")
/// .data(PathConfig::default().error_handler(|err, req| { /// .app_data(PathConfig::default().error_handler(|err, req| {
/// error::InternalError::from_response( /// error::InternalError::from_response(
/// err, /// err,
/// HttpResponse::Conflict().finish(), /// HttpResponse::Conflict().finish(),
@ -358,7 +358,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_custom_err_handler() { async fn test_custom_err_handler() {
let (req, mut pl) = TestRequest::with_uri("/name/user1/") let (req, mut pl) = TestRequest::with_uri("/name/user1/")
.data(PathConfig::default().error_handler(|err, _| { .app_data(PathConfig::default().error_handler(|err, _| {
error::InternalError::from_response( error::InternalError::from_response(
err, err,
HttpResponse::Conflict().finish(), HttpResponse::Conflict().finish(),

View File

@ -176,7 +176,7 @@ impl FromRequest for Bytes {
/// fn main() { /// fn main() {
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html") /// web::resource("/index.html")
/// .data(String::configure(|cfg| { // <- limit size of the payload /// .app_data(String::configure(|cfg| { // <- limit size of the payload
/// cfg.limit(4096) /// cfg.limit(4096)
/// })) /// }))
/// .route(web::get().to(index)) // <- register handler with extractor params /// .route(web::get().to(index)) // <- register handler with extractor params

View File

@ -185,7 +185,7 @@ where
/// ///
/// fn main() { /// fn main() {
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html").data( /// web::resource("/index.html").app_data(
/// // change query extractor configuration /// // change query extractor configuration
/// web::Query::<Info>::configure(|cfg| { /// web::Query::<Info>::configure(|cfg| {
/// cfg.error_handler(|err, req| { // <- create custom error response /// cfg.error_handler(|err, req| { // <- create custom error response
@ -273,7 +273,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_custom_error_responder() { async fn test_custom_error_responder() {
let req = TestRequest::with_uri("/name/user1/") let req = TestRequest::with_uri("/name/user1/")
.data(QueryConfig::default().error_handler(|e, _| { .app_data(QueryConfig::default().error_handler(|e, _| {
let resp = HttpResponse::UnprocessableEntity().finish(); let resp = HttpResponse::UnprocessableEntity().finish();
InternalError::from_response(e, resp).into() InternalError::from_response(e, resp).into()
})) }))

View File

@ -675,7 +675,7 @@ async fn test_brotli_encoding_large() {
let srv = test::start_with(test::config().h1(), || { let srv = test::start_with(test::config().h1(), || {
App::new().service( App::new().service(
web::resource("/") web::resource("/")
.data(web::PayloadConfig::new(320_000)) .app_data(web::PayloadConfig::new(320_000))
.route(web::to(move |body: Bytes| { .route(web::to(move |body: Bytes| {
HttpResponse::Ok().streaming(TestBody::new(body, 10240)) HttpResponse::Ok().streaming(TestBody::new(body, 10240))
})), })),