rfc7540_SUITE.erl 161 KB

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