http_serve/
gzip.rs

1// Copyright (c) 2016-2018 The http-serve developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE.txt or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT.txt or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::chunker;
10use std::io::{self, Write};
11use std::mem;
12
13/// A `std::io::Write` implementation that makes a chunked hyper response body stream.
14/// Automatically applies `gzip` content encoding if requested by the client.
15///
16/// The stream is infinitely buffered; calls to `write` and `flush` never block. `flush` thus is a
17/// hint that data should be sent to the client as soon as possible, but this shouldn't be expected
18/// to happen before it returns. `write` and `flush` may return error; this indicates that the
19/// client certainly won't receive any additional bytes, so the calling code should stop producing
20/// them.
21///
22/// The infinite buffering avoids the need for calling code to deal with backpressure via futures
23/// or blocking. Many applications anyway produce output while holding a lock or database
24/// transaction that should finish quickly, so backpressure must be ignored anyway.
25///
26/// On drop, the stream will be "finished" (for gzip, this writes a special footer). There's no way
27/// to know the complete stream was written successfully. It's inherent in the combination of
28/// HTTP / TCP / Unix sockets / hyper anyway that only the client knows this.
29pub struct BodyWriter<D, E>(Inner<D, E>)
30where
31    D: From<Vec<u8>> + Send + 'static,
32    E: Send + 'static;
33
34enum Inner<D, E>
35where
36    D: From<Vec<u8>> + Send + 'static,
37    E: Send + 'static,
38{
39    Raw(chunker::BodyWriter<D, E>),
40    Gzipped(flate2::write::GzEncoder<chunker::BodyWriter<D, E>>),
41
42    /// No more data should be sent. `abort()` or `drop()` has been called, or a previous call
43    /// discovered that the receiver has been dropped.
44    Dead,
45}
46
47impl<D, E> BodyWriter<D, E>
48where
49    D: From<Vec<u8>> + Send + 'static,
50    E: Send + 'static,
51{
52    pub(crate) fn raw(raw: chunker::BodyWriter<D, E>) -> Self {
53        BodyWriter(Inner::Raw(raw))
54    }
55
56    pub(crate) fn gzipped(raw: chunker::BodyWriter<D, E>, level: flate2::Compression) -> Self {
57        BodyWriter(Inner::Gzipped(flate2::GzBuilder::new().write(raw, level)))
58    }
59
60    /// Causes the HTTP connection to be dropped abruptly.
61    /// The actual value of `error` is mostly irrelevant, although hyper may choose to log it.
62    pub fn abort(&mut self, error: E) {
63        match mem::replace(&mut self.0, Inner::Dead) {
64            Inner::Dead => (),
65            Inner::Raw(ref mut w) => w.abort(error),
66            Inner::Gzipped(ref mut g) => g.get_mut().abort(error),
67        };
68    }
69}
70
71impl<D, E> Write for BodyWriter<D, E>
72where
73    D: From<Vec<u8>> + Send + 'static,
74    E: Send + 'static,
75{
76    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
77        let r = match self.0 {
78            Inner::Dead => return Err(io::Error::new(io::ErrorKind::BrokenPipe, "body is dead")),
79            Inner::Raw(ref mut w) => w.write(buf),
80            Inner::Gzipped(ref mut w) => w.write(buf),
81        };
82        if r.is_err() {
83            self.0 = Inner::Dead;
84        }
85        r
86    }
87
88    fn flush(&mut self) -> io::Result<()> {
89        let r = match self.0 {
90            Inner::Dead => return Err(io::Error::new(io::ErrorKind::BrokenPipe, "body is dead")),
91            Inner::Raw(ref mut w) => w.flush(),
92            Inner::Gzipped(ref mut w) => w.flush(),
93        };
94        if r.is_err() {
95            self.0 = Inner::Dead;
96        }
97        r
98    }
99}