Skip to content

Commit

Permalink
gzip: examine Content-Length header before removing (#578)
Browse files Browse the repository at this point in the history
Fixes #575.
  • Loading branch information
jsha authored Jan 3, 2023
1 parent 78ec3a4 commit 032bfb4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.6.1

## Fixed
* gzip: examine Content-Length header before removing (#578)

# 2.6.0

## Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ureq"
version = "2.6.0"
version = "2.6.1"
authors = ["Martin Algesten <martin@algesten.se>", "Jacob Hoffman-Andrews <ureq@hoffman-andrews.com>"]
description = "Simple, safe HTTP client"
license = "MIT/Apache-2.0"
Expand Down
36 changes: 35 additions & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,13 @@ impl Response {
let compression =
get_header(&headers, "content-encoding").and_then(Compression::from_header_value);

let body_type = Self::body_type(&unit.method, status, http_version, &headers);

// remove Content-Encoding and length due to automatic decompression
if compression.is_some() {
headers.retain(|h| !h.is_name("content-encoding") && !h.is_name("content-length"));
}

let body_type = Self::body_type(&unit.method, status, http_version, &headers);
let reader = Self::stream_to_reader(stream, &unit, body_type, compression);

let url = unit.url.clone();
Expand Down Expand Up @@ -1183,4 +1184,37 @@ mod tests {
.unwrap();
assert_eq!(agent2.state.pool.len(), 1);
}

#[test]
#[cfg(feature = "gzip")]
fn gzip_content_length() {
use std::io::Cursor;
let response_bytes =
b"HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Length: 23\r\n\r\n\
\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xcb\xc8\xe4\x02\x00\x7a\x7a\x6f\xed\x03\x00\x00\x00";
// Follow the response with an infinite stream of 0 bytes, so the content-length
// is important.
let reader = Cursor::new(response_bytes).chain(std::io::repeat(0u8));
let test_stream = crate::test::TestStream::new(reader, std::io::sink());
let agent = Agent::new();
let stream = Stream::new(
test_stream,
"1.1.1.1:4343".parse().unwrap(),
PoolReturner::none(),
);
let resp = Response::do_from_stream(
stream,
Unit::new(
&agent,
"GET",
&"https://example.com/".parse().unwrap(),
vec![],
&Payload::Empty.into_read(),
None,
),
)
.unwrap();
let body = resp.into_string().unwrap();
assert_eq!(body, "hi\n");
}
}

0 comments on commit 032bfb4

Please sign in to comment.