ws_SUITE.erl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. %% Copyright (c) 2011-2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(ws_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. %% ct.
  20. all() ->
  21. [{group, autobahn}, {group, ws}].
  22. groups() ->
  23. BaseTests = ct_helper:all(?MODULE) -- [autobahn_fuzzingclient],
  24. [{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}].
  25. init_per_group(Name = autobahn, Config) ->
  26. %% Some systems have it named pip2.
  27. Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
  28. case string:str(Out, "autobahntestsuite") of
  29. 0 ->
  30. ct:print("Skipping the autobahn group because the "
  31. "Autobahn Test Suite is not installed.~nTo install it, "
  32. "please follow the instructions on this page:~n~n "
  33. "http://autobahn.ws/testsuite/installation.html"),
  34. {skip, "Autobahn Test Suite not installed."};
  35. _ ->
  36. {ok, _} = cowboy:start_clear(Name, [{port, 33080}], #{
  37. env => #{dispatch => init_dispatch()}
  38. }),
  39. Config
  40. end;
  41. init_per_group(Name = ws, Config) ->
  42. cowboy_test:init_http(Name, #{
  43. env => #{dispatch => init_dispatch()}
  44. }, Config).
  45. end_per_group(Listener, _Config) ->
  46. cowboy:stop_listener(Listener).
  47. %% Dispatch configuration.
  48. init_dispatch() ->
  49. cowboy_router:compile([
  50. {"localhost", [
  51. {"/ws_echo", ws_echo, []},
  52. {"/ws_echo_timer", ws_echo_timer, []},
  53. {"/ws_init", ws_init_h, []},
  54. {"/ws_init_shutdown", ws_init_shutdown, []},
  55. {"/ws_send_many", ws_send_many, [
  56. {sequence, [
  57. {text, <<"one">>},
  58. {text, <<"two">>},
  59. {text, <<"seven!">>}]}
  60. ]},
  61. {"/ws_send_close", ws_send_many, [
  62. {sequence, [
  63. {text, <<"send">>},
  64. close,
  65. {text, <<"won't be received">>}]}
  66. ]},
  67. {"/ws_send_close_payload", ws_send_many, [
  68. {sequence, [
  69. {text, <<"send">>},
  70. {close, 1001, <<"some text!">>},
  71. {text, <<"won't be received">>}]}
  72. ]},
  73. {"/ws_subprotocol", ws_subprotocol, []},
  74. {"/terminate", ws_terminate_h, []},
  75. {"/ws_timeout_hibernate", ws_timeout_hibernate, []},
  76. {"/ws_timeout_cancel", ws_timeout_cancel, []},
  77. {"/ws_max_frame_size", ws_max_frame_size, []}
  78. ]}
  79. ]).
  80. %% Tests.
  81. autobahn_fuzzingclient(Config) ->
  82. doc("Autobahn test suite for the Websocket protocol."),
  83. Self = self(),
  84. spawn_link(fun() -> start_port(Config, Self) end),
  85. receive autobahn_exit -> ok end,
  86. ct:log("<h2><a href=\"log_private/reports/servers/index.html\">Full report</a></h2>~n"),
  87. Report = config(priv_dir, Config) ++ "reports/servers/index.html",
  88. ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
  89. {ok, HTML} = file:read_file(Report),
  90. case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
  91. true -> error(failed);
  92. false -> ok
  93. end.
  94. start_port(Config, Pid) ->
  95. Port = open_port({spawn, "wstest -m fuzzingclient -s " ++ config(data_dir, Config) ++ "client.json"},
  96. [{line, 10000}, {cd, config(priv_dir, Config)}, binary, eof]),
  97. receive_infinity(Port, Pid).
  98. receive_infinity(Port, Pid) ->
  99. receive
  100. {Port, {data, {eol, Line}}} ->
  101. io:format(user, "~s~n", [Line]),
  102. receive_infinity(Port, Pid);
  103. {Port, eof} ->
  104. Pid ! autobahn_exit
  105. end.
  106. ws0(Config) ->
  107. doc("Websocket version 0 (hixie-76 draft) is no longer supported."),
  108. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  109. ok = gen_tcp:send(Socket,
  110. "GET /ws_echo_timer HTTP/1.1\r\n"
  111. "Host: localhost\r\n"
  112. "Connection: Upgrade\r\n"
  113. "Upgrade: WebSocket\r\n"
  114. "Origin: http://localhost\r\n"
  115. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  116. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  117. "\r\n"),
  118. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  119. {ok, {http_response, {1, 1}, 400, _}, _} = erlang:decode_packet(http, Handshake, []),
  120. ok.
  121. ws7(Config) ->
  122. doc("Websocket version 7 (draft) is supported."),
  123. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  124. ok = gen_tcp:send(Socket, [
  125. "GET /ws_echo_timer HTTP/1.1\r\n"
  126. "Host: localhost\r\n"
  127. "Connection: Upgrade\r\n"
  128. "Upgrade: websocket\r\n"
  129. "Sec-WebSocket-Origin: http://localhost\r\n"
  130. "Sec-WebSocket-Version: 7\r\n"
  131. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  132. "\r\n"]),
  133. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  134. {ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  135. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  136. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  137. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  138. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  139. do_ws_version(Socket).
  140. ws8(Config) ->
  141. doc("Websocket version 8 (draft) is supported."),
  142. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  143. ok = gen_tcp:send(Socket, [
  144. "GET /ws_echo_timer HTTP/1.1\r\n"
  145. "Host: localhost\r\n"
  146. "Connection: Upgrade\r\n"
  147. "Upgrade: websocket\r\n"
  148. "Sec-WebSocket-Origin: http://localhost\r\n"
  149. "Sec-WebSocket-Version: 8\r\n"
  150. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  151. "\r\n"]),
  152. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  153. {ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  154. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  155. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  156. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  157. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  158. do_ws_version(Socket).
  159. ws13(Config) ->
  160. doc("Websocket version 13 (RFC) is supported."),
  161. {ok, Socket, _} = do_handshake("/ws_echo_timer", Config),
  162. do_ws_version(Socket).
  163. do_ws_version(Socket) ->
  164. %% Masked text hello echoed back clear by the server.
  165. Mask = 16#37fa213d,
  166. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  167. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  168. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  169. %% Empty binary frame echoed back.
  170. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 0:7, 0:32 >>),
  171. {ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  172. %% Masked binary hello echoed back clear by the server.
  173. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  174. {ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  175. %% Frames sent on timer by the handler.
  176. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} = gen_tcp:recv(Socket, 0, 6000),
  177. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
  178. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
  179. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
  180. %% Client-initiated ping/pong.
  181. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>),
  182. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  183. %% Client-initiated close.
  184. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  185. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  186. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  187. ok.
  188. ws_init_return_ok(Config) ->
  189. doc("Handler does nothing."),
  190. {ok, Socket, _} = do_handshake("/ws_init?ok", Config),
  191. %% The handler does nothing; nothing should happen here.
  192. {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
  193. ok.
  194. ws_init_return_ok_hibernate(Config) ->
  195. doc("Handler does nothing; hibernates."),
  196. {ok, Socket, _} = do_handshake("/ws_init?ok_hibernate", Config),
  197. %% The handler does nothing; nothing should happen here.
  198. {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
  199. ok.
  200. ws_init_return_reply(Config) ->
  201. doc("Handler sends a text frame just after the handshake."),
  202. {ok, Socket, _} = do_handshake("/ws_init?reply", Config),
  203. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  204. ok.
  205. ws_init_return_reply_hibernate(Config) ->
  206. doc("Handler sends a text frame just after the handshake and then hibernates."),
  207. {ok, Socket, _} = do_handshake("/ws_init?reply_hibernate", Config),
  208. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  209. ok.
  210. ws_init_return_reply_close(Config) ->
  211. doc("Handler closes immediately after the handshake."),
  212. {ok, Socket, _} = do_handshake("/ws_init?reply_close", Config),
  213. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  214. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  215. ok.
  216. ws_init_return_reply_close_hibernate(Config) ->
  217. doc("Handler closes immediately after the handshake, then attempts to hibernate."),
  218. {ok, Socket, _} = do_handshake("/ws_init?reply_close_hibernate", Config),
  219. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  220. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  221. ok.
  222. ws_init_return_reply_many(Config) ->
  223. doc("Handler sends many frames just after the handshake."),
  224. {ok, Socket, _} = do_handshake("/ws_init?reply_many", Config),
  225. %% We catch all frames at once and check them directly.
  226. {ok, <<
  227. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  228. 1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000),
  229. ok.
  230. ws_init_return_reply_many_hibernate(Config) ->
  231. doc("Handler sends many frames just after the handshake and then hibernates."),
  232. {ok, Socket, _} = do_handshake("/ws_init?reply_many_hibernate", Config),
  233. %% We catch all frames at once and check them directly.
  234. {ok, <<
  235. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  236. 1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000),
  237. ok.
  238. ws_init_return_reply_many_close(Config) ->
  239. doc("Handler sends many frames including a close frame just after the handshake."),
  240. {ok, Socket, _} = do_handshake("/ws_init?reply_many_close", Config),
  241. %% We catch all frames at once and check them directly.
  242. {ok, <<
  243. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  244. 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000),
  245. ok.
  246. ws_init_return_reply_many_close_hibernate(Config) ->
  247. doc("Handler sends many frames including a close frame just after the handshake and then hibernates."),
  248. {ok, Socket, _} = do_handshake("/ws_init?reply_many_close_hibernate", Config),
  249. %% We catch all frames at once and check them directly.
  250. {ok, <<
  251. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  252. 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000),
  253. ok.
  254. ws_init_return_stop(Config) ->
  255. doc("Handler closes immediately after the handshake."),
  256. {ok, Socket, _} = do_handshake("/ws_init?stop", Config),
  257. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  258. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  259. ok.
  260. ws_init_shutdown_before_handshake(Config) ->
  261. doc("Handler stops before Websocket handshake."),
  262. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  263. ok = gen_tcp:send(Socket, [
  264. "GET /ws_init_shutdown HTTP/1.1\r\n"
  265. "Host: localhost\r\n"
  266. "Connection: Upgrade\r\n"
  267. "Origin: http://localhost\r\n"
  268. "Sec-WebSocket-Version: 13\r\n"
  269. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  270. "Upgrade: websocket\r\n"
  271. "\r\n"]),
  272. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  273. {ok, {http_response, {1, 1}, 403, _}, _Rest} = erlang:decode_packet(http, Handshake, []),
  274. ok.
  275. ws_max_frame_size_close(Config) ->
  276. doc("Server closes connection when frame size exceeds max_frame_size option"),
  277. %% max_frame_size is set to 8 bytes in ws_max_frame_size.
  278. {ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
  279. Mask = 16#11223344,
  280. MaskedHello = do_mask(<<"HelloHello">>, Mask, <<>>),
  281. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 10:7, Mask:32, MaskedHello/binary >>),
  282. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  283. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  284. ok.
  285. ws_max_frame_size_final_fragment_close(Config) ->
  286. doc("Server closes connection when final fragmented frame "
  287. "exceeds max_frame_size option"),
  288. %% max_frame_size is set to 8 bytes in ws_max_frame_size.
  289. {ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
  290. Mask = 16#11223344,
  291. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  292. ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  293. ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  294. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  295. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  296. ok.
  297. ws_max_frame_size_intermediate_fragment_close(Config) ->
  298. doc("Server closes connection when intermediate fragmented frame "
  299. "exceeds max_frame_size option"),
  300. %% max_frame_size is set to 8 bytes in ws_max_frame_size.
  301. {ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
  302. Mask = 16#11223344,
  303. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  304. ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  305. ok = gen_tcp:send(Socket, << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  306. ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  307. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  308. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  309. ok.
  310. ws_send_close(Config) ->
  311. doc("Server-initiated close frame ends the connection."),
  312. {ok, Socket, _} = do_handshake("/ws_send_close", Config),
  313. %% We catch all frames at once and check them directly.
  314. {ok, <<
  315. 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  316. 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 8, 6000),
  317. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  318. ok.
  319. ws_send_close_payload(Config) ->
  320. doc("Server-initiated close frame with payload ends the connection."),
  321. {ok, Socket, _} = do_handshake("/ws_send_close_payload", Config),
  322. %% We catch all frames at once and check them directly.
  323. {ok, <<
  324. 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  325. 1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >>} = gen_tcp:recv(Socket, 20, 6000),
  326. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  327. ok.
  328. ws_send_many(Config) ->
  329. doc("Server sends many frames in a single reply."),
  330. {ok, Socket, _} = do_handshake("/ws_send_many", Config),
  331. %% We catch all frames at once and check them directly.
  332. {ok, <<
  333. 1:1, 0:3, 1:4, 0:1, 3:7, "one",
  334. 1:1, 0:3, 1:4, 0:1, 3:7, "two",
  335. 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >>} = gen_tcp:recv(Socket, 18, 6000),
  336. ok.
  337. ws_single_bytes(Config) ->
  338. doc("Client sends a text frame one byte at a time."),
  339. {ok, Socket, _} = do_handshake("/ws_echo", Config),
  340. %% We sleep between sends to make sure only one byte is sent.
  341. ok = gen_tcp:send(Socket, << 16#81 >>), timer:sleep(100),
  342. ok = gen_tcp:send(Socket, << 16#85 >>), timer:sleep(100),
  343. ok = gen_tcp:send(Socket, << 16#37 >>), timer:sleep(100),
  344. ok = gen_tcp:send(Socket, << 16#fa >>), timer:sleep(100),
  345. ok = gen_tcp:send(Socket, << 16#21 >>), timer:sleep(100),
  346. ok = gen_tcp:send(Socket, << 16#3d >>), timer:sleep(100),
  347. ok = gen_tcp:send(Socket, << 16#7f >>), timer:sleep(100),
  348. ok = gen_tcp:send(Socket, << 16#9f >>), timer:sleep(100),
  349. ok = gen_tcp:send(Socket, << 16#4d >>), timer:sleep(100),
  350. ok = gen_tcp:send(Socket, << 16#51 >>), timer:sleep(100),
  351. ok = gen_tcp:send(Socket, << 16#58 >>),
  352. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  353. ok.
  354. ws_subprotocol(Config) ->
  355. doc("Websocket sub-protocol negotiation."),
  356. {ok, _, Headers} = do_handshake("/ws_subprotocol",
  357. "Sec-WebSocket-Protocol: foo, bar\r\n", Config),
  358. {_, "foo"} = lists:keyfind("sec-websocket-protocol", 1, Headers),
  359. ok.
  360. ws_terminate(Config) ->
  361. doc("The Req object is kept in a more compact form by default."),
  362. {ok, Socket, _} = do_handshake("/terminate",
  363. "x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
  364. %% Send a close frame.
  365. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  366. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  367. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  368. %% Confirm terminate/3 was called with a compacted Req.
  369. receive {terminate, _, Req} ->
  370. true = maps:is_key(path, Req),
  371. false = maps:is_key(headers, Req),
  372. ok
  373. after 1000 ->
  374. error(timeout)
  375. end.
  376. ws_terminate_fun(Config) ->
  377. doc("A function can be given to filter the Req object."),
  378. {ok, Socket, _} = do_handshake("/terminate?req_filter",
  379. "x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
  380. %% Send a close frame.
  381. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  382. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  383. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  384. %% Confirm terminate/3 was called with a compacted Req.
  385. receive {terminate, _, Req} ->
  386. filtered = Req,
  387. ok
  388. after 1000 ->
  389. error(timeout)
  390. end.
  391. ws_text_fragments(Config) ->
  392. doc("Client sends fragmented text frames."),
  393. {ok, Socket, _} = do_handshake("/ws_echo", Config),
  394. %% Send two "Hello" over two fragments and two sends.
  395. Mask = 16#37fa213d,
  396. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  397. ok = gen_tcp:send(Socket, << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  398. ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  399. {ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
  400. %% Send three "Hello" over three fragments and one send.
  401. ok = gen_tcp:send(Socket, [
  402. << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
  403. << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
  404. << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>]),
  405. {ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
  406. ok.
  407. ws_timeout_hibernate(Config) ->
  408. doc("Server-initiated close on timeout with hibernating process."),
  409. {ok, Socket, _} = do_handshake("/ws_timeout_hibernate", Config),
  410. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  411. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  412. ok.
  413. ws_timeout_no_cancel(Config) ->
  414. doc("Server-initiated timeout is not influenced by reception of Erlang messages."),
  415. {ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
  416. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  417. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  418. ok.
  419. ws_timeout_reset(Config) ->
  420. doc("Server-initiated timeout is reset when client sends more data."),
  421. {ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
  422. %% Send and receive back a frame a few times.
  423. Mask = 16#37fa213d,
  424. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  425. [begin
  426. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  427. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  428. timer:sleep(500)
  429. end || _ <- [1, 2, 3, 4]],
  430. %% Timeout will occur after we stop sending data.
  431. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  432. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  433. ok.
  434. ws_webkit_deflate(Config) ->
  435. doc("x-webkit-deflate-frame compression."),
  436. {ok, Socket, Headers} = do_handshake("/ws_echo",
  437. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  438. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  439. %% Send and receive a compressed "Hello" frame.
  440. Mask = 16#11223344,
  441. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  442. MaskedHello = do_mask(CompressedHello, Mask, <<>>),
  443. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary >>),
  444. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  445. %% Client-initiated close.
  446. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  447. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  448. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  449. ok.
  450. ws_webkit_deflate_fragments(Config) ->
  451. doc("Client sends an x-webkit-deflate-frame compressed and fragmented text frame."),
  452. {ok, Socket, Headers} = do_handshake("/ws_echo",
  453. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  454. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  455. %% Send a compressed "Hello" over two fragments and two sends.
  456. Mask = 16#11223344,
  457. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  458. MaskedHello1 = do_mask(binary:part(CompressedHello, 0, 4), Mask, <<>>),
  459. MaskedHello2 = do_mask(binary:part(CompressedHello, 4, 3), Mask, <<>>),
  460. ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32, MaskedHello1/binary >>),
  461. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32, MaskedHello2/binary >>),
  462. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  463. ok.
  464. ws_webkit_deflate_single_bytes(Config) ->
  465. doc("Client sends an x-webkit-deflate-frame compressed text frame one byte at a time."),
  466. {ok, Socket, Headers} = do_handshake("/ws_echo",
  467. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  468. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  469. %% We sleep between sends to make sure only one byte is sent.
  470. Mask = 16#11223344,
  471. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  472. MaskedHello = do_mask(CompressedHello, Mask, <<>>),
  473. ok = gen_tcp:send(Socket, << 16#c1 >>), timer:sleep(100),
  474. ok = gen_tcp:send(Socket, << 16#87 >>), timer:sleep(100),
  475. ok = gen_tcp:send(Socket, << 16#11 >>), timer:sleep(100),
  476. ok = gen_tcp:send(Socket, << 16#22 >>), timer:sleep(100),
  477. ok = gen_tcp:send(Socket, << 16#33 >>), timer:sleep(100),
  478. ok = gen_tcp:send(Socket, << 16#44 >>), timer:sleep(100),
  479. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 0)]), timer:sleep(100),
  480. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 1)]), timer:sleep(100),
  481. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 2)]), timer:sleep(100),
  482. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 3)]), timer:sleep(100),
  483. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 4)]), timer:sleep(100),
  484. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 5)]), timer:sleep(100),
  485. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 6)]),
  486. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  487. ok.
  488. %% Internal.
  489. do_handshake(Path, Config) ->
  490. do_handshake(Path, "", Config).
  491. do_handshake(Path, ExtraHeaders, Config) ->
  492. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
  493. [binary, {active, false}]),
  494. ok = gen_tcp:send(Socket, [
  495. "GET ", Path, " HTTP/1.1\r\n"
  496. "Host: localhost\r\n"
  497. "Connection: Upgrade\r\n"
  498. "Origin: http://localhost\r\n"
  499. "Sec-WebSocket-Version: 13\r\n"
  500. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  501. "Upgrade: websocket\r\n",
  502. ExtraHeaders,
  503. "\r\n"]),
  504. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  505. {ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  506. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  507. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  508. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  509. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  510. {ok, Socket, Headers}.
  511. do_decode_headers({ok, http_eoh, Rest}, Acc) ->
  512. [Acc, Rest];
  513. do_decode_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  514. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  515. do_decode_headers(erlang:decode_packet(httph, Rest, []), [{F(Key), Value}|Acc]).
  516. do_mask(<<>>, _, Acc) ->
  517. Acc;
  518. do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) ->
  519. T = O bxor MaskKey,
  520. do_mask(Rest, MaskKey, << Acc/binary, T:32 >>);
  521. do_mask(<< O:24 >>, MaskKey, Acc) ->
  522. << MaskKey2:24, _:8 >> = << MaskKey:32 >>,
  523. T = O bxor MaskKey2,
  524. << Acc/binary, T:24 >>;
  525. do_mask(<< O:16 >>, MaskKey, Acc) ->
  526. << MaskKey2:16, _:16 >> = << MaskKey:32 >>,
  527. T = O bxor MaskKey2,
  528. << Acc/binary, T:16 >>;
  529. do_mask(<< O:8 >>, MaskKey, Acc) ->
  530. << MaskKey2:8, _:24 >> = << MaskKey:32 >>,
  531. T = O bxor MaskKey2,
  532. << Acc/binary, T:8 >>.