rfc7540_SUITE.erl 179 KB

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