rfc9114_SUITE.erl 87 KB

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