2017-11-10 07:08:54 +01:00
|
|
|
//! Middlewares
|
2017-11-25 07:19:06 +01:00
|
|
|
use futures::Future;
|
2017-11-25 07:15:52 +01:00
|
|
|
|
2018-01-10 07:48:35 +01:00
|
|
|
use error::{Error, Result};
|
2017-11-10 07:08:54 +01:00
|
|
|
use httprequest::HttpRequest;
|
|
|
|
use httpresponse::HttpResponse;
|
|
|
|
|
|
|
|
mod logger;
|
2018-03-07 08:38:58 +01:00
|
|
|
|
2018-01-10 07:33:51 +01:00
|
|
|
pub mod cors;
|
2018-03-02 19:35:41 +01:00
|
|
|
pub mod csrf;
|
2018-04-14 01:02:01 +02:00
|
|
|
mod defaultheaders;
|
|
|
|
mod errhandlers;
|
|
|
|
#[cfg(feature = "session")]
|
2018-04-19 05:16:29 +02:00
|
|
|
pub mod identity;
|
|
|
|
#[cfg(feature = "session")]
|
2018-04-19 05:11:49 +02:00
|
|
|
pub mod session;
|
2018-04-03 06:43:50 +02:00
|
|
|
pub use self::defaultheaders::DefaultHeaders;
|
2018-04-14 01:02:01 +02:00
|
|
|
pub use self::errhandlers::ErrorHandlers;
|
|
|
|
pub use self::logger::Logger;
|
2018-03-07 08:38:58 +01:00
|
|
|
|
2017-11-10 07:08:54 +01:00
|
|
|
/// Middleware start result
|
|
|
|
pub enum Started {
|
2018-05-03 00:53:07 +02:00
|
|
|
/// Middleware is completed, continue to next middleware
|
2017-11-27 06:47:33 +01:00
|
|
|
Done,
|
2017-11-10 07:08:54 +01:00
|
|
|
/// New http response got generated. If middleware generates response
|
|
|
|
/// handler execution halts.
|
2017-12-16 01:24:15 +01:00
|
|
|
Response(HttpResponse),
|
2017-11-25 07:15:52 +01:00
|
|
|
/// Execution completed, runs future to completion.
|
2018-04-14 01:02:01 +02:00
|
|
|
Future(Box<Future<Item = Option<HttpResponse>, Error = Error>>),
|
2017-11-10 07:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Middleware execution result
|
|
|
|
pub enum Response {
|
|
|
|
/// New http response got generated
|
2017-12-16 01:24:15 +01:00
|
|
|
Done(HttpResponse),
|
2017-11-10 07:08:54 +01:00
|
|
|
/// Result is a future that resolves to a new http response
|
2018-04-14 01:02:01 +02:00
|
|
|
Future(Box<Future<Item = HttpResponse, Error = Error>>),
|
2017-11-10 07:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Middleware finish result
|
|
|
|
pub enum Finished {
|
|
|
|
/// Execution completed
|
|
|
|
Done,
|
|
|
|
/// Execution completed, but run future to completion
|
2018-04-14 01:02:01 +02:00
|
|
|
Future(Box<Future<Item = (), Error = Error>>),
|
2017-11-10 07:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Middleware definition
|
|
|
|
#[allow(unused_variables)]
|
2018-01-10 05:00:18 +01:00
|
|
|
pub trait Middleware<S>: 'static {
|
2017-11-10 07:08:54 +01:00
|
|
|
/// Method is called when request is ready. It may return
|
|
|
|
/// future, which should resolve before next middleware get called.
|
2018-01-10 07:48:35 +01:00
|
|
|
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
|
|
|
Ok(Started::Done)
|
2017-11-10 07:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Method is called when handler returns response,
|
2017-11-25 19:24:45 +01:00
|
|
|
/// but before sending http message to peer.
|
2018-04-14 01:02:01 +02:00
|
|
|
fn response(
|
2018-04-29 07:55:47 +02:00
|
|
|
&self, req: &mut HttpRequest<S>, resp: HttpResponse,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> Result<Response> {
|
2018-01-10 07:48:35 +01:00
|
|
|
Ok(Response::Done(resp))
|
2017-11-10 07:08:54 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 19:24:45 +01:00
|
|
|
/// Method is called after body stream get sent to peer.
|
2017-12-09 13:33:40 +01:00
|
|
|
fn finish(&self, req: &mut HttpRequest<S>, resp: &HttpResponse) -> Finished {
|
2017-11-10 07:08:54 +01:00
|
|
|
Finished::Done
|
|
|
|
}
|
|
|
|
}
|