rfc7540_SUITE.erl 162 KB

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