rfc9114_SUITE.erl 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  1. %% Copyright (c) 2023, 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(rfc9114_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -include_lib("quicer/include/quicer.hrl").
  20. all() -> [{group, h3}].
  21. groups() ->
  22. %% @todo Enable parallel tests but for this issues in the
  23. %% QUIC accept loop need to be figured out (can't connect
  24. %% concurrently somehow, no backlog?).
  25. [{h3, [], ct_helper:all(?MODULE)}].
  26. init_per_group(Name = h3, Config) ->
  27. cowboy_test:init_http3(Name, #{
  28. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  29. }, Config).
  30. end_per_group(Name, _) ->
  31. cowboy_test:stop_group(Name).
  32. init_routes(_) -> [
  33. {"localhost", [
  34. {"/", hello_h, []},
  35. {"/echo/:key", echo_h, []}
  36. ]}
  37. ].
  38. %% Starting HTTP/3 for "https" URIs.
  39. alpn(Config) ->
  40. doc("Successful ALPN negotiation. (RFC9114 3.1)"),
  41. {ok, Conn} = quicer:connect("localhost", config(port, Config),
  42. #{alpn => ["h3"], verify => none}, 5000),
  43. {ok, <<"h3">>} = quicer:negotiated_protocol(Conn),
  44. %% To make sure the connection is fully established we wait
  45. %% to receive the SETTINGS frame on the control stream.
  46. {ok, _ControlRef, _Settings} = do_wait_settings(Conn),
  47. ok.
  48. alpn_error(Config) ->
  49. doc("Failed ALPN negotiation using the 'h2' token. (RFC9114 3.1)"),
  50. {error, transport_down, #{status := alpn_neg_failure}}
  51. = quicer:connect("localhost", config(port, Config),
  52. #{alpn => ["h2"], verify => none}, 5000),
  53. ok.
  54. %% @todo 3.2. Connection Establishment
  55. %% After the QUIC connection is established, a SETTINGS frame MUST be sent by each endpoint as the initial frame of their respective HTTP control stream.
  56. %% @todo 3.3. Connection Reuse
  57. %% Servers are encouraged to maintain open HTTP/3 connections for as long as
  58. %possible but are permitted to terminate idle connections if necessary. When
  59. %either endpoint chooses to close the HTTP/3 connection, the terminating
  60. %endpoint SHOULD first send a GOAWAY frame (Section 5.2) so that both endpoints
  61. %can reliably determine whether previously sent frames have been processed and
  62. %gracefully complete or terminate any necessary remaining tasks.
  63. %% Frame format.
  64. req_stream(Config) ->
  65. doc("Complete lifecycle of a request stream. (RFC9114 4.1)"),
  66. {ok, Conn} = quicer:connect("localhost", config(port, Config),
  67. #{alpn => ["h3"], verify => none}, 5000),
  68. %% To make sure the connection is fully established we wait
  69. %% to receive the SETTINGS frame on the control stream.
  70. {ok, ControlRef, _Settings} = do_wait_settings(Conn),
  71. %% Send a request on a request stream.
  72. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  73. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  74. {<<":method">>, <<"GET">>},
  75. {<<":scheme">>, <<"https">>},
  76. {<<":authority">>, <<"localhost">>},
  77. {<<":path">>, <<"/">>},
  78. {<<"content-length">>, <<"0">>}
  79. ], 0, cow_qpack:init()),
  80. {ok, _} = quicer:send(StreamRef, [
  81. <<1>>, %% HEADERS frame.
  82. cow_http3:encode_int(iolist_size(EncodedRequest)),
  83. EncodedRequest
  84. ], ?QUIC_SEND_FLAG_FIN),
  85. %% Receive the response.
  86. {ok, Data} = do_receive_data(StreamRef),
  87. {HLenEnc, HLenBits} = do_guess_int_encoding(Data),
  88. <<
  89. 1, %% HEADERS frame.
  90. HLenEnc:2, HLen:HLenBits,
  91. EncodedResponse:HLen/bytes,
  92. Rest/bits
  93. >> = Data,
  94. {ok, DecodedResponse, _DecData, _DecSt}
  95. = cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init()),
  96. #{
  97. <<":status">> := <<"200">>,
  98. <<"content-length">> := BodyLen
  99. } = maps:from_list(DecodedResponse),
  100. {DLenEnc, DLenBits} = do_guess_int_encoding(Rest),
  101. <<
  102. 0, %% DATA frame.
  103. DLenEnc:2, DLen:DLenBits,
  104. Body:DLen/bytes
  105. >> = Rest,
  106. <<"Hello world!">> = Body,
  107. BodyLen = integer_to_binary(byte_size(Body)),
  108. ok = do_wait_peer_send_shutdown(StreamRef),
  109. ok = do_wait_stream_closed(StreamRef).
  110. %% @todo Same test as above but with content-length unset?
  111. req_stream_two_requests(Config) ->
  112. doc("Receipt of multiple requests on a single stream must "
  113. "be rejected with an H3_MESSAGE_ERROR stream error. "
  114. "(RFC9114 4.1, RFC9114 4.1.2)"),
  115. {ok, Conn} = quicer:connect("localhost", config(port, Config),
  116. #{alpn => ["h3"], verify => none}, 5000),
  117. %% To make sure the connection is fully established we wait
  118. %% to receive the SETTINGS frame on the control stream.
  119. {ok, ControlRef, _Settings} = do_wait_settings(Conn),
  120. %% Send two requests on a request stream.
  121. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  122. {ok, EncodedRequest1, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  123. {<<":method">>, <<"GET">>},
  124. {<<":scheme">>, <<"https">>},
  125. {<<":authority">>, <<"localhost">>},
  126. {<<":path">>, <<"/">>},
  127. {<<"content-length">>, <<"0">>}
  128. ], 0, cow_qpack:init()),
  129. {ok, EncodedRequest2, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  130. {<<":method">>, <<"GET">>},
  131. {<<":scheme">>, <<"https">>},
  132. {<<":authority">>, <<"localhost">>},
  133. {<<":path">>, <<"/">>},
  134. {<<"content-length">>, <<"0">>}
  135. ], 0, EncSt0),
  136. {ok, _} = quicer:send(StreamRef, [
  137. <<1>>, %% HEADERS frame.
  138. cow_http3:encode_int(iolist_size(EncodedRequest1)),
  139. EncodedRequest1,
  140. <<1>>, %% HEADERS frame.
  141. cow_http3:encode_int(iolist_size(EncodedRequest2)),
  142. EncodedRequest2
  143. ]),
  144. %% The stream should have been aborted.
  145. #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
  146. ok.
  147. headers_then_trailers(Config) ->
  148. doc("Receipt of HEADERS followed by trailer HEADERS must be accepted. (RFC9114 4.1)"),
  149. #{conn := Conn} = do_connect(Config),
  150. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  151. {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  152. {<<":method">>, <<"GET">>},
  153. {<<":scheme">>, <<"https">>},
  154. {<<":authority">>, <<"localhost">>},
  155. {<<":path">>, <<"/">>},
  156. {<<"content-length">>, <<"0">>}
  157. ], 0, cow_qpack:init()),
  158. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  159. {<<"content-type">>, <<"text/plain">>}
  160. ], 0, EncSt0),
  161. {ok, _} = quicer:send(StreamRef, [
  162. <<1>>, %% HEADERS frame.
  163. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  164. EncodedHeaders,
  165. <<1>>, %% HEADERS frame for trailers.
  166. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  167. EncodedTrailers
  168. ], ?QUIC_SEND_FLAG_FIN),
  169. #{
  170. headers := #{<<":status">> := <<"200">>},
  171. body := <<"Hello world!">>
  172. } = do_receive_response(StreamRef),
  173. ok.
  174. headers_then_data_then_trailers(Config) ->
  175. doc("Receipt of HEADERS followed by DATA followed by trailer HEADERS "
  176. "must be accepted. (RFC9114 4.1)"),
  177. #{conn := Conn} = do_connect(Config),
  178. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  179. {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  180. {<<":method">>, <<"GET">>},
  181. {<<":scheme">>, <<"https">>},
  182. {<<":authority">>, <<"localhost">>},
  183. {<<":path">>, <<"/">>},
  184. {<<"content-length">>, <<"13">>}
  185. ], 0, cow_qpack:init()),
  186. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  187. {<<"content-type">>, <<"text/plain">>}
  188. ], 0, EncSt0),
  189. {ok, _} = quicer:send(StreamRef, [
  190. <<1>>, %% HEADERS frame.
  191. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  192. EncodedHeaders,
  193. <<0>>, %% DATA frame.
  194. cow_http3:encode_int(13),
  195. <<"Hello server!">>,
  196. <<1>>, %% HEADERS frame for trailers.
  197. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  198. EncodedTrailers
  199. ], ?QUIC_SEND_FLAG_FIN),
  200. #{
  201. headers := #{<<":status">> := <<"200">>},
  202. body := <<"Hello world!">>
  203. } = do_receive_response(StreamRef),
  204. ok.
  205. data_then_headers(Config) ->
  206. doc("Receipt of DATA before HEADERS must be rejected "
  207. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
  208. #{conn := Conn} = do_connect(Config),
  209. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  210. {ok, EncodedHeaders, _EncData1, _EncSt} = cow_qpack:encode_field_section([
  211. {<<":method">>, <<"GET">>},
  212. {<<":scheme">>, <<"https">>},
  213. {<<":authority">>, <<"localhost">>},
  214. {<<":path">>, <<"/">>},
  215. {<<"content-length">>, <<"13">>}
  216. ], 0, cow_qpack:init()),
  217. {ok, _} = quicer:send(StreamRef, [
  218. <<0>>, %% DATA frame.
  219. cow_http3:encode_int(13),
  220. <<"Hello server!">>,
  221. <<1>>, %% HEADERS frame.
  222. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  223. EncodedHeaders
  224. ], ?QUIC_SEND_FLAG_FIN),
  225. %% The connection should have been closed.
  226. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  227. ok.
  228. headers_then_trailers_then_data(Config) ->
  229. doc("Receipt of DATA after trailer HEADERS must be rejected "
  230. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
  231. #{conn := Conn} = do_connect(Config),
  232. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  233. {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  234. {<<":method">>, <<"GET">>},
  235. {<<":scheme">>, <<"https">>},
  236. {<<":authority">>, <<"localhost">>},
  237. {<<":path">>, <<"/">>},
  238. {<<"content-length">>, <<"13">>}
  239. ], 0, cow_qpack:init()),
  240. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  241. {<<"content-type">>, <<"text/plain">>}
  242. ], 0, EncSt0),
  243. {ok, _} = quicer:send(StreamRef, [
  244. <<1>>, %% HEADERS frame.
  245. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  246. EncodedHeaders,
  247. <<1>>, %% HEADERS frame for trailers.
  248. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  249. EncodedTrailers,
  250. <<0>>, %% DATA frame.
  251. cow_http3:encode_int(13),
  252. <<"Hello server!">>
  253. ], ?QUIC_SEND_FLAG_FIN),
  254. %% The connection should have been closed.
  255. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  256. ok.
  257. headers_then_data_then_trailers_then_data(Config) ->
  258. doc("Receipt of DATA after trailer HEADERS must be rejected "
  259. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
  260. #{conn := Conn} = do_connect(Config),
  261. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  262. {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  263. {<<":method">>, <<"GET">>},
  264. {<<":scheme">>, <<"https">>},
  265. {<<":authority">>, <<"localhost">>},
  266. {<<":path">>, <<"/">>},
  267. {<<"content-length">>, <<"13">>}
  268. ], 0, cow_qpack:init()),
  269. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  270. {<<"content-type">>, <<"text/plain">>}
  271. ], 0, EncSt0),
  272. {ok, _} = quicer:send(StreamRef, [
  273. <<1>>, %% HEADERS frame.
  274. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  275. EncodedHeaders,
  276. <<0>>, %% DATA frame.
  277. cow_http3:encode_int(13),
  278. <<"Hello server!">>,
  279. <<1>>, %% HEADERS frame for trailers.
  280. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  281. EncodedTrailers,
  282. <<0>>, %% DATA frame.
  283. cow_http3:encode_int(13),
  284. <<"Hello server!">>
  285. ], ?QUIC_SEND_FLAG_FIN),
  286. %% The connection should have been closed.
  287. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  288. ok.
  289. headers_then_data_then_trailers_then_trailers(Config) ->
  290. doc("Receipt of DATA after trailer HEADERS must be rejected "
  291. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
  292. #{conn := Conn} = do_connect(Config),
  293. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  294. {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  295. {<<":method">>, <<"GET">>},
  296. {<<":scheme">>, <<"https">>},
  297. {<<":authority">>, <<"localhost">>},
  298. {<<":path">>, <<"/">>},
  299. {<<"content-length">>, <<"13">>}
  300. ], 0, cow_qpack:init()),
  301. {ok, EncodedTrailers1, _EncData2, EncSt1} = cow_qpack:encode_field_section([
  302. {<<"content-type">>, <<"text/plain">>}
  303. ], 0, EncSt0),
  304. {ok, EncodedTrailers2, _EncData3, _EncSt} = cow_qpack:encode_field_section([
  305. {<<"content-type">>, <<"text/plain">>}
  306. ], 0, EncSt1),
  307. {ok, _} = quicer:send(StreamRef, [
  308. <<1>>, %% HEADERS frame.
  309. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  310. EncodedHeaders,
  311. <<0>>, %% DATA frame.
  312. cow_http3:encode_int(13),
  313. <<"Hello server!">>,
  314. <<1>>, %% HEADERS frame for trailers.
  315. cow_http3:encode_int(iolist_size(EncodedTrailers1)),
  316. EncodedTrailers1,
  317. <<1>>, %% HEADERS frame for trailers.
  318. cow_http3:encode_int(iolist_size(EncodedTrailers2)),
  319. EncodedTrailers2
  320. ], ?QUIC_SEND_FLAG_FIN),
  321. %% The connection should have been closed.
  322. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  323. ok.
  324. unknown_then_headers(Config) ->
  325. doc("Receipt of unknown frame followed by HEADERS "
  326. "must be accepted. (RFC9114 4.1, RFC9114 9)"),
  327. unknown_then_headers(Config, do_unknown_frame_type(),
  328. rand:bytes(rand:uniform(4096))).
  329. unknown_then_headers(Config, Type, Bytes) ->
  330. #{conn := Conn} = do_connect(Config),
  331. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  332. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  333. {<<":method">>, <<"GET">>},
  334. {<<":scheme">>, <<"https">>},
  335. {<<":authority">>, <<"localhost">>},
  336. {<<":path">>, <<"/">>},
  337. {<<"content-length">>, <<"0">>}
  338. ], 0, cow_qpack:init()),
  339. {ok, _} = quicer:send(StreamRef, [
  340. cow_http3:encode_int(Type), %% Unknown frame.
  341. cow_http3:encode_int(iolist_size(Bytes)),
  342. Bytes,
  343. <<1>>, %% HEADERS frame.
  344. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  345. EncodedHeaders
  346. ], ?QUIC_SEND_FLAG_FIN),
  347. #{
  348. headers := #{<<":status">> := <<"200">>},
  349. body := <<"Hello world!">>
  350. } = do_receive_response(StreamRef),
  351. ok.
  352. headers_then_unknown(Config) ->
  353. doc("Receipt of HEADERS followed by unknown frame "
  354. "must be accepted. (RFC9114 4.1, RFC9114 9)"),
  355. headers_then_unknown(Config, do_unknown_frame_type(),
  356. rand:bytes(rand:uniform(4096))).
  357. headers_then_unknown(Config, Type, Bytes) ->
  358. #{conn := Conn} = do_connect(Config),
  359. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  360. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  361. {<<":method">>, <<"GET">>},
  362. {<<":scheme">>, <<"https">>},
  363. {<<":authority">>, <<"localhost">>},
  364. {<<":path">>, <<"/">>},
  365. {<<"content-length">>, <<"0">>}
  366. ], 0, cow_qpack:init()),
  367. {ok, _} = quicer:send(StreamRef, [
  368. <<1>>, %% HEADERS frame.
  369. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  370. EncodedHeaders,
  371. cow_http3:encode_int(Type), %% Unknown frame.
  372. cow_http3:encode_int(iolist_size(Bytes)),
  373. Bytes
  374. ], ?QUIC_SEND_FLAG_FIN),
  375. #{
  376. headers := #{<<":status">> := <<"200">>},
  377. body := <<"Hello world!">>
  378. } = do_receive_response(StreamRef),
  379. ok.
  380. headers_then_data_then_unknown(Config) ->
  381. doc("Receipt of HEADERS followed by DATA followed by unknown frame "
  382. "must be accepted. (RFC9114 4.1, RFC9114 9)"),
  383. headers_then_data_then_unknown(Config, do_unknown_frame_type(),
  384. rand:bytes(rand:uniform(4096))).
  385. headers_then_data_then_unknown(Config, Type, Bytes) ->
  386. #{conn := Conn} = do_connect(Config),
  387. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  388. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  389. {<<":method">>, <<"GET">>},
  390. {<<":scheme">>, <<"https">>},
  391. {<<":authority">>, <<"localhost">>},
  392. {<<":path">>, <<"/">>},
  393. {<<"content-length">>, <<"13">>}
  394. ], 0, cow_qpack:init()),
  395. {ok, _} = quicer:send(StreamRef, [
  396. <<1>>, %% HEADERS frame.
  397. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  398. EncodedHeaders,
  399. <<0>>, %% DATA frame.
  400. cow_http3:encode_int(13),
  401. <<"Hello server!">>,
  402. cow_http3:encode_int(Type), %% Unknown frame.
  403. cow_http3:encode_int(iolist_size(Bytes)),
  404. Bytes
  405. ], ?QUIC_SEND_FLAG_FIN),
  406. #{
  407. headers := #{<<":status">> := <<"200">>},
  408. body := <<"Hello world!">>
  409. } = do_receive_response(StreamRef),
  410. ok.
  411. headers_then_trailers_then_unknown(Config) ->
  412. doc("Receipt of HEADERS followed by trailer HEADERS followed by unknown frame "
  413. "must be accepted. (RFC9114 4.1, RFC9114 9)"),
  414. headers_then_data_then_unknown(Config, do_unknown_frame_type(),
  415. rand:bytes(rand:uniform(4096))).
  416. headers_then_trailers_then_unknown(Config, Type, Bytes) ->
  417. #{conn := Conn} = do_connect(Config),
  418. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  419. {ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
  420. {<<":method">>, <<"GET">>},
  421. {<<":scheme">>, <<"https">>},
  422. {<<":authority">>, <<"localhost">>},
  423. {<<":path">>, <<"/">>},
  424. {<<"content-length">>, <<"13">>}
  425. ], 0, cow_qpack:init()),
  426. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  427. {<<"content-type">>, <<"text/plain">>}
  428. ], 0, EncSt0),
  429. {ok, _} = quicer:send(StreamRef, [
  430. <<1>>, %% HEADERS frame.
  431. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  432. EncodedHeaders,
  433. <<1>>, %% HEADERS frame for trailers.
  434. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  435. EncodedTrailers,
  436. cow_http3:encode_int(Type), %% Unknown frame.
  437. cow_http3:encode_int(iolist_size(Bytes)),
  438. Bytes
  439. ], ?QUIC_SEND_FLAG_FIN),
  440. #{
  441. headers := #{<<":status">> := <<"200">>},
  442. body := <<"Hello world!">>
  443. } = do_receive_response(StreamRef),
  444. ok.
  445. headers_then_data_then_unknown_then_trailers(Config) ->
  446. doc("Receipt of HEADERS followed by DATA followed by "
  447. "unknown frame followed by trailer HEADERS "
  448. "must be accepted. (RFC9114 4.1, RFC9114 9)"),
  449. headers_then_data_then_unknown_then_trailers(Config,
  450. do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).
  451. headers_then_data_then_unknown_then_trailers(Config, Type, Bytes) ->
  452. #{conn := Conn} = do_connect(Config),
  453. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  454. {ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
  455. {<<":method">>, <<"GET">>},
  456. {<<":scheme">>, <<"https">>},
  457. {<<":authority">>, <<"localhost">>},
  458. {<<":path">>, <<"/">>},
  459. {<<"content-length">>, <<"13">>}
  460. ], 0, cow_qpack:init()),
  461. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  462. {<<"content-type">>, <<"text/plain">>}
  463. ], 0, EncSt0),
  464. {ok, _} = quicer:send(StreamRef, [
  465. <<1>>, %% HEADERS frame.
  466. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  467. EncodedHeaders,
  468. <<0>>, %% DATA frame.
  469. cow_http3:encode_int(13),
  470. <<"Hello server!">>,
  471. cow_http3:encode_int(Type), %% Unknown frame.
  472. cow_http3:encode_int(iolist_size(Bytes)),
  473. Bytes,
  474. <<1>>, %% HEADERS frame for trailers.
  475. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  476. EncodedTrailers
  477. ], ?QUIC_SEND_FLAG_FIN),
  478. #{
  479. headers := #{<<":status">> := <<"200">>},
  480. body := <<"Hello world!">>
  481. } = do_receive_response(StreamRef),
  482. ok.
  483. headers_then_data_then_unknown_then_data(Config) ->
  484. doc("Receipt of HEADERS followed by DATA followed by "
  485. "unknown frame followed by DATA "
  486. "must be accepted. (RFC9114 4.1, RFC9114 9)"),
  487. headers_then_data_then_unknown_then_data(Config,
  488. do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).
  489. headers_then_data_then_unknown_then_data(Config, Type, Bytes) ->
  490. #{conn := Conn} = do_connect(Config),
  491. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  492. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  493. {<<":method">>, <<"GET">>},
  494. {<<":scheme">>, <<"https">>},
  495. {<<":authority">>, <<"localhost">>},
  496. {<<":path">>, <<"/">>},
  497. {<<"content-length">>, <<"13">>}
  498. ], 0, cow_qpack:init()),
  499. {ok, _} = quicer:send(StreamRef, [
  500. <<1>>, %% HEADERS frame.
  501. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  502. EncodedHeaders,
  503. <<0>>, %% DATA frame.
  504. cow_http3:encode_int(6),
  505. <<"Hello ">>,
  506. cow_http3:encode_int(Type), %% Unknown frame.
  507. cow_http3:encode_int(iolist_size(Bytes)),
  508. Bytes,
  509. <<0>>, %% DATA frame.
  510. cow_http3:encode_int(7),
  511. <<"server!">>
  512. ], ?QUIC_SEND_FLAG_FIN),
  513. #{
  514. headers := #{<<":status">> := <<"200">>},
  515. body := <<"Hello world!">>
  516. } = do_receive_response(StreamRef),
  517. ok.
  518. headers_then_data_then_trailers_then_unknown(Config) ->
  519. doc("Receipt of HEADERS followed by DATA followed by "
  520. "trailer HEADERS followed by unknown frame "
  521. "must be accepted. (RFC9114 4.1, RFC9114 9)"),
  522. headers_then_data_then_trailers_then_unknown(Config,
  523. do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).
  524. headers_then_data_then_trailers_then_unknown(Config, Type, Bytes) ->
  525. #{conn := Conn} = do_connect(Config),
  526. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  527. {ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
  528. {<<":method">>, <<"GET">>},
  529. {<<":scheme">>, <<"https">>},
  530. {<<":authority">>, <<"localhost">>},
  531. {<<":path">>, <<"/">>},
  532. {<<"content-length">>, <<"13">>}
  533. ], 0, cow_qpack:init()),
  534. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  535. {<<"content-type">>, <<"text/plain">>}
  536. ], 0, EncSt0),
  537. {ok, _} = quicer:send(StreamRef, [
  538. <<1>>, %% HEADERS frame.
  539. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  540. EncodedHeaders,
  541. <<0>>, %% DATA frame.
  542. cow_http3:encode_int(13),
  543. <<"Hello server!">>,
  544. <<1>>, %% HEADERS frame for trailers.
  545. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  546. EncodedTrailers,
  547. cow_http3:encode_int(Type), %% Unknown frame.
  548. cow_http3:encode_int(iolist_size(Bytes)),
  549. Bytes
  550. ], ?QUIC_SEND_FLAG_FIN),
  551. #{
  552. headers := #{<<":status">> := <<"200">>},
  553. body := <<"Hello world!">>
  554. } = do_receive_response(StreamRef),
  555. ok.
  556. do_unknown_frame_type() ->
  557. Type = rand:uniform(4611686018427387904) - 1,
  558. %% Retry if we get a value that's specified.
  559. case lists:member(Type, [
  560. 16#0, 16#1, 16#3, 16#4, 16#5, 16#7, 16#d, %% HTTP/3 core frame types.
  561. 16#2, 16#6, 16#8, 16#9 %% HTTP/3 reserved frame types that must be rejected.
  562. ]) of
  563. true -> do_unknown_frame_type();
  564. false -> Type
  565. end.
  566. reserved_then_headers(Config) ->
  567. doc("Receipt of reserved frame followed by HEADERS "
  568. "must be accepted when the reserved frame type is "
  569. "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
  570. unknown_then_headers(Config, do_reserved_type(),
  571. rand:bytes(rand:uniform(4096))).
  572. headers_then_reserved(Config) ->
  573. doc("Receipt of HEADERS followed by reserved frame "
  574. "must be accepted when the reserved frame type is "
  575. "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
  576. headers_then_unknown(Config, do_reserved_type(),
  577. rand:bytes(rand:uniform(4096))).
  578. headers_then_data_then_reserved(Config) ->
  579. doc("Receipt of HEADERS followed by DATA followed by reserved frame "
  580. "must be accepted when the reserved frame type is "
  581. "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
  582. headers_then_data_then_unknown(Config, do_reserved_type(),
  583. rand:bytes(rand:uniform(4096))).
  584. headers_then_trailers_then_reserved(Config) ->
  585. doc("Receipt of HEADERS followed by trailer HEADERS followed by reserved frame "
  586. "must be accepted when the reserved frame type is "
  587. "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
  588. headers_then_trailers_then_unknown(Config, do_reserved_type(),
  589. rand:bytes(rand:uniform(4096))).
  590. headers_then_data_then_reserved_then_trailers(Config) ->
  591. doc("Receipt of HEADERS followed by DATA followed by "
  592. "reserved frame followed by trailer HEADERS "
  593. "must be accepted when the reserved frame type is "
  594. "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
  595. headers_then_data_then_unknown_then_trailers(Config,
  596. do_reserved_type(), rand:bytes(rand:uniform(4096))).
  597. headers_then_data_then_reserved_then_data(Config) ->
  598. doc("Receipt of HEADERS followed by DATA followed by "
  599. "reserved frame followed by DATA "
  600. "must be accepted when the reserved frame type is "
  601. "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
  602. headers_then_data_then_unknown_then_data(Config,
  603. do_reserved_type(), rand:bytes(rand:uniform(4096))).
  604. headers_then_data_then_trailers_then_reserved(Config) ->
  605. doc("Receipt of HEADERS followed by DATA followed by "
  606. "trailer HEADERS followed by reserved frame "
  607. "must be accepted when the reserved frame type is "
  608. "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
  609. headers_then_data_then_trailers_then_unknown(Config,
  610. do_reserved_type(), rand:bytes(rand:uniform(4096))).
  611. reject_transfer_encoding_header_with_body(Config) ->
  612. doc("Requests containing a transfer-encoding header must be rejected "
  613. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.1, RFC9114 4.1.2, RFC9114 4.2)"),
  614. #{conn := Conn} = do_connect(Config),
  615. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  616. {ok, EncodedHeaders, _EncData1, _EncSt0} = cow_qpack:encode_field_section([
  617. {<<":method">>, <<"GET">>},
  618. {<<":scheme">>, <<"https">>},
  619. {<<":authority">>, <<"localhost">>},
  620. {<<":path">>, <<"/">>},
  621. {<<"transfer-encoding">>, <<"chunked">>}
  622. ], 0, cow_qpack:init()),
  623. {ok, _} = quicer:send(StreamRef, [
  624. <<1>>, %% HEADERS frame.
  625. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  626. EncodedHeaders,
  627. <<0>>, %% DATA frame.
  628. cow_http3:encode_int(24),
  629. <<"13\r\nHello server!\r\n0\r\n\r\n">>
  630. ]),
  631. %% The stream should have been aborted.
  632. #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
  633. ok.
  634. %% 4. Expressing HTTP Semantics in HTTP/3
  635. %% 4.1. HTTP Message Framing
  636. %% An HTTP request/response exchange fully consumes a client-initiated
  637. %bidirectional QUIC stream. After sending a request, a client MUST close the
  638. %stream for sending. Unless using the CONNECT method (see Section 4.4), clients
  639. %MUST NOT make stream closure dependent on receiving a response to their
  640. %request. After sending a final response, the server MUST close the stream for
  641. %sending. At this point, the QUIC stream is fully closed.
  642. %% @todo What to do with clients that DON'T close the stream
  643. %% for sending after the request is sent?
  644. %% If a client-initiated stream terminates without enough of the HTTP message
  645. %to provide a complete response, the server SHOULD abort its response stream
  646. %with the error code H3_REQUEST_INCOMPLETE.
  647. %% @todo difficult!!
  648. %% When the server does not need to receive the remainder of the request, it
  649. %MAY abort reading the request stream, send a complete response, and cleanly
  650. %close the sending part of the stream. The error code H3_NO_ERROR SHOULD be
  651. %used when requesting that the client stop sending on the request stream.
  652. %% @todo read_body related; h2 has this behavior but there is no corresponding test
  653. %% 4.1.1. Request Cancellation and Rejection
  654. %% When possible, it is RECOMMENDED that servers send an HTTP response with an
  655. %appropriate status code rather than cancelling a request it has already begun
  656. %processing.
  657. %% Implementations SHOULD cancel requests by abruptly terminating any
  658. %directions of a stream that are still open. To do so, an implementation resets
  659. %the sending parts of streams and aborts reading on the receiving parts of
  660. %streams; see Section 2.4 of [QUIC-TRANSPORT].
  661. %% When the server cancels a request without performing any application
  662. %processing, the request is considered "rejected". The server SHOULD abort its
  663. %response stream with the error code H3_REQUEST_REJECTED. In this context,
  664. %"processed" means that some data from the stream was passed to some higher
  665. %layer of software that might have taken some action as a result. The client
  666. %can treat requests rejected by the server as though they had never been sent
  667. %at all, thereby allowing them to be retried later.
  668. %% Servers MUST NOT use the H3_REQUEST_REJECTED error code for requests that
  669. %were partially or fully processed. When a server abandons a response after
  670. %partial processing, it SHOULD abort its response stream with the error code
  671. %H3_REQUEST_CANCELLED.
  672. %% @todo
  673. %% Client SHOULD use the error code H3_REQUEST_CANCELLED to cancel requests.
  674. %Upon receipt of this error code, a server MAY abruptly terminate the response
  675. %using the error code H3_REQUEST_REJECTED if no processing was performed.
  676. %Clients MUST NOT use the H3_REQUEST_REJECTED error code, except when a server
  677. %has requested closure of the request stream with this error code.
  678. %% @todo
  679. %4.1.2. Malformed Requests and Responses
  680. %A malformed request or response is one that is an otherwise valid sequence of
  681. %frames but is invalid due to:
  682. %
  683. %the presence of prohibited fields or pseudo-header fields,
  684. %% @todo reject_response_pseudo_headers
  685. %% @todo reject_unknown_pseudo_headers
  686. %% @todo reject_pseudo_headers_in_trailers
  687. %the absence of mandatory pseudo-header fields,
  688. %invalid values for pseudo-header fields,
  689. %pseudo-header fields after fields,
  690. %% @todo reject_pseudo_headers_after_regular_headers
  691. %an invalid sequence of HTTP messages,
  692. %the inclusion of invalid characters in field names or values.
  693. %
  694. %A request or response that is defined as having content when it contains a
  695. %Content-Length header field (Section 8.6 of [HTTP]) is malformed if the value
  696. %of the Content-Length header field does not equal the sum of the DATA frame
  697. %lengths received. A response that is defined as never having content, even
  698. %when a Content-Length is present, can have a non-zero Content-Length header
  699. %field even though no content is included in DATA frames.
  700. %
  701. %For malformed requests, a server MAY send an HTTP response indicating the
  702. %error prior to closing or resetting the stream.
  703. %% @todo All the malformed tests
  704. headers_reject_uppercase_header_name(Config) ->
  705. doc("Requests containing uppercase header names must be rejected "
  706. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
  707. do_reject_malformed_header(Config,
  708. {<<"I-AM-GIGANTIC">>, <<"How's the weather up there?">>}
  709. ).
  710. %% 4.2. HTTP Fields
  711. %% An endpoint MUST NOT generate an HTTP/3 field section containing
  712. %connection-specific fields; any message containing connection-specific fields
  713. %MUST be treated as malformed.
  714. reject_connection_header(Config) ->
  715. doc("Requests containing a connection header must be rejected "
  716. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
  717. do_reject_malformed_header(Config,
  718. {<<"connection">>, <<"close">>}
  719. ).
  720. reject_keep_alive_header(Config) ->
  721. doc("Requests containing a keep-alive header must be rejected "
  722. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
  723. do_reject_malformed_header(Config,
  724. {<<"keep-alive">>, <<"timeout=5, max=1000">>}
  725. ).
  726. reject_proxy_authenticate_header(Config) ->
  727. doc("Requests containing a proxy-authenticate header must be rejected "
  728. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
  729. do_reject_malformed_header(Config,
  730. {<<"proxy-authenticate">>, <<"Basic">>}
  731. ).
  732. reject_proxy_authorization_header(Config) ->
  733. doc("Requests containing a proxy-authorization header must be rejected "
  734. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
  735. do_reject_malformed_header(Config,
  736. {<<"proxy-authorization">>, <<"Basic YWxhZGRpbjpvcGVuc2VzYW1l">>}
  737. ).
  738. reject_transfer_encoding_header(Config) ->
  739. doc("Requests containing a transfer-encoding header must be rejected "
  740. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
  741. do_reject_malformed_header(Config,
  742. {<<"transfer-encoding">>, <<"chunked">>}
  743. ).
  744. reject_upgrade_header(Config) ->
  745. doc("Requests containing an upgrade header must be rejected "
  746. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.5, RFC9114 4.1.2)"),
  747. do_reject_malformed_header(Config,
  748. {<<"upgrade">>, <<"websocket">>}
  749. ).
  750. accept_te_header_value_trailers(Config) ->
  751. doc("Requests containing a TE header with a value of \"trailers\" "
  752. "must be accepted. (RFC9114 4.2)"),
  753. #{conn := Conn} = do_connect(Config),
  754. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  755. {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  756. {<<":method">>, <<"GET">>},
  757. {<<":scheme">>, <<"https">>},
  758. {<<":authority">>, <<"localhost">>},
  759. {<<":path">>, <<"/">>},
  760. {<<"content-length">>, <<"0">>},
  761. {<<"te">>, <<"trailers">>}
  762. ], 0, cow_qpack:init()),
  763. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  764. {<<"content-type">>, <<"text/plain">>}
  765. ], 0, EncSt0),
  766. {ok, _} = quicer:send(StreamRef, [
  767. <<1>>, %% HEADERS frame.
  768. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  769. EncodedHeaders,
  770. <<1>>, %% HEADERS frame for trailers.
  771. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  772. EncodedTrailers
  773. ], ?QUIC_SEND_FLAG_FIN),
  774. #{
  775. headers := #{<<":status">> := <<"200">>},
  776. body := <<"Hello world!">>
  777. } = do_receive_response(StreamRef),
  778. ok.
  779. reject_te_header_other_values(Config) ->
  780. doc("Requests containing a TE header with a value other than \"trailers\" must be rejected "
  781. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
  782. do_reject_malformed_header(Config,
  783. {<<"te">>, <<"trailers, deflate;q=0.5">>}
  784. ).
  785. %% @todo response_dont_send_header_in_connection
  786. %% @todo response_dont_send_connection_header
  787. %% @todo response_dont_send_keep_alive_header
  788. %% @todo response_dont_send_proxy_connection_header
  789. %% @todo response_dont_send_transfer_encoding_header
  790. %% @todo response_dont_send_upgrade_header
  791. %% 4.2.1. Field Compression
  792. %% To allow for better compression efficiency, the Cookie header field
  793. %([COOKIES]) MAY be split into separate field lines, each with one or more
  794. %cookie-pairs, before compression. If a decompressed field section contains
  795. %multiple cookie field lines, these MUST be concatenated into a single byte
  796. %string using the two-byte delimiter of "; " (ASCII 0x3b, 0x20) before being
  797. %passed into a context other than HTTP/2 or HTTP/3, such as an HTTP/1.1
  798. %connection, or a generic HTTP server application.
  799. %% 4.2.2. Header Size Constraints
  800. %% An HTTP/3 implementation MAY impose a limit on the maximum size of the
  801. %message header it will accept on an individual HTTP message. A server that
  802. %receives a larger header section than it is willing to handle can send an HTTP
  803. %431 (Request Header Fields Too Large) status code ([RFC6585]). The size of a
  804. %field list is calculated based on the uncompressed size of fields, including
  805. %the length of the name and value in bytes plus an overhead of 32 bytes for
  806. %each field.
  807. %% If an implementation wishes to advise its peer of this limit, it can be
  808. %conveyed as a number of bytes in the SETTINGS_MAX_FIELD_SECTION_SIZE
  809. %parameter.
  810. reject_unknown_pseudo_headers(Config) ->
  811. doc("Requests containing unknown pseudo-headers must be rejected "
  812. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
  813. do_reject_malformed_header(Config,
  814. {<<":upgrade">>, <<"websocket">>}
  815. ).
  816. reject_response_pseudo_headers(Config) ->
  817. doc("Requests containing response pseudo-headers must be rejected "
  818. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
  819. do_reject_malformed_header(Config,
  820. {<<":status">>, <<"200">>}
  821. ).
  822. reject_pseudo_headers_in_trailers(Config) ->
  823. doc("Requests containing pseudo-headers in trailers must be rejected "
  824. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
  825. #{conn := Conn} = do_connect(Config),
  826. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  827. {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
  828. {<<":method">>, <<"GET">>},
  829. {<<":scheme">>, <<"https">>},
  830. {<<":authority">>, <<"localhost">>},
  831. {<<":path">>, <<"/">>},
  832. {<<"trailer">>, <<"x-checksum">>}
  833. ], 0, cow_qpack:init()),
  834. {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
  835. {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>},
  836. {<<":path">>, <<"/">>}
  837. ], 0, EncSt0),
  838. {ok, _} = quicer:send(StreamRef, [
  839. <<1>>, %% HEADERS frame.
  840. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  841. EncodedHeaders,
  842. <<0>>, %% DATA frame.
  843. cow_http3:encode_int(10000),
  844. <<0:10000/unit:8>>,
  845. <<1>>, %% HEADERS frame for trailers.
  846. cow_http3:encode_int(iolist_size(EncodedTrailers)),
  847. EncodedTrailers
  848. ]),
  849. %% The stream should have been aborted.
  850. #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
  851. ok.
  852. reject_pseudo_headers_after_regular_headers(Config) ->
  853. doc("Requests containing pseudo-headers after regular headers must be rejected "
  854. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
  855. do_reject_malformed_headers(Config, [
  856. {<<":method">>, <<"GET">>},
  857. {<<":scheme">>, <<"https">>},
  858. {<<":authority">>, <<"localhost">>},
  859. {<<"content-length">>, <<"0">>},
  860. {<<":path">>, <<"/">>}
  861. ]).
  862. reject_userinfo(Config) ->
  863. doc("An authority containing a userinfo component must be rejected "
  864. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  865. do_reject_malformed_headers(Config, [
  866. {<<":method">>, <<"GET">>},
  867. {<<":scheme">>, <<"http">>},
  868. {<<":authority">>, <<"user@localhost">>},
  869. {<<":path">>, <<"/">>}
  870. ]).
  871. %% To ensure that the HTTP/1.1 request line can be reproduced accurately, this
  872. %% pseudo-header field (:authority) MUST be omitted when translating from an
  873. %% HTTP/1.1 request that has a request target in a method-specific form;
  874. %% see Section 7.1 of [HTTP].
  875. reject_empty_path(Config) ->
  876. doc("A request containing an empty path component must be rejected "
  877. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  878. do_reject_malformed_headers(Config, [
  879. {<<":method">>, <<"GET">>},
  880. {<<":scheme">>, <<"http">>},
  881. {<<":authority">>, <<"localhost">>},
  882. {<<":path">>, <<>>}
  883. ]).
  884. reject_missing_pseudo_header_method(Config) ->
  885. doc("A request without a method component must be rejected "
  886. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  887. do_reject_malformed_headers(Config, [
  888. {<<":scheme">>, <<"http">>},
  889. {<<":authority">>, <<"localhost">>},
  890. {<<":path">>, <<"/">>}
  891. ]).
  892. reject_many_pseudo_header_method(Config) ->
  893. doc("A request containing more than one method component must be rejected "
  894. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  895. do_reject_malformed_headers(Config, [
  896. {<<":method">>, <<"GET">>},
  897. {<<":method">>, <<"GET">>},
  898. {<<":scheme">>, <<"http">>},
  899. {<<":authority">>, <<"localhost">>},
  900. {<<":path">>, <<"/">>}
  901. ]).
  902. reject_missing_pseudo_header_scheme(Config) ->
  903. doc("A request without a scheme component must be rejected "
  904. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  905. do_reject_malformed_headers(Config, [
  906. {<<":method">>, <<"GET">>},
  907. {<<":authority">>, <<"localhost">>},
  908. {<<":path">>, <<"/">>}
  909. ]).
  910. reject_many_pseudo_header_scheme(Config) ->
  911. doc("A request containing more than one scheme component must be rejected "
  912. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  913. do_reject_malformed_headers(Config, [
  914. {<<":method">>, <<"GET">>},
  915. {<<":scheme">>, <<"http">>},
  916. {<<":scheme">>, <<"http">>},
  917. {<<":authority">>, <<"localhost">>},
  918. {<<":path">>, <<"/">>}
  919. ]).
  920. reject_missing_pseudo_header_authority(Config) ->
  921. doc("A request without an authority or host component must be rejected "
  922. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  923. do_reject_malformed_headers(Config, [
  924. {<<":method">>, <<"GET">>},
  925. {<<":scheme">>, <<"http">>},
  926. {<<":path">>, <<"/">>}
  927. ]).
  928. accept_host_header_on_missing_pseudo_header_authority(Config) ->
  929. doc("A request without an authority but with a host header must be accepted. "
  930. "(RFC9114 4.3.1)"),
  931. #{conn := Conn} = do_connect(Config),
  932. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  933. {ok, EncodedHeaders, _EncData1, _EncSt0} = cow_qpack:encode_field_section([
  934. {<<":method">>, <<"GET">>},
  935. {<<":scheme">>, <<"https">>},
  936. {<<":path">>, <<"/">>},
  937. {<<"host">>, <<"localhost">>}
  938. ], 0, cow_qpack:init()),
  939. {ok, _} = quicer:send(StreamRef, [
  940. <<1>>, %% HEADERS frame.
  941. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  942. EncodedHeaders
  943. ], ?QUIC_SEND_FLAG_FIN),
  944. #{
  945. headers := #{<<":status">> := <<"200">>},
  946. body := <<"Hello world!">>
  947. } = do_receive_response(StreamRef),
  948. ok.
  949. %% @todo
  950. %% If the :scheme pseudo-header field identifies a scheme that has a mandatory
  951. %% authority component (including "http" and "https"), the request MUST contain
  952. %% either an :authority pseudo-header field or a Host header field.
  953. %% - If both fields are present, they MUST NOT be empty.
  954. %% - If both fields are present, they MUST contain the same value.
  955. reject_many_pseudo_header_authority(Config) ->
  956. doc("A request containing more than one authority component must be rejected "
  957. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  958. do_reject_malformed_headers(Config, [
  959. {<<":method">>, <<"GET">>},
  960. {<<":scheme">>, <<"http">>},
  961. {<<":authority">>, <<"localhost">>},
  962. {<<":authority">>, <<"localhost">>},
  963. {<<":path">>, <<"/">>}
  964. ]).
  965. reject_missing_pseudo_header_path(Config) ->
  966. doc("A request without a path component must be rejected "
  967. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  968. do_reject_malformed_headers(Config, [
  969. {<<":method">>, <<"GET">>},
  970. {<<":scheme">>, <<"http">>},
  971. {<<":authority">>, <<"localhost">>}
  972. ]).
  973. reject_many_pseudo_header_path(Config) ->
  974. doc("A request containing more than one path component must be rejected "
  975. "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
  976. do_reject_malformed_headers(Config, [
  977. {<<":method">>, <<"GET">>},
  978. {<<":scheme">>, <<"http">>},
  979. {<<":authority">>, <<"localhost">>},
  980. {<<":path">>, <<"/">>},
  981. {<<":path">>, <<"/">>}
  982. ]).
  983. do_reject_malformed_header(Config, Header) ->
  984. do_reject_malformed_headers(Config, [
  985. {<<":method">>, <<"GET">>},
  986. {<<":scheme">>, <<"https">>},
  987. {<<":authority">>, <<"localhost">>},
  988. {<<":path">>, <<"/">>},
  989. Header
  990. ]).
  991. do_reject_malformed_headers(Config, Headers) ->
  992. #{conn := Conn} = do_connect(Config),
  993. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  994. {ok, EncodedHeaders, _EncData1, _EncSt0}
  995. = cow_qpack:encode_field_section(Headers, 0, cow_qpack:init()),
  996. {ok, _} = quicer:send(StreamRef, [
  997. <<1>>, %% HEADERS frame.
  998. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  999. EncodedHeaders
  1000. ]),
  1001. %% The stream should have been aborted.
  1002. #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
  1003. ok.
  1004. %% For responses, a single ":status" pseudo-header field is defined that
  1005. %% carries the HTTP status code; see Section 15 of [HTTP]. This pseudo-header
  1006. %% field MUST be included in all responses; otherwise, the response is malformed
  1007. %% (see Section 4.1.2).
  1008. %% @todo Implement CONNECT. (RFC9114 4.4. The CONNECT Method)
  1009. %% @todo Maybe block the sending of 101 responses? (RFC9114 4.5. HTTP Upgrade) - also HTTP/2.
  1010. %% @todo Implement server push (RFC9114 4.6. Server Push)
  1011. %% @todo - need a way to list connections
  1012. %% 5.2. Connection Shutdown
  1013. %% Endpoints initiate the graceful shutdown of an HTTP/3 connection by sending
  1014. %% a GOAWAY frame. The GOAWAY frame contains an identifier that indicates to the
  1015. %% receiver the range of requests or pushes that were or might be processed in
  1016. %% this connection. The server sends a client-initiated bidirectional stream ID;
  1017. %% the client sends a push ID. Requests or pushes with the indicated identifier
  1018. %% or greater are rejected (Section 4.1.1) by the sender of the GOAWAY. This
  1019. %% identifier MAY be zero if no requests or pushes were processed.
  1020. %% @todo
  1021. %% Upon sending a GOAWAY frame, the endpoint SHOULD explicitly cancel (see
  1022. %% Sections 4.1.1 and 7.2.3) any requests or pushes that have identifiers greater
  1023. %% than or equal to the one indicated, in order to clean up transport state for
  1024. %% the affected streams. The endpoint SHOULD continue to do so as more requests
  1025. %% or pushes arrive.
  1026. %% @todo
  1027. %% Endpoints MUST NOT initiate new requests or promise new pushes on the
  1028. %% connection after receipt of a GOAWAY frame from the peer.
  1029. %% @todo
  1030. %% Requests on stream IDs less than the stream ID in a GOAWAY frame from the
  1031. %% server might have been processed; their status cannot be known until a
  1032. %% response is received, the stream is reset individually, another GOAWAY is
  1033. %% received with a lower stream ID than that of the request in question, or the
  1034. %% connection terminates.
  1035. %% @todo
  1036. %% Servers MAY reject individual requests on streams below the indicated ID if
  1037. %% these requests were not processed.
  1038. %% @todo
  1039. %% If a server receives a GOAWAY frame after having promised pushes with a push
  1040. %% ID greater than or equal to the identifier contained in the GOAWAY frame,
  1041. %% those pushes will not be accepted.
  1042. %% @todo
  1043. %% Servers SHOULD send a GOAWAY frame when the closing of a connection is known
  1044. %% in advance, even if the advance notice is small, so that the remote peer can
  1045. %% know whether or not a request has been partially processed.
  1046. %% @todo
  1047. %% An endpoint MAY send multiple GOAWAY frames indicating different
  1048. %% identifiers, but the identifier in each frame MUST NOT be greater than the
  1049. %% identifier in any previous frame, since clients might already have retried
  1050. %% unprocessed requests on another HTTP connection. Receiving a GOAWAY containing
  1051. %% a larger identifier than previously received MUST be treated as a connection
  1052. %% error of type H3_ID_ERROR.
  1053. %% @todo
  1054. %% An endpoint that is attempting to gracefully shut down a connection can send
  1055. %% a GOAWAY frame with a value set to the maximum possible value (2^62-4 for
  1056. %% servers, 2^62-1 for clients).
  1057. %% @todo
  1058. %% Even when a GOAWAY indicates that a given request or push will not be
  1059. %% processed or accepted upon receipt, the underlying transport resources still
  1060. %% exist. The endpoint that initiated these requests can cancel them to clean up
  1061. %% transport state.
  1062. %% @todo
  1063. %% Once all accepted requests and pushes have been processed, the endpoint can
  1064. %% permit the connection to become idle, or it MAY initiate an immediate closure
  1065. %% of the connection. An endpoint that completes a graceful shutdown SHOULD use
  1066. %% the H3_NO_ERROR error code when closing the connection.
  1067. %% @todo
  1068. %% If a client has consumed all available bidirectional stream IDs with
  1069. %% requests, the server need not send a GOAWAY frame, since the client is unable
  1070. %% to make further requests. @todo OK that one's some weird stuff lol
  1071. %% @todo
  1072. %% 5.3. Immediate Application Closure
  1073. %% Before closing the connection, a GOAWAY frame MAY be sent to allow the
  1074. %% client to retry some requests. Including the GOAWAY frame in the same packet
  1075. %% as the QUIC CONNECTION_CLOSE frame improves the chances of the frame being
  1076. %% received by clients.
  1077. bidi_allow_at_least_a_hundred(Config) ->
  1078. doc("Endpoints must allow the peer to create at least "
  1079. "one hundred bidirectional streams. (RFC9114 6.1"),
  1080. #{conn := Conn} = do_connect(Config),
  1081. receive
  1082. {quic, streams_available, Conn, #{bidi_streams := NumStreams}} ->
  1083. true = NumStreams >= 100,
  1084. ok
  1085. after 5000 ->
  1086. error(timeout)
  1087. end.
  1088. unidi_allow_at_least_three(Config) ->
  1089. doc("Endpoints must allow the peer to create at least "
  1090. "three unidirectional streams. (RFC9114 6.2"),
  1091. #{conn := Conn} = do_connect(Config),
  1092. %% Confirm that the server advertised support for at least 3 unidi streams.
  1093. receive
  1094. {quic, streams_available, Conn, #{unidi_streams := NumStreams}} ->
  1095. true = NumStreams >= 3,
  1096. ok
  1097. after 5000 ->
  1098. error(timeout)
  1099. end,
  1100. %% Confirm that we can create the unidi streams.
  1101. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1102. {ok, ControlRef} = quicer:start_stream(Conn,
  1103. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1104. {ok, _} = quicer:send(ControlRef, [<<0>>, SettingsBin]),
  1105. {ok, EncoderRef} = quicer:start_stream(Conn,
  1106. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1107. {ok, _} = quicer:send(EncoderRef, <<2>>),
  1108. {ok, DecoderRef} = quicer:start_stream(Conn,
  1109. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1110. {ok, _} = quicer:send(DecoderRef, <<3>>),
  1111. %% Streams shouldn't get closed.
  1112. fun Loop() ->
  1113. receive
  1114. %% We don't care about these messages.
  1115. {quic, dgram_state_changed, Conn, _} ->
  1116. Loop();
  1117. {quic, peer_needs_streams, Conn, _} ->
  1118. Loop();
  1119. %% Any other we do care.
  1120. Msg ->
  1121. error(Msg)
  1122. after 1000 ->
  1123. ok
  1124. end
  1125. end().
  1126. unidi_create_critical_first(Config) ->
  1127. doc("Endpoints should create the HTTP control stream as well as "
  1128. "the QPACK encoder and decoder streams first. (RFC9114 6.2"),
  1129. %% The control stream is accepted in the do_connect/1 function.
  1130. #{conn := Conn} = do_connect(Config, #{peer_unidi_stream_count => 3}),
  1131. Unidi1 = do_accept_qpack_stream(Conn),
  1132. Unidi2 = do_accept_qpack_stream(Conn),
  1133. case {Unidi1, Unidi2} of
  1134. {{encoder, _}, {decoder, _}} ->
  1135. ok;
  1136. {{decoder, _}, {encoder, _}} ->
  1137. ok
  1138. end.
  1139. do_accept_qpack_stream(Conn) ->
  1140. receive
  1141. {quic, new_stream, StreamRef, #{flags := Flags}} ->
  1142. ok = quicer:setopt(StreamRef, active, true),
  1143. true = quicer:is_unidirectional(Flags),
  1144. receive {quic, <<Type>>, StreamRef, _} ->
  1145. {case Type of
  1146. 2 -> encoder;
  1147. 3 -> decoder
  1148. end, StreamRef}
  1149. after 5000 ->
  1150. error(timeout)
  1151. end
  1152. after 5000 ->
  1153. error(timeout)
  1154. end.
  1155. %% @todo We should also confirm that there's at least 1,024 bytes of
  1156. %% flow-control credit for each unidi stream the server creates. (How?)
  1157. %% It can be set via stream_recv_window_default in quicer.
  1158. unidi_abort_unknown_type(Config) ->
  1159. doc("Receipt of an unknown stream type must be aborted "
  1160. "with an H3_STREAM_CREATION_ERROR stream error. (RFC9114 6.2, RFC9114 9)"),
  1161. #{conn := Conn} = do_connect(Config),
  1162. %% Create an unknown unidirectional stream.
  1163. {ok, StreamRef} = quicer:start_stream(Conn,
  1164. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1165. {ok, _} = quicer:send(StreamRef, [
  1166. cow_http3:encode_int(1 + do_reserved_type()),
  1167. rand:bytes(rand:uniform(4096))
  1168. ]),
  1169. %% The stream should have been aborted.
  1170. #{reason := h3_stream_creation_error} = do_wait_stream_aborted(StreamRef),
  1171. ok.
  1172. unidi_abort_reserved_type(Config) ->
  1173. doc("Receipt of a reserved stream type must be aborted "
  1174. "with an H3_STREAM_CREATION_ERROR stream error. "
  1175. "(RFC9114 6.2, RFC9114 6.2.3, RFC9114 9)"),
  1176. #{conn := Conn} = do_connect(Config),
  1177. %% Create a reserved unidirectional stream.
  1178. {ok, StreamRef} = quicer:start_stream(Conn,
  1179. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1180. {ok, _} = quicer:send(StreamRef, [
  1181. cow_http3:encode_int(do_reserved_type()),
  1182. rand:bytes(rand:uniform(4096))
  1183. ]),
  1184. %% The stream should have been aborted.
  1185. #{reason := h3_stream_creation_error} = do_wait_stream_aborted(StreamRef),
  1186. ok.
  1187. %% As certain stream types can affect connection state, a recipient SHOULD NOT
  1188. %% discard data from incoming unidirectional streams prior to reading the stream type.
  1189. %% Implementations MAY send stream types before knowing whether the peer
  1190. %supports them. However, stream types that could modify the state or semantics
  1191. %of existing protocol components, including QPACK or other extensions, MUST NOT
  1192. %be sent until the peer is known to support them.
  1193. %% @todo It may make sense for Cowboy to delay the creation of unidi streams
  1194. %% a little in order to save resources. We could create them when the
  1195. %% client does as well, or something similar.
  1196. %% A receiver MUST tolerate unidirectional streams being closed or reset prior
  1197. %% to the reception of the unidirectional stream header.
  1198. %% Each side MUST initiate a single control stream at the beginning of the
  1199. %% connection and send its SETTINGS frame as the first frame on this stream.
  1200. %% @todo What to do when the client never opens a control stream?
  1201. %% @todo Similarly, a stream could be opened but with no data being sent.
  1202. %% @todo Similarly, a control stream could be opened with no SETTINGS frame sent.
  1203. control_reject_first_frame_data(Config) ->
  1204. doc("The first frame on a control stream must be a SETTINGS frame "
  1205. "or the connection must be closed with an H3_MISSING_SETTINGS "
  1206. "connection error. (RFC9114 6.2.1, RFC9114 9)"),
  1207. #{conn := Conn} = do_connect(Config),
  1208. {ok, ControlRef} = quicer:start_stream(Conn,
  1209. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1210. {ok, _} = quicer:send(ControlRef, [
  1211. <<0>>, %% CONTROL stream.
  1212. <<0>>, %% DATA frame.
  1213. cow_http3:encode_int(12),
  1214. <<"Hello world!">>
  1215. ]),
  1216. %% The connection should have been closed.
  1217. #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
  1218. ok.
  1219. control_reject_first_frame_headers(Config) ->
  1220. doc("The first frame on a control stream must be a SETTINGS frame "
  1221. "or the connection must be closed with an H3_MISSING_SETTINGS "
  1222. "connection error. (RFC9114 6.2.1, RFC9114 9)"),
  1223. #{conn := Conn} = do_connect(Config),
  1224. {ok, ControlRef} = quicer:start_stream(Conn,
  1225. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1226. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1227. {<<":method">>, <<"GET">>},
  1228. {<<":scheme">>, <<"https">>},
  1229. {<<":authority">>, <<"localhost">>},
  1230. {<<":path">>, <<"/">>},
  1231. {<<"content-length">>, <<"0">>}
  1232. ], 0, cow_qpack:init()),
  1233. {ok, _} = quicer:send(ControlRef, [
  1234. <<0>>, %% CONTROL stream.
  1235. <<1>>, %% HEADERS frame.
  1236. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  1237. EncodedHeaders
  1238. ]),
  1239. %% The connection should have been closed.
  1240. #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
  1241. ok.
  1242. control_reject_first_frame_cancel_push(Config) ->
  1243. doc("The first frame on a control stream must be a SETTINGS frame "
  1244. "or the connection must be closed with an H3_MISSING_SETTINGS "
  1245. "connection error. (RFC9114 6.2.1, RFC9114 9)"),
  1246. #{conn := Conn} = do_connect(Config),
  1247. {ok, ControlRef} = quicer:start_stream(Conn,
  1248. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1249. {ok, _} = quicer:send(ControlRef, [
  1250. <<0>>, %% CONTROL stream.
  1251. <<3>>, %% CANCEL_PUSH frame.
  1252. cow_http3:encode_int(1),
  1253. cow_http3:encode_int(0)
  1254. ]),
  1255. %% The connection should have been closed.
  1256. #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
  1257. ok.
  1258. control_accept_first_frame_settings(Config) ->
  1259. doc("The first frame on a control stream "
  1260. "must be a SETTINGS frame. (RFC9114 6.2.1, RFC9114 9)"),
  1261. #{conn := Conn} = do_connect(Config),
  1262. {ok, ControlRef} = quicer:start_stream(Conn,
  1263. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1264. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1265. {ok, _} = quicer:send(ControlRef, [
  1266. <<0>>, %% CONTROL stream.
  1267. SettingsBin
  1268. ]),
  1269. %% The connection should remain up.
  1270. receive
  1271. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  1272. Reason = cow_http3:code_to_error(Code),
  1273. error(Reason)
  1274. after 1000 ->
  1275. ok
  1276. end.
  1277. control_reject_first_frame_push_promise(Config) ->
  1278. doc("The first frame on a control stream must be a SETTINGS frame "
  1279. "or the connection must be closed with an H3_MISSING_SETTINGS "
  1280. "connection error. (RFC9114 6.2.1, RFC9114 9)"),
  1281. #{conn := Conn} = do_connect(Config),
  1282. {ok, ControlRef} = quicer:start_stream(Conn,
  1283. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1284. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1285. {<<":method">>, <<"GET">>},
  1286. {<<":scheme">>, <<"https">>},
  1287. {<<":authority">>, <<"localhost">>},
  1288. {<<":path">>, <<"/">>},
  1289. {<<"content-length">>, <<"0">>}
  1290. ], 0, cow_qpack:init()),
  1291. {ok, _} = quicer:send(ControlRef, [
  1292. <<0>>, %% CONTROL stream.
  1293. <<5>>, %% PUSH_PROMISE frame.
  1294. cow_http3:encode_int(iolist_size(EncodedHeaders) + 1),
  1295. cow_http3:encode_int(0),
  1296. EncodedHeaders
  1297. ]),
  1298. %% The connection should have been closed.
  1299. #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
  1300. ok.
  1301. control_reject_first_frame_goaway(Config) ->
  1302. doc("The first frame on a control stream must be a SETTINGS frame "
  1303. "or the connection must be closed with an H3_MISSING_SETTINGS "
  1304. "connection error. (RFC9114 6.2.1, RFC9114 9)"),
  1305. #{conn := Conn} = do_connect(Config),
  1306. {ok, ControlRef} = quicer:start_stream(Conn,
  1307. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1308. {ok, _} = quicer:send(ControlRef, [
  1309. <<0>>, %% CONTROL stream.
  1310. <<7>>, %% GOAWAY frame.
  1311. cow_http3:encode_int(1),
  1312. cow_http3:encode_int(0)
  1313. ]),
  1314. %% The connection should have been closed.
  1315. #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
  1316. ok.
  1317. control_reject_first_frame_max_push_id(Config) ->
  1318. doc("The first frame on a control stream must be a SETTINGS frame "
  1319. "or the connection must be closed with an H3_MISSING_SETTINGS "
  1320. "connection error. (RFC9114 6.2.1, RFC9114 9)"),
  1321. #{conn := Conn} = do_connect(Config),
  1322. {ok, ControlRef} = quicer:start_stream(Conn,
  1323. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1324. {ok, _} = quicer:send(ControlRef, [
  1325. <<0>>, %% CONTROL stream.
  1326. <<13>>, %% MAX_PUSH_ID frame.
  1327. cow_http3:encode_int(1),
  1328. cow_http3:encode_int(0)
  1329. ]),
  1330. %% The connection should have been closed.
  1331. #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
  1332. ok.
  1333. control_reject_first_frame_reserved(Config) ->
  1334. doc("The first frame on a control stream must be a SETTINGS frame "
  1335. "or the connection must be closed with an H3_MISSING_SETTINGS "
  1336. "connection error. (RFC9114 6.2.1, RFC9114 9)"),
  1337. #{conn := Conn} = do_connect(Config),
  1338. {ok, ControlRef} = quicer:start_stream(Conn,
  1339. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1340. Len = rand:uniform(512),
  1341. {ok, _} = quicer:send(ControlRef, [
  1342. <<0>>, %% CONTROL stream.
  1343. cow_http3:encode_int(do_reserved_type()),
  1344. cow_http3:encode_int(Len),
  1345. rand:bytes(Len)
  1346. ]),
  1347. %% The connection should have been closed.
  1348. #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
  1349. ok.
  1350. control_reject_multiple(Config) ->
  1351. doc("Endpoints must not create multiple control streams. (RFC9114 6.2.1)"),
  1352. #{conn := Conn} = do_connect(Config),
  1353. %% Create two control streams.
  1354. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1355. {ok, ControlRef1} = quicer:start_stream(Conn,
  1356. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1357. {ok, _} = quicer:send(ControlRef1, [<<0>>, SettingsBin]),
  1358. {ok, ControlRef2} = quicer:start_stream(Conn,
  1359. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1360. {ok, _} = quicer:send(ControlRef2, [<<0>>, SettingsBin]),
  1361. %% The connection should have been closed.
  1362. #{reason := h3_stream_creation_error} = do_wait_connection_closed(Conn),
  1363. ok.
  1364. control_local_closed_abort(Config) ->
  1365. doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
  1366. #{conn := Conn} = do_connect(Config),
  1367. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1368. {ok, ControlRef} = quicer:start_stream(Conn,
  1369. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1370. {ok, _} = quicer:send(ControlRef, [<<0>>, SettingsBin]),
  1371. %% Wait a little to make sure the stream data was received before we abort.
  1372. timer:sleep(100),
  1373. %% Close the control stream.
  1374. quicer:async_shutdown_stream(ControlRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
  1375. %% The connection should have been closed.
  1376. timer:sleep(1000),
  1377. ct:pal("~p", [process_info(self(), messages)]),
  1378. #{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
  1379. ok.
  1380. control_local_closed_graceful(Config) ->
  1381. doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
  1382. #{conn := Conn} = do_connect(Config),
  1383. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1384. {ok, ControlRef} = quicer:start_stream(Conn,
  1385. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1386. {ok, _} = quicer:send(ControlRef, [<<0>>, SettingsBin]),
  1387. %% Close the control stream.
  1388. quicer:async_shutdown_stream(ControlRef, ?QUIC_STREAM_SHUTDOWN_FLAG_GRACEFUL, 0),
  1389. %% The connection should have been closed.
  1390. #{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
  1391. ok.
  1392. control_remote_closed_abort(Config) ->
  1393. doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
  1394. #{conn := Conn, control := ControlRef} = do_connect(Config),
  1395. %% Close the control stream.
  1396. quicer:async_shutdown_stream(ControlRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
  1397. %% The connection should have been closed.
  1398. #{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
  1399. ok.
  1400. %% We cannot gracefully shutdown a remote unidi stream; only abort reading.
  1401. %% Because the contents of the control stream are used to manage the behavior
  1402. %% of other streams, endpoints SHOULD provide enough flow-control credit to keep
  1403. %% the peer's control stream from becoming blocked.
  1404. %% @todo Implement server push (RFC9114 6.2.2 Push Streams)
  1405. data_frame_can_span_multiple_packets(Config) ->
  1406. doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
  1407. #{conn := Conn} = do_connect(Config),
  1408. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1409. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1410. {<<":method">>, <<"GET">>},
  1411. {<<":scheme">>, <<"https">>},
  1412. {<<":authority">>, <<"localhost">>},
  1413. {<<":path">>, <<"/echo/read_body">>},
  1414. {<<"content-length">>, <<"13">>}
  1415. ], 0, cow_qpack:init()),
  1416. {ok, _} = quicer:send(StreamRef, [
  1417. <<1>>, %% HEADERS frame.
  1418. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  1419. EncodedHeaders,
  1420. <<0>>, %% DATA frame.
  1421. cow_http3:encode_int(13),
  1422. <<"Hello ">>
  1423. ]),
  1424. timer:sleep(100),
  1425. {ok, _} = quicer:send(StreamRef, [
  1426. <<"server!">>
  1427. ], ?QUIC_SEND_FLAG_FIN),
  1428. #{
  1429. headers := #{<<":status">> := <<"200">>},
  1430. body := <<"Hello server!">>
  1431. } = do_receive_response(StreamRef),
  1432. ok.
  1433. headers_frame_can_span_multiple_packets(Config) ->
  1434. doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
  1435. #{conn := Conn} = do_connect(Config),
  1436. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1437. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1438. {<<":method">>, <<"GET">>},
  1439. {<<":scheme">>, <<"https">>},
  1440. {<<":authority">>, <<"localhost">>},
  1441. {<<":path">>, <<"/">>},
  1442. {<<"content-length">>, <<"0">>}
  1443. ], 0, cow_qpack:init()),
  1444. Half = iolist_size(EncodedHeaders) div 2,
  1445. <<EncodedHeadersPart1:Half/binary, EncodedHeadersPart2/bits>>
  1446. = iolist_to_binary(EncodedHeaders),
  1447. {ok, _} = quicer:send(StreamRef, [
  1448. <<1>>, %% HEADERS frame.
  1449. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  1450. EncodedHeadersPart1
  1451. ]),
  1452. timer:sleep(100),
  1453. {ok, _} = quicer:send(StreamRef, [
  1454. EncodedHeadersPart2
  1455. ]),
  1456. #{
  1457. headers := #{<<":status">> := <<"200">>},
  1458. body := <<"Hello world!">>
  1459. } = do_receive_response(StreamRef),
  1460. ok.
  1461. %% @todo Implement server push. cancel_push_frame_can_span_multiple_packets(Config) ->
  1462. settings_frame_can_span_multiple_packets(Config) ->
  1463. doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
  1464. #{conn := Conn} = do_connect(Config),
  1465. {ok, ControlRef} = quicer:start_stream(Conn,
  1466. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1467. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1468. <<SettingsPart1:1/binary, SettingsPart2/bits>> = SettingsBin,
  1469. {ok, _} = quicer:send(ControlRef, [
  1470. <<0>>, %% CONTROL stream.
  1471. SettingsPart1
  1472. ]),
  1473. timer:sleep(100),
  1474. {ok, _} = quicer:send(ControlRef, [
  1475. SettingsPart2
  1476. ]),
  1477. %% The connection should remain up.
  1478. receive
  1479. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  1480. Reason = cow_http3:code_to_error(Code),
  1481. error(Reason)
  1482. after 1000 ->
  1483. ok
  1484. end.
  1485. goaway_frame_can_span_multiple_packets(Config) ->
  1486. doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
  1487. #{conn := Conn} = do_connect(Config),
  1488. {ok, ControlRef} = quicer:start_stream(Conn,
  1489. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1490. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1491. {ok, _} = quicer:send(ControlRef, [
  1492. <<0>>, %% CONTROL stream.
  1493. SettingsBin,
  1494. <<7>>, cow_http3:encode_int(1) %% GOAWAY part 1.
  1495. ]),
  1496. timer:sleep(100),
  1497. {ok, _} = quicer:send(ControlRef, [
  1498. cow_http3:encode_int(0) %% GOAWAY part 2.
  1499. ]),
  1500. %% The connection should be closed gracefully.
  1501. receive
  1502. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  1503. h3_no_error = cow_http3:code_to_error(Code),
  1504. ok;
  1505. %% @todo Temporarily also accept this message. I am
  1506. %% not sure why it happens but it isn't wrong per se.
  1507. {quic, shutdown, Conn, success} ->
  1508. ok
  1509. after 1000 ->
  1510. error(timeout)
  1511. end.
  1512. max_push_id_frame_can_span_multiple_packets(Config) ->
  1513. doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
  1514. #{conn := Conn} = do_connect(Config),
  1515. {ok, ControlRef} = quicer:start_stream(Conn,
  1516. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1517. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1518. {ok, _} = quicer:send(ControlRef, [
  1519. <<0>>, %% CONTROL stream.
  1520. SettingsBin,
  1521. <<13>>, cow_http3:encode_int(1) %% MAX_PUSH_ID part 1.
  1522. ]),
  1523. timer:sleep(100),
  1524. {ok, _} = quicer:send(ControlRef, [
  1525. cow_http3:encode_int(0) %% MAX_PUSH_ID part 2.
  1526. ]),
  1527. %% The connection should remain up.
  1528. receive
  1529. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  1530. Reason = cow_http3:code_to_error(Code),
  1531. error(Reason)
  1532. after 1000 ->
  1533. ok
  1534. end.
  1535. %% The DATA and SETTINGS frames can be zero-length therefore
  1536. %% they cannot be too short.
  1537. headers_frame_too_short(Config) ->
  1538. doc("Frames that terminate before the end of identified fields "
  1539. "must be rejected with an H3_FRAME_ERROR connection error. "
  1540. "(RFC9114 7.1, RFC9114 10.8)"),
  1541. #{conn := Conn} = do_connect(Config),
  1542. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1543. {ok, _} = quicer:send(StreamRef, [
  1544. <<1>>, %% HEADERS frame.
  1545. cow_http3:encode_int(0)
  1546. ]),
  1547. %% The connection should have been closed.
  1548. #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
  1549. ok.
  1550. %% @todo Implement server push. cancel_push_frame_too_short(Config) ->
  1551. goaway_frame_too_short(Config) ->
  1552. doc("Frames that terminate before the end of identified fields "
  1553. "must be rejected with an H3_FRAME_ERROR connection error. "
  1554. "(RFC9114 7.1, RFC9114 10.8)"),
  1555. #{conn := Conn} = do_connect(Config),
  1556. {ok, ControlRef} = quicer:start_stream(Conn,
  1557. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1558. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1559. {ok, _} = quicer:send(ControlRef, [
  1560. <<0>>, %% CONTROL stream.
  1561. SettingsBin,
  1562. <<7>>, cow_http3:encode_int(0) %% GOAWAY.
  1563. ]),
  1564. %% The connection should have been closed.
  1565. #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
  1566. ok.
  1567. max_push_id_frame_too_short(Config) ->
  1568. doc("Frames that terminate before the end of identified fields "
  1569. "must be rejected with an H3_FRAME_ERROR connection error. "
  1570. "(RFC9114 7.1, RFC9114 10.8)"),
  1571. #{conn := Conn} = do_connect(Config),
  1572. {ok, ControlRef} = quicer:start_stream(Conn,
  1573. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1574. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1575. {ok, _} = quicer:send(ControlRef, [
  1576. <<0>>, %% CONTROL stream.
  1577. SettingsBin,
  1578. <<13>>, cow_http3:encode_int(0) %% MAX_PUSH_ID.
  1579. ]),
  1580. %% The connection should have been closed.
  1581. #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
  1582. ok.
  1583. data_frame_truncated(Config) ->
  1584. doc("Truncated frames must be rejected with an "
  1585. "H3_FRAME_ERROR connection error. (RFC9114 7.1, RFC9114 10.8)"),
  1586. #{conn := Conn} = do_connect(Config),
  1587. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1588. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1589. {<<":method">>, <<"GET">>},
  1590. {<<":scheme">>, <<"https">>},
  1591. {<<":authority">>, <<"localhost">>},
  1592. {<<":path">>, <<"/echo/read_body">>},
  1593. {<<"content-length">>, <<"13">>}
  1594. ], 0, cow_qpack:init()),
  1595. {ok, _} = quicer:send(StreamRef, [
  1596. <<1>>, %% HEADERS frame.
  1597. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  1598. EncodedHeaders,
  1599. <<0>>, %% DATA frame.
  1600. cow_http3:encode_int(13),
  1601. <<"Hello ">>
  1602. ], ?QUIC_SEND_FLAG_FIN),
  1603. %% The connection should have been closed.
  1604. #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
  1605. ok.
  1606. headers_frame_truncated(Config) ->
  1607. doc("Truncated frames must be rejected with an "
  1608. "H3_FRAME_ERROR connection error. (RFC9114 7.1, RFC9114 10.8)"),
  1609. #{conn := Conn} = do_connect(Config),
  1610. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1611. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1612. {<<":method">>, <<"GET">>},
  1613. {<<":scheme">>, <<"https">>},
  1614. {<<":authority">>, <<"localhost">>},
  1615. {<<":path">>, <<"/">>},
  1616. {<<"content-length">>, <<"0">>}
  1617. ], 0, cow_qpack:init()),
  1618. {ok, _} = quicer:send(StreamRef, [
  1619. <<1>>, %% HEADERS frame.
  1620. cow_http3:encode_int(iolist_size(EncodedHeaders))
  1621. ], ?QUIC_SEND_FLAG_FIN),
  1622. %% The connection should have been closed.
  1623. #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
  1624. ok.
  1625. %% I am not sure how to test truncated CANCEL_PUSH, SETTINGS, GOAWAY
  1626. %% or MAX_PUSH_ID frames, as those are sent on the control stream,
  1627. %% which we cannot terminate.
  1628. %% The DATA, HEADERS and SETTINGS frames can be of any length
  1629. %% therefore they cannot be too long per se, even if unwanted
  1630. %% data can be included at the end of the frame's payload.
  1631. %% @todo Implement server push. cancel_push_frame_too_long(Config) ->
  1632. goaway_frame_too_long(Config) ->
  1633. doc("Frames that contain additional bytes after the end of identified fields "
  1634. "must be rejected with an H3_FRAME_ERROR connection error. "
  1635. "(RFC9114 7.1, RFC9114 10.8)"),
  1636. #{conn := Conn} = do_connect(Config),
  1637. {ok, ControlRef} = quicer:start_stream(Conn,
  1638. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1639. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1640. {ok, _} = quicer:send(ControlRef, [
  1641. <<0>>, %% CONTROL stream.
  1642. SettingsBin,
  1643. <<7>>, cow_http3:encode_int(3), %% GOAWAY.
  1644. <<0, 1, 2>>
  1645. ]),
  1646. %% The connection should have been closed.
  1647. #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
  1648. ok.
  1649. max_push_id_frame_too_long(Config) ->
  1650. doc("Frames that contain additional bytes after the end of identified fields "
  1651. "must be rejected with an H3_FRAME_ERROR connection error. "
  1652. "(RFC9114 7.1, RFC9114 10.8)"),
  1653. #{conn := Conn} = do_connect(Config),
  1654. {ok, ControlRef} = quicer:start_stream(Conn,
  1655. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1656. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1657. {ok, _} = quicer:send(ControlRef, [
  1658. <<0>>, %% CONTROL stream.
  1659. SettingsBin,
  1660. <<13>>, cow_http3:encode_int(9), %% MAX_PUSH_ID.
  1661. <<0, 1, 2, 3, 4, 5, 6, 7, 8>>
  1662. ]),
  1663. %% The connection should have been closed.
  1664. #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
  1665. ok.
  1666. %% Streams may terminate abruptly in the middle of frames.
  1667. data_frame_rejected_on_control_stream(Config) ->
  1668. doc("DATA frames received on the control stream must be rejected "
  1669. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.1)"),
  1670. #{conn := Conn} = do_connect(Config),
  1671. {ok, ControlRef} = quicer:start_stream(Conn,
  1672. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1673. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1674. {ok, _} = quicer:send(ControlRef, [
  1675. <<0>>, %% CONTROL stream.
  1676. SettingsBin,
  1677. <<0>>, %% DATA frame.
  1678. cow_http3:encode_int(12),
  1679. <<"Hello world!">>
  1680. ]),
  1681. %% The connection should have been closed.
  1682. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1683. ok.
  1684. headers_frame_rejected_on_control_stream(Config) ->
  1685. doc("HEADERS frames received on the control stream must be rejected "
  1686. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.2)"),
  1687. #{conn := Conn} = do_connect(Config),
  1688. {ok, ControlRef} = quicer:start_stream(Conn,
  1689. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1690. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1691. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1692. {<<":method">>, <<"GET">>},
  1693. {<<":scheme">>, <<"https">>},
  1694. {<<":authority">>, <<"localhost">>},
  1695. {<<":path">>, <<"/">>},
  1696. {<<"content-length">>, <<"0">>}
  1697. ], 0, cow_qpack:init()),
  1698. {ok, _} = quicer:send(ControlRef, [
  1699. <<0>>, %% CONTROL stream.
  1700. SettingsBin,
  1701. <<1>>, %% HEADERS frame.
  1702. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  1703. EncodedHeaders
  1704. ]),
  1705. %% The connection should have been closed.
  1706. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1707. ok.
  1708. %% @todo Implement server push. (RFC9114 7.2.3. CANCEL_PUSH)
  1709. settings_twice(Config) ->
  1710. doc("Receipt of a second SETTINGS frame on the control stream "
  1711. "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.4)"),
  1712. #{conn := Conn} = do_connect(Config),
  1713. {ok, ControlRef} = quicer:start_stream(Conn,
  1714. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1715. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1716. {ok, _} = quicer:send(ControlRef, [
  1717. <<0>>, %% CONTROL stream.
  1718. SettingsBin,
  1719. SettingsBin
  1720. ]),
  1721. %% The connection should have been closed.
  1722. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1723. ok.
  1724. settings_on_bidi_stream(Config) ->
  1725. doc("Receipt of a SETTINGS frame on a bidirectional stream "
  1726. "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.4)"),
  1727. #{conn := Conn} = do_connect(Config),
  1728. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1729. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1730. {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1731. {<<":method">>, <<"GET">>},
  1732. {<<":scheme">>, <<"https">>},
  1733. {<<":authority">>, <<"localhost">>},
  1734. {<<":path">>, <<"/">>},
  1735. {<<"content-length">>, <<"0">>}
  1736. ], 0, cow_qpack:init()),
  1737. {ok, _} = quicer:send(StreamRef, [
  1738. SettingsBin,
  1739. <<1>>, %% HEADERS frame.
  1740. cow_http3:encode_int(iolist_size(EncodedRequest)),
  1741. EncodedRequest
  1742. ], ?QUIC_SEND_FLAG_FIN),
  1743. %% The connection should have been closed.
  1744. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1745. ok.
  1746. settings_identifier_twice(Config) ->
  1747. doc("Receipt of a duplicate SETTINGS identifier must be rejected "
  1748. "with an H3_SETTINGS_ERROR connection error. (RFC9114 7.2.4)"),
  1749. #{conn := Conn} = do_connect(Config),
  1750. {ok, ControlRef} = quicer:start_stream(Conn,
  1751. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1752. SettingsPayload = [
  1753. cow_http3:encode_int(6), cow_http3:encode_int(4096),
  1754. cow_http3:encode_int(6), cow_http3:encode_int(8192)
  1755. ],
  1756. {ok, _} = quicer:send(ControlRef, [
  1757. <<0>>, %% CONTROL stream.
  1758. <<4>>, %% SETTINGS frame.
  1759. cow_http3:encode_int(iolist_size(SettingsPayload)),
  1760. SettingsPayload
  1761. ]),
  1762. %% The connection should have been closed.
  1763. #{reason := h3_settings_error} = do_wait_connection_closed(Conn),
  1764. ok.
  1765. settings_ignore_unknown_identifier(Config) ->
  1766. doc("Unknown SETTINGS identifiers must be ignored (RFC9114 7.2.4, RFC9114 9)"),
  1767. #{conn := Conn} = do_connect(Config),
  1768. {ok, ControlRef} = quicer:start_stream(Conn,
  1769. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1770. SettingsPayload = [
  1771. cow_http3:encode_int(999), cow_http3:encode_int(4096)
  1772. ],
  1773. {ok, _} = quicer:send(ControlRef, [
  1774. <<0>>, %% CONTROL stream.
  1775. <<4>>, %% SETTINGS frame.
  1776. cow_http3:encode_int(iolist_size(SettingsPayload)),
  1777. SettingsPayload
  1778. ]),
  1779. %% The connection should remain up.
  1780. receive
  1781. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  1782. Reason = cow_http3:code_to_error(Code),
  1783. error(Reason)
  1784. after 1000 ->
  1785. ok
  1786. end.
  1787. settings_ignore_reserved_identifier(Config) ->
  1788. doc("Reserved SETTINGS identifiers must be ignored (RFC9114 7.2.4.1)"),
  1789. #{conn := Conn} = do_connect(Config),
  1790. {ok, ControlRef} = quicer:start_stream(Conn,
  1791. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1792. SettingsPayload = [
  1793. cow_http3:encode_int(do_reserved_type()), cow_http3:encode_int(4096)
  1794. ],
  1795. {ok, _} = quicer:send(ControlRef, [
  1796. <<0>>, %% CONTROL stream.
  1797. <<4>>, %% SETTINGS frame.
  1798. cow_http3:encode_int(iolist_size(SettingsPayload)),
  1799. SettingsPayload
  1800. ]),
  1801. %% The connection should remain up.
  1802. receive
  1803. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  1804. Reason = cow_http3:code_to_error(Code),
  1805. error(Reason)
  1806. after 1000 ->
  1807. ok
  1808. end.
  1809. %% @todo Check that we send a reserved SETTINGS identifier when sending a
  1810. %% non-empty SETTINGS frame. (7.2.4.1. Defined SETTINGS Parameters)
  1811. %% @todo Check that setting SETTINGS_MAX_FIELD_SECTION_SIZE works.
  1812. %% It is unclear whether the SETTINGS identifier 0x00 must be rejected or ignored.
  1813. settings_reject_http2_0x02(Config) ->
  1814. do_settings_reject_http2(Config, 2, 1).
  1815. settings_reject_http2_0x03(Config) ->
  1816. do_settings_reject_http2(Config, 3, 100).
  1817. settings_reject_http2_0x04(Config) ->
  1818. do_settings_reject_http2(Config, 4, 128000).
  1819. settings_reject_http2_0x05(Config) ->
  1820. do_settings_reject_http2(Config, 5, 1000000).
  1821. do_settings_reject_http2(Config, Identifier, Value) ->
  1822. doc("Receipt of an unused HTTP/2 SETTINGS identifier must be rejected "
  1823. "with an H3_SETTINGS_ERROR connection error. (RFC9114 7.2.4, RFC9114 11.2.2)"),
  1824. #{conn := Conn} = do_connect(Config),
  1825. {ok, ControlRef} = quicer:start_stream(Conn,
  1826. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1827. SettingsPayload = [
  1828. cow_http3:encode_int(Identifier), cow_http3:encode_int(Value)
  1829. ],
  1830. {ok, _} = quicer:send(ControlRef, [
  1831. <<0>>, %% CONTROL stream.
  1832. <<4>>, %% SETTINGS frame.
  1833. cow_http3:encode_int(iolist_size(SettingsPayload)),
  1834. SettingsPayload
  1835. ]),
  1836. %% The connection should have been closed.
  1837. #{reason := h3_settings_error} = do_wait_connection_closed(Conn),
  1838. ok.
  1839. %% 7.2.4.2. Initialization
  1840. %% An HTTP implementation MUST NOT send frames or requests that would be
  1841. %% invalid based on its current understanding of the peer's settings.
  1842. %% @todo In the case of SETTINGS_MAX_FIELD_SECTION_SIZE I don't think we have a choice.
  1843. %% All settings begin at an initial value. Each endpoint SHOULD use these
  1844. %% initial values to send messages before the peer's SETTINGS frame has arrived,
  1845. %% as packets carrying the settings can be lost or delayed. When the SETTINGS
  1846. %% frame arrives, any settings are changed to their new values.
  1847. %% Endpoints MUST NOT require any data to be received from the peer prior to
  1848. %% sending the SETTINGS frame; settings MUST be sent as soon as the transport is
  1849. %% ready to send data.
  1850. %% @todo Implement 0-RTT. (7.2.4.2. Initialization)
  1851. %% @todo Implement server push. (7.2.5. PUSH_PROMISE)
  1852. goaway_on_bidi_stream(Config) ->
  1853. doc("Receipt of a GOAWAY frame on a bidirectional stream "
  1854. "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.6)"),
  1855. #{conn := Conn} = do_connect(Config),
  1856. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1857. {ok, _} = quicer:send(StreamRef, [
  1858. <<7>>, cow_http3:encode_int(1), cow_http3:encode_int(0) %% GOAWAY.
  1859. ], ?QUIC_SEND_FLAG_FIN),
  1860. %% The connection should have been closed.
  1861. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1862. ok.
  1863. %% @todo Implement server push. (7.2.6 GOAWAY - will have to reject too large push IDs)
  1864. max_push_id_on_bidi_stream(Config) ->
  1865. doc("Receipt of a MAX_PUSH_ID frame on a bidirectional stream "
  1866. "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.7)"),
  1867. #{conn := Conn} = do_connect(Config),
  1868. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1869. {ok, _} = quicer:send(StreamRef, [
  1870. <<13>>, cow_http3:encode_int(1), cow_http3:encode_int(0) %% MAX_PUSH_ID.
  1871. ], ?QUIC_SEND_FLAG_FIN),
  1872. %% The connection should have been closed.
  1873. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1874. ok.
  1875. %% @todo Implement server push. (7.2.7 MAX_PUSH_ID)
  1876. max_push_id_reject_lower(Config) ->
  1877. doc("Receipt of a MAX_PUSH_ID value lower than previously received "
  1878. "must be rejected with an H3_ID_ERROR connection error. (RFC9114 7.2.7)"),
  1879. #{conn := Conn} = do_connect(Config),
  1880. {ok, ControlRef} = quicer:start_stream(Conn,
  1881. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1882. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1883. {ok, _} = quicer:send(ControlRef, [
  1884. <<0>>, %% CONTROL stream.
  1885. SettingsBin,
  1886. <<13>>, cow_http3:encode_int(1), cow_http3:encode_int(20), %% MAX_PUSH_ID.
  1887. <<13>>, cow_http3:encode_int(1), cow_http3:encode_int(10) %% MAX_PUSH_ID.
  1888. ]),
  1889. %% The connection should have been closed.
  1890. #{reason := h3_id_error} = do_wait_connection_closed(Conn),
  1891. ok.
  1892. reserved_on_control_stream(Config) ->
  1893. doc("Receipt of a reserved frame type on a control stream "
  1894. "must be ignored. (RFC9114 7.2.8)"),
  1895. #{conn := Conn} = do_connect(Config),
  1896. {ok, ControlRef} = quicer:start_stream(Conn,
  1897. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1898. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1899. Len = rand:uniform(512),
  1900. {ok, _} = quicer:send(ControlRef, [
  1901. <<0>>, %% CONTROL stream.
  1902. SettingsBin,
  1903. cow_http3:encode_int(do_reserved_type()),
  1904. cow_http3:encode_int(Len),
  1905. rand:bytes(Len)
  1906. ]),
  1907. %% The connection should remain up.
  1908. receive
  1909. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  1910. Reason = cow_http3:code_to_error(Code),
  1911. error(Reason)
  1912. after 1000 ->
  1913. ok
  1914. end.
  1915. reserved_reject_http2_0x02_control(Config) ->
  1916. do_reserved_reject_http2_control(Config, 2).
  1917. reserved_reject_http2_0x06_control(Config) ->
  1918. do_reserved_reject_http2_control(Config, 6).
  1919. reserved_reject_http2_0x08_control(Config) ->
  1920. do_reserved_reject_http2_control(Config, 8).
  1921. reserved_reject_http2_0x09_control(Config) ->
  1922. do_reserved_reject_http2_control(Config, 9).
  1923. do_reserved_reject_http2_control(Config, Type) ->
  1924. doc("Receipt of an unused HTTP/2 frame type must be rejected "
  1925. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.8, RFC9114 11.2.1)"),
  1926. #{conn := Conn} = do_connect(Config),
  1927. {ok, ControlRef} = quicer:start_stream(Conn,
  1928. #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
  1929. {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
  1930. Len = rand:uniform(512),
  1931. {ok, _} = quicer:send(ControlRef, [
  1932. <<0>>, %% CONTROL stream.
  1933. SettingsBin,
  1934. cow_http3:encode_int(Type),
  1935. cow_http3:encode_int(Len),
  1936. rand:bytes(Len)
  1937. ]),
  1938. %% The connection should have been closed.
  1939. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1940. ok.
  1941. reserved_reject_http2_0x02_bidi(Config) ->
  1942. do_reserved_reject_http2_bidi(Config, 2).
  1943. reserved_reject_http2_0x06_bidi(Config) ->
  1944. do_reserved_reject_http2_bidi(Config, 6).
  1945. reserved_reject_http2_0x08_bidi(Config) ->
  1946. do_reserved_reject_http2_bidi(Config, 8).
  1947. reserved_reject_http2_0x09_bidi(Config) ->
  1948. do_reserved_reject_http2_bidi(Config, 9).
  1949. do_reserved_reject_http2_bidi(Config, Type) ->
  1950. doc("Receipt of an unused HTTP/2 frame type must be rejected "
  1951. "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.8, RFC9114 11.2.1)"),
  1952. #{conn := Conn} = do_connect(Config),
  1953. {ok, StreamRef} = quicer:start_stream(Conn, #{}),
  1954. {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
  1955. {<<":method">>, <<"GET">>},
  1956. {<<":scheme">>, <<"https">>},
  1957. {<<":authority">>, <<"localhost">>},
  1958. {<<":path">>, <<"/">>},
  1959. {<<"content-length">>, <<"0">>}
  1960. ], 0, cow_qpack:init()),
  1961. Len = rand:uniform(512),
  1962. {ok, _} = quicer:send(StreamRef, [
  1963. cow_http3:encode_int(Type),
  1964. cow_http3:encode_int(Len),
  1965. rand:bytes(Len),
  1966. <<1>>, %% HEADERS frame.
  1967. cow_http3:encode_int(iolist_size(EncodedHeaders)),
  1968. EncodedHeaders
  1969. ], ?QUIC_SEND_FLAG_FIN),
  1970. %% The connection should have been closed.
  1971. #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
  1972. ok.
  1973. %% An endpoint MAY choose to treat a stream error as a connection error under
  1974. %% certain circumstances, closing the entire connection in response to a
  1975. %% condition on a single stream.
  1976. %% Because new error codes can be defined without negotiation (see Section 9),
  1977. %% use of an error code in an unexpected context or receipt of an unknown error
  1978. %% code MUST be treated as equivalent to H3_NO_ERROR.
  1979. %% 8.1. HTTP/3 Error Codes
  1980. %% H3_INTERNAL_ERROR (0x0102): An internal error has occurred in the HTTP stack.
  1981. %% H3_EXCESSIVE_LOAD (0x0107): The endpoint detected that its peer is
  1982. %% exhibiting a behavior that might be generating excessive load.
  1983. %% H3_MISSING_SETTINGS (0x010a): No SETTINGS frame was received
  1984. %% at the beginning of the control stream.
  1985. %% H3_REQUEST_REJECTED (0x010b): A server rejected a request without
  1986. %% performing any application processing.
  1987. %% H3_REQUEST_CANCELLED (0x010c): The request or its response
  1988. %% (including pushed response) is cancelled.
  1989. %% H3_REQUEST_INCOMPLETE (0x010d): The client's stream terminated
  1990. %% without containing a fully formed request.
  1991. %% H3_CONNECT_ERROR (0x010f): The TCP connection established in
  1992. %% response to a CONNECT request was reset or abnormally closed.
  1993. %% H3_VERSION_FALLBACK (0x0110): The requested operation cannot
  1994. %% be served over HTTP/3. The peer should retry over HTTP/1.1.
  1995. %% 9. Extensions to HTTP/3
  1996. %% If a setting is used for extension negotiation, the default value MUST be
  1997. %% defined in such a fashion that the extension is disabled if the setting is
  1998. %% omitted.
  1999. %% 10. Security Considerations
  2000. %% 10.3. Intermediary-Encapsulation Attacks
  2001. %% Requests or responses containing invalid field names MUST be treated as malformed.
  2002. %% Any request or response that contains a character not permitted in a field
  2003. %% value MUST be treated as malformed.
  2004. %% 10.5. Denial-of-Service Considerations
  2005. %% Implementations SHOULD track the use of these features and set limits on
  2006. %% their use. An endpoint MAY treat activity that is suspicious as a connection
  2007. %% error of type H3_EXCESSIVE_LOAD, but false positives will result in disrupting
  2008. %% valid connections and requests.
  2009. %% 10.5.1. Limits on Field Section Size
  2010. %% An endpoint can use the SETTINGS_MAX_FIELD_SECTION_SIZE (Section 4.2.2)
  2011. %% setting to advise peers of limits that might apply on the size of field
  2012. %% sections.
  2013. %%
  2014. %% A server that receives a larger field section than it is willing to handle
  2015. %% can send an HTTP 431 (Request Header Fields Too Large) status code
  2016. %% ([RFC6585]).
  2017. %% 10.6. Use of Compression
  2018. %% Implementations communicating on a secure channel MUST NOT compress content
  2019. %% that includes both confidential and attacker-controlled data unless separate
  2020. %% compression contexts are used for each source of data. Compression MUST NOT be
  2021. %% used if the source of data cannot be reliably determined.
  2022. %% 10.9. Early Data
  2023. %% The anti-replay mitigations in [HTTP-REPLAY] MUST be applied when using HTTP/3 with 0-RTT.
  2024. %% 10.10. Migration
  2025. %% Certain HTTP implementations use the client address for logging or
  2026. %% access-control purposes. Since a QUIC client's address might change during a
  2027. %% connection (and future versions might support simultaneous use of multiple
  2028. %% addresses), such implementations will need to either actively retrieve the
  2029. %% client's current address or addresses when they are relevant or explicitly
  2030. %% accept that the original address might change. @todo Document this behavior.
  2031. %% Appendix A. Considerations for Transitioning from HTTP/2
  2032. %% A.1. Streams
  2033. %% QUIC considers a stream closed when all data has been received and sent data
  2034. %% has been acknowledged by the peer. HTTP/2 considers a stream closed when the
  2035. %% frame containing the END_STREAM bit has been committed to the transport. As a
  2036. %% result, the stream for an equivalent exchange could remain "active" for a
  2037. %% longer period of time. HTTP/3 servers might choose to permit a larger number
  2038. %% of concurrent client-initiated bidirectional streams to achieve equivalent
  2039. %% concurrency to HTTP/2, depending on the expected usage patterns. @todo Document this.
  2040. %% Helper functions.
  2041. %% @todo Maybe have a function in cow_http3.
  2042. do_reserved_type() ->
  2043. 16#1f * (rand:uniform(148764065110560900) - 1) + 16#21.
  2044. do_connect(Config) ->
  2045. do_connect(Config, #{}).
  2046. do_connect(Config, Opts) ->
  2047. {ok, Conn} = quicer:connect("localhost", config(port, Config),
  2048. Opts#{alpn => ["h3"], verify => none}, 5000),
  2049. %% To make sure the connection is fully established we wait
  2050. %% to receive the SETTINGS frame on the control stream.
  2051. {ok, ControlRef, Settings} = do_wait_settings(Conn),
  2052. #{
  2053. conn => Conn,
  2054. control => ControlRef, %% This is the peer control stream.
  2055. settings => Settings
  2056. }.
  2057. do_wait_settings(Conn) ->
  2058. receive
  2059. {quic, new_stream, StreamRef, #{flags := Flags}} ->
  2060. ok = quicer:setopt(StreamRef, active, true),
  2061. true = quicer:is_unidirectional(Flags),
  2062. receive {quic, <<
  2063. 0, %% Control stream.
  2064. SettingsFrame/bits
  2065. >>, StreamRef, _} ->
  2066. {ok, {settings, Settings}, <<>>} = cow_http3:parse(SettingsFrame),
  2067. {ok, StreamRef, Settings}
  2068. after 5000 ->
  2069. {error, timeout}
  2070. end
  2071. after 5000 ->
  2072. {error, timeout}
  2073. end.
  2074. do_receive_data(StreamRef) ->
  2075. receive
  2076. {quic, Data, StreamRef, _Flags} when is_binary(Data) ->
  2077. {ok, Data}
  2078. after 5000 ->
  2079. {error, timeout}
  2080. end.
  2081. do_guess_int_encoding(Data) ->
  2082. SizeWithLen = byte_size(Data) - 1,
  2083. if
  2084. SizeWithLen < 64 + 1 ->
  2085. {0, 6};
  2086. SizeWithLen < 16384 + 2 ->
  2087. {1, 14};
  2088. SizeWithLen < 1073741824 + 4 ->
  2089. {2, 30};
  2090. SizeWithLen < 4611686018427387904 + 8 ->
  2091. {3, 62}
  2092. end.
  2093. do_wait_peer_send_shutdown(StreamRef) ->
  2094. receive
  2095. {quic, peer_send_shutdown, StreamRef, undefined} ->
  2096. ok
  2097. after 5000 ->
  2098. {error, timeout}
  2099. end.
  2100. do_wait_stream_aborted(StreamRef) ->
  2101. receive
  2102. {quic, peer_send_aborted, StreamRef, Code} ->
  2103. Reason = cow_http3:code_to_error(Code),
  2104. #{reason => Reason};
  2105. {quic, peer_receive_aborted, StreamRef, Code} ->
  2106. Reason = cow_http3:code_to_error(Code),
  2107. #{reason => Reason}
  2108. after 5000 ->
  2109. {error, timeout}
  2110. end.
  2111. do_wait_stream_closed(StreamRef) ->
  2112. receive
  2113. {quic, stream_closed, StreamRef, #{error := Error, is_conn_shutdown := false}} ->
  2114. 0 = Error,
  2115. ok
  2116. after 5000 ->
  2117. {error, timeout}
  2118. end.
  2119. do_receive_response(StreamRef) ->
  2120. {ok, Data} = do_receive_data(StreamRef),
  2121. {HLenEnc, HLenBits} = do_guess_int_encoding(Data),
  2122. <<
  2123. 1, %% HEADERS frame.
  2124. HLenEnc:2, HLen:HLenBits,
  2125. EncodedResponse:HLen/bytes,
  2126. Rest/bits
  2127. >> = Data,
  2128. {ok, DecodedResponse, _DecData, _DecSt}
  2129. = cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init()),
  2130. Headers = maps:from_list(DecodedResponse),
  2131. #{<<"content-length">> := BodyLen} = Headers,
  2132. {DLenEnc, DLenBits} = do_guess_int_encoding(Rest),
  2133. Body = case Rest of
  2134. <<>> ->
  2135. <<>>;
  2136. <<
  2137. 0, %% DATA frame.
  2138. DLenEnc:2, DLen:DLenBits,
  2139. Body0:DLen/bytes
  2140. >> ->
  2141. BodyLen = integer_to_binary(byte_size(Body0)),
  2142. Body0
  2143. end,
  2144. ok = do_wait_peer_send_shutdown(StreamRef),
  2145. #{
  2146. headers => Headers,
  2147. body => Body
  2148. }.
  2149. do_wait_connection_closed(Conn) ->
  2150. receive
  2151. {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
  2152. Reason = cow_http3:code_to_error(Code),
  2153. #{reason => Reason}
  2154. after 5000 ->
  2155. {error, timeout}
  2156. end.