Browse Source

The Websocket subprotocol tokens are case sensitive

As clarified in https://tools.ietf.org/html/rfc7936 the IANA
registry only accepts case insensitive values for clarity's
sake but the actual tokens are case sensitive.
Loïc Hoguin 7 years ago
parent
commit
8b9f9f0866
1 changed files with 9 additions and 10 deletions
  1. 9 10
      src/cow_http_hd.erl

+ 9 - 10
src/cow_http_hd.erl

@@ -2624,12 +2624,13 @@ parse_sec_websocket_key(SecWebSocketKey) ->
 
 
 -spec parse_sec_websocket_protocol_req(binary()) -> [binary()].
 -spec parse_sec_websocket_protocol_req(binary()) -> [binary()].
 parse_sec_websocket_protocol_req(SecWebSocketProtocol) ->
 parse_sec_websocket_protocol_req(SecWebSocketProtocol) ->
-	nonempty(token_ci_list(SecWebSocketProtocol, [])).
+	nonempty(token_list(SecWebSocketProtocol, [])).
 
 
 -ifdef(TEST).
 -ifdef(TEST).
 parse_sec_websocket_protocol_req_test_() ->
 parse_sec_websocket_protocol_req_test_() ->
 	Tests = [
 	Tests = [
-		{<<"chat, superchat">>, [<<"chat">>, <<"superchat">>]}
+		{<<"chat, superchat">>, [<<"chat">>, <<"superchat">>]},
+		{<<"Chat, SuperChat">>, [<<"Chat">>, <<"SuperChat">>]}
 	],
 	],
 	[{V, fun() -> R = parse_sec_websocket_protocol_req(V) end} || {V, R} <- Tests].
 	[{V, fun() -> R = parse_sec_websocket_protocol_req(V) end} || {V, R} <- Tests].
 
 
@@ -2649,23 +2650,21 @@ horse_parse_sec_websocket_protocol_req() ->
 %% @doc Parse the Sec-Websocket-Protocol response header.
 %% @doc Parse the Sec-Websocket-Protocol response header.
 
 
 -spec parse_sec_websocket_protocol_resp(binary()) -> binary().
 -spec parse_sec_websocket_protocol_resp(binary()) -> binary().
-parse_sec_websocket_protocol_resp(<< C, R/bits >>) when ?IS_TOKEN(C) ->
-	?LOWER(token_ci, R, <<>>).
-
-token_ci(<<>>, T) -> T;
-token_ci(<< C, R/bits >>, T) when ?IS_TOKEN(C) ->
-	?LOWER(token_ci, R, T).
+parse_sec_websocket_protocol_resp(Protocol) ->
+	true = <<>> =/= Protocol,
+	ok = validate_token(Protocol),
+	Protocol.
 
 
 -ifdef(TEST).
 -ifdef(TEST).
 prop_parse_sec_websocket_protocol_resp() ->
 prop_parse_sec_websocket_protocol_resp() ->
 	?FORALL(T,
 	?FORALL(T,
 		token(),
 		token(),
-		?LOWER(T) =:= parse_sec_websocket_protocol_resp(T)).
+		T =:= parse_sec_websocket_protocol_resp(T)).
 
 
 parse_sec_websocket_protocol_resp_test_() ->
 parse_sec_websocket_protocol_resp_test_() ->
 	Tests = [
 	Tests = [
 		{<<"chat">>, <<"chat">>},
 		{<<"chat">>, <<"chat">>},
-		{<<"CHAT">>, <<"chat">>}
+		{<<"CHAT">>, <<"CHAT">>}
 	],
 	],
 	[{V, fun() -> R = parse_sec_websocket_protocol_resp(V) end} || {V, R} <- Tests].
 	[{V, fun() -> R = parse_sec_websocket_protocol_resp(V) end} || {V, R} <- Tests].