ws_SUITE.erl 28 KB

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