rfc7540_SUITE.erl 179 KB

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