PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ws_SUITE.erl

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