2014-02-06 19:57:23 +01:00
|
|
|
%% Copyright (c) 2011-2014, 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
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-export([upgrade/6]).
|
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]).
|
2012-08-27 12:16:07 +02:00
|
|
|
-export([handler_loop/4]).
|
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()}
|
|
|
|
| {module(), Req, any(), hibernate}
|
|
|
|
| {module(), Req, any(), timeout()}
|
|
|
|
| {module(), Req, any(), timeout(), hibernate}
|
|
|
|
when Req::cowboy_req:req().
|
2016-08-11 11:06:03 +02:00
|
|
|
|
|
|
|
-callback websocket_init(Req, State)
|
|
|
|
-> {ok, Req, State}
|
|
|
|
when Req::cowboy_req:req(), State::any().
|
|
|
|
-optional_callbacks([websocket_init/2]).
|
|
|
|
|
2014-09-30 20:12:13 +03:00
|
|
|
-callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
|
|
|
|
-> {ok, Req, State}
|
|
|
|
| {ok, Req, State, hibernate}
|
2015-02-16 15:48:04 +01:00
|
|
|
| {reply, cow_ws:frame() | [cow_ws:frame()], Req, State}
|
|
|
|
| {reply, cow_ws:frame() | [cow_ws:frame()], Req, State, hibernate}
|
2014-11-07 19:22:36 +02:00
|
|
|
| {stop, Req, State}
|
2014-09-30 20:12:13 +03:00
|
|
|
when Req::cowboy_req:req(), State::any().
|
|
|
|
-callback websocket_info(any(), Req, State)
|
|
|
|
-> {ok, Req, State}
|
|
|
|
| {ok, Req, State, hibernate}
|
2015-02-16 15:48:04 +01:00
|
|
|
| {reply, cow_ws:frame() | [cow_ws:frame()], Req, State}
|
|
|
|
| {reply, cow_ws:frame() | [cow_ws:frame()], Req, State, hibernate}
|
2014-11-07 19:22:36 +02:00
|
|
|
| {stop, Req, State}
|
2014-09-30 20:12:13 +03:00
|
|
|
when Req::cowboy_req:req(), 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
|
|
|
|
2011-04-14 21:21:17 +02:00
|
|
|
-record(state, {
|
2012-09-15 21:09:12 +02:00
|
|
|
socket = undefined :: inet:socket(),
|
|
|
|
transport = undefined :: module(),
|
2011-04-14 21:21:17 +02:00
|
|
|
handler :: module(),
|
2013-01-10 21:58:38 +01:00
|
|
|
key = undefined :: undefined | binary(),
|
2011-04-14 21:21:17 +02:00
|
|
|
timeout = infinity :: timeout(),
|
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(),
|
|
|
|
extensions = #{} :: map()
|
2011-04-14 21:21:17 +02:00
|
|
|
}).
|
|
|
|
|
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.
|
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-spec upgrade(Req, Env, module(), any(), timeout(), run | hibernate)
|
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.
|
|
|
|
%% @todo Error out if HTTP/2.
|
|
|
|
upgrade(Req0, Env, Handler, HandlerState, Timeout, Hibernate) ->
|
|
|
|
try websocket_upgrade(#state{handler=Handler, timeout=Timeout,
|
|
|
|
hibernate=Hibernate =:= hibernate}, Req0) of
|
|
|
|
{ok, State, Req} ->
|
|
|
|
websocket_handshake(State, Req, HandlerState, 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.
|
|
|
|
|
2012-09-15 01:31:51 +02:00
|
|
|
-spec websocket_upgrade(#state{}, Req)
|
|
|
|
-> {ok, #state{}, Req} when Req::cowboy_req:req().
|
2011-05-05 14:03:39 +02:00
|
|
|
websocket_upgrade(State, Req) ->
|
2014-09-23 16:43:29 +03:00
|
|
|
ConnTokens = cowboy_req:parse_header(<<"connection">>, Req),
|
2011-10-20 19:04:49 +02:00
|
|
|
true = lists:member(<<"upgrade">>, ConnTokens),
|
2011-12-22 22:09:08 +01:00
|
|
|
%% @todo Should probably send a 426 if the Upgrade header is missing.
|
2014-09-23 16:43:29 +03:00
|
|
|
[<<"websocket">>] = cowboy_req:parse_header(<<"upgrade">>, Req),
|
|
|
|
Version = cowboy_req:header(<<"sec-websocket-version">>, Req),
|
2016-08-10 17:50:28 +02:00
|
|
|
IntVersion = binary_to_integer(Version),
|
2013-01-10 21:58:38 +01:00
|
|
|
true = (IntVersion =:= 7) orelse (IntVersion =:= 8)
|
|
|
|
orelse (IntVersion =:= 13),
|
2014-09-23 16:43:29 +03:00
|
|
|
Key = cowboy_req:header(<<"sec-websocket-key">>, Req),
|
2013-01-10 21:58:38 +01:00
|
|
|
false = Key =:= undefined,
|
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
|
|
|
websocket_extensions(State#state{key=Key}, Req#{websocket_version => IntVersion}).
|
2013-04-15 18:36:33 +02:00
|
|
|
|
|
|
|
-spec websocket_extensions(#state{}, Req)
|
|
|
|
-> {ok, #state{}, Req} when Req::cowboy_req:req().
|
2016-08-12 16:56:08 +02:00
|
|
|
websocket_extensions(State, Req=#{ref := Ref}) ->
|
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
|
2016-08-12 16:56:08 +02:00
|
|
|
Compress = maps:get(websocket_compress, ranch:get_protocol_options(Ref), false),
|
|
|
|
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)};
|
2016-08-12 16:56:08 +02:00
|
|
|
websocket_extensions(State=#state{extensions=Extensions}, Req=#{pid := Pid},
|
|
|
|
[{<<"permessage-deflate">>, Params}|Tail], RespHeader) ->
|
2015-03-06 01:56:30 +01:00
|
|
|
%% @todo Make deflate options configurable.
|
|
|
|
Opts = #{level => best_compression, mem_level => 8, strategy => default},
|
2016-08-12 16:56:08 +02:00
|
|
|
case cow_ws:negotiate_permessage_deflate(Params, Extensions, Opts#{owner => Pid}) 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)
|
|
|
|
end;
|
2016-08-12 16:56:08 +02:00
|
|
|
websocket_extensions(State=#state{extensions=Extensions}, Req=#{pid := Pid},
|
|
|
|
[{<<"x-webkit-deflate-frame">>, Params}|Tail], RespHeader) ->
|
2015-03-06 01:56:30 +01:00
|
|
|
%% @todo Make deflate options configurable.
|
|
|
|
Opts = #{level => best_compression, mem_level => 8, strategy => default},
|
2016-08-12 16:56:08 +02:00
|
|
|
case cow_ws:negotiate_x_webkit_deflate_frame(Params, Extensions, Opts#{owner => Pid}) 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)
|
|
|
|
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},
|
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
|
|
|
Req=#{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" >>)),
|
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),
|
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
|
|
|
Pid ! {{Pid, StreamID}, {switch_protocol, Headers, ?MODULE, {Req, State, HandlerState}}},
|
|
|
|
{ok, Req, Env}.
|
|
|
|
|
|
|
|
%% Connection process.
|
|
|
|
|
2016-03-06 18:09:43 +01:00
|
|
|
%% @todo Keep parent and handle system messages.
|
|
|
|
-spec takeover(pid(), ranch:ref(), inet:socket(), module(), any(), binary(),
|
|
|
|
{cowboy_req:req(), #state{}, any()}) -> ok.
|
|
|
|
takeover(_Parent, Ref, Socket, Transport, _Opts, Buffer,
|
|
|
|
{Req0, State=#state{handler=Handler}, HandlerState0}) ->
|
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),
|
|
|
|
%% @todo Remove Req from Websocket callbacks.
|
|
|
|
%% @todo Allow sending a reply from websocket_init.
|
|
|
|
%% @todo Try/catch.
|
|
|
|
{ok, Req, HandlerState} = case erlang:function_exported(Handler, websocket_init, 2) of
|
|
|
|
true -> Handler:websocket_init(Req0, HandlerState0);
|
|
|
|
false -> {ok, Req0, HandlerState0}
|
|
|
|
end,
|
|
|
|
State2 = handler_loop_timeout(State#state{socket=Socket, transport=Transport}),
|
2013-01-10 21:58:38 +01:00
|
|
|
handler_before_loop(State2#state{key=undefined,
|
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
|
|
|
messages=Transport:messages()}, Req, HandlerState, Buffer).
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec handler_before_loop(#state{}, Req, any(), binary())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
2012-09-15 21:09:12 +02:00
|
|
|
handler_before_loop(State=#state{
|
|
|
|
socket=Socket, transport=Transport, hibernate=true},
|
|
|
|
Req, HandlerState, SoFar) ->
|
2011-04-14 21:21:17 +02:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
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
|
|
|
proc_lib:hibernate(?MODULE, handler_loop,
|
|
|
|
[State#state{hibernate=false}, Req, HandlerState, SoFar]);
|
2012-09-15 21:09:12 +02:00
|
|
|
handler_before_loop(State=#state{socket=Socket, transport=Transport},
|
|
|
|
Req, HandlerState, SoFar) ->
|
2011-06-06 18:15:36 +02:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
2012-12-19 11:34:44 -08:00
|
|
|
handler_loop(State, Req, HandlerState, SoFar).
|
2011-09-22 21:33:56 +02:00
|
|
|
|
|
|
|
-spec handler_loop_timeout(#state{}) -> #state{}.
|
|
|
|
handler_loop_timeout(State=#state{timeout=infinity}) ->
|
|
|
|
State#state{timeout_ref=undefined};
|
|
|
|
handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) ->
|
|
|
|
_ = case PrevRef of undefined -> ignore; PrevRef ->
|
|
|
|
erlang:cancel_timer(PrevRef) end,
|
2012-04-24 11:38:27 -07:00
|
|
|
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
|
2011-09-22 21:33:56 +02:00
|
|
|
State#state{timeout_ref=TRef}.
|
2011-06-06 18:15:36 +02:00
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec handler_loop(#state{}, Req, any(), binary())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
2013-01-12 22:31:23 +01:00
|
|
|
handler_loop(State=#state{socket=Socket, messages={OK, Closed, Error},
|
|
|
|
timeout_ref=TRef}, Req, HandlerState, SoFar) ->
|
2011-04-14 21:21:17 +02:00
|
|
|
receive
|
|
|
|
{OK, Socket, Data} ->
|
2012-12-19 11:34:44 -08:00
|
|
|
State2 = handler_loop_timeout(State),
|
|
|
|
websocket_data(State2, Req, HandlerState,
|
2011-04-14 21:21:17 +02:00
|
|
|
<< SoFar/binary, Data/binary >>);
|
|
|
|
{Closed, Socket} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, closed});
|
|
|
|
{Error, Socket, Reason} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, Reason});
|
2012-04-24 11:38:27 -07:00
|
|
|
{timeout, TRef, ?MODULE} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
websocket_close(State, Req, HandlerState, timeout);
|
2012-04-24 11:38:27 -07:00
|
|
|
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
|
2011-09-22 21:33:56 +02:00
|
|
|
handler_loop(State, Req, HandlerState, SoFar);
|
2011-04-14 21:21:17 +02:00
|
|
|
Message ->
|
|
|
|
handler_call(State, Req, HandlerState,
|
2011-07-19 12:12:25 +02:00
|
|
|
SoFar, websocket_info, Message, fun handler_before_loop/4)
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec websocket_data(#state{}, Req, any(), binary())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
2015-03-06 01:56:30 +01:00
|
|
|
websocket_data(State=#state{frag_state=FragState, extensions=Extensions}, Req, HandlerState, Data) ->
|
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, _} ->
|
2013-01-14 16:20:33 +01:00
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2015-02-16 15:48:04 +01:00
|
|
|
{Type, FragState2, Rsv, Len, MaskKey, Rest} ->
|
2015-03-06 01:56:30 +01:00
|
|
|
websocket_payload(State#state{frag_state=FragState2}, Req, HandlerState, Type, Len, MaskKey, Rsv, undefined, <<>>, 0, Rest);
|
2015-02-16 15:48:04 +01:00
|
|
|
more ->
|
|
|
|
handler_before_loop(State, Req, HandlerState, Data);
|
|
|
|
error ->
|
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe})
|
|
|
|
end.
|
2013-01-12 22:31:23 +01:00
|
|
|
|
2015-03-06 01:56:30 +01:00
|
|
|
websocket_payload(State=#state{frag_state=FragState, utf8_state=Incomplete, extensions=Extensions},
|
2015-02-16 15:48:04 +01:00
|
|
|
Req, HandlerState, Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen, Data) ->
|
|
|
|
case cow_ws:parse_payload(Data, MaskKey, Incomplete, UnmaskedLen, Type, Len, FragState, Extensions, Rsv) of
|
2015-03-06 01:56:30 +01:00
|
|
|
{ok, CloseCode2, Payload, Utf8State, Rest} ->
|
|
|
|
websocket_dispatch(State#state{utf8_state=Utf8State},
|
|
|
|
Req, HandlerState, Type, << Unmasked/binary, Payload/binary >>, CloseCode2, Rest);
|
2015-02-16 15:48:04 +01:00
|
|
|
{ok, Payload, Utf8State, Rest} ->
|
|
|
|
websocket_dispatch(State#state{utf8_state=Utf8State},
|
|
|
|
Req, HandlerState, Type, << Unmasked/binary, Payload/binary >>, CloseCode, Rest);
|
2015-03-06 01:56:30 +01:00
|
|
|
{more, CloseCode2, Payload, Utf8State} ->
|
|
|
|
websocket_payload_loop(State#state{utf8_state=Utf8State},
|
|
|
|
Req, HandlerState, Type, Len - byte_size(Data), MaskKey, Rsv, CloseCode2,
|
|
|
|
<< Unmasked/binary, Payload/binary >>, UnmaskedLen + byte_size(Data));
|
2015-02-16 15:48:04 +01:00
|
|
|
{more, Payload, Utf8State} ->
|
|
|
|
websocket_payload_loop(State#state{utf8_state=Utf8State},
|
|
|
|
Req, HandlerState, Type, Len - byte_size(Data), MaskKey, Rsv, CloseCode,
|
|
|
|
<< Unmasked/binary, Payload/binary >>, UnmaskedLen + byte_size(Data));
|
2015-03-06 01:56:30 +01:00
|
|
|
Error = {error, _Reason} ->
|
|
|
|
websocket_close(State, Req, HandlerState, Error)
|
2015-02-16 15:48:04 +01:00
|
|
|
end.
|
2013-01-13 00:10:32 +01:00
|
|
|
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_payload_loop(State=#state{socket=Socket, transport=Transport,
|
|
|
|
messages={OK, Closed, Error}, timeout_ref=TRef},
|
2015-02-16 15:48:04 +01:00
|
|
|
Req, HandlerState, Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
|
|
|
receive
|
|
|
|
{OK, Socket, Data} ->
|
|
|
|
State2 = handler_loop_timeout(State),
|
|
|
|
websocket_payload(State2, Req, HandlerState,
|
2015-02-16 15:48:04 +01:00
|
|
|
Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen, Data);
|
2013-01-12 22:31:23 +01:00
|
|
|
{Closed, Socket} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, closed});
|
|
|
|
{Error, Socket, Reason} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, Reason});
|
|
|
|
{timeout, TRef, ?MODULE} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
websocket_close(State, Req, HandlerState, timeout);
|
2013-01-12 22:31:23 +01:00
|
|
|
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
|
|
|
|
websocket_payload_loop(State, Req, HandlerState,
|
2015-02-16 15:48:04 +01:00
|
|
|
Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen);
|
2013-01-12 22:31:23 +01:00
|
|
|
Message ->
|
|
|
|
handler_call(State, Req, HandlerState,
|
|
|
|
<<>>, websocket_info, Message,
|
|
|
|
fun (State2, Req2, HandlerState2, _) ->
|
|
|
|
websocket_payload_loop(State2, Req2, HandlerState2,
|
2015-02-16 15:48:04 +01:00
|
|
|
Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen)
|
2013-01-12 22:31:23 +01:00
|
|
|
end)
|
|
|
|
end.
|
2011-08-23 16:24:02 +02:00
|
|
|
|
2015-03-06 01:56:30 +01:00
|
|
|
websocket_dispatch(State=#state{socket=Socket, transport=Transport, frag_state=FragState, frag_buffer=SoFar, extensions=Extensions},
|
|
|
|
Req, HandlerState, Type0, Payload0, CloseCode0, RemainingData) ->
|
|
|
|
case cow_ws:make_frame(Type0, Payload0, CloseCode0, FragState) of
|
|
|
|
%% @todo Allow receiving fragments.
|
|
|
|
{fragment, nofin, _, Payload} ->
|
|
|
|
websocket_data(State#state{frag_buffer= << SoFar/binary, Payload/binary >>}, Req, HandlerState, RemainingData);
|
|
|
|
{fragment, fin, Type, Payload} ->
|
|
|
|
handler_call(State#state{frag_state=undefined, frag_buffer= <<>>}, Req, HandlerState, RemainingData,
|
|
|
|
websocket_handle, {Type, << SoFar/binary, Payload/binary >>}, fun websocket_data/4);
|
|
|
|
close ->
|
|
|
|
websocket_close(State, Req, HandlerState, remote);
|
|
|
|
{close, CloseCode, Payload} ->
|
|
|
|
websocket_close(State, Req, HandlerState, {remote, CloseCode, Payload});
|
|
|
|
Frame = ping ->
|
|
|
|
Transport:send(Socket, cow_ws:frame(pong, Extensions)),
|
|
|
|
handler_call(State, Req, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/4);
|
|
|
|
Frame = {ping, Payload} ->
|
|
|
|
Transport:send(Socket, cow_ws:frame({pong, Payload}, Extensions)),
|
|
|
|
handler_call(State, Req, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/4);
|
|
|
|
Frame ->
|
|
|
|
handler_call(State, Req, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/4)
|
|
|
|
end.
|
2011-08-23 16:24:02 +02:00
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec handler_call(#state{}, Req, any(), binary(), atom(), any(), fun())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
2013-08-24 20:36:23 +02:00
|
|
|
handler_call(State=#state{handler=Handler}, Req, HandlerState,
|
|
|
|
RemainingData, Callback, Message, NextState) ->
|
2011-07-19 12:12:25 +02:00
|
|
|
try Handler:Callback(Message, Req, HandlerState) of
|
2011-04-14 21:21:17 +02:00
|
|
|
{ok, Req2, HandlerState2} ->
|
|
|
|
NextState(State, Req2, HandlerState2, RemainingData);
|
2011-06-06 18:15:36 +02:00
|
|
|
{ok, Req2, HandlerState2, hibernate} ->
|
|
|
|
NextState(State#state{hibernate=true},
|
|
|
|
Req2, HandlerState2, RemainingData);
|
2012-10-11 21:46:43 +02:00
|
|
|
{reply, Payload, Req2, HandlerState2}
|
2013-08-13 23:29:16 +04:00
|
|
|
when is_list(Payload) ->
|
|
|
|
case websocket_send_many(Payload, State) of
|
2015-02-16 15:48:04 +01:00
|
|
|
ok ->
|
|
|
|
NextState(State, Req2, HandlerState2, RemainingData);
|
|
|
|
stop ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, stop);
|
|
|
|
Error = {error, _} ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2012-10-11 21:46:43 +02:00
|
|
|
{reply, Payload, Req2, HandlerState2, hibernate}
|
2013-08-13 23:29:16 +04:00
|
|
|
when is_list(Payload) ->
|
|
|
|
case websocket_send_many(Payload, State) of
|
2015-02-16 15:48:04 +01:00
|
|
|
ok ->
|
|
|
|
NextState(State#state{hibernate=true},
|
2012-04-08 11:57:30 +02:00
|
|
|
Req2, HandlerState2, RemainingData);
|
2015-02-16 15:48:04 +01:00
|
|
|
stop ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, stop);
|
|
|
|
Error = {error, _} ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2013-08-13 23:29:16 +04:00
|
|
|
{reply, Payload, Req2, HandlerState2} ->
|
|
|
|
case websocket_send(Payload, State) of
|
2015-02-16 15:48:04 +01:00
|
|
|
ok ->
|
|
|
|
NextState(State, Req2, HandlerState2, RemainingData);
|
|
|
|
stop ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, stop);
|
|
|
|
Error = {error, _} ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2013-08-13 23:29:16 +04:00
|
|
|
{reply, Payload, Req2, HandlerState2, hibernate} ->
|
|
|
|
case websocket_send(Payload, State) of
|
2015-02-16 15:48:04 +01:00
|
|
|
ok ->
|
|
|
|
NextState(State#state{hibernate=true},
|
2012-04-08 11:57:30 +02:00
|
|
|
Req2, HandlerState2, RemainingData);
|
2015-02-16 15:48:04 +01:00
|
|
|
stop ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, stop);
|
|
|
|
Error = {error, _} ->
|
|
|
|
handler_terminate(State, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2014-11-07 19:22:36 +02:00
|
|
|
{stop, Req2, HandlerState2} ->
|
|
|
|
websocket_close(State, Req2, HandlerState2, stop)
|
2011-05-27 12:32:02 +02:00
|
|
|
catch Class:Reason ->
|
2014-09-30 20:12:13 +03:00
|
|
|
_ = websocket_close(State, Req, HandlerState, {crash, Class, Reason}),
|
2016-08-10 17:45:28 +02:00
|
|
|
erlang:raise(Class, Reason, erlang:get_stacktrace())
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2015-02-16 15:48:04 +01:00
|
|
|
-spec websocket_send(cow_ws:frame(), #state{}) -> ok | stop | {error, atom()}.
|
2015-03-06 01:56:30 +01:00
|
|
|
websocket_send(Frame, #state{socket=Socket, transport=Transport, extensions=Extensions}) ->
|
2015-02-16 15:48:04 +01:00
|
|
|
Res = Transport:send(Socket, cow_ws:frame(Frame, Extensions)),
|
|
|
|
case Frame of
|
|
|
|
close -> stop;
|
|
|
|
{close, _} -> stop;
|
|
|
|
{close, _, _} -> stop;
|
|
|
|
_ -> Res
|
2014-09-26 15:58:44 +03:00
|
|
|
end.
|
|
|
|
|
2015-02-16 15:48:04 +01:00
|
|
|
-spec websocket_send_many([cow_ws:frame()], #state{}) -> ok | stop | {error, atom()}.
|
|
|
|
websocket_send_many([], _) ->
|
|
|
|
ok;
|
2012-10-11 21:46:43 +02:00
|
|
|
websocket_send_many([Frame|Tail], State) ->
|
2012-04-08 11:57:30 +02:00
|
|
|
case websocket_send(Frame, State) of
|
2015-02-16 15:48:04 +01:00
|
|
|
ok -> websocket_send_many(Tail, State);
|
|
|
|
stop -> stop;
|
|
|
|
Error -> Error
|
2012-04-08 11:57:30 +02:00
|
|
|
end.
|
2012-10-11 21:46:43 +02:00
|
|
|
|
2014-03-08 19:51:39 +01:00
|
|
|
-spec websocket_close(#state{}, Req, any(), terminate_reason())
|
2013-01-03 22:47:51 +01:00
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
2015-03-06 01:56:30 +01:00
|
|
|
websocket_close(State=#state{socket=Socket, transport=Transport, extensions=Extensions},
|
2012-09-15 21:09:12 +02:00
|
|
|
Req, HandlerState, Reason) ->
|
2013-01-14 16:20:33 +01:00
|
|
|
case Reason of
|
2014-11-07 19:22:36 +02:00
|
|
|
Normal when Normal =:= stop; Normal =:= timeout ->
|
2015-02-16 15:48:04 +01:00
|
|
|
Transport:send(Socket, cow_ws:frame({close, 1000, <<>>}, Extensions));
|
2013-01-14 16:20:33 +01:00
|
|
|
{error, badframe} ->
|
2015-02-16 15:48:04 +01:00
|
|
|
Transport:send(Socket, cow_ws:frame({close, 1002, <<>>}, Extensions));
|
2013-01-14 16:20:33 +01:00
|
|
|
{error, badencoding} ->
|
2015-02-16 15:48:04 +01:00
|
|
|
Transport:send(Socket, cow_ws:frame({close, 1007, <<>>}, Extensions));
|
2014-09-30 20:12:13 +03:00
|
|
|
{crash, _, _} ->
|
2015-02-16 15:48:04 +01:00
|
|
|
Transport:send(Socket, cow_ws:frame({close, 1011, <<>>}, Extensions));
|
2014-09-30 20:12:13 +03:00
|
|
|
remote ->
|
2015-02-16 15:48:04 +01:00
|
|
|
Transport:send(Socket, cow_ws:frame(close, Extensions));
|
2013-01-14 16:20:33 +01:00
|
|
|
{remote, Code, _} ->
|
2015-02-16 15:48:04 +01:00
|
|
|
Transport:send(Socket, cow_ws:frame({close, Code, <<>>}, Extensions))
|
2013-01-14 16:20:33 +01:00
|
|
|
end,
|
2011-04-14 21:21:17 +02:00
|
|
|
handler_terminate(State, Req, HandlerState, Reason).
|
|
|
|
|
2014-03-08 19:51:39 +01:00
|
|
|
-spec handler_terminate(#state{}, Req, any(), terminate_reason())
|
2013-01-03 22:47:51 +01:00
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
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
|
|
|
handler_terminate(#state{handler=Handler},
|
2014-09-26 15:58:44 +03:00
|
|
|
Req, HandlerState, Reason) ->
|
2014-09-30 20:12:13 +03:00
|
|
|
cowboy_handler:terminate(Reason, Req, HandlerState, Handler),
|
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).
|