Skip to content

Commit

Permalink
metricdog: prepend scheme to https proxy value
Browse files Browse the repository at this point in the history
reqwest does not support HTTPS_PROXY values that are not in URL format
(i.e. if there is no protocol scheme). On Bottlerocket we expect an
HTTPS_PROXY value with no scheme to be treated as http://.

Here we check the HTTPS_PROXY value, and if it has no scheme we reset
the value to prepend https://
  • Loading branch information
webern committed Feb 22, 2021
1 parent 6a04c4c commit 9d6b77d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions sources/metricdog/src/metricdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Metricdog {

fn send_get_request(url: Url, timeout_sec: Option<u64>) -> Result<()> {
debug!("sending: {}", url.as_str());
fix_https_proxy_env();
let client = Client::builder()
.timeout(Duration::from_secs(
timeout_sec.unwrap_or(DEFAULT_TIMEOUT_SECONDS),
Expand All @@ -156,3 +157,30 @@ impl Metricdog {
Ok(())
}
}

/// reqwest needs a URL protocol scheme to be present, but other implementations assume
/// `http://` as the scheme when none is present. We need to check the `HTTPS_PROXY` env and
/// reset it with `http://` when no scheme is present. This can be removed when we are using
/// reqwest >= 0.11.1
// TODO - remove this workaround, see https://github.com/bottlerocket-os/bottlerocket/issues/1332
fn fix_https_proxy_env() {
let proxy = match std::env::var("HTTPS_PROXY") {
Ok(s) if !s.is_empty() => s,
_ => return,
};

// try to parse it as a URL. if it works, a scheme is present
if Url::parse(&proxy).is_ok() {
return;
}

// since the URL could not be parsed, try parsing with http:// prepended
let proxy = format!("http://{}", proxy);

// use URL again to see if the proxy value is now valid
if Url::parse(&proxy).is_ok() {
debug!("Adding a scheme to HTTPS_PROXY: '{}'", proxy);
// success, so let's change the HTTPS_PROXY value for the sake of reqwest
std::env::set_var("HTTPS_PROXY", proxy);
}
}

0 comments on commit 9d6b77d

Please sign in to comment.