Fix ws client to upgrade with proper host headers

This commit is contained in:
Heinz Gies 2019-09-11 17:23:09 +02:00 committed by Heinz N. Gies
parent 06f98e64aa
commit 75b01c802c
1 changed files with 24 additions and 4 deletions

View File

@ -234,10 +234,30 @@ impl WebsocketsRequest {
} }
if !self.head.headers.contains_key(header::HOST) { if !self.head.headers.contains_key(header::HOST) {
self.head.headers.insert( let port = uri.port_u16();
header::HOST, let scheme = uri.scheme();
HeaderValue::from_str(uri.host().unwrap()).unwrap(), // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
); // requires us to include the port if it's not standard
let needs_port = ((scheme == Some("http") || scheme == Some("ws"))
&& port != Some(80))
|| ((scheme == Some("https") || scheme == Some("wss"))
&& port != Some(443));
if needs_port {
self.head.headers.insert(
header::HOST,
HeaderValue::from_str(&format!(
"{}:{}",
uri.host().unwrap(),
port.unwrap()
))
.unwrap(),
);
} else {
self.head.headers.insert(
header::HOST,
HeaderValue::from_str(uri.host().unwrap()).unwrap(),
);
};
} }
// set cookies // set cookies