rfc7540_SUITE.erl 145 KB

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