diff --git a/src/response.rs b/src/response.rs index c650d5c..3eee28d 100644 --- a/src/response.rs +++ b/src/response.rs @@ -74,6 +74,11 @@ pub struct Response { /// /// If this response was not redirected, the history is empty. pub(crate) history: Vec, + /// The Content-Length value. The header itself may have been removed due to + /// the automatic decompression system. + length: Option, + /// The compression type of the response body. + compression: Option, } /// 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::().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::().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, }) } diff --git a/src/test/body_read.rs b/src/test/body_read.rs index c15e7c5..6a3b440 100644 --- a/src/test/body_read.rs +++ b/src/test/body_read.rs @@ -101,6 +101,8 @@ fn gzip_text() { // echo -n INPUT | gzip -9 -f | hexdump -ve '1/1 "0x%.2X,"' // INPUT is `hello world ` repeated 14 times, no trailing space let resp = get("test://host/gzip_text").call().unwrap(); + assert_eq!(resp.header("content-encoding"), None); + assert_eq!(resp.header("content-length"), None); let text = resp.into_string().unwrap(); assert_eq!(text, "hello world ".repeat(14).trim_end()); } @@ -121,6 +123,8 @@ fn brotli_text() { }); // echo -n INPUT | brotli -Z -f | hexdump -ve '1/1 "0x%.2X,"' let resp = get("test://host/brotli_text").call().unwrap(); + assert_eq!(resp.header("content-encoding"), None); + assert_eq!(resp.header("content-length"), None); let text = resp.into_string().unwrap(); assert_eq!(text, "hello world ".repeat(14).trim_end()); }