From f7d04aee0b40e55082b788f2772522ab2ee295da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 21 Feb 2022 15:19:46 +0200 Subject: [PATCH] `HeaderName::from_static` requires all-lowercase HTTP2 compatible header names and was passed header names with uppercase characters instead, which made it panic. --- src/handshake/client.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index 3bee72c1..11393aa5 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -105,7 +105,10 @@ fn generate_request(mut request: Request) -> Result<(Vec, String)> { .unwrap(); // Headers that must be present in a correct request. + const KEY_HEADERNAME_LOWERCASE: &str = "sec-websocket-key"; const KEY_HEADERNAME: &str = "Sec-WebSocket-Key"; + const WEBSOCKET_HEADERS_LOWERCASE: [&str; 5] = + ["host", "connection", "upgrade", "sec-websocket-version", KEY_HEADERNAME_LOWERCASE]; const WEBSOCKET_HEADERS: [&str; 5] = ["Host", "Connection", "Upgrade", "Sec-WebSocket-Version", KEY_HEADERNAME]; @@ -114,7 +117,9 @@ fn generate_request(mut request: Request) -> Result<(Vec, String)> { .headers() .get(KEY_HEADERNAME) .ok_or_else(|| { - Error::Protocol(ProtocolError::InvalidHeader(HeaderName::from_static(KEY_HEADERNAME))) + Error::Protocol(ProtocolError::InvalidHeader(HeaderName::from_static( + KEY_HEADERNAME_LOWERCASE, + ))) })? .to_str()? .to_owned(); @@ -127,9 +132,11 @@ fn generate_request(mut request: Request) -> Result<(Vec, String)> { // // See similar problem in `hyper`: https://github.com/hyperium/hyper/issues/1492 let headers = request.headers_mut(); - for header in WEBSOCKET_HEADERS { + for (header_lowercase, &header) in + Iterator::zip(WEBSOCKET_HEADERS_LOWERCASE.iter(), WEBSOCKET_HEADERS.iter()) + { let value = headers.remove(header).ok_or_else(|| { - Error::Protocol(ProtocolError::InvalidHeader(HeaderName::from_static(header))) + Error::Protocol(ProtocolError::InvalidHeader(HeaderName::from_static(header_lowercase))) })?; write!(req, "{header}: {value}\r\n", value = value.to_str()?).unwrap(); }