rfc7540_SUITE.erl 174 KB

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