mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Use lazy_static for HOST_TABLE.
This might change behaviour if the file is changed between Servo startup and the moment HOST_TABLE is first accessed. I don't think we care.
This commit is contained in:
parent
a862384841
commit
ec1f720662
9 changed files with 45 additions and 56 deletions
|
@ -25,6 +25,7 @@ heapsize = "0.3.0"
|
|||
heapsize_plugin = "0.1.2"
|
||||
hyper = { version = "0.7", features = [ "serde-serialization" ] }
|
||||
image = "0.7"
|
||||
lazy_static = "0.1.15"
|
||||
log = "0.3"
|
||||
serde = "0.6"
|
||||
serde_macros = "0.6"
|
||||
|
|
|
@ -9,38 +9,34 @@ use std::io::{BufReader, Read};
|
|||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
use url::Url;
|
||||
|
||||
static mut HOST_TABLE: Option<*mut HashMap<String, String>> = None;
|
||||
lazy_static! {
|
||||
static ref HOST_TABLE: Option<HashMap<String, String>> = create_host_table();
|
||||
}
|
||||
|
||||
|
||||
pub fn global_init() {
|
||||
fn create_host_table() -> Option<HashMap<String, String>> {
|
||||
//TODO: handle bad file path
|
||||
let path = match env::var("HOST_FILE") {
|
||||
Ok(host_file_path) => host_file_path,
|
||||
Err(_) => return,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
let mut file = match File::open(&path) {
|
||||
Ok(f) => BufReader::new(f),
|
||||
Err(_) => return,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
let mut lines = String::new();
|
||||
match file.read_to_string(&mut lines) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
unsafe {
|
||||
let host_table = Box::into_raw(parse_hostsfile(&lines));
|
||||
HOST_TABLE = Some(host_table);
|
||||
}
|
||||
return Some(parse_hostsfile(&lines));
|
||||
}
|
||||
|
||||
pub fn parse_hostsfile(hostsfile_content: &str) -> Box<HashMap<String, String>> {
|
||||
pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, String> {
|
||||
let mut host_table = HashMap::new();
|
||||
let lines: Vec<&str> = hostsfile_content.split('\n').collect();
|
||||
|
||||
for line in &lines {
|
||||
for line in hostsfile_content.split('\n') {
|
||||
let ip_host: Vec<&str> = line.trim().split(|c: char| c == ' ' || c == '\t').collect();
|
||||
if ip_host.len() > 1 {
|
||||
if ip_host[0].parse::<Ipv4Addr>().is_err() && ip_host[0].parse::<Ipv6Addr>().is_err() {
|
||||
|
@ -56,25 +52,21 @@ pub fn parse_hostsfile(hostsfile_content: &str) -> Box<HashMap<String, String>>
|
|||
}
|
||||
}
|
||||
}
|
||||
box host_table
|
||||
host_table
|
||||
}
|
||||
|
||||
pub fn replace_hosts(url: &Url) -> Url {
|
||||
unsafe {
|
||||
HOST_TABLE.map_or_else(|| url.clone(), |host_table| {
|
||||
host_replacement(host_table, url)
|
||||
})
|
||||
}
|
||||
HOST_TABLE.as_ref().map_or_else(|| url.clone(), |host_table| {
|
||||
host_replacement(host_table, url)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn host_replacement(host_table: *mut HashMap<String, String>,
|
||||
pub fn host_replacement(host_table: &HashMap<String, String>,
|
||||
url: &Url) -> Url {
|
||||
unsafe {
|
||||
url.domain().and_then(|domain|
|
||||
(*host_table).get(domain).map(|ip| {
|
||||
let mut net_url = url.clone();
|
||||
*net_url.domain_mut().unwrap() = ip.clone();
|
||||
net_url
|
||||
})).unwrap_or(url.clone())
|
||||
}
|
||||
url.domain().and_then(|domain|
|
||||
host_table.get(domain).map(|ip| {
|
||||
let mut net_url = url.clone();
|
||||
*net_url.domain_mut().unwrap() = ip.clone();
|
||||
net_url
|
||||
})).unwrap_or(url.clone())
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@ extern crate hyper;
|
|||
extern crate image as piston_image;
|
||||
extern crate ipc_channel;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate msg;
|
||||
extern crate serde;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue