From 9044538ed63075aaf44fb683b3da4202f41902c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 12 Dec 2014 19:42:20 +0200 Subject: Add cow_http_hd:parse_expect/1 From RFC7231. --- src/cow_http_hd.erl | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/cow_http_hd.erl') 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. -- cgit v1.2.3