2017-01-02 19:36:36 +01:00
|
|
|
%% Copyright (c) 2011-2017, Loïc Hoguin <essen@ninenines.eu>
|
2011-12-22 20:06:58 +01: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.
|
|
|
|
|
|
|
|
-module(ws_SUITE).
|
2014-04-20 22:20:54 +02:00
|
|
|
-compile(export_all).
|
2017-11-29 16:57:10 +01:00
|
|
|
-compile(nowarn_export_all).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
2015-05-05 19:59:37 +03:00
|
|
|
-import(ct_helper, [config/2]).
|
2016-08-15 18:06:16 +02:00
|
|
|
-import(ct_helper, [doc/1]).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
%% ct.
|
|
|
|
|
|
|
|
all() ->
|
2018-11-20 13:23:59 +01:00
|
|
|
[{group, ws}].
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
groups() ->
|
2018-11-20 13:23:59 +01:00
|
|
|
[{ws, [parallel], ct_helper:all(?MODULE)}].
|
2011-12-22 20:06:58 +01:00
|
|
|
|
2018-11-20 13:23:59 +01:00
|
|
|
init_per_group(Name, Config) ->
|
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
|
|
|
cowboy_test:init_http(Name, #{
|
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
|
|
|
env => #{dispatch => init_dispatch()}
|
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
|
|
|
}, Config).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
end_per_group(Listener, _Config) ->
|
2014-04-22 22:50:45 +02:00
|
|
|
cowboy:stop_listener(Listener).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
%% Dispatch configuration.
|
|
|
|
|
|
|
|
init_dispatch() ->
|
2013-01-28 16:53:09 +01:00
|
|
|
cowboy_router:compile([
|
|
|
|
{"localhost", [
|
2013-04-22 15:55:22 +02:00
|
|
|
{"/ws_echo", ws_echo, []},
|
2014-04-20 22:20:54 +02:00
|
|
|
{"/ws_echo_timer", ws_echo_timer, []},
|
2016-08-15 19:21:38 +02:00
|
|
|
{"/ws_init", ws_init_h, []},
|
2013-04-22 15:55:22 +02:00
|
|
|
{"/ws_init_shutdown", ws_init_shutdown, []},
|
|
|
|
{"/ws_send_many", ws_send_many, [
|
2012-12-02 21:37:24 +01:00
|
|
|
{sequence, [
|
|
|
|
{text, <<"one">>},
|
|
|
|
{text, <<"two">>},
|
|
|
|
{text, <<"seven!">>}]}
|
|
|
|
]},
|
2013-04-22 15:55:22 +02:00
|
|
|
{"/ws_send_close", ws_send_many, [
|
2012-12-02 21:37:24 +01:00
|
|
|
{sequence, [
|
|
|
|
{text, <<"send">>},
|
|
|
|
close,
|
|
|
|
{text, <<"won't be received">>}]}
|
|
|
|
]},
|
2013-04-22 15:55:22 +02:00
|
|
|
{"/ws_send_close_payload", ws_send_many, [
|
2012-12-02 21:37:24 +01:00
|
|
|
{sequence, [
|
|
|
|
{text, <<"send">>},
|
2012-12-08 19:11:56 +01:00
|
|
|
{close, 1001, <<"some text!">>},
|
2012-12-02 21:37:24 +01:00
|
|
|
{text, <<"won't be received">>}]}
|
|
|
|
]},
|
2016-08-12 17:01:01 +02:00
|
|
|
{"/ws_subprotocol", ws_subprotocol, []},
|
2017-05-28 19:04:16 +02:00
|
|
|
{"/terminate", ws_terminate_h, []},
|
2013-04-22 15:55:22 +02:00
|
|
|
{"/ws_timeout_hibernate", ws_timeout_hibernate, []},
|
2017-08-25 12:08:26 +03:00
|
|
|
{"/ws_timeout_cancel", ws_timeout_cancel, []},
|
2018-11-12 18:12:44 +01:00
|
|
|
{"/ws_max_frame_size", ws_max_frame_size, []},
|
2019-10-05 17:32:50 +02:00
|
|
|
{"/ws_deflate_opts", ws_deflate_opts_h, []},
|
|
|
|
{"/ws_dont_validate_utf8", ws_dont_validate_utf8_h, []}
|
2011-12-22 20:06:58 +01:00
|
|
|
]}
|
2013-01-28 16:53:09 +01:00
|
|
|
]).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
2014-04-20 22:20:54 +02:00
|
|
|
%% Tests.
|
|
|
|
|
2018-04-17 18:54:47 +02:00
|
|
|
unlimited_connections(Config) ->
|
|
|
|
doc("Websocket connections are not limited. The connections "
|
|
|
|
"are removed from the count after the handshake completes."),
|
2020-04-02 17:36:03 +02:00
|
|
|
case os:type() of
|
|
|
|
{win32, _} ->
|
|
|
|
{skip, "Tests that use too many sockets are disabled on Windows "
|
|
|
|
"to prevent intermittent failures."};
|
|
|
|
{unix, _} ->
|
|
|
|
case list_to_integer(os:cmd("printf `ulimit -n`")) of
|
|
|
|
Limit when Limit > 6100 ->
|
|
|
|
do_unlimited_connections(Config);
|
|
|
|
_ ->
|
|
|
|
{skip, "`ulimit -n` reports a limit too low for this test."}
|
|
|
|
end
|
2020-04-02 16:23:36 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
do_unlimited_connections(Config) ->
|
2018-04-17 18:54:47 +02:00
|
|
|
_ = [begin
|
|
|
|
spawn_link(fun() -> do_connect_and_loop(Config) end),
|
|
|
|
timer:sleep(1)
|
|
|
|
end || _ <- lists:seq(1, 3000)],
|
|
|
|
timer:sleep(1000),
|
|
|
|
%% We have at least 3000 client and 3000 server sockets.
|
|
|
|
true = length(erlang:ports()) > 6000,
|
|
|
|
%% Ranch thinks we have no connections.
|
|
|
|
0 = ranch_server:count_connections(ws),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
do_connect_and_loop(Config) ->
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_echo", Config),
|
|
|
|
do_loop(Socket).
|
|
|
|
|
|
|
|
do_loop(Socket) ->
|
|
|
|
%% Masked text hello echoed back clear by the server.
|
|
|
|
Mask = 16#37fa213d,
|
|
|
|
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
timer:sleep(1000),
|
|
|
|
do_loop(Socket).
|
|
|
|
|
2011-12-22 20:06:58 +01:00
|
|
|
ws0(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Websocket version 0 (hixie-76 draft) is no longer supported."),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
|
2011-12-22 20:06:58 +01:00
|
|
|
ok = gen_tcp:send(Socket,
|
2013-04-22 15:55:22 +02:00
|
|
|
"GET /ws_echo_timer HTTP/1.1\r\n"
|
2011-12-22 20:06:58 +01:00
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: WebSocket\r\n"
|
|
|
|
"Origin: http://localhost\r\n"
|
|
|
|
"Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
|
|
|
|
"Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
|
|
|
|
"\r\n"),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, {http_response, {1, 1}, 400, _}, _} = erlang:decode_packet(http, Handshake, []),
|
2011-12-22 20:06:58 +01:00
|
|
|
ok.
|
|
|
|
|
2017-02-05 11:27:54 +01:00
|
|
|
ws7(Config) ->
|
|
|
|
doc("Websocket version 7 (draft) is supported."),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_echo_timer HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 7\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
|
|
|
|
[Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
do_ws_version(Socket).
|
2012-10-11 21:46:43 +02:00
|
|
|
|
2016-08-15 18:06:16 +02:00
|
|
|
ws8(Config) ->
|
|
|
|
doc("Websocket version 8 (draft) is supported."),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
|
2011-12-22 20:06:58 +01:00
|
|
|
ok = gen_tcp:send(Socket, [
|
2013-04-22 15:55:22 +02:00
|
|
|
"GET /ws_echo_timer HTTP/1.1\r\n"
|
2011-12-22 20:06:58 +01:00
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
|
|
|
|
[Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
do_ws_version(Socket).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
ws13(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Websocket version 13 (RFC) is supported."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_echo_timer", Config),
|
|
|
|
do_ws_version(Socket).
|
|
|
|
|
|
|
|
do_ws_version(Socket) ->
|
|
|
|
%% Masked text hello echoed back clear by the server.
|
|
|
|
Mask = 16#37fa213d,
|
|
|
|
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Empty binary frame echoed back.
|
2013-01-12 16:04:35 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 0:7, 0:32 >>),
|
2011-12-22 20:19:05 +01:00
|
|
|
{ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
2016-08-15 18:06:16 +02:00
|
|
|
%% Masked binary hello echoed back clear by the server.
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Frames sent on timer by the handler.
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Client-initiated ping/pong.
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>),
|
|
|
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Client-initiated close.
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
|
2011-12-22 20:06:58 +01:00
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
2013-04-15 18:36:33 +02:00
|
|
|
ok.
|
|
|
|
|
2018-11-12 18:12:44 +01:00
|
|
|
ws_deflate_opts_client_context_takeover(Config) ->
|
|
|
|
doc("Handler is configured with client context takeover enabled."),
|
|
|
|
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?client_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config),
|
|
|
|
{_, "permessage-deflate"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers1),
|
|
|
|
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?client_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover\r\n", Config),
|
|
|
|
{_, "permessage-deflate; client_no_context_takeover"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers2),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_deflate_opts_client_no_context_takeover(Config) ->
|
|
|
|
doc("Handler is configured with client context takeover disabled."),
|
|
|
|
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?client_no_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config),
|
|
|
|
{_, "permessage-deflate; client_no_context_takeover"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers1),
|
|
|
|
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?client_no_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover\r\n", Config),
|
|
|
|
{_, "permessage-deflate; client_no_context_takeover"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers2),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%% We must send client_max_window_bits to indicate we support it.
|
|
|
|
ws_deflate_opts_client_max_window_bits(Config) ->
|
|
|
|
doc("Handler is configured with client max window bits."),
|
|
|
|
{ok, _, Headers} = do_handshake("/ws_deflate_opts?client_max_window_bits",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n", Config),
|
|
|
|
{_, "permessage-deflate; client_max_window_bits=9"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_deflate_opts_client_max_window_bits_override(Config) ->
|
|
|
|
doc("Handler is configured with client max window bits."),
|
|
|
|
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?client_max_window_bits",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits=8\r\n", Config),
|
|
|
|
{_, "permessage-deflate; client_max_window_bits=8"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers1),
|
|
|
|
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?client_max_window_bits",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits=12\r\n", Config),
|
|
|
|
{_, "permessage-deflate; client_max_window_bits=9"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers2),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_deflate_opts_server_context_takeover(Config) ->
|
|
|
|
doc("Handler is configured with server context takeover enabled."),
|
|
|
|
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?server_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config),
|
|
|
|
{_, "permessage-deflate"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers1),
|
|
|
|
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?server_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config),
|
|
|
|
{_, "permessage-deflate; server_no_context_takeover"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers2),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_deflate_opts_server_no_context_takeover(Config) ->
|
|
|
|
doc("Handler is configured with server context takeover disabled."),
|
|
|
|
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?server_no_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config),
|
|
|
|
{_, "permessage-deflate; server_no_context_takeover"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers1),
|
|
|
|
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?server_no_context_takeover",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config),
|
|
|
|
{_, "permessage-deflate; server_no_context_takeover"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers2),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_deflate_opts_server_max_window_bits(Config) ->
|
|
|
|
doc("Handler is configured with server max window bits."),
|
|
|
|
{ok, _, Headers} = do_handshake("/ws_deflate_opts?server_max_window_bits",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config),
|
|
|
|
{_, "permessage-deflate; server_max_window_bits=9"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_deflate_opts_server_max_window_bits_override(Config) ->
|
|
|
|
doc("Handler is configured with server max window bits."),
|
|
|
|
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?server_max_window_bits",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=8\r\n", Config),
|
|
|
|
{_, "permessage-deflate; server_max_window_bits=8"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers1),
|
|
|
|
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?server_max_window_bits",
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=12\r\n", Config),
|
|
|
|
{_, "permessage-deflate; server_max_window_bits=9"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers2),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_deflate_opts_zlevel(Config) ->
|
|
|
|
doc("Handler is configured with zlib level."),
|
|
|
|
do_ws_deflate_opts_z("/ws_deflate_opts?level", Config).
|
|
|
|
|
|
|
|
ws_deflate_opts_zmemlevel(Config) ->
|
|
|
|
doc("Handler is configured with zlib mem_level."),
|
|
|
|
do_ws_deflate_opts_z("/ws_deflate_opts?mem_level", Config).
|
|
|
|
|
|
|
|
ws_deflate_opts_zstrategy(Config) ->
|
|
|
|
doc("Handler is configured with zlib strategy."),
|
|
|
|
do_ws_deflate_opts_z("/ws_deflate_opts?strategy", Config).
|
|
|
|
|
|
|
|
do_ws_deflate_opts_z(Path, Config) ->
|
|
|
|
{ok, Socket, Headers} = do_handshake(Path,
|
|
|
|
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config),
|
|
|
|
{_, "permessage-deflate"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
%% Send and receive a compressed "Hello" frame.
|
|
|
|
Mask = 16#11223344,
|
|
|
|
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
|
|
|
|
MaskedHello = do_mask(CompressedHello, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Client-initiated close.
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
2019-10-05 17:32:50 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_dont_validate_utf8(Config) ->
|
|
|
|
doc("Handler is configured with UTF-8 validation disabled."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_dont_validate_utf8", Config),
|
|
|
|
%% Send an invalid UTF-8 text frame and receive it back.
|
|
|
|
Mask = 16#37fa213d,
|
|
|
|
MaskedInvalid = do_mask(<<255, 255, 255, 255>>, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, <<1:1, 0:3, 1:4, 1:1, 4:7, Mask:32, MaskedInvalid/binary>>),
|
|
|
|
{ok, <<1:1, 0:3, 1:4, 0:1, 4:7, 255, 255, 255, 255>>} = gen_tcp:recv(Socket, 0, 6000),
|
2018-11-12 18:12:44 +01:00
|
|
|
ok.
|
|
|
|
|
2019-10-05 13:04:21 +02:00
|
|
|
ws_first_frame_with_handshake(Config) ->
|
|
|
|
doc("Client sends the first frame immediately with the handshake. "
|
|
|
|
"This is invalid according to the protocol but we still want "
|
|
|
|
"to accept it if the handshake is successful."),
|
|
|
|
Mask = 16#37fa213d,
|
|
|
|
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_echo", "",
|
|
|
|
<<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary>>,
|
|
|
|
Config),
|
|
|
|
{ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2016-08-15 19:21:38 +02:00
|
|
|
ws_init_return_ok(Config) ->
|
|
|
|
doc("Handler does nothing."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?ok", Config),
|
|
|
|
%% The handler does nothing; nothing should happen here.
|
|
|
|
{error, timeout} = gen_tcp:recv(Socket, 0, 1000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_ok_hibernate(Config) ->
|
|
|
|
doc("Handler does nothing; hibernates."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?ok_hibernate", Config),
|
|
|
|
%% The handler does nothing; nothing should happen here.
|
|
|
|
{error, timeout} = gen_tcp:recv(Socket, 0, 1000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply(Config) ->
|
|
|
|
doc("Handler sends a text frame just after the handshake."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply", Config),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply_hibernate(Config) ->
|
|
|
|
doc("Handler sends a text frame just after the handshake and then hibernates."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply_hibernate", Config),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply_close(Config) ->
|
|
|
|
doc("Handler closes immediately after the handshake."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply_close", Config),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply_close_hibernate(Config) ->
|
|
|
|
doc("Handler closes immediately after the handshake, then attempts to hibernate."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply_close_hibernate", Config),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply_many(Config) ->
|
|
|
|
doc("Handler sends many frames just after the handshake."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply_many", Config),
|
|
|
|
%% We catch all frames at once and check them directly.
|
|
|
|
{ok, <<
|
|
|
|
1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
|
|
|
|
1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply_many_hibernate(Config) ->
|
|
|
|
doc("Handler sends many frames just after the handshake and then hibernates."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply_many_hibernate", Config),
|
|
|
|
%% We catch all frames at once and check them directly.
|
|
|
|
{ok, <<
|
|
|
|
1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
|
|
|
|
1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply_many_close(Config) ->
|
|
|
|
doc("Handler sends many frames including a close frame just after the handshake."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply_many_close", Config),
|
|
|
|
%% We catch all frames at once and check them directly.
|
|
|
|
{ok, <<
|
|
|
|
1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
|
|
|
|
1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_return_reply_many_close_hibernate(Config) ->
|
|
|
|
doc("Handler sends many frames including a close frame just after the handshake and then hibernates."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_init?reply_many_close_hibernate", Config),
|
|
|
|
%% We catch all frames at once and check them directly.
|
|
|
|
{ok, <<
|
|
|
|
1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
|
|
|
|
1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_init_shutdown_before_handshake(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Handler stops before Websocket handshake."),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
|
2013-04-15 18:36:33 +02:00
|
|
|
ok = gen_tcp:send(Socket, [
|
2016-08-15 18:06:16 +02:00
|
|
|
"GET /ws_init_shutdown HTTP/1.1\r\n"
|
2013-07-02 13:44:28 +02:00
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
2016-08-15 18:06:16 +02:00
|
|
|
"Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 13\r\n"
|
2013-07-02 13:44:28 +02:00
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, {http_response, {1, 1}, 403, _}, _Rest} = erlang:decode_packet(http, Handshake, []),
|
2011-12-22 20:06:58 +01:00
|
|
|
ok.
|
|
|
|
|
2017-08-25 12:08:26 +03:00
|
|
|
ws_max_frame_size_close(Config) ->
|
|
|
|
doc("Server closes connection when frame size exceeds max_frame_size option"),
|
|
|
|
%% max_frame_size is set to 8 bytes in ws_max_frame_size.
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
|
|
|
|
Mask = 16#11223344,
|
|
|
|
MaskedHello = do_mask(<<"HelloHello">>, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 10:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_max_frame_size_final_fragment_close(Config) ->
|
|
|
|
doc("Server closes connection when final fragmented frame "
|
|
|
|
"exceeds max_frame_size option"),
|
|
|
|
%% max_frame_size is set to 8 bytes in ws_max_frame_size.
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
|
|
|
|
Mask = 16#11223344,
|
|
|
|
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_max_frame_size_intermediate_fragment_close(Config) ->
|
|
|
|
doc("Server closes connection when intermediate fragmented frame "
|
|
|
|
"exceeds max_frame_size option"),
|
|
|
|
%% max_frame_size is set to 8 bytes in ws_max_frame_size.
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
|
|
|
|
Mask = 16#11223344,
|
|
|
|
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
ok = gen_tcp:send(Socket, << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2012-12-02 21:37:24 +01:00
|
|
|
ws_send_close(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Server-initiated close frame ends the connection."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_send_close", Config),
|
2012-12-02 21:37:24 +01:00
|
|
|
%% We catch all frames at once and check them directly.
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, <<
|
|
|
|
1:1, 0:3, 1:4, 0:1, 4:7, "send",
|
|
|
|
1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 8, 6000),
|
2012-12-02 21:37:24 +01:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_send_close_payload(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Server-initiated close frame with payload ends the connection."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_send_close_payload", Config),
|
2012-12-02 21:37:24 +01:00
|
|
|
%% We catch all frames at once and check them directly.
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, <<
|
|
|
|
1:1, 0:3, 1:4, 0:1, 4:7, "send",
|
|
|
|
1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >>} = gen_tcp:recv(Socket, 20, 6000),
|
2012-12-02 21:37:24 +01:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2012-10-11 21:46:43 +02:00
|
|
|
ws_send_many(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Server sends many frames in a single reply."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_send_many", Config),
|
2012-11-27 17:31:54 +01:00
|
|
|
%% We catch all frames at once and check them directly.
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, <<
|
|
|
|
1:1, 0:3, 1:4, 0:1, 3:7, "one",
|
2012-11-27 17:31:54 +01:00
|
|
|
1:1, 0:3, 1:4, 0:1, 3:7, "two",
|
2016-08-15 18:06:16 +02:00
|
|
|
1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >>} = gen_tcp:recv(Socket, 18, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_single_bytes(Config) ->
|
|
|
|
doc("Client sends a text frame one byte at a time."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_echo", Config),
|
|
|
|
%% We sleep between sends to make sure only one byte is sent.
|
|
|
|
ok = gen_tcp:send(Socket, << 16#81 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#85 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#37 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#fa >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#21 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#3d >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#7f >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#9f >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#4d >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#51 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
2012-10-11 21:46:43 +02:00
|
|
|
ok.
|
|
|
|
|
2016-08-12 17:01:01 +02:00
|
|
|
ws_subprotocol(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Websocket sub-protocol negotiation."),
|
|
|
|
{ok, _, Headers} = do_handshake("/ws_subprotocol",
|
|
|
|
"Sec-WebSocket-Protocol: foo, bar\r\n", Config),
|
|
|
|
{_, "foo"} = lists:keyfind("sec-websocket-protocol", 1, Headers),
|
2016-08-12 17:01:01 +02:00
|
|
|
ok.
|
|
|
|
|
2017-05-28 19:04:16 +02:00
|
|
|
ws_terminate(Config) ->
|
|
|
|
doc("The Req object is kept in a more compact form by default."),
|
|
|
|
{ok, Socket, _} = do_handshake("/terminate",
|
|
|
|
"x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
|
|
|
|
%% Send a close frame.
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Confirm terminate/3 was called with a compacted Req.
|
|
|
|
receive {terminate, _, Req} ->
|
|
|
|
true = maps:is_key(path, Req),
|
|
|
|
false = maps:is_key(headers, Req),
|
|
|
|
ok
|
|
|
|
after 1000 ->
|
|
|
|
error(timeout)
|
|
|
|
end.
|
|
|
|
|
|
|
|
ws_terminate_fun(Config) ->
|
|
|
|
doc("A function can be given to filter the Req object."),
|
|
|
|
{ok, Socket, _} = do_handshake("/terminate?req_filter",
|
|
|
|
"x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
|
|
|
|
%% Send a close frame.
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Confirm terminate/3 was called with a compacted Req.
|
|
|
|
receive {terminate, _, Req} ->
|
|
|
|
filtered = Req,
|
|
|
|
ok
|
|
|
|
after 1000 ->
|
|
|
|
error(timeout)
|
|
|
|
end.
|
|
|
|
|
2012-04-01 18:58:28 -07:00
|
|
|
ws_text_fragments(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Client sends fragmented text frames."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_echo", Config),
|
|
|
|
%% Send two "Hello" over two fragments and two sends.
|
|
|
|
Mask = 16#37fa213d,
|
|
|
|
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Send three "Hello" over three fragments and one send.
|
2012-04-01 18:58:28 -07:00
|
|
|
ok = gen_tcp:send(Socket, [
|
2016-08-15 18:06:16 +02:00
|
|
|
<< 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
|
2017-08-25 12:08:26 +03:00
|
|
|
<< 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
|
|
|
|
<< 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>]),
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
2012-04-01 18:58:28 -07:00
|
|
|
ok.
|
|
|
|
|
2012-10-11 21:46:43 +02:00
|
|
|
ws_timeout_hibernate(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Server-initiated close on timeout with hibernating process."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_timeout_hibernate", Config),
|
2013-01-14 16:20:33 +01:00
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
|
2012-10-11 21:46:43 +02:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2016-08-15 18:06:16 +02:00
|
|
|
ws_timeout_no_cancel(Config) ->
|
|
|
|
doc("Server-initiated timeout is not influenced by reception of Erlang messages."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
|
2013-01-14 16:20:33 +01:00
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
|
2012-12-19 11:34:44 -08:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_timeout_reset(Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
doc("Server-initiated timeout is reset when client sends more data."),
|
|
|
|
{ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
|
|
|
|
%% Send and receive back a frame a few times.
|
|
|
|
Mask = 16#37fa213d,
|
|
|
|
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
|
2013-01-10 21:58:38 +01:00
|
|
|
[begin
|
2016-08-15 18:06:16 +02:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
timer:sleep(500)
|
2013-01-10 21:58:38 +01:00
|
|
|
end || _ <- [1, 2, 3, 4]],
|
2016-08-15 18:06:16 +02:00
|
|
|
%% Timeout will occur after we stop sending data.
|
2013-01-14 16:20:33 +01:00
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
|
2012-12-19 11:34:44 -08:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2016-08-15 18:06:16 +02:00
|
|
|
ws_webkit_deflate(Config) ->
|
|
|
|
doc("x-webkit-deflate-frame compression."),
|
|
|
|
{ok, Socket, Headers} = do_handshake("/ws_echo",
|
|
|
|
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
|
|
|
|
{_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
%% Send and receive a compressed "Hello" frame.
|
|
|
|
Mask = 16#11223344,
|
|
|
|
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
|
|
|
|
MaskedHello = do_mask(CompressedHello, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary >>),
|
|
|
|
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Client-initiated close.
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_webkit_deflate_fragments(Config) ->
|
|
|
|
doc("Client sends an x-webkit-deflate-frame compressed and fragmented text frame."),
|
|
|
|
{ok, Socket, Headers} = do_handshake("/ws_echo",
|
|
|
|
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
|
|
|
|
{_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
%% Send a compressed "Hello" over two fragments and two sends.
|
|
|
|
Mask = 16#11223344,
|
|
|
|
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
|
|
|
|
MaskedHello1 = do_mask(binary:part(CompressedHello, 0, 4), Mask, <<>>),
|
|
|
|
MaskedHello2 = do_mask(binary:part(CompressedHello, 4, 3), Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32, MaskedHello1/binary >>),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32, MaskedHello2/binary >>),
|
|
|
|
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_webkit_deflate_single_bytes(Config) ->
|
|
|
|
doc("Client sends an x-webkit-deflate-frame compressed text frame one byte at a time."),
|
|
|
|
{ok, Socket, Headers} = do_handshake("/ws_echo",
|
|
|
|
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
|
|
|
|
{_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
%% We sleep between sends to make sure only one byte is sent.
|
|
|
|
Mask = 16#11223344,
|
|
|
|
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
|
|
|
|
MaskedHello = do_mask(CompressedHello, Mask, <<>>),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#c1 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#87 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#11 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#22 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#33 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#44 >>), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 0)]), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 1)]), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 2)]), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 3)]), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 4)]), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 5)]), timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 6)]),
|
|
|
|
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2012-10-11 21:46:43 +02:00
|
|
|
%% Internal.
|
2012-04-01 18:58:28 -07:00
|
|
|
|
2016-08-15 18:06:16 +02:00
|
|
|
do_handshake(Path, Config) ->
|
2019-10-05 13:04:21 +02:00
|
|
|
do_handshake(Path, "", "", Config).
|
2016-08-15 18:06:16 +02:00
|
|
|
|
|
|
|
do_handshake(Path, ExtraHeaders, Config) ->
|
2019-10-05 13:04:21 +02:00
|
|
|
do_handshake(Path, ExtraHeaders, "", Config).
|
|
|
|
|
|
|
|
do_handshake(Path, ExtraHeaders, ExtraData, Config) ->
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
|
|
|
|
[binary, {active, false}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET ", Path, " HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 13\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"Upgrade: websocket\r\n",
|
|
|
|
ExtraHeaders,
|
2019-10-05 13:04:21 +02:00
|
|
|
"\r\n",
|
|
|
|
ExtraData]),
|
2016-08-15 18:06:16 +02:00
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
|
2019-10-05 13:04:21 +02:00
|
|
|
[Headers, Data] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
%% Queue extra data back, if any. We don't want to receive it yet.
|
|
|
|
case Data of
|
|
|
|
<<>> -> ok;
|
|
|
|
_ -> gen_tcp:unrecv(Socket, Data)
|
|
|
|
end,
|
2016-08-15 18:06:16 +02:00
|
|
|
{_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
{ok, Socket, Headers}.
|
|
|
|
|
2014-04-20 22:20:54 +02:00
|
|
|
do_decode_headers({ok, http_eoh, Rest}, Acc) ->
|
2011-12-22 20:06:58 +01:00
|
|
|
[Acc, Rest];
|
2014-04-20 22:20:54 +02:00
|
|
|
do_decode_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
|
2011-12-22 20:06:58 +01:00
|
|
|
F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
|
2016-08-15 18:06:16 +02:00
|
|
|
do_decode_headers(erlang:decode_packet(httph, Rest, []), [{F(Key), Value}|Acc]).
|
2013-07-02 13:44:28 +02:00
|
|
|
|
2014-04-20 22:20:54 +02:00
|
|
|
do_mask(<<>>, _, Acc) ->
|
|
|
|
Acc;
|
|
|
|
do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) ->
|
2013-07-02 13:44:28 +02:00
|
|
|
T = O bxor MaskKey,
|
2014-04-20 22:20:54 +02:00
|
|
|
do_mask(Rest, MaskKey, << Acc/binary, T:32 >>);
|
|
|
|
do_mask(<< O:24 >>, MaskKey, Acc) ->
|
2013-07-02 13:44:28 +02:00
|
|
|
<< MaskKey2:24, _:8 >> = << MaskKey:32 >>,
|
|
|
|
T = O bxor MaskKey2,
|
|
|
|
<< Acc/binary, T:24 >>;
|
2014-04-20 22:20:54 +02:00
|
|
|
do_mask(<< O:16 >>, MaskKey, Acc) ->
|
2013-07-02 13:44:28 +02:00
|
|
|
<< MaskKey2:16, _:16 >> = << MaskKey:32 >>,
|
|
|
|
T = O bxor MaskKey2,
|
|
|
|
<< Acc/binary, T:16 >>;
|
2014-04-20 22:20:54 +02:00
|
|
|
do_mask(<< O:8 >>, MaskKey, Acc) ->
|
2013-07-02 13:44:28 +02:00
|
|
|
<< MaskKey2:8, _:24 >> = << MaskKey:32 >>,
|
|
|
|
T = O bxor MaskKey2,
|
|
|
|
<< Acc/binary, T:8 >>.
|