1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00
actix-web/actix-http/src/encoding/decoder.rs

297 lines
9.2 KiB
Rust
Raw Normal View History

2021-02-11 22:39:54 +00:00
//! Stream decoders.
2021-02-12 00:15:25 +00:00
use std::{
convert::TryFrom,
2021-02-12 00:15:25 +00:00
future::Future,
io::{self, Write as _},
pin::Pin,
task::{Context, Poll},
};
2019-03-26 22:14:32 +00:00
use actix_rt::task::{spawn_blocking, JoinHandle};
2019-03-28 18:08:24 +00:00
use bytes::Bytes;
2019-12-13 05:24:57 +00:00
use futures_core::{ready, Stream};
#[cfg(feature = "compress-brotli")]
use brotli2::write::BrotliDecoder;
#[cfg(feature = "compress-gzip")]
use flate2::write::{GzDecoder, ZlibDecoder};
#[cfg(feature = "compress-zstd")]
use zstd::stream::write::Decoder as ZstdDecoder;
2019-03-26 22:14:32 +00:00
2021-02-12 00:15:25 +00:00
use crate::{
encoding::Writer,
error::{BlockingError, PayloadError},
http::header::{ContentEncoding, HeaderMap, CONTENT_ENCODING},
};
2019-03-26 22:14:32 +00:00
2021-02-12 00:15:25 +00:00
const MAX_CHUNK_SIZE_DECODE_IN_PLACE: usize = 2049;
2019-03-28 18:08:24 +00:00
pub struct Decoder<S> {
2019-03-26 22:14:32 +00:00
decoder: Option<ContentDecoder>,
2019-03-28 18:08:24 +00:00
stream: S,
eof: bool,
fut: Option<JoinHandle<Result<(Option<Bytes>, ContentDecoder), io::Error>>>,
2019-03-26 22:14:32 +00:00
}
2019-03-28 18:08:24 +00:00
impl<S> Decoder<S>
2019-03-26 22:14:32 +00:00
where
S: Stream<Item = Result<Bytes, PayloadError>>,
2019-03-26 22:14:32 +00:00
{
2019-03-28 18:08:24 +00:00
/// Construct a decoder.
#[inline]
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
2019-03-26 22:14:32 +00:00
let decoder = match encoding {
#[cfg(feature = "compress-brotli")]
2019-03-26 22:14:32 +00:00
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(
2019-12-20 07:50:07 +00:00
BrotliDecoder::new(Writer::new()),
2019-03-26 22:14:32 +00:00
))),
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
ZlibDecoder::new(Writer::new()),
))),
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(
GzDecoder::new(Writer::new()),
))),
#[cfg(feature = "compress-zstd")]
ContentEncoding::Zstd => Some(ContentDecoder::Zstd(Box::new(
ZstdDecoder::new(Writer::new()).expect(
"Failed to create zstd decoder. This is a bug. \
Please report it to the actix-web repository.",
),
))),
2019-03-26 22:14:32 +00:00
_ => None,
};
2021-02-12 00:15:25 +00:00
2019-03-28 18:08:24 +00:00
Decoder {
decoder,
stream,
fut: None,
eof: false,
}
2019-03-26 22:14:32 +00:00
}
2019-03-28 18:08:24 +00:00
/// Construct decoder based on headers.
#[inline]
pub fn from_headers(stream: S, headers: &HeaderMap) -> Decoder<S> {
2019-03-26 22:14:32 +00:00
// check content-encoding
2021-02-12 00:15:25 +00:00
let encoding = headers
.get(&CONTENT_ENCODING)
.and_then(|val| val.to_str().ok())
.and_then(|x| ContentEncoding::try_from(x).ok())
2021-02-12 00:15:25 +00:00
.unwrap_or(ContentEncoding::Identity);
2019-03-26 22:14:32 +00:00
Self::new(stream, encoding)
}
}
2019-03-28 18:08:24 +00:00
impl<S> Stream for Decoder<S>
2019-03-26 22:14:32 +00:00
where
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin,
2019-03-26 22:14:32 +00:00
{
type Item = Result<Bytes, PayloadError>;
2019-03-26 22:14:32 +00:00
fn poll_next(
mut self: Pin<&mut Self>,
2019-12-07 18:46:51 +00:00
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
2019-03-26 22:14:32 +00:00
loop {
2019-03-28 18:08:24 +00:00
if let Some(ref mut fut) = self.fut {
let (chunk, decoder) =
ready!(Pin::new(fut).poll(cx)).map_err(|_| BlockingError)??;
2021-02-12 00:15:25 +00:00
2019-03-28 18:08:24 +00:00
self.decoder = Some(decoder);
self.fut.take();
2021-02-12 00:15:25 +00:00
2019-03-28 18:08:24 +00:00
if let Some(chunk) = chunk {
return Poll::Ready(Some(Ok(chunk)));
2019-03-28 18:08:24 +00:00
}
}
if self.eof {
return Poll::Ready(None);
2019-03-28 18:08:24 +00:00
}
2021-02-12 00:15:25 +00:00
match ready!(Pin::new(&mut self.stream).poll_next(cx)) {
Some(Err(err)) => return Poll::Ready(Some(Err(err))),
Some(Ok(chunk)) => {
2019-03-28 18:08:24 +00:00
if let Some(mut decoder) = self.decoder.take() {
2021-02-12 00:15:25 +00:00
if chunk.len() < MAX_CHUNK_SIZE_DECODE_IN_PLACE {
2019-03-28 18:08:24 +00:00
let chunk = decoder.feed_data(chunk)?;
self.decoder = Some(decoder);
2021-02-12 00:15:25 +00:00
if let Some(chunk) = chunk {
return Poll::Ready(Some(Ok(chunk)));
}
} else {
self.fut = Some(spawn_blocking(move || {
let chunk = decoder.feed_data(chunk)?;
Ok((chunk, decoder))
}));
}
2021-02-12 00:15:25 +00:00
2019-03-28 18:08:24 +00:00
continue;
2019-03-26 22:14:32 +00:00
} else {
return Poll::Ready(Some(Ok(chunk)));
2019-03-26 22:14:32 +00:00
}
}
2021-02-12 00:15:25 +00:00
None => {
2019-03-28 18:08:24 +00:00
self.eof = true;
2021-02-12 00:15:25 +00:00
return if let Some(mut decoder) = self.decoder.take() {
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 22:14:32 +00:00
} else {
Poll::Ready(None)
2019-03-26 22:14:32 +00:00
};
}
}
}
}
}
enum ContentDecoder {
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
Deflate(Box<ZlibDecoder<Writer>>),
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
Gzip(Box<GzDecoder<Writer>>),
#[cfg(feature = "compress-brotli")]
2019-12-20 07:50:07 +00:00
Br(Box<BrotliDecoder<Writer>>),
// We need explicit 'static lifetime here because ZstdDecoder need lifetime
// argument, and we use `spawn_blocking` in `Decoder::poll_next` that require `FnOnce() -> R + Send + 'static`
#[cfg(feature = "compress-zstd")]
Zstd(Box<ZstdDecoder<'static, Writer>>),
2019-03-26 22:14:32 +00:00
}
impl ContentDecoder {
fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
match self {
#[cfg(feature = "compress-brotli")]
ContentDecoder::Br(ref mut decoder) => match decoder.flush() {
Ok(()) => {
let b = decoder.get_mut().take();
2021-02-12 00:15:25 +00:00
2019-03-26 22:14:32 +00:00
if !b.is_empty() {
Ok(Some(b))
} else {
Ok(None)
}
}
Err(e) => Err(e),
},
2021-02-12 00:15:25 +00:00
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
ContentDecoder::Gzip(ref mut decoder) => match decoder.try_finish() {
Ok(_) => {
let b = decoder.get_mut().take();
2021-02-12 00:15:25 +00:00
2019-03-26 22:14:32 +00:00
if !b.is_empty() {
Ok(Some(b))
} else {
Ok(None)
}
}
Err(e) => Err(e),
},
2021-02-12 00:15:25 +00:00
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
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),
},
#[cfg(feature = "compress-zstd")]
ContentDecoder::Zstd(ref mut decoder) => match decoder.flush() {
Ok(_) => {
let b = decoder.get_mut().take();
if !b.is_empty() {
Ok(Some(b))
} else {
Ok(None)
}
}
Err(e) => Err(e),
},
2019-03-26 22:14:32 +00:00
}
}
fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
match self {
#[cfg(feature = "compress-brotli")]
2019-03-26 22:14:32 +00:00
ContentDecoder::Br(ref mut decoder) => match decoder.write_all(&data) {
Ok(_) => {
decoder.flush()?;
let b = decoder.get_mut().take();
2021-02-12 00:15:25 +00:00
2019-03-26 22:14:32 +00:00
if !b.is_empty() {
Ok(Some(b))
} else {
Ok(None)
}
}
Err(e) => Err(e),
},
2021-02-12 00:15:25 +00:00
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
ContentDecoder::Gzip(ref mut decoder) => match decoder.write_all(&data) {
Ok(_) => {
decoder.flush()?;
let b = decoder.get_mut().take();
2021-02-12 00:15:25 +00:00
2019-03-26 22:14:32 +00:00
if !b.is_empty() {
Ok(Some(b))
} else {
Ok(None)
}
}
Err(e) => Err(e),
},
2021-02-12 00:15:25 +00:00
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
ContentDecoder::Deflate(ref mut decoder) => match decoder.write_all(&data) {
Ok(_) => {
decoder.flush()?;
2021-02-12 00:15:25 +00:00
2019-03-26 22:14:32 +00:00
let b = decoder.get_mut().take();
if !b.is_empty() {
Ok(Some(b))
} else {
Ok(None)
}
}
Err(e) => Err(e),
},
#[cfg(feature = "compress-zstd")]
ContentDecoder::Zstd(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),
},
2019-03-26 22:14:32 +00:00
}
}
}