Replace manual host parsing code with parse-host crate

This commit is contained in:
Fernando Jiménez Moreno 2017-03-01 17:11:54 +01:00
parent 5c46e86546
commit 79b7b9de54
4 changed files with 27 additions and 10 deletions

View file

@ -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"]}

View file

@ -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;
@ -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>) {
@ -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
}

View file

@ -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;