mirror of
https://github.com/servo/servo.git
synced 2025-08-12 00:45:33 +01:00
Use hosts-replaced URL only when loading resources
This commit is contained in:
parent
56a9eab2a0
commit
f52276d2cc
15 changed files with 162 additions and 132 deletions
|
@ -34,6 +34,7 @@ features = [ "serde_serialization" ]
|
|||
[dependencies]
|
||||
log = "0.3"
|
||||
euclid = "0.1"
|
||||
regex = "0.1.33"
|
||||
regex_macros = "0.1.19"
|
||||
serde = "0.4"
|
||||
serde_macros = "0.4"
|
||||
|
||||
|
|
79
components/net_traits/hosts.rs
Normal file
79
components/net_traits/hosts.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
/* 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::collections::HashMap;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
use url::Url;
|
||||
|
||||
use {IPV4_REGEX, IPV6_REGEX};
|
||||
|
||||
static mut HOST_TABLE: Option<*mut HashMap<String, String>> = None;
|
||||
|
||||
|
||||
pub fn global_init() {
|
||||
//TODO: handle bad file path
|
||||
let path = match env::var("HOST_FILE") {
|
||||
Ok(host_file_path) => host_file_path,
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
let mut file = match File::open(&path) {
|
||||
Ok(f) => BufReader::new(f),
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
let mut lines = String::new();
|
||||
match file.read_to_string(&mut lines) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
unsafe {
|
||||
let host_table = Box::into_raw(parse_hostsfile(&lines));
|
||||
HOST_TABLE = Some(host_table);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_hostsfile(hostsfile_content: &str) -> Box<HashMap<String, String>> {
|
||||
let mut host_table = HashMap::new();
|
||||
let lines: Vec<&str> = hostsfile_content.split('\n').collect();
|
||||
|
||||
for line in lines.iter() {
|
||||
let ip_host: Vec<&str> = line.trim().split(|c: char| c == ' ' || c == '\t').collect();
|
||||
if ip_host.len() > 1 {
|
||||
if !IPV4_REGEX.is_match(ip_host[0]) && !IPV6_REGEX.is_match(ip_host[0]) { continue; }
|
||||
let address = ip_host[0].to_owned();
|
||||
|
||||
for token in ip_host.iter().skip(1) {
|
||||
if token.as_bytes()[0] == b'#' {
|
||||
break;
|
||||
}
|
||||
host_table.insert(token.to_owned().to_string(), address.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
box host_table
|
||||
}
|
||||
|
||||
pub fn replace_hosts(url: &Url) -> Url {
|
||||
unsafe {
|
||||
HOST_TABLE.map(|host_table| {
|
||||
host_replacement(host_table, url)
|
||||
}).unwrap_or_else(|| url.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn host_replacement(host_table: *mut HashMap<String, String>,
|
||||
url: &Url) -> Url {
|
||||
unsafe {
|
||||
url.domain().and_then(|domain|
|
||||
(*host_table).get(domain).map(|ip| {
|
||||
let mut net_url = url.clone();
|
||||
*net_url.domain_mut().unwrap() = ip.clone();
|
||||
net_url
|
||||
})).unwrap_or(url.clone())
|
||||
}
|
||||
}
|
|
@ -4,18 +4,22 @@
|
|||
|
||||
#![feature(box_syntax)]
|
||||
#![feature(custom_derive)]
|
||||
#![feature(box_raw)]
|
||||
#![feature(plugin)]
|
||||
#![feature(slice_patterns)]
|
||||
#![feature(step_by)]
|
||||
#![feature(vec_push_all)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
#![plugin(regex_macros)]
|
||||
|
||||
extern crate euclid;
|
||||
extern crate hyper;
|
||||
extern crate ipc_channel;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate png;
|
||||
extern crate regex;
|
||||
extern crate serde;
|
||||
extern crate stb_image;
|
||||
extern crate url;
|
||||
|
@ -28,14 +32,20 @@ use hyper::method::Method;
|
|||
use hyper::mime::{Mime, Attr};
|
||||
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
|
||||
use msg::constellation_msg::{PipelineId};
|
||||
use regex::Regex;
|
||||
use serde::{Deserializer, Serializer};
|
||||
use url::Url;
|
||||
|
||||
use std::thread;
|
||||
|
||||
pub mod hosts;
|
||||
pub mod image_cache_task;
|
||||
pub mod storage_task;
|
||||
|
||||
pub static IPV4_REGEX: Regex = regex!(
|
||||
r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
|
||||
pub static IPV6_REGEX: Regex = regex!(r"^([a-fA-F0-9]{0,4}[:]?){1,8}(/\d{1,3})?$");
|
||||
|
||||
/// Image handling.
|
||||
///
|
||||
/// It may be surprising that this goes in the network crate as opposed to the graphics crate.
|
||||
|
@ -342,4 +352,3 @@ impl Iterator for ProgressMsgPortIterator {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue