From 035c35cdcc5f0b8e72678f2ab27d57563cdf4650 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Wed, 23 Nov 2016 15:29:41 +0100 Subject: [PATCH] Provide an API to replace the host table. --- components/net_traits/hosts.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/net_traits/hosts.rs b/components/net_traits/hosts.rs index 5dd947f7c0b..c3c48ce9938 100644 --- a/components/net_traits/hosts.rs +++ b/components/net_traits/hosts.rs @@ -8,9 +8,10 @@ use std::env; use std::fs::File; use std::io::{BufReader, Read}; use std::net::IpAddr; +use std::sync::Mutex; lazy_static! { - static ref HOST_TABLE: Option> = create_host_table(); + static ref HOST_TABLE: Mutex>> = Mutex::new(create_host_table()); } fn create_host_table() -> Option> { @@ -34,6 +35,10 @@ fn create_host_table() -> Option> { return Some(parse_hostsfile(&lines)); } +pub fn replace_host_table(table: HashMap) { + *HOST_TABLE.lock().unwrap() = Some(table); +} + pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap { let mut host_table = HashMap::new(); for line in hostsfile_content.split('\n') { @@ -53,7 +58,7 @@ pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap { } pub fn replace_hosts(url: &ServoUrl) -> ServoUrl { - HOST_TABLE.as_ref().map_or_else(|| url.clone(), |host_table| { + HOST_TABLE.lock().unwrap().as_ref().map_or_else(|| url.clone(), |host_table| { host_replacement(host_table, url) }) }