aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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
-rw-r--r--ports/cef/Cargo.lock1
-rw-r--r--ports/gonk/Cargo.lock1
-rw-r--r--ports/gonk/src/main.rs3
-rw-r--r--tests/unit/net/resource_thread.rs38
9 files changed, 45 insertions, 56 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)
}
diff --git a/ports/cef/Cargo.lock b/ports/cef/Cargo.lock
index 55bc0699a01..e0c4a3e41b2 100644
--- a/ports/cef/Cargo.lock
+++ b/ports/cef/Cargo.lock
@@ -1226,6 +1226,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/ports/gonk/Cargo.lock b/ports/gonk/Cargo.lock
index 298de62fb40..525979f48da 100644
--- a/ports/gonk/Cargo.lock
+++ b/ports/gonk/Cargo.lock
@@ -1208,6 +1208,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/ports/gonk/src/main.rs b/ports/gonk/src/main.rs
index 4336ae493f9..d477c48f2a7 100644
--- a/ports/gonk/src/main.rs
+++ b/ports/gonk/src/main.rs
@@ -44,7 +44,6 @@ extern crate util;
extern {}
use compositing::windowing::WindowEvent;
-use net_traits::hosts;
use servo::Browser;
use std::env;
use util::opts;
@@ -62,8 +61,6 @@ fn main() {
// Parse the command line options and store them globally
opts::from_cmdline_args(env::args().collect::<Vec<_>>().as_slice());
- hosts::global_init();
-
let window = if opts::get().headless {
None
} else {
diff --git a/tests/unit/net/resource_thread.rs b/tests/unit/net/resource_thread.rs
index 2d3d52f589e..fd9140befd9 100644
--- a/tests/unit/net/resource_thread.rs
+++ b/tests/unit/net/resource_thread.rs
@@ -36,7 +36,7 @@ fn test_bad_scheme() {
fn test_parse_hostsfile() {
let mock_hosts_file_content = "127.0.0.1 foo.bar.com\n127.0.0.2 servo.test.server";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(2, (*hosts_table).len());
+ assert_eq!(2, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"foo.bar.com".to_owned()).unwrap());
assert_eq!("127.0.0.2".to_owned(), *hosts_table.get(&"servo.test.server".to_owned()).unwrap());
}
@@ -45,7 +45,7 @@ fn test_parse_hostsfile() {
fn test_parse_malformed_hostsfile() {
let mock_hosts_file_content = "malformed file\n127.0.0.1 foo.bar.com\nservo.test.server 127.0.0.1";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(1, (*hosts_table).len());
+ assert_eq!(1, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"foo.bar.com".to_owned()).unwrap());
}
@@ -53,7 +53,7 @@ fn test_parse_malformed_hostsfile() {
fn test_parse_hostsfile_with_line_comment() {
let mock_hosts_file_content = "# this is a line comment\n127.0.0.1 foo.bar.com\n# anothercomment";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(1, (*hosts_table).len());
+ assert_eq!(1, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"foo.bar.com".to_owned()).unwrap());
}
@@ -61,7 +61,7 @@ fn test_parse_hostsfile_with_line_comment() {
fn test_parse_hostsfile_with_end_of_line_comment() {
let mock_hosts_file_content = "127.0.0.1 foo.bar.com # line ending comment\n127.0.0.2 servo.test.server #comment";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(2, (*hosts_table).len());
+ assert_eq!(2, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"foo.bar.com".to_owned()).unwrap());
assert_eq!("127.0.0.2".to_owned(), *hosts_table.get(&"servo.test.server".to_owned()).unwrap());
}
@@ -70,7 +70,7 @@ fn test_parse_hostsfile_with_end_of_line_comment() {
fn test_parse_hostsfile_with_2_hostnames_for_1_address() {
let mock_hosts_file_content = "127.0.0.1 foo.bar.com baz.bar.com";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(2, (*hosts_table).len());
+ assert_eq!(2, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"foo.bar.com".to_owned()).unwrap());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"baz.bar.com".to_owned()).unwrap());
}
@@ -79,7 +79,7 @@ fn test_parse_hostsfile_with_2_hostnames_for_1_address() {
fn test_parse_hostsfile_with_4_hostnames_for_1_address() {
let mock_hosts_file_content = "127.0.0.1 moz.foo.com moz.bar.com moz.baz.com moz.moz.com";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(4, (*hosts_table).len());
+ assert_eq!(4, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"moz.foo.com".to_owned()).unwrap());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"moz.bar.com".to_owned()).unwrap());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"moz.baz.com".to_owned()).unwrap());
@@ -90,7 +90,7 @@ fn test_parse_hostsfile_with_4_hostnames_for_1_address() {
fn test_parse_hostsfile_with_tabs_instead_spaces() {
let mock_hosts_file_content = "127.0.0.1\tfoo.bar.com\n127.0.0.2\tservo.test.server";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(2, (*hosts_table).len());
+ assert_eq!(2, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"foo.bar.com".to_owned()).unwrap());
assert_eq!("127.0.0.2".to_owned(), *hosts_table.get(&"servo.test.server".to_owned()).unwrap());
}
@@ -101,7 +101,7 @@ fn test_parse_hostsfile_with_valid_ipv4_addresses()
let mock_hosts_file_content =
"255.255.255.255 foo.bar.com\n169.0.1.201 servo.test.server\n192.168.5.0 servo.foo.com";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(3, (*hosts_table).len());
+ assert_eq!(3, hosts_table.len());
}
#[test]
@@ -110,7 +110,7 @@ fn test_parse_hostsfile_with_invalid_ipv4_addresses()
let mock_hosts_file_content = "256.255.255.255 foo.bar.com\n169.0.1000.201 servo.test.server \
\n192.168.5.500 servo.foo.com\n192.abc.100.2 test.servo.com";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(0, (*hosts_table).len());
+ assert_eq!(0, hosts_table.len());
}
#[test]
@@ -124,7 +124,7 @@ fn test_parse_hostsfile_with_valid_ipv6_addresses()
2001:0DB8:85A3:0042:1000:8A2E:0370:7334 baz.bar.moz\n\
:: unspecified.moz.com";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(9, (*hosts_table).len());
+ assert_eq!(9, hosts_table.len());
}
#[test]
@@ -134,7 +134,7 @@ fn test_parse_hostsfile_with_invalid_ipv6_addresses()
2001:zdb8:0:0:0:gg00:42:t329 moz.foo.com\n\
2002:0DB8:85A3:0042:1000:8A2E:0370:7334/1289 baz3.bar.moz";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(0, (*hosts_table).len());
+ assert_eq!(0, hosts_table.len());
}
#[test]
@@ -144,7 +144,7 @@ fn test_parse_hostsfile_with_end_of_line_whitespace()
2001:db8:0:0:0:ff00:42:8329 moz.foo.com\n \
127.0.0.2 servo.test.server ";
let hosts_table = parse_hostsfile(mock_hosts_file_content);
- assert_eq!(3, (*hosts_table).len());
+ assert_eq!(3, hosts_table.len());
assert_eq!("127.0.0.1".to_owned(), *hosts_table.get(&"foo.bar.com".to_owned()).unwrap());
assert_eq!("2001:db8:0:0:0:ff00:42:8329".to_owned(), *hosts_table.get(&"moz.foo.com".to_owned()).unwrap());
assert_eq!("127.0.0.2".to_owned(), *hosts_table.get(&"servo.test.server".to_owned()).unwrap());
@@ -152,20 +152,18 @@ fn test_parse_hostsfile_with_end_of_line_whitespace()
#[test]
fn test_replace_hosts() {
- let mut host_table_box = Box::new(HashMap::new());
- host_table_box.insert("foo.bar.com".to_owned(), "127.0.0.1".to_owned());
- host_table_box.insert("servo.test.server".to_owned(), "127.0.0.2".to_owned());
-
- let host_table: *mut HashMap<String, String> = Box::into_raw(host_table_box);
+ let mut host_table = HashMap::new();
+ host_table.insert("foo.bar.com".to_owned(), "127.0.0.1".to_owned());
+ host_table.insert("servo.test.server".to_owned(), "127.0.0.2".to_owned());
let url = url!("http://foo.bar.com:8000/foo");
- assert_eq!(host_replacement(host_table, &url).domain().unwrap(), "127.0.0.1");
+ assert_eq!(host_replacement(&host_table, &url).domain().unwrap(), "127.0.0.1");
let url = url!("http://servo.test.server");
- assert_eq!(host_replacement(host_table, &url).domain().unwrap(), "127.0.0.2");
+ assert_eq!(host_replacement(&host_table, &url).domain().unwrap(), "127.0.0.2");
let url = url!("http://a.foo.bar.com");
- assert_eq!(host_replacement(host_table, &url).domain().unwrap(), "a.foo.bar.com");
+ assert_eq!(host_replacement(&host_table, &url).domain().unwrap(), "a.foo.bar.com");
}
#[test]