PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ws_SUITE.erl

https://github.com/si14/cowboy
Erlang | 579 lines | 516 code | 31 blank | 32 comment | 1 complexity | 2361f12f6d6185b760368523b171dd21 MD5 | raw file
Possible License(s): 0BSD
  1. %% Copyright (c) 2011-2013, 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. -include_lib("common_test/include/ct.hrl").
  16. %% ct.
  17. -export([all/0]).
  18. -export([groups/0]).
  19. -export([init_per_suite/1]).
  20. -export([end_per_suite/1]).
  21. -export([init_per_group/2]).
  22. -export([end_per_group/2]).
  23. %% Tests.
  24. -export([ws0/1]).
  25. -export([ws8/1]).
  26. -export([ws8_init_shutdown/1]).
  27. -export([ws8_single_bytes/1]).
  28. -export([ws13/1]).
  29. -export([ws_send_close/1]).
  30. -export([ws_send_close_payload/1]).
  31. -export([ws_send_many/1]).
  32. -export([ws_text_fragments/1]).
  33. -export([ws_timeout_hibernate/1]).
  34. -export([ws_timeout_cancel/1]).
  35. -export([ws_timeout_reset/1]).
  36. -export([ws_upgrade_with_opts/1]).
  37. %% ct.
  38. all() ->
  39. [{group, ws}].
  40. groups() ->
  41. BaseTests = [
  42. ws0,
  43. ws8,
  44. ws8_init_shutdown,
  45. ws8_single_bytes,
  46. ws13,
  47. ws_send_close,
  48. ws_send_close_payload,
  49. ws_send_many,
  50. ws_text_fragments,
  51. ws_timeout_hibernate,
  52. ws_timeout_cancel,
  53. ws_timeout_reset,
  54. ws_upgrade_with_opts
  55. ],
  56. [{ws, [], BaseTests}].
  57. init_per_suite(Config) ->
  58. application:start(crypto),
  59. application:start(ranch),
  60. application:start(cowboy),
  61. Config.
  62. end_per_suite(_Config) ->
  63. application:stop(cowboy),
  64. application:stop(ranch),
  65. application:stop(crypto),
  66. ok.
  67. init_per_group(ws, Config) ->
  68. Port = 33080,
  69. cowboy:start_http(ws, 100, [{port, Port}], [
  70. {env, [{dispatch, init_dispatch()}]}
  71. ]),
  72. [{port, Port}|Config].
  73. end_per_group(Listener, _Config) ->
  74. cowboy:stop_listener(Listener),
  75. ok.
  76. %% Dispatch configuration.
  77. init_dispatch() ->
  78. cowboy_router:compile([
  79. {"localhost", [
  80. {"/websocket", websocket_handler, []},
  81. {"/ws_echo_handler", websocket_echo_handler, []},
  82. {"/ws_init_shutdown", websocket_handler_init_shutdown, []},
  83. {"/ws_send_many", ws_send_many_handler, [
  84. {sequence, [
  85. {text, <<"one">>},
  86. {text, <<"two">>},
  87. {text, <<"seven!">>}]}
  88. ]},
  89. {"/ws_send_close", ws_send_many_handler, [
  90. {sequence, [
  91. {text, <<"send">>},
  92. close,
  93. {text, <<"won't be received">>}]}
  94. ]},
  95. {"/ws_send_close_payload", ws_send_many_handler, [
  96. {sequence, [
  97. {text, <<"send">>},
  98. {close, 1001, <<"some text!">>},
  99. {text, <<"won't be received">>}]}
  100. ]},
  101. {"/ws_timeout_hibernate", ws_timeout_hibernate_handler, []},
  102. {"/ws_timeout_cancel", ws_timeout_cancel_handler, []},
  103. {"/ws_upgrade_with_opts", ws_upgrade_with_opts_handler,
  104. <<"failure">>}
  105. ]}
  106. ]).
  107. %% ws and wss.
  108. %% We do not support hixie76 anymore.
  109. ws0(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 /websocket HTTP/1.1\r\n"
  115. "Host: localhost\r\n"
  116. "Connection: Upgrade\r\n"
  117. "Upgrade: WebSocket\r\n"
  118. "Origin: http://localhost\r\n"
  119. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  120. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  121. "\r\n"),
  122. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  123. {ok, {http_response, {1, 1}, 400, _}, _}
  124. = erlang:decode_packet(http, Handshake, []).
  125. ws8(Config) ->
  126. {port, Port} = lists:keyfind(port, 1, Config),
  127. {ok, Socket} = gen_tcp:connect("localhost", Port,
  128. [binary, {active, false}, {packet, raw}]),
  129. ok = gen_tcp:send(Socket, [
  130. "GET /websocket HTTP/1.1\r\n"
  131. "Host: localhost\r\n"
  132. "Connection: Upgrade\r\n"
  133. "Upgrade: websocket\r\n"
  134. "Sec-WebSocket-Origin: http://localhost\r\n"
  135. "Sec-WebSocket-Version: 8\r\n"
  136. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  137. "\r\n"]),
  138. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  139. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  140. = erlang:decode_packet(http, Handshake, []),
  141. [Headers, <<>>] = websocket_headers(
  142. erlang:decode_packet(httph, Rest, []), []),
  143. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  144. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  145. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  146. = lists:keyfind("sec-websocket-accept", 1, Headers),
  147. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  148. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  149. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  150. = gen_tcp:recv(Socket, 0, 6000),
  151. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  152. = gen_tcp:recv(Socket, 0, 6000),
  153. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  154. = gen_tcp:recv(Socket, 0, 6000),
  155. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  156. = gen_tcp:recv(Socket, 0, 6000),
  157. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  158. = gen_tcp:recv(Socket, 0, 6000),
  159. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
  160. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  161. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  162. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  163. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  164. ok.
  165. ws8_init_shutdown(Config) ->
  166. {port, Port} = lists:keyfind(port, 1, Config),
  167. {ok, Socket} = gen_tcp:connect("localhost", Port,
  168. [binary, {active, false}, {packet, raw}]),
  169. ok = gen_tcp:send(Socket, [
  170. "GET /ws_init_shutdown HTTP/1.1\r\n"
  171. "Host: localhost\r\n"
  172. "Connection: Upgrade\r\n"
  173. "Upgrade: websocket\r\n"
  174. "Sec-WebSocket-Origin: http://localhost\r\n"
  175. "Sec-WebSocket-Version: 8\r\n"
  176. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  177. "\r\n"]),
  178. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  179. {ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest}
  180. = erlang:decode_packet(http, Handshake, []),
  181. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  182. ok.
  183. ws8_single_bytes(Config) ->
  184. {port, Port} = lists:keyfind(port, 1, Config),
  185. {ok, Socket} = gen_tcp:connect("localhost", Port,
  186. [binary, {active, false}, {packet, raw}]),
  187. ok = gen_tcp:send(Socket, [
  188. "GET /websocket HTTP/1.1\r\n"
  189. "Host: localhost\r\n"
  190. "Connection: Upgrade\r\n"
  191. "Upgrade: websocket\r\n"
  192. "Sec-WebSocket-Origin: http://localhost\r\n"
  193. "Sec-WebSocket-Version: 8\r\n"
  194. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  195. "\r\n"]),
  196. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  197. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  198. = erlang:decode_packet(http, Handshake, []),
  199. [Headers, <<>>] = websocket_headers(
  200. erlang:decode_packet(httph, Rest, []), []),
  201. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  202. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  203. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  204. = lists:keyfind("sec-websocket-accept", 1, Headers),
  205. ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
  206. ok = timer:sleep(100), %% sleep for a period
  207. ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
  208. ok = timer:sleep(100),
  209. ok = gen_tcp:send(Socket, << 16#37 >>),
  210. ok = timer:sleep(100),
  211. ok = gen_tcp:send(Socket, << 16#fa >>),
  212. ok = timer:sleep(100),
  213. ok = gen_tcp:send(Socket, << 16#21 >>),
  214. ok = timer:sleep(100),
  215. ok = gen_tcp:send(Socket, << 16#3d >>),
  216. ok = timer:sleep(100),
  217. ok = gen_tcp:send(Socket, << 16#7f >>),
  218. ok = timer:sleep(100),
  219. ok = gen_tcp:send(Socket, << 16#9f >>),
  220. ok = timer:sleep(100),
  221. ok = gen_tcp:send(Socket, << 16#4d >>),
  222. ok = timer:sleep(100),
  223. ok = gen_tcp:send(Socket, << 16#51 >>),
  224. ok = timer:sleep(100),
  225. ok = gen_tcp:send(Socket, << 16#58 >>),
  226. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  227. = gen_tcp:recv(Socket, 0, 6000),
  228. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  229. = gen_tcp:recv(Socket, 0, 6000),
  230. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  231. = gen_tcp:recv(Socket, 0, 6000),
  232. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  233. = gen_tcp:recv(Socket, 0, 6000),
  234. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  235. = gen_tcp:recv(Socket, 0, 6000),
  236. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
  237. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  238. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  239. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  240. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  241. ok.
  242. ws13(Config) ->
  243. {port, Port} = lists:keyfind(port, 1, Config),
  244. {ok, Socket} = gen_tcp:connect("localhost", Port,
  245. [binary, {active, false}, {packet, raw}]),
  246. ok = gen_tcp:send(Socket, [
  247. "GET /websocket HTTP/1.1\r\n"
  248. "Host: localhost\r\n"
  249. "Connection: Upgrade\r\n"
  250. "Origin: http://localhost\r\n"
  251. "Sec-WebSocket-Version: 13\r\n"
  252. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  253. "Upgrade: websocket\r\n"
  254. "\r\n"]),
  255. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  256. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  257. = erlang:decode_packet(http, Handshake, []),
  258. [Headers, <<>>] = websocket_headers(
  259. erlang:decode_packet(httph, Rest, []), []),
  260. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  261. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  262. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  263. = lists:keyfind("sec-websocket-accept", 1, Headers),
  264. %% text
  265. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  266. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  267. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  268. = gen_tcp:recv(Socket, 0, 6000),
  269. %% binary (empty)
  270. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 0:7, 0:32 >>),
  271. {ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  272. %% binary
  273. ok = gen_tcp:send(Socket, << 16#82, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  274. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  275. {ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>}
  276. = gen_tcp:recv(Socket, 0, 6000),
  277. %% Receives.
  278. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  279. = gen_tcp:recv(Socket, 0, 6000),
  280. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  281. = gen_tcp:recv(Socket, 0, 6000),
  282. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  283. = gen_tcp:recv(Socket, 0, 6000),
  284. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  285. = gen_tcp:recv(Socket, 0, 6000),
  286. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), %% ping
  287. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  288. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  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_send_close(Config) ->
  293. {port, Port} = lists:keyfind(port, 1, Config),
  294. {ok, Socket} = gen_tcp:connect("localhost", Port,
  295. [binary, {active, false}, {packet, raw}]),
  296. ok = gen_tcp:send(Socket, [
  297. "GET /ws_send_close HTTP/1.1\r\n"
  298. "Host: localhost\r\n"
  299. "Connection: Upgrade\r\n"
  300. "Upgrade: websocket\r\n"
  301. "Sec-WebSocket-Origin: http://localhost\r\n"
  302. "Sec-WebSocket-Version: 8\r\n"
  303. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  304. "\r\n"]),
  305. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  306. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  307. = erlang:decode_packet(http, Handshake, []),
  308. [Headers, <<>>] = websocket_headers(
  309. erlang:decode_packet(httph, Rest, []), []),
  310. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  311. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  312. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  313. = lists:keyfind("sec-websocket-accept", 1, Headers),
  314. %% We catch all frames at once and check them directly.
  315. {ok, Many} = gen_tcp:recv(Socket, 8, 6000),
  316. << 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  317. 1:1, 0:3, 8:4, 0:8 >> = Many,
  318. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  319. ok.
  320. ws_send_close_payload(Config) ->
  321. {port, Port} = lists:keyfind(port, 1, Config),
  322. {ok, Socket} = gen_tcp:connect("localhost", Port,
  323. [binary, {active, false}, {packet, raw}]),
  324. ok = gen_tcp:send(Socket, [
  325. "GET /ws_send_close_payload HTTP/1.1\r\n"
  326. "Host: localhost\r\n"
  327. "Connection: Upgrade\r\n"
  328. "Upgrade: websocket\r\n"
  329. "Sec-WebSocket-Origin: http://localhost\r\n"
  330. "Sec-WebSocket-Version: 8\r\n"
  331. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  332. "\r\n"]),
  333. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  334. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  335. = erlang:decode_packet(http, Handshake, []),
  336. [Headers, <<>>] = websocket_headers(
  337. erlang:decode_packet(httph, Rest, []), []),
  338. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  339. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  340. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  341. = lists:keyfind("sec-websocket-accept", 1, Headers),
  342. %% We catch all frames at once and check them directly.
  343. {ok, Many} = gen_tcp:recv(Socket, 20, 6000),
  344. << 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  345. 1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >> = Many,
  346. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  347. ok.
  348. ws_send_many(Config) ->
  349. {port, Port} = lists:keyfind(port, 1, Config),
  350. {ok, Socket} = gen_tcp:connect("localhost", Port,
  351. [binary, {active, false}, {packet, raw}]),
  352. ok = gen_tcp:send(Socket, [
  353. "GET /ws_send_many HTTP/1.1\r\n"
  354. "Host: localhost\r\n"
  355. "Connection: Upgrade\r\n"
  356. "Upgrade: websocket\r\n"
  357. "Sec-WebSocket-Origin: http://localhost\r\n"
  358. "Sec-WebSocket-Version: 8\r\n"
  359. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  360. "\r\n"]),
  361. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  362. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  363. = erlang:decode_packet(http, Handshake, []),
  364. [Headers, <<>>] = websocket_headers(
  365. erlang:decode_packet(httph, Rest, []), []),
  366. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  367. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  368. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  369. = lists:keyfind("sec-websocket-accept", 1, Headers),
  370. %% We catch all frames at once and check them directly.
  371. {ok, Many} = gen_tcp:recv(Socket, 18, 6000),
  372. << 1:1, 0:3, 1:4, 0:1, 3:7, "one",
  373. 1:1, 0:3, 1:4, 0:1, 3:7, "two",
  374. 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >> = Many,
  375. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  376. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  377. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  378. ok.
  379. ws_text_fragments(Config) ->
  380. {port, Port} = lists:keyfind(port, 1, Config),
  381. {ok, Socket} = gen_tcp:connect("localhost", Port,
  382. [binary, {active, false}, {packet, raw}]),
  383. ok = gen_tcp:send(Socket, [
  384. "GET /ws_echo_handler HTTP/1.1\r\n"
  385. "Host: localhost\r\n"
  386. "Connection: Upgrade\r\n"
  387. "Upgrade: websocket\r\n"
  388. "Sec-WebSocket-Origin: http://localhost\r\n"
  389. "Sec-WebSocket-Version: 8\r\n"
  390. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  391. "\r\n"]),
  392. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  393. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  394. = erlang:decode_packet(http, Handshake, []),
  395. [Headers, <<>>] = websocket_headers(
  396. erlang:decode_packet(httph, Rest, []), []),
  397. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  398. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  399. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  400. = lists:keyfind("sec-websocket-accept", 1, Headers),
  401. ok = gen_tcp:send(Socket, [
  402. << 0:1, 0:3, 1:4, 1:1, 5:7 >>,
  403. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  404. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
  405. ok = gen_tcp:send(Socket, [
  406. << 1:1, 0:3, 0:4, 1:1, 5:7 >>,
  407. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  408. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
  409. {ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>}
  410. = gen_tcp:recv(Socket, 0, 6000),
  411. ok = gen_tcp:send(Socket, [
  412. %% #1
  413. << 0:1, 0:3, 1:4, 1:1, 5:7 >>,
  414. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  415. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
  416. %% #2
  417. << 0:1, 0:3, 0:4, 1:1, 5:7 >>,
  418. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  419. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
  420. %% #3
  421. << 1:1, 0:3, 0:4, 1:1, 5:7 >>,
  422. << 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
  423. << 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
  424. {ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>}
  425. = gen_tcp:recv(Socket, 0, 6000),
  426. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  427. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  428. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  429. ok.
  430. ws_timeout_hibernate(Config) ->
  431. {port, Port} = lists:keyfind(port, 1, Config),
  432. {ok, Socket} = gen_tcp:connect("localhost", Port,
  433. [binary, {active, false}, {packet, raw}]),
  434. ok = gen_tcp:send(Socket, [
  435. "GET /ws_timeout_hibernate HTTP/1.1\r\n"
  436. "Host: localhost\r\n"
  437. "Connection: Upgrade\r\n"
  438. "Upgrade: websocket\r\n"
  439. "Sec-WebSocket-Origin: http://localhost\r\n"
  440. "Sec-WebSocket-Version: 8\r\n"
  441. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  442. "\r\n"]),
  443. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  444. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  445. = erlang:decode_packet(http, Handshake, []),
  446. [Headers, <<>>] = websocket_headers(
  447. erlang:decode_packet(httph, Rest, []), []),
  448. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  449. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  450. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  451. = lists:keyfind("sec-websocket-accept", 1, Headers),
  452. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  453. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  454. ok.
  455. ws_timeout_cancel(Config) ->
  456. %% Erlang messages to a socket should not cancel the timeout
  457. {port, Port} = lists:keyfind(port, 1, Config),
  458. {ok, Socket} = gen_tcp:connect("localhost", Port,
  459. [binary, {active, false}, {packet, raw}]),
  460. ok = gen_tcp:send(Socket, [
  461. "GET /ws_timeout_cancel HTTP/1.1\r\n"
  462. "Host: localhost\r\n"
  463. "Connection: Upgrade\r\n"
  464. "Upgrade: websocket\r\n"
  465. "Sec-WebSocket-Origin: http://localhost\r\n"
  466. "Sec-WebSocket-Version: 8\r\n"
  467. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  468. "\r\n"]),
  469. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  470. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  471. = erlang:decode_packet(http, Handshake, []),
  472. [Headers, <<>>] = websocket_headers(
  473. erlang:decode_packet(httph, Rest, []), []),
  474. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  475. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  476. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  477. = lists:keyfind("sec-websocket-accept", 1, Headers),
  478. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  479. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  480. ok.
  481. ws_timeout_reset(Config) ->
  482. %% Erlang messages across a socket should reset the timeout
  483. {port, Port} = lists:keyfind(port, 1, Config),
  484. {ok, Socket} = gen_tcp:connect("localhost", Port,
  485. [binary, {active, false}, {packet, raw}]),
  486. ok = gen_tcp:send(Socket, [
  487. "GET /ws_timeout_cancel HTTP/1.1\r\n"
  488. "Host: localhost\r\n"
  489. "Connection: Upgrade\r\n"
  490. "Upgrade: websocket\r\n"
  491. "Sec-WebSocket-Origin: http://localhost\r\n"
  492. "Sec-Websocket-Version: 13\r\n"
  493. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  494. "\r\n"]),
  495. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  496. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  497. = erlang:decode_packet(http, Handshake, []),
  498. [Headers, <<>>] = websocket_headers(
  499. erlang:decode_packet(httph, Rest, []), []),
  500. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  501. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  502. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  503. = lists:keyfind("sec-websocket-accept", 1, Headers),
  504. [begin
  505. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  506. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  507. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  508. = gen_tcp:recv(Socket, 0, 6000),
  509. ok = timer:sleep(500)
  510. end || _ <- [1, 2, 3, 4]],
  511. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  512. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  513. ok.
  514. ws_upgrade_with_opts(Config) ->
  515. {port, Port} = lists:keyfind(port, 1, Config),
  516. {ok, Socket} = gen_tcp:connect("localhost", Port,
  517. [binary, {active, false}, {packet, raw}]),
  518. ok = gen_tcp:send(Socket, [
  519. "GET /ws_upgrade_with_opts HTTP/1.1\r\n"
  520. "Host: localhost\r\n"
  521. "Connection: Upgrade\r\n"
  522. "Upgrade: websocket\r\n"
  523. "Sec-WebSocket-Origin: http://localhost\r\n"
  524. "Sec-WebSocket-Version: 8\r\n"
  525. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  526. "\r\n"]),
  527. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  528. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  529. = erlang:decode_packet(http, Handshake, []),
  530. [Headers, <<>>] = websocket_headers(
  531. erlang:decode_packet(httph, Rest, []), []),
  532. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  533. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  534. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  535. = lists:keyfind("sec-websocket-accept", 1, Headers),
  536. {ok, Response} = gen_tcp:recv(Socket, 9, 6000),
  537. << 1:1, 0:3, 1:4, 0:1, 7:7, "success" >> = Response,
  538. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close
  539. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  540. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  541. ok.
  542. %% Internal.
  543. websocket_headers({ok, http_eoh, Rest}, Acc) ->
  544. [Acc, Rest];
  545. websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  546. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  547. websocket_headers(erlang:decode_packet(httph, Rest, []),
  548. [{F(Key), Value}|Acc]).