From 5b75deccef13f1ceb2b8d546a735c0d3bc3bfc83 Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Tue, 6 Oct 2020 00:10:56 -0700 Subject: [PATCH] Use correct host on redirect. (#180) --- src/test/redirect.rs | 25 +++++++++++++++++++++++++ src/unit.rs | 6 +++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/test/redirect.rs b/src/test/redirect.rs index 03639e2d..5ec1a412 100644 --- a/src/test/redirect.rs +++ b/src/test/redirect.rs @@ -1,3 +1,9 @@ +use std::{ + io::{self, Write}, + net::TcpStream, +}; +use test::testserver::{self, TestServer}; + use crate::test; use super::super::*; @@ -76,6 +82,25 @@ fn redirect_get() { assert_eq!(resp.header("x-foo").unwrap(), "bar"); } +#[test] +fn redirect_host() { + // Set up a redirect to a host that doesn't exist; it should fail. + let srv = TestServer::new(|mut stream: TcpStream| -> io::Result<()> { + testserver::read_headers(&stream); + write!(stream, "HTTP/1.1 302 Found\r\n")?; + write!(stream, "Location: http://example.invalid/\r\n")?; + write!(stream, "\r\n")?; + Ok(()) + }); + let url = format!("http://localhost:{}/", srv.port); + let resp = crate::get(&url).call(); + assert!( + matches!(resp.synthetic_error(), Some(Error::DnsFailed(_))), + "{:?}", + resp.synthetic_error() + ); +} + #[test] fn redirect_post() { test::set_handler("/redirect_post1", |_| { diff --git a/src/unit.rs b/src/unit.rs index bb080f01..3ec47944 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -145,7 +145,10 @@ pub(crate) fn connect( ) -> Result { // - let host = req.get_host()?; + let host = unit + .url + .host_str() + .ok_or(Error::BadUrl("no host".to_string()))?; let url = &unit.url; let method = &unit.req.method; // open socket @@ -361,6 +364,7 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> io::Result<()> // finish write!(prelude, "\r\n")?; + debug!("writing prelude: {}", String::from_utf8_lossy(&prelude)); // write all to the wire stream.write_all(&prelude[..])?;