ws_SUITE.erl 28 KB

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