Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NetworkConnector directly to account for replaced hosts #16131

Merged
merged 5 commits into from Mar 26, 2017
Next

Rework replace_hosts and host_replacement

They do less cloning now.
  • Loading branch information
nox committed Mar 26, 2017
commit bc5c4fa8925c707b8de2b68f8204bb2fbbe1ca96
@@ -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
@@ -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
@@ -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
}
@@ -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");
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.