2017-11-10 07:08:54 +01:00
|
|
|
//! Middlewares
|
2017-11-25 07:15:52 +01:00
|
|
|
#![allow(unused_imports, dead_code)]
|
|
|
|
|
2017-11-10 07:08:54 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use futures::{Async, Future, Poll};
|
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.
|
|
|
|
Future(Box<Future<Item=(HttpRequest, Option<HttpResponse>), Error=(HttpRequest, HttpResponse)>>),
|
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
|
|
|
|
Future(Box<Future<Item=HttpResponse, Error=HttpResponse>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
}
|