Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct host on redirect. #180

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/test/redirect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::{
io::{self, Write},
net::TcpStream,
};
use test::testserver::{self, TestServer};

use crate::test;

use super::super::*;
Expand Down Expand Up @@ -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", |_| {
Expand Down
6 changes: 5 additions & 1 deletion src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ pub(crate) fn connect(
) -> Result<Response, Error> {
//

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
Expand Down Expand Up @@ -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[..])?;

Expand Down