rfc9114_SUITE.erl 87 KB

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