rfc7540_SUITE.erl 144 KB

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