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