rfc8441_SUITE.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. %% @todo Refer to RFC9110 7.8 about the case insensitive comparison.
  114. accept_uppercase_pseudo_header_protocol(Config) ->
  115. doc("The :protocol pseudo header is case insensitive. (draft-01 4)"),
  116. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  117. {ok, Socket, Settings} = do_handshake(Config),
  118. #{enable_connect_protocol := true} = Settings,
  119. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  120. {ReqHeadersBlock, _} = cow_hpack:encode([
  121. {<<":method">>, <<"CONNECT">>},
  122. {<<":protocol">>, <<"WEBSOCKET">>},
  123. {<<":scheme">>, <<"http">>},
  124. {<<":path">>, <<"/ws">>},
  125. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  126. {<<"sec-websocket-version">>, <<"13">>},
  127. {<<"origin">>, <<"http://localhost">>}
  128. ]),
  129. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  130. %% Receive a 200 response.
  131. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  132. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  133. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  134. {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  135. ok.
  136. reject_many_pseudo_header_protocol(Config) ->
  137. doc("An extended CONNECT request containing more than one protocol component "
  138. "must be rejected with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  139. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  140. {ok, Socket, Settings} = do_handshake(Config),
  141. #{enable_connect_protocol := true} = Settings,
  142. %% Send an extended CONNECT request with more than one :protocol pseudo-header.
  143. {ReqHeadersBlock, _} = cow_hpack:encode([
  144. {<<":method">>, <<"CONNECT">>},
  145. {<<":protocol">>, <<"websocket">>},
  146. {<<":protocol">>, <<"mqtt">>},
  147. {<<":scheme">>, <<"http">>},
  148. {<<":path">>, <<"/ws">>},
  149. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  150. {<<"sec-websocket-version">>, <<"13">>},
  151. {<<"origin">>, <<"http://localhost">>}
  152. ]),
  153. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  154. %% Receive a PROTOCOL_ERROR stream error.
  155. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  156. ok.
  157. reject_unknown_pseudo_header_protocol(Config) ->
  158. %% @todo This probably shouldn't send 400 but 501 instead based on RFC 9220.
  159. doc("An extended CONNECT request with an unknown protocol must be rejected "
  160. "with a 400 error. (draft-01 4)"),
  161. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  162. {ok, Socket, Settings} = do_handshake(Config),
  163. #{enable_connect_protocol := true} = Settings,
  164. %% Send an extended CONNECT request with an unknown :protocol pseudo-header.
  165. {ReqHeadersBlock, _} = cow_hpack:encode([
  166. {<<":method">>, <<"CONNECT">>},
  167. {<<":protocol">>, <<"mqtt">>},
  168. {<<":scheme">>, <<"http">>},
  169. {<<":path">>, <<"/ws">>},
  170. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  171. {<<"sec-websocket-version">>, <<"13">>},
  172. {<<"origin">>, <<"http://localhost">>}
  173. ]),
  174. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  175. %% Receive a 400 response.
  176. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  177. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  178. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  179. {_, <<"501">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  180. ok.
  181. reject_invalid_pseudo_header_protocol(Config) ->
  182. %% @todo This probably shouldn't send 400 but 501 instead based on RFC 9220.
  183. doc("An extended CONNECT request with an invalid protocol must be rejected "
  184. "with a 400 error. (draft-01 4)"),
  185. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  186. {ok, Socket, Settings} = do_handshake(Config),
  187. #{enable_connect_protocol := true} = Settings,
  188. %% Send an extended CONNECT request with an invalid :protocol pseudo-header.
  189. {ReqHeadersBlock, _} = cow_hpack:encode([
  190. {<<":method">>, <<"CONNECT">>},
  191. {<<":protocol">>, <<"websocket mqtt">>},
  192. {<<":scheme">>, <<"http">>},
  193. {<<":path">>, <<"/ws">>},
  194. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  195. {<<"sec-websocket-version">>, <<"13">>},
  196. {<<"origin">>, <<"http://localhost">>}
  197. ]),
  198. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  199. %% Receive a 400 response.
  200. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  201. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  202. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  203. {_, <<"501">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  204. ok.
  205. reject_missing_pseudo_header_scheme(Config) ->
  206. doc("An extended CONNECT request without a scheme component must be rejected "
  207. "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  208. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  209. {ok, Socket, Settings} = do_handshake(Config),
  210. #{enable_connect_protocol := true} = Settings,
  211. %% Send an extended CONNECT request without a :scheme pseudo-header.
  212. {ReqHeadersBlock, _} = cow_hpack:encode([
  213. {<<":method">>, <<"CONNECT">>},
  214. {<<":protocol">>, <<"websocket">>},
  215. {<<":path">>, <<"/ws">>},
  216. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  217. {<<"sec-websocket-version">>, <<"13">>},
  218. {<<"origin">>, <<"http://localhost">>}
  219. ]),
  220. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  221. %% Receive a PROTOCOL_ERROR stream error.
  222. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  223. ok.
  224. reject_missing_pseudo_header_path(Config) ->
  225. doc("An extended CONNECT request without a path component must be rejected "
  226. "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  227. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  228. {ok, Socket, Settings} = do_handshake(Config),
  229. #{enable_connect_protocol := true} = Settings,
  230. %% Send an extended CONNECT request without a :path pseudo-header.
  231. {ReqHeadersBlock, _} = cow_hpack:encode([
  232. {<<":method">>, <<"CONNECT">>},
  233. {<<":protocol">>, <<"websocket">>},
  234. {<<":scheme">>, <<"http">>},
  235. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  236. {<<"sec-websocket-version">>, <<"13">>},
  237. {<<"origin">>, <<"http://localhost">>}
  238. ]),
  239. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  240. %% Receive a PROTOCOL_ERROR stream error.
  241. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  242. ok.
  243. % On requests bearing the :protocol pseudo-header, the :authority
  244. % pseudo-header field is interpreted according to Section 8.1.2.3 of
  245. % [RFC7540] instead of Section 8.3 of [RFC7540]. In particular the
  246. % server MUST not make a new TCP connection to the host and port
  247. % indicated by the :authority.
  248. reject_missing_pseudo_header_authority(Config) ->
  249. doc("An extended CONNECT request without an authority component must be rejected "
  250. "with a PROTOCOL_ERROR stream error. (draft-01 4, draft-01 5)"),
  251. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  252. {ok, Socket, Settings} = do_handshake(Config),
  253. #{enable_connect_protocol := true} = Settings,
  254. %% Send an extended CONNECT request without an :authority pseudo-header.
  255. {ReqHeadersBlock, _} = cow_hpack:encode([
  256. {<<":method">>, <<"CONNECT">>},
  257. {<<":protocol">>, <<"websocket">>},
  258. {<<":scheme">>, <<"http">>},
  259. {<<":path">>, <<"/ws">>},
  260. {<<"sec-websocket-version">>, <<"13">>},
  261. {<<"origin">>, <<"http://localhost">>}
  262. ]),
  263. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  264. %% Receive a PROTOCOL_ERROR stream error.
  265. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  266. ok.
  267. % Using Extended CONNECT To Bootstrap The WebSocket Protocol.
  268. reject_missing_pseudo_header_protocol(Config) ->
  269. doc("An extended CONNECT request without a protocol component must be rejected "
  270. "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
  271. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  272. {ok, Socket, Settings} = do_handshake(Config),
  273. #{enable_connect_protocol := true} = Settings,
  274. %% Send an extended CONNECT request without a :protocol pseudo-header.
  275. {ReqHeadersBlock, _} = cow_hpack:encode([
  276. {<<":method">>, <<"CONNECT">>},
  277. {<<":scheme">>, <<"http">>},
  278. {<<":path">>, <<"/ws">>},
  279. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  280. {<<"sec-websocket-version">>, <<"13">>},
  281. {<<"origin">>, <<"http://localhost">>}
  282. ]),
  283. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  284. %% Receive a PROTOCOL_ERROR stream error.
  285. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  286. ok.
  287. % The scheme of the Target URI [RFC7230] MUST be https for wss schemed
  288. % WebSockets and http for ws schemed WebSockets. The websocket URI is
  289. % still used for proxy autoconfiguration.
  290. reject_connection_header(Config) ->
  291. doc("An extended CONNECT request with a connection header must be rejected "
  292. "with a PROTOCOL_ERROR stream error. (draft-01 5, RFC7540 8.1.2.6)"),
  293. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  294. {ok, Socket, Settings} = do_handshake(Config),
  295. #{enable_connect_protocol := true} = Settings,
  296. %% Send an extended CONNECT request with a connection header.
  297. {ReqHeadersBlock, _} = cow_hpack:encode([
  298. {<<":method">>, <<"CONNECT">>},
  299. {<<":protocol">>, <<"websocket">>},
  300. {<<":scheme">>, <<"http">>},
  301. {<<":path">>, <<"/ws">>},
  302. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  303. {<<"connection">>, <<"upgrade">>},
  304. {<<"sec-websocket-version">>, <<"13">>},
  305. {<<"origin">>, <<"http://localhost">>}
  306. ]),
  307. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  308. %% Receive a PROTOCOL_ERROR stream error.
  309. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  310. ok.
  311. reject_upgrade_header(Config) ->
  312. doc("An extended CONNECT request with a upgrade header must be rejected "
  313. "with a PROTOCOL_ERROR stream error. (draft-01 5, RFC7540 8.1.2.6)"),
  314. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  315. {ok, Socket, Settings} = do_handshake(Config),
  316. #{enable_connect_protocol := true} = Settings,
  317. %% Send an extended CONNECT request with a upgrade header.
  318. {ReqHeadersBlock, _} = cow_hpack:encode([
  319. {<<":method">>, <<"CONNECT">>},
  320. {<<":protocol">>, <<"websocket">>},
  321. {<<":scheme">>, <<"http">>},
  322. {<<":path">>, <<"/ws">>},
  323. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  324. {<<"upgrade">>, <<"websocket">>},
  325. {<<"sec-websocket-version">>, <<"13">>},
  326. {<<"origin">>, <<"http://localhost">>}
  327. ]),
  328. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  329. %% Receive a PROTOCOL_ERROR stream error.
  330. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  331. ok.
  332. % After successfully processing the opening handshake the peers should
  333. % proceed with The WebSocket Protocol [RFC6455] using the HTTP/2 stream
  334. % from the CONNECT transaction as if it were the TCP connection
  335. % referred to in [RFC6455]. The state of the WebSocket connection at
  336. % this point is OPEN as defined by [RFC6455], Section 4.1.
  337. %% @todo I'm guessing we should test for things like RST_STREAM,
  338. %% closing the connection and others?
  339. % Examples.
  340. %% @todo Probably worth testing that we get the correct option
  341. %% over all different connection types (alpn, prior, upgrade).
  342. accept_handshake_when_enabled(Config) ->
  343. doc("Confirm the example for Websocket over HTTP/2 works. (draft-01 5.1)"),
  344. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  345. {ok, Socket, Settings} = do_handshake(Config),
  346. #{enable_connect_protocol := true} = Settings,
  347. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  348. {ReqHeadersBlock, _} = cow_hpack:encode([
  349. {<<":method">>, <<"CONNECT">>},
  350. {<<":protocol">>, <<"websocket">>},
  351. {<<":scheme">>, <<"http">>},
  352. {<<":path">>, <<"/ws">>},
  353. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  354. {<<"sec-websocket-version">>, <<"13">>},
  355. {<<"origin">>, <<"http://localhost">>}
  356. ]),
  357. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
  358. %% Receive a 200 response.
  359. {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  360. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
  361. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  362. {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  363. %% Masked text hello echoed back clear by the server.
  364. Mask = 16#37fa213d,
  365. MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
  366. ok = gen_tcp:send(Socket, cow_http2:data(1, nofin,
  367. <<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary>>)),
  368. {ok, <<Len2:24, _:8, _:8, _:32>>} = gen_tcp:recv(Socket, 9, 1000),
  369. {ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, Len2, 1000),
  370. ok.
  371. %% Closing a Websocket stream.
  372. % The HTTP/2 stream closure is also analagous to the TCP connection closure of
  373. % [RFC6455]. Orderly TCP level closures are represented as END_STREAM
  374. % ([RFC7540] Section 6.1) flags and RST exceptions are represented with
  375. % the RST_STREAM ([RFC7540] Section 6.4) frame with the CANCEL
  376. % ([RFC7540] Secion 7) error code.
  377. %% @todo client close frame with END_STREAM
  378. %% @todo server close frame with END_STREAM
  379. %% @todo client other frame with END_STREAM
  380. %% @todo server other frame with END_STREAM
  381. %% @todo client close connection