use flate2::read::{GzDecoder, ZlibDecoder};
use futures::future::{BoxFuture, FutureExt};
use httpio::compression::traits::CompressionDecoder;
use httpio::enums::content_encoding::ContentEncoding;
use httpio::enums::http_error::HttpError;
use std::io::Read;
pub struct Flate2Decoder;
impl CompressionDecoder for Flate2Decoder {
fn gzip_decode(&self, compressed: Vec<u8>) -> BoxFuture<'static, Result<Vec<u8>, HttpError>> {
async move {
let mut decoder = GzDecoder::new(&compressed[..]);
let mut decompressed = Vec::new();
decoder.read_to_end(&mut decompressed)?;
Ok(decompressed)
}
.boxed()
}
fn zlib_decode(
&self,
compressed: Vec<u8>,
size_hint: Option<usize>,
) -> BoxFuture<'static, Result<Vec<u8>, HttpError>> {
async move {
let mut decoder = ZlibDecoder::new(&compressed[..]);
let mut decompressed = size_hint.map_or_else(Vec::new, Vec::with_capacity);
decoder.read_to_end(&mut decompressed)?;
Ok(decompressed)
}
.boxed()
}
fn supported_encodings(&self) -> Vec<ContentEncoding> {
vec![ContentEncoding::Gzip, ContentEncoding::Deflate]
}
}