ws_SUITE.erl 21 KB

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