Rework replace_hosts and host_replacement

They do less cloning now.
This commit is contained in:
Anthony Ramine 2017-03-25 12:07:14 +01:00
parent c2d9f663af
commit bc5c4fa892
4 changed files with 19 additions and 20 deletions

View file

@ -56,19 +56,18 @@ pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
host_table
}
pub fn replace_hosts(url: &ServoUrl) -> ServoUrl {
HOST_TABLE.lock().unwrap().as_ref().map_or_else(|| url.clone(),
|host_table| host_replacement(host_table, url))
pub fn replace_host_in_url(url: ServoUrl) -> ServoUrl {
if let Some(table) = HOST_TABLE.lock().unwrap().as_ref() {
host_replacement(table, url)
} else {
url
}
}
pub fn host_replacement(host_table: &HashMap<String, IpAddr>, url: &ServoUrl) -> ServoUrl {
url.domain()
.and_then(|domain| {
host_table.get(domain).map(|ip| {
let mut new_url = url.clone();
new_url.set_ip_host(*ip).unwrap();
new_url
})
})
.unwrap_or_else(|| url.clone())
pub fn host_replacement(host_table: &HashMap<String, IpAddr>, mut url: ServoUrl) -> ServoUrl {
let replacement = url.domain().and_then(|domain| host_table.get(domain));
if let Some(ip) = replacement {
url.set_ip_host(*ip).unwrap();
}
url
}