Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5591cc7

Browse files
committed
Work around set_scheme() breakage in url-2.1.1
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.
1 parent 0f5ee6e commit 5591cc7

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use lazy_static::lazy_static;
5656
use log::warn;
5757

5858
use std::env::var_os;
59-
use url::Url;
59+
use url::{self, Url};
6060

6161
macro_rules! env_var_pair {
6262
($lc_var:expr, $uc_var:expr) => {
@@ -190,9 +190,17 @@ impl ProxyUrl {
190190
return None;
191191
}
192192
if let Some(orig_scheme) = orig_scheme {
193-
if url.set_scheme(orig_scheme).is_err() {
194-
warn!("could not set URL scheme back to {}", orig_scheme);
195-
return None;
193+
let port = url.port();
194+
url = match format!("{}{}", orig_scheme, &url[url::Position::AfterScheme..]).parse() {
195+
Ok(url) => url,
196+
Err(e) => {
197+
warn!("could not set URL scheme back to {}: {}", orig_scheme, e);
198+
return None;
199+
},
200+
};
201+
if port.is_some() {
202+
url.set_port(port).unwrap_or(());
203+
return Some(url);
196204
}
197205
}
198206
if url.port().is_some() {
@@ -214,7 +222,7 @@ impl ProxyUrl {
214222
/// The raw URL will first be transformed into a `Url`, with any errors in the conversion
215223
/// producing a `None` (see [`to_url()`](#method.to_url)).
216224
pub fn host_port(self) -> Option<(String, u16)> {
217-
self.to_url().map(|u| (u.host_str().expect("host_str").to_string(), u.port().expect("port")))
225+
self.to_url().map(|u| (u.host_str().expect("host_str").to_string(), u.port_or_known_default().expect("port")))
218226
}
219227

220228

@@ -418,13 +426,13 @@ mod tests {
418426
assert_eq!(for_url(&u).host_port(), Some(("proxy.example.com".to_string(), 80)));
419427
assert_eq!(
420428
for_url_str("http://www.example.org").to_string(),
421-
Some("http://proxy.example.com:80/".to_string())
429+
Some("http://proxy.example.com/".to_string())
422430
);
423431
set_var("http_proxy", "https://proxy.example.com:443");
424432
assert_eq!(for_url(&u).host_port(), Some(("proxy.example.com".to_string(), 443)));
425433
assert_eq!(
426434
for_url_str("http://www.example.org").to_string(),
427-
Some("https://proxy.example.com:443/".to_string())
435+
Some("https://proxy.example.com/".to_string())
428436
);
429437
}
430438

0 commit comments

Comments
 (0)