2019-03-26 15:14:32 -07:00
|
|
|
//! Stream encoder
|
|
|
|
use std::io::{self, Write};
|
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
use actix_threadpool::{run, CpuFuture};
|
2019-03-26 15:14:32 -07:00
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
use brotli2::write::BrotliEncoder;
|
2019-04-04 13:17:55 -07:00
|
|
|
use bytes::Bytes;
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
use flate2::write::{GzEncoder, ZlibEncoder};
|
2019-04-04 13:17:55 -07:00
|
|
|
use futures::{Async, Future, Poll};
|
2019-03-26 15:14:32 -07:00
|
|
|
|
2019-03-27 09:24:55 -07:00
|
|
|
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
|
2019-03-26 15:14:32 -07:00
|
|
|
use crate::http::header::{ContentEncoding, CONTENT_ENCODING};
|
|
|
|
use crate::http::{HeaderValue, HttpTryFrom, StatusCode};
|
2019-03-27 10:38:01 -07:00
|
|
|
use crate::{Error, ResponseHead};
|
2019-03-26 15:14:32 -07:00
|
|
|
|
|
|
|
use super::Writer;
|
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
const INPLACE: usize = 2049;
|
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
pub struct Encoder<B> {
|
2019-04-04 15:03:40 -07:00
|
|
|
eof: bool,
|
2019-03-26 15:14:32 -07:00
|
|
|
body: EncoderBody<B>,
|
|
|
|
encoder: Option<ContentEncoder>,
|
2019-04-04 13:17:55 -07:00
|
|
|
fut: Option<CpuFuture<ContentEncoder, io::Error>>,
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: MessageBody> Encoder<B> {
|
|
|
|
pub fn response(
|
|
|
|
encoding: ContentEncoding,
|
|
|
|
head: &mut ResponseHead,
|
|
|
|
body: ResponseBody<B>,
|
|
|
|
) -> ResponseBody<Encoder<B>> {
|
2019-04-04 13:17:55 -07:00
|
|
|
let can_encode = !(head.headers().contains_key(CONTENT_ENCODING)
|
|
|
|
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|
|
|
|
|| encoding == ContentEncoding::Identity
|
|
|
|
|| encoding == ContentEncoding::Auto);
|
|
|
|
|
|
|
|
let body = match body {
|
2019-03-26 15:14:32 -07:00
|
|
|
ResponseBody::Other(b) => match b {
|
2019-04-04 13:17:55 -07:00
|
|
|
Body::None => return ResponseBody::Other(Body::None),
|
|
|
|
Body::Empty => return ResponseBody::Other(Body::Empty),
|
2019-03-26 15:14:32 -07:00
|
|
|
Body::Bytes(buf) => {
|
2019-04-04 13:17:55 -07:00
|
|
|
if can_encode {
|
|
|
|
EncoderBody::Bytes(buf)
|
2019-03-26 15:14:32 -07:00
|
|
|
} else {
|
2019-04-04 13:17:55 -07:00
|
|
|
return ResponseBody::Other(Body::Bytes(buf));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 13:17:55 -07:00
|
|
|
Body::Message(stream) => EncoderBody::BoxedStream(stream),
|
2019-03-26 15:14:32 -07:00
|
|
|
},
|
2019-04-04 13:17:55 -07:00
|
|
|
ResponseBody::Body(stream) => EncoderBody::Stream(stream),
|
|
|
|
};
|
|
|
|
|
|
|
|
if can_encode {
|
|
|
|
update_head(encoding, head);
|
|
|
|
head.no_chunking(false);
|
|
|
|
ResponseBody::Body(Encoder {
|
|
|
|
body,
|
2019-04-04 15:03:40 -07:00
|
|
|
eof: false,
|
2019-04-04 13:17:55 -07:00
|
|
|
fut: None,
|
|
|
|
encoder: ContentEncoder::encoder(encoding),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
ResponseBody::Body(Encoder {
|
|
|
|
body,
|
2019-04-04 15:03:40 -07:00
|
|
|
eof: false,
|
2019-04-04 13:17:55 -07:00
|
|
|
fut: None,
|
|
|
|
encoder: None,
|
|
|
|
})
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum EncoderBody<B> {
|
2019-04-04 13:17:55 -07:00
|
|
|
Bytes(Bytes),
|
|
|
|
Stream(B),
|
|
|
|
BoxedStream(Box<dyn MessageBody>),
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: MessageBody> MessageBody for Encoder<B> {
|
2019-03-27 09:24:55 -07:00
|
|
|
fn length(&self) -> BodySize {
|
2019-03-26 15:14:32 -07:00
|
|
|
if self.encoder.is_none() {
|
|
|
|
match self.body {
|
2019-04-04 13:17:55 -07:00
|
|
|
EncoderBody::Bytes(ref b) => b.length(),
|
|
|
|
EncoderBody::Stream(ref b) => b.length(),
|
|
|
|
EncoderBody::BoxedStream(ref b) => b.length(),
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-27 09:24:55 -07:00
|
|
|
BodySize::Stream
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_next(&mut self) -> Poll<Option<Bytes>, Error> {
|
|
|
|
loop {
|
2019-04-04 15:03:40 -07:00
|
|
|
if self.eof {
|
|
|
|
return Ok(Async::Ready(None));
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
if let Some(ref mut fut) = self.fut {
|
|
|
|
let mut encoder = futures::try_ready!(fut.poll());
|
|
|
|
let chunk = encoder.take();
|
|
|
|
self.encoder = Some(encoder);
|
|
|
|
self.fut.take();
|
|
|
|
if !chunk.is_empty() {
|
|
|
|
return Ok(Async::Ready(Some(chunk)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
let result = match self.body {
|
2019-04-04 13:17:55 -07:00
|
|
|
EncoderBody::Bytes(ref mut b) => {
|
|
|
|
if b.is_empty() {
|
|
|
|
Async::Ready(None)
|
|
|
|
} else {
|
|
|
|
Async::Ready(Some(std::mem::replace(b, Bytes::new())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EncoderBody::Stream(ref mut b) => b.poll_next()?,
|
|
|
|
EncoderBody::BoxedStream(ref mut b) => b.poll_next()?,
|
2019-03-26 15:14:32 -07:00
|
|
|
};
|
|
|
|
match result {
|
|
|
|
Async::NotReady => return Ok(Async::NotReady),
|
|
|
|
Async::Ready(Some(chunk)) => {
|
2019-04-04 13:17:55 -07:00
|
|
|
if let Some(mut encoder) = self.encoder.take() {
|
|
|
|
if chunk.len() < INPLACE {
|
|
|
|
encoder.write(&chunk)?;
|
|
|
|
let chunk = encoder.take();
|
|
|
|
self.encoder = Some(encoder);
|
|
|
|
if !chunk.is_empty() {
|
|
|
|
return Ok(Async::Ready(Some(chunk)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.fut = Some(run(move || {
|
|
|
|
encoder.write(&chunk)?;
|
|
|
|
Ok(encoder)
|
|
|
|
}));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
2019-04-04 15:03:40 -07:00
|
|
|
} else {
|
|
|
|
return Ok(Async::Ready(Some(chunk)));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Async::Ready(None) => {
|
|
|
|
if let Some(encoder) = self.encoder.take() {
|
|
|
|
let chunk = encoder.finish()?;
|
|
|
|
if chunk.is_empty() {
|
|
|
|
return Ok(Async::Ready(None));
|
|
|
|
} else {
|
2019-04-04 15:03:40 -07:00
|
|
|
self.eof = true;
|
2019-03-26 15:14:32 -07:00
|
|
|
return Ok(Async::Ready(Some(chunk)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Async::Ready(None));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
|
|
|
|
head.headers_mut().insert(
|
|
|
|
CONTENT_ENCODING,
|
|
|
|
HeaderValue::try_from(Bytes::from_static(encoding.as_str().as_bytes())).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ContentEncoder {
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
Deflate(ZlibEncoder<Writer>),
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
Gzip(GzEncoder<Writer>),
|
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
Br(BrotliEncoder<Writer>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContentEncoder {
|
|
|
|
fn encoder(encoding: ContentEncoding) -> Option<Self> {
|
|
|
|
match encoding {
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoding::Deflate => Some(ContentEncoder::Deflate(ZlibEncoder::new(
|
|
|
|
Writer::new(),
|
|
|
|
flate2::Compression::fast(),
|
|
|
|
))),
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoding::Gzip => Some(ContentEncoder::Gzip(GzEncoder::new(
|
|
|
|
Writer::new(),
|
|
|
|
flate2::Compression::fast(),
|
|
|
|
))),
|
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
ContentEncoding::Br => {
|
|
|
|
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3)))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn take(&mut self) -> Bytes {
|
|
|
|
match *self {
|
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
ContentEncoder::Br(ref mut encoder) => encoder.get_mut().take(),
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Gzip(ref mut encoder) => encoder.get_mut().take(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(self) -> Result<Bytes, io::Error> {
|
|
|
|
match self {
|
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
ContentEncoder::Br(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Gzip(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Deflate(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
fn write(&mut self, data: &[u8]) -> Result<(), io::Error> {
|
2019-03-26 15:14:32 -07:00
|
|
|
match *self {
|
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
ContentEncoder::Br(ref mut encoder) => match encoder.write_all(data) {
|
2019-04-04 13:17:55 -07:00
|
|
|
Ok(_) => Ok(()),
|
2019-03-26 15:14:32 -07:00
|
|
|
Err(err) => {
|
|
|
|
trace!("Error decoding br encoding: {}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Gzip(ref mut encoder) => match encoder.write_all(data) {
|
2019-04-04 13:17:55 -07:00
|
|
|
Ok(_) => Ok(()),
|
2019-03-26 15:14:32 -07:00
|
|
|
Err(err) => {
|
|
|
|
trace!("Error decoding gzip encoding: {}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
2019-03-26 18:46:06 -07:00
|
|
|
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Deflate(ref mut encoder) => match encoder.write_all(data) {
|
2019-04-04 13:17:55 -07:00
|
|
|
Ok(_) => Ok(()),
|
2019-03-26 15:14:32 -07:00
|
|
|
Err(err) => {
|
|
|
|
trace!("Error decoding deflate encoding: {}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|