From 092ef3d91b07ed0470ccfaba339d45986f65e40a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 26 Dec 2014 14:34:44 +0100 Subject: Add cow_http_hd:parse_if_range/1 From RFC7233. --- src/cow_http_hd.erl | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/cow_http_hd.erl') diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl index 584309c..e9417cf 100644 --- a/src/cow_http_hd.erl +++ b/src/cow_http_hd.erl @@ -34,6 +34,7 @@ -export([parse_if_match/1]). -export([parse_if_modified_since/1]). -export([parse_if_none_match/1]). +-export([parse_if_range/1]). -export([parse_if_unmodified_since/1]). -export([parse_last_modified/1]). -export([parse_max_forwards/1]). @@ -1835,6 +1836,44 @@ horse_parse_if_none_match() -> ). -endif. +%% @doc Parse the If-Range header. + +-spec parse_if_range(binary()) -> etag() | calendar:datetime(). +parse_if_range(<< $W, $/, $", R/bits >>) -> + etag(R, weak, <<>>); +parse_if_range(<< $", R/bits >>) -> + etag(R, strong, <<>>); +parse_if_range(IfRange) -> + cow_date:parse_date(IfRange). + +-ifdef(TEST). +parse_if_range_test_() -> + Tests = [ + {<<"W/\"xyzzy\"">>, {weak, <<"xyzzy">>}}, + {<<"\"xyzzy\"">>, {strong, <<"xyzzy">>}}, + {<<"Sat, 29 Oct 1994 19:43:31 GMT">>, {{1994, 10, 29}, {19, 43, 31}}} + ], + [{V, fun() -> R = parse_if_range(V) end} || {V, R} <- Tests]. + +parse_if_range_error_test_() -> + Tests = [ + <<>> + ], + [{V, fun() -> {'EXIT', _} = (catch parse_if_range(V)) end} || V <- Tests]. +-endif. + +-ifdef(PERF). +horse_parse_if_range_etag() -> + horse:repeat(200000, + parse_if_range(<<"\"xyzzy\"">>) + ). + +horse_parse_if_range_date() -> + horse:repeat(200000, + parse_if_range(<<"Sat, 29 Oct 1994 19:43:31 GMT">>) + ). +-endif. + %% @doc Parse the If-Unmodified-Since header. -spec parse_if_unmodified_since(binary()) -> calendar:datetime(). -- cgit v1.2.3