2019-11-15 15:54:11 +06:00
|
|
|
use std::future::Future;
|
2019-03-26 15:14:32 -07:00
|
|
|
use std::io::{self, Write};
|
2019-11-15 15:54:11 +06:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-03-26 15:14:32 -07:00
|
|
|
|
2021-01-10 00:04:19 +08:00
|
|
|
use actix_rt::task::{spawn_blocking, JoinHandle};
|
2019-12-20 13:50:07 +06:00
|
|
|
use brotli2::write::BrotliDecoder;
|
2019-03-28 11:08:24 -07:00
|
|
|
use bytes::Bytes;
|
2019-03-26 15:14:32 -07:00
|
|
|
use flate2::write::{GzDecoder, ZlibDecoder};
|
2019-12-13 11:24:57 +06:00
|
|
|
use futures_core::{ready, Stream};
|
2019-03-26 15:14:32 -07:00
|
|
|
|
|
|
|
use super::Writer;
|
2021-01-10 00:04:19 +08:00
|
|
|
use crate::error::{BlockingError, PayloadError};
|
2019-03-26 15:14:32 -07:00
|
|
|
use crate::http::header::{ContentEncoding, HeaderMap, CONTENT_ENCODING};
|
|
|
|
|
2019-03-28 21:15:26 -07:00
|
|
|
const INPLACE: usize = 2049;
|
|
|
|
|
2019-03-28 11:08:24 -07:00
|
|
|
pub struct Decoder<S> {
|
2019-03-26 15:14:32 -07:00
|
|
|
decoder: Option<ContentDecoder>,
|
2019-03-28 11:08:24 -07:00
|
|
|
stream: S,
|
|
|
|
eof: bool,
|
2021-01-10 00:04:19 +08:00
|
|
|
fut: Option<JoinHandle<Result<(Option<Bytes>, ContentDecoder), io::Error>>>,
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 11:08:24 -07:00
|
|
|
impl<S> Decoder<S>
|
2019-03-26 15:14:32 -07:00
|
|
|
where
|
2019-11-15 15:54:11 +06:00
|
|
|
S: Stream<Item = Result<Bytes, PayloadError>>,
|
2019-03-26 15:14:32 -07:00
|
|
|
{
|
2019-03-28 11:08:24 -07:00
|
|
|
/// Construct a decoder.
|
|
|
|
#[inline]
|
|
|
|
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
|
2019-03-26 15:14:32 -07:00
|
|
|
let decoder = match encoding {
|
|
|
|
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(
|
2019-12-20 13:50:07 +06:00
|
|
|
BrotliDecoder::new(Writer::new()),
|
2019-03-26 15:14:32 -07:00
|
|
|
))),
|
|
|
|
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
|
|
|
|
ZlibDecoder::new(Writer::new()),
|
|
|
|
))),
|
|
|
|
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(
|
|
|
|
GzDecoder::new(Writer::new()),
|
|
|
|
))),
|
|
|
|
_ => None,
|
|
|
|
};
|
2019-03-28 11:08:24 -07:00
|
|
|
Decoder {
|
|
|
|
decoder,
|
|
|
|
stream,
|
|
|
|
fut: None,
|
|
|
|
eof: false,
|
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 11:08:24 -07:00
|
|
|
/// Construct decoder based on headers.
|
|
|
|
#[inline]
|
|
|
|
pub fn from_headers(stream: S, headers: &HeaderMap) -> Decoder<S> {
|
2019-03-26 15:14:32 -07:00
|
|
|
// check content-encoding
|
2019-04-06 15:02:02 -07:00
|
|
|
let encoding = if let Some(enc) = headers.get(&CONTENT_ENCODING) {
|
2019-03-26 15:14:32 -07:00
|
|
|
if let Ok(enc) = enc.to_str() {
|
|
|
|
ContentEncoding::from(enc)
|
|
|
|
} else {
|
|
|
|
ContentEncoding::Identity
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ContentEncoding::Identity
|
|
|
|
};
|
|
|
|
|
|
|
|
Self::new(stream, encoding)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 11:08:24 -07:00
|
|
|
impl<S> Stream for Decoder<S>
|
2019-03-26 15:14:32 -07:00
|
|
|
where
|
2019-11-15 15:54:11 +06:00
|
|
|
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin,
|
2019-03-26 15:14:32 -07:00
|
|
|
{
|
2019-11-15 15:54:11 +06:00
|
|
|
type Item = Result<Bytes, PayloadError>;
|
2019-03-26 15:14:32 -07:00
|
|
|
|
2019-11-15 15:54:11 +06:00
|
|
|
fn poll_next(
|
|
|
|
mut self: Pin<&mut Self>,
|
2019-12-08 00:46:51 +06:00
|
|
|
cx: &mut Context<'_>,
|
2019-11-15 15:54:11 +06:00
|
|
|
) -> Poll<Option<Self::Item>> {
|
2019-03-26 15:14:32 -07:00
|
|
|
loop {
|
2019-03-28 11:08:24 -07:00
|
|
|
if let Some(ref mut fut) = self.fut {
|
2021-02-06 08:23:59 -08:00
|
|
|
let (chunk, decoder) =
|
|
|
|
ready!(Pin::new(fut).poll(cx)).map_err(|_| BlockingError)??;
|
2019-03-28 11:08:24 -07:00
|
|
|
self.decoder = Some(decoder);
|
|
|
|
self.fut.take();
|
|
|
|
if let Some(chunk) = chunk {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-03-28 11:08:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.eof {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(None);
|
2019-03-28 11:08:24 -07:00
|
|
|
}
|
|
|
|
|
2019-11-15 15:54:11 +06:00
|
|
|
match Pin::new(&mut self.stream).poll_next(cx) {
|
|
|
|
Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))),
|
|
|
|
Poll::Ready(Some(Ok(chunk))) => {
|
2019-03-28 11:08:24 -07:00
|
|
|
if let Some(mut decoder) = self.decoder.take() {
|
2019-03-28 21:15:26 -07:00
|
|
|
if chunk.len() < INPLACE {
|
2019-03-28 11:08:24 -07:00
|
|
|
let chunk = decoder.feed_data(chunk)?;
|
2019-03-28 21:15:26 -07:00
|
|
|
self.decoder = Some(decoder);
|
|
|
|
if let Some(chunk) = chunk {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-03-28 21:15:26 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-10 00:04:19 +08:00
|
|
|
self.fut = Some(spawn_blocking(move || {
|
2019-03-28 21:15:26 -07:00
|
|
|
let chunk = decoder.feed_data(chunk)?;
|
|
|
|
Ok((chunk, decoder))
|
|
|
|
}));
|
|
|
|
}
|
2019-03-28 11:08:24 -07:00
|
|
|
continue;
|
2019-03-26 15:14:32 -07:00
|
|
|
} else {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 15:54:11 +06:00
|
|
|
Poll::Ready(None) => {
|
2019-03-28 11:08:24 -07:00
|
|
|
self.eof = true;
|
2019-03-28 21:15:26 -07:00
|
|
|
return if let Some(mut decoder) = self.decoder.take() {
|
2019-11-15 15:54:11 +06:00
|
|
|
match decoder.feed_eof() {
|
|
|
|
Ok(Some(res)) => Poll::Ready(Some(Ok(res))),
|
|
|
|
Ok(None) => Poll::Ready(None),
|
|
|
|
Err(err) => Poll::Ready(Some(Err(err.into()))),
|
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
} else {
|
2019-11-15 15:54:11 +06:00
|
|
|
Poll::Ready(None)
|
2019-03-26 15:14:32 -07:00
|
|
|
};
|
|
|
|
}
|
2019-11-15 15:54:11 +06:00
|
|
|
Poll::Pending => break,
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 15:54:11 +06:00
|
|
|
Poll::Pending
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ContentDecoder {
|
|
|
|
Deflate(Box<ZlibDecoder<Writer>>),
|
|
|
|
Gzip(Box<GzDecoder<Writer>>),
|
2019-12-20 13:50:07 +06:00
|
|
|
Br(Box<BrotliDecoder<Writer>>),
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContentDecoder {
|
|
|
|
fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
|
|
|
|
match self {
|
2019-12-07 16:55:41 +01:00
|
|
|
ContentDecoder::Br(ref mut decoder) => match decoder.flush() {
|
|
|
|
Ok(()) => {
|
|
|
|
let b = decoder.get_mut().take();
|
2019-03-26 15:14:32 -07:00
|
|
|
if !b.is_empty() {
|
|
|
|
Ok(Some(b))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
},
|
|
|
|
ContentDecoder::Gzip(ref mut decoder) => match decoder.try_finish() {
|
|
|
|
Ok(_) => {
|
|
|
|
let b = decoder.get_mut().take();
|
|
|
|
if !b.is_empty() {
|
|
|
|
Ok(Some(b))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
},
|
|
|
|
ContentDecoder::Deflate(ref mut decoder) => match decoder.try_finish() {
|
|
|
|
Ok(_) => {
|
|
|
|
let b = decoder.get_mut().take();
|
|
|
|
if !b.is_empty() {
|
|
|
|
Ok(Some(b))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
|
|
|
|
match self {
|
|
|
|
ContentDecoder::Br(ref mut decoder) => match decoder.write_all(&data) {
|
|
|
|
Ok(_) => {
|
|
|
|
decoder.flush()?;
|
|
|
|
let b = decoder.get_mut().take();
|
|
|
|
if !b.is_empty() {
|
|
|
|
Ok(Some(b))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
},
|
|
|
|
ContentDecoder::Gzip(ref mut decoder) => match decoder.write_all(&data) {
|
|
|
|
Ok(_) => {
|
|
|
|
decoder.flush()?;
|
|
|
|
let b = decoder.get_mut().take();
|
|
|
|
if !b.is_empty() {
|
|
|
|
Ok(Some(b))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
},
|
|
|
|
ContentDecoder::Deflate(ref mut decoder) => match decoder.write_all(&data) {
|
|
|
|
Ok(_) => {
|
|
|
|
decoder.flush()?;
|
|
|
|
let b = decoder.get_mut().take();
|
|
|
|
if !b.is_empty() {
|
|
|
|
Ok(Some(b))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|