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