rfc7540_SUITE.erl 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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.