2019-03-02 07:51:32 +01:00
|
|
|
use std::cell::RefCell;
|
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::{
|
|
|
|
ApplyNewService, IntoNewService, IntoNewTransform, NewService, NewTransform, Service,
|
|
|
|
};
|
|
|
|
use futures::future::{ok, Either, FutureResult};
|
|
|
|
use futures::{Async, Future, IntoFuture, Poll};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-03-03 22:53:31 +01:00
|
|
|
use crate::extract::FromRequest;
|
|
|
|
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-03-02 08:59:44 +01:00
|
|
|
type HttpService<P> = BoxedService<ServiceRequest<P>, ServiceResponse, ()>;
|
|
|
|
type HttpNewService<P> = BoxedNewService<(), ServiceRequest<P>, ServiceResponse, (), ()>;
|
|
|
|
|
2019-03-03 23:45:56 +01:00
|
|
|
/// *Resource* is an entry in route 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() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .resource(
|
|
|
|
/// "/", |r| r.route(web::get().to(|| HttpResponse::Ok())));
|
|
|
|
/// }
|
2019-03-02 07:51:32 +01:00
|
|
|
pub struct Resource<P, T = ResourceEndpoint<P>> {
|
|
|
|
routes: Vec<Route<P>>,
|
|
|
|
endpoint: T,
|
2019-03-02 08:59:44 +01:00
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService<P>>>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
factory_ref: Rc<RefCell<Option<ResourceFactory<P>>>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P> Resource<P> {
|
|
|
|
pub fn new() -> Resource<P> {
|
|
|
|
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(),
|
|
|
|
endpoint: ResourceEndpoint::new(fref.clone()),
|
|
|
|
factory_ref: fref,
|
|
|
|
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-03-02 07:51:32 +01:00
|
|
|
impl<P> Default for Resource<P> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
2017-12-05 20:31:35 +01:00
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P: 'static, T> Resource<P, T>
|
|
|
|
where
|
|
|
|
T: NewService<
|
|
|
|
Request = ServiceRequest<P>,
|
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
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() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2018-06-01 18:37:14 +02:00
|
|
|
/// .resource("/", |r| {
|
2019-03-03 21:09:38 +01:00
|
|
|
/// r.route(web::route()
|
|
|
|
/// .guard(guard::Any(guard::Get()).or(guard::Put()))
|
|
|
|
/// .guard(guard::Header("Content-Type", "text/plain"))
|
|
|
|
/// .to(|| HttpResponse::Ok()))
|
|
|
|
/// });
|
2017-12-04 22:32:05 +01:00
|
|
|
/// }
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
2019-03-03 23:45:56 +01:00
|
|
|
///
|
|
|
|
/// Multiple routes could be added to a resource.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, guard, App, HttpResponse};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .resource("/container/", |r| {
|
|
|
|
/// r.route(web::get().to(get_handler))
|
|
|
|
/// .route(web::post().to(post_handler))
|
|
|
|
/// .route(web::delete().to(delete_handler))
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
/// # fn get_handler() {}
|
|
|
|
/// # fn post_handler() {}
|
|
|
|
/// # fn delete_handler() {}
|
|
|
|
/// ```
|
2019-03-03 04:19:56 +01:00
|
|
|
pub fn route(mut self, route: Route<P>) -> 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-03 23:45:56 +01:00
|
|
|
/// Register a new route and add handler. This route get called for 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!()
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// App::new().resource("/", |r| r.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-03 21:09:38 +01:00
|
|
|
/// App::new().resource("/", |r| r.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,
|
|
|
|
I: FromRequest<P> + '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-03 21:09:38 +01:00
|
|
|
/// App::new().resource("/", |r| r.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-03 21:09:38 +01:00
|
|
|
/// App::new().resource("/", |r| r.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>,
|
|
|
|
I: FromRequest<P> + 'static,
|
|
|
|
R: IntoFuture + 'static,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
/// Register a resource middleware
|
2018-01-10 05:00:18 +01:00
|
|
|
///
|
2018-03-31 09:16:55 +02:00
|
|
|
/// This is similar to `App's` middlewares, but
|
2019-03-03 23:45:56 +01:00
|
|
|
/// middleware is not allowed to change response type (i.e modify response's body).
|
|
|
|
/// Middleware get invoked on resource level.
|
2019-03-02 07:51:32 +01:00
|
|
|
pub fn middleware<M, F>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Resource<
|
|
|
|
P,
|
|
|
|
impl NewService<
|
|
|
|
Request = ServiceRequest<P>,
|
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
|
|
|
M: NewTransform<
|
|
|
|
T::Service,
|
|
|
|
Request = ServiceRequest<P>,
|
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
F: IntoNewTransform<M, T::Service>,
|
|
|
|
{
|
|
|
|
let endpoint = ApplyNewService::new(mw, self.endpoint);
|
|
|
|
Resource {
|
|
|
|
endpoint,
|
|
|
|
routes: self.routes,
|
|
|
|
default: self.default,
|
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Default resource to be used if no matching route could be found.
|
|
|
|
pub fn default_resource<F, R, U>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(Resource<P>) -> R,
|
|
|
|
R: IntoNewService<U>,
|
|
|
|
U: NewService<
|
|
|
|
Request = ServiceRequest<P>,
|
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
> + 'static,
|
|
|
|
{
|
|
|
|
// 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(
|
|
|
|
f(Resource::new()).into_new_service().map_init_err(|_| ()),
|
2019-03-02 07:51:32 +01:00
|
|
|
)))));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-02 08:59:44 +01:00
|
|
|
pub(crate) fn get_default(&self) -> Rc<RefCell<Option<Rc<HttpNewService<P>>>>> {
|
2019-03-02 07:51:32 +01:00
|
|
|
self.default.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P, T> IntoNewService<T> for Resource<P, T>
|
|
|
|
where
|
|
|
|
T: NewService<
|
|
|
|
Request = ServiceRequest<P>,
|
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
fn into_new_service(self) -> T {
|
|
|
|
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
|
|
|
routes: self.routes,
|
|
|
|
default: self.default,
|
|
|
|
});
|
|
|
|
|
|
|
|
self.endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ResourceFactory<P> {
|
|
|
|
routes: Vec<Route<P>>,
|
2019-03-02 08:59:44 +01:00
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService<P>>>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 08:59:44 +01:00
|
|
|
impl<P: 'static> NewService for ResourceFactory<P> {
|
2019-03-02 07:51:32 +01:00
|
|
|
type Request = ServiceRequest<P>;
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = ResourceService<P>;
|
|
|
|
type Future = CreateResourceService<P>;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
enum CreateRouteServiceItem<P> {
|
|
|
|
Future(CreateRouteService<P>),
|
|
|
|
Service(RouteService<P>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CreateResourceService<P> {
|
|
|
|
fut: Vec<CreateRouteServiceItem<P>>,
|
2019-03-02 08:59:44 +01:00
|
|
|
default: Option<HttpService<P>>,
|
|
|
|
default_fut: Option<Box<Future<Item = HttpService<P>, Error = ()>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> Future for CreateResourceService<P> {
|
|
|
|
type Item = ResourceService<P>;
|
|
|
|
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-03-02 07:51:32 +01:00
|
|
|
pub struct ResourceService<P> {
|
|
|
|
routes: Vec<RouteService<P>>,
|
2019-03-02 08:59:44 +01:00
|
|
|
default: Option<HttpService<P>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> Service for ResourceService<P> {
|
|
|
|
type Request = ServiceRequest<P>;
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type Future = Either<
|
|
|
|
Box<Future<Item = ServiceResponse, Error = ()>>,
|
|
|
|
Either<
|
|
|
|
Box<Future<Item = Self::Response, Error = Self::Error>>,
|
|
|
|
FutureResult<Self::Response, Self::Error>,
|
|
|
|
>,
|
|
|
|
>;
|
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-03-02 07:51:32 +01:00
|
|
|
fn call(&mut self, mut req: ServiceRequest<P>) -> Self::Future {
|
|
|
|
for route in self.routes.iter_mut() {
|
|
|
|
if route.check(&mut req) {
|
|
|
|
return Either::A(route.call(req));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(ref mut default) = self.default {
|
|
|
|
Either::B(Either::A(default.call(req)))
|
|
|
|
} else {
|
|
|
|
let req = req.into_request();
|
|
|
|
Either::B(Either::B(ok(ServiceResponse::new(
|
|
|
|
req,
|
|
|
|
Response::NotFound().finish(),
|
|
|
|
))))
|
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct ResourceEndpoint<P> {
|
|
|
|
factory: Rc<RefCell<Option<ResourceFactory<P>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> ResourceEndpoint<P> {
|
|
|
|
fn new(factory: Rc<RefCell<Option<ResourceFactory<P>>>>) -> Self {
|
|
|
|
ResourceEndpoint { factory }
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 08:59:44 +01:00
|
|
|
impl<P: 'static> NewService for ResourceEndpoint<P> {
|
2019-03-02 07:51:32 +01:00
|
|
|
type Request = ServiceRequest<P>;
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = ResourceService<P>;
|
|
|
|
type Future = CreateResourceService<P>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|