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
use middleware::Middleware;
use util::buf_stream::BufStream;
use util::http::HttpService;

use http::{Request, Response};

/// HTTP middleware trait
///
/// A trait "alias" for `Middleware` where the yielded service is an
/// `HttpService`.
///
/// Using `HttpMiddleware` in where bounds is easier than trying to use `Middleware`
/// directly.
pub trait HttpMiddleware<S>: sealed::Sealed<S> {
    /// The HTTP request body handled by the wrapped service.
    type RequestBody: BufStream;

    /// The HTTP response body returned by the wrapped service.
    type ResponseBody: BufStream;

    /// The wrapped service's error type.
    type Error;

    /// The wrapped service.
    type Service: HttpService<RequestBody = Self::RequestBody,
                             ResponseBody = Self::ResponseBody,
                                    Error = Self::Error>;

    /// Wrap the given service with the middleware, returning a new servicee
    /// that has been decorated with the middleware.
    fn wrap_http(&self, inner: S) -> Self::Service;
}

impl<T, S, B1, B2> HttpMiddleware<S> for T
where T: Middleware<S, Request = Request<B1>,
                      Response = Response<B2>>,
      B1: BufStream,
      B2: BufStream,
{
    type RequestBody = B1;
    type ResponseBody = B2;
    type Error = T::Error;
    type Service = T::Service;

    fn wrap_http(&self, inner: S) -> Self::Service {
        Middleware::wrap(self, inner)
    }
}

impl<T, S, B1, B2> sealed::Sealed<S> for T
where T: Middleware<S, Request = Request<B1>,
                      Response = Response<B2>>,
      B1: BufStream,
      B2: BufStream,
{}

mod sealed {
    pub trait Sealed<S> {}
}