Skip to content

Commit 5ad92c0

Browse files
committed
fix(awc): ws host req header includes port
1 parent e0918fb commit 5ad92c0

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

awc/CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
## Unreleased
44

5+
- Fix WebSocket `Host` request header value when using a non-default port.
6+
57
## 3.5.0
68

79
- Add `rustls-0_23`, `rustls-0_23-webpki-roots`, and `rustls-0_23-native-roots` crate features.
810
- Add `awc::Connector::rustls_0_23()` constructor.
9-
- Fix `rustls-0_22-native-roots` root store lookup
11+
- Fix `rustls-0_22-native-roots` root store lookup.
1012
- Update `brotli` dependency to `6`.
1113
- Minimum supported Rust version (MSRV) is now 1.72.
1214

awc/src/ws.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ impl WebsocketsRequest {
257257
return Err(e.into());
258258
}
259259

260-
// validate uri
260+
// validate URI
261261
let uri = &self.head.uri;
262+
262263
if uri.host().is_none() {
263264
return Err(InvalidUrl::MissingHost.into());
264265
} else if uri.scheme().is_none() {
@@ -273,9 +274,12 @@ impl WebsocketsRequest {
273274
}
274275

275276
if !self.head.headers.contains_key(header::HOST) {
277+
let hostname = uri.host().unwrap();
278+
let port = uri.port();
279+
276280
self.head.headers.insert(
277281
header::HOST,
278-
HeaderValue::from_str(uri.host().unwrap()).unwrap(),
282+
HeaderValue::from_str(&Host { hostname, port }.to_string()).unwrap(),
279283
);
280284
}
281285

@@ -434,6 +438,25 @@ impl fmt::Debug for WebsocketsRequest {
434438
}
435439
}
436440

441+
/// Formatter for host (hostname+port) header values.
442+
struct Host<'a> {
443+
hostname: &'a str,
444+
port: Option<http::uri::Port<&'a str>>,
445+
}
446+
447+
impl<'a> fmt::Display for Host<'a> {
448+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
449+
f.write_str(self.hostname)?;
450+
451+
if let Some(port) = &self.port {
452+
f.write_str(":")?;
453+
f.write_str(port.as_str())?;
454+
}
455+
456+
Ok(())
457+
}
458+
}
459+
437460
#[cfg(test)]
438461
mod tests {
439462
use super::*;

0 commit comments

Comments
 (0)