Move hosts module into net crate. Remove obsolete functions.

This commit is contained in:
Josh Matthews 2017-04-05 18:32:19 -04:00
parent e772086b8c
commit 6f590a87bf
11 changed files with 13 additions and 45 deletions

View file

@ -20,6 +20,7 @@ hyper_serde = "0.6"
hyper-openssl = "0.2.2"
immeta = "0.3.1"
ipc-channel = "0.7"
lazy_static = "0.2"
log = "0.3.5"
matches = "0.1"
mime = "0.2.1"
@ -27,6 +28,7 @@ mime_guess = "1.8.0"
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
openssl = "0.9"
parse-hosts = "0.3.0"
profile_traits = {path = "../profile_traits"}
serde = "0.9"
serde_derive = "0.9"

View file

@ -2,11 +2,11 @@
* 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 hosts::replace_host;
use hyper::client::Pool;
use hyper::error::{Result as HyperResult, Error as HyperError};
use hyper::net::{NetworkConnector, HttpsStream, HttpStream, SslClient};
use hyper_openssl::OpensslClient;
use net_traits::hosts::replace_host;
use openssl::ssl::{SSL_OP_NO_COMPRESSION, SSL_OP_NO_SSLV2, SSL_OP_NO_SSLV3};
use openssl::ssl::{SslConnectorBuilder, SslMethod};
use std::io;

63
components/net/hosts.rs Normal file
View file

@ -0,0 +1,63 @@
/* 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>> {
// TODO: handle bad file path
let path = match env::var("HOST_FILE") {
Ok(host_file_path) => host_file_path,
Err(_) => return None,
};
let mut file = match File::open(&path) {
Ok(f) => BufReader::new(f),
Err(_) => return None,
};
let mut lines = String::new();
match file.read_to_string(&mut lines) {
Ok(_) => (),
Err(_) => return None,
};
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> {
let mut host_table = HashMap::new();
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
}
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())
}

View file

@ -16,6 +16,8 @@ extern crate hyper_openssl;
extern crate hyper_serde;
extern crate immeta;
extern crate ipc_channel;
#[macro_use]
extern crate lazy_static;
#[macro_use] extern crate log;
#[macro_use] #[no_link] extern crate matches;
#[macro_use]
@ -24,6 +26,7 @@ extern crate mime_guess;
extern crate msg;
extern crate net_traits;
extern crate openssl;
extern crate parse_hosts;
extern crate profile_traits;
extern crate serde;
#[macro_use]
@ -47,6 +50,7 @@ pub mod cookie;
pub mod cookie_storage;
mod data_loader;
pub mod filemanager_thread;
mod hosts;
pub mod hsts;
mod http_loader;
pub mod image_cache;
@ -65,4 +69,5 @@ pub mod fetch {
pub mod test {
pub use chrome_loader::resolve_chrome_url;
pub use http_loader::HttpState;
pub use hosts::{replace_host_table, parse_hostsfile};
}

View file

@ -4,6 +4,7 @@
use cookie::Cookie;
use fetch::methods::{should_be_blocked_due_to_bad_port, should_be_blocked_due_to_nosniff};
use hosts::replace_host;
use http_loader::{HttpState, is_redirect_status, set_default_accept};
use http_loader::{set_default_accept_language, set_request_cookies};
use hyper::buffer::BufReader;
@ -16,7 +17,6 @@ use hyper::status::StatusCode;
use hyper::version::HttpVersion;
use net_traits::{CookieSource, MessageData, NetworkError, WebSocketCommunicate, WebSocketConnectData};
use net_traits::{WebSocketDomAction, WebSocketNetworkEvent};
use net_traits::hosts::replace_host;
use net_traits::request::{Destination, Type};
use servo_url::ServoUrl;
use std::ascii::AsciiExt;