mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
192 lines
6.2 KiB
Rust
192 lines
6.2 KiB
Rust
use std::{future::Future, io, io::Write, pin::Pin, task::Context, task::Poll};
|
|
|
|
use flate2::write::{GzDecoder, ZlibDecoder};
|
|
|
|
use super::Writer;
|
|
use crate::http::error::PayloadError;
|
|
use crate::http::header::{ContentEncoding, HeaderMap, CONTENT_ENCODING};
|
|
use crate::rt::{spawn_blocking, JoinHandle};
|
|
use crate::util::{Bytes, Stream};
|
|
|
|
const INPLACE: usize = 2049;
|
|
|
|
pub struct Decoder<S> {
|
|
decoder: Option<ContentDecoder>,
|
|
stream: S,
|
|
eof: bool,
|
|
fut: Option<JoinHandle<Result<(Option<Bytes>, ContentDecoder), io::Error>>>,
|
|
}
|
|
|
|
impl<S> Decoder<S>
|
|
where
|
|
S: Stream<Item = Result<Bytes, PayloadError>>,
|
|
{
|
|
/// Construct a decoder.
|
|
#[inline]
|
|
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
|
|
let decoder = match encoding {
|
|
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
|
|
ZlibDecoder::new(Writer::new()),
|
|
))),
|
|
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(GzDecoder::new(
|
|
Writer::new(),
|
|
)))),
|
|
_ => None,
|
|
};
|
|
Decoder {
|
|
decoder,
|
|
stream,
|
|
fut: None,
|
|
eof: false,
|
|
}
|
|
}
|
|
|
|
/// Construct decoder based on headers.
|
|
#[inline]
|
|
pub fn from_headers(stream: S, headers: &HeaderMap) -> Decoder<S> {
|
|
// check content-encoding
|
|
let encoding = if let Some(enc) = headers.get(&CONTENT_ENCODING) {
|
|
if let Ok(enc) = enc.to_str() {
|
|
ContentEncoding::from(enc)
|
|
} else {
|
|
ContentEncoding::Identity
|
|
}
|
|
} else {
|
|
ContentEncoding::Identity
|
|
};
|
|
|
|
Self::new(stream, encoding)
|
|
}
|
|
}
|
|
|
|
impl<S> Stream for Decoder<S>
|
|
where
|
|
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin,
|
|
{
|
|
type Item = Result<Bytes, PayloadError>;
|
|
|
|
fn poll_next(
|
|
mut self: Pin<&mut Self>,
|
|
cx: &mut Context<'_>,
|
|
) -> Poll<Option<Self::Item>> {
|
|
loop {
|
|
if let Some(ref mut fut) = self.fut {
|
|
let (chunk, decoder) = match Pin::new(fut).poll(cx) {
|
|
Poll::Ready(Ok(Ok(item))) => item,
|
|
Poll::Ready(Ok(Err(e))) => return Poll::Ready(Some(Err(e.into()))),
|
|
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
|
|
Poll::Pending => return Poll::Pending,
|
|
};
|
|
self.decoder = Some(decoder);
|
|
self.fut.take();
|
|
if let Some(chunk) = chunk {
|
|
return Poll::Ready(Some(Ok(chunk)));
|
|
}
|
|
}
|
|
|
|
if self.eof {
|
|
return Poll::Ready(None);
|
|
}
|
|
|
|
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))) => {
|
|
if let Some(mut decoder) = self.decoder.take() {
|
|
if chunk.len() < INPLACE {
|
|
let chunk = decoder.feed_data(chunk)?;
|
|
self.decoder = Some(decoder);
|
|
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))
|
|
}));
|
|
}
|
|
continue;
|
|
} else {
|
|
return Poll::Ready(Some(Ok(chunk)));
|
|
}
|
|
}
|
|
Poll::Ready(None) => {
|
|
self.eof = true;
|
|
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()))),
|
|
}
|
|
} else {
|
|
Poll::Ready(None)
|
|
};
|
|
}
|
|
Poll::Pending => break,
|
|
}
|
|
}
|
|
Poll::Pending
|
|
}
|
|
}
|
|
|
|
enum ContentDecoder {
|
|
Deflate(Box<ZlibDecoder<Writer>>),
|
|
Gzip(Box<GzDecoder<Writer>>),
|
|
}
|
|
|
|
impl ContentDecoder {
|
|
fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
|
|
match self {
|
|
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::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),
|
|
},
|
|
}
|
|
}
|
|
}
|