From 92c4a43946d97eada60b400d06afa92754afc88b Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 25 Mar 2017 15:14:20 +0100 Subject: [PATCH] Use NetworkConnector directly to account for replaced hosts This let us remove a hack where we had to replace the host in the request URL. --- components/net/http_loader.rs | 23 +++++++++++++++-------- components/net_traits/hosts.rs | 7 +++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 8f3c9a36d109..425f38f1e0ee 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -15,6 +15,7 @@ use hsts::HstsList; use hyper::Error as HttpError; use hyper::LanguageTag; use hyper::client::{Pool, Request as HyperRequest, Response as HyperResponse}; +use hyper::client::pool::PooledStream; use hyper::header::{AcceptEncoding, AcceptLanguage, AccessControlAllowCredentials}; use hyper::header::{AccessControlAllowOrigin, AccessControlAllowHeaders, AccessControlAllowMethods}; use hyper::header::{AccessControlRequestHeaders, AccessControlMaxAge, AccessControlRequestMethod}; @@ -24,17 +25,18 @@ use hyper::header::{IfUnmodifiedSince, IfModifiedSince, IfNoneMatch, Location, P use hyper::header::{QualityItem, Referer, SetCookie, UserAgent, qitem}; use hyper::header::Origin as HyperOrigin; use hyper::method::Method; -use hyper::net::Fresh; +use hyper::net::{Fresh, HttpStream, HttpsStream, NetworkConnector}; use hyper::status::StatusCode; use hyper_serde::Serde; use log; use msg::constellation_msg::PipelineId; use net_traits::{CookieSource, FetchMetadata, NetworkError, ReferrerPolicy}; -use net_traits::hosts::replace_host_in_url; +use net_traits::hosts::replace_host; 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}; use openssl; +use openssl::ssl::SslStream; use openssl::ssl::error::{OpensslError, SslError}; use resource_thread::AuthCache; use servo_url::{ImmutableOrigin, ServoUrl}; @@ -125,12 +127,18 @@ struct NetworkHttpRequestFactory { pub connector: Arc>, } +impl NetworkConnector for NetworkHttpRequestFactory { + type Stream = PooledStream>>; + + fn connect(&self, host: &str, port: u16, scheme: &str) -> Result { + self.connector.connect(&replace_host(host), port, scheme) + } +} + impl NetworkHttpRequestFactory { fn create(&self, url: ServoUrl, method: Method, headers: Headers) -> Result, NetworkError> { - let connection = HyperRequest::with_connector(method, - url.clone().into_url(), - &*self.connector); + let connection = HyperRequest::with_connector(method, url.clone().into_url(), self); if let Err(HttpError::Ssl(ref error)) = connection { let error: &(Error + Send + 'static) = &**error; @@ -408,7 +416,6 @@ fn obtain_response(request_factory: &NetworkHttpRequestFactory, is_xhr: bool) -> Result<(WrappedHttpResponse, Option), NetworkError> { let null_data = None; - 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 @@ -439,7 +446,7 @@ fn obtain_response(request_factory: &NetworkHttpRequestFactory, } if log_enabled!(log::LogLevel::Info) { - info!("{} {}", method, connection_url); + info!("{} {}", method, url); for header in headers.iter() { info!(" - {}", header); } @@ -448,7 +455,7 @@ fn obtain_response(request_factory: &NetworkHttpRequestFactory, let connect_start = precise_time_ms(); - let request = try!(request_factory.create(connection_url.clone(), method.clone(), + let request = try!(request_factory.create(url.clone(), method.clone(), headers.clone())); let connect_end = precise_time_ms(); diff --git a/components/net_traits/hosts.rs b/components/net_traits/hosts.rs index 2afc79b1340d..741d9734df49 100644 --- a/components/net_traits/hosts.rs +++ b/components/net_traits/hosts.rs @@ -4,6 +4,7 @@ use parse_hosts::HostsFile; use servo_url::ServoUrl; +use std::borrow::Cow; use std::collections::HashMap; use std::env; use std::fs::File; @@ -56,6 +57,12 @@ pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap { host_table } +pub fn replace_host(host: &str) -> Cow { + HOST_TABLE.lock().unwrap().as_ref() + .and_then(|table| table.get(host)) + .map_or(host.into(), |replaced_host| replaced_host.to_string().into()) +} + pub fn replace_host_in_url(url: ServoUrl) -> ServoUrl { if let Some(table) = HOST_TABLE.lock().unwrap().as_ref() { host_replacement(table, url)