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

decode header value with utf-8 #375

Merged
merged 6 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 9 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod vendored;
use std::env;
use std::fs::File;
use std::io::{self, IsTerminal, Read};
use std::net::{IpAddr, SocketAddr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::path::PathBuf;
use std::process;
use std::str::FromStr;
Expand Down Expand Up @@ -265,23 +265,19 @@ fn run(args: Cli) -> Result<i32> {
}?);
}

if matches!(
args.http_version,
Some(HttpVersion::Http10) | Some(HttpVersion::Http11)
) {
client = client.http1_only();
}

if matches!(args.http_version, Some(HttpVersion::Http2PriorKnowledge)) {
client = client.http2_prior_knowledge();
}
client = match args.http_version {
Some(HttpVersion::Http10 | HttpVersion::Http11) => client.http1_only(),
Some(HttpVersion::Http2PriorKnowledge) => client.http2_prior_knowledge(),
Some(HttpVersion::Http2) => client,
None => client,
};

let cookie_jar = Arc::new(reqwest_cookie_store::CookieStoreMutex::default());
client = client.cookie_provider(cookie_jar.clone());

client = match (args.ipv4, args.ipv6) {
(true, false) => client.local_address(IpAddr::from_str("0.0.0.0")?),
(false, true) => client.local_address(IpAddr::from_str("::")?),
(true, false) => client.local_address(IpAddr::from(Ipv4Addr::UNSPECIFIED)),
(false, true) => client.local_address(IpAddr::from(Ipv6Addr::UNSPECIFIED)),
_ => client,
};

Expand Down
4 changes: 2 additions & 2 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ impl Printer {
header_string.push_str(key.as_str());
}
header_string.push_str(": ");
match value.to_str() {
Ok(value) => header_string.push_str(value),
match String::from_utf8(value.as_bytes().to_vec()) {
zuisong marked this conversation as resolved.
Show resolved Hide resolved
Ok(value) => header_string.push_str(&value),
#[allow(clippy::format_push_string)]
Err(_) => header_string.push_str(&format!("{:?}", value)),
}
Expand Down
4 changes: 2 additions & 2 deletions src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn get_next_request(mut request: Request, response: &Response) -> Option<Request
response
.headers()
.get(LOCATION)
.and_then(|location| location.to_str().ok())
.and_then(|location| request.url().join(location).ok())
.and_then(|location| String::from_utf8(location.as_bytes().to_vec()).ok())
.and_then(|location| request.url().join(&location).ok())
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm realizing now that if decoding or joining fails we should log::warn!() (after #371). We can pick that up in another PR though.

};

match response.status() {
Expand Down
6 changes: 5 additions & 1 deletion src/to_curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ pub fn translate(args: Cli) -> Result<Command> {
if value.is_empty() {
cmd.arg(format!("{};", header));
} else {
cmd.arg(format!("{}: {}", header, value.to_str()?));
cmd.arg(format!(
"{}: {}",
header,
String::from_utf8(value.as_bytes().to_vec())?
));
}
}
for header in headers_to_unset {
Expand Down
Loading