1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-26 01:50:35 +00:00

use new brotli2 version

This commit is contained in:
Nikolay Kim 2017-11-07 15:59:37 -08:00
parent 72c8ad9fe1
commit 6974213036
2 changed files with 23 additions and 57 deletions

View file

@ -45,7 +45,7 @@ regex = "0.2"
sha1 = "0.2" sha1 = "0.2"
url = "1.5" url = "1.5"
flate2 = "0.2" flate2 = "0.2"
brotli2 = "0.3" brotli2 = "^0.3.2"
percent-encoding = "1.0" percent-encoding = "1.0"
# tokio # tokio
@ -64,7 +64,7 @@ tokio-tls = { version="0.1", optional = true }
tokio-openssl = { version="0.1", optional = true } tokio-openssl = { version="0.1", optional = true }
[dependencies.actix] [dependencies.actix]
version = ">=0.3.1" version = "^0.3.1"
#path = "../actix" #path = "../actix"
#git = "https://github.com/actix/actix.git" #git = "https://github.com/actix/actix.git"
default-features = false default-features = false

View file

@ -1,6 +1,4 @@
use std::{io, cmp}; use std::{io, cmp};
use std::rc::Rc;
use std::cell::RefCell;
use std::io::{Read, Write}; use std::io::{Read, Write};
use http::header::{HeaderMap, CONTENT_ENCODING}; use http::header::{HeaderMap, CONTENT_ENCODING};
@ -99,7 +97,7 @@ impl PayloadWriter for PayloadType {
enum Decoder { enum Decoder {
Zlib(DeflateDecoder<BytesWriter>), Zlib(DeflateDecoder<BytesWriter>),
Gzip(Option<GzDecoder<Wrapper>>), Gzip(Option<GzDecoder<Wrapper>>),
Br(Rc<RefCell<BytesMut>>, BrotliDecoder<WrapperRc>), Br(BrotliDecoder<BytesWriter>),
Identity, 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<RefCell<BytesMut>>,
}
impl io::Write for WrapperRc {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buf.borrow_mut().extend(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub(crate) struct EncodedPayload { pub(crate) struct EncodedPayload {
inner: PayloadSender, inner: PayloadSender,
decoder: Decoder, decoder: Decoder,
@ -165,14 +146,11 @@ pub(crate) struct EncodedPayload {
impl EncodedPayload { impl EncodedPayload {
pub fn new(inner: PayloadSender, enc: ContentEncoding) -> EncodedPayload { pub fn new(inner: PayloadSender, enc: ContentEncoding) -> EncodedPayload {
let dec = match enc { let dec = match enc {
ContentEncoding::Br => Decoder::Br(
BrotliDecoder::new(BytesWriter::default())),
ContentEncoding::Deflate => Decoder::Zlib( ContentEncoding::Deflate => Decoder::Zlib(
DeflateDecoder::new(BytesWriter::default())), DeflateDecoder::new(BytesWriter::default())),
ContentEncoding::Gzip => Decoder::Gzip(None), 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, _ => Decoder::Identity,
}; };
EncodedPayload { EncodedPayload {
@ -195,10 +173,10 @@ impl PayloadWriter for EncodedPayload {
return return
} }
let err = match self.decoder { let err = match self.decoder {
Decoder::Br(ref mut buf, ref mut decoder) => { Decoder::Br(ref mut decoder) => {
match decoder.flush() { match decoder.finish() {
Ok(_) => { Ok(mut writer) => {
let b = buf.borrow_mut().take().freeze(); let b = writer.buf.take().freeze();
if !b.is_empty() { if !b.is_empty() {
self.inner.feed_data(b); self.inner.feed_data(b);
} }
@ -207,8 +185,7 @@ impl PayloadWriter for EncodedPayload {
}, },
Err(err) => Some(err), Err(err) => Some(err),
} }
} },
Decoder::Gzip(ref mut decoder) => { Decoder::Gzip(ref mut decoder) => {
if decoder.is_none() { if decoder.is_none() {
self.inner.feed_eof(); self.inner.feed_eof();
@ -234,9 +211,9 @@ impl PayloadWriter for EncodedPayload {
Err(err) => break Some(err) Err(err) => break Some(err)
} }
} }
} },
Decoder::Zlib(ref mut decoder) => { Decoder::Zlib(ref mut decoder) => {
match decoder.flush() { match decoder.try_finish() {
Ok(_) => { Ok(_) => {
let b = decoder.get_mut().buf.take().freeze(); let b = decoder.get_mut().buf.take().freeze();
if !b.is_empty() { if !b.is_empty() {
@ -268,19 +245,17 @@ impl PayloadWriter for EncodedPayload {
return return
} }
match self.decoder { match self.decoder {
Decoder::Br(ref mut buf, ref mut decoder) => { Decoder::Br(ref mut decoder) => {
match decoder.write(&data) { if decoder.write(&data).is_ok() {
Ok(_) => { if decoder.flush().is_ok() {
let b = buf.borrow_mut().take().freeze(); let b = decoder.get_mut().buf.take().freeze();
if !b.is_empty() { if !b.is_empty() {
self.inner.feed_data(b); self.inner.feed_data(b);
} }
return return
}, }
Err(err) => {
trace!("Error decoding br encoding: {}", err);
},
} }
trace!("Error decoding br encoding");
} }
Decoder::Gzip(ref mut decoder) => { Decoder::Gzip(ref mut decoder) => {
@ -317,18 +292,16 @@ impl PayloadWriter for EncodedPayload {
} }
Decoder::Zlib(ref mut decoder) => { Decoder::Zlib(ref mut decoder) => {
match decoder.write(&data) { if decoder.write(&data).is_ok() {
Ok(_) => { if decoder.flush().is_ok() {
let b = decoder.get_mut().buf.take().freeze(); let b = decoder.get_mut().buf.take().freeze();
if !b.is_empty() { if !b.is_empty() {
self.inner.feed_data(b); self.inner.feed_data(b);
} }
return return
}, }
Err(err) => {
trace!("Error decoding deflate encoding: {}", err);
},
} }
trace!("Error decoding deflate encoding");
} }
Decoder::Identity => { Decoder::Identity => {
self.inner.feed_data(data); self.inner.feed_data(data);
@ -342,13 +315,6 @@ impl PayloadWriter for EncodedPayload {
} }
fn capacity(&self) -> usize { fn capacity(&self) -> usize {
match self.decoder { self.inner.capacity()
Decoder::Br(ref buf, _) => {
buf.borrow().len() + self.inner.capacity()
}
_ => {
self.inner.capacity()
}
}
} }
} }