rfc7540_SUITE.erl 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143
  1. %% Copyright (c) 2016-2017, 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(rfc7540_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. -import(cowboy_test, [raw_open/1]).
  21. -import(cowboy_test, [raw_send/2]).
  22. -import(cowboy_test, [raw_recv_head/1]).
  23. -import(cowboy_test, [raw_recv/3]).
  24. all() -> [{group, clear}, {group, tls}].
  25. groups() ->
  26. Modules = ct_helper:all(?MODULE),
  27. Clear = [M || M <- Modules, lists:sublist(atom_to_list(M), 4) =/= "alpn"] -- [prior_knowledge_reject_tls],
  28. TLS = [M || M <- Modules, lists:sublist(atom_to_list(M), 4) =:= "alpn"] ++ [prior_knowledge_reject_tls],
  29. [{clear, [parallel], Clear}, {tls, [parallel], TLS}].
  30. init_per_group(Name = clear, Config) ->
  31. cowboy_test:init_http(Name, #{
  32. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  33. }, Config);
  34. init_per_group(Name = tls, Config) ->
  35. cowboy_test:init_http2(Name, #{
  36. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  37. }, Config).
  38. end_per_group(Name, _) ->
  39. ok = cowboy:stop_listener(Name).
  40. init_routes(_) -> [
  41. {"localhost", [
  42. {"/", hello_h, []},
  43. {"/echo/:key", echo_h, []},
  44. {"/long_polling", long_polling_h, []},
  45. {"/loop_handler_abort", loop_handler_abort_h, []},
  46. {"/resp/:key[/:arg]", resp_h, []}
  47. ]}
  48. ].
  49. %% Starting HTTP/2 for "http" URIs.
  50. http_upgrade_ignore_h2(Config) ->
  51. doc("An h2 token in an Upgrade field must be ignored. (RFC7540 3.2)"),
  52. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  53. ok = gen_tcp:send(Socket, [
  54. "GET / HTTP/1.1\r\n"
  55. "Host: localhost\r\n"
  56. "Connection: Upgrade, HTTP2-Settings\r\n"
  57. "Upgrade: h2\r\n"
  58. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  59. "\r\n"]),
  60. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  61. ok.
  62. http_upgrade_ignore_if_http_10(Config) ->
  63. doc("The Upgrade header must be ignored if part of an HTTP/1.0 request. (RFC7230 6.7)"),
  64. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  65. ok = gen_tcp:send(Socket, [
  66. "GET / HTTP/1.0\r\n"
  67. "Host: localhost\r\n"
  68. "Connection: Upgrade, HTTP2-Settings\r\n"
  69. "Upgrade: h2c\r\n"
  70. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  71. "\r\n"]),
  72. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  73. ok.
  74. http_upgrade_ignore_missing_upgrade_in_connection(Config) ->
  75. doc("The Upgrade header must be listed in the "
  76. "Connection header field. (RFC7230 6.7)"),
  77. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  78. ok = gen_tcp:send(Socket, [
  79. "GET / HTTP/1.1\r\n"
  80. "Host: localhost\r\n"
  81. "Connection: HTTP2-Settings\r\n"
  82. "Upgrade: h2c\r\n"
  83. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  84. "\r\n"]),
  85. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  86. ok.
  87. http_upgrade_ignore_missing_http2_settings_in_connection(Config) ->
  88. doc("The HTTP2-Settings header must be listed in the "
  89. "Connection header field. (RFC7540 3.2.1, RFC7230 6.7)"),
  90. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  91. ok = gen_tcp:send(Socket, [
  92. "GET / HTTP/1.1\r\n"
  93. "Host: localhost\r\n"
  94. "Connection: Upgrade\r\n"
  95. "Upgrade: h2c\r\n"
  96. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  97. "\r\n"]),
  98. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  99. ok.
  100. http_upgrade_ignore_zero_http2_settings_header(Config) ->
  101. doc("The HTTP Upgrade request must include "
  102. "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  103. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  104. ok = gen_tcp:send(Socket, [
  105. "GET / HTTP/1.1\r\n"
  106. "Host: localhost\r\n"
  107. "Connection: Upgrade, HTTP2-Settings\r\n"
  108. "Upgrade: h2c\r\n"
  109. "\r\n"]),
  110. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  111. ok.
  112. http_upgrade_reject_two_http2_settings_header(Config) ->
  113. doc("The HTTP Upgrade request must include "
  114. "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  115. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  116. ok = gen_tcp:send(Socket, [
  117. "GET / HTTP/1.1\r\n"
  118. "Host: localhost\r\n"
  119. "Connection: Upgrade, HTTP2-Settings\r\n"
  120. "Upgrade: h2c\r\n"
  121. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  122. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  123. "\r\n"]),
  124. {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
  125. ok.
  126. http_upgrade_reject_bad_http2_settings_header(Config) ->
  127. doc("The HTTP Upgrade request must include "
  128. "a valid HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  129. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  130. ok = gen_tcp:send(Socket, [
  131. "GET / HTTP/1.1\r\n"
  132. "Host: localhost\r\n"
  133. "Connection: Upgrade, HTTP2-Settings\r\n"
  134. "Upgrade: h2c\r\n"
  135. %% We send a full SETTINGS frame on purpose.
  136. "HTTP2-Settings: ", base64:encode(iolist_to_binary(cow_http2:settings(#{}))), "\r\n",
  137. "\r\n"]),
  138. {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
  139. ok.
  140. %% Match directly for now.
  141. do_recv_101(Socket) ->
  142. {ok, <<
  143. "HTTP/1.1 101 Switching Protocols\r\n"
  144. "connection: Upgrade\r\n"
  145. "upgrade: h2c\r\n"
  146. "\r\n"
  147. >>} = gen_tcp:recv(Socket, 71, 1000),
  148. ok.
  149. http_upgrade_101(Config) ->
  150. doc("A 101 response must be sent on successful upgrade "
  151. "to HTTP/2 when using the HTTP Upgrade mechanism. (RFC7540 3.2, RFC7230 6.7)"),
  152. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  153. ok = gen_tcp:send(Socket, [
  154. "GET / HTTP/1.1\r\n"
  155. "Host: localhost\r\n"
  156. "Connection: Upgrade, HTTP2-Settings\r\n"
  157. "Upgrade: h2c\r\n"
  158. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  159. "\r\n"]),
  160. ok = do_recv_101(Socket),
  161. ok.
  162. http_upgrade_server_preface(Config) ->
  163. doc("The first frame after the upgrade must be a "
  164. "SETTINGS frame for the server connection preface. (RFC7540 3.2, RFC7540 3.5, RFC7540 6.5)"),
  165. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  166. ok = gen_tcp:send(Socket, [
  167. "GET / HTTP/1.1\r\n"
  168. "Host: localhost\r\n"
  169. "Connection: Upgrade, HTTP2-Settings\r\n"
  170. "Upgrade: h2c\r\n"
  171. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  172. "\r\n"]),
  173. ok = do_recv_101(Socket),
  174. %% Receive the server preface.
  175. {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
  176. ok.
  177. http_upgrade_client_preface_timeout(Config) ->
  178. doc("Clients negotiating HTTP/2 and not sending a preface in "
  179. "a timely manner must be disconnected."),
  180. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  181. ok = gen_tcp:send(Socket, [
  182. "GET / HTTP/1.1\r\n"
  183. "Host: localhost\r\n"
  184. "Connection: Upgrade, HTTP2-Settings\r\n"
  185. "Upgrade: h2c\r\n"
  186. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  187. "\r\n"]),
  188. ok = do_recv_101(Socket),
  189. %% Receive the server preface.
  190. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  191. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  192. %% Receive the response to the initial HTTP/1.1 request.
  193. {ok, << HeadersSkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  194. {ok, _} = gen_tcp:recv(Socket, HeadersSkipLen, 1000),
  195. {ok, << DataSkipLen:24, 0:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  196. {ok, _} = gen_tcp:recv(Socket, DataSkipLen, 1000),
  197. %% Do not send the preface. Wait for the server to disconnect us.
  198. {error, closed} = gen_tcp:recv(Socket, 9, 6000),
  199. ok.
  200. http_upgrade_reject_missing_client_preface(Config) ->
  201. doc("Servers must treat an invalid connection preface as a "
  202. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  203. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  204. ok = gen_tcp:send(Socket, [
  205. "GET / HTTP/1.1\r\n"
  206. "Host: localhost\r\n"
  207. "Connection: Upgrade, HTTP2-Settings\r\n"
  208. "Upgrade: h2c\r\n"
  209. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  210. "\r\n"]),
  211. ok = do_recv_101(Socket),
  212. %% Send a SETTINGS frame directly instead of the proper preface.
  213. ok = gen_tcp:send(Socket, cow_http2:settings(#{})),
  214. %% Receive the server preface.
  215. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  216. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  217. %% We expect the server to close the connection when it receives a bad preface.
  218. %% The server may however have already started sending the response to the
  219. %% initial HTTP/1.1 request.
  220. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  221. case gen_tcp:recv(Socket, 9, 1000) of
  222. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  223. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  224. [headers|Acc];
  225. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  226. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  227. [data|Acc];
  228. {error, _} ->
  229. [closed|Acc]
  230. end
  231. end, [], [1, 2, 3])),
  232. case Received of
  233. [closed|_] -> ok;
  234. [headers, closed|_] -> ok;
  235. [headers, data, closed] -> ok
  236. end.
  237. http_upgrade_reject_invalid_client_preface(Config) ->
  238. doc("Servers must treat an invalid connection preface as a "
  239. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  240. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  241. ok = gen_tcp:send(Socket, [
  242. "GET / HTTP/1.1\r\n"
  243. "Host: localhost\r\n"
  244. "Connection: Upgrade, HTTP2-Settings\r\n"
  245. "Upgrade: h2c\r\n"
  246. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  247. "\r\n"]),
  248. ok = do_recv_101(Socket),
  249. %% Send a slightly incorrect preface.
  250. ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  251. %% Receive the server preface.
  252. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  253. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  254. %% We expect the server to close the connection when it receives a bad preface.
  255. %% The server may however have already started sending the response to the
  256. %% initial HTTP/1.1 request.
  257. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  258. case gen_tcp:recv(Socket, 9, 1000) of
  259. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  260. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  261. [headers|Acc];
  262. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  263. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  264. [data|Acc];
  265. {error, _} ->
  266. [closed|Acc]
  267. end
  268. end, [], [1, 2, 3])),
  269. case Received of
  270. [closed|_] -> ok;
  271. [headers, closed|_] -> ok;
  272. [headers, data, closed] -> ok
  273. end.
  274. http_upgrade_reject_missing_client_preface_settings(Config) ->
  275. doc("Servers must treat an invalid connection preface as a "
  276. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  277. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  278. ok = gen_tcp:send(Socket, [
  279. "GET / HTTP/1.1\r\n"
  280. "Host: localhost\r\n"
  281. "Connection: Upgrade, HTTP2-Settings\r\n"
  282. "Upgrade: h2c\r\n"
  283. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  284. "\r\n"]),
  285. ok = do_recv_101(Socket),
  286. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  287. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  288. %% Receive the server preface.
  289. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  290. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  291. %% We expect the server to close the connection when it receives a bad preface.
  292. %% The server may however have already started sending the response to the
  293. %% initial HTTP/1.1 request.
  294. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  295. case gen_tcp:recv(Socket, 9, 1000) of
  296. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  297. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  298. [headers|Acc];
  299. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  300. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  301. [data|Acc];
  302. {error, _} ->
  303. [closed|Acc]
  304. end
  305. end, [], [1, 2, 3])),
  306. case Received of
  307. [closed|_] -> ok;
  308. [headers, closed|_] -> ok;
  309. [headers, data, closed] -> ok
  310. end.
  311. http_upgrade_reject_invalid_client_preface_settings(Config) ->
  312. doc("Servers must treat an invalid connection preface as a "
  313. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  314. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  315. ok = gen_tcp:send(Socket, [
  316. "GET / HTTP/1.1\r\n"
  317. "Host: localhost\r\n"
  318. "Connection: Upgrade, HTTP2-Settings\r\n"
  319. "Upgrade: h2c\r\n"
  320. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  321. "\r\n"]),
  322. ok = do_recv_101(Socket),
  323. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  324. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
  325. %% Receive the server preface.
  326. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  327. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  328. %% We expect the server to close the connection when it receives a bad preface.
  329. %% The server may however have already started sending the response to the
  330. %% initial HTTP/1.1 request.
  331. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  332. case gen_tcp:recv(Socket, 9, 1000) of
  333. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  334. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  335. [headers|Acc];
  336. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  337. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  338. [data|Acc];
  339. {error, _} ->
  340. [closed|Acc]
  341. end
  342. end, [], [1, 2, 3])),
  343. case Received of
  344. [closed|_] -> ok;
  345. [headers, closed|_] -> ok;
  346. [headers, data, closed] -> ok
  347. end.
  348. http_upgrade_accept_client_preface_empty_settings(Config) ->
  349. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.2, RFC7540 3.5)"),
  350. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  351. ok = gen_tcp:send(Socket, [
  352. "GET / HTTP/1.1\r\n"
  353. "Host: localhost\r\n"
  354. "Connection: Upgrade, HTTP2-Settings\r\n"
  355. "Upgrade: h2c\r\n"
  356. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  357. "\r\n"]),
  358. ok = do_recv_101(Socket),
  359. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  360. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  361. %% Receive the server preface.
  362. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  363. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  364. %% Receive the SETTINGS ack. The response might arrive beforehand.
  365. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  366. case gen_tcp:recv(Socket, 9, 1000) of
  367. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  368. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  369. [headers|Acc];
  370. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  371. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  372. [data|Acc];
  373. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  374. [settings_ack|Acc]
  375. end
  376. end, [], [1, 2, 3])),
  377. case Received of
  378. [settings_ack|_] -> ok;
  379. [headers, settings_ack|_] -> ok;
  380. [headers, data, settings_ack] -> ok
  381. end.
  382. http_upgrade_client_preface_settings_ack_timeout(Config) ->
  383. doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
  384. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  385. ok = gen_tcp:send(Socket, [
  386. "GET / HTTP/1.1\r\n"
  387. "Host: localhost\r\n"
  388. "Connection: Upgrade, HTTP2-Settings\r\n"
  389. "Upgrade: h2c\r\n"
  390. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  391. "\r\n"]),
  392. ok = do_recv_101(Socket),
  393. %% Send a valid preface.
  394. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  395. %% Receive the server preface.
  396. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  397. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  398. %% Skip the SETTINGS ack. Receive a GOAWAY with reason SETTINGS_TIMEOUT,
  399. %% possibly following a HEADERS or HEADERS and DATA frames.
  400. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  401. case gen_tcp:recv(Socket, 9, 6000) of
  402. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  403. Acc;
  404. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  405. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  406. [headers|Acc];
  407. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  408. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  409. [data|Acc];
  410. {ok, << 8:24, 7:8, 0:40 >>} ->
  411. %% We expect a SETTINGS_TIMEOUT reason.
  412. {ok, << 1:32, 4:32 >>} = gen_tcp:recv(Socket, 8, 1000),
  413. [goaway|Acc];
  414. {error, _} ->
  415. %% Can be timeouts, ignore them.
  416. Acc
  417. end
  418. end, [], [1, 2, 3, 4])),
  419. case Received of
  420. [goaway] -> ok;
  421. [headers, goaway] -> ok;
  422. [headers, data, goaway] -> ok
  423. end.
  424. %% @todo We need a successful test with actual options in HTTP2-Settings.
  425. %% SETTINGS_MAX_FRAME_SIZE is probably the easiest to test. The relevant
  426. %% RFC quote is:
  427. %%
  428. %% 3.2.1
  429. %% A server decodes and interprets these values as it would any other
  430. %% SETTINGS frame. Explicit acknowledgement of these settings
  431. %% (Section 6.5.3) is not necessary, since a 101 response serves as
  432. %% implicit acknowledgement.
  433. %% @todo We need to test an upgrade with a request body. It is probably
  434. %% worth having a configuration value for how much we accept while still
  435. %% upgrading (if too big, we would just stay on HTTP/1.1). We therefore
  436. %% needs a test for when the body is small enough, and one for when the
  437. %% body is larger than we accept. The relevant RFC quote is:
  438. %%
  439. %% 3.2
  440. %% Requests that contain a payload body MUST be sent in their entirety
  441. %% before the client can send HTTP/2 frames. This means that a large
  442. %% request can block the use of the connection until it is completely
  443. %% sent.
  444. %% @todo We should definitely have a test with OPTIONS. The relevant
  445. %% RFC quote is:
  446. %%
  447. %% 3.2
  448. %% If concurrency of an initial request with subsequent requests is
  449. %% important, an OPTIONS request can be used to perform the upgrade to
  450. %% HTTP/2, at the cost of an additional round trip.
  451. %% @todo If we ever handle priority, we need to check that the initial
  452. %% HTTP/1.1 request has default priority. The relevant RFC quote is:
  453. %%
  454. %% 3.2
  455. %% The HTTP/1.1 request that is sent prior to upgrade is assigned a
  456. %% stream identifier of 1 (see Section 5.1.1) with default priority
  457. %% values (Section 5.3.5).
  458. http_upgrade_response(Config) ->
  459. doc("A response must be sent to the initial HTTP/1.1 request "
  460. "after switching to HTTP/2. The response must use "
  461. "the stream identifier 1. (RFC7540 3.2)"),
  462. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  463. ok = gen_tcp:send(Socket, [
  464. "GET / HTTP/1.1\r\n"
  465. "Host: localhost\r\n"
  466. "Connection: Upgrade, HTTP2-Settings\r\n"
  467. "Upgrade: h2c\r\n"
  468. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  469. "\r\n"]),
  470. ok = do_recv_101(Socket),
  471. %% Send a valid preface.
  472. %% @todo Use non-empty SETTINGS here. Just because.
  473. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  474. %% Receive the server preface.
  475. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  476. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  477. %% Send the SETTINGS ack.
  478. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  479. %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
  480. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  481. case gen_tcp:recv(Socket, 9, 1000) of
  482. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  483. [settings_ack|Acc];
  484. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  485. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  486. [headers|Acc];
  487. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  488. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  489. [data|Acc]
  490. end
  491. end, [], [1, 2, 3])),
  492. case Received of
  493. [settings_ack, headers, data] -> ok;
  494. [headers, settings_ack, data] -> ok;
  495. [headers, data, settings_ack] -> ok
  496. end.
  497. http_upgrade_response_half_closed(Config) ->
  498. doc("The stream for the initial HTTP/1.1 request is half-closed. (RFC7540 3.2)"),
  499. %% Try sending more data after the upgrade and get an error.
  500. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  501. ok = gen_tcp:send(Socket, [
  502. "GET /long_polling HTTP/1.1\r\n"
  503. "Host: localhost\r\n"
  504. "Connection: Upgrade, HTTP2-Settings\r\n"
  505. "Upgrade: h2c\r\n"
  506. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  507. "\r\n"]),
  508. ok = do_recv_101(Socket),
  509. %% Send a valid preface followed by an unexpected DATA frame.
  510. ok = gen_tcp:send(Socket, [
  511. "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
  512. cow_http2:settings(#{}),
  513. cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)
  514. ]),
  515. %% Receive the server preface.
  516. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  517. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  518. %% Skip the SETTINGS ack. Receive an RST_STREAM possibly following by
  519. %% a HEADERS frame, or a GOAWAY following HEADERS and DATA. This
  520. %% corresponds to the stream being in half-closed and closed states.
  521. %% The reason must be STREAM_CLOSED.
  522. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  523. case gen_tcp:recv(Socket, 9, 1000) of
  524. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  525. Acc;
  526. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  527. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  528. [headers|Acc];
  529. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  530. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  531. [data|Acc];
  532. {ok, << 4:24, 3:8, 0:8, 1:32 >>} ->
  533. %% We expect a STREAM_CLOSED reason.
  534. {ok, << 5:32 >>} = gen_tcp:recv(Socket, 4, 1000),
  535. [rst_stream|Acc];
  536. {ok, << 8:24, 7:8, 0:40 >>} ->
  537. %% We expect a STREAM_CLOSED reason.
  538. {ok, << 1:32, 5:32 >>} = gen_tcp:recv(Socket, 8, 1000),
  539. [goaway|Acc];
  540. {error, _} ->
  541. %% Can be timeouts, ignore them.
  542. Acc
  543. end
  544. end, [], [1, 2, 3, 4])),
  545. case Received of
  546. [rst_stream] -> ok;
  547. [headers, rst_stream] -> ok;
  548. [headers, data, goaway] -> ok
  549. end.
  550. %% Starting HTTP/2 for "https" URIs.
  551. alpn_ignore_h2c(Config) ->
  552. doc("An h2c ALPN protocol identifier must be ignored. (RFC7540 3.3)"),
  553. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  554. [{alpn_advertised_protocols, [<<"h2c">>, <<"http/1.1">>]}, binary, {active, false}]),
  555. {ok, <<"http/1.1">>} = ssl:negotiated_protocol(Socket),
  556. ok.
  557. alpn_server_preface(Config) ->
  558. doc("The first frame must be a SETTINGS frame "
  559. "for the server connection preface. (RFC7540 3.3, RFC7540 3.5, RFC7540 6.5)"),
  560. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  561. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  562. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  563. %% Receive the server preface.
  564. {ok, << _:24, 4:8, 0:40 >>} = ssl:recv(Socket, 9, 1000),
  565. ok.
  566. alpn_client_preface_timeout(Config) ->
  567. doc("Clients negotiating HTTP/2 and not sending a preface in "
  568. "a timely manner must be disconnected."),
  569. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  570. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  571. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  572. %% Receive the server preface.
  573. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  574. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  575. %% Do not send the preface. Wait for the server to disconnect us.
  576. {error, closed} = ssl:recv(Socket, 3, 6000),
  577. ok.
  578. alpn_reject_missing_client_preface(Config) ->
  579. doc("Servers must treat an invalid connection preface as a "
  580. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  581. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  582. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  583. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  584. %% Send a SETTINGS frame directly instead of the proper preface.
  585. ok = ssl:send(Socket, cow_http2:settings(#{})),
  586. %% Receive the server preface.
  587. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  588. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  589. %% We expect the server to close the connection when it receives a bad preface.
  590. {error, closed} = ssl:recv(Socket, 3, 1000),
  591. ok.
  592. alpn_reject_invalid_client_preface(Config) ->
  593. doc("Servers must treat an invalid connection preface as a "
  594. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  595. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  596. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  597. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  598. %% Send a slightly incorrect preface.
  599. ok = ssl:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  600. %% Receive the server preface.
  601. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  602. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  603. %% We expect the server to close the connection when it receives a bad preface.
  604. {error, closed} = ssl:recv(Socket, 3, 1000),
  605. ok.
  606. alpn_reject_missing_client_preface_settings(Config) ->
  607. doc("Servers must treat an invalid connection preface as a "
  608. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  609. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  610. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  611. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  612. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  613. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  614. %% Receive the server preface.
  615. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  616. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  617. %% We expect the server to close the connection when it receives a bad preface.
  618. {error, closed} = ssl:recv(Socket, 3, 1000),
  619. ok.
  620. alpn_reject_invalid_client_preface_settings(Config) ->
  621. doc("Servers must treat an invalid connection preface as a "
  622. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  623. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  624. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  625. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  626. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  627. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
  628. %% Receive the server preface.
  629. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  630. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  631. %% We expect the server to close the connection when it receives a bad preface.
  632. {error, closed} = ssl:recv(Socket, 3, 1000),
  633. ok.
  634. alpn_accept_client_preface_empty_settings(Config) ->
  635. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.3, RFC7540 3.5)"),
  636. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  637. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  638. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  639. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  640. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  641. %% Receive the server preface.
  642. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  643. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  644. %% Receive the SETTINGS ack.
  645. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  646. ok.
  647. alpn_client_preface_settings_ack_timeout(Config) ->
  648. doc("Failure to acknowledge the server's SETTINGS frame "
  649. "results in a SETTINGS_TIMEOUT connection error. (RFC7540 3.5, RFC7540 6.5.3)"),
  650. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  651. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  652. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  653. %% Send a valid preface.
  654. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  655. %% Receive the server preface.
  656. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  657. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  658. %% Receive the SETTINGS ack.
  659. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  660. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  661. {ok, << _:24, 7:8, _:72, 4:32 >>} = ssl:recv(Socket, 17, 6000),
  662. ok.
  663. alpn(Config) ->
  664. doc("Successful ALPN negotiation. (RFC7540 3.3)"),
  665. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  666. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  667. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  668. %% Send a valid preface.
  669. %% @todo Use non-empty SETTINGS here. Just because.
  670. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  671. %% Receive the server preface.
  672. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  673. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  674. %% Send the SETTINGS ack.
  675. ok = ssl:send(Socket, cow_http2:settings_ack()),
  676. %% Receive the SETTINGS ack.
  677. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  678. %% Wait until after the SETTINGS ack timeout was supposed to trigger.
  679. receive after 6000 -> ok end,
  680. %% Send a PING.
  681. ok = ssl:send(Socket, cow_http2:ping(0)),
  682. %% Receive a PING ack back, indicating the connection is still up.
  683. {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = ssl:recv(Socket, 17, 1000),
  684. ok.
  685. %% Starting HTTP/2 with prior knowledge.
  686. prior_knowledge_reject_tls(Config) ->
  687. doc("Implementations that support HTTP/2 over TLS must use ALPN. (RFC7540 3.4)"),
  688. {ok, Socket} = ssl:connect("localhost", config(port, Config), [binary, {active, false}]),
  689. %% Send a valid preface.
  690. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  691. %% We expect the server to send an HTTP 400 error
  692. %% when trying to use HTTP/2 without going through ALPN negotiation.
  693. {ok, <<"HTTP/1.1 400">>} = ssl:recv(Socket, 12, 1000),
  694. ok.
  695. prior_knowledge_server_preface(Config) ->
  696. doc("The first frame must be a SETTINGS frame "
  697. "for the server connection preface. (RFC7540 3.4, RFC7540 3.5, RFC7540 6.5)"),
  698. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  699. %% Send a valid preface.
  700. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  701. %% Receive the server preface.
  702. {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
  703. ok.
  704. %% Note: the client preface timeout doesn't apply in this case,
  705. %% so we don't test it. An HTTP/1.1 client that does not send
  706. %% a request in a timely manner will get disconnected by the
  707. %% HTTP protocol code, not by HTTP/2's.
  708. %% Note: the test that starts by sending a SETTINGS frame is
  709. %% redundant with tests sending garbage on the connection.
  710. %% From the point of view of an HTTP/1.1 connection, a
  711. %% SETTINGS frame is indistinguishable from garbage.
  712. prior_knowledge_reject_invalid_client_preface(Config) ->
  713. doc("An incorrect preface is an invalid HTTP/1.1 request. (RFC7540 3.4)"),
  714. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  715. %% Send a slightly incorrect preface.
  716. ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  717. %% We propagate to HTTP/2 after checking only the request-line.
  718. %% The server then sends its preface before checking the full client preface.
  719. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  720. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  721. %% We expect the server to close the connection when it receives a bad preface.
  722. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  723. ok.
  724. prior_knowledge_reject_missing_client_preface_settings(Config) ->
  725. doc("Servers must treat an invalid connection preface as a "
  726. "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
  727. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  728. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  729. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  730. %% Receive the server preface.
  731. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  732. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  733. %% We expect the server to close the connection when it receives a bad preface.
  734. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  735. ok.
  736. prior_knowledge_reject_invalid_client_preface_settings(Config) ->
  737. doc("Servers must treat an invalid connection preface as a "
  738. "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
  739. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  740. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  741. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
  742. %% Receive the server preface.
  743. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  744. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  745. %% We expect the server to close the connection when it receives a bad preface.
  746. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  747. ok.
  748. prior_knowledge_accept_client_preface_empty_settings(Config) ->
  749. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.4, RFC7540 3.5)"),
  750. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  751. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  752. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  753. %% Receive the server preface.
  754. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  755. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  756. %% Receive the SETTINGS ack.
  757. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  758. ok.
  759. prior_knowledge_client_preface_settings_ack_timeout(Config) ->
  760. doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
  761. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  762. %% Send a valid preface.
  763. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  764. %% Receive the server preface.
  765. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  766. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  767. %% Receive the SETTINGS ack.
  768. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  769. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  770. {ok, << _:24, 7:8, _:72, 4:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  771. ok.
  772. %% Do a prior knowledge handshake.
  773. do_handshake(Config) ->
  774. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  775. %% Send a valid preface.
  776. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  777. %% Receive the server preface.
  778. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  779. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  780. %% Send the SETTINGS ack.
  781. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  782. %% Receive the SETTINGS ack.
  783. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  784. {ok, Socket}.
  785. prior_knowledge(Config) ->
  786. doc("Streams can be initiated after a successful HTTP/2 connection "
  787. "with prior knowledge of server capabilities. (RFC7540 3.4)"),
  788. %% @todo Use non-empty SETTINGS here. Just because.
  789. {ok, Socket} = do_handshake(Config),
  790. %% Wait until after the SETTINGS ack timeout was supposed to trigger.
  791. receive after 6000 -> ok end,
  792. %% Send a PING.
  793. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  794. %% Receive a PING ack back, indicating the connection is still up.
  795. {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = gen_tcp:recv(Socket, 17, 1000),
  796. ok.
  797. %% @todo If we ever add an option to disable HTTP/2, we need to check
  798. %% the following things:
  799. %% * HTTP/1.1 Upgrade returns an HTTP/1.1 response (3.2)
  800. %% * HTTP/1.1 Upgrade errors out if the client sends HTTP/2 frames
  801. %% without waiting for the 101 response (3.2, 3.5)
  802. %% * Prior knowledge handshake fails (3.4)
  803. %% * ALPN selects HTTP/1.1 (3.3)
  804. %% Frame format.
  805. ignore_unknown_frames(Config) ->
  806. doc("Frames of unknown type must be ignored and discarded. (RFC7540 4.1)"),
  807. {ok, Socket} = do_handshake(Config),
  808. %% Send a POST request with a single DATA frame,
  809. %% and an unknown frame type interleaved.
  810. {HeadersBlock, _} = cow_hpack:encode([
  811. {<<":method">>, <<"POST">>},
  812. {<<":scheme">>, <<"http">>},
  813. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  814. {<<":path">>, <<"/echo/read_body">>}
  815. ]),
  816. ok = gen_tcp:send(Socket, [
  817. cow_http2:headers(1, nofin, HeadersBlock),
  818. << 10:24, 99:8, 0:40, 0:80 >>,
  819. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  820. ]),
  821. %% Receive a response with the same DATA frame.
  822. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  823. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  824. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  825. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  826. ok.
  827. ignore_data_unknown_flags(Config) ->
  828. doc("Undefined DATA frame flags must be ignored. (RFC7540 4.1, RFC7540 6.1)"),
  829. {ok, Socket} = do_handshake(Config),
  830. %% Send a POST request with a DATA frame with unknown flags.
  831. {HeadersBlock, _} = cow_hpack:encode([
  832. {<<":method">>, <<"POST">>},
  833. {<<":scheme">>, <<"http">>},
  834. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  835. {<<":path">>, <<"/echo/read_body">>}
  836. ]),
  837. ok = gen_tcp:send(Socket, [
  838. cow_http2:headers(1, nofin, HeadersBlock),
  839. << 100:24, 0:8,
  840. 1:1, 1:1, 1:1, 1:1, %% Undefined.
  841. 0:1, %% PADDED.
  842. 1:1, 1:1, %% Undefined.
  843. 1:1, %% END_STREAM.
  844. 0:1, 1:31, 0:100/unit:8 >>
  845. ]),
  846. %% Receive a response with the same DATA frame.
  847. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  848. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  849. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  850. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  851. ok.
  852. ignore_headers_unknown_flags(Config) ->
  853. doc("Undefined HEADERS frame flags must be ignored. (RFC7540 4.1, RFC7540 6.2)"),
  854. {ok, Socket} = do_handshake(Config),
  855. %% Send a POST request with a HEADERS frame with unknown flags.
  856. {HeadersBlock, _} = cow_hpack:encode([
  857. {<<":method">>, <<"POST">>},
  858. {<<":scheme">>, <<"http">>},
  859. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  860. {<<":path">>, <<"/echo/read_body">>}
  861. ]),
  862. Len = iolist_size(HeadersBlock),
  863. ok = gen_tcp:send(Socket, [
  864. << Len:24, 1:8,
  865. 1:1, 1:1, %% Undefined.
  866. 0:1, %% PRIORITY.
  867. 1:1, %% Undefined.
  868. 0:1, %% PADDED.
  869. 1:1, %% END_HEADERS.
  870. 1:1, %% Undefined.
  871. 0:1, %% END_STREAM.
  872. 0:1, 1:31 >>,
  873. HeadersBlock,
  874. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  875. ]),
  876. %% Receive a response with the same DATA frame.
  877. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  878. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  879. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  880. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  881. ok.
  882. ignore_priority_unknown_flags(Config) ->
  883. doc("Undefined PRIORITY frame flags must be ignored. (RFC7540 4.1, RFC7540 6.3)"),
  884. {ok, Socket} = do_handshake(Config),
  885. %% Send a POST request with an interleaved PRIORITY frame with unknown flags.
  886. {HeadersBlock, _} = cow_hpack:encode([
  887. {<<":method">>, <<"POST">>},
  888. {<<":scheme">>, <<"http">>},
  889. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  890. {<<":path">>, <<"/echo/read_body">>}
  891. ]),
  892. ok = gen_tcp:send(Socket, [
  893. cow_http2:headers(1, nofin, HeadersBlock),
  894. << 5:24, 2:8,
  895. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  896. 0:1, 1:31, 0:1, 3:31, 0:8 >>,
  897. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  898. ]),
  899. %% Receive a response with the same DATA frame.
  900. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  901. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  902. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  903. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  904. ok.
  905. ignore_rst_stream_unknown_flags(Config) ->
  906. doc("Undefined RST_STREAM frame flags must be ignored. (RFC7540 4.1, RFC7540 6.4)"),
  907. {ok, Socket} = do_handshake(Config),
  908. %% Send a POST request then cancel it with an RST_STREAM frame with unknown flags.
  909. {HeadersBlock, _} = cow_hpack:encode([
  910. {<<":method">>, <<"POST">>},
  911. {<<":scheme">>, <<"http">>},
  912. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  913. {<<":path">>, <<"/echo/read_body">>}
  914. ]),
  915. ok = gen_tcp:send(Socket, [
  916. cow_http2:headers(1, nofin, HeadersBlock),
  917. << 4:24, 3:8,
  918. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  919. 0:1, 1:31, 8:32 >>,
  920. cow_http2:headers(3, nofin, HeadersBlock),
  921. cow_http2:data(3, fin, << 0:100/unit:8 >>)
  922. ]),
  923. %% Receive a response with the same DATA frame.
  924. {ok, << SkipLen:24, 1:8, _:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  925. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  926. {ok, << 100:24, 0:8, 1:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  927. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  928. ok.
  929. ignore_settings_unknown_flags(Config) ->
  930. doc("Undefined SETTINGS frame flags must be ignored. (RFC7540 4.1, RFC7540 6.5)"),
  931. {ok, Socket} = do_handshake(Config),
  932. %% Send a SETTINGS frame with unknown flags.
  933. ok = gen_tcp:send(Socket, << 6:24, 4:8,
  934. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  935. 0:1, %% ACK.
  936. 0:32, 2:16, 0:32 >>),
  937. %% Receive a SETTINGS ack.
  938. {ok, << 0:24, 4:8, 0:7, 1:1, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  939. ok.
  940. ignore_push_promise_unknown_flags(Config) ->
  941. doc("Undefined PUSH_PROMISE frame flags must be ignored. (RFC7540 4.1, RFC7540 6.6)"),
  942. {ok, Socket} = do_handshake(Config),
  943. %% Send a PUSH_PROMISE frame with unknown flags.
  944. ok = gen_tcp:send(Socket, << 4:24, 5:8,
  945. 1:1, 1:1, 1:1, 1:1, %% Undefined.
  946. 0:1, %% PADDED.
  947. 1:1, %% END_HEADERS.
  948. 1:1, 1:1, %% Undefined.
  949. 0:1, 1:31, 0:1, 3:31 >>
  950. ),
  951. %% Receive a PROTOCOL_ERROR connection error.
  952. %%
  953. %% Note that it is not possible to distinguish between the expected
  954. %% result and the server rejecting PUSH_PROMISE frames.
  955. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  956. ok.
  957. ignore_ping_unknown_flags(Config) ->
  958. doc("Undefined PING frame flags must be ignored. (RFC7540 4.1, RFC7540 6.7)"),
  959. {ok, Socket} = do_handshake(Config),
  960. %% Send a PING frame with unknown flags.
  961. ok = gen_tcp:send(Socket, << 8:24, 6:8,
  962. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  963. 0:1, %% ACK.
  964. 0:32, 0:64 >>),
  965. %% Receive a PING ACK in return.
  966. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  967. ok.
  968. ignore_goaway_unknown_flags(Config) ->
  969. doc("Undefined GOAWAY frame flags must be ignored. (RFC7540 4.1, RFC7540 6.8)"),
  970. {ok, Socket} = do_handshake(Config),
  971. %% Send a GOAWAY frame with unknown flags.
  972. ok = gen_tcp:send(Socket, << 8:24, 7:8,
  973. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  974. 0:32, 0:64 >>),
  975. %% Receive a GOAWAY frame back.
  976. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  977. ok.
  978. ignore_window_update_unknown_flags(Config) ->
  979. doc("Undefined WINDOW_UPDATE frame flags must be ignored. (RFC7540 4.1, RFC7540 6.9)"),
  980. {ok, Socket} = do_handshake(Config),
  981. %% Send a WINDOW_UPDATE frame with unknown flags.
  982. ok = gen_tcp:send(Socket, << 4:24, 8:8,
  983. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  984. 0:32, 1000:32 >>),
  985. %% We expect no errors or replies, therefore we send a PING frame.
  986. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  987. %% And receive a PING ACK in return.
  988. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  989. ok.
  990. ignore_continuation_unknown_flags(Config) ->
  991. doc("Undefined CONTINUATION frame flags must be ignored. (RFC7540 4.1, RFC7540 6.10)"),
  992. {ok, Socket} = do_handshake(Config),
  993. %% Send a POST request with a CONTINUATION frame with unknown flags.
  994. {HeadersBlock, _} = cow_hpack:encode([
  995. {<<":method">>, <<"POST">>},
  996. {<<":scheme">>, <<"http">>},
  997. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  998. {<<":path">>, <<"/echo/read_body">>}
  999. ]),
  1000. Len = iolist_size(HeadersBlock),
  1001. ok = gen_tcp:send(Socket, [
  1002. << 0:24, 1:8, 0:8, 0:1, 1:31 >>,
  1003. << Len:24, 9:8,
  1004. 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  1005. 1:1, %% END_HEADERS.
  1006. 1:1, 1:1, %% Undefined.
  1007. 0:1, 1:31 >>,
  1008. HeadersBlock,
  1009. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  1010. ]),
  1011. %% Receive a response with the same DATA frame.
  1012. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1013. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1014. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1015. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1016. ok.
  1017. %% @todo Flags that have no defined semantics for
  1018. %% a particular frame type MUST be left unset (0x0) when sending. (RFC7540 4.1)
  1019. ignore_data_reserved_bit(Config) ->
  1020. doc("Reserved 1-bit field of DATA frame must be ignored. (RFC7540 4.1, RFC7540 6.1)"),
  1021. {ok, Socket} = do_handshake(Config),
  1022. %% Send a POST request with a DATA frame with the reserved bit set.
  1023. {HeadersBlock, _} = cow_hpack:encode([
  1024. {<<":method">>, <<"POST">>},
  1025. {<<":scheme">>, <<"http">>},
  1026. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1027. {<<":path">>, <<"/echo/read_body">>}
  1028. ]),
  1029. ok = gen_tcp:send(Socket, [
  1030. cow_http2:headers(1, nofin, HeadersBlock),
  1031. << 100:24, 0:8, 0:7, 1:1,
  1032. 1:1, %% Reserved bit.
  1033. 1:31, 0:100/unit:8 >>
  1034. ]),
  1035. %% Receive a response with the same DATA frame.
  1036. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1037. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1038. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1039. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1040. ok.
  1041. ignore_headers_reserved_bit(Config) ->
  1042. doc("Reserved 1-bit field of HEADERS frame must be ignored. (RFC7540 4.1, RFC7540 6.2)"),
  1043. {ok, Socket} = do_handshake(Config),
  1044. %% Send a POST request with a HEADERS frame with the reserved bit set.
  1045. {HeadersBlock, _} = cow_hpack:encode([
  1046. {<<":method">>, <<"POST">>},
  1047. {<<":scheme">>, <<"http">>},
  1048. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1049. {<<":path">>, <<"/echo/read_body">>}
  1050. ]),
  1051. Len = iolist_size(HeadersBlock),
  1052. ok = gen_tcp:send(Socket, [
  1053. << Len:24, 1:8, 0:5, 1:1, 0:2,
  1054. 1:1, %% Reserved bit.
  1055. 1:31 >>,
  1056. HeadersBlock,
  1057. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  1058. ]),
  1059. %% Receive a response with the same DATA frame.
  1060. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1061. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1062. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1063. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1064. ok.
  1065. ignore_priority_reserved_bit(Config) ->
  1066. doc("Reserved 1-bit field of PRIORITY frame must be ignored. (RFC7540 4.1, RFC7540 6.3)"),
  1067. {ok, Socket} = do_handshake(Config),
  1068. %% Send a POST request with an interleaved PRIORITY frame with the reserved bit set.
  1069. {HeadersBlock, _} = cow_hpack:encode([
  1070. {<<":method">>, <<"POST">>},
  1071. {<<":scheme">>, <<"http">>},
  1072. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1073. {<<":path">>, <<"/echo/read_body">>}
  1074. ]),
  1075. ok = gen_tcp:send(Socket, [
  1076. cow_http2:headers(1, nofin, HeadersBlock),
  1077. << 5:24, 2:8, 0:8,
  1078. 1:1, %% Reserved bit.
  1079. 1:31, 0:1, 3:31, 0:8 >>,
  1080. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  1081. ]),
  1082. %% Receive a response with the same DATA frame.
  1083. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1084. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1085. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1086. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1087. ok.
  1088. ignore_rst_stream_reserved_bit(Config) ->
  1089. doc("Reserved 1-bit field of RST_STREAM frame must be ignored. (RFC7540 4.1, RFC7540 6.4)"),
  1090. {ok, Socket} = do_handshake(Config),
  1091. %% Send a POST request then cancel it with an RST_STREAM frame with the reserved bit set.
  1092. {HeadersBlock, _} = cow_hpack:encode([
  1093. {<<":method">>, <<"POST">>},
  1094. {<<":scheme">>, <<"http">>},
  1095. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1096. {<<":path">>, <<"/echo/read_body">>}
  1097. ]),
  1098. ok = gen_tcp:send(Socket, [
  1099. cow_http2:headers(1, nofin, HeadersBlock),
  1100. << 4:24, 3:8, 0:8,
  1101. 1:1, %% Reserved bit.
  1102. 1:31, 8:32 >>,
  1103. cow_http2:headers(3, nofin, HeadersBlock),
  1104. cow_http2:data(3, fin, << 0:100/unit:8 >>)
  1105. ]),
  1106. %% Receive a response with the same DATA frame.
  1107. {ok, << SkipLen:24, 1:8, _:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1108. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1109. {ok, << 100:24, 0:8, 1:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1110. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1111. ok.
  1112. ignore_settings_reserved_bit(Config) ->
  1113. doc("Reserved 1-bit field of SETTINGS frame must be ignored. (RFC7540 4.1, RFC7540 6.5)"),
  1114. {ok, Socket} = do_handshake(Config),
  1115. %% Send a SETTINGS frame with the reserved bit set.
  1116. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:8,
  1117. 1:1, %% Reserved bit.
  1118. 0:31, 2:16, 0:32 >>),
  1119. %% Receive a SETTINGS ack.
  1120. {ok, << 0:24, 4:8, 0:7, 1:1, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1121. ok.
  1122. ignore_push_promise_reserved_bit(Config) ->
  1123. doc("Reserved 1-bit field of PUSH_PROMISE frame must be ignored. (RFC7540 4.1, RFC7540 6.6)"),
  1124. {ok, Socket} = do_handshake(Config),
  1125. %% Send a PUSH_PROMISE frame with the reserved bit set.
  1126. ok = gen_tcp:send(Socket, << 4:24, 5:8, 0:5, 1:1, 0:2,
  1127. 1:1, %% Reserved bit.
  1128. 1:31, 0:1, 3:31 >>
  1129. ),
  1130. %% Receive a PROTOCOL_ERROR connection error.
  1131. %%
  1132. %% Note that it is not possible to distinguish between the expected
  1133. %% result and the server rejecting PUSH_PROMISE frames.
  1134. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1135. ok.
  1136. ignore_ping_reserved_bit(Config) ->
  1137. doc("Reserved 1-bit field of PING frame must be ignored. (RFC7540 4.1, RFC7540 6.7)"),
  1138. {ok, Socket} = do_handshake(Config),
  1139. %% Send a PING frame with the reserved bit set.
  1140. ok = gen_tcp:send(Socket, << 8:24, 6:8, 0:8,
  1141. 1:1, %% Reserved bit.
  1142. 0:31, 0:64 >>),
  1143. %% Receive a PING ACK in return.
  1144. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  1145. ok.
  1146. ignore_goaway_reserved_bit(Config) ->
  1147. doc("Reserved 1-bit field of GOAWAY frame must be ignored. (RFC7540 4.1, RFC7540 6.8)"),
  1148. {ok, Socket} = do_handshake(Config),
  1149. %% Send a GOAWAY frame with the reserved bit set.
  1150. ok = gen_tcp:send(Socket, << 8:24, 7:8, 0:8,
  1151. 1:1, %% Reserved bit.
  1152. 0:31, 0:64 >>),
  1153. %% Receive a GOAWAY frame back.
  1154. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1155. ok.
  1156. ignore_window_update_reserved_bit(Config) ->
  1157. doc("Reserved 1-bit field of WINDOW_UPDATE frame must be ignored. (RFC7540 4.1, RFC7540 6.9)"),
  1158. {ok, Socket} = do_handshake(Config),
  1159. %% Send a WINDOW_UPDATE frame with the reserved bit set.
  1160. ok = gen_tcp:send(Socket, << 4:24, 8:8, 0:8,
  1161. 1:1, %% Reserved bit.
  1162. 0:31, 1000:32 >>),
  1163. %% We expect no errors or replies, therefore we send a PING frame.
  1164. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  1165. %% And receive a PING ACK in return.
  1166. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  1167. ok.
  1168. ignore_continuation_reserved_bit(Config) ->
  1169. doc("Reserved 1-bit field of CONTINUATION frame must be ignored. (RFC7540 4.1, RFC7540 6.10)"),
  1170. {ok, Socket} = do_handshake(Config),
  1171. %% Send a POST request with a CONTINUATION frame with the reserved bit set.
  1172. {HeadersBlock, _} = cow_hpack:encode([
  1173. {<<":method">>, <<"POST">>},
  1174. {<<":scheme">>, <<"http">>},
  1175. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1176. {<<":path">>, <<"/echo/read_body">>}
  1177. ]),
  1178. Len = iolist_size(HeadersBlock),
  1179. ok = gen_tcp:send(Socket, [
  1180. << 0:24, 1:8, 0:8, 0:1, 1:31 >>,
  1181. << Len:24, 9:8, 0:5, 1:1, 0:2,
  1182. 1:1, %% Reserved bit.
  1183. 1:31 >>,
  1184. HeadersBlock,
  1185. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  1186. ]),
  1187. %% Receive a response with the same DATA frame.
  1188. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1189. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1190. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1191. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1192. ok.
  1193. %% @todo The reserved 1-bit field MUST remain unset (0x0) when sending. (RFC7540 4.1)
  1194. %% Frame size.
  1195. max_frame_size_allow_exactly_default(Config) ->
  1196. doc("All implementations must allow frame sizes of at least 16384. (RFC7540 4.1, RFC7540 4.2)"),
  1197. {ok, Socket} = do_handshake(Config),
  1198. %% Send a POST request with a DATA frame of exactly 16384 bytes.
  1199. {HeadersBlock, _} = cow_hpack:encode([
  1200. {<<":method">>, <<"POST">>},
  1201. {<<":scheme">>, <<"http">>},
  1202. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1203. {<<":path">>, <<"/echo/read_body">>}
  1204. ]),
  1205. ok = gen_tcp:send(Socket, [
  1206. cow_http2:headers(1, nofin, HeadersBlock),
  1207. cow_http2:data(1, fin, << 0:16384/unit:8 >>)
  1208. ]),
  1209. %% Receive a response with the same DATA frame.
  1210. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of
  1211. %% We received a WINDOW_UPDATE first. Skip it and the next.
  1212. {ok, <<4:24, 8:8, 0:40>>} ->
  1213. {ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000),
  1214. gen_tcp:recv(Socket, 9, 1000);
  1215. Res ->
  1216. Res
  1217. end,
  1218. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1219. {ok, << 16384:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1220. {ok, << 0:16384/unit:8 >>} = gen_tcp:recv(Socket, 16384, 1000),
  1221. ok.
  1222. max_frame_size_reject_larger_than_default(Config) ->
  1223. doc("A FRAME_SIZE_ERROR connection error must be sent when receiving "
  1224. "frames larger than the default 16384 length. (RFC7540 4.1, RFC7540 4.2)"),
  1225. {ok, Socket} = do_handshake(Config),
  1226. %% Send a POST request with a DATA frame larger than 16384 bytes.
  1227. {HeadersBlock, _} = cow_hpack:encode([
  1228. {<<":method">>, <<"POST">>},
  1229. {<<":scheme">>, <<"http">>},
  1230. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1231. {<<":path">>, <<"/echo/read_body">>}
  1232. ]),
  1233. ok = gen_tcp:send(Socket, [
  1234. cow_http2:headers(1, nofin, HeadersBlock),
  1235. cow_http2:data(1, fin, << 0:16385/unit:8 >>)
  1236. ]),
  1237. %% Receive a FRAME_SIZE_ERROR connection error.
  1238. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1239. ok.
  1240. max_frame_size_allow_exactly_custom(Config0) ->
  1241. doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must allow frames "
  1242. "of up to that size. (RFC7540 4.2, RFC7540 6.5.2)"),
  1243. %% Create a new listener that sets the maximum frame size to 30000.
  1244. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  1245. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  1246. max_frame_size_received => 30000
  1247. }, Config0),
  1248. try
  1249. %% Do the handshake.
  1250. {ok, Socket} = do_handshake(Config),
  1251. %% Send a HEADERS frame initiating a stream followed by
  1252. %% a single 30000 bytes DATA frame.
  1253. Headers = [
  1254. {<<":method">>, <<"POST">>},
  1255. {<<":scheme">>, <<"http">>},
  1256. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1257. {<<":path">>, <<"/long_polling">>}
  1258. ],
  1259. {HeadersBlock, _} = cow_hpack:encode(Headers),
  1260. ok = gen_tcp:send(Socket, [
  1261. cow_http2:headers(1, nofin, HeadersBlock),
  1262. cow_http2:data(1, fin, <<0:30000/unit:8>>)
  1263. ]),
  1264. %% Receive a proper response.
  1265. {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1266. {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
  1267. %% No errors follow due to our sending of a 25000 bytes frame.
  1268. {error, timeout} = gen_tcp:recv(Socket, 0, 1000)
  1269. after
  1270. cowboy:stop_listener(?FUNCTION_NAME)
  1271. end.
  1272. max_frame_size_reject_larger_than_custom(Config0) ->
  1273. doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must reject frames "
  1274. "of up to that size with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5.2)"),
  1275. %% Create a new listener that sets the maximum frame size to 30000.
  1276. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  1277. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  1278. max_frame_size_received => 30000
  1279. }, Config0),
  1280. try
  1281. %% Do the handshake.
  1282. {ok, Socket} = do_handshake(Config),
  1283. %% Send a HEADERS frame initiating a stream followed by
  1284. %% a single DATA frame larger than 30000 bytes.
  1285. Headers = [
  1286. {<<":method">>, <<"POST">>},
  1287. {<<":scheme">>, <<"http">>},
  1288. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1289. {<<":path">>, <<"/long_polling">>}
  1290. ],
  1291. {HeadersBlock, _} = cow_hpack:encode(Headers),
  1292. ok = gen_tcp:send(Socket, [
  1293. cow_http2:headers(1, nofin, HeadersBlock),
  1294. cow_http2:data(1, fin, <<0:30001/unit:8>>)
  1295. ]),
  1296. %% Receive a FRAME_SIZE_ERROR connection error.
  1297. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000)
  1298. after
  1299. cowboy:stop_listener(?FUNCTION_NAME)
  1300. end.
  1301. %% I am using FRAME_SIZE_ERROR here because the information in the
  1302. %% frame header tells us this frame is at least 1 byte long, while
  1303. %% the given length is smaller; i.e. it is too small to contain
  1304. %% mandatory frame data (the pad length).
  1305. data_reject_frame_size_0_padded_flag(Config) ->
  1306. doc("DATA frames of size 0 with the PADDED flag set must be rejected "
  1307. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.1)"),
  1308. {ok, Socket} = do_handshake(Config),
  1309. %% Send a POST request with an incorrect padded DATA frame size.
  1310. {HeadersBlock, _} = cow_hpack:encode([
  1311. {<<":method">>, <<"POST">>},
  1312. {<<":scheme">>, <<"http">>},
  1313. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1314. {<<":path">>, <<"/echo/read_body">>}
  1315. ]),
  1316. ok = gen_tcp:send(Socket, [
  1317. cow_http2:headers(1, nofin, HeadersBlock),
  1318. << 0:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>
  1319. ]),
  1320. %% Receive a FRAME_SIZE_ERROR connection error.
  1321. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1322. ok.
  1323. %% This case on the other hand is noted specifically in the RFC
  1324. %% as being a PROTOCOL_ERROR. It can be thought of as the Pad Length
  1325. %% being incorrect, rather than the frame size.
  1326. data_reject_frame_size_too_small_padded_flag(Config) ->
  1327. doc("DATA frames with Pad Length >= Length must be rejected "
  1328. "with a PROTOCOL_ERROR connection error. (RFC7540 6.1)"),
  1329. {ok, Socket} = do_handshake(Config),
  1330. %% Send a POST request with an incorrect padded DATA frame size.
  1331. {HeadersBlock, _} = cow_hpack:encode([
  1332. {<<":method">>, <<"POST">>},
  1333. {<<":scheme">>, <<"http">>},
  1334. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1335. {<<":path">>, <<"/echo/read_body">>}
  1336. ]),
  1337. ok = gen_tcp:send(Socket, [
  1338. cow_http2:headers(1, nofin, HeadersBlock),
  1339. << 10:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>
  1340. ]),
  1341. %% Receive a PROTOCOL_ERROR connection error.
  1342. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1343. ok.
  1344. headers_reject_frame_size_0_padded_flag(Config) ->
  1345. doc("HEADERS frames of size 0 with the PADDED flag set must be rejected "
  1346. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  1347. {ok, Socket} = do_handshake(Config),
  1348. %% Send a padded HEADERS frame with an incorrect size.
  1349. ok = gen_tcp:send(Socket, << 0:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>),
  1350. %% Receive a FRAME_SIZE_ERROR connection error.
  1351. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1352. ok.
  1353. headers_reject_frame_size_too_small_padded_flag(Config) ->
  1354. doc("HEADERS frames with no priority flag and Pad Length >= Length "
  1355. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 6.2)"),
  1356. {ok, Socket} = do_handshake(Config),
  1357. %% Send a padded HEADERS frame with an incorrect size.
  1358. ok = gen_tcp:send(Socket, << 10:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>),
  1359. %% Receive a PROTOCOL_ERROR connection error.
  1360. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1361. ok.
  1362. headers_reject_frame_size_too_small_priority_flag(Config) ->
  1363. doc("HEADERS frames of size smaller than 5 with the PRIORITY flag set must be rejected "
  1364. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  1365. {ok, Socket} = do_handshake(Config),
  1366. %% Send a HEADERS frame with priority set and an incorrect size.
  1367. ok = gen_tcp:send(Socket, << 4:24, 1:8,
  1368. 0:2, 1:1, 0:4, 1:1, 0:1, 1:31, 0:1, 3:31, 0:8 >>),
  1369. %% Receive a FRAME_SIZE_ERROR connection error.
  1370. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1371. ok.
  1372. headers_reject_frame_size_5_padded_and_priority_flags(Config) ->
  1373. doc("HEADERS frames of size smaller than 6 with the PADDED "
  1374. "and PRIORITY flags set must be rejected "
  1375. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  1376. {ok, Socket} = do_handshake(Config),
  1377. %% Send a padded HEADERS frame with an incorrect size.
  1378. ok = gen_tcp:send(Socket, << 5:24, 1:8,
  1379. 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 0:8, 0:1, 3:31, 0:8 >>),
  1380. %% Receive a FRAME_SIZE_ERROR connection error.
  1381. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1382. ok.
  1383. headers_reject_frame_size_too_small_padded_and_priority_flags(Config) ->
  1384. doc("HEADERS frames of size smaller than Length+6 with the PADDED and PRIORITY flags set "
  1385. "must be rejected with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  1386. {ok, Socket} = do_handshake(Config),
  1387. %% Send a padded HEADERS frame with an incorrect size.
  1388. ok = gen_tcp:send(Socket, << 15:24, 1:8,
  1389. 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:1, 3:31, 0:8, 0:80 >>),
  1390. %% Receive a PROTOCOL_ERROR connection error.
  1391. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1392. ok.
  1393. priority_reject_frame_size_too_small(Config) ->
  1394. doc("PRIORITY frames of size smaller than 5 must be rejected "
  1395. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  1396. {ok, Socket} = do_handshake(Config),
  1397. %% Send a PRIORITY frame with an incorrect size.
  1398. ok = gen_tcp:send(Socket, << 4:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:8 >>),
  1399. %% Receive a FRAME_SIZE_ERROR stream error.
  1400. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1401. ok.
  1402. priority_reject_frame_size_too_large(Config) ->
  1403. doc("PRIORITY frames of size larger than 5 must be rejected "
  1404. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  1405. {ok, Socket} = do_handshake(Config),
  1406. %% Send a PRIORITY frame with an incorrect size.
  1407. ok = gen_tcp:send(Socket, << 6:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:16 >>),
  1408. %% Receive a FRAME_SIZE_ERROR stream error.
  1409. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1410. ok.
  1411. rst_stream_reject_frame_size_too_small(Config) ->
  1412. doc("RST_STREAM frames of size smaller than 4 must be rejected "
  1413. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  1414. {ok, Socket} = do_handshake(Config),
  1415. %% Send a request and reset it immediately.
  1416. {HeadersBlock, _} = cow_hpack:encode([
  1417. {<<":method">>, <<"GET">>},
  1418. {<<":scheme">>, <<"http">>},
  1419. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1420. {<<":path">>, <<"/">>}
  1421. ]),
  1422. ok = gen_tcp:send(Socket, [
  1423. cow_http2:headers(1, fin, HeadersBlock),
  1424. << 3:24, 3:8, 0:9, 1:31, 8:32 >>
  1425. ]),
  1426. %% Receive a FRAME_SIZE_ERROR connection error.
  1427. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1428. ok.
  1429. rst_stream_reject_frame_size_too_large(Config) ->
  1430. doc("RST_STREAM frames of size larger than 4 must be rejected "
  1431. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  1432. {ok, Socket} = do_handshake(Config),
  1433. %% Send a request and reset it immediately.
  1434. {HeadersBlock, _} = cow_hpack:encode([
  1435. {<<":method">>, <<"GET">>},
  1436. {<<":scheme">>, <<"http">>},
  1437. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1438. {<<":path">>, <<"/">>}
  1439. ]),
  1440. ok = gen_tcp:send(Socket, [
  1441. cow_http2:headers(1, fin, HeadersBlock),
  1442. << 5:24, 3:8, 0:9, 1:31, 8:32 >>
  1443. ]),
  1444. %% Receive a FRAME_SIZE_ERROR connection error.
  1445. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1446. ok.
  1447. settings_reject_bad_frame_size(Config) ->
  1448. doc("SETTINGS frames must have a size multiple of 6 or be rejected "
  1449. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5)"),
  1450. {ok, Socket} = do_handshake(Config),
  1451. %% Send a SETTINGS frame with an incorrect size.
  1452. ok = gen_tcp:send(Socket, << 5:24, 4:8, 0:40, 1:16, 4096:32 >>),
  1453. %% Receive a FRAME_SIZE_ERROR connection error.
  1454. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1455. ok.
  1456. settings_ack_reject_non_empty_frame_size(Config) ->
  1457. doc("SETTINGS frames with the ACK flag set and a non-empty payload "
  1458. "must be rejected with a FRAME_SIZE_ERROR connection error (RFC7540 4.2, RFC7540 6.5)"),
  1459. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  1460. %% Send a valid preface.
  1461. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1462. %% Receive the server preface.
  1463. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  1464. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  1465. %% Send a SETTINGS ack with a payload.
  1466. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:7, 1:1, 0:32, 1:16, 4096:32 >>),
  1467. %% Receive the SETTINGS ack.
  1468. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1469. %% Receive a FRAME_SIZE_ERROR connection error.
  1470. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1471. ok.
  1472. %% Note that clients are not supposed to send PUSH_PROMISE frames.
  1473. %% However when they do, we need to be able to parse it in order
  1474. %% to reject it, and so these errors may still occur.
  1475. push_promise_reject_frame_size_too_small(Config) ->
  1476. doc("PUSH_PROMISE frames of size smaller than 4 must be rejected "
  1477. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1478. {ok, Socket} = do_handshake(Config),
  1479. %% Send a PUSH_PROMISE frame with an incorrect size.
  1480. ok = gen_tcp:send(Socket, << 3:24, 5:8, 0:5, 1:1, 0:3, 1:31, 0:1, 3:31 >>),
  1481. %% Receive a FRAME_SIZE_ERROR connection error.
  1482. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1483. ok.
  1484. push_promise_reject_frame_size_4_padded_flag(Config) ->
  1485. doc("PUSH_PROMISE frames of size smaller than 5 with the PADDED flag set must be rejected "
  1486. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1487. {ok, Socket} = do_handshake(Config),
  1488. %% Send a PUSH_PROMISE frame with an incorrect size.
  1489. ok = gen_tcp:send(Socket, << 4:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 0:1, 0:8, 3:31 >>),
  1490. %% Receive a FRAME_SIZE_ERROR connection error.
  1491. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1492. ok.
  1493. push_promise_reject_frame_size_too_small_padded_flag(Config) ->
  1494. doc("PUSH_PROMISE frames of size smaller than Length+5 with the PADDED flag set "
  1495. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1496. {ok, Socket} = do_handshake(Config),
  1497. %% Send a PUSH_PROMISE frame with an incorrect size.
  1498. {HeadersBlock, _} = cow_hpack:encode([
  1499. {<<":method">>, <<"GET">>},
  1500. {<<":scheme">>, <<"http">>},
  1501. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1502. {<<":path">>, <<"/">>}
  1503. ]),
  1504. Len = 14 + iolist_size(HeadersBlock),
  1505. ok = gen_tcp:send(Socket, [
  1506. << Len:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 10:8, 0:1, 3:31 >>,
  1507. HeadersBlock,
  1508. << 0:80 >>
  1509. ]),
  1510. %% Receive a PROTOCOL_ERROR connection error.
  1511. %%
  1512. %% Note that it is not possible to distinguish between a Pad Length
  1513. %% error and the server rejecting PUSH_PROMISE frames.
  1514. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1515. ok.
  1516. ping_reject_frame_size_too_small(Config) ->
  1517. doc("PING frames of size smaller than 8 must be rejected "
  1518. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1519. {ok, Socket} = do_handshake(Config),
  1520. %% Send a PING frame with an incorrect size.
  1521. ok = gen_tcp:send(Socket, << 7:24, 6:8, 0:40, 0:56 >>),
  1522. %% Receive a FRAME_SIZE_ERROR connection error.
  1523. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1524. ok.
  1525. ping_reject_frame_size_too_large(Config) ->
  1526. doc("PING frames of size larger than 8 must be rejected "
  1527. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1528. {ok, Socket} = do_handshake(Config),
  1529. %% Send a PING frame with an incorrect size.
  1530. ok = gen_tcp:send(Socket, << 9:24, 6:8, 0:40, 0:72 >>),
  1531. %% Receive a FRAME_SIZE_ERROR connection error.
  1532. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1533. ok.
  1534. goaway_reject_frame_size_too_small(Config) ->
  1535. doc("GOAWAY frames of size smaller than 8 must be rejected "
  1536. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.8)"),
  1537. {ok, Socket} = do_handshake(Config),
  1538. %% Send a GOAWAY frame with an incorrect size.
  1539. ok = gen_tcp:send(Socket, << 7:24, 7:8, 0:40, 0:56 >>),
  1540. %% Receive a FRAME_SIZE_ERROR connection error.
  1541. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1542. ok.
  1543. goaway_allow_frame_size_too_large(Config) ->
  1544. doc("GOAWAY frames of size larger than 8 must be allowed. (RFC7540 6.8)"),
  1545. {ok, Socket} = do_handshake(Config),
  1546. %% Send a GOAWAY frame with debug data.
  1547. ok = gen_tcp:send(Socket, << 12:24, 7:8, 0:40, 0:64, 99999:32 >>),
  1548. %% Receive a GOAWAY frame back.
  1549. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1550. ok.
  1551. window_update_reject_frame_size_too_small(Config) ->
  1552. doc("WINDOW_UPDATE frames of size smaller than 4 must be rejected "
  1553. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1554. {ok, Socket} = do_handshake(Config),
  1555. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1556. ok = gen_tcp:send(Socket, << 3:24, 8:8, 0:40, 1000:24 >>),
  1557. %% Receive a FRAME_SIZE_ERROR connection error.
  1558. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1559. ok.
  1560. window_update_reject_frame_size_too_large(Config) ->
  1561. doc("WINDOW_UPDATE frames of size larger than 4 must be rejected "
  1562. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1563. {ok, Socket} = do_handshake(Config),
  1564. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1565. ok = gen_tcp:send(Socket, << 5:24, 8:8, 0:40, 1000:40 >>),
  1566. %% Receive a FRAME_SIZE_ERROR connection error.
  1567. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1568. ok.
  1569. %% Note: There is no particular limits on the size of CONTINUATION frames,
  1570. %% they can go from 0 to SETTINGS_MAX_FRAME_SIZE.
  1571. %% Header compression and decompression.
  1572. headers_compression_error(Config) ->
  1573. doc("A decoding error in a HEADERS frame's header block must be rejected "
  1574. "with a COMPRESSION_ERROR connection error. (RFC7540 4.3, RFC7540 6.2)"),
  1575. {ok, Socket} = do_handshake(Config),
  1576. %% Send a HEADERS frame with an invalid header block.
  1577. ok = gen_tcp:send(Socket, << 10:24, 1:8, 0:5, 1:1, 0:1, 1:1, 0:1, 1:31, 0:10/unit:8 >>),
  1578. %% Receive a COMPRESSION_ERROR connection error.
  1579. {ok, << _:24, 7:8, _:72, 9:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1580. ok.
  1581. continuation_compression_error(Config) ->
  1582. doc("A decoding error in a CONTINUATION frame's header block must be rejected "
  1583. "with a COMPRESSION_ERROR connection error. (RFC7540 4.3, RFC7540 6.10)"),
  1584. {ok, Socket} = do_handshake(Config),
  1585. %% Send a CONTINUATION frame with an invalid header block.
  1586. ok = gen_tcp:send(Socket, [
  1587. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  1588. << 10:24, 9:8, 0:5, 1:1, 0:3, 1:31, 0:10/unit:8 >>
  1589. ]),
  1590. %% Receive a COMPRESSION_ERROR connection error.
  1591. {ok, << _:24, 7:8, _:72, 9:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1592. ok.
  1593. continuation_with_frame_interleaved_error(Config) ->
  1594. doc("Frames interleaved in a header block must be rejected "
  1595. "with a PROTOCOL_ERROR connection error. (RFC7540 4.3, RFC7540 6.2, RFC7540 6.10)"),
  1596. {ok, Socket} = do_handshake(Config),
  1597. %% Send an unterminated HEADERS frame followed by a PING frame.
  1598. ok = gen_tcp:send(Socket, [
  1599. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  1600. cow_http2:ping(0)
  1601. ]),
  1602. %% Receive a PROTOCOL_ERROR connection error.
  1603. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1604. ok.
  1605. continuation_wrong_stream_error(Config) ->
  1606. doc("CONTINUATION frames with an incorrect stream identifier must be rejected "
  1607. "with a PROTOCOL_ERROR connection error. (RFC7540 4.3, RFC7540 6.2)"),
  1608. {ok, Socket} = do_handshake(Config),
  1609. %% Send an unterminated HEADERS frame followed by a CONTINUATION frame for another stream.
  1610. ok = gen_tcp:send(Socket, [
  1611. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  1612. << 0:24, 9:8, 0:9, 3:31 >>
  1613. ]),
  1614. %% Receive a PROTOCOL_ERROR connection error.
  1615. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1616. ok.
  1617. %% Stream states.
  1618. idle_stream_reject_data(Config) ->
  1619. doc("DATA frames received on an idle stream must be rejected "
  1620. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1, RFC7540 6.1)"),
  1621. {ok, Socket} = do_handshake(Config),
  1622. %% Send a DATA frame on an idle stream.
  1623. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1624. %% Receive a PROTOCOL_ERROR connection error.
  1625. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1626. ok.
  1627. idle_stream_accept_headers(Config) ->
  1628. doc("HEADERS frames received on an idle stream must be accepted. (RFC7540 5.1)"),
  1629. {ok, Socket} = do_handshake(Config),
  1630. %% Send a HEADERS frame on an idle stream.
  1631. {HeadersBlock, _} = cow_hpack:encode([
  1632. {<<":method">>, <<"GET">>},
  1633. {<<":scheme">>, <<"http">>},
  1634. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1635. {<<":path">>, <<"/">>}
  1636. ]),
  1637. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1638. %% Receive a HEADERS frame as a response.
  1639. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1640. ok.
  1641. idle_stream_accept_priority(Config) ->
  1642. doc("PRIORITY frames received on an idle stream must be accepted. (RFC7540 5.1)"),
  1643. {ok, Socket} = do_handshake(Config),
  1644. %% Send a PRIORITY frame on an idle stream.
  1645. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1646. %% Receive no error.
  1647. {error, timeout} = gen_tcp:recv(Socket, 7, 1000),
  1648. %% Send a HEADERS frame on the same stream.
  1649. {HeadersBlock, _} = cow_hpack:encode([
  1650. {<<":method">>, <<"GET">>},
  1651. {<<":scheme">>, <<"http">>},
  1652. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1653. {<<":path">>, <<"/">>}
  1654. ]),
  1655. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1656. %% Receive a HEADERS frame as a response.
  1657. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1658. ok.
  1659. idle_stream_reject_rst_stream(Config) ->
  1660. doc("RST_STREAM frames received on an idle stream must be rejected "
  1661. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
  1662. {ok, Socket} = do_handshake(Config),
  1663. %% Send an RST_STREAM frame on an idle stream.
  1664. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, no_error)),
  1665. %% Receive a PROTOCOL_ERROR connection error.
  1666. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1667. ok.
  1668. idle_stream_reject_push_promise(Config) ->
  1669. doc("PUSH_PROMISE frames received on an idle stream must be rejected "
  1670. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
  1671. {ok, Socket} = do_handshake(Config),
  1672. %% Send a PUSH_PROMISE frame on an idle stream.
  1673. {HeadersBlock, _} = cow_hpack:encode([
  1674. {<<":method">>, <<"GET">>},
  1675. {<<":scheme">>, <<"http">>},
  1676. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1677. {<<":path">>, <<"/">>}
  1678. ]),
  1679. ok = gen_tcp:send(Socket, cow_http2:push_promise(1, 3, HeadersBlock)),
  1680. %% Receive a PROTOCOL_ERROR connection error.
  1681. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1682. ok.
  1683. idle_stream_reject_window_update(Config) ->
  1684. doc("WINDOW_UPDATE frames received on an idle stream must be rejected "
  1685. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
  1686. {ok, Socket} = do_handshake(Config),
  1687. %% Send a WINDOW_UPDATE frame on an idle stream.
  1688. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1689. %% Receive a PROTOCOL_ERROR connection error.
  1690. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1691. ok.
  1692. %reserved (local) - after sending PUSH_PROMISE:
  1693. % An endpoint MUST NOT send any type of frame other than HEADERS,
  1694. % RST_STREAM, or PRIORITY in this state.
  1695. %%% how to test this?
  1696. %
  1697. % A PRIORITY or WINDOW_UPDATE frame MAY be received in this state.
  1698. % Receiving any type of frame other than RST_STREAM, PRIORITY, or
  1699. % WINDOW_UPDATE on a stream in this state MUST be treated as a
  1700. % connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  1701. %%% we need to use a large enough file for this
  1702. %
  1703. %reserved_local_reject_data
  1704. %reserved_local_reject_headers
  1705. %reserved_local_accept_priority
  1706. %reserved_local_accept_rst_stream
  1707. %reserved_local_reject_push_promise %% do we even care? we reject it always
  1708. %reserved_local_accept_window_update
  1709. %
  1710. %half-closed (remote):
  1711. % If an endpoint receives additional frames, other than
  1712. % WINDOW_UPDATE, PRIORITY, or RST_STREAM, for a stream that is in
  1713. % this state, it MUST respond with a stream error (Section 5.4.2) of
  1714. % type STREAM_CLOSED.
  1715. half_closed_remote_reject_data(Config) ->
  1716. doc("DATA frames received on a half-closed (remote) stream must be rejected "
  1717. "with a STREAM_CLOSED stream error. (RFC7540 5.1, RFC7540 6.1)"),
  1718. {ok, Socket} = do_handshake(Config),
  1719. %% Send a HEADERS frame with the FIN flag set.
  1720. {HeadersBlock, _} = cow_hpack:encode([
  1721. {<<":method">>, <<"GET">>},
  1722. {<<":scheme">>, <<"http">>},
  1723. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1724. {<<":path">>, <<"/long_polling">>}
  1725. ]),
  1726. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1727. %% Send a DATA frame on that now half-closed (remote) stream.
  1728. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1729. %% Receive a STREAM_CLOSED stream error.
  1730. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1731. ok.
  1732. %% We reject all invalid HEADERS with a connection error because
  1733. %% we do not want to waste resources decoding them.
  1734. half_closed_remote_reject_headers(Config) ->
  1735. doc("HEADERS frames received on a half-closed (remote) stream must be rejected "
  1736. "with a STREAM_CLOSED connection error. (RFC7540 4.3, RFC7540 5.1)"),
  1737. {ok, Socket} = do_handshake(Config),
  1738. %% Send a HEADERS frame with the FIN flag set.
  1739. {HeadersBlock, _} = cow_hpack:encode([
  1740. {<<":method">>, <<"GET">>},
  1741. {<<":scheme">>, <<"http">>},
  1742. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1743. {<<":path">>, <<"/long_polling">>}
  1744. ]),
  1745. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1746. %% Send a HEADERS frame on that now half-closed (remote) stream.
  1747. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1748. %% Receive a STREAM_CLOSED connection error.
  1749. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1750. ok.
  1751. half_closed_remote_accept_priority(Config) ->
  1752. doc("PRIORITY frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1753. {ok, Socket} = do_handshake(Config),
  1754. %% Send a HEADERS frame with the FIN flag set.
  1755. {HeadersBlock, _} = cow_hpack:encode([
  1756. {<<":method">>, <<"GET">>},
  1757. {<<":scheme">>, <<"http">>},
  1758. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1759. {<<":path">>, <<"/long_polling">>}
  1760. ]),
  1761. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1762. %% Send a PRIORITY frame on that now half-closed (remote) stream.
  1763. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1764. %% Receive a HEADERS frame as a response.
  1765. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1766. ok.
  1767. half_closed_remote_accept_rst_stream(Config) ->
  1768. doc("RST_STREAM frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1769. {ok, Socket} = do_handshake(Config),
  1770. %% Send a HEADERS frame with the FIN flag set.
  1771. {HeadersBlock, _} = cow_hpack:encode([
  1772. {<<":method">>, <<"GET">>},
  1773. {<<":scheme">>, <<"http">>},
  1774. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1775. {<<":path">>, <<"/long_polling">>}
  1776. ]),
  1777. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1778. %% Send an RST_STREAM frame on that now half-closed (remote) stream.
  1779. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, no_error)),
  1780. %% Receive nothing back.
  1781. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1782. ok.
  1783. %% half_closed_remote_reject_push_promise
  1784. %%
  1785. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1786. %% because PUSH is disabled in that direction. We therefore cannot test other
  1787. %% error conditions.
  1788. half_closed_remote_accept_window_update(Config) ->
  1789. doc("WINDOW_UPDATE frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1790. {ok, Socket} = do_handshake(Config),
  1791. %% Send a HEADERS frame with the FIN flag set.
  1792. {HeadersBlock, _} = cow_hpack:encode([
  1793. {<<":method">>, <<"GET">>},
  1794. {<<":scheme">>, <<"http">>},
  1795. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1796. {<<":path">>, <<"/long_polling">>}
  1797. ]),
  1798. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1799. %% Send a WINDOW_UPDATE frame on that now half-closed (remote) stream.
  1800. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1801. %% Receive a HEADERS frame as a response.
  1802. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1803. ok.
  1804. %% We reject DATA frames sent on closed streams with a STREAM_CLOSED
  1805. %% connection error regardless of how the stream was closed to simplify
  1806. %% the implementation. This excludes the few frames we ignore from
  1807. %% lingering streams that we canceled.
  1808. rst_stream_closed_reject_data(Config) ->
  1809. doc("DATA frames received on a stream closed via RST_STREAM must be rejected "
  1810. "with a STREAM_CLOSED connection error. (RFC7540 5.1, RFC7540 6.1)"),
  1811. {ok, Socket} = do_handshake(Config),
  1812. %% Send a HEADERS frame.
  1813. {HeadersBlock, _} = cow_hpack:encode([
  1814. {<<":method">>, <<"GET">>},
  1815. {<<":scheme">>, <<"http">>},
  1816. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1817. {<<":path">>, <<"/long_polling">>}
  1818. ]),
  1819. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1820. %% Send an RST_STREAM frame to close the stream.
  1821. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1822. %% Send a DATA frame on the now RST_STREAM closed stream.
  1823. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1824. %% Receive a STREAM_CLOSED connection error.
  1825. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1826. ok.
  1827. %% We reject all invalid HEADERS with a connection error because
  1828. %% we do not want to waste resources decoding them.
  1829. rst_stream_closed_reject_headers(Config) ->
  1830. doc("HEADERS frames received on a stream closed via RST_STREAM must be rejected "
  1831. "with a STREAM_CLOSED connection error. (RFC7540 4.3, RFC7540 5.1)"),
  1832. {ok, Socket} = do_handshake(Config),
  1833. %% Send a HEADERS frame.
  1834. {HeadersBlock, _} = cow_hpack:encode([
  1835. {<<":method">>, <<"GET">>},
  1836. {<<":scheme">>, <<"http">>},
  1837. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1838. {<<":path">>, <<"/long_polling">>}
  1839. ]),
  1840. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1841. %% Send an RST_STREAM frame to close the stream.
  1842. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1843. %% Send a HEADERS frame on the now RST_STREAM closed stream.
  1844. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1845. %% Receive a STREAM_CLOSED connection error.
  1846. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1847. ok.
  1848. rst_stream_closed_accept_priority(Config) ->
  1849. doc("PRIORITY frames received on a stream closed via RST_STREAM "
  1850. "must be accepted. (RFC7540 5.1)"),
  1851. {ok, Socket} = do_handshake(Config),
  1852. %% Send a HEADERS frame.
  1853. {HeadersBlock, _} = cow_hpack:encode([
  1854. {<<":method">>, <<"GET">>},
  1855. {<<":scheme">>, <<"http">>},
  1856. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1857. {<<":path">>, <<"/long_polling">>}
  1858. ]),
  1859. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1860. %% Send an RST_STREAM frame to close the stream.
  1861. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1862. %% Send a PRIORITY frame on that now RST_STREAM closed stream.
  1863. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1864. %% Receive nothing back.
  1865. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1866. ok.
  1867. rst_stream_closed_ignore_rst_stream(Config) ->
  1868. doc("RST_STREAM frames received on a stream closed via RST_STREAM "
  1869. "must be ignored to avoid looping. (RFC7540 5.1, RFC7540 5.4.2)"),
  1870. {ok, Socket} = do_handshake(Config),
  1871. %% Send a HEADERS frame.
  1872. {HeadersBlock, _} = cow_hpack:encode([
  1873. {<<":method">>, <<"GET">>},
  1874. {<<":scheme">>, <<"http">>},
  1875. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1876. {<<":path">>, <<"/long_polling">>}
  1877. ]),
  1878. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1879. %% Send an RST_STREAM frame to close the stream.
  1880. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1881. %% Send an extra RST_STREAM.
  1882. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1883. %% Receive nothing back.
  1884. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1885. ok.
  1886. %% rst_stream_closed_reject_push_promise
  1887. %%
  1888. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1889. %% because PUSH is disabled in that direction. We therefore cannot test other
  1890. %% error conditions.
  1891. rst_stream_closed_reject_window_update(Config) ->
  1892. doc("WINDOW_UPDATE frames received on a stream closed via RST_STREAM "
  1893. "must be rejected with a STREAM_CLOSED stream error. (RFC7540 5.1)"),
  1894. {ok, Socket} = do_handshake(Config),
  1895. %% Send a HEADERS frame.
  1896. {HeadersBlock, _} = cow_hpack:encode([
  1897. {<<":method">>, <<"GET">>},
  1898. {<<":scheme">>, <<"http">>},
  1899. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1900. {<<":path">>, <<"/long_polling">>}
  1901. ]),
  1902. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1903. %% Send an RST_STREAM frame to close the stream.
  1904. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1905. %% Send a WINDOW_UPDATE frame on the now RST_STREAM closed stream.
  1906. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1907. %% Receive a STREAM_CLOSED stream error.
  1908. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1909. ok.
  1910. stream_closed_reject_data(Config) ->
  1911. doc("DATA frames received on a stream closed normally must be rejected "
  1912. "with a STREAM_CLOSED connection error. (RFC7540 5.1, RFC7540 6.1)"),
  1913. {ok, Socket} = do_handshake(Config),
  1914. %% Send a HEADERS frame.
  1915. {HeadersBlock, _} = cow_hpack:encode([
  1916. {<<":method">>, <<"GET">>},
  1917. {<<":scheme">>, <<"http">>},
  1918. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1919. {<<":path">>, <<"/">>}
  1920. ]),
  1921. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1922. %% Receive the response.
  1923. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1924. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1925. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1926. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1927. %% Send a DATA frame on the now closed stream.
  1928. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1929. %% Receive a STREAM_CLOSED connection error.
  1930. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1931. ok.
  1932. stream_closed_reject_headers(Config) ->
  1933. doc("HEADERS frames received on a stream closed normally must be rejected "
  1934. "with a STREAM_CLOSED connection error. (RFC7540 5.1)"),
  1935. {ok, Socket} = do_handshake(Config),
  1936. %% Send a HEADERS frame.
  1937. {HeadersBlock, _} = cow_hpack:encode([
  1938. {<<":method">>, <<"GET">>},
  1939. {<<":scheme">>, <<"http">>},
  1940. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1941. {<<":path">>, <<"/">>}
  1942. ]),
  1943. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1944. %% Receive the response.
  1945. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1946. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1947. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1948. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1949. %% Send a HEADERS frame on the now closed stream.
  1950. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1951. %% Receive a STREAM_CLOSED connection error.
  1952. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1953. ok.
  1954. stream_closed_accept_priority(Config) ->
  1955. doc("PRIORITY frames received on a stream closed normally must be accepted. (RFC7540 5.1)"),
  1956. {ok, Socket} = do_handshake(Config),
  1957. %% Send a HEADERS frame.
  1958. {HeadersBlock, _} = cow_hpack:encode([
  1959. {<<":method">>, <<"GET">>},
  1960. {<<":scheme">>, <<"http">>},
  1961. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1962. {<<":path">>, <<"/">>}
  1963. ]),
  1964. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1965. %% Receive the response.
  1966. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1967. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1968. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1969. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1970. %% Send a PRIORITY frame on the now closed stream.
  1971. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1972. %% Receive nothing back.
  1973. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1974. ok.
  1975. stream_closed_accept_rst_stream(Config) ->
  1976. doc("RST_STREAM frames received on a stream closed normally "
  1977. "must be accepted for a short period. (RFC7540 5.1)"),
  1978. {ok, Socket} = do_handshake(Config),
  1979. %% Send a HEADERS frame.
  1980. {HeadersBlock, _} = cow_hpack:encode([
  1981. {<<":method">>, <<"GET">>},
  1982. {<<":scheme">>, <<"http">>},
  1983. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1984. {<<":path">>, <<"/">>}
  1985. ]),
  1986. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1987. %% Receive the response.
  1988. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1989. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1990. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1991. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1992. %% Send an RST_STREAM frame on the now closed stream.
  1993. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1994. %% Receive nothing back.
  1995. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1996. ok.
  1997. %% stream_closed_reject_push_promise
  1998. %%
  1999. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  2000. %% because PUSH is disabled in that direction. We therefore cannot test other
  2001. %% error conditions.
  2002. stream_closed_accept_window_update(Config) ->
  2003. doc("WINDOW_UPDATE frames received on a stream closed normally "
  2004. "must be accepted for a short period. (RFC7540 5.1)"),
  2005. {ok, Socket} = do_handshake(Config),
  2006. %% Send a HEADERS frame.
  2007. {HeadersBlock, _} = cow_hpack:encode([
  2008. {<<":method">>, <<"GET">>},
  2009. {<<":scheme">>, <<"http">>},
  2010. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2011. {<<":path">>, <<"/">>}
  2012. ]),
  2013. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  2014. %% Receive the response.
  2015. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2016. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  2017. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2018. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  2019. %% Send a WINDOW_UPDATE frame on the now closed stream.
  2020. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  2021. %% Receive nothing back.
  2022. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  2023. ok.
  2024. %% @todo While we accept RST_STREAM and WINDOW_UPDATE for a short period
  2025. %% after the stream closed normally, we may want to reject the ones coming
  2026. %% a significant amount of time after that.
  2027. %% @todo Frames may arrive on a stream after we send an RST_STREAM for it.
  2028. %% They must be ignored for a short period of time:
  2029. %
  2030. % If this state is reached as a result of sending a RST_STREAM
  2031. % frame, the peer that receives the RST_STREAM might have already
  2032. % sent -- or enqueued for sending -- frames on the stream that
  2033. % cannot be withdrawn. An endpoint MUST ignore frames that it
  2034. % receives on closed streams after it has sent a RST_STREAM frame.
  2035. % An endpoint MAY choose to limit the period over which it ignores
  2036. % frames and treat frames that arrive after this time as being in
  2037. % error.
  2038. %% @todo Ensure that rejected DATA frames result in the connection
  2039. %% flow-control window being updated. How to test this?
  2040. %
  2041. % Flow-controlled frames (i.e., DATA) received after sending
  2042. % RST_STREAM are counted toward the connection flow-control window.
  2043. % Even though these frames might be ignored, because they are sent
  2044. % before the sender receives the RST_STREAM, the sender will
  2045. % consider the frames to count against the flow-control window.
  2046. %% Stream identifiers.
  2047. reject_streamid_even(Config) ->
  2048. doc("HEADERS frames received with an even-numbered streamid "
  2049. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  2050. {ok, Socket} = do_handshake(Config),
  2051. %% Send a HEADERS frame with an even-numbered streamid.
  2052. {HeadersBlock, _} = cow_hpack:encode([
  2053. {<<":method">>, <<"GET">>},
  2054. {<<":scheme">>, <<"http">>},
  2055. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2056. {<<":path">>, <<"/">>}
  2057. ]),
  2058. ok = gen_tcp:send(Socket, cow_http2:headers(2, fin, HeadersBlock)),
  2059. %% Receive a PROTOCOL_ERROR connection error.
  2060. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2061. ok.
  2062. reject_streamid_0(Config) ->
  2063. doc("HEADERS frames received with streamid 0 (zero) "
  2064. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  2065. {ok, Socket} = do_handshake(Config),
  2066. %% Send a HEADERS frame with an streamid 0.
  2067. {HeadersBlock, _} = cow_hpack:encode([
  2068. {<<":method">>, <<"GET">>},
  2069. {<<":scheme">>, <<"http">>},
  2070. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2071. {<<":path">>, <<"/">>}
  2072. ]),
  2073. ok = gen_tcp:send(Socket, cow_http2:headers(0, fin, HeadersBlock)),
  2074. %% Receive a PROTOCOL_ERROR connection error.
  2075. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2076. ok.
  2077. %% See the comment for reject_streamid_lower for the rationale behind
  2078. %% having a STREAM_CLOSED connection error.
  2079. http_upgrade_reject_reuse_streamid_1(Config) ->
  2080. doc("Attempts to reuse streamid 1 after upgrading to HTTP/2 "
  2081. "must be rejected with a STREAM_CLOSED connection error. (RFC7540 5.1.1)"),
  2082. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2083. ok = gen_tcp:send(Socket, [
  2084. "GET / HTTP/1.1\r\n"
  2085. "Host: localhost\r\n"
  2086. "Connection: Upgrade, HTTP2-Settings\r\n"
  2087. "Upgrade: h2c\r\n"
  2088. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  2089. "\r\n"]),
  2090. ok = do_recv_101(Socket),
  2091. %% Send a valid preface.
  2092. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2093. %% Receive the server preface.
  2094. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2095. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  2096. %% Send the SETTINGS ack.
  2097. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2098. %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
  2099. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  2100. case gen_tcp:recv(Socket, 9, 1000) of
  2101. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  2102. [settings_ack|Acc];
  2103. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  2104. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  2105. [headers|Acc];
  2106. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  2107. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  2108. [data|Acc]
  2109. end
  2110. end, [], [1, 2, 3])),
  2111. case Received of
  2112. [settings_ack, headers, data] -> ok;
  2113. [headers, settings_ack, data] -> ok;
  2114. [headers, data, settings_ack] -> ok
  2115. end,
  2116. %% Send a HEADERS frame with streamid 1.
  2117. {HeadersBlock, _} = cow_hpack:encode([
  2118. {<<":method">>, <<"GET">>},
  2119. {<<":scheme">>, <<"http">>},
  2120. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2121. {<<":path">>, <<"/">>}
  2122. ]),
  2123. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  2124. %% Receive a STREAM_CLOSED connection error.
  2125. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2126. ok.
  2127. %% The RFC gives us various error codes to return for this case,
  2128. %% depending on whether the stream existed previously and how it
  2129. %% ended up being (half-)closed. Cowboy rejects all these HEADERS
  2130. %% frames the same way: with a STREAM_CLOSED connection error.
  2131. %% Making it a connection error is particularly important in the
  2132. %% cases where a stream error would be allowed because we avoid
  2133. %% having to decode the headers and save up resources.
  2134. reject_streamid_lower(Config) ->
  2135. doc("HEADERS frames received with streamid lower than the previous stream "
  2136. "must be rejected with a STREAM_CLOSED connection error. (RFC7540 5.1.1)"),
  2137. {ok, Socket} = do_handshake(Config),
  2138. %% Send a HEADERS frame with streamid 5.
  2139. {HeadersBlock, _} = cow_hpack:encode([
  2140. {<<":method">>, <<"GET">>},
  2141. {<<":scheme">>, <<"http">>},
  2142. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2143. {<<":path">>, <<"/">>}
  2144. ]),
  2145. ok = gen_tcp:send(Socket, cow_http2:headers(5, fin, HeadersBlock)),
  2146. %% Receive the response.
  2147. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2148. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  2149. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2150. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  2151. %% Send a HEADERS frame with streamid 3.
  2152. ok = gen_tcp:send(Socket, cow_http2:headers(3, fin, HeadersBlock)),
  2153. %% Receive a STREAM_CLOSED connection error.
  2154. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2155. ok.
  2156. %% @todo We need an option to limit the number of streams one can open
  2157. %% on a connection. And we need to enforce it. (RFC7540 5.1.1)
  2158. %
  2159. % Stream identifiers cannot be reused. Long-lived connections can
  2160. % result in an endpoint exhausting the available range of stream
  2161. % identifiers. A server
  2162. % that is unable to establish a new stream identifier can send a GOAWAY
  2163. % frame so that the client is forced to open a new connection for new
  2164. % streams.
  2165. %% (RFC7540 5.2.1)
  2166. % 3. Flow control is directional with overall control provided by the
  2167. % receiver. A receiver MAY choose to set any window size that it
  2168. % desires for each stream and for the entire connection. A sender
  2169. % MUST respect flow-control limits imposed by a receiver. Clients,
  2170. % servers, and intermediaries all independently advertise their
  2171. % flow-control window as a receiver and abide by the flow-control
  2172. % limits set by their peer when sending.
  2173. %
  2174. % 4. The initial value for the flow-control window is 65,535 octets
  2175. % for both new streams and the overall connection.
  2176. %
  2177. % 5. The frame type determines whether flow control applies to a
  2178. % frame. Of the frames specified in this document, only DATA
  2179. % frames are subject to flow control; all other frame types do not
  2180. % consume space in the advertised flow-control window. This
  2181. % ensures that important control frames are not blocked by flow
  2182. % control.
  2183. %
  2184. % 6. Flow control cannot be disabled.
  2185. %% (RFC7540 5.2.2)
  2186. % Even with full awareness of the current bandwidth-delay product,
  2187. % implementation of flow control can be difficult. When using flow
  2188. % control, the receiver MUST read from the TCP receive buffer in a
  2189. % timely fashion. Failure to do so could lead to a deadlock when
  2190. % critical frames, such as WINDOW_UPDATE, are not read and acted upon.
  2191. %% (RFC7540 5.3.1)
  2192. % Inside the dependency tree, a dependent stream SHOULD only be
  2193. % allocated resources if either all of the streams that it depends on
  2194. % (the chain of parent streams up to 0x0) are closed or it is not
  2195. % possible to make progress on them.
  2196. %% We reject all invalid HEADERS with a connection error because
  2197. %% we do not want to waste resources decoding them.
  2198. reject_self_dependent_stream_headers(Config) ->
  2199. doc("HEADERS frames opening a stream that depends on itself "
  2200. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.3.1)"),
  2201. {ok, Socket} = do_handshake(Config),
  2202. %% Send a HEADERS frame with priority set to depend on itself.
  2203. ok = gen_tcp:send(Socket, << 5:24, 1:8,
  2204. 0:2, 1:1, 0:4, 1:1, 0:1, 1:31, 0:1, 1:31, 0:8 >>),
  2205. %% Receive a PROTOCOL_ERROR connection error.
  2206. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2207. ok.
  2208. %% We reject all invalid HEADERS with a connection error because
  2209. %% we do not want to waste resources decoding them.
  2210. reject_self_dependent_stream_headers_with_padding(Config) ->
  2211. doc("HEADERS frames opening a stream that depends on itself "
  2212. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.3.1)"),
  2213. {ok, Socket} = do_handshake(Config),
  2214. %% Send a HEADERS frame with priority set to depend on itself.
  2215. ok = gen_tcp:send(Socket, << 6:24, 1:8,
  2216. 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 0:8, 0:1, 1:31, 0:8 >>),
  2217. %% Receive a PROTOCOL_ERROR connection error.
  2218. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2219. ok.
  2220. reject_self_dependent_stream_priority(Config) ->
  2221. doc("PRIORITY frames making a stream depend on itself "
  2222. "must be rejected with a PROTOCOL_ERROR stream error. (RFC7540 5.3.1)"),
  2223. {ok, Socket} = do_handshake(Config),
  2224. %% Send a PRIORITY frame making a stream depend on itself.
  2225. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 1, 123)),
  2226. %% Receive a PROTOCOL_ERROR stream error.
  2227. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  2228. ok.
  2229. %% @todo Stream priorities. (RFC7540 5.3.2 5.3.3 5.3.4 5.3.5)
  2230. %% (RFC7540 5.4.1)
  2231. % An endpoint that encounters a connection error SHOULD first send a
  2232. % GOAWAY frame (Section 6.8) with the stream identifier of the last
  2233. % stream that it successfully received from its peer. The GOAWAY frame
  2234. % includes an error code that indicates why the connection is
  2235. % terminating. After sending the GOAWAY frame for an error condition,
  2236. % the endpoint MUST close the TCP connection.
  2237. %
  2238. % An endpoint can end a connection at any time. In particular, an
  2239. % endpoint MAY choose to treat a stream error as a connection error.
  2240. % Endpoints SHOULD send a GOAWAY frame when ending a connection,
  2241. % providing that circumstances permit it.
  2242. %% (RFC7540 5.4.2)
  2243. % A RST_STREAM is the last frame that an endpoint can send on a stream.
  2244. % The peer that sends the RST_STREAM frame MUST be prepared to receive
  2245. % any frames that were sent or enqueued for sending by the remote peer.
  2246. % These frames can be ignored, except where they modify connection
  2247. % state (such as the state maintained for header compression
  2248. % (Section 4.3) or flow control).
  2249. %
  2250. % Normally, an endpoint SHOULD NOT send more than one RST_STREAM frame
  2251. % for any stream. However, an endpoint MAY send additional RST_STREAM
  2252. % frames if it receives frames on a closed stream after more than a
  2253. % round-trip time. This behavior is permitted to deal with misbehaving
  2254. % implementations.
  2255. %
  2256. % To avoid looping, an endpoint MUST NOT send a RST_STREAM in response
  2257. % to a RST_STREAM frame.
  2258. %% (RFC7540 5.5)
  2259. % Extensions are permitted to use new frame types (Section 4.1), new
  2260. % settings (Section 6.5.2), or new error codes (Section 7). Registries
  2261. % are established for managing these extension points: frame types
  2262. % (Section 11.2), settings (Section 11.3), and error codes
  2263. % (Section 11.4).
  2264. %
  2265. % Implementations MUST ignore unknown or unsupported values in all
  2266. % extensible protocol elements. Implementations MUST discard frames
  2267. % that have unknown or unsupported types. This means that any of these
  2268. % extension points can be safely used by extensions without prior
  2269. % arrangement or negotiation. However, extension frames that appear in
  2270. % the middle of a header block (Section 4.3) are not permitted; these
  2271. % MUST be treated as a connection error (Section 5.4.1) of type
  2272. % PROTOCOL_ERROR.
  2273. continuation_with_extension_frame_interleaved_error(Config) ->
  2274. doc("Extension frames interleaved in a header block must be rejected "
  2275. "with a PROTOCOL_ERROR connection error. "
  2276. "(RFC7540 4.3, RFC7540 5.5, RFC7540 6.2, RFC7540 6.10)"),
  2277. {ok, Socket} = do_handshake(Config),
  2278. %% Send an unterminated HEADERS frame followed by an extension frame.
  2279. ok = gen_tcp:send(Socket, [
  2280. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  2281. << 0:24, 128:8, 0:8, 0:32 >>
  2282. ]),
  2283. %% Receive a PROTOCOL_ERROR connection error.
  2284. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2285. ok.
  2286. %% (RFC7540 6.1) DATA
  2287. % Padding: Padding octets that contain no application semantic value.
  2288. % Padding octets MUST be set to zero when sending. A receiver is
  2289. % not obligated to verify padding but MAY treat non-zero padding as
  2290. % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  2291. %
  2292. % DATA frames MUST be associated with a stream. If a DATA frame is
  2293. % received whose stream identifier field is 0x0, the recipient MUST
  2294. % respond with a connection error (Section 5.4.1) of type
  2295. % PROTOCOL_ERROR.
  2296. %% (RFC7540 6.2) HEADERS
  2297. % Padding: Padding octets that contain no application semantic value.
  2298. % Padding octets MUST be set to zero when sending. A receiver is
  2299. % not obligated to verify padding but MAY treat non-zero padding as
  2300. % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  2301. %
  2302. % A HEADERS frame carries the END_STREAM flag that signals the end
  2303. % of a stream. However, a HEADERS frame with the END_STREAM flag
  2304. % set can be followed by CONTINUATION frames on the same stream.
  2305. % Logically, the CONTINUATION frames are part of the HEADERS frame.
  2306. %
  2307. %% @todo We probably need a test for the server sending HEADERS too large.
  2308. % The payload of a HEADERS frame contains a header block fragment
  2309. % (Section 4.3). A header block that does not fit within a HEADERS
  2310. % frame is continued in a CONTINUATION frame (Section 6.10).
  2311. %
  2312. % HEADERS frames MUST be associated with a stream. If a HEADERS frame
  2313. % is received whose stream identifier field is 0x0, the recipient MUST
  2314. % respond with a connection error (Section 5.4.1) of type
  2315. % PROTOCOL_ERROR.
  2316. %% (RFC7540 6.3) PRIORITY
  2317. % The PRIORITY frame always identifies a stream. If a PRIORITY frame
  2318. % is received with a stream identifier of 0x0, the recipient MUST
  2319. % respond with a connection error (Section 5.4.1) of type
  2320. % PROTOCOL_ERROR.
  2321. %% (RFC7540 6.4) RST_STREAM
  2322. % The RST_STREAM frame fully terminates the referenced stream and
  2323. % causes it to enter the "closed" state. After receiving a RST_STREAM
  2324. % on a stream, the receiver MUST NOT send additional frames for that
  2325. % stream, with the exception of PRIORITY. However, after sending the
  2326. % RST_STREAM, the sending endpoint MUST be prepared to receive and
  2327. % process additional frames sent on the stream that might have been
  2328. % sent by the peer prior to the arrival of the RST_STREAM.
  2329. %
  2330. % RST_STREAM frames MUST be associated with a stream. If a RST_STREAM
  2331. % frame is received with a stream identifier of 0x0, the recipient MUST
  2332. % treat this as a connection error (Section 5.4.1) of type
  2333. % PROTOCOL_ERROR.
  2334. %% (RFC7540 6.5) SETTINGS
  2335. % A SETTINGS frame MUST be sent by both endpoints at the start of a
  2336. % connection and MAY be sent at any other time by either endpoint over
  2337. % the lifetime of the connection. Implementations MUST support all of
  2338. % the parameters defined by this specification.
  2339. %
  2340. % SETTINGS frames always apply to a connection, never a single stream.
  2341. % The stream identifier for a SETTINGS frame MUST be zero (0x0). If an
  2342. % endpoint receives a SETTINGS frame whose stream identifier field is
  2343. % anything other than 0x0, the endpoint MUST respond with a connection
  2344. % error (Section 5.4.1) of type PROTOCOL_ERROR.
  2345. %
  2346. % The SETTINGS frame affects connection state. A badly formed or
  2347. % incomplete SETTINGS frame MUST be treated as a connection error
  2348. % (Section 5.4.1) of type PROTOCOL_ERROR.
  2349. %% Settings.
  2350. settings_header_table_size_client(Config) ->
  2351. doc("The SETTINGS_HEADER_TABLE_SIZE setting can be used to "
  2352. "inform the server of the maximum header table size "
  2353. "used by the client to decode header blocks. (RFC7540 6.5.2)"),
  2354. HeaderTableSize = 128,
  2355. %% Do the handhsake.
  2356. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2357. %% Send a valid preface.
  2358. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
  2359. cow_http2:settings(#{header_table_size => HeaderTableSize})]),
  2360. %% Receive the server preface.
  2361. {ok, << Len0:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2362. {ok, << 4:8, 0:40, _:Len0/binary >>} = gen_tcp:recv(Socket, 6 + Len0, 1000),
  2363. %% Send the SETTINGS ack.
  2364. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2365. %% Receive the SETTINGS ack.
  2366. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2367. %% Initialize decoding/encoding states.
  2368. DecodeState = cow_hpack:set_max_size(HeaderTableSize, cow_hpack:init()),
  2369. EncodeState = cow_hpack:init(),
  2370. %% Send a HEADERS frame as a request.
  2371. {ReqHeadersBlock1, _} = cow_hpack:encode([
  2372. {<<":method">>, <<"GET">>},
  2373. {<<":scheme">>, <<"http">>},
  2374. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2375. {<<":path">>, <<"/">>}
  2376. ], EncodeState),
  2377. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, ReqHeadersBlock1)),
  2378. %% Receive a HEADERS frame as a response.
  2379. {ok, << Len1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2380. {ok, RespHeadersBlock1} = gen_tcp:recv(Socket, Len1, 6000),
  2381. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock1, DecodeState),
  2382. {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  2383. %% The decoding succeeded, confirming that the table size is
  2384. %% lower than or equal to HeaderTableSize.
  2385. ok.
  2386. settings_header_table_size_server(Config0) ->
  2387. doc("The SETTINGS_HEADER_TABLE_SIZE setting can be used to "
  2388. "inform the client of the maximum header table size "
  2389. "used by the server to decode header blocks. (RFC7540 6.5.2)"),
  2390. HeaderTableSize = 128,
  2391. %% Create a new listener that allows larger header table sizes.
  2392. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2393. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2394. max_decode_table_size => HeaderTableSize
  2395. }, Config0),
  2396. try
  2397. %% Do the handhsake.
  2398. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2399. %% Send a valid preface.
  2400. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
  2401. cow_http2:settings(#{header_table_size => HeaderTableSize})]),
  2402. %% Receive the server preface.
  2403. {ok, << Len0:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2404. {ok, Data = <<_:48, _:Len0/binary>>} = gen_tcp:recv(Socket, 6 + Len0, 1000),
  2405. %% Confirm the server's SETTINGS_HEADERS_TABLE_SIZE uses HeaderTableSize.
  2406. {ok, {settings, #{header_table_size := HeaderTableSize}}, <<>>}
  2407. = cow_http2:parse(<<Len0:24, Data/binary>>),
  2408. %% Send the SETTINGS ack.
  2409. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2410. %% Receive the SETTINGS ack.
  2411. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2412. %% Initialize decoding/encoding states.
  2413. DecodeState = cow_hpack:init(),
  2414. EncodeState = cow_hpack:set_max_size(HeaderTableSize, cow_hpack:init()),
  2415. %% Send a HEADERS frame as a request.
  2416. {ReqHeadersBlock1, _} = cow_hpack:encode([
  2417. {<<":method">>, <<"GET">>},
  2418. {<<":scheme">>, <<"http">>},
  2419. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2420. {<<":path">>, <<"/">>}
  2421. ], EncodeState),
  2422. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, ReqHeadersBlock1)),
  2423. %% Receive a HEADERS frame as a response.
  2424. {ok, << Len1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2425. {ok, RespHeadersBlock1} = gen_tcp:recv(Socket, Len1, 6000),
  2426. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock1, DecodeState),
  2427. {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders)
  2428. %% The decoding succeeded on the server, confirming that
  2429. %% the table size was updated to HeaderTableSize.
  2430. after
  2431. cowboy:stop_listener(?FUNCTION_NAME)
  2432. end.
  2433. settings_max_concurrent_streams(Config0) ->
  2434. doc("The SETTINGS_MAX_CONCURRENT_STREAMS setting can be used to "
  2435. "restrict the number of concurrent streams. (RFC7540 5.1.2, RFC7540 6.5.2)"),
  2436. %% Create a new listener that allows only a single concurrent stream.
  2437. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2438. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2439. max_concurrent_streams => 1
  2440. }, Config0),
  2441. try
  2442. {ok, Socket} = do_handshake(Config),
  2443. %% Send two HEADERS frames as two separate streams.
  2444. Headers = [
  2445. {<<":method">>, <<"GET">>},
  2446. {<<":scheme">>, <<"http">>},
  2447. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2448. {<<":path">>, <<"/long_polling">>}
  2449. ],
  2450. {ReqHeadersBlock1, EncodeState} = cow_hpack:encode(Headers),
  2451. {ReqHeadersBlock2, _} = cow_hpack:encode(Headers, EncodeState),
  2452. ok = gen_tcp:send(Socket, [
  2453. cow_http2:headers(1, fin, ReqHeadersBlock1),
  2454. cow_http2:headers(3, fin, ReqHeadersBlock2)
  2455. ]),
  2456. %% Receive a REFUSED_STREAM stream error.
  2457. {ok, << _:24, 3:8, _:8, 3:32, 7:32 >>} = gen_tcp:recv(Socket, 13, 6000)
  2458. after
  2459. cowboy:stop_listener(?FUNCTION_NAME)
  2460. end.
  2461. settings_max_concurrent_streams_0(Config0) ->
  2462. doc("The SETTINGS_MAX_CONCURRENT_STREAMS setting can be set to "
  2463. "0 to refuse all incoming streams. (RFC7540 5.1.2, RFC7540 6.5.2)"),
  2464. %% Create a new listener that allows only a single concurrent stream.
  2465. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2466. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2467. max_concurrent_streams => 0
  2468. }, Config0),
  2469. try
  2470. {ok, Socket} = do_handshake(Config),
  2471. %% Send a HEADERS frame.
  2472. {HeadersBlock, _} = cow_hpack:encode([
  2473. {<<":method">>, <<"GET">>},
  2474. {<<":scheme">>, <<"http">>},
  2475. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2476. {<<":path">>, <<"/long_polling">>}
  2477. ]),
  2478. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  2479. %% Receive a REFUSED_STREAM stream error.
  2480. {ok, << _:24, 3:8, _:8, 1:32, 7:32 >>} = gen_tcp:recv(Socket, 13, 6000)
  2481. after
  2482. cowboy:stop_listener(?FUNCTION_NAME)
  2483. end.
  2484. %% @todo The client can limit the number of concurrent streams too. (RFC7540 5.1.2)
  2485. %
  2486. % A peer can limit the number of concurrently active streams using the
  2487. % SETTINGS_MAX_CONCURRENT_STREAMS parameter (see Section 6.5.2) within
  2488. % a SETTINGS frame. The maximum concurrent streams setting is specific
  2489. % to each endpoint and applies only to the peer that receives the
  2490. % setting. That is, clients specify the maximum number of concurrent
  2491. % streams the server can initiate, and servers specify the maximum
  2492. % number of concurrent streams the client can initiate.
  2493. %
  2494. % Endpoints MUST NOT exceed the limit set by their peer. An endpoint
  2495. % that receives a HEADERS frame that causes its advertised concurrent
  2496. % stream limit to be exceeded MUST treat this as a stream error
  2497. % (Section 5.4.2) of type PROTOCOL_ERROR or REFUSED_STREAM. The choice
  2498. % of error code determines whether the endpoint wishes to enable
  2499. % automatic retry (see Section 8.1.4) for details).
  2500. settings_initial_window_size(Config0) ->
  2501. doc("The SETTINGS_INITIAL_WINDOW_SIZE setting can be used to "
  2502. "change the initial window size of streams. (RFC7540 6.5.2)"),
  2503. %% Create a new listener that sets initial window sizes to 100000.
  2504. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2505. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2506. initial_connection_window_size => 100000,
  2507. initial_stream_window_size => 100000
  2508. }, Config0),
  2509. try
  2510. %% We need to do the handshake manually because a WINDOW_UPDATE
  2511. %% frame will be sent to update the connection window.
  2512. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2513. %% Send a valid preface.
  2514. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2515. %% Receive the server preface.
  2516. {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2517. {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
  2518. %% Send the SETTINGS ack.
  2519. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2520. %% Receive the WINDOW_UPDATE for the connection.
  2521. {ok, << 4:24, 8:8, 0:40, _:32 >>} = gen_tcp:recv(Socket, 13, 1000),
  2522. %% Receive the SETTINGS ack.
  2523. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2524. %% Send a HEADERS frame initiating a stream followed by
  2525. %% DATA frames totaling 90000 bytes of body.
  2526. Headers = [
  2527. {<<":method">>, <<"POST">>},
  2528. {<<":scheme">>, <<"http">>},
  2529. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2530. {<<":path">>, <<"/long_polling">>}
  2531. ],
  2532. {HeadersBlock, _} = cow_hpack:encode(Headers),
  2533. ok = gen_tcp:send(Socket, [
  2534. cow_http2:headers(1, nofin, HeadersBlock),
  2535. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2536. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2537. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2538. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2539. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2540. cow_http2:data(1, fin, <<0:15000/unit:8>>)
  2541. ]),
  2542. %% Receive a proper response.
  2543. {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2544. {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
  2545. %% No errors follow due to our sending of more than 65535 bytes of data.
  2546. {error, timeout} = gen_tcp:recv(Socket, 0, 1000)
  2547. after
  2548. cowboy:stop_listener(?FUNCTION_NAME)
  2549. end.
  2550. settings_initial_window_size_after_ack(Config0) ->
  2551. doc("The SETTINGS_INITIAL_WINDOW_SIZE setting can be used to "
  2552. "change the initial window size of streams. It is applied "
  2553. "to all existing streams upon receipt of the SETTINGS ack. (RFC7540 6.5.2)"),
  2554. %% Create a new listener that sets the initial stream window sizes to 0.
  2555. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2556. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2557. initial_stream_window_size => 0
  2558. }, Config0),
  2559. try
  2560. %% We need to do the handshake manually because we don't
  2561. %% want to send the SETTINGS ack immediately.
  2562. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2563. %% Send a valid preface.
  2564. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2565. %% Receive the server preface.
  2566. {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2567. {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
  2568. %%
  2569. %% Don't send the SETTINGS ack yet! We want to create a stream first.
  2570. %%
  2571. %% Receive the SETTINGS ack.
  2572. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2573. %% Send a HEADERS frame initiating a stream, a SETTINGS ack
  2574. %% and a small DATA frame despite no window available in the stream.
  2575. Headers = [
  2576. {<<":method">>, <<"POST">>},
  2577. {<<":scheme">>, <<"http">>},
  2578. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2579. {<<":path">>, <<"/long_polling">>}
  2580. ],
  2581. {HeadersBlock, _} = cow_hpack:encode(Headers),
  2582. ok = gen_tcp:send(Socket, [
  2583. cow_http2:headers(1, nofin, HeadersBlock),
  2584. cow_http2:settings_ack(),
  2585. cow_http2:data(1, fin, <<0:32/unit:8>>)
  2586. ]),
  2587. %% Receive a FLOW_CONTROL_ERROR stream error.
  2588. {ok, << _:24, 3:8, _:8, 1:32, 3:32 >>} = gen_tcp:recv(Socket, 13, 6000)
  2589. after
  2590. cowboy:stop_listener(?FUNCTION_NAME)
  2591. end.
  2592. settings_initial_window_size_before_ack(Config0) ->
  2593. doc("The SETTINGS_INITIAL_WINDOW_SIZE setting can be used to "
  2594. "change the initial window size of streams. It is only "
  2595. "applied upon receipt of the SETTINGS ack. (RFC7540 6.5.2)"),
  2596. %% Create a new listener that sets the initial stream window sizes to 0.
  2597. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2598. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2599. initial_stream_window_size => 0
  2600. }, Config0),
  2601. try
  2602. %% We need to do the handshake manually because we don't
  2603. %% want to send the SETTINGS ack.
  2604. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2605. %% Send a valid preface.
  2606. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2607. %% Receive the server preface.
  2608. {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2609. {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
  2610. %%
  2611. %% Don't send the SETTINGS ack! We want the server to keep the original settings.
  2612. %%
  2613. %% Receive the SETTINGS ack.
  2614. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2615. %% Send a HEADERS frame initiating a stream followed by
  2616. %% DATA frames totaling 60000 bytes of body.
  2617. Headers = [
  2618. {<<":method">>, <<"POST">>},
  2619. {<<":scheme">>, <<"http">>},
  2620. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2621. {<<":path">>, <<"/long_polling">>}
  2622. ],
  2623. {HeadersBlock, _} = cow_hpack:encode(Headers),
  2624. ok = gen_tcp:send(Socket, [
  2625. cow_http2:headers(1, nofin, HeadersBlock),
  2626. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2627. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2628. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2629. cow_http2:data(1, fin, <<0:15000/unit:8>>)
  2630. ]),
  2631. %% Receive a proper response.
  2632. {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2633. {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
  2634. %% No errors follow due to our sending of more than 0 bytes of data.
  2635. {error, timeout} = gen_tcp:recv(Socket, 0, 1000)
  2636. after
  2637. cowboy:stop_listener(?FUNCTION_NAME)
  2638. end.
  2639. settings_max_frame_size(Config0) ->
  2640. doc("The SETTINGS_MAX_FRAME_SIZE setting can be used to "
  2641. "change the maximum frame size allowed. (RFC7540 6.5.2)"),
  2642. %% Create a new listener that sets the maximum frame size to 30000.
  2643. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2644. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2645. max_frame_size_received => 30000
  2646. }, Config0),
  2647. try
  2648. %% Do the handshake.
  2649. {ok, Socket} = do_handshake(Config),
  2650. %% Send a HEADERS frame initiating a stream followed by
  2651. %% a single 25000 bytes DATA frame.
  2652. Headers = [
  2653. {<<":method">>, <<"POST">>},
  2654. {<<":scheme">>, <<"http">>},
  2655. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2656. {<<":path">>, <<"/long_polling">>}
  2657. ],
  2658. {HeadersBlock, _} = cow_hpack:encode(Headers),
  2659. ok = gen_tcp:send(Socket, [
  2660. cow_http2:headers(1, nofin, HeadersBlock),
  2661. cow_http2:data(1, fin, <<0:25000/unit:8>>)
  2662. ]),
  2663. %% Receive a proper response.
  2664. {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2665. {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
  2666. %% No errors follow due to our sending of a 25000 bytes frame.
  2667. {error, timeout} = gen_tcp:recv(Socket, 0, 1000)
  2668. after
  2669. cowboy:stop_listener(?FUNCTION_NAME)
  2670. end.
  2671. settings_max_frame_size_reject_too_small(Config) ->
  2672. doc("A SETTINGS_MAX_FRAME_SIZE smaller than 16384 must be rejected "
  2673. "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
  2674. {ok, Socket} = do_handshake(Config),
  2675. %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE lower than 16384.
  2676. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16383:32 >>),
  2677. %% Receive a PROTOCOL_ERROR connection error.
  2678. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2679. ok.
  2680. settings_max_frame_size_reject_too_large(Config) ->
  2681. doc("A SETTINGS_MAX_FRAME_SIZE larger than 16777215 must be rejected "
  2682. "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
  2683. {ok, Socket} = do_handshake(Config),
  2684. %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE larger than 16777215.
  2685. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16777216:32 >>),
  2686. %% Receive a PROTOCOL_ERROR connection error.
  2687. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2688. ok.
  2689. % SETTINGS_MAX_HEADER_LIST_SIZE (0x6): This advisory setting informs a
  2690. % peer of the maximum size of header list that the sender is
  2691. % prepared to accept, in octets. The value is based on the
  2692. % uncompressed size of header fields, including the length of the
  2693. % name and value in octets plus an overhead of 32 octets for each
  2694. % header field.
  2695. %
  2696. % For any given request, a lower limit than what is advertised MAY
  2697. % be enforced. The initial value of this setting is unlimited.
  2698. %
  2699. % An endpoint that receives a SETTINGS frame with any unknown or
  2700. % unsupported identifier MUST ignore that setting. (6.5.2 and 6.5.3)
  2701. %% (RFC7540 6.5.3)
  2702. % Upon receiving a SETTINGS frame with the ACK flag set, the
  2703. % sender of the altered parameters can rely on the setting having been
  2704. % applied.
  2705. %
  2706. % If the sender of a SETTINGS frame does not receive an acknowledgement
  2707. % within a reasonable amount of time, it MAY issue a connection error
  2708. % (Section 5.4.1) of type SETTINGS_TIMEOUT.
  2709. %% (RFC7540 6.6) PUSH_PROMISE
  2710. % @todo PUSH_PROMISE frames have a reserved bit in the payload that must be ignored.
  2711. %
  2712. % Padding: Padding octets that contain no application semantic value.
  2713. % Padding octets MUST be set to zero when sending. A receiver is
  2714. % not obligated to verify padding but MAY treat non-zero padding as
  2715. % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  2716. %
  2717. % PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that
  2718. % is in either the "open" or "half-closed (remote)" state. The stream
  2719. % identifier of a PUSH_PROMISE frame indicates the stream it is
  2720. % associated with. If the stream identifier field specifies the value
  2721. % 0x0, a recipient MUST respond with a connection error (Section 5.4.1)
  2722. % of type PROTOCOL_ERROR.
  2723. client_settings_disable_push(Config) ->
  2724. doc("PUSH_PROMISE frames must not be sent when the setting "
  2725. "SETTINGS_ENABLE_PUSH is disabled. (RFC7540 6.5.2, RFC7540 6.6, RFC7540 8.2)"),
  2726. %% Do a prior knowledge handshake.
  2727. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2728. %% Send a valid preface.
  2729. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{
  2730. enable_push => false
  2731. })]),
  2732. %% Receive the server preface.
  2733. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2734. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  2735. %% Send the SETTINGS ack.
  2736. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2737. %% Receive the SETTINGS ack.
  2738. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2739. %% Send a HEADERS frame on a resource that sends PUSH_PROMISE frames.
  2740. {HeadersBlock, _} = cow_hpack:encode([
  2741. {<<":method">>, <<"GET">>},
  2742. {<<":scheme">>, <<"http">>},
  2743. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2744. {<<":path">>, <<"/resp/push">>}
  2745. ]),
  2746. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  2747. %% Receive a HEADERS frame as a response, no PUSH_PROMISE frames.
  2748. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2749. ok.
  2750. % Since PUSH_PROMISE reserves a stream, ignoring a PUSH_PROMISE frame
  2751. % causes the stream state to become indeterminate. A receiver MUST
  2752. % treat the receipt of a PUSH_PROMISE on a stream that is neither
  2753. % "open" nor "half-closed (local)" as a connection error
  2754. % (Section 5.4.1) of type PROTOCOL_ERROR.
  2755. %
  2756. % A receiver MUST treat the receipt of a PUSH_PROMISE that promises an
  2757. % illegal stream identifier (Section 5.1.1) as a connection error
  2758. % (Section 5.4.1) of type PROTOCOL_ERROR. Note that an illegal stream
  2759. % identifier is an identifier for a stream that is not currently in the
  2760. % "idle" state.
  2761. %% (RFC7540 6.7) PING
  2762. % PING frames are not associated with any individual stream. If a PING
  2763. % frame is received with a stream identifier field value other than
  2764. % 0x0, the recipient MUST respond with a connection error
  2765. % (Section 5.4.1) of type PROTOCOL_ERROR.
  2766. %% (RFC7540 6.8) GOAWAY
  2767. % @todo GOAWAY frames have a reserved bit in the payload that must be ignored.
  2768. %
  2769. %% @todo We should eventually implement the mechanism for gracefully
  2770. %% shutting down of the connection. (Send the GOAWAY, finish processing
  2771. %% the current set of streams, give up after a certain timeout.)
  2772. %
  2773. %% @todo If we graceful shutdown and receive a GOAWAY, we give up too.
  2774. % A GOAWAY frame might not immediately precede closing of the
  2775. % connection; a receiver of a GOAWAY that has no more use for the
  2776. % connection SHOULD still send a GOAWAY frame before terminating the
  2777. % connection.
  2778. %
  2779. %% @todo And it gets more complex when you think about h1 to h2 proxies.
  2780. % A server that is attempting to gracefully shut down a
  2781. % connection SHOULD send an initial GOAWAY frame with the last stream
  2782. % identifier set to 2^31-1 and a NO_ERROR code. This signals to the
  2783. % client that a shutdown is imminent and that initiating further
  2784. % requests is prohibited. After allowing time for any in-flight stream
  2785. % creation (at least one round-trip time), the server can send another
  2786. % GOAWAY frame with an updated last stream identifier. This ensures
  2787. % that a connection can be cleanly shut down without losing requests.
  2788. %
  2789. %% @todo And of course even if we shutdown we need to be careful about
  2790. %% the connection state.
  2791. % After sending a GOAWAY frame, the sender can discard frames for
  2792. % streams initiated by the receiver with identifiers higher than the
  2793. % identified last stream. However, any frames that alter connection
  2794. % state cannot be completely ignored. For instance, HEADERS,
  2795. % PUSH_PROMISE, and CONTINUATION frames MUST be minimally processed to
  2796. % ensure the state maintained for header compression is consistent (see
  2797. % Section 4.3); similarly, DATA frames MUST be counted toward the
  2798. % connection flow-control window. Failure to process these frames can
  2799. % cause flow control or header compression state to become
  2800. % unsynchronized.
  2801. %
  2802. % The GOAWAY frame applies to the connection, not a specific stream.
  2803. % An endpoint MUST treat a GOAWAY frame with a stream identifier other
  2804. % than 0x0 as a connection error (Section 5.4.1) of type
  2805. % PROTOCOL_ERROR.
  2806. %% (RFC7540 6.9) WINDOW_UPDATE
  2807. % @todo WINDOW_UPDATE frames have a reserved bit in the payload that must be ignored.
  2808. window_update_reject_0(Config) ->
  2809. doc("WINDOW_UPDATE frames with an increment of 0 for the connection "
  2810. "flow control window must be rejected with a "
  2811. "PROTOCOL_ERROR connection error. (RFC7540 6.9.1)"),
  2812. {ok, Socket} = do_handshake(Config),
  2813. %% Send connection-wide WINDOW_UPDATE frame with a value of 0.
  2814. ok = gen_tcp:send(Socket, [
  2815. cow_http2:window_update(0)
  2816. ]),
  2817. %% Receive a PROTOCOL_ERROR connection error.
  2818. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2819. ok.
  2820. window_update_reject_0_stream(Config) ->
  2821. doc("WINDOW_UPDATE frames with an increment of 0 for a stream "
  2822. "flow control window must be rejected with a "
  2823. "PROTOCOL_ERROR stream error. (RFC7540 6.9.1)"),
  2824. {ok, Socket} = do_handshake(Config),
  2825. %% Send a HEADERS frame immediately followed by
  2826. %% a WINDOW_UPDATE frame with a value of 0.
  2827. {HeadersBlock, _} = cow_hpack:encode([
  2828. {<<":method">>, <<"GET">>},
  2829. {<<":scheme">>, <<"http">>},
  2830. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2831. {<<":path">>, <<"/">>}
  2832. ]),
  2833. ok = gen_tcp:send(Socket, [
  2834. cow_http2:headers(1, fin, HeadersBlock),
  2835. cow_http2:window_update(1, 0)
  2836. ]),
  2837. %% Receive a PROTOCOL_ERROR stream error.
  2838. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  2839. ok.
  2840. % A receiver that receives a flow-controlled frame MUST always account
  2841. % for its contribution against the connection flow-control window,
  2842. % unless the receiver treats this as a connection error
  2843. % (Section 5.4.1). This is necessary even if the frame is in error.
  2844. % The sender counts the frame toward the flow-control window, but if
  2845. % the receiver does not, the flow-control window at the sender and
  2846. % receiver can become different.
  2847. data_reject_overflow(Config0) ->
  2848. doc("DATA frames that cause the connection flow control window "
  2849. "to overflow must be rejected with a FLOW_CONTROL_ERROR "
  2850. "connection error. (RFC7540 6.9.1)"),
  2851. %% Create a new listener that allows only a single concurrent stream.
  2852. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2853. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2854. initial_stream_window_size => 100000
  2855. }, Config0),
  2856. try
  2857. {ok, Socket} = do_handshake(Config),
  2858. %% Send a HEADERS frame initiating a stream followed by
  2859. %% DATA frames totaling 90000 bytes of body.
  2860. Headers = [
  2861. {<<":method">>, <<"POST">>},
  2862. {<<":scheme">>, <<"http">>},
  2863. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2864. {<<":path">>, <<"/long_polling">>}
  2865. ],
  2866. {HeadersBlock, _} = cow_hpack:encode(Headers),
  2867. ok = gen_tcp:send(Socket, [
  2868. cow_http2:headers(1, nofin, HeadersBlock),
  2869. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2870. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2871. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2872. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2873. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2874. cow_http2:data(1, fin, <<0:15000/unit:8>>)
  2875. ]),
  2876. %% Receive a FLOW_CONTROL_ERROR connection error.
  2877. {ok, << _:24, 7:8, _:72, 3:32 >>} = gen_tcp:recv(Socket, 17, 6000)
  2878. after
  2879. cowboy:stop_listener(?FUNCTION_NAME)
  2880. end.
  2881. data_reject_overflow_stream(Config0) ->
  2882. doc("DATA frames that cause the stream flow control window "
  2883. "to overflow must be rejected with a FLOW_CONTROL_ERROR "
  2884. "stream error. (RFC7540 6.9.1)"),
  2885. %% Create a new listener that allows only a single concurrent stream.
  2886. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2887. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2888. initial_connection_window_size => 100000
  2889. }, Config0),
  2890. try
  2891. %% We need to do the handshake manually because a WINDOW_UPDATE
  2892. %% frame will be sent to update the connection window.
  2893. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2894. %% Send a valid preface.
  2895. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2896. %% Receive the server preface.
  2897. {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2898. {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
  2899. %% Send the SETTINGS ack.
  2900. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2901. %% Receive the WINDOW_UPDATE for the connection.
  2902. {ok, << 4:24, 8:8, 0:40, _:32 >>} = gen_tcp:recv(Socket, 13, 1000),
  2903. %% Receive the SETTINGS ack.
  2904. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2905. %% Send a HEADERS frame initiating a stream followed by
  2906. %% DATA frames totaling 90000 bytes of body.
  2907. Headers = [
  2908. {<<":method">>, <<"POST">>},
  2909. {<<":scheme">>, <<"http">>},
  2910. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2911. {<<":path">>, <<"/long_polling">>}
  2912. ],
  2913. {HeadersBlock, _} = cow_hpack:encode(Headers),
  2914. ok = gen_tcp:send(Socket, [
  2915. cow_http2:headers(1, nofin, HeadersBlock),
  2916. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2917. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2918. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2919. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2920. cow_http2:data(1, nofin, <<0:15000/unit:8>>),
  2921. cow_http2:data(1, fin, <<0:15000/unit:8>>)
  2922. ]),
  2923. %% Receive a FLOW_CONTROL_ERROR stream error.
  2924. {ok, << _:24, 3:8, _:8, 1:32, 3:32 >>} = gen_tcp:recv(Socket, 13, 6000)
  2925. after
  2926. cowboy:stop_listener(?FUNCTION_NAME)
  2927. end.
  2928. lingering_data_counts_toward_connection_window(Config0) ->
  2929. doc("DATA frames received after sending RST_STREAM must be counted "
  2930. "toward the connection flow-control window. (RFC7540 5.1)"),
  2931. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  2932. env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
  2933. initial_connection_window_size => 100000
  2934. }, Config0),
  2935. try
  2936. %% We need to do the handshake manually because a WINDOW_UPDATE
  2937. %% frame will be sent to update the connection window.
  2938. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2939. %% Send a valid preface.
  2940. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2941. %% Receive the server preface.
  2942. {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2943. {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
  2944. %% Send the SETTINGS ack.
  2945. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2946. %% Receive the WINDOW_UPDATE for the connection.
  2947. {ok, << 4:24, 8:8, 0:40, _:32 >>} = gen_tcp:recv(Socket, 13, 1000),
  2948. %% Receive the SETTINGS ack.
  2949. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2950. Headers = [
  2951. {<<":method">>, <<"POST">>},
  2952. {<<":scheme">>, <<"http">>},
  2953. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2954. {<<":path">>, <<"/loop_handler_abort">>}
  2955. ],
  2956. {HeadersBlock, _} = cow_hpack:encode(Headers),
  2957. ok = gen_tcp:send(Socket, [
  2958. cow_http2:headers(1, nofin, HeadersBlock),
  2959. cow_http2:data(1, nofin, <<0:1000/unit:8>>)
  2960. ]),
  2961. % Make sure server send RST_STREAM.
  2962. timer:sleep(100),
  2963. ok = gen_tcp:send(Socket, [
  2964. cow_http2:data(1, nofin, <<0:0/unit:8>>),
  2965. cow_http2:data(1, fin, <<0:1000/unit:8>>)
  2966. ]),
  2967. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  2968. % Skip the header.
  2969. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  2970. % Skip RST_STREAM.
  2971. {ok, << 4:24, 3:8, 1:40, _:32 >>} = gen_tcp:recv(Socket, 13, 1000),
  2972. % Received a WINDOW_UPDATE frame after we got RST_STREAM.
  2973. {ok, << 4:24, 8:8, 0:40, 1000:32 >>} = gen_tcp:recv(Socket, 13, 1000)
  2974. after
  2975. cowboy:stop_listener(?FUNCTION_NAME)
  2976. end.
  2977. %% (RFC7540 6.9.1)
  2978. % Frames with zero length with the END_STREAM flag set (that
  2979. % is, an empty DATA frame) MAY be sent if there is no available space
  2980. % in either flow-control window.
  2981. window_update_reject_overflow(Config) ->
  2982. doc("WINDOW_UPDATE frames that cause the connection flow control "
  2983. "window to exceed 2^31-1 must be rejected with a "
  2984. "FLOW_CONTROL_ERROR connection error. (RFC7540 6.9.1)"),
  2985. {ok, Socket} = do_handshake(Config),
  2986. %% Send a connection-wide WINDOW_UPDATE frame that causes the window to overflow.
  2987. ok = gen_tcp:send(Socket, [
  2988. cow_http2:window_update(16#7fffffff)
  2989. ]),
  2990. %% Receive a FLOW_CONTROL_ERROR connection error.
  2991. {ok, << _:24, 7:8, _:72, 3:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2992. ok.
  2993. window_update_reject_overflow_stream(Config) ->
  2994. doc("WINDOW_UPDATE frames that cause a stream flow control "
  2995. "window to exceed 2^31-1 must be rejected with a "
  2996. "FLOW_CONTROL_ERROR stream error. (RFC7540 6.9.1)"),
  2997. {ok, Socket} = do_handshake(Config),
  2998. %% Send a HEADERS frame immediately followed by a WINDOW_UPDATE
  2999. %% frame that causes the stream window to overflow.
  3000. {HeadersBlock, _} = cow_hpack:encode([
  3001. {<<":method">>, <<"GET">>},
  3002. {<<":scheme">>, <<"http">>},
  3003. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3004. {<<":path">>, <<"/">>}
  3005. ]),
  3006. ok = gen_tcp:send(Socket, [
  3007. cow_http2:headers(1, fin, HeadersBlock),
  3008. cow_http2:window_update(1, 16#7fffffff)
  3009. ]),
  3010. %% Receive a FLOW_CONTROL_ERROR stream error.
  3011. {ok, << _:24, 3:8, _:8, 1:32, 3:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3012. ok.
  3013. settings_initial_window_size_changes(Config) ->
  3014. doc("When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, the server "
  3015. "must adjust the size of the flow control windows of the active "
  3016. "streams. (RFC7540 6.9.2)"),
  3017. {ok, Socket} = do_handshake(Config),
  3018. %% Set SETTINGS_INITIAL_WINDOW_SIZE to 0 to prevent sending of DATA.
  3019. ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 0})),
  3020. %% Receive the SETTINGS ack.
  3021. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3022. %% Send a HEADERS frame.
  3023. {HeadersBlock, _} = cow_hpack:encode([
  3024. {<<":method">>, <<"GET">>},
  3025. {<<":scheme">>, <<"http">>},
  3026. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3027. {<<":path">>, <<"/">>}
  3028. ]),
  3029. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3030. %% Receive a response but no DATA frames are coming.
  3031. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3032. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  3033. {error, timeout} = gen_tcp:recv(Socket, 9, 1000),
  3034. %% Set SETTINGS_INITIAL_WINDOW_SIZE to a larger value.
  3035. ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 5})),
  3036. %% Receive the SETTINGS ack.
  3037. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3038. %% Receive a DATA frame of that size and no other.
  3039. {ok, << 5:24, 0:8, 0:8, 1:32, "Hello" >>} = gen_tcp:recv(Socket, 14, 1000),
  3040. {error, timeout} = gen_tcp:recv(Socket, 9, 1000),
  3041. %% Set SETTINGS_INITIAL_WINDOW_SIZE to exactly the size in the body.
  3042. ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 12})),
  3043. %% Receive the SETTINGS ack.
  3044. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3045. %% Receive the rest of the response.
  3046. {ok, << 7:24, 0:8, 1:8, 1:32, " world!" >>} = gen_tcp:recv(Socket, 16, 1000),
  3047. ok.
  3048. settings_initial_window_size_changes_negative(Config) ->
  3049. doc("When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, the server "
  3050. "must adjust the size of the flow control windows of the active "
  3051. "streams even if their window end up negative. (RFC7540 6.9.2)"),
  3052. {ok, Socket} = do_handshake(Config),
  3053. %% Set SETTINGS_INITIAL_WINDOW_SIZE to 5.
  3054. ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 5})),
  3055. %% Receive the SETTINGS ack.
  3056. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3057. %% Send a HEADERS frame.
  3058. {HeadersBlock, _} = cow_hpack:encode([
  3059. {<<":method">>, <<"GET">>},
  3060. {<<":scheme">>, <<"http">>},
  3061. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3062. {<<":path">>, <<"/">>}
  3063. ]),
  3064. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3065. %% Receive a response with a single DATA frame of the initial size we set.
  3066. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3067. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  3068. {ok, << 5:24, 0:8, 0:8, 1:32, "Hello" >>} = gen_tcp:recv(Socket, 14, 1000),
  3069. {error, timeout} = gen_tcp:recv(Socket, 9, 1000),
  3070. %% Set SETTINGS_INITIAL_WINDOW_SIZE to 0 to make the stream's window negative.
  3071. ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 0})),
  3072. %% Receive the SETTINGS ack.
  3073. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3074. %% Set SETTINGS_INITIAL_WINDOW_SIZE to exactly the size in the body.
  3075. ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 12})),
  3076. %% Receive the SETTINGS ack.
  3077. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3078. %% Receive the rest of the response.
  3079. {ok, << 7:24, 0:8, 1:8, 1:32, " world!" >>} = gen_tcp:recv(Socket, 16, 1000),
  3080. ok.
  3081. settings_initial_window_size_reject_overflow(Config) ->
  3082. doc("A SETTINGS_INITIAL_WINDOW_SIZE that causes a flow control window "
  3083. "to exceed 2^31-1 must be rejected with a FLOW_CONTROL_ERROR "
  3084. "connection error. (RFC7540 6.5.2, RFC7540 6.9.2)"),
  3085. {ok, Socket} = do_handshake(Config),
  3086. %% Set SETTINGS_INITIAL_WINDOW_SIZE to 2^31.
  3087. ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 16#80000000})),
  3088. %% Receive a FLOW_CONTROL_ERROR connection error.
  3089. {ok, << _:24, 7:8, _:72, 3:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  3090. ok.
  3091. %% (RFC7540 6.9.3)
  3092. %% @todo The right way to do this seems to be to wait for the SETTINGS ack
  3093. %% before we KNOW the flow control window was updated on the other side.
  3094. % A receiver that wishes to use a smaller flow-control window than the
  3095. % current size can send a new SETTINGS frame. However, the receiver
  3096. % MUST be prepared to receive data that exceeds this window size, since
  3097. % the sender might send data that exceeds the lower limit prior to
  3098. % processing the SETTINGS frame.
  3099. %% (RFC7540 6.10) CONTINUATION
  3100. % CONTINUATION frames MUST be associated with a stream. If a
  3101. % CONTINUATION frame is received whose stream identifier field is 0x0,
  3102. % the recipient MUST respond with a connection error (Section 5.4.1) of
  3103. % type PROTOCOL_ERROR.
  3104. %
  3105. % A CONTINUATION frame MUST be preceded by a HEADERS, PUSH_PROMISE or
  3106. % CONTINUATION frame without the END_HEADERS flag set. A recipient
  3107. % that observes violation of this rule MUST respond with a connection
  3108. % error (Section 5.4.1) of type PROTOCOL_ERROR.
  3109. %% (RFC7540 7) Error Codes
  3110. % Unknown or unsupported error codes MUST NOT trigger any special
  3111. % behavior. These MAY be treated by an implementation as being
  3112. % equivalent to INTERNAL_ERROR.
  3113. accept_trailers(Config) ->
  3114. doc("Trailing HEADERS frames must be accepted. (RFC7540 8.1)"),
  3115. {ok, Socket} = do_handshake(Config),
  3116. %% Send a request containing DATA and trailing HEADERS frames.
  3117. {HeadersBlock, EncodeState} = cow_hpack:encode([
  3118. {<<":method">>, <<"POST">>},
  3119. {<<":scheme">>, <<"http">>},
  3120. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3121. {<<":path">>, <<"/long_polling">>},
  3122. {<<"trailer">>, <<"x-checksum">>}
  3123. ]),
  3124. {TrailersBlock, _} = cow_hpack:encode([
  3125. {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
  3126. ], EncodeState),
  3127. ok = gen_tcp:send(Socket, [
  3128. cow_http2:headers(1, nofin, HeadersBlock),
  3129. cow_http2:data(1, nofin, <<0:10000/unit:8>>),
  3130. cow_http2:headers(1, fin, TrailersBlock)
  3131. ]),
  3132. %% Receive a HEADERS frame as a response.
  3133. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  3134. ok.
  3135. accept_trailers_continuation(Config) ->
  3136. doc("Trailing HEADERS and CONTINUATION frames must be accepted. (RFC7540 8.1)"),
  3137. {ok, Socket} = do_handshake(Config),
  3138. %% Send a request containing DATA and trailing HEADERS and CONTINUATION frames.
  3139. {HeadersBlock, EncodeState} = cow_hpack:encode([
  3140. {<<":method">>, <<"POST">>},
  3141. {<<":scheme">>, <<"http">>},
  3142. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3143. {<<":path">>, <<"/long_polling">>},
  3144. {<<"trailer">>, <<"x-checksum">>}
  3145. ]),
  3146. {TrailersBlock, _} = cow_hpack:encode([
  3147. {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
  3148. ], EncodeState),
  3149. Len = iolist_size(TrailersBlock),
  3150. ok = gen_tcp:send(Socket, [
  3151. cow_http2:headers(1, nofin, HeadersBlock),
  3152. cow_http2:data(1, nofin, <<0:10000/unit:8>>),
  3153. <<0:24, 1:8, 0:7, 1:1, 0:1, 1:31>>,
  3154. <<Len:24, 9:8, 0:5, 1:1, 0:3, 1:31>>,
  3155. TrailersBlock
  3156. ]),
  3157. %% Receive a HEADERS frame as a response.
  3158. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  3159. ok.
  3160. %% We reject all invalid HEADERS with a connection error because
  3161. %% we do not want to waste resources decoding them.
  3162. reject_trailers_nofin(Config) ->
  3163. doc("Trailing HEADERS frames received without the END_STREAM flag "
  3164. "set must be rejected with a PROTOCOL_ERROR connection error. "
  3165. "(RFC7540 8.1, RFC7540 8.1.2.6)"),
  3166. {ok, Socket} = do_handshake(Config),
  3167. %% Send a request containing DATA and trailing HEADERS frames.
  3168. %% The trailing HEADERS does not have the END_STREAM flag set.
  3169. {HeadersBlock, EncodeState} = cow_hpack:encode([
  3170. {<<":method">>, <<"POST">>},
  3171. {<<":scheme">>, <<"http">>},
  3172. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3173. {<<":path">>, <<"/long_polling">>},
  3174. {<<"trailer">>, <<"x-checksum">>}
  3175. ]),
  3176. {TrailersBlock, _} = cow_hpack:encode([
  3177. {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
  3178. ], EncodeState),
  3179. ok = gen_tcp:send(Socket, [
  3180. cow_http2:headers(1, nofin, HeadersBlock),
  3181. cow_http2:data(1, nofin, <<0:10000/unit:8>>),
  3182. cow_http2:headers(1, nofin, TrailersBlock)
  3183. ]),
  3184. %% Receive a PROTOCOL_ERROR connection error.
  3185. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  3186. ok.
  3187. %% We reject all invalid HEADERS with a connection error because
  3188. %% we do not want to waste resources decoding them.
  3189. reject_trailers_nofin_continuation(Config) ->
  3190. doc("Trailing HEADERS frames received without the END_STREAM flag "
  3191. "set must be rejected with a PROTOCOL_ERROR connection error. "
  3192. "(RFC7540 8.1, RFC7540 8.1.2.6)"),
  3193. {ok, Socket} = do_handshake(Config),
  3194. %% Send a request containing DATA and trailing HEADERS and CONTINUATION frames.
  3195. %% The trailing HEADERS does not have the END_STREAM flag set.
  3196. {HeadersBlock, EncodeState} = cow_hpack:encode([
  3197. {<<":method">>, <<"POST">>},
  3198. {<<":scheme">>, <<"http">>},
  3199. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3200. {<<":path">>, <<"/long_polling">>},
  3201. {<<"trailer">>, <<"x-checksum">>}
  3202. ]),
  3203. {TrailersBlock, _} = cow_hpack:encode([
  3204. {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
  3205. ], EncodeState),
  3206. Len = iolist_size(TrailersBlock),
  3207. ok = gen_tcp:send(Socket, [
  3208. cow_http2:headers(1, nofin, HeadersBlock),
  3209. cow_http2:data(1, nofin, <<0:10000/unit:8>>),
  3210. <<0:24, 1:8, 0:9, 1:31>>,
  3211. <<Len:24, 9:8, 0:5, 1:1, 0:3, 1:31>>,
  3212. TrailersBlock
  3213. ]),
  3214. %% Receive a PROTOCOL_ERROR connection error.
  3215. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  3216. ok.
  3217. headers_informational_nofin(Config) ->
  3218. doc("Informational HEADERS frames must not have the END_STREAM flag set. (RFC7540 8.1)"),
  3219. {ok, Socket} = do_handshake(Config),
  3220. %% Send a HEADERS frame on an idle stream.
  3221. {HeadersBlock, _} = cow_hpack:encode([
  3222. {<<":method">>, <<"POST">>},
  3223. {<<":scheme">>, <<"http">>},
  3224. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3225. {<<":path">>, <<"/echo/read_body">>},
  3226. {<<"expect">>, <<"100-continue">>},
  3227. {<<"content-length">>, <<"1000000">>}
  3228. ]),
  3229. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  3230. %% Receive an informational HEADERS frame without the END_STREAM flag.
  3231. {ok, << Len:24, 1:8, 0:5, 1:1, 0:2, _:32 >>} = gen_tcp:recv(Socket, 9, 6000),
  3232. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len, 6000),
  3233. %% Confirm it has a 100 status code.
  3234. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  3235. {_, <<"100">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  3236. ok.
  3237. %% @todo This one is interesting to implement because Cowboy DOES this.
  3238. % A server can
  3239. % send a complete response prior to the client sending an entire
  3240. % request if the response does not depend on any portion of the request
  3241. % that has not been sent and received. When this is true, a server MAY
  3242. % request that the client abort transmission of a request without error
  3243. % by sending a RST_STREAM with an error code of NO_ERROR after sending
  3244. % a complete response (i.e., a frame with the END_STREAM flag).
  3245. headers_reject_uppercase_header_name(Config) ->
  3246. doc("Requests containing uppercase header names must be rejected "
  3247. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2, RFC7540 8.1.2.6)"),
  3248. {ok, Socket} = do_handshake(Config),
  3249. %% Send a HEADERS frame with a uppercase header name.
  3250. {HeadersBlock, _} = cow_hpack:encode([
  3251. {<<":method">>, <<"GET">>},
  3252. {<<":scheme">>, <<"http">>},
  3253. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3254. {<<":path">>, <<"/">>},
  3255. {<<"HELLO">>, <<"world">>}
  3256. ]),
  3257. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3258. %% Receive a PROTOCOL_ERROR stream error.
  3259. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3260. ok.
  3261. reject_response_pseudo_headers(Config) ->
  3262. doc("Requests containing response pseudo-headers must be rejected "
  3263. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
  3264. {ok, Socket} = do_handshake(Config),
  3265. %% Send a HEADERS frame with a response pseudo-header.
  3266. {HeadersBlock, _} = cow_hpack:encode([
  3267. {<<":method">>, <<"GET">>},
  3268. {<<":scheme">>, <<"http">>},
  3269. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3270. {<<":path">>, <<"/">>},
  3271. {<<":status">>, <<"200">>}
  3272. ]),
  3273. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3274. %% Receive a PROTOCOL_ERROR stream error.
  3275. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3276. ok.
  3277. reject_unknown_pseudo_headers(Config) ->
  3278. doc("Requests containing unknown pseudo-headers must be rejected "
  3279. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
  3280. {ok, Socket} = do_handshake(Config),
  3281. %% Send a HEADERS frame with an unknown pseudo-header.
  3282. {HeadersBlock, _} = cow_hpack:encode([
  3283. {<<":method">>, <<"GET">>},
  3284. {<<":scheme">>, <<"http">>},
  3285. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3286. {<<":path">>, <<"/">>},
  3287. {<<":upgrade">>, <<"websocket">>}
  3288. ]),
  3289. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3290. %% Receive a PROTOCOL_ERROR stream error.
  3291. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3292. ok.
  3293. reject_pseudo_headers_in_trailers(Config) ->
  3294. doc("Requests containing pseudo-headers in trailers must be rejected "
  3295. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
  3296. {ok, Socket} = do_handshake(Config),
  3297. %% Send a request containing DATA and trailing HEADERS frames.
  3298. %% The trailing HEADERS contains pseudo-headers.
  3299. {HeadersBlock, EncodeState} = cow_hpack:encode([
  3300. {<<":method">>, <<"POST">>},
  3301. {<<":scheme">>, <<"http">>},
  3302. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3303. {<<":path">>, <<"/long_polling">>},
  3304. {<<"trailer">>, <<"x-checksum">>}
  3305. ]),
  3306. {TrailersBlock, _} = cow_hpack:encode([
  3307. {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>},
  3308. {<<":path">>, <<"/">>}
  3309. ], EncodeState),
  3310. ok = gen_tcp:send(Socket, [
  3311. cow_http2:headers(1, nofin, HeadersBlock),
  3312. cow_http2:data(1, nofin, <<0:10000/unit:8>>),
  3313. cow_http2:headers(1, fin, TrailersBlock)
  3314. ]),
  3315. %% Receive a PROTOCOL_ERROR stream error.
  3316. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3317. ok.
  3318. reject_pseudo_headers_after_regular_headers(Config) ->
  3319. doc("Requests containing pseudo-headers after regular headers must be rejected "
  3320. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
  3321. {ok, Socket} = do_handshake(Config),
  3322. %% Send a HEADERS frame with a pseudo-header after regular headers.
  3323. {HeadersBlock, _} = cow_hpack:encode([
  3324. {<<":method">>, <<"GET">>},
  3325. {<<":scheme">>, <<"http">>},
  3326. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3327. {<<"content-length">>, <<"0">>},
  3328. {<<":path">>, <<"/">>}
  3329. ]),
  3330. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3331. %% Receive a PROTOCOL_ERROR stream error.
  3332. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3333. ok.
  3334. reject_connection_header(Config) ->
  3335. doc("Requests containing a connection header must be rejected "
  3336. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
  3337. {ok, Socket} = do_handshake(Config),
  3338. %% Send a HEADERS frame with a connection header.
  3339. {HeadersBlock, _} = cow_hpack:encode([
  3340. {<<":method">>, <<"GET">>},
  3341. {<<":scheme">>, <<"http">>},
  3342. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3343. {<<":path">>, <<"/">>},
  3344. {<<"connection">>, <<"close">>}
  3345. ]),
  3346. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3347. %% Receive a PROTOCOL_ERROR stream error.
  3348. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3349. ok.
  3350. reject_keep_alive_header(Config) ->
  3351. doc("Requests containing a keep-alive header must be rejected "
  3352. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
  3353. {ok, Socket} = do_handshake(Config),
  3354. %% Send a HEADERS frame with a keep-alive header.
  3355. {HeadersBlock, _} = cow_hpack:encode([
  3356. {<<":method">>, <<"GET">>},
  3357. {<<":scheme">>, <<"http">>},
  3358. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3359. {<<":path">>, <<"/">>},
  3360. {<<"keep-alive">>, <<"timeout=5, max=1000">>}
  3361. ]),
  3362. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3363. %% Receive a PROTOCOL_ERROR stream error.
  3364. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3365. ok.
  3366. reject_proxy_authenticate_header(Config) ->
  3367. doc("Requests containing a connection header must be rejected "
  3368. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
  3369. {ok, Socket} = do_handshake(Config),
  3370. %% Send a HEADERS frame with a proxy-authenticate header.
  3371. {HeadersBlock, _} = cow_hpack:encode([
  3372. {<<":method">>, <<"GET">>},
  3373. {<<":scheme">>, <<"http">>},
  3374. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3375. {<<":path">>, <<"/">>},
  3376. {<<"proxy-authenticate">>, <<"Basic">>}
  3377. ]),
  3378. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3379. %% Receive a PROTOCOL_ERROR stream error.
  3380. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3381. ok.
  3382. reject_proxy_authorization_header(Config) ->
  3383. doc("Requests containing a connection header must be rejected "
  3384. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
  3385. {ok, Socket} = do_handshake(Config),
  3386. %% Send a HEADERS frame with a proxy-authorization header.
  3387. {HeadersBlock, _} = cow_hpack:encode([
  3388. {<<":method">>, <<"GET">>},
  3389. {<<":scheme">>, <<"http">>},
  3390. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3391. {<<":path">>, <<"/">>},
  3392. {<<"proxy-authorization">>, <<"Basic YWxhZGRpbjpvcGVuc2VzYW1l">>}
  3393. ]),
  3394. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3395. %% Receive a PROTOCOL_ERROR stream error.
  3396. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3397. ok.
  3398. reject_transfer_encoding_header(Config) ->
  3399. doc("Requests containing a connection header must be rejected "
  3400. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
  3401. {ok, Socket} = do_handshake(Config),
  3402. %% Send a HEADERS frame with a transfer-encoding header.
  3403. {HeadersBlock, _} = cow_hpack:encode([
  3404. {<<":method">>, <<"GET">>},
  3405. {<<":scheme">>, <<"http">>},
  3406. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3407. {<<":path">>, <<"/">>},
  3408. {<<"transfer-encoding">>, <<"chunked">>}
  3409. ]),
  3410. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3411. %% Receive a PROTOCOL_ERROR stream error.
  3412. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3413. ok.
  3414. reject_upgrade_header(Config) ->
  3415. doc("Requests containing a connection header must be rejected "
  3416. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
  3417. {ok, Socket} = do_handshake(Config),
  3418. %% Send a HEADERS frame with a upgrade header.
  3419. {HeadersBlock, _} = cow_hpack:encode([
  3420. {<<":method">>, <<"GET">>},
  3421. {<<":scheme">>, <<"http">>},
  3422. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3423. {<<":path">>, <<"/">>},
  3424. {<<"upgrade">>, <<"websocket">>}
  3425. ]),
  3426. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3427. %% Receive a PROTOCOL_ERROR stream error.
  3428. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3429. ok.
  3430. accept_te_header_value_trailers(Config) ->
  3431. doc("Requests containing a TE header with a value of \"trailers\" "
  3432. "must be accepted. (RFC7540 8.1.2.2)"),
  3433. {ok, Socket} = do_handshake(Config),
  3434. %% Send a HEADERS frame with a TE header with value "trailers".
  3435. {HeadersBlock, _} = cow_hpack:encode([
  3436. {<<":method">>, <<"GET">>},
  3437. {<<":scheme">>, <<"http">>},
  3438. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3439. {<<":path">>, <<"/">>},
  3440. {<<"te">>, <<"trailers">>}
  3441. ]),
  3442. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3443. %% Receive a response.
  3444. {ok, << _:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  3445. ok.
  3446. reject_te_header_other_values(Config) ->
  3447. doc("Requests containing a TE header with a value other than \"trailers\" must be rejected "
  3448. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
  3449. {ok, Socket} = do_handshake(Config),
  3450. %% Send a HEADERS frame with a TE header with a different value.
  3451. {HeadersBlock, _} = cow_hpack:encode([
  3452. {<<":method">>, <<"GET">>},
  3453. {<<":scheme">>, <<"http">>},
  3454. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3455. {<<":path">>, <<"/">>},
  3456. {<<"te">>, <<"trailers, deflate;q=0.5">>}
  3457. ]),
  3458. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3459. %% Receive a PROTOCOL_ERROR stream error.
  3460. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3461. ok.
  3462. %% (RFC7540 8.1.2.2)
  3463. % This means that an intermediary transforming an HTTP/1.x message to
  3464. % HTTP/2 will need to remove any header fields nominated by the
  3465. % Connection header field, along with the Connection header field
  3466. % itself. Such intermediaries SHOULD also remove other connection-
  3467. % specific header fields, such as Keep-Alive, Proxy-Connection,
  3468. % Transfer-Encoding, and Upgrade, even if they are not nominated by the
  3469. % Connection header field.
  3470. reject_userinfo(Config) ->
  3471. doc("An authority containing a userinfo component must be rejected "
  3472. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3473. {ok, Socket} = do_handshake(Config),
  3474. %% Send a HEADERS frame with a userinfo authority component.
  3475. {HeadersBlock, _} = cow_hpack:encode([
  3476. {<<":method">>, <<"GET">>},
  3477. {<<":scheme">>, <<"http">>},
  3478. {<<":authority">>, <<"user@localhost">>}, %% @todo Correct port number.
  3479. {<<":path">>, <<"/">>}
  3480. ]),
  3481. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3482. %% Receive a PROTOCOL_ERROR stream error.
  3483. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3484. ok.
  3485. %% (RFC7540 8.1.2.3)
  3486. % To ensure that the HTTP/1.1 request line can be reproduced
  3487. % accurately, this pseudo-header field MUST be omitted when
  3488. % translating from an HTTP/1.1 request that has a request target in
  3489. % origin or asterisk form (see [RFC7230], Section 5.3). Clients
  3490. % that generate HTTP/2 requests directly SHOULD use the ":authority"
  3491. % pseudo-header field instead of the Host header field. An
  3492. % intermediary that converts an HTTP/2 request to HTTP/1.1 MUST
  3493. % create a Host header field if one is not present in a request by
  3494. % copying the value of the ":authority" pseudo-header field.
  3495. reject_empty_path(Config) ->
  3496. doc("A request containing an empty path component must be rejected "
  3497. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3498. {ok, Socket} = do_handshake(Config),
  3499. %% Send a HEADERS frame with an empty path component.
  3500. {HeadersBlock, _} = cow_hpack:encode([
  3501. {<<":method">>, <<"GET">>},
  3502. {<<":scheme">>, <<"http">>},
  3503. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3504. {<<":path">>, <<>>}
  3505. ]),
  3506. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3507. %% Receive a PROTOCOL_ERROR stream error.
  3508. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3509. ok.
  3510. reject_missing_pseudo_header_method(Config) ->
  3511. doc("A request without a method component must be rejected "
  3512. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3513. {ok, Socket} = do_handshake(Config),
  3514. %% Send a HEADERS frame without a :method pseudo-header.
  3515. {HeadersBlock, _} = cow_hpack:encode([
  3516. {<<":scheme">>, <<"http">>},
  3517. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3518. {<<":path">>, <<"/">>}
  3519. ]),
  3520. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3521. %% Receive a PROTOCOL_ERROR stream error.
  3522. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3523. ok.
  3524. reject_many_pseudo_header_method(Config) ->
  3525. doc("A request containing more than one method component must be rejected "
  3526. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3527. {ok, Socket} = do_handshake(Config),
  3528. %% Send a HEADERS frame with more than one :method pseudo-header.
  3529. {HeadersBlock, _} = cow_hpack:encode([
  3530. {<<":method">>, <<"GET">>},
  3531. {<<":method">>, <<"GET">>},
  3532. {<<":scheme">>, <<"http">>},
  3533. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3534. {<<":path">>, <<"/">>}
  3535. ]),
  3536. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3537. %% Receive a PROTOCOL_ERROR stream error.
  3538. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3539. ok.
  3540. reject_missing_pseudo_header_scheme(Config) ->
  3541. doc("A request without a scheme component must be rejected "
  3542. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3543. {ok, Socket} = do_handshake(Config),
  3544. %% Send a HEADERS frame without a :scheme pseudo-header.
  3545. {HeadersBlock, _} = cow_hpack:encode([
  3546. {<<":method">>, <<"GET">>},
  3547. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3548. {<<":path">>, <<"/">>}
  3549. ]),
  3550. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3551. %% Receive a PROTOCOL_ERROR stream error.
  3552. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3553. ok.
  3554. reject_many_pseudo_header_scheme(Config) ->
  3555. doc("A request containing more than one scheme component must be rejected "
  3556. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3557. {ok, Socket} = do_handshake(Config),
  3558. %% Send a HEADERS frame with more than one :scheme pseudo-header.
  3559. {HeadersBlock, _} = cow_hpack:encode([
  3560. {<<":method">>, <<"GET">>},
  3561. {<<":scheme">>, <<"http">>},
  3562. {<<":scheme">>, <<"http">>},
  3563. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3564. {<<":path">>, <<"/">>}
  3565. ]),
  3566. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3567. %% Receive a PROTOCOL_ERROR stream error.
  3568. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3569. ok.
  3570. reject_missing_pseudo_header_authority(Config) ->
  3571. doc("A request without an authority or host component must be rejected "
  3572. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3573. {ok, Socket} = do_handshake(Config),
  3574. %% Send a HEADERS frame without an :authority pseudo-header.
  3575. {HeadersBlock, _} = cow_hpack:encode([
  3576. {<<":method">>, <<"GET">>},
  3577. {<<":scheme">>, <<"http">>},
  3578. {<<":path">>, <<"/">>}
  3579. ]),
  3580. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3581. %% Receive a PROTOCOL_ERROR stream error.
  3582. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3583. ok.
  3584. accept_host_header_on_missing_pseudo_header_authority(Config) ->
  3585. doc("A request without an authority but with a host header must be accepted. "
  3586. "(RFC7540 8.1.2.3, RFC7540 8.1.3)"),
  3587. {ok, Socket} = do_handshake(Config),
  3588. %% Send a HEADERS frame with host header and without an :authority pseudo-header.
  3589. {HeadersBlock, _} = cow_hpack:encode([
  3590. {<<":method">>, <<"GET">>},
  3591. {<<":scheme">>, <<"http">>},
  3592. {<<":path">>, <<"/">>},
  3593. {<<"host">>, <<"localhost">>}
  3594. ]),
  3595. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3596. %% Receive a 200 response.
  3597. {ok, << Len:24, 1:8, _:8, _:32 >>} = gen_tcp:recv(Socket, 9, 6000),
  3598. {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len, 6000),
  3599. {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
  3600. {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
  3601. ok.
  3602. %% When both :authority and host headers are received, the current behavior
  3603. %% is to favor :authority and ignore the host header. The specification does
  3604. %% not describe the correct behavior to follow in that case.
  3605. reject_many_pseudo_header_authority(Config) ->
  3606. doc("A request containing more than one authority component must be rejected "
  3607. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3608. {ok, Socket} = do_handshake(Config),
  3609. %% Send a HEADERS frame with more than one :authority pseudo-header.
  3610. {HeadersBlock, _} = cow_hpack:encode([
  3611. {<<":method">>, <<"GET">>},
  3612. {<<":scheme">>, <<"http">>},
  3613. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3614. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3615. {<<":path">>, <<"/">>}
  3616. ]),
  3617. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3618. %% Receive a PROTOCOL_ERROR stream error.
  3619. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3620. ok.
  3621. reject_missing_pseudo_header_path(Config) ->
  3622. doc("A request without a path component must be rejected "
  3623. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3624. {ok, Socket} = do_handshake(Config),
  3625. %% Send a HEADERS frame without a :path pseudo-header.
  3626. {HeadersBlock, _} = cow_hpack:encode([
  3627. {<<":method">>, <<"GET">>},
  3628. {<<":scheme">>, <<"http">>},
  3629. {<<":authority">>, <<"localhost">>} %% @todo Correct port number.
  3630. ]),
  3631. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3632. %% Receive a PROTOCOL_ERROR stream error.
  3633. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3634. ok.
  3635. reject_many_pseudo_header_path(Config) ->
  3636. doc("A request containing more than one path component must be rejected "
  3637. "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
  3638. {ok, Socket} = do_handshake(Config),
  3639. %% Send a HEADERS frame with more than one :path pseudo-header.
  3640. {HeadersBlock, _} = cow_hpack:encode([
  3641. {<<":method">>, <<"GET">>},
  3642. {<<":scheme">>, <<"http">>},
  3643. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3644. {<<":path">>, <<"/">>},
  3645. {<<":path">>, <<"/">>}
  3646. ]),
  3647. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3648. %% Receive a PROTOCOL_ERROR stream error.
  3649. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3650. ok.
  3651. %% (RFC7540 8.1.2.4)
  3652. % For HTTP/2 responses, a single ":status" pseudo-header field is
  3653. % defined that carries the HTTP status code field (see [RFC7231],
  3654. % Section 6). This pseudo-header field MUST be included in all
  3655. % responses; otherwise, the response is malformed (Section 8.1.2.6).
  3656. %% (RFC7540 8.1.2.5)
  3657. % To allow for better compression efficiency, the Cookie header field
  3658. % MAY be split into separate header fields, each with one or more
  3659. % cookie-pairs. If there are multiple Cookie header fields after
  3660. % decompression, these MUST be concatenated into a single octet string
  3661. % using the two-octet delimiter of 0x3B, 0x20 (the ASCII string "; ")
  3662. % before being passed into a non-HTTP/2 context, such as an HTTP/1.1
  3663. % connection, or a generic HTTP server application.
  3664. reject_data_size_smaller_than_content_length(Config) ->
  3665. doc("Requests that have a content-length header whose value does not "
  3666. "match the total length of the DATA frames must be rejected with "
  3667. "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
  3668. {ok, Socket} = do_handshake(Config),
  3669. %% Send a HEADERS frame with a content-length header different
  3670. %% than the sum of the DATA frame sizes.
  3671. {HeadersBlock, _} = cow_hpack:encode([
  3672. {<<":method">>, <<"POST">>},
  3673. {<<":scheme">>, <<"http">>},
  3674. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3675. {<<":path">>, <<"/long_polling">>},
  3676. {<<"content-length">>, <<"12">>}
  3677. ]),
  3678. ok = gen_tcp:send(Socket, [
  3679. cow_http2:headers(1, nofin, HeadersBlock),
  3680. cow_http2:data(1, fin, <<"Hello!">>)
  3681. ]),
  3682. %% Receive a PROTOCOL_ERROR stream error.
  3683. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3684. ok.
  3685. reject_data_size_larger_than_content_length(Config) ->
  3686. doc("Requests that have a content-length header whose value does not "
  3687. "match the total length of the DATA frames must be rejected with "
  3688. "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
  3689. {ok, Socket} = do_handshake(Config),
  3690. %% Send a HEADERS frame with a content-length header different
  3691. %% than the sum of the DATA frame sizes.
  3692. {HeadersBlock, _} = cow_hpack:encode([
  3693. {<<":method">>, <<"POST">>},
  3694. {<<":scheme">>, <<"http">>},
  3695. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3696. {<<":path">>, <<"/long_polling">>},
  3697. {<<"content-length">>, <<"12">>}
  3698. ]),
  3699. ok = gen_tcp:send(Socket, [
  3700. cow_http2:headers(1, nofin, HeadersBlock),
  3701. cow_http2:data(1, nofin, <<"Hello! World! Universe!">>),
  3702. cow_http2:data(1, fin, <<"Multiverse!">>)
  3703. ]),
  3704. %% Receive a PROTOCOL_ERROR stream error.
  3705. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3706. ok.
  3707. reject_content_length_without_data(Config) ->
  3708. doc("Requests that have a content-length header whose value does not "
  3709. "match the total length of the DATA frames must be rejected with "
  3710. "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
  3711. {ok, Socket} = do_handshake(Config),
  3712. %% Send a HEADERS frame with a content-length header different
  3713. %% than the sum of the DATA frame sizes.
  3714. {HeadersBlock, _} = cow_hpack:encode([
  3715. {<<":method">>, <<"POST">>},
  3716. {<<":scheme">>, <<"http">>},
  3717. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3718. {<<":path">>, <<"/long_polling">>},
  3719. {<<"content-length">>, <<"12">>}
  3720. ]),
  3721. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  3722. %% Receive a PROTOCOL_ERROR stream error.
  3723. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3724. ok.
  3725. reject_data_size_different_than_content_length_with_trailers(Config) ->
  3726. doc("Requests that have a content-length header whose value does not "
  3727. "match the total length of the DATA frames must be rejected with "
  3728. "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
  3729. {ok, Socket} = do_handshake(Config),
  3730. %% Send a HEADERS frame with a content-length header different
  3731. %% than the sum of the DATA frame sizes.
  3732. {HeadersBlock, EncodeState} = cow_hpack:encode([
  3733. {<<":method">>, <<"POST">>},
  3734. {<<":scheme">>, <<"http">>},
  3735. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3736. {<<":path">>, <<"/long_polling">>},
  3737. {<<"content-length">>, <<"12">>},
  3738. {<<"trailer">>, <<"x-checksum">>}
  3739. ]),
  3740. {TrailersBlock, _} = cow_hpack:encode([
  3741. {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
  3742. ], EncodeState),
  3743. ok = gen_tcp:send(Socket, [
  3744. cow_http2:headers(1, nofin, HeadersBlock),
  3745. cow_http2:data(1, nofin, <<"Hello!">>),
  3746. cow_http2:headers(1, fin, TrailersBlock)
  3747. ]),
  3748. %% Receive a PROTOCOL_ERROR stream error.
  3749. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3750. ok.
  3751. reject_duplicate_content_length_header(Config) ->
  3752. doc("A request with duplicate content-length headers must be rejected "
  3753. "with a PROTOCOL_ERROR stream error. (RFC7230 3.3.2, RFC7540 8.1.2.6)"),
  3754. {ok, Socket} = do_handshake(Config),
  3755. %% Send a HEADERS frame with more than one content-length header.
  3756. {HeadersBlock, _} = cow_hpack:encode([
  3757. {<<":method">>, <<"GET">>},
  3758. {<<":scheme">>, <<"http">>},
  3759. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  3760. {<<":path">>, <<"/">>},
  3761. {<<"content-length">>, <<"12">>},
  3762. {<<"content-length">>, <<"12">>}
  3763. ]),
  3764. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  3765. %% Receive a PROTOCOL_ERROR stream error.
  3766. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  3767. ok.
  3768. % Intermediaries that process HTTP requests or responses (i.e., any
  3769. % intermediary not acting as a tunnel) MUST NOT forward a malformed
  3770. % request or response. Malformed requests or responses that are
  3771. % detected MUST be treated as a stream error (Section 5.4.2) of type
  3772. % PROTOCOL_ERROR.
  3773. %
  3774. % For malformed requests, a server MAY send an HTTP response prior to
  3775. % closing or resetting the stream. Clients MUST NOT accept a malformed
  3776. % response. Note that these requirements are intended to protect
  3777. % against several types of common attacks against HTTP; they are
  3778. % deliberately strict because being permissive can expose
  3779. % implementations to these vulnerabilities.
  3780. %% @todo It migh be worth reproducing the good examples. (RFC7540 8.1.3)
  3781. %% (RFC7540 8.1.4)
  3782. % A server MUST NOT indicate that a stream has not been processed
  3783. % unless it can guarantee that fact. If frames that are on a stream
  3784. % are passed to the application layer for any stream, then
  3785. % REFUSED_STREAM MUST NOT be used for that stream, and a GOAWAY frame
  3786. % MUST include a stream identifier that is greater than or equal to the
  3787. % given stream identifier.
  3788. %% (RFC7540 8.2)
  3789. % Promised requests MUST be cacheable (see [RFC7231], Section 4.2.3),
  3790. % MUST be safe (see [RFC7231], Section 4.2.1), and MUST NOT include a
  3791. % request body.
  3792. %
  3793. % The server MUST include a value in the ":authority" pseudo-header
  3794. % field for which the server is authoritative (see Section 10.1).
  3795. %
  3796. % A client cannot push. Thus, servers MUST treat the receipt of a
  3797. % PUSH_PROMISE frame as a connection error (Section 5.4.1) of type
  3798. % PROTOCOL_ERROR.
  3799. %% (RFC7540 8.2.1)
  3800. % The header fields in PUSH_PROMISE and any subsequent CONTINUATION
  3801. % frames MUST be a valid and complete set of request header fields
  3802. % (Section 8.1.2.3). The server MUST include a method in the ":method"
  3803. % pseudo-header field that is safe and cacheable. If a client receives
  3804. % a PUSH_PROMISE that does not include a complete and valid set of
  3805. % header fields or the ":method" pseudo-header field identifies a
  3806. % method that is not safe, it MUST respond with a stream error
  3807. % (Section 5.4.2) of type PROTOCOL_ERROR.
  3808. %
  3809. %% @todo This probably should be documented.
  3810. % The server SHOULD send PUSH_PROMISE (Section 6.6) frames prior to
  3811. % sending any frames that reference the promised responses. This
  3812. % avoids a race where clients issue requests prior to receiving any
  3813. % PUSH_PROMISE frames.
  3814. %
  3815. % PUSH_PROMISE frames MUST NOT be sent by the client.
  3816. %
  3817. % PUSH_PROMISE frames can be sent by the server in response to any
  3818. % client-initiated stream, but the stream MUST be in either the "open"
  3819. % or "half-closed (remote)" state with respect to the server.
  3820. % PUSH_PROMISE frames are interspersed with the frames that comprise a
  3821. % response, though they cannot be interspersed with HEADERS and
  3822. % CONTINUATION frames that comprise a single header block.
  3823. %% (RFC7540 8.2.2)
  3824. % If the client determines, for any reason, that it does not wish to
  3825. % receive the pushed response from the server or if the server takes
  3826. % too long to begin sending the promised response, the client can send
  3827. % a RST_STREAM frame, using either the CANCEL or REFUSED_STREAM code
  3828. % and referencing the pushed stream's identifier.
  3829. %
  3830. % A client can use the SETTINGS_MAX_CONCURRENT_STREAMS setting to limit
  3831. % the number of responses that can be concurrently pushed by a server.
  3832. % Advertising a SETTINGS_MAX_CONCURRENT_STREAMS value of zero disables
  3833. % server push by preventing the server from creating the necessary
  3834. % streams. This does not prohibit a server from sending PUSH_PROMISE
  3835. % frames; clients need to reset any promised streams that are not
  3836. % wanted.
  3837. %% @todo Implement CONNECT. (RFC7540 8.3)
  3838. status_code_421(Config) ->
  3839. doc("The 421 Misdirected Request status code can be sent. (RFC7540 9.1.2)"),
  3840. ConnPid = gun_open(Config),
  3841. Ref = gun:get(ConnPid, "/resp/reply2/421"),
  3842. {response, fin, 421, _} = gun:await(ConnPid, Ref),
  3843. ok.
  3844. %% @todo Review (RFC7540 9.2, 9.2.1, 9.2.2) TLS 1.2 usage.
  3845. %% We probably want different ways to enforce these to simplify the life
  3846. %% of users. A function cowboy:start_h2_tls could do the same as start_tls
  3847. %% but with the security requirements of HTTP/2 enforced. Another way is to
  3848. %% have an option at the establishment of the connection that checks that
  3849. %% the security of the connection is adequate.
  3850. %% (RFC7540 10.3)
  3851. % The HTTP/2 header field encoding allows the expression of names that
  3852. % are not valid field names in the Internet Message Syntax used by
  3853. % HTTP/1.1. Requests or responses containing invalid header field
  3854. % names MUST be treated as malformed (Section 8.1.2.6).
  3855. %
  3856. % Similarly, HTTP/2 allows header field values that are not valid.
  3857. % While most of the values that can be encoded will not alter header
  3858. % field parsing, carriage return (CR, ASCII 0xd), line feed (LF, ASCII
  3859. % 0xa), and the zero character (NUL, ASCII 0x0) might be exploited by
  3860. % an attacker if they are translated verbatim. Any request or response
  3861. % that contains a character not permitted in a header field value MUST
  3862. % be treated as malformed (Section 8.1.2.6). Valid characters are
  3863. % defined by the "field-content" ABNF rule in Section 3.2 of [RFC7230].
  3864. %% (RFC7540 10.5) Denial-of-Service Considerations
  3865. % An endpoint that doesn't monitor this behavior exposes itself to a
  3866. % risk of denial-of-service attack. Implementations SHOULD track the
  3867. % use of these features and set limits on their use. An endpoint MAY
  3868. % treat activity that is suspicious as a connection error
  3869. % (Section 5.4.1) of type ENHANCE_YOUR_CALM.
  3870. %% (RFC7540 10.5.1)
  3871. % A server that receives a larger header block than it is willing to
  3872. % handle can send an HTTP 431 (Request Header Fields Too Large) status
  3873. % code [RFC6585]. A client can discard responses that it cannot
  3874. % process. The header block MUST be processed to ensure a consistent
  3875. % connection state, unless the connection is closed.
  3876. %% @todo Implement CONNECT and limit the number of CONNECT streams (RFC7540 10.5.2).
  3877. %% @todo This probably should be documented. (RFC7540 10.6)
  3878. % Implementations communicating on a secure channel MUST NOT compress
  3879. % content that includes both confidential and attacker-controlled data
  3880. % unless separate compression dictionaries are used for each source of
  3881. % data. Compression MUST NOT be used if the source of data cannot be
  3882. % reliably determined. Generic stream compression, such as that
  3883. % provided by TLS, MUST NOT be used with HTTP/2 (see Section 9.2).
  3884. %% (RFC7540 A)
  3885. % An HTTP/2 implementation MAY treat the negotiation of any of the
  3886. % following cipher suites with TLS 1.2 as a connection error
  3887. % (Section 5.4.1) of type INADEQUATE_SECURITY.