Skip to content

Commit

Permalink
HeaderName::from_static requires all-lowercase HTTP2 compatible hea…
Browse files Browse the repository at this point in the history
…der names

and was passed header names with uppercase characters instead, which
made it panic.
  • Loading branch information
sdroege committed Feb 21, 2022
1 parent 5695f5d commit f7d04ae
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/handshake/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ fn generate_request(mut request: Request) -> Result<(Vec<u8>, 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];

Expand All @@ -114,7 +117,9 @@ fn generate_request(mut request: Request) -> Result<(Vec<u8>, 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();
Expand All @@ -127,9 +132,11 @@ fn generate_request(mut request: Request) -> Result<(Vec<u8>, 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();
}
Expand Down

0 comments on commit f7d04ae

Please sign in to comment.