Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace manual host parsing code with parse-host crate #15783

Merged
merged 1 commit into from Mar 10, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Replace manual host parsing code with parse-host crate

  • Loading branch information
ferjm committed Mar 9, 2017
commit 79b7b9de54e42469b6d1a1ad096ea8edebeefd2d

Some generated files are not rendered by default. Learn more.

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

@@ -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;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.