mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-21 09:15:35 +00:00
aca60f24b9
- [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes do not require tests because they are refactoring only Source-Repo: https://github.com/servo/servo Source-Revision: 2b03a9974c61d1481d4b40351ff1305ad0b26588 --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : ff1d2ee734428d0ff4590fcf8dd03de2bc2302b6
46 lines
1.3 KiB
Rust
46 lines
1.3 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 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::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::read_buffered(hostsfile_content.as_bytes())
|
|
.pairs()
|
|
.filter_map(Result::ok)
|
|
.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())
|
|
}
|