1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Middlewares
use futures::Future;

use error::{Error, Result};
use httprequest::HttpRequest;
use httpresponse::HttpResponse;

mod logger;

pub mod cors;
pub mod csrf;
mod defaultheaders;
mod errhandlers;
#[cfg(feature = "session")]
pub mod identity;
#[cfg(feature = "session")]
pub mod session;
pub use self::defaultheaders::DefaultHeaders;
pub use self::errhandlers::ErrorHandlers;
pub use self::logger::Logger;

/// Middleware start result
pub enum Started {
    /// Middleware is completed, continue to next middleware
    Done,
    /// New http response got generated. If middleware generates response
    /// handler execution halts.
    Response(HttpResponse),
    /// Execution completed, runs future to completion.
    Future(Box<Future<Item = Option<HttpResponse>, Error = Error>>),
}

/// Middleware execution result
pub enum Response {
    /// New http response got generated
    Done(HttpResponse),
    /// Result is a future that resolves to a new http response
    Future(Box<Future<Item = HttpResponse, Error = Error>>),
}

/// Middleware finish result
pub enum Finished {
    /// Execution completed
    Done,
    /// Execution completed, but run future to completion
    Future(Box<Future<Item = (), Error = Error>>),
}

/// Middleware definition
#[allow(unused_variables)]
pub trait Middleware<S>: 'static {
    /// Method is called when request is ready. It may return
    /// future, which should resolve before next middleware get called.
    fn start(&self, req: &HttpRequest<S>) -> Result<Started> {
        Ok(Started::Done)
    }

    /// Method is called when handler returns response,
    /// but before sending http message to peer.
    fn response(&self, req: &HttpRequest<S>, resp: HttpResponse) -> Result<Response> {
        Ok(Response::Done(resp))
    }

    /// Method is called after body stream get sent to peer.
    fn finish(&self, req: &HttpRequest<S>, resp: &HttpResponse) -> Finished {
        Finished::Done
    }
}