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

Numerous Dialyzer fixes

This commit is contained in:
Loïc Hoguin 2017-01-02 16:47:16 +01:00
parent ed664345d7
commit e10daf39fa
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
11 changed files with 43 additions and 35 deletions

View file

@ -37,14 +37,17 @@
-type http_version() :: 'HTTP/2' | 'HTTP/1.1' | 'HTTP/1.0'. -type http_version() :: 'HTTP/2' | 'HTTP/1.1' | 'HTTP/1.0'.
-export_type([http_version/0]). -export_type([http_version/0]).
-spec start_clear(ranch:ref(), non_neg_integer(), ranch_tcp:opts(), %% @todo We should hide NbAcceptors in a socket variable, even if Ranch
cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}. %% doesn't let us do that yet.
-spec start_clear(ranch:ref(), non_neg_integer(), ranch_tcp:opts(), opts())
-> {ok, pid()} | {error, any()}.
start_clear(Ref, NbAcceptors, TransOpts0, ProtoOpts) start_clear(Ref, NbAcceptors, TransOpts0, ProtoOpts)
when is_integer(NbAcceptors), NbAcceptors > 0 -> when is_integer(NbAcceptors), NbAcceptors > 0 ->
TransOpts = [connection_type(ProtoOpts)|TransOpts0], TransOpts = [connection_type(ProtoOpts)|TransOpts0],
ranch:start_listener(Ref, NbAcceptors, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts). ranch:start_listener(Ref, NbAcceptors, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts).
-spec start_tls(ranch:ref(), non_neg_integer(), ranch_ssl:opts(), opts()) -> {ok, pid()} | {error, any()}. -spec start_tls(ranch:ref(), non_neg_integer(), ranch_ssl:opts(), opts())
-> {ok, pid()} | {error, any()}.
start_tls(Ref, NbAcceptors, TransOpts0, ProtoOpts) start_tls(Ref, NbAcceptors, TransOpts0, ProtoOpts)
when is_integer(NbAcceptors), NbAcceptors > 0 -> when is_integer(NbAcceptors), NbAcceptors > 0 ->
TransOpts = [ TransOpts = [

View file

@ -58,3 +58,4 @@ int(Value) when is_binary(Value) ->
nonempty(<<>>) -> false; nonempty(<<>>) -> false;
nonempty(Value) when is_binary(Value) -> true. nonempty(Value) when is_binary(Value) -> true.
%% @todo Perhaps return true for any other type except empty list?

View file

@ -39,7 +39,7 @@ execute(Req, Env=#{handler := Handler, handler_opts := HandlerOpts}) ->
try Handler:init(Req, HandlerOpts) of try Handler:init(Req, HandlerOpts) of
{ok, Req2, State} -> {ok, Req2, State} ->
Result = terminate(normal, Req2, State, Handler), Result = terminate(normal, Req2, State, Handler),
{ok, Req2, [{result, Result}|Env]}; {ok, Req2, Env#{result => Result}};
{Mod, Req2, State} -> {Mod, Req2, State} ->
Mod:upgrade(Req2, Env, Handler, State, infinity, run); Mod:upgrade(Req2, Env, Handler, State, infinity, run);
{Mod, Req2, State, hibernate} -> {Mod, Req2, State, hibernate} ->
@ -53,7 +53,7 @@ execute(Req, Env=#{handler := Handler, handler_opts := HandlerOpts}) ->
erlang:raise(Class, Reason, erlang:get_stacktrace()) erlang:raise(Class, Reason, erlang:get_stacktrace())
end. end.
-spec terminate(any(), Req, any(), module()) -> ok when Req::cowboy_req:req(). -spec terminate(any(), Req | undefined, any(), module()) -> ok when Req::cowboy_req:req().
terminate(Reason, Req, State, Handler) -> terminate(Reason, Req, State, Handler) ->
case erlang:function_exported(Handler, terminate, 3) of case erlang:function_exported(Handler, terminate, 3) of
true -> true ->

View file

@ -44,7 +44,7 @@
qs = undefined :: binary(), qs = undefined :: binary(),
version = undefined :: cowboy:http_version(), version = undefined :: cowboy:http_version(),
headers = undefined :: map() | undefined, %% @todo better type than map() headers = undefined :: map() | undefined, %% @todo better type than map()
name = undefined :: binary() name = undefined :: binary() | undefined
}). }).
%% @todo We need a state where we wait for the stream process to ask for the body. %% @todo We need a state where we wait for the stream process to ask for the body.
@ -1027,7 +1027,7 @@ terminate(_State, _Reason) ->
%% System callbacks. %% System callbacks.
-spec system_continue(_, _, #state{}) -> ok. -spec system_continue(_, _, {#state{}, binary()}) -> ok.
system_continue(_, _, {State, Buffer}) -> system_continue(_, _, {State, Buffer}) ->
loop(State, Buffer). loop(State, Buffer).

View file

@ -14,7 +14,7 @@
-module(cowboy_middleware). -module(cowboy_middleware).
-type env() :: [{atom(), any()}]. -type env() :: #{atom() => any()}.
-export_type([env/0]). -export_type([env/0]).
-callback execute(Req, Env) -callback execute(Req, Env)

View file

@ -48,6 +48,7 @@
-export([read_body/2]). -export([read_body/2]).
-export([read_urlencoded_body/1]). -export([read_urlencoded_body/1]).
-export([read_urlencoded_body/2]). -export([read_urlencoded_body/2]).
%% @todo read_and_match_urlencoded_body?
%% Multipart. %% Multipart.
-export([read_part/1]). -export([read_part/1]).
@ -63,7 +64,7 @@
-export([has_resp_header/2]). -export([has_resp_header/2]).
%% @todo resp_header %% @todo resp_header
-export([delete_resp_header/2]). -export([delete_resp_header/2]).
-export([set_resp_body/2]). %% @todo Use set_resp_body for iodata() | {sendfile ...} -export([set_resp_body/2]).
%% @todo set_resp_body/3 with a ContentType or even Headers argument, to set content headers. %% @todo set_resp_body/3 with a ContentType or even Headers argument, to set content headers.
-export([has_resp_body/1]). -export([has_resp_body/1]).
-export([reply/2]). -export([reply/2]).
@ -80,15 +81,16 @@
%% Internal. %% Internal.
-export([response_headers/2]). -export([response_headers/2]).
-type cookie_opts() :: cow_cookie:cookie_opts(). %% @todo Get rid of this type, use cow_cookie directly.
-type cookie_opts() :: map().
-export_type([cookie_opts/0]). -export_type([cookie_opts/0]).
-type body_opts() :: #{ -type read_body_opts() :: #{
length => non_neg_integer(), length => non_neg_integer(),
period => non_neg_integer(), period => non_neg_integer(),
timeout => timeout() timeout => timeout()
}. }.
-export_type([body_opts/0]). -export_type([read_body_opts/0]).
%% While sendfile allows a Len of 0 that means "everything past Offset", %% While sendfile allows a Len of 0 that means "everything past Offset",
%% Cowboy expects the real length as it is used as metadata. %% Cowboy expects the real length as it is used as metadata.
@ -97,7 +99,13 @@
| {sendfile, non_neg_integer(), pos_integer(), file:name_all()}. | {sendfile, non_neg_integer(), pos_integer(), file:name_all()}.
-export_type([resp_body/0]). -export_type([resp_body/0]).
-type push_opts() :: map(). %% @todo -type push_opts() :: #{
method => binary(),
scheme => binary(),
host => binary(),
port => binary(),
qs => binary()
}.
-export_type([push_opts/0]). -export_type([push_opts/0]).
-type req() :: map(). %% @todo #{ -type req() :: map(). %% @todo #{
@ -107,7 +115,7 @@
% peer := {inet:ip_address(), inet:port_number()}, % peer := {inet:ip_address(), inet:port_number()},
% %
% method := binary(), %% case sensitive % method := binary(), %% case sensitive
% version := cowboy:http_version(), % version := cowboy:http_version() | atom(),
% scheme := binary(), %% <<"http">> or <<"https">> % scheme := binary(), %% <<"http">> or <<"https">>
% host := binary(), %% lowercase; case insensitive % host := binary(), %% lowercase; case insensitive
% port := inet:port_number(), % port := inet:port_number(),
@ -408,7 +416,7 @@ body_length(#{body_length := Length}) ->
read_body(Req) -> read_body(Req) ->
read_body(Req, #{}). read_body(Req, #{}).
-spec read_body(Req, body_opts()) -> {ok, binary(), Req} | {more, binary(), Req} when Req::req(). -spec read_body(Req, read_body_opts()) -> {ok, binary(), Req} | {more, binary(), Req} when Req::req().
read_body(Req=#{has_body := false}, _) -> read_body(Req=#{has_body := false}, _) ->
{ok, <<>>, Req}; {ok, <<>>, Req};
read_body(Req=#{has_read_body := true}, _) -> read_body(Req=#{has_read_body := true}, _) ->
@ -439,7 +447,7 @@ set_body_length(Req=#{headers := Headers}, BodyLength) ->
read_urlencoded_body(Req) -> read_urlencoded_body(Req) ->
read_urlencoded_body(Req, #{length => 64000, period => 5000}). read_urlencoded_body(Req, #{length => 64000, period => 5000}).
-spec read_urlencoded_body(Req, body_opts()) -> {ok, [{binary(), binary() | true}], Req} when Req::req(). -spec read_urlencoded_body(Req, read_body_opts()) -> {ok, [{binary(), binary() | true}], Req} when Req::req().
read_urlencoded_body(Req0, Opts) -> read_urlencoded_body(Req0, Opts) ->
{ok, Body, Req} = read_body(Req0, Opts), {ok, Body, Req} = read_body(Req0, Opts),
{ok, cow_qs:parse_qs(Body), Req}. {ok, cow_qs:parse_qs(Body), Req}.
@ -452,7 +460,7 @@ read_urlencoded_body(Req0, Opts) ->
read_part(Req) -> read_part(Req) ->
read_part(Req, #{length => 64000, period => 5000}). read_part(Req, #{length => 64000, period => 5000}).
-spec read_part(Req, body_opts()) -spec read_part(Req, read_body_opts())
-> {ok, cow_multipart:headers(), Req} | {done, Req} -> {ok, cow_multipart:headers(), Req} | {done, Req}
when Req::req(). when Req::req().
read_part(Req, Opts) -> read_part(Req, Opts) ->
@ -487,7 +495,7 @@ read_part(Buffer, Opts, Req=#{multipart := {Boundary, _}}) ->
read_part_body(Req) -> read_part_body(Req) ->
read_part_body(Req, #{}). read_part_body(Req, #{}).
-spec read_part_body(Req, body_opts()) -spec read_part_body(Req, read_body_opts())
-> {ok, binary(), Req} | {more, binary(), Req} -> {ok, binary(), Req} | {more, binary(), Req}
when Req::req(). when Req::req().
read_part_body(Req, Opts) -> read_part_body(Req, Opts) ->
@ -662,7 +670,7 @@ push(Path, Headers, Req) ->
%% @todo Optimization: don't send anything at all for HTTP/1.0 and HTTP/1.1. %% @todo Optimization: don't send anything at all for HTTP/1.0 and HTTP/1.1.
%% @todo Path, Headers, Opts, everything should be in proper binary, %% @todo Path, Headers, Opts, everything should be in proper binary,
%% or normalized when creating the Req object. %% or normalized when creating the Req object.
-spec push(binary(), cowboy:http_headers(), req(), push_opts()) -> ok. -spec push(iodata(), cowboy:http_headers(), req(), push_opts()) -> ok.
push(Path, Headers, #{pid := Pid, streamid := StreamID, push(Path, Headers, #{pid := Pid, streamid := StreamID,
scheme := Scheme0, host := Host0, port := Port0}, Opts) -> scheme := Scheme0, host := Host0, port := Port0}, Opts) ->
Method = maps:get(method, Opts, <<"GET">>), Method = maps:get(method, Opts, <<"GET">>),

View file

@ -205,7 +205,7 @@
handler_state :: any(), handler_state :: any(),
%% Allowed methods. Only used for OPTIONS requests. %% Allowed methods. Only used for OPTIONS requests.
allowed_methods :: [binary()], allowed_methods :: [binary()] | undefined,
%% Media type. %% Media type.
content_types_p = [] :: content_types_p = [] ::

View file

@ -285,10 +285,9 @@ last_modified(Req, State={_, {ok, #file_info{mtime=Modified}}, _}) ->
{Modified, Req, State}. {Modified, Req, State}.
%% Stream the file. %% Stream the file.
%% @todo Export cowboy_req:resp_body_fun()?
-spec get_file(Req, State) -spec get_file(Req, State)
-> {{stream, non_neg_integer(), fun()}, Req, State} -> {{sendfile, 0, non_neg_integer(), binary()}, Req, State}
when State::state(). when State::state().
get_file(Req, State={Path, {ok, #file_info{size=Size}}, _}) -> get_file(Req, State={Path, {ok, #file_info{size=Size}}, _}) ->
{{sendfile, 0, Size, Path}, Req, State}. {{sendfile, 0, Size, Path}, Req, State}.

View file

@ -28,10 +28,10 @@
-record(state, { -record(state, {
ref = undefined :: ranch:ref(), ref = undefined :: ranch:ref(),
pid = undefined :: pid(), pid = undefined :: pid(),
read_body_ref = undefined :: reference(), read_body_ref = undefined :: reference() | undefined,
read_body_timer_ref = undefined :: reference(), read_body_timer_ref = undefined :: reference() | undefined,
read_body_length = 0 :: non_neg_integer() | infinity, read_body_length = 0 :: non_neg_integer() | infinity,
read_body_is_fin = nofin :: nofin | fin, read_body_is_fin = nofin :: nofin | {fin, non_neg_integer()},
read_body_buffer = <<>> :: binary() read_body_buffer = <<>> :: binary()
}). }).

View file

@ -18,12 +18,12 @@
-export([start_link/4]). -export([start_link/4]).
-export([proc_lib_hack/5]). -export([proc_lib_hack/5]).
-spec start_link(ranch:ref(), inet:socket(), module(), cowboy:opts()) -> {ok, pid()}. -spec start_link(ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> {ok, pid()}.
start_link(Ref, Socket, Transport, Opts) -> start_link(Ref, Socket, Transport, Opts) ->
Pid = proc_lib:spawn_link(?MODULE, proc_lib_hack, [self(), Ref, Socket, Transport, Opts]), Pid = proc_lib:spawn_link(?MODULE, proc_lib_hack, [self(), Ref, Socket, Transport, Opts]),
{ok, Pid}. {ok, Pid}.
-spec proc_lib_hack(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts()) -> ok. -spec proc_lib_hack(pid(), ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> ok.
proc_lib_hack(Parent, Ref, Socket, Transport, Opts) -> proc_lib_hack(Parent, Ref, Socket, Transport, Opts) ->
try try
init(Parent, Ref, Socket, Transport, Opts) init(Parent, Ref, Socket, Transport, Opts)
@ -34,7 +34,7 @@ proc_lib_hack(Parent, Ref, Socket, Transport, Opts) ->
_:Reason -> exit({Reason, erlang:get_stacktrace()}) _:Reason -> exit({Reason, erlang:get_stacktrace()})
end. end.
-spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts()) -> ok. -spec init(pid(), ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> ok.
init(Parent, Ref, Socket, Transport, Opts) -> init(Parent, Ref, Socket, Transport, Opts) ->
ok = ranch:accept_ack(Ref), ok = ranch:accept_ack(Ref),
case ssl:negotiated_protocol(Socket) of case ssl:negotiated_protocol(Socket) of

View file

@ -54,7 +54,7 @@
-optional_callbacks([terminate/3]). -optional_callbacks([terminate/3]).
-record(state, { -record(state, {
socket = undefined :: inet:socket(), socket = undefined :: inet:socket() | undefined,
transport = undefined :: module(), transport = undefined :: module(),
handler :: module(), handler :: module(),
key = undefined :: undefined | binary(), key = undefined :: undefined | binary(),
@ -309,8 +309,7 @@ websocket_dispatch(State=#state{socket=Socket, transport=Transport, frag_state=F
handler_call(State, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/3) handler_call(State, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/3)
end. end.
-spec handler_call(#state{}, any(), binary(), atom(), any(), fun()) -spec handler_call(#state{}, any(), binary(), atom(), any(), fun()) -> no_return().
-> {ok, cowboy_middleware:env()}.
handler_call(State=#state{handler=Handler}, HandlerState, handler_call(State=#state{handler=Handler}, HandlerState,
RemainingData, Callback, Message, NextState) -> RemainingData, Callback, Message, NextState) ->
try case Callback of try case Callback of
@ -375,8 +374,7 @@ is_close_frame({close, _}) -> true;
is_close_frame({close, _, _}) -> true; is_close_frame({close, _, _}) -> true;
is_close_frame(_) -> false. is_close_frame(_) -> false.
-spec websocket_close(#state{}, any(), terminate_reason()) -spec websocket_close(#state{}, any(), terminate_reason()) -> no_return().
-> {ok, cowboy_middleware:env()}.
websocket_close(State=#state{socket=Socket, transport=Transport, extensions=Extensions}, websocket_close(State=#state{socket=Socket, transport=Transport, extensions=Extensions},
HandlerState, Reason) -> HandlerState, Reason) ->
case Reason of case Reason of
@ -395,8 +393,7 @@ websocket_close(State=#state{socket=Socket, transport=Transport, extensions=Exte
end, end,
handler_terminate(State, HandlerState, Reason). handler_terminate(State, HandlerState, Reason).
-spec handler_terminate(#state{}, any(), terminate_reason()) -spec handler_terminate(#state{}, any(), terminate_reason()) -> no_return().
-> {ok, cowboy_middleware:env()}.
handler_terminate(#state{handler=Handler}, handler_terminate(#state{handler=Handler},
HandlerState, Reason) -> HandlerState, Reason) ->
cowboy_handler:terminate(Reason, undefined, HandlerState, Handler), cowboy_handler:terminate(Reason, undefined, HandlerState, Handler),