Rework replace_hosts and host_replacement

They do less cloning now.
This commit is contained in:
Anthony Ramine 2017-03-25 12:07:14 +01:00
parent c2d9f663af
commit bc5c4fa892
4 changed files with 19 additions and 20 deletions

View file

@ -30,7 +30,7 @@ use hyper_serde::Serde;
use log;
use msg::constellation_msg::PipelineId;
use net_traits::{CookieSource, FetchMetadata, NetworkError, ReferrerPolicy};
use net_traits::hosts::replace_hosts;
use net_traits::hosts::replace_host_in_url;
use net_traits::request::{CacheMode, CredentialsMode, Destination, Origin};
use net_traits::request::{RedirectMode, Referrer, Request, RequestMode, ResponseTainting};
use net_traits::response::{HttpsState, Response, ResponseBody, ResponseType};
@ -408,7 +408,7 @@ fn obtain_response(request_factory: &NetworkHttpRequestFactory,
is_xhr: bool)
-> Result<(WrappedHttpResponse, Option<ChromeToDevtoolsControlMsg>), NetworkError> {
let null_data = None;
let connection_url = replace_hosts(&url);
let connection_url = replace_host_in_url(url.clone());
// loop trying connections in connection pool
// they may have grown stale (disconnected), in which case we'll get

View file

@ -9,7 +9,7 @@ use http_loader;
use hyper::header::{Host, SetCookie};
use net_traits::{CookieSource, MessageData, WebSocketCommunicate};
use net_traits::{WebSocketConnectData, WebSocketDomAction, WebSocketNetworkEvent};
use net_traits::hosts::replace_hosts;
use net_traits::hosts::replace_host_in_url;
use servo_url::ServoUrl;
use std::ascii::AsciiExt;
use std::sync::{Arc, Mutex, RwLock};
@ -44,7 +44,7 @@ fn establish_a_websocket_connection(resource_url: &ServoUrl,
}
// Steps 3-7.
let net_url = replace_hosts(resource_url);
let net_url = replace_host_in_url(resource_url.clone());
let mut request = try!(Client::connect(net_url.as_url()));
// Client::connect sets the Host header to the host of the URL that is

View file

@ -56,19 +56,18 @@ pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
host_table
}
pub fn replace_hosts(url: &ServoUrl) -> ServoUrl {
HOST_TABLE.lock().unwrap().as_ref().map_or_else(|| url.clone(),
|host_table| host_replacement(host_table, url))
pub fn replace_host_in_url(url: ServoUrl) -> ServoUrl {
if let Some(table) = HOST_TABLE.lock().unwrap().as_ref() {
host_replacement(table, url)
} else {
url
}
}
pub fn host_replacement(host_table: &HashMap<String, IpAddr>, url: &ServoUrl) -> ServoUrl {
url.domain()
.and_then(|domain| {
host_table.get(domain).map(|ip| {
let mut new_url = url.clone();
new_url.set_ip_host(*ip).unwrap();
new_url
})
})
.unwrap_or_else(|| url.clone())
pub fn host_replacement(host_table: &HashMap<String, IpAddr>, mut url: ServoUrl) -> ServoUrl {
let replacement = url.domain().and_then(|domain| host_table.get(domain));
if let Some(ip) = replacement {
url.set_ip_host(*ip).unwrap();
}
url
}

View file

@ -151,11 +151,11 @@ fn test_replace_hosts() {
host_table.insert("servo.test.server".to_owned(), ip("127.0.0.2"));
let url = ServoUrl::parse("http://foo.bar.com:8000/foo").unwrap();
assert_eq!(host_replacement(&host_table, &url).host_str().unwrap(), "127.0.0.1");
assert_eq!(host_replacement(&host_table, url).host_str().unwrap(), "127.0.0.1");
let url = ServoUrl::parse("http://servo.test.server").unwrap();
assert_eq!(host_replacement(&host_table, &url).host_str().unwrap(), "127.0.0.2");
assert_eq!(host_replacement(&host_table, url).host_str().unwrap(), "127.0.0.2");
let url = ServoUrl::parse("http://a.foo.bar.com").unwrap();
assert_eq!(host_replacement(&host_table, &url).host_str().unwrap(), "a.foo.bar.com");
assert_eq!(host_replacement(&host_table, url).host_str().unwrap(), "a.foo.bar.com");
}