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
69
70
71
72
73
74
75
76
use flate2::Compression;
use futures::{Future, Poll};
use http;
use http::header::{CONTENT_ENCODING, HeaderValue};
use tower_service::Service;
use util::buf_stream::BufStream;
use util::buf_stream::deflate::CompressStream;

/// Deflates the inner service's response bodies.
#[derive(Debug)]
pub struct DeflateService<S> {
    inner: S,
    level: Compression,
}

/// Deflate the response body.
#[derive(Debug)]
pub struct ResponseFuture<T> {
    inner: T,
    level: Compression,
}

impl<S> DeflateService<S> {
    pub(super) fn new(inner: S, level: Compression) -> DeflateService<S> {
        DeflateService {
            inner,
            level,
        }
    }
}

impl<S, RequestBody, ResponseBody> Service for DeflateService<S>
where S: Service<Request = http::Request<RequestBody>,
                Response = http::Response<ResponseBody>>,
      ResponseBody: BufStream,
      S::Error: ::std::error::Error,
{
    type Request = http::Request<RequestBody>;
    type Response = http::Response<CompressStream<ResponseBody>>;
    type Error = S::Error;
    type Future = ResponseFuture<S::Future>;

    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
        self.inner.poll_ready()
    }

    fn call(&mut self, request: Self::Request) -> Self::Future {
        ResponseFuture {
            inner: self.inner.call(request),
            level: self.level,
        }
    }
}

impl<T, B> Future for ResponseFuture<T>
where
    T: Future<Item = http::Response<B>>,
    B: BufStream,
    T::Error: ::std::error::Error,
{
    type Item = http::Response<CompressStream<B>>;
    type Error = T::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let mut response = try_ready!(self.inner.poll())
            .map(|body| CompressStream::new(body, self.level));

        let content_encoding = HeaderValue::from_static("deflate");

        // Set content-encoding
        response.headers_mut()
            .insert(CONTENT_ENCODING, content_encoding);

        Ok(response.into())
    }
}