2019-03-26 15:14:32 -07:00
|
|
|
//! Stream encoder
|
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
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
use actix_threadpool::{run, CpuFuture};
|
2019-12-20 13:50:07 +06:00
|
|
|
use brotli2::write::BrotliEncoder;
|
2019-04-04 13:17:55 -07:00
|
|
|
use bytes::Bytes;
|
2019-03-26 15:14:32 -07:00
|
|
|
use flate2::write::{GzEncoder, ZlibEncoder};
|
2019-12-13 11:24:57 +06:00
|
|
|
use futures_core::ready;
|
2020-06-06 06:44:14 +09:00
|
|
|
use pin_project::pin_project;
|
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};
|
2019-12-05 23:35:43 +06:00
|
|
|
use crate::http::{HeaderValue, 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-12-20 13:50:07 +06:00
|
|
|
const INPLACE: usize = 1024;
|
2019-04-04 13:17:55 -07:00
|
|
|
|
2020-01-29 11:15:13 +03:00
|
|
|
#[pin_project]
|
2019-03-26 15:14:32 -07:00
|
|
|
pub struct Encoder<B> {
|
2019-04-04 15:03:40 -07:00
|
|
|
eof: bool,
|
2020-01-29 11:15:13 +03:00
|
|
|
#[pin]
|
2019-03-26 15:14:32 -07:00
|
|
|
body: EncoderBody<B>,
|
|
|
|
encoder: Option<ContentEncoder>,
|
2019-12-02 23:33:39 +06: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-06 15:02:02 -07:00
|
|
|
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|
2019-04-04 13:17:55 -07:00
|
|
|
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|
2019-06-16 21:54:17 +06:00
|
|
|
|| head.status == StatusCode::NO_CONTENT
|
2019-04-04 13:17:55 -07:00
|
|
|
|| 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 {
|
2019-07-22 11:35:00 +06:00
|
|
|
// Modify response body only if encoder is not None
|
|
|
|
if let Some(enc) = ContentEncoder::encoder(encoding) {
|
|
|
|
update_head(encoding, head);
|
|
|
|
head.no_chunking(false);
|
|
|
|
return ResponseBody::Body(Encoder {
|
|
|
|
body,
|
|
|
|
eof: false,
|
|
|
|
fut: None,
|
|
|
|
encoder: Some(enc),
|
|
|
|
});
|
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
2019-07-22 11:35:00 +06:00
|
|
|
ResponseBody::Body(Encoder {
|
|
|
|
body,
|
|
|
|
eof: false,
|
|
|
|
fut: None,
|
|
|
|
encoder: None,
|
|
|
|
})
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 06:44:14 +09:00
|
|
|
#[pin_project(project = EncoderBodyProj)]
|
2019-03-26 15:14:32 -07:00
|
|
|
enum EncoderBody<B> {
|
2019-04-04 13:17:55 -07:00
|
|
|
Bytes(Bytes),
|
2020-01-29 11:15:13 +03:00
|
|
|
Stream(#[pin] B),
|
2020-02-10 15:19:56 +02:00
|
|
|
BoxedStream(Box<dyn MessageBody + Unpin>),
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
2020-01-29 11:15:13 +03:00
|
|
|
impl<B: MessageBody> MessageBody for EncoderBody<B> {
|
|
|
|
fn size(&self) -> BodySize {
|
|
|
|
match self {
|
|
|
|
EncoderBody::Bytes(ref b) => b.size(),
|
|
|
|
EncoderBody::Stream(ref b) => b.size(),
|
|
|
|
EncoderBody::BoxedStream(ref b) => b.size(),
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 11:10:55 +09:00
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
2020-01-29 11:15:13 +03:00
|
|
|
match self.project() {
|
2020-06-06 06:44:14 +09:00
|
|
|
EncoderBodyProj::Bytes(b) => {
|
2020-01-29 11:15:13 +03:00
|
|
|
if b.is_empty() {
|
|
|
|
Poll::Ready(None)
|
|
|
|
} else {
|
2020-05-17 02:54:42 +01:00
|
|
|
Poll::Ready(Some(Ok(std::mem::take(b))))
|
2020-01-29 11:15:13 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 06:44:14 +09:00
|
|
|
EncoderBodyProj::Stream(b) => b.poll_next(cx),
|
|
|
|
EncoderBodyProj::BoxedStream(ref mut b) => {
|
|
|
|
Pin::new(b.as_mut()).poll_next(cx)
|
|
|
|
}
|
2020-01-29 11:15:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
impl<B: MessageBody> MessageBody for Encoder<B> {
|
2019-04-10 12:24:17 -07:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-26 15:14:32 -07:00
|
|
|
if self.encoder.is_none() {
|
2020-01-29 11:15:13 +03:00
|
|
|
self.body.size()
|
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
|
|
|
}
|
|
|
|
}
|
2020-02-27 11:10:55 +09:00
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
2020-01-29 11:15:13 +03:00
|
|
|
let mut this = self.project();
|
2019-03-26 15:14:32 -07:00
|
|
|
loop {
|
2020-01-29 11:15:13 +03:00
|
|
|
if *this.eof {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(None);
|
2019-04-04 15:03:40 -07:00
|
|
|
}
|
|
|
|
|
2020-01-29 11:15:13 +03:00
|
|
|
if let Some(ref mut fut) = this.fut {
|
2019-12-13 11:24:57 +06:00
|
|
|
let mut encoder = match ready!(Pin::new(fut).poll(cx)) {
|
2019-12-02 23:33:39 +06:00
|
|
|
Ok(item) => item,
|
2019-11-15 15:54:11 +06:00
|
|
|
Err(e) => return Poll::Ready(Some(Err(e.into()))),
|
|
|
|
};
|
2019-04-04 13:17:55 -07:00
|
|
|
let chunk = encoder.take();
|
2020-01-29 11:15:13 +03:00
|
|
|
*this.encoder = Some(encoder);
|
|
|
|
this.fut.take();
|
2019-04-04 13:17:55 -07:00
|
|
|
if !chunk.is_empty() {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-04-04 13:17:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:15:13 +03:00
|
|
|
let result = this.body.as_mut().poll_next(cx);
|
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
match result {
|
2019-11-15 15:54:11 +06:00
|
|
|
Poll::Ready(Some(Ok(chunk))) => {
|
2020-01-29 11:15:13 +03:00
|
|
|
if let Some(mut encoder) = this.encoder.take() {
|
2019-04-04 13:17:55 -07:00
|
|
|
if chunk.len() < INPLACE {
|
|
|
|
encoder.write(&chunk)?;
|
|
|
|
let chunk = encoder.take();
|
2020-01-29 11:15:13 +03:00
|
|
|
*this.encoder = Some(encoder);
|
2019-04-04 13:17:55 -07:00
|
|
|
if !chunk.is_empty() {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-04-04 13:17:55 -07:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-29 11:15:13 +03:00
|
|
|
*this.fut = Some(run(move || {
|
2019-04-04 13:17:55 -07:00
|
|
|
encoder.write(&chunk)?;
|
|
|
|
Ok(encoder)
|
|
|
|
}));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
2019-04-04 15:03:40 -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) => {
|
2020-01-29 11:15:13 +03:00
|
|
|
if let Some(encoder) = this.encoder.take() {
|
2019-03-26 15:14:32 -07:00
|
|
|
let chunk = encoder.finish()?;
|
|
|
|
if chunk.is_empty() {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(None);
|
2019-03-26 15:14:32 -07:00
|
|
|
} else {
|
2020-01-29 11:15:13 +03:00
|
|
|
*this.eof = true;
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(None);
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 15:54:11 +06:00
|
|
|
val => return val,
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
|
|
|
|
head.headers_mut().insert(
|
|
|
|
CONTENT_ENCODING,
|
2019-12-05 23:35:43 +06:00
|
|
|
HeaderValue::from_static(encoding.as_str()),
|
2019-03-26 15:14:32 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ContentEncoder {
|
|
|
|
Deflate(ZlibEncoder<Writer>),
|
|
|
|
Gzip(GzEncoder<Writer>),
|
2019-12-20 13:50:07 +06:00
|
|
|
Br(BrotliEncoder<Writer>),
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContentEncoder {
|
|
|
|
fn encoder(encoding: ContentEncoding) -> Option<Self> {
|
|
|
|
match encoding {
|
|
|
|
ContentEncoding::Deflate => Some(ContentEncoder::Deflate(ZlibEncoder::new(
|
|
|
|
Writer::new(),
|
|
|
|
flate2::Compression::fast(),
|
|
|
|
))),
|
|
|
|
ContentEncoding::Gzip => Some(ContentEncoder::Gzip(GzEncoder::new(
|
|
|
|
Writer::new(),
|
|
|
|
flate2::Compression::fast(),
|
|
|
|
))),
|
2019-12-20 13:50:07 +06:00
|
|
|
ContentEncoding::Br => {
|
|
|
|
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3)))
|
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn take(&mut self) -> Bytes {
|
|
|
|
match *self {
|
2019-12-20 13:50:07 +06:00
|
|
|
ContentEncoder::Br(ref mut encoder) => encoder.get_mut().take(),
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
|
|
|
|
ContentEncoder::Gzip(ref mut encoder) => encoder.get_mut().take(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(self) -> Result<Bytes, io::Error> {
|
|
|
|
match self {
|
2019-12-20 13:50:07 +06:00
|
|
|
ContentEncoder::Br(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Gzip(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|