rfc7540_SUITE.erl 172 KB

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