From a5da247c3efe960096ae0a4bcdd0d50a2fa1cb95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 26 Dec 2014 13:07:50 +0100 Subject: Add cow_http_hd:parse_retry_after/1 From RFC7231. --- src/cow_http_hd.erl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src') diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl index b277292..48a8705 100644 --- a/src/cow_http_hd.erl +++ b/src/cow_http_hd.erl @@ -36,6 +36,7 @@ -export([parse_if_unmodified_since/1]). -export([parse_last_modified/1]). -export([parse_max_forwards/1]). +-export([parse_retry_after/1]). -export([parse_sec_websocket_accept/1]). -export([parse_sec_websocket_extensions/1]). -export([parse_sec_websocket_key/1]). @@ -1853,6 +1854,42 @@ parse_max_forwards_error_test_() -> <<"4.17">> ], [{V, fun() -> {'EXIT', _} = (catch parse_content_length(V)) end} || V <- Tests]. + +%% @doc Parse the Retry-After header. + +-spec parse_retry_after(binary()) -> non_neg_integer() | calendar:datetime(). +parse_retry_after(RetryAfter = << D, _/bits >>) when ?IS_DIGIT(D) -> + I = binary_to_integer(RetryAfter), + true = I >= 0, + I; +parse_retry_after(RetryAfter) -> + cow_date:parse_date(RetryAfter). + +-ifdef(TEST). +parse_retry_after_test_() -> + Tests = [ + {<<"Fri, 31 Dec 1999 23:59:59 GMT">>, {{1999, 12, 31}, {23, 59, 59}}}, + {<<"120">>, 120} + ], + [{V, fun() -> R = parse_retry_after(V) end} || {V, R} <- Tests]. + +parse_retry_after_error_test_() -> + Tests = [ + <<>> + ], + [{V, fun() -> {'EXIT', _} = (catch parse_retry_after(V)) end} || V <- Tests]. +-endif. + +-ifdef(PERF). +horse_parse_retry_after_date() -> + horse:repeat(200000, + parse_retry_after(<<"Fri, 31 Dec 1999 23:59:59 GMT">>) + ). + +horse_parse_retry_after_delay_seconds() -> + horse:repeat(200000, + parse_retry_after(<<"120">>) + ). -endif. %% @doc Dummy parsing function for the Sec-WebSocket-Accept header. -- cgit v1.2.3