From 341f8eae6eb33e2242be09541807cdad9afc732e Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Fri, 28 Aug 2015 09:18:27 +1200 Subject: [PATCH] fix(headers): case insensitive values for Connection header Closes #635 --- src/header/common/connection.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/header/common/connection.rs b/src/header/common/connection.rs index cbc0156ad0..beaeac0c7d 100644 --- a/src/header/common/connection.rs +++ b/src/header/common/connection.rs @@ -4,6 +4,9 @@ use unicase::UniCase; pub use self::ConnectionOption::{KeepAlive, Close, ConnectionHeader}; +const KEEP_ALIVE: UniCase<&'static str> = UniCase("keep-alive"); +const CLOSE: UniCase<&'static str> = UniCase("close"); + /// Values that can be in the `Connection` header. #[derive(Clone, PartialEq, Debug)] pub enum ConnectionOption { @@ -25,10 +28,12 @@ pub enum ConnectionOption { impl FromStr for ConnectionOption { type Err = (); fn from_str(s: &str) -> Result { - match s { - "keep-alive" => Ok(KeepAlive), - "close" => Ok(Close), - s => Ok(ConnectionHeader(UniCase(s.to_owned()))) + if UniCase(s) == KEEP_ALIVE { + Ok(KeepAlive) + } else if UniCase(s) == CLOSE { + Ok(Close) + } else { + Ok(ConnectionHeader(UniCase(s.to_owned()))) } } } @@ -131,6 +136,7 @@ mod tests { fn test_parse() { assert_eq!(Connection::close(),parse_option(b"close".to_vec())); assert_eq!(Connection::keep_alive(),parse_option(b"keep-alive".to_vec())); + assert_eq!(Connection::keep_alive(),parse_option(b"Keep-Alive".to_vec())); assert_eq!(Connection(vec![ConnectionHeader(UniCase("upgrade".to_owned()))]), parse_option(b"upgrade".to_vec())); }