Default HTTPS proxy protocol to HTTP if not explicitly specified otherwise (#1082)
Fix #1080
This commit is contained in:
70
src/proxy.rs
70
src/proxy.rs
@@ -778,12 +778,12 @@ fn parse_registry_values_impl(registry_values: RegistryProxyValues) -> Result<Sy
|
|||||||
let protocol_parts: Vec<&str> = p.split("=").collect();
|
let protocol_parts: Vec<&str> = p.split("=").collect();
|
||||||
match protocol_parts.as_slice() {
|
match protocol_parts.as_slice() {
|
||||||
[protocol, address] => {
|
[protocol, address] => {
|
||||||
// See if address has a type:// prefix
|
// If address doesn't specify an explicit protocol as protocol://address
|
||||||
let address = if !contains_type_prefix(*address) {
|
// then default to HTTP
|
||||||
format!("{}://{}", protocol, address)
|
let address = if extract_type_prefix(*address).is_some() {
|
||||||
}
|
|
||||||
else {
|
|
||||||
String::from(*address)
|
String::from(*address)
|
||||||
|
} else {
|
||||||
|
format!("http://{}", address)
|
||||||
};
|
};
|
||||||
|
|
||||||
insert_proxy(&mut proxies, *protocol, address);
|
insert_proxy(&mut proxies, *protocol, address);
|
||||||
@@ -797,32 +797,39 @@ fn parse_registry_values_impl(registry_values: RegistryProxyValues) -> Result<Sy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Use one setting for all protocols.
|
if let Some(scheme) = extract_type_prefix(&proxy_server) {
|
||||||
if proxy_server.starts_with("http:") {
|
// Explicit protocol has been specified
|
||||||
insert_proxy(&mut proxies, "http", proxy_server);
|
insert_proxy(&mut proxies, scheme, proxy_server.to_owned());
|
||||||
} else {
|
} else {
|
||||||
|
// No explicit protocol has been specified, default to HTTP
|
||||||
insert_proxy(&mut proxies, "http", format!("http://{}", proxy_server));
|
insert_proxy(&mut proxies, "http", format!("http://{}", proxy_server));
|
||||||
insert_proxy(&mut proxies, "https", format!("https://{}", proxy_server));
|
insert_proxy(&mut proxies, "https", format!("http://{}", proxy_server));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(proxies)
|
Ok(proxies)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract the protocol from the given address, if present
|
||||||
|
/// For example, "https://example.com" will return Some("https")
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
fn contains_type_prefix(address: &str) -> bool {
|
fn extract_type_prefix(address: &str) -> Option<&str> {
|
||||||
if let Some(indice) = address.find("://") {
|
if let Some(indice) = address.find("://") {
|
||||||
if indice == 0 {
|
if indice == 0 {
|
||||||
false
|
None
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let prefix = &address[..indice];
|
let prefix = &address[..indice];
|
||||||
let contains_banned = prefix.contains(|c| c == ':' || c == '/');
|
let contains_banned = prefix.contains(|c| c == ':' || c == '/');
|
||||||
|
|
||||||
!contains_banned
|
if !contains_banned {
|
||||||
|
Some(prefix)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
false
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -999,7 +1006,10 @@ mod tests {
|
|||||||
let disabled_proxies = get_sys_proxies(Some((0, String::from("http://127.0.0.1/"))));
|
let disabled_proxies = get_sys_proxies(Some((0, String::from("http://127.0.0.1/"))));
|
||||||
// set valid proxy
|
// set valid proxy
|
||||||
let valid_proxies = get_sys_proxies(Some((1, String::from("http://127.0.0.1/"))));
|
let valid_proxies = get_sys_proxies(Some((1, String::from("http://127.0.0.1/"))));
|
||||||
|
let valid_proxies_no_schema = get_sys_proxies(Some((1, String::from("127.0.0.1"))));
|
||||||
|
let valid_proxies_explicit_https = get_sys_proxies(Some((1, String::from("https://127.0.0.1/"))));
|
||||||
let multiple_proxies = get_sys_proxies(Some((1, String::from("http=127.0.0.1:8888;https=127.0.0.2:8888"))));
|
let multiple_proxies = get_sys_proxies(Some((1, String::from("http=127.0.0.1:8888;https=127.0.0.2:8888"))));
|
||||||
|
let multiple_proxies_explicit_schema = get_sys_proxies(Some((1, String::from("http=http://127.0.0.1:8888;https=https://127.0.0.2:8888"))));
|
||||||
|
|
||||||
// reset user setting when guards drop
|
// reset user setting when guards drop
|
||||||
drop(_g1);
|
drop(_g1);
|
||||||
@@ -1014,11 +1024,31 @@ mod tests {
|
|||||||
assert_eq!(p.scheme(), "http");
|
assert_eq!(p.scheme(), "http");
|
||||||
assert_eq!(p.host(), "127.0.0.1");
|
assert_eq!(p.host(), "127.0.0.1");
|
||||||
|
|
||||||
|
let p = &valid_proxies_no_schema["http"];
|
||||||
|
assert_eq!(p.scheme(), "http");
|
||||||
|
assert_eq!(p.host(), "127.0.0.1");
|
||||||
|
|
||||||
|
let p = &valid_proxies_no_schema["https"];
|
||||||
|
assert_eq!(p.scheme(), "http");
|
||||||
|
assert_eq!(p.host(), "127.0.0.1");
|
||||||
|
|
||||||
|
let p = &valid_proxies_explicit_https["https"];
|
||||||
|
assert_eq!(p.scheme(), "https");
|
||||||
|
assert_eq!(p.host(), "127.0.0.1");
|
||||||
|
|
||||||
let p = &multiple_proxies["http"];
|
let p = &multiple_proxies["http"];
|
||||||
assert_eq!(p.scheme(), "http");
|
assert_eq!(p.scheme(), "http");
|
||||||
assert_eq!(p.host(), "127.0.0.1:8888");
|
assert_eq!(p.host(), "127.0.0.1:8888");
|
||||||
|
|
||||||
let p = &multiple_proxies["https"];
|
let p = &multiple_proxies["https"];
|
||||||
|
assert_eq!(p.scheme(), "http");
|
||||||
|
assert_eq!(p.host(), "127.0.0.2:8888");
|
||||||
|
|
||||||
|
let p = &multiple_proxies_explicit_schema["http"];
|
||||||
|
assert_eq!(p.scheme(), "http");
|
||||||
|
assert_eq!(p.host(), "127.0.0.1:8888");
|
||||||
|
|
||||||
|
let p = &multiple_proxies_explicit_schema["https"];
|
||||||
assert_eq!(p.scheme(), "https");
|
assert_eq!(p.scheme(), "https");
|
||||||
assert_eq!(p.host(), "127.0.0.2:8888");
|
assert_eq!(p.host(), "127.0.0.2:8888");
|
||||||
}
|
}
|
||||||
@@ -1145,14 +1175,14 @@ mod tests {
|
|||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_type_prefix_detection() {
|
fn test_type_prefix_extraction() {
|
||||||
assert!(!contains_type_prefix("test"));
|
assert!(extract_type_prefix("test").is_none());
|
||||||
assert!(!contains_type_prefix("://test"));
|
assert!(extract_type_prefix("://test").is_none());
|
||||||
assert!(!contains_type_prefix("some:prefix://test"));
|
assert!(extract_type_prefix("some:prefix://test").is_none());
|
||||||
assert!(!contains_type_prefix("some/prefix://test"));
|
assert!(extract_type_prefix("some/prefix://test").is_none());
|
||||||
|
|
||||||
assert!(contains_type_prefix("http://test"));
|
assert_eq!(extract_type_prefix("http://test").unwrap(), "http");
|
||||||
assert!(contains_type_prefix("a://test"));
|
assert_eq!(extract_type_prefix("a://test").unwrap(), "a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Guard an environment variable, resetting it to the original value
|
/// Guard an environment variable, resetting it to the original value
|
||||||
|
|||||||
Reference in New Issue
Block a user