From ddd349dbce7ca81758757aeb759d434621312841 Mon Sep 17 00:00:00 2001 From: jonasBoss Date: Mon, 28 Jul 2025 13:39:16 +0200 Subject: [PATCH 1/2] make http header parsing case insensitive --- src/http.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/http.rs b/src/http.rs index 408a3c5..fb67dc5 100644 --- a/src/http.rs +++ b/src/http.rs @@ -45,7 +45,7 @@ pub fn read_http_header<'a>( let mut sec_websocket_key = String::new(); for (name, value) in headers { - match name { + match_ascii_case_insensitive!{ name, "Upgrade" => is_websocket_request = str::from_utf8(value)? == "websocket", "Sec-WebSocket-Protocol" => { // extract a csv list of supported sub protocols @@ -54,15 +54,15 @@ pub fn read_http_header<'a>( // it is safe to unwrap here because we have checked // the size of the list beforehand sec_websocket_protocol_list - .push(String::from(item)) + .push(String::try_from(item)?) .unwrap(); } } - } + }, "Sec-WebSocket-Key" => { - sec_websocket_key = String::from(str::from_utf8(value)?); - } - &_ => { + sec_websocket_key = String::try_from(str::from_utf8(value)?)?; + }, + _ => { // ignore all other headers } } @@ -78,6 +78,14 @@ pub fn read_http_header<'a>( } } +macro_rules! match_ascii_case_insensitive { + ($m:expr, $($case:literal => $do:expr),+ , _ => $default:expr) => { + if false {} + $(else if $m.eq_ignore_ascii_case($case) { $do })+ + else {$default} + }; +} + pub fn read_server_connect_handshake_response( sec_websocket_key: &WebSocketKey, from: &[u8], From 0a1c60825ed7ece2a8a5d507bcdb17094aa8bf9a Mon Sep 17 00:00:00 2001 From: jonasBoss Date: Mon, 28 Jul 2025 13:41:40 +0200 Subject: [PATCH 2/2] fixup --- src/http.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http.rs b/src/http.rs index fb67dc5..6afd1ad 100644 --- a/src/http.rs +++ b/src/http.rs @@ -54,13 +54,13 @@ pub fn read_http_header<'a>( // it is safe to unwrap here because we have checked // the size of the list beforehand sec_websocket_protocol_list - .push(String::try_from(item)?) + .push(String::from(item)) .unwrap(); } } }, "Sec-WebSocket-Key" => { - sec_websocket_key = String::try_from(str::from_utf8(value)?)?; + sec_websocket_key = String::from(str::from_utf8(value)?); }, _ => { // ignore all other headers