aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-12-18 12:33:40 +0200
committerLoïc Hoguin <[email protected]>2014-12-18 13:23:18 +0200
commitbfa660285fe7babf7e6f334d6517e4d119bd3d10 (patch)
tree5c162ba9cfafd190dd9458d10136a6e11bdf30c2 /src
parent152db97abd9501df8edf72e1c7b7fed58d7fea34 (diff)
downloadcowlib-bfa660285fe7babf7e6f334d6517e4d119bd3d10.tar.gz
cowlib-bfa660285fe7babf7e6f334d6517e4d119bd3d10.tar.bz2
cowlib-bfa660285fe7babf7e6f334d6517e4d119bd3d10.zip
Add cow_http_hd:parse_sec_websocket_version_client/1
From RFC6455. The code is slower than a plain binary_to_integer(Version), because it enforces that the value is in the 0..255 range as per the specification, and that the input binary is no longer than 3 characters. It is however better to enforce this because the client could also send a huge integer and we don't really want to convert it and waste resources.
Diffstat (limited to 'src')
-rw-r--r--src/cow_http_hd.erl45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl
index b844de4..b1bafaa 100644
--- a/src/cow_http_hd.erl
+++ b/src/cow_http_hd.erl
@@ -31,6 +31,7 @@
-export([parse_if_unmodified_since/1]).
-export([parse_last_modified/1]).
-export([parse_max_forwards/1]).
+-export([parse_sec_websocket_version_client/1]).
-export([parse_transfer_encoding/1]).
-export([parse_upgrade/1]).
@@ -1293,6 +1294,50 @@ parse_max_forwards_error_test_() ->
[{V, fun() -> {'EXIT', _} = (catch parse_content_length(V)) end} || V <- Tests].
-endif.
+%% @doc Parse the Sec-WebSocket-Version request header.
+
+-spec parse_sec_websocket_version_client(binary()) -> 0..255.
+parse_sec_websocket_version_client(SecWebSocketVersion) when byte_size(SecWebSocketVersion) < 4 ->
+ Version = binary_to_integer(SecWebSocketVersion),
+ true = Version >= 0 andalso Version =< 255,
+ Version.
+
+-ifdef(TEST).
+prop_parse_sec_websocket_version_client() ->
+ ?FORALL(Version,
+ int(0, 255),
+ Version =:= parse_sec_websocket_version_client(integer_to_binary(Version))).
+
+parse_sec_websocket_version_client_test_() ->
+ Tests = [
+ {<<"13">>, 13},
+ {<<"25">>, 25}
+ ],
+ [{V, fun() -> R = parse_sec_websocket_version_client(V) end} || {V, R} <- Tests].
+
+parse_sec_websocket_version_client_error_test_() ->
+ Tests = [
+ <<>>,
+ <<" ">>,
+ <<"7, 8, 13">>,
+ <<"invalid">>
+ ],
+ [{V, fun() -> {'EXIT', _} = (catch parse_sec_websocket_version_client(V)) end}
+ || V <- Tests].
+-endif.
+
+-ifdef(PERF).
+horse_parse_sec_websocket_version_client_13() ->
+ horse:repeat(200000,
+ parse_sec_websocket_version_client(<<"13">>)
+ ).
+
+horse_parse_sec_websocket_version_client_255() ->
+ horse:repeat(200000,
+ parse_sec_websocket_version_client(<<"255">>)
+ ).
+-endif.
+
%% @doc Parse the Transfer-Encoding header.
%%
%% @todo This function does not support parsing of transfer-parameter.