gecko-dev/servo/components/net/hosts.rs
Simon Sapin 681567b0a4 servo: Merge #19411 - Replace parse-hosts crate with 10 lines of code (from servo:parse-hosts); r=nox
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

Source-Repo: https://github.com/servo/servo
Source-Revision: ae3c3ba1c2f265e01a377f73f4a9016aded54367

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : b7d289b529cbdba3679c1e6c626264d90dd648d2
2017-11-28 09:20:44 -06:00

52 lines
1.7 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufReader, Read};
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Mutex;
lazy_static! {
static ref HOST_TABLE: Mutex<Option<HashMap<String, IpAddr>>> = Mutex::new(create_host_table());
}
fn create_host_table() -> Option<HashMap<String, IpAddr>> {
let path = env::var_os("HOST_FILE")?;
let file = File::open(&path).ok()?;
let mut reader = BufReader::new(file);
let mut lines = String::new();
reader.read_to_string(&mut lines).ok()?;
Some(parse_hostsfile(&lines))
}
pub fn replace_host_table(table: HashMap<String, IpAddr>) {
*HOST_TABLE.lock().unwrap() = Some(table);
}
pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
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> {
HOST_TABLE.lock().unwrap().as_ref()
.and_then(|table| table.get(host))
.map_or(host.into(), |replaced_host| replaced_host.to_string().into())
}