2018-01-10 04:00:18 +00:00
|
|
|
use std::mem;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use futures::{Async, Future, Poll};
|
2017-12-05 00:09:22 +00:00
|
|
|
|
|
|
|
use error::Error;
|
2017-12-11 22:16:29 +00:00
|
|
|
use pred::Predicate;
|
2018-01-10 04:00:18 +00:00
|
|
|
use handler::{Reply, ReplyItem, Handler,
|
|
|
|
Responder, RouteHandler, AsyncHandler, WrapHandler};
|
|
|
|
use middleware::{Middleware, Response as MiddlewareResponse, Started as MiddlewareStarted};
|
2018-03-02 03:12:59 +00:00
|
|
|
use httpcodes::HttpNotFound;
|
2017-12-05 00:09:22 +00:00
|
|
|
use httprequest::HttpRequest;
|
2018-01-10 04:00:18 +00:00
|
|
|
use httpresponse::HttpResponse;
|
2018-03-28 21:24:32 +00:00
|
|
|
use with::{with, with2, with3, WithHandler};
|
2018-03-27 06:10:31 +00:00
|
|
|
use extractor::HttpRequestExtractor;
|
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.
|
|
|
|
pub struct Route<S> {
|
|
|
|
preds: Vec<Box<Predicate<S>>>,
|
2018-01-10 04:00:18 +00:00
|
|
|
handler: InnerHandler<S>,
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> Default for Route<S> {
|
|
|
|
|
|
|
|
fn default() -> Route<S> {
|
|
|
|
Route {
|
|
|
|
preds: Vec::new(),
|
2018-03-02 03:12:59 +00:00
|
|
|
handler: InnerHandler::new(|_| HttpNotFound),
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> Route<S> {
|
|
|
|
|
2018-01-10 04:00:18 +00:00
|
|
|
#[inline]
|
2017-12-05 00:09:22 +00:00
|
|
|
pub(crate) fn check(&self, req: &mut HttpRequest<S>) -> bool {
|
|
|
|
for pred in &self.preds {
|
|
|
|
if !pred.check(req) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2018-01-10 04:00:18 +00:00
|
|
|
#[inline]
|
2017-12-26 17:00:45 +00:00
|
|
|
pub(crate) fn handle(&mut self, req: HttpRequest<S>) -> Reply {
|
2017-12-05 00:09:22 +00:00
|
|
|
self.handler.handle(req)
|
|
|
|
}
|
|
|
|
|
2018-01-10 04:00:18 +00:00
|
|
|
#[inline]
|
|
|
|
pub(crate) fn compose(&mut self,
|
|
|
|
req: HttpRequest<S>,
|
|
|
|
mws: Rc<Vec<Box<Middleware<S>>>>) -> Reply {
|
|
|
|
Reply::async(Compose::new(req, mws, self.handler.clone()))
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:32:31 +00:00
|
|
|
/// Add match predicate to route.
|
2017-12-20 06:36:06 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use actix_web::httpcodes::*;
|
|
|
|
/// # fn main() {
|
|
|
|
/// Application::new()
|
|
|
|
/// .resource("/path", |r|
|
|
|
|
/// r.route()
|
2018-03-02 02:32:31 +00:00
|
|
|
/// .filter(pred::Get())
|
|
|
|
/// .filter(pred::Header("content-type", "text/plain"))
|
2018-03-02 03:12:59 +00:00
|
|
|
/// .f(|req| HttpOk)
|
2017-12-20 06:36:06 +00:00
|
|
|
/// )
|
|
|
|
/// # .finish();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2018-03-02 02:32:31 +00:00
|
|
|
pub fn filter<T: Predicate<S> + 'static>(&mut self, p: T) -> &mut Self {
|
2017-12-20 21:23:50 +00:00
|
|
|
self.preds.push(Box::new(p));
|
2017-12-05 00:09:22 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-03-02 02:32:31 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[deprecated(since="0.4.1", note="please use `.filter()` instead")]
|
|
|
|
pub fn p<T: Predicate<S> + 'static>(&mut self, p: T) -> &mut Self {
|
|
|
|
self.filter(p)
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:09:22 +00:00
|
|
|
/// Set handler object. Usually call to this method is last call
|
2018-03-07 22:56:53 +00:00
|
|
|
/// during route configuration, so it does not return reference to self.
|
2017-12-05 00:09:22 +00:00
|
|
|
pub fn h<H: Handler<S>>(&mut self, handler: H) {
|
2018-01-10 04:00:18 +00:00
|
|
|
self.handler = InnerHandler::new(handler);
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set handler function. Usually call to this method is last call
|
2018-03-07 22:56:53 +00:00
|
|
|
/// during route configuration, so it does not return reference to self.
|
2017-12-05 00:09:22 +00:00
|
|
|
pub fn f<F, R>(&mut self, handler: F)
|
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-12-14 17:43:42 +00:00
|
|
|
R: Responder + 'static,
|
2017-12-05 00:09:22 +00:00
|
|
|
{
|
2018-01-10 04:00:18 +00:00
|
|
|
self.handler = InnerHandler::new(handler);
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set async handler function.
|
2017-12-20 20:51:39 +00:00
|
|
|
pub fn a<H, R, F, E>(&mut self, handler: H)
|
|
|
|
where H: Fn(HttpRequest<S>) -> F + 'static,
|
|
|
|
F: Future<Item=R, Error=E> + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static
|
2017-12-05 00:09:22 +00:00
|
|
|
{
|
2018-01-10 04:00:18 +00:00
|
|
|
self.handler = InnerHandler::async(handler);
|
|
|
|
}
|
2018-03-27 06:10:31 +00:00
|
|
|
|
|
|
|
/// Set handler function with http request extractor.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate bytes;
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// #[macro_use] extern crate serde_derive;
|
|
|
|
/// use actix_web::*;
|
2018-03-27 06:29:53 +00:00
|
|
|
/// use actix_web::Path;
|
2018-03-27 06:10:31 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2018-03-28 03:33:24 +00:00
|
|
|
/// fn index(info: Path<Info>) -> Result<String> {
|
2018-03-27 06:10:31 +00:00
|
|
|
/// Ok(format!("Welcome {}!", info.username))
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = Application::new().resource(
|
|
|
|
/// "/{username}/index.html", // <- define path parameters
|
|
|
|
/// |r| r.method(Method::GET).with(index)); // <- use `with` extractor
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 21:24:32 +00:00
|
|
|
pub fn with<T, H>(&mut self, handler: H)
|
|
|
|
where H: WithHandler<T, S>,
|
|
|
|
T: HttpRequestExtractor<S> + 'static,
|
2018-03-27 06:10:31 +00:00
|
|
|
{
|
|
|
|
self.h(with(handler))
|
|
|
|
}
|
2018-03-28 21:24:32 +00:00
|
|
|
|
|
|
|
/// Set handler function, function has to accept two request extractors.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate bytes;
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// #[macro_use] extern crate serde_derive;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// use actix_web::Path;
|
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct PParam {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct QParam {
|
|
|
|
/// count: u32,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
|
|
|
/// fn index(p: Path<PParam>, q: Query<QParam>) -> Result<String> {
|
|
|
|
/// Ok(format!("Welcome {}!", p.username))
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = Application::new().resource(
|
|
|
|
/// "/{username}/index.html", // <- define path parameters
|
|
|
|
/// |r| r.method(Method::GET).with2(index)); // <- use `with` extractor
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn with2<T1, T2, F, R>(&mut self, handler: F)
|
|
|
|
where F: Fn(T1, T2) -> R + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
T1: HttpRequestExtractor<S> + 'static,
|
|
|
|
T2: HttpRequestExtractor<S> + 'static,
|
|
|
|
{
|
|
|
|
self.h(with2(handler))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with3<T1, T2, T3, F, R>(&mut self, handler: F)
|
|
|
|
where F: Fn(T1, T2, T3) -> R + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
T1: HttpRequestExtractor<S> + 'static,
|
|
|
|
T2: HttpRequestExtractor<S> + 'static,
|
|
|
|
T3: HttpRequestExtractor<S> + 'static,
|
|
|
|
{
|
|
|
|
self.h(with3(handler))
|
|
|
|
}
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 18:12:34 +00:00
|
|
|
/// `RouteHandler` wrapper. This struct is required because it needs to be shared
|
2018-01-10 04:00:18 +00:00
|
|
|
/// for resource level middlewares.
|
|
|
|
struct InnerHandler<S>(Rc<Box<RouteHandler<S>>>);
|
|
|
|
|
|
|
|
impl<S: 'static> InnerHandler<S> {
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn new<H: Handler<S>>(h: H) -> Self {
|
|
|
|
InnerHandler(Rc::new(Box::new(WrapHandler::new(h))))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn async<H, R, F, E>(h: H) -> Self
|
|
|
|
where H: Fn(HttpRequest<S>) -> F + 'static,
|
|
|
|
F: Future<Item=R, Error=E> + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static
|
|
|
|
{
|
|
|
|
InnerHandler(Rc::new(Box::new(AsyncHandler::new(h))))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn handle(&self, req: HttpRequest<S>) -> Reply {
|
|
|
|
// reason: handler is unique per thread,
|
2018-03-07 22:56:53 +00:00
|
|
|
// handler get called from async code only
|
2018-01-10 04:00:18 +00:00
|
|
|
#[allow(mutable_transmutes)]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
|
|
|
|
let h: &mut Box<RouteHandler<S>> = unsafe { mem::transmute(self.0.as_ref()) };
|
|
|
|
h.handle(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> Clone for InnerHandler<S> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
InnerHandler(Rc::clone(&self.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Compose resource level middlewares with route handler.
|
|
|
|
struct Compose<S: 'static> {
|
|
|
|
info: ComposeInfo<S>,
|
|
|
|
state: ComposeState<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ComposeInfo<S: 'static> {
|
|
|
|
count: usize,
|
|
|
|
req: HttpRequest<S>,
|
|
|
|
mws: Rc<Vec<Box<Middleware<S>>>>,
|
|
|
|
handler: InnerHandler<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ComposeState<S: 'static> {
|
|
|
|
Starting(StartMiddlewares<S>),
|
|
|
|
Handler(WaitingResponse<S>),
|
|
|
|
RunMiddlewares(RunMiddlewares<S>),
|
|
|
|
Response(Response<S>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> ComposeState<S> {
|
|
|
|
fn poll(&mut self, info: &mut ComposeInfo<S>) -> Option<ComposeState<S>> {
|
|
|
|
match *self {
|
|
|
|
ComposeState::Starting(ref mut state) => state.poll(info),
|
|
|
|
ComposeState::Handler(ref mut state) => state.poll(info),
|
|
|
|
ComposeState::RunMiddlewares(ref mut state) => state.poll(info),
|
|
|
|
ComposeState::Response(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> Compose<S> {
|
|
|
|
fn new(req: HttpRequest<S>,
|
|
|
|
mws: Rc<Vec<Box<Middleware<S>>>>,
|
|
|
|
handler: InnerHandler<S>) -> Self
|
|
|
|
{
|
2018-02-26 22:33:56 +00:00
|
|
|
let mut info = ComposeInfo { count: 0, req, mws, handler };
|
2018-01-10 04:00:18 +00:00
|
|
|
let state = StartMiddlewares::init(&mut info);
|
|
|
|
|
2018-02-26 22:33:56 +00:00
|
|
|
Compose {state, info}
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> Future for Compose<S> {
|
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
loop {
|
|
|
|
if let ComposeState::Response(ref mut resp) = self.state {
|
|
|
|
let resp = resp.resp.take().unwrap();
|
|
|
|
return Ok(Async::Ready(resp))
|
|
|
|
}
|
|
|
|
if let Some(state) = self.state.poll(&mut self.info) {
|
|
|
|
self.state = state;
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Middlewares start executor
|
|
|
|
struct StartMiddlewares<S> {
|
|
|
|
fut: Option<Fut>,
|
|
|
|
_s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
type Fut = Box<Future<Item=Option<HttpResponse>, Error=Error>>;
|
|
|
|
|
|
|
|
impl<S: 'static> StartMiddlewares<S> {
|
|
|
|
|
|
|
|
fn init(info: &mut ComposeInfo<S>) -> ComposeState<S> {
|
|
|
|
let len = info.mws.len();
|
|
|
|
loop {
|
|
|
|
if info.count == len {
|
|
|
|
let reply = info.handler.handle(info.req.clone());
|
|
|
|
return WaitingResponse::init(info, reply)
|
|
|
|
} else {
|
|
|
|
match info.mws[info.count].start(&mut info.req) {
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareStarted::Done) =>
|
2018-01-10 04:00:18 +00:00
|
|
|
info.count += 1,
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareStarted::Response(resp)) =>
|
2018-01-10 04:00:18 +00:00
|
|
|
return RunMiddlewares::init(info, resp),
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareStarted::Future(mut fut)) =>
|
2018-01-10 04:00:18 +00:00
|
|
|
match fut.poll() {
|
|
|
|
Ok(Async::NotReady) =>
|
|
|
|
return ComposeState::Starting(StartMiddlewares {
|
|
|
|
fut: Some(fut),
|
|
|
|
_s: PhantomData}),
|
|
|
|
Ok(Async::Ready(resp)) => {
|
|
|
|
if let Some(resp) = resp {
|
|
|
|
return RunMiddlewares::init(info, resp);
|
|
|
|
}
|
|
|
|
info.count += 1;
|
|
|
|
}
|
|
|
|
Err(err) =>
|
|
|
|
return Response::init(err.into()),
|
|
|
|
},
|
2018-01-10 06:48:35 +00:00
|
|
|
Err(err) =>
|
2018-01-10 04:00:18 +00:00
|
|
|
return Response::init(err.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(&mut self, info: &mut ComposeInfo<S>) -> Option<ComposeState<S>>
|
|
|
|
{
|
|
|
|
let len = info.mws.len();
|
|
|
|
'outer: loop {
|
|
|
|
match self.fut.as_mut().unwrap().poll() {
|
|
|
|
Ok(Async::NotReady) =>
|
|
|
|
return None,
|
|
|
|
Ok(Async::Ready(resp)) => {
|
|
|
|
info.count += 1;
|
|
|
|
if let Some(resp) = resp {
|
|
|
|
return Some(RunMiddlewares::init(info, resp));
|
|
|
|
}
|
|
|
|
if info.count == len {
|
|
|
|
let reply = info.handler.handle(info.req.clone());
|
|
|
|
return Some(WaitingResponse::init(info, reply));
|
|
|
|
} else {
|
|
|
|
loop {
|
|
|
|
match info.mws[info.count].start(&mut info.req) {
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareStarted::Done) =>
|
2018-01-10 04:00:18 +00:00
|
|
|
info.count += 1,
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareStarted::Response(resp)) => {
|
2018-01-10 04:00:18 +00:00
|
|
|
return Some(RunMiddlewares::init(info, resp));
|
|
|
|
},
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareStarted::Future(fut)) => {
|
2018-01-10 04:00:18 +00:00
|
|
|
self.fut = Some(fut);
|
|
|
|
continue 'outer
|
|
|
|
},
|
2018-01-10 06:48:35 +00:00
|
|
|
Err(err) =>
|
2018-01-10 04:00:18 +00:00
|
|
|
return Some(Response::init(err.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) =>
|
|
|
|
return Some(Response::init(err.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// waiting for response
|
|
|
|
struct WaitingResponse<S> {
|
|
|
|
fut: Box<Future<Item=HttpResponse, Error=Error>>,
|
|
|
|
_s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> WaitingResponse<S> {
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn init(info: &mut ComposeInfo<S>, reply: Reply) -> ComposeState<S> {
|
|
|
|
match reply.into() {
|
|
|
|
ReplyItem::Message(resp) =>
|
|
|
|
RunMiddlewares::init(info, resp),
|
|
|
|
ReplyItem::Future(fut) =>
|
|
|
|
ComposeState::Handler(
|
2018-02-26 22:33:56 +00:00
|
|
|
WaitingResponse { fut, _s: PhantomData }),
|
2018-01-10 04:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(&mut self, info: &mut ComposeInfo<S>) -> Option<ComposeState<S>> {
|
|
|
|
match self.fut.poll() {
|
|
|
|
Ok(Async::NotReady) => None,
|
|
|
|
Ok(Async::Ready(response)) =>
|
|
|
|
Some(RunMiddlewares::init(info, response)),
|
|
|
|
Err(err) =>
|
|
|
|
Some(Response::init(err.into())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Middlewares response executor
|
|
|
|
struct RunMiddlewares<S> {
|
|
|
|
curr: usize,
|
|
|
|
fut: Option<Box<Future<Item=HttpResponse, Error=Error>>>,
|
|
|
|
_s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> RunMiddlewares<S> {
|
|
|
|
|
|
|
|
fn init(info: &mut ComposeInfo<S>, mut resp: HttpResponse) -> ComposeState<S> {
|
|
|
|
let mut curr = 0;
|
|
|
|
let len = info.mws.len();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
resp = match info.mws[curr].response(&mut info.req, resp) {
|
2018-01-10 06:48:35 +00:00
|
|
|
Err(err) => {
|
2018-01-10 04:00:18 +00:00
|
|
|
info.count = curr + 1;
|
|
|
|
return Response::init(err.into())
|
|
|
|
},
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareResponse::Done(r)) => {
|
2018-01-10 04:00:18 +00:00
|
|
|
curr += 1;
|
|
|
|
if curr == len {
|
|
|
|
return Response::init(r)
|
|
|
|
} else {
|
|
|
|
r
|
|
|
|
}
|
|
|
|
},
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareResponse::Future(fut)) => {
|
2018-01-10 04:00:18 +00:00
|
|
|
return ComposeState::RunMiddlewares(
|
2018-02-26 22:33:56 +00:00
|
|
|
RunMiddlewares { curr, fut: Some(fut), _s: PhantomData })
|
2018-01-10 04:00:18 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(&mut self, info: &mut ComposeInfo<S>) -> Option<ComposeState<S>>
|
|
|
|
{
|
|
|
|
let len = info.mws.len();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// poll latest fut
|
|
|
|
let mut resp = match self.fut.as_mut().unwrap().poll() {
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(resp)) => {
|
|
|
|
self.curr += 1;
|
|
|
|
resp
|
|
|
|
}
|
|
|
|
Err(err) =>
|
|
|
|
return Some(Response::init(err.into())),
|
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if self.curr == len {
|
|
|
|
return Some(Response::init(resp));
|
|
|
|
} else {
|
|
|
|
match info.mws[self.curr].response(&mut info.req, resp) {
|
2018-01-10 06:48:35 +00:00
|
|
|
Err(err) =>
|
2018-01-10 04:00:18 +00:00
|
|
|
return Some(Response::init(err.into())),
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareResponse::Done(r)) => {
|
2018-01-10 04:00:18 +00:00
|
|
|
self.curr += 1;
|
|
|
|
resp = r
|
|
|
|
},
|
2018-01-10 06:48:35 +00:00
|
|
|
Ok(MiddlewareResponse::Future(fut)) => {
|
2018-01-10 04:00:18 +00:00
|
|
|
self.fut = Some(fut);
|
|
|
|
break
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Response<S> {
|
|
|
|
resp: Option<HttpResponse>,
|
|
|
|
_s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> Response<S> {
|
|
|
|
|
|
|
|
fn init(resp: HttpResponse) -> ComposeState<S> {
|
|
|
|
ComposeState::Response(
|
|
|
|
Response{resp: Some(resp), _s: PhantomData})
|
2017-12-05 00:09:22 +00:00
|
|
|
}
|
|
|
|
}
|