rfc7540_SUITE.erl 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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)"),
  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)"),
  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. half_closed_remote_reject_headers(Config) ->
  1671. doc("HEADERS frames received on a half-closed (remote) stream must be rejected "
  1672. "with a STREAM_CLOSED stream error. (RFC7540 5.1)"),
  1673. {ok, Socket} = do_handshake(Config),
  1674. %% Send a HEADERS frame with the FIN flag set.
  1675. {HeadersBlock, _} = cow_hpack:encode([
  1676. {<<":method">>, <<"GET">>},
  1677. {<<":scheme">>, <<"http">>},
  1678. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1679. {<<":path">>, <<"/">>}
  1680. ]),
  1681. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1682. %% Send a HEADERS frame on that now half-closed (remote) stream.
  1683. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1684. %% Receive a STREAM_CLOSED stream error.
  1685. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1686. ok.
  1687. half_closed_remote_accept_priority(Config) ->
  1688. doc("PRIORITY frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1689. {ok, Socket} = do_handshake(Config),
  1690. %% Send a HEADERS frame with the FIN flag set.
  1691. {HeadersBlock, _} = cow_hpack:encode([
  1692. {<<":method">>, <<"GET">>},
  1693. {<<":scheme">>, <<"http">>},
  1694. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1695. {<<":path">>, <<"/">>}
  1696. ]),
  1697. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1698. %% Send a PRIORITY frame on that now half-closed (remote) stream.
  1699. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1700. %% Receive a HEADERS frame as a response.
  1701. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1702. ok.
  1703. half_closed_remote_accept_rst_stream(Config) ->
  1704. doc("RST_STREAM frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1705. {ok, Socket} = do_handshake(Config),
  1706. %% Send a HEADERS frame with the FIN flag set.
  1707. {HeadersBlock, _} = cow_hpack:encode([
  1708. {<<":method">>, <<"GET">>},
  1709. {<<":scheme">>, <<"http">>},
  1710. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1711. {<<":path">>, <<"/">>}
  1712. ]),
  1713. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1714. %% Send an RST_STREAM frame on that now half-closed (remote) stream.
  1715. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, no_error)),
  1716. %% Receive nothing back.
  1717. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1718. ok.
  1719. %% half_closed_remote_reject_push_promise
  1720. %%
  1721. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1722. %% because PUSH is disabled in that direction. We therefore cannot test other
  1723. %% error conditions.
  1724. half_closed_remote_accept_window_update(Config) ->
  1725. doc("WINDOW_UPDATE frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
  1726. {ok, Socket} = do_handshake(Config),
  1727. %% Send a HEADERS frame with the FIN flag set.
  1728. {HeadersBlock, _} = cow_hpack:encode([
  1729. {<<":method">>, <<"GET">>},
  1730. {<<":scheme">>, <<"http">>},
  1731. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1732. {<<":path">>, <<"/">>}
  1733. ]),
  1734. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1735. %% Send a WINDOW_UPDATE frame on that now half-closed (remote) stream.
  1736. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1737. %% Receive a HEADERS frame as a response.
  1738. {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1739. ok.
  1740. rst_stream_closed_reject_data(Config) ->
  1741. doc("DATA frames received on a stream closed via RST_STREAM must be rejected "
  1742. "with a STREAM_CLOSED stream error. (RFC7540 5.1)"),
  1743. {ok, Socket} = do_handshake(Config),
  1744. %% Send a HEADERS frame.
  1745. {HeadersBlock, _} = cow_hpack:encode([
  1746. {<<":method">>, <<"GET">>},
  1747. {<<":scheme">>, <<"http">>},
  1748. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1749. {<<":path">>, <<"/">>}
  1750. ]),
  1751. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1752. %% Send an RST_STREAM frame to close the stream.
  1753. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1754. %% Send a DATA frame on the now RST_STREAM closed stream.
  1755. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1756. %% Receive a STREAM_CLOSED stream error.
  1757. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1758. ok.
  1759. rst_stream_closed_reject_headers(Config) ->
  1760. doc("HEADERS frames received on a stream closed via RST_STREAM must be rejected "
  1761. "with a STREAM_CLOSED stream error. (RFC7540 5.1)"),
  1762. {ok, Socket} = do_handshake(Config),
  1763. %% Send a HEADERS frame.
  1764. {HeadersBlock, _} = cow_hpack:encode([
  1765. {<<":method">>, <<"GET">>},
  1766. {<<":scheme">>, <<"http">>},
  1767. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1768. {<<":path">>, <<"/">>}
  1769. ]),
  1770. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1771. %% Send an RST_STREAM frame to close the stream.
  1772. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1773. %% Send a HEADERS frame on the now RST_STREAM closed stream.
  1774. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1775. %% Receive a STREAM_CLOSED stream error.
  1776. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1777. ok.
  1778. rst_stream_closed_accept_priority(Config) ->
  1779. doc("PRIORITY frames received on a stream closed via RST_STREAM "
  1780. "must be accepted. (RFC7540 5.1)"),
  1781. {ok, Socket} = do_handshake(Config),
  1782. %% Send a HEADERS frame.
  1783. {HeadersBlock, _} = cow_hpack:encode([
  1784. {<<":method">>, <<"GET">>},
  1785. {<<":scheme">>, <<"http">>},
  1786. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1787. {<<":path">>, <<"/">>}
  1788. ]),
  1789. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1790. %% Send an RST_STREAM frame to close the stream.
  1791. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1792. %% Send a PRIORITY frame on that now RST_STREAM closed stream.
  1793. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1794. %% Receive nothing back.
  1795. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1796. ok.
  1797. rst_stream_closed_ignore_rst_stream(Config) ->
  1798. doc("RST_STREAM frames received on a stream closed via RST_STREAM "
  1799. "must be ignored to avoid looping. (RFC7540 5.1, RFC7540 5.4.2)"),
  1800. {ok, Socket} = do_handshake(Config),
  1801. %% Send a HEADERS frame.
  1802. {HeadersBlock, _} = cow_hpack:encode([
  1803. {<<":method">>, <<"GET">>},
  1804. {<<":scheme">>, <<"http">>},
  1805. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1806. {<<":path">>, <<"/">>}
  1807. ]),
  1808. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1809. %% Send an RST_STREAM frame to close the stream.
  1810. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1811. %% Send an extra RST_STREAM.
  1812. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1813. %% Receive nothing back.
  1814. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1815. ok.
  1816. %% rst_stream_closed_reject_push_promise
  1817. %%
  1818. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1819. %% because PUSH is disabled in that direction. We therefore cannot test other
  1820. %% error conditions.
  1821. rst_stream_closed_reject_window_update(Config) ->
  1822. doc("WINDOW_UPDATE frames received on a stream closed via RST_STREAM "
  1823. "must be rejected with a STREAM_CLOSED stream error. (RFC7540 5.1)"),
  1824. {ok, Socket} = do_handshake(Config),
  1825. %% Send a HEADERS frame.
  1826. {HeadersBlock, _} = cow_hpack:encode([
  1827. {<<":method">>, <<"GET">>},
  1828. {<<":scheme">>, <<"http">>},
  1829. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1830. {<<":path">>, <<"/">>}
  1831. ]),
  1832. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  1833. %% Send an RST_STREAM frame to close the stream.
  1834. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1835. %% Send a WINDOW_UPDATE frame on the now RST_STREAM closed stream.
  1836. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1837. %% Receive a STREAM_CLOSED stream error.
  1838. {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  1839. ok.
  1840. stream_closed_reject_data(Config) ->
  1841. doc("DATA frames received on a stream closed normally must be rejected "
  1842. "with a STREAM_CLOSED connection error. (RFC7540 5.1)"),
  1843. {ok, Socket} = do_handshake(Config),
  1844. %% Send a HEADERS frame.
  1845. {HeadersBlock, _} = cow_hpack:encode([
  1846. {<<":method">>, <<"GET">>},
  1847. {<<":scheme">>, <<"http">>},
  1848. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1849. {<<":path">>, <<"/">>}
  1850. ]),
  1851. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1852. %% Receive the response.
  1853. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1854. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1855. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1856. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1857. %% Send a DATA frame on the now closed stream.
  1858. ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
  1859. %% Receive a STREAM_CLOSED connection error.
  1860. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1861. ok.
  1862. stream_closed_reject_headers(Config) ->
  1863. doc("HEADERS frames received on a stream closed normally must be rejected "
  1864. "with a STREAM_CLOSED connection error. (RFC7540 5.1)"),
  1865. {ok, Socket} = do_handshake(Config),
  1866. %% Send a HEADERS frame.
  1867. {HeadersBlock, _} = cow_hpack:encode([
  1868. {<<":method">>, <<"GET">>},
  1869. {<<":scheme">>, <<"http">>},
  1870. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1871. {<<":path">>, <<"/">>}
  1872. ]),
  1873. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1874. %% Receive the response.
  1875. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1876. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1877. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1878. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1879. %% Send a HEADERS frame on the now closed stream.
  1880. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1881. %% Receive a STREAM_CLOSED connection error.
  1882. {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1883. ok.
  1884. stream_closed_accept_priority(Config) ->
  1885. doc("PRIORITY frames received on a stream closed normally must be accepted. (RFC7540 5.1)"),
  1886. {ok, Socket} = do_handshake(Config),
  1887. %% Send a HEADERS frame.
  1888. {HeadersBlock, _} = cow_hpack:encode([
  1889. {<<":method">>, <<"GET">>},
  1890. {<<":scheme">>, <<"http">>},
  1891. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1892. {<<":path">>, <<"/">>}
  1893. ]),
  1894. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1895. %% Receive the response.
  1896. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1897. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1898. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1899. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1900. %% Send a PRIORITY frame on the now closed stream.
  1901. ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
  1902. %% Receive nothing back.
  1903. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1904. ok.
  1905. stream_closed_accept_rst_stream(Config) ->
  1906. doc("RST_STREAM frames received on a stream closed normally "
  1907. "must be accepted for a short period. (RFC7540 5.1)"),
  1908. {ok, Socket} = do_handshake(Config),
  1909. %% Send a HEADERS frame.
  1910. {HeadersBlock, _} = cow_hpack:encode([
  1911. {<<":method">>, <<"GET">>},
  1912. {<<":scheme">>, <<"http">>},
  1913. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1914. {<<":path">>, <<"/">>}
  1915. ]),
  1916. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1917. %% Receive the response.
  1918. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1919. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1920. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1921. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1922. %% Send an RST_STREAM frame on the now closed stream.
  1923. ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
  1924. %% Receive nothing back.
  1925. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1926. ok.
  1927. %% stream_closed_reject_push_promise
  1928. %%
  1929. %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
  1930. %% because PUSH is disabled in that direction. We therefore cannot test other
  1931. %% error conditions.
  1932. stream_closed_accept_window_update(Config) ->
  1933. doc("WINDOW_UPDATE frames received on a stream closed normally "
  1934. "must be accepted for a short period. (RFC7540 5.1)"),
  1935. {ok, Socket} = do_handshake(Config),
  1936. %% Send a HEADERS frame.
  1937. {HeadersBlock, _} = cow_hpack:encode([
  1938. {<<":method">>, <<"GET">>},
  1939. {<<":scheme">>, <<"http">>},
  1940. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1941. {<<":path">>, <<"/">>}
  1942. ]),
  1943. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1944. %% Receive the response.
  1945. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1946. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  1947. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  1948. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  1949. %% Send a WINDOW_UPDATE frame on the now closed stream.
  1950. ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
  1951. %% Receive nothing back.
  1952. {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
  1953. ok.
  1954. %% @todo While we accept RST_STREAM and WINDOW_UPDATE for a short period
  1955. %% after the stream closed normally, we may want to reject the ones coming
  1956. %% a significant amount of time after that.
  1957. %% @todo Frames may arrive on a stream after we send an RST_STREAM for it.
  1958. %% They must be ignored for a short period of time:
  1959. %
  1960. % If this state is reached as a result of sending a RST_STREAM
  1961. % frame, the peer that receives the RST_STREAM might have already
  1962. % sent -- or enqueued for sending -- frames on the stream that
  1963. % cannot be withdrawn. An endpoint MUST ignore frames that it
  1964. % receives on closed streams after it has sent a RST_STREAM frame.
  1965. % An endpoint MAY choose to limit the period over which it ignores
  1966. % frames and treat frames that arrive after this time as being in
  1967. % error.
  1968. %% @todo Ensure that rejected DATA frames result in the connection
  1969. %% flow-control window being updated. How to test this?
  1970. %
  1971. % Flow-controlled frames (i.e., DATA) received after sending
  1972. % RST_STREAM are counted toward the connection flow-control window.
  1973. % Even though these frames might be ignored, because they are sent
  1974. % before the sender receives the RST_STREAM, the sender will
  1975. % consider the frames to count against the flow-control window.
  1976. %% Stream identifiers.
  1977. reject_streamid_even(Config) ->
  1978. doc("HEADERS frames received with an even-numbered streamid "
  1979. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  1980. {ok, Socket} = do_handshake(Config),
  1981. %% Send a HEADERS frame with an even-numbered streamid.
  1982. {HeadersBlock, _} = cow_hpack:encode([
  1983. {<<":method">>, <<"GET">>},
  1984. {<<":scheme">>, <<"http">>},
  1985. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1986. {<<":path">>, <<"/">>}
  1987. ]),
  1988. ok = gen_tcp:send(Socket, cow_http2:headers(2, fin, HeadersBlock)),
  1989. %% Receive a PROTOCOL_ERROR connection error.
  1990. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1991. ok.
  1992. reject_streamid_0(Config) ->
  1993. doc("HEADERS frames received with streamid 0 (zero) "
  1994. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  1995. {ok, Socket} = do_handshake(Config),
  1996. %% Send a HEADERS frame with an streamid 0.
  1997. {HeadersBlock, _} = cow_hpack:encode([
  1998. {<<":method">>, <<"GET">>},
  1999. {<<":scheme">>, <<"http">>},
  2000. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2001. {<<":path">>, <<"/">>}
  2002. ]),
  2003. ok = gen_tcp:send(Socket, cow_http2:headers(0, fin, HeadersBlock)),
  2004. %% Receive a PROTOCOL_ERROR connection error.
  2005. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2006. ok.
  2007. http_upgrade_reject_reuse_streamid_1(Config) ->
  2008. doc("Attempts to reuse streamid 1 after upgrading to HTTP/2 "
  2009. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  2010. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  2011. ok = gen_tcp:send(Socket, [
  2012. "GET / HTTP/1.1\r\n"
  2013. "Host: localhost\r\n"
  2014. "Connection: Upgrade, HTTP2-Settings\r\n"
  2015. "Upgrade: h2c\r\n"
  2016. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  2017. "\r\n"]),
  2018. ok = do_recv_101(Socket),
  2019. %% Send a valid preface.
  2020. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  2021. %% Receive the server preface.
  2022. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  2023. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  2024. %% Send the SETTINGS ack.
  2025. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  2026. %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
  2027. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  2028. case gen_tcp:recv(Socket, 9, 1000) of
  2029. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  2030. [settings_ack|Acc];
  2031. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  2032. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  2033. [headers|Acc];
  2034. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  2035. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  2036. [data|Acc]
  2037. end
  2038. end, [], [1, 2, 3])),
  2039. case Received of
  2040. [settings_ack, headers, data] -> ok;
  2041. [headers, settings_ack, data] -> ok;
  2042. [headers, data, settings_ack] -> ok
  2043. end,
  2044. %% Send a HEADERS frame with streamid 1.
  2045. {HeadersBlock, _} = cow_hpack:encode([
  2046. {<<":method">>, <<"GET">>},
  2047. {<<":scheme">>, <<"http">>},
  2048. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2049. {<<":path">>, <<"/">>}
  2050. ]),
  2051. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  2052. %% Receive a PROTOCOL_ERROR connection error.
  2053. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2054. ok.
  2055. reject_streamid_lower(Config) ->
  2056. doc("HEADERS frames received with streamid lower than the previous stream "
  2057. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
  2058. {ok, Socket} = do_handshake(Config),
  2059. %% Send a HEADERS frame with streamid 5.
  2060. {HeadersBlock, _} = cow_hpack:encode([
  2061. {<<":method">>, <<"GET">>},
  2062. {<<":scheme">>, <<"http">>},
  2063. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  2064. {<<":path">>, <<"/">>}
  2065. ]),
  2066. ok = gen_tcp:send(Socket, cow_http2:headers(5, fin, HeadersBlock)),
  2067. %% Receive the response.
  2068. {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2069. {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
  2070. {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
  2071. {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
  2072. %% Send a HEADERS frame with streamid 3.
  2073. ok = gen_tcp:send(Socket, cow_http2:headers(3, fin, HeadersBlock)),
  2074. %% Receive a PROTOCOL_ERROR connection error.
  2075. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  2076. ok.
  2077. %% @todo We need an option to limit the number of streams one can open
  2078. %% on a connection. And we need to enforce it.
  2079. %
  2080. % Stream identifiers cannot be reused. Long-lived connections can
  2081. % result in an endpoint exhausting the available range of stream
  2082. % identifiers. A server
  2083. % that is unable to establish a new stream identifier can send a GOAWAY
  2084. % frame so that the client is forced to open a new connection for new
  2085. % streams.