1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-19 16:58:14 +00:00
actix-web/actix-http/src/encoding/decoder.rs

303 lines
9.3 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::{
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;
#[cfg(feature = "compress-gzip")]
use flate2::write::{GzDecoder, ZlibDecoder};
2023-07-17 01:38:12 +00:00
use futures_core::{ready, Stream};
#[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,
2022-02-22 08:45:28 +00:00
error::PayloadError,
header::{ContentEncoding, HeaderMap, CONTENT_ENCODING},
2021-02-12 00:15:25 +00:00
};
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;
pin_project_lite::pin_project! {
pub struct Decoder<S> {
decoder: Option<ContentDecoder>,
#[pin]
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")]
2022-01-03 13:17:57 +00:00
ContentEncoding::Brotli => Some(ContentDecoder::Brotli(Box::new(
2022-01-15 14:03:16 +00:00
brotli::DecompressorWriter::new(Writer::new(), 8_096),
2022-01-03 13:17:57 +00:00
))),
2022-02-22 08:45:28 +00:00
#[cfg(feature = "compress-gzip")]
2023-07-17 01:38:12 +00:00
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(ZlibDecoder::new(
Writer::new(),
)))),
2022-02-22 08:45:28 +00:00
#[cfg(feature = "compress-gzip")]
2021-12-08 06:01:11 +00:00
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(GzDecoder::new(
Writer::new(),
)))),
2022-02-22 08:45:28 +00:00
#[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| x.parse().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>>,
2019-03-26 22:14:32 +00:00
{
type Item = Result<Bytes, PayloadError>;
2019-03-26 22:14:32 +00:00
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
2019-03-26 22:14:32 +00:00
loop {
if let Some(ref mut fut) = this.fut {
2022-02-22 08:45:28 +00:00
let (chunk, decoder) = ready!(Pin::new(fut).poll(cx)).map_err(|_| {
PayloadError::Io(io::Error::new(
io::ErrorKind::Other,
"Blocking task was cancelled unexpectedly",
))
})??;
2021-02-12 00:15:25 +00:00
*this.decoder = Some(decoder);
this.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 *this.eof {
return Poll::Ready(None);
2019-03-28 18:08:24 +00:00
}
match ready!(this.stream.as_mut().poll_next(cx)) {
2021-02-12 00:15:25 +00:00
Some(Err(err)) => return Poll::Ready(Some(Err(err))),
Some(Ok(chunk)) => {
if let Some(mut decoder) = this.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)?;
*this.decoder = Some(decoder);
2021-02-12 00:15:25 +00:00
if let Some(chunk) = chunk {
return Poll::Ready(Some(Ok(chunk)));
}
} else {
*this.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 => {
*this.eof = true;
2021-02-12 00:15:25 +00:00
return if let Some(mut decoder) = this.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>>),
2022-02-22 08:45:28 +00:00
#[cfg(feature = "compress-gzip")]
2019-03-26 22:14:32 +00:00
Gzip(Box<GzDecoder<Writer>>),
2022-02-22 08:45:28 +00:00
#[cfg(feature = "compress-brotli")]
2022-01-15 14:03:16 +00:00
Brotli(Box<brotli::DecompressorWriter<Writer>>),
2022-02-22 08:45:28 +00:00
// 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")]
2022-01-03 13:17:57 +00:00
ContentDecoder::Brotli(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")]
2022-01-03 13:17:57 +00:00
ContentDecoder::Brotli(ref mut decoder) => match decoder.write_all(&data) {
2019-03-26 22:14:32 +00:00
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
}
}
}