rfc9114_SUITE.erl 89 KB

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