port in host header. close #63
This commit is contained in:
@@ -187,3 +187,25 @@ pub fn header_with_spaces_before_value() {
|
||||
.call();
|
||||
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"));
|
||||
}
|
||||
|
||||
19
src/unit.rs
19
src/unit.rs
@@ -307,7 +307,24 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> IoResult<()> {
|
||||
|
||||
// host header if not set by user.
|
||||
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") {
|
||||
write!(prelude, "User-Agent: ureq\r\n")?;
|
||||
|
||||
Reference in New Issue
Block a user