1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-30 18:42:52 +01:00
actix-web/src/handler.rs

393 lines
9.4 KiB
Rust
Raw Normal View History

2019-03-03 00:57:48 -08:00
use std::cell::RefCell;
2018-04-13 16:02:01 -07:00
use std::marker::PhantomData;
2019-03-03 00:57:48 -08:00
use std::rc::Rc;
2017-10-06 21:48:14 -07:00
2019-03-03 00:57:48 -08:00
use actix_http::{Error, Extensions, Response};
2019-03-02 13:57:00 -08:00
use actix_service::{NewService, Service, Void};
2019-03-01 22:51:32 -08:00
use futures::future::{ok, FutureResult};
use futures::{try_ready, Async, Future, IntoFuture, Poll};
2018-05-01 17:30:06 -07:00
use crate::extract::FromRequest;
2019-03-01 22:51:32 -08:00
use crate::request::HttpRequest;
use crate::responder::Responder;
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
2017-11-03 13:35:34 -07:00
2019-03-01 22:51:32 -08:00
/// Handler converter factory
pub trait Factory<T, R>: Clone
where
R: Responder,
{
fn call(&self, param: T) -> R;
2018-03-10 09:39:43 -08:00
}
2019-03-01 22:51:32 -08:00
impl<F, R> Factory<(), R> for F
2018-04-13 16:02:01 -07:00
where
F: Fn() -> R + Clone,
R: Responder,
2018-03-10 09:39:43 -08:00
{
2019-03-01 22:51:32 -08:00
fn call(&self, _: ()) -> R {
(self)()
2018-03-10 09:39:43 -08:00
}
}
2019-03-01 22:51:32 -08:00
#[doc(hidden)]
pub struct Handler<F, T, R>
2018-04-13 16:02:01 -07:00
where
2019-03-01 22:51:32 -08:00
F: Factory<T, R>,
R: Responder,
2018-04-02 16:19:18 -07:00
{
2019-03-01 22:51:32 -08:00
hnd: F,
_t: PhantomData<(T, R)>,
2018-04-02 16:19:18 -07:00
}
impl<F, T, R> Handler<F, T, R>
where
2019-03-01 22:51:32 -08:00
F: Factory<T, R>,
R: Responder,
{
2019-03-01 22:51:32 -08:00
pub fn new(hnd: F) -> Self {
Handler {
2019-03-01 22:51:32 -08:00
hnd,
_t: PhantomData,
}
}
}
impl<F, T, R> NewService for Handler<F, T, R>
2019-03-01 22:51:32 -08:00
where
F: Factory<T, R>,
R: Responder,
2019-03-01 22:51:32 -08:00
{
type Request = (T, HttpRequest);
2019-03-01 22:51:32 -08:00
type Response = ServiceResponse;
2019-03-02 13:57:00 -08:00
type Error = Void;
2019-03-01 22:51:32 -08:00
type InitError = ();
type Service = HandlerService<F, T, R>;
2019-03-01 22:51:32 -08:00
type Future = FutureResult<Self::Service, ()>;
fn new_service(&self, _: &()) -> Self::Future {
ok(HandlerService {
2019-03-01 22:51:32 -08:00
hnd: self.hnd.clone(),
_t: PhantomData,
})
}
2017-12-20 20:30:54 -08:00
}
2019-03-01 22:51:32 -08:00
#[doc(hidden)]
pub struct HandlerService<F, T, R>
2018-04-13 16:02:01 -07:00
where
2019-03-01 22:51:32 -08:00
F: Factory<T, R>,
R: Responder,
2017-12-20 20:30:54 -08:00
{
2019-03-01 22:51:32 -08:00
hnd: F,
_t: PhantomData<(T, R)>,
2017-12-20 20:30:54 -08:00
}
impl<F, T, R> Service for HandlerService<F, T, R>
2018-04-13 16:02:01 -07:00
where
2019-03-01 22:51:32 -08:00
F: Factory<T, R>,
R: Responder,
2017-10-15 14:17:41 -07:00
{
type Request = (T, HttpRequest);
2019-03-01 22:51:32 -08:00
type Response = ServiceResponse;
2019-03-02 13:57:00 -08:00
type Error = Void;
type Future = HandlerServiceResponse<<R::Future as IntoFuture>::Future>;
2017-10-15 14:17:41 -07:00
2019-03-01 22:51:32 -08:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
2017-10-15 14:17:41 -07:00
}
2019-03-01 22:51:32 -08:00
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
let fut = self.hnd.call(param).respond_to(&req).into_future();
HandlerServiceResponse {
2019-03-01 22:51:32 -08:00
fut,
req: Some(req),
2018-05-01 17:30:06 -07:00
}
}
}
pub struct HandlerServiceResponse<T> {
2019-03-01 22:51:32 -08:00
fut: T,
req: Option<HttpRequest>,
2017-11-28 19:49:17 -08:00
}
impl<T> Future for HandlerServiceResponse<T>
2019-03-01 22:51:32 -08:00
where
T: Future<Item = Response>,
T::Error: Into<Error>,
{
type Item = ServiceResponse;
2019-03-02 13:57:00 -08:00
type Error = Void;
2019-03-01 22:51:32 -08:00
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-28 19:49:17 -08:00
}
2019-03-01 22:51:32 -08:00
}
2017-11-28 19:49:17 -08:00
2019-03-01 22:51:32 -08: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-01 17:19:15 -07:00
2019-03-01 22:51:32 -08: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-28 19:49:17 -08:00
}
2019-03-01 22:51:32 -08:00
}
2017-12-09 13:25:06 -08:00
2019-03-01 22:51:32 -08:00
#[doc(hidden)]
pub struct AsyncHandler<F, T, R>
2019-03-01 22:51:32 -08:00
where
F: AsyncFactory<T, R>,
R: IntoFuture,
R::Item: Into<Response>,
R::Error: Into<Error>,
{
hnd: F,
_t: PhantomData<(T, R)>,
}
2018-05-01 17:19:15 -07:00
impl<F, T, R> AsyncHandler<F, T, R>
2019-03-01 22:51:32 -08:00
where
F: AsyncFactory<T, R>,
R: IntoFuture,
R::Item: Into<Response>,
R::Error: Into<Error>,
{
pub fn new(hnd: F) -> Self {
AsyncHandler {
2019-03-01 22:51:32 -08:00
hnd,
_t: PhantomData,
2017-12-09 13:25:06 -08:00
}
}
2017-11-28 19:49:17 -08:00
}
impl<F, T, R> NewService for AsyncHandler<F, T, R>
2019-03-01 22:51:32 -08:00
where
F: AsyncFactory<T, R>,
R: IntoFuture,
R::Item: Into<Response>,
R::Error: Into<Error>,
{
type Request = (T, HttpRequest);
2019-03-01 22:51:32 -08:00
type Response = ServiceResponse;
type Error = Error;
2019-03-01 22:51:32 -08:00
type InitError = ();
type Service = AsyncHandlerService<F, T, R>;
2019-03-01 22:51:32 -08:00
type Future = FutureResult<Self::Service, ()>;
fn new_service(&self, _: &()) -> Self::Future {
ok(AsyncHandlerService {
2019-03-01 22:51:32 -08:00
hnd: self.hnd.clone(),
_t: PhantomData,
})
}
}
2019-03-01 22:51:32 -08:00
#[doc(hidden)]
pub struct AsyncHandlerService<F, T, R>
2019-03-01 22:51:32 -08:00
where
F: AsyncFactory<T, R>,
R: IntoFuture,
R::Item: Into<Response>,
R::Error: Into<Error>,
{
hnd: F,
_t: PhantomData<(T, R)>,
2017-10-15 14:17:41 -07:00
}
2017-11-29 10:31:24 -08:00
impl<F, T, R> Service for AsyncHandlerService<F, T, R>
2019-03-01 22:51:32 -08:00
where
F: AsyncFactory<T, R>,
R: IntoFuture,
R::Item: Into<Response>,
R::Error: Into<Error>,
{
type Request = (T, HttpRequest);
2019-03-01 22:51:32 -08:00
type Response = ServiceResponse;
type Error = Error;
type Future = AsyncHandlerServiceResponse<R::Future>;
2017-12-01 21:29:22 -08:00
2019-03-01 22:51:32 -08:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
2019-03-01 22:51:32 -08:00
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
AsyncHandlerServiceResponse {
2019-03-01 22:51:32 -08:00
fut: self.hnd.call(param).into_future(),
req: Some(req),
}
}
}
2019-03-01 22:51:32 -08:00
#[doc(hidden)]
pub struct AsyncHandlerServiceResponse<T> {
2019-03-01 22:51:32 -08:00
fut: T,
req: Option<HttpRequest>,
2018-05-01 17:19:15 -07:00
}
impl<T> Future for AsyncHandlerServiceResponse<T>
2018-08-23 09:48:01 -07:00
where
2019-03-01 22:51:32 -08:00
T: Future,
T::Item: Into<Response>,
T::Error: Into<Error>,
2018-05-01 17:19:15 -07:00
{
2019-03-01 22:51:32 -08:00
type Item = ServiceResponse;
type Error = Error;
2019-03-01 22:51:32 -08:00
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,
)))
}
}
}
}
2019-03-01 22:51:32 -08:00
/// Extract arguments from request
pub struct Extract<P, T: FromRequest<P>> {
2019-03-03 00:57:48 -08:00
config: Rc<RefCell<Option<Rc<Extensions>>>>,
2019-03-01 22:51:32 -08:00
_t: PhantomData<(P, T)>,
}
2019-03-01 22:51:32 -08:00
impl<P, T: FromRequest<P>> Extract<P, T> {
2019-03-03 00:57:48 -08:00
pub fn new(config: Rc<RefCell<Option<Rc<Extensions>>>>) -> Self {
Extract {
config,
_t: PhantomData,
}
}
2019-03-01 22:51:32 -08:00
}
impl<P, T: FromRequest<P>> NewService for Extract<P, T> {
type Request = ServiceRequest<P>;
2019-03-01 22:51:32 -08:00
type Response = (T, HttpRequest);
type Error = (Error, ServiceFromRequest<P>);
2019-03-01 22:51:32 -08:00
type InitError = ();
type Service = ExtractService<P, T>;
type Future = FutureResult<Self::Service, ()>;
2018-07-15 15:12:21 +06:00
2019-03-01 22:51:32 -08:00
fn new_service(&self, _: &()) -> Self::Future {
2019-03-03 00:57:48 -08:00
ok(ExtractService {
_t: PhantomData,
config: self.config.borrow().clone(),
})
2019-03-01 22:51:32 -08:00
}
2017-11-29 13:26:55 -08:00
}
2019-03-01 22:51:32 -08:00
pub struct ExtractService<P, T: FromRequest<P>> {
2019-03-03 00:57:48 -08:00
config: Option<Rc<Extensions>>,
2019-03-01 22:51:32 -08:00
_t: PhantomData<(P, T)>,
2017-11-29 13:26:55 -08:00
}
impl<P, T: FromRequest<P>> Service for ExtractService<P, T> {
type Request = ServiceRequest<P>;
2019-03-01 22:51:32 -08:00
type Response = (T, HttpRequest);
type Error = (Error, ServiceFromRequest<P>);
2019-03-01 22:51:32 -08:00
type Future = ExtractResponse<P, T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
2017-11-29 13:26:55 -08:00
}
fn call(&mut self, req: ServiceRequest<P>) -> Self::Future {
2019-03-03 00:57:48 -08:00
let mut req = ServiceFromRequest::new(req, self.config.clone());
2019-03-01 22:51:32 -08:00
ExtractResponse {
fut: T::from_request(&mut req).into_future(),
2019-03-01 22:51:32 -08:00
req: Some(req),
}
2017-11-29 13:26:55 -08:00
}
}
2019-03-01 22:51:32 -08:00
pub struct ExtractResponse<P, T: FromRequest<P>> {
req: Option<ServiceFromRequest<P>>,
fut: <T::Future as IntoFuture>::Future,
2017-11-29 13:26:55 -08:00
}
2019-03-01 22:51:32 -08:00
impl<P, T: FromRequest<P>> Future for ExtractResponse<P, T> {
type Item = (T, HttpRequest);
type Error = (Error, ServiceFromRequest<P>);
2017-11-29 13:26:55 -08:00
2019-03-01 22:51:32 -08: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-29 15:41:13 -07:00
2019-03-01 22:51:32 -08:00
let req = self.req.take().unwrap();
let req = req.into_request();
Ok(Async::Ready((item, req)))
2018-03-29 15:41:13 -07:00
}
}
2019-03-01 22:51:32 -08: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,
Res: Responder,
2019-03-01 22:51:32 -08:00
{
fn call(&self, param: ($($T,)+)) -> Res {
(self)($(param.$n,)+)
}
}
2018-03-29 15:41:13 -07:00
2019-03-01 22:51:32 -08:00
impl<Func, $($T,)+ Res> AsyncFactory<($($T,)+), Res> for Func
where Func: Fn($($T,)+) -> Res + Clone + 'static,
Res: IntoFuture,
2019-03-01 22:51:32 -08:00
Res::Item: Into<Response>,
Res::Error: Into<Error>,
{
fn call(&self, param: ($($T,)+)) -> Res {
(self)($(param.$n,)+)
}
2018-03-29 15:41:13 -07:00
}
2019-03-01 22:51:32 -08: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-29 15:41:13 -07:00
}