rfc7540_SUITE.erl 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  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. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [raw_open/1]).
  19. -import(cowboy_test, [raw_send/2]).
  20. -import(cowboy_test, [raw_recv_head/1]).
  21. -import(cowboy_test, [raw_recv/3]).
  22. all() -> [{group, clear}, {group, tls}].
  23. groups() ->
  24. Modules = ct_helper:all(?MODULE),
  25. Clear = [M || M <- Modules, lists:sublist(atom_to_list(M), 4) =/= "alpn"] -- [prior_knowledge_reject_tls],
  26. TLS = [M || M <- Modules, lists:sublist(atom_to_list(M), 4) =:= "alpn"] ++ [prior_knowledge_reject_tls],
  27. [{clear, [parallel], Clear}, {tls, [parallel], TLS}].
  28. init_per_group(Name = clear, Config) ->
  29. cowboy_test:init_http(Name = clear, #{
  30. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  31. }, Config);
  32. init_per_group(Name = tls, Config) ->
  33. cowboy_test:init_http2(Name = tls, #{
  34. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  35. }, Config).
  36. end_per_group(Name, _) ->
  37. ok = cowboy:stop_listener(Name).
  38. init_routes(_) -> [
  39. {"localhost", [
  40. {"/", hello_h, []},
  41. {"/echo/:key", echo_h, []}
  42. ]}
  43. ].
  44. %% Starting HTTP/2 for "http" URIs.
  45. http_upgrade_ignore_h2(Config) ->
  46. doc("An h2 token in an Upgrade field must be ignored. (RFC7540 3.2)"),
  47. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  48. ok = gen_tcp:send(Socket, [
  49. "GET / HTTP/1.1\r\n"
  50. "Host: localhost\r\n"
  51. "Connection: Upgrade, HTTP2-Settings\r\n"
  52. "Upgrade: h2\r\n"
  53. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  54. "\r\n"]),
  55. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  56. ok.
  57. http_upgrade_ignore_if_http_10(Config) ->
  58. doc("The Upgrade header must be ignored if part of an HTTP/1.0 request. (RFC7230 6.7)"),
  59. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  60. ok = gen_tcp:send(Socket, [
  61. "GET / HTTP/1.0\r\n"
  62. "Host: localhost\r\n"
  63. "Connection: Upgrade, HTTP2-Settings\r\n"
  64. "Upgrade: h2c\r\n"
  65. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  66. "\r\n"]),
  67. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  68. ok.
  69. http_upgrade_ignore_missing_upgrade_in_connection(Config) ->
  70. doc("The Upgrade header must be listed in the "
  71. "Connection header field. (RFC7230 6.7)"),
  72. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  73. ok = gen_tcp:send(Socket, [
  74. "GET / HTTP/1.1\r\n"
  75. "Host: localhost\r\n"
  76. "Connection: HTTP2-Settings\r\n"
  77. "Upgrade: h2c\r\n"
  78. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  79. "\r\n"]),
  80. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  81. ok.
  82. http_upgrade_ignore_missing_http2_settings_in_connection(Config) ->
  83. doc("The HTTP2-Settings header must be listed in the "
  84. "Connection header field. (RFC7540 3.2.1, RFC7230 6.7)"),
  85. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  86. ok = gen_tcp:send(Socket, [
  87. "GET / HTTP/1.1\r\n"
  88. "Host: localhost\r\n"
  89. "Connection: Upgrade\r\n"
  90. "Upgrade: h2c\r\n"
  91. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  92. "\r\n"]),
  93. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  94. ok.
  95. http_upgrade_ignore_zero_http2_settings_header(Config) ->
  96. doc("The HTTP Upgrade request must include "
  97. "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  98. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  99. ok = gen_tcp:send(Socket, [
  100. "GET / HTTP/1.1\r\n"
  101. "Host: localhost\r\n"
  102. "Connection: Upgrade, HTTP2-Settings\r\n"
  103. "Upgrade: h2c\r\n"
  104. "\r\n"]),
  105. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  106. ok.
  107. http_upgrade_reject_two_http2_settings_header(Config) ->
  108. doc("The HTTP Upgrade request must include "
  109. "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  110. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  111. ok = gen_tcp:send(Socket, [
  112. "GET / HTTP/1.1\r\n"
  113. "Host: localhost\r\n"
  114. "Connection: Upgrade, HTTP2-Settings\r\n"
  115. "Upgrade: h2c\r\n"
  116. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  117. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  118. "\r\n"]),
  119. {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
  120. ok.
  121. http_upgrade_reject_bad_http2_settings_header(Config) ->
  122. doc("The HTTP Upgrade request must include "
  123. "a valid HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  124. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  125. ok = gen_tcp:send(Socket, [
  126. "GET / HTTP/1.1\r\n"
  127. "Host: localhost\r\n"
  128. "Connection: Upgrade, HTTP2-Settings\r\n"
  129. "Upgrade: h2c\r\n"
  130. %% We send a full SETTINGS frame on purpose.
  131. "HTTP2-Settings: ", base64:encode(cow_http2:settings(#{})), "\r\n",
  132. "\r\n"]),
  133. {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
  134. ok.
  135. %% Match directly for now.
  136. do_recv_101(Socket) ->
  137. {ok, <<
  138. "HTTP/1.1 101 Switching Protocols\r\n"
  139. "connection: Upgrade\r\n"
  140. "upgrade: h2c\r\n"
  141. "\r\n"
  142. >>} = gen_tcp:recv(Socket, 71, 1000),
  143. ok.
  144. http_upgrade_101(Config) ->
  145. doc("A 101 response must be sent on successful upgrade "
  146. "to HTTP/2 when using the HTTP Upgrade mechanism. (RFC7540 3.2, RFC7230 6.7)"),
  147. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  148. ok = gen_tcp:send(Socket, [
  149. "GET / HTTP/1.1\r\n"
  150. "Host: localhost\r\n"
  151. "Connection: Upgrade, HTTP2-Settings\r\n"
  152. "Upgrade: h2c\r\n"
  153. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  154. "\r\n"]),
  155. ok = do_recv_101(Socket),
  156. ok.
  157. http_upgrade_server_preface(Config) ->
  158. doc("The first frame after the upgrade must be a "
  159. "SETTINGS frame for the server connection preface. (RFC7540 3.2, RFC7540 3.5, RFC7540 6.5)"),
  160. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  161. ok = gen_tcp:send(Socket, [
  162. "GET / HTTP/1.1\r\n"
  163. "Host: localhost\r\n"
  164. "Connection: Upgrade, HTTP2-Settings\r\n"
  165. "Upgrade: h2c\r\n"
  166. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  167. "\r\n"]),
  168. ok = do_recv_101(Socket),
  169. %% Receive the server preface.
  170. {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
  171. ok.
  172. http_upgrade_client_preface_timeout(Config) ->
  173. doc("Clients negotiating HTTP/2 and not sending a preface in "
  174. "a timely manner must be disconnected."),
  175. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  176. ok = gen_tcp:send(Socket, [
  177. "GET / HTTP/1.1\r\n"
  178. "Host: localhost\r\n"
  179. "Connection: Upgrade, HTTP2-Settings\r\n"
  180. "Upgrade: h2c\r\n"
  181. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  182. "\r\n"]),
  183. ok = do_recv_101(Socket),
  184. %% Receive the server preface.
  185. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  186. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  187. %% Receive the response to the initial HTTP/1.1 request.
  188. {ok, << HeadersSkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  189. {ok, _} = gen_tcp:recv(Socket, HeadersSkipLen, 1000),
  190. {ok, << DataSkipLen:24, 0:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  191. {ok, _} = gen_tcp:recv(Socket, DataSkipLen, 1000),
  192. %% Do not send the preface. Wait for the server to disconnect us.
  193. {error, closed} = gen_tcp:recv(Socket, 9, 6000),
  194. ok.
  195. http_upgrade_reject_missing_client_preface(Config) ->
  196. doc("Servers must treat an invalid connection preface as a "
  197. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  198. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  199. ok = gen_tcp:send(Socket, [
  200. "GET / HTTP/1.1\r\n"
  201. "Host: localhost\r\n"
  202. "Connection: Upgrade, HTTP2-Settings\r\n"
  203. "Upgrade: h2c\r\n"
  204. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  205. "\r\n"]),
  206. ok = do_recv_101(Socket),
  207. %% Send a SETTINGS frame directly instead of the proper preface.
  208. ok = gen_tcp:send(Socket, cow_http2:settings(#{})),
  209. %% Receive the server preface.
  210. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  211. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  212. %% We expect the server to close the connection when it receives a bad preface.
  213. %% The server may however have already started sending the response to the
  214. %% initial HTTP/1.1 request.
  215. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  216. case gen_tcp:recv(Socket, 9, 1000) of
  217. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  218. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  219. [headers|Acc];
  220. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  221. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  222. [data|Acc];
  223. {error, _} ->
  224. [closed|Acc]
  225. end
  226. end, [], [1, 2, 3])),
  227. case Received of
  228. [closed|_] -> ok;
  229. [headers, closed|_] -> ok;
  230. [headers, data, closed] -> ok
  231. end.
  232. http_upgrade_reject_invalid_client_preface(Config) ->
  233. doc("Servers must treat an invalid connection preface as a "
  234. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  235. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  236. ok = gen_tcp:send(Socket, [
  237. "GET / HTTP/1.1\r\n"
  238. "Host: localhost\r\n"
  239. "Connection: Upgrade, HTTP2-Settings\r\n"
  240. "Upgrade: h2c\r\n"
  241. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  242. "\r\n"]),
  243. ok = do_recv_101(Socket),
  244. %% Send a slightly incorrect preface.
  245. ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  246. %% Receive the server preface.
  247. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  248. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  249. %% We expect the server to close the connection when it receives a bad preface.
  250. %% The server may however have already started sending the response to the
  251. %% initial HTTP/1.1 request.
  252. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  253. case gen_tcp:recv(Socket, 9, 1000) of
  254. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  255. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  256. [headers|Acc];
  257. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  258. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  259. [data|Acc];
  260. {error, _} ->
  261. [closed|Acc]
  262. end
  263. end, [], [1, 2, 3])),
  264. case Received of
  265. [closed|_] -> ok;
  266. [headers, closed|_] -> ok;
  267. [headers, data, closed] -> ok
  268. end.
  269. http_upgrade_reject_missing_client_preface_settings(Config) ->
  270. doc("Servers must treat an invalid connection preface as a "
  271. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  272. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  273. ok = gen_tcp:send(Socket, [
  274. "GET / HTTP/1.1\r\n"
  275. "Host: localhost\r\n"
  276. "Connection: Upgrade, HTTP2-Settings\r\n"
  277. "Upgrade: h2c\r\n"
  278. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  279. "\r\n"]),
  280. ok = do_recv_101(Socket),
  281. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  282. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  283. %% Receive the server preface.
  284. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  285. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  286. %% We expect the server to close the connection when it receives a bad preface.
  287. %% The server may however have already started sending the response to the
  288. %% initial HTTP/1.1 request.
  289. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  290. case gen_tcp:recv(Socket, 9, 1000) of
  291. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  292. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  293. [headers|Acc];
  294. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  295. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  296. [data|Acc];
  297. {error, _} ->
  298. [closed|Acc]
  299. end
  300. end, [], [1, 2, 3])),
  301. case Received of
  302. [closed|_] -> ok;
  303. [headers, closed|_] -> ok;
  304. [headers, data, closed] -> ok
  305. end.
  306. http_upgrade_reject_invalid_client_preface_settings(Config) ->
  307. doc("Servers must treat an invalid connection preface as a "
  308. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  309. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  310. ok = gen_tcp:send(Socket, [
  311. "GET / HTTP/1.1\r\n"
  312. "Host: localhost\r\n"
  313. "Connection: Upgrade, HTTP2-Settings\r\n"
  314. "Upgrade: h2c\r\n"
  315. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  316. "\r\n"]),
  317. ok = do_recv_101(Socket),
  318. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  319. 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 >>]),
  320. %% Receive the server preface.
  321. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  322. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  323. %% We expect the server to close the connection when it receives a bad preface.
  324. %% The server may however have already started sending the response to the
  325. %% initial HTTP/1.1 request.
  326. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  327. case gen_tcp:recv(Socket, 9, 1000) of
  328. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  329. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  330. [headers|Acc];
  331. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  332. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  333. [data|Acc];
  334. {error, _} ->
  335. [closed|Acc]
  336. end
  337. end, [], [1, 2, 3])),
  338. case Received of
  339. [closed|_] -> ok;
  340. [headers, closed|_] -> ok;
  341. [headers, data, closed] -> ok
  342. end.
  343. http_upgrade_accept_client_preface_empty_settings(Config) ->
  344. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.2, RFC7540 3.5)"),
  345. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  346. ok = gen_tcp:send(Socket, [
  347. "GET / HTTP/1.1\r\n"
  348. "Host: localhost\r\n"
  349. "Connection: Upgrade, HTTP2-Settings\r\n"
  350. "Upgrade: h2c\r\n"
  351. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  352. "\r\n"]),
  353. ok = do_recv_101(Socket),
  354. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  355. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  356. %% Receive the server preface.
  357. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  358. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  359. %% Receive the SETTINGS ack.
  360. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  361. ok.
  362. http_upgrade_client_preface_settings_ack_timeout(Config) ->
  363. doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
  364. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  365. ok = gen_tcp:send(Socket, [
  366. "GET / HTTP/1.1\r\n"
  367. "Host: localhost\r\n"
  368. "Connection: Upgrade, HTTP2-Settings\r\n"
  369. "Upgrade: h2c\r\n"
  370. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  371. "\r\n"]),
  372. ok = do_recv_101(Socket),
  373. %% Send a valid preface.
  374. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  375. %% Receive the server preface.
  376. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  377. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  378. %% Receive the SETTINGS ack.
  379. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  380. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  381. {ok, << _:24, 7:8, _:72, 4:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  382. ok.
  383. %% @todo We need a successful test with actual options in HTTP2-Settings.
  384. %% SETTINGS_MAX_FRAME_SIZE is probably the easiest to test. The relevant
  385. %% RFC quote is:
  386. %%
  387. %% 3.2.1
  388. %% A server decodes and interprets these values as it would any other
  389. %% SETTINGS frame. Explicit acknowledgement of these settings
  390. %% (Section 6.5.3) is not necessary, since a 101 response serves as
  391. %% implicit acknowledgement.
  392. %% @todo We need to test an upgrade with a request body. It is probably
  393. %% worth having a configuration value for how much we accept while still
  394. %% upgrading (if too big, we would just stay on HTTP/1.1). We therefore
  395. %% needs a test for when the body is small enough, and one for when the
  396. %% body is larger than we accept. The relevant RFC quote is:
  397. %%
  398. %% 3.2
  399. %% Requests that contain a payload body MUST be sent in their entirety
  400. %% before the client can send HTTP/2 frames. This means that a large
  401. %% request can block the use of the connection until it is completely
  402. %% sent.
  403. %% @todo We should definitely have a test with OPTIONS. The relevant
  404. %% RFC quote is:
  405. %%
  406. %% 3.2
  407. %% If concurrency of an initial request with subsequent requests is
  408. %% important, an OPTIONS request can be used to perform the upgrade to
  409. %% HTTP/2, at the cost of an additional round trip.
  410. %% @todo If we ever handle priority, we need to check that the initial
  411. %% HTTP/1.1 request has default priority. The relevant RFC quote is:
  412. %%
  413. %% 3.2
  414. %% The HTTP/1.1 request that is sent prior to upgrade is assigned a
  415. %% stream identifier of 1 (see Section 5.1.1) with default priority
  416. %% values (Section 5.3.5).
  417. http_upgrade_response(Config) ->
  418. doc("A response must be sent to the initial HTTP/1.1 request "
  419. "after switching to HTTP/2. The response must use "
  420. "the stream identifier 1. (RFC7540 3.2)"),
  421. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  422. ok = gen_tcp:send(Socket, [
  423. "GET / HTTP/1.1\r\n"
  424. "Host: localhost\r\n"
  425. "Connection: Upgrade, HTTP2-Settings\r\n"
  426. "Upgrade: h2c\r\n"
  427. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  428. "\r\n"]),
  429. ok = do_recv_101(Socket),
  430. %% Send a valid preface.
  431. %% @todo Use non-empty SETTINGS here. Just because.
  432. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  433. %% Receive the server preface.
  434. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  435. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  436. %% Send the SETTINGS ack.
  437. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  438. %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
  439. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  440. case gen_tcp:recv(Socket, 9, 1000) of
  441. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  442. [settings_ack|Acc];
  443. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  444. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  445. [headers|Acc];
  446. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  447. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  448. [data|Acc]
  449. end
  450. end, [], [1, 2, 3])),
  451. case Received of
  452. [settings_ack, headers, data] -> ok;
  453. [headers, settings_ack, data] -> ok;
  454. [headers, data, settings_ack] -> ok
  455. end.
  456. http_upgrade_response_half_closed(Config) ->
  457. doc("The stream for the initial HTTP/1.1 request is half-closed. (RFC7540 3.2)"),
  458. %% Try sending more data after the upgrade and get an error.
  459. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  460. ok = gen_tcp:send(Socket, [
  461. "GET / HTTP/1.1\r\n"
  462. "Host: localhost\r\n"
  463. "Connection: Upgrade, HTTP2-Settings\r\n"
  464. "Upgrade: h2c\r\n"
  465. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  466. "\r\n"]),
  467. ok = do_recv_101(Socket),
  468. %% Send a valid preface followed by an unexpected DATA frame.
  469. ok = gen_tcp:send(Socket, [
  470. "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
  471. cow_http2:settings(#{}),
  472. cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)
  473. ]),
  474. %% Receive the server preface.
  475. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  476. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  477. %% Skip the SETTINGS ack, receive the response HEADERS, DATA and RST_STREAM (streamid 1).
  478. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  479. case gen_tcp:recv(Socket, 9, 1000) of
  480. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  481. Acc;
  482. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  483. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  484. [headers|Acc];
  485. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  486. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  487. [data|Acc];
  488. {ok, << 4:24, 3:8, 0:8, 1:32 >>} ->
  489. %% We expect a STREAM_CLOSED reason.
  490. {ok, << 5:32 >>} = gen_tcp:recv(Socket, 4, 1000),
  491. [rst_stream|Acc];
  492. {error, _} ->
  493. %% Can be timeouts, ignore them.
  494. Acc
  495. end
  496. end, [], [1, 2, 3, 4])),
  497. case Received of
  498. [rst_stream] -> ok;
  499. [headers, rst_stream] -> ok;
  500. [headers, data, rst_stream] -> ok
  501. end.
  502. %% Starting HTTP/2 for "https" URIs.
  503. alpn_ignore_h2c(Config) ->
  504. doc("An h2c ALPN protocol identifier must be ignored. (RFC7540 3.3)"),
  505. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  506. [{alpn_advertised_protocols, [<<"h2c">>, <<"http/1.1">>]}, binary, {active, false}]),
  507. {ok, <<"http/1.1">>} = ssl:negotiated_protocol(Socket),
  508. ok.
  509. alpn_server_preface(Config) ->
  510. doc("The first frame must be a SETTINGS frame "
  511. "for the server connection preface. (RFC7540 3.3, RFC7540 3.5, RFC7540 6.5)"),
  512. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  513. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  514. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  515. %% Receive the server preface.
  516. {ok, << _:24, 4:8, 0:40 >>} = ssl:recv(Socket, 9, 1000),
  517. ok.
  518. alpn_client_preface_timeout(Config) ->
  519. doc("Clients negotiating HTTP/2 and not sending a preface in "
  520. "a timely manner must be disconnected."),
  521. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  522. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  523. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  524. %% Receive the server preface.
  525. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  526. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  527. %% Do not send the preface. Wait for the server to disconnect us.
  528. {error, closed} = ssl:recv(Socket, 3, 6000),
  529. ok.
  530. alpn_reject_missing_client_preface(Config) ->
  531. doc("Servers must treat an invalid connection preface as a "
  532. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  533. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  534. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  535. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  536. %% Send a SETTINGS frame directly instead of the proper preface.
  537. ok = ssl:send(Socket, cow_http2:settings(#{})),
  538. %% Receive the server preface.
  539. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  540. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  541. %% We expect the server to close the connection when it receives a bad preface.
  542. {error, closed} = ssl:recv(Socket, 3, 1000),
  543. ok.
  544. alpn_reject_invalid_client_preface(Config) ->
  545. doc("Servers must treat an invalid connection preface as a "
  546. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  547. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  548. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  549. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  550. %% Send a slightly incorrect preface.
  551. ok = ssl:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  552. %% Receive the server preface.
  553. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  554. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  555. %% We expect the server to close the connection when it receives a bad preface.
  556. {error, closed} = ssl:recv(Socket, 3, 1000),
  557. ok.
  558. alpn_reject_missing_client_preface_settings(Config) ->
  559. doc("Servers must treat an invalid connection preface as a "
  560. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  561. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  562. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  563. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  564. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  565. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  566. %% Receive the server preface.
  567. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  568. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  569. %% We expect the server to close the connection when it receives a bad preface.
  570. {error, closed} = ssl:recv(Socket, 3, 1000),
  571. ok.
  572. alpn_reject_invalid_client_preface_settings(Config) ->
  573. doc("Servers must treat an invalid connection preface as a "
  574. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  575. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  576. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  577. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  578. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  579. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
  580. %% Receive the server preface.
  581. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  582. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  583. %% We expect the server to close the connection when it receives a bad preface.
  584. {error, closed} = ssl:recv(Socket, 3, 1000),
  585. ok.
  586. alpn_accept_client_preface_empty_settings(Config) ->
  587. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.3, RFC7540 3.5)"),
  588. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  589. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  590. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  591. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  592. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  593. %% Receive the server preface.
  594. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  595. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  596. %% Receive the SETTINGS ack.
  597. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  598. ok.
  599. alpn_client_preface_settings_ack_timeout(Config) ->
  600. doc("Failure to acknowledge the server's SETTINGS frame "
  601. "results in a SETTINGS_TIMEOUT connection error. (RFC7540 3.5, RFC7540 6.5.3)"),
  602. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  603. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  604. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  605. %% Send a valid preface.
  606. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  607. %% Receive the server preface.
  608. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  609. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  610. %% Receive the SETTINGS ack.
  611. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  612. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  613. {ok, << _:24, 7:8, _:72, 4:32 >>} = ssl:recv(Socket, 17, 6000),
  614. ok.
  615. alpn(Config) ->
  616. doc("Successful ALPN negotiation. (RFC7540 3.3)"),
  617. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  618. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  619. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  620. %% Send a valid preface.
  621. %% @todo Use non-empty SETTINGS here. Just because.
  622. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  623. %% Receive the server preface.
  624. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  625. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  626. %% Send the SETTINGS ack.
  627. ok = ssl:send(Socket, cow_http2:settings_ack()),
  628. %% Receive the SETTINGS ack.
  629. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  630. %% Wait until after the SETTINGS ack timeout was supposed to trigger.
  631. receive after 6000 -> ok end,
  632. %% Send a PING.
  633. ok = ssl:send(Socket, cow_http2:ping(0)),
  634. %% Receive a PING ack back, indicating the connection is still up.
  635. {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = ssl:recv(Socket, 17, 1000),
  636. ok.
  637. %% Starting HTTP/2 with prior knowledge.
  638. prior_knowledge_reject_tls(Config) ->
  639. doc("Implementations that support HTTP/2 over TLS must use ALPN. (RFC7540 3.4)"),
  640. {ok, Socket} = ssl:connect("localhost", config(port, Config), [binary, {active, false}]),
  641. %% Send a valid preface.
  642. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  643. %% We expect the server to send an HTTP 400 error
  644. %% when trying to use HTTP/2 without going through ALPN negotiation.
  645. {ok, <<"HTTP/1.1 400">>} = ssl:recv(Socket, 12, 1000),
  646. ok.
  647. prior_knowledge_server_preface(Config) ->
  648. doc("The first frame must be a SETTINGS frame "
  649. "for the server connection preface. (RFC7540 3.4, RFC7540 3.5, RFC7540 6.5)"),
  650. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  651. %% Send a valid preface.
  652. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  653. %% Receive the server preface.
  654. {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
  655. ok.
  656. %% Note: the client preface timeout doesn't apply in this case,
  657. %% so we don't test it. An HTTP/1.1 client that does not send
  658. %% a request in a timely manner will get disconnected by the
  659. %% HTTP protocol code, not by HTTP/2's.
  660. %% Note: the test that starts by sending a SETTINGS frame is
  661. %% redundant with tests sending garbage on the connection.
  662. %% From the point of view of an HTTP/1.1 connection, a
  663. %% SETTINGS frame is indistinguishable from garbage.
  664. prior_knowledge_reject_invalid_client_preface(Config) ->
  665. doc("An incorrect preface is an invalid HTTP/1.1 request. (RFC7540 3.4)"),
  666. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  667. %% Send a slightly incorrect preface.
  668. ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  669. %% We propagate to HTTP/2 after checking only the request-line.
  670. %% The server then sends its preface before checking the full client preface.
  671. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  672. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  673. %% We expect the server to close the connection when it receives a bad preface.
  674. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  675. ok.
  676. prior_knowledge_reject_missing_client_preface_settings(Config) ->
  677. doc("Servers must treat an invalid connection preface as a "
  678. "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
  679. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  680. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  681. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  682. %% Receive the server preface.
  683. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  684. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  685. %% We expect the server to close the connection when it receives a bad preface.
  686. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  687. ok.
  688. prior_knowledge_reject_invalid_client_preface_settings(Config) ->
  689. doc("Servers must treat an invalid connection preface as a "
  690. "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
  691. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  692. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  693. 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 >>]),
  694. %% Receive the server preface.
  695. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  696. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  697. %% We expect the server to close the connection when it receives a bad preface.
  698. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  699. ok.
  700. prior_knowledge_accept_client_preface_empty_settings(Config) ->
  701. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.4, RFC7540 3.5)"),
  702. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  703. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  704. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  705. %% Receive the server preface.
  706. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  707. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  708. %% Receive the SETTINGS ack.
  709. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  710. ok.
  711. prior_knowledge_client_preface_settings_ack_timeout(Config) ->
  712. doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
  713. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  714. %% Send a valid preface.
  715. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  716. %% Receive the server preface.
  717. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  718. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  719. %% Receive the SETTINGS ack.
  720. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  721. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  722. {ok, << _:24, 7:8, _:72, 4:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  723. ok.
  724. %% Do a prior knowledge handshake.
  725. do_handshake(Config) ->
  726. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  727. %% Send a valid preface.
  728. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  729. %% Receive the server preface.
  730. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  731. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  732. %% Send the SETTINGS ack.
  733. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  734. %% Receive the SETTINGS ack.
  735. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  736. {ok, Socket}.
  737. prior_knowledge(Config) ->
  738. doc("Streams can be initiated after a successful HTTP/2 connection "
  739. "with prior knowledge of server capabilities. (RFC7540 3.4)"),
  740. %% @todo Use non-empty SETTINGS here. Just because.
  741. {ok, Socket} = do_handshake(Config),
  742. %% Wait until after the SETTINGS ack timeout was supposed to trigger.
  743. receive after 6000 -> ok end,
  744. %% Send a PING.
  745. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  746. %% Receive a PING ack back, indicating the connection is still up.
  747. {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = gen_tcp:recv(Socket, 17, 1000),
  748. ok.
  749. %% @todo If we ever add an option to disable HTTP/2, we need to check
  750. %% the following things:
  751. %% * HTTP/1.1 Upgrade returns an HTTP/1.1 response (3.2)
  752. %% * HTTP/1.1 Upgrade errors out if the client sends HTTP/2 frames
  753. %% without waiting for the 101 response (3.2, 3.5)
  754. %% * Prior knowledge handshake fails (3.4)
  755. %% * ALPN selects HTTP/1.1 (3.3)
  756. %% Frame format.
  757. ignore_unknown_frames(Config) ->
  758. doc("Frames of unknown type must be ignored and discarded. (RFC7540 4.1)"),
  759. {ok, Socket} = do_handshake(Config),
  760. %% Send a POST request with a single DATA frame,
  761. %% and an unknown frame type interleaved.
  762. {HeadersBlock, _} = cow_hpack:encode([
  763. {<<":method">>, <<"POST">>},
  764. {<<":scheme">>, <<"http">>},
  765. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  766. {<<":path">>, <<"/echo/read_body">>}
  767. ]),
  768. ok = gen_tcp:send(Socket, [
  769. cow_http2:headers(1, nofin, HeadersBlock),
  770. << 10:24, 99:8, 0:40, 0:80 >>,
  771. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  772. ]),
  773. %% Receive a response with the same DATA frame.
  774. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  775. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  776. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  777. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  778. ok.
  779. ignore_data_unknown_flags(Config) ->
  780. doc("Undefined DATA frame flags must be ignored. (RFC7540 4.1, RFC7540 6.1)"),
  781. {ok, Socket} = do_handshake(Config),
  782. %% Send a POST request with a DATA frame with unknown flags.
  783. {HeadersBlock, _} = cow_hpack:encode([
  784. {<<":method">>, <<"POST">>},
  785. {<<":scheme">>, <<"http">>},
  786. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  787. {<<":path">>, <<"/echo/read_body">>}
  788. ]),
  789. ok = gen_tcp:send(Socket, [
  790. cow_http2:headers(1, nofin, HeadersBlock),
  791. << 100:24, 0:8,
  792. 1:1, 1:1, 1:1, 1:1, %% Undefined.
  793. 0:1, %% PADDED.
  794. 1:1, 1:1, %% Undefined.
  795. 1:1, %% END_STREAM.
  796. 0:1, 1:31, 0:100/unit:8 >>
  797. ]),
  798. %% Receive a response with the same DATA frame.
  799. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  800. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  801. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  802. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  803. ok.
  804. ignore_headers_unknown_flags(Config) ->
  805. doc("Undefined HEADERS frame flags must be ignored. (RFC7540 4.1, RFC7540 6.2)"),
  806. {ok, Socket} = do_handshake(Config),
  807. %% Send a POST request with a HEADERS frame with unknown flags.
  808. {HeadersBlock, _} = cow_hpack:encode([
  809. {<<":method">>, <<"POST">>},
  810. {<<":scheme">>, <<"http">>},
  811. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  812. {<<":path">>, <<"/echo/read_body">>}
  813. ]),
  814. Len = iolist_size(HeadersBlock),
  815. ok = gen_tcp:send(Socket, [
  816. << Len:24, 1:8,
  817. 1:1, 1:1, %% Undefined.
  818. 0:1, %% PRIORITY.
  819. 1:1, %% Undefined.
  820. 0:1, %% PADDED.
  821. 1:1, %% END_HEADERS.
  822. 1:1, %% Undefined.
  823. 0:1, %% END_STREAM.
  824. 0:1, 1:31 >>,
  825. HeadersBlock,
  826. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  827. ]),
  828. %% Receive a response with the same DATA frame.
  829. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  830. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  831. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  832. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  833. ok.
  834. ignore_priority_unknown_flags(Config) ->
  835. doc("Undefined PRIORITY frame flags must be ignored. (RFC7540 4.1, RFC7540 6.3)"),
  836. {ok, Socket} = do_handshake(Config),
  837. %% Send a POST request with an interleaved PRIORITY frame with unknown flags.
  838. {HeadersBlock, _} = cow_hpack:encode([
  839. {<<":method">>, <<"POST">>},
  840. {<<":scheme">>, <<"http">>},
  841. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  842. {<<":path">>, <<"/echo/read_body">>}
  843. ]),
  844. ok = gen_tcp:send(Socket, [
  845. cow_http2:headers(1, nofin, HeadersBlock),
  846. << 5:24, 2:8,
  847. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  848. 0:1, 1:31, 0:1, 3:31, 0:8 >>,
  849. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  850. ]),
  851. %% Receive a response with the same DATA frame.
  852. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  853. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  854. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  855. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  856. ok.
  857. ignore_rst_stream_unknown_flags(Config) ->
  858. doc("Undefined RST_STREAM frame flags must be ignored. (RFC7540 4.1, RFC7540 6.4)"),
  859. {ok, Socket} = do_handshake(Config),
  860. %% Send a POST request then cancel it with an RST_STREAM frame with unknown flags.
  861. {HeadersBlock, _} = cow_hpack:encode([
  862. {<<":method">>, <<"POST">>},
  863. {<<":scheme">>, <<"http">>},
  864. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  865. {<<":path">>, <<"/echo/read_body">>}
  866. ]),
  867. ok = gen_tcp:send(Socket, [
  868. cow_http2:headers(1, nofin, HeadersBlock),
  869. << 4:24, 3:8,
  870. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  871. 0:1, 1:31, 8:32 >>,
  872. cow_http2:headers(3, nofin, HeadersBlock),
  873. cow_http2:data(3, fin, << 0:100/unit:8 >>)
  874. ]),
  875. %% Receive a response with the same DATA frame.
  876. {ok, << SkipLen:24, 1:8, _:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  877. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  878. {ok, << 100:24, 0:8, 1:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  879. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  880. ok.
  881. ignore_settings_unknown_flags(Config) ->
  882. doc("Undefined SETTINGS frame flags must be ignored. (RFC7540 4.1, RFC7540 6.5)"),
  883. {ok, Socket} = do_handshake(Config),
  884. %% Send a SETTINGS frame with unknown flags.
  885. ok = gen_tcp:send(Socket, << 6:24, 4:8,
  886. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  887. 0:1, %% ACK.
  888. 0:32, 2:16, 0:32 >>),
  889. %% Receive a SETTINGS ack.
  890. {ok, << 0:24, 4:8, 0:7, 1:1, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  891. ok.
  892. ignore_push_promise_unknown_flags(Config) ->
  893. doc("Undefined PUSH_PROMISE frame flags must be ignored. (RFC7540 4.1, RFC7540 6.6)"),
  894. {ok, Socket} = do_handshake(Config),
  895. %% Send a PUSH_PROMISE frame with unknown flags.
  896. ok = gen_tcp:send(Socket, << 4:24, 5:8,
  897. 1:1, 1:1, 1:1, 1:1, %% Undefined.
  898. 0:1, %% PADDED.
  899. 1:1, %% END_HEADERS.
  900. 1:1, 1:1, %% Undefined.
  901. 0:1, 1:31, 0:1, 3:31 >>
  902. ),
  903. %% Receive a PROTOCOL_ERROR connection error.
  904. %%
  905. %% Note that it is not possible to distinguish between the expected
  906. %% result and the server rejecting PUSH_PROMISE frames.
  907. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  908. ok.
  909. ignore_ping_unknown_flags(Config) ->
  910. doc("Undefined PING frame flags must be ignored. (RFC7540 4.1, RFC7540 6.7)"),
  911. {ok, Socket} = do_handshake(Config),
  912. %% Send a PING frame with unknown flags.
  913. ok = gen_tcp:send(Socket, << 8:24, 6:8,
  914. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  915. 0:1, %% ACK.
  916. 0:32, 0:64 >>),
  917. %% Receive a PING ACK in return.
  918. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  919. ok.
  920. ignore_goaway_unknown_flags(Config) ->
  921. doc("Undefined GOAWAY frame flags must be ignored. (RFC7540 4.1, RFC7540 6.8)"),
  922. {ok, Socket} = do_handshake(Config),
  923. %% Send a GOAWAY frame with unknown flags.
  924. ok = gen_tcp:send(Socket, << 8:24, 7:8,
  925. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  926. 0:32, 0:64 >>),
  927. %% Receive a GOAWAY frame back.
  928. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  929. ok.
  930. ignore_window_update_unknown_flags(Config) ->
  931. doc("Undefined WINDOW_UPDATE frame flags must be ignored. (RFC7540 4.1, RFC7540 6.9)"),
  932. {ok, Socket} = do_handshake(Config),
  933. %% Send a WINDOW_UPDATE frame with unknown flags.
  934. ok = gen_tcp:send(Socket, << 4:24, 8:8,
  935. 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  936. 0:32, 1000:32 >>),
  937. %% We expect no errors or replies, therefore we send a PING frame.
  938. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  939. %% And receive a PING ACK in return.
  940. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  941. ok.
  942. ignore_continuation_unknown_flags(Config) ->
  943. doc("Undefined CONTINUATION frame flags must be ignored. (RFC7540 4.1, RFC7540 6.10)"),
  944. {ok, Socket} = do_handshake(Config),
  945. %% Send a POST request with a CONTINUATION frame with unknown flags.
  946. {HeadersBlock, _} = cow_hpack:encode([
  947. {<<":method">>, <<"POST">>},
  948. {<<":scheme">>, <<"http">>},
  949. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  950. {<<":path">>, <<"/echo/read_body">>}
  951. ]),
  952. Len = iolist_size(HeadersBlock),
  953. ok = gen_tcp:send(Socket, [
  954. << 0:24, 1:8, 0:8, 0:1, 1:31 >>,
  955. << Len:24, 9:8,
  956. 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
  957. 1:1, %% END_HEADERS.
  958. 1:1, 1:1, %% Undefined.
  959. 0:1, 1:31 >>,
  960. HeadersBlock,
  961. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  962. ]),
  963. %% Receive a response with the same DATA frame.
  964. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  965. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  966. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  967. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  968. ok.
  969. %% @todo Flags that have no defined semantics for
  970. %% a particular frame type MUST be left unset (0x0) when sending. (RFC7540 4.1)
  971. ignore_data_reserved_bit(Config) ->
  972. doc("Reserved 1-bit field of DATA frame must be ignored. (RFC7540 4.1, RFC7540 6.1)"),
  973. {ok, Socket} = do_handshake(Config),
  974. %% Send a POST request with a DATA frame with the reserved bit set.
  975. {HeadersBlock, _} = cow_hpack:encode([
  976. {<<":method">>, <<"POST">>},
  977. {<<":scheme">>, <<"http">>},
  978. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  979. {<<":path">>, <<"/echo/read_body">>}
  980. ]),
  981. ok = gen_tcp:send(Socket, [
  982. cow_http2:headers(1, nofin, HeadersBlock),
  983. << 100:24, 0:8, 0:7, 1:1,
  984. 1:1, %% Reserved bit.
  985. 1:31, 0:100/unit:8 >>
  986. ]),
  987. %% Receive a response with the same DATA frame.
  988. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  989. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  990. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  991. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  992. ok.
  993. ignore_headers_reserved_bit(Config) ->
  994. doc("Reserved 1-bit field of HEADERS frame must be ignored. (RFC7540 4.1, RFC7540 6.2)"),
  995. {ok, Socket} = do_handshake(Config),
  996. %% Send a POST request with a HEADERS frame with the reserved bit set.
  997. {HeadersBlock, _} = cow_hpack:encode([
  998. {<<":method">>, <<"POST">>},
  999. {<<":scheme">>, <<"http">>},
  1000. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1001. {<<":path">>, <<"/echo/read_body">>}
  1002. ]),
  1003. Len = iolist_size(HeadersBlock),
  1004. ok = gen_tcp:send(Socket, [
  1005. << Len:24, 1:8, 0:5, 1:1, 0:2,
  1006. 1:1, %% Reserved bit.
  1007. 1:31 >>,
  1008. HeadersBlock,
  1009. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  1010. ]),
  1011. %% Receive a response with the same DATA frame.
  1012. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1013. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1014. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1015. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1016. ok.
  1017. ignore_priority_reserved_bit(Config) ->
  1018. doc("Reserved 1-bit field of PRIORITY frame must be ignored. (RFC7540 4.1, RFC7540 6.3)"),
  1019. {ok, Socket} = do_handshake(Config),
  1020. %% Send a POST request with an interleaved PRIORITY frame with the reserved bit set.
  1021. {HeadersBlock, _} = cow_hpack:encode([
  1022. {<<":method">>, <<"POST">>},
  1023. {<<":scheme">>, <<"http">>},
  1024. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1025. {<<":path">>, <<"/echo/read_body">>}
  1026. ]),
  1027. ok = gen_tcp:send(Socket, [
  1028. cow_http2:headers(1, nofin, HeadersBlock),
  1029. << 5:24, 2:8, 0:8,
  1030. 1:1, %% Reserved bit.
  1031. 1:31, 0:1, 3:31, 0:8 >>,
  1032. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  1033. ]),
  1034. %% Receive a response with the same DATA frame.
  1035. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1036. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1037. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1038. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1039. ok.
  1040. ignore_rst_stream_reserved_bit(Config) ->
  1041. doc("Reserved 1-bit field of RST_STREAM frame must be ignored. (RFC7540 4.1, RFC7540 6.4)"),
  1042. {ok, Socket} = do_handshake(Config),
  1043. %% Send a POST request then cancel it with an RST_STREAM frame with the reserved bit set.
  1044. {HeadersBlock, _} = cow_hpack:encode([
  1045. {<<":method">>, <<"POST">>},
  1046. {<<":scheme">>, <<"http">>},
  1047. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1048. {<<":path">>, <<"/echo/read_body">>}
  1049. ]),
  1050. ok = gen_tcp:send(Socket, [
  1051. cow_http2:headers(1, nofin, HeadersBlock),
  1052. << 4:24, 3:8, 0:8,
  1053. 1:1, %% Reserved bit.
  1054. 1:31, 8:32 >>,
  1055. cow_http2:headers(3, nofin, HeadersBlock),
  1056. cow_http2:data(3, fin, << 0:100/unit:8 >>)
  1057. ]),
  1058. %% Receive a response with the same DATA frame.
  1059. {ok, << SkipLen:24, 1:8, _:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1060. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1061. {ok, << 100:24, 0:8, 1:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1062. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1063. ok.
  1064. ignore_settings_reserved_bit(Config) ->
  1065. doc("Reserved 1-bit field of SETTINGS frame must be ignored. (RFC7540 4.1, RFC7540 6.5)"),
  1066. {ok, Socket} = do_handshake(Config),
  1067. %% Send a SETTINGS frame with the reserved bit set.
  1068. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:8,
  1069. 1:1, %% Reserved bit.
  1070. 0:31, 2:16, 0:32 >>),
  1071. %% Receive a SETTINGS ack.
  1072. {ok, << 0:24, 4:8, 0:7, 1:1, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1073. ok.
  1074. ignore_push_promise_reserved_bit(Config) ->
  1075. doc("Reserved 1-bit field of PUSH_PROMISE frame must be ignored. (RFC7540 4.1, RFC7540 6.6)"),
  1076. {ok, Socket} = do_handshake(Config),
  1077. %% Send a PUSH_PROMISE frame with the reserved bit set.
  1078. ok = gen_tcp:send(Socket, << 4:24, 5:8, 0:5, 1:1, 0:2,
  1079. 1:1, %% Reserved bit.
  1080. 1:31, 0:1, 3:31 >>
  1081. ),
  1082. %% Receive a PROTOCOL_ERROR connection error.
  1083. %%
  1084. %% Note that it is not possible to distinguish between the expected
  1085. %% result and the server rejecting PUSH_PROMISE frames.
  1086. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1087. ok.
  1088. ignore_ping_reserved_bit(Config) ->
  1089. doc("Reserved 1-bit field of PING frame must be ignored. (RFC7540 4.1, RFC7540 6.7)"),
  1090. {ok, Socket} = do_handshake(Config),
  1091. %% Send a PING frame with the reserved bit set.
  1092. ok = gen_tcp:send(Socket, << 8:24, 6:8, 0:8,
  1093. 1:1, %% Reserved bit.
  1094. 0:31, 0:64 >>),
  1095. %% Receive a PING ACK in return.
  1096. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  1097. ok.
  1098. ignore_goaway_reserved_bit(Config) ->
  1099. doc("Reserved 1-bit field of GOAWAY frame must be ignored. (RFC7540 4.1, RFC7540 6.8)"),
  1100. {ok, Socket} = do_handshake(Config),
  1101. %% Send a GOAWAY frame with the reserved bit set.
  1102. ok = gen_tcp:send(Socket, << 8:24, 7:8, 0:8,
  1103. 1:1, %% Reserved bit.
  1104. 0:31, 0:64 >>),
  1105. %% Receive a GOAWAY frame back.
  1106. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1107. ok.
  1108. ignore_window_update_reserved_bit(Config) ->
  1109. doc("Reserved 1-bit field of WINDOW_UPDATE frame must be ignored. (RFC7540 4.1, RFC7540 6.9)"),
  1110. {ok, Socket} = do_handshake(Config),
  1111. %% Send a WINDOW_UPDATE frame with the reserved bit set.
  1112. ok = gen_tcp:send(Socket, << 4:24, 8:8, 0:8,
  1113. 1:1, %% Reserved bit.
  1114. 0:31, 1000:32 >>),
  1115. %% We expect no errors or replies, therefore we send a PING frame.
  1116. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  1117. %% And receive a PING ACK in return.
  1118. {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
  1119. ok.
  1120. ignore_continuation_reserved_bit(Config) ->
  1121. doc("Reserved 1-bit field of CONTINUATION frame must be ignored. (RFC7540 4.1, RFC7540 6.10)"),
  1122. {ok, Socket} = do_handshake(Config),
  1123. %% Send a POST request with a CONTINUATION frame with the reserved bit set.
  1124. {HeadersBlock, _} = cow_hpack:encode([
  1125. {<<":method">>, <<"POST">>},
  1126. {<<":scheme">>, <<"http">>},
  1127. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1128. {<<":path">>, <<"/echo/read_body">>}
  1129. ]),
  1130. Len = iolist_size(HeadersBlock),
  1131. ok = gen_tcp:send(Socket, [
  1132. << 0:24, 1:8, 0:8, 0:1, 1:31 >>,
  1133. << Len:24, 9:8, 0:5, 1:1, 0:2,
  1134. 1:1, %% Reserved bit.
  1135. 1:31 >>,
  1136. HeadersBlock,
  1137. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  1138. ]),
  1139. %% Receive a response with the same DATA frame.
  1140. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1141. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1142. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1143. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  1144. ok.
  1145. %% @todo The reserved 1-bit field MUST remain unset (0x0) when sending. (RFC7540 4.1)
  1146. %% Frame size.
  1147. max_frame_size_allow_exactly_default(Config) ->
  1148. doc("All implementations must allow frame sizes of at least 16384. (RFC7540 4.1, RFC7540 4.2)"),
  1149. {ok, Socket} = do_handshake(Config),
  1150. %% Send a POST request with a DATA frame of exactly 16384 bytes.
  1151. {HeadersBlock, _} = cow_hpack:encode([
  1152. {<<":method">>, <<"POST">>},
  1153. {<<":scheme">>, <<"http">>},
  1154. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1155. {<<":path">>, <<"/echo/read_body">>}
  1156. ]),
  1157. ok = gen_tcp:send(Socket, [
  1158. cow_http2:headers(1, nofin, HeadersBlock),
  1159. cow_http2:data(1, fin, << 0:16384/unit:8 >>)
  1160. ]),
  1161. %% Receive a response with the same DATA frame.
  1162. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1163. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  1164. {ok, << 16384:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1165. {ok, << 0:16384/unit:8 >>} = gen_tcp:recv(Socket, 16384, 1000),
  1166. ok.
  1167. max_frame_size_reject_larger_than_default(Config) ->
  1168. doc("A FRAME_SIZE_ERROR connection error must be sent when receiving "
  1169. "frames larger than the default 16384 length. (RFC7540 4.1, RFC7540 4.2)"),
  1170. {ok, Socket} = do_handshake(Config),
  1171. %% Send a POST request with a DATA frame larger than 16384 bytes.
  1172. {HeadersBlock, _} = cow_hpack:encode([
  1173. {<<":method">>, <<"POST">>},
  1174. {<<":scheme">>, <<"http">>},
  1175. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1176. {<<":path">>, <<"/echo/read_body">>}
  1177. ]),
  1178. ok = gen_tcp:send(Socket, [
  1179. cow_http2:headers(1, nofin, HeadersBlock),
  1180. cow_http2:data(1, fin, << 0:16385/unit:8 >>)
  1181. ]),
  1182. %% Receive a FRAME_SIZE_ERROR connection error.
  1183. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1184. ok.
  1185. %% @todo We need configurable SETTINGS in Cowboy for these tests.
  1186. %% max_frame_size_config_reject_too_small(Config) ->
  1187. %% doc("SETTINGS_MAX_FRAME_SIZE configuration values smaller than "
  1188. %% "16384 must be rejected. (RFC7540 6.5.2)"),
  1189. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  1190. %% todo.
  1191. %%
  1192. %% max_frame_size_config_reject_too_large(Config) ->
  1193. %% doc("SETTINGS_MAX_FRAME_SIZE configuration values larger than "
  1194. %% "16777215 must be rejected. (RFC7540 6.5.2)"),
  1195. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  1196. %% todo.
  1197. %%
  1198. %% max_frame_size_allow_exactly_custom(Config) ->
  1199. %% doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must allow frames "
  1200. %% "of up to that size. (RFC7540 4.2, RFC7540 6.5.2)"),
  1201. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  1202. %% todo.
  1203. %%
  1204. %% max_frame_size_reject_larger_than_custom(Config) ->
  1205. %% doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must reject frames "
  1206. %% "of up to that size with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5.2)"),
  1207. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  1208. %% todo.
  1209. %% @todo How do I test this?
  1210. %%
  1211. %% max_frame_size_client_default_respect_limits(Config) ->
  1212. %% doc("The server must not send frame sizes of more "
  1213. %% "than 16384 by default. (RFC7540 4.1, RFC7540 4.2)"),
  1214. %% This is about the client sending a SETTINGS frame.
  1215. max_frame_size_client_override_reject_too_small(Config) ->
  1216. doc("A SETTINGS_MAX_FRAME_SIZE smaller than 16384 must be rejected "
  1217. "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
  1218. {ok, Socket} = do_handshake(Config),
  1219. %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE lower than 16384.
  1220. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16383:32 >>),
  1221. %% Receive a PROTOCOL_ERROR connection error.
  1222. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1223. ok.
  1224. %% This is about the client sending a SETTINGS frame.
  1225. max_frame_size_client_override_reject_too_large(Config) ->
  1226. doc("A SETTINGS_MAX_FRAME_SIZE larger than 16777215 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 larger than 16777215.
  1230. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16777216: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. %% @todo How do I test this?
  1235. %%
  1236. %% max_frame_size_client_custom_respect_limits(Config) ->
  1237. %% doc("The server must not send frame sizes of more than "
  1238. %% "client's advertised limits. (RFC7540 4.1, RFC7540 4.2)"),
  1239. %% I am using FRAME_SIZE_ERROR here because the information in the
  1240. %% frame header tells us this frame is at least 1 byte long, while
  1241. %% the given length is smaller; i.e. it is too small to contain
  1242. %% mandatory frame data (the pad length).
  1243. data_reject_frame_size_0_padded_flag(Config) ->
  1244. doc("DATA frames of size 0 with the PADDED flag set must be rejected "
  1245. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.1)"),
  1246. {ok, Socket} = do_handshake(Config),
  1247. %% Send a POST request with an incorrect padded DATA frame size.
  1248. {HeadersBlock, _} = cow_hpack:encode([
  1249. {<<":method">>, <<"POST">>},
  1250. {<<":scheme">>, <<"http">>},
  1251. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1252. {<<":path">>, <<"/echo/read_body">>}
  1253. ]),
  1254. ok = gen_tcp:send(Socket, [
  1255. cow_http2:headers(1, nofin, HeadersBlock),
  1256. << 0:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>
  1257. ]),
  1258. %% Receive a FRAME_SIZE_ERROR connection error.
  1259. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1260. ok.
  1261. %% This case on the other hand is noted specifically in the RFC
  1262. %% as being a PROTOCOL_ERROR. It can be thought of as the Pad Length
  1263. %% being incorrect, rather than the frame size.
  1264. data_reject_frame_size_too_small_padded_flag(Config) ->
  1265. doc("DATA frames with Pad Length >= Length must be rejected "
  1266. "with a PROTOCOL_ERROR connection error. (RFC7540 6.1)"),
  1267. {ok, Socket} = do_handshake(Config),
  1268. %% Send a POST request with an incorrect padded DATA frame size.
  1269. {HeadersBlock, _} = cow_hpack:encode([
  1270. {<<":method">>, <<"POST">>},
  1271. {<<":scheme">>, <<"http">>},
  1272. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1273. {<<":path">>, <<"/echo/read_body">>}
  1274. ]),
  1275. ok = gen_tcp:send(Socket, [
  1276. cow_http2:headers(1, nofin, HeadersBlock),
  1277. << 10:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>
  1278. ]),
  1279. %% Receive a PROTOCOL_ERROR connection error.
  1280. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1281. ok.
  1282. headers_reject_frame_size_0_padded_flag(Config) ->
  1283. doc("HEADERS frames of size 0 with the PADDED flag set must be rejected "
  1284. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  1285. {ok, Socket} = do_handshake(Config),
  1286. %% Send a padded HEADERS frame with an incorrect size.
  1287. ok = gen_tcp:send(Socket, << 0:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>),
  1288. %% Receive a FRAME_SIZE_ERROR connection error.
  1289. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1290. ok.
  1291. headers_reject_frame_size_too_small_padded_flag(Config) ->
  1292. doc("HEADERS frames with no priority flag and Pad Length >= Length "
  1293. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 6.2)"),
  1294. {ok, Socket} = do_handshake(Config),
  1295. %% Send a padded HEADERS frame with an incorrect size.
  1296. 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 >>),
  1297. %% Receive a PROTOCOL_ERROR connection error.
  1298. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1299. ok.
  1300. headers_reject_frame_size_too_small_priority_flag(Config) ->
  1301. doc("HEADERS frames of size smaller than 5 with the PRIORITY flag set must be rejected "
  1302. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  1303. {ok, Socket} = do_handshake(Config),
  1304. %% Send a HEADERS frame with priority set and an incorrect size.
  1305. ok = gen_tcp:send(Socket, << 4:24, 1:8,
  1306. 0:2, 1:1, 0:4, 1:1, 0:1, 1:31, 0:1, 3:31, 0:8 >>),
  1307. %% Receive a FRAME_SIZE_ERROR connection error.
  1308. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1309. ok.
  1310. headers_reject_frame_size_5_padded_and_priority_flags(Config) ->
  1311. doc("HEADERS frames of size smaller than 6 with the PADDED "
  1312. "and PRIORITY flags set must be rejected "
  1313. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  1314. {ok, Socket} = do_handshake(Config),
  1315. %% Send a padded HEADERS frame with an incorrect size.
  1316. ok = gen_tcp:send(Socket, << 5:24, 1:8,
  1317. 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 0:8, 0:1, 3:31, 0:8 >>),
  1318. %% Receive a FRAME_SIZE_ERROR connection error.
  1319. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1320. ok.
  1321. headers_reject_frame_size_too_small_padded_and_priority_flags(Config) ->
  1322. doc("HEADERS frames of size smaller than Length+6 with the PADDED and PRIORITY flags set "
  1323. "must be rejected 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, << 15:24, 1:8,
  1327. 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 >>),
  1328. %% Receive a PROTOCOL_ERROR connection error.
  1329. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1330. ok.
  1331. priority_reject_frame_size_too_small(Config) ->
  1332. doc("PRIORITY frames of size smaller than 5 must be rejected "
  1333. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  1334. {ok, Socket} = do_handshake(Config),
  1335. %% Send a PRIORITY frame with an incorrect size.
  1336. ok = gen_tcp:send(Socket, << 4:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:8 >>),
  1337. %% Receive a FRAME_SIZE_ERROR stream error.
  1338. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1339. ok.
  1340. priority_reject_frame_size_too_large(Config) ->
  1341. doc("PRIORITY frames of size larger than 5 must be rejected "
  1342. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  1343. {ok, Socket} = do_handshake(Config),
  1344. %% Send a PRIORITY frame with an incorrect size.
  1345. ok = gen_tcp:send(Socket, << 6:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:16 >>),
  1346. %% Receive a FRAME_SIZE_ERROR stream error.
  1347. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1348. ok.
  1349. rst_stream_reject_frame_size_too_small(Config) ->
  1350. doc("RST_STREAM frames of size smaller than 4 must be rejected "
  1351. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  1352. {ok, Socket} = do_handshake(Config),
  1353. %% Send a request and reset it immediately.
  1354. {HeadersBlock, _} = cow_hpack:encode([
  1355. {<<":method">>, <<"GET">>},
  1356. {<<":scheme">>, <<"http">>},
  1357. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1358. {<<":path">>, <<"/">>}
  1359. ]),
  1360. ok = gen_tcp:send(Socket, [
  1361. cow_http2:headers(1, fin, HeadersBlock),
  1362. << 3:24, 3:8, 0:9, 1:31, 8:32 >>
  1363. ]),
  1364. %% Receive a FRAME_SIZE_ERROR connection error.
  1365. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1366. ok.
  1367. rst_stream_reject_frame_size_too_large(Config) ->
  1368. doc("RST_STREAM frames of size larger than 4 must be rejected "
  1369. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  1370. {ok, Socket} = do_handshake(Config),
  1371. %% Send a request and reset it immediately.
  1372. {HeadersBlock, _} = cow_hpack:encode([
  1373. {<<":method">>, <<"GET">>},
  1374. {<<":scheme">>, <<"http">>},
  1375. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1376. {<<":path">>, <<"/">>}
  1377. ]),
  1378. ok = gen_tcp:send(Socket, [
  1379. cow_http2:headers(1, fin, HeadersBlock),
  1380. << 5:24, 3:8, 0:9, 1:31, 8:32 >>
  1381. ]),
  1382. %% Receive a FRAME_SIZE_ERROR connection error.
  1383. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1384. ok.
  1385. settings_reject_bad_frame_size(Config) ->
  1386. doc("SETTINGS frames must have a size multiple of 6 or be rejected "
  1387. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5)"),
  1388. {ok, Socket} = do_handshake(Config),
  1389. %% Send a SETTINGS frame with an incorrect size.
  1390. ok = gen_tcp:send(Socket, << 5:24, 4:8, 0:40, 1:16, 4096:32 >>),
  1391. %% Receive a FRAME_SIZE_ERROR connection error.
  1392. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1393. ok.
  1394. settings_ack_reject_non_empty_frame_size(Config) ->
  1395. doc("SETTINGS frames with the ACK flag set and a non-empty payload "
  1396. "must be rejected with a FRAME_SIZE_ERROR connection error (RFC7540 4.2, RFC7540 6.5)"),
  1397. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  1398. %% Send a valid preface.
  1399. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1400. %% Receive the server preface.
  1401. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  1402. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  1403. %% Send a SETTINGS ack with a payload.
  1404. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:7, 1:1, 0:32, 1:16, 4096:32 >>),
  1405. %% Receive the SETTINGS ack.
  1406. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1407. %% Receive a FRAME_SIZE_ERROR connection error.
  1408. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1409. ok.
  1410. %% Note that clients are not supposed to send PUSH_PROMISE frames.
  1411. %% However when they do, we need to be able to parse it in order
  1412. %% to reject it, and so these errors may still occur.
  1413. push_promise_reject_frame_size_too_small(Config) ->
  1414. doc("PUSH_PROMISE frames of size smaller than 4 must be rejected "
  1415. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1416. {ok, Socket} = do_handshake(Config),
  1417. %% Send a PUSH_PROMISE frame with an incorrect size.
  1418. ok = gen_tcp:send(Socket, << 3:24, 5:8, 0:5, 1:1, 0:3, 1:31, 0:1, 3:31 >>),
  1419. %% Receive a FRAME_SIZE_ERROR connection error.
  1420. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1421. ok.
  1422. push_promise_reject_frame_size_4_padded_flag(Config) ->
  1423. doc("PUSH_PROMISE frames of size smaller than 5 with the PADDED flag set must be rejected "
  1424. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1425. {ok, Socket} = do_handshake(Config),
  1426. %% Send a PUSH_PROMISE frame with an incorrect size.
  1427. 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 >>),
  1428. %% Receive a FRAME_SIZE_ERROR connection error.
  1429. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1430. ok.
  1431. push_promise_reject_frame_size_too_small_padded_flag(Config) ->
  1432. doc("PUSH_PROMISE frames of size smaller than Length+5 with the PADDED flag set "
  1433. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1434. {ok, Socket} = do_handshake(Config),
  1435. %% Send a PUSH_PROMISE frame with an incorrect size.
  1436. {HeadersBlock, _} = cow_hpack:encode([
  1437. {<<":method">>, <<"GET">>},
  1438. {<<":scheme">>, <<"http">>},
  1439. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1440. {<<":path">>, <<"/">>}
  1441. ]),
  1442. Len = 14 + iolist_size(HeadersBlock),
  1443. ok = gen_tcp:send(Socket, [
  1444. << Len:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 10:8, 0:1, 3:31 >>,
  1445. HeadersBlock,
  1446. << 0:80 >>
  1447. ]),
  1448. %% Receive a PROTOCOL_ERROR connection error.
  1449. %%
  1450. %% Note that it is not possible to distinguish between a Pad Length
  1451. %% error and the server rejecting PUSH_PROMISE frames.
  1452. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1453. ok.
  1454. ping_reject_frame_size_too_small(Config) ->
  1455. doc("PING frames of size smaller than 8 must be rejected "
  1456. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1457. {ok, Socket} = do_handshake(Config),
  1458. %% Send a PING frame with an incorrect size.
  1459. ok = gen_tcp:send(Socket, << 7:24, 6:8, 0:40, 0:56 >>),
  1460. %% Receive a FRAME_SIZE_ERROR connection error.
  1461. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1462. ok.
  1463. ping_reject_frame_size_too_large(Config) ->
  1464. doc("PING frames of size larger than 8 must be rejected "
  1465. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1466. {ok, Socket} = do_handshake(Config),
  1467. %% Send a PING frame with an incorrect size.
  1468. ok = gen_tcp:send(Socket, << 9:24, 6:8, 0:40, 0:72 >>),
  1469. %% Receive a FRAME_SIZE_ERROR connection error.
  1470. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1471. ok.
  1472. goaway_reject_frame_size_too_small(Config) ->
  1473. doc("GOAWAY frames of size smaller than 8 must be rejected "
  1474. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.8)"),
  1475. {ok, Socket} = do_handshake(Config),
  1476. %% Send a GOAWAY frame with an incorrect size.
  1477. ok = gen_tcp:send(Socket, << 7:24, 7:8, 0:40, 0:56 >>),
  1478. %% Receive a FRAME_SIZE_ERROR connection error.
  1479. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1480. ok.
  1481. goaway_allow_frame_size_too_large(Config) ->
  1482. doc("GOAWAY frames of size larger than 8 must be allowed. (RFC7540 6.8)"),
  1483. {ok, Socket} = do_handshake(Config),
  1484. %% Send a GOAWAY frame with debug data.
  1485. ok = gen_tcp:send(Socket, << 12:24, 7:8, 0:40, 0:64, 99999:32 >>),
  1486. %% Receive a GOAWAY frame back.
  1487. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1488. ok.
  1489. window_update_reject_frame_size_too_small(Config) ->
  1490. doc("WINDOW_UPDATE frames of size smaller than 4 must be rejected "
  1491. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1492. {ok, Socket} = do_handshake(Config),
  1493. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1494. ok = gen_tcp:send(Socket, << 3:24, 8:8, 0:40, 1000:24 >>),
  1495. %% Receive a FRAME_SIZE_ERROR connection error.
  1496. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1497. ok.
  1498. window_update_reject_frame_size_too_large(Config) ->
  1499. doc("WINDOW_UPDATE frames of size larger than 4 must be rejected "
  1500. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1501. {ok, Socket} = do_handshake(Config),
  1502. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1503. ok = gen_tcp:send(Socket, << 5:24, 8:8, 0:40, 1000:40 >>),
  1504. %% Receive a FRAME_SIZE_ERROR connection error.
  1505. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1506. ok.
  1507. %% Note: There is no particular limits on the size of CONTINUATION frames,
  1508. %% they can go from 0 to SETTINGS_MAX_FRAME_SIZE.
  1509. %% Header compression and decompression.
  1510. headers_compression_error(Config) ->
  1511. doc("A decoding error in a HEADERS frame's header block must be rejected "
  1512. "with a COMPRESSION_ERROR connection error. (RFC7540 4.3, RFC7540 6.2)"),
  1513. {ok, Socket} = do_handshake(Config),
  1514. %% Send a HEADERS frame with an invalid header block.
  1515. 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 >>),
  1516. %% Receive a COMPRESSION_ERROR connection error.
  1517. {ok, << _:24, 7:8, _:72, 9:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1518. ok.
  1519. continuation_compression_error(Config) ->
  1520. doc("A decoding error in a CONTINUATION frame's header block must be rejected "
  1521. "with a COMPRESSION_ERROR connection error. (RFC7540 4.3, RFC7540 6.10)"),
  1522. {ok, Socket} = do_handshake(Config),
  1523. %% Send a CONTINUATION frame with an invalid header block.
  1524. ok = gen_tcp:send(Socket, [
  1525. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  1526. << 10:24, 9:8, 0:5, 1:1, 0:3, 1:31, 0:10/unit:8 >>
  1527. ]),
  1528. %% Receive a COMPRESSION_ERROR connection error.
  1529. {ok, << _:24, 7:8, _:72, 9:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1530. ok.
  1531. continuation_with_frame_interleaved_error(Config) ->
  1532. doc("Frames interleaved in a header block must be rejected "
  1533. "with a PROTOCOL_ERROR connection error. (RFC7540 4.3, RFC7540 6.2, RFC7540 6.10)"),
  1534. {ok, Socket} = do_handshake(Config),
  1535. %% Send an unterminated HEADERS frame followed by a PING frame.
  1536. ok = gen_tcp:send(Socket, [
  1537. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  1538. cow_http2:ping(0)
  1539. ]),
  1540. %% Receive a PROTOCOL_ERROR connection error.
  1541. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1542. ok.
  1543. continuation_wrong_stream_error(Config) ->
  1544. doc("CONTINUATION frames with an incorrect stream identifier must be rejected "
  1545. "with a PROTOCOL_ERROR connection error. (RFC7540 4.3, RFC7540 6.2)"),
  1546. {ok, Socket} = do_handshake(Config),
  1547. %% Send an unterminated HEADERS frame followed by a CONTINUATION frame for another stream.
  1548. ok = gen_tcp:send(Socket, [
  1549. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  1550. << 0:24, 9:8, 0:9, 3:31 >>
  1551. ]),
  1552. %% Receive a PROTOCOL_ERROR connection error.
  1553. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1554. ok.
  1555. %% Stream states.
  1556. idle_stream_reject_data(Config) ->
  1557. doc("DATA frames received on an idle stream must be rejected "
  1558. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1, RFC7540 6.1)"),
  1559. {ok, Socket} = do_handshake(Config),
  1560. %% Send a DATA frame on an idle stream.
  1561. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1562. %% Receive a PROTOCOL_ERROR connection error.
  1563. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1564. ok.
  1565. idle_stream_accept_headers(Config) ->
  1566. doc("HEADERS frames received on an idle stream must be accepted. (RFC7540 5.1)"),
  1567. {ok, Socket} = do_handshake(Config),
  1568. %% Send a HEADERS frame on an idle stream.
  1569. {HeadersBlock, _} = cow_hpack:encode([
  1570. {<<":method">>, <<"GET">>},
  1571. {<<":scheme">>, <<"http">>},
  1572. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1573. {<<":path">>, <<"/">>}
  1574. ]),
  1575. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1576. %% Receive a HEADERS frame as a response.
  1577. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1578. ok.
  1579. idle_stream_accept_priority(Config) ->
  1580. doc("PRIORITY frames received on an idle stream must be accepted. (RFC7540 5.1)"),
  1581. {ok, Socket} = do_handshake(Config),
  1582. %% Send a PRIORITY frame on an idle stream.
  1583. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1584. %% Receive no error.
  1585. {error, timeout} = gen_tcp:recv(Socket, 7, 1000),
  1586. %% Send a HEADERS frame on the same stream.
  1587. {HeadersBlock, _} = cow_hpack:encode([
  1588. {<<":method">>, <<"GET">>},
  1589. {<<":scheme">>, <<"http">>},
  1590. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1591. {<<":path">>, <<"/">>}
  1592. ]),
  1593. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1594. %% Receive a HEADERS frame as a response.
  1595. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1596. ok.
  1597. idle_stream_reject_rst_stream(Config) ->
  1598. doc("RST_STREAM frames received on an idle stream must be rejected "
  1599. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
  1600. {ok, Socket} = do_handshake(Config),
  1601. %% Send an RST_STREAM frame on an idle stream.
  1602. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, no_error)),
  1603. %% Receive a PROTOCOL_ERROR connection error.
  1604. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1605. ok.
  1606. idle_stream_reject_push_promise(Config) ->
  1607. doc("PUSH_PROMISE frames received on an idle stream must be rejected "
  1608. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
  1609. {ok, Socket} = do_handshake(Config),
  1610. %% Send a PUSH_PROMISE frame on an idle stream.
  1611. {HeadersBlock, _} = cow_hpack:encode([
  1612. {<<":method">>, <<"GET">>},
  1613. {<<":scheme">>, <<"http">>},
  1614. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1615. {<<":path">>, <<"/">>}
  1616. ]),
  1617. ok = gen_tcp:send(Socket, cow_http2:push_promise(1, 3, HeadersBlock)),
  1618. %% Receive a PROTOCOL_ERROR connection error.
  1619. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1620. ok.
  1621. idle_stream_reject_window_update(Config) ->
  1622. doc("WINDOW_UPDATE frames received on an idle stream must be rejected "
  1623. "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
  1624. {ok, Socket} = do_handshake(Config),
  1625. %% Send a WINDOW_UPDATE frame on an idle stream.
  1626. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1627. %% Receive a PROTOCOL_ERROR connection error.
  1628. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1629. ok.
  1630. %reserved (local) - after sending PUSH_PROMISE:
  1631. % An endpoint MUST NOT send any type of frame other than HEADERS,
  1632. % RST_STREAM, or PRIORITY in this state.
  1633. %%% how to test this?
  1634. %
  1635. % A PRIORITY or WINDOW_UPDATE frame MAY be received in this state.
  1636. % Receiving any type of frame other than RST_STREAM, PRIORITY, or
  1637. % WINDOW_UPDATE on a stream in this state MUST be treated as a
  1638. % connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  1639. %%% we need to use a large enough file for this
  1640. %
  1641. %reserved_local_reject_data
  1642. %reserved_local_reject_headers
  1643. %reserved_local_accept_priority
  1644. %reserved_local_accept_rst_stream
  1645. %reserved_local_reject_push_promise %% do we even care? we reject it always
  1646. %reserved_local_accept_window_update
  1647. %
  1648. %half-closed (remote):
  1649. % If an endpoint receives additional frames, other than
  1650. % WINDOW_UPDATE, PRIORITY, or RST_STREAM, for a stream that is in
  1651. % this state, it MUST respond with a stream error (Section 5.4.2) of
  1652. % type STREAM_CLOSED.
  1653. half_closed_remote_reject_data(Config) ->
  1654. doc("DATA frames received on a half-closed (remote) stream must be rejected "
  1655. "with a STREAM_CLOSED stream error. (RFC7540 5.1, RFC7540 6.1)"),
  1656. {ok, Socket} = do_handshake(Config),
  1657. %% Send a HEADERS frame with the FIN flag set.
  1658. {HeadersBlock, _} = cow_hpack:encode([
  1659. {<<":method">>, <<"GET">>},
  1660. {<<":scheme">>, <<"http">>},
  1661. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1662. {<<":path">>, <<"/">>}
  1663. ]),
  1664. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1665. %% Send a DATA frame on that now half-closed (remote) stream.
  1666. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1667. %% Receive a STREAM_CLOSED stream error.
  1668. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1669. ok.
  1670. %% We reject all invalid HEADERS with a connection error because
  1671. %% we do not want to waste resources decoding them.
  1672. half_closed_remote_reject_headers(Config) ->
  1673. doc("HEADERS frames received on a half-closed (remote) stream must be rejected "
  1674. "with a STREAM_CLOSED connection error. (RFC7540 4.3, RFC7540 5.1)"),
  1675. {ok, Socket} = do_handshake(Config),
  1676. %% Send a HEADERS frame with the FIN flag set.
  1677. {HeadersBlock, _} = cow_hpack:encode([
  1678. {<<":method">>, <<"GET">>},
  1679. {<<":scheme">>, <<"http">>},
  1680. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1681. {<<":path">>, <<"/">>}
  1682. ]),
  1683. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1684. %% Send a HEADERS frame on that now half-closed (remote) stream.
  1685. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1686. %% Receive a STREAM_CLOSED connection error.
  1687. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1688. ok.
  1689. half_closed_remote_accept_priority(Config) ->
  1690. doc("PRIORITY frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1691. {ok, Socket} = do_handshake(Config),
  1692. %% Send a HEADERS frame with the FIN flag set.
  1693. {HeadersBlock, _} = cow_hpack:encode([
  1694. {<<":method">>, <<"GET">>},
  1695. {<<":scheme">>, <<"http">>},
  1696. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1697. {<<":path">>, <<"/">>}
  1698. ]),
  1699. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1700. %% Send a PRIORITY frame on that now half-closed (remote) stream.
  1701. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1702. %% Receive a HEADERS frame as a response.
  1703. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1704. ok.
  1705. half_closed_remote_accept_rst_stream(Config) ->
  1706. doc("RST_STREAM frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1707. {ok, Socket} = do_handshake(Config),
  1708. %% Send a HEADERS frame with the FIN flag set.
  1709. {HeadersBlock, _} = cow_hpack:encode([
  1710. {<<":method">>, <<"GET">>},
  1711. {<<":scheme">>, <<"http">>},
  1712. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1713. {<<":path">>, <<"/">>}
  1714. ]),
  1715. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1716. %% Send an RST_STREAM frame on that now half-closed (remote) stream.
  1717. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, no_error)),
  1718. %% Receive nothing back.
  1719. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1720. ok.
  1721. %% half_closed_remote_reject_push_promise
  1722. %%
  1723. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1724. %% because PUSH is disabled in that direction. We therefore cannot test other
  1725. %% error conditions.
  1726. half_closed_remote_accept_window_update(Config) ->
  1727. doc("WINDOW_UPDATE frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1728. {ok, Socket} = do_handshake(Config),
  1729. %% Send a HEADERS frame with the FIN flag set.
  1730. {HeadersBlock, _} = cow_hpack:encode([
  1731. {<<":method">>, <<"GET">>},
  1732. {<<":scheme">>, <<"http">>},
  1733. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1734. {<<":path">>, <<"/">>}
  1735. ]),
  1736. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1737. %% Send a WINDOW_UPDATE frame on that now half-closed (remote) stream.
  1738. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1739. %% Receive a HEADERS frame as a response.
  1740. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1741. ok.
  1742. %% We reject DATA frames sent on closed streams with a STREAM_CLOSED
  1743. %% connection error regardless of how the stream was closed to simplify
  1744. %% the implementation. This excludes the few frames we ignore from
  1745. %% lingering streams that we canceled.
  1746. rst_stream_closed_reject_data(Config) ->
  1747. doc("DATA frames received on a stream closed via RST_STREAM must be rejected "
  1748. "with a STREAM_CLOSED connection error. (RFC7540 5.1, RFC7540 6.1)"),
  1749. {ok, Socket} = do_handshake(Config),
  1750. %% Send a HEADERS frame.
  1751. {HeadersBlock, _} = cow_hpack:encode([
  1752. {<<":method">>, <<"GET">>},
  1753. {<<":scheme">>, <<"http">>},
  1754. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1755. {<<":path">>, <<"/">>}
  1756. ]),
  1757. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1758. %% Send an RST_STREAM frame to close the stream.
  1759. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1760. %% Send a DATA frame on the now RST_STREAM closed stream.
  1761. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1762. %% Receive a STREAM_CLOSED connection error.
  1763. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1764. ok.
  1765. %% We reject all invalid HEADERS with a connection error because
  1766. %% we do not want to waste resources decoding them.
  1767. rst_stream_closed_reject_headers(Config) ->
  1768. doc("HEADERS frames received on a stream closed via RST_STREAM must be rejected "
  1769. "with a STREAM_CLOSED connection error. (RFC7540 4.3, RFC7540 5.1)"),
  1770. {ok, Socket} = do_handshake(Config),
  1771. %% Send a HEADERS frame.
  1772. {HeadersBlock, _} = cow_hpack:encode([
  1773. {<<":method">>, <<"GET">>},
  1774. {<<":scheme">>, <<"http">>},
  1775. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1776. {<<":path">>, <<"/">>}
  1777. ]),
  1778. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1779. %% Send an RST_STREAM frame to close the stream.
  1780. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1781. %% Send a HEADERS frame on the now RST_STREAM closed stream.
  1782. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1783. %% Receive a STREAM_CLOSED connection error.
  1784. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1785. ok.
  1786. rst_stream_closed_accept_priority(Config) ->
  1787. doc("PRIORITY frames received on a stream closed via RST_STREAM "
  1788. "must be accepted. (RFC7540 5.1)"),
  1789. {ok, Socket} = do_handshake(Config),
  1790. %% Send a HEADERS frame.
  1791. {HeadersBlock, _} = cow_hpack:encode([
  1792. {<<":method">>, <<"GET">>},
  1793. {<<":scheme">>, <<"http">>},
  1794. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1795. {<<":path">>, <<"/">>}
  1796. ]),
  1797. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1798. %% Send an RST_STREAM frame to close the stream.
  1799. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1800. %% Send a PRIORITY frame on that now RST_STREAM closed stream.
  1801. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1802. %% Receive nothing back.
  1803. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1804. ok.
  1805. rst_stream_closed_ignore_rst_stream(Config) ->
  1806. doc("RST_STREAM frames received on a stream closed via RST_STREAM "
  1807. "must be ignored to avoid looping. (RFC7540 5.1, RFC7540 5.4.2)"),
  1808. {ok, Socket} = do_handshake(Config),
  1809. %% Send a HEADERS frame.
  1810. {HeadersBlock, _} = cow_hpack:encode([
  1811. {<<":method">>, <<"GET">>},
  1812. {<<":scheme">>, <<"http">>},
  1813. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1814. {<<":path">>, <<"/">>}
  1815. ]),
  1816. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1817. %% Send an RST_STREAM frame to close the stream.
  1818. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1819. %% Send an extra RST_STREAM.
  1820. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1821. %% Receive nothing back.
  1822. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1823. ok.
  1824. %% rst_stream_closed_reject_push_promise
  1825. %%
  1826. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1827. %% because PUSH is disabled in that direction. We therefore cannot test other
  1828. %% error conditions.
  1829. rst_stream_closed_reject_window_update(Config) ->
  1830. doc("WINDOW_UPDATE frames received on a stream closed via RST_STREAM "
  1831. "must be rejected with a STREAM_CLOSED stream error. (RFC7540 5.1)"),
  1832. {ok, Socket} = do_handshake(Config),
  1833. %% Send a HEADERS frame.
  1834. {HeadersBlock, _} = cow_hpack:encode([
  1835. {<<":method">>, <<"GET">>},
  1836. {<<":scheme">>, <<"http">>},
  1837. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1838. {<<":path">>, <<"/">>}
  1839. ]),
  1840. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1841. %% Send an RST_STREAM frame to close the stream.
  1842. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1843. %% Send a WINDOW_UPDATE frame on the now RST_STREAM closed stream.
  1844. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1845. %% Receive a STREAM_CLOSED stream error.
  1846. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1847. ok.
  1848. stream_closed_reject_data(Config) ->
  1849. doc("DATA frames received on a stream closed normally must be rejected "
  1850. "with a STREAM_CLOSED connection error. (RFC7540 5.1, RFC7540 6.1)"),
  1851. {ok, Socket} = do_handshake(Config),
  1852. %% Send a HEADERS frame.
  1853. {HeadersBlock, _} = cow_hpack:encode([
  1854. {<<":method">>, <<"GET">>},
  1855. {<<":scheme">>, <<"http">>},
  1856. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1857. {<<":path">>, <<"/">>}
  1858. ]),
  1859. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1860. %% Receive the response.
  1861. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1862. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1863. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1864. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1865. %% Send a DATA frame on the now closed stream.
  1866. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1867. %% Receive a STREAM_CLOSED connection error.
  1868. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1869. ok.
  1870. stream_closed_reject_headers(Config) ->
  1871. doc("HEADERS frames received on a stream closed normally must be rejected "
  1872. "with a STREAM_CLOSED connection error. (RFC7540 5.1)"),
  1873. {ok, Socket} = do_handshake(Config),
  1874. %% Send a HEADERS frame.
  1875. {HeadersBlock, _} = cow_hpack:encode([
  1876. {<<":method">>, <<"GET">>},
  1877. {<<":scheme">>, <<"http">>},
  1878. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1879. {<<":path">>, <<"/">>}
  1880. ]),
  1881. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1882. %% Receive the response.
  1883. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1884. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1885. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1886. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1887. %% Send a HEADERS frame on the now closed stream.
  1888. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1889. %% Receive a STREAM_CLOSED connection error.
  1890. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1891. ok.
  1892. stream_closed_accept_priority(Config) ->
  1893. doc("PRIORITY frames received on a stream closed normally must be accepted. (RFC7540 5.1)"),
  1894. {ok, Socket} = do_handshake(Config),
  1895. %% Send a HEADERS frame.
  1896. {HeadersBlock, _} = cow_hpack:encode([
  1897. {<<":method">>, <<"GET">>},
  1898. {<<":scheme">>, <<"http">>},
  1899. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1900. {<<":path">>, <<"/">>}
  1901. ]),
  1902. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1903. %% Receive the response.
  1904. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1905. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1906. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1907. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1908. %% Send a PRIORITY frame on the now closed stream.
  1909. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1910. %% Receive nothing back.
  1911. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1912. ok.
  1913. stream_closed_accept_rst_stream(Config) ->
  1914. doc("RST_STREAM frames received on a stream closed normally "
  1915. "must be accepted for a short period. (RFC7540 5.1)"),
  1916. {ok, Socket} = do_handshake(Config),
  1917. %% Send a HEADERS frame.
  1918. {HeadersBlock, _} = cow_hpack:encode([
  1919. {<<":method">>, <<"GET">>},
  1920. {<<":scheme">>, <<"http">>},
  1921. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1922. {<<":path">>, <<"/">>}
  1923. ]),
  1924. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1925. %% Receive the response.
  1926. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1927. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1928. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1929. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1930. %% Send an RST_STREAM frame on the now closed stream.
  1931. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1932. %% Receive nothing back.
  1933. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1934. ok.
  1935. %% stream_closed_reject_push_promise
  1936. %%
  1937. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1938. %% because PUSH is disabled in that direction. We therefore cannot test other
  1939. %% error conditions.
  1940. stream_closed_accept_window_update(Config) ->
  1941. doc("WINDOW_UPDATE frames received on a stream closed normally "
  1942. "must be accepted for a short period. (RFC7540 5.1)"),
  1943. {ok, Socket} = do_handshake(Config),
  1944. %% Send a HEADERS frame.
  1945. {HeadersBlock, _} = cow_hpack:encode([
  1946. {<<":method">>, <<"GET">>},
  1947. {<<":scheme">>, <<"http">>},
  1948. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1949. {<<":path">>, <<"/">>}
  1950. ]),
  1951. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1952. %% Receive the response.
  1953. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1954. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1955. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1956. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1957. %% Send a WINDOW_UPDATE frame on the now closed stream.
  1958. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1959. %% Receive nothing back.
  1960. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1961. ok.
  1962. %% @todo While we accept RST_STREAM and WINDOW_UPDATE for a short period
  1963. %% after the stream closed normally, we may want to reject the ones coming
  1964. %% a significant amount of time after that.
  1965. %% @todo Frames may arrive on a stream after we send an RST_STREAM for it.
  1966. %% They must be ignored for a short period of time:
  1967. %
  1968. % If this state is reached as a result of sending a RST_STREAM
  1969. % frame, the peer that receives the RST_STREAM might have already
  1970. % sent -- or enqueued for sending -- frames on the stream that
  1971. % cannot be withdrawn. An endpoint MUST ignore frames that it
  1972. % receives on closed streams after it has sent a RST_STREAM frame.
  1973. % An endpoint MAY choose to limit the period over which it ignores
  1974. % frames and treat frames that arrive after this time as being in
  1975. % error.
  1976. %% @todo Ensure that rejected DATA frames result in the connection
  1977. %% flow-control window being updated. How to test this?
  1978. %
  1979. % Flow-controlled frames (i.e., DATA) received after sending
  1980. % RST_STREAM are counted toward the connection flow-control window.
  1981. % Even though these frames might be ignored, because they are sent
  1982. % before the sender receives the RST_STREAM, the sender will
  1983. % consider the frames to count against the flow-control window.
  1984. %% Stream identifiers.
  1985. reject_streamid_even(Config) ->
  1986. doc("HEADERS frames received with an even-numbered streamid "
  1987. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  1988. {ok, Socket} = do_handshake(Config),
  1989. %% Send a HEADERS frame with an even-numbered streamid.
  1990. {HeadersBlock, _} = cow_hpack:encode([
  1991. {<<":method">>, <<"GET">>},
  1992. {<<":scheme">>, <<"http">>},
  1993. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1994. {<<":path">>, <<"/">>}
  1995. ]),
  1996. ok = gen_tcp:send(Socket, cow_http2:headers(2, fin, HeadersBlock)),
  1997. %% Receive a PROTOCOL_ERROR connection error.
  1998. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1999. ok.
  2000. reject_streamid_0(Config) ->
  2001. doc("HEADERS frames received with streamid 0 (zero) "
  2002. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  2003. {ok, Socket} = do_handshake(Config),
  2004. %% Send a HEADERS frame with an streamid 0.
  2005. {HeadersBlock, _} = cow_hpack:encode([
  2006. {<<":method">>, <<"GET">>},
  2007. {<<":scheme">>, <<"http">>},
  2008. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2009. {<<":path">>, <<"/">>}
  2010. ]),
  2011. ok = gen_tcp:send(Socket, cow_http2:headers(0, fin, HeadersBlock)),
  2012. %% Receive a PROTOCOL_ERROR connection error.
  2013. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2014. ok.
  2015. http_upgrade_reject_reuse_streamid_1(Config) ->
  2016. doc("Attempts to reuse streamid 1 after upgrading to HTTP/2 "
  2017. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  2018. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2019. ok = gen_tcp:send(Socket, [
  2020. "GET / HTTP/1.1\r\n"
  2021. "Host: localhost\r\n"
  2022. "Connection: Upgrade, HTTP2-Settings\r\n"
  2023. "Upgrade: h2c\r\n"
  2024. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  2025. "\r\n"]),
  2026. ok = do_recv_101(Socket),
  2027. %% Send a valid preface.
  2028. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2029. %% Receive the server preface.
  2030. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2031. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  2032. %% Send the SETTINGS ack.
  2033. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2034. %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
  2035. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  2036. case gen_tcp:recv(Socket, 9, 1000) of
  2037. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  2038. [settings_ack|Acc];
  2039. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  2040. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  2041. [headers|Acc];
  2042. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  2043. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  2044. [data|Acc]
  2045. end
  2046. end, [], [1, 2, 3])),
  2047. case Received of
  2048. [settings_ack, headers, data] -> ok;
  2049. [headers, settings_ack, data] -> ok;
  2050. [headers, data, settings_ack] -> ok
  2051. end,
  2052. %% Send a HEADERS frame with streamid 1.
  2053. {HeadersBlock, _} = cow_hpack:encode([
  2054. {<<":method">>, <<"GET">>},
  2055. {<<":scheme">>, <<"http">>},
  2056. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2057. {<<":path">>, <<"/">>}
  2058. ]),
  2059. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  2060. %% Receive a PROTOCOL_ERROR connection error.
  2061. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2062. ok.
  2063. %% The RFC gives us various error codes to return for this case,
  2064. %% depending on whether the stream existed previously and how it
  2065. %% ended up being (half-)closed. Cowboy rejects all these HEADERS
  2066. %% frames the same way: with a STREAM_CLOSED connection error.
  2067. %% Making it a connection error is particularly important in the
  2068. %% cases where a stream error would be allowed because we avoid
  2069. %% having to decode the headers and save up resources.
  2070. reject_streamid_lower(Config) ->
  2071. doc("HEADERS frames received with streamid lower than the previous stream "
  2072. "must be rejected with a STREAM_CLOSED connection error. (RFC7540 5.1.1)"),
  2073. {ok, Socket} = do_handshake(Config),
  2074. %% Send a HEADERS frame with streamid 5.
  2075. {HeadersBlock, _} = cow_hpack:encode([
  2076. {<<":method">>, <<"GET">>},
  2077. {<<":scheme">>, <<"http">>},
  2078. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2079. {<<":path">>, <<"/">>}
  2080. ]),
  2081. ok = gen_tcp:send(Socket, cow_http2:headers(5, fin, HeadersBlock)),
  2082. %% Receive the response.
  2083. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2084. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  2085. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2086. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  2087. %% Send a HEADERS frame with streamid 3.
  2088. ok = gen_tcp:send(Socket, cow_http2:headers(3, fin, HeadersBlock)),
  2089. %% Receive a STREAM_CLOSED connection error.
  2090. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2091. ok.
  2092. %% @todo We need an option to limit the number of streams one can open
  2093. %% on a connection. And we need to enforce it. (RFC7540 5.1.1)
  2094. %
  2095. % Stream identifiers cannot be reused. Long-lived connections can
  2096. % result in an endpoint exhausting the available range of stream
  2097. % identifiers. A server
  2098. % that is unable to establish a new stream identifier can send a GOAWAY
  2099. % frame so that the client is forced to open a new connection for new
  2100. % streams.
  2101. %% @todo We need this option too. (RFC7540 5.1.2)
  2102. % A peer can limit the number of concurrently active streams using the
  2103. % SETTINGS_MAX_CONCURRENT_STREAMS parameter (see Section 6.5.2) within
  2104. % a SETTINGS frame. The maximum concurrent streams setting is specific
  2105. % to each endpoint and applies only to the peer that receives the
  2106. % setting. That is, clients specify the maximum number of concurrent
  2107. % streams the server can initiate, and servers specify the maximum
  2108. % number of concurrent streams the client can initiate.
  2109. %
  2110. % Streams that are in the "open" state or in either of the "half-
  2111. % closed" states count toward the maximum number of streams that an
  2112. % endpoint is permitted to open. Streams in any of these three states
  2113. % count toward the limit advertised in the
  2114. % SETTINGS_MAX_CONCURRENT_STREAMS setting. Streams in either of the
  2115. % "reserved" states do not count toward the stream limit.
  2116. %
  2117. % Endpoints MUST NOT exceed the limit set by their peer. An endpoint
  2118. % that receives a HEADERS frame that causes its advertised concurrent
  2119. % stream limit to be exceeded MUST treat this as a stream error
  2120. % (Section 5.4.2) of type PROTOCOL_ERROR or REFUSED_STREAM. The choice
  2121. % of error code determines whether the endpoint wishes to enable
  2122. % automatic retry (see Section 8.1.4) for details).
  2123. %
  2124. % An endpoint that wishes to reduce the value of
  2125. % SETTINGS_MAX_CONCURRENT_STREAMS to a value that is below the current
  2126. % number of open streams can either close streams that exceed the new
  2127. % value or allow streams to complete.
  2128. %% (RFC7540 5.2.1)
  2129. % 3. Flow control is directional with overall control provided by the
  2130. % receiver. A receiver MAY choose to set any window size that it
  2131. % desires for each stream and for the entire connection. A sender
  2132. % MUST respect flow-control limits imposed by a receiver. Clients,
  2133. % servers, and intermediaries all independently advertise their
  2134. % flow-control window as a receiver and abide by the flow-control
  2135. % limits set by their peer when sending.
  2136. %
  2137. % 4. The initial value for the flow-control window is 65,535 octets
  2138. % for both new streams and the overall connection.
  2139. %
  2140. % 5. The frame type determines whether flow control applies to a
  2141. % frame. Of the frames specified in this document, only DATA
  2142. % frames are subject to flow control; all other frame types do not
  2143. % consume space in the advertised flow-control window. This
  2144. % ensures that important control frames are not blocked by flow
  2145. % control.
  2146. %
  2147. % 6. Flow control cannot be disabled.
  2148. %% (RFC7540 5.2.2)
  2149. % Even with full awareness of the current bandwidth-delay product,
  2150. % implementation of flow control can be difficult. When using flow
  2151. % control, the receiver MUST read from the TCP receive buffer in a
  2152. % timely fashion. Failure to do so could lead to a deadlock when
  2153. % critical frames, such as WINDOW_UPDATE, are not read and acted upon.
  2154. %% @todo Stream priorities. (RFC7540 5.3 5.3.x)
  2155. %% (RFC7540 5.4.1)
  2156. % An endpoint that encounters a connection error SHOULD first send a
  2157. % GOAWAY frame (Section 6.8) with the stream identifier of the last
  2158. % stream that it successfully received from its peer. The GOAWAY frame
  2159. % includes an error code that indicates why the connection is
  2160. % terminating. After sending the GOAWAY frame for an error condition,
  2161. % the endpoint MUST close the TCP connection.
  2162. %
  2163. % An endpoint can end a connection at any time. In particular, an
  2164. % endpoint MAY choose to treat a stream error as a connection error.
  2165. % Endpoints SHOULD send a GOAWAY frame when ending a connection,
  2166. % providing that circumstances permit it.
  2167. %% (RFC7540 5.4.2)
  2168. % A RST_STREAM is the last frame that an endpoint can send on a stream.
  2169. % The peer that sends the RST_STREAM frame MUST be prepared to receive
  2170. % any frames that were sent or enqueued for sending by the remote peer.
  2171. % These frames can be ignored, except where they modify connection
  2172. % state (such as the state maintained for header compression
  2173. % (Section 4.3) or flow control).
  2174. %
  2175. % Normally, an endpoint SHOULD NOT send more than one RST_STREAM frame
  2176. % for any stream. However, an endpoint MAY send additional RST_STREAM
  2177. % frames if it receives frames on a closed stream after more than a
  2178. % round-trip time. This behavior is permitted to deal with misbehaving
  2179. % implementations.
  2180. %
  2181. % To avoid looping, an endpoint MUST NOT send a RST_STREAM in response
  2182. % to a RST_STREAM frame.
  2183. %% (RFC7540 5.5)
  2184. % Extensions are permitted to use new frame types (Section 4.1), new
  2185. % settings (Section 6.5.2), or new error codes (Section 7). Registries
  2186. % are established for managing these extension points: frame types
  2187. % (Section 11.2), settings (Section 11.3), and error codes
  2188. % (Section 11.4).
  2189. %
  2190. % Implementations MUST ignore unknown or unsupported values in all
  2191. % extensible protocol elements. Implementations MUST discard frames
  2192. % that have unknown or unsupported types. This means that any of these
  2193. % extension points can be safely used by extensions without prior
  2194. % arrangement or negotiation. However, extension frames that appear in
  2195. % the middle of a header block (Section 4.3) are not permitted; these
  2196. % MUST be treated as a connection error (Section 5.4.1) of type
  2197. % PROTOCOL_ERROR.
  2198. continuation_with_extension_frame_interleaved_error(Config) ->
  2199. doc("Extension frames interleaved in a header block must be rejected "
  2200. "with a PROTOCOL_ERROR connection error. "
  2201. "(RFC7540 4.3, RFC7540 5.5, RFC7540 6.2, RFC7540 6.10)"),
  2202. {ok, Socket} = do_handshake(Config),
  2203. %% Send an unterminated HEADERS frame followed by an extension frame.
  2204. ok = gen_tcp:send(Socket, [
  2205. << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
  2206. << 0:24, 128:8, 0:8, 0:32 >>
  2207. ]),
  2208. %% Receive a PROTOCOL_ERROR connection error.
  2209. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2210. ok.
  2211. %% (RFC7540 6.1) DATA
  2212. % Padding: Padding octets that contain no application semantic value.
  2213. % Padding octets MUST be set to zero when sending. A receiver is
  2214. % not obligated to verify padding but MAY treat non-zero padding as
  2215. % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  2216. %
  2217. % DATA frames MUST be associated with a stream. If a DATA frame is
  2218. % received whose stream identifier field is 0x0, the recipient MUST
  2219. % respond with a connection error (Section 5.4.1) of type
  2220. % PROTOCOL_ERROR.
  2221. %% (RFC7540 6.2) HEADERS
  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. % A HEADERS frame carries the END_STREAM flag that signals the end
  2228. % of a stream. However, a HEADERS frame with the END_STREAM flag
  2229. % set can be followed by CONTINUATION frames on the same stream.
  2230. % Logically, the CONTINUATION frames are part of the HEADERS frame.
  2231. %
  2232. %% @todo We probably need a test for the server sending HEADERS too large.
  2233. % The payload of a HEADERS frame contains a header block fragment
  2234. % (Section 4.3). A header block that does not fit within a HEADERS
  2235. % frame is continued in a CONTINUATION frame (Section 6.10).
  2236. %
  2237. % HEADERS frames MUST be associated with a stream. If a HEADERS frame
  2238. % is received whose stream identifier field is 0x0, the recipient MUST
  2239. % respond with a connection error (Section 5.4.1) of type
  2240. % PROTOCOL_ERROR.
  2241. %% (RFC7540 6.3) PRIORITY
  2242. % The PRIORITY frame always identifies a stream. If a PRIORITY frame
  2243. % is received with a stream identifier of 0x0, the recipient MUST
  2244. % respond with a connection error (Section 5.4.1) of type
  2245. % PROTOCOL_ERROR.
  2246. %% (RFC7540 6.4) RST_STREAM
  2247. % The RST_STREAM frame fully terminates the referenced stream and
  2248. % causes it to enter the "closed" state. After receiving a RST_STREAM
  2249. % on a stream, the receiver MUST NOT send additional frames for that
  2250. % stream, with the exception of PRIORITY. However, after sending the
  2251. % RST_STREAM, the sending endpoint MUST be prepared to receive and
  2252. % process additional frames sent on the stream that might have been
  2253. % sent by the peer prior to the arrival of the RST_STREAM.
  2254. %
  2255. % RST_STREAM frames MUST be associated with a stream. If a RST_STREAM
  2256. % frame is received with a stream identifier of 0x0, the recipient MUST
  2257. % treat this as a connection error (Section 5.4.1) of type
  2258. % PROTOCOL_ERROR.
  2259. %% (RFC7540 6.5) SETTINGS
  2260. % A SETTINGS frame MUST be sent by both endpoints at the start of a
  2261. % connection and MAY be sent at any other time by either endpoint over
  2262. % the lifetime of the connection. Implementations MUST support all of
  2263. % the parameters defined by this specification.
  2264. %
  2265. % SETTINGS frames always apply to a connection, never a single stream.
  2266. % The stream identifier for a SETTINGS frame MUST be zero (0x0). If an
  2267. % endpoint receives a SETTINGS frame whose stream identifier field is
  2268. % anything other than 0x0, the endpoint MUST respond with a connection
  2269. % error (Section 5.4.1) of type PROTOCOL_ERROR.
  2270. %
  2271. % The SETTINGS frame affects connection state. A badly formed or
  2272. % incomplete SETTINGS frame MUST be treated as a connection error
  2273. % (Section 5.4.1) of type PROTOCOL_ERROR.
  2274. %% (RFC7540 6.5.2)
  2275. % SETTINGS_ENABLE_PUSH (0x2): This setting can be used to disable
  2276. % server push (Section 8.2). An endpoint MUST NOT send a
  2277. % PUSH_PROMISE frame if it receives this parameter set to a value of
  2278. % 0. An endpoint that has both set this parameter to 0 and had it
  2279. % acknowledged MUST treat the receipt of a PUSH_PROMISE frame as a
  2280. % connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  2281. %
  2282. % SETTINGS_MAX_CONCURRENT_STREAMS (0x3): Indicates the maximum number
  2283. % of concurrent streams that the sender will allow. This limit is
  2284. % directional: it applies to the number of streams that the sender
  2285. % permits the receiver to create. Initially, there is no limit to
  2286. % this value. It is recommended that this value be no smaller than
  2287. % 100, so as to not unnecessarily limit parallelism.
  2288. %
  2289. % A value of 0 for SETTINGS_MAX_CONCURRENT_STREAMS SHOULD NOT be
  2290. % treated as special by endpoints. A zero value does prevent the
  2291. % creation of new streams; however, this can also happen for any
  2292. % limit that is exhausted with active streams. Servers SHOULD only
  2293. % set a zero value for short durations; if a server does not wish to
  2294. % accept requests, closing the connection is more appropriate.
  2295. %
  2296. % SETTINGS_INITIAL_WINDOW_SIZE (0x4):
  2297. % Values above the maximum flow-control window size of 2^31-1 MUST
  2298. % be treated as a connection error (Section 5.4.1) of type
  2299. % FLOW_CONTROL_ERROR.
  2300. %
  2301. % SETTINGS_MAX_FRAME_SIZE (0x5):
  2302. % The initial value is 2^14 (16,384) octets. The value advertised
  2303. % by an endpoint MUST be between this initial value and the maximum
  2304. % allowed frame size (2^24-1 or 16,777,215 octets), inclusive.
  2305. % Values outside this range MUST be treated as a connection error
  2306. % (Section 5.4.1) of type PROTOCOL_ERROR.
  2307. %
  2308. % An endpoint that receives a SETTINGS frame with any unknown or
  2309. % unsupported identifier MUST ignore that setting. (6.5.2 and 6.5.3)
  2310. %% (RFC7540 6.5.3)
  2311. % Upon receiving a SETTINGS frame with the ACK flag set, the
  2312. % sender of the altered parameters can rely on the setting having been
  2313. % applied.
  2314. %
  2315. % If the sender of a SETTINGS frame does not receive an acknowledgement
  2316. % within a reasonable amount of time, it MAY issue a connection error
  2317. % (Section 5.4.1) of type SETTINGS_TIMEOUT.
  2318. %% (RFC7540 6.6) PUSH_PROMISE
  2319. % @todo PUSH_PROMISE frames have a reserved bit in the payload that must be ignored.
  2320. %
  2321. % Padding: Padding octets that contain no application semantic value.
  2322. % Padding octets MUST be set to zero when sending. A receiver is
  2323. % not obligated to verify padding but MAY treat non-zero padding as
  2324. % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  2325. %
  2326. % PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that
  2327. % is in either the "open" or "half-closed (remote)" state. The stream
  2328. % identifier of a PUSH_PROMISE frame indicates the stream it is
  2329. % associated with. If the stream identifier field specifies the value
  2330. % 0x0, a recipient MUST respond with a connection error (Section 5.4.1)
  2331. % of type PROTOCOL_ERROR.
  2332. %
  2333. % PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH setting of
  2334. % the peer endpoint is set to 0. An endpoint that has set this setting
  2335. % and has received acknowledgement MUST treat the receipt of a
  2336. % PUSH_PROMISE frame as a connection error (Section 5.4.1) of type
  2337. % PROTOCOL_ERROR.
  2338. %
  2339. % Since PUSH_PROMISE reserves a stream, ignoring a PUSH_PROMISE frame
  2340. % causes the stream state to become indeterminate. A receiver MUST
  2341. % treat the receipt of a PUSH_PROMISE on a stream that is neither
  2342. % "open" nor "half-closed (local)" as a connection error
  2343. % (Section 5.4.1) of type PROTOCOL_ERROR.
  2344. %
  2345. % A receiver MUST treat the receipt of a PUSH_PROMISE that promises an
  2346. % illegal stream identifier (Section 5.1.1) as a connection error
  2347. % (Section 5.4.1) of type PROTOCOL_ERROR. Note that an illegal stream
  2348. % identifier is an identifier for a stream that is not currently in the
  2349. % "idle" state.
  2350. %% (RFC7540 6.7) PING
  2351. % PING frames are not associated with any individual stream. If a PING
  2352. % frame is received with a stream identifier field value other than
  2353. % 0x0, the recipient MUST respond with a connection error
  2354. % (Section 5.4.1) of type PROTOCOL_ERROR.
  2355. %% (RFC7540 6.8) GOAWAY
  2356. % @todo GOAWAY frames have a reserved bit in the payload that must be ignored.
  2357. %
  2358. %% @todo We should eventually implement the mechanism for gracefully
  2359. %% shutting down of the connection. (Send the GOAWAY, finish processing
  2360. %% the current set of streams, give up after a certain timeout.)
  2361. %
  2362. %% @todo If we graceful shutdown and receive a GOAWAY, we give up too.
  2363. % A GOAWAY frame might not immediately precede closing of the
  2364. % connection; a receiver of a GOAWAY that has no more use for the
  2365. % connection SHOULD still send a GOAWAY frame before terminating the
  2366. % connection.
  2367. %
  2368. %% @todo And it gets more complex when you think about h1 to h2 proxies.
  2369. % A server that is attempting to gracefully shut down a
  2370. % connection SHOULD send an initial GOAWAY frame with the last stream
  2371. % identifier set to 2^31-1 and a NO_ERROR code. This signals to the
  2372. % client that a shutdown is imminent and that initiating further
  2373. % requests is prohibited. After allowing time for any in-flight stream
  2374. % creation (at least one round-trip time), the server can send another
  2375. % GOAWAY frame with an updated last stream identifier. This ensures
  2376. % that a connection can be cleanly shut down without losing requests.
  2377. %
  2378. %% @todo And of course even if we shutdown we need to be careful about
  2379. %% the connection state.
  2380. % After sending a GOAWAY frame, the sender can discard frames for
  2381. % streams initiated by the receiver with identifiers higher than the
  2382. % identified last stream. However, any frames that alter connection
  2383. % state cannot be completely ignored. For instance, HEADERS,
  2384. % PUSH_PROMISE, and CONTINUATION frames MUST be minimally processed to
  2385. % ensure the state maintained for header compression is consistent (see
  2386. % Section 4.3); similarly, DATA frames MUST be counted toward the
  2387. % connection flow-control window. Failure to process these frames can
  2388. % cause flow control or header compression state to become
  2389. % unsynchronized.
  2390. %
  2391. % The GOAWAY frame applies to the connection, not a specific stream.
  2392. % An endpoint MUST treat a GOAWAY frame with a stream identifier other
  2393. % than 0x0 as a connection error (Section 5.4.1) of type
  2394. % PROTOCOL_ERROR.
  2395. %% (RFC7540 6.9) WINDOW_UPDATE
  2396. % @todo WINDOW_UPDATE frames have a reserved bit in the payload that must be ignored.
  2397. window_update_reject_0(Config) ->
  2398. doc("WINDOW_UPDATE frames with an increment of 0 for the connection "
  2399. "flow control window must be rejected with a "
  2400. "PROTOCOL_ERROR connection error. (RFC7540 6.9.1)"),
  2401. {ok, Socket} = do_handshake(Config),
  2402. %% Send connection-wide WINDOW_UPDATE frame with a value of 0.
  2403. ok = gen_tcp:send(Socket, [
  2404. cow_http2:window_update(0)
  2405. ]),
  2406. %% Receive a PROTOCOL_ERROR connection error.
  2407. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2408. ok.
  2409. window_update_reject_0_stream(Config) ->
  2410. doc("WINDOW_UPDATE frames with an increment of 0 for a stream "
  2411. "flow control window must be rejected with a "
  2412. "PROTOCOL_ERROR stream error. (RFC7540 6.9.1)"),
  2413. {ok, Socket} = do_handshake(Config),
  2414. %% Send a HEADERS frame immediately followed by
  2415. %% a WINDOW_UPDATE frame with a value of 0.
  2416. {HeadersBlock, _} = cow_hpack:encode([
  2417. {<<":method">>, <<"GET">>},
  2418. {<<":scheme">>, <<"http">>},
  2419. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2420. {<<":path">>, <<"/">>}
  2421. ]),
  2422. ok = gen_tcp:send(Socket, [
  2423. cow_http2:headers(1, fin, HeadersBlock),
  2424. cow_http2:window_update(1, 0)
  2425. ]),
  2426. %% Receive a PROTOCOL_ERROR stream error.
  2427. {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  2428. ok.
  2429. % A receiver that receives a flow-controlled frame MUST always account
  2430. % for its contribution against the connection flow-control window,
  2431. % unless the receiver treats this as a connection error
  2432. % (Section 5.4.1). This is necessary even if the frame is in error.
  2433. % The sender counts the frame toward the flow-control window, but if
  2434. % the receiver does not, the flow-control window at the sender and
  2435. % receiver can become different.
  2436. %% (RFC7540 6.9.1)
  2437. % The sender MUST NOT
  2438. % send a flow-controlled frame with a length that exceeds the space
  2439. % available in either of the flow-control windows advertised by the
  2440. % receiver. Frames with zero length with the END_STREAM flag set (that
  2441. % is, an empty DATA frame) MAY be sent if there is no available space
  2442. % in either flow-control window.
  2443. window_update_reject_overflow(Config) ->
  2444. doc("WINDOW_UPDATE frames that cause the connection flow control "
  2445. "window to exceed 2^31-1 must be rejected with a "
  2446. "FLOW_CONTROL_ERROR connection error. (RFC7540 6.9.1)"),
  2447. {ok, Socket} = do_handshake(Config),
  2448. %% Send connection-wide WINDOW_UPDATE frame that causes the window to overflow.
  2449. ok = gen_tcp:send(Socket, [
  2450. cow_http2:window_update(2147483647)
  2451. ]),
  2452. %% Receive a FLOW_CONTROL_ERROR connection error.
  2453. {ok, << _:24, 7:8, _:72, 3:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2454. ok.
  2455. window_update_reject_overflow_stream(Config) ->
  2456. doc("WINDOW_UPDATE frames that cause a stream flow control "
  2457. "window to exceed 2^31-1 must be rejected with a "
  2458. "FLOW_CONTROL_ERROR stream error. (RFC7540 6.9.1)"),
  2459. {ok, Socket} = do_handshake(Config),
  2460. %% Send a HEADERS frame immediately followed by a WINDOW_UPDATE
  2461. %% frame that causes the stream window to overflow.
  2462. {HeadersBlock, _} = cow_hpack:encode([
  2463. {<<":method">>, <<"GET">>},
  2464. {<<":scheme">>, <<"http">>},
  2465. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2466. {<<":path">>, <<"/">>}
  2467. ]),
  2468. ok = gen_tcp:send(Socket, [
  2469. cow_http2:headers(1, fin, HeadersBlock),
  2470. cow_http2:window_update(1, 2147483647)
  2471. ]),
  2472. %% Receive a FLOW_CONTROL_ERROR stream error.
  2473. {ok, << _:24, 3:8, _:8, 1:32, 3:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  2474. ok.