rfc7540_SUITE.erl 159 KB

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