0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Fail early in cookie-related API functions

This commit is contained in:
Tom Burdick 2011-07-18 15:50:29 -05:00 committed by Loïc Hoguin
parent 5bd936db66
commit b75859e075
2 changed files with 5 additions and 5 deletions

View file

@ -43,17 +43,17 @@
-spec parse_cookie(binary()) -> kvlist(). -spec parse_cookie(binary()) -> kvlist().
parse_cookie(<<>>) -> parse_cookie(<<>>) ->
[]; [];
parse_cookie(Cookie) -> parse_cookie(Cookie) when is_binary(Cookie) ->
parse_cookie(Cookie, []). parse_cookie(Cookie, []).
%% @doc Short-hand for <code>cookie(Key, Value, [])</code>. %% @doc Short-hand for <code>cookie(Key, Value, [])</code>.
-spec cookie(binary(), binary()) -> kvlist(). -spec cookie(binary(), binary()) -> kvlist().
cookie(Key, Value) -> cookie(Key, Value) when is_binary(Key) andalso is_binary(Value) ->
cookie(Key, Value, []). cookie(Key, Value, []).
%% @doc Generate a Set-Cookie header field tuple. %% @doc Generate a Set-Cookie header field tuple.
-spec cookie(binary(), binary(), [cookie_option()]) -> kvlist(). -spec cookie(binary(), binary(), [cookie_option()]) -> kvlist().
cookie(Key, Value, Options) -> cookie(Key, Value, Options) when is_binary(Key) andalso is_binary(Value) andalso is_list(Options) ->
Cookie = <<(any_to_binary(Key))/binary, "=", (quote(Value))/binary, "; Version=1">>, Cookie = <<(any_to_binary(Key))/binary, "=", (quote(Value))/binary, "; Version=1">>,
%% Set-Cookie: %% Set-Cookie:
%% Comment, Domain, Max-Age, Path, Secure, Version %% Comment, Domain, Max-Age, Path, Secure, Version

View file

@ -181,14 +181,14 @@ headers(Req) ->
%% @equiv cookie(Name, Req, undefined) %% @equiv cookie(Name, Req, undefined)
-spec cookie(binary(), #http_req{}) -spec cookie(binary(), #http_req{})
-> {binary() | true | undefined, #http_req{}}. -> {binary() | true | undefined, #http_req{}}.
cookie(Name, Req) -> cookie(Name, Req) when is_binary(Name) ->
cookie(Name, Req, undefined). cookie(Name, Req, undefined).
%% @doc Return the cookie value for the given key, or a default if %% @doc Return the cookie value for the given key, or a default if
%% missing. %% missing.
-spec cookie(binary(), #http_req{}, Default) -spec cookie(binary(), #http_req{}, Default)
-> {binary() | true | Default, #http_req{}} when Default::any(). -> {binary() | true | Default, #http_req{}} when Default::any().
cookie(Name, Req=#http_req{cookies=undefined}, Default) -> cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
case header('Cookie', Req) of case header('Cookie', Req) of
{undefined, Req2} -> {undefined, Req2} ->
{Default, Req2#http_req{cookies=[]}}; {Default, Req2#http_req{cookies=[]}};