replace libflate with flate2 (#476)
This commit is contained in:
@@ -19,7 +19,7 @@ http = "0.1.15"
|
|||||||
hyper = "0.12.22"
|
hyper = "0.12.22"
|
||||||
hyper-old-types = { version = "0.11", optional = true, features = ["compat"] }
|
hyper-old-types = { version = "0.11", optional = true, features = ["compat"] }
|
||||||
hyper-tls = { version = "0.3", optional = true }
|
hyper-tls = { version = "0.3", optional = true }
|
||||||
libflate = "0.1.18"
|
flate2 = { version = "^1.0.7", default-features = false, features = ["rust_backend"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3.7"
|
mime = "0.3.7"
|
||||||
mime_guess = "2.0.0-alpha.6"
|
mime_guess = "2.0.0-alpha.6"
|
||||||
@@ -44,6 +44,7 @@ rustls = { version = "0.15", features = ["dangerous_configuration"], optional =
|
|||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
tokio-tcp = "0.1"
|
tokio-tcp = "0.1"
|
||||||
|
libflate = "0.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["default-tls"]
|
default = ["default-tls"]
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ This module consists of a few main types:
|
|||||||
The following types directly support the gzip compression case:
|
The following types directly support the gzip compression case:
|
||||||
|
|
||||||
- `Pending` is a non-blocking constructor for a `Decoder` in case the body needs to be checked for EOF
|
- `Pending` is a non-blocking constructor for a `Decoder` in case the body needs to be checked for EOF
|
||||||
- `Peeked` is a buffer that keeps a few bytes available so `libflate`s `read_exact` calls won't fail
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@@ -27,7 +26,7 @@ use std::cmp;
|
|||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
|
|
||||||
use bytes::{Buf, BufMut, BytesMut};
|
use bytes::{Buf, BufMut, BytesMut};
|
||||||
use libflate::non_blocking::gzip;
|
use flate2::read::GzDecoder;
|
||||||
use futures::{Async, Future, Poll, Stream};
|
use futures::{Async, Future, Poll, Stream};
|
||||||
use hyper::{HeaderMap};
|
use hyper::{HeaderMap};
|
||||||
use hyper::header::{CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING};
|
use hyper::header::{CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING};
|
||||||
@@ -38,7 +37,7 @@ use error;
|
|||||||
const INIT_BUFFER_SIZE: usize = 8192;
|
const INIT_BUFFER_SIZE: usize = 8192;
|
||||||
|
|
||||||
/// A response decompressor over a non-blocking stream of chunks.
|
/// A response decompressor over a non-blocking stream of chunks.
|
||||||
///
|
///
|
||||||
/// The inner decoder may be constructed asynchronously.
|
/// The inner decoder may be constructed asynchronously.
|
||||||
pub struct Decoder {
|
pub struct Decoder {
|
||||||
inner: Inner
|
inner: Inner
|
||||||
@@ -58,10 +57,10 @@ struct Pending {
|
|||||||
body: ReadableChunks<Body>,
|
body: ReadableChunks<Body>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A gzip decoder that reads from a `libflate::gzip::Decoder` into a `BytesMut` and emits the results
|
/// A gzip decoder that reads from a `flate2::read::GzDecoder` into a `BytesMut` and emits the results
|
||||||
/// as a `Chunk`.
|
/// as a `Chunk`.
|
||||||
struct Gzip {
|
struct Gzip {
|
||||||
inner: Box<gzip::Decoder<Peeked<ReadableChunks<Body>>>>,
|
inner: Box<GzDecoder<ReadableChunks<Body>>>,
|
||||||
buf: BytesMut,
|
buf: BytesMut,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +73,7 @@ impl fmt::Debug for Decoder {
|
|||||||
|
|
||||||
impl Decoder {
|
impl Decoder {
|
||||||
/// An empty decoder.
|
/// An empty decoder.
|
||||||
///
|
///
|
||||||
/// This decoder will produce a single 0 byte chunk.
|
/// This decoder will produce a single 0 byte chunk.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn empty() -> Decoder {
|
pub fn empty() -> Decoder {
|
||||||
@@ -84,7 +83,7 @@ impl Decoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A plain text decoder.
|
/// A plain text decoder.
|
||||||
///
|
///
|
||||||
/// This decoder will emit the underlying chunks as-is.
|
/// This decoder will emit the underlying chunks as-is.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn plain_text(body: Body) -> Decoder {
|
fn plain_text(body: Body) -> Decoder {
|
||||||
@@ -94,7 +93,7 @@ impl Decoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A gzip decoder.
|
/// A gzip decoder.
|
||||||
///
|
///
|
||||||
/// This decoder will buffer and decompress chunks that are gzipped.
|
/// This decoder will buffer and decompress chunks that are gzipped.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn gzip(body: Body) -> Decoder {
|
fn gzip(body: Body) -> Decoder {
|
||||||
@@ -188,9 +187,6 @@ impl Future for Pending {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let body = mem::replace(&mut self.body, ReadableChunks::new(Body::empty()));
|
let body = mem::replace(&mut self.body, ReadableChunks::new(Body::empty()));
|
||||||
// libflate does a read_exact([0; 2]), so its impossible to tell
|
|
||||||
// if the stream was empty, or truly had an UnexpectedEof.
|
|
||||||
// Therefore, we need to check for EOF first.
|
|
||||||
match body_state {
|
match body_state {
|
||||||
StreamState::Eof => Ok(Async::Ready(Inner::PlainText(Body::empty()))),
|
StreamState::Eof => Ok(Async::Ready(Inner::PlainText(Body::empty()))),
|
||||||
StreamState::HasMore => Ok(Async::Ready(Inner::Gzip(Gzip::new(body))))
|
StreamState::HasMore => Ok(Async::Ready(Inner::Gzip(Gzip::new(body))))
|
||||||
@@ -202,7 +198,7 @@ impl Gzip {
|
|||||||
fn new(stream: ReadableChunks<Body>) -> Self {
|
fn new(stream: ReadableChunks<Body>) -> Self {
|
||||||
Gzip {
|
Gzip {
|
||||||
buf: BytesMut::with_capacity(INIT_BUFFER_SIZE),
|
buf: BytesMut::with_capacity(INIT_BUFFER_SIZE),
|
||||||
inner: Box::new(gzip::Decoder::new(Peeked::new(stream))),
|
inner: Box::new(GzDecoder::new(stream)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,10 +213,10 @@ impl Stream for Gzip {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The buffer contains uninitialised memory so getting a readable slice is unsafe.
|
// The buffer contains uninitialised memory so getting a readable slice is unsafe.
|
||||||
// We trust the `libflate` writer not to read from the memory given.
|
// We trust the `flate2` and `miniz` writer not to read from the memory given.
|
||||||
//
|
//
|
||||||
// To be safe, this memory could be zeroed before passing to `libflate`.
|
// To be safe, this memory could be zeroed before passing to `flate2`.
|
||||||
// Otherwise we might need to deal with the case where `libflate` panics.
|
// Otherwise we might need to deal with the case where `flate2` panics.
|
||||||
let read = {
|
let read = {
|
||||||
let mut buf = unsafe { self.buf.bytes_mut() };
|
let mut buf = unsafe { self.buf.bytes_mut() };
|
||||||
self.inner.read(&mut buf)
|
self.inner.read(&mut buf)
|
||||||
@@ -266,84 +262,6 @@ enum StreamState {
|
|||||||
Eof
|
Eof
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A buffering reader that ensures `Read`s return at least a few bytes.
|
|
||||||
struct Peeked<R> {
|
|
||||||
state: PeekedState,
|
|
||||||
peeked_buf: [u8; 2],
|
|
||||||
pos: usize,
|
|
||||||
inner: R,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum PeekedState {
|
|
||||||
/// The internal buffer hasn't filled yet.
|
|
||||||
NotReady,
|
|
||||||
/// The internal buffer can be read.
|
|
||||||
Ready(usize)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R> Peeked<R> {
|
|
||||||
#[inline]
|
|
||||||
fn new(inner: R) -> Self {
|
|
||||||
Peeked {
|
|
||||||
state: PeekedState::NotReady,
|
|
||||||
peeked_buf: [0; 2],
|
|
||||||
inner: inner,
|
|
||||||
pos: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn ready(&mut self) {
|
|
||||||
self.state = PeekedState::Ready(self.pos);
|
|
||||||
self.pos = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn not_ready(&mut self) {
|
|
||||||
self.state = PeekedState::NotReady;
|
|
||||||
self.pos = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: Read> Read for Peeked<R> {
|
|
||||||
#[inline]
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
||||||
loop {
|
|
||||||
match self.state {
|
|
||||||
PeekedState::Ready(peeked_buf_len) => {
|
|
||||||
let len = cmp::min(buf.len(), peeked_buf_len - self.pos);
|
|
||||||
let start = self.pos;
|
|
||||||
let end = self.pos + len;
|
|
||||||
|
|
||||||
buf[..len].copy_from_slice(&self.peeked_buf[start..end]);
|
|
||||||
self.pos += len;
|
|
||||||
if self.pos == peeked_buf_len {
|
|
||||||
self.not_ready();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(len)
|
|
||||||
},
|
|
||||||
PeekedState::NotReady => {
|
|
||||||
let read = self.inner.read(&mut self.peeked_buf[self.pos..]);
|
|
||||||
|
|
||||||
match read {
|
|
||||||
Ok(0) => {
|
|
||||||
self.ready();
|
|
||||||
},
|
|
||||||
Ok(read) => {
|
|
||||||
self.pos += read;
|
|
||||||
if self.pos == self.peeked_buf.len() {
|
|
||||||
self.ready();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(e) => return Err(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> ReadableChunks<S> {
|
impl<S> ReadableChunks<S> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn new(stream: S) -> Self {
|
pub(crate) fn new(stream: S) -> Self {
|
||||||
@@ -402,11 +320,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> ReadableChunks<S>
|
impl<S> ReadableChunks<S>
|
||||||
where S: Stream<Item = Chunk, Error = error::Error>
|
where S: Stream<Item = Chunk, Error = error::Error>
|
||||||
{
|
{
|
||||||
/// Poll the readiness of the inner reader.
|
/// Poll the readiness of the inner reader.
|
||||||
///
|
///
|
||||||
/// This function will update the internal state and return a simplified
|
/// This function will update the internal state and return a simplified
|
||||||
/// version of the `ReadState`.
|
/// version of the `ReadState`.
|
||||||
fn poll_stream(&mut self) -> Poll<StreamState, error::Error> {
|
fn poll_stream(&mut self) -> Poll<StreamState, error::Error> {
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ pub extern crate hyper_old_types as hyper_011;
|
|||||||
extern crate hyper_tls;
|
extern crate hyper_tls;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
extern crate libflate;
|
extern crate flate2;
|
||||||
extern crate mime;
|
extern crate mime;
|
||||||
extern crate mime_guess;
|
extern crate mime_guess;
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "default-tls")]
|
||||||
|
|||||||
Reference in New Issue
Block a user