1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/middleware/mod.rs

68 lines
2.0 KiB
Rust
Raw Normal View History

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
use error::Error;
2017-11-10 07:08:54 +01:00
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
mod logger;
2017-11-25 18:52:32 +01:00
mod session;
2017-12-04 05:47:15 +01:00
mod defaultheaders;
2017-11-10 07:08:54 +01:00
pub use self::logger::Logger;
2017-12-04 05:47:15 +01:00
pub use self::defaultheaders::{DefaultHeaders, DefaultHeadersBuilder};
2017-11-27 02:30:35 +01:00
pub use self::session::{RequestSession, Session, SessionImpl, SessionBackend, SessionStorage,
CookieSessionError, CookieSessionBackend, CookieSessionBackendBuilder};
2017-11-10 07:08:54 +01:00
/// Middleware start result
pub enum Started {
/// Execution completed
2017-11-27 06:47:33 +01:00
Done,
2017-11-27 07:53:28 +01:00
/// Moddleware error
Err(Error),
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.
2017-11-27 06:47:33 +01:00
Future(Box<Future<Item=Option<HttpResponse>, Error=Error>>),
2017-11-10 07:08:54 +01:00
}
/// Middleware execution result
pub enum Response {
2017-11-25 19:24:45 +01:00
/// Moddleware error
Err(Error),
2017-11-10 07:08:54 +01:00
/// 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
2017-11-25 18:40:57 +01: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
2017-11-25 07:15:52 +01: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.
2017-12-09 13:33:40 +01:00
fn start(&self, req: &mut HttpRequest<S>) -> Started {
2017-11-27 06:47:33 +01:00
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.
2017-12-16 01:24:15 +01:00
fn response(&self, req: &mut HttpRequest<S>, resp: HttpResponse) -> Response {
2017-11-25 19:24:45 +01:00
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
}
}