Skip to content

Commit

Permalink
Work around set_scheme() breakage in url-2.1.1
Browse files Browse the repository at this point in the history
Since url-2.1.1, Url::set_scheme() is stricter and doesn't let us
change the scheme back to http/https. Work around the new behavior.
The changed logic also works with url < 2.1.1.

One difference from the previous version is that the string
representation of the proxy URL which was given with the explicit
scheme-default port in the environment (80 for http, 443 for https)
will no longer contain the port number.
  • Loading branch information
inejge committed Mar 6, 2020
1 parent 0f5ee6e commit 5591cc7
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use lazy_static::lazy_static;
use log::warn;

use std::env::var_os;
use url::Url;
use url::{self, Url};

macro_rules! env_var_pair {
($lc_var:expr, $uc_var:expr) => {
Expand Down Expand Up @@ -190,9 +190,17 @@ impl ProxyUrl {
return None;
}
if let Some(orig_scheme) = orig_scheme {
if url.set_scheme(orig_scheme).is_err() {
warn!("could not set URL scheme back to {}", orig_scheme);
return None;
let port = url.port();
url = match format!("{}{}", orig_scheme, &url[url::Position::AfterScheme..]).parse() {
Ok(url) => url,
Err(e) => {
warn!("could not set URL scheme back to {}: {}", orig_scheme, e);
return None;
},
};
if port.is_some() {
url.set_port(port).unwrap_or(());
return Some(url);
}
}
if url.port().is_some() {
Expand All @@ -214,7 +222,7 @@ impl ProxyUrl {
/// The raw URL will first be transformed into a `Url`, with any errors in the conversion
/// producing a `None` (see [`to_url()`](#method.to_url)).
pub fn host_port(self) -> Option<(String, u16)> {
self.to_url().map(|u| (u.host_str().expect("host_str").to_string(), u.port().expect("port")))
self.to_url().map(|u| (u.host_str().expect("host_str").to_string(), u.port_or_known_default().expect("port")))
}


Expand Down Expand Up @@ -418,13 +426,13 @@ mod tests {
assert_eq!(for_url(&u).host_port(), Some(("proxy.example.com".to_string(), 80)));
assert_eq!(
for_url_str("http://www.example.org").to_string(),
Some("http://proxy.example.com:80/".to_string())
Some("http://proxy.example.com/".to_string())
);
set_var("http_proxy", "https://proxy.example.com:443");
assert_eq!(for_url(&u).host_port(), Some(("proxy.example.com".to_string(), 443)));
assert_eq!(
for_url_str("http://www.example.org").to_string(),
Some("https://proxy.example.com:443/".to_string())
Some("https://proxy.example.com/".to_string())
);
}

Expand Down

0 comments on commit 5591cc7

Please sign in to comment.