tower_web/util/http/
middleware.rs

1use middleware::Middleware;
2use util::buf_stream::BufStream;
3use util::http::HttpService;
4
5use http::{Request, Response};
6
7/// HTTP middleware trait
8///
9/// A trait "alias" for `Middleware` where the yielded service is an
10/// `HttpService`.
11///
12/// Using `HttpMiddleware` in where bounds is easier than trying to use `Middleware`
13/// directly.
14pub trait HttpMiddleware<S>: sealed::Sealed<S> {
15    /// The HTTP request body handled by the wrapped service.
16    type RequestBody: BufStream;
17
18    /// The HTTP response body returned by the wrapped service.
19    type ResponseBody: BufStream;
20
21    /// The wrapped service's error type.
22    type Error;
23
24    /// The wrapped service.
25    type Service: HttpService<RequestBody = Self::RequestBody,
26                             ResponseBody = Self::ResponseBody,
27                                    Error = Self::Error>;
28
29    /// Wrap the given service with the middleware, returning a new servicee
30    /// that has been decorated with the middleware.
31    fn wrap_http(&self, inner: S) -> Self::Service;
32}
33
34impl<T, S, B1, B2> HttpMiddleware<S> for T
35where T: Middleware<S, Request = Request<B1>,
36                      Response = Response<B2>>,
37      B1: BufStream,
38      B2: BufStream,
39{
40    type RequestBody = B1;
41    type ResponseBody = B2;
42    type Error = T::Error;
43    type Service = T::Service;
44
45    fn wrap_http(&self, inner: S) -> Self::Service {
46        Middleware::wrap(self, inner)
47    }
48}
49
50impl<T, S, B1, B2> sealed::Sealed<S> for T
51where T: Middleware<S, Request = Request<B1>,
52                      Response = Response<B2>>,
53      B1: BufStream,
54      B2: BufStream,
55{}
56
57mod sealed {
58    pub trait Sealed<S> {}
59}