diff --git a/src/stream.rs b/src/stream.rs index d0c1b7f..1e626f0 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -328,10 +328,10 @@ pub(crate) fn connect_http(unit: &Unit, hostname: &str) -> Result connect_host(unit, hostname, port).map(|(t, r)| Stream::new(t, r, pool_returner)) } -pub(crate) fn connect_https(unit: &Unit, hostname: &str) -> Result { +pub(crate) fn connect_https(unit: &Unit, hostname: &str, spoofed_host: Option<&str>) -> Result { let port = unit.url.port().unwrap_or(443); - let (sock, remote_addr) = connect_host(unit, hostname, port)?; + let (sock, remote_addr) = connect_host(unit, spoofed_host.unwrap_or(hostname), port)?; let tls_conf = &unit.agent.config.tls_config; let https_stream = tls_conf.connect(hostname, Box::new(sock))?; diff --git a/src/unit.rs b/src/unit.rs index 5b60c00..2c7f7fb 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -259,12 +259,10 @@ fn connect_inner( let url = &unit.url; let method = &unit.method; - let socket_host = unit.spoofed_host.as_ref() - .map(|r|r.as_ref()) - .unwrap_or(host); + let spoofed_host = unit.spoofed_host.as_ref().map(|r|r.as_ref()); // open socket - let (mut stream, is_recycled) = connect_socket(unit, socket_host, use_pooled)?; + let (mut stream, is_recycled) = connect_socket(unit, host, use_pooled, spoofed_host)?; if is_recycled { debug!("sending request (reused connection) {} {}", method, url); @@ -354,7 +352,7 @@ fn extract_cookies(agent: &Agent, url: &Url) -> Option
{ } /// Connect the socket, either by using the pool or grab a new one. -fn connect_socket(unit: &Unit, hostname: &str, use_pooled: bool) -> Result<(Stream, bool), Error> { +fn connect_socket(unit: &Unit, hostname: &str, use_pooled: bool, spoofed_host: Option<&str>) -> Result<(Stream, bool), Error> { match unit.url.scheme() { "http" | "https" | "test" => (), scheme => return Err(ErrorKind::UnknownScheme.msg(format!("unknown scheme '{}'", scheme))), @@ -378,8 +376,13 @@ fn connect_socket(unit: &Unit, hostname: &str, use_pooled: bool) -> Result<(Stre } } let stream = match unit.url.scheme() { - "http" => stream::connect_http(unit, hostname), - "https" => stream::connect_https(unit, hostname), + + // http can just have the hostname spoofed as is + "http" => stream::connect_http(unit, spoofed_host.unwrap_or(hostname)), + + // https requires both the spoofed and non spoofed host names + "https" => stream::connect_https(unit, hostname, spoofed_host), + "test" => connect_test(unit), scheme => Err(ErrorKind::UnknownScheme.msg(format!("unknown scheme {}", scheme))), };