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

Add --preserve-encoding option to keep response body encoding #294

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/cli.rs
Expand Up @@ -159,6 +159,12 @@ Defaults to \"format\" if the NO_COLOR env is set and to \"none\" if stdout is n
#[clap(short = 'd', long)]
pub download: bool,

/// During download, keep the raw encoding of the body. Requires --download.
ducaale marked this conversation as resolved.
Show resolved Hide resolved
///
/// For example, set Accept-Encoding: gzip and use --preserve-encoding to skip decompression.
#[clap(long, requires = "download")]
pub preserve_encoding: bool,

/// Resume an interrupted download. Requires --download and --output.
#[clap(
short = 'c',
Expand Down
8 changes: 6 additions & 2 deletions src/download.rs
Expand Up @@ -160,6 +160,7 @@ const UNCOLORED_SPINNER_TEMPLATE: &str = "{spinner} {bytes} {bytes_per_sec} {wid

pub fn download_file(
mut response: Response,
preserve_encoding: bool,
file_name: Option<PathBuf>,
// If we fall back on taking the filename from the URL it has to be the
// original URL, before redirects. That's less surprising and matches
Expand Down Expand Up @@ -244,9 +245,13 @@ pub fn download_file(
pb.reset_eta();
}

let compression_type = if !preserve_encoding {
get_compression_type(response.headers())
} else {
None
};
match pb {
Some(ref pb) => {
let compression_type = get_compression_type(response.headers());
copy_largebuf(
&mut decompress(&mut pb.wrap_read(response), compression_type),
&mut buffer,
Expand All @@ -267,7 +272,6 @@ pub fn download_file(
}
}
None => {
let compression_type = get_compression_type(response.headers());
copy_largebuf(
&mut decompress(&mut response, compression_type),
&mut buffer,
Expand Down
27 changes: 18 additions & 9 deletions src/main.rs
Expand Up @@ -353,11 +353,25 @@ fn run(args: Cli) -> Result<i32> {
let mut request = {
let mut request_builder = client
.request(method, args.url.clone())
.header(
.header(USER_AGENT, get_user_agent());

if args.download {
if let Some(encoding) = headers.get(ACCEPT_ENCODING) {
if args.resume && encoding != HeaderValue::from_static("identity") {
return Err(anyhow!(
"Cannot use --continue with --download, when the encoding is not 'identity'"
));
}
} else {
request_builder =
request_builder.header(ACCEPT_ENCODING, HeaderValue::from_static("identity"));
}
Comment on lines +366 to +369
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the else branching so that ACCEPT_ENCODING is set to a default value when args.resume && encoding != HeaderValue::from_static("identity") is false?

Suggested change
} else {
request_builder =
request_builder.header(ACCEPT_ENCODING, HeaderValue::from_static("identity"));
}
}
request_builder =
request_builder.header(ACCEPT_ENCODING, HeaderValue::from_static("identity"));

This doesn't matter that much since it will be overridden later with header values provided by the user. However, reducing possible branches helps with any potential debugging needed in the future.

} else {
request_builder = request_builder.header(
ACCEPT_ENCODING,
HeaderValue::from_static("gzip, deflate, br"),
)
.header(USER_AGENT, get_user_agent());
);
}

if matches!(
args.http_version,
Expand Down Expand Up @@ -464,12 +478,6 @@ fn run(args: Cli) -> Result<i32> {
request
};

if args.download {
request
.headers_mut()
.insert(ACCEPT_ENCODING, HeaderValue::from_static("identity"));
}

let buffer = Buffer::new(
args.download,
args.output.as_deref(),
Expand Down Expand Up @@ -555,6 +563,7 @@ fn run(args: Cli) -> Result<i32> {
if exit_code == 0 {
download_file(
response,
args.preserve_encoding,
args.output,
&args.url,
resume,
Expand Down
62 changes: 60 additions & 2 deletions tests/cli.rs
Expand Up @@ -413,9 +413,9 @@ fn download() {
}

#[test]
fn accept_encoding_not_modifiable_in_download_mode() {
fn accept_encoding_modifiable_in_download_mode() {
trueb2 marked this conversation as resolved.
Show resolved Hide resolved
let server = server::http(|req| async move {
assert_eq!(req.headers()["accept-encoding"], "identity");
assert_eq!(req.headers()["accept-encoding"], "gzip");
hyper::Response::builder()
.body(r#"{"ids":[1,2,3]}"#.into())
.unwrap()
Expand All @@ -429,6 +429,64 @@ fn accept_encoding_not_modifiable_in_download_mode() {
.success();
}

#[test]
fn accept_encoding_identity_default_in_download_mode() {
let server = server::http(|req| async move {
assert_eq!(req.headers()["accept-encoding"], "identity");
hyper::Response::builder()
.body(r#"{"ids":[1,2,3]}"#.into())
.unwrap()
});

let dir = tempdir().unwrap();
get_command()
.current_dir(&dir)
.args([&server.base_url(), "--download"])
.assert()
.success();
}

#[test]
fn accept_encoding_identity_in_resumable_download_mode() {
let server = server::http(|req| async move {
assert_eq!(req.headers()["accept-encoding"], "identity");
hyper::Response::builder()
.body(r#"{"ids":[1,2,3]}"#.into())
.unwrap()
});

let dir = tempdir().unwrap();
get_command()
.current_dir(&dir)
.args([
&server.base_url(),
"--download",
"--output",
"foo",
"--continue",
"accept-encoding:identity",
])
.assert()
.success();
}

#[test]
fn accept_encoding_not_modifiable_in_resumable_download_mode() {
let dir = tempdir().unwrap();
get_command()
.current_dir(&dir)
.args([
":",
"--download",
"--output",
"foo",
"--continue",
"accept-encoding:gzip",
])
.assert()
.failure();
trueb2 marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn download_generated_filename() {
let dir = tempdir().unwrap();
Expand Down