rfc7540_SUITE.erl 180 KB

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