1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 00:43:00 +01:00
actix-web/src/server/encoding.rs

874 lines
28 KiB
Rust
Raw Normal View History

2017-11-09 01:44:23 +01:00
use std::{io, cmp, mem};
2018-01-12 21:31:33 +01:00
use std::io::{Read, Write};
2017-11-09 01:44:23 +01:00
use std::fmt::Write as FmtWrite;
use std::str::FromStr;
use http::Version;
use http::header::{HeaderMap, HeaderValue,
ACCEPT_ENCODING, CONNECTION,
CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING};
use flate2::Compression;
2018-01-12 21:31:33 +01:00
use flate2::read::GzDecoder;
use flate2::write::{GzEncoder, DeflateDecoder, DeflateEncoder};
2017-11-09 01:44:23 +01:00
use brotli2::write::{BrotliDecoder, BrotliEncoder};
2017-11-07 01:23:58 +01:00
use bytes::{Bytes, BytesMut, BufMut, Writer};
2018-01-12 03:41:33 +01:00
use headers::ContentEncoding;
use body::{Body, Binary};
2017-11-16 07:06:28 +01:00
use error::PayloadError;
2017-12-16 16:29:15 +01:00
use helpers::SharedBytes;
2017-12-09 13:33:40 +01:00
use httprequest::HttpMessage;
2017-11-09 01:44:23 +01:00
use httpresponse::HttpResponse;
2017-11-16 07:06:28 +01:00
use payload::{PayloadSender, PayloadWriter};
2017-11-07 01:23:58 +01:00
2017-11-09 01:44:23 +01:00
impl ContentEncoding {
2017-12-14 01:44:35 +01:00
2018-01-11 06:02:28 +01:00
#[inline]
2017-12-14 01:44:35 +01:00
fn is_compression(&self) -> bool {
match *self {
ContentEncoding::Identity | ContentEncoding::Auto => false,
_ => true
}
}
2017-11-09 01:44:23 +01:00
fn as_str(&self) -> &'static str {
match *self {
ContentEncoding::Br => "br",
ContentEncoding::Gzip => "gzip",
ContentEncoding::Deflate => "deflate",
ContentEncoding::Identity | ContentEncoding::Auto => "identity",
}
}
2018-01-11 06:02:28 +01:00
/// default quality value
2017-11-09 04:42:13 +01:00
fn quality(&self) -> f64 {
match *self {
ContentEncoding::Br => 1.1,
ContentEncoding::Gzip => 1.0,
ContentEncoding::Deflate => 0.9,
ContentEncoding::Identity | ContentEncoding::Auto => 0.1,
}
}
2017-11-09 01:44:23 +01:00
}
2018-01-11 06:02:28 +01:00
// TODO: remove memory allocation
2017-11-07 01:23:58 +01:00
impl<'a> From<&'a str> for ContentEncoding {
fn from(s: &'a str) -> ContentEncoding {
match s.trim().to_lowercase().as_ref() {
"br" => ContentEncoding::Br,
"gzip" => ContentEncoding::Gzip,
"deflate" => ContentEncoding::Deflate,
"identity" => ContentEncoding::Identity,
_ => ContentEncoding::Auto,
}
}
}
pub(crate) enum PayloadType {
Sender(PayloadSender),
2017-11-27 02:30:35 +01:00
Encoding(Box<EncodedPayload>),
2017-11-07 01:23:58 +01:00
}
impl PayloadType {
pub fn new(headers: &HeaderMap, sender: PayloadSender) -> PayloadType {
// check content-encoding
let enc = if let Some(enc) = headers.get(CONTENT_ENCODING) {
if let Ok(enc) = enc.to_str() {
ContentEncoding::from(enc)
} else {
ContentEncoding::Auto
}
} else {
ContentEncoding::Auto
};
match enc {
ContentEncoding::Auto | ContentEncoding::Identity =>
PayloadType::Sender(sender),
2017-11-27 02:30:35 +01:00
_ => PayloadType::Encoding(Box::new(EncodedPayload::new(sender, enc))),
2017-11-07 01:23:58 +01:00
}
}
}
impl PayloadWriter for PayloadType {
fn set_error(&mut self, err: PayloadError) {
match *self {
PayloadType::Sender(ref mut sender) => sender.set_error(err),
PayloadType::Encoding(ref mut enc) => enc.set_error(err),
}
}
fn feed_eof(&mut self) {
match *self {
PayloadType::Sender(ref mut sender) => sender.feed_eof(),
PayloadType::Encoding(ref mut enc) => enc.feed_eof(),
}
}
fn feed_data(&mut self, data: Bytes) {
match *self {
PayloadType::Sender(ref mut sender) => sender.feed_data(data),
PayloadType::Encoding(ref mut enc) => enc.feed_data(data),
}
}
fn capacity(&self) -> usize {
match *self {
PayloadType::Sender(ref sender) => sender.capacity(),
PayloadType::Encoding(ref enc) => enc.capacity(),
}
}
}
enum Decoder {
2017-12-13 20:10:03 +01:00
Deflate(Box<DeflateDecoder<Writer<BytesMut>>>),
2018-01-12 21:31:33 +01:00
Gzip(Option<Box<GzDecoder<Wrapper>>>),
2017-12-13 20:10:03 +01:00
Br(Box<BrotliDecoder<Writer<BytesMut>>>),
2017-11-07 01:23:58 +01:00
Identity,
}
2018-01-12 21:31:33 +01:00
// should go after write::GzDecoder get implemented
#[derive(Debug)]
struct Wrapper {
buf: BytesMut,
eof: bool,
}
impl io::Read for Wrapper {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = cmp::min(buf.len(), self.buf.len());
buf[..len].copy_from_slice(&self.buf[..len]);
self.buf.split_to(len);
if len == 0 {
if self.eof {
Ok(0)
} else {
Err(io::Error::new(io::ErrorKind::WouldBlock, ""))
}
} else {
Ok(len)
}
}
}
impl io::Write for Wrapper {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buf.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
2017-11-09 01:44:23 +01:00
/// Payload wrapper with content decompression support
2017-11-07 01:23:58 +01:00
pub(crate) struct EncodedPayload {
inner: PayloadSender,
decoder: Decoder,
2018-01-12 21:31:33 +01:00
dst: BytesMut,
2017-11-07 01:23:58 +01:00
error: bool,
}
impl EncodedPayload {
pub fn new(inner: PayloadSender, enc: ContentEncoding) -> EncodedPayload {
let dec = match enc {
2017-11-08 00:59:37 +01:00
ContentEncoding::Br => Decoder::Br(
2017-12-13 20:10:03 +01:00
Box::new(BrotliDecoder::new(BytesMut::with_capacity(8192).writer()))),
2017-11-08 01:08:10 +01:00
ContentEncoding::Deflate => Decoder::Deflate(
2017-12-13 20:10:03 +01:00
Box::new(DeflateDecoder::new(BytesMut::with_capacity(8192).writer()))),
2018-01-12 21:31:33 +01:00
ContentEncoding::Gzip => Decoder::Gzip(None),
2017-11-07 01:23:58 +01:00
_ => Decoder::Identity,
};
2018-01-12 21:31:33 +01:00
EncodedPayload{ inner: inner, decoder: dec, error: false, dst: BytesMut::new() }
2017-11-07 01:23:58 +01:00
}
}
impl PayloadWriter for EncodedPayload {
fn set_error(&mut self, err: PayloadError) {
self.inner.set_error(err)
}
fn feed_eof(&mut self) {
if self.error {
return
}
let err = match self.decoder {
2017-11-08 00:59:37 +01:00
Decoder::Br(ref mut decoder) => {
match decoder.finish() {
Ok(mut writer) => {
2017-11-09 05:08:16 +01:00
let b = writer.get_mut().take().freeze();
2017-11-07 01:23:58 +01:00
if !b.is_empty() {
self.inner.feed_data(b);
}
self.inner.feed_eof();
return
},
Err(err) => Some(err),
}
2017-11-08 00:59:37 +01:00
},
2017-11-07 01:23:58 +01:00
Decoder::Gzip(ref mut decoder) => {
2018-01-12 21:31:33 +01:00
if let Some(ref mut decoder) = *decoder {
decoder.as_mut().get_mut().eof = true;
loop {
self.dst.reserve(8192);
match decoder.read(unsafe{self.dst.bytes_mut()}) {
Ok(n) => {
if n == 0 {
self.inner.feed_eof();
return
} else {
unsafe{self.dst.set_len(n)};
self.inner.feed_data(self.dst.split_to(n).freeze());
}
}
Err(err) => {
break Some(err);
}
2017-11-07 01:23:58 +01:00
}
2018-01-12 21:31:33 +01:00
}
} else {
return
2017-11-07 01:23:58 +01:00
}
2017-11-08 00:59:37 +01:00
},
2017-11-08 01:08:10 +01:00
Decoder::Deflate(ref mut decoder) => {
2017-11-08 00:59:37 +01:00
match decoder.try_finish() {
2017-11-07 01:23:58 +01:00
Ok(_) => {
2017-11-09 05:08:16 +01:00
let b = decoder.get_mut().get_mut().take().freeze();
2017-11-07 01:23:58 +01:00
if !b.is_empty() {
self.inner.feed_data(b);
}
self.inner.feed_eof();
return
},
Err(err) => Some(err),
}
},
Decoder::Identity => {
self.inner.feed_eof();
return
}
};
self.error = true;
self.decoder = Decoder::Identity;
if let Some(err) = err {
self.set_error(PayloadError::ParseError(err));
} else {
self.set_error(PayloadError::Incomplete);
}
}
fn feed_data(&mut self, data: Bytes) {
if self.error {
return
}
match self.decoder {
2017-11-08 00:59:37 +01:00
Decoder::Br(ref mut decoder) => {
2017-11-09 01:44:23 +01:00
if decoder.write(&data).is_ok() && decoder.flush().is_ok() {
2017-11-09 05:08:16 +01:00
let b = decoder.get_mut().get_mut().take().freeze();
2017-11-09 01:44:23 +01:00
if !b.is_empty() {
self.inner.feed_data(b);
2017-11-08 00:59:37 +01:00
}
2017-11-09 01:44:23 +01:00
return
2017-11-07 01:23:58 +01:00
}
2017-11-08 00:59:37 +01:00
trace!("Error decoding br encoding");
2017-11-07 01:23:58 +01:00
}
Decoder::Gzip(ref mut decoder) => {
2018-01-12 21:31:33 +01:00
if decoder.is_none() {
*decoder = Some(
Box::new(GzDecoder::new(
Wrapper{buf: BytesMut::from(data), eof: false})));
} else {
let _ = decoder.as_mut().unwrap().write(&data);
}
loop {
self.dst.reserve(8192);
match decoder.as_mut().as_mut().unwrap().read(unsafe{self.dst.bytes_mut()}) {
Ok(n) => {
if n == 0 {
return
} else {
unsafe{self.dst.set_len(n)};
self.inner.feed_data(self.dst.split_to(n).freeze());
}
}
Err(e) => {
if e.kind() == io::ErrorKind::WouldBlock {
return
}
break
}
2017-11-07 01:23:58 +01:00
}
}
}
2017-11-08 01:08:10 +01:00
Decoder::Deflate(ref mut decoder) => {
2017-11-09 01:44:23 +01:00
if decoder.write(&data).is_ok() && decoder.flush().is_ok() {
2017-11-09 05:08:16 +01:00
let b = decoder.get_mut().get_mut().take().freeze();
2017-11-09 01:44:23 +01:00
if !b.is_empty() {
self.inner.feed_data(b);
2017-11-08 00:59:37 +01:00
}
2017-11-09 01:44:23 +01:00
return
2017-11-07 01:23:58 +01:00
}
2017-11-08 00:59:37 +01:00
trace!("Error decoding deflate encoding");
2017-11-07 01:23:58 +01:00
}
Decoder::Identity => {
self.inner.feed_data(data);
return
}
};
self.error = true;
self.decoder = Decoder::Identity;
self.set_error(PayloadError::EncodingCorrupted);
}
fn capacity(&self) -> usize {
2017-11-08 00:59:37 +01:00
self.inner.capacity()
2017-11-07 01:23:58 +01:00
}
}
2017-11-09 01:44:23 +01:00
pub(crate) struct PayloadEncoder(ContentEncoder);
impl PayloadEncoder {
2017-12-15 04:34:31 +01:00
pub fn empty(bytes: SharedBytes) -> PayloadEncoder {
PayloadEncoder(ContentEncoder::Identity(TransferEncoding::eof(bytes)))
}
2018-01-11 07:42:26 +01:00
pub fn new(buf: SharedBytes, req: &HttpMessage, resp: &mut HttpResponse) -> PayloadEncoder {
2017-12-09 13:33:40 +01:00
let version = resp.version().unwrap_or_else(|| req.version);
let mut body = resp.replace_body(Body::Empty);
let has_body = match body {
Body::Empty => false,
2018-01-11 07:42:26 +01:00
Body::Binary(ref bin) => bin.len() >= 512,
_ => true,
};
2017-11-09 01:44:23 +01:00
// Enable content encoding only if response does not contain Content-Encoding header
2018-01-11 07:42:26 +01:00
let mut encoding = if has_body {
2017-11-09 01:44:23 +01:00
let encoding = match *resp.content_encoding() {
ContentEncoding::Auto => {
// negotiate content-encoding
2017-12-09 13:33:40 +01:00
if let Some(val) = req.headers.get(ACCEPT_ENCODING) {
2017-11-09 01:44:23 +01:00
if let Ok(enc) = val.to_str() {
AcceptEncoding::parse(enc)
} else {
ContentEncoding::Identity
}
} else {
ContentEncoding::Identity
}
}
encoding => encoding,
};
2017-12-14 01:44:35 +01:00
if encoding.is_compression() {
resp.headers_mut().insert(
CONTENT_ENCODING, HeaderValue::from_static(encoding.as_str()));
}
2017-11-09 01:44:23 +01:00
encoding
} else {
ContentEncoding::Identity
};
let transfer = match body {
Body::Empty => {
if resp.chunked() {
error!("Chunked transfer is enabled but body is set to Empty");
}
2017-12-16 16:29:15 +01:00
resp.headers_mut().remove(CONTENT_LENGTH);
2017-12-15 04:34:31 +01:00
TransferEncoding::eof(buf)
2017-11-09 01:44:23 +01:00
},
Body::Binary(ref mut bytes) => {
2018-01-11 07:42:26 +01:00
if encoding.is_compression() {
let tmp = SharedBytes::default();
let transfer = TransferEncoding::eof(tmp.clone());
let mut enc = match encoding {
ContentEncoding::Deflate => ContentEncoder::Deflate(
2018-01-11 05:28:06 +01:00
DeflateEncoder::new(transfer, Compression::default())),
ContentEncoding::Gzip => ContentEncoder::Gzip(
2018-01-11 05:28:06 +01:00
GzEncoder::new(transfer, Compression::default())),
ContentEncoding::Br => ContentEncoder::Br(
BrotliEncoder::new(transfer, 5)),
ContentEncoding::Identity => ContentEncoder::Identity(transfer),
ContentEncoding::Auto => unreachable!()
};
// TODO return error!
let _ = enc.write(bytes.as_ref());
let _ = enc.write_eof();
2018-01-11 07:42:26 +01:00
*bytes = Binary::from(tmp.get_mut().take());
encoding = ContentEncoding::Identity;
2017-11-09 01:44:23 +01:00
}
2017-12-16 16:29:15 +01:00
resp.headers_mut().remove(CONTENT_LENGTH);
TransferEncoding::eof(buf)
2017-11-09 01:44:23 +01:00
}
2018-01-01 02:26:32 +01:00
Body::Streaming(_) | Body::Actor(_) => {
if resp.upgrade() {
if version == Version::HTTP_2 {
error!("Connection upgrade is forbidden for HTTP/2");
} else {
resp.headers_mut().insert(
CONNECTION, HeaderValue::from_static("upgrade"));
}
if encoding != ContentEncoding::Identity {
encoding = ContentEncoding::Identity;
resp.headers_mut().remove(CONTENT_ENCODING);
}
TransferEncoding::eof(buf)
2017-11-09 01:44:23 +01:00
} else {
2018-01-12 00:26:46 +01:00
PayloadEncoder::streaming_encoding(buf, version, resp)
2017-11-09 01:44:23 +01:00
}
}
};
resp.replace_body(body);
PayloadEncoder(
match encoding {
ContentEncoding::Deflate => ContentEncoder::Deflate(
2018-01-11 05:28:06 +01:00
DeflateEncoder::new(transfer, Compression::default())),
2017-11-09 01:44:23 +01:00
ContentEncoding::Gzip => ContentEncoder::Gzip(
2018-01-11 05:28:06 +01:00
GzEncoder::new(transfer, Compression::default())),
2017-11-09 01:44:23 +01:00
ContentEncoding::Br => ContentEncoder::Br(
2017-11-09 04:42:13 +01:00
BrotliEncoder::new(transfer, 5)),
2017-11-09 01:44:23 +01:00
ContentEncoding::Identity => ContentEncoder::Identity(transfer),
2018-01-11 06:02:28 +01:00
ContentEncoding::Auto => unreachable!()
2017-11-09 01:44:23 +01:00
}
)
}
2018-01-12 00:26:46 +01:00
fn streaming_encoding(buf: SharedBytes, version: Version,
resp: &mut HttpResponse) -> TransferEncoding {
if resp.chunked() {
// Enable transfer encoding
resp.headers_mut().remove(CONTENT_LENGTH);
if version == Version::HTTP_2 {
resp.headers_mut().remove(TRANSFER_ENCODING);
TransferEncoding::eof(buf)
} else {
resp.headers_mut().insert(
TRANSFER_ENCODING, HeaderValue::from_static("chunked"));
TransferEncoding::chunked(buf)
}
} else {
// if Content-Length is specified, then use it as length hint
let (len, chunked) =
if let Some(len) = resp.headers().get(CONTENT_LENGTH) {
// Content-Length
if let Ok(s) = len.to_str() {
if let Ok(len) = s.parse::<u64>() {
(Some(len), false)
} else {
error!("illegal Content-Length: {:?}", len);
(None, false)
}
} else {
error!("illegal Content-Length: {:?}", len);
(None, false)
}
} else {
(None, true)
};
if !chunked {
if let Some(len) = len {
TransferEncoding::length(len, buf)
} else {
TransferEncoding::eof(buf)
}
} else {
// Enable transfer encoding
match version {
Version::HTTP_11 => {
resp.headers_mut().insert(
TRANSFER_ENCODING, HeaderValue::from_static("chunked"));
TransferEncoding::chunked(buf)
},
_ => {
resp.headers_mut().remove(TRANSFER_ENCODING);
TransferEncoding::eof(buf)
}
2018-01-12 00:26:46 +01:00
}
}
}
}
2017-11-09 01:44:23 +01:00
}
impl PayloadEncoder {
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn len(&self) -> usize {
self.0.get_ref().len()
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn get_mut(&mut self) -> &mut BytesMut {
self.0.get_mut()
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn is_eof(&self) -> bool {
self.0.is_eof()
}
2017-12-14 01:44:35 +01:00
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
#[inline(always)]
2017-11-09 01:44:23 +01:00
pub fn write(&mut self, payload: &[u8]) -> Result<(), io::Error> {
self.0.write(payload)
}
2017-12-14 01:44:35 +01:00
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
#[inline(always)]
2017-11-09 01:44:23 +01:00
pub fn write_eof(&mut self) -> Result<(), io::Error> {
self.0.write_eof()
}
}
enum ContentEncoder {
Deflate(DeflateEncoder<TransferEncoding>),
Gzip(GzEncoder<TransferEncoding>),
Br(BrotliEncoder<TransferEncoding>),
Identity(TransferEncoding),
}
impl ContentEncoder {
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn is_eof(&self) -> bool {
match *self {
ContentEncoder::Br(ref encoder) =>
encoder.get_ref().is_eof(),
ContentEncoder::Deflate(ref encoder) =>
encoder.get_ref().is_eof(),
ContentEncoder::Gzip(ref encoder) =>
encoder.get_ref().is_eof(),
ContentEncoder::Identity(ref encoder) =>
encoder.is_eof(),
}
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn get_ref(&self) -> &BytesMut {
match *self {
ContentEncoder::Br(ref encoder) =>
2017-12-15 04:34:31 +01:00
encoder.get_ref().buffer.get_ref(),
2017-11-09 01:44:23 +01:00
ContentEncoder::Deflate(ref encoder) =>
2017-12-15 04:34:31 +01:00
encoder.get_ref().buffer.get_ref(),
2017-11-09 01:44:23 +01:00
ContentEncoder::Gzip(ref encoder) =>
2017-12-15 04:34:31 +01:00
encoder.get_ref().buffer.get_ref(),
2017-11-09 01:44:23 +01:00
ContentEncoder::Identity(ref encoder) =>
2017-12-15 04:34:31 +01:00
encoder.buffer.get_ref(),
2017-11-09 01:44:23 +01:00
}
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn get_mut(&mut self) -> &mut BytesMut {
match *self {
ContentEncoder::Br(ref mut encoder) =>
2017-12-15 04:34:31 +01:00
encoder.get_mut().buffer.get_mut(),
2017-11-09 01:44:23 +01:00
ContentEncoder::Deflate(ref mut encoder) =>
2017-12-15 04:34:31 +01:00
encoder.get_mut().buffer.get_mut(),
2017-11-09 01:44:23 +01:00
ContentEncoder::Gzip(ref mut encoder) =>
2017-12-15 04:34:31 +01:00
encoder.get_mut().buffer.get_mut(),
2017-11-09 01:44:23 +01:00
ContentEncoder::Identity(ref mut encoder) =>
2017-12-15 04:34:31 +01:00
encoder.buffer.get_mut(),
2017-11-09 01:44:23 +01:00
}
}
2017-12-14 01:44:35 +01:00
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
2017-12-13 21:47:07 +01:00
#[inline(always)]
2017-11-09 01:44:23 +01:00
pub fn write_eof(&mut self) -> Result<(), io::Error> {
2017-12-15 04:34:31 +01:00
let encoder = mem::replace(
2017-12-15 05:48:31 +01:00
self, ContentEncoder::Identity(TransferEncoding::eof(SharedBytes::empty())));
2017-11-09 01:44:23 +01:00
match encoder {
ContentEncoder::Br(encoder) => {
match encoder.finish() {
Ok(mut writer) => {
writer.encode_eof();
2018-01-02 23:53:51 +01:00
*self = ContentEncoder::Identity(writer);
2017-11-09 01:44:23 +01:00
Ok(())
},
Err(err) => Err(err),
}
}
ContentEncoder::Gzip(encoder) => {
match encoder.finish() {
Ok(mut writer) => {
writer.encode_eof();
2018-01-02 23:53:51 +01:00
*self = ContentEncoder::Identity(writer);
2017-11-09 01:44:23 +01:00
Ok(())
},
Err(err) => Err(err),
}
},
ContentEncoder::Deflate(encoder) => {
match encoder.finish() {
Ok(mut writer) => {
writer.encode_eof();
2018-01-02 23:53:51 +01:00
*self = ContentEncoder::Identity(writer);
2017-11-09 01:44:23 +01:00
Ok(())
},
Err(err) => Err(err),
}
},
ContentEncoder::Identity(mut writer) => {
writer.encode_eof();
2018-01-02 23:53:51 +01:00
*self = ContentEncoder::Identity(writer);
2017-11-09 01:44:23 +01:00
Ok(())
}
}
}
2017-12-14 01:44:35 +01:00
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
2017-12-13 21:47:07 +01:00
#[inline(always)]
2017-11-09 01:44:23 +01:00
pub fn write(&mut self, data: &[u8]) -> Result<(), io::Error> {
match *self {
ContentEncoder::Br(ref mut encoder) => {
match encoder.write(data) {
2017-12-13 06:32:58 +01:00
Ok(_) =>
encoder.flush(),
2017-11-09 01:44:23 +01:00
Err(err) => {
trace!("Error decoding br encoding: {}", err);
Err(err)
},
}
},
ContentEncoder::Gzip(ref mut encoder) => {
match encoder.write(data) {
2017-12-13 06:32:58 +01:00
Ok(_) =>
encoder.flush(),
2017-11-09 01:44:23 +01:00
Err(err) => {
2017-12-13 06:32:58 +01:00
trace!("Error decoding gzip encoding: {}", err);
2017-11-09 01:44:23 +01:00
Err(err)
},
}
}
ContentEncoder::Deflate(ref mut encoder) => {
match encoder.write(data) {
2017-12-13 06:32:58 +01:00
Ok(_) =>
encoder.flush(),
2017-11-09 01:44:23 +01:00
Err(err) => {
trace!("Error decoding deflate encoding: {}", err);
Err(err)
},
}
}
ContentEncoder::Identity(ref mut encoder) => {
2018-01-04 18:32:15 +01:00
encoder.encode(data)?;
2017-11-09 01:44:23 +01:00
Ok(())
}
}
}
}
/// Encoders to handle different Transfer-Encodings.
#[derive(Debug, Clone)]
pub(crate) struct TransferEncoding {
kind: TransferEncodingKind,
2017-12-15 04:34:31 +01:00
buffer: SharedBytes,
2017-11-09 01:44:23 +01:00
}
#[derive(Debug, PartialEq, Clone)]
enum TransferEncodingKind {
/// An Encoder for when Transfer-Encoding includes `chunked`.
Chunked(bool),
/// An Encoder for when Content-Length is set.
///
/// Enforces that the body is not longer than the Content-Length header.
Length(u64),
/// An Encoder for when Content-Length is not known.
///
/// Appliction decides when to stop writing.
Eof,
}
impl TransferEncoding {
2017-12-13 06:32:58 +01:00
#[inline]
2017-12-15 04:34:31 +01:00
pub fn eof(bytes: SharedBytes) -> TransferEncoding {
2017-11-09 01:44:23 +01:00
TransferEncoding {
kind: TransferEncodingKind::Eof,
2017-12-15 04:34:31 +01:00
buffer: bytes,
2017-11-09 01:44:23 +01:00
}
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-12-15 04:34:31 +01:00
pub fn chunked(bytes: SharedBytes) -> TransferEncoding {
2017-11-09 01:44:23 +01:00
TransferEncoding {
kind: TransferEncodingKind::Chunked(false),
2017-12-15 04:34:31 +01:00
buffer: bytes,
2017-11-09 01:44:23 +01:00
}
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-12-15 04:34:31 +01:00
pub fn length(len: u64, bytes: SharedBytes) -> TransferEncoding {
2017-11-09 01:44:23 +01:00
TransferEncoding {
kind: TransferEncodingKind::Length(len),
2017-12-15 04:34:31 +01:00
buffer: bytes,
2017-11-09 01:44:23 +01:00
}
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn is_eof(&self) -> bool {
match self.kind {
TransferEncodingKind::Eof => true,
2018-01-11 06:02:28 +01:00
TransferEncodingKind::Chunked(ref eof) => *eof,
TransferEncodingKind::Length(ref remaining) => *remaining == 0,
2017-11-09 01:44:23 +01:00
}
}
/// Encode message. Return `EOF` state of encoder
2017-12-14 01:44:35 +01:00
#[inline]
2018-01-04 18:32:15 +01:00
pub fn encode(&mut self, msg: &[u8]) -> io::Result<bool> {
2017-11-09 01:44:23 +01:00
match self.kind {
TransferEncodingKind::Eof => {
2017-12-15 04:34:31 +01:00
self.buffer.get_mut().extend_from_slice(msg);
2018-01-04 18:32:15 +01:00
Ok(msg.is_empty())
2017-11-09 01:44:23 +01:00
},
TransferEncodingKind::Chunked(ref mut eof) => {
if *eof {
2018-01-04 18:32:15 +01:00
return Ok(true);
2017-11-09 01:44:23 +01:00
}
if msg.is_empty() {
*eof = true;
2017-12-15 04:34:31 +01:00
self.buffer.get_mut().extend_from_slice(b"0\r\n\r\n");
2017-11-09 01:44:23 +01:00
} else {
2018-01-04 18:32:15 +01:00
write!(self.buffer.get_mut(), "{:X}\r\n", msg.len())
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
2017-12-15 04:34:31 +01:00
self.buffer.get_mut().extend_from_slice(msg);
self.buffer.get_mut().extend_from_slice(b"\r\n");
2017-11-09 01:44:23 +01:00
}
2018-01-04 18:32:15 +01:00
Ok(*eof)
2017-11-09 01:44:23 +01:00
},
TransferEncodingKind::Length(ref mut remaining) => {
if msg.is_empty() {
2018-01-04 18:32:15 +01:00
return Ok(*remaining == 0)
2017-11-09 01:44:23 +01:00
}
let max = cmp::min(*remaining, msg.len() as u64);
2017-12-15 04:34:31 +01:00
self.buffer.get_mut().extend_from_slice(msg[..max as usize].as_ref());
2017-11-09 01:44:23 +01:00
*remaining -= max as u64;
2018-01-04 18:32:15 +01:00
Ok(*remaining == 0)
2017-11-09 01:44:23 +01:00
},
}
}
/// Encode eof. Return `EOF` state of encoder
2017-12-14 01:44:35 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
pub fn encode_eof(&mut self) {
match self.kind {
TransferEncodingKind::Eof | TransferEncodingKind::Length(_) => (),
TransferEncodingKind::Chunked(ref mut eof) => {
if !*eof {
*eof = true;
2017-12-15 04:34:31 +01:00
self.buffer.get_mut().extend_from_slice(b"0\r\n\r\n");
2017-11-09 01:44:23 +01:00
}
},
}
}
}
impl io::Write for TransferEncoding {
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2018-01-04 18:32:15 +01:00
self.encode(buf)?;
2017-11-09 01:44:23 +01:00
Ok(buf.len())
}
2017-12-13 06:32:58 +01:00
#[inline]
2017-11-09 01:44:23 +01:00
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
struct AcceptEncoding {
encoding: ContentEncoding,
quality: f64,
}
impl Eq for AcceptEncoding {}
impl Ord for AcceptEncoding {
fn cmp(&self, other: &AcceptEncoding) -> cmp::Ordering {
if self.quality > other.quality {
cmp::Ordering::Less
} else if self.quality < other.quality {
cmp::Ordering::Greater
} else {
cmp::Ordering::Equal
}
}
}
impl PartialOrd for AcceptEncoding {
fn partial_cmp(&self, other: &AcceptEncoding) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for AcceptEncoding {
fn eq(&self, other: &AcceptEncoding) -> bool {
self.quality == other.quality
}
}
impl AcceptEncoding {
fn new(tag: &str) -> Option<AcceptEncoding> {
let parts: Vec<&str> = tag.split(';').collect();
let encoding = match parts.len() {
0 => return None,
_ => ContentEncoding::from(parts[0]),
};
let quality = match parts.len() {
2017-11-09 04:42:13 +01:00
1 => encoding.quality(),
2017-11-09 01:44:23 +01:00
_ => match f64::from_str(parts[1]) {
Ok(q) => q,
Err(_) => 0.0,
}
};
Some(AcceptEncoding {
encoding: encoding,
quality: quality,
})
}
/// Parse a raw Accept-Encoding header value into an ordered list.
pub fn parse(raw: &str) -> ContentEncoding {
let mut encodings: Vec<_> =
raw.replace(' ', "").split(',').map(|l| AcceptEncoding::new(l)).collect();
encodings.sort();
for enc in encodings {
if let Some(enc) = enc {
return enc.encoding
}
}
ContentEncoding::Identity
}
}
2018-01-04 18:32:15 +01:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chunked_te() {
let bytes = SharedBytes::default();
let mut enc = TransferEncoding::chunked(bytes.clone());
assert!(!enc.encode(b"test").ok().unwrap());
assert!(enc.encode(b"").ok().unwrap());
assert_eq!(bytes.get_mut().take().freeze(),
Bytes::from_static(b"4\r\ntest\r\n0\r\n\r\n"));
}
}