rfc7540_SUITE.erl 144 KB

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