aboutsummaryrefslogtreecommitdiffstats
path: root/src/cow_http_hd.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-12-26 11:00:48 +0100
committerLoïc Hoguin <[email protected]>2014-12-26 11:00:48 +0100
commit387e536dc0878cb5c586498bd288f6e0d42039d2 (patch)
treee48906dc7bdc01ecd0d7d26877a0d5a0056d4d3a /src/cow_http_hd.erl
parent6b97792d8990ceb816b5fa9a5f133fa3936df446 (diff)
downloadcowlib-387e536dc0878cb5c586498bd288f6e0d42039d2.tar.gz
cowlib-387e536dc0878cb5c586498bd288f6e0d42039d2.tar.bz2
cowlib-387e536dc0878cb5c586498bd288f6e0d42039d2.zip
Add cow_http_hd:parse_age/1
From RFC7234.
Diffstat (limited to 'src/cow_http_hd.erl')
-rw-r--r--src/cow_http_hd.erl30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl
index 2c51fcc..7c5a805 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_age/1]).
-export([parse_cache_control/1]).
-export([parse_connection/1]).
-export([parse_content_encoding/1]).
@@ -693,6 +694,35 @@ horse_parse_accept_language() ->
).
-endif.
+%% @doc Parse the Age header.
+
+-spec parse_age(binary()) -> non_neg_integer().
+parse_age(Age) ->
+ I = binary_to_integer(Age),
+ true = I >= 0,
+ I.
+
+-ifdef(TEST).
+parse_age_test_() ->
+ Tests = [
+ {<<"0">>, 0},
+ {<<"42">>, 42},
+ {<<"69">>, 69},
+ {<<"1337">>, 1337},
+ {<<"3495">>, 3495},
+ {<<"1234567890">>, 1234567890}
+ ],
+ [{V, fun() -> R = parse_age(V) end} || {V, R} <- Tests].
+
+parse_age_error_test_() ->
+ Tests = [
+ <<>>,
+ <<"123, 123">>,
+ <<"4.17">>
+ ],
+ [{V, fun() -> {'EXIT', _} = (catch parse_age(V)) end} || V <- Tests].
+-endif.
+
%% @doc Parse the Cache-Control header.
%%
%% In the fields list case, we do not support escaping, which shouldn't be needed anyway.