Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gzip: examine Content-Length header before removing #578

Merged
merged 4 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>", "Jacob Hoffman-Andrews <[email protected]>"]
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");
}
}