rfc9220_SUITE.erl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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(rfc9220_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, [], Tests}]. %% @todo Enable parallel when all is better.
  23. init_per_group(Name = enabled, Config) ->
  24. cowboy_test:init_http3(Name, #{
  25. enable_connect_protocol => true,
  26. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  27. }, Config).
  28. end_per_group(Name, _) ->
  29. cowboy_test:stop_group(Name).
  30. init_routes(_) -> [
  31. {"localhost", [
  32. {"/ws", ws_echo, []}
  33. ]}
  34. ].
  35. % The SETTINGS_ENABLE_CONNECT_PROTOCOL SETTINGS Parameter.
  36. % The new parameter name is SETTINGS_ENABLE_CONNECT_PROTOCOL. The
  37. % value of the parameter MUST be 0 or 1.
  38. % Upon receipt of SETTINGS_ENABLE_CONNECT_PROTOCOL with a value of 1 a
  39. % client MAY use the Extended CONNECT definition of this document when
  40. % creating new streams. Receipt of this parameter by a server does not
  41. % have any impact.
  42. %% @todo ignore_client_enable_setting(Config) ->
  43. reject_handshake_when_disabled(Config0) ->
  44. doc("Extended CONNECT requests MUST be rejected with a "
  45. "H3_MESSAGE_ERROR stream error when enable_connect_protocol=false. "
  46. "(RFC9220, RFC8441 4)"),
  47. Config = cowboy_test:init_http3(disabled, #{
  48. enable_connect_protocol => false,
  49. env => #{dispatch => cowboy_router:compile(init_routes(Config0))}
  50. }, Config0),
  51. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 0.
  52. #{
  53. conn := Conn,
  54. settings := Settings
  55. } = rfc9114_SUITE:do_connect(Config),
  56. case Settings of
  57. #{enable_connect_protocol := false} -> ok;
  58. _ when map_size(Settings) =:= 0 -> ok
  59. end,
  60. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  61. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  62. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  63. {<<":method">>, <<"CONNECT">>},
  64. {<<":protocol">>, <<"websocket">>},
  65. {<<":scheme">>, <<"https">>},
  66. {<<":path">>, <<"/ws">>},
  67. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  68. {<<"sec-websocket-version">>, <<"13">>},
  69. {<<"origin">>, <<"http://localhost">>}
  70. ], 0, cow_qpack:init()),
  71. {ok, _} = quicer:send(StreamRef, [
  72. <<1>>, %% HEADERS frame.
  73. cow_http3:encode_int(iolist_size(EncodedRequest)),
  74. EncodedRequest
  75. ]),
  76. %% The stream should have been aborted.
  77. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  78. ok.
  79. reject_handshake_disabled_by_default(Config0) ->
  80. doc("Extended CONNECT requests MUST be rejected with a "
  81. "H3_MESSAGE_ERROR stream error when enable_connect_protocol=false. "
  82. "(RFC9220, RFC8441 4)"),
  83. Config = cowboy_test:init_http3(disabled, #{
  84. env => #{dispatch => cowboy_router:compile(init_routes(Config0))}
  85. }, Config0),
  86. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 0.
  87. #{
  88. conn := Conn,
  89. settings := Settings
  90. } = rfc9114_SUITE:do_connect(Config),
  91. case Settings of
  92. #{enable_connect_protocol := false} -> ok;
  93. _ when map_size(Settings) =:= 0 -> ok
  94. end,
  95. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  96. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  97. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  98. {<<":method">>, <<"CONNECT">>},
  99. {<<":protocol">>, <<"websocket">>},
  100. {<<":scheme">>, <<"https">>},
  101. {<<":path">>, <<"/ws">>},
  102. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  103. {<<"sec-websocket-version">>, <<"13">>},
  104. {<<"origin">>, <<"http://localhost">>}
  105. ], 0, cow_qpack:init()),
  106. {ok, _} = quicer:send(StreamRef, [
  107. <<1>>, %% HEADERS frame.
  108. cow_http3:encode_int(iolist_size(EncodedRequest)),
  109. EncodedRequest
  110. ]),
  111. %% The stream should have been aborted.
  112. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  113. ok.
  114. % The Extended CONNECT Method.
  115. accept_uppercase_pseudo_header_protocol(Config) ->
  116. doc("The :protocol pseudo header is case insensitive. (RFC9220, RFC8441 4, RFC9110 7.8)"),
  117. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  118. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  119. #{enable_connect_protocol := true} = Settings,
  120. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  121. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  122. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  123. {<<":method">>, <<"CONNECT">>},
  124. {<<":protocol">>, <<"WEBSOCKET">>},
  125. {<<":scheme">>, <<"https">>},
  126. {<<":path">>, <<"/ws">>},
  127. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  128. {<<"sec-websocket-version">>, <<"13">>},
  129. {<<"origin">>, <<"http://localhost">>}
  130. ], 0, cow_qpack:init()),
  131. {ok, _} = quicer:send(StreamRef, [
  132. <<1>>, %% HEADERS frame.
  133. cow_http3:encode_int(iolist_size(EncodedRequest)),
  134. EncodedRequest
  135. ]),
  136. %% Receive a 200 response.
  137. {ok, Data} = rfc9114_SUITE:do_receive_data(StreamRef),
  138. {HLenEnc, HLenBits} = rfc9114_SUITE:do_guess_int_encoding(Data),
  139. <<
  140. 1, %% HEADERS frame.
  141. HLenEnc:2, HLen:HLenBits,
  142. EncodedResponse:HLen/bytes
  143. >> = Data,
  144. {ok, DecodedResponse, _DecData, _DecSt}
  145. = cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init()),
  146. #{<<":status">> := <<"200">>} = maps:from_list(DecodedResponse),
  147. ok.
  148. reject_many_pseudo_header_protocol(Config) ->
  149. doc("An extended CONNECT request containing more than one "
  150. "protocol component must be rejected with a H3_MESSAGE_ERROR "
  151. "stream error. (RFC9220, RFC9114 4.3.1, RFC9114 4.1.2)"),
  152. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  153. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  154. #{enable_connect_protocol := true} = Settings,
  155. %% Send an extended CONNECT request with more than one :protocol pseudo-header.
  156. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  157. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  158. {<<":method">>, <<"CONNECT">>},
  159. {<<":protocol">>, <<"websocket">>},
  160. {<<":protocol">>, <<"mqtt">>},
  161. {<<":scheme">>, <<"https">>},
  162. {<<":path">>, <<"/ws">>},
  163. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  164. {<<"sec-websocket-version">>, <<"13">>},
  165. {<<"origin">>, <<"http://localhost">>}
  166. ], 0, cow_qpack:init()),
  167. {ok, _} = quicer:send(StreamRef, [
  168. <<1>>, %% HEADERS frame.
  169. cow_http3:encode_int(iolist_size(EncodedRequest)),
  170. EncodedRequest
  171. ]),
  172. %% The stream should have been aborted.
  173. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  174. ok.
  175. reject_unknown_pseudo_header_protocol(Config) ->
  176. doc("An extended CONNECT request containing more than one "
  177. "protocol component must be rejected with a 501 Not Implemented "
  178. "response. (RFC9220, RFC8441 4)"),
  179. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  180. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  181. #{enable_connect_protocol := true} = Settings,
  182. %% Send an extended CONNECT request with an unknown protocol.
  183. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  184. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  185. {<<":method">>, <<"CONNECT">>},
  186. {<<":protocol">>, <<"mqtt">>},
  187. {<<":scheme">>, <<"https">>},
  188. {<<":path">>, <<"/ws">>},
  189. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  190. {<<"sec-websocket-version">>, <<"13">>},
  191. {<<"origin">>, <<"http://localhost">>}
  192. ], 0, cow_qpack:init()),
  193. {ok, _} = quicer:send(StreamRef, [
  194. <<1>>, %% HEADERS frame.
  195. cow_http3:encode_int(iolist_size(EncodedRequest)),
  196. EncodedRequest
  197. ]),
  198. %% The stream should have been rejected with a 501 Not Implemented.
  199. #{headers := #{<<":status">> := <<"501">>}} = rfc9114_SUITE:do_receive_response(StreamRef),
  200. ok.
  201. reject_invalid_pseudo_header_protocol(Config) ->
  202. doc("An extended CONNECT request with an invalid protocol "
  203. "component must be rejected with a 501 Not Implemented "
  204. "response. (RFC9220, RFC8441 4)"),
  205. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  206. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  207. #{enable_connect_protocol := true} = Settings,
  208. %% Send an extended CONNECT request with an invalid protocol.
  209. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  210. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  211. {<<":method">>, <<"CONNECT">>},
  212. {<<":protocol">>, <<"websocket mqtt">>},
  213. {<<":scheme">>, <<"https">>},
  214. {<<":path">>, <<"/ws">>},
  215. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  216. {<<"sec-websocket-version">>, <<"13">>},
  217. {<<"origin">>, <<"http://localhost">>}
  218. ], 0, cow_qpack:init()),
  219. {ok, _} = quicer:send(StreamRef, [
  220. <<1>>, %% HEADERS frame.
  221. cow_http3:encode_int(iolist_size(EncodedRequest)),
  222. EncodedRequest
  223. ]),
  224. %% The stream should have been rejected with a 501 Not Implemented.
  225. #{headers := #{<<":status">> := <<"501">>}} = rfc9114_SUITE:do_receive_response(StreamRef),
  226. ok.
  227. reject_missing_pseudo_header_scheme(Config) ->
  228. doc("An extended CONNECT request whtout a scheme component "
  229. "must be rejected with a H3_MESSAGE_ERROR stream error. "
  230. "(RFC9220, RFC9114 4.3.1, RFC9114 4.1.2)"),
  231. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  232. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  233. #{enable_connect_protocol := true} = Settings,
  234. %% Send an extended CONNECT request without a :scheme pseudo-header.
  235. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  236. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  237. {<<":method">>, <<"CONNECT">>},
  238. {<<":protocol">>, <<"websocket">>},
  239. {<<":path">>, <<"/ws">>},
  240. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  241. {<<"sec-websocket-version">>, <<"13">>},
  242. {<<"origin">>, <<"http://localhost">>}
  243. ], 0, cow_qpack:init()),
  244. {ok, _} = quicer:send(StreamRef, [
  245. <<1>>, %% HEADERS frame.
  246. cow_http3:encode_int(iolist_size(EncodedRequest)),
  247. EncodedRequest
  248. ]),
  249. %% The stream should have been aborted.
  250. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  251. ok.
  252. reject_missing_pseudo_header_path(Config) ->
  253. doc("An extended CONNECT request whtout a path component "
  254. "must be rejected with a H3_MESSAGE_ERROR stream error. "
  255. "(RFC9220, RFC9114 4.3.1, RFC9114 4.1.2)"),
  256. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  257. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  258. #{enable_connect_protocol := true} = Settings,
  259. %% Send an extended CONNECT request without a :path pseudo-header.
  260. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  261. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  262. {<<":method">>, <<"CONNECT">>},
  263. {<<":protocol">>, <<"websocket">>},
  264. {<<":scheme">>, <<"https">>},
  265. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  266. {<<"sec-websocket-version">>, <<"13">>},
  267. {<<"origin">>, <<"http://localhost">>}
  268. ], 0, cow_qpack:init()),
  269. {ok, _} = quicer:send(StreamRef, [
  270. <<1>>, %% HEADERS frame.
  271. cow_http3:encode_int(iolist_size(EncodedRequest)),
  272. EncodedRequest
  273. ]),
  274. %% The stream should have been aborted.
  275. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  276. ok.
  277. % On requests bearing the :protocol pseudo-header, the :authority
  278. % pseudo-header field is interpreted according to Section 8.1.2.3 of
  279. % [RFC7540] instead of Section 8.3 of [RFC7540]. In particular the
  280. % server MUST not make a new TCP connection to the host and port
  281. % indicated by the :authority.
  282. reject_missing_pseudo_header_authority(Config) ->
  283. doc("An extended CONNECT request whtout an authority component "
  284. "must be rejected with a H3_MESSAGE_ERROR stream error. "
  285. "(RFC9220, RFC9114 4.3.1, RFC9114 4.1.2)"),
  286. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  287. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  288. #{enable_connect_protocol := true} = Settings,
  289. %% Send an extended CONNECT request without an :authority pseudo-header.
  290. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  291. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  292. {<<":method">>, <<"CONNECT">>},
  293. {<<":protocol">>, <<"websocket">>},
  294. {<<":scheme">>, <<"https">>},
  295. {<<":path">>, <<"/ws">>},
  296. {<<"sec-websocket-version">>, <<"13">>},
  297. {<<"origin">>, <<"http://localhost">>}
  298. ], 0, cow_qpack:init()),
  299. {ok, _} = quicer:send(StreamRef, [
  300. <<1>>, %% HEADERS frame.
  301. cow_http3:encode_int(iolist_size(EncodedRequest)),
  302. EncodedRequest
  303. ]),
  304. %% The stream should have been aborted.
  305. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  306. ok.
  307. % Using Extended CONNECT To Bootstrap The WebSocket Protocol.
  308. reject_missing_pseudo_header_protocol(Config) ->
  309. doc("An extended CONNECT request whtout a protocol component "
  310. "must be rejected with a H3_MESSAGE_ERROR stream error. "
  311. "(RFC9220, RFC9114 4.3.1, RFC9114 4.1.2)"),
  312. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  313. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  314. #{enable_connect_protocol := true} = Settings,
  315. %% Send an extended CONNECT request without a :protocol pseudo-header.
  316. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  317. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  318. {<<":method">>, <<"CONNECT">>},
  319. {<<":scheme">>, <<"https">>},
  320. {<<":path">>, <<"/ws">>},
  321. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  322. {<<"sec-websocket-version">>, <<"13">>},
  323. {<<"origin">>, <<"http://localhost">>}
  324. ], 0, cow_qpack:init()),
  325. {ok, _} = quicer:send(StreamRef, [
  326. <<1>>, %% HEADERS frame.
  327. cow_http3:encode_int(iolist_size(EncodedRequest)),
  328. EncodedRequest
  329. ]),
  330. %% The stream should have been aborted.
  331. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  332. ok.
  333. % The scheme of the Target URI [RFC7230] MUST be https for wss schemed
  334. % WebSockets. HTTP/3 does not provide support for ws schemed WebSockets.
  335. % The websocket URI is still used for proxy autoconfiguration.
  336. reject_connection_header(Config) ->
  337. doc("An extended CONNECT request with a connection header "
  338. "must be rejected with a H3_MESSAGE_ERROR stream error. "
  339. "(RFC9220, RFC8441 4, RFC9114 4.2, RFC9114 4.5, RFC9114 4.1.2)"),
  340. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  341. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  342. #{enable_connect_protocol := true} = Settings,
  343. %% Send an extended CONNECT request with a connection header.
  344. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  345. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  346. {<<":method">>, <<"CONNECT">>},
  347. {<<":protocol">>, <<"websocket">>},
  348. {<<":scheme">>, <<"https">>},
  349. {<<":path">>, <<"/ws">>},
  350. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  351. {<<"connection">>, <<"upgrade">>},
  352. {<<"sec-websocket-version">>, <<"13">>},
  353. {<<"origin">>, <<"http://localhost">>}
  354. ], 0, cow_qpack:init()),
  355. {ok, _} = quicer:send(StreamRef, [
  356. <<1>>, %% HEADERS frame.
  357. cow_http3:encode_int(iolist_size(EncodedRequest)),
  358. EncodedRequest
  359. ]),
  360. %% The stream should have been aborted.
  361. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  362. ok.
  363. reject_upgrade_header(Config) ->
  364. doc("An extended CONNECT request with a upgrade header "
  365. "must be rejected with a H3_MESSAGE_ERROR stream error. "
  366. "(RFC9220, RFC8441 4, RFC9114 4.2, RFC9114 4.5, RFC9114 4.1.2)"),
  367. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  368. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  369. #{enable_connect_protocol := true} = Settings,
  370. %% Send an extended CONNECT request with a upgrade header.
  371. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  372. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  373. {<<":method">>, <<"CONNECT">>},
  374. {<<":protocol">>, <<"websocket">>},
  375. {<<":scheme">>, <<"https">>},
  376. {<<":path">>, <<"/ws">>},
  377. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  378. {<<"upgrade">>, <<"websocket">>},
  379. {<<"sec-websocket-version">>, <<"13">>},
  380. {<<"origin">>, <<"http://localhost">>}
  381. ], 0, cow_qpack:init()),
  382. {ok, _} = quicer:send(StreamRef, [
  383. <<1>>, %% HEADERS frame.
  384. cow_http3:encode_int(iolist_size(EncodedRequest)),
  385. EncodedRequest
  386. ]),
  387. %% The stream should have been aborted.
  388. #{reason := h3_message_error} = rfc9114_SUITE:do_wait_stream_aborted(StreamRef),
  389. ok.
  390. % After successfully processing the opening handshake the peers should
  391. % proceed with The WebSocket Protocol [RFC6455] using the HTTP/2 stream
  392. % from the CONNECT transaction as if it were the TCP connection
  393. % referred to in [RFC6455]. The state of the WebSocket connection at
  394. % this point is OPEN as defined by [RFC6455], Section 4.1.
  395. %% @todo I'm guessing we should test for things like RST_STREAM,
  396. %% closing the connection and others?
  397. % Examples.
  398. accept_handshake_when_enabled(Config) ->
  399. doc("Confirm the example for Websocket over HTTP/2 works. (RFC9220, RFC8441 5.1)"),
  400. %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
  401. #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config),
  402. #{enable_connect_protocol := true} = Settings,
  403. %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
  404. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  405. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  406. {<<":method">>, <<"CONNECT">>},
  407. {<<":protocol">>, <<"websocket">>},
  408. {<<":scheme">>, <<"https">>},
  409. {<<":path">>, <<"/ws">>},
  410. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  411. {<<"sec-websocket-version">>, <<"13">>},
  412. {<<"origin">>, <<"http://localhost">>}
  413. ], 0, cow_qpack:init()),
  414. {ok, _} = quicer:send(StreamRef, [
  415. <<1>>, %% HEADERS frame.
  416. cow_http3:encode_int(iolist_size(EncodedRequest)),
  417. EncodedRequest
  418. ]),
  419. %% Receive a 200 response.
  420. {ok, Data} = rfc9114_SUITE:do_receive_data(StreamRef),
  421. {HLenEnc, HLenBits} = rfc9114_SUITE:do_guess_int_encoding(Data),
  422. <<
  423. 1, %% HEADERS frame.
  424. HLenEnc:2, HLen:HLenBits,
  425. EncodedResponse:HLen/bytes
  426. >> = Data,
  427. {ok, DecodedResponse, _DecData, _DecSt}
  428. = cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init()),
  429. #{<<":status">> := <<"200">>} = maps:from_list(DecodedResponse),
  430. %% Masked text hello echoed back clear by the server.
  431. Mask = 16#37fa213d,
  432. MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
  433. {ok, _} = quicer:send(StreamRef, cow_http3:data(
  434. <<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary>>)),
  435. {ok, WsData} = rfc9114_SUITE:do_receive_data(StreamRef),
  436. <<
  437. 0, %% DATA frame.
  438. 0:2, 7:6, %% Length (2 bytes header + "Hello").
  439. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" %% Websocket frame.
  440. >> = WsData,
  441. ok.
  442. %% Closing a Websocket stream.
  443. % The HTTP/3 stream closure is also analogous to the TCP connection
  444. % closure of [RFC6455]. Orderly TCP-level closures are represented
  445. % as a FIN bit on the stream (Section 4.4 of [HTTP/3]). RST exceptions
  446. % are represented with a stream error (Section 8 of [HTTP/3]) of type
  447. % H3_REQUEST_CANCELLED (Section 8.1 of [HTTP/3]).
  448. %% @todo client close frame with FIN
  449. %% @todo server close frame with FIN
  450. %% @todo client other frame with FIN
  451. %% @todo server other frame with FIN
  452. %% @todo client close connection