rfc7540_SUITE.erl 179 KB

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