diff options
author | Loïc Hoguin <[email protected]> | 2014-12-18 01:36:18 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2014-12-18 01:36:18 +0200 |
commit | 152db97abd9501df8edf72e1c7b7fed58d7fea34 (patch) | |
tree | 6fcce7d16afd9db1e20414edf38b8a4cd2d399fa /src | |
parent | 8d24bfa2c056697b6cfdfb83e1ee12065d55ed0e (diff) | |
download | cowlib-152db97abd9501df8edf72e1c7b7fed58d7fea34.tar.gz cowlib-152db97abd9501df8edf72e1c7b7fed58d7fea34.tar.bz2 cowlib-152db97abd9501df8edf72e1c7b7fed58d7fea34.zip |
Add cow_http_hd:parse_expires/1
From RFC7234.
Diffstat (limited to 'src')
-rw-r--r-- | src/cow_http_hd.erl | 38 |
1 files changed, 38 insertions, 0 deletions
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()]. |