mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Auto merge of #15783 - ferjm:issue15756-parse-hosts, r=avadacatavra
Replace manual host parsing code with parse-host crate This patch is replacing the code to parse the hosts file with the [parse-hosts](https://crates.io/crates/parse-hosts) crate. This crate has a [CC0 1.0 Universal License](https://creativecommons.org/publicdomain/zero/1.0/deed.en). I could have used [HostsFile::load()](https://clarcharr.github.io/parse-hosts/parse_hosts/struct.HostsFile.html#method.load) directly, but this method loads `/etc/hosts` by default and does not allow to override the default path (for example with [env::var("HOST_FILE")](https://dxr.mozilla.org/servo/source/components/net_traits/hosts.rs#19)), so I kept the existing code to open and read the content of `env::var("HOST_FILE")` and also kept the [parse_hostsfile](https://dxr.mozilla.org/servo/source/components/net_traits/hosts.rs#42) method (used by the unit tests), but I modified it to use [HostsFile::read_buffered](https://clarcharr.github.io/parse-hosts/parse_hosts/struct.HostsFile.html#method.read_buffered), which is doing the actual hosts parsing for a given string buffer. - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #15756 . - [X] There are tests for these changes (tests/unit/net/resource_thread.rs and tests/unit/net/http_loader.rs) <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/15783) <!-- Reviewable:end -->
This commit is contained in:
commit
8f7c69e15f
4 changed files with 27 additions and 10 deletions
|
@ -21,6 +21,7 @@ lazy_static = "0.2"
|
|||
log = "0.3.5"
|
||||
msg = {path = "../msg"}
|
||||
num-traits = "0.1.32"
|
||||
parse-hosts = "0.3.0"
|
||||
serde = "0.9"
|
||||
serde_derive = "0.9"
|
||||
servo_config = {path = "../config", features = ["servo"]}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
* 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 servo_url::ServoUrl;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
|
@ -32,7 +33,7 @@ fn create_host_table() -> Option<HashMap<String, IpAddr>> {
|
|||
Err(_) => return None,
|
||||
};
|
||||
|
||||
return Some(parse_hostsfile(&lines));
|
||||
Some(parse_hostsfile(&lines))
|
||||
}
|
||||
|
||||
pub fn replace_host_table(table: HashMap<String, IpAddr>) {
|
||||
|
@ -41,19 +42,17 @@ pub fn replace_host_table(table: HashMap<String, IpAddr>) {
|
|||
|
||||
pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
|
||||
let mut host_table = HashMap::new();
|
||||
for line in hostsfile_content.split('\n') {
|
||||
let mut ip_host = line.trim().split(|c: char| c == ' ' || c == '\t');
|
||||
if let Some(ip) = ip_host.next() {
|
||||
if let Ok(address) = ip.parse::<IpAddr>() {
|
||||
for token in ip_host {
|
||||
if token.as_bytes()[0] == b'#' {
|
||||
break;
|
||||
}
|
||||
host_table.insert((*token).to_owned(), address);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ extern crate lazy_static;
|
|||
extern crate log;
|
||||
extern crate msg;
|
||||
extern crate num_traits;
|
||||
extern crate parse_hosts;
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue