aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-12-12 19:42:20 +0200
committerLoïc Hoguin <[email protected]>2014-12-12 19:42:20 +0200
commit9044538ed63075aaf44fb683b3da4202f41902c4 (patch)
tree7521503b09f4726b19c9bc3c297cf91f4f43f7d7
parent7d8a571b1e50602d701ca203fbf28036b2cf80f5 (diff)
downloadcowlib-9044538ed63075aaf44fb683b3da4202f41902c4.tar.gz
cowlib-9044538ed63075aaf44fb683b3da4202f41902c4.tar.bz2
cowlib-9044538ed63075aaf44fb683b3da4202f41902c4.zip
Add cow_http_hd:parse_expect/1
From RFC7231.
-rw-r--r--src/cow_http_hd.erl43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl
index 35cf2f4..13bab30 100644
--- a/src/cow_http_hd.erl
+++ b/src/cow_http_hd.erl
@@ -16,6 +16,7 @@
-export([parse_connection/1]).
-export([parse_content_length/1]).
+-export([parse_expect/1]).
-export([parse_transfer_encoding/1]).
-include("cow_inline.hrl").
@@ -100,6 +101,48 @@ horse_parse_content_length_giga() ->
).
-endif.
+%% @doc Parse the Expect header.
+
+-spec parse_expect(binary()) -> continue.
+parse_expect(<<"100-continue", Rest/bits >>) ->
+ ws_end(Rest),
+ continue;
+parse_expect(<<"100-", C, O, N, T, I, M, U, E, Rest/bits >>)
+ when C =:= $C orelse C =:= $c, O =:= $O orelse O =:= $o,
+ N =:= $N orelse N =:= $n, T =:= $T orelse T =:= $t,
+ I =:= $I orelse I =:= $i, M =:= $N orelse M =:= $n,
+ U =:= $U orelse U =:= $u, E =:= $E orelse E =:= $e ->
+ ws_end(Rest),
+ continue.
+
+-ifdef(TEST).
+parse_expect_test_() ->
+ Tests = [
+ <<"100-continue">>,
+ <<"100-CONTINUE">>,
+ <<"100-Continue">>,
+ <<"100-CoNtInUe">>,
+ <<"100-continue ">>
+ ],
+ [{V, fun() -> continue = parse_expect(V) end} || V <- Tests].
+
+parse_expect_error_test_() ->
+ Tests = [
+ <<>>,
+ <<" ">>,
+ <<"200-OK">>,
+ <<"Cookies">>
+ ],
+ [{V, fun() -> {'EXIT', _} = (catch parse_expect(V)) end} || V <- Tests].
+-endif.
+
+-ifdef(PERF).
+horse_parse_expect() ->
+ horse:repeat(200000,
+ parse_expect(<<"100-continue">>)
+ ).
+-endif.
+
%% @doc Parse the Transfer-Encoding header.
%%
%% @todo Extension parameters.