ws_SUITE.erl 20 KB

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