Browse Source

Add cow_http_hd:parse_accept_ranges/1

From RFC7233.
Loïc Hoguin 10 years ago
parent
commit
70f438de74
1 changed files with 42 additions and 0 deletions
  1. 42 0
      src/cow_http_hd.erl

+ 42 - 0
src/cow_http_hd.erl

@@ -18,6 +18,7 @@
 -export([parse_accept_charset/1]).
 -export([parse_accept_encoding/1]).
 -export([parse_accept_language/1]).
+-export([parse_accept_ranges/1]).
 -export([parse_age/1]).
 -export([parse_allow/1]).
 -export([parse_cache_control/1]).
@@ -696,6 +697,47 @@ horse_parse_accept_language() ->
 	).
 -endif.
 
+%% @doc Parse the Accept-Ranges header.
+
+-spec parse_accept_ranges(binary()) -> [binary()].
+parse_accept_ranges(<<"none">>) -> [];
+parse_accept_ranges(<<"bytes">>) -> [<<"bytes">>];
+parse_accept_ranges(AcceptRanges) ->
+	nonempty(token_ci_list(AcceptRanges, [])).
+
+-ifdef(TEST).
+parse_accept_ranges_test_() ->
+	Tests = [
+		{<<"bytes">>, [<<"bytes">>]},
+		{<<"none">>, []},
+		{<<"bytes, pages, kilos">>, [<<"bytes">>, <<"pages">>, <<"kilos">>]}
+	],
+	[{V, fun() -> R = parse_accept_ranges(V) end} || {V, R} <- Tests].
+
+parse_accept_ranges_error_test_() ->
+	Tests = [
+		<<>>
+	],
+	[{V, fun() -> {'EXIT', _} = (catch parse_accept_ranges(V)) end} || V <- Tests].
+-endif.
+
+-ifdef(PERF).
+horse_parse_accept_ranges_none() ->
+	horse:repeat(200000,
+		parse_accept_ranges(<<"none">>)
+	).
+
+horse_parse_accept_ranges_bytes() ->
+	horse:repeat(200000,
+		parse_accept_ranges(<<"bytes">>)
+	).
+
+horse_parse_accept_ranges_other() ->
+	horse:repeat(200000,
+		parse_accept_ranges(<<"bytes, pages, kilos">>)
+	).
+-endif.
+
 %% @doc Parse the Age header.
 
 -spec parse_age(binary()) -> non_neg_integer().