2019-03-02 07:51:32 +01:00
|
|
|
use std::cell::RefCell;
|
2019-04-14 07:25:00 +02:00
|
|
|
use std::fmt;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::rc::Rc;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-03-03 04:19:56 +01:00
|
|
|
use actix_http::{Error, Response};
|
2019-03-02 08:59:44 +01:00
|
|
|
use actix_service::boxed::{self, BoxedNewService, BoxedService};
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_service::{
|
2019-04-01 03:19:18 +02:00
|
|
|
apply_transform, IntoNewService, IntoTransform, NewService, Service, Transform,
|
2019-03-02 07:51:32 +01:00
|
|
|
};
|
|
|
|
use futures::future::{ok, Either, FutureResult};
|
|
|
|
use futures::{Async, Future, IntoFuture, Poll};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-04-15 16:32:49 +02:00
|
|
|
use crate::dev::{insert_slash, AppService, HttpServiceFactory, ResourceDef};
|
2019-03-03 22:53:31 +01:00
|
|
|
use crate::extract::FromRequest;
|
2019-03-07 00:47:15 +01:00
|
|
|
use crate::guard::Guard;
|
2019-03-03 22:53:31 +01:00
|
|
|
use crate::handler::{AsyncFactory, Factory};
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::responder::Responder;
|
2019-03-03 04:19:56 +01:00
|
|
|
use crate::route::{CreateRouteService, Route, RouteService};
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
2018-06-25 06:58:04 +02:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
type HttpService = BoxedService<ServiceRequest, ServiceResponse, Error>;
|
|
|
|
type HttpNewService = BoxedNewService<(), ServiceRequest, ServiceResponse, Error, ()>;
|
2019-03-02 08:59:44 +01:00
|
|
|
|
2019-03-17 05:35:02 +01:00
|
|
|
/// *Resource* is an entry in resources table which corresponds to requested URL.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
2019-03-03 23:45:56 +01:00
|
|
|
/// Resource in turn has at least one route.
|
|
|
|
/// Route consists of an handlers objects and list of guards
|
|
|
|
/// (objects that implement `Guard` trait).
|
|
|
|
/// Resources and rouets uses builder-like pattern for configuration.
|
|
|
|
/// During request handling, resource object iterate through all routes
|
|
|
|
/// and check guards for specific route, if request matches all
|
|
|
|
/// guards, route considered matched and route handler get called.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, App, HttpResponse};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/")
|
|
|
|
/// .route(web::get().to(|| HttpResponse::Ok())));
|
2019-03-03 23:45:56 +01:00
|
|
|
/// }
|
2019-03-25 00:15:34 +01:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If no matching route could be found, *405* response code get returned.
|
|
|
|
/// Default behavior could be overriden with `default_resource()` method.
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct Resource<T = ResourceEndpoint> {
|
2019-03-02 07:51:32 +01:00
|
|
|
endpoint: T,
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: String,
|
2019-03-09 23:06:24 +01:00
|
|
|
name: Option<String>,
|
2019-04-13 23:50:54 +02:00
|
|
|
routes: Vec<Route>,
|
2019-03-07 00:47:15 +01:00
|
|
|
guards: Vec<Box<Guard>>,
|
2019-04-13 23:50:54 +02:00
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
|
|
|
factory_ref: Rc<RefCell<Option<ResourceFactory>>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Resource {
|
|
|
|
pub fn new(path: &str) -> Resource {
|
2019-03-02 07:51:32 +01:00
|
|
|
let fref = Rc::new(RefCell::new(None));
|
|
|
|
|
2018-07-12 11:30:01 +02:00
|
|
|
Resource {
|
2019-03-02 07:51:32 +01:00
|
|
|
routes: Vec::new(),
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: path.to_string(),
|
2019-03-09 23:06:24 +01:00
|
|
|
name: None,
|
2019-03-02 07:51:32 +01:00
|
|
|
endpoint: ResourceEndpoint::new(fref.clone()),
|
|
|
|
factory_ref: fref,
|
2019-03-07 00:47:15 +01:00
|
|
|
guards: Vec::new(),
|
2019-03-02 07:51:32 +01:00
|
|
|
default: Rc::new(RefCell::new(None)),
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T> Resource<T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-03-09 23:06:24 +01:00
|
|
|
/// Set resource name.
|
|
|
|
///
|
|
|
|
/// Name is used for url generation.
|
|
|
|
pub fn name(mut self, name: &str) -> Self {
|
|
|
|
self.name = Some(name.to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Add match guard to a resource.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, guard, App, HttpResponse};
|
2019-03-07 00:47:15 +01:00
|
|
|
///
|
2019-03-07 23:01:52 +01:00
|
|
|
/// fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .service(
|
|
|
|
/// web::resource("/app")
|
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
|
|
|
/// .route(web::get().to(index))
|
|
|
|
/// )
|
|
|
|
/// .service(
|
|
|
|
/// web::resource("/app")
|
|
|
|
/// .guard(guard::Header("content-type", "text/json"))
|
|
|
|
/// .route(web::get().to(|| HttpResponse::MethodNotAllowed()))
|
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
|
|
|
self.guards.push(Box::new(guard));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn add_guards(mut self, guards: Vec<Box<Guard>>) -> Self {
|
|
|
|
self.guards.extend(guards);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:09:38 +01:00
|
|
|
/// Register a new route.
|
2017-12-04 22:32:05 +01:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, guard, App, HttpResponse};
|
2017-12-04 22:32:05 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/").route(
|
|
|
|
/// web::route()
|
2019-03-03 21:09:38 +01:00
|
|
|
/// .guard(guard::Any(guard::Get()).or(guard::Put()))
|
|
|
|
/// .guard(guard::Header("Content-Type", "text/plain"))
|
|
|
|
/// .to(|| HttpResponse::Ok()))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2017-12-04 22:32:05 +01:00
|
|
|
/// }
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
2019-03-03 23:45:56 +01:00
|
|
|
///
|
2019-03-17 05:35:02 +01:00
|
|
|
/// Multiple routes could be added to a resource. Resource object uses
|
|
|
|
/// match guards for route selection.
|
2019-03-03 23:45:56 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, guard, App, HttpResponse};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/container/")
|
|
|
|
/// .route(web::get().to(get_handler))
|
2019-03-03 23:45:56 +01:00
|
|
|
/// .route(web::post().to(post_handler))
|
|
|
|
/// .route(web::delete().to(delete_handler))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2019-03-03 23:45:56 +01:00
|
|
|
/// }
|
|
|
|
/// # fn get_handler() {}
|
|
|
|
/// # fn post_handler() {}
|
|
|
|
/// # fn delete_handler() {}
|
|
|
|
/// ```
|
2019-04-13 23:50:54 +02:00
|
|
|
pub fn route(mut self, route: Route) -> Self {
|
2019-03-03 09:57:48 +01:00
|
|
|
self.routes.push(route.finish());
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
2017-12-06 17:03:08 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Register a new route and add handler. This route matches all requests.
|
2018-03-27 08:10:31 +02:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-06-02 00:57:24 +02:00
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
///
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/").to(index));
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```
|
|
|
|
///
|
2018-03-27 08:10:31 +02:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-06-02 00:57:07 +02:00
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/").route(web::route().to(index)));
|
2018-03-27 08:10:31 +02:00
|
|
|
/// ```
|
2019-03-02 07:51:32 +01:00
|
|
|
pub fn to<F, I, R>(mut self, handler: F) -> Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: Factory<I, R> + 'static,
|
2019-04-13 23:50:54 +02:00
|
|
|
I: FromRequest + 'static,
|
2018-04-14 01:02:01 +02:00
|
|
|
R: Responder + 'static,
|
2018-03-27 08:10:31 +02:00
|
|
|
{
|
2019-03-03 04:19:56 +01:00
|
|
|
self.routes.push(Route::new().to(handler));
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
2018-03-27 08:10:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 01:27:31 +02:00
|
|
|
/// Register a new route and add async handler.
|
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-06-02 00:57:24 +02:00
|
|
|
/// use actix_web::*;
|
2019-03-03 21:09:38 +01:00
|
|
|
/// use futures::future::{ok, Future};
|
2018-06-02 00:57:24 +02:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// fn index(req: HttpRequest) -> impl Future<Item=HttpResponse, Error=Error> {
|
|
|
|
/// ok(HttpResponse::Ok().finish())
|
2018-06-02 00:57:24 +02:00
|
|
|
/// }
|
|
|
|
///
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/").to_async(index));
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```
|
|
|
|
///
|
2018-05-10 01:27:31 +02:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-06-02 00:57:07 +02:00
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use futures::future::Future;
|
|
|
|
/// # fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|
|
|
/// # unimplemented!()
|
|
|
|
/// # }
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/").route(web::route().to_async(index)));
|
2018-05-10 01:27:31 +02:00
|
|
|
/// ```
|
2019-03-02 07:51:32 +01:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
|
|
|
pub fn to_async<F, I, R>(mut self, handler: F) -> Self
|
2018-05-10 01:27:31 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: AsyncFactory<I, R>,
|
2019-04-13 23:50:54 +02:00
|
|
|
I: FromRequest + 'static,
|
2019-03-02 07:51:32 +01:00
|
|
|
R: IntoFuture + 'static,
|
2019-04-22 23:22:08 +02:00
|
|
|
R::Item: Responder,
|
2019-03-02 07:51:32 +01:00
|
|
|
R::Error: Into<Error>,
|
2018-05-10 01:27:31 +02:00
|
|
|
{
|
2019-03-03 04:19:56 +01:00
|
|
|
self.routes.push(Route::new().to_async(handler));
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
2018-05-10 01:27:31 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 05:35:02 +01:00
|
|
|
/// Register a resource middleware.
|
2018-01-10 05:00:18 +01:00
|
|
|
///
|
2019-03-17 05:35:02 +01:00
|
|
|
/// This is similar to `App's` middlewares, but middleware get invoked on resource level.
|
|
|
|
/// Resource level middlewares are not allowed to change response
|
|
|
|
/// type (i.e modify response's body).
|
2019-03-25 20:47:58 +01:00
|
|
|
///
|
|
|
|
/// **Note**: middlewares get called in opposite order of middlewares registration.
|
|
|
|
pub fn wrap<M, F>(
|
2019-03-02 07:51:32 +01:00
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Resource<
|
|
|
|
impl NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-03-05 06:37:57 +01:00
|
|
|
M: Transform<
|
2019-03-02 07:51:32 +01:00
|
|
|
T::Service,
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2019-03-09 18:49:11 +01:00
|
|
|
F: IntoTransform<M, T::Service>,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
2019-04-01 03:19:18 +02:00
|
|
|
let endpoint = apply_transform(mw, self.endpoint);
|
2019-03-02 07:51:32 +01:00
|
|
|
Resource {
|
|
|
|
endpoint,
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: self.rdef,
|
2019-03-09 23:06:24 +01:00
|
|
|
name: self.name,
|
2019-03-07 00:47:15 +01:00
|
|
|
guards: self.guards,
|
2019-03-02 07:51:32 +01:00
|
|
|
routes: self.routes,
|
|
|
|
default: self.default,
|
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 20:47:58 +01:00
|
|
|
/// Register a resource middleware function.
|
|
|
|
///
|
|
|
|
/// This function accepts instance of `ServiceRequest` type and
|
|
|
|
/// mutable reference to the next middleware in chain.
|
|
|
|
///
|
|
|
|
/// This is similar to `App's` middlewares, but middleware get invoked on resource level.
|
|
|
|
/// Resource level middlewares are not allowed to change response
|
|
|
|
/// type (i.e modify response's body).
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_service::Service;
|
|
|
|
/// # use futures::Future;
|
|
|
|
/// use actix_web::{web, App};
|
|
|
|
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
|
|
|
///
|
|
|
|
/// fn index() -> &'static str {
|
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/index.html")
|
|
|
|
/// .wrap_fn(|req, srv|
|
|
|
|
/// srv.call(req).map(|mut res| {
|
|
|
|
/// res.headers_mut().insert(
|
|
|
|
/// CONTENT_TYPE, HeaderValue::from_static("text/plain"),
|
|
|
|
/// );
|
|
|
|
/// res
|
|
|
|
/// }))
|
|
|
|
/// .route(web::get().to(index)));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn wrap_fn<F, R>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Resource<
|
|
|
|
impl NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-25 20:47:58 +01:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-04-13 23:50:54 +02:00
|
|
|
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
|
2019-03-25 20:47:58 +01:00
|
|
|
R: IntoFuture<Item = ServiceResponse, Error = Error>,
|
|
|
|
{
|
|
|
|
self.wrap(mw)
|
|
|
|
}
|
|
|
|
|
2019-04-14 07:25:00 +02:00
|
|
|
/// Default service to be used if no matching route could be found.
|
2019-03-25 00:15:34 +01:00
|
|
|
/// By default *405* response get returned. Resource does not use
|
|
|
|
/// default handler from `App` or `Scope`.
|
2019-04-14 07:25:00 +02:00
|
|
|
pub fn default_service<F, U>(mut self, f: F) -> Self
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
2019-04-14 07:25:00 +02:00
|
|
|
F: IntoNewService<U>,
|
2019-03-09 18:49:11 +01:00
|
|
|
U: NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-09 18:49:11 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-09 18:49:11 +01:00
|
|
|
> + 'static,
|
2019-04-14 07:25:00 +02:00
|
|
|
U::InitError: fmt::Debug,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
|
|
|
// create and configure default resource
|
2019-03-02 08:59:44 +01:00
|
|
|
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
|
2019-04-14 07:25:00 +02:00
|
|
|
f.into_new_service().map_init_err(|e| {
|
|
|
|
log::error!("Can not construct default service: {:?}", e)
|
|
|
|
}),
|
2019-03-02 07:51:32 +01:00
|
|
|
)))));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T> HttpServiceFactory for Resource<T>
|
2019-03-07 00:47:15 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-07 00:47:15 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-07 00:47:15 +01:00
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
|
|
|
{
|
2019-04-15 16:32:49 +02:00
|
|
|
fn register(mut self, config: &mut AppService) {
|
2019-03-07 00:47:15 +01:00
|
|
|
let guards = if self.guards.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(std::mem::replace(&mut self.guards, Vec::new()))
|
|
|
|
};
|
2019-03-09 23:06:24 +01:00
|
|
|
let mut rdef = if config.is_root() || !self.rdef.is_empty() {
|
2019-03-07 00:47:15 +01:00
|
|
|
ResourceDef::new(&insert_slash(&self.rdef))
|
|
|
|
} else {
|
2019-03-07 04:19:27 +01:00
|
|
|
ResourceDef::new(&self.rdef)
|
2019-03-07 00:47:15 +01:00
|
|
|
};
|
2019-03-09 23:06:24 +01:00
|
|
|
if let Some(ref name) = self.name {
|
|
|
|
*rdef.name_mut() = name.clone();
|
|
|
|
}
|
2019-03-09 16:39:34 +01:00
|
|
|
config.register_service(rdef, guards, self, None)
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T> IntoNewService<T> for Resource<T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
fn into_new_service(self) -> T {
|
|
|
|
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
|
|
|
routes: self.routes,
|
|
|
|
default: self.default,
|
|
|
|
});
|
|
|
|
|
|
|
|
self.endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ResourceFactory {
|
|
|
|
routes: Vec<Route>,
|
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl NewService for ResourceFactory {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-03-02 07:51:32 +01:00
|
|
|
type InitError = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ResourceService;
|
|
|
|
type Future = CreateResourceService;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
let default_fut = if let Some(ref default) = *self.default.borrow() {
|
|
|
|
Some(default.new_service(&()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
CreateResourceService {
|
|
|
|
fut: self
|
|
|
|
.routes
|
|
|
|
.iter()
|
|
|
|
.map(|route| CreateRouteServiceItem::Future(route.new_service(&())))
|
|
|
|
.collect(),
|
|
|
|
default: None,
|
|
|
|
default_fut,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2018-06-25 06:58:04 +02:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
enum CreateRouteServiceItem {
|
|
|
|
Future(CreateRouteService),
|
|
|
|
Service(RouteService),
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct CreateResourceService {
|
|
|
|
fut: Vec<CreateRouteServiceItem>,
|
|
|
|
default: Option<HttpService>,
|
|
|
|
default_fut: Option<Box<Future<Item = HttpService, Error = ()>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Future for CreateResourceService {
|
|
|
|
type Item = ResourceService;
|
2019-03-02 07:51:32 +01:00
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let mut done = true;
|
|
|
|
|
|
|
|
if let Some(ref mut fut) = self.default_fut {
|
|
|
|
match fut.poll()? {
|
|
|
|
Async::Ready(default) => self.default = Some(default),
|
|
|
|
Async::NotReady => done = false,
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 19:46:58 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
// poll http services
|
|
|
|
for item in &mut self.fut {
|
|
|
|
match item {
|
|
|
|
CreateRouteServiceItem::Future(ref mut fut) => match fut.poll()? {
|
|
|
|
Async::Ready(route) => {
|
|
|
|
*item = CreateRouteServiceItem::Service(route)
|
|
|
|
}
|
|
|
|
Async::NotReady => {
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CreateRouteServiceItem::Service(_) => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
let routes = self
|
|
|
|
.fut
|
|
|
|
.drain(..)
|
|
|
|
.map(|item| match item {
|
|
|
|
CreateRouteServiceItem::Service(service) => service,
|
|
|
|
CreateRouteServiceItem::Future(_) => unreachable!(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Ok(Async::Ready(ResourceService {
|
|
|
|
routes,
|
|
|
|
default: self.default.take(),
|
|
|
|
}))
|
2018-06-25 06:58:04 +02:00
|
|
|
} else {
|
2019-03-02 07:51:32 +01:00
|
|
|
Ok(Async::NotReady)
|
2018-06-25 06:58:04 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ResourceService {
|
|
|
|
routes: Vec<RouteService>,
|
|
|
|
default: Option<HttpService>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Service for ResourceService {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-03-02 07:51:32 +01:00
|
|
|
type Future = Either<
|
2019-04-08 08:06:21 +02:00
|
|
|
FutureResult<ServiceResponse, Error>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Box<Future<Item = ServiceResponse, Error = Error>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
>;
|
2018-07-15 11:12:21 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
2019-03-02 07:51:32 +01:00
|
|
|
for route in self.routes.iter_mut() {
|
|
|
|
if route.check(&mut req) {
|
2019-04-08 08:06:21 +02:00
|
|
|
return route.call(req);
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(ref mut default) = self.default {
|
2019-04-08 08:06:21 +02:00
|
|
|
default.call(req)
|
2019-03-02 07:51:32 +01:00
|
|
|
} else {
|
2019-03-26 23:14:32 +01:00
|
|
|
let req = req.into_parts().0;
|
2019-04-08 08:06:21 +02:00
|
|
|
Either::A(ok(ServiceResponse::new(
|
2019-03-02 07:51:32 +01:00
|
|
|
req,
|
2019-03-25 00:15:34 +01:00
|
|
|
Response::MethodNotAllowed().finish(),
|
2019-04-08 08:06:21 +02:00
|
|
|
)))
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ResourceEndpoint {
|
|
|
|
factory: Rc<RefCell<Option<ResourceFactory>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl ResourceEndpoint {
|
|
|
|
fn new(factory: Rc<RefCell<Option<ResourceFactory>>>) -> Self {
|
2019-03-02 07:51:32 +01:00
|
|
|
ResourceEndpoint { factory }
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl NewService for ResourceEndpoint {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-03-02 07:51:32 +01:00
|
|
|
type InitError = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ResourceService;
|
|
|
|
type Future = CreateResourceService;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 00:15:34 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-25 22:33:34 +01:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2019-03-25 20:47:58 +01:00
|
|
|
use actix_service::Service;
|
|
|
|
use futures::{Future, IntoFuture};
|
2019-03-25 22:33:34 +01:00
|
|
|
use tokio_timer::sleep;
|
2019-03-25 20:47:58 +01:00
|
|
|
|
|
|
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
2019-04-15 16:44:07 +02:00
|
|
|
use crate::test::{call_service, init_service, TestRequest};
|
2019-03-25 20:47:58 +01:00
|
|
|
use crate::{web, App, Error, HttpResponse};
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
fn md<S, B>(
|
|
|
|
req: ServiceRequest,
|
2019-03-25 20:47:58 +01:00
|
|
|
srv: &mut S,
|
|
|
|
) -> impl IntoFuture<Item = ServiceResponse<B>, Error = Error>
|
|
|
|
where
|
|
|
|
S: Service<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-25 20:47:58 +01:00
|
|
|
Response = ServiceResponse<B>,
|
|
|
|
Error = Error,
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
srv.call(req).map(|mut res| {
|
|
|
|
res.headers_mut()
|
|
|
|
.insert(header::CONTENT_TYPE, HeaderValue::from_static("0001"));
|
|
|
|
res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_middleware() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/test")
|
2019-03-25 22:33:34 +01:00
|
|
|
.name("test")
|
2019-03-25 21:02:10 +01:00
|
|
|
.wrap(md)
|
2019-03-25 20:47:58 +01:00
|
|
|
.route(web::get().to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2019-04-15 16:44:07 +02:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 20:47:58 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_middleware_fn() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/test")
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
srv.call(req).map(|mut res| {
|
|
|
|
res.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("0001"),
|
|
|
|
);
|
|
|
|
res
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.route(web::get().to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2019-04-15 16:44:07 +02:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 20:47:58 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
2019-03-25 00:15:34 +01:00
|
|
|
|
2019-03-25 22:33:34 +01:00
|
|
|
#[test]
|
|
|
|
fn test_to_async() {
|
|
|
|
let mut srv =
|
|
|
|
init_service(App::new().service(web::resource("/test").to_async(|| {
|
|
|
|
sleep(Duration::from_millis(100)).then(|_| HttpResponse::Ok())
|
|
|
|
})));
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2019-04-15 16:44:07 +02:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 22:33:34 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
2019-03-25 00:15:34 +01:00
|
|
|
#[test]
|
|
|
|
fn test_default_resource() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.service(
|
|
|
|
web::resource("/test").route(web::get().to(|| HttpResponse::Ok())),
|
|
|
|
)
|
2019-04-14 07:25:00 +02:00
|
|
|
.default_service(|r: ServiceRequest| {
|
|
|
|
r.into_response(HttpResponse::BadRequest())
|
|
|
|
}),
|
2019-03-25 00:15:34 +01:00
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2019-04-15 16:44:07 +02:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 00:15:34 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2019-04-15 16:44:07 +02:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 00:15:34 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/test")
|
|
|
|
.route(web::get().to(|| HttpResponse::Ok()))
|
2019-04-14 07:25:00 +02:00
|
|
|
.default_service(|r: ServiceRequest| {
|
|
|
|
r.into_response(HttpResponse::BadRequest())
|
|
|
|
}),
|
2019-03-25 00:15:34 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2019-04-15 16:44:07 +02:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 00:15:34 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2019-04-15 16:44:07 +02:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 00:15:34 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
}
|