-
Notifications
You must be signed in to change notification settings - Fork 89
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
base: master
Are you sure you want to change the base?
Changes from all commits
bcbb38a
f025ddf
dffabdc
eafb676
f6c7961
966b985
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove the else branching so that
Suggested change
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, | ||||||||||||||||
|
@@ -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(), | ||||||||||||||||
|
@@ -501,6 +509,7 @@ fn run(args: Cli) -> Result<i32> { | |||||||||||||||
printer.print_request_body(&mut request)?; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
let preserve_encoding = args.preserve_encoding; | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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); | ||||||||||||||||
|
@@ -515,6 +524,7 @@ fn run(args: Cli) -> Result<i32> { | |||||||||||||||
prev_response, | ||||||||||||||||
response_charset, | ||||||||||||||||
response_mime, | ||||||||||||||||
preserve_encoding, | ||||||||||||||||
)?; | ||||||||||||||||
printer.print_separator()?; | ||||||||||||||||
} | ||||||||||||||||
|
@@ -556,6 +566,7 @@ fn run(args: Cli) -> Result<i32> { | |||||||||||||||
if exit_code == 0 { | ||||||||||||||||
download_file( | ||||||||||||||||
response, | ||||||||||||||||
args.preserve_encoding, | ||||||||||||||||
args.output, | ||||||||||||||||
&url, | ||||||||||||||||
resume, | ||||||||||||||||
|
@@ -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, | ||||||||||||||||
)?; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use the same approach you took in
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. Although we have some logic for binary detection (see At the moment, plain text is treated as binary if // 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 |
||||||||||||||||||||||||
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 | ||||||||||||||||||||||||
|
There was a problem hiding this comment.
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
andcargo run -- help
)Also, should we move this option to after
response_mime
to improve discoverability?