2014-02-06 19:57:23 +01:00
|
|
|
%% Copyright (c) 2011-2014, 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).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
2014-04-20 22:20:54 +02:00
|
|
|
-import(cowboy_test, [config/2]).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
%% ct.
|
|
|
|
|
|
|
|
all() ->
|
2014-04-20 22:20:54 +02:00
|
|
|
[{group, autobahn}, {group, ws}].
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
groups() ->
|
2014-04-20 22:20:54 +02:00
|
|
|
BaseTests = cowboy_test:all(?MODULE) -- [autobahn_fuzzingclient],
|
|
|
|
[{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}].
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
init_per_suite(Config) ->
|
2014-04-20 22:20:54 +02:00
|
|
|
cowboy_test:start([cowboy]),
|
2011-12-22 20:06:58 +01:00
|
|
|
Config.
|
|
|
|
|
|
|
|
end_per_suite(_Config) ->
|
2014-04-20 22:20:54 +02:00
|
|
|
cowboy_test:stop([cowboy]).
|
|
|
|
|
|
|
|
init_per_group(autobahn, Config) ->
|
|
|
|
%% Some systems have it named pip2.
|
|
|
|
Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
|
|
|
|
case string:str(Out, "autobahntestsuite") of
|
|
|
|
0 ->
|
|
|
|
ct:print("Skipping the autobahn group because the "
|
|
|
|
"Autobahn Test Suite is not installed.~nTo install it, "
|
|
|
|
"please follow the instructions on this page:~n~n "
|
|
|
|
"http://autobahn.ws/testsuite/installation.html"),
|
|
|
|
{skip, "Autobahn Test Suite not installed."};
|
|
|
|
_ ->
|
|
|
|
{ok, _} = cowboy:start_http(autobahn, 100, [{port, 33080}], [
|
|
|
|
{env, [{dispatch, init_dispatch()}]}]),
|
|
|
|
Config
|
|
|
|
end;
|
2011-12-22 20:06:58 +01:00
|
|
|
init_per_group(ws, Config) ->
|
2013-03-02 14:38:10 +01:00
|
|
|
cowboy:start_http(ws, 100, [{port, 0}], [
|
2013-04-15 18:36:33 +02:00
|
|
|
{env, [{dispatch, init_dispatch()}]},
|
|
|
|
{compress, true}
|
2012-08-27 11:50:35 +02:00
|
|
|
]),
|
2013-03-02 14:38:10 +01:00
|
|
|
Port = ranch:get_port(ws),
|
2011-12-22 20:06:58 +01:00
|
|
|
[{port, Port}|Config].
|
|
|
|
|
|
|
|
end_per_group(Listener, _Config) ->
|
|
|
|
cowboy:stop_listener(Listener),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%% 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, []},
|
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">>}]}
|
|
|
|
]},
|
2013-04-22 15:55:22 +02:00
|
|
|
{"/ws_timeout_hibernate", ws_timeout_hibernate, []},
|
|
|
|
{"/ws_timeout_cancel", ws_timeout_cancel, []},
|
|
|
|
{"/ws_upgrade_with_opts", ws_upgrade_with_opts,
|
2012-12-03 14:13:46 +01:00
|
|
|
<<"failure">>}
|
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.
|
|
|
|
|
|
|
|
autobahn_fuzzingclient(Config) ->
|
|
|
|
Out = os:cmd("cd " ++ config(priv_dir, Config)
|
|
|
|
++ " && wstest -m fuzzingclient -s "
|
|
|
|
++ config(data_dir, Config) ++ "client.json"),
|
|
|
|
Report = config(priv_dir, Config) ++ "reports/servers/index.html",
|
|
|
|
ct:log("<h2><a href=\"~s\">Full report</a></h2>~n", [Report]),
|
|
|
|
ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
|
|
|
|
ct:log("~s~n", [Out]),
|
|
|
|
{ok, HTML} = file:read_file(Report),
|
|
|
|
case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
|
|
|
|
true -> error(failed);
|
|
|
|
false -> ok
|
|
|
|
end.
|
2011-12-22 20:06:58 +01:00
|
|
|
|
2013-01-10 21:58:38 +01:00
|
|
|
%% We do not support hixie76 anymore.
|
2011-12-22 20:06:58 +01:00
|
|
|
ws0(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
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),
|
2013-01-10 21:58:38 +01:00
|
|
|
{ok, {http_response, {1, 1}, 400, _}, _}
|
|
|
|
= erlang:decode_packet(http, Handshake, []).
|
2011-12-22 20:06:58 +01:00
|
|
|
|
|
|
|
ws8(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
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),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2011-12-22 20:06:58 +01:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
|
|
|
|
16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{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),
|
2013-01-12 16:04:35 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
|
2011-12-22 20:06:58 +01:00
|
|
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
|
2013-01-12 16:04:35 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
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),
|
|
|
|
ok.
|
|
|
|
|
2012-10-11 21:46:43 +02:00
|
|
|
ws8_init_shutdown(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_init_shutdown 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2011-12-22 20:06:58 +01:00
|
|
|
ws8_single_bytes(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
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),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2011-12-22 20:06:58 +01:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
|
|
|
|
ok = timer:sleep(100), %% sleep for a period
|
|
|
|
ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
|
2013-01-10 19:54:10 +01:00
|
|
|
ok = timer:sleep(100),
|
2011-12-22 20:06:58 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 16#37 >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#fa >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#21 >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#3d >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#7f >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#9f >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#4d >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#51 >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#58 >>),
|
|
|
|
{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, 5:7, "Hello" >>}
|
|
|
|
= 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),
|
2013-01-12 16:04:35 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
|
2011-12-22 20:06:58 +01:00
|
|
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
|
2013-01-12 16:04:35 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
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),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws13(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
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"
|
|
|
|
"Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 13\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2011-12-22 20:06:58 +01:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
2011-12-22 20:19:05 +01:00
|
|
|
%% text
|
2011-12-22 20:06:58 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
|
|
|
|
16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
2011-12-22 20:19:05 +01:00
|
|
|
%% binary (empty)
|
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),
|
|
|
|
%% binary
|
|
|
|
ok = gen_tcp:send(Socket, << 16#82, 16#85, 16#37, 16#fa, 16#21, 16#3d,
|
|
|
|
16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
%% Receives.
|
2011-12-22 20:06:58 +01:00
|
|
|
{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),
|
2013-01-12 16:04:35 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
|
2011-12-22 20:06:58 +01:00
|
|
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
|
2013-01-12 16:04:35 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
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.
|
|
|
|
|
|
|
|
ws_deflate(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
2013-07-02 13:44:28 +02:00
|
|
|
[binary, {active, false}, {packet, raw}, {nodelay, true}]),
|
2013-04-15 18:36:33 +02:00
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_echo 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2013-04-15 18:36:33 +02:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
{"sec-websocket-extensions", "x-webkit-deflate-frame"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
|
2013-07-02 13:44:28 +02:00
|
|
|
Mask = 16#11223344,
|
|
|
|
Hello = << 242, 72, 205, 201, 201, 7, 0 >>,
|
2014-04-20 22:20:54 +02:00
|
|
|
MaskedHello = do_mask(Hello, Mask, <<>>),
|
2013-07-02 13:44:28 +02:00
|
|
|
|
|
|
|
% send compressed text frame containing the Hello string
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32,
|
|
|
|
MaskedHello/binary >>),
|
2013-04-15 18:36:33 +02:00
|
|
|
% receive compressed text frame containing the Hello string
|
2013-07-02 13:44:28 +02:00
|
|
|
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>}
|
2013-04-15 18:36:33 +02:00
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
|
2013-07-02 13:44:28 +02:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
|
|
|
{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_deflate_chunks(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}, {nodelay, true}]),
|
2013-04-15 18:36:33 +02:00
|
|
|
ok = gen_tcp:send(Socket, [
|
2013-07-02 13:44:28 +02:00
|
|
|
"GET /ws_echo 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2013-07-02 13:44:28 +02:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
{"sec-websocket-extensions", "x-webkit-deflate-frame"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
|
|
|
|
Mask = 16#11223344,
|
|
|
|
Hello = << 242, 72, 205, 201, 201, 7, 0 >>,
|
2014-04-20 22:20:54 +02:00
|
|
|
MaskedHello = do_mask(Hello, Mask, <<>>),
|
2013-07-02 13:44:28 +02:00
|
|
|
|
|
|
|
% send compressed text frame containing the Hello string
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32,
|
|
|
|
(binary:part(MaskedHello, 0, 4))/binary >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, binary:part(MaskedHello, 4, 3)),
|
|
|
|
|
|
|
|
% receive compressed text frame containing the Hello string
|
|
|
|
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
|
|
|
{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_deflate_fragments(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}, {nodelay, true}]),
|
2013-04-15 18:36:33 +02:00
|
|
|
ok = gen_tcp:send(Socket, [
|
2013-07-02 13:44:28 +02:00
|
|
|
"GET /ws_echo 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2013-07-02 13:44:28 +02:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
{"sec-websocket-extensions", "x-webkit-deflate-frame"}
|
|
|
|
= lists:keyfind("sec-websocket-extensions", 1, Headers),
|
|
|
|
|
|
|
|
Mask = 16#11223344,
|
|
|
|
Hello = << 242, 72, 205, 201, 201, 7, 0 >>,
|
|
|
|
|
|
|
|
% send compressed text frame containing the Hello string
|
|
|
|
% as 2 separate fragments
|
|
|
|
ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32,
|
2014-04-20 22:20:54 +02:00
|
|
|
(do_mask(binary:part(Hello, 0, 4), Mask, <<>>))/binary >>),
|
2013-07-02 13:44:28 +02:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32,
|
2014-04-20 22:20:54 +02:00
|
|
|
(do_mask(binary:part(Hello, 4, 3), Mask, <<>>))/binary >>),
|
2013-07-02 13:44:28 +02:00
|
|
|
% receive compressed text frame containing the Hello string
|
|
|
|
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>}
|
2013-04-15 18:36:33 +02:00
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
2011-12-22 20:06:58 +01:00
|
|
|
ok.
|
|
|
|
|
2012-12-02 21:37:24 +01:00
|
|
|
ws_send_close(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_send_close 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-12-02 21:37:24 +01:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
%% We catch all frames at once and check them directly.
|
|
|
|
{ok, Many} = gen_tcp:recv(Socket, 8, 6000),
|
|
|
|
<< 1:1, 0:3, 1:4, 0:1, 4:7, "send",
|
|
|
|
1:1, 0:3, 8:4, 0:8 >> = Many,
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws_send_close_payload(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_send_close_payload 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-12-02 21:37:24 +01:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
%% We catch all frames at once and check them directly.
|
2012-12-08 19:11:56 +01:00
|
|
|
{ok, Many} = gen_tcp:recv(Socket, 20, 6000),
|
2012-12-02 21:37:24 +01:00
|
|
|
<< 1:1, 0:3, 1:4, 0:1, 4:7, "send",
|
2012-12-08 19:11:56 +01:00
|
|
|
1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >> = Many,
|
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) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_send_many 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-10-11 21:46:43 +02:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
2012-11-27 17:31:54 +01:00
|
|
|
%% We catch all frames at once and check them directly.
|
|
|
|
{ok, Many} = gen_tcp:recv(Socket, 18, 6000),
|
|
|
|
<< 1:1, 0:3, 1:4, 0:1, 3:7, "one",
|
|
|
|
1:1, 0:3, 1:4, 0:1, 3:7, "two",
|
|
|
|
1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >> = Many,
|
2013-01-14 16:20:33 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
2012-10-11 21:46:43 +02:00
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2012-04-01 18:58:28 -07:00
|
|
|
ws_text_fragments(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
2013-04-22 15:55:22 +02:00
|
|
|
"GET /ws_echo HTTP/1.1\r\n"
|
2012-04-01 18:58:28 -07: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),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-04-01 18:58:28 -07:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
<< 0:1, 0:3, 1:4, 1:1, 5:7 >>,
|
|
|
|
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
|
|
|
|
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
<< 1:1, 0:3, 0:4, 1:1, 5:7 >>,
|
|
|
|
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
|
|
|
|
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
%% #1
|
|
|
|
<< 0:1, 0:3, 1:4, 1:1, 5:7 >>,
|
|
|
|
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
|
|
|
|
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
|
|
|
|
%% #2
|
|
|
|
<< 0:1, 0:3, 0:4, 1:1, 5:7 >>,
|
|
|
|
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
|
|
|
|
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
|
|
|
|
%% #3
|
|
|
|
<< 1:1, 0:3, 0:4, 1:1, 5:7 >>,
|
|
|
|
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
|
|
|
|
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
2013-01-14 16:20:33 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
2012-04-01 18:58:28 -07:00
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2012-10-11 21:46:43 +02:00
|
|
|
ws_timeout_hibernate(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_timeout_hibernate 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-10-11 21:46:43 +02:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
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.
|
|
|
|
|
2012-12-19 11:34:44 -08:00
|
|
|
ws_timeout_cancel(Config) ->
|
|
|
|
%% Erlang messages to a socket should not cancel the timeout
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_timeout_cancel 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-12-19 11:34:44 -08:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
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) ->
|
|
|
|
%% Erlang messages across a socket should reset the timeout
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_timeout_cancel HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
2013-01-10 21:58:38 +01:00
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Origin: http://localhost\r\n"
|
|
|
|
"Sec-Websocket-Version: 13\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
2012-12-19 11:34:44 -08:00
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
2013-01-10 21:58:38 +01:00
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
2012-12-19 11:34:44 -08:00
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-12-19 11:34:44 -08:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
2013-01-10 21:58:38 +01:00
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
[begin
|
|
|
|
ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
|
|
|
|
16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok = timer:sleep(500)
|
|
|
|
end || _ <- [1, 2, 3, 4]],
|
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.
|
|
|
|
|
2012-12-03 14:13:46 +01:00
|
|
|
ws_upgrade_with_opts(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_upgrade_with_opts 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: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2014-04-20 22:20:54 +02:00
|
|
|
[Headers, <<>>] = do_decode_headers(
|
2012-12-03 14:13:46 +01:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
{ok, Response} = gen_tcp:recv(Socket, 9, 6000),
|
|
|
|
<< 1:1, 0:3, 1:4, 0:1, 7:7, "success" >> = Response,
|
2013-01-14 16:20:33 +01:00
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
|
2012-12-03 14:13:46 +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),
|
|
|
|
ok.
|
|
|
|
|
2012-10-11 21:46:43 +02:00
|
|
|
%% Internal.
|
2012-04-01 18:58:28 -07:00
|
|
|
|
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,
|
2014-04-20 22:20:54 +02:00
|
|
|
do_decode_headers(erlang:decode_packet(httph, Rest, []),
|
2011-12-22 20:06:58 +01:00
|
|
|
[{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 >>.
|