From 69742130364180798b89f37e354fc613096e01bb Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 7 Nov 2017 15:59:37 -0800 Subject: [PATCH] use new brotli2 version --- Cargo.toml | 4 +-- src/encoding.rs | 76 ++++++++++++++----------------------------------- 2 files changed, 23 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5697abb01..763d646da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ regex = "0.2" sha1 = "0.2" url = "1.5" flate2 = "0.2" -brotli2 = "0.3" +brotli2 = "^0.3.2" percent-encoding = "1.0" # tokio @@ -64,7 +64,7 @@ tokio-tls = { version="0.1", optional = true } tokio-openssl = { version="0.1", optional = true } [dependencies.actix] -version = ">=0.3.1" +version = "^0.3.1" #path = "../actix" #git = "https://github.com/actix/actix.git" default-features = false diff --git a/src/encoding.rs b/src/encoding.rs index 55b3981df..45f123197 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -1,6 +1,4 @@ use std::{io, cmp}; -use std::rc::Rc; -use std::cell::RefCell; use std::io::{Read, Write}; use http::header::{HeaderMap, CONTENT_ENCODING}; @@ -99,7 +97,7 @@ impl PayloadWriter for PayloadType { enum Decoder { Zlib(DeflateDecoder), Gzip(Option>), - Br(Rc>, BrotliDecoder), + Br(BrotliDecoder), Identity, } @@ -138,23 +136,6 @@ impl io::Write for BytesWriter { } } - -// should go after brotli2::write::BrotliDecoder::get_mut get implemented -#[derive(Debug)] -struct WrapperRc { - buf: Rc>, -} - -impl io::Write for WrapperRc { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.buf.borrow_mut().extend(buf); - Ok(buf.len()) - } - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } -} - pub(crate) struct EncodedPayload { inner: PayloadSender, decoder: Decoder, @@ -165,14 +146,11 @@ pub(crate) struct EncodedPayload { impl EncodedPayload { pub fn new(inner: PayloadSender, enc: ContentEncoding) -> EncodedPayload { let dec = match enc { + ContentEncoding::Br => Decoder::Br( + BrotliDecoder::new(BytesWriter::default())), ContentEncoding::Deflate => Decoder::Zlib( DeflateDecoder::new(BytesWriter::default())), ContentEncoding::Gzip => Decoder::Gzip(None), - ContentEncoding::Br => { - let buf = Rc::new(RefCell::new(BytesMut::new())); - let buf2 = Rc::clone(&buf); - Decoder::Br(buf, BrotliDecoder::new(WrapperRc{buf: buf2})) - } _ => Decoder::Identity, }; EncodedPayload { @@ -195,10 +173,10 @@ impl PayloadWriter for EncodedPayload { return } let err = match self.decoder { - Decoder::Br(ref mut buf, ref mut decoder) => { - match decoder.flush() { - Ok(_) => { - let b = buf.borrow_mut().take().freeze(); + Decoder::Br(ref mut decoder) => { + match decoder.finish() { + Ok(mut writer) => { + let b = writer.buf.take().freeze(); if !b.is_empty() { self.inner.feed_data(b); } @@ -207,8 +185,7 @@ impl PayloadWriter for EncodedPayload { }, Err(err) => Some(err), } - } - + }, Decoder::Gzip(ref mut decoder) => { if decoder.is_none() { self.inner.feed_eof(); @@ -234,9 +211,9 @@ impl PayloadWriter for EncodedPayload { Err(err) => break Some(err) } } - } + }, Decoder::Zlib(ref mut decoder) => { - match decoder.flush() { + match decoder.try_finish() { Ok(_) => { let b = decoder.get_mut().buf.take().freeze(); if !b.is_empty() { @@ -268,19 +245,17 @@ impl PayloadWriter for EncodedPayload { return } match self.decoder { - Decoder::Br(ref mut buf, ref mut decoder) => { - match decoder.write(&data) { - Ok(_) => { - let b = buf.borrow_mut().take().freeze(); + Decoder::Br(ref mut decoder) => { + if decoder.write(&data).is_ok() { + if decoder.flush().is_ok() { + let b = decoder.get_mut().buf.take().freeze(); if !b.is_empty() { self.inner.feed_data(b); } return - }, - Err(err) => { - trace!("Error decoding br encoding: {}", err); - }, + } } + trace!("Error decoding br encoding"); } Decoder::Gzip(ref mut decoder) => { @@ -317,18 +292,16 @@ impl PayloadWriter for EncodedPayload { } Decoder::Zlib(ref mut decoder) => { - match decoder.write(&data) { - Ok(_) => { + if decoder.write(&data).is_ok() { + if decoder.flush().is_ok() { let b = decoder.get_mut().buf.take().freeze(); if !b.is_empty() { self.inner.feed_data(b); } return - }, - Err(err) => { - trace!("Error decoding deflate encoding: {}", err); - }, + } } + trace!("Error decoding deflate encoding"); } Decoder::Identity => { self.inner.feed_data(data); @@ -342,13 +315,6 @@ impl PayloadWriter for EncodedPayload { } fn capacity(&self) -> usize { - match self.decoder { - Decoder::Br(ref buf, _) => { - buf.borrow().len() + self.inner.capacity() - } - _ => { - self.inner.capacity() - } - } + self.inner.capacity() } }