Remove Content-Encoding and length when decompressing

This should lower the chance of breakage. Probably also more proper for a client library.
This commit is contained in:
Malloc Voidstar
2021-10-05 09:46:31 -07:00
committed by Martin Algesten
parent 6281a0bea6
commit 598ebf4393
2 changed files with 23 additions and 8 deletions

View File

@@ -74,6 +74,11 @@ pub struct Response {
///
/// If this response was not redirected, the history is empty.
pub(crate) history: Vec<Url>,
/// The Content-Length value. The header itself may have been removed due to
/// the automatic decompression system.
length: Option<usize>,
/// The compression type of the response body.
compression: Option<Compression>,
}
/// index into status_line where we split: HTTP/1.1 200 OK
@@ -283,15 +288,9 @@ impl Response {
// head requests never have a body
Some(0)
} else {
self.header("content-length")
.and_then(|l| l.parse::<usize>().ok())
self.length
};
let compression = self
.header("content-encoding")
.map(Compression::from_header_value)
.flatten();
let stream = self.stream;
let unit = self.unit;
if let Some(unit) = &unit {
@@ -311,7 +310,7 @@ impl Response {
(false, None) => Box::new(stream),
};
match compression {
match self.compression {
None => body_reader,
Some(c) => c.wrap_reader(body_reader),
}
@@ -487,6 +486,16 @@ impl Response {
));
}
let length = get_header(&headers, "content-length").and_then(|v| v.parse::<usize>().ok());
let compression =
get_header(&headers, "content-encoding").and_then(Compression::from_header_value);
if compression.is_some() {
headers.retain(|h| h.name() != "content-encoding" && h.name() != "content-length");
// remove Content-Encoding and length due to automatic decompression
}
Ok(Response {
url: None,
status_line,
@@ -496,6 +505,8 @@ impl Response {
unit: unit.map(Box::new),
stream: Box::new(stream.into()),
history: vec![],
length,
compression,
})
}