From 152db97abd9501df8edf72e1c7b7fed58d7fea34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 18 Dec 2014 01:36:18 +0200 Subject: Add cow_http_hd:parse_expires/1 From RFC7234. --- src/cow_http_hd.erl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/cow_http_hd.erl') diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl index f761b6a..b844de4 100644 --- a/src/cow_http_hd.erl +++ b/src/cow_http_hd.erl @@ -24,6 +24,7 @@ -export([parse_date/1]). -export([parse_etag/1]). -export([parse_expect/1]). +-export([parse_expires/1]). -export([parse_if_match/1]). -export([parse_if_modified_since/1]). -export([parse_if_none_match/1]). @@ -1079,6 +1080,43 @@ horse_parse_expect() -> ). -endif. +%% @doc Parse the Expires header. +%% +%% Recipients must interpret invalid date formats as a date +%% in the past. The value "0" is commonly used. + +-spec parse_expires(binary()) -> calendar:datetime(). +parse_expires(<<"0">>) -> + {{1, 1, 1}, {0, 0, 0}}; +parse_expires(Expires) -> + try + cow_date:parse_date(Expires) + catch _:_ -> + {{1, 1, 1}, {0, 0, 0}} + end. + +-ifdef(TEST). +parse_expires_test_() -> + Tests = [ + {<<"0">>, {{1, 1, 1}, {0, 0, 0}}}, + {<<"Thu, 01 Dec 1994 nope invalid">>, {{1, 1, 1}, {0, 0, 0}}}, + {<<"Thu, 01 Dec 1994 16:00:00 GMT">>, {{1994, 12, 1}, {16, 0, 0}}} + ], + [{V, fun() -> R = parse_expires(V) end} || {V, R} <- Tests]. +-endif. + +-ifdef(PERF). +horse_parse_expires_0() -> + horse:repeat(200000, + parse_expires(<<"0">>) + ). + +horse_parse_expires_invalid() -> + horse:repeat(200000, + parse_expires(<<"Thu, 01 Dec 1994 nope invalid">>) + ). +-endif. + %% @doc Parse the If-Match header. -spec parse_if_match(binary()) -> '*' | [etag()]. -- cgit v1.2.3