aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/net_traits/Cargo.toml1
-rw-r--r--components/net_traits/hosts.rs50
-rw-r--r--components/net_traits/lib.rs2
-rw-r--r--components/servo/Cargo.lock1
-rw-r--r--components/servo/main.rs4
5 files changed, 25 insertions, 33 deletions
diff --git a/components/net_traits/Cargo.toml b/components/net_traits/Cargo.toml
index 461dc467962..1ad98091a31 100644
--- a/components/net_traits/Cargo.toml
+++ b/components/net_traits/Cargo.toml
@@ -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"
diff --git a/components/net_traits/hosts.rs b/components/net_traits/hosts.rs
index 3d80344fd00..4d6824ae033 100644
--- a/components/net_traits/hosts.rs
+++ b/components/net_traits/hosts.rs
@@ -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())
}
diff --git a/components/net_traits/lib.rs b/components/net_traits/lib.rs
index 975ab684e4b..1bb6221ebd2 100644
--- a/components/net_traits/lib.rs
+++ b/components/net_traits/lib.rs
@@ -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;
diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock
index 04129feb28f..5d8fb868fd0 100644
--- a/components/servo/Cargo.lock
+++ b/components/servo/Cargo.lock
@@ -1325,6 +1325,7 @@ dependencies = [
"hyper 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.2.1 (git+https://github.com/servo/ipc-channel)",
+ "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1",
"plugins 0.0.1",
diff --git a/components/servo/main.rs b/components/servo/main.rs
index eca0b3de2eb..8033110e7f1 100644
--- a/components/servo/main.rs
+++ b/components/servo/main.rs
@@ -36,7 +36,6 @@ use gleam::gl;
use offscreen_gl_context::{GLContext, NativeGLContext};
use servo::Browser;
use servo::compositing::windowing::WindowEvent;
-use servo::net_traits::hosts;
use servo::util::opts::{self, ArgumentParsingResult};
use std::rc::Rc;
@@ -66,9 +65,6 @@ fn main() {
setup_logging();
- // Possibly interpret the `HOST_FILE` environment variable
- hosts::global_init();
-
if let Some(token) = content_process_token {
return servo::run_content_process(token)
}