mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Refresh the type specifications.
Following discussions on #erlounge. Also fixes compilation in R14B03 and fixes a few underspecs dialyzer warnings.
This commit is contained in:
parent
0720d6b9e3
commit
3e55cb62c9
15 changed files with 164 additions and 210 deletions
|
@ -13,15 +13,13 @@
|
|||
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
-include_lib("kernel/include/inet.hrl").
|
||||
|
||||
-type http_method() :: 'OPTIONS' | 'GET' | 'HEAD'
|
||||
| 'POST' | 'PUT' | 'DELETE' | 'TRACE' | binary().
|
||||
-type http_uri() :: '*' | {absoluteURI, http | https, Host::binary(),
|
||||
Port::integer() | undefined, Path::binary()}
|
||||
| {scheme, Scheme::binary(), binary()}
|
||||
| {abs_path, binary()} | binary().
|
||||
-type http_version() :: {Major::integer(), Minor::integer()}.
|
||||
-type http_version() :: {Major::non_neg_integer(), Minor::non_neg_integer()}.
|
||||
-type http_header() :: 'Cache-Control' | 'Connection' | 'Date' | 'Pragma'
|
||||
| 'Transfer-Encoding' | 'Upgrade' | 'Via' | 'Accept' | 'Accept-Charset'
|
||||
| 'Accept-Encoding' | 'Accept-Language' | 'Authorization' | 'From' | 'Host'
|
||||
|
@ -48,16 +46,15 @@
|
|||
%% Request.
|
||||
method = 'GET' :: http_method(),
|
||||
version = {1, 1} :: http_version(),
|
||||
peer = undefined :: undefined | {Address::ip_address(), Port::ip_port()},
|
||||
peer = undefined :: undefined | {inet:ip_address(), inet:ip_port()},
|
||||
host = undefined :: undefined | cowboy_dispatcher:path_tokens(),
|
||||
host_info = undefined :: undefined | cowboy_dispatcher:path_tokens(),
|
||||
raw_host = undefined :: undefined | binary(),
|
||||
port = undefined :: undefined | ip_port(),
|
||||
port = undefined :: undefined | inet:ip_port(),
|
||||
path = undefined :: undefined | '*' | cowboy_dispatcher:path_tokens(),
|
||||
path_info = undefined :: undefined | cowboy_dispatcher:path_tokens(),
|
||||
raw_path = undefined :: undefined | binary(),
|
||||
qs_vals = undefined :: undefined
|
||||
| list({Name::binary(), Value::binary() | true}),
|
||||
qs_vals = undefined :: undefined | list({binary(), binary() | true}),
|
||||
raw_qs = undefined :: undefined | binary(),
|
||||
bindings = undefined :: undefined | cowboy_dispatcher:bindings(),
|
||||
headers = [] :: http_headers(),
|
||||
|
|
|
@ -17,9 +17,8 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_listener(Ref::term(), NbAcceptors::non_neg_integer(),
|
||||
Transport::module(), TransOpts::term(), Protocol::module(),
|
||||
ProtoOpts::term()) -> {ok, Pid::pid()}.
|
||||
-spec start_listener(any(), non_neg_integer(), module(), any(), module(), any())
|
||||
-> {ok, pid()}.
|
||||
start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) ->
|
||||
supervisor:start_child(cowboy_sup,
|
||||
{{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [
|
||||
|
@ -27,7 +26,7 @@ start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) ->
|
|||
]},
|
||||
permanent, 5000, supervisor, [cowboy_listener_sup]}).
|
||||
|
||||
-spec stop_listener(Ref::term()) -> ok | {error, not_found}.
|
||||
-spec stop_listener(any()) -> ok | {error, not_found}.
|
||||
stop_listener(Ref) ->
|
||||
case supervisor:terminate_child(cowboy_sup, {cowboy_listener_sup, Ref}) of
|
||||
ok ->
|
||||
|
|
|
@ -18,9 +18,8 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_link(LSocket::inet:socket(), Transport::module(),
|
||||
Protocol::module(), Opts::term(),
|
||||
MaxConns::non_neg_integer(), ReqsSup::pid()) -> {ok, Pid::pid()}.
|
||||
-spec start_link(inet:socket(), module(), module(), any(),
|
||||
non_neg_integer(), pid()) -> {ok, pid()}.
|
||||
start_link(LSocket, Transport, Protocol, Opts, MaxConns, ReqsSup) ->
|
||||
Pid = spawn_link(?MODULE, acceptor,
|
||||
[LSocket, Transport, Protocol, Opts, MaxConns, ReqsSup]),
|
||||
|
@ -28,9 +27,8 @@ start_link(LSocket, Transport, Protocol, Opts, MaxConns, ReqsSup) ->
|
|||
|
||||
%% Internal.
|
||||
|
||||
-spec acceptor(LSocket::inet:socket(), Transport::module(),
|
||||
Protocol::module(), Opts::term(),
|
||||
MaxConns::non_neg_integer(), ReqsSup::pid()) -> no_return().
|
||||
-spec acceptor(inet:socket(), module(), module(), any(),
|
||||
non_neg_integer(), pid()) -> no_return().
|
||||
acceptor(LSocket, Transport, Protocol, Opts, MaxConns, ReqsSup) ->
|
||||
case Transport:accept(LSocket, 2000) of
|
||||
{ok, CSocket} ->
|
||||
|
@ -47,7 +45,7 @@ acceptor(LSocket, Transport, Protocol, Opts, MaxConns, ReqsSup) ->
|
|||
end,
|
||||
?MODULE:acceptor(LSocket, Transport, Protocol, Opts, MaxConns, ReqsSup).
|
||||
|
||||
-spec limit_reqs(MaxConns::non_neg_integer(), ReqsSup::pid()) -> ok.
|
||||
-spec limit_reqs(non_neg_integer(), pid()) -> ok.
|
||||
limit_reqs(MaxConns, ReqsSup) ->
|
||||
Counts = supervisor:count_children(ReqsSup),
|
||||
Active = lists:keyfind(active, 1, Counts),
|
||||
|
|
|
@ -20,16 +20,15 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_link(NbAcceptors::non_neg_integer(), Transport::module(),
|
||||
TransOpts::term(), Protocol::module(), ProtoOpts::term(), ReqsPid::pid())
|
||||
-> {ok, Pid::pid()}.
|
||||
-spec start_link(non_neg_integer(), module(), any(), module(), any(), pid())
|
||||
-> {ok, pid()}.
|
||||
start_link(NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts, ReqsPid) ->
|
||||
supervisor:start_link(?MODULE, [NbAcceptors,
|
||||
Transport, TransOpts, Protocol, ProtoOpts, ReqsPid]).
|
||||
|
||||
%% supervisor.
|
||||
|
||||
-spec init(list(term())) -> term(). %% @todo These specs should be improved.
|
||||
-spec init(list()) -> {ok, {{one_for_one, 10, 10}, list()}}.
|
||||
init([NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts, ReqsPid]) ->
|
||||
{ok, LSocket} = Transport:listen(TransOpts),
|
||||
MaxConns = proplists:get_value(max_connections, TransOpts, 1024),
|
||||
|
|
|
@ -17,15 +17,15 @@
|
|||
|
||||
-export([start/2, stop/1]). %% API.
|
||||
|
||||
-type application_start_type() :: normal |
|
||||
{takeover, Node::node()} | {failover, Node::node()}.
|
||||
-type application_start_type() :: normal
|
||||
| {takeover, node()} | {failover, node()}.
|
||||
|
||||
%% API.
|
||||
|
||||
-spec start(Type::application_start_type(), Args::term()) -> {ok, Pid::pid()}.
|
||||
-spec start(application_start_type(), any()) -> {ok, pid()}.
|
||||
start(_Type, _Args) ->
|
||||
cowboy_sup:start_link().
|
||||
|
||||
-spec stop(State::term()) -> ok.
|
||||
-spec stop(any()) -> ok.
|
||||
stop(_State) ->
|
||||
ok.
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_link() -> {ok, Pid::pid()}.
|
||||
-spec start_link() -> {ok, pid()}.
|
||||
start_link() ->
|
||||
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
|
||||
|
||||
|
@ -96,8 +96,7 @@ code_change(_OldVsn, State, _Extra) ->
|
|||
|
||||
%% Internal.
|
||||
|
||||
-spec update_rfc1123(Prev::undefined | datetime(), Now::datetime(),
|
||||
Bin::binary()) -> binary().
|
||||
-spec update_rfc1123(undefined | datetime(), datetime(), binary()) -> binary().
|
||||
update_rfc1123(Now, Now, Bin) ->
|
||||
Bin;
|
||||
update_rfc1123({Date, {H, M, _}}, {Date, {H, M, S}},
|
||||
|
@ -136,7 +135,7 @@ pad_int(X) when X < 10 ->
|
|||
pad_int(X) ->
|
||||
list_to_binary(integer_to_list(X)).
|
||||
|
||||
-spec weekday(daynum()) -> binary().
|
||||
-spec weekday(daynum()) -> <<_:24>>.
|
||||
weekday(1) -> <<"Mon">>;
|
||||
weekday(2) -> <<"Tue">>;
|
||||
weekday(3) -> <<"Wed">>;
|
||||
|
@ -145,7 +144,7 @@ weekday(5) -> <<"Fri">>;
|
|||
weekday(6) -> <<"Sat">>;
|
||||
weekday(7) -> <<"Sun">>.
|
||||
|
||||
-spec month(month()) -> binary().
|
||||
-spec month(month()) -> <<_:24>>.
|
||||
month( 1) -> <<"Jan">>;
|
||||
month( 2) -> <<"Feb">>;
|
||||
month( 3) -> <<"Mar">>;
|
||||
|
|
|
@ -16,22 +16,21 @@
|
|||
-module(cowboy_dispatcher).
|
||||
-export([split_host/1, split_path/1, match/3]). %% API.
|
||||
|
||||
-type bindings() :: list({Key::atom(), Value::binary()}).
|
||||
-type bindings() :: list({atom(), binary()}).
|
||||
-type path_tokens() :: list(binary()).
|
||||
-type match_rule() :: '_' | '*' | list(binary() | '_' | atom()).
|
||||
-type dispatch_rule() :: {Host::match_rule(), list({Path::match_rule(),
|
||||
Handler::module(), Opts::term()})}.
|
||||
-type dispatch_path() :: list({match_rule(), module(), any()}).
|
||||
-type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}.
|
||||
-type dispatch_rules() :: list(dispatch_rule()).
|
||||
|
||||
-export_type([bindings/0, path_tokens/0, dispatch_rules/0]).
|
||||
|
||||
-include_lib("kernel/include/inet.hrl").
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
%% API.
|
||||
|
||||
-spec split_host(Host::binary())
|
||||
-> {Tokens::path_tokens(), RawHost::binary(), Port::undefined | ip_port()}.
|
||||
-spec split_host(binary())
|
||||
-> {path_tokens(), binary(), undefined | inet:ip_port()}.
|
||||
split_host(<<>>) ->
|
||||
{[], <<>>, undefined};
|
||||
split_host(Host) ->
|
||||
|
@ -43,8 +42,7 @@ split_host(Host) ->
|
|||
list_to_integer(binary_to_list(Port))}
|
||||
end.
|
||||
|
||||
-spec split_path(Path::binary())
|
||||
-> {Tokens::path_tokens(), RawPath::binary(), Qs::binary()}.
|
||||
-spec split_path(binary()) -> {path_tokens(), binary(), binary()}.
|
||||
split_path(Path) ->
|
||||
case binary:split(Path, <<"?">>) of
|
||||
[Path] -> {do_split_path(Path, <<"/">>), Path, <<>>};
|
||||
|
@ -52,17 +50,15 @@ split_path(Path) ->
|
|||
[Path2, Qs] -> {do_split_path(Path2, <<"/">>), Path2, Qs}
|
||||
end.
|
||||
|
||||
-spec do_split_path(RawPath::binary(), Separator::binary())
|
||||
-> Tokens::path_tokens().
|
||||
-spec do_split_path(binary(), <<_:8>>) -> path_tokens().
|
||||
do_split_path(RawPath, Separator) ->
|
||||
case binary:split(RawPath, Separator, [global, trim]) of
|
||||
[<<>>|Path] -> Path;
|
||||
Path -> Path
|
||||
end.
|
||||
|
||||
-spec match(Host::path_tokens(), Path::path_tokens(),
|
||||
Dispatch::dispatch_rules())
|
||||
-> {ok, Handler::module(), Opts::term(), Binds::bindings(),
|
||||
-spec match(Host::path_tokens(), Path::path_tokens(), dispatch_rules())
|
||||
-> {ok, module(), any(), bindings(),
|
||||
HostInfo::undefined | path_tokens(),
|
||||
PathInfo::undefined | path_tokens()}
|
||||
| {error, notfound, host} | {error, notfound, path}.
|
||||
|
@ -80,10 +76,9 @@ match(Host, Path, [{HostMatch, PathMatchs}|Tail]) ->
|
|||
match_path(Path, PathMatchs, HostBinds, lists:reverse(HostInfo))
|
||||
end.
|
||||
|
||||
-spec match_path(Path::path_tokens(), list({Path::match_rule(),
|
||||
Handler::module(), Opts::term()}), HostBinds::bindings(),
|
||||
-spec match_path(path_tokens(), dispatch_path(), bindings(),
|
||||
HostInfo::undefined | path_tokens())
|
||||
-> {ok, Handler::module(), Opts::term(), Binds::bindings(),
|
||||
-> {ok, module(), any(), bindings(),
|
||||
HostInfo::undefined | path_tokens(),
|
||||
PathInfo::undefined | path_tokens()}
|
||||
| {error, notfound, path}.
|
||||
|
@ -103,15 +98,15 @@ match_path(Path, [{PathMatch, Handler, Opts}|Tail], HostBinds, HostInfo) ->
|
|||
|
||||
%% Internal.
|
||||
|
||||
-spec try_match(Type::host | path, List::path_tokens(), Match::match_rule())
|
||||
-> {true, Binds::bindings(), ListInfo::undefined | path_tokens()} | false.
|
||||
-spec try_match(host | path, path_tokens(), match_rule())
|
||||
-> {true, bindings(), undefined | path_tokens()} | false.
|
||||
try_match(host, List, Match) ->
|
||||
list_match(lists:reverse(List), lists:reverse(Match), []);
|
||||
try_match(path, List, Match) ->
|
||||
list_match(List, Match, []).
|
||||
|
||||
-spec list_match(List::path_tokens(), Match::match_rule(), Binds::bindings())
|
||||
-> {true, Binds::bindings(), ListInfo::undefined | path_tokens()} | false.
|
||||
-spec list_match(path_tokens(), match_rule(), bindings())
|
||||
-> {true, bindings(), undefined | path_tokens()} | false.
|
||||
%% Atom '...' matches any trailing path, stop right now.
|
||||
list_match(List, ['...'], Binds) ->
|
||||
{true, Binds, List};
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
socket :: inet:socket(),
|
||||
transport :: module(),
|
||||
dispatch :: cowboy_dispatcher:dispatch_rules(),
|
||||
handler :: {Handler::module(), Opts::term()},
|
||||
handler :: {module(), any()},
|
||||
req_empty_lines = 0 :: integer(),
|
||||
max_empty_lines :: integer(),
|
||||
timeout :: timeout(),
|
||||
|
@ -33,15 +33,14 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_link(Socket::inet:socket(), Transport::module(), Opts::term())
|
||||
-> {ok, Pid::pid()}.
|
||||
-spec start_link(inet:socket(), module(), any()) -> {ok, pid()}.
|
||||
start_link(Socket, Transport, Opts) ->
|
||||
Pid = spawn_link(?MODULE, init, [Socket, Transport, Opts]),
|
||||
{ok, Pid}.
|
||||
|
||||
%% FSM.
|
||||
|
||||
-spec init(Socket::inet:socket(), Transport::module(), Opts::term()) -> ok.
|
||||
-spec init(inet:socket(), module(), any()) -> ok.
|
||||
init(Socket, Transport, Opts) ->
|
||||
Dispatch = proplists:get_value(dispatch, Opts, []),
|
||||
MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5),
|
||||
|
@ -49,7 +48,7 @@ init(Socket, Transport, Opts) ->
|
|||
wait_request(#state{socket=Socket, transport=Transport,
|
||||
dispatch=Dispatch, max_empty_lines=MaxEmptyLines, timeout=Timeout}).
|
||||
|
||||
-spec parse_request(State::#state{}) -> ok.
|
||||
-spec parse_request(#state{}) -> ok.
|
||||
%% @todo Use decode_packet options to limit length?
|
||||
parse_request(State=#state{buffer=Buffer}) ->
|
||||
case erlang:decode_packet(http_bin, Buffer, []) of
|
||||
|
@ -58,7 +57,7 @@ parse_request(State=#state{buffer=Buffer}) ->
|
|||
{error, _Reason} -> error_response(400, State)
|
||||
end.
|
||||
|
||||
-spec wait_request(State::#state{}) -> ok.
|
||||
-spec wait_request(#state{}) -> ok.
|
||||
wait_request(State=#state{socket=Socket, transport=Transport,
|
||||
timeout=T, buffer=Buffer}) ->
|
||||
case Transport:recv(Socket, 0, T) of
|
||||
|
@ -68,8 +67,8 @@ wait_request(State=#state{socket=Socket, transport=Transport,
|
|||
{error, closed} -> terminate(State)
|
||||
end.
|
||||
|
||||
-spec request({http_request, Method::http_method(), URI::http_uri(),
|
||||
Version::http_version()}, State::#state{}) -> ok.
|
||||
-spec request({http_request, http_method(), http_uri(),
|
||||
http_version()}, #state{}) -> ok.
|
||||
%% @todo We probably want to handle some things differently between versions.
|
||||
request({http_request, _Method, _URI, Version}, State)
|
||||
when Version =/= {1, 0}, Version =/= {1, 1} ->
|
||||
|
@ -100,7 +99,7 @@ request({http_error, <<"\r\n">>}, State=#state{req_empty_lines=N}) ->
|
|||
request({http_error, _Any}, State) ->
|
||||
error_terminate(400, State).
|
||||
|
||||
-spec parse_header(Req::#http_req{}, State::#state{}) -> ok.
|
||||
-spec parse_header(#http_req{}, #state{}) -> ok.
|
||||
parse_header(Req, State=#state{buffer=Buffer}) ->
|
||||
case erlang:decode_packet(httph_bin, Buffer, []) of
|
||||
{ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest});
|
||||
|
@ -108,7 +107,7 @@ parse_header(Req, State=#state{buffer=Buffer}) ->
|
|||
{error, _Reason} -> error_response(400, State)
|
||||
end.
|
||||
|
||||
-spec wait_header(Req::#http_req{}, State::#state{}) -> ok.
|
||||
-spec wait_header(#http_req{}, #state{}) -> ok.
|
||||
wait_header(Req, State=#state{socket=Socket,
|
||||
transport=Transport, timeout=T, buffer=Buffer}) ->
|
||||
case Transport:recv(Socket, 0, T) of
|
||||
|
@ -118,8 +117,8 @@ wait_header(Req, State=#state{socket=Socket,
|
|||
{error, closed} -> terminate(State)
|
||||
end.
|
||||
|
||||
-spec header({http_header, I::integer(), Field::http_header(), R::term(),
|
||||
Value::binary()} | http_eoh, Req::#http_req{}, State::#state{}) -> ok.
|
||||
-spec header({http_header, integer(), http_header(), any(), binary()}
|
||||
| http_eoh, #http_req{}, #state{}) -> ok.
|
||||
header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{
|
||||
transport=Transport, host=undefined}, State) ->
|
||||
RawHost2 = binary_to_lower(RawHost),
|
||||
|
@ -153,7 +152,7 @@ header(http_eoh, Req, State=#state{buffer=Buffer}) ->
|
|||
header({http_error, _Bin}, _Req, State) ->
|
||||
error_terminate(500, State).
|
||||
|
||||
-spec dispatch(Req::#http_req{}, State::#state{}) -> ok.
|
||||
-spec dispatch(#http_req{}, #state{}) -> ok.
|
||||
dispatch(Req=#http_req{host=Host, path=Path},
|
||||
State=#state{dispatch=Dispatch}) ->
|
||||
%% @todo We probably want to filter the Host and Path here to allow
|
||||
|
@ -169,7 +168,7 @@ dispatch(Req=#http_req{host=Host, path=Path},
|
|||
error_terminate(404, State)
|
||||
end.
|
||||
|
||||
-spec handler_init(Req::#http_req{}, State::#state{}) -> ok.
|
||||
-spec handler_init(#http_req{}, #state{}) -> ok.
|
||||
handler_init(Req, State=#state{
|
||||
transport=Transport, handler={Handler, Opts}}) ->
|
||||
try Handler:init({Transport:name(), http}, Req, Opts) of
|
||||
|
@ -186,8 +185,7 @@ handler_init(Req, State=#state{
|
|||
[Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()])
|
||||
end.
|
||||
|
||||
-spec handler_loop(HandlerState::term(), Req::#http_req{},
|
||||
State::#state{}) -> ok.
|
||||
-spec handler_loop(any(), #http_req{}, #state{}) -> ok.
|
||||
handler_loop(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
|
||||
try Handler:handle(Req#http_req{resp_state=waiting}, HandlerState) of
|
||||
{ok, Req2, HandlerState2} ->
|
||||
|
@ -202,8 +200,7 @@ handler_loop(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
|
|||
HandlerState, Req, erlang:get_stacktrace()])
|
||||
end.
|
||||
|
||||
-spec handler_terminate(HandlerState::term(), Req::#http_req{},
|
||||
State::#state{}) -> ok.
|
||||
-spec handler_terminate(any(), #http_req{}, #state{}) -> ok.
|
||||
handler_terminate(HandlerState, Req=#http_req{buffer=Buffer},
|
||||
State=#state{handler={Handler, Opts}}) ->
|
||||
try
|
||||
|
@ -227,7 +224,7 @@ handler_terminate(HandlerState, Req=#http_req{buffer=Buffer},
|
|||
HandlerState, Req, erlang:get_stacktrace()])
|
||||
end.
|
||||
|
||||
-spec ensure_body_processed(Req::#http_req{}) -> ok | close.
|
||||
-spec ensure_body_processed(#http_req{}) -> ok | close.
|
||||
ensure_body_processed(#http_req{body_state=done}) ->
|
||||
ok;
|
||||
ensure_body_processed(Req=#http_req{body_state=waiting}) ->
|
||||
|
@ -237,7 +234,7 @@ ensure_body_processed(Req=#http_req{body_state=waiting}) ->
|
|||
_Any -> ok
|
||||
end.
|
||||
|
||||
-spec ensure_response(Req::#http_req{}, State::#state{}) -> ok.
|
||||
-spec ensure_response(#http_req{}, #state{}) -> ok.
|
||||
%% The handler has already fully replied to the client.
|
||||
ensure_response(#http_req{resp_state=done}, _State) ->
|
||||
ok;
|
||||
|
@ -251,7 +248,7 @@ ensure_response(#http_req{socket=Socket, transport=Transport,
|
|||
Transport:send(Socket, <<"0\r\n\r\n">>),
|
||||
close.
|
||||
|
||||
-spec error_response(Code::http_status(), State::#state{}) -> ok.
|
||||
-spec error_response(http_status(), #state{}) -> ok.
|
||||
error_response(Code, #state{socket=Socket,
|
||||
transport=Transport, connection=Connection}) ->
|
||||
_ = cowboy_http_req:reply(Code, [], [], #http_req{
|
||||
|
@ -259,23 +256,23 @@ error_response(Code, #state{socket=Socket,
|
|||
connection=Connection, resp_state=waiting}),
|
||||
ok.
|
||||
|
||||
-spec error_terminate(Code::http_status(), State::#state{}) -> ok.
|
||||
-spec error_terminate(http_status(), #state{}) -> ok.
|
||||
error_terminate(Code, State) ->
|
||||
error_response(Code, State#state{connection=close}),
|
||||
terminate(State).
|
||||
|
||||
-spec terminate(State::#state{}) -> ok.
|
||||
-spec terminate(#state{}) -> ok.
|
||||
terminate(#state{socket=Socket, transport=Transport}) ->
|
||||
Transport:close(Socket),
|
||||
ok.
|
||||
|
||||
%% Internal.
|
||||
|
||||
-spec version_to_connection(Version::http_version()) -> keepalive | close.
|
||||
-spec version_to_connection(http_version()) -> keepalive | close.
|
||||
version_to_connection({1, 1}) -> keepalive;
|
||||
version_to_connection(_Any) -> close.
|
||||
|
||||
-spec connection_to_atom(Connection::binary()) -> keepalive | close.
|
||||
-spec connection_to_atom(binary()) -> keepalive | close.
|
||||
connection_to_atom(<<"keep-alive">>) ->
|
||||
keepalive;
|
||||
connection_to_atom(<<"close">>) ->
|
||||
|
@ -286,7 +283,7 @@ connection_to_atom(Connection) ->
|
|||
_Any -> keepalive
|
||||
end.
|
||||
|
||||
-spec default_port(TransportName::atom()) -> 80 | 443.
|
||||
-spec default_port(atom()) -> 80 | 443.
|
||||
default_port(ssl) -> 443;
|
||||
default_port(_) -> 80.
|
||||
|
||||
|
|
|
@ -38,65 +38,59 @@
|
|||
|
||||
%% Request API.
|
||||
|
||||
-spec method(Req::#http_req{}) -> {Method::http_method(), Req::#http_req{}}.
|
||||
-spec method(#http_req{}) -> {http_method(), #http_req{}}.
|
||||
method(Req) ->
|
||||
{Req#http_req.method, Req}.
|
||||
|
||||
-spec version(Req::#http_req{}) -> {Version::http_version(), Req::#http_req{}}.
|
||||
-spec version(#http_req{}) -> {http_version(), #http_req{}}.
|
||||
version(Req) ->
|
||||
{Req#http_req.version, Req}.
|
||||
|
||||
-spec peer(Req::#http_req{})
|
||||
-> {{Address::ip_address(), Port::ip_port()}, Req::#http_req{}}.
|
||||
-spec peer(#http_req{}) -> {{inet:ip_address(), inet:ip_port()}, #http_req{}}.
|
||||
peer(Req=#http_req{socket=Socket, transport=Transport, peer=undefined}) ->
|
||||
{ok, Peer} = Transport:peername(Socket),
|
||||
{Peer, Req#http_req{peer=Peer}};
|
||||
peer(Req) ->
|
||||
{Req#http_req.peer, Req}.
|
||||
|
||||
-spec host(Req::#http_req{})
|
||||
-> {Host::cowboy_dispatcher:path_tokens(), Req::#http_req{}}.
|
||||
-spec host(#http_req{}) -> {cowboy_dispatcher:path_tokens(), #http_req{}}.
|
||||
host(Req) ->
|
||||
{Req#http_req.host, Req}.
|
||||
|
||||
-spec host_info(Req::#http_req{})
|
||||
-> {HostInfo::cowboy_dispatcher:path_tokens() | undefined,
|
||||
Req::#http_req{}}.
|
||||
-spec host_info(#http_req{})
|
||||
-> {cowboy_dispatcher:path_tokens() | undefined, #http_req{}}.
|
||||
host_info(Req) ->
|
||||
{Req#http_req.host_info, Req}.
|
||||
|
||||
-spec raw_host(Req::#http_req{}) -> {RawHost::binary(), Req::#http_req{}}.
|
||||
-spec raw_host(#http_req{}) -> {binary(), #http_req{}}.
|
||||
raw_host(Req) ->
|
||||
{Req#http_req.raw_host, Req}.
|
||||
|
||||
-spec port(Req::#http_req{}) -> {Port::ip_port(), Req::#http_req{}}.
|
||||
-spec port(#http_req{}) -> {inet:ip_port(), #http_req{}}.
|
||||
port(Req) ->
|
||||
{Req#http_req.port, Req}.
|
||||
|
||||
-spec path(Req::#http_req{})
|
||||
-> {Path::cowboy_dispatcher:path_tokens(), Req::#http_req{}}.
|
||||
-spec path(#http_req{}) -> {cowboy_dispatcher:path_tokens(), #http_req{}}.
|
||||
path(Req) ->
|
||||
{Req#http_req.path, Req}.
|
||||
|
||||
-spec path_info(Req::#http_req{})
|
||||
-> {PathInfo::cowboy_dispatcher:path_tokens() | undefined,
|
||||
Req::#http_req{}}.
|
||||
-spec path_info(#http_req{})
|
||||
-> {cowboy_dispatcher:path_tokens() | undefined, #http_req{}}.
|
||||
path_info(Req) ->
|
||||
{Req#http_req.path_info, Req}.
|
||||
|
||||
-spec raw_path(Req::#http_req{}) -> {RawPath::binary(), Req::#http_req{}}.
|
||||
-spec raw_path(#http_req{}) -> {binary(), #http_req{}}.
|
||||
raw_path(Req) ->
|
||||
{Req#http_req.raw_path, Req}.
|
||||
|
||||
-spec qs_val(Name::binary(), Req::#http_req{})
|
||||
-> {Value::binary() | true | undefined, Req::#http_req{}}.
|
||||
-spec qs_val(binary(), #http_req{})
|
||||
-> {binary() | true | undefined, #http_req{}}.
|
||||
%% @equiv qs_val(Name, Req) -> qs_val(Name, Req, undefined)
|
||||
qs_val(Name, Req) ->
|
||||
qs_val(Name, Req, undefined).
|
||||
|
||||
-spec qs_val(Name::binary(), Req::#http_req{}, Default)
|
||||
-> {Value::binary() | true | Default, Req::#http_req{}}
|
||||
when Default::term().
|
||||
-spec qs_val(binary(), #http_req{}, Default)
|
||||
-> {binary() | true | Default, #http_req{}} when Default::any().
|
||||
qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}, Default) ->
|
||||
QsVals = parse_qs(RawQs),
|
||||
qs_val(Name, Req#http_req{qs_vals=QsVals}, Default);
|
||||
|
@ -106,61 +100,56 @@ qs_val(Name, Req, Default) ->
|
|||
false -> {Default, Req}
|
||||
end.
|
||||
|
||||
-spec qs_vals(Req::#http_req{})
|
||||
-> {list({Name::binary(), Value::binary() | true}), Req::#http_req{}}.
|
||||
-spec qs_vals(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}.
|
||||
qs_vals(Req=#http_req{raw_qs=RawQs, qs_vals=undefined}) ->
|
||||
QsVals = parse_qs(RawQs),
|
||||
qs_vals(Req#http_req{qs_vals=QsVals});
|
||||
qs_vals(Req=#http_req{qs_vals=QsVals}) ->
|
||||
{QsVals, Req}.
|
||||
|
||||
-spec raw_qs(Req::#http_req{}) -> {RawQs::binary(), Req::#http_req{}}.
|
||||
-spec raw_qs(#http_req{}) -> {binary(), #http_req{}}.
|
||||
raw_qs(Req) ->
|
||||
{Req#http_req.raw_qs, Req}.
|
||||
|
||||
-spec binding(Name::atom(), Req::#http_req{})
|
||||
-> {Value::binary() | undefined, Req::#http_req{}}.
|
||||
-spec binding(atom(), #http_req{}) -> {binary() | undefined, #http_req{}}.
|
||||
%% @equiv binding(Name, Req) -> binding(Name, Req, undefined)
|
||||
binding(Name, Req) ->
|
||||
binding(Name, Req, undefined).
|
||||
|
||||
-spec binding(Name::atom(), Req::#http_req{}, Default)
|
||||
-> {Value::binary() | Default, Req::#http_req{}} when Default::term().
|
||||
-spec binding(atom(), #http_req{}, Default)
|
||||
-> {binary() | Default, #http_req{}} when Default::any().
|
||||
binding(Name, Req, Default) ->
|
||||
case lists:keyfind(Name, 1, Req#http_req.bindings) of
|
||||
{Name, Value} -> {Value, Req};
|
||||
false -> {Default, Req}
|
||||
end.
|
||||
|
||||
-spec bindings(Req::#http_req{})
|
||||
-> {list({Name::atom(), Value::binary()}), Req::#http_req{}}.
|
||||
-spec bindings(#http_req{}) -> {list({atom(), binary()}), #http_req{}}.
|
||||
bindings(Req) ->
|
||||
{Req#http_req.bindings, Req}.
|
||||
|
||||
-spec header(Name::atom() | binary(), Req::#http_req{})
|
||||
-> {Value::binary() | undefined, Req::#http_req{}}.
|
||||
-spec header(atom() | binary(), #http_req{})
|
||||
-> {binary() | undefined, #http_req{}}.
|
||||
%% @equiv header(Name, Req) -> header(Name, Req, undefined)
|
||||
header(Name, Req) ->
|
||||
header(Name, Req, undefined).
|
||||
|
||||
-spec header(Name::atom() | binary(), Req::#http_req{}, Default)
|
||||
-> {Value::binary() | Default, Req::#http_req{}} when Default::term().
|
||||
-spec header(atom() | binary(), #http_req{}, Default)
|
||||
-> {binary() | Default, #http_req{}} when Default::any().
|
||||
header(Name, Req, Default) ->
|
||||
case lists:keyfind(Name, 1, Req#http_req.headers) of
|
||||
{Name, Value} -> {Value, Req};
|
||||
false -> {Default, Req}
|
||||
end.
|
||||
|
||||
-spec headers(Req::#http_req{})
|
||||
-> {Headers::http_headers(), Req::#http_req{}}.
|
||||
-spec headers(#http_req{}) -> {http_headers(), #http_req{}}.
|
||||
headers(Req) ->
|
||||
{Req#http_req.headers, Req}.
|
||||
|
||||
%% Request Body API.
|
||||
|
||||
%% @todo We probably want to allow a max length.
|
||||
-spec body(Req::#http_req{})
|
||||
-> {ok, Body::binary(), Req::#http_req{}} | {error, Reason::atom()}.
|
||||
-spec body(#http_req{}) -> {ok, binary(), #http_req{}} | {error, atom()}.
|
||||
body(Req) ->
|
||||
{Length, Req2} = cowboy_http_req:header('Content-Length', Req),
|
||||
case Length of
|
||||
|
@ -171,28 +160,28 @@ body(Req) ->
|
|||
end.
|
||||
|
||||
%% @todo We probably want to configure the timeout.
|
||||
-spec body(Length::non_neg_integer(), Req::#http_req{})
|
||||
-> {ok, Body::binary(), Req::#http_req{}} | {error, Reason::atom()}.
|
||||
-spec body(non_neg_integer(), #http_req{})
|
||||
-> {ok, binary(), #http_req{}} | {error, atom()}.
|
||||
body(Length, Req=#http_req{body_state=waiting, buffer=Buffer})
|
||||
when Length =:= byte_size(Buffer) ->
|
||||
{ok, Buffer, Req#http_req{body_state=done, buffer= <<>>}};
|
||||
body(Length, Req=#http_req{socket=Socket, transport=Transport,
|
||||
body_state=waiting, buffer=Buffer}) when Length > byte_size(Buffer) ->
|
||||
case Transport:recv(Socket, Length - byte_size(Buffer), 5000) of
|
||||
{ok, Body} -> {ok, << Buffer/binary, Body/binary >>, Req#http_req{body_state=done, buffer= <<>>}};
|
||||
{ok, Body} -> {ok, << Buffer/binary, Body/binary >>,
|
||||
Req#http_req{body_state=done, buffer= <<>>}};
|
||||
{error, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
-spec body_qs(Req::#http_req{})
|
||||
-> {list({Name::binary(), Value::binary() | true}), Req::#http_req{}}.
|
||||
-spec body_qs(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}.
|
||||
body_qs(Req) ->
|
||||
{ok, Body, Req2} = body(Req),
|
||||
{parse_qs(Body), Req2}.
|
||||
|
||||
%% Response API.
|
||||
|
||||
-spec reply(Code::http_status(), Headers::http_headers(),
|
||||
Body::iodata(), Req::#http_req{}) -> {ok, Req::#http_req{}}.
|
||||
-spec reply(http_status(), http_headers(), iodata(), #http_req{})
|
||||
-> {ok, #http_req{}}.
|
||||
reply(Code, Headers, Body, Req=#http_req{socket=Socket,
|
||||
transport=Transport, connection=Connection,
|
||||
resp_state=waiting}) ->
|
||||
|
@ -206,8 +195,8 @@ reply(Code, Headers, Body, Req=#http_req{socket=Socket,
|
|||
Transport:send(Socket, [Head, Body]),
|
||||
{ok, Req#http_req{resp_state=done}}.
|
||||
|
||||
-spec chunked_reply(Code::http_status(), Headers::http_headers(),
|
||||
Req::#http_req{}) -> {ok, Req::#http_req{}}.
|
||||
-spec chunked_reply(http_status(), http_headers(), #http_req{})
|
||||
-> {ok, #http_req{}}.
|
||||
chunked_reply(Code, Headers, Req=#http_req{socket=Socket, transport=Transport,
|
||||
resp_state=waiting}) ->
|
||||
Head = response_head(Code, Headers, [
|
||||
|
@ -219,14 +208,14 @@ chunked_reply(Code, Headers, Req=#http_req{socket=Socket, transport=Transport,
|
|||
Transport:send(Socket, Head),
|
||||
{ok, Req#http_req{resp_state=chunks}}.
|
||||
|
||||
-spec chunk(Data::iodata(), Req::#http_req{}) -> ok.
|
||||
-spec chunk(iodata(), #http_req{}) -> ok.
|
||||
chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) ->
|
||||
Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
|
||||
<<"\r\n">>, Data, <<"\r\n">>]).
|
||||
|
||||
%% Internal.
|
||||
|
||||
-spec parse_qs(Qs::binary()) -> list({Name::binary(), Value::binary() | true}).
|
||||
-spec parse_qs(binary()) -> list({binary(), binary() | true}).
|
||||
parse_qs(<<>>) ->
|
||||
[];
|
||||
parse_qs(Qs) ->
|
||||
|
@ -236,8 +225,7 @@ parse_qs(Qs) ->
|
|||
[Name, Value] -> {Name, Value}
|
||||
end || Token <- Tokens].
|
||||
|
||||
-spec response_head(Code::http_status(), Headers::http_headers(),
|
||||
DefaultHeaders::http_headers()) -> iolist().
|
||||
-spec response_head(http_status(), http_headers(), http_headers()) -> iolist().
|
||||
response_head(Code, Headers, DefaultHeaders) ->
|
||||
StatusLine = <<"HTTP/1.1 ", (status(Code))/binary, "\r\n">>,
|
||||
Headers2 = [{header_to_binary(Key), Value} || {Key, Value} <- Headers],
|
||||
|
@ -247,13 +235,14 @@ response_head(Code, Headers, DefaultHeaders) ->
|
|||
|| {Key, Value} <- Headers4],
|
||||
[StatusLine, Headers5, <<"\r\n">>].
|
||||
|
||||
-spec atom_to_connection(Atom::keepalive | close) -> binary().
|
||||
-spec atom_to_connection(keepalive) -> <<_:80>>;
|
||||
(close) -> <<_:40>>.
|
||||
atom_to_connection(keepalive) ->
|
||||
<<"keep-alive">>;
|
||||
atom_to_connection(close) ->
|
||||
<<"close">>.
|
||||
|
||||
-spec status(Code::http_status()) -> binary().
|
||||
-spec status(http_status()) -> binary().
|
||||
status(100) -> <<"100 Continue">>;
|
||||
status(101) -> <<"101 Switching Protocols">>;
|
||||
status(102) -> <<"102 Processing">>;
|
||||
|
@ -375,7 +364,8 @@ parse_qs_test_() ->
|
|||
{<<"a=b">>, [{<<"a">>, <<"b">>}]},
|
||||
{<<"aaa=bbb">>, [{<<"aaa">>, <<"bbb">>}]},
|
||||
{<<"a&b">>, [{<<"a">>, true}, {<<"b">>, true}]},
|
||||
{<<"a=b&c&d=e">>, [{<<"a">>, <<"b">>}, {<<"c">>, true}, {<<"d">>, <<"e">>}]},
|
||||
{<<"a=b&c&d=e">>, [{<<"a">>, <<"b">>},
|
||||
{<<"c">>, true}, {<<"d">>, <<"e">>}]},
|
||||
{<<"a=b=c=d=e&f=g">>, [{<<"a">>, <<"b=c=d=e">>}, {<<"f">>, <<"g">>}]}
|
||||
],
|
||||
[{Qs, fun() -> R = parse_qs(Qs) end} || {Qs, R} <- Tests].
|
||||
|
|
|
@ -19,22 +19,21 @@
|
|||
|
||||
-record(state, {
|
||||
handler :: module(),
|
||||
opts :: term(),
|
||||
opts :: any(),
|
||||
origin = undefined :: undefined | binary(),
|
||||
challenge = undefined :: undefined | binary(),
|
||||
timeout = infinity :: timeout(),
|
||||
messages = undefined :: undefined | {atom(), atom(), atom()}
|
||||
}).
|
||||
|
||||
-spec upgrade(Handler::module(), Opts::term(), Req::#http_req{}) -> ok.
|
||||
-spec upgrade(module(), any(), #http_req{}) -> ok.
|
||||
upgrade(Handler, Opts, Req) ->
|
||||
case catch websocket_upgrade(#state{handler=Handler, opts=Opts}, Req) of
|
||||
{ok, State, Req2} -> handler_init(State, Req2);
|
||||
{'EXIT', _Reason} -> upgrade_error(Req)
|
||||
end.
|
||||
|
||||
-spec websocket_upgrade(State::#state{}, Req::#http_req{})
|
||||
-> {ok, State::#state{}, Req::#http_req{}}.
|
||||
-spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}.
|
||||
websocket_upgrade(State, Req) ->
|
||||
{<<"Upgrade">>, Req2} = cowboy_http_req:header('Connection', Req),
|
||||
{<<"WebSocket">>, Req3} = cowboy_http_req:header('Upgrade', Req2),
|
||||
|
@ -46,19 +45,19 @@ websocket_upgrade(State, Req) ->
|
|||
Challenge = challenge(Key1, Key2, Key3),
|
||||
{ok, State#state{origin=Origin, challenge=Challenge}, Req7}.
|
||||
|
||||
-spec challenge(Key1::binary(), Key2::binary(), Key3::binary()) -> binary().
|
||||
-spec challenge(binary(), binary(), binary()) -> binary().
|
||||
challenge(Key1, Key2, Key3) ->
|
||||
IntKey1 = key_to_integer(Key1),
|
||||
IntKey2 = key_to_integer(Key2),
|
||||
erlang:md5(<< IntKey1:32, IntKey2:32, Key3/binary >>).
|
||||
|
||||
-spec key_to_integer(Key::binary()) -> integer().
|
||||
-spec key_to_integer(binary()) -> integer().
|
||||
key_to_integer(Key) ->
|
||||
Number = list_to_integer([C || << C >> <= Key, C >= $0, C =< $9]),
|
||||
Spaces = length([C || << C >> <= Key, C =:= 32]),
|
||||
Number div Spaces.
|
||||
|
||||
-spec handler_init(State::#state{}, Req::#http_req{}) -> ok.
|
||||
-spec handler_init(#state{}, #http_req{}) -> ok.
|
||||
handler_init(State=#state{handler=Handler, opts=Opts},
|
||||
Req=#http_req{transport=Transport}) ->
|
||||
case catch Handler:websocket_init(Transport:name(), Req, Opts) of
|
||||
|
@ -71,14 +70,13 @@ handler_init(State=#state{handler=Handler, opts=Opts},
|
|||
upgrade_error(Req)
|
||||
end.
|
||||
|
||||
-spec upgrade_error(Req::#http_req{}) -> ok.
|
||||
-spec upgrade_error(#http_req{}) -> ok.
|
||||
upgrade_error(Req=#http_req{socket=Socket, transport=Transport}) ->
|
||||
{ok, _Req} = cowboy_http_req:reply(400, [], [],
|
||||
Req#http_req{resp_state=waiting}),
|
||||
Transport:close(Socket).
|
||||
|
||||
-spec websocket_handshake(State::#state{}, Req::#http_req{},
|
||||
HandlerState::term()) -> ok.
|
||||
-spec websocket_handshake(#state{}, #http_req{}, any()) -> ok.
|
||||
websocket_handshake(State=#state{origin=Origin, challenge=Challenge},
|
||||
Req=#http_req{transport=Transport, raw_host=Host, port=Port,
|
||||
raw_path=Path}, HandlerState) ->
|
||||
|
@ -93,8 +91,8 @@ websocket_handshake(State=#state{origin=Origin, challenge=Challenge},
|
|||
handler_loop(State#state{messages=Transport:messages()},
|
||||
Req2, HandlerState, <<>>).
|
||||
|
||||
-spec websocket_location(TransportName::atom(), Host::binary(),
|
||||
Port::ip_port(), Path::binary()) -> binary().
|
||||
-spec websocket_location(atom(), binary(), inet:ip_port(), binary())
|
||||
-> binary().
|
||||
websocket_location(ssl, Host, Port, Path) ->
|
||||
<< "wss://", Host/binary, ":",
|
||||
(list_to_binary(integer_to_list(Port)))/binary, Path/binary >>;
|
||||
|
@ -102,8 +100,7 @@ websocket_location(_Any, Host, Port, Path) ->
|
|||
<< "ws://", Host/binary, ":",
|
||||
(list_to_binary(integer_to_list(Port)))/binary, Path/binary >>.
|
||||
|
||||
-spec handler_loop(State::#state{}, Req::#http_req{},
|
||||
HandlerState::term(), SoFar::binary()) -> ok.
|
||||
-spec handler_loop(#state{}, #http_req{}, any(), binary()) -> ok.
|
||||
handler_loop(State=#state{messages={OK, Closed, Error}, timeout=Timeout},
|
||||
Req=#http_req{socket=Socket, transport=Transport},
|
||||
HandlerState, SoFar) ->
|
||||
|
@ -123,8 +120,7 @@ handler_loop(State=#state{messages={OK, Closed, Error}, timeout=Timeout},
|
|||
websocket_close(State, Req, HandlerState, {normal, timeout})
|
||||
end.
|
||||
|
||||
-spec websocket_data(State::#state{}, Req::#http_req{},
|
||||
HandlerState::term(), Data::binary()) -> ok.
|
||||
-spec websocket_data(#state{}, #http_req{}, any(), binary()) -> ok.
|
||||
websocket_data(State, Req, HandlerState, << 255, 0, _Rest/bits >>) ->
|
||||
websocket_close(State, Req, HandlerState, {normal, closed});
|
||||
websocket_data(State, Req, HandlerState, Data) when byte_size(Data) < 3 ->
|
||||
|
@ -133,8 +129,7 @@ websocket_data(State, Req, HandlerState, Data) ->
|
|||
websocket_frame(State, Req, HandlerState, Data, binary:first(Data)).
|
||||
|
||||
%% We do not support any frame type other than 0 yet. Just like the specs.
|
||||
-spec websocket_frame(State::#state{}, Req::#http_req{},
|
||||
HandlerState::term(), Data::binary(), FrameType::byte()) -> ok.
|
||||
-spec websocket_frame(#state{}, #http_req{}, any(), binary(), byte()) -> ok.
|
||||
websocket_frame(State, Req, HandlerState, Data, 0) ->
|
||||
case binary:match(Data, << 255 >>) of
|
||||
{Pos, 1} ->
|
||||
|
@ -149,8 +144,7 @@ websocket_frame(State, Req, HandlerState, Data, 0) ->
|
|||
websocket_frame(State, Req, HandlerState, _Data, _FrameType) ->
|
||||
websocket_close(State, Req, HandlerState, {error, badframe}).
|
||||
|
||||
-spec handler_call(State::#state{}, Req::#http_req{}, HandlerState::term(),
|
||||
RemainingData::binary(), Message::term(), NextState::fun()) -> ok.
|
||||
-spec handler_call(#state{}, #http_req{}, any(), binary(), any(), fun()) -> ok.
|
||||
handler_call(State=#state{handler=Handler}, Req, HandlerState,
|
||||
RemainingData, Message, NextState) ->
|
||||
case catch Handler:websocket_handle(Message, Req, HandlerState) of
|
||||
|
@ -165,19 +159,18 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
|
|||
websocket_close(State, Req, HandlerState, {error, handler})
|
||||
end.
|
||||
|
||||
-spec websocket_send(Data::binary(), Req::#http_req{}) -> ok.
|
||||
-spec websocket_send(binary(), #http_req{}) -> ok.
|
||||
websocket_send(Data, #http_req{socket=Socket, transport=Transport}) ->
|
||||
Transport:send(Socket, << 0, Data/binary, 255 >>).
|
||||
|
||||
-spec websocket_close(State::#state{}, Req::#http_req{},
|
||||
HandlerState::term(), Reason::{atom(), atom()}) -> ok.
|
||||
-spec websocket_close(#state{}, #http_req{}, any(), {atom(), atom()}) -> ok.
|
||||
websocket_close(State, Req=#http_req{socket=Socket, transport=Transport},
|
||||
HandlerState, Reason) ->
|
||||
Transport:send(Socket, << 255, 0 >>),
|
||||
Transport:close(Socket),
|
||||
handler_terminate(State, Req, HandlerState, Reason).
|
||||
|
||||
-spec handler_terminate(State::#state{}, Req::#http_req{},
|
||||
HandlerState::term(), Reason::atom() | {atom(), atom()}) -> ok.
|
||||
-spec handler_terminate(#state{}, #http_req{},
|
||||
any(), atom() | {atom(), atom()}) -> ok.
|
||||
handler_terminate(#state{handler=Handler}, Req, HandlerState, Reason) ->
|
||||
Handler:websocket_terminate(Reason, Req, HandlerState).
|
||||
|
|
|
@ -20,9 +20,8 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_link(NbAcceptors::non_neg_integer(), Transport::module(),
|
||||
TransOpts::term(), Protocol::module(), ProtoOpts::term())
|
||||
-> {ok, Pid::pid()}.
|
||||
-spec start_link(non_neg_integer(), module(), any(), module(), any())
|
||||
-> {ok, pid()}.
|
||||
start_link(NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) ->
|
||||
{ok, SupPid} = supervisor:start_link(?MODULE, []),
|
||||
{ok, ReqsPid} = supervisor:start_child(SupPid,
|
||||
|
@ -36,7 +35,6 @@ start_link(NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) ->
|
|||
|
||||
%% supervisor.
|
||||
|
||||
%% @todo These specs should be improved.
|
||||
-spec init([]) -> term().
|
||||
-spec init([]) -> {ok, {{one_for_one, 0, 1}, []}}.
|
||||
init([]) ->
|
||||
{ok, {{one_for_one, 0, 1}, []}}.
|
||||
|
|
|
@ -20,18 +20,17 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_link() -> {ok, Pid::pid()}.
|
||||
-spec start_link() -> {ok, pid()}.
|
||||
start_link() ->
|
||||
supervisor:start_link(?MODULE, []).
|
||||
|
||||
-spec start_request(Socket::inet:socket(), Transport::module(),
|
||||
Protocol::module(), Opts::term()) -> {ok, Pid::pid()}.
|
||||
-spec start_request(inet:socket(), module(), module(), any()) -> {ok, pid()}.
|
||||
start_request(Socket, Transport, Protocol, Opts) ->
|
||||
Protocol:start_link(Socket, Transport, Opts).
|
||||
|
||||
%% supervisor.
|
||||
|
||||
-spec init([]) -> term(). %% @todo These specs should be improved.
|
||||
-spec init([]) -> {ok, {{simple_one_for_one, 0, 1}, [{_, _, _, _, _, _}, ...]}}.
|
||||
init([]) ->
|
||||
{ok, {{simple_one_for_one, 0, 1}, [{?MODULE, {?MODULE, start_request, []},
|
||||
temporary, brutal_kill, worker, [?MODULE]}]}}.
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
-export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2,
|
||||
controlling_process/2, peername/1, close/1]). %% API.
|
||||
|
||||
-include_lib("kernel/include/inet.hrl").
|
||||
|
||||
%% API.
|
||||
|
||||
-spec name() -> ssl.
|
||||
|
@ -26,9 +24,9 @@ name() -> ssl.
|
|||
-spec messages() -> {ssl, ssl_closed, ssl_error}.
|
||||
messages() -> {ssl, ssl_closed, ssl_error}.
|
||||
|
||||
-spec listen([{port, Port::ip_port()} | {certfile, CertPath::string()}
|
||||
| {keyfile, KeyPath::string()} | {password, Password::string()}])
|
||||
-> {ok, LSocket::ssl:sslsocket()} | {error, Reason::atom()}.
|
||||
-spec listen([{port, inet:ip_port()} | {certfile, string()}
|
||||
| {keyfile, string()} | {password, string()}])
|
||||
-> {ok, ssl:sslsocket()} | {error, atom()}.
|
||||
listen(Opts) ->
|
||||
require([crypto, public_key, ssl]),
|
||||
{port, Port} = lists:keyfind(port, 1, Opts),
|
||||
|
@ -40,8 +38,8 @@ listen(Opts) ->
|
|||
{backlog, Backlog}, {packet, raw}, {reuseaddr, true},
|
||||
{certfile, CertFile}, {keyfile, KeyFile}, {password, Password}]).
|
||||
|
||||
-spec accept(LSocket::ssl:sslsocket(), Timeout::timeout())
|
||||
-> {ok, Socket::ssl:sslsocket()} | {error, Reason::closed | timeout | atom()}.
|
||||
-spec accept(ssl:sslsocket(), timeout())
|
||||
-> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
|
||||
accept(LSocket, Timeout) ->
|
||||
case ssl:transport_accept(LSocket, Timeout) of
|
||||
{ok, CSocket} ->
|
||||
|
@ -50,38 +48,36 @@ accept(LSocket, Timeout) ->
|
|||
{error, Reason}
|
||||
end.
|
||||
|
||||
-spec recv(Socket::ssl:sslsocket(), Length::integer(), Timeout::timeout())
|
||||
-> {ok, Packet::term()} | {error, Reason::closed | atom()}.
|
||||
-spec recv(ssl:sslsocket(), non_neg_integer(), timeout())
|
||||
-> {ok, any()} | {error, closed | atom()}.
|
||||
recv(Socket, Length, Timeout) ->
|
||||
ssl:recv(Socket, Length, Timeout).
|
||||
|
||||
-spec send(Socket::ssl:sslsocket(), Packet::iolist())
|
||||
-> ok | {error, Reason::atom()}.
|
||||
-spec send(ssl:sslsocket(), iolist()) -> ok | {error, atom()}.
|
||||
send(Socket, Packet) ->
|
||||
ssl:send(Socket, Packet).
|
||||
|
||||
-spec setopts(Socket::ssl:sslsocket(), Opts::list(term()))
|
||||
-> ok | {error, Reason::atom()}.
|
||||
-spec setopts(ssl:sslsocket(), list()) -> ok | {error, atom()}.
|
||||
setopts(Socket, Opts) ->
|
||||
ssl:setopts(Socket, Opts).
|
||||
|
||||
-spec controlling_process(Socket::ssl:sslsocket(), Pid::pid())
|
||||
-> ok | {error, Reason::closed | not_owner | atom()}.
|
||||
-spec controlling_process(ssl:sslsocket(), pid())
|
||||
-> ok | {error, closed | not_owner | atom()}.
|
||||
controlling_process(Socket, Pid) ->
|
||||
ssl:controlling_process(Socket, Pid).
|
||||
|
||||
-spec peername(Socket::ssl:sslsocket())
|
||||
-> {ok, {Address::ip_address(), Port::ip_port()}} | {error, atom()}.
|
||||
-spec peername(ssl:sslsocket())
|
||||
-> {ok, {inet:ip_address(), inet:ip_port()}} | {error, atom()}.
|
||||
peername(Socket) ->
|
||||
ssl:peername(Socket).
|
||||
|
||||
-spec close(Socket::ssl:sslsocket()) -> ok.
|
||||
-spec close(ssl:sslsocket()) -> ok.
|
||||
close(Socket) ->
|
||||
ssl:close(Socket).
|
||||
|
||||
%% Internal.
|
||||
|
||||
-spec require(Apps::list(module())) -> ok.
|
||||
-spec require(list(module())) -> ok.
|
||||
require([]) ->
|
||||
ok;
|
||||
require([App|Tail]) ->
|
||||
|
@ -91,12 +87,12 @@ require([App|Tail]) ->
|
|||
end,
|
||||
require(Tail).
|
||||
|
||||
-spec ssl_accept(CSocket::ssl:sslsocket(), Timeout::timeout())
|
||||
-> {ok, Socket::ssl:sslsocket()} | {error, Reason::closed | timeout | atom()}.
|
||||
ssl_accept(CSocket, Timeout) ->
|
||||
case ssl:ssl_accept(CSocket, Timeout) of
|
||||
-spec ssl_accept(ssl:sslsocket(), timeout())
|
||||
-> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
|
||||
ssl_accept(Socket, Timeout) ->
|
||||
case ssl:ssl_accept(Socket, Timeout) of
|
||||
ok ->
|
||||
{ok, CSocket};
|
||||
{ok, Socket};
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
|
|
|
@ -22,13 +22,13 @@
|
|||
|
||||
%% API.
|
||||
|
||||
-spec start_link() -> {ok, Pid::pid()}.
|
||||
-spec start_link() -> {ok, pid()}.
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []).
|
||||
|
||||
%% supervisor.
|
||||
|
||||
-spec init([]) -> term(). %% @todo These specs should be improved.
|
||||
-spec init([]) -> {ok, {{one_for_one, 10, 10}, [{_, _, _, _, _, _}, ...]}}.
|
||||
init([]) ->
|
||||
Procs = [{cowboy_clock, {cowboy_clock, start_link, []},
|
||||
permanent, 5000, worker, dynamic}],
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
-export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2,
|
||||
controlling_process/2, peername/1, close/1]). %% API.
|
||||
|
||||
-include_lib("kernel/include/inet.hrl").
|
||||
|
||||
%% API.
|
||||
|
||||
-spec name() -> tcp.
|
||||
|
@ -26,45 +24,41 @@ name() -> tcp.
|
|||
-spec messages() -> {tcp, tcp_closed, tcp_error}.
|
||||
messages() -> {tcp, tcp_closed, tcp_error}.
|
||||
|
||||
-spec listen([{port, Port::ip_port()}])
|
||||
-> {ok, LSocket::inet:socket()} | {error, Reason::atom()}.
|
||||
-spec listen([{port, inet:ip_port()}]) -> {ok, inet:socket()} | {error, atom()}.
|
||||
listen(Opts) ->
|
||||
{port, Port} = lists:keyfind(port, 1, Opts),
|
||||
Backlog = proplists:get_value(backlog, Opts, 1024),
|
||||
gen_tcp:listen(Port, [binary, {active, false},
|
||||
{backlog, Backlog}, {packet, raw}, {reuseaddr, true}]).
|
||||
|
||||
-spec accept(LSocket::inet:socket(), Timeout::timeout())
|
||||
-> {ok, Socket::inet:socket()}
|
||||
| {error, Reason::closed | timeout | atom()}.
|
||||
-spec accept(inet:socket(), timeout())
|
||||
-> {ok, inet:socket()} | {error, closed | timeout | atom()}.
|
||||
accept(LSocket, Timeout) ->
|
||||
gen_tcp:accept(LSocket, Timeout).
|
||||
|
||||
-spec recv(Socket::inet:socket(), Length::integer(), Timeout::timeout())
|
||||
-> {ok, Packet::term()} | {error, Reason::closed | atom()}.
|
||||
-spec recv(inet:socket(), non_neg_integer(), timeout())
|
||||
-> {ok, any()} | {error, closed | atom()}.
|
||||
recv(Socket, Length, Timeout) ->
|
||||
gen_tcp:recv(Socket, Length, Timeout).
|
||||
|
||||
-spec send(Socket::inet:socket(), Packet::iolist())
|
||||
-> ok | {error, Reason::atom()}.
|
||||
-spec send(inet:socket(), iolist()) -> ok | {error, atom()}.
|
||||
send(Socket, Packet) ->
|
||||
gen_tcp:send(Socket, Packet).
|
||||
|
||||
-spec setopts(Socket::inet:socket(), Opts::list(term()))
|
||||
-> ok | {error, Reason::atom()}.
|
||||
-spec setopts(inet:socket(), list()) -> ok | {error, atom()}.
|
||||
setopts(Socket, Opts) ->
|
||||
inet:setopts(Socket, Opts).
|
||||
|
||||
-spec controlling_process(Socket::inet:socket(), Pid::pid())
|
||||
-> ok | {error, Reason::closed | not_owner | atom()}.
|
||||
-spec controlling_process(inet:socket(), pid())
|
||||
-> ok | {error, closed | not_owner | atom()}.
|
||||
controlling_process(Socket, Pid) ->
|
||||
gen_tcp:controlling_process(Socket, Pid).
|
||||
|
||||
-spec peername(Socket::inet:socket())
|
||||
-> {ok, {Address::ip_address(), Port::ip_port()}} | {error, atom()}.
|
||||
-spec peername(inet:socket())
|
||||
-> {ok, {inet:ip_address(), inet:ip_port()}} | {error, atom()}.
|
||||
peername(Socket) ->
|
||||
inet:peername(Socket).
|
||||
|
||||
-spec close(Socket::inet:socket()) -> ok.
|
||||
-spec close(inet:socket()) -> ok.
|
||||
close(Socket) ->
|
||||
gen_tcp:close(Socket).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue