aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMs2ger <ms2ger@gmail.com>2015-03-20 21:07:16 +0100
committerMs2ger <ms2ger@gmail.com>2015-03-20 21:07:16 +0100
commit2cde68f13f997476ffd387fc19b347106b4d7ea4 (patch)
treef38fc7d9e2c93c551ef8fe914f1c433c62f0cfdc
parentaa6ed369b80cbbc81eef9e8697963d0d64358f9b (diff)
downloadservo-2cde68f13f997476ffd387fc19b347106b4d7ea4.tar.gz
servo-2cde68f13f997476ffd387fc19b347106b4d7ea4.zip
Cleanup resource_task's global_init().
-rw-r--r--components/net/lib.rs1
-rw-r--r--components/net/resource_task.rs35
2 files changed, 24 insertions, 12 deletions
diff --git a/components/net/lib.rs b/components/net/lib.rs
index 2387583b76e..7137b410af7 100644
--- a/components/net/lib.rs
+++ b/components/net/lib.rs
@@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+#![feature(alloc)]
#![feature(box_syntax)]
#![feature(collections)]
#![feature(core)]
diff --git a/components/net/resource_task.rs b/components/net/resource_task.rs
index 688c507c06f..8208aef00a9 100644
--- a/components/net/resource_task.rs
+++ b/components/net/resource_task.rs
@@ -23,9 +23,9 @@ use hyper::mime::{Mime, Attr};
use url::Url;
use std::borrow::{ToOwned, IntoCow};
+use std::boxed;
use std::collections::HashMap;
use std::env;
-use std::mem;
use std::old_io::{BufferedReader, File};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thunk::Invoke;
@@ -38,16 +38,25 @@ use std::old_io::net::tcp::TcpListener;
static mut HOST_TABLE: Option<*mut HashMap<String, String>> = None;
pub fn global_init() {
- if let Ok(host_file_path) = env::var("HOST_FILE") {
- //TODO: handle bad file path
- let path = Path::new(host_file_path);
- let mut file = BufferedReader::new(File::open(&path));
- if let Ok(lines) = file.read_to_string(){
- unsafe {
- let host_table: *mut HashMap<String, String> = mem::transmute(parse_hostsfile(lines.as_slice()));
- HOST_TABLE = Some(host_table);
- }
- }
+ //TODO: handle bad file path
+ let path = match env::var("HOST_FILE") {
+ Ok(host_file_path) => Path::new(host_file_path),
+ Err(_) => return,
+ };
+
+ let mut file = match File::open(&path) {
+ Ok(f) => BufferedReader::new(f),
+ Err(_) => return,
+ };
+
+ let lines = match file.read_to_string() {
+ Ok(lines) => lines,
+ Err(_) => return,
+ };
+
+ unsafe {
+ let host_table = boxed::into_raw(parse_hostsfile(lines.as_slice()));
+ HOST_TABLE = Some(host_table);
}
}
@@ -526,7 +535,9 @@ fn test_replace_hosts() {
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> = unsafe {mem::transmute(host_table_box)};
+ let host_table: *mut HashMap<String, String> = unsafe {
+ boxed::into_raw(host_table_box)
+ };
//Start the TCP server
let mut listener = TcpListener::bind("127.0.0.1:0").unwrap();