0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 13:10:24 +00:00

Apply a trick to the erlang:hibernate calls to suppress dialyzer warnings

Initially recommended by Magnus Klaar, the trick is to add a catch
instruction before the erlang:hibernate/3 call so that Dialyzer
thinks it will return, followed by the expected return value
('ok' for HTTP, 'closed' for websockets).

This should be good enough until a real solution is found.
This commit is contained in:
Loïc Hoguin 2012-02-02 20:04:06 +01:00
parent 096f40bf08
commit 062db95653
2 changed files with 33 additions and 32 deletions

View file

@ -71,7 +71,7 @@ start_link(ListenerPid, Socket, Transport, Opts) ->
%% FSM. %% FSM.
%% @private %% @private
-spec init(pid(), inet:socket(), module(), any()) -> ok | none(). -spec init(pid(), inet:socket(), module(), any()) -> ok.
init(ListenerPid, Socket, Transport, Opts) -> init(ListenerPid, Socket, Transport, Opts) ->
Dispatch = proplists:get_value(dispatch, Opts, []), Dispatch = proplists:get_value(dispatch, Opts, []),
MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5), MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5),
@ -87,7 +87,7 @@ init(ListenerPid, Socket, Transport, Opts) ->
timeout=Timeout, urldecode=URLDec}). timeout=Timeout, urldecode=URLDec}).
%% @private %% @private
-spec parse_request(#state{}) -> ok | none(). -spec parse_request(#state{}) -> ok.
%% We limit the length of the Request-line to MaxLength to avoid endlessly %% We limit the length of the Request-line to MaxLength to avoid endlessly
%% reading from the socket and eventually crashing. %% reading from the socket and eventually crashing.
parse_request(State=#state{buffer=Buffer, max_line_length=MaxLength}) -> parse_request(State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
@ -99,7 +99,7 @@ parse_request(State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
{error, _Reason} -> error_terminate(400, State) {error, _Reason} -> error_terminate(400, State)
end. end.
-spec wait_request(#state{}) -> ok | none(). -spec wait_request(#state{}) -> ok.
wait_request(State=#state{socket=Socket, transport=Transport, wait_request(State=#state{socket=Socket, transport=Transport,
timeout=T, buffer=Buffer}) -> timeout=T, buffer=Buffer}) ->
case Transport:recv(Socket, 0, T) of case Transport:recv(Socket, 0, T) of
@ -109,7 +109,7 @@ wait_request(State=#state{socket=Socket, transport=Transport,
end. end.
-spec request({http_request, cowboy_http:method(), cowboy_http:uri(), -spec request({http_request, cowboy_http:method(), cowboy_http:uri(),
cowboy_http:version()}, #state{}) -> ok | none(). cowboy_http:version()}, #state{}) -> ok.
request({http_request, _Method, _URI, Version}, State) request({http_request, _Method, _URI, Version}, State)
when Version =/= {1, 0}, Version =/= {1, 1} -> when Version =/= {1, 0}, Version =/= {1, 1} ->
error_terminate(505, State); error_terminate(505, State);
@ -138,7 +138,7 @@ request({http_error, <<"\r\n">>}, State=#state{req_empty_lines=N}) ->
request(_Any, State) -> request(_Any, State) ->
error_terminate(400, State). error_terminate(400, State).
-spec parse_header(#http_req{}, #state{}) -> ok | none(). -spec parse_header(#http_req{}, #state{}) -> ok.
parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) -> parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
case erlang:decode_packet(httph_bin, Buffer, []) of case erlang:decode_packet(httph_bin, Buffer, []) of
{ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest}); {ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest});
@ -148,7 +148,7 @@ parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
{error, _Reason} -> error_terminate(400, State) {error, _Reason} -> error_terminate(400, State)
end. end.
-spec wait_header(#http_req{}, #state{}) -> ok | none(). -spec wait_header(#http_req{}, #state{}) -> ok.
wait_header(Req, State=#state{socket=Socket, wait_header(Req, State=#state{socket=Socket,
transport=Transport, timeout=T, buffer=Buffer}) -> transport=Transport, timeout=T, buffer=Buffer}) ->
case Transport:recv(Socket, 0, T) of case Transport:recv(Socket, 0, T) of
@ -159,7 +159,7 @@ wait_header(Req, State=#state{socket=Socket,
end. end.
-spec header({http_header, integer(), cowboy_http:header(), any(), binary()} -spec header({http_header, integer(), cowboy_http:header(), any(), binary()}
| http_eoh, #http_req{}, #state{}) -> ok | none(). | http_eoh, #http_req{}, #state{}) -> ok.
header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{ header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{
transport=Transport, host=undefined}, State) -> transport=Transport, host=undefined}, State) ->
RawHost2 = cowboy_bstr:to_lower(RawHost), RawHost2 = cowboy_bstr:to_lower(RawHost),
@ -205,7 +205,7 @@ header(_Any, _Req, State) ->
error_terminate(400, State). error_terminate(400, State).
-spec dispatch(fun((#http_req{}, #state{}) -> ok), -spec dispatch(fun((#http_req{}, #state{}) -> ok),
#http_req{}, #state{}) -> ok | none(). #http_req{}, #state{}) -> ok.
dispatch(Next, Req=#http_req{host=Host, path=Path}, dispatch(Next, Req=#http_req{host=Host, path=Path},
State=#state{dispatch=Dispatch}) -> State=#state{dispatch=Dispatch}) ->
%% @todo We should allow a configurable chain of handlers here to %% @todo We should allow a configurable chain of handlers here to
@ -222,7 +222,7 @@ dispatch(Next, Req=#http_req{host=Host, path=Path},
error_terminate(404, State) error_terminate(404, State)
end. end.
-spec handler_init(#http_req{}, #state{}) -> ok | none(). -spec handler_init(#http_req{}, #state{}) -> ok.
handler_init(Req, State=#state{transport=Transport, handler_init(Req, State=#state{transport=Transport,
handler={Handler, Opts}}) -> handler={Handler, Opts}}) ->
try Handler:init({Transport:name(), http}, Req, Opts) of try Handler:init({Transport:name(), http}, Req, Opts) of
@ -254,7 +254,7 @@ handler_init(Req, State=#state{transport=Transport,
[Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()]) [Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()])
end. end.
-spec upgrade_protocol(#http_req{}, #state{}, atom()) -> ok | none(). -spec upgrade_protocol(#http_req{}, #state{}, atom()) -> ok.
upgrade_protocol(Req, State=#state{listener=ListenerPid, upgrade_protocol(Req, State=#state{listener=ListenerPid,
handler={Handler, Opts}}, Module) -> handler={Handler, Opts}}, Module) ->
case Module:upgrade(ListenerPid, Handler, Opts, Req) of case Module:upgrade(ListenerPid, Handler, Opts, Req) of
@ -262,7 +262,7 @@ upgrade_protocol(Req, State=#state{listener=ListenerPid,
_Any -> terminate(State) _Any -> terminate(State)
end. end.
-spec handler_handle(any(), #http_req{}, #state{}) -> ok | none(). -spec handler_handle(any(), #http_req{}, #state{}) -> ok.
handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) -> handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
try Handler:handle(Req, HandlerState) of try Handler:handle(Req, HandlerState) of
{ok, Req2, HandlerState2} -> {ok, Req2, HandlerState2} ->
@ -281,11 +281,12 @@ handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
%% We don't listen for Transport closes because that would force us %% We don't listen for Transport closes because that would force us
%% to receive data and buffer it indefinitely. %% to receive data and buffer it indefinitely.
-spec handler_before_loop(any(), #http_req{}, #state{}) -> ok | none(). -spec handler_before_loop(any(), #http_req{}, #state{}) -> ok.
handler_before_loop(HandlerState, Req, State=#state{hibernate=true}) -> handler_before_loop(HandlerState, Req, State=#state{hibernate=true}) ->
State2 = handler_loop_timeout(State), State2 = handler_loop_timeout(State),
erlang:hibernate(?MODULE, handler_loop, catch erlang:hibernate(?MODULE, handler_loop,
[HandlerState, Req, State2#state{hibernate=false}]); [HandlerState, Req, State2#state{hibernate=false}]),
ok;
handler_before_loop(HandlerState, Req, State) -> handler_before_loop(HandlerState, Req, State) ->
State2 = handler_loop_timeout(State), State2 = handler_loop_timeout(State),
handler_loop(HandlerState, Req, State2). handler_loop(HandlerState, Req, State2).
@ -302,7 +303,7 @@ handler_loop_timeout(State=#state{loop_timeout=Timeout,
erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}), erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}),
State#state{loop_timeout_ref=TRef}. State#state{loop_timeout_ref=TRef}.
-spec handler_loop(any(), #http_req{}, #state{}) -> ok | none(). -spec handler_loop(any(), #http_req{}, #state{}) -> ok.
handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) -> handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) ->
receive receive
{?MODULE, timeout, TRef} -> {?MODULE, timeout, TRef} ->
@ -313,7 +314,7 @@ handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) ->
handler_call(HandlerState, Req, State, Message) handler_call(HandlerState, Req, State, Message)
end. end.
-spec handler_call(any(), #http_req{}, #state{}, any()) -> ok | none(). -spec handler_call(any(), #http_req{}, #state{}, any()) -> ok.
handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}}, handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}},
Message) -> Message) ->
try Handler:info(Message, Req, HandlerState) of try Handler:info(Message, Req, HandlerState) of
@ -350,12 +351,12 @@ handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
HandlerState, Req, erlang:get_stacktrace()]) HandlerState, Req, erlang:get_stacktrace()])
end. end.
-spec terminate_request(any(), #http_req{}, #state{}) -> ok | none(). -spec terminate_request(any(), #http_req{}, #state{}) -> ok.
terminate_request(HandlerState, Req, State) -> terminate_request(HandlerState, Req, State) ->
HandlerRes = handler_terminate(HandlerState, Req, State), HandlerRes = handler_terminate(HandlerState, Req, State),
next_request(Req, State, HandlerRes). next_request(Req, State, HandlerRes).
-spec next_request(#http_req{}, #state{}, any()) -> ok | none(). -spec next_request(#http_req{}, #state{}, any()) -> ok.
next_request(Req=#http_req{connection=Conn}, next_request(Req=#http_req{connection=Conn},
State=#state{req_keepalive=Keepalive, max_keepalive=MaxKeepalive}, State=#state{req_keepalive=Keepalive, max_keepalive=MaxKeepalive},
HandlerRes) -> HandlerRes) ->

View file

@ -64,7 +64,7 @@
%% You do not need to call this function manually. To upgrade to the WebSocket %% You do not need to call this function manually. To upgrade to the WebSocket
%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em> %% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em>
%% in your <em>cowboy_http_handler:init/3</em> handler function. %% in your <em>cowboy_http_handler:init/3</em> handler function.
-spec upgrade(pid(), module(), any(), #http_req{}) -> closed | none(). -spec upgrade(pid(), module(), any(), #http_req{}) -> closed.
upgrade(ListenerPid, Handler, Opts, Req) -> upgrade(ListenerPid, Handler, Opts, Req) ->
cowboy_listener:move_connection(ListenerPid, websocket, self()), cowboy_listener:move_connection(ListenerPid, websocket, self()),
case catch websocket_upgrade(#state{handler=Handler, opts=Opts}, Req) of case catch websocket_upgrade(#state{handler=Handler, opts=Opts}, Req) of
@ -111,7 +111,7 @@ websocket_upgrade(Version, State, Req=#http_req{meta=Meta})
{ok, State#state{version=IntVersion, challenge=Challenge}, {ok, State#state{version=IntVersion, challenge=Challenge},
Req2#http_req{meta=[{websocket_version, IntVersion}|Meta]}}. Req2#http_req{meta=[{websocket_version, IntVersion}|Meta]}}.
-spec handler_init(#state{}, #http_req{}) -> closed | none(). -spec handler_init(#state{}, #http_req{}) -> closed.
handler_init(State=#state{handler=Handler, opts=Opts}, handler_init(State=#state{handler=Handler, opts=Opts},
Req=#http_req{transport=Transport}) -> Req=#http_req{transport=Transport}) ->
try Handler:websocket_init(Transport:name(), Req, Opts) of try Handler:websocket_init(Transport:name(), Req, Opts) of
@ -157,7 +157,7 @@ upgrade_denied(#http_req{socket=Socket, transport=Transport,
Transport:send(Socket, <<"0\r\n\r\n">>), Transport:send(Socket, <<"0\r\n\r\n">>),
closed. closed.
-spec websocket_handshake(#state{}, #http_req{}, any()) -> closed | none(). -spec websocket_handshake(#state{}, #http_req{}, any()) -> closed.
websocket_handshake(State=#state{version=0, origin=Origin, websocket_handshake(State=#state{version=0, origin=Origin,
challenge={Key1, Key2}}, Req=#http_req{socket=Socket, challenge={Key1, Key2}}, Req=#http_req{socket=Socket,
transport=Transport, raw_host=Host, port=Port, transport=Transport, raw_host=Host, port=Port,
@ -195,14 +195,15 @@ websocket_handshake(State=#state{challenge=Challenge},
handler_before_loop(State#state{messages=Transport:messages()}, handler_before_loop(State#state{messages=Transport:messages()},
Req2, HandlerState, <<>>). Req2, HandlerState, <<>>).
-spec handler_before_loop(#state{}, #http_req{}, any(), binary()) -> closed | none(). -spec handler_before_loop(#state{}, #http_req{}, any(), binary()) -> closed.
handler_before_loop(State=#state{hibernate=true}, handler_before_loop(State=#state{hibernate=true},
Req=#http_req{socket=Socket, transport=Transport}, Req=#http_req{socket=Socket, transport=Transport},
HandlerState, SoFar) -> HandlerState, SoFar) ->
Transport:setopts(Socket, [{active, once}]), Transport:setopts(Socket, [{active, once}]),
State2 = handler_loop_timeout(State), State2 = handler_loop_timeout(State),
erlang:hibernate(?MODULE, handler_loop, [State2#state{hibernate=false}, catch erlang:hibernate(?MODULE, handler_loop,
Req, HandlerState, SoFar]); [State2#state{hibernate=false}, Req, HandlerState, SoFar]),
closed;
handler_before_loop(State, Req=#http_req{socket=Socket, transport=Transport}, handler_before_loop(State, Req=#http_req{socket=Socket, transport=Transport},
HandlerState, SoFar) -> HandlerState, SoFar) ->
Transport:setopts(Socket, [{active, once}]), Transport:setopts(Socket, [{active, once}]),
@ -220,7 +221,7 @@ handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) ->
State#state{timeout_ref=TRef}. State#state{timeout_ref=TRef}.
%% @private %% @private
-spec handler_loop(#state{}, #http_req{}, any(), binary()) -> closed | none(). -spec handler_loop(#state{}, #http_req{}, any(), binary()) -> closed.
handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef}, handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef},
Req=#http_req{socket=Socket}, HandlerState, SoFar) -> Req=#http_req{socket=Socket}, HandlerState, SoFar) ->
receive receive
@ -240,7 +241,7 @@ handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef},
SoFar, websocket_info, Message, fun handler_before_loop/4) SoFar, websocket_info, Message, fun handler_before_loop/4)
end. end.
-spec websocket_data(#state{}, #http_req{}, any(), binary()) -> closed | none(). -spec websocket_data(#state{}, #http_req{}, any(), binary()) -> closed.
%% No more data. %% No more data.
websocket_data(State, Req, HandlerState, <<>>) -> websocket_data(State, Req, HandlerState, <<>>) ->
handler_before_loop(State, Req, HandlerState, <<>>); handler_before_loop(State, Req, HandlerState, <<>>);
@ -292,8 +293,7 @@ websocket_data(State, Req, HandlerState, _Bad) ->
%% hybi routing depending on whether unmasking is needed. %% hybi routing depending on whether unmasking is needed.
-spec websocket_before_unmask(#state{}, #http_req{}, any(), binary(), -spec websocket_before_unmask(#state{}, #http_req{}, any(), binary(),
binary(), opcode(), 0 | 1, non_neg_integer() | undefined) binary(), opcode(), 0 | 1, non_neg_integer() | undefined) -> closed.
-> closed | none().
websocket_before_unmask(State, Req, HandlerState, Data, websocket_before_unmask(State, Req, HandlerState, Data,
Rest, Opcode, Mask, PayloadLen) -> Rest, Opcode, Mask, PayloadLen) ->
case {Mask, PayloadLen} of case {Mask, PayloadLen} of
@ -310,14 +310,14 @@ websocket_before_unmask(State, Req, HandlerState, Data,
%% hybi unmasking. %% hybi unmasking.
-spec websocket_unmask(#state{}, #http_req{}, any(), binary(), -spec websocket_unmask(#state{}, #http_req{}, any(), binary(),
opcode(), binary(), mask_key()) -> closed | none(). opcode(), binary(), mask_key()) -> closed.
websocket_unmask(State, Req, HandlerState, RemainingData, websocket_unmask(State, Req, HandlerState, RemainingData,
Opcode, Payload, MaskKey) -> Opcode, Payload, MaskKey) ->
websocket_unmask(State, Req, HandlerState, RemainingData, websocket_unmask(State, Req, HandlerState, RemainingData,
Opcode, Payload, MaskKey, <<>>). Opcode, Payload, MaskKey, <<>>).
-spec websocket_unmask(#state{}, #http_req{}, any(), binary(), -spec websocket_unmask(#state{}, #http_req{}, any(), binary(),
opcode(), binary(), mask_key(), binary()) -> closed | none(). opcode(), binary(), mask_key(), binary()) -> closed.
websocket_unmask(State, Req, HandlerState, RemainingData, websocket_unmask(State, Req, HandlerState, RemainingData,
Opcode, << O:32, Rest/bits >>, MaskKey, Acc) -> Opcode, << O:32, Rest/bits >>, MaskKey, Acc) ->
T = O bxor MaskKey, T = O bxor MaskKey,
@ -348,7 +348,7 @@ websocket_unmask(State, Req, HandlerState, RemainingData,
%% hybi dispatching. %% hybi dispatching.
-spec websocket_dispatch(#state{}, #http_req{}, any(), binary(), -spec websocket_dispatch(#state{}, #http_req{}, any(), binary(),
opcode(), binary()) -> closed | none(). opcode(), binary()) -> closed.
%% @todo Fragmentation. %% @todo Fragmentation.
%~ websocket_dispatch(State, Req, HandlerState, RemainingData, 0, Payload) -> %~ websocket_dispatch(State, Req, HandlerState, RemainingData, 0, Payload) ->
%% Text frame. %% Text frame.
@ -376,7 +376,7 @@ websocket_dispatch(State, Req, HandlerState, RemainingData, 10, Payload) ->
websocket_handle, {pong, Payload}, fun websocket_data/4). websocket_handle, {pong, Payload}, fun websocket_data/4).
-spec handler_call(#state{}, #http_req{}, any(), binary(), -spec handler_call(#state{}, #http_req{}, any(), binary(),
atom(), any(), fun()) -> closed | none(). atom(), any(), fun()) -> closed.
handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState, handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState,
RemainingData, Callback, Message, NextState) -> RemainingData, Callback, Message, NextState) ->
try Handler:Callback(Message, Req, HandlerState) of try Handler:Callback(Message, Req, HandlerState) of