PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ws_SUITE.erl

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