rfc7540_SUITE.erl 175 KB

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