Skip to content

Commit

Permalink
Replace manual host parsing code with parse-host crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Mar 9, 2017
1 parent 5c46e86 commit 79b7b9d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/net_traits/Cargo.toml
Expand Up @@ -21,6 +21,7 @@ lazy_static = "0.2"
log = "0.3.5"
msg = {path = "../msg"}
num-traits = "0.1.32"
parse-hosts = "0.3.0"
serde = "0.9"
serde_derive = "0.9"
servo_config = {path = "../config", features = ["servo"]}
Expand Down
19 changes: 9 additions & 10 deletions components/net_traits/hosts.rs
Expand Up @@ -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/. */

use parse_hosts::HostsFile;
use servo_url::ServoUrl;
use std::collections::HashMap;
use std::env;
Expand Down Expand Up @@ -32,7 +33,7 @@ fn create_host_table() -> Option<HashMap<String, IpAddr>> {
Err(_) => return None,
};

return Some(parse_hostsfile(&lines));
Some(parse_hostsfile(&lines))
}

pub fn replace_host_table(table: HashMap<String, IpAddr>) {
Expand All @@ -41,19 +42,17 @@ pub fn replace_host_table(table: HashMap<String, IpAddr>) {

pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
let mut host_table = HashMap::new();
for line in hostsfile_content.split('\n') {
let mut ip_host = line.trim().split(|c: char| c == ' ' || c == '\t');
if let Some(ip) = ip_host.next() {
if let Ok(address) = ip.parse::<IpAddr>() {
for token in ip_host {
if token.as_bytes()[0] == b'#' {
break;
}
host_table.insert((*token).to_owned(), address);

for line in HostsFile::read_buffered(hostsfile_content.as_bytes()).lines() {
if let Ok(ref line) = line {
for host in line.hosts() {
if let Some(ip) = line.ip() {
host_table.insert(host.to_owned(), ip);
}
}
}
}

host_table
}

Expand Down
1 change: 1 addition & 0 deletions components/net_traits/lib.rs
Expand Up @@ -22,6 +22,7 @@ extern crate lazy_static;
extern crate log;
extern crate msg;
extern crate num_traits;
extern crate parse_hosts;
extern crate serde;
#[macro_use]
extern crate serde_derive;
Expand Down

0 comments on commit 79b7b9d

Please sign in to comment.