aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-12-26 13:46:32 +0100
committerLoïc Hoguin <[email protected]>2014-12-26 13:46:32 +0100
commit70f438de744155d5d1e5c1716ccf640cc1c0c95d (patch)
tree5f1f3fb8ce9d9a66ff3200bce4de42ddad0874b1
parent8a51e3b533d4be1f102c331709fb244776ff40dd (diff)
downloadcowlib-70f438de744155d5d1e5c1716ccf640cc1c0c95d.tar.gz
cowlib-70f438de744155d5d1e5c1716ccf640cc1c0c95d.tar.bz2
cowlib-70f438de744155d5d1e5c1716ccf640cc1c0c95d.zip
Add cow_http_hd:parse_accept_ranges/1
From RFC7233.
-rw-r--r--src/cow_http_hd.erl42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl
index 6d6562e..584309c 100644
--- a/src/cow_http_hd.erl
+++ b/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().