2018-04-14 01:02:01 +02:00
|
|
|
use std::marker::PhantomData;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_http::{Error, Response};
|
|
|
|
use actix_service::{NewService, Service};
|
|
|
|
use actix_utils::Never;
|
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
2018-05-02 02:30:06 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::request::HttpRequest;
|
|
|
|
use crate::responder::Responder;
|
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
2017-11-03 21:35:34 +01:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Trait implemented by types that can be extracted from request.
|
2017-12-14 18:43:42 +01:00
|
|
|
///
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Types that implement this trait can be used with `Route` handlers.
|
|
|
|
pub trait FromRequest<P>: Sized {
|
2017-12-03 23:22:04 +01:00
|
|
|
/// The associated error which can be returned.
|
|
|
|
type Error: Into<Error>;
|
|
|
|
|
2018-04-04 07:06:18 +02:00
|
|
|
/// Future that resolves to a Self
|
2019-03-02 07:51:32 +01:00
|
|
|
type Future: Future<Item = Self, Error = Self::Error>;
|
2018-05-02 15:07:30 +02:00
|
|
|
|
|
|
|
/// Convert request to a Self
|
2019-03-02 07:51:32 +01:00
|
|
|
fn from_request(req: &mut ServiceRequest<P>) -> Self::Future;
|
2018-03-29 22:12:28 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Handler converter factory
|
|
|
|
pub trait Factory<T, R>: Clone
|
|
|
|
where
|
|
|
|
R: Responder,
|
|
|
|
{
|
|
|
|
fn call(&self, param: T) -> R;
|
2018-03-10 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, R> Factory<(), R> for F
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: Fn() -> R + Clone + 'static,
|
|
|
|
R: Responder + 'static,
|
2018-03-10 18:39:43 +01:00
|
|
|
{
|
2019-03-02 07:51:32 +01:00
|
|
|
fn call(&self, _: ()) -> R {
|
|
|
|
(self)()
|
2018-03-10 18:39:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct Handle<F, T, R>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: Factory<T, R>,
|
|
|
|
R: Responder,
|
2018-04-03 01:19:18 +02:00
|
|
|
{
|
2019-03-02 07:51:32 +01:00
|
|
|
hnd: F,
|
|
|
|
_t: PhantomData<(T, R)>,
|
2018-04-03 01:19:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, T, R> Handle<F, T, R>
|
2018-06-11 13:05:41 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: Factory<T, R>,
|
|
|
|
R: Responder,
|
2018-06-11 13:05:41 +02:00
|
|
|
{
|
2019-03-02 07:51:32 +01:00
|
|
|
pub fn new(hnd: F) -> Self {
|
|
|
|
Handle {
|
|
|
|
hnd,
|
|
|
|
_t: PhantomData,
|
2018-06-11 13:05:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, T, R> NewService for Handle<F, T, R>
|
|
|
|
where
|
|
|
|
F: Factory<T, R>,
|
|
|
|
R: Responder + 'static,
|
|
|
|
{
|
|
|
|
type Request = (T, HttpRequest);
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = Never;
|
|
|
|
type InitError = ();
|
|
|
|
type Service = HandleService<F, T, R>;
|
|
|
|
type Future = FutureResult<Self::Service, ()>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
ok(HandleService {
|
|
|
|
hnd: self.hnd.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
2017-12-21 05:30:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct HandleService<F, T, R>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: Factory<T, R>,
|
|
|
|
R: Responder + 'static,
|
2017-12-21 05:30:54 +01:00
|
|
|
{
|
2019-03-02 07:51:32 +01:00
|
|
|
hnd: F,
|
|
|
|
_t: PhantomData<(T, R)>,
|
2017-12-21 05:30:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, T, R> Service for HandleService<F, T, R>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: Factory<T, R>,
|
2018-04-14 01:02:01 +02:00
|
|
|
R: Responder + 'static,
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
2019-03-02 07:51:32 +01:00
|
|
|
type Request = (T, HttpRequest);
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = Never;
|
|
|
|
type Future = HandleServiceResponse<R::Future>;
|
2017-10-15 23:17:41 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
|
|
|
|
let fut = self.hnd.call(param).respond_to(&req);
|
|
|
|
HandleServiceResponse {
|
|
|
|
fut,
|
|
|
|
req: Some(req),
|
2018-05-02 02:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
pub struct HandleServiceResponse<T> {
|
|
|
|
fut: T,
|
|
|
|
req: Option<HttpRequest>,
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<T> Future for HandleServiceResponse<T>
|
|
|
|
where
|
|
|
|
T: Future<Item = Response>,
|
|
|
|
T::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
type Item = ServiceResponse;
|
|
|
|
type Error = Never;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll() {
|
|
|
|
Ok(Async::Ready(res)) => Ok(Async::Ready(ServiceResponse::new(
|
|
|
|
self.req.take().unwrap(),
|
|
|
|
res,
|
|
|
|
))),
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Err(e) => {
|
|
|
|
let res: Response = e.into().into();
|
|
|
|
Ok(Async::Ready(ServiceResponse::new(
|
|
|
|
self.req.take().unwrap(),
|
|
|
|
res,
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2017-11-29 04:49:17 +01:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Async handler converter factory
|
|
|
|
pub trait AsyncFactory<T, R>: Clone + 'static
|
|
|
|
where
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
fn call(&self, param: T) -> R;
|
|
|
|
}
|
2018-05-02 02:19:15 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, R> AsyncFactory<(), R> for F
|
|
|
|
where
|
|
|
|
F: Fn() -> R + Clone + 'static,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
fn call(&self, _: ()) -> R {
|
|
|
|
(self)()
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2017-12-09 22:25:06 +01:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct AsyncHandle<F, T, R>
|
|
|
|
where
|
|
|
|
F: AsyncFactory<T, R>,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
hnd: F,
|
|
|
|
_t: PhantomData<(T, R)>,
|
|
|
|
}
|
2018-05-02 02:19:15 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, T, R> AsyncHandle<F, T, R>
|
|
|
|
where
|
|
|
|
F: AsyncFactory<T, R>,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
pub fn new(hnd: F) -> Self {
|
|
|
|
AsyncHandle {
|
|
|
|
hnd,
|
|
|
|
_t: PhantomData,
|
2017-12-09 22:25:06 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, T, R> NewService for AsyncHandle<F, T, R>
|
|
|
|
where
|
|
|
|
F: AsyncFactory<T, R>,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
type Request = (T, HttpRequest);
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = AsyncHandleService<F, T, R>;
|
|
|
|
type Future = FutureResult<Self::Service, ()>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
ok(AsyncHandleService {
|
|
|
|
hnd: self.hnd.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
})
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct AsyncHandleService<F, T, R>
|
|
|
|
where
|
|
|
|
F: AsyncFactory<T, R>,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
hnd: F,
|
|
|
|
_t: PhantomData<(T, R)>,
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
2017-11-29 19:31:24 +01:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<F, T, R> Service for AsyncHandleService<F, T, R>
|
|
|
|
where
|
|
|
|
F: AsyncFactory<T, R>,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
type Request = (T, HttpRequest);
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type Future = AsyncHandleServiceResponse<R::Future>;
|
2017-12-02 06:29:22 +01:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
|
|
|
|
AsyncHandleServiceResponse {
|
|
|
|
fut: self.hnd.call(param).into_future(),
|
|
|
|
req: Some(req),
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct AsyncHandleServiceResponse<T> {
|
|
|
|
fut: T,
|
|
|
|
req: Option<HttpRequest>,
|
2018-05-02 02:19:15 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<T> Future for AsyncHandleServiceResponse<T>
|
2018-08-23 18:48:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
T: Future,
|
|
|
|
T::Item: Into<Response>,
|
|
|
|
T::Error: Into<Error>,
|
2018-05-02 02:19:15 +02:00
|
|
|
{
|
2019-03-02 07:51:32 +01:00
|
|
|
type Item = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll() {
|
|
|
|
Ok(Async::Ready(res)) => Ok(Async::Ready(ServiceResponse::new(
|
|
|
|
self.req.take().unwrap(),
|
|
|
|
res.into(),
|
|
|
|
))),
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Err(e) => {
|
|
|
|
let res: Response = e.into().into();
|
|
|
|
Ok(Async::Ready(ServiceResponse::new(
|
|
|
|
self.req.take().unwrap(),
|
|
|
|
res,
|
|
|
|
)))
|
|
|
|
}
|
2018-01-03 08:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Extract arguments from request
|
|
|
|
pub struct Extract<P, T: FromRequest<P>> {
|
|
|
|
_t: PhantomData<(P, T)>,
|
2018-01-03 08:43:17 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P, T: FromRequest<P>> Extract<P, T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Extract { _t: PhantomData }
|
2017-12-01 00:13:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P, T: FromRequest<P>> Default for Extract<P, T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
2018-06-21 10:17:27 +02:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2018-06-21 10:17:27 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P, T: FromRequest<P>> NewService for Extract<P, T> {
|
|
|
|
type Request = ServiceRequest<P>;
|
|
|
|
type Response = (T, HttpRequest);
|
|
|
|
type Error = (Error, ServiceRequest<P>);
|
|
|
|
type InitError = ();
|
|
|
|
type Service = ExtractService<P, T>;
|
|
|
|
type Future = FutureResult<Self::Service, ()>;
|
2018-07-15 11:12:21 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
ok(ExtractService { _t: PhantomData })
|
|
|
|
}
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
pub struct ExtractService<P, T: FromRequest<P>> {
|
|
|
|
_t: PhantomData<(P, T)>,
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P, T: FromRequest<P>> Service for ExtractService<P, T> {
|
|
|
|
type Request = ServiceRequest<P>;
|
|
|
|
type Response = (T, HttpRequest);
|
|
|
|
type Error = (Error, ServiceRequest<P>);
|
|
|
|
type Future = ExtractResponse<P, T>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn call(&mut self, mut req: ServiceRequest<P>) -> Self::Future {
|
|
|
|
ExtractResponse {
|
|
|
|
fut: T::from_request(&mut req),
|
|
|
|
req: Some(req),
|
2017-12-03 23:22:04 +01:00
|
|
|
}
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
pub struct ExtractResponse<P, T: FromRequest<P>> {
|
|
|
|
req: Option<ServiceRequest<P>>,
|
|
|
|
fut: T::Future,
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P, T: FromRequest<P>> Future for ExtractResponse<P, T> {
|
|
|
|
type Item = (T, HttpRequest);
|
|
|
|
type Error = (Error, ServiceRequest<P>);
|
2017-11-29 22:26:55 +01:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let item = try_ready!(self
|
|
|
|
.fut
|
|
|
|
.poll()
|
|
|
|
.map_err(|e| (e.into(), self.req.take().unwrap())));
|
2018-03-30 00:41:13 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
let req = self.req.take().unwrap();
|
|
|
|
let req = req.into_request();
|
|
|
|
|
|
|
|
Ok(Async::Ready((item, req)))
|
2018-03-30 00:41:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// FromRequest trait impl for tuples
|
|
|
|
macro_rules! factory_tuple ({ $(($n:tt, $T:ident)),+} => {
|
|
|
|
impl<Func, $($T,)+ Res> Factory<($($T,)+), Res> for Func
|
|
|
|
where Func: Fn($($T,)+) -> Res + Clone + 'static,
|
|
|
|
//$($T,)+
|
|
|
|
Res: Responder + 'static,
|
|
|
|
{
|
|
|
|
fn call(&self, param: ($($T,)+)) -> Res {
|
|
|
|
(self)($(param.$n,)+)
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 00:41:13 +02:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<Func, $($T,)+ Res> AsyncFactory<($($T,)+), Res> for Func
|
|
|
|
where Func: Fn($($T,)+) -> Res + Clone + 'static,
|
|
|
|
Res: IntoFuture + 'static,
|
|
|
|
Res::Item: Into<Response>,
|
|
|
|
Res::Error: Into<Error>,
|
|
|
|
{
|
|
|
|
fn call(&self, param: ($($T,)+)) -> Res {
|
|
|
|
(self)($(param.$n,)+)
|
|
|
|
}
|
2018-03-30 00:41:13 +02:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
mod m {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
factory_tuple!((0, A));
|
|
|
|
factory_tuple!((0, A), (1, B));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C), (3, D));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I));
|
|
|
|
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I), (9, J));
|
2018-03-30 00:41:13 +02:00
|
|
|
}
|