2017-10-07 06:48:14 +02:00
|
|
|
use std::marker::PhantomData;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::rc::Rc;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-05-10 01:27:31 +02:00
|
|
|
use futures::Future;
|
2018-06-19 19:46:58 +02:00
|
|
|
use http::Method;
|
2018-04-14 01:02:01 +02:00
|
|
|
use smallvec::SmallVec;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-05-10 01:27:31 +02:00
|
|
|
use error::Error;
|
2018-05-04 01:22:08 +02:00
|
|
|
use handler::{AsyncResult, FromRequest, Handler, Responder};
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-12-12 01:26:51 +01:00
|
|
|
use httpresponse::HttpResponse;
|
2018-04-14 01:02:01 +02:00
|
|
|
use middleware::Middleware;
|
|
|
|
use pred;
|
|
|
|
use route::Route;
|
2017-12-04 22:32:05 +01:00
|
|
|
|
2017-12-05 01:09:22 +01:00
|
|
|
/// *Resource* is an entry in route table which corresponds to requested URL.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
|
|
|
/// Resource in turn has at least one route.
|
2017-12-04 22:32:05 +01:00
|
|
|
/// Route consists of an object that implements `Handler` trait (handler)
|
|
|
|
/// and list of predicates (objects that implement `Predicate` trait).
|
|
|
|
/// Route uses builder-like pattern for configuration.
|
|
|
|
/// During request handling, resource object iterate through all routes
|
2018-04-14 01:02:01 +02:00
|
|
|
/// and check all predicates for specific route, if request matches all
|
|
|
|
/// predicates route route considered matched and route handler get called.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
2017-11-29 22:26:55 +01:00
|
|
|
/// ```rust
|
2018-06-01 19:27:23 +02:00
|
|
|
/// # extern crate actix_web;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{App, HttpResponse, http};
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2017-12-04 22:32:05 +01:00
|
|
|
/// .resource(
|
2018-03-31 02:31:18 +02:00
|
|
|
/// "/", |r| r.method(http::Method::GET).f(|r| HttpResponse::Ok()))
|
2017-10-15 23:17:41 +02:00
|
|
|
/// .finish();
|
2017-10-08 08:59:57 +02:00
|
|
|
/// }
|
2018-04-14 01:02:01 +02:00
|
|
|
pub struct ResourceHandler<S = ()> {
|
2017-10-17 04:21:24 +02:00
|
|
|
name: String,
|
2017-10-07 06:48:14 +02:00
|
|
|
state: PhantomData<S>,
|
2018-03-09 17:00:44 +01:00
|
|
|
routes: SmallVec<[Route<S>; 3]>,
|
2018-06-21 19:06:23 +02:00
|
|
|
middlewares: Rc<Vec<Box<Middleware<S>>>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
impl<S> Default for ResourceHandler<S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
fn default() -> Self {
|
2018-04-02 02:37:22 +02:00
|
|
|
ResourceHandler {
|
2017-10-17 04:21:24 +02:00
|
|
|
name: String::new(),
|
2017-10-07 06:48:14 +02:00
|
|
|
state: PhantomData,
|
2018-03-09 17:00:44 +01:00
|
|
|
routes: SmallVec::new(),
|
2018-06-21 19:06:23 +02:00
|
|
|
middlewares: Rc::new(Vec::new()),
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
impl<S> ResourceHandler<S> {
|
2017-10-29 14:05:31 +01:00
|
|
|
pub(crate) fn default_not_found() -> Self {
|
2018-04-02 02:37:22 +02:00
|
|
|
ResourceHandler {
|
2017-10-29 14:05:31 +01:00
|
|
|
name: String::new(),
|
|
|
|
state: PhantomData,
|
2018-03-09 17:00:44 +01:00
|
|
|
routes: SmallVec::new(),
|
2018-06-21 19:06:23 +02:00
|
|
|
middlewares: Rc::new(Vec::new()),
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-10-29 14:05:31 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 04:21:24 +02:00
|
|
|
/// Set resource name
|
2017-12-03 01:37:21 +01:00
|
|
|
pub fn name<T: Into<String>>(&mut self, name: T) {
|
2017-11-27 02:30:35 +01:00
|
|
|
self.name = name.into();
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
pub(crate) fn get_name(&self) -> &str {
|
|
|
|
&self.name
|
2017-12-05 20:31:35 +01:00
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
impl<S: 'static> ResourceHandler<S> {
|
2017-12-04 22:32:05 +01:00
|
|
|
/// Register a new route and return mutable reference to *Route* object.
|
2018-04-14 01:02:01 +02:00
|
|
|
/// *Route* is used for route configuration, i.e. adding predicates,
|
|
|
|
/// setting up handler.
|
2017-12-04 22:32:05 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
2017-12-06 20:00:39 +01:00
|
|
|
/// # extern crate actix_web;
|
2017-12-04 22:32:05 +01:00
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2018-06-01 18:37:14 +02:00
|
|
|
/// .resource("/", |r| {
|
|
|
|
/// r.route()
|
|
|
|
/// .filter(pred::Any(pred::Get()).or(pred::Put()))
|
|
|
|
/// .filter(pred::Header("Content-Type", "text/plain"))
|
|
|
|
/// .f(|r| HttpResponse::Ok())
|
|
|
|
/// })
|
2017-12-04 22:32:05 +01:00
|
|
|
/// .finish();
|
|
|
|
/// }
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
2017-12-04 22:32:05 +01:00
|
|
|
pub fn route(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap()
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 19:57:53 +02:00
|
|
|
/// Register a new `GET` route.
|
|
|
|
pub fn get(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().filter(pred::Get())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a new `POST` route.
|
|
|
|
pub fn post(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().filter(pred::Post())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a new `PUT` route.
|
|
|
|
pub fn put(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().filter(pred::Put())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a new `DELETE` route.
|
|
|
|
pub fn delete(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().filter(pred::Delete())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a new `HEAD` route.
|
|
|
|
pub fn head(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().filter(pred::Head())
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
/// Register a new route and add method check to route.
|
2017-12-06 17:03:08 +01:00
|
|
|
///
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
///
|
|
|
|
/// App::new().resource("/", |r| r.method(http::Method::GET).f(index));
|
|
|
|
/// ```
|
|
|
|
///
|
2017-12-06 20:00:39 +01:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2018-06-02 00:57:07 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
/// App::new().resource("/", |r| r.route().filter(pred::Get()).f(index));
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
2017-12-04 22:32:05 +01:00
|
|
|
pub fn method(&mut self, method: Method) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
2018-05-17 21:20:20 +02:00
|
|
|
self.routes.last_mut().unwrap().filter(pred::Method(method))
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:03:08 +01:00
|
|
|
/// Register a new route and add handler object.
|
|
|
|
///
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// fn handler(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
///
|
|
|
|
/// App::new().resource("/", |r| r.h(handler));
|
|
|
|
/// ```
|
|
|
|
///
|
2017-12-06 20:00:39 +01:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2018-06-02 00:57:07 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn handler(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
/// App::new().resource("/", |r| r.route().h(handler));
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
|
|
|
pub fn h<H: Handler<S>>(&mut self, handler: H) {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().h(handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a new route and add handler function.
|
|
|
|
///
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
///
|
|
|
|
/// App::new().resource("/", |r| r.f(index));
|
|
|
|
/// ```
|
|
|
|
///
|
2017-12-06 20:00:39 +01:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2018-06-02 00:57:07 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
/// App::new().resource("/", |r| r.route().f(index));
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
|
|
|
pub fn f<F, R>(&mut self, handler: F)
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: Fn(HttpRequest<S>) -> R + 'static,
|
|
|
|
R: Responder + 'static,
|
2017-12-06 17:03:08 +01:00
|
|
|
{
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().f(handler)
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-03-27 08:10:31 +02:00
|
|
|
/// Register a new route and add handler.
|
|
|
|
///
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
///
|
|
|
|
/// App::new().resource("/", |r| r.with(index));
|
|
|
|
/// ```
|
|
|
|
///
|
2018-03-27 08:10:31 +02:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2018-06-02 00:57:07 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
|
|
|
/// App::new().resource("/", |r| r.route().with(index));
|
2018-03-27 08:10:31 +02:00
|
|
|
/// ```
|
2018-03-31 18:58:33 +02:00
|
|
|
pub fn with<T, F, R>(&mut self, handler: F)
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: Fn(T) -> R + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
T: FromRequest<S> + 'static,
|
2018-03-27 08:10:31 +02:00
|
|
|
{
|
|
|
|
self.routes.push(Route::default());
|
2018-04-04 07:06:18 +02:00
|
|
|
self.routes.last_mut().unwrap().with(handler);
|
2018-03-27 08:10:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 01:27:31 +02:00
|
|
|
/// Register a new route and add async handler.
|
|
|
|
///
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// use futures::future::Future;
|
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// App::new().resource("/", |r| r.with_async(index));
|
|
|
|
/// ```
|
|
|
|
///
|
2018-05-10 01:27:31 +02:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2018-06-02 00:57:07 +02:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use futures::future::Future;
|
|
|
|
/// # fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|
|
|
/// # unimplemented!()
|
|
|
|
/// # }
|
|
|
|
/// App::new().resource("/", |r| r.route().with_async(index));
|
2018-05-10 01:27:31 +02:00
|
|
|
/// ```
|
|
|
|
pub fn with_async<T, F, R, I, E>(&mut self, handler: F)
|
|
|
|
where
|
|
|
|
F: Fn(T) -> R + 'static,
|
|
|
|
R: Future<Item = I, Error = E> + 'static,
|
|
|
|
I: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static,
|
|
|
|
T: FromRequest<S> + 'static,
|
|
|
|
{
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().with_async(handler);
|
|
|
|
}
|
|
|
|
|
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
|
2018-01-10 05:00:18 +01:00
|
|
|
/// middlewares get invoked on resource level.
|
2018-04-30 05:50:38 +02:00
|
|
|
///
|
|
|
|
/// *Note* `Middleware::finish()` fires right after response get
|
|
|
|
/// prepared. It does not wait until body get sent to peer.
|
2018-01-10 05:00:18 +01:00
|
|
|
pub fn middleware<M: Middleware<S>>(&mut self, mw: M) {
|
2018-04-29 18:09:08 +02:00
|
|
|
Rc::get_mut(&mut self.middlewares)
|
|
|
|
.unwrap()
|
|
|
|
.push(Box::new(mw));
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) fn handle(
|
2018-06-21 13:07:54 +02:00
|
|
|
&self, mut req: HttpRequest<S>,
|
2018-06-19 19:46:58 +02:00
|
|
|
) -> Result<AsyncResult<HttpResponse>, HttpRequest<S>> {
|
2018-06-21 13:07:54 +02:00
|
|
|
for route in &self.routes {
|
2017-12-04 22:32:05 +01:00
|
|
|
if route.check(&mut req) {
|
2018-06-21 19:06:23 +02:00
|
|
|
return if self.middlewares.is_empty() {
|
2018-06-19 19:46:58 +02:00
|
|
|
Ok(route.handle(req))
|
2018-01-10 05:00:18 +01:00
|
|
|
} else {
|
2018-06-19 19:46:58 +02:00
|
|
|
Ok(route.compose(req, Rc::clone(&self.middlewares)))
|
2018-01-10 05:00:18 +01:00
|
|
|
};
|
2017-12-04 22:32:05 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2018-06-19 19:46:58 +02:00
|
|
|
|
|
|
|
Err(req)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|