Skip to content

Commit

Permalink
Use NetworkConnector directly to account for replaced hosts
Browse files Browse the repository at this point in the history
This let us remove a hack where we had to replace the host in the request URL.
  • Loading branch information
nox committed Mar 26, 2017
1 parent 54d37d9 commit 92c4a43
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
23 changes: 15 additions & 8 deletions components/net/http_loader.rs
Expand Up @@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -125,12 +127,18 @@ struct NetworkHttpRequestFactory {
pub connector: Arc<Pool<Connector>>,
}

impl NetworkConnector for NetworkHttpRequestFactory {
type Stream = PooledStream<HttpsStream<SslStream<HttpStream>>>;

fn connect(&self, host: &str, port: u16, scheme: &str) -> Result<Self::Stream, HttpError> {
self.connector.connect(&replace_host(host), port, scheme)
}
}

impl NetworkHttpRequestFactory {
fn create(&self, url: ServoUrl, method: Method, headers: Headers)
-> Result<HyperRequest<Fresh>, 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;
Expand Down Expand Up @@ -408,7 +416,6 @@ fn obtain_response(request_factory: &NetworkHttpRequestFactory,
is_xhr: bool)
-> Result<(WrappedHttpResponse, Option<ChromeToDevtoolsControlMsg>), 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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions components/net_traits/hosts.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +57,12 @@ pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
host_table
}

pub fn replace_host(host: &str) -> Cow<str> {
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)
Expand Down

0 comments on commit 92c4a43

Please sign in to comment.