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

59 lines
1.6 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;
pub use self::logger::Logger;
/// Middleware start result
pub enum Started {
/// Execution completed
2017-11-25 07:15:52 +01:00
Done(HttpRequest),
2017-11-10 07:08:54 +01:00
/// New http response got generated. If middleware generates response
/// handler execution halts.
2017-11-25 07:15:52 +01:00
Response(HttpRequest, HttpResponse),
/// Execution completed, runs future to completion.
2017-11-25 18:40:57 +01:00
Future(Box<Future<Item=(HttpRequest, Option<HttpResponse>), Error=Error>>),
2017-11-10 07:08:54 +01:00
}
/// Middleware execution result
pub enum Response {
/// New http response got generated
Response(HttpResponse),
/// 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-25 07:15:52 +01:00
fn start(&self, req: HttpRequest) -> Started {
Started::Done(req)
2017-11-10 07:08:54 +01:00
}
/// Method is called when handler returns response,
/// but before sending body stream to peer.
fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> Response {
Response::Response(resp)
}
/// Method is called after http response get sent to peer.
fn finish(&self, req: &mut HttpRequest, resp: &HttpResponse) -> Finished {
Finished::Done
}
}