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

Fail fast when a wrong type is given to API functions

Idea given by bfrog, fixes issue #34.
This commit is contained in:
Loïc Hoguin 2011-07-18 14:21:45 +02:00
parent 87366e36ad
commit d363f91410
2 changed files with 12 additions and 8 deletions

View file

@ -38,7 +38,9 @@
%% <em>Ref</em> can be used to stop the listener later on. %% <em>Ref</em> can be used to stop the listener later on.
-spec start_listener(any(), non_neg_integer(), module(), any(), module(), any()) -spec start_listener(any(), non_neg_integer(), module(), any(), module(), any())
-> {ok, pid()}. -> {ok, pid()}.
start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) -> start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
when is_integer(NbAcceptors) andalso is_atom(Transport)
andalso is_atom(Protocol) ->
supervisor:start_child(cowboy_sup, supervisor:start_child(cowboy_sup,
{{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [ {{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [
NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts

View file

@ -108,14 +108,15 @@ raw_path(Req) ->
%% @equiv qs_val(Name, Req, undefined) %% @equiv qs_val(Name, Req, undefined)
-spec qs_val(binary(), #http_req{}) -spec qs_val(binary(), #http_req{})
-> {binary() | true | undefined, #http_req{}}. -> {binary() | true | undefined, #http_req{}}.
qs_val(Name, Req) -> qs_val(Name, Req) when is_binary(Name) ->
qs_val(Name, Req, undefined). qs_val(Name, Req, undefined).
%% @doc Return the query string value for the given key, or a default if %% @doc Return the query string value for the given key, or a default if
%% missing. %% missing.
-spec qs_val(binary(), #http_req{}, Default) -spec qs_val(binary(), #http_req{}, Default)
-> {binary() | true | Default, #http_req{}} when Default::any(). -> {binary() | true | Default, #http_req{}} when Default::any().
qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}, Default) -> qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}, Default)
when is_binary(Name) ->
QsVals = parse_qs(RawQs), QsVals = parse_qs(RawQs),
qs_val(Name, Req#http_req{qs_vals=QsVals}, Default); qs_val(Name, Req#http_req{qs_vals=QsVals}, Default);
qs_val(Name, Req, Default) -> qs_val(Name, Req, Default) ->
@ -139,14 +140,14 @@ raw_qs(Req) ->
%% @equiv binding(Name, Req, undefined) %% @equiv binding(Name, Req, undefined)
-spec binding(atom(), #http_req{}) -> {binary() | undefined, #http_req{}}. -spec binding(atom(), #http_req{}) -> {binary() | undefined, #http_req{}}.
binding(Name, Req) -> binding(Name, Req) when is_atom(Name) ->
binding(Name, Req, undefined). binding(Name, Req, undefined).
%% @doc Return the binding value for the given key obtained when matching %% @doc Return the binding value for the given key obtained when matching
%% the host and path against the dispatch list, or a default if missing. %% the host and path against the dispatch list, or a default if missing.
-spec binding(atom(), #http_req{}, Default) -spec binding(atom(), #http_req{}, Default)
-> {binary() | Default, #http_req{}} when Default::any(). -> {binary() | Default, #http_req{}} when Default::any().
binding(Name, Req, Default) -> binding(Name, Req, Default) when is_atom(Name) ->
case lists:keyfind(Name, 1, Req#http_req.bindings) of case lists:keyfind(Name, 1, Req#http_req.bindings) of
{Name, Value} -> {Value, Req}; {Name, Value} -> {Value, Req};
false -> {Default, Req} false -> {Default, Req}
@ -160,13 +161,13 @@ bindings(Req) ->
%% @equiv header(Name, Req, undefined) %% @equiv header(Name, Req, undefined)
-spec header(atom() | binary(), #http_req{}) -spec header(atom() | binary(), #http_req{})
-> {binary() | undefined, #http_req{}}. -> {binary() | undefined, #http_req{}}.
header(Name, Req) -> header(Name, Req) when is_atom(Name) orelse is_binary(Name) ->
header(Name, Req, undefined). header(Name, Req, undefined).
%% @doc Return the header value for the given key, or a default if missing. %% @doc Return the header value for the given key, or a default if missing.
-spec header(atom() | binary(), #http_req{}, Default) -spec header(atom() | binary(), #http_req{}, Default)
-> {binary() | Default, #http_req{}} when Default::any(). -> {binary() | Default, #http_req{}} when Default::any().
header(Name, Req, Default) -> header(Name, Req, Default) when is_atom(Name) orelse is_binary(Name) ->
case lists:keyfind(Name, 1, Req#http_req.headers) of case lists:keyfind(Name, 1, Req#http_req.headers) of
{Name, Value} -> {Value, Req}; {Name, Value} -> {Value, Req};
false -> {Default, Req} false -> {Default, Req}
@ -204,7 +205,8 @@ body(Length, Req=#http_req{body_state=waiting, buffer=Buffer})
when Length =:= byte_size(Buffer) -> when Length =:= byte_size(Buffer) ->
{ok, Buffer, Req#http_req{body_state=done, buffer= <<>>}}; {ok, Buffer, Req#http_req{body_state=done, buffer= <<>>}};
body(Length, Req=#http_req{socket=Socket, transport=Transport, body(Length, Req=#http_req{socket=Socket, transport=Transport,
body_state=waiting, buffer=Buffer}) when Length > byte_size(Buffer) -> body_state=waiting, buffer=Buffer})
when is_integer(Length) andalso Length > byte_size(Buffer) ->
case Transport:recv(Socket, Length - byte_size(Buffer), 5000) of case Transport:recv(Socket, Length - byte_size(Buffer), 5000) of
{ok, Body} -> {ok, << Buffer/binary, Body/binary >>, {ok, Body} -> {ok, << Buffer/binary, Body/binary >>,
Req#http_req{body_state=done, buffer= <<>>}}; Req#http_req{body_state=done, buffer= <<>>}};