port in host header. close #63

This commit is contained in:
Martin Algesten
2020-06-12 22:00:30 +02:00
parent 4196eeff40
commit deb2002f6f
2 changed files with 40 additions and 1 deletions

View File

@@ -187,3 +187,25 @@ pub fn header_with_spaces_before_value() {
.call(); .call();
assert_eq!(resp.status(), 200); assert_eq!(resp.status(), 200);
} }
#[test]
pub fn host_no_port() {
test::set_handler("/host_no_port", |_| {
test::make_response(200, "OK", vec![], vec![])
});
let resp = get("test://myhost/host_no_port").call();
let vec = resp.to_write_vec();
let s = String::from_utf8_lossy(&vec);
assert!(s.contains("\r\nHost: myhost\r\n"));
}
#[test]
pub fn host_with_port() {
test::set_handler("/host_with_port", |_| {
test::make_response(200, "OK", vec![], vec![])
});
let resp = get("test://myhost:234/host_with_port").call();
let vec = resp.to_write_vec();
let s = String::from_utf8_lossy(&vec);
assert!(s.contains("\r\nHost: myhost:234\r\n"));
}

View File

@@ -307,7 +307,24 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> IoResult<()> {
// host header if not set by user. // host header if not set by user.
if !header::has_header(&unit.headers, "host") { if !header::has_header(&unit.headers, "host") {
write!(prelude, "Host: {}\r\n", unit.url.host().unwrap())?; let host = unit.url.host().unwrap();
match unit.url.port() {
Some(port) => {
let scheme_default: u16 = match unit.url.scheme() {
"http" => 80,
"https" => 443,
_ => 0,
};
if scheme_default != 0 && scheme_default == port {
write!(prelude, "Host: {}\r\n", host)?;
} else {
write!(prelude, "Host: {}:{}\r\n", host, port)?;
}
}
None => {
write!(prelude, "Host: {}\r\n", host)?;
}
}
} }
if !header::has_header(&unit.headers, "user-agent") { if !header::has_header(&unit.headers, "user-agent") {
write!(prelude, "User-Agent: ureq\r\n")?; write!(prelude, "User-Agent: ureq\r\n")?;