aboutsummaryrefslogtreecommitdiffstats
path: root/components/net/hosts.rs
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2017-11-28 16:15:33 +0100
committerSimon Sapin <simon.sapin@exyr.org>2017-11-28 16:19:29 +0100
commiteb3652b3c6ac8f0c27f75e259a37f81089911ba6 (patch)
tree589dbda6c4cb7c98f4e769b5c1e745a9c1201a58 /components/net/hosts.rs
parent66d0a41127a350d162c6a809053290c3da30c7eb (diff)
downloadservo-eb3652b3c6ac8f0c27f75e259a37f81089911ba6.tar.gz
servo-eb3652b3c6ac8f0c27f75e259a37f81089911ba6.zip
Replace parse-hosts crate with 10 lines of code.
This removes 3927 lines of Rust code in 6 crates from the dependency graph: parse-hosts, multistr, bow, extra-default, len-trait, and push-trait. One of these crates doesn’t build in today’s Nightly: https://github.com/rust-lang/rust/issues/46328
Diffstat (limited to 'components/net/hosts.rs')
-rw-r--r--components/net/hosts.rs18
1 files changed, 12 insertions, 6 deletions
diff --git a/components/net/hosts.rs b/components/net/hosts.rs
index 06e58fab3b9..4cc688a474b 100644
--- a/components/net/hosts.rs
+++ b/components/net/hosts.rs
@@ -2,13 +2,12 @@
* 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 std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufReader, Read};
-use std::net::IpAddr;
+use std::net::{IpAddr, Ipv4Addr};
use std::sync::Mutex;
lazy_static! {
@@ -32,10 +31,17 @@ pub fn replace_host_table(table: HashMap<String, IpAddr>) {
}
pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
- HostsFile::read_buffered(hostsfile_content.as_bytes())
- .pairs()
- .filter_map(Result::ok)
- .collect()
+ hostsfile_content.lines().filter_map(|line| {
+ let mut iter = line.split('#').next().unwrap().split_whitespace();
+ Some((iter.next()?.parse().ok()?, iter))
+ }).flat_map(|(ip, hosts)| {
+ hosts.filter(|host| {
+ let invalid = ['\0', '\t', '\n', '\r', ' ', '#', '%', '/', ':', '?', '@', '[', '\\', ']'];
+ host.parse::<Ipv4Addr>().is_err() && !host.contains(&invalid[..])
+ }).map(move |host| {
+ (host.to_owned(), ip)
+ })
+ }).collect()
}
pub fn replace_host(host: &str) -> Cow<str> {