1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 17:52:40 +01:00
actix-extras/src/middlewares/mod.rs

66 lines
1.9 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-11-10 07:08:54 +01:00
pub use self::logger::Logger;
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 {
2017-11-25 19:24:45 +01:00
/// Moddleware error
Err(Error),
2017-11-10 07:08:54 +01:00
/// Execution completed
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-11-27 06:47:33 +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-11-25 19:24:45 +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)]
pub trait Middleware {
/// Method is called when request is ready. It may return
/// future, which should resolve before next middleware get called.
2017-11-27 06:47:33 +01:00
fn start(&self, req: &mut HttpRequest) -> Started {
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-11-10 07:08:54 +01:00
fn response(&self, req: &mut HttpRequest, 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-11-10 07:08:54 +01:00
fn finish(&self, req: &mut HttpRequest, resp: &HttpResponse) -> Finished {
Finished::Done
}
}