rfc8441_SUITE.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. %% Copyright (c) 2018, 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(rfc8441_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. all() -> [{group, enabled}].
  20. groups() ->
  21. Tests = ct_helper:all(?MODULE),
  22. [{enabled, [parallel], Tests}].
  23. init_per_group(Name = enabled, Config) ->
  24. cowboy_test:init_http(Name, #{
  25. enable_connect_protocol => true,
  26. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  27. }, Config).
  28. end_per_group(Name, _) ->
  29. ok = cowboy:stop_listener(Name).
  30. init_routes(_) -> [
  31. {"localhost", [
  32. {"/ws", ws_echo, []}
  33. ]}
  34. ].
  35. %% Do a prior knowledge handshake.
  36. do_handshake(Config) ->
  37. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  38. %% Send a valid preface.
  39. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  40. %% Receive the server preface.
  41. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  42. {ok, << 4:8, 0:40, SettingsPayload:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  43. Settings = cow_http2:parse_settings_payload(SettingsPayload),
  44. %% Send the SETTINGS ack.
  45. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  46. %% Receive the SETTINGS ack.
  47. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  48. {ok, Socket, Settings}.
  49. % The SETTINGS_ENABLE_CONNECT_PROTOCOL SETTINGS Parameter.
  50. % The new parameter name is SETTINGS_ENABLE_CONNECT_PROTOCOL. The
  51. % value of the parameter MUST be 0 or 1.
  52. % Upon receipt of SETTINGS_ENABLE_CONNECT_PROTOCOL with a value of 1 a
  53. % client MAY use the Extended CONNECT definition of this document when
  54. % creating new streams. Receipt of this parameter by a server does not
  55. % have any impact.
  56. %% @todo ignore_client_enable_setting(Config) ->
  57. % A sender MUST NOT send a SETTINGS_ENABLE_CONNECT_PROTOCOL parameter
  58. % with the value of 0 after previously sending a value of 1.
  59. reject_handshake_when_disabled(Config0) ->
  60. doc("Extended CONNECT requests MUST be rejected with a "
  61. "PROTOCOL_ERROR stream error when enable_connect_protocol=false. (draft-01 3)"),
  62. Config = cowboy_test:init_http(disabled, #{
  63. enable_connect_protocol => false,
  64. env => #{dispatch => cowboy_router:compile(init_routes(Config0))}
  65. }, Config0),
  66. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 0.
  67. {ok, Socket, Settings} = do_handshake(Config),
  68. case Settings of
  69. #{enable_connect_protocol := false} -> ok;
  70. _ when map_size(Settings) =:= 0 -> ok
  71. end,
  72. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  73. {ReqHeadersBlock, _} = cow_hpack:encode([
  74. {<<":method">>, <<"CONNECT">>},
  75. {<<":protocol">>, <<"websocket">>},
  76. {<<":scheme">>, <<"http">>},
  77. {<<":path">>, <<"/ws">>},
  78. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  79. {<<"sec-websocket-version">>, <<"13">>},
  80. {<<"origin">>, <<"http://localhost">>}
  81. ]),
  82. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  83. %% Receive a PROTOCOL_ERROR stream error.
  84. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  85. ok.
  86. reject_handshake_disabled_by_default(Config0) ->
  87. doc("Extended CONNECT requests MUST be rejected with a "
  88. "PROTOCOL_ERROR stream error with default enable_connect_protocol. (draft-01 3)"),
  89. Config = cowboy_test:init_http(disabled_by_default, #{
  90. env => #{dispatch => cowboy_router:compile(init_routes(Config0))}
  91. }, Config0),
  92. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 0.
  93. {ok, Socket, Settings} = do_handshake(Config),
  94. case Settings of
  95. #{enable_connect_protocol := false} -> ok;
  96. _ when map_size(Settings) =:= 0 -> ok
  97. end,
  98. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  99. {ReqHeadersBlock, _} = cow_hpack:encode([
  100. {<<":method">>, <<"CONNECT">>},
  101. {<<":protocol">>, <<"websocket">>},
  102. {<<":scheme">>, <<"http">>},
  103. {<<":path">>, <<"/ws">>},
  104. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  105. {<<"sec-websocket-version">>, <<"13">>},
  106. {<<"origin">>, <<"http://localhost">>}
  107. ]),
  108. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  109. %% Receive a PROTOCOL_ERROR stream error.
  110. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  111. ok.
  112. % The Extended CONNECT Method.
  113. accept_uppercase_pseudo_header_protocol(Config) ->
  114. doc("The :protocol pseudo header is case insensitive. (draft-01 4)"),
  115. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  116. {ok, Socket, Settings} = do_handshake(Config),
  117. #{enable_connect_protocol := true} = Settings,
  118. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  119. {ReqHeadersBlock, _} = cow_hpack:encode([
  120. {<<":method">>, <<"CONNECT">>},
  121. {<<":protocol">>, <<"WEBSOCKET">>},
  122. {<<":scheme">>, <<"http">>},
  123. {<<":path">>, <<"/ws">>},
  124. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  125. {<<"sec-websocket-version">>, <<"13">>},
  126. {<<"origin">>, <<"http://localhost">>}
  127. ]),
  128. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  129. %% Receive a 200 response.
  130. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  131. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  132. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  133. {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  134. ok.
  135. reject_many_pseudo_header_protocol(Config) ->
  136. doc("An extended CONNECT request containing more than one protocol component "
  137. "must be rejected with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  138. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  139. {ok, Socket, Settings} = do_handshake(Config),
  140. #{enable_connect_protocol := true} = Settings,
  141. %% Send an extended CONNECT request with more than one :protocol pseudo-header.
  142. {ReqHeadersBlock, _} = cow_hpack:encode([
  143. {<<":method">>, <<"CONNECT">>},
  144. {<<":protocol">>, <<"websocket">>},
  145. {<<":protocol">>, <<"mqtt">>},
  146. {<<":scheme">>, <<"http">>},
  147. {<<":path">>, <<"/ws">>},
  148. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  149. {<<"sec-websocket-version">>, <<"13">>},
  150. {<<"origin">>, <<"http://localhost">>}
  151. ]),
  152. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  153. %% Receive a PROTOCOL_ERROR stream error.
  154. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  155. ok.
  156. reject_unknown_pseudo_header_protocol(Config) ->
  157. doc("An extended CONNECT request with an unknown protocol must be rejected "
  158. "with a 400 error. (draft-01 4)"),
  159. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  160. {ok, Socket, Settings} = do_handshake(Config),
  161. #{enable_connect_protocol := true} = Settings,
  162. %% Send an extended CONNECT request with an unknown :protocol pseudo-header.
  163. {ReqHeadersBlock, _} = cow_hpack:encode([
  164. {<<":method">>, <<"CONNECT">>},
  165. {<<":protocol">>, <<"mqtt">>},
  166. {<<":scheme">>, <<"http">>},
  167. {<<":path">>, <<"/ws">>},
  168. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  169. {<<"sec-websocket-version">>, <<"13">>},
  170. {<<"origin">>, <<"http://localhost">>}
  171. ]),
  172. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  173. %% Receive a 400 response.
  174. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  175. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  176. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  177. {_, <<"400">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  178. ok.
  179. reject_invalid_pseudo_header_protocol(Config) ->
  180. doc("An extended CONNECT request with an invalid protocol must be rejected "
  181. "with a 400 error. (draft-01 4)"),
  182. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  183. {ok, Socket, Settings} = do_handshake(Config),
  184. #{enable_connect_protocol := true} = Settings,
  185. %% Send an extended CONNECT request with an invalid :protocol pseudo-header.
  186. {ReqHeadersBlock, _} = cow_hpack:encode([
  187. {<<":method">>, <<"CONNECT">>},
  188. {<<":protocol">>, <<"websocket mqtt">>},
  189. {<<":scheme">>, <<"http">>},
  190. {<<":path">>, <<"/ws">>},
  191. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  192. {<<"sec-websocket-version">>, <<"13">>},
  193. {<<"origin">>, <<"http://localhost">>}
  194. ]),
  195. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  196. %% Receive a 400 response.
  197. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  198. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  199. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  200. {_, <<"400">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  201. ok.
  202. reject_missing_pseudo_header_scheme(Config) ->
  203. doc("An extended CONNECT request without a scheme component must be rejected "
  204. "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  205. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  206. {ok, Socket, Settings} = do_handshake(Config),
  207. #{enable_connect_protocol := true} = Settings,
  208. %% Send an extended CONNECT request without a :scheme pseudo-header.
  209. {ReqHeadersBlock, _} = cow_hpack:encode([
  210. {<<":method">>, <<"CONNECT">>},
  211. {<<":protocol">>, <<"websocket">>},
  212. {<<":path">>, <<"/ws">>},
  213. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  214. {<<"sec-websocket-version">>, <<"13">>},
  215. {<<"origin">>, <<"http://localhost">>}
  216. ]),
  217. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  218. %% Receive a PROTOCOL_ERROR stream error.
  219. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  220. ok.
  221. reject_missing_pseudo_header_path(Config) ->
  222. doc("An extended CONNECT request without a path component must be rejected "
  223. "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  224. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  225. {ok, Socket, Settings} = do_handshake(Config),
  226. #{enable_connect_protocol := true} = Settings,
  227. %% Send an extended CONNECT request without a :path pseudo-header.
  228. {ReqHeadersBlock, _} = cow_hpack:encode([
  229. {<<":method">>, <<"CONNECT">>},
  230. {<<":protocol">>, <<"websocket">>},
  231. {<<":scheme">>, <<"http">>},
  232. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  233. {<<"sec-websocket-version">>, <<"13">>},
  234. {<<"origin">>, <<"http://localhost">>}
  235. ]),
  236. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  237. %% Receive a PROTOCOL_ERROR stream error.
  238. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  239. ok.
  240. % On requests bearing the :protocol pseudo-header, the :authority
  241. % pseudo-header field is interpreted according to Section 8.1.2.3 of
  242. % [RFC7540] instead of Section 8.3 of [RFC7540]. In particular the
  243. % server MUST not make a new TCP connection to the host and port
  244. % indicated by the :authority.
  245. reject_missing_pseudo_header_authority(Config) ->
  246. doc("An extended CONNECT request without an authority component must be rejected "
  247. "with a PROTOCOL_ERROR stream error. (draft-01 4, draft-01 5)"),
  248. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  249. {ok, Socket, Settings} = do_handshake(Config),
  250. #{enable_connect_protocol := true} = Settings,
  251. %% Send an extended CONNECT request without an :authority pseudo-header.
  252. {ReqHeadersBlock, _} = cow_hpack:encode([
  253. {<<":method">>, <<"CONNECT">>},
  254. {<<":protocol">>, <<"websocket">>},
  255. {<<":scheme">>, <<"http">>},
  256. {<<":path">>, <<"/ws">>},
  257. {<<"sec-websocket-version">>, <<"13">>},
  258. {<<"origin">>, <<"http://localhost">>}
  259. ]),
  260. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  261. %% Receive a PROTOCOL_ERROR stream error.
  262. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  263. ok.
  264. % Using Extended CONNECT To Bootstrap The WebSocket Protocol.
  265. reject_missing_pseudo_header_protocol(Config) ->
  266. doc("An extended CONNECT request without a protocol component must be rejected "
  267. "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  268. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  269. {ok, Socket, Settings} = do_handshake(Config),
  270. #{enable_connect_protocol := true} = Settings,
  271. %% Send an extended CONNECT request without a :scheme pseudo-header.
  272. {ReqHeadersBlock, _} = cow_hpack:encode([
  273. {<<":method">>, <<"CONNECT">>},
  274. {<<":scheme">>, <<"http">>},
  275. {<<":path">>, <<"/ws">>},
  276. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  277. {<<"sec-websocket-version">>, <<"13">>},
  278. {<<"origin">>, <<"http://localhost">>}
  279. ]),
  280. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  281. %% Receive a PROTOCOL_ERROR stream error.
  282. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  283. ok.
  284. % The scheme of the Target URI [RFC7230] MUST be https for wss schemed
  285. % WebSockets and http for ws schemed WebSockets. The websocket URI is
  286. % still used for proxy autoconfiguration.
  287. reject_connection_header(Config) ->
  288. doc("An extended CONNECT request with a connection header must be rejected "
  289. "with a PROTOCOL_ERROR stream error. (draft-01 5, RFC7540 8.1.2.6)"),
  290. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  291. {ok, Socket, Settings} = do_handshake(Config),
  292. #{enable_connect_protocol := true} = Settings,
  293. %% Send an extended CONNECT request without a :scheme pseudo-header.
  294. {ReqHeadersBlock, _} = cow_hpack:encode([
  295. {<<":method">>, <<"CONNECT">>},
  296. {<<":protocol">>, <<"websocket">>},
  297. {<<":scheme">>, <<"http">>},
  298. {<<":path">>, <<"/ws">>},
  299. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  300. {<<"connection">>, <<"upgrade">>},
  301. {<<"sec-websocket-version">>, <<"13">>},
  302. {<<"origin">>, <<"http://localhost">>}
  303. ]),
  304. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  305. %% Receive a PROTOCOL_ERROR stream error.
  306. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  307. ok.
  308. reject_upgrade_header(Config) ->
  309. doc("An extended CONNECT request with a upgrade header must be rejected "
  310. "with a PROTOCOL_ERROR stream error. (draft-01 5, RFC7540 8.1.2.6)"),
  311. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  312. {ok, Socket, Settings} = do_handshake(Config),
  313. #{enable_connect_protocol := true} = Settings,
  314. %% Send an extended CONNECT request without a :scheme pseudo-header.
  315. {ReqHeadersBlock, _} = cow_hpack:encode([
  316. {<<":method">>, <<"CONNECT">>},
  317. {<<":protocol">>, <<"websocket">>},
  318. {<<":scheme">>, <<"http">>},
  319. {<<":path">>, <<"/ws">>},
  320. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  321. {<<"upgrade">>, <<"websocket">>},
  322. {<<"sec-websocket-version">>, <<"13">>},
  323. {<<"origin">>, <<"http://localhost">>}
  324. ]),
  325. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  326. %% Receive a PROTOCOL_ERROR stream error.
  327. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  328. ok.
  329. % After successfully processing the opening handshake the peers should
  330. % proceed with The WebSocket Protocol [RFC6455] using the HTTP/2 stream
  331. % from the CONNECT transaction as if it were the TCP connection
  332. % referred to in [RFC6455]. The state of the WebSocket connection at
  333. % this point is OPEN as defined by [RFC6455], Section 4.1.
  334. %% @todo I'm guessing we should test for things like RST_STREAM,
  335. %% closing the connection and others?
  336. % Examples.
  337. %% @todo Probably worth testing that we get the correct option
  338. %% over all different connection types (alpn, prior, upgrade).
  339. accept_handshake_when_enabled(Config) ->
  340. doc("Confirm the example for Websocket over HTTP/2 works. (draft-01 5.1)"),
  341. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  342. {ok, Socket, Settings} = do_handshake(Config),
  343. #{enable_connect_protocol := true} = Settings,
  344. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  345. {ReqHeadersBlock, _} = cow_hpack:encode([
  346. {<<":method">>, <<"CONNECT">>},
  347. {<<":protocol">>, <<"websocket">>},
  348. {<<":scheme">>, <<"http">>},
  349. {<<":path">>, <<"/ws">>},
  350. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  351. {<<"sec-websocket-version">>, <<"13">>},
  352. {<<"origin">>, <<"http://localhost">>}
  353. ]),
  354. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  355. %% Receive a 200 response.
  356. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  357. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  358. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  359. {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  360. %% Masked text hello echoed back clear by the server.
  361. Mask = 16#37fa213d,
  362. MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
  363. ok = gen_tcp:send(Socket, cow_http2:data(1, nofin,
  364. <<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary>>)),
  365. {ok, <<Len2:24, _:8, _:8, _:32>>} = gen_tcp:recv(Socket, 9, 1000),
  366. {ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, Len2, 1000),
  367. ok.
  368. %% Closing a Websocket stream.
  369. % The HTTP/2 stream closure is also analagous to the TCP connection closure of
  370. % [RFC6455]. Orderly TCP level closures are represented as END_STREAM
  371. % ([RFC7540] Section 6.1) flags and RST exceptions are represented with
  372. % the RST_STREAM ([RFC7540] Section 6.4) frame with the CANCEL
  373. % ([RFC7540] Secion 7) error code.
  374. %% @todo client close frame with END_STREAM
  375. %% @todo server close frame with END_STREAM
  376. %% @todo client other frame with END_STREAM
  377. %% @todo server other frame with END_STREAM
  378. %% @todo client close connection