ws_SUITE.erl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. -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_shutdown", ws_init_shutdown, []},
  55. {"/ws_send_many", ws_send_many, [
  56. {sequence, [
  57. {text, <<"one">>},
  58. {text, <<"two">>},
  59. {text, <<"seven!">>}]}
  60. ]},
  61. {"/ws_send_close", ws_send_many, [
  62. {sequence, [
  63. {text, <<"send">>},
  64. close,
  65. {text, <<"won't be received">>}]}
  66. ]},
  67. {"/ws_send_close_payload", ws_send_many, [
  68. {sequence, [
  69. {text, <<"send">>},
  70. {close, 1001, <<"some text!">>},
  71. {text, <<"won't be received">>}]}
  72. ]},
  73. {"/ws_subprotocol", ws_subprotocol, []},
  74. {"/ws_timeout_hibernate", ws_timeout_hibernate, []},
  75. {"/ws_timeout_cancel", ws_timeout_cancel, []}
  76. ]}
  77. ]).
  78. %% Tests.
  79. autobahn_fuzzingclient(Config) ->
  80. doc("Autobahn test suite for the Websocket protocol."),
  81. Self = self(),
  82. spawn_link(fun() -> start_port(Config, Self) end),
  83. receive autobahn_exit -> ok end,
  84. ct:log("<h2><a href=\"log_private/reports/servers/index.html\">Full report</a></h2>~n"),
  85. Report = config(priv_dir, Config) ++ "reports/servers/index.html",
  86. ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
  87. {ok, HTML} = file:read_file(Report),
  88. case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
  89. true -> error(failed);
  90. false -> ok
  91. end.
  92. start_port(Config, Pid) ->
  93. Port = open_port({spawn, "wstest -m fuzzingclient -s " ++ config(data_dir, Config) ++ "client.json"},
  94. [{line, 10000}, {cd, config(priv_dir, Config)}, binary, eof]),
  95. receive_infinity(Port, Pid).
  96. receive_infinity(Port, Pid) ->
  97. receive
  98. {Port, {data, {eol, Line}}} ->
  99. io:format(user, "~s~n", [Line]),
  100. receive_infinity(Port, Pid);
  101. {Port, eof} ->
  102. Pid ! autobahn_exit
  103. end.
  104. ws0(Config) ->
  105. doc("Websocket version 0 (hixie-76 draft) is no longer supported."),
  106. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  107. ok = gen_tcp:send(Socket,
  108. "GET /ws_echo_timer HTTP/1.1\r\n"
  109. "Host: localhost\r\n"
  110. "Connection: Upgrade\r\n"
  111. "Upgrade: WebSocket\r\n"
  112. "Origin: http://localhost\r\n"
  113. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  114. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  115. "\r\n"),
  116. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  117. {ok, {http_response, {1, 1}, 400, _}, _} = erlang:decode_packet(http, Handshake, []),
  118. ok.
  119. %% @todo What about version 7?
  120. ws8(Config) ->
  121. doc("Websocket version 8 (draft) is supported."),
  122. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  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, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  134. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  135. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  136. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  137. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  138. do_ws_version(Socket).
  139. ws13(Config) ->
  140. doc("Websocket version 13 (RFC) is supported."),
  141. {ok, Socket, _} = do_handshake("/ws_echo_timer", Config),
  142. do_ws_version(Socket).
  143. do_ws_version(Socket) ->
  144. %% Masked text hello echoed back clear by the server.
  145. Mask = 16#37fa213d,
  146. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  147. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  148. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  149. %% Empty binary frame echoed back.
  150. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 0:7, 0:32 >>),
  151. {ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  152. %% Masked binary hello echoed back clear by the server.
  153. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  154. {ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  155. %% Frames sent on timer by the handler.
  156. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} = gen_tcp:recv(Socket, 0, 6000),
  157. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = 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. %% Client-initiated ping/pong.
  161. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>),
  162. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  163. %% Client-initiated close.
  164. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  165. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  166. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  167. ok.
  168. ws_init_shutdown(Config) ->
  169. doc("Handler stops before Websocket handshake."),
  170. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  171. ok = gen_tcp:send(Socket, [
  172. "GET /ws_init_shutdown HTTP/1.1\r\n"
  173. "Host: localhost\r\n"
  174. "Connection: Upgrade\r\n"
  175. "Origin: http://localhost\r\n"
  176. "Sec-WebSocket-Version: 13\r\n"
  177. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  178. "Upgrade: websocket\r\n"
  179. "\r\n"]),
  180. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  181. {ok, {http_response, {1, 1}, 403, _}, _Rest} = erlang:decode_packet(http, Handshake, []),
  182. ok.
  183. ws_send_close(Config) ->
  184. doc("Server-initiated close frame ends the connection."),
  185. {ok, Socket, _} = do_handshake("/ws_send_close", Config),
  186. %% We catch all frames at once and check them directly.
  187. {ok, <<
  188. 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  189. 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 8, 6000),
  190. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  191. ok.
  192. ws_send_close_payload(Config) ->
  193. doc("Server-initiated close frame with payload ends the connection."),
  194. {ok, Socket, _} = do_handshake("/ws_send_close_payload", Config),
  195. %% We catch all frames at once and check them directly.
  196. {ok, <<
  197. 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  198. 1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >>} = gen_tcp:recv(Socket, 20, 6000),
  199. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  200. ok.
  201. ws_send_many(Config) ->
  202. doc("Server sends many frames in a single reply."),
  203. {ok, Socket, _} = do_handshake("/ws_send_many", Config),
  204. %% We catch all frames at once and check them directly.
  205. {ok, <<
  206. 1:1, 0:3, 1:4, 0:1, 3:7, "one",
  207. 1:1, 0:3, 1:4, 0:1, 3:7, "two",
  208. 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >>} = gen_tcp:recv(Socket, 18, 6000),
  209. ok.
  210. ws_single_bytes(Config) ->
  211. doc("Client sends a text frame one byte at a time."),
  212. {ok, Socket, _} = do_handshake("/ws_echo", Config),
  213. %% We sleep between sends to make sure only one byte is sent.
  214. ok = gen_tcp:send(Socket, << 16#81 >>), timer:sleep(100),
  215. ok = gen_tcp:send(Socket, << 16#85 >>), timer:sleep(100),
  216. ok = gen_tcp:send(Socket, << 16#37 >>), timer:sleep(100),
  217. ok = gen_tcp:send(Socket, << 16#fa >>), timer:sleep(100),
  218. ok = gen_tcp:send(Socket, << 16#21 >>), timer:sleep(100),
  219. ok = gen_tcp:send(Socket, << 16#3d >>), timer:sleep(100),
  220. ok = gen_tcp:send(Socket, << 16#7f >>), timer:sleep(100),
  221. ok = gen_tcp:send(Socket, << 16#9f >>), timer:sleep(100),
  222. ok = gen_tcp:send(Socket, << 16#4d >>), timer:sleep(100),
  223. ok = gen_tcp:send(Socket, << 16#51 >>), timer:sleep(100),
  224. ok = gen_tcp:send(Socket, << 16#58 >>),
  225. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  226. ok.
  227. ws_subprotocol(Config) ->
  228. doc("Websocket sub-protocol negotiation."),
  229. {ok, _, Headers} = do_handshake("/ws_subprotocol",
  230. "Sec-WebSocket-Protocol: foo, bar\r\n", Config),
  231. {_, "foo"} = lists:keyfind("sec-websocket-protocol", 1, Headers),
  232. ok.
  233. ws_text_fragments(Config) ->
  234. doc("Client sends fragmented text frames."),
  235. {ok, Socket, _} = do_handshake("/ws_echo", Config),
  236. %% Send two "Hello" over two fragments and two sends.
  237. Mask = 16#37fa213d,
  238. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  239. ok = gen_tcp:send(Socket, << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  240. ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  241. {ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
  242. %% Send three "Hello" over three fragments and one send.
  243. ok = gen_tcp:send(Socket, [
  244. << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
  245. << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
  246. << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>]),
  247. {ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
  248. ok.
  249. ws_timeout_hibernate(Config) ->
  250. doc("Server-initiated close on timeout with hibernating process."),
  251. {ok, Socket, _} = do_handshake("/ws_timeout_hibernate", Config),
  252. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  253. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  254. ok.
  255. ws_timeout_no_cancel(Config) ->
  256. doc("Server-initiated timeout is not influenced by reception of Erlang messages."),
  257. {ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
  258. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  259. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  260. ok.
  261. ws_timeout_reset(Config) ->
  262. doc("Server-initiated timeout is reset when client sends more data."),
  263. {ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
  264. %% Send and receive back a frame a few times.
  265. Mask = 16#37fa213d,
  266. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  267. [begin
  268. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  269. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  270. timer:sleep(500)
  271. end || _ <- [1, 2, 3, 4]],
  272. %% Timeout will occur after we stop sending data.
  273. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  274. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  275. ok.
  276. ws_webkit_deflate(Config) ->
  277. doc("x-webkit-deflate-frame compression."),
  278. {ok, Socket, Headers} = do_handshake("/ws_echo",
  279. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  280. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  281. %% Send and receive a compressed "Hello" frame.
  282. Mask = 16#11223344,
  283. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  284. MaskedHello = do_mask(CompressedHello, Mask, <<>>),
  285. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary >>),
  286. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  287. %% Client-initiated close.
  288. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  289. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  290. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  291. ok.
  292. ws_webkit_deflate_fragments(Config) ->
  293. doc("Client sends an x-webkit-deflate-frame compressed and fragmented text frame."),
  294. {ok, Socket, Headers} = do_handshake("/ws_echo",
  295. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  296. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  297. %% Send a compressed "Hello" over two fragments and two sends.
  298. Mask = 16#11223344,
  299. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  300. MaskedHello1 = do_mask(binary:part(CompressedHello, 0, 4), Mask, <<>>),
  301. MaskedHello2 = do_mask(binary:part(CompressedHello, 4, 3), Mask, <<>>),
  302. ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32, MaskedHello1/binary >>),
  303. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32, MaskedHello2/binary >>),
  304. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  305. ok.
  306. ws_webkit_deflate_single_bytes(Config) ->
  307. doc("Client sends an x-webkit-deflate-frame compressed text frame one byte at a time."),
  308. {ok, Socket, Headers} = do_handshake("/ws_echo",
  309. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  310. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  311. %% We sleep between sends to make sure only one byte is sent.
  312. Mask = 16#11223344,
  313. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  314. MaskedHello = do_mask(CompressedHello, Mask, <<>>),
  315. ok = gen_tcp:send(Socket, << 16#c1 >>), timer:sleep(100),
  316. ok = gen_tcp:send(Socket, << 16#87 >>), timer:sleep(100),
  317. ok = gen_tcp:send(Socket, << 16#11 >>), timer:sleep(100),
  318. ok = gen_tcp:send(Socket, << 16#22 >>), timer:sleep(100),
  319. ok = gen_tcp:send(Socket, << 16#33 >>), timer:sleep(100),
  320. ok = gen_tcp:send(Socket, << 16#44 >>), timer:sleep(100),
  321. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 0)]), timer:sleep(100),
  322. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 1)]), timer:sleep(100),
  323. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 2)]), timer:sleep(100),
  324. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 3)]), timer:sleep(100),
  325. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 4)]), timer:sleep(100),
  326. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 5)]), timer:sleep(100),
  327. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 6)]),
  328. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  329. ok.
  330. %% Internal.
  331. do_handshake(Path, Config) ->
  332. do_handshake(Path, "", Config).
  333. do_handshake(Path, ExtraHeaders, Config) ->
  334. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
  335. [binary, {active, false}]),
  336. ok = gen_tcp:send(Socket, [
  337. "GET ", Path, " HTTP/1.1\r\n"
  338. "Host: localhost\r\n"
  339. "Connection: Upgrade\r\n"
  340. "Origin: http://localhost\r\n"
  341. "Sec-WebSocket-Version: 13\r\n"
  342. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  343. "Upgrade: websocket\r\n",
  344. ExtraHeaders,
  345. "\r\n"]),
  346. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  347. {ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  348. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  349. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  350. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  351. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  352. {ok, Socket, Headers}.
  353. do_decode_headers({ok, http_eoh, Rest}, Acc) ->
  354. [Acc, Rest];
  355. do_decode_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  356. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  357. do_decode_headers(erlang:decode_packet(httph, Rest, []), [{F(Key), Value}|Acc]).
  358. do_mask(<<>>, _, Acc) ->
  359. Acc;
  360. do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) ->
  361. T = O bxor MaskKey,
  362. do_mask(Rest, MaskKey, << Acc/binary, T:32 >>);
  363. do_mask(<< O:24 >>, MaskKey, Acc) ->
  364. << MaskKey2:24, _:8 >> = << MaskKey:32 >>,
  365. T = O bxor MaskKey2,
  366. << Acc/binary, T:24 >>;
  367. do_mask(<< O:16 >>, MaskKey, Acc) ->
  368. << MaskKey2:16, _:16 >> = << MaskKey:32 >>,
  369. T = O bxor MaskKey2,
  370. << Acc/binary, T:16 >>;
  371. do_mask(<< O:8 >>, MaskKey, Acc) ->
  372. << MaskKey2:8, _:24 >> = << MaskKey:32 >>,
  373. T = O bxor MaskKey2,
  374. << Acc/binary, T:8 >>.