diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 750a94c5636..9a23ff0e4d7 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -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), 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 diff --git a/components/net/websocket_loader.rs b/components/net/websocket_loader.rs index 34c17798993..e3f4c20da08 100644 --- a/components/net/websocket_loader.rs +++ b/components/net/websocket_loader.rs @@ -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 diff --git a/components/net_traits/hosts.rs b/components/net_traits/hosts.rs index dc81906683e..2afc79b1340 100644 --- a/components/net_traits/hosts.rs +++ b/components/net_traits/hosts.rs @@ -56,19 +56,18 @@ pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap { 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, 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, 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 } diff --git a/tests/unit/net/resource_thread.rs b/tests/unit/net/resource_thread.rs index 0e3f01bb54c..5062917b8dc 100644 --- a/tests/unit/net/resource_thread.rs +++ b/tests/unit/net/resource_thread.rs @@ -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"); }