ws_SUITE.erl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. %% Copyright (c) 2011-2014, 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(cowboy_test, [config/2]).
  17. %% ct.
  18. all() ->
  19. [{group, autobahn}, {group, ws}].
  20. groups() ->
  21. BaseTests = cowboy_test:all(?MODULE) -- [autobahn_fuzzingclient],
  22. [{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}].
  23. init_per_suite(Config) ->
  24. Config.
  25. init_per_group(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_http(autobahn, 100, [{port, 33080}], [
  37. {env, [{dispatch, init_dispatch()}]}]),
  38. Config
  39. end;
  40. init_per_group(ws, Config) ->
  41. cowboy:start_http(ws, 100, [{port, 0}], [
  42. {env, [{dispatch, init_dispatch()}]},
  43. {compress, true}
  44. ]),
  45. Port = ranch:get_port(ws),
  46. [{port, Port}|Config].
  47. end_per_group(Listener, _Config) ->
  48. cowboy:stop_listener(Listener),
  49. ok.
  50. %% Dispatch configuration.
  51. init_dispatch() ->
  52. cowboy_router:compile([
  53. {"localhost", [
  54. {"/ws_echo", ws_echo, []},
  55. {"/ws_echo_timer", ws_echo_timer, []},
  56. {"/ws_init_shutdown", ws_init_shutdown, []},
  57. {"/ws_send_many", ws_send_many, [
  58. {sequence, [
  59. {text, <<"one">>},
  60. {text, <<"two">>},
  61. {text, <<"seven!">>}]}
  62. ]},
  63. {"/ws_send_close", ws_send_many, [
  64. {sequence, [
  65. {text, <<"send">>},
  66. close,
  67. {text, <<"won't be received">>}]}
  68. ]},
  69. {"/ws_send_close_payload", ws_send_many, [
  70. {sequence, [
  71. {text, <<"send">>},
  72. {close, 1001, <<"some text!">>},
  73. {text, <<"won't be received">>}]}
  74. ]},
  75. {"/ws_timeout_hibernate", ws_timeout_hibernate, []},
  76. {"/ws_timeout_cancel", ws_timeout_cancel, []},
  77. {"/ws_upgrade_with_opts", ws_upgrade_with_opts,
  78. <<"failure">>}
  79. ]}
  80. ]).
  81. %% Tests.
  82. autobahn_fuzzingclient(Config) ->
  83. Out = os:cmd("cd " ++ config(priv_dir, Config)
  84. ++ " && wstest -m fuzzingclient -s "
  85. ++ config(data_dir, Config) ++ "client.json"),
  86. Report = config(priv_dir, Config) ++ "reports/servers/index.html",
  87. ct:log("<h2><a href=\"~s\">Full report</a></h2>~n", [Report]),
  88. ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
  89. ct:log("~s~n", [Out]),
  90. {ok, HTML} = file:read_file(Report),
  91. case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
  92. true -> error(failed);
  93. false -> ok
  94. end.
  95. %% We do not support hixie76 anymore.
  96. ws0(Config) ->
  97. {port, Port} = lists:keyfind(port, 1, Config),
  98. {ok, Socket} = gen_tcp:connect("localhost", Port,
  99. [binary, {active, false}, {packet, raw}]),
  100. ok = gen_tcp:send(Socket,
  101. "GET /ws_echo_timer HTTP/1.1\r\n"
  102. "Host: localhost\r\n"
  103. "Connection: Upgrade\r\n"
  104. "Upgrade: WebSocket\r\n"
  105. "Origin: http://localhost\r\n"
  106. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  107. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  108. "\r\n"),
  109. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  110. {ok, {http_response, {1, 1}, 400, _}, _}
  111. = erlang:decode_packet(http, Handshake, []).
  112. ws8(Config) ->
  113. {port, Port} = lists:keyfind(port, 1, Config),
  114. {ok, Socket} = gen_tcp:connect("localhost", Port,
  115. [binary, {active, false}, {packet, raw}]),
  116. ok = gen_tcp:send(Socket, [
  117. "GET /ws_echo_timer HTTP/1.1\r\n"
  118. "Host: localhost\r\n"
  119. "Connection: Upgrade\r\n"
  120. "Upgrade: websocket\r\n"
  121. "Sec-WebSocket-Origin: http://localhost\r\n"
  122. "Sec-WebSocket-Version: 8\r\n"
  123. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  124. "\r\n"]),
  125. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  126. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  127. = erlang:decode_packet(http, Handshake, []),
  128. [Headers, <<>>] = do_decode_headers(
  129. erlang:decode_packet(httph, Rest, []), []),
  130. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  131. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  132. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  133. = lists:keyfind("sec-websocket-accept", 1, Headers),
  134. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  135. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  136. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  137. = gen_tcp:recv(Socket, 0, 6000),
  138. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  139. = gen_tcp:recv(Socket, 0, 6000),
  140. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  141. = gen_tcp:recv(Socket, 0, 6000),
  142. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  143. = gen_tcp:recv(Socket, 0, 6000),
  144. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  145. = gen_tcp:recv(Socket, 0, 6000),
  146. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
  147. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  148. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  149. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  150. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  151. ok.
  152. ws8_init_shutdown(Config) ->
  153. {port, Port} = lists:keyfind(port, 1, Config),
  154. {ok, Socket} = gen_tcp:connect("localhost", Port,
  155. [binary, {active, false}, {packet, raw}]),
  156. ok = gen_tcp:send(Socket, [
  157. "GET /ws_init_shutdown HTTP/1.1\r\n"
  158. "Host: localhost\r\n"
  159. "Connection: Upgrade\r\n"
  160. "Upgrade: websocket\r\n"
  161. "Sec-WebSocket-Origin: http://localhost\r\n"
  162. "Sec-WebSocket-Version: 8\r\n"
  163. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  164. "\r\n"]),
  165. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  166. {ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest}
  167. = erlang:decode_packet(http, Handshake, []),
  168. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  169. ok.
  170. ws8_single_bytes(Config) ->
  171. {port, Port} = lists:keyfind(port, 1, Config),
  172. {ok, Socket} = gen_tcp:connect("localhost", Port,
  173. [binary, {active, false}, {packet, raw}]),
  174. ok = gen_tcp:send(Socket, [
  175. "GET /ws_echo_timer HTTP/1.1\r\n"
  176. "Host: localhost\r\n"
  177. "Connection: Upgrade\r\n"
  178. "Upgrade: websocket\r\n"
  179. "Sec-WebSocket-Origin: http://localhost\r\n"
  180. "Sec-WebSocket-Version: 8\r\n"
  181. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  182. "\r\n"]),
  183. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  184. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  185. = erlang:decode_packet(http, Handshake, []),
  186. [Headers, <<>>] = do_decode_headers(
  187. erlang:decode_packet(httph, Rest, []), []),
  188. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  189. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  190. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  191. = lists:keyfind("sec-websocket-accept", 1, Headers),
  192. ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
  193. ok = timer:sleep(100), %% sleep for a period
  194. ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
  195. ok = timer:sleep(100),
  196. ok = gen_tcp:send(Socket, << 16#37 >>),
  197. ok = timer:sleep(100),
  198. ok = gen_tcp:send(Socket, << 16#fa >>),
  199. ok = timer:sleep(100),
  200. ok = gen_tcp:send(Socket, << 16#21 >>),
  201. ok = timer:sleep(100),
  202. ok = gen_tcp:send(Socket, << 16#3d >>),
  203. ok = timer:sleep(100),
  204. ok = gen_tcp:send(Socket, << 16#7f >>),
  205. ok = timer:sleep(100),
  206. ok = gen_tcp:send(Socket, << 16#9f >>),
  207. ok = timer:sleep(100),
  208. ok = gen_tcp:send(Socket, << 16#4d >>),
  209. ok = timer:sleep(100),
  210. ok = gen_tcp:send(Socket, << 16#51 >>),
  211. ok = timer:sleep(100),
  212. ok = gen_tcp:send(Socket, << 16#58 >>),
  213. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  214. = gen_tcp:recv(Socket, 0, 6000),
  215. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  216. = gen_tcp:recv(Socket, 0, 6000),
  217. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  218. = gen_tcp:recv(Socket, 0, 6000),
  219. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  220. = gen_tcp:recv(Socket, 0, 6000),
  221. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  222. = gen_tcp:recv(Socket, 0, 6000),
  223. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
  224. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  225. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  226. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  227. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  228. ok.
  229. ws13(Config) ->
  230. {port, Port} = lists:keyfind(port, 1, Config),
  231. {ok, Socket} = gen_tcp:connect("localhost", Port,
  232. [binary, {active, false}, {packet, raw}]),
  233. ok = gen_tcp:send(Socket, [
  234. "GET /ws_echo_timer HTTP/1.1\r\n"
  235. "Host: localhost\r\n"
  236. "Connection: Upgrade\r\n"
  237. "Origin: http://localhost\r\n"
  238. "Sec-WebSocket-Version: 13\r\n"
  239. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  240. "Upgrade: websocket\r\n"
  241. "\r\n"]),
  242. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  243. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  244. = erlang:decode_packet(http, Handshake, []),
  245. [Headers, <<>>] = do_decode_headers(
  246. erlang:decode_packet(httph, Rest, []), []),
  247. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  248. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  249. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  250. = lists:keyfind("sec-websocket-accept", 1, Headers),
  251. %% text
  252. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  253. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  254. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  255. = gen_tcp:recv(Socket, 0, 6000),
  256. %% binary (empty)
  257. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 0:7, 0:32 >>),
  258. {ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  259. %% binary
  260. ok = gen_tcp:send(Socket, << 16#82, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  261. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  262. {ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>}
  263. = gen_tcp:recv(Socket, 0, 6000),
  264. %% Receives.
  265. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  266. = gen_tcp:recv(Socket, 0, 6000),
  267. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  268. = gen_tcp:recv(Socket, 0, 6000),
  269. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  270. = gen_tcp:recv(Socket, 0, 6000),
  271. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  272. = gen_tcp:recv(Socket, 0, 6000),
  273. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
  274. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  275. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  276. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  277. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  278. ok.
  279. ws_deflate(Config) ->
  280. {port, Port} = lists:keyfind(port, 1, Config),
  281. {ok, Socket} = gen_tcp:connect("localhost", Port,
  282. [binary, {active, false}, {packet, raw}, {nodelay, true}]),
  283. ok = gen_tcp:send(Socket, [
  284. "GET /ws_echo HTTP/1.1\r\n"
  285. "Host: localhost\r\n"
  286. "Connection: Upgrade\r\n"
  287. "Upgrade: websocket\r\n"
  288. "Sec-WebSocket-Origin: http://localhost\r\n"
  289. "Sec-WebSocket-Version: 8\r\n"
  290. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  291. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n"
  292. "\r\n"]),
  293. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  294. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  295. = erlang:decode_packet(http, Handshake, []),
  296. [Headers, <<>>] = do_decode_headers(
  297. erlang:decode_packet(httph, Rest, []), []),
  298. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  299. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  300. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  301. = lists:keyfind("sec-websocket-accept", 1, Headers),
  302. {"sec-websocket-extensions", "x-webkit-deflate-frame"}
  303. = lists:keyfind("sec-websocket-extensions", 1, Headers),
  304. Mask = 16#11223344,
  305. Hello = << 242, 72, 205, 201, 201, 7, 0 >>,
  306. MaskedHello = do_mask(Hello, Mask, <<>>),
  307. % send compressed text frame containing the Hello string
  308. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32,
  309. MaskedHello/binary >>),
  310. % receive compressed text frame containing the Hello string
  311. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>}
  312. = gen_tcp:recv(Socket, 0, 6000),
  313. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  314. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  315. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  316. ok.
  317. ws_deflate_chunks(Config) ->
  318. {port, Port} = lists:keyfind(port, 1, Config),
  319. {ok, Socket} = gen_tcp:connect("localhost", Port,
  320. [binary, {active, false}, {packet, raw}, {nodelay, true}]),
  321. ok = gen_tcp:send(Socket, [
  322. "GET /ws_echo HTTP/1.1\r\n"
  323. "Host: localhost\r\n"
  324. "Connection: Upgrade\r\n"
  325. "Upgrade: websocket\r\n"
  326. "Sec-WebSocket-Origin: http://localhost\r\n"
  327. "Sec-WebSocket-Version: 8\r\n"
  328. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  329. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n"
  330. "\r\n"]),
  331. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  332. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  333. = erlang:decode_packet(http, Handshake, []),
  334. [Headers, <<>>] = do_decode_headers(
  335. erlang:decode_packet(httph, Rest, []), []),
  336. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  337. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  338. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  339. = lists:keyfind("sec-websocket-accept", 1, Headers),
  340. {"sec-websocket-extensions", "x-webkit-deflate-frame"}
  341. = lists:keyfind("sec-websocket-extensions", 1, Headers),
  342. Mask = 16#11223344,
  343. Hello = << 242, 72, 205, 201, 201, 7, 0 >>,
  344. MaskedHello = do_mask(Hello, Mask, <<>>),
  345. % send compressed text frame containing the Hello string
  346. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32,
  347. (binary:part(MaskedHello, 0, 4))/binary >>),
  348. ok = timer:sleep(100),
  349. ok = gen_tcp:send(Socket, binary:part(MaskedHello, 4, 3)),
  350. % receive compressed text frame containing the Hello string
  351. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>}
  352. = gen_tcp:recv(Socket, 0, 6000),
  353. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  354. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  355. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  356. ok.
  357. ws_deflate_fragments(Config) ->
  358. {port, Port} = lists:keyfind(port, 1, Config),
  359. {ok, Socket} = gen_tcp:connect("localhost", Port,
  360. [binary, {active, false}, {packet, raw}, {nodelay, true}]),
  361. ok = gen_tcp:send(Socket, [
  362. "GET /ws_echo HTTP/1.1\r\n"
  363. "Host: localhost\r\n"
  364. "Connection: Upgrade\r\n"
  365. "Upgrade: websocket\r\n"
  366. "Sec-WebSocket-Origin: http://localhost\r\n"
  367. "Sec-WebSocket-Version: 8\r\n"
  368. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  369. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n"
  370. "\r\n"]),
  371. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  372. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  373. = erlang:decode_packet(http, Handshake, []),
  374. [Headers, <<>>] = do_decode_headers(
  375. erlang:decode_packet(httph, Rest, []), []),
  376. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  377. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  378. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  379. = lists:keyfind("sec-websocket-accept", 1, Headers),
  380. {"sec-websocket-extensions", "x-webkit-deflate-frame"}
  381. = lists:keyfind("sec-websocket-extensions", 1, Headers),
  382. Mask = 16#11223344,
  383. Hello = << 242, 72, 205, 201, 201, 7, 0 >>,
  384. % send compressed text frame containing the Hello string
  385. % as 2 separate fragments
  386. ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32,
  387. (do_mask(binary:part(Hello, 0, 4), Mask, <<>>))/binary >>),
  388. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32,
  389. (do_mask(binary:part(Hello, 4, 3), Mask, <<>>))/binary >>),
  390. % receive compressed text frame containing the Hello string
  391. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>}
  392. = gen_tcp:recv(Socket, 0, 6000),
  393. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  394. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  395. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  396. ok.
  397. ws_send_close(Config) ->
  398. {port, Port} = lists:keyfind(port, 1, Config),
  399. {ok, Socket} = gen_tcp:connect("localhost", Port,
  400. [binary, {active, false}, {packet, raw}]),
  401. ok = gen_tcp:send(Socket, [
  402. "GET /ws_send_close HTTP/1.1\r\n"
  403. "Host: localhost\r\n"
  404. "Connection: Upgrade\r\n"
  405. "Upgrade: websocket\r\n"
  406. "Sec-WebSocket-Origin: http://localhost\r\n"
  407. "Sec-WebSocket-Version: 8\r\n"
  408. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  409. "\r\n"]),
  410. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  411. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  412. = erlang:decode_packet(http, Handshake, []),
  413. [Headers, <<>>] = do_decode_headers(
  414. erlang:decode_packet(httph, Rest, []), []),
  415. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  416. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  417. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  418. = lists:keyfind("sec-websocket-accept", 1, Headers),
  419. %% We catch all frames at once and check them directly.
  420. {ok, Many} = gen_tcp:recv(Socket, 8, 6000),
  421. << 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  422. 1:1, 0:3, 8:4, 0:8 >> = Many,
  423. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  424. ok.
  425. ws_send_close_payload(Config) ->
  426. {port, Port} = lists:keyfind(port, 1, Config),
  427. {ok, Socket} = gen_tcp:connect("localhost", Port,
  428. [binary, {active, false}, {packet, raw}]),
  429. ok = gen_tcp:send(Socket, [
  430. "GET /ws_send_close_payload HTTP/1.1\r\n"
  431. "Host: localhost\r\n"
  432. "Connection: Upgrade\r\n"
  433. "Upgrade: websocket\r\n"
  434. "Sec-WebSocket-Origin: http://localhost\r\n"
  435. "Sec-WebSocket-Version: 8\r\n"
  436. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  437. "\r\n"]),
  438. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  439. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  440. = erlang:decode_packet(http, Handshake, []),
  441. [Headers, <<>>] = do_decode_headers(
  442. erlang:decode_packet(httph, Rest, []), []),
  443. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  444. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  445. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  446. = lists:keyfind("sec-websocket-accept", 1, Headers),
  447. %% We catch all frames at once and check them directly.
  448. {ok, Many} = gen_tcp:recv(Socket, 20, 6000),
  449. << 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  450. 1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >> = Many,
  451. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  452. ok.
  453. ws_send_many(Config) ->
  454. {port, Port} = lists:keyfind(port, 1, Config),
  455. {ok, Socket} = gen_tcp:connect("localhost", Port,
  456. [binary, {active, false}, {packet, raw}]),
  457. ok = gen_tcp:send(Socket, [
  458. "GET /ws_send_many HTTP/1.1\r\n"
  459. "Host: localhost\r\n"
  460. "Connection: Upgrade\r\n"
  461. "Upgrade: websocket\r\n"
  462. "Sec-WebSocket-Origin: http://localhost\r\n"
  463. "Sec-WebSocket-Version: 8\r\n"
  464. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  465. "\r\n"]),
  466. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  467. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  468. = erlang:decode_packet(http, Handshake, []),
  469. [Headers, <<>>] = do_decode_headers(
  470. erlang:decode_packet(httph, Rest, []), []),
  471. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  472. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  473. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  474. = lists:keyfind("sec-websocket-accept", 1, Headers),
  475. %% We catch all frames at once and check them directly.
  476. {ok, Many} = gen_tcp:recv(Socket, 18, 6000),
  477. << 1:1, 0:3, 1:4, 0:1, 3:7, "one",
  478. 1:1, 0:3, 1:4, 0:1, 3:7, "two",
  479. 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >> = Many,
  480. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  481. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  482. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  483. ok.
  484. ws_text_fragments(Config) ->
  485. {port, Port} = lists:keyfind(port, 1, Config),
  486. {ok, Socket} = gen_tcp:connect("localhost", Port,
  487. [binary, {active, false}, {packet, raw}]),
  488. ok = gen_tcp:send(Socket, [
  489. "GET /ws_echo HTTP/1.1\r\n"
  490. "Host: localhost\r\n"
  491. "Connection: Upgrade\r\n"
  492. "Upgrade: websocket\r\n"
  493. "Sec-WebSocket-Origin: http://localhost\r\n"
  494. "Sec-WebSocket-Version: 8\r\n"
  495. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  496. "\r\n"]),
  497. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  498. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  499. = erlang:decode_packet(http, Handshake, []),
  500. [Headers, <<>>] = do_decode_headers(
  501. erlang:decode_packet(httph, Rest, []), []),
  502. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  503. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  504. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  505. = lists:keyfind("sec-websocket-accept", 1, Headers),
  506. ok = gen_tcp:send(Socket, [
  507. << 0:1, 0:3, 1:4, 1:1, 5:7 >>,
  508. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  509. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
  510. ok = gen_tcp:send(Socket, [
  511. << 1:1, 0:3, 0:4, 1:1, 5:7 >>,
  512. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  513. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
  514. {ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>}
  515. = gen_tcp:recv(Socket, 0, 6000),
  516. ok = gen_tcp:send(Socket, [
  517. %% #1
  518. << 0:1, 0:3, 1:4, 1:1, 5:7 >>,
  519. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  520. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
  521. %% #2
  522. << 0:1, 0:3, 0:4, 1:1, 5:7 >>,
  523. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  524. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
  525. %% #3
  526. << 1:1, 0:3, 0:4, 1:1, 5:7 >>,
  527. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  528. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
  529. {ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>}
  530. = gen_tcp:recv(Socket, 0, 6000),
  531. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  532. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  533. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  534. ok.
  535. ws_timeout_hibernate(Config) ->
  536. {port, Port} = lists:keyfind(port, 1, Config),
  537. {ok, Socket} = gen_tcp:connect("localhost", Port,
  538. [binary, {active, false}, {packet, raw}]),
  539. ok = gen_tcp:send(Socket, [
  540. "GET /ws_timeout_hibernate HTTP/1.1\r\n"
  541. "Host: localhost\r\n"
  542. "Connection: Upgrade\r\n"
  543. "Upgrade: websocket\r\n"
  544. "Sec-WebSocket-Origin: http://localhost\r\n"
  545. "Sec-WebSocket-Version: 8\r\n"
  546. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  547. "\r\n"]),
  548. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  549. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  550. = erlang:decode_packet(http, Handshake, []),
  551. [Headers, <<>>] = do_decode_headers(
  552. erlang:decode_packet(httph, Rest, []), []),
  553. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  554. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  555. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  556. = lists:keyfind("sec-websocket-accept", 1, Headers),
  557. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  558. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  559. ok.
  560. ws_timeout_cancel(Config) ->
  561. %% Erlang messages to a socket should not cancel the timeout
  562. {port, Port} = lists:keyfind(port, 1, Config),
  563. {ok, Socket} = gen_tcp:connect("localhost", Port,
  564. [binary, {active, false}, {packet, raw}]),
  565. ok = gen_tcp:send(Socket, [
  566. "GET /ws_timeout_cancel HTTP/1.1\r\n"
  567. "Host: localhost\r\n"
  568. "Connection: Upgrade\r\n"
  569. "Upgrade: websocket\r\n"
  570. "Sec-WebSocket-Origin: http://localhost\r\n"
  571. "Sec-WebSocket-Version: 8\r\n"
  572. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  573. "\r\n"]),
  574. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  575. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  576. = erlang:decode_packet(http, Handshake, []),
  577. [Headers, <<>>] = do_decode_headers(
  578. erlang:decode_packet(httph, Rest, []), []),
  579. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  580. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  581. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  582. = lists:keyfind("sec-websocket-accept", 1, Headers),
  583. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  584. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  585. ok.
  586. ws_timeout_reset(Config) ->
  587. %% Erlang messages across a socket should reset the timeout
  588. {port, Port} = lists:keyfind(port, 1, Config),
  589. {ok, Socket} = gen_tcp:connect("localhost", Port,
  590. [binary, {active, false}, {packet, raw}]),
  591. ok = gen_tcp:send(Socket, [
  592. "GET /ws_timeout_cancel HTTP/1.1\r\n"
  593. "Host: localhost\r\n"
  594. "Connection: Upgrade\r\n"
  595. "Upgrade: websocket\r\n"
  596. "Sec-WebSocket-Origin: http://localhost\r\n"
  597. "Sec-Websocket-Version: 13\r\n"
  598. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  599. "\r\n"]),
  600. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  601. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  602. = erlang:decode_packet(http, Handshake, []),
  603. [Headers, <<>>] = do_decode_headers(
  604. erlang:decode_packet(httph, Rest, []), []),
  605. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  606. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  607. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  608. = lists:keyfind("sec-websocket-accept", 1, Headers),
  609. [begin
  610. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  611. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  612. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  613. = gen_tcp:recv(Socket, 0, 6000),
  614. ok = timer:sleep(500)
  615. end || _ <- [1, 2, 3, 4]],
  616. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  617. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  618. ok.
  619. ws_upgrade_with_opts(Config) ->
  620. {port, Port} = lists:keyfind(port, 1, Config),
  621. {ok, Socket} = gen_tcp:connect("localhost", Port,
  622. [binary, {active, false}, {packet, raw}]),
  623. ok = gen_tcp:send(Socket, [
  624. "GET /ws_upgrade_with_opts HTTP/1.1\r\n"
  625. "Host: localhost\r\n"
  626. "Connection: Upgrade\r\n"
  627. "Upgrade: websocket\r\n"
  628. "Sec-WebSocket-Origin: http://localhost\r\n"
  629. "Sec-WebSocket-Version: 8\r\n"
  630. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  631. "\r\n"]),
  632. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  633. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  634. = erlang:decode_packet(http, Handshake, []),
  635. [Headers, <<>>] = do_decode_headers(
  636. erlang:decode_packet(httph, Rest, []), []),
  637. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  638. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  639. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  640. = lists:keyfind("sec-websocket-accept", 1, Headers),
  641. {ok, Response} = gen_tcp:recv(Socket, 9, 6000),
  642. << 1:1, 0:3, 1:4, 0:1, 7:7, "success" >> = Response,
  643. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  644. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  645. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  646. ok.
  647. %% Internal.
  648. do_decode_headers({ok, http_eoh, Rest}, Acc) ->
  649. [Acc, Rest];
  650. do_decode_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  651. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  652. do_decode_headers(erlang:decode_packet(httph, Rest, []),
  653. [{F(Key), Value}|Acc]).
  654. do_mask(<<>>, _, Acc) ->
  655. Acc;
  656. do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) ->
  657. T = O bxor MaskKey,
  658. do_mask(Rest, MaskKey, << Acc/binary, T:32 >>);
  659. do_mask(<< O:24 >>, MaskKey, Acc) ->
  660. << MaskKey2:24, _:8 >> = << MaskKey:32 >>,
  661. T = O bxor MaskKey2,
  662. << Acc/binary, T:24 >>;
  663. do_mask(<< O:16 >>, MaskKey, Acc) ->
  664. << MaskKey2:16, _:16 >> = << MaskKey:32 >>,
  665. T = O bxor MaskKey2,
  666. << Acc/binary, T:16 >>;
  667. do_mask(<< O:8 >>, MaskKey, Acc) ->
  668. << MaskKey2:8, _:24 >> = << MaskKey:32 >>,
  669. T = O bxor MaskKey2,
  670. << Acc/binary, T:8 >>.