rfc7540_SUITE.erl 150 KB

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