2017-01-02 19:36:36 +01:00
|
|
|
%% Copyright (c) 2011-2017, Loïc Hoguin <essen@ninenines.eu>
|
2011-04-14 21:21:17 +02:00
|
|
|
%%
|
|
|
|
%% Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
%% purpose with or without fee is hereby granted, provided that the above
|
|
|
|
%% copyright notice and this permission notice appear in all copies.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2013-01-10 21:58:38 +01:00
|
|
|
%% Cowboy supports versions 7 through 17 of the Websocket drafts.
|
|
|
|
%% It also supports RFC6455, the proposed standard for Websocket.
|
2012-08-27 14:00:28 +02:00
|
|
|
-module(cowboy_websocket).
|
2013-02-15 00:53:40 +00:00
|
|
|
-behaviour(cowboy_sub_protocol).
|
2011-07-06 17:42:20 +02:00
|
|
|
|
2018-06-27 17:52:25 +02:00
|
|
|
-ifdef(OTP_RELEASE).
|
|
|
|
-compile({nowarn_deprecated_function, [{erlang, get_stacktrace, 0}]}).
|
|
|
|
-endif.
|
|
|
|
|
2018-04-04 17:23:37 +02:00
|
|
|
-export([is_upgrade_request/1]).
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
-export([upgrade/4]).
|
|
|
|
-export([upgrade/5]).
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
-export([takeover/7]).
|
2018-03-23 16:32:53 +01:00
|
|
|
-export([loop/3]).
|
2016-08-12 19:27:23 +02:00
|
|
|
|
2018-03-13 11:19:13 +01:00
|
|
|
-export([system_continue/3]).
|
|
|
|
-export([system_terminate/4]).
|
|
|
|
-export([system_code_change/4]).
|
|
|
|
|
2018-11-13 15:55:09 +01:00
|
|
|
-type commands() :: [cow_ws:frame()
|
|
|
|
| {active, boolean()}
|
|
|
|
| {deflate, boolean()}
|
2018-11-16 13:48:15 +01:00
|
|
|
| {set_options, map()}
|
2018-11-13 15:55:09 +01:00
|
|
|
].
|
2018-09-11 14:33:58 +02:00
|
|
|
-export_type([commands/0]).
|
|
|
|
|
|
|
|
-type call_result(State) :: {commands(), State} | {commands(), State, hibernate}.
|
|
|
|
|
|
|
|
-type deprecated_call_result(State) :: {ok, State}
|
2016-08-12 19:27:23 +02:00
|
|
|
| {ok, State, hibernate}
|
|
|
|
| {reply, cow_ws:frame() | [cow_ws:frame()], State}
|
|
|
|
| {reply, cow_ws:frame() | [cow_ws:frame()], State, hibernate}
|
|
|
|
| {stop, State}.
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2014-11-07 19:22:36 +02:00
|
|
|
-type terminate_reason() :: normal | stop | timeout
|
2015-02-16 15:48:04 +01:00
|
|
|
| remote | {remote, cow_ws:close_code(), binary()}
|
2014-09-30 20:12:13 +03:00
|
|
|
| {error, badencoding | badframe | closed | atom()}
|
|
|
|
| {crash, error | exit | throw, any()}.
|
|
|
|
|
|
|
|
-callback init(Req, any())
|
|
|
|
-> {ok | module(), Req, any()}
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
| {module(), Req, any(), any()}
|
2014-09-30 20:12:13 +03:00
|
|
|
when Req::cowboy_req:req().
|
2016-08-11 11:06:03 +02:00
|
|
|
|
2016-08-12 19:27:23 +02:00
|
|
|
-callback websocket_init(State)
|
2018-09-11 14:33:58 +02:00
|
|
|
-> call_result(State) | deprecated_call_result(State) when State::any().
|
2016-08-12 19:27:23 +02:00
|
|
|
-optional_callbacks([websocket_init/1]).
|
2016-08-11 11:06:03 +02:00
|
|
|
|
2018-06-26 10:59:22 +02:00
|
|
|
-callback websocket_handle(ping | pong | {text | binary | ping | pong, binary()}, State)
|
2018-09-11 14:33:58 +02:00
|
|
|
-> call_result(State) | deprecated_call_result(State) when State::any().
|
2016-08-12 19:27:23 +02:00
|
|
|
-callback websocket_info(any(), State)
|
2018-09-11 14:33:58 +02:00
|
|
|
-> call_result(State) | deprecated_call_result(State) when State::any().
|
2015-07-27 23:58:58 +02:00
|
|
|
|
|
|
|
-callback terminate(any(), cowboy_req:req(), any()) -> ok.
|
|
|
|
-optional_callbacks([terminate/3]).
|
2012-04-01 18:58:28 -07:00
|
|
|
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
-type opts() :: #{
|
2017-05-28 19:04:16 +02:00
|
|
|
compress => boolean(),
|
2018-11-12 18:12:44 +01:00
|
|
|
deflate_opts => cow_ws:deflate_opts(),
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
idle_timeout => timeout(),
|
2017-08-25 12:08:26 +03:00
|
|
|
max_frame_size => non_neg_integer() | infinity,
|
2017-05-28 19:04:16 +02:00
|
|
|
req_filter => fun((cowboy_req:req()) -> map())
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
}.
|
|
|
|
-export_type([opts/0]).
|
|
|
|
|
2011-04-14 21:21:17 +02:00
|
|
|
-record(state, {
|
2018-03-14 17:43:13 +01:00
|
|
|
parent :: undefined | pid(),
|
2018-03-13 11:19:13 +01:00
|
|
|
ref :: ranch:ref(),
|
2018-04-04 17:34:10 +02:00
|
|
|
socket = undefined :: inet:socket() | {pid(), cowboy_stream:streamid()} | undefined,
|
|
|
|
transport = undefined :: module() | undefined,
|
2018-11-12 18:12:44 +01:00
|
|
|
opts = #{} :: opts(),
|
2018-09-21 14:04:20 +02:00
|
|
|
active = true :: boolean(),
|
2011-04-14 21:21:17 +02:00
|
|
|
handler :: module(),
|
2013-01-10 21:58:38 +01:00
|
|
|
key = undefined :: undefined | binary(),
|
2011-09-22 21:33:56 +02:00
|
|
|
timeout_ref = undefined :: undefined | reference(),
|
2011-06-06 13:48:41 +02:00
|
|
|
messages = undefined :: undefined | {atom(), atom(), atom()},
|
2011-08-23 16:24:02 +02:00
|
|
|
hibernate = false :: boolean(),
|
2015-02-16 15:48:04 +01:00
|
|
|
frag_state = undefined :: cow_ws:frag_state(),
|
|
|
|
frag_buffer = <<>> :: binary(),
|
2015-03-06 01:56:30 +01:00
|
|
|
utf8_state = 0 :: cow_ws:utf8_state(),
|
2018-11-13 15:55:09 +01:00
|
|
|
deflate = true :: boolean(),
|
2017-05-28 19:04:16 +02:00
|
|
|
extensions = #{} :: map(),
|
|
|
|
req = #{} :: map()
|
2011-04-14 21:21:17 +02:00
|
|
|
}).
|
|
|
|
|
2018-04-04 17:23:37 +02:00
|
|
|
%% Because the HTTP/1.1 and HTTP/2 handshakes are so different,
|
|
|
|
%% this function is necessary to figure out whether a request
|
|
|
|
%% is trying to upgrade to the Websocket protocol.
|
|
|
|
|
|
|
|
-spec is_upgrade_request(cowboy_req:req()) -> boolean().
|
|
|
|
is_upgrade_request(#{version := 'HTTP/2', method := <<"CONNECT">>, protocol := Protocol}) ->
|
|
|
|
<<"websocket">> =:= cowboy_bstr:to_lower(Protocol);
|
|
|
|
is_upgrade_request(Req=#{version := 'HTTP/1.1', method := <<"GET">>}) ->
|
|
|
|
ConnTokens = cowboy_req:parse_header(<<"connection">>, Req, []),
|
|
|
|
case lists:member(<<"upgrade">>, ConnTokens) of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
true ->
|
|
|
|
UpgradeTokens = cowboy_req:parse_header(<<"upgrade">>, Req),
|
|
|
|
lists:member(<<"websocket">>, UpgradeTokens)
|
|
|
|
end;
|
|
|
|
is_upgrade_request(_) ->
|
|
|
|
false.
|
|
|
|
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
%% Stream process.
|
|
|
|
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
-spec upgrade(Req, Env, module(), any())
|
|
|
|
-> {ok, Req, Env}
|
|
|
|
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
|
|
|
upgrade(Req, Env, Handler, HandlerState) ->
|
|
|
|
upgrade(Req, Env, Handler, HandlerState, #{}).
|
|
|
|
|
|
|
|
-spec upgrade(Req, Env, module(), any(), opts())
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
-> {ok, Req, Env}
|
2013-01-03 22:47:51 +01:00
|
|
|
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
2016-08-12 16:56:08 +02:00
|
|
|
%% @todo Immediately crash if a response has already been sent.
|
2018-04-04 17:23:37 +02:00
|
|
|
upgrade(Req0=#{version := Version}, Env, Handler, HandlerState, Opts) ->
|
2017-05-28 19:04:16 +02:00
|
|
|
FilteredReq = case maps:get(req_filter, Opts, undefined) of
|
|
|
|
undefined -> maps:with([method, version, scheme, host, port, path, qs, peer], Req0);
|
|
|
|
FilterFun -> FilterFun(Req0)
|
|
|
|
end,
|
2018-11-12 18:12:44 +01:00
|
|
|
State0 = #state{opts=Opts, handler=Handler, req=FilteredReq},
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
try websocket_upgrade(State0, Req0) of
|
2016-08-12 16:56:08 +02:00
|
|
|
{ok, State, Req} ->
|
2017-12-06 17:31:32 +01:00
|
|
|
websocket_handshake(State, Req, HandlerState, Env);
|
2018-04-04 17:23:37 +02:00
|
|
|
%% The status code 426 is specific to HTTP/1.1 connections.
|
|
|
|
{error, upgrade_required} when Version =:= 'HTTP/1.1' ->
|
2017-12-06 17:31:32 +01:00
|
|
|
{ok, cowboy_req:reply(426, #{
|
|
|
|
<<"connection">> => <<"upgrade">>,
|
|
|
|
<<"upgrade">> => <<"websocket">>
|
2018-04-04 17:23:37 +02:00
|
|
|
}, Req0), Env};
|
|
|
|
%% Use a generic 400 error for HTTP/2.
|
|
|
|
{error, upgrade_required} ->
|
|
|
|
{ok, cowboy_req:reply(400, Req0), Env}
|
2013-08-24 20:36:23 +02:00
|
|
|
catch _:_ ->
|
2016-08-12 16:56:08 +02:00
|
|
|
%% @todo Probably log something here?
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
%% @todo Test that we can have 2 /ws 400 status code in a row on the same connection.
|
2016-08-10 17:15:02 +02:00
|
|
|
%% @todo Does this even work?
|
2016-08-12 16:56:08 +02:00
|
|
|
{ok, cowboy_req:reply(400, Req0), Env}
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2018-04-04 17:23:37 +02:00
|
|
|
websocket_upgrade(State, Req=#{version := Version}) ->
|
|
|
|
case is_upgrade_request(Req) of
|
2017-12-06 17:31:32 +01:00
|
|
|
false ->
|
|
|
|
{error, upgrade_required};
|
2018-04-04 17:23:37 +02:00
|
|
|
true when Version =:= 'HTTP/1.1' ->
|
|
|
|
Key = cowboy_req:header(<<"sec-websocket-key">>, Req),
|
|
|
|
false = Key =:= undefined,
|
|
|
|
websocket_version(State#state{key=Key}, Req);
|
2017-12-06 17:31:32 +01:00
|
|
|
true ->
|
2018-04-04 17:23:37 +02:00
|
|
|
websocket_version(State, Req)
|
2017-12-06 17:31:32 +01:00
|
|
|
end.
|
2013-04-15 18:36:33 +02:00
|
|
|
|
2018-04-04 17:23:37 +02:00
|
|
|
websocket_version(State, Req) ->
|
|
|
|
WsVersion = cowboy_req:parse_header(<<"sec-websocket-version">>, Req),
|
|
|
|
case WsVersion of
|
|
|
|
7 -> ok;
|
|
|
|
8 -> ok;
|
|
|
|
13 -> ok
|
|
|
|
end,
|
|
|
|
websocket_extensions(State, Req#{websocket_version => WsVersion}).
|
|
|
|
|
2018-11-12 18:12:44 +01:00
|
|
|
websocket_extensions(State=#state{opts=Opts}, Req) ->
|
2016-03-06 18:09:43 +01:00
|
|
|
%% @todo We want different options for this. For example
|
|
|
|
%% * compress everything auto
|
|
|
|
%% * compress only text auto
|
|
|
|
%% * compress only binary auto
|
|
|
|
%% * compress nothing auto (but still enabled it)
|
|
|
|
%% * disable compression
|
2018-11-12 18:12:44 +01:00
|
|
|
Compress = maps:get(compress, Opts, false),
|
2016-08-12 16:56:08 +02:00
|
|
|
case {Compress, cowboy_req:parse_header(<<"sec-websocket-extensions">>, Req)} of
|
2015-03-06 01:56:30 +01:00
|
|
|
{true, Extensions} when Extensions =/= undefined ->
|
2016-08-12 16:56:08 +02:00
|
|
|
websocket_extensions(State, Req, Extensions, []);
|
2015-03-06 01:56:30 +01:00
|
|
|
_ ->
|
2016-08-12 16:56:08 +02:00
|
|
|
{ok, State, Req}
|
2013-04-15 18:36:33 +02:00
|
|
|
end.
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2015-03-06 01:56:30 +01:00
|
|
|
websocket_extensions(State, Req, [], []) ->
|
|
|
|
{ok, State, Req};
|
|
|
|
websocket_extensions(State, Req, [], [<<", ">>|RespHeader]) ->
|
|
|
|
{ok, State, cowboy_req:set_resp_header(<<"sec-websocket-extensions">>, lists:reverse(RespHeader), Req)};
|
2018-04-04 17:23:37 +02:00
|
|
|
%% For HTTP/2 we ARE on the controlling process and do NOT want to update the owner.
|
2018-11-12 18:12:44 +01:00
|
|
|
websocket_extensions(State=#state{opts=Opts, extensions=Extensions},
|
|
|
|
Req=#{pid := Pid, version := Version},
|
2016-08-12 16:56:08 +02:00
|
|
|
[{<<"permessage-deflate">>, Params}|Tail], RespHeader) ->
|
2018-11-12 18:12:44 +01:00
|
|
|
DeflateOpts0 = maps:get(deflate_opts, Opts, #{}),
|
|
|
|
DeflateOpts = case Version of
|
|
|
|
'HTTP/1.1' -> DeflateOpts0#{owner => Pid};
|
|
|
|
_ -> DeflateOpts0
|
2018-04-04 17:23:37 +02:00
|
|
|
end,
|
2018-11-12 18:12:44 +01:00
|
|
|
try cow_ws:negotiate_permessage_deflate(Params, Extensions, DeflateOpts) of
|
2015-03-06 01:56:30 +01:00
|
|
|
{ok, RespExt, Extensions2} ->
|
|
|
|
websocket_extensions(State#state{extensions=Extensions2},
|
2016-08-12 16:56:08 +02:00
|
|
|
Req, Tail, [<<", ">>, RespExt|RespHeader]);
|
2015-03-06 01:56:30 +01:00
|
|
|
ignore ->
|
|
|
|
websocket_extensions(State, Req, Tail, RespHeader)
|
2017-11-01 15:41:52 +00:00
|
|
|
catch exit:{error, incompatible_zlib_version, _} ->
|
|
|
|
websocket_extensions(State, Req, Tail, RespHeader)
|
2015-03-06 01:56:30 +01:00
|
|
|
end;
|
2018-11-12 18:12:44 +01:00
|
|
|
websocket_extensions(State=#state{opts=Opts, extensions=Extensions},
|
|
|
|
Req=#{pid := Pid, version := Version},
|
2016-08-12 16:56:08 +02:00
|
|
|
[{<<"x-webkit-deflate-frame">>, Params}|Tail], RespHeader) ->
|
2018-11-12 18:12:44 +01:00
|
|
|
DeflateOpts0 = maps:get(deflate_opts, Opts, #{}),
|
|
|
|
DeflateOpts = case Version of
|
|
|
|
'HTTP/1.1' -> DeflateOpts0#{owner => Pid};
|
|
|
|
_ -> DeflateOpts0
|
2018-04-04 17:23:37 +02:00
|
|
|
end,
|
2018-11-12 18:12:44 +01:00
|
|
|
try cow_ws:negotiate_x_webkit_deflate_frame(Params, Extensions, DeflateOpts) of
|
2015-03-06 01:56:30 +01:00
|
|
|
{ok, RespExt, Extensions2} ->
|
|
|
|
websocket_extensions(State#state{extensions=Extensions2},
|
2016-08-12 16:56:08 +02:00
|
|
|
Req, Tail, [<<", ">>, RespExt|RespHeader]);
|
2015-03-06 01:56:30 +01:00
|
|
|
ignore ->
|
|
|
|
websocket_extensions(State, Req, Tail, RespHeader)
|
2017-11-01 15:41:52 +00:00
|
|
|
catch exit:{error, incompatible_zlib_version, _} ->
|
|
|
|
websocket_extensions(State, Req, Tail, RespHeader)
|
2015-03-06 01:56:30 +01:00
|
|
|
end;
|
|
|
|
websocket_extensions(State, Req, [_|Tail], RespHeader) ->
|
|
|
|
websocket_extensions(State, Req, Tail, RespHeader).
|
|
|
|
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
-spec websocket_handshake(#state{}, Req, any(), Env)
|
|
|
|
-> {ok, Req, Env}
|
|
|
|
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
2016-03-06 18:09:43 +01:00
|
|
|
websocket_handshake(State=#state{key=Key},
|
2018-04-04 17:23:37 +02:00
|
|
|
Req=#{version := 'HTTP/1.1', pid := Pid, streamid := StreamID},
|
|
|
|
HandlerState, Env) ->
|
2014-07-12 14:19:29 +02:00
|
|
|
Challenge = base64:encode(crypto:hash(sha,
|
2013-01-10 21:58:38 +01:00
|
|
|
<< Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" >>)),
|
2017-10-31 17:15:11 +00:00
|
|
|
%% @todo We don't want date and server headers.
|
2016-08-12 17:01:01 +02:00
|
|
|
Headers = cowboy_req:response_headers(#{
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
<<"connection">> => <<"Upgrade">>,
|
|
|
|
<<"upgrade">> => <<"websocket">>,
|
|
|
|
<<"sec-websocket-accept">> => Challenge
|
2016-08-12 17:01:01 +02:00
|
|
|
}, Req),
|
2016-08-12 19:27:23 +02:00
|
|
|
Pid ! {{Pid, StreamID}, {switch_protocol, Headers, ?MODULE, {State, HandlerState}}},
|
2018-04-04 17:23:37 +02:00
|
|
|
{ok, Req, Env};
|
|
|
|
%% For HTTP/2 we do not let the process die, we instead keep it
|
|
|
|
%% for the Websocket stream. This is because in HTTP/2 we only
|
|
|
|
%% have a stream, it doesn't take over the whole connection.
|
|
|
|
websocket_handshake(State, Req=#{ref := Ref, pid := Pid, streamid := StreamID},
|
|
|
|
HandlerState, _Env) ->
|
|
|
|
%% @todo We don't want date and server headers.
|
|
|
|
Headers = cowboy_req:response_headers(#{}, Req),
|
|
|
|
Pid ! {{Pid, StreamID}, {switch_protocol, Headers, ?MODULE, {State, HandlerState}}},
|
|
|
|
takeover(Pid, Ref, {Pid, StreamID}, undefined, undefined, <<>>,
|
|
|
|
{State, HandlerState}).
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
|
|
|
|
%% Connection process.
|
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
-record(ps_header, {
|
|
|
|
buffer = <<>> :: binary()
|
|
|
|
}).
|
|
|
|
|
|
|
|
-record(ps_payload, {
|
|
|
|
type :: cow_ws:frame_type(),
|
|
|
|
len :: non_neg_integer(),
|
|
|
|
mask_key :: cow_ws:mask_key(),
|
|
|
|
rsv :: cow_ws:rsv(),
|
|
|
|
close_code = undefined :: undefined | cow_ws:close_code(),
|
|
|
|
unmasked = <<>> :: binary(),
|
|
|
|
unmasked_len = 0 :: non_neg_integer(),
|
|
|
|
buffer = <<>> :: binary()
|
|
|
|
}).
|
|
|
|
|
|
|
|
-type parse_state() :: #ps_header{} | #ps_payload{}.
|
|
|
|
|
2018-04-04 17:23:37 +02:00
|
|
|
-spec takeover(pid(), ranch:ref(), inet:socket() | {pid(), cowboy_stream:streamid()},
|
|
|
|
module() | undefined, any(), binary(),
|
2018-03-23 16:32:53 +01:00
|
|
|
{#state{}, any()}) -> no_return().
|
2018-03-13 11:19:13 +01:00
|
|
|
takeover(Parent, Ref, Socket, Transport, _Opts, Buffer,
|
2016-08-15 19:21:38 +02:00
|
|
|
{State0=#state{handler=Handler}, HandlerState}) ->
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
%% @todo We should have an option to disable this behavior.
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
ranch:remove_connection(Ref),
|
2018-04-04 17:23:37 +02:00
|
|
|
Messages = case Transport of
|
|
|
|
undefined -> undefined;
|
|
|
|
_ -> Transport:messages()
|
|
|
|
end,
|
2018-03-23 16:32:53 +01:00
|
|
|
State = loop_timeout(State0#state{parent=Parent,
|
|
|
|
ref=Ref, socket=Socket, transport=Transport,
|
2018-04-04 17:23:37 +02:00
|
|
|
key=undefined, messages=Messages}),
|
2019-10-05 13:04:21 +02:00
|
|
|
%% We call parse_header/3 immediately because there might be
|
|
|
|
%% some data in the buffer that was sent along with the handshake.
|
|
|
|
%% While it is not allowed by the protocol to send frames immediately,
|
|
|
|
%% we still want to process that data if any.
|
2016-08-15 19:21:38 +02:00
|
|
|
case erlang:function_exported(Handler, websocket_init, 1) of
|
2018-03-23 16:32:53 +01:00
|
|
|
true -> handler_call(State, HandlerState, #ps_header{buffer=Buffer},
|
2019-10-05 13:04:21 +02:00
|
|
|
websocket_init, undefined, fun parse_header/3);
|
|
|
|
false -> parse_header(State, HandlerState, #ps_header{buffer=Buffer})
|
2016-08-15 19:21:38 +02:00
|
|
|
end.
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2018-09-21 14:04:20 +02:00
|
|
|
before_loop(State=#state{active=false}, HandlerState, ParseState) ->
|
|
|
|
loop(State, HandlerState, ParseState);
|
2018-04-04 17:23:37 +02:00
|
|
|
%% @todo We probably shouldn't do the setopts if we have not received a socket message.
|
|
|
|
%% @todo We need to hibernate when HTTP/2 is used too.
|
|
|
|
before_loop(State=#state{socket=Stream={Pid, _}, transport=undefined},
|
|
|
|
HandlerState, ParseState) ->
|
|
|
|
%% @todo Keep Ref around.
|
|
|
|
ReadBodyRef = make_ref(),
|
2019-10-02 19:12:05 +02:00
|
|
|
Pid ! {Stream, {read_body, self(), ReadBodyRef, auto, infinity}},
|
2018-04-04 17:23:37 +02:00
|
|
|
loop(State, HandlerState, ParseState);
|
2018-03-23 16:32:53 +01:00
|
|
|
before_loop(State=#state{socket=Socket, transport=Transport, hibernate=true},
|
|
|
|
HandlerState, ParseState) ->
|
2011-04-14 21:21:17 +02:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
2018-03-23 16:32:53 +01:00
|
|
|
proc_lib:hibernate(?MODULE, loop,
|
|
|
|
[State#state{hibernate=false}, HandlerState, ParseState]);
|
|
|
|
before_loop(State=#state{socket=Socket, transport=Transport},
|
|
|
|
HandlerState, ParseState) ->
|
2011-06-06 18:15:36 +02:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
2018-03-23 16:32:53 +01:00
|
|
|
loop(State, HandlerState, ParseState).
|
2011-09-22 21:33:56 +02:00
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
-spec loop_timeout(#state{}) -> #state{}.
|
2018-11-12 18:12:44 +01:00
|
|
|
loop_timeout(State=#state{opts=Opts, timeout_ref=PrevRef}) ->
|
|
|
|
_ = case PrevRef of
|
|
|
|
undefined -> ignore;
|
|
|
|
PrevRef -> erlang:cancel_timer(PrevRef)
|
|
|
|
end,
|
|
|
|
case maps:get(idle_timeout, Opts, 60000) of
|
|
|
|
infinity ->
|
|
|
|
State#state{timeout_ref=undefined};
|
|
|
|
Timeout ->
|
|
|
|
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
|
|
|
|
State#state{timeout_ref=TRef}
|
|
|
|
end.
|
2011-06-06 18:15:36 +02:00
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
-spec loop(#state{}, any(), parse_state()) -> no_return().
|
2018-04-04 17:23:37 +02:00
|
|
|
loop(State=#state{parent=Parent, socket=Socket, messages=Messages,
|
2018-03-23 16:32:53 +01:00
|
|
|
timeout_ref=TRef}, HandlerState, ParseState) ->
|
2011-04-14 21:21:17 +02:00
|
|
|
receive
|
2018-04-04 17:23:37 +02:00
|
|
|
%% Socket messages. (HTTP/1.1)
|
|
|
|
{OK, Socket, Data} when OK =:= element(1, Messages) ->
|
2018-03-23 16:32:53 +01:00
|
|
|
State2 = loop_timeout(State),
|
|
|
|
parse(State2, HandlerState, ParseState, Data);
|
2018-04-04 17:23:37 +02:00
|
|
|
{Closed, Socket} when Closed =:= element(2, Messages) ->
|
2017-02-18 20:21:02 +01:00
|
|
|
terminate(State, HandlerState, {error, closed});
|
2018-04-04 17:23:37 +02:00
|
|
|
{Error, Socket, Reason} when Error =:= element(3, Messages) ->
|
2017-02-18 20:21:02 +01:00
|
|
|
terminate(State, HandlerState, {error, Reason});
|
2018-04-04 17:23:37 +02:00
|
|
|
%% Body reading messages. (HTTP/2)
|
|
|
|
{request_body, _Ref, nofin, Data} ->
|
|
|
|
State2 = loop_timeout(State),
|
|
|
|
parse(State2, HandlerState, ParseState, Data);
|
|
|
|
%% @todo We need to handle this case as if it was an {error, closed}
|
|
|
|
%% but not before we finish processing frames. We probably should have
|
|
|
|
%% a check in before_loop to let us stop looping if a flag is set.
|
|
|
|
{request_body, _Ref, fin, _, Data} ->
|
|
|
|
State2 = loop_timeout(State),
|
|
|
|
parse(State2, HandlerState, ParseState, Data);
|
|
|
|
%% Timeouts.
|
2012-04-24 11:38:27 -07:00
|
|
|
{timeout, TRef, ?MODULE} ->
|
2016-08-12 19:27:23 +02:00
|
|
|
websocket_close(State, HandlerState, timeout);
|
2012-04-24 11:38:27 -07:00
|
|
|
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
|
2018-04-04 17:23:37 +02:00
|
|
|
%% @todo This should call before_loop.
|
2018-03-23 16:32:53 +01:00
|
|
|
loop(State, HandlerState, ParseState);
|
2018-03-13 11:19:13 +01:00
|
|
|
%% System messages.
|
|
|
|
{'EXIT', Parent, Reason} ->
|
|
|
|
%% @todo We should exit gracefully.
|
|
|
|
exit(Reason);
|
|
|
|
{system, From, Request} ->
|
|
|
|
sys:handle_system_msg(Request, From, Parent, ?MODULE, [],
|
2018-03-23 16:32:53 +01:00
|
|
|
{State, HandlerState, ParseState});
|
2018-03-13 10:40:14 +01:00
|
|
|
%% Calls from supervisor module.
|
|
|
|
{'$gen_call', From, Call} ->
|
|
|
|
cowboy_children:handle_supervisor_call(Call, From, [], ?MODULE),
|
2018-04-04 17:23:37 +02:00
|
|
|
%% @todo This should call before_loop.
|
2018-03-23 16:32:53 +01:00
|
|
|
loop(State, HandlerState, ParseState);
|
2011-04-14 21:21:17 +02:00
|
|
|
Message ->
|
2018-03-23 16:32:53 +01:00
|
|
|
handler_call(State, HandlerState, ParseState,
|
|
|
|
websocket_info, Message, fun before_loop/3)
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
parse(State, HandlerState, PS=#ps_header{buffer=Buffer}, Data) ->
|
|
|
|
parse_header(State, HandlerState, PS#ps_header{
|
|
|
|
buffer= <<Buffer/binary, Data/binary>>});
|
|
|
|
parse(State, HandlerState, PS=#ps_payload{buffer=Buffer}, Data) ->
|
|
|
|
parse_payload(State, HandlerState, PS#ps_payload{buffer= <<>>},
|
|
|
|
<<Buffer/binary, Data/binary>>).
|
|
|
|
|
2018-11-12 18:12:44 +01:00
|
|
|
parse_header(State=#state{opts=Opts, frag_state=FragState, extensions=Extensions},
|
2017-08-25 12:08:26 +03:00
|
|
|
HandlerState, ParseState=#ps_header{buffer=Data}) ->
|
2018-11-12 18:12:44 +01:00
|
|
|
MaxFrameSize = maps:get(max_frame_size, Opts, infinity),
|
2015-02-16 15:48:04 +01:00
|
|
|
case cow_ws:parse_header(Data, Extensions, FragState) of
|
|
|
|
%% All frames sent from the client to the server are masked.
|
|
|
|
{_, _, _, _, undefined, _} ->
|
2016-08-12 19:27:23 +02:00
|
|
|
websocket_close(State, HandlerState, {error, badframe});
|
2017-08-25 12:08:26 +03:00
|
|
|
{_, _, _, Len, _, _} when Len > MaxFrameSize ->
|
|
|
|
websocket_close(State, HandlerState, {error, badsize});
|
2015-02-16 15:48:04 +01:00
|
|
|
{Type, FragState2, Rsv, Len, MaskKey, Rest} ->
|
2018-03-23 16:32:53 +01:00
|
|
|
parse_payload(State#state{frag_state=FragState2}, HandlerState,
|
|
|
|
#ps_payload{type=Type, len=Len, mask_key=MaskKey, rsv=Rsv}, Rest);
|
2015-02-16 15:48:04 +01:00
|
|
|
more ->
|
2018-03-23 16:32:53 +01:00
|
|
|
before_loop(State, HandlerState, ParseState);
|
2015-02-16 15:48:04 +01:00
|
|
|
error ->
|
2016-08-12 19:27:23 +02:00
|
|
|
websocket_close(State, HandlerState, {error, badframe})
|
2015-02-16 15:48:04 +01:00
|
|
|
end.
|
2013-01-12 22:31:23 +01:00
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
parse_payload(State=#state{frag_state=FragState, utf8_state=Incomplete, extensions=Extensions},
|
|
|
|
HandlerState, ParseState=#ps_payload{
|
|
|
|
type=Type, len=Len, mask_key=MaskKey, rsv=Rsv,
|
|
|
|
unmasked=Unmasked, unmasked_len=UnmaskedLen}, Data) ->
|
|
|
|
case cow_ws:parse_payload(Data, MaskKey, Incomplete, UnmaskedLen,
|
|
|
|
Type, Len, FragState, Extensions, Rsv) of
|
|
|
|
{ok, CloseCode, Payload, Utf8State, Rest} ->
|
|
|
|
dispatch_frame(State#state{utf8_state=Utf8State}, HandlerState,
|
|
|
|
ParseState#ps_payload{unmasked= <<Unmasked/binary, Payload/binary>>,
|
|
|
|
close_code=CloseCode}, Rest);
|
2015-02-16 15:48:04 +01:00
|
|
|
{ok, Payload, Utf8State, Rest} ->
|
2018-03-23 16:32:53 +01:00
|
|
|
dispatch_frame(State#state{utf8_state=Utf8State}, HandlerState,
|
|
|
|
ParseState#ps_payload{unmasked= <<Unmasked/binary, Payload/binary>>},
|
|
|
|
Rest);
|
|
|
|
{more, CloseCode, Payload, Utf8State} ->
|
|
|
|
before_loop(State#state{utf8_state=Utf8State}, HandlerState,
|
|
|
|
ParseState#ps_payload{len=Len - byte_size(Data), close_code=CloseCode,
|
|
|
|
unmasked= <<Unmasked/binary, Payload/binary>>,
|
|
|
|
unmasked_len=UnmaskedLen + byte_size(Data)});
|
2015-02-16 15:48:04 +01:00
|
|
|
{more, Payload, Utf8State} ->
|
2018-03-23 16:32:53 +01:00
|
|
|
before_loop(State#state{utf8_state=Utf8State}, HandlerState,
|
|
|
|
ParseState#ps_payload{len=Len - byte_size(Data),
|
|
|
|
unmasked= <<Unmasked/binary, Payload/binary>>,
|
|
|
|
unmasked_len=UnmaskedLen + byte_size(Data)});
|
2015-03-06 01:56:30 +01:00
|
|
|
Error = {error, _Reason} ->
|
2016-08-12 19:27:23 +02:00
|
|
|
websocket_close(State, HandlerState, Error)
|
2015-02-16 15:48:04 +01:00
|
|
|
end.
|
2013-01-13 00:10:32 +01:00
|
|
|
|
2018-11-13 15:55:09 +01:00
|
|
|
dispatch_frame(State=#state{opts=Opts, frag_state=FragState, frag_buffer=SoFar}, HandlerState,
|
|
|
|
#ps_payload{type=Type0, unmasked=Payload0, close_code=CloseCode0}, RemainingData) ->
|
2018-11-12 18:12:44 +01:00
|
|
|
MaxFrameSize = maps:get(max_frame_size, Opts, infinity),
|
2015-03-06 01:56:30 +01:00
|
|
|
case cow_ws:make_frame(Type0, Payload0, CloseCode0, FragState) of
|
|
|
|
%% @todo Allow receiving fragments.
|
2017-08-25 12:08:26 +03:00
|
|
|
{fragment, _, _, Payload} when byte_size(Payload) + byte_size(SoFar) > MaxFrameSize ->
|
|
|
|
websocket_close(State, HandlerState, {error, badsize});
|
2015-03-06 01:56:30 +01:00
|
|
|
{fragment, nofin, _, Payload} ->
|
2018-03-23 16:32:53 +01:00
|
|
|
parse_header(State#state{frag_buffer= << SoFar/binary, Payload/binary >>},
|
|
|
|
HandlerState, #ps_header{buffer=RemainingData});
|
2015-03-06 01:56:30 +01:00
|
|
|
{fragment, fin, Type, Payload} ->
|
2018-03-23 16:32:53 +01:00
|
|
|
handler_call(State#state{frag_state=undefined, frag_buffer= <<>>}, HandlerState,
|
|
|
|
#ps_header{buffer=RemainingData},
|
|
|
|
websocket_handle, {Type, << SoFar/binary, Payload/binary >>},
|
|
|
|
fun parse_header/3);
|
2015-03-06 01:56:30 +01:00
|
|
|
close ->
|
2016-08-12 19:27:23 +02:00
|
|
|
websocket_close(State, HandlerState, remote);
|
2015-03-06 01:56:30 +01:00
|
|
|
{close, CloseCode, Payload} ->
|
2016-08-12 19:27:23 +02:00
|
|
|
websocket_close(State, HandlerState, {remote, CloseCode, Payload});
|
2015-03-06 01:56:30 +01:00
|
|
|
Frame = ping ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, nofin, frame(pong, State)),
|
2018-03-23 16:32:53 +01:00
|
|
|
handler_call(State, HandlerState,
|
|
|
|
#ps_header{buffer=RemainingData},
|
|
|
|
websocket_handle, Frame, fun parse_header/3);
|
2015-03-06 01:56:30 +01:00
|
|
|
Frame = {ping, Payload} ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, nofin, frame({pong, Payload}, State)),
|
2018-03-23 16:32:53 +01:00
|
|
|
handler_call(State, HandlerState,
|
|
|
|
#ps_header{buffer=RemainingData},
|
|
|
|
websocket_handle, Frame, fun parse_header/3);
|
2015-03-06 01:56:30 +01:00
|
|
|
Frame ->
|
2018-03-23 16:32:53 +01:00
|
|
|
handler_call(State, HandlerState,
|
|
|
|
#ps_header{buffer=RemainingData},
|
|
|
|
websocket_handle, Frame, fun parse_header/3)
|
2015-03-06 01:56:30 +01:00
|
|
|
end.
|
2011-08-23 16:24:02 +02:00
|
|
|
|
2016-08-12 19:27:23 +02:00
|
|
|
handler_call(State=#state{handler=Handler}, HandlerState,
|
2018-03-23 16:32:53 +01:00
|
|
|
ParseState, Callback, Message, NextState) ->
|
2016-08-15 19:21:38 +02:00
|
|
|
try case Callback of
|
|
|
|
websocket_init -> Handler:websocket_init(HandlerState);
|
|
|
|
_ -> Handler:Callback(Message, HandlerState)
|
|
|
|
end of
|
2018-09-11 14:33:58 +02:00
|
|
|
{Commands, HandlerState2} when is_list(Commands) ->
|
|
|
|
handler_call_result(State,
|
|
|
|
HandlerState2, ParseState, NextState, Commands);
|
|
|
|
{Commands, HandlerState2, hibernate} when is_list(Commands) ->
|
|
|
|
handler_call_result(State#state{hibernate=true},
|
|
|
|
HandlerState2, ParseState, NextState, Commands);
|
|
|
|
%% The following call results are deprecated.
|
2016-08-12 19:27:23 +02:00
|
|
|
{ok, HandlerState2} ->
|
2018-03-23 16:32:53 +01:00
|
|
|
NextState(State, HandlerState2, ParseState);
|
2016-08-12 19:27:23 +02:00
|
|
|
{ok, HandlerState2, hibernate} ->
|
2018-03-23 16:32:53 +01:00
|
|
|
NextState(State#state{hibernate=true}, HandlerState2, ParseState);
|
2016-08-12 19:27:23 +02:00
|
|
|
{reply, Payload, HandlerState2} ->
|
2013-08-13 23:29:16 +04:00
|
|
|
case websocket_send(Payload, State) of
|
2015-02-16 15:48:04 +01:00
|
|
|
ok ->
|
2018-03-23 16:32:53 +01:00
|
|
|
NextState(State, HandlerState2, ParseState);
|
2015-02-16 15:48:04 +01:00
|
|
|
stop ->
|
2017-02-18 20:21:02 +01:00
|
|
|
terminate(State, HandlerState2, stop);
|
2015-02-16 15:48:04 +01:00
|
|
|
Error = {error, _} ->
|
2017-02-18 20:21:02 +01:00
|
|
|
terminate(State, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2016-08-12 19:27:23 +02:00
|
|
|
{reply, Payload, HandlerState2, hibernate} ->
|
2013-08-13 23:29:16 +04:00
|
|
|
case websocket_send(Payload, State) of
|
2015-02-16 15:48:04 +01:00
|
|
|
ok ->
|
|
|
|
NextState(State#state{hibernate=true},
|
2018-03-23 16:32:53 +01:00
|
|
|
HandlerState2, ParseState);
|
2015-02-16 15:48:04 +01:00
|
|
|
stop ->
|
2017-02-18 20:21:02 +01:00
|
|
|
terminate(State, HandlerState2, stop);
|
2015-02-16 15:48:04 +01:00
|
|
|
Error = {error, _} ->
|
2017-02-18 20:21:02 +01:00
|
|
|
terminate(State, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2016-08-12 19:27:23 +02:00
|
|
|
{stop, HandlerState2} ->
|
|
|
|
websocket_close(State, HandlerState2, stop)
|
2011-05-27 12:32:02 +02:00
|
|
|
catch Class:Reason ->
|
2019-03-13 11:36:04 +00:00
|
|
|
StackTrace = erlang:get_stacktrace(),
|
2017-02-18 20:21:02 +01:00
|
|
|
websocket_send_close(State, {crash, Class, Reason}),
|
|
|
|
handler_terminate(State, HandlerState, {crash, Class, Reason}),
|
2019-03-13 11:36:04 +00:00
|
|
|
erlang:raise(Class, Reason, StackTrace)
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2018-09-11 14:33:58 +02:00
|
|
|
-spec handler_call_result(#state{}, any(), parse_state(), fun(), commands()) -> no_return().
|
|
|
|
handler_call_result(State0, HandlerState, ParseState, NextState, Commands) ->
|
|
|
|
case commands(Commands, State0, []) of
|
|
|
|
{ok, State} ->
|
|
|
|
NextState(State, HandlerState, ParseState);
|
|
|
|
{stop, State} ->
|
|
|
|
terminate(State, HandlerState, stop);
|
|
|
|
{Error = {error, _}, State} ->
|
|
|
|
terminate(State, HandlerState, Error)
|
|
|
|
end.
|
|
|
|
|
|
|
|
commands([], State, []) ->
|
|
|
|
{ok, State};
|
|
|
|
commands([], State, Data) ->
|
|
|
|
Result = transport_send(State, nofin, lists:reverse(Data)),
|
|
|
|
{Result, State};
|
2018-09-21 14:04:20 +02:00
|
|
|
commands([{active, Active}|Tail], State, Data) when is_boolean(Active) ->
|
|
|
|
commands(Tail, State#state{active=Active}, Data);
|
2018-11-13 15:55:09 +01:00
|
|
|
commands([{deflate, Deflate}|Tail], State, Data) when is_boolean(Deflate) ->
|
|
|
|
commands(Tail, State#state{deflate=Deflate}, Data);
|
2018-11-16 13:48:15 +01:00
|
|
|
commands([{set_options, SetOpts}|Tail], State0=#state{opts=Opts}, Data) ->
|
|
|
|
State = case SetOpts of
|
|
|
|
#{idle_timeout := IdleTimeout} ->
|
|
|
|
loop_timeout(State0#state{opts=Opts#{idle_timeout => IdleTimeout}});
|
|
|
|
_ ->
|
|
|
|
State0
|
|
|
|
end,
|
|
|
|
commands(Tail, State, Data);
|
2018-11-13 15:55:09 +01:00
|
|
|
commands([Frame|Tail], State, Data0) ->
|
|
|
|
Data = [frame(Frame, State)|Data0],
|
2018-09-11 14:33:58 +02:00
|
|
|
case is_close_frame(Frame) of
|
|
|
|
true ->
|
|
|
|
_ = transport_send(State, fin, lists:reverse(Data)),
|
|
|
|
{stop, State};
|
|
|
|
false ->
|
|
|
|
commands(Tail, State, Data)
|
|
|
|
end.
|
|
|
|
|
2018-04-04 17:23:37 +02:00
|
|
|
transport_send(#state{socket=Stream={Pid, _}, transport=undefined}, IsFin, Data) ->
|
|
|
|
Pid ! {Stream, {data, IsFin, Data}},
|
|
|
|
ok;
|
|
|
|
transport_send(#state{socket=Socket, transport=Transport}, _, Data) ->
|
|
|
|
Transport:send(Socket, Data).
|
|
|
|
|
2015-02-16 15:48:04 +01:00
|
|
|
-spec websocket_send(cow_ws:frame(), #state{}) -> ok | stop | {error, atom()}.
|
2016-08-15 20:04:37 +02:00
|
|
|
websocket_send(Frames, State) when is_list(Frames) ->
|
|
|
|
websocket_send_many(Frames, State, []);
|
2018-11-13 15:55:09 +01:00
|
|
|
websocket_send(Frame, State) ->
|
|
|
|
Data = frame(Frame, State),
|
2016-08-15 20:04:37 +02:00
|
|
|
case is_close_frame(Frame) of
|
2018-04-04 17:23:37 +02:00
|
|
|
true ->
|
|
|
|
_ = transport_send(State, fin, Data),
|
|
|
|
stop;
|
|
|
|
false ->
|
|
|
|
transport_send(State, nofin, Data)
|
2014-09-26 15:58:44 +03:00
|
|
|
end.
|
|
|
|
|
2018-04-04 17:23:37 +02:00
|
|
|
websocket_send_many([], State, Acc) ->
|
|
|
|
transport_send(State, nofin, lists:reverse(Acc));
|
2018-11-13 15:55:09 +01:00
|
|
|
websocket_send_many([Frame|Tail], State, Acc0) ->
|
|
|
|
Acc = [frame(Frame, State)|Acc0],
|
2016-08-15 20:04:37 +02:00
|
|
|
case is_close_frame(Frame) of
|
|
|
|
true ->
|
2018-04-04 17:23:37 +02:00
|
|
|
_ = transport_send(State, fin, lists:reverse(Acc)),
|
2016-08-15 20:04:37 +02:00
|
|
|
stop;
|
|
|
|
false ->
|
|
|
|
websocket_send_many(Tail, State, Acc)
|
2012-04-08 11:57:30 +02:00
|
|
|
end.
|
2012-10-11 21:46:43 +02:00
|
|
|
|
2016-08-15 20:04:37 +02:00
|
|
|
is_close_frame(close) -> true;
|
|
|
|
is_close_frame({close, _}) -> true;
|
|
|
|
is_close_frame({close, _, _}) -> true;
|
|
|
|
is_close_frame(_) -> false.
|
|
|
|
|
2017-01-02 16:47:16 +01:00
|
|
|
-spec websocket_close(#state{}, any(), terminate_reason()) -> no_return().
|
2017-02-18 20:21:02 +01:00
|
|
|
websocket_close(State, HandlerState, Reason) ->
|
|
|
|
websocket_send_close(State, Reason),
|
|
|
|
terminate(State, HandlerState, Reason).
|
|
|
|
|
2018-11-13 15:55:09 +01:00
|
|
|
websocket_send_close(State, Reason) ->
|
2017-02-18 20:21:02 +01:00
|
|
|
_ = case Reason of
|
2014-11-07 19:22:36 +02:00
|
|
|
Normal when Normal =:= stop; Normal =:= timeout ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, fin, frame({close, 1000, <<>>}, State));
|
2013-01-14 16:20:33 +01:00
|
|
|
{error, badframe} ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, fin, frame({close, 1002, <<>>}, State));
|
2013-01-14 16:20:33 +01:00
|
|
|
{error, badencoding} ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, fin, frame({close, 1007, <<>>}, State));
|
2017-08-25 12:08:26 +03:00
|
|
|
{error, badsize} ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, fin, frame({close, 1009, <<>>}, State));
|
2014-09-30 20:12:13 +03:00
|
|
|
{crash, _, _} ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, fin, frame({close, 1011, <<>>}, State));
|
2014-09-30 20:12:13 +03:00
|
|
|
remote ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, fin, frame(close, State));
|
2013-01-14 16:20:33 +01:00
|
|
|
{remote, Code, _} ->
|
2018-11-13 15:55:09 +01:00
|
|
|
transport_send(State, fin, frame({close, Code, <<>>}, State))
|
2013-01-14 16:20:33 +01:00
|
|
|
end,
|
2017-02-18 20:21:02 +01:00
|
|
|
ok.
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2018-11-13 15:55:09 +01:00
|
|
|
%% Don't compress frames while deflate is disabled.
|
|
|
|
frame(Frame, #state{deflate=false, extensions=Extensions}) ->
|
|
|
|
cow_ws:frame(Frame, Extensions#{deflate => false});
|
|
|
|
frame(Frame, #state{extensions=Extensions}) ->
|
|
|
|
cow_ws:frame(Frame, Extensions).
|
|
|
|
|
2017-02-18 20:21:02 +01:00
|
|
|
-spec terminate(#state{}, any(), terminate_reason()) -> no_return().
|
|
|
|
terminate(State, HandlerState, Reason) ->
|
|
|
|
handler_terminate(State, HandlerState, Reason),
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
exit(normal).
|
2017-02-18 20:21:02 +01:00
|
|
|
|
2017-05-28 19:04:16 +02:00
|
|
|
handler_terminate(#state{handler=Handler, req=Req}, HandlerState, Reason) ->
|
|
|
|
cowboy_handler:terminate(Reason, Req, HandlerState, Handler).
|
2018-03-13 11:19:13 +01:00
|
|
|
|
|
|
|
%% System callbacks.
|
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
-spec system_continue(_, _, {#state{}, any(), parse_state()}) -> no_return().
|
|
|
|
system_continue(_, _, {State, HandlerState, ParseState}) ->
|
|
|
|
loop(State, HandlerState, ParseState).
|
2018-03-13 11:19:13 +01:00
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
-spec system_terminate(any(), _, _, {#state{}, any(), parse_state()}) -> no_return().
|
2018-03-13 11:19:13 +01:00
|
|
|
system_terminate(Reason, _, _, {State, HandlerState, _}) ->
|
|
|
|
%% @todo We should exit gracefully, if possible.
|
|
|
|
terminate(State, HandlerState, Reason).
|
|
|
|
|
2018-03-23 16:32:53 +01:00
|
|
|
-spec system_code_change(Misc, _, _, _)
|
|
|
|
-> {ok, Misc} when Misc::{#state{}, any(), parse_state()}.
|
2018-03-13 11:19:13 +01:00
|
|
|
system_code_change(Misc, _, _, _) ->
|
|
|
|
{ok, Misc}.
|