mirror of
https://github.com/servo/servo.git
synced 2025-08-16 10:55:34 +01:00
Rework replace_hosts and host_replacement
They do less cloning now.
This commit is contained in:
parent
c2d9f663af
commit
bc5c4fa892
4 changed files with 19 additions and 20 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue