From 387e536dc0878cb5c586498bd288f6e0d42039d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 26 Dec 2014 11:00:48 +0100 Subject: Add cow_http_hd:parse_age/1 From RFC7234. --- src/cow_http_hd.erl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/cow_http_hd.erl') 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. -- cgit v1.2.3