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
/// Compression modifier for Iron. This simple crate lets you pass bytes to a response such
/// that they can be encoded by either GZIP or deflate algorithms. The modifiers also set the
/// proper content encoding header.
extern crate flate2;
extern crate iron;

use std::io::Write;

use flate2::Compression;
use flate2::write::{DeflateEncoder, GzEncoder};

use iron::Response;
use iron::headers::{ContentEncoding, Encoding};
use iron::modifier::Modifier;

/// A type that knows how to encode a stream of bytes with the deflate algorithm.
pub struct DeflateWriter<'a>(pub &'a [u8]);

/// A type that knows how to encode a stream of bytes with the GZIP algorithm.
pub struct GzipWriter<'a>(pub &'a [u8]);

impl<'a> Modifier<Response> for DeflateWriter<'a> {
    /// Encode the bytes with the deflate algorithm.
    ///
    /// # Panics
    ///
    /// This will panic if the encoder cannot finish writing the stream.
    fn modify(self, res: &mut Response) {
        res.headers.set(ContentEncoding(vec![Encoding::Deflate]));
        let mut encoder = DeflateEncoder::new(Vec::new(), Compression::Best);
        let _ = encoder.write_all(self.0);
        let compressed_bytes = encoder.finish().unwrap();
        compressed_bytes.modify(res);
    }
}

impl<'a> Modifier<Response> for GzipWriter<'a> {
    /// Encode the bytes with the GZIP algorithm.
    ///
    /// # Panics
    ///
    /// This will panic if the encoder cannot finish writing the stream.
    fn modify(self, res: &mut Response) {
        res.headers.set(ContentEncoding(vec![Encoding::Gzip]));
        let mut encoder = GzEncoder::new(Vec::new(), Compression::Best);
        let _ = encoder.write_all(self.0);
        let compressed_bytes = encoder.finish().unwrap();
        compressed_bytes.modify(res);
    }
}