2018-01-10 04:00:18 +00:00
|
|
|
use std::rc::Rc;
|
2017-12-05 00:09:22 +00:00
|
|
|
|
2019-05-05 02:43:49 +00:00
|
|
|
use actix_http::{http::Method, Error};
|
2019-03-02 06:51:32 +00:00
|
|
|
use actix_service::{NewService, Service};
|
2019-04-08 06:06:21 +00:00
|
|
|
use futures::future::{ok, Either, FutureResult};
|
2019-03-02 06:51:32 +00:00
|
|
|
use futures::{Async, Future, IntoFuture, Poll};
|
|
|
|
|
2019-03-10 17:53:56 +00:00
|
|
|
use crate::extract::FromRequest;
|
2019-03-03 20:09:38 +00:00
|
|
|
use crate::guard::{self, Guard};
|
2019-03-07 19:09:42 +00:00
|
|
|
use crate::handler::{AsyncFactory, AsyncHandler, Extract, Factory, Handler};
|
2019-03-02 06:51:32 +00:00
|
|
|
use crate::responder::Responder;
|
2019-04-07 21:43:07 +00:00
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
2019-03-03 03:19:56 +00:00
|
|
|
use crate::HttpResponse;
|
2019-03-02 06:51:32 +00:00
|
|
|
|
|
|
|
type BoxedRouteService<Req, Res> = Box<
|
|
|
|
Service<
|
2019-03-09 17:49:11 +00:00
|
|
|
Request = Req,
|
2019-03-02 06:51:32 +00:00
|
|
|
Response = Res,
|
2019-03-10 23:35:38 +00:00
|
|
|
Error = Error,
|
2019-04-08 06:06:21 +00:00
|
|
|
Future = Either<
|
|
|
|
FutureResult<Res, Error>,
|
|
|
|
Box<Future<Item = Res, Error = Error>>,
|
|
|
|
>,
|
2019-03-02 06:51:32 +00:00
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
type BoxedRouteNewService<Req, Res> = Box<
|
|
|
|
NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-03-09 17:49:11 +00:00
|
|
|
Request = Req,
|
2019-03-02 06:51:32 +00:00
|
|
|
Response = Res,
|
2019-03-10 23:35:38 +00:00
|
|
|
Error = Error,
|
2019-03-02 06:51:32 +00:00
|
|
|
InitError = (),
|
|
|
|
Service = BoxedRouteService<Req, Res>,
|
|
|
|
Future = Box<Future<Item = BoxedRouteService<Req, Res>, Error = ()>>,
|
|
|
|
>,
|
|
|
|
>;
|
2017-12-05 00:09:22 +00:00
|
|
|
|
|
|
|
/// Resource route definition
|
|
|
|
///
|
|
|
|
/// Route uses builder-like pattern for configuration.
|
|
|
|
/// If handler is not explicitly set, default *404 Not Found* handler is used.
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct Route {
|
|
|
|
service: BoxedRouteNewService<ServiceRequest, ServiceResponse>,
|
2019-03-03 20:09:38 +00:00
|
|
|
guards: Rc<Vec<Box<Guard>>>,
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl Route {
|
2019-03-03 08:57:48 +00:00
|
|
|
/// Create new route which matches any request.
|
2019-04-13 21:50:54 +00:00
|
|
|
pub fn new() -> Route {
|
2019-03-03 03:19:56 +00:00
|
|
|
Route {
|
2019-05-05 02:43:49 +00:00
|
|
|
service: Box::new(RouteNewService::new(Extract::new(Handler::new(|| {
|
|
|
|
HttpResponse::NotFound()
|
|
|
|
})))),
|
2019-03-03 20:09:38 +00:00
|
|
|
guards: Rc::new(Vec::new()),
|
2019-03-03 03:19:56 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 23:47:15 +00:00
|
|
|
pub(crate) fn take_guards(&mut self) -> Vec<Box<Guard>> {
|
|
|
|
std::mem::replace(Rc::get_mut(&mut self.guards).unwrap(), Vec::new())
|
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl NewService for Route {
|
2019-05-12 15:34:51 +00:00
|
|
|
type Config = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Request = ServiceRequest;
|
2019-03-02 06:51:32 +00:00
|
|
|
type Response = ServiceResponse;
|
2019-03-10 23:35:38 +00:00
|
|
|
type Error = Error;
|
2019-03-02 06:51:32 +00:00
|
|
|
type InitError = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Service = RouteService;
|
|
|
|
type Future = CreateRouteService;
|
2019-03-02 06:51:32 +00:00
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
CreateRouteService {
|
|
|
|
fut: self.service.new_service(&()),
|
2019-03-03 20:09:38 +00:00
|
|
|
guards: self.guards.clone(),
|
2019-03-02 06:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
type RouteFuture =
|
|
|
|
Box<Future<Item = BoxedRouteService<ServiceRequest, ServiceResponse>, Error = ()>>;
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct CreateRouteService {
|
|
|
|
fut: RouteFuture,
|
2019-03-03 20:09:38 +00:00
|
|
|
guards: Rc<Vec<Box<Guard>>>,
|
2019-03-02 06:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl Future for CreateRouteService {
|
|
|
|
type Item = RouteService;
|
2019-03-02 06:51:32 +00:00
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll()? {
|
|
|
|
Async::Ready(service) => Ok(Async::Ready(RouteService {
|
|
|
|
service,
|
2019-03-03 20:09:38 +00:00
|
|
|
guards: self.guards.clone(),
|
2019-03-02 06:51:32 +00:00
|
|
|
})),
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct RouteService {
|
|
|
|
service: BoxedRouteService<ServiceRequest, ServiceResponse>,
|
2019-03-03 20:09:38 +00:00
|
|
|
guards: Rc<Vec<Box<Guard>>>,
|
2019-03-02 06:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl RouteService {
|
|
|
|
pub fn check(&self, req: &mut ServiceRequest) -> bool {
|
2019-03-03 20:09:38 +00:00
|
|
|
for f in self.guards.iter() {
|
2019-03-03 03:19:56 +00:00
|
|
|
if !f.check(req.head()) {
|
2018-04-13 23:02:01 +00:00
|
|
|
return false;
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
}
|
2017-12-05 00:09:22 +00:00
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl Service for RouteService {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-02 06:51:32 +00:00
|
|
|
type Response = ServiceResponse;
|
2019-03-10 23:35:38 +00:00
|
|
|
type Error = Error;
|
2019-04-08 06:06:21 +00:00
|
|
|
type Future = Either<
|
|
|
|
FutureResult<Self::Response, Self::Error>,
|
|
|
|
Box<Future<Item = Self::Response, Error = Self::Error>>,
|
|
|
|
>;
|
2019-03-02 06:51:32 +00:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.service.poll_ready()
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
2019-03-02 06:51:32 +00:00
|
|
|
self.service.call(req)
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
}
|
2018-01-10 04:00:18 +00:00
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl Route {
|
2019-03-03 20:09:38 +00:00
|
|
|
/// Add method guard to the route.
|
2017-12-20 06:36:06 +00:00
|
|
|
///
|
2019-03-03 20:09:38 +00:00
|
|
|
/// ```rust
|
2017-12-20 06:36:06 +00:00
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// App::new().service(web::resource("/path").route(
|
|
|
|
/// web::get()
|
|
|
|
/// .method(http::Method::CONNECT)
|
2019-03-03 20:09:38 +00:00
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
|
|
|
/// .to(|req: HttpRequest| HttpResponse::Ok()))
|
2019-03-06 23:47:15 +00:00
|
|
|
/// );
|
2017-12-20 06:36:06 +00:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2019-03-02 06:51:32 +00:00
|
|
|
pub fn method(mut self, method: Method) -> Self {
|
2019-03-03 20:09:38 +00:00
|
|
|
Rc::get_mut(&mut self.guards)
|
2019-03-03 03:19:56 +00:00
|
|
|
.unwrap()
|
2019-03-03 20:09:38 +00:00
|
|
|
.push(Box::new(guard::Method(method)));
|
2017-12-05 00:09:22 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:09:38 +00:00
|
|
|
/// Add guard to the route.
|
2019-03-02 06:51:32 +00:00
|
|
|
///
|
2019-03-03 20:09:38 +00:00
|
|
|
/// ```rust
|
2019-03-02 06:51:32 +00:00
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// App::new().service(web::resource("/path").route(
|
|
|
|
/// web::route()
|
2019-03-03 20:09:38 +00:00
|
|
|
/// .guard(guard::Get())
|
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
|
|
|
/// .to(|req: HttpRequest| HttpResponse::Ok()))
|
2019-03-06 23:47:15 +00:00
|
|
|
/// );
|
2019-03-02 06:51:32 +00:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2019-03-03 20:09:38 +00:00
|
|
|
pub fn guard<F: Guard + 'static>(mut self, f: F) -> Self {
|
|
|
|
Rc::get_mut(&mut self.guards).unwrap().push(Box::new(f));
|
2019-03-02 06:51:32 +00:00
|
|
|
self
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:09:38 +00:00
|
|
|
/// Set handler function, use request extractors for parameters.
|
2018-03-27 06:10:31 +00:00
|
|
|
///
|
2019-03-03 20:09:38 +00:00
|
|
|
/// ```rust
|
2018-03-27 06:10:31 +00:00
|
|
|
/// #[macro_use] extern crate serde_derive;
|
2019-03-07 22:01:52 +00:00
|
|
|
/// use actix_web::{web, http, App};
|
2018-03-27 06:10:31 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2019-03-07 22:01:52 +00:00
|
|
|
/// fn index(info: web::Path<Info>) -> String {
|
2019-03-03 20:09:38 +00:00
|
|
|
/// format!("Welcome {}!", info.username)
|
2018-03-27 06:10:31 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/{username}/index.html") // <- define path parameters
|
|
|
|
/// .route(web::get().to(index)) // <- register handler
|
2019-03-03 20:09:38 +00:00
|
|
|
/// );
|
2018-03-27 06:10:31 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-05-09 23:27:31 +00:00
|
|
|
///
|
2018-10-02 04:16:56 +00:00
|
|
|
/// It is possible to use multiple extractors for one handler function.
|
2018-05-09 23:27:31 +00:00
|
|
|
///
|
2019-03-03 20:09:38 +00:00
|
|
|
/// ```rust
|
2018-05-09 23:27:31 +00:00
|
|
|
/// # use std::collections::HashMap;
|
2019-03-03 20:09:38 +00:00
|
|
|
/// # use serde_derive::Deserialize;
|
2019-03-07 19:43:46 +00:00
|
|
|
/// use actix_web::{web, App};
|
2018-05-09 23:27:31 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2019-03-07 19:43:46 +00:00
|
|
|
/// fn index(path: web::Path<Info>, query: web::Query<HashMap<String, String>>, body: web::Json<Info>) -> String {
|
2019-03-03 20:09:38 +00:00
|
|
|
/// format!("Welcome {}!", path.username)
|
2018-05-09 23:27:31 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/{username}/index.html") // <- define path parameters
|
|
|
|
/// .route(web::get().to(index))
|
2019-03-03 20:09:38 +00:00
|
|
|
/// );
|
2018-05-09 23:27:31 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-04-13 21:50:54 +00:00
|
|
|
pub fn to<F, T, R>(mut self, handler: F) -> Route
|
2018-06-21 05:47:01 +00:00
|
|
|
where
|
2019-03-02 06:51:32 +00:00
|
|
|
F: Factory<T, R> + 'static,
|
2019-04-13 21:50:54 +00:00
|
|
|
T: FromRequest + 'static,
|
2018-06-21 05:47:01 +00:00
|
|
|
R: Responder + 'static,
|
|
|
|
{
|
2019-05-05 02:43:49 +00:00
|
|
|
self.service =
|
|
|
|
Box::new(RouteNewService::new(Extract::new(Handler::new(handler))));
|
2019-03-03 03:19:56 +00:00
|
|
|
self
|
2018-03-27 06:10:31 +00:00
|
|
|
}
|
2018-03-28 21:24:32 +00:00
|
|
|
|
2019-03-03 20:09:38 +00:00
|
|
|
/// Set async handler function, use request extractors for parameters.
|
|
|
|
/// This method has to be used if your handler function returns `impl Future<>`
|
2018-05-09 23:27:31 +00:00
|
|
|
///
|
2019-03-03 20:09:38 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # use futures::future::ok;
|
2018-05-09 23:27:31 +00:00
|
|
|
/// #[macro_use] extern crate serde_derive;
|
2019-03-07 22:01:52 +00:00
|
|
|
/// use actix_web::{web, App, Error};
|
2018-05-09 23:27:31 +00:00
|
|
|
/// use futures::Future;
|
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2019-03-07 22:01:52 +00:00
|
|
|
/// fn index(info: web::Path<Info>) -> impl Future<Item = &'static str, Error = Error> {
|
2019-03-03 20:09:38 +00:00
|
|
|
/// ok("Hello World!")
|
2018-05-09 23:27:31 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/{username}/index.html") // <- define path parameters
|
|
|
|
/// .route(web::get().to_async(index)) // <- register async handler
|
2019-03-03 20:09:38 +00:00
|
|
|
/// );
|
2018-05-09 23:27:31 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-02 06:51:32 +00:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2019-03-03 03:19:56 +00:00
|
|
|
pub fn to_async<F, T, R>(mut self, handler: F) -> Self
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
2019-03-02 06:51:32 +00:00
|
|
|
F: AsyncFactory<T, R>,
|
2019-04-13 21:50:54 +00:00
|
|
|
T: FromRequest + 'static,
|
2019-03-02 06:51:32 +00:00
|
|
|
R: IntoFuture + 'static,
|
2019-04-22 21:22:08 +00:00
|
|
|
R::Item: Responder,
|
2019-03-02 06:51:32 +00:00
|
|
|
R::Error: Into<Error>,
|
2018-01-10 04:00:18 +00:00
|
|
|
{
|
2019-05-05 02:43:49 +00:00
|
|
|
self.service = Box::new(RouteNewService::new(Extract::new(AsyncHandler::new(
|
|
|
|
handler,
|
|
|
|
))));
|
2019-03-03 08:57:48 +00:00
|
|
|
self
|
|
|
|
}
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
struct RouteNewService<T>
|
2019-03-02 06:51:32 +00:00
|
|
|
where
|
2019-04-13 21:50:54 +00:00
|
|
|
T: NewService<Request = ServiceRequest, Error = (Error, ServiceRequest)>,
|
2019-03-02 06:51:32 +00:00
|
|
|
{
|
|
|
|
service: T,
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<T> RouteNewService<T>
|
2019-03-02 06:51:32 +00:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 06:51:32 +00:00
|
|
|
Response = ServiceResponse,
|
2019-04-13 21:50:54 +00:00
|
|
|
Error = (Error, ServiceRequest),
|
2019-03-02 06:51:32 +00:00
|
|
|
>,
|
|
|
|
T::Future: 'static,
|
|
|
|
T::Service: 'static,
|
2019-03-09 17:49:11 +00:00
|
|
|
<T::Service as Service>::Future: 'static,
|
2019-03-02 06:51:32 +00:00
|
|
|
{
|
|
|
|
pub fn new(service: T) -> Self {
|
2019-04-13 21:50:54 +00:00
|
|
|
RouteNewService { service }
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<T> NewService for RouteNewService<T>
|
2019-03-02 06:51:32 +00:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 06:51:32 +00:00
|
|
|
Response = ServiceResponse,
|
2019-04-13 21:50:54 +00:00
|
|
|
Error = (Error, ServiceRequest),
|
2019-03-02 06:51:32 +00:00
|
|
|
>,
|
|
|
|
T::Future: 'static,
|
|
|
|
T::Service: 'static,
|
2019-03-09 17:49:11 +00:00
|
|
|
<T::Service as Service>::Future: 'static,
|
2019-03-02 06:51:32 +00:00
|
|
|
{
|
2019-05-12 15:34:51 +00:00
|
|
|
type Config = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Request = ServiceRequest;
|
2019-03-02 06:51:32 +00:00
|
|
|
type Response = ServiceResponse;
|
2019-03-10 23:35:38 +00:00
|
|
|
type Error = Error;
|
2019-03-02 06:51:32 +00:00
|
|
|
type InitError = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Service = BoxedRouteService<ServiceRequest, Self::Response>;
|
2019-03-02 06:51:32 +00:00
|
|
|
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
Box::new(
|
|
|
|
self.service
|
|
|
|
.new_service(&())
|
|
|
|
.map_err(|_| ())
|
|
|
|
.and_then(|service| {
|
|
|
|
let service: BoxedRouteService<_, _> =
|
2019-04-13 21:50:54 +00:00
|
|
|
Box::new(RouteServiceWrapper { service });
|
2019-03-02 06:51:32 +00:00
|
|
|
Ok(service)
|
|
|
|
}),
|
|
|
|
)
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
struct RouteServiceWrapper<T: Service> {
|
2019-03-02 06:51:32 +00:00
|
|
|
service: T,
|
2018-04-30 03:50:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<T> Service for RouteServiceWrapper<T>
|
2019-03-02 06:51:32 +00:00
|
|
|
where
|
|
|
|
T::Future: 'static,
|
|
|
|
T: Service<
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 06:51:32 +00:00
|
|
|
Response = ServiceResponse,
|
2019-04-13 21:50:54 +00:00
|
|
|
Error = (Error, ServiceRequest),
|
2019-03-02 06:51:32 +00:00
|
|
|
>,
|
|
|
|
{
|
2019-04-13 21:50:54 +00:00
|
|
|
type Request = ServiceRequest;
|
2019-03-02 06:51:32 +00:00
|
|
|
type Response = ServiceResponse;
|
2019-03-10 23:35:38 +00:00
|
|
|
type Error = Error;
|
2019-04-08 06:06:21 +00:00
|
|
|
type Future = Either<
|
|
|
|
FutureResult<Self::Response, Self::Error>,
|
|
|
|
Box<Future<Item = Self::Response, Error = Self::Error>>,
|
|
|
|
>;
|
2019-03-02 06:51:32 +00:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2019-03-10 23:35:38 +00:00
|
|
|
self.service.poll_ready().map_err(|(e, _)| e)
|
2018-04-30 03:50:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
2019-04-08 06:06:21 +00:00
|
|
|
let mut fut = self.service.call(req);
|
|
|
|
match fut.poll() {
|
|
|
|
Ok(Async::Ready(res)) => Either::A(ok(res)),
|
|
|
|
Err((e, req)) => Either::A(ok(req.error_response(e))),
|
|
|
|
Ok(Async::NotReady) => Either::B(Box::new(fut.then(|res| match res {
|
|
|
|
Ok(res) => Ok(res),
|
|
|
|
Err((err, req)) => Ok(req.error_response(err)),
|
|
|
|
}))),
|
|
|
|
}
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 01:33:47 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-25 21:33:34 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2019-04-22 21:22:08 +00:00
|
|
|
use bytes::Bytes;
|
2019-03-25 21:33:34 +00:00
|
|
|
use futures::Future;
|
2019-04-22 21:22:08 +00:00
|
|
|
use serde_derive::Serialize;
|
2019-03-25 21:33:34 +00:00
|
|
|
use tokio_timer::sleep;
|
|
|
|
|
2019-03-11 01:33:47 +00:00
|
|
|
use crate::http::{Method, StatusCode};
|
2019-04-22 21:22:08 +00:00
|
|
|
use crate::test::{call_service, init_service, read_body, TestRequest};
|
2019-03-25 21:33:34 +00:00
|
|
|
use crate::{error, web, App, HttpResponse};
|
2019-03-11 01:33:47 +00:00
|
|
|
|
2019-04-22 21:22:08 +00:00
|
|
|
#[derive(Serialize, PartialEq, Debug)]
|
|
|
|
struct MyObject {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
2019-03-11 01:33:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_route() {
|
2019-04-22 21:22:08 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.service(
|
2019-03-25 21:33:34 +00:00
|
|
|
web::resource("/test")
|
|
|
|
.route(web::get().to(|| HttpResponse::Ok()))
|
|
|
|
.route(web::put().to(|| {
|
|
|
|
Err::<HttpResponse, _>(error::ErrorBadRequest("err"))
|
|
|
|
}))
|
|
|
|
.route(web::post().to_async(|| {
|
|
|
|
sleep(Duration::from_millis(100))
|
|
|
|
.then(|_| HttpResponse::Created())
|
|
|
|
}))
|
|
|
|
.route(web::delete().to_async(|| {
|
|
|
|
sleep(Duration::from_millis(100)).then(|_| {
|
|
|
|
Err::<HttpResponse, _>(error::ErrorBadRequest("err"))
|
|
|
|
})
|
|
|
|
})),
|
2019-04-22 21:22:08 +00:00
|
|
|
)
|
|
|
|
.service(web::resource("/json").route(web::get().to_async(|| {
|
|
|
|
sleep(Duration::from_millis(25)).then(|_| {
|
|
|
|
Ok::<_, crate::Error>(web::Json(MyObject {
|
|
|
|
name: "test".to_string(),
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}))),
|
|
|
|
);
|
2019-03-11 01:33:47 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
2019-04-15 14:44:07 +00:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-11 01:33:47 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2019-04-15 14:44:07 +00:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-11 01:33:47 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
2019-03-25 21:33:34 +00:00
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::PUT)
|
|
|
|
.to_request();
|
2019-04-15 14:44:07 +00:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 21:33:34 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::DELETE)
|
|
|
|
.to_request();
|
2019-04-15 14:44:07 +00:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 21:33:34 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
2019-03-11 01:33:47 +00:00
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::HEAD)
|
|
|
|
.to_request();
|
2019-04-15 14:44:07 +00:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-24 23:28:16 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2019-04-22 21:22:08 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/json").to_request();
|
|
|
|
let resp = call_service(&mut srv, req);
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let body = read_body(resp);
|
|
|
|
assert_eq!(body, Bytes::from_static(b"{\"name\":\"test\"}"));
|
2019-03-11 01:33:47 +00:00
|
|
|
}
|
|
|
|
}
|