Remove content-encoding and content-length headers from response, if we ungzip the response,

and add accept-encoding: gzip header to the reqeuest when ungzipping is enabled
This commit is contained in:
Ryan Coffman
2017-03-13 16:29:13 -07:00
parent c9291049ee
commit e557fe155d

View File

@@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use hyper::client::IntoUrl;
use hyper::header::{Headers, ContentType, Location, Referer, UserAgent, Accept, ContentEncoding, Encoding, ContentLength,
TransferEncoding};
TransferEncoding, AcceptEncoding, Range, qitem};
use hyper::method::Method;
use hyper::status::StatusCode;
use hyper::version::HttpVersion;
@@ -226,7 +226,11 @@ impl RequestBuilder {
if !self.headers.has::<Accept>() {
self.headers.set(Accept::star());
}
if self.client.auto_ungzip.load(Ordering::Relaxed) &&
!self.headers.has::<AcceptEncoding>() &&
!self.headers.has::<Range>() {
self.headers.set(AcceptEncoding(vec![qitem(Encoding::Gzip)]));
}
let client = self.client;
let mut method = self.method;
let mut url = try!(self.url);
@@ -411,18 +415,23 @@ impl Decoder {
/// how to decode the content body of the request.
///
/// Uses the correct variant by inspecting the Content-Encoding header.
fn from_hyper_response(res: ::hyper::client::Response, check_gzip: bool) -> Self {
fn from_hyper_response(mut res: ::hyper::client::Response, check_gzip: bool) -> Self {
if !check_gzip {
return Decoder::PlainText(res);
}
let content_encoding_gzip: bool;
let mut is_gzip = {
res.headers.get::<ContentEncoding>().map_or(false, |encs|{
content_encoding_gzip = res.headers.get::<ContentEncoding>().map_or(false, |encs|{
encs.contains(&Encoding::Gzip)
}) ||
res.headers.get::<TransferEncoding>().map_or(false, |encs|{
});
content_encoding_gzip || res.headers.get::<TransferEncoding>().map_or(false, |encs|{
encs.contains(&Encoding::Gzip)
})
};
if content_encoding_gzip {
res.headers.remove::<ContentEncoding>();
res.headers.remove::<ContentLength>();
}
if is_gzip {
if let Some(content_length) = res.headers.get::<ContentLength>() {
if content_length.0 == 0 {