aboutsummaryrefslogtreecommitdiffstats
path: root/components/net/hosts.rs
diff options
context:
space:
mode:
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> {