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 all 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
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ 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. Intended for use with download or
/// to debug an encoded response body.
///
/// For example, set Accept-Encoding: gzip and use --preserve-encoding to skip decompression.
#[clap(long)]
pub preserve_encoding: bool,

Comment on lines +162 to +168
Copy link
Owner

Choose a reason for hiding this comment

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

Let's shorten the first line to fit within ~80 chars. Any extended help can go in subsequent lines.

(to see the difference, try cargo run -- --help and cargo run -- help)

/// Skip decompressing the response body
///
/// longer help

Also, should we move this option to after response_mime to improve discoverability?

/// Resume an interrupted download. Requires --download and --output.
#[clap(
short = 'c',
Expand Down
8 changes: 6 additions & 2 deletions src/download.rs
Original file line number Diff line number Diff line change
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
36 changes: 26 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,25 @@ fn run(args: Cli) -> Result<i32> {
let mut request = {
let mut request_builder = client
.request(method, 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 @@ -465,12 +479,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 @@ -501,6 +509,7 @@ fn run(args: Cli) -> Result<i32> {
printer.print_request_body(&mut request)?;
}

let preserve_encoding = args.preserve_encoding;
Copy link
Owner

@ducaale ducaale Jan 4, 2023

Choose a reason for hiding this comment

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

Nit: can you move this line to after L503 and replace all instances of args.preserve_encoding with preserve_encoding?

let response_charset = args.response_charset;
let response_mime = args.response_mime.as_deref();
let preserve_encoding = args.preserve_encoding;

if !args.offline {
let mut response = {
let history_print = args.history_print.unwrap_or(print);
Expand All @@ -515,6 +524,7 @@ fn run(args: Cli) -> Result<i32> {
prev_response,
response_charset,
response_mime,
preserve_encoding,
)?;
printer.print_separator()?;
}
Expand Down Expand Up @@ -556,6 +566,7 @@ fn run(args: Cli) -> Result<i32> {
if exit_code == 0 {
download_file(
response,
args.preserve_encoding,
args.output,
&url,
resume,
Expand All @@ -564,7 +575,12 @@ fn run(args: Cli) -> Result<i32> {
)?;
}
} else if print.response_body {
printer.print_response_body(&mut response, response_charset, response_mime)?;
printer.print_response_body(
&mut response,
response_charset,
response_mime,
args.preserve_encoding,
)?;
}
}

Expand Down
22 changes: 20 additions & 2 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,33 @@ impl Printer {
response: &mut Response,
encoding: Option<&'static Encoding>,
mime: Option<&str>,
preserve_encoding: bool,
) -> anyhow::Result<()> {
let url = response.url().clone();
let content_type =
mime.map_or_else(|| get_content_type(response.headers()), ContentType::from);
let encoding = encoding.or_else(|| get_charset(response));
let compression_type = get_compression_type(response.headers());
let (may_decode, compression_type) = if !preserve_encoding {
let compression_type = get_compression_type(response.headers());
(true, compression_type)
} else {
(false, None)
};
Comment on lines +437 to +442
Copy link
Owner

Choose a reason for hiding this comment

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

Is it possible to use the same approach you took in download.rs?

Suggested change
let (may_decode, compression_type) = if !preserve_encoding {
let compression_type = get_compression_type(response.headers());
(true, compression_type)
} else {
(false, None)
};
let compression_type = if !preserve_encoding {
get_compression_type(response.headers())
} else {
None
};

Copy link
Author

Choose a reason for hiding this comment

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

I don't think that it is possible since the logic below assumes that the content has been decompressed before colorizing, formatting, or decoding. We wouldn't want that logic to run on potentially encoded data. The other branches call code like decode_stream to interpret UTF8 or other format types.

Copy link
Owner

Choose a reason for hiding this comment

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

Fair enough. Although we have some logic for binary detection (see decode_blob and BinaryGuard), they might not cover all cases.

At the moment, plain text is treated as binary if preserve_encoding is true. Can you handle that case?

// TODO: rename preserve_encoding to skip_decompression
let (mut body, is_binary) = if preserve_encoding {
    let compression_type = get_compression_type(response.headers());
    let body = decompress(response, None);
    (body, compression_type.is_some())
} else {
    let compression_type = get_compression_type(response.headers());
    let body = decompress(response, compression_type);
    (body, false)
};

Also, the term decode is a bit overloaded in this module. Can we rename this function's preserve_encoding parameter to skip_decompression?

let mut body = decompress(response, compression_type);

if !self.buffer.is_terminal() {
if !may_decode {
// The user explicitly asked for preserving the encoding. We don't
// decode the body, even if it's text, so we can't write it to the terminal.
if self.buffer.is_terminal() {
self.buffer.print(BINARY_SUPPRESSOR)?;
} else if self.stream {
copy_largebuf(&mut body, &mut self.buffer, true)?;
} else {
let mut buf = Vec::new();
body.read_to_end(&mut buf)?;
self.buffer.print(&buf)?;
}
} else if !self.buffer.is_terminal() {
if (self.color || self.indent_json) && content_type.is_text() {
// The user explicitly asked for formatting even though this is
// going into a file, and the response is at least supposed to be
Expand Down
48 changes: 46 additions & 2 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ fn download() {
}

#[test]
fn accept_encoding_not_modifiable_in_download_mode() {
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()
Expand All @@ -424,11 +424,55 @@ fn accept_encoding_not_modifiable_in_download_mode() {
let dir = tempdir().unwrap();
get_command()
.current_dir(&dir)
.args([&server.base_url(), "--download", "accept-encoding:gzip"])
.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()
.stderr(indoc! {r#"
xh: error: Cannot use --continue with --download, when the encoding is not 'identity'
"#});
}

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