nghttp2_session.c 250 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "nghttp2_session.h"
  26. #include <string.h>
  27. #include <stddef.h>
  28. #include <stdio.h>
  29. #include <assert.h>
  30. #include <stdarg.h>
  31. #include "nghttp2_helper.h"
  32. #include "nghttp2_net.h"
  33. #include "nghttp2_priority_spec.h"
  34. #include "nghttp2_option.h"
  35. #include "nghttp2_http.h"
  36. #include "nghttp2_pq.h"
  37. #include "nghttp2_extpri.h"
  38. #include "nghttp2_time.h"
  39. #include "nghttp2_debug.h"
  40. /*
  41. * Returns non-zero if the number of outgoing opened streams is larger
  42. * than or equal to
  43. * remote_settings.max_concurrent_streams.
  44. */
  45. static int
  46. session_is_outgoing_concurrent_streams_max(nghttp2_session *session) {
  47. return session->remote_settings.max_concurrent_streams <=
  48. session->num_outgoing_streams;
  49. }
  50. /*
  51. * Returns non-zero if the number of incoming opened streams is larger
  52. * than or equal to
  53. * local_settings.max_concurrent_streams.
  54. */
  55. static int
  56. session_is_incoming_concurrent_streams_max(nghttp2_session *session) {
  57. return session->local_settings.max_concurrent_streams <=
  58. session->num_incoming_streams;
  59. }
  60. /*
  61. * Returns non-zero if the number of incoming opened streams is larger
  62. * than or equal to
  63. * session->pending_local_max_concurrent_stream.
  64. */
  65. static int
  66. session_is_incoming_concurrent_streams_pending_max(nghttp2_session *session) {
  67. return session->pending_local_max_concurrent_stream <=
  68. session->num_incoming_streams;
  69. }
  70. /*
  71. * Returns non-zero if |lib_error| is non-fatal error.
  72. */
  73. static int is_non_fatal(int lib_error_code) {
  74. return lib_error_code < 0 && lib_error_code > NGHTTP2_ERR_FATAL;
  75. }
  76. int nghttp2_is_fatal(int lib_error_code) {
  77. return lib_error_code < NGHTTP2_ERR_FATAL;
  78. }
  79. static int session_enforce_http_messaging(nghttp2_session *session) {
  80. return (session->opt_flags & NGHTTP2_OPTMASK_NO_HTTP_MESSAGING) == 0;
  81. }
  82. /*
  83. * Returns nonzero if |frame| is trailer headers.
  84. */
  85. static int session_trailer_headers(nghttp2_session *session,
  86. nghttp2_stream *stream,
  87. nghttp2_frame *frame) {
  88. if (!stream || frame->hd.type != NGHTTP2_HEADERS) {
  89. return 0;
  90. }
  91. if (session->server) {
  92. return frame->headers.cat == NGHTTP2_HCAT_HEADERS;
  93. }
  94. return frame->headers.cat == NGHTTP2_HCAT_HEADERS &&
  95. (stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) == 0;
  96. }
  97. /* Returns nonzero if the |stream| is in reserved(remote) state */
  98. static int state_reserved_remote(nghttp2_session *session,
  99. nghttp2_stream *stream) {
  100. return stream->state == NGHTTP2_STREAM_RESERVED &&
  101. !nghttp2_session_is_my_stream_id(session, stream->stream_id);
  102. }
  103. /* Returns nonzero if the |stream| is in reserved(local) state */
  104. static int state_reserved_local(nghttp2_session *session,
  105. nghttp2_stream *stream) {
  106. return stream->state == NGHTTP2_STREAM_RESERVED &&
  107. nghttp2_session_is_my_stream_id(session, stream->stream_id);
  108. }
  109. /*
  110. * Checks whether received stream_id is valid. This function returns
  111. * 1 if it succeeds, or 0.
  112. */
  113. static int session_is_new_peer_stream_id(nghttp2_session *session,
  114. int32_t stream_id) {
  115. return stream_id != 0 &&
  116. !nghttp2_session_is_my_stream_id(session, stream_id) &&
  117. session->last_recv_stream_id < stream_id;
  118. }
  119. static int session_detect_idle_stream(nghttp2_session *session,
  120. int32_t stream_id) {
  121. /* Assume that stream object with stream_id does not exist */
  122. if (nghttp2_session_is_my_stream_id(session, stream_id)) {
  123. if (session->last_sent_stream_id < stream_id) {
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. if (session_is_new_peer_stream_id(session, stream_id)) {
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. static int session_no_rfc7540_pri_no_fallback(nghttp2_session *session) {
  134. return session->pending_no_rfc7540_priorities == 1 &&
  135. !session->fallback_rfc7540_priorities;
  136. }
  137. static int check_ext_type_set(const uint8_t *ext_types, uint8_t type) {
  138. return (ext_types[type / 8] & (1 << (type & 0x7))) > 0;
  139. }
  140. static int session_call_error_callback(nghttp2_session *session,
  141. int lib_error_code, const char *fmt,
  142. ...) {
  143. size_t bufsize;
  144. va_list ap;
  145. char *buf;
  146. int rv;
  147. nghttp2_mem *mem;
  148. if (!session->callbacks.error_callback &&
  149. !session->callbacks.error_callback2) {
  150. return 0;
  151. }
  152. mem = &session->mem;
  153. va_start(ap, fmt);
  154. rv = vsnprintf(NULL, 0, fmt, ap);
  155. va_end(ap);
  156. if (rv < 0) {
  157. return NGHTTP2_ERR_NOMEM;
  158. }
  159. bufsize = (size_t)(rv + 1);
  160. buf = nghttp2_mem_malloc(mem, bufsize);
  161. if (buf == NULL) {
  162. return NGHTTP2_ERR_NOMEM;
  163. }
  164. va_start(ap, fmt);
  165. rv = vsnprintf(buf, bufsize, fmt, ap);
  166. va_end(ap);
  167. if (rv < 0) {
  168. nghttp2_mem_free(mem, buf);
  169. /* vsnprintf may return error because of various things we can
  170. imagine, but typically we don't want to drop session just for
  171. debug callback. */
  172. DEBUGF("error_callback: vsnprintf failed. The template was %s\n", fmt);
  173. return 0;
  174. }
  175. if (session->callbacks.error_callback2) {
  176. rv = session->callbacks.error_callback2(session, lib_error_code, buf,
  177. (size_t)rv, session->user_data);
  178. } else {
  179. rv = session->callbacks.error_callback(session, buf, (size_t)rv,
  180. session->user_data);
  181. }
  182. nghttp2_mem_free(mem, buf);
  183. if (rv != 0) {
  184. return NGHTTP2_ERR_CALLBACK_FAILURE;
  185. }
  186. return 0;
  187. }
  188. static int session_terminate_session(nghttp2_session *session,
  189. int32_t last_stream_id,
  190. uint32_t error_code, const char *reason) {
  191. int rv;
  192. const uint8_t *debug_data;
  193. size_t debug_datalen;
  194. if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
  195. return 0;
  196. }
  197. /* Ignore all incoming frames because we are going to tear down the
  198. session. */
  199. session->iframe.state = NGHTTP2_IB_IGN_ALL;
  200. if (reason == NULL) {
  201. debug_data = NULL;
  202. debug_datalen = 0;
  203. } else {
  204. debug_data = (const uint8_t *)reason;
  205. debug_datalen = strlen(reason);
  206. }
  207. rv = nghttp2_session_add_goaway(session, last_stream_id, error_code,
  208. debug_data, debug_datalen,
  209. NGHTTP2_GOAWAY_AUX_TERM_ON_SEND);
  210. if (rv != 0) {
  211. return rv;
  212. }
  213. session->goaway_flags |= NGHTTP2_GOAWAY_TERM_ON_SEND;
  214. return 0;
  215. }
  216. int nghttp2_session_terminate_session(nghttp2_session *session,
  217. uint32_t error_code) {
  218. return session_terminate_session(session, session->last_proc_stream_id,
  219. error_code, NULL);
  220. }
  221. int nghttp2_session_terminate_session2(nghttp2_session *session,
  222. int32_t last_stream_id,
  223. uint32_t error_code) {
  224. return session_terminate_session(session, last_stream_id, error_code, NULL);
  225. }
  226. int nghttp2_session_terminate_session_with_reason(nghttp2_session *session,
  227. uint32_t error_code,
  228. const char *reason) {
  229. return session_terminate_session(session, session->last_proc_stream_id,
  230. error_code, reason);
  231. }
  232. int nghttp2_session_is_my_stream_id(nghttp2_session *session,
  233. int32_t stream_id) {
  234. int rem;
  235. if (stream_id == 0) {
  236. return 0;
  237. }
  238. rem = stream_id & 0x1;
  239. if (session->server) {
  240. return rem == 0;
  241. }
  242. return rem == 1;
  243. }
  244. nghttp2_stream *nghttp2_session_get_stream(nghttp2_session *session,
  245. int32_t stream_id) {
  246. nghttp2_stream *stream;
  247. stream = (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
  248. if (stream == NULL || (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) ||
  249. stream->state == NGHTTP2_STREAM_IDLE) {
  250. return NULL;
  251. }
  252. return stream;
  253. }
  254. nghttp2_stream *nghttp2_session_get_stream_raw(nghttp2_session *session,
  255. int32_t stream_id) {
  256. return (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
  257. }
  258. static void session_inbound_frame_reset(nghttp2_session *session) {
  259. nghttp2_inbound_frame *iframe = &session->iframe;
  260. nghttp2_mem *mem = &session->mem;
  261. /* A bit risky code, since if this function is called from
  262. nghttp2_session_new(), we rely on the fact that
  263. iframe->frame.hd.type is 0, so that no free is performed. */
  264. switch (iframe->frame.hd.type) {
  265. case NGHTTP2_DATA:
  266. break;
  267. case NGHTTP2_HEADERS:
  268. nghttp2_frame_headers_free(&iframe->frame.headers, mem);
  269. break;
  270. case NGHTTP2_PRIORITY:
  271. nghttp2_frame_priority_free(&iframe->frame.priority);
  272. break;
  273. case NGHTTP2_RST_STREAM:
  274. nghttp2_frame_rst_stream_free(&iframe->frame.rst_stream);
  275. break;
  276. case NGHTTP2_SETTINGS:
  277. nghttp2_frame_settings_free(&iframe->frame.settings, mem);
  278. nghttp2_mem_free(mem, iframe->iv);
  279. iframe->iv = NULL;
  280. iframe->niv = 0;
  281. iframe->max_niv = 0;
  282. break;
  283. case NGHTTP2_PUSH_PROMISE:
  284. nghttp2_frame_push_promise_free(&iframe->frame.push_promise, mem);
  285. break;
  286. case NGHTTP2_PING:
  287. nghttp2_frame_ping_free(&iframe->frame.ping);
  288. break;
  289. case NGHTTP2_GOAWAY:
  290. nghttp2_frame_goaway_free(&iframe->frame.goaway, mem);
  291. break;
  292. case NGHTTP2_WINDOW_UPDATE:
  293. nghttp2_frame_window_update_free(&iframe->frame.window_update);
  294. break;
  295. default:
  296. /* extension frame */
  297. if (check_ext_type_set(session->user_recv_ext_types,
  298. iframe->frame.hd.type)) {
  299. nghttp2_frame_extension_free(&iframe->frame.ext);
  300. } else {
  301. switch (iframe->frame.hd.type) {
  302. case NGHTTP2_ALTSVC:
  303. if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ALTSVC) == 0) {
  304. break;
  305. }
  306. nghttp2_frame_altsvc_free(&iframe->frame.ext, mem);
  307. break;
  308. case NGHTTP2_ORIGIN:
  309. if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ORIGIN) == 0) {
  310. break;
  311. }
  312. nghttp2_frame_origin_free(&iframe->frame.ext, mem);
  313. break;
  314. case NGHTTP2_PRIORITY_UPDATE:
  315. if ((session->builtin_recv_ext_types &
  316. NGHTTP2_TYPEMASK_PRIORITY_UPDATE) == 0) {
  317. break;
  318. }
  319. /* Do not call nghttp2_frame_priority_update_free, because all
  320. fields point to sbuf. */
  321. break;
  322. }
  323. }
  324. break;
  325. }
  326. memset(&iframe->frame, 0, sizeof(nghttp2_frame));
  327. memset(&iframe->ext_frame_payload, 0, sizeof(nghttp2_ext_frame_payload));
  328. iframe->state = NGHTTP2_IB_READ_HEAD;
  329. nghttp2_buf_wrap_init(&iframe->sbuf, iframe->raw_sbuf,
  330. sizeof(iframe->raw_sbuf));
  331. iframe->sbuf.mark += NGHTTP2_FRAME_HDLEN;
  332. nghttp2_buf_free(&iframe->lbuf, mem);
  333. nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
  334. iframe->raw_lbuf = NULL;
  335. iframe->payloadleft = 0;
  336. iframe->padlen = 0;
  337. }
  338. static void init_settings(nghttp2_settings_storage *settings) {
  339. settings->header_table_size = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
  340. settings->enable_push = 1;
  341. settings->max_concurrent_streams = NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
  342. settings->initial_window_size = NGHTTP2_INITIAL_WINDOW_SIZE;
  343. settings->max_frame_size = NGHTTP2_MAX_FRAME_SIZE_MIN;
  344. settings->max_header_list_size = UINT32_MAX;
  345. settings->no_rfc7540_priorities = UINT32_MAX;
  346. }
  347. static void active_outbound_item_reset(nghttp2_active_outbound_item *aob,
  348. nghttp2_mem *mem) {
  349. DEBUGF("send: reset nghttp2_active_outbound_item\n");
  350. DEBUGF("send: aob->item = %p\n", aob->item);
  351. nghttp2_outbound_item_free(aob->item, mem);
  352. nghttp2_mem_free(mem, aob->item);
  353. aob->item = NULL;
  354. nghttp2_bufs_reset(&aob->framebufs);
  355. aob->state = NGHTTP2_OB_POP_ITEM;
  356. }
  357. #define NGHTTP2_STREAM_MAX_CYCLE_GAP ((uint64_t)NGHTTP2_MAX_FRAME_SIZE_MAX)
  358. static int stream_less(const void *lhsx, const void *rhsx) {
  359. const nghttp2_stream *lhs, *rhs;
  360. lhs = nghttp2_struct_of(lhsx, nghttp2_stream, pq_entry);
  361. rhs = nghttp2_struct_of(rhsx, nghttp2_stream, pq_entry);
  362. if (lhs->cycle == rhs->cycle) {
  363. return lhs->seq < rhs->seq;
  364. }
  365. return rhs->cycle - lhs->cycle <= NGHTTP2_STREAM_MAX_CYCLE_GAP;
  366. }
  367. int nghttp2_enable_strict_preface = 1;
  368. static int session_new(nghttp2_session **session_ptr,
  369. const nghttp2_session_callbacks *callbacks,
  370. void *user_data, int server,
  371. const nghttp2_option *option, nghttp2_mem *mem) {
  372. int rv;
  373. size_t nbuffer;
  374. size_t max_deflate_dynamic_table_size =
  375. NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE;
  376. size_t i;
  377. if (mem == NULL) {
  378. mem = nghttp2_mem_default();
  379. }
  380. *session_ptr = nghttp2_mem_calloc(mem, 1, sizeof(nghttp2_session));
  381. if (*session_ptr == NULL) {
  382. rv = NGHTTP2_ERR_NOMEM;
  383. goto fail_session;
  384. }
  385. (*session_ptr)->mem = *mem;
  386. mem = &(*session_ptr)->mem;
  387. /* next_stream_id is initialized in either
  388. nghttp2_session_client_new2 or nghttp2_session_server_new2 */
  389. nghttp2_stream_init(&(*session_ptr)->root, 0, NGHTTP2_STREAM_FLAG_NONE,
  390. NGHTTP2_STREAM_IDLE, NGHTTP2_DEFAULT_WEIGHT, 0, 0, NULL,
  391. mem);
  392. (*session_ptr)->remote_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
  393. (*session_ptr)->recv_window_size = 0;
  394. (*session_ptr)->consumed_size = 0;
  395. (*session_ptr)->recv_reduction = 0;
  396. (*session_ptr)->local_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
  397. (*session_ptr)->goaway_flags = NGHTTP2_GOAWAY_NONE;
  398. (*session_ptr)->local_last_stream_id = (1u << 31) - 1;
  399. (*session_ptr)->remote_last_stream_id = (1u << 31) - 1;
  400. (*session_ptr)->pending_local_max_concurrent_stream =
  401. NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
  402. (*session_ptr)->pending_enable_push = 1;
  403. (*session_ptr)->pending_no_rfc7540_priorities = UINT8_MAX;
  404. nghttp2_ratelim_init(&(*session_ptr)->stream_reset_ratelim,
  405. NGHTTP2_DEFAULT_STREAM_RESET_BURST,
  406. NGHTTP2_DEFAULT_STREAM_RESET_RATE);
  407. if (server) {
  408. (*session_ptr)->server = 1;
  409. }
  410. init_settings(&(*session_ptr)->remote_settings);
  411. init_settings(&(*session_ptr)->local_settings);
  412. (*session_ptr)->max_incoming_reserved_streams =
  413. NGHTTP2_MAX_INCOMING_RESERVED_STREAMS;
  414. /* Limit max outgoing concurrent streams to sensible value */
  415. (*session_ptr)->remote_settings.max_concurrent_streams = 100;
  416. (*session_ptr)->max_send_header_block_length = NGHTTP2_MAX_HEADERSLEN;
  417. (*session_ptr)->max_outbound_ack = NGHTTP2_DEFAULT_MAX_OBQ_FLOOD_ITEM;
  418. (*session_ptr)->max_settings = NGHTTP2_DEFAULT_MAX_SETTINGS;
  419. if (option) {
  420. if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE) &&
  421. option->no_auto_window_update) {
  422. (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE;
  423. }
  424. if (option->opt_set_mask & NGHTTP2_OPT_PEER_MAX_CONCURRENT_STREAMS) {
  425. (*session_ptr)->remote_settings.max_concurrent_streams =
  426. option->peer_max_concurrent_streams;
  427. }
  428. if (option->opt_set_mask & NGHTTP2_OPT_MAX_RESERVED_REMOTE_STREAMS) {
  429. (*session_ptr)->max_incoming_reserved_streams =
  430. option->max_reserved_remote_streams;
  431. }
  432. if ((option->opt_set_mask & NGHTTP2_OPT_NO_RECV_CLIENT_MAGIC) &&
  433. option->no_recv_client_magic) {
  434. (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC;
  435. }
  436. if ((option->opt_set_mask & NGHTTP2_OPT_NO_HTTP_MESSAGING) &&
  437. option->no_http_messaging) {
  438. (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_HTTP_MESSAGING;
  439. }
  440. if (option->opt_set_mask & NGHTTP2_OPT_USER_RECV_EXT_TYPES) {
  441. memcpy((*session_ptr)->user_recv_ext_types, option->user_recv_ext_types,
  442. sizeof((*session_ptr)->user_recv_ext_types));
  443. }
  444. if (option->opt_set_mask & NGHTTP2_OPT_BUILTIN_RECV_EXT_TYPES) {
  445. (*session_ptr)->builtin_recv_ext_types = option->builtin_recv_ext_types;
  446. }
  447. if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_PING_ACK) &&
  448. option->no_auto_ping_ack) {
  449. (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_PING_ACK;
  450. }
  451. if (option->opt_set_mask & NGHTTP2_OPT_MAX_SEND_HEADER_BLOCK_LENGTH) {
  452. (*session_ptr)->max_send_header_block_length =
  453. option->max_send_header_block_length;
  454. }
  455. if (option->opt_set_mask & NGHTTP2_OPT_MAX_DEFLATE_DYNAMIC_TABLE_SIZE) {
  456. max_deflate_dynamic_table_size = option->max_deflate_dynamic_table_size;
  457. }
  458. if ((option->opt_set_mask & NGHTTP2_OPT_NO_CLOSED_STREAMS) &&
  459. option->no_closed_streams) {
  460. (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_CLOSED_STREAMS;
  461. }
  462. if (option->opt_set_mask & NGHTTP2_OPT_MAX_OUTBOUND_ACK) {
  463. (*session_ptr)->max_outbound_ack = option->max_outbound_ack;
  464. }
  465. if ((option->opt_set_mask & NGHTTP2_OPT_MAX_SETTINGS) &&
  466. option->max_settings) {
  467. (*session_ptr)->max_settings = option->max_settings;
  468. }
  469. if ((option->opt_set_mask &
  470. NGHTTP2_OPT_SERVER_FALLBACK_RFC7540_PRIORITIES) &&
  471. option->server_fallback_rfc7540_priorities) {
  472. (*session_ptr)->opt_flags |=
  473. NGHTTP2_OPTMASK_SERVER_FALLBACK_RFC7540_PRIORITIES;
  474. }
  475. if ((option->opt_set_mask &
  476. NGHTTP2_OPT_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION) &&
  477. option->no_rfc9113_leading_and_trailing_ws_validation) {
  478. (*session_ptr)->opt_flags |=
  479. NGHTTP2_OPTMASK_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION;
  480. }
  481. if (option->opt_set_mask & NGHTTP2_OPT_STREAM_RESET_RATE_LIMIT) {
  482. nghttp2_ratelim_init(&(*session_ptr)->stream_reset_ratelim,
  483. option->stream_reset_burst,
  484. option->stream_reset_rate);
  485. }
  486. }
  487. rv = nghttp2_hd_deflate_init2(&(*session_ptr)->hd_deflater,
  488. max_deflate_dynamic_table_size, mem);
  489. if (rv != 0) {
  490. goto fail_hd_deflater;
  491. }
  492. rv = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, mem);
  493. if (rv != 0) {
  494. goto fail_hd_inflater;
  495. }
  496. nbuffer = ((*session_ptr)->max_send_header_block_length +
  497. NGHTTP2_FRAMEBUF_CHUNKLEN - 1) /
  498. NGHTTP2_FRAMEBUF_CHUNKLEN;
  499. if (nbuffer == 0) {
  500. nbuffer = 1;
  501. }
  502. /* 1 for Pad Field. */
  503. rv = nghttp2_bufs_init3(&(*session_ptr)->aob.framebufs,
  504. NGHTTP2_FRAMEBUF_CHUNKLEN, nbuffer, 1,
  505. NGHTTP2_FRAME_HDLEN + 1, mem);
  506. if (rv != 0) {
  507. goto fail_aob_framebuf;
  508. }
  509. nghttp2_map_init(&(*session_ptr)->streams, mem);
  510. active_outbound_item_reset(&(*session_ptr)->aob, mem);
  511. (*session_ptr)->callbacks = *callbacks;
  512. (*session_ptr)->user_data = user_data;
  513. session_inbound_frame_reset(*session_ptr);
  514. if (nghttp2_enable_strict_preface) {
  515. nghttp2_inbound_frame *iframe = &(*session_ptr)->iframe;
  516. if (server && ((*session_ptr)->opt_flags &
  517. NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC) == 0) {
  518. iframe->state = NGHTTP2_IB_READ_CLIENT_MAGIC;
  519. iframe->payloadleft = NGHTTP2_CLIENT_MAGIC_LEN;
  520. } else {
  521. iframe->state = NGHTTP2_IB_READ_FIRST_SETTINGS;
  522. }
  523. if (!server) {
  524. (*session_ptr)->aob.state = NGHTTP2_OB_SEND_CLIENT_MAGIC;
  525. nghttp2_bufs_add(&(*session_ptr)->aob.framebufs, NGHTTP2_CLIENT_MAGIC,
  526. NGHTTP2_CLIENT_MAGIC_LEN);
  527. }
  528. }
  529. for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
  530. nghttp2_pq_init(&(*session_ptr)->sched[i].ob_data, stream_less, mem);
  531. }
  532. return 0;
  533. fail_aob_framebuf:
  534. nghttp2_hd_inflate_free(&(*session_ptr)->hd_inflater);
  535. fail_hd_inflater:
  536. nghttp2_hd_deflate_free(&(*session_ptr)->hd_deflater);
  537. fail_hd_deflater:
  538. nghttp2_mem_free(mem, *session_ptr);
  539. fail_session:
  540. return rv;
  541. }
  542. int nghttp2_session_client_new(nghttp2_session **session_ptr,
  543. const nghttp2_session_callbacks *callbacks,
  544. void *user_data) {
  545. return nghttp2_session_client_new3(session_ptr, callbacks, user_data, NULL,
  546. NULL);
  547. }
  548. int nghttp2_session_client_new2(nghttp2_session **session_ptr,
  549. const nghttp2_session_callbacks *callbacks,
  550. void *user_data, const nghttp2_option *option) {
  551. return nghttp2_session_client_new3(session_ptr, callbacks, user_data, option,
  552. NULL);
  553. }
  554. int nghttp2_session_client_new3(nghttp2_session **session_ptr,
  555. const nghttp2_session_callbacks *callbacks,
  556. void *user_data, const nghttp2_option *option,
  557. nghttp2_mem *mem) {
  558. int rv;
  559. nghttp2_session *session;
  560. rv = session_new(&session, callbacks, user_data, 0, option, mem);
  561. if (rv != 0) {
  562. return rv;
  563. }
  564. /* IDs for use in client */
  565. session->next_stream_id = 1;
  566. *session_ptr = session;
  567. return 0;
  568. }
  569. int nghttp2_session_server_new(nghttp2_session **session_ptr,
  570. const nghttp2_session_callbacks *callbacks,
  571. void *user_data) {
  572. return nghttp2_session_server_new3(session_ptr, callbacks, user_data, NULL,
  573. NULL);
  574. }
  575. int nghttp2_session_server_new2(nghttp2_session **session_ptr,
  576. const nghttp2_session_callbacks *callbacks,
  577. void *user_data, const nghttp2_option *option) {
  578. return nghttp2_session_server_new3(session_ptr, callbacks, user_data, option,
  579. NULL);
  580. }
  581. int nghttp2_session_server_new3(nghttp2_session **session_ptr,
  582. const nghttp2_session_callbacks *callbacks,
  583. void *user_data, const nghttp2_option *option,
  584. nghttp2_mem *mem) {
  585. int rv;
  586. nghttp2_session *session;
  587. rv = session_new(&session, callbacks, user_data, 1, option, mem);
  588. if (rv != 0) {
  589. return rv;
  590. }
  591. /* IDs for use in client */
  592. session->next_stream_id = 2;
  593. *session_ptr = session;
  594. return 0;
  595. }
  596. static int free_streams(void *entry, void *ptr) {
  597. nghttp2_session *session;
  598. nghttp2_stream *stream;
  599. nghttp2_outbound_item *item;
  600. nghttp2_mem *mem;
  601. session = (nghttp2_session *)ptr;
  602. mem = &session->mem;
  603. stream = (nghttp2_stream *)entry;
  604. item = stream->item;
  605. if (item && !item->queued && item != session->aob.item) {
  606. nghttp2_outbound_item_free(item, mem);
  607. nghttp2_mem_free(mem, item);
  608. }
  609. nghttp2_stream_free(stream);
  610. nghttp2_mem_free(mem, stream);
  611. return 0;
  612. }
  613. static void ob_q_free(nghttp2_outbound_queue *q, nghttp2_mem *mem) {
  614. nghttp2_outbound_item *item, *next;
  615. for (item = q->head; item;) {
  616. next = item->qnext;
  617. nghttp2_outbound_item_free(item, mem);
  618. nghttp2_mem_free(mem, item);
  619. item = next;
  620. }
  621. }
  622. static int inflight_settings_new(nghttp2_inflight_settings **settings_ptr,
  623. const nghttp2_settings_entry *iv, size_t niv,
  624. nghttp2_mem *mem) {
  625. *settings_ptr = nghttp2_mem_malloc(mem, sizeof(nghttp2_inflight_settings));
  626. if (!*settings_ptr) {
  627. return NGHTTP2_ERR_NOMEM;
  628. }
  629. if (niv > 0) {
  630. (*settings_ptr)->iv = nghttp2_frame_iv_copy(iv, niv, mem);
  631. if (!(*settings_ptr)->iv) {
  632. nghttp2_mem_free(mem, *settings_ptr);
  633. return NGHTTP2_ERR_NOMEM;
  634. }
  635. } else {
  636. (*settings_ptr)->iv = NULL;
  637. }
  638. (*settings_ptr)->niv = niv;
  639. (*settings_ptr)->next = NULL;
  640. return 0;
  641. }
  642. static void inflight_settings_del(nghttp2_inflight_settings *settings,
  643. nghttp2_mem *mem) {
  644. if (!settings) {
  645. return;
  646. }
  647. nghttp2_mem_free(mem, settings->iv);
  648. nghttp2_mem_free(mem, settings);
  649. }
  650. void nghttp2_session_del(nghttp2_session *session) {
  651. nghttp2_mem *mem;
  652. nghttp2_inflight_settings *settings;
  653. size_t i;
  654. if (session == NULL) {
  655. return;
  656. }
  657. mem = &session->mem;
  658. for (settings = session->inflight_settings_head; settings;) {
  659. nghttp2_inflight_settings *next = settings->next;
  660. inflight_settings_del(settings, mem);
  661. settings = next;
  662. }
  663. for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
  664. nghttp2_pq_free(&session->sched[i].ob_data);
  665. }
  666. nghttp2_stream_free(&session->root);
  667. /* Have to free streams first, so that we can check
  668. stream->item->queued */
  669. nghttp2_map_each_free(&session->streams, free_streams, session);
  670. nghttp2_map_free(&session->streams);
  671. ob_q_free(&session->ob_urgent, mem);
  672. ob_q_free(&session->ob_reg, mem);
  673. ob_q_free(&session->ob_syn, mem);
  674. active_outbound_item_reset(&session->aob, mem);
  675. session_inbound_frame_reset(session);
  676. nghttp2_hd_deflate_free(&session->hd_deflater);
  677. nghttp2_hd_inflate_free(&session->hd_inflater);
  678. nghttp2_bufs_free(&session->aob.framebufs);
  679. nghttp2_mem_free(mem, session);
  680. }
  681. int nghttp2_session_reprioritize_stream(
  682. nghttp2_session *session, nghttp2_stream *stream,
  683. const nghttp2_priority_spec *pri_spec_in) {
  684. int rv;
  685. nghttp2_stream *dep_stream = NULL;
  686. nghttp2_priority_spec pri_spec_default;
  687. const nghttp2_priority_spec *pri_spec = pri_spec_in;
  688. assert((!session->server && session->pending_no_rfc7540_priorities != 1) ||
  689. (session->server && !session_no_rfc7540_pri_no_fallback(session)));
  690. assert(pri_spec->stream_id != stream->stream_id);
  691. if (!nghttp2_stream_in_dep_tree(stream)) {
  692. return 0;
  693. }
  694. if (pri_spec->stream_id != 0) {
  695. dep_stream = nghttp2_session_get_stream_raw(session, pri_spec->stream_id);
  696. if (!dep_stream &&
  697. session_detect_idle_stream(session, pri_spec->stream_id)) {
  698. nghttp2_priority_spec_default_init(&pri_spec_default);
  699. dep_stream = nghttp2_session_open_stream(
  700. session, pri_spec->stream_id, NGHTTP2_FLAG_NONE, &pri_spec_default,
  701. NGHTTP2_STREAM_IDLE, NULL);
  702. if (dep_stream == NULL) {
  703. return NGHTTP2_ERR_NOMEM;
  704. }
  705. } else if (!dep_stream || !nghttp2_stream_in_dep_tree(dep_stream)) {
  706. nghttp2_priority_spec_default_init(&pri_spec_default);
  707. pri_spec = &pri_spec_default;
  708. }
  709. }
  710. if (pri_spec->stream_id == 0) {
  711. dep_stream = &session->root;
  712. } else if (nghttp2_stream_dep_find_ancestor(dep_stream, stream)) {
  713. DEBUGF("stream: cycle detected, dep_stream(%p)=%d stream(%p)=%d\n",
  714. dep_stream, dep_stream->stream_id, stream, stream->stream_id);
  715. nghttp2_stream_dep_remove_subtree(dep_stream);
  716. rv = nghttp2_stream_dep_add_subtree(stream->dep_prev, dep_stream);
  717. if (rv != 0) {
  718. return rv;
  719. }
  720. }
  721. assert(dep_stream);
  722. if (dep_stream == stream->dep_prev && !pri_spec->exclusive) {
  723. /* This is minor optimization when just weight is changed. */
  724. nghttp2_stream_change_weight(stream, pri_spec->weight);
  725. return 0;
  726. }
  727. nghttp2_stream_dep_remove_subtree(stream);
  728. /* We have to update weight after removing stream from tree */
  729. stream->weight = pri_spec->weight;
  730. if (pri_spec->exclusive) {
  731. rv = nghttp2_stream_dep_insert_subtree(dep_stream, stream);
  732. } else {
  733. rv = nghttp2_stream_dep_add_subtree(dep_stream, stream);
  734. }
  735. if (rv != 0) {
  736. return rv;
  737. }
  738. return 0;
  739. }
  740. static uint64_t pq_get_first_cycle(nghttp2_pq *pq) {
  741. nghttp2_stream *stream;
  742. if (nghttp2_pq_empty(pq)) {
  743. return 0;
  744. }
  745. stream = nghttp2_struct_of(nghttp2_pq_top(pq), nghttp2_stream, pq_entry);
  746. return stream->cycle;
  747. }
  748. static int session_ob_data_push(nghttp2_session *session,
  749. nghttp2_stream *stream) {
  750. int rv;
  751. uint32_t urgency;
  752. int inc;
  753. nghttp2_pq *pq;
  754. assert(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES);
  755. assert(stream->queued == 0);
  756. urgency = nghttp2_extpri_uint8_urgency(stream->extpri);
  757. inc = nghttp2_extpri_uint8_inc(stream->extpri);
  758. assert(urgency < NGHTTP2_EXTPRI_URGENCY_LEVELS);
  759. pq = &session->sched[urgency].ob_data;
  760. stream->cycle = pq_get_first_cycle(pq);
  761. if (inc) {
  762. stream->cycle += stream->last_writelen;
  763. }
  764. rv = nghttp2_pq_push(pq, &stream->pq_entry);
  765. if (rv != 0) {
  766. return rv;
  767. }
  768. stream->queued = 1;
  769. return 0;
  770. }
  771. static void session_ob_data_remove(nghttp2_session *session,
  772. nghttp2_stream *stream) {
  773. uint32_t urgency;
  774. assert(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES);
  775. assert(stream->queued == 1);
  776. urgency = nghttp2_extpri_uint8_urgency(stream->extpri);
  777. assert(urgency < NGHTTP2_EXTPRI_URGENCY_LEVELS);
  778. nghttp2_pq_remove(&session->sched[urgency].ob_data, &stream->pq_entry);
  779. stream->queued = 0;
  780. }
  781. static int session_attach_stream_item(nghttp2_session *session,
  782. nghttp2_stream *stream,
  783. nghttp2_outbound_item *item) {
  784. int rv;
  785. rv = nghttp2_stream_attach_item(stream, item);
  786. if (rv != 0) {
  787. return rv;
  788. }
  789. if (!(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES)) {
  790. return 0;
  791. }
  792. return session_ob_data_push(session, stream);
  793. }
  794. static void session_detach_stream_item(nghttp2_session *session,
  795. nghttp2_stream *stream) {
  796. nghttp2_stream_detach_item(stream);
  797. if (!(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) ||
  798. !stream->queued) {
  799. return;
  800. }
  801. session_ob_data_remove(session, stream);
  802. }
  803. static void session_defer_stream_item(nghttp2_session *session,
  804. nghttp2_stream *stream, uint8_t flags) {
  805. nghttp2_stream_defer_item(stream, flags);
  806. if (!(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) ||
  807. !stream->queued) {
  808. return;
  809. }
  810. session_ob_data_remove(session, stream);
  811. }
  812. static int session_resume_deferred_stream_item(nghttp2_session *session,
  813. nghttp2_stream *stream,
  814. uint8_t flags) {
  815. int rv;
  816. rv = nghttp2_stream_resume_deferred_item(stream, flags);
  817. if (rv != 0) {
  818. return rv;
  819. }
  820. if (!(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) ||
  821. (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL)) {
  822. return 0;
  823. }
  824. return session_ob_data_push(session, stream);
  825. }
  826. static nghttp2_outbound_item *
  827. session_sched_get_next_outbound_item(nghttp2_session *session) {
  828. size_t i;
  829. nghttp2_pq_entry *ent;
  830. nghttp2_stream *stream;
  831. for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
  832. ent = nghttp2_pq_top(&session->sched[i].ob_data);
  833. if (!ent) {
  834. continue;
  835. }
  836. stream = nghttp2_struct_of(ent, nghttp2_stream, pq_entry);
  837. return stream->item;
  838. }
  839. return NULL;
  840. }
  841. static int session_sched_empty(nghttp2_session *session) {
  842. size_t i;
  843. for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
  844. if (!nghttp2_pq_empty(&session->sched[i].ob_data)) {
  845. return 0;
  846. }
  847. }
  848. return 1;
  849. }
  850. static void session_sched_reschedule_stream(nghttp2_session *session,
  851. nghttp2_stream *stream) {
  852. nghttp2_pq *pq;
  853. uint32_t urgency = nghttp2_extpri_uint8_urgency(stream->extpri);
  854. int inc = nghttp2_extpri_uint8_inc(stream->extpri);
  855. uint64_t penalty = (uint64_t)stream->last_writelen;
  856. int rv;
  857. (void)rv;
  858. assert(urgency < NGHTTP2_EXTPRI_URGENCY_LEVELS);
  859. pq = &session->sched[urgency].ob_data;
  860. if (!inc || nghttp2_pq_size(pq) == 1) {
  861. return;
  862. }
  863. nghttp2_pq_remove(pq, &stream->pq_entry);
  864. stream->cycle += penalty;
  865. rv = nghttp2_pq_push(pq, &stream->pq_entry);
  866. assert(0 == rv);
  867. }
  868. static int session_update_stream_priority(nghttp2_session *session,
  869. nghttp2_stream *stream,
  870. uint8_t u8extpri) {
  871. if (stream->extpri == u8extpri) {
  872. return 0;
  873. }
  874. if (stream->queued) {
  875. session_ob_data_remove(session, stream);
  876. stream->extpri = u8extpri;
  877. return session_ob_data_push(session, stream);
  878. }
  879. stream->extpri = u8extpri;
  880. return 0;
  881. }
  882. int nghttp2_session_add_item(nghttp2_session *session,
  883. nghttp2_outbound_item *item) {
  884. /* TODO Return error if stream is not found for the frame requiring
  885. stream presence. */
  886. int rv = 0;
  887. nghttp2_stream *stream;
  888. nghttp2_frame *frame;
  889. frame = &item->frame;
  890. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  891. switch (frame->hd.type) {
  892. case NGHTTP2_DATA:
  893. if (!stream) {
  894. return NGHTTP2_ERR_STREAM_CLOSED;
  895. }
  896. if (stream->item) {
  897. return NGHTTP2_ERR_DATA_EXIST;
  898. }
  899. rv = session_attach_stream_item(session, stream, item);
  900. if (rv != 0) {
  901. return rv;
  902. }
  903. return 0;
  904. case NGHTTP2_HEADERS:
  905. /* We push request HEADERS and push response HEADERS to
  906. dedicated queue because their transmission is affected by
  907. SETTINGS_MAX_CONCURRENT_STREAMS */
  908. /* TODO If 2 HEADERS are submitted for reserved stream, then
  909. both of them are queued into ob_syn, which is not
  910. desirable. */
  911. if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
  912. (stream && stream->state == NGHTTP2_STREAM_RESERVED)) {
  913. nghttp2_outbound_queue_push(&session->ob_syn, item);
  914. item->queued = 1;
  915. return 0;
  916. ;
  917. }
  918. nghttp2_outbound_queue_push(&session->ob_reg, item);
  919. item->queued = 1;
  920. return 0;
  921. case NGHTTP2_SETTINGS:
  922. case NGHTTP2_PING:
  923. nghttp2_outbound_queue_push(&session->ob_urgent, item);
  924. item->queued = 1;
  925. return 0;
  926. case NGHTTP2_RST_STREAM:
  927. if (stream) {
  928. stream->state = NGHTTP2_STREAM_CLOSING;
  929. }
  930. nghttp2_outbound_queue_push(&session->ob_reg, item);
  931. item->queued = 1;
  932. return 0;
  933. case NGHTTP2_PUSH_PROMISE: {
  934. nghttp2_headers_aux_data *aux_data;
  935. nghttp2_priority_spec pri_spec;
  936. aux_data = &item->aux_data.headers;
  937. if (!stream) {
  938. return NGHTTP2_ERR_STREAM_CLOSED;
  939. }
  940. nghttp2_priority_spec_init(&pri_spec, stream->stream_id,
  941. NGHTTP2_DEFAULT_WEIGHT, 0);
  942. if (!nghttp2_session_open_stream(
  943. session, frame->push_promise.promised_stream_id,
  944. NGHTTP2_STREAM_FLAG_NONE, &pri_spec, NGHTTP2_STREAM_RESERVED,
  945. aux_data->stream_user_data)) {
  946. return NGHTTP2_ERR_NOMEM;
  947. }
  948. /* We don't have to call nghttp2_session_adjust_closed_stream()
  949. here, since stream->stream_id is local stream_id, and it does
  950. not affect closed stream count. */
  951. nghttp2_outbound_queue_push(&session->ob_reg, item);
  952. item->queued = 1;
  953. return 0;
  954. }
  955. case NGHTTP2_WINDOW_UPDATE:
  956. if (stream) {
  957. stream->window_update_queued = 1;
  958. } else if (frame->hd.stream_id == 0) {
  959. session->window_update_queued = 1;
  960. }
  961. nghttp2_outbound_queue_push(&session->ob_reg, item);
  962. item->queued = 1;
  963. return 0;
  964. default:
  965. nghttp2_outbound_queue_push(&session->ob_reg, item);
  966. item->queued = 1;
  967. return 0;
  968. }
  969. }
  970. int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id,
  971. uint32_t error_code) {
  972. int rv;
  973. nghttp2_outbound_item *item;
  974. nghttp2_frame *frame;
  975. nghttp2_stream *stream;
  976. nghttp2_mem *mem;
  977. mem = &session->mem;
  978. stream = nghttp2_session_get_stream(session, stream_id);
  979. if (stream && stream->state == NGHTTP2_STREAM_CLOSING) {
  980. return 0;
  981. }
  982. /* Sending RST_STREAM to an idle stream is subject to protocol
  983. violation. Historically, nghttp2 allows this. In order not to
  984. disrupt the existing applications, we don't error out this case
  985. and simply ignore it. */
  986. if (nghttp2_session_is_my_stream_id(session, stream_id)) {
  987. if ((uint32_t)stream_id >= session->next_stream_id) {
  988. return 0;
  989. }
  990. } else if (session->last_recv_stream_id < stream_id) {
  991. return 0;
  992. }
  993. /* Cancel pending request HEADERS in ob_syn if this RST_STREAM
  994. refers to that stream. */
  995. if (!session->server && nghttp2_session_is_my_stream_id(session, stream_id) &&
  996. nghttp2_outbound_queue_top(&session->ob_syn)) {
  997. nghttp2_headers_aux_data *aux_data;
  998. nghttp2_frame *headers_frame;
  999. headers_frame = &nghttp2_outbound_queue_top(&session->ob_syn)->frame;
  1000. assert(headers_frame->hd.type == NGHTTP2_HEADERS);
  1001. if (headers_frame->hd.stream_id <= stream_id) {
  1002. for (item = session->ob_syn.head; item; item = item->qnext) {
  1003. aux_data = &item->aux_data.headers;
  1004. if (item->frame.hd.stream_id < stream_id) {
  1005. continue;
  1006. }
  1007. /* stream_id in ob_syn queue must be strictly increasing. If
  1008. we found larger ID, then we can break here. */
  1009. if (item->frame.hd.stream_id > stream_id || aux_data->canceled) {
  1010. break;
  1011. }
  1012. aux_data->error_code = error_code;
  1013. aux_data->canceled = 1;
  1014. return 0;
  1015. }
  1016. }
  1017. }
  1018. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  1019. if (item == NULL) {
  1020. return NGHTTP2_ERR_NOMEM;
  1021. }
  1022. nghttp2_outbound_item_init(item);
  1023. frame = &item->frame;
  1024. nghttp2_frame_rst_stream_init(&frame->rst_stream, stream_id, error_code);
  1025. rv = nghttp2_session_add_item(session, item);
  1026. if (rv != 0) {
  1027. nghttp2_frame_rst_stream_free(&frame->rst_stream);
  1028. nghttp2_mem_free(mem, item);
  1029. return rv;
  1030. }
  1031. return 0;
  1032. }
  1033. nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
  1034. int32_t stream_id, uint8_t flags,
  1035. nghttp2_priority_spec *pri_spec_in,
  1036. nghttp2_stream_state initial_state,
  1037. void *stream_user_data) {
  1038. int rv;
  1039. nghttp2_stream *stream;
  1040. nghttp2_stream *dep_stream = NULL;
  1041. int stream_alloc = 0;
  1042. nghttp2_priority_spec pri_spec_default;
  1043. nghttp2_priority_spec *pri_spec = pri_spec_in;
  1044. nghttp2_mem *mem;
  1045. mem = &session->mem;
  1046. stream = nghttp2_session_get_stream_raw(session, stream_id);
  1047. if (session->opt_flags &
  1048. NGHTTP2_OPTMASK_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION) {
  1049. flags |= NGHTTP2_STREAM_FLAG_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION;
  1050. }
  1051. if (stream) {
  1052. assert(stream->state == NGHTTP2_STREAM_IDLE);
  1053. assert((stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) ||
  1054. nghttp2_stream_in_dep_tree(stream));
  1055. if (nghttp2_stream_in_dep_tree(stream)) {
  1056. assert(!(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES));
  1057. nghttp2_session_detach_idle_stream(session, stream);
  1058. rv = nghttp2_stream_dep_remove(stream);
  1059. if (rv != 0) {
  1060. return NULL;
  1061. }
  1062. if (session_no_rfc7540_pri_no_fallback(session)) {
  1063. stream->flags |= NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES;
  1064. }
  1065. }
  1066. } else {
  1067. stream = nghttp2_mem_malloc(mem, sizeof(nghttp2_stream));
  1068. if (stream == NULL) {
  1069. return NULL;
  1070. }
  1071. stream_alloc = 1;
  1072. }
  1073. if (session_no_rfc7540_pri_no_fallback(session) ||
  1074. session->remote_settings.no_rfc7540_priorities == 1) {
  1075. /* For client which has not received server
  1076. SETTINGS_NO_RFC7540_PRIORITIES = 1, send a priority signal
  1077. opportunistically. */
  1078. if (session->server ||
  1079. session->remote_settings.no_rfc7540_priorities == 1) {
  1080. nghttp2_priority_spec_default_init(&pri_spec_default);
  1081. pri_spec = &pri_spec_default;
  1082. }
  1083. if (session->pending_no_rfc7540_priorities == 1) {
  1084. flags |= NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES;
  1085. }
  1086. } else if (pri_spec->stream_id != 0) {
  1087. dep_stream = nghttp2_session_get_stream_raw(session, pri_spec->stream_id);
  1088. if (!dep_stream &&
  1089. session_detect_idle_stream(session, pri_spec->stream_id)) {
  1090. /* Depends on idle stream, which does not exist in memory.
  1091. Assign default priority for it. */
  1092. nghttp2_priority_spec_default_init(&pri_spec_default);
  1093. dep_stream = nghttp2_session_open_stream(
  1094. session, pri_spec->stream_id, NGHTTP2_FLAG_NONE, &pri_spec_default,
  1095. NGHTTP2_STREAM_IDLE, NULL);
  1096. if (dep_stream == NULL) {
  1097. if (stream_alloc) {
  1098. nghttp2_mem_free(mem, stream);
  1099. }
  1100. return NULL;
  1101. }
  1102. } else if (!dep_stream || !nghttp2_stream_in_dep_tree(dep_stream)) {
  1103. /* If dep_stream is not part of dependency tree, stream will get
  1104. default priority. This handles the case when
  1105. pri_spec->stream_id == stream_id. This happens because we
  1106. don't check pri_spec->stream_id against new stream ID in
  1107. nghttp2_submit_request. This also handles the case when idle
  1108. stream created by PRIORITY frame was opened. Somehow we
  1109. first remove the idle stream from dependency tree. This is
  1110. done to simplify code base, but ideally we should retain old
  1111. dependency. But I'm not sure this adds values. */
  1112. nghttp2_priority_spec_default_init(&pri_spec_default);
  1113. pri_spec = &pri_spec_default;
  1114. }
  1115. }
  1116. if (initial_state == NGHTTP2_STREAM_RESERVED) {
  1117. flags |= NGHTTP2_STREAM_FLAG_PUSH;
  1118. }
  1119. if (stream_alloc) {
  1120. nghttp2_stream_init(stream, stream_id, flags, initial_state,
  1121. pri_spec->weight,
  1122. (int32_t)session->remote_settings.initial_window_size,
  1123. (int32_t)session->local_settings.initial_window_size,
  1124. stream_user_data, mem);
  1125. if (session_no_rfc7540_pri_no_fallback(session)) {
  1126. stream->seq = session->stream_seq++;
  1127. }
  1128. rv = nghttp2_map_insert(&session->streams, stream_id, stream);
  1129. if (rv != 0) {
  1130. nghttp2_stream_free(stream);
  1131. nghttp2_mem_free(mem, stream);
  1132. return NULL;
  1133. }
  1134. } else {
  1135. stream->flags = flags;
  1136. stream->state = initial_state;
  1137. stream->weight = pri_spec->weight;
  1138. stream->stream_user_data = stream_user_data;
  1139. }
  1140. switch (initial_state) {
  1141. case NGHTTP2_STREAM_RESERVED:
  1142. if (nghttp2_session_is_my_stream_id(session, stream_id)) {
  1143. /* reserved (local) */
  1144. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
  1145. } else {
  1146. /* reserved (remote) */
  1147. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
  1148. ++session->num_incoming_reserved_streams;
  1149. }
  1150. /* Reserved stream does not count in the concurrent streams
  1151. limit. That is one of the DOS vector. */
  1152. break;
  1153. case NGHTTP2_STREAM_IDLE:
  1154. /* Idle stream does not count toward the concurrent streams limit.
  1155. This is used as anchor node in dependency tree. */
  1156. nghttp2_session_keep_idle_stream(session, stream);
  1157. break;
  1158. default:
  1159. if (nghttp2_session_is_my_stream_id(session, stream_id)) {
  1160. ++session->num_outgoing_streams;
  1161. } else {
  1162. ++session->num_incoming_streams;
  1163. }
  1164. }
  1165. if (stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) {
  1166. return stream;
  1167. }
  1168. if (pri_spec->stream_id == 0) {
  1169. dep_stream = &session->root;
  1170. }
  1171. assert(dep_stream);
  1172. if (pri_spec->exclusive) {
  1173. rv = nghttp2_stream_dep_insert(dep_stream, stream);
  1174. if (rv != 0) {
  1175. return NULL;
  1176. }
  1177. } else {
  1178. nghttp2_stream_dep_add(dep_stream, stream);
  1179. }
  1180. return stream;
  1181. }
  1182. int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id,
  1183. uint32_t error_code) {
  1184. int rv;
  1185. nghttp2_stream *stream;
  1186. nghttp2_mem *mem;
  1187. int is_my_stream_id;
  1188. mem = &session->mem;
  1189. stream = nghttp2_session_get_stream(session, stream_id);
  1190. if (!stream) {
  1191. return NGHTTP2_ERR_INVALID_ARGUMENT;
  1192. }
  1193. DEBUGF("stream: stream(%p)=%d close\n", stream, stream->stream_id);
  1194. if (stream->item) {
  1195. nghttp2_outbound_item *item;
  1196. item = stream->item;
  1197. session_detach_stream_item(session, stream);
  1198. /* If item is queued, it will be deleted when it is popped
  1199. (nghttp2_session_prep_frame() will fail). If session->aob.item
  1200. points to this item, let active_outbound_item_reset()
  1201. free the item. */
  1202. if (!item->queued && item != session->aob.item) {
  1203. nghttp2_outbound_item_free(item, mem);
  1204. nghttp2_mem_free(mem, item);
  1205. }
  1206. }
  1207. /* We call on_stream_close_callback even if stream->state is
  1208. NGHTTP2_STREAM_INITIAL. This will happen while sending request
  1209. HEADERS, a local endpoint receives RST_STREAM for that stream. It
  1210. may be PROTOCOL_ERROR, but without notifying stream closure will
  1211. hang the stream in a local endpoint.
  1212. */
  1213. if (session->callbacks.on_stream_close_callback) {
  1214. if (session->callbacks.on_stream_close_callback(
  1215. session, stream_id, error_code, session->user_data) != 0) {
  1216. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1217. }
  1218. }
  1219. is_my_stream_id = nghttp2_session_is_my_stream_id(session, stream_id);
  1220. /* pushed streams which is not opened yet is not counted toward max
  1221. concurrent limits */
  1222. if ((stream->flags & NGHTTP2_STREAM_FLAG_PUSH)) {
  1223. if (!is_my_stream_id) {
  1224. --session->num_incoming_reserved_streams;
  1225. }
  1226. } else {
  1227. if (is_my_stream_id) {
  1228. --session->num_outgoing_streams;
  1229. } else {
  1230. --session->num_incoming_streams;
  1231. }
  1232. }
  1233. /* Closes both directions just in case they are not closed yet */
  1234. stream->flags |= NGHTTP2_STREAM_FLAG_CLOSED;
  1235. if (session->pending_no_rfc7540_priorities == 1) {
  1236. return nghttp2_session_destroy_stream(session, stream);
  1237. }
  1238. if ((session->opt_flags & NGHTTP2_OPTMASK_NO_CLOSED_STREAMS) == 0 &&
  1239. session->server && !is_my_stream_id &&
  1240. nghttp2_stream_in_dep_tree(stream)) {
  1241. /* On server side, retain stream at most MAX_CONCURRENT_STREAMS
  1242. combined with the current active incoming streams to make
  1243. dependency tree work better. */
  1244. nghttp2_session_keep_closed_stream(session, stream);
  1245. } else {
  1246. rv = nghttp2_session_destroy_stream(session, stream);
  1247. if (rv != 0) {
  1248. return rv;
  1249. }
  1250. }
  1251. return 0;
  1252. }
  1253. int nghttp2_session_destroy_stream(nghttp2_session *session,
  1254. nghttp2_stream *stream) {
  1255. nghttp2_mem *mem;
  1256. int rv;
  1257. DEBUGF("stream: destroy closed stream(%p)=%d\n", stream, stream->stream_id);
  1258. mem = &session->mem;
  1259. if (nghttp2_stream_in_dep_tree(stream)) {
  1260. rv = nghttp2_stream_dep_remove(stream);
  1261. if (rv != 0) {
  1262. return rv;
  1263. }
  1264. }
  1265. nghttp2_map_remove(&session->streams, stream->stream_id);
  1266. nghttp2_stream_free(stream);
  1267. nghttp2_mem_free(mem, stream);
  1268. return 0;
  1269. }
  1270. void nghttp2_session_keep_closed_stream(nghttp2_session *session,
  1271. nghttp2_stream *stream) {
  1272. DEBUGF("stream: keep closed stream(%p)=%d, state=%d\n", stream,
  1273. stream->stream_id, stream->state);
  1274. if (session->closed_stream_tail) {
  1275. session->closed_stream_tail->closed_next = stream;
  1276. stream->closed_prev = session->closed_stream_tail;
  1277. } else {
  1278. session->closed_stream_head = stream;
  1279. }
  1280. session->closed_stream_tail = stream;
  1281. ++session->num_closed_streams;
  1282. }
  1283. void nghttp2_session_keep_idle_stream(nghttp2_session *session,
  1284. nghttp2_stream *stream) {
  1285. DEBUGF("stream: keep idle stream(%p)=%d, state=%d\n", stream,
  1286. stream->stream_id, stream->state);
  1287. if (session->idle_stream_tail) {
  1288. session->idle_stream_tail->closed_next = stream;
  1289. stream->closed_prev = session->idle_stream_tail;
  1290. } else {
  1291. session->idle_stream_head = stream;
  1292. }
  1293. session->idle_stream_tail = stream;
  1294. ++session->num_idle_streams;
  1295. }
  1296. void nghttp2_session_detach_idle_stream(nghttp2_session *session,
  1297. nghttp2_stream *stream) {
  1298. nghttp2_stream *prev_stream, *next_stream;
  1299. DEBUGF("stream: detach idle stream(%p)=%d, state=%d\n", stream,
  1300. stream->stream_id, stream->state);
  1301. prev_stream = stream->closed_prev;
  1302. next_stream = stream->closed_next;
  1303. if (prev_stream) {
  1304. prev_stream->closed_next = next_stream;
  1305. } else {
  1306. session->idle_stream_head = next_stream;
  1307. }
  1308. if (next_stream) {
  1309. next_stream->closed_prev = prev_stream;
  1310. } else {
  1311. session->idle_stream_tail = prev_stream;
  1312. }
  1313. stream->closed_prev = NULL;
  1314. stream->closed_next = NULL;
  1315. --session->num_idle_streams;
  1316. }
  1317. int nghttp2_session_adjust_closed_stream(nghttp2_session *session) {
  1318. size_t num_stream_max;
  1319. int rv;
  1320. if (session->local_settings.max_concurrent_streams ==
  1321. NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS) {
  1322. num_stream_max = session->pending_local_max_concurrent_stream;
  1323. } else {
  1324. num_stream_max = session->local_settings.max_concurrent_streams;
  1325. }
  1326. DEBUGF("stream: adjusting kept closed streams num_closed_streams=%zu, "
  1327. "num_incoming_streams=%zu, max_concurrent_streams=%zu\n",
  1328. session->num_closed_streams, session->num_incoming_streams,
  1329. num_stream_max);
  1330. while (session->num_closed_streams > 0 &&
  1331. session->num_closed_streams + session->num_incoming_streams >
  1332. num_stream_max) {
  1333. nghttp2_stream *head_stream;
  1334. nghttp2_stream *next;
  1335. head_stream = session->closed_stream_head;
  1336. assert(head_stream);
  1337. next = head_stream->closed_next;
  1338. rv = nghttp2_session_destroy_stream(session, head_stream);
  1339. if (rv != 0) {
  1340. return rv;
  1341. }
  1342. /* head_stream is now freed */
  1343. session->closed_stream_head = next;
  1344. if (session->closed_stream_head) {
  1345. session->closed_stream_head->closed_prev = NULL;
  1346. } else {
  1347. session->closed_stream_tail = NULL;
  1348. }
  1349. --session->num_closed_streams;
  1350. }
  1351. return 0;
  1352. }
  1353. int nghttp2_session_adjust_idle_stream(nghttp2_session *session) {
  1354. size_t max;
  1355. int rv;
  1356. /* Make minimum number of idle streams 16, and maximum 100, which
  1357. are arbitrary chosen numbers. */
  1358. max = nghttp2_min(
  1359. 100, nghttp2_max(
  1360. 16, nghttp2_min(session->local_settings.max_concurrent_streams,
  1361. session->pending_local_max_concurrent_stream)));
  1362. DEBUGF("stream: adjusting kept idle streams num_idle_streams=%zu, max=%zu\n",
  1363. session->num_idle_streams, max);
  1364. while (session->num_idle_streams > max) {
  1365. nghttp2_stream *head;
  1366. nghttp2_stream *next;
  1367. head = session->idle_stream_head;
  1368. assert(head);
  1369. next = head->closed_next;
  1370. rv = nghttp2_session_destroy_stream(session, head);
  1371. if (rv != 0) {
  1372. return rv;
  1373. }
  1374. /* head is now destroyed */
  1375. session->idle_stream_head = next;
  1376. if (session->idle_stream_head) {
  1377. session->idle_stream_head->closed_prev = NULL;
  1378. } else {
  1379. session->idle_stream_tail = NULL;
  1380. }
  1381. --session->num_idle_streams;
  1382. }
  1383. return 0;
  1384. }
  1385. /*
  1386. * Closes stream with stream ID |stream_id| if both transmission and
  1387. * reception of the stream were disallowed. The |error_code| indicates
  1388. * the reason of the closure.
  1389. *
  1390. * This function returns 0 if it succeeds, or one of the following
  1391. * negative error codes:
  1392. *
  1393. * NGHTTP2_ERR_INVALID_ARGUMENT
  1394. * The stream is not found.
  1395. * NGHTTP2_ERR_CALLBACK_FAILURE
  1396. * The callback function failed.
  1397. */
  1398. int nghttp2_session_close_stream_if_shut_rdwr(nghttp2_session *session,
  1399. nghttp2_stream *stream) {
  1400. if ((stream->shut_flags & NGHTTP2_SHUT_RDWR) == NGHTTP2_SHUT_RDWR) {
  1401. return nghttp2_session_close_stream(session, stream->stream_id,
  1402. NGHTTP2_NO_ERROR);
  1403. }
  1404. return 0;
  1405. }
  1406. /*
  1407. * Returns nonzero if local endpoint allows reception of new stream
  1408. * from remote.
  1409. */
  1410. static int session_allow_incoming_new_stream(nghttp2_session *session) {
  1411. return (session->goaway_flags &
  1412. (NGHTTP2_GOAWAY_TERM_ON_SEND | NGHTTP2_GOAWAY_SENT)) == 0;
  1413. }
  1414. /*
  1415. * This function returns nonzero if session is closing.
  1416. */
  1417. static int session_is_closing(nghttp2_session *session) {
  1418. return (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) != 0 ||
  1419. (nghttp2_session_want_read(session) == 0 &&
  1420. nghttp2_session_want_write(session) == 0);
  1421. }
  1422. /*
  1423. * Check that we can send a frame to the |stream|. This function
  1424. * returns 0 if we can send a frame to the |frame|, or one of the
  1425. * following negative error codes:
  1426. *
  1427. * NGHTTP2_ERR_STREAM_CLOSED
  1428. * The stream is already closed.
  1429. * NGHTTP2_ERR_STREAM_SHUT_WR
  1430. * The stream is half-closed for transmission.
  1431. * NGHTTP2_ERR_SESSION_CLOSING
  1432. * This session is closing.
  1433. */
  1434. static int session_predicate_for_stream_send(nghttp2_session *session,
  1435. nghttp2_stream *stream) {
  1436. if (stream == NULL) {
  1437. return NGHTTP2_ERR_STREAM_CLOSED;
  1438. }
  1439. if (session_is_closing(session)) {
  1440. return NGHTTP2_ERR_SESSION_CLOSING;
  1441. }
  1442. if (stream->shut_flags & NGHTTP2_SHUT_WR) {
  1443. return NGHTTP2_ERR_STREAM_SHUT_WR;
  1444. }
  1445. return 0;
  1446. }
  1447. int nghttp2_session_check_request_allowed(nghttp2_session *session) {
  1448. return !session->server && session->next_stream_id <= INT32_MAX &&
  1449. (session->goaway_flags & NGHTTP2_GOAWAY_RECV) == 0 &&
  1450. !session_is_closing(session);
  1451. }
  1452. /*
  1453. * This function checks request HEADERS frame, which opens stream, can
  1454. * be sent at this time.
  1455. *
  1456. * This function returns 0 if it succeeds, or one of the following
  1457. * negative error codes:
  1458. *
  1459. * NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
  1460. * New stream cannot be created because of GOAWAY: session is
  1461. * going down or received last_stream_id is strictly less than
  1462. * frame->hd.stream_id.
  1463. * NGHTTP2_ERR_STREAM_CLOSING
  1464. * request HEADERS was canceled by RST_STREAM while it is in queue.
  1465. */
  1466. static int session_predicate_request_headers_send(nghttp2_session *session,
  1467. nghttp2_outbound_item *item) {
  1468. if (item->aux_data.headers.canceled) {
  1469. return NGHTTP2_ERR_STREAM_CLOSING;
  1470. }
  1471. /* If we are terminating session (NGHTTP2_GOAWAY_TERM_ON_SEND),
  1472. GOAWAY was received from peer, or session is about to close, new
  1473. request is not allowed. */
  1474. if ((session->goaway_flags & NGHTTP2_GOAWAY_RECV) ||
  1475. session_is_closing(session)) {
  1476. return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
  1477. }
  1478. return 0;
  1479. }
  1480. /*
  1481. * This function checks HEADERS, which is the first frame from the
  1482. * server, with the |stream| can be sent at this time. The |stream|
  1483. * can be NULL.
  1484. *
  1485. * This function returns 0 if it succeeds, or one of the following
  1486. * negative error codes:
  1487. *
  1488. * NGHTTP2_ERR_STREAM_CLOSED
  1489. * The stream is already closed or does not exist.
  1490. * NGHTTP2_ERR_STREAM_SHUT_WR
  1491. * The transmission is not allowed for this stream (e.g., a frame
  1492. * with END_STREAM flag set has already sent)
  1493. * NGHTTP2_ERR_INVALID_STREAM_ID
  1494. * The stream ID is invalid.
  1495. * NGHTTP2_ERR_STREAM_CLOSING
  1496. * RST_STREAM was queued for this stream.
  1497. * NGHTTP2_ERR_INVALID_STREAM_STATE
  1498. * The state of the stream is not valid.
  1499. * NGHTTP2_ERR_SESSION_CLOSING
  1500. * This session is closing.
  1501. * NGHTTP2_ERR_PROTO
  1502. * Client side attempted to send response.
  1503. */
  1504. static int session_predicate_response_headers_send(nghttp2_session *session,
  1505. nghttp2_stream *stream) {
  1506. int rv;
  1507. rv = session_predicate_for_stream_send(session, stream);
  1508. if (rv != 0) {
  1509. return rv;
  1510. }
  1511. assert(stream);
  1512. if (!session->server) {
  1513. return NGHTTP2_ERR_PROTO;
  1514. }
  1515. if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
  1516. return NGHTTP2_ERR_INVALID_STREAM_ID;
  1517. }
  1518. switch (stream->state) {
  1519. case NGHTTP2_STREAM_OPENING:
  1520. return 0;
  1521. case NGHTTP2_STREAM_CLOSING:
  1522. return NGHTTP2_ERR_STREAM_CLOSING;
  1523. default:
  1524. return NGHTTP2_ERR_INVALID_STREAM_STATE;
  1525. }
  1526. }
  1527. /*
  1528. * This function checks HEADERS for reserved stream can be sent. The
  1529. * |stream| must be reserved state and the |session| is server side.
  1530. * The |stream| can be NULL.
  1531. *
  1532. * This function returns 0 if it succeeds, or one of the following
  1533. * error codes:
  1534. *
  1535. * NGHTTP2_ERR_STREAM_CLOSED
  1536. * The stream is already closed.
  1537. * NGHTTP2_ERR_STREAM_SHUT_WR
  1538. * The stream is half-closed for transmission.
  1539. * NGHTTP2_ERR_PROTO
  1540. * The stream is not reserved state
  1541. * NGHTTP2_ERR_STREAM_CLOSED
  1542. * RST_STREAM was queued for this stream.
  1543. * NGHTTP2_ERR_SESSION_CLOSING
  1544. * This session is closing.
  1545. * NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
  1546. * New stream cannot be created because GOAWAY is already sent or
  1547. * received.
  1548. * NGHTTP2_ERR_PROTO
  1549. * Client side attempted to send push response.
  1550. */
  1551. static int
  1552. session_predicate_push_response_headers_send(nghttp2_session *session,
  1553. nghttp2_stream *stream) {
  1554. int rv;
  1555. /* TODO Should disallow HEADERS if GOAWAY has already been issued? */
  1556. rv = session_predicate_for_stream_send(session, stream);
  1557. if (rv != 0) {
  1558. return rv;
  1559. }
  1560. assert(stream);
  1561. if (!session->server) {
  1562. return NGHTTP2_ERR_PROTO;
  1563. }
  1564. if (stream->state != NGHTTP2_STREAM_RESERVED) {
  1565. return NGHTTP2_ERR_PROTO;
  1566. }
  1567. if (session->goaway_flags & NGHTTP2_GOAWAY_RECV) {
  1568. return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
  1569. }
  1570. return 0;
  1571. }
  1572. /*
  1573. * This function checks HEADERS, which is neither stream-opening nor
  1574. * first response header, with the |stream| can be sent at this time.
  1575. * The |stream| can be NULL.
  1576. *
  1577. * This function returns 0 if it succeeds, or one of the following
  1578. * negative error codes:
  1579. *
  1580. * NGHTTP2_ERR_STREAM_CLOSED
  1581. * The stream is already closed or does not exist.
  1582. * NGHTTP2_ERR_STREAM_SHUT_WR
  1583. * The transmission is not allowed for this stream (e.g., a frame
  1584. * with END_STREAM flag set has already sent)
  1585. * NGHTTP2_ERR_STREAM_CLOSING
  1586. * RST_STREAM was queued for this stream.
  1587. * NGHTTP2_ERR_INVALID_STREAM_STATE
  1588. * The state of the stream is not valid.
  1589. * NGHTTP2_ERR_SESSION_CLOSING
  1590. * This session is closing.
  1591. */
  1592. static int session_predicate_headers_send(nghttp2_session *session,
  1593. nghttp2_stream *stream) {
  1594. int rv;
  1595. rv = session_predicate_for_stream_send(session, stream);
  1596. if (rv != 0) {
  1597. return rv;
  1598. }
  1599. assert(stream);
  1600. switch (stream->state) {
  1601. case NGHTTP2_STREAM_OPENED:
  1602. return 0;
  1603. case NGHTTP2_STREAM_CLOSING:
  1604. return NGHTTP2_ERR_STREAM_CLOSING;
  1605. default:
  1606. if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
  1607. return 0;
  1608. }
  1609. return NGHTTP2_ERR_INVALID_STREAM_STATE;
  1610. }
  1611. }
  1612. /*
  1613. * This function checks PUSH_PROMISE frame |frame| with the |stream|
  1614. * can be sent at this time. The |stream| can be NULL.
  1615. *
  1616. * This function returns 0 if it succeeds, or one of the following
  1617. * negative error codes:
  1618. *
  1619. * NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
  1620. * New stream cannot be created because GOAWAY is already sent or
  1621. * received.
  1622. * NGHTTP2_ERR_PROTO
  1623. * The client side attempts to send PUSH_PROMISE, or the server
  1624. * sends PUSH_PROMISE for the stream not initiated by the client.
  1625. * NGHTTP2_ERR_STREAM_CLOSED
  1626. * The stream is already closed or does not exist.
  1627. * NGHTTP2_ERR_STREAM_CLOSING
  1628. * RST_STREAM was queued for this stream.
  1629. * NGHTTP2_ERR_STREAM_SHUT_WR
  1630. * The transmission is not allowed for this stream (e.g., a frame
  1631. * with END_STREAM flag set has already sent)
  1632. * NGHTTP2_ERR_PUSH_DISABLED
  1633. * The remote peer disabled reception of PUSH_PROMISE.
  1634. * NGHTTP2_ERR_SESSION_CLOSING
  1635. * This session is closing.
  1636. */
  1637. static int session_predicate_push_promise_send(nghttp2_session *session,
  1638. nghttp2_stream *stream) {
  1639. int rv;
  1640. if (!session->server) {
  1641. return NGHTTP2_ERR_PROTO;
  1642. }
  1643. rv = session_predicate_for_stream_send(session, stream);
  1644. if (rv != 0) {
  1645. return rv;
  1646. }
  1647. assert(stream);
  1648. if (session->remote_settings.enable_push == 0) {
  1649. return NGHTTP2_ERR_PUSH_DISABLED;
  1650. }
  1651. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  1652. return NGHTTP2_ERR_STREAM_CLOSING;
  1653. }
  1654. if (session->goaway_flags & NGHTTP2_GOAWAY_RECV) {
  1655. return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
  1656. }
  1657. return 0;
  1658. }
  1659. /*
  1660. * This function checks WINDOW_UPDATE with the stream ID |stream_id|
  1661. * can be sent at this time. Note that END_STREAM flag of the previous
  1662. * frame does not affect the transmission of the WINDOW_UPDATE frame.
  1663. *
  1664. * This function returns 0 if it succeeds, or one of the following
  1665. * negative error codes:
  1666. *
  1667. * NGHTTP2_ERR_STREAM_CLOSED
  1668. * The stream is already closed or does not exist.
  1669. * NGHTTP2_ERR_STREAM_CLOSING
  1670. * RST_STREAM was queued for this stream.
  1671. * NGHTTP2_ERR_INVALID_STREAM_STATE
  1672. * The state of the stream is not valid.
  1673. * NGHTTP2_ERR_SESSION_CLOSING
  1674. * This session is closing.
  1675. */
  1676. static int session_predicate_window_update_send(nghttp2_session *session,
  1677. int32_t stream_id) {
  1678. nghttp2_stream *stream;
  1679. if (session_is_closing(session)) {
  1680. return NGHTTP2_ERR_SESSION_CLOSING;
  1681. }
  1682. if (stream_id == 0) {
  1683. /* Connection-level window update */
  1684. return 0;
  1685. }
  1686. stream = nghttp2_session_get_stream(session, stream_id);
  1687. if (stream == NULL) {
  1688. return NGHTTP2_ERR_STREAM_CLOSED;
  1689. }
  1690. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  1691. return NGHTTP2_ERR_STREAM_CLOSING;
  1692. }
  1693. if (state_reserved_local(session, stream)) {
  1694. return NGHTTP2_ERR_INVALID_STREAM_STATE;
  1695. }
  1696. return 0;
  1697. }
  1698. static int session_predicate_altsvc_send(nghttp2_session *session,
  1699. int32_t stream_id) {
  1700. nghttp2_stream *stream;
  1701. if (session_is_closing(session)) {
  1702. return NGHTTP2_ERR_SESSION_CLOSING;
  1703. }
  1704. if (stream_id == 0) {
  1705. return 0;
  1706. }
  1707. stream = nghttp2_session_get_stream(session, stream_id);
  1708. if (stream == NULL) {
  1709. return NGHTTP2_ERR_STREAM_CLOSED;
  1710. }
  1711. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  1712. return NGHTTP2_ERR_STREAM_CLOSING;
  1713. }
  1714. return 0;
  1715. }
  1716. static int session_predicate_origin_send(nghttp2_session *session) {
  1717. if (session_is_closing(session)) {
  1718. return NGHTTP2_ERR_SESSION_CLOSING;
  1719. }
  1720. return 0;
  1721. }
  1722. static int session_predicate_priority_update_send(nghttp2_session *session,
  1723. int32_t stream_id) {
  1724. nghttp2_stream *stream;
  1725. if (session_is_closing(session)) {
  1726. return NGHTTP2_ERR_SESSION_CLOSING;
  1727. }
  1728. stream = nghttp2_session_get_stream(session, stream_id);
  1729. if (stream == NULL) {
  1730. return 0;
  1731. }
  1732. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  1733. return NGHTTP2_ERR_STREAM_CLOSING;
  1734. }
  1735. if (stream->shut_flags & NGHTTP2_SHUT_RD) {
  1736. return NGHTTP2_ERR_INVALID_STREAM_STATE;
  1737. }
  1738. return 0;
  1739. }
  1740. /* Take into account settings max frame size and both connection-level
  1741. flow control here */
  1742. static ssize_t
  1743. nghttp2_session_enforce_flow_control_limits(nghttp2_session *session,
  1744. nghttp2_stream *stream,
  1745. ssize_t requested_window_size) {
  1746. DEBUGF("send: remote windowsize connection=%d, remote maxframsize=%u, "
  1747. "stream(id %d)=%d\n",
  1748. session->remote_window_size, session->remote_settings.max_frame_size,
  1749. stream->stream_id, stream->remote_window_size);
  1750. return nghttp2_min(nghttp2_min(nghttp2_min(requested_window_size,
  1751. stream->remote_window_size),
  1752. session->remote_window_size),
  1753. (int32_t)session->remote_settings.max_frame_size);
  1754. }
  1755. /*
  1756. * Returns the maximum length of next data read. If the
  1757. * connection-level and/or stream-wise flow control are enabled, the
  1758. * return value takes into account those current window sizes. The remote
  1759. * settings for max frame size is also taken into account.
  1760. */
  1761. static size_t nghttp2_session_next_data_read(nghttp2_session *session,
  1762. nghttp2_stream *stream) {
  1763. ssize_t window_size;
  1764. window_size = nghttp2_session_enforce_flow_control_limits(
  1765. session, stream, NGHTTP2_DATA_PAYLOADLEN);
  1766. DEBUGF("send: available window=%zd\n", window_size);
  1767. return window_size > 0 ? (size_t)window_size : 0;
  1768. }
  1769. /*
  1770. * This function checks DATA with the |stream| can be sent at this
  1771. * time. The |stream| can be NULL.
  1772. *
  1773. * This function returns 0 if it succeeds, or one of the following
  1774. * negative error codes:
  1775. *
  1776. * NGHTTP2_ERR_STREAM_CLOSED
  1777. * The stream is already closed or does not exist.
  1778. * NGHTTP2_ERR_STREAM_SHUT_WR
  1779. * The transmission is not allowed for this stream (e.g., a frame
  1780. * with END_STREAM flag set has already sent)
  1781. * NGHTTP2_ERR_STREAM_CLOSING
  1782. * RST_STREAM was queued for this stream.
  1783. * NGHTTP2_ERR_INVALID_STREAM_STATE
  1784. * The state of the stream is not valid.
  1785. * NGHTTP2_ERR_SESSION_CLOSING
  1786. * This session is closing.
  1787. */
  1788. static int nghttp2_session_predicate_data_send(nghttp2_session *session,
  1789. nghttp2_stream *stream) {
  1790. int rv;
  1791. rv = session_predicate_for_stream_send(session, stream);
  1792. if (rv != 0) {
  1793. return rv;
  1794. }
  1795. assert(stream);
  1796. if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
  1797. /* Request body data */
  1798. /* If stream->state is NGHTTP2_STREAM_CLOSING, RST_STREAM was
  1799. queued but not yet sent. In this case, we won't send DATA
  1800. frames. */
  1801. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  1802. return NGHTTP2_ERR_STREAM_CLOSING;
  1803. }
  1804. if (stream->state == NGHTTP2_STREAM_RESERVED) {
  1805. return NGHTTP2_ERR_INVALID_STREAM_STATE;
  1806. }
  1807. return 0;
  1808. }
  1809. /* Response body data */
  1810. if (stream->state == NGHTTP2_STREAM_OPENED) {
  1811. return 0;
  1812. }
  1813. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  1814. return NGHTTP2_ERR_STREAM_CLOSING;
  1815. }
  1816. return NGHTTP2_ERR_INVALID_STREAM_STATE;
  1817. }
  1818. static ssize_t session_call_select_padding(nghttp2_session *session,
  1819. const nghttp2_frame *frame,
  1820. size_t max_payloadlen) {
  1821. ssize_t rv;
  1822. if (frame->hd.length >= max_payloadlen) {
  1823. return (ssize_t)frame->hd.length;
  1824. }
  1825. if (session->callbacks.select_padding_callback) {
  1826. size_t max_paddedlen;
  1827. max_paddedlen =
  1828. nghttp2_min(frame->hd.length + NGHTTP2_MAX_PADLEN, max_payloadlen);
  1829. rv = session->callbacks.select_padding_callback(
  1830. session, frame, max_paddedlen, session->user_data);
  1831. if (rv < (ssize_t)frame->hd.length || rv > (ssize_t)max_paddedlen) {
  1832. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1833. }
  1834. return rv;
  1835. }
  1836. return (ssize_t)frame->hd.length;
  1837. }
  1838. /* Add padding to HEADERS or PUSH_PROMISE. We use
  1839. frame->headers.padlen in this function to use the fact that
  1840. frame->push_promise has also padlen in the same position. */
  1841. static int session_headers_add_pad(nghttp2_session *session,
  1842. nghttp2_frame *frame) {
  1843. ssize_t padded_payloadlen;
  1844. nghttp2_active_outbound_item *aob;
  1845. nghttp2_bufs *framebufs;
  1846. size_t padlen;
  1847. size_t max_payloadlen;
  1848. aob = &session->aob;
  1849. framebufs = &aob->framebufs;
  1850. max_payloadlen = nghttp2_min(NGHTTP2_MAX_PAYLOADLEN,
  1851. frame->hd.length + NGHTTP2_MAX_PADLEN);
  1852. padded_payloadlen =
  1853. session_call_select_padding(session, frame, max_payloadlen);
  1854. if (nghttp2_is_fatal((int)padded_payloadlen)) {
  1855. return (int)padded_payloadlen;
  1856. }
  1857. padlen = (size_t)padded_payloadlen - frame->hd.length;
  1858. DEBUGF("send: padding selected: payloadlen=%zd, padlen=%zu\n",
  1859. padded_payloadlen, padlen);
  1860. nghttp2_frame_add_pad(framebufs, &frame->hd, padlen, 0);
  1861. frame->headers.padlen = padlen;
  1862. return 0;
  1863. }
  1864. static size_t session_estimate_headers_payload(nghttp2_session *session,
  1865. const nghttp2_nv *nva,
  1866. size_t nvlen,
  1867. size_t additional) {
  1868. return nghttp2_hd_deflate_bound(&session->hd_deflater, nva, nvlen) +
  1869. additional;
  1870. }
  1871. static int session_pack_extension(nghttp2_session *session, nghttp2_bufs *bufs,
  1872. nghttp2_frame *frame) {
  1873. ssize_t rv;
  1874. nghttp2_buf *buf;
  1875. size_t buflen;
  1876. size_t framelen;
  1877. assert(session->callbacks.pack_extension_callback);
  1878. buf = &bufs->head->buf;
  1879. buflen = nghttp2_min(nghttp2_buf_avail(buf), NGHTTP2_MAX_PAYLOADLEN);
  1880. rv = session->callbacks.pack_extension_callback(session, buf->last, buflen,
  1881. frame, session->user_data);
  1882. if (rv == NGHTTP2_ERR_CANCEL) {
  1883. return (int)rv;
  1884. }
  1885. if (rv < 0 || (size_t)rv > buflen) {
  1886. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1887. }
  1888. framelen = (size_t)rv;
  1889. frame->hd.length = framelen;
  1890. assert(buf->pos == buf->last);
  1891. buf->last += framelen;
  1892. buf->pos -= NGHTTP2_FRAME_HDLEN;
  1893. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  1894. return 0;
  1895. }
  1896. /*
  1897. * This function serializes frame for transmission.
  1898. *
  1899. * This function returns 0 if it succeeds, or one of negative error
  1900. * codes, including both fatal and non-fatal ones.
  1901. */
  1902. static int session_prep_frame(nghttp2_session *session,
  1903. nghttp2_outbound_item *item) {
  1904. int rv;
  1905. nghttp2_frame *frame;
  1906. nghttp2_mem *mem;
  1907. mem = &session->mem;
  1908. frame = &item->frame;
  1909. switch (frame->hd.type) {
  1910. case NGHTTP2_DATA: {
  1911. size_t next_readmax;
  1912. nghttp2_stream *stream;
  1913. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  1914. if (stream) {
  1915. assert(stream->item == item);
  1916. }
  1917. rv = nghttp2_session_predicate_data_send(session, stream);
  1918. if (rv != 0) {
  1919. // If stream was already closed, nghttp2_session_get_stream()
  1920. // returns NULL, but item is still attached to the stream.
  1921. // Search stream including closed again.
  1922. stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
  1923. if (stream) {
  1924. session_detach_stream_item(session, stream);
  1925. }
  1926. return rv;
  1927. }
  1928. /* Assuming stream is not NULL */
  1929. assert(stream);
  1930. next_readmax = nghttp2_session_next_data_read(session, stream);
  1931. if (next_readmax == 0) {
  1932. /* This must be true since we only pop DATA frame item from
  1933. queue when session->remote_window_size > 0 */
  1934. assert(session->remote_window_size > 0);
  1935. session_defer_stream_item(session, stream,
  1936. NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
  1937. session->aob.item = NULL;
  1938. active_outbound_item_reset(&session->aob, mem);
  1939. return NGHTTP2_ERR_DEFERRED;
  1940. }
  1941. rv = nghttp2_session_pack_data(session, &session->aob.framebufs,
  1942. next_readmax, frame, &item->aux_data.data,
  1943. stream);
  1944. if (rv == NGHTTP2_ERR_PAUSE) {
  1945. return rv;
  1946. }
  1947. if (rv == NGHTTP2_ERR_DEFERRED) {
  1948. session_defer_stream_item(session, stream,
  1949. NGHTTP2_STREAM_FLAG_DEFERRED_USER);
  1950. session->aob.item = NULL;
  1951. active_outbound_item_reset(&session->aob, mem);
  1952. return NGHTTP2_ERR_DEFERRED;
  1953. }
  1954. if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  1955. session_detach_stream_item(session, stream);
  1956. rv = nghttp2_session_add_rst_stream(session, frame->hd.stream_id,
  1957. NGHTTP2_INTERNAL_ERROR);
  1958. if (nghttp2_is_fatal(rv)) {
  1959. return rv;
  1960. }
  1961. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  1962. }
  1963. if (rv != 0) {
  1964. session_detach_stream_item(session, stream);
  1965. return rv;
  1966. }
  1967. return 0;
  1968. }
  1969. case NGHTTP2_HEADERS: {
  1970. nghttp2_headers_aux_data *aux_data;
  1971. size_t estimated_payloadlen;
  1972. aux_data = &item->aux_data.headers;
  1973. if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
  1974. /* initial HEADERS, which opens stream */
  1975. nghttp2_stream *stream;
  1976. stream = nghttp2_session_open_stream(
  1977. session, frame->hd.stream_id, NGHTTP2_STREAM_FLAG_NONE,
  1978. &frame->headers.pri_spec, NGHTTP2_STREAM_INITIAL,
  1979. aux_data->stream_user_data);
  1980. if (stream == NULL) {
  1981. return NGHTTP2_ERR_NOMEM;
  1982. }
  1983. /* We don't call nghttp2_session_adjust_closed_stream() here,
  1984. since we don't keep closed stream in client side */
  1985. rv = session_predicate_request_headers_send(session, item);
  1986. if (rv != 0) {
  1987. return rv;
  1988. }
  1989. if (session_enforce_http_messaging(session)) {
  1990. nghttp2_http_record_request_method(stream, frame);
  1991. }
  1992. } else {
  1993. nghttp2_stream *stream;
  1994. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  1995. if (stream && stream->state == NGHTTP2_STREAM_RESERVED) {
  1996. rv = session_predicate_push_response_headers_send(session, stream);
  1997. if (rv == 0) {
  1998. frame->headers.cat = NGHTTP2_HCAT_PUSH_RESPONSE;
  1999. if (aux_data->stream_user_data) {
  2000. stream->stream_user_data = aux_data->stream_user_data;
  2001. }
  2002. }
  2003. } else if (session_predicate_response_headers_send(session, stream) ==
  2004. 0) {
  2005. frame->headers.cat = NGHTTP2_HCAT_RESPONSE;
  2006. rv = 0;
  2007. } else {
  2008. frame->headers.cat = NGHTTP2_HCAT_HEADERS;
  2009. rv = session_predicate_headers_send(session, stream);
  2010. }
  2011. if (rv != 0) {
  2012. return rv;
  2013. }
  2014. }
  2015. estimated_payloadlen = session_estimate_headers_payload(
  2016. session, frame->headers.nva, frame->headers.nvlen,
  2017. NGHTTP2_PRIORITY_SPECLEN);
  2018. if (estimated_payloadlen > session->max_send_header_block_length) {
  2019. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  2020. }
  2021. rv = nghttp2_frame_pack_headers(&session->aob.framebufs, &frame->headers,
  2022. &session->hd_deflater);
  2023. if (rv != 0) {
  2024. return rv;
  2025. }
  2026. DEBUGF("send: before padding, HEADERS serialized in %zd bytes\n",
  2027. nghttp2_bufs_len(&session->aob.framebufs));
  2028. rv = session_headers_add_pad(session, frame);
  2029. if (rv != 0) {
  2030. return rv;
  2031. }
  2032. DEBUGF("send: HEADERS finally serialized in %zd bytes\n",
  2033. nghttp2_bufs_len(&session->aob.framebufs));
  2034. if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
  2035. assert(session->last_sent_stream_id < frame->hd.stream_id);
  2036. session->last_sent_stream_id = frame->hd.stream_id;
  2037. }
  2038. return 0;
  2039. }
  2040. case NGHTTP2_PRIORITY: {
  2041. if (session_is_closing(session)) {
  2042. return NGHTTP2_ERR_SESSION_CLOSING;
  2043. }
  2044. /* PRIORITY frame can be sent at any time and to any stream
  2045. ID. */
  2046. nghttp2_frame_pack_priority(&session->aob.framebufs, &frame->priority);
  2047. /* Peer can send PRIORITY frame against idle stream to create
  2048. "anchor" in dependency tree. Only client can do this in
  2049. nghttp2. In nghttp2, only server retains non-active (closed
  2050. or idle) streams in memory, so we don't open stream here. */
  2051. return 0;
  2052. }
  2053. case NGHTTP2_RST_STREAM:
  2054. if (session_is_closing(session)) {
  2055. return NGHTTP2_ERR_SESSION_CLOSING;
  2056. }
  2057. nghttp2_frame_pack_rst_stream(&session->aob.framebufs, &frame->rst_stream);
  2058. return 0;
  2059. case NGHTTP2_SETTINGS: {
  2060. if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
  2061. assert(session->obq_flood_counter_ > 0);
  2062. --session->obq_flood_counter_;
  2063. /* When session is about to close, don't send SETTINGS ACK.
  2064. We are required to send SETTINGS without ACK though; for
  2065. example, we have to send SETTINGS as a part of connection
  2066. preface. */
  2067. if (session_is_closing(session)) {
  2068. return NGHTTP2_ERR_SESSION_CLOSING;
  2069. }
  2070. }
  2071. rv = nghttp2_frame_pack_settings(&session->aob.framebufs, &frame->settings);
  2072. if (rv != 0) {
  2073. return rv;
  2074. }
  2075. return 0;
  2076. }
  2077. case NGHTTP2_PUSH_PROMISE: {
  2078. nghttp2_stream *stream;
  2079. size_t estimated_payloadlen;
  2080. /* stream could be NULL if associated stream was already
  2081. closed. */
  2082. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  2083. /* predicate should fail if stream is NULL. */
  2084. rv = session_predicate_push_promise_send(session, stream);
  2085. if (rv != 0) {
  2086. return rv;
  2087. }
  2088. assert(stream);
  2089. estimated_payloadlen = session_estimate_headers_payload(
  2090. session, frame->push_promise.nva, frame->push_promise.nvlen, 0);
  2091. if (estimated_payloadlen > session->max_send_header_block_length) {
  2092. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  2093. }
  2094. rv = nghttp2_frame_pack_push_promise(
  2095. &session->aob.framebufs, &frame->push_promise, &session->hd_deflater);
  2096. if (rv != 0) {
  2097. return rv;
  2098. }
  2099. rv = session_headers_add_pad(session, frame);
  2100. if (rv != 0) {
  2101. return rv;
  2102. }
  2103. assert(session->last_sent_stream_id + 2 <=
  2104. frame->push_promise.promised_stream_id);
  2105. session->last_sent_stream_id = frame->push_promise.promised_stream_id;
  2106. return 0;
  2107. }
  2108. case NGHTTP2_PING:
  2109. if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
  2110. assert(session->obq_flood_counter_ > 0);
  2111. --session->obq_flood_counter_;
  2112. }
  2113. /* PING frame is allowed to be sent unless termination GOAWAY is
  2114. sent */
  2115. if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
  2116. return NGHTTP2_ERR_SESSION_CLOSING;
  2117. }
  2118. nghttp2_frame_pack_ping(&session->aob.framebufs, &frame->ping);
  2119. return 0;
  2120. case NGHTTP2_GOAWAY:
  2121. rv = nghttp2_frame_pack_goaway(&session->aob.framebufs, &frame->goaway);
  2122. if (rv != 0) {
  2123. return rv;
  2124. }
  2125. session->local_last_stream_id = frame->goaway.last_stream_id;
  2126. return 0;
  2127. case NGHTTP2_WINDOW_UPDATE:
  2128. rv = session_predicate_window_update_send(session, frame->hd.stream_id);
  2129. if (rv != 0) {
  2130. return rv;
  2131. }
  2132. nghttp2_frame_pack_window_update(&session->aob.framebufs,
  2133. &frame->window_update);
  2134. return 0;
  2135. case NGHTTP2_CONTINUATION:
  2136. /* We never handle CONTINUATION here. */
  2137. assert(0);
  2138. return 0;
  2139. default: {
  2140. nghttp2_ext_aux_data *aux_data;
  2141. /* extension frame */
  2142. aux_data = &item->aux_data.ext;
  2143. if (aux_data->builtin == 0) {
  2144. if (session_is_closing(session)) {
  2145. return NGHTTP2_ERR_SESSION_CLOSING;
  2146. }
  2147. return session_pack_extension(session, &session->aob.framebufs, frame);
  2148. }
  2149. switch (frame->hd.type) {
  2150. case NGHTTP2_ALTSVC:
  2151. rv = session_predicate_altsvc_send(session, frame->hd.stream_id);
  2152. if (rv != 0) {
  2153. return rv;
  2154. }
  2155. nghttp2_frame_pack_altsvc(&session->aob.framebufs, &frame->ext);
  2156. return 0;
  2157. case NGHTTP2_ORIGIN:
  2158. rv = session_predicate_origin_send(session);
  2159. if (rv != 0) {
  2160. return rv;
  2161. }
  2162. rv = nghttp2_frame_pack_origin(&session->aob.framebufs, &frame->ext);
  2163. if (rv != 0) {
  2164. return rv;
  2165. }
  2166. return 0;
  2167. case NGHTTP2_PRIORITY_UPDATE: {
  2168. nghttp2_ext_priority_update *priority_update = frame->ext.payload;
  2169. rv = session_predicate_priority_update_send(session,
  2170. priority_update->stream_id);
  2171. if (rv != 0) {
  2172. return rv;
  2173. }
  2174. nghttp2_frame_pack_priority_update(&session->aob.framebufs, &frame->ext);
  2175. return 0;
  2176. }
  2177. default:
  2178. /* Unreachable here */
  2179. assert(0);
  2180. return 0;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. nghttp2_outbound_item *
  2186. nghttp2_session_get_next_ob_item(nghttp2_session *session) {
  2187. nghttp2_outbound_item *item;
  2188. if (nghttp2_outbound_queue_top(&session->ob_urgent)) {
  2189. return nghttp2_outbound_queue_top(&session->ob_urgent);
  2190. }
  2191. if (nghttp2_outbound_queue_top(&session->ob_reg)) {
  2192. return nghttp2_outbound_queue_top(&session->ob_reg);
  2193. }
  2194. if (!session_is_outgoing_concurrent_streams_max(session)) {
  2195. if (nghttp2_outbound_queue_top(&session->ob_syn)) {
  2196. return nghttp2_outbound_queue_top(&session->ob_syn);
  2197. }
  2198. }
  2199. if (session->remote_window_size > 0) {
  2200. item = nghttp2_stream_next_outbound_item(&session->root);
  2201. if (item) {
  2202. return item;
  2203. }
  2204. return session_sched_get_next_outbound_item(session);
  2205. }
  2206. return NULL;
  2207. }
  2208. nghttp2_outbound_item *
  2209. nghttp2_session_pop_next_ob_item(nghttp2_session *session) {
  2210. nghttp2_outbound_item *item;
  2211. item = nghttp2_outbound_queue_top(&session->ob_urgent);
  2212. if (item) {
  2213. nghttp2_outbound_queue_pop(&session->ob_urgent);
  2214. item->queued = 0;
  2215. return item;
  2216. }
  2217. item = nghttp2_outbound_queue_top(&session->ob_reg);
  2218. if (item) {
  2219. nghttp2_outbound_queue_pop(&session->ob_reg);
  2220. item->queued = 0;
  2221. return item;
  2222. }
  2223. if (!session_is_outgoing_concurrent_streams_max(session)) {
  2224. item = nghttp2_outbound_queue_top(&session->ob_syn);
  2225. if (item) {
  2226. nghttp2_outbound_queue_pop(&session->ob_syn);
  2227. item->queued = 0;
  2228. return item;
  2229. }
  2230. }
  2231. if (session->remote_window_size > 0) {
  2232. item = nghttp2_stream_next_outbound_item(&session->root);
  2233. if (item) {
  2234. return item;
  2235. }
  2236. return session_sched_get_next_outbound_item(session);
  2237. }
  2238. return NULL;
  2239. }
  2240. static int session_call_before_frame_send(nghttp2_session *session,
  2241. nghttp2_frame *frame) {
  2242. int rv;
  2243. if (session->callbacks.before_frame_send_callback) {
  2244. rv = session->callbacks.before_frame_send_callback(session, frame,
  2245. session->user_data);
  2246. if (rv == NGHTTP2_ERR_CANCEL) {
  2247. return rv;
  2248. }
  2249. if (rv != 0) {
  2250. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2251. }
  2252. }
  2253. return 0;
  2254. }
  2255. static int session_call_on_frame_send(nghttp2_session *session,
  2256. nghttp2_frame *frame) {
  2257. int rv;
  2258. if (session->callbacks.on_frame_send_callback) {
  2259. rv = session->callbacks.on_frame_send_callback(session, frame,
  2260. session->user_data);
  2261. if (rv != 0) {
  2262. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2263. }
  2264. }
  2265. return 0;
  2266. }
  2267. static int find_stream_on_goaway_func(void *entry, void *ptr) {
  2268. nghttp2_close_stream_on_goaway_arg *arg;
  2269. nghttp2_stream *stream;
  2270. arg = (nghttp2_close_stream_on_goaway_arg *)ptr;
  2271. stream = (nghttp2_stream *)entry;
  2272. if (nghttp2_session_is_my_stream_id(arg->session, stream->stream_id)) {
  2273. if (arg->incoming) {
  2274. return 0;
  2275. }
  2276. } else if (!arg->incoming) {
  2277. return 0;
  2278. }
  2279. if (stream->state != NGHTTP2_STREAM_IDLE &&
  2280. (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) == 0 &&
  2281. stream->stream_id > arg->last_stream_id) {
  2282. /* We are collecting streams to close because we cannot call
  2283. nghttp2_session_close_stream() inside nghttp2_map_each().
  2284. Reuse closed_next member.. bad choice? */
  2285. assert(stream->closed_next == NULL);
  2286. assert(stream->closed_prev == NULL);
  2287. if (arg->head) {
  2288. stream->closed_next = arg->head;
  2289. arg->head = stream;
  2290. } else {
  2291. arg->head = stream;
  2292. }
  2293. }
  2294. return 0;
  2295. }
  2296. /* Closes non-idle and non-closed streams whose stream ID >
  2297. last_stream_id. If incoming is nonzero, we are going to close
  2298. incoming streams. Otherwise, close outgoing streams. */
  2299. static int session_close_stream_on_goaway(nghttp2_session *session,
  2300. int32_t last_stream_id,
  2301. int incoming) {
  2302. int rv;
  2303. nghttp2_stream *stream, *next_stream;
  2304. nghttp2_close_stream_on_goaway_arg arg = {session, NULL, last_stream_id,
  2305. incoming};
  2306. rv = nghttp2_map_each(&session->streams, find_stream_on_goaway_func, &arg);
  2307. assert(rv == 0);
  2308. stream = arg.head;
  2309. while (stream) {
  2310. next_stream = stream->closed_next;
  2311. stream->closed_next = NULL;
  2312. rv = nghttp2_session_close_stream(session, stream->stream_id,
  2313. NGHTTP2_REFUSED_STREAM);
  2314. /* stream may be deleted here */
  2315. stream = next_stream;
  2316. if (nghttp2_is_fatal(rv)) {
  2317. /* Clean up closed_next member just in case */
  2318. while (stream) {
  2319. next_stream = stream->closed_next;
  2320. stream->closed_next = NULL;
  2321. stream = next_stream;
  2322. }
  2323. return rv;
  2324. }
  2325. }
  2326. return 0;
  2327. }
  2328. static void session_reschedule_stream(nghttp2_session *session,
  2329. nghttp2_stream *stream) {
  2330. stream->last_writelen = stream->item->frame.hd.length;
  2331. if (!(stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES)) {
  2332. nghttp2_stream_reschedule(stream);
  2333. return;
  2334. }
  2335. if (!session->server) {
  2336. return;
  2337. }
  2338. session_sched_reschedule_stream(session, stream);
  2339. }
  2340. static int session_update_stream_consumed_size(nghttp2_session *session,
  2341. nghttp2_stream *stream,
  2342. size_t delta_size);
  2343. static int session_update_connection_consumed_size(nghttp2_session *session,
  2344. size_t delta_size);
  2345. /*
  2346. * Called after a frame is sent. This function runs
  2347. * on_frame_send_callback and handles stream closure upon END_STREAM
  2348. * or RST_STREAM. This function does not reset session->aob. It is a
  2349. * responsibility of session_after_frame_sent2.
  2350. *
  2351. * This function returns 0 if it succeeds, or one of the following
  2352. * negative error codes:
  2353. *
  2354. * NGHTTP2_ERR_NOMEM
  2355. * Out of memory.
  2356. * NGHTTP2_ERR_CALLBACK_FAILURE
  2357. * The callback function failed.
  2358. */
  2359. static int session_after_frame_sent1(nghttp2_session *session) {
  2360. int rv;
  2361. nghttp2_active_outbound_item *aob = &session->aob;
  2362. nghttp2_outbound_item *item = aob->item;
  2363. nghttp2_bufs *framebufs = &aob->framebufs;
  2364. nghttp2_frame *frame;
  2365. nghttp2_stream *stream;
  2366. frame = &item->frame;
  2367. if (frame->hd.type == NGHTTP2_DATA) {
  2368. nghttp2_data_aux_data *aux_data;
  2369. aux_data = &item->aux_data.data;
  2370. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  2371. /* We update flow control window after a frame was completely
  2372. sent. This is possible because we choose payload length not to
  2373. exceed the window */
  2374. session->remote_window_size -= (int32_t)frame->hd.length;
  2375. if (stream) {
  2376. stream->remote_window_size -= (int32_t)frame->hd.length;
  2377. }
  2378. if (stream && aux_data->eof) {
  2379. session_detach_stream_item(session, stream);
  2380. /* Call on_frame_send_callback after
  2381. nghttp2_stream_detach_item(), so that application can issue
  2382. nghttp2_submit_data() in the callback. */
  2383. if (session->callbacks.on_frame_send_callback) {
  2384. rv = session_call_on_frame_send(session, frame);
  2385. if (nghttp2_is_fatal(rv)) {
  2386. return rv;
  2387. }
  2388. }
  2389. if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  2390. int stream_closed;
  2391. stream_closed =
  2392. (stream->shut_flags & NGHTTP2_SHUT_RDWR) == NGHTTP2_SHUT_RDWR;
  2393. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
  2394. rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
  2395. if (nghttp2_is_fatal(rv)) {
  2396. return rv;
  2397. }
  2398. /* stream may be NULL if it was closed */
  2399. if (stream_closed) {
  2400. stream = NULL;
  2401. }
  2402. }
  2403. return 0;
  2404. }
  2405. if (session->callbacks.on_frame_send_callback) {
  2406. rv = session_call_on_frame_send(session, frame);
  2407. if (nghttp2_is_fatal(rv)) {
  2408. return rv;
  2409. }
  2410. }
  2411. return 0;
  2412. }
  2413. /* non-DATA frame */
  2414. if (frame->hd.type == NGHTTP2_HEADERS ||
  2415. frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  2416. if (nghttp2_bufs_next_present(framebufs)) {
  2417. DEBUGF("send: CONTINUATION exists, just return\n");
  2418. return 0;
  2419. }
  2420. }
  2421. rv = session_call_on_frame_send(session, frame);
  2422. if (nghttp2_is_fatal(rv)) {
  2423. return rv;
  2424. }
  2425. switch (frame->hd.type) {
  2426. case NGHTTP2_HEADERS: {
  2427. nghttp2_headers_aux_data *aux_data;
  2428. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  2429. if (!stream) {
  2430. return 0;
  2431. }
  2432. switch (frame->headers.cat) {
  2433. case NGHTTP2_HCAT_REQUEST: {
  2434. stream->state = NGHTTP2_STREAM_OPENING;
  2435. if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  2436. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
  2437. }
  2438. rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
  2439. if (nghttp2_is_fatal(rv)) {
  2440. return rv;
  2441. }
  2442. /* We assume aux_data is a pointer to nghttp2_headers_aux_data */
  2443. aux_data = &item->aux_data.headers;
  2444. if (aux_data->data_prd.read_callback) {
  2445. /* nghttp2_submit_data() makes a copy of aux_data->data_prd */
  2446. rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM,
  2447. frame->hd.stream_id, &aux_data->data_prd);
  2448. if (nghttp2_is_fatal(rv)) {
  2449. return rv;
  2450. }
  2451. /* TODO nghttp2_submit_data() may fail if stream has already
  2452. DATA frame item. We might have to handle it here. */
  2453. }
  2454. return 0;
  2455. }
  2456. case NGHTTP2_HCAT_PUSH_RESPONSE:
  2457. stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_PUSH);
  2458. ++session->num_outgoing_streams;
  2459. /* Fall through */
  2460. case NGHTTP2_HCAT_RESPONSE:
  2461. stream->state = NGHTTP2_STREAM_OPENED;
  2462. /* Fall through */
  2463. case NGHTTP2_HCAT_HEADERS:
  2464. if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  2465. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
  2466. }
  2467. rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
  2468. if (nghttp2_is_fatal(rv)) {
  2469. return rv;
  2470. }
  2471. /* We assume aux_data is a pointer to nghttp2_headers_aux_data */
  2472. aux_data = &item->aux_data.headers;
  2473. if (aux_data->data_prd.read_callback) {
  2474. rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM,
  2475. frame->hd.stream_id, &aux_data->data_prd);
  2476. if (nghttp2_is_fatal(rv)) {
  2477. return rv;
  2478. }
  2479. /* TODO nghttp2_submit_data() may fail if stream has already
  2480. DATA frame item. We might have to handle it here. */
  2481. }
  2482. return 0;
  2483. default:
  2484. /* Unreachable */
  2485. assert(0);
  2486. return 0;
  2487. }
  2488. }
  2489. case NGHTTP2_PRIORITY:
  2490. if (session->server || session->pending_no_rfc7540_priorities == 1) {
  2491. return 0;
  2492. }
  2493. stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
  2494. if (!stream) {
  2495. if (!session_detect_idle_stream(session, frame->hd.stream_id)) {
  2496. return 0;
  2497. }
  2498. stream = nghttp2_session_open_stream(
  2499. session, frame->hd.stream_id, NGHTTP2_FLAG_NONE,
  2500. &frame->priority.pri_spec, NGHTTP2_STREAM_IDLE, NULL);
  2501. if (!stream) {
  2502. return NGHTTP2_ERR_NOMEM;
  2503. }
  2504. } else {
  2505. rv = nghttp2_session_reprioritize_stream(session, stream,
  2506. &frame->priority.pri_spec);
  2507. if (nghttp2_is_fatal(rv)) {
  2508. return rv;
  2509. }
  2510. }
  2511. rv = nghttp2_session_adjust_idle_stream(session);
  2512. if (nghttp2_is_fatal(rv)) {
  2513. return rv;
  2514. }
  2515. return 0;
  2516. case NGHTTP2_RST_STREAM:
  2517. rv = nghttp2_session_close_stream(session, frame->hd.stream_id,
  2518. frame->rst_stream.error_code);
  2519. if (nghttp2_is_fatal(rv)) {
  2520. return rv;
  2521. }
  2522. return 0;
  2523. case NGHTTP2_GOAWAY: {
  2524. nghttp2_goaway_aux_data *aux_data;
  2525. aux_data = &item->aux_data.goaway;
  2526. if ((aux_data->flags & NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE) == 0) {
  2527. if (aux_data->flags & NGHTTP2_GOAWAY_AUX_TERM_ON_SEND) {
  2528. session->goaway_flags |= NGHTTP2_GOAWAY_TERM_SENT;
  2529. }
  2530. session->goaway_flags |= NGHTTP2_GOAWAY_SENT;
  2531. rv = session_close_stream_on_goaway(session, frame->goaway.last_stream_id,
  2532. 1);
  2533. if (nghttp2_is_fatal(rv)) {
  2534. return rv;
  2535. }
  2536. }
  2537. return 0;
  2538. }
  2539. case NGHTTP2_WINDOW_UPDATE:
  2540. if (frame->hd.stream_id == 0) {
  2541. session->window_update_queued = 0;
  2542. if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
  2543. rv = session_update_connection_consumed_size(session, 0);
  2544. } else {
  2545. rv = nghttp2_session_update_recv_connection_window_size(session, 0);
  2546. }
  2547. if (nghttp2_is_fatal(rv)) {
  2548. return rv;
  2549. }
  2550. return 0;
  2551. }
  2552. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  2553. if (!stream) {
  2554. return 0;
  2555. }
  2556. stream->window_update_queued = 0;
  2557. /* We don't have to send WINDOW_UPDATE if END_STREAM from peer
  2558. is seen. */
  2559. if (stream->shut_flags & NGHTTP2_SHUT_RD) {
  2560. return 0;
  2561. }
  2562. if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
  2563. rv = session_update_stream_consumed_size(session, stream, 0);
  2564. } else {
  2565. rv =
  2566. nghttp2_session_update_recv_stream_window_size(session, stream, 0, 1);
  2567. }
  2568. if (nghttp2_is_fatal(rv)) {
  2569. return rv;
  2570. }
  2571. return 0;
  2572. default:
  2573. return 0;
  2574. }
  2575. }
  2576. /*
  2577. * Called after a frame is sent and session_after_frame_sent1. This
  2578. * function is responsible to reset session->aob.
  2579. */
  2580. static void session_after_frame_sent2(nghttp2_session *session) {
  2581. nghttp2_active_outbound_item *aob = &session->aob;
  2582. nghttp2_outbound_item *item = aob->item;
  2583. nghttp2_bufs *framebufs = &aob->framebufs;
  2584. nghttp2_frame *frame;
  2585. nghttp2_mem *mem;
  2586. nghttp2_stream *stream;
  2587. nghttp2_data_aux_data *aux_data;
  2588. mem = &session->mem;
  2589. frame = &item->frame;
  2590. if (frame->hd.type != NGHTTP2_DATA) {
  2591. if (frame->hd.type == NGHTTP2_HEADERS ||
  2592. frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  2593. if (nghttp2_bufs_next_present(framebufs)) {
  2594. framebufs->cur = framebufs->cur->next;
  2595. DEBUGF("send: next CONTINUATION frame, %zu bytes\n",
  2596. nghttp2_buf_len(&framebufs->cur->buf));
  2597. return;
  2598. }
  2599. }
  2600. active_outbound_item_reset(&session->aob, mem);
  2601. return;
  2602. }
  2603. /* DATA frame */
  2604. aux_data = &item->aux_data.data;
  2605. /* On EOF, we have already detached data. Please note that
  2606. application may issue nghttp2_submit_data() in
  2607. on_frame_send_callback (call from session_after_frame_sent1),
  2608. which attach data to stream. We don't want to detach it. */
  2609. if (aux_data->eof) {
  2610. active_outbound_item_reset(aob, mem);
  2611. return;
  2612. }
  2613. /* Reset no_copy here because next write may not use this. */
  2614. aux_data->no_copy = 0;
  2615. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  2616. /* If session is closed or RST_STREAM was queued, we won't send
  2617. further data. */
  2618. if (nghttp2_session_predicate_data_send(session, stream) != 0) {
  2619. if (stream) {
  2620. session_detach_stream_item(session, stream);
  2621. }
  2622. active_outbound_item_reset(aob, mem);
  2623. return;
  2624. }
  2625. aob->item = NULL;
  2626. active_outbound_item_reset(&session->aob, mem);
  2627. return;
  2628. }
  2629. static int session_call_send_data(nghttp2_session *session,
  2630. nghttp2_outbound_item *item,
  2631. nghttp2_bufs *framebufs) {
  2632. int rv;
  2633. nghttp2_buf *buf;
  2634. size_t length;
  2635. nghttp2_frame *frame;
  2636. nghttp2_data_aux_data *aux_data;
  2637. buf = &framebufs->cur->buf;
  2638. frame = &item->frame;
  2639. length = frame->hd.length - frame->data.padlen;
  2640. aux_data = &item->aux_data.data;
  2641. rv = session->callbacks.send_data_callback(session, frame, buf->pos, length,
  2642. &aux_data->data_prd.source,
  2643. session->user_data);
  2644. switch (rv) {
  2645. case 0:
  2646. case NGHTTP2_ERR_WOULDBLOCK:
  2647. case NGHTTP2_ERR_PAUSE:
  2648. case NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE:
  2649. return rv;
  2650. default:
  2651. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2652. }
  2653. }
  2654. static ssize_t nghttp2_session_mem_send_internal(nghttp2_session *session,
  2655. const uint8_t **data_ptr,
  2656. int fast_cb) {
  2657. int rv;
  2658. nghttp2_active_outbound_item *aob;
  2659. nghttp2_bufs *framebufs;
  2660. nghttp2_mem *mem;
  2661. mem = &session->mem;
  2662. aob = &session->aob;
  2663. framebufs = &aob->framebufs;
  2664. /* We may have idle streams more than we expect (e.g.,
  2665. nghttp2_session_change_stream_priority() or
  2666. nghttp2_session_create_idle_stream()). Adjust them here. */
  2667. rv = nghttp2_session_adjust_idle_stream(session);
  2668. if (nghttp2_is_fatal(rv)) {
  2669. return rv;
  2670. }
  2671. for (;;) {
  2672. switch (aob->state) {
  2673. case NGHTTP2_OB_POP_ITEM: {
  2674. nghttp2_outbound_item *item;
  2675. item = nghttp2_session_pop_next_ob_item(session);
  2676. if (item == NULL) {
  2677. return 0;
  2678. }
  2679. rv = session_prep_frame(session, item);
  2680. if (rv == NGHTTP2_ERR_PAUSE) {
  2681. return 0;
  2682. }
  2683. if (rv == NGHTTP2_ERR_DEFERRED) {
  2684. DEBUGF("send: frame transmission deferred\n");
  2685. break;
  2686. }
  2687. if (rv < 0) {
  2688. int32_t opened_stream_id = 0;
  2689. uint32_t error_code = NGHTTP2_INTERNAL_ERROR;
  2690. int rv2 = 0;
  2691. DEBUGF("send: frame preparation failed with %s\n",
  2692. nghttp2_strerror(rv));
  2693. /* TODO If the error comes from compressor, the connection
  2694. must be closed. */
  2695. if (item->frame.hd.type != NGHTTP2_DATA &&
  2696. session->callbacks.on_frame_not_send_callback && is_non_fatal(rv)) {
  2697. nghttp2_frame *frame = &item->frame;
  2698. /* The library is responsible for the transmission of
  2699. WINDOW_UPDATE frame, so we don't call error callback for
  2700. it. */
  2701. if (frame->hd.type != NGHTTP2_WINDOW_UPDATE &&
  2702. session->callbacks.on_frame_not_send_callback(
  2703. session, frame, rv, session->user_data) != 0) {
  2704. nghttp2_outbound_item_free(item, mem);
  2705. nghttp2_mem_free(mem, item);
  2706. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2707. }
  2708. }
  2709. /* We have to close stream opened by failed request HEADERS
  2710. or PUSH_PROMISE. */
  2711. switch (item->frame.hd.type) {
  2712. case NGHTTP2_HEADERS:
  2713. if (item->frame.headers.cat == NGHTTP2_HCAT_REQUEST) {
  2714. opened_stream_id = item->frame.hd.stream_id;
  2715. if (item->aux_data.headers.canceled) {
  2716. error_code = item->aux_data.headers.error_code;
  2717. } else {
  2718. /* Set error_code to REFUSED_STREAM so that application
  2719. can send request again. */
  2720. error_code = NGHTTP2_REFUSED_STREAM;
  2721. }
  2722. }
  2723. break;
  2724. case NGHTTP2_PUSH_PROMISE:
  2725. opened_stream_id = item->frame.push_promise.promised_stream_id;
  2726. break;
  2727. }
  2728. if (opened_stream_id) {
  2729. /* careful not to override rv */
  2730. rv2 = nghttp2_session_close_stream(session, opened_stream_id,
  2731. error_code);
  2732. }
  2733. nghttp2_outbound_item_free(item, mem);
  2734. nghttp2_mem_free(mem, item);
  2735. active_outbound_item_reset(aob, mem);
  2736. if (nghttp2_is_fatal(rv2)) {
  2737. return rv2;
  2738. }
  2739. if (rv == NGHTTP2_ERR_HEADER_COMP) {
  2740. /* If header compression error occurred, should terminiate
  2741. connection. */
  2742. rv = nghttp2_session_terminate_session(session,
  2743. NGHTTP2_INTERNAL_ERROR);
  2744. }
  2745. if (nghttp2_is_fatal(rv)) {
  2746. return rv;
  2747. }
  2748. break;
  2749. }
  2750. aob->item = item;
  2751. nghttp2_bufs_rewind(framebufs);
  2752. if (item->frame.hd.type != NGHTTP2_DATA) {
  2753. nghttp2_frame *frame;
  2754. frame = &item->frame;
  2755. DEBUGF("send: next frame: payloadlen=%zu, type=%u, flags=0x%02x, "
  2756. "stream_id=%d\n",
  2757. frame->hd.length, frame->hd.type, frame->hd.flags,
  2758. frame->hd.stream_id);
  2759. rv = session_call_before_frame_send(session, frame);
  2760. if (nghttp2_is_fatal(rv)) {
  2761. return rv;
  2762. }
  2763. if (rv == NGHTTP2_ERR_CANCEL) {
  2764. int32_t opened_stream_id = 0;
  2765. uint32_t error_code = NGHTTP2_INTERNAL_ERROR;
  2766. if (session->callbacks.on_frame_not_send_callback) {
  2767. if (session->callbacks.on_frame_not_send_callback(
  2768. session, frame, rv, session->user_data) != 0) {
  2769. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2770. }
  2771. }
  2772. /* We have to close stream opened by canceled request
  2773. HEADERS or PUSH_PROMISE. */
  2774. switch (item->frame.hd.type) {
  2775. case NGHTTP2_HEADERS:
  2776. if (item->frame.headers.cat == NGHTTP2_HCAT_REQUEST) {
  2777. opened_stream_id = item->frame.hd.stream_id;
  2778. /* We don't have to check
  2779. item->aux_data.headers.canceled since it has already
  2780. been checked. */
  2781. /* Set error_code to REFUSED_STREAM so that application
  2782. can send request again. */
  2783. error_code = NGHTTP2_REFUSED_STREAM;
  2784. }
  2785. break;
  2786. case NGHTTP2_PUSH_PROMISE:
  2787. opened_stream_id = item->frame.push_promise.promised_stream_id;
  2788. break;
  2789. }
  2790. if (opened_stream_id) {
  2791. /* careful not to override rv */
  2792. int rv2;
  2793. rv2 = nghttp2_session_close_stream(session, opened_stream_id,
  2794. error_code);
  2795. if (nghttp2_is_fatal(rv2)) {
  2796. return rv2;
  2797. }
  2798. }
  2799. active_outbound_item_reset(aob, mem);
  2800. break;
  2801. }
  2802. } else {
  2803. DEBUGF("send: next frame: DATA\n");
  2804. if (item->aux_data.data.no_copy) {
  2805. aob->state = NGHTTP2_OB_SEND_NO_COPY;
  2806. break;
  2807. }
  2808. }
  2809. DEBUGF("send: start transmitting frame type=%u, length=%zd\n",
  2810. framebufs->cur->buf.pos[3],
  2811. framebufs->cur->buf.last - framebufs->cur->buf.pos);
  2812. aob->state = NGHTTP2_OB_SEND_DATA;
  2813. break;
  2814. }
  2815. case NGHTTP2_OB_SEND_DATA: {
  2816. size_t datalen;
  2817. nghttp2_buf *buf;
  2818. buf = &framebufs->cur->buf;
  2819. if (buf->pos == buf->last) {
  2820. DEBUGF("send: end transmission of a frame\n");
  2821. /* Frame has completely sent */
  2822. if (fast_cb) {
  2823. session_after_frame_sent2(session);
  2824. } else {
  2825. rv = session_after_frame_sent1(session);
  2826. if (rv < 0) {
  2827. /* FATAL */
  2828. assert(nghttp2_is_fatal(rv));
  2829. return rv;
  2830. }
  2831. session_after_frame_sent2(session);
  2832. }
  2833. /* We have already adjusted the next state */
  2834. break;
  2835. }
  2836. *data_ptr = buf->pos;
  2837. datalen = nghttp2_buf_len(buf);
  2838. /* We increment the offset here. If send_callback does not send
  2839. everything, we will adjust it. */
  2840. buf->pos += datalen;
  2841. return (ssize_t)datalen;
  2842. }
  2843. case NGHTTP2_OB_SEND_NO_COPY: {
  2844. nghttp2_stream *stream;
  2845. nghttp2_frame *frame;
  2846. int pause;
  2847. DEBUGF("send: no copy DATA\n");
  2848. frame = &aob->item->frame;
  2849. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  2850. if (stream == NULL) {
  2851. DEBUGF("send: no copy DATA cancelled because stream was closed\n");
  2852. active_outbound_item_reset(aob, mem);
  2853. break;
  2854. }
  2855. rv = session_call_send_data(session, aob->item, framebufs);
  2856. if (nghttp2_is_fatal(rv)) {
  2857. return rv;
  2858. }
  2859. if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  2860. session_detach_stream_item(session, stream);
  2861. rv = nghttp2_session_add_rst_stream(session, frame->hd.stream_id,
  2862. NGHTTP2_INTERNAL_ERROR);
  2863. if (nghttp2_is_fatal(rv)) {
  2864. return rv;
  2865. }
  2866. active_outbound_item_reset(aob, mem);
  2867. break;
  2868. }
  2869. if (rv == NGHTTP2_ERR_WOULDBLOCK) {
  2870. return 0;
  2871. }
  2872. pause = (rv == NGHTTP2_ERR_PAUSE);
  2873. rv = session_after_frame_sent1(session);
  2874. if (rv < 0) {
  2875. assert(nghttp2_is_fatal(rv));
  2876. return rv;
  2877. }
  2878. session_after_frame_sent2(session);
  2879. /* We have already adjusted the next state */
  2880. if (pause) {
  2881. return 0;
  2882. }
  2883. break;
  2884. }
  2885. case NGHTTP2_OB_SEND_CLIENT_MAGIC: {
  2886. size_t datalen;
  2887. nghttp2_buf *buf;
  2888. buf = &framebufs->cur->buf;
  2889. if (buf->pos == buf->last) {
  2890. DEBUGF("send: end transmission of client magic\n");
  2891. active_outbound_item_reset(aob, mem);
  2892. break;
  2893. }
  2894. *data_ptr = buf->pos;
  2895. datalen = nghttp2_buf_len(buf);
  2896. buf->pos += datalen;
  2897. return (ssize_t)datalen;
  2898. }
  2899. }
  2900. }
  2901. }
  2902. ssize_t nghttp2_session_mem_send(nghttp2_session *session,
  2903. const uint8_t **data_ptr) {
  2904. int rv;
  2905. ssize_t len;
  2906. *data_ptr = NULL;
  2907. len = nghttp2_session_mem_send_internal(session, data_ptr, 1);
  2908. if (len <= 0) {
  2909. return len;
  2910. }
  2911. if (session->aob.item) {
  2912. /* We have to call session_after_frame_sent1 here to handle stream
  2913. closure upon transmission of frames. Otherwise, END_STREAM may
  2914. be reached to client before we call nghttp2_session_mem_send
  2915. again and we may get exceeding number of incoming streams. */
  2916. rv = session_after_frame_sent1(session);
  2917. if (rv < 0) {
  2918. assert(nghttp2_is_fatal(rv));
  2919. return (ssize_t)rv;
  2920. }
  2921. }
  2922. return len;
  2923. }
  2924. int nghttp2_session_send(nghttp2_session *session) {
  2925. const uint8_t *data = NULL;
  2926. ssize_t datalen;
  2927. ssize_t sentlen;
  2928. nghttp2_bufs *framebufs;
  2929. framebufs = &session->aob.framebufs;
  2930. for (;;) {
  2931. datalen = nghttp2_session_mem_send_internal(session, &data, 0);
  2932. if (datalen <= 0) {
  2933. return (int)datalen;
  2934. }
  2935. sentlen = session->callbacks.send_callback(session, data, (size_t)datalen,
  2936. 0, session->user_data);
  2937. if (sentlen < 0) {
  2938. if (sentlen == NGHTTP2_ERR_WOULDBLOCK) {
  2939. /* Transmission canceled. Rewind the offset */
  2940. framebufs->cur->buf.pos -= datalen;
  2941. return 0;
  2942. }
  2943. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2944. }
  2945. /* Rewind the offset to the amount of unsent bytes */
  2946. framebufs->cur->buf.pos -= datalen - sentlen;
  2947. }
  2948. }
  2949. static ssize_t session_recv(nghttp2_session *session, uint8_t *buf,
  2950. size_t len) {
  2951. ssize_t rv;
  2952. rv = session->callbacks.recv_callback(session, buf, len, 0,
  2953. session->user_data);
  2954. if (rv > 0) {
  2955. if ((size_t)rv > len) {
  2956. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2957. }
  2958. } else if (rv < 0 && rv != NGHTTP2_ERR_WOULDBLOCK && rv != NGHTTP2_ERR_EOF) {
  2959. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2960. }
  2961. return rv;
  2962. }
  2963. static int session_call_on_begin_frame(nghttp2_session *session,
  2964. const nghttp2_frame_hd *hd) {
  2965. int rv;
  2966. if (session->callbacks.on_begin_frame_callback) {
  2967. rv = session->callbacks.on_begin_frame_callback(session, hd,
  2968. session->user_data);
  2969. if (rv != 0) {
  2970. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2971. }
  2972. }
  2973. return 0;
  2974. }
  2975. static int session_call_on_frame_received(nghttp2_session *session,
  2976. nghttp2_frame *frame) {
  2977. int rv;
  2978. if (session->callbacks.on_frame_recv_callback) {
  2979. rv = session->callbacks.on_frame_recv_callback(session, frame,
  2980. session->user_data);
  2981. if (rv != 0) {
  2982. return NGHTTP2_ERR_CALLBACK_FAILURE;
  2983. }
  2984. }
  2985. return 0;
  2986. }
  2987. static int session_call_on_begin_headers(nghttp2_session *session,
  2988. nghttp2_frame *frame) {
  2989. int rv;
  2990. DEBUGF("recv: call on_begin_headers callback stream_id=%d\n",
  2991. frame->hd.stream_id);
  2992. if (session->callbacks.on_begin_headers_callback) {
  2993. rv = session->callbacks.on_begin_headers_callback(session, frame,
  2994. session->user_data);
  2995. if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  2996. return rv;
  2997. }
  2998. if (rv != 0) {
  2999. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3000. }
  3001. }
  3002. return 0;
  3003. }
  3004. static int session_call_on_header(nghttp2_session *session,
  3005. const nghttp2_frame *frame,
  3006. const nghttp2_hd_nv *nv) {
  3007. int rv = 0;
  3008. if (session->callbacks.on_header_callback2) {
  3009. rv = session->callbacks.on_header_callback2(
  3010. session, frame, nv->name, nv->value, nv->flags, session->user_data);
  3011. } else if (session->callbacks.on_header_callback) {
  3012. rv = session->callbacks.on_header_callback(
  3013. session, frame, nv->name->base, nv->name->len, nv->value->base,
  3014. nv->value->len, nv->flags, session->user_data);
  3015. }
  3016. if (rv == NGHTTP2_ERR_PAUSE || rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  3017. return rv;
  3018. }
  3019. if (rv != 0) {
  3020. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3021. }
  3022. return 0;
  3023. }
  3024. static int session_call_on_invalid_header(nghttp2_session *session,
  3025. const nghttp2_frame *frame,
  3026. const nghttp2_hd_nv *nv) {
  3027. int rv;
  3028. if (session->callbacks.on_invalid_header_callback2) {
  3029. rv = session->callbacks.on_invalid_header_callback2(
  3030. session, frame, nv->name, nv->value, nv->flags, session->user_data);
  3031. } else if (session->callbacks.on_invalid_header_callback) {
  3032. rv = session->callbacks.on_invalid_header_callback(
  3033. session, frame, nv->name->base, nv->name->len, nv->value->base,
  3034. nv->value->len, nv->flags, session->user_data);
  3035. } else {
  3036. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  3037. }
  3038. if (rv == NGHTTP2_ERR_PAUSE || rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  3039. return rv;
  3040. }
  3041. if (rv != 0) {
  3042. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3043. }
  3044. return 0;
  3045. }
  3046. static int
  3047. session_call_on_extension_chunk_recv_callback(nghttp2_session *session,
  3048. const uint8_t *data, size_t len) {
  3049. int rv;
  3050. nghttp2_inbound_frame *iframe = &session->iframe;
  3051. nghttp2_frame *frame = &iframe->frame;
  3052. if (session->callbacks.on_extension_chunk_recv_callback) {
  3053. rv = session->callbacks.on_extension_chunk_recv_callback(
  3054. session, &frame->hd, data, len, session->user_data);
  3055. if (rv == NGHTTP2_ERR_CANCEL) {
  3056. return rv;
  3057. }
  3058. if (rv != 0) {
  3059. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3060. }
  3061. }
  3062. return 0;
  3063. }
  3064. static int session_call_unpack_extension_callback(nghttp2_session *session) {
  3065. int rv;
  3066. nghttp2_inbound_frame *iframe = &session->iframe;
  3067. nghttp2_frame *frame = &iframe->frame;
  3068. void *payload = NULL;
  3069. rv = session->callbacks.unpack_extension_callback(
  3070. session, &payload, &frame->hd, session->user_data);
  3071. if (rv == NGHTTP2_ERR_CANCEL) {
  3072. return rv;
  3073. }
  3074. if (rv != 0) {
  3075. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3076. }
  3077. frame->ext.payload = payload;
  3078. return 0;
  3079. }
  3080. /*
  3081. * Handles frame size error.
  3082. *
  3083. * This function returns 0 if it succeeds, or one of the following
  3084. * negative error codes:
  3085. *
  3086. * NGHTTP2_ERR_NOMEM
  3087. * Out of memory.
  3088. */
  3089. static int session_handle_frame_size_error(nghttp2_session *session) {
  3090. /* TODO Currently no callback is called for this error, because we
  3091. call this callback before reading any payload */
  3092. return nghttp2_session_terminate_session(session, NGHTTP2_FRAME_SIZE_ERROR);
  3093. }
  3094. static uint32_t get_error_code_from_lib_error_code(int lib_error_code) {
  3095. switch (lib_error_code) {
  3096. case NGHTTP2_ERR_STREAM_CLOSED:
  3097. return NGHTTP2_STREAM_CLOSED;
  3098. case NGHTTP2_ERR_HEADER_COMP:
  3099. return NGHTTP2_COMPRESSION_ERROR;
  3100. case NGHTTP2_ERR_FRAME_SIZE_ERROR:
  3101. return NGHTTP2_FRAME_SIZE_ERROR;
  3102. case NGHTTP2_ERR_FLOW_CONTROL:
  3103. return NGHTTP2_FLOW_CONTROL_ERROR;
  3104. case NGHTTP2_ERR_REFUSED_STREAM:
  3105. return NGHTTP2_REFUSED_STREAM;
  3106. case NGHTTP2_ERR_PROTO:
  3107. case NGHTTP2_ERR_HTTP_HEADER:
  3108. case NGHTTP2_ERR_HTTP_MESSAGING:
  3109. return NGHTTP2_PROTOCOL_ERROR;
  3110. default:
  3111. return NGHTTP2_INTERNAL_ERROR;
  3112. }
  3113. }
  3114. /*
  3115. * Calls on_invalid_frame_recv_callback if it is set to |session|.
  3116. *
  3117. * This function returns 0 if it succeeds, or one of the following
  3118. * negative error codes:
  3119. *
  3120. * NGHTTP2_ERR_CALLBACK_FAILURE
  3121. * User defined callback function fails.
  3122. */
  3123. static int session_call_on_invalid_frame_recv_callback(nghttp2_session *session,
  3124. nghttp2_frame *frame,
  3125. int lib_error_code) {
  3126. if (session->callbacks.on_invalid_frame_recv_callback) {
  3127. if (session->callbacks.on_invalid_frame_recv_callback(
  3128. session, frame, lib_error_code, session->user_data) != 0) {
  3129. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3130. }
  3131. }
  3132. return 0;
  3133. }
  3134. static int session_handle_invalid_stream2(nghttp2_session *session,
  3135. int32_t stream_id,
  3136. nghttp2_frame *frame,
  3137. int lib_error_code) {
  3138. int rv;
  3139. rv = nghttp2_session_add_rst_stream(
  3140. session, stream_id, get_error_code_from_lib_error_code(lib_error_code));
  3141. if (rv != 0) {
  3142. return rv;
  3143. }
  3144. if (session->callbacks.on_invalid_frame_recv_callback) {
  3145. if (session->callbacks.on_invalid_frame_recv_callback(
  3146. session, frame, lib_error_code, session->user_data) != 0) {
  3147. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3148. }
  3149. }
  3150. return 0;
  3151. }
  3152. static int session_handle_invalid_stream(nghttp2_session *session,
  3153. nghttp2_frame *frame,
  3154. int lib_error_code) {
  3155. return session_handle_invalid_stream2(session, frame->hd.stream_id, frame,
  3156. lib_error_code);
  3157. }
  3158. static int session_inflate_handle_invalid_stream(nghttp2_session *session,
  3159. nghttp2_frame *frame,
  3160. int lib_error_code) {
  3161. int rv;
  3162. rv = session_handle_invalid_stream(session, frame, lib_error_code);
  3163. if (nghttp2_is_fatal(rv)) {
  3164. return rv;
  3165. }
  3166. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3167. }
  3168. /*
  3169. * Handles invalid frame which causes connection error.
  3170. */
  3171. static int session_handle_invalid_connection(nghttp2_session *session,
  3172. nghttp2_frame *frame,
  3173. int lib_error_code,
  3174. const char *reason) {
  3175. if (session->callbacks.on_invalid_frame_recv_callback) {
  3176. if (session->callbacks.on_invalid_frame_recv_callback(
  3177. session, frame, lib_error_code, session->user_data) != 0) {
  3178. return NGHTTP2_ERR_CALLBACK_FAILURE;
  3179. }
  3180. }
  3181. return nghttp2_session_terminate_session_with_reason(
  3182. session, get_error_code_from_lib_error_code(lib_error_code), reason);
  3183. }
  3184. static int session_inflate_handle_invalid_connection(nghttp2_session *session,
  3185. nghttp2_frame *frame,
  3186. int lib_error_code,
  3187. const char *reason) {
  3188. int rv;
  3189. rv =
  3190. session_handle_invalid_connection(session, frame, lib_error_code, reason);
  3191. if (nghttp2_is_fatal(rv)) {
  3192. return rv;
  3193. }
  3194. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3195. }
  3196. /*
  3197. * Inflates header block in the memory pointed by |in| with |inlen|
  3198. * bytes. If this function returns NGHTTP2_ERR_PAUSE, the caller must
  3199. * call this function again, until it returns 0 or one of negative
  3200. * error code. If |call_header_cb| is zero, the on_header_callback
  3201. * are not invoked and the function never return NGHTTP2_ERR_PAUSE. If
  3202. * the given |in| is the last chunk of header block, the |final| must
  3203. * be nonzero. If header block is successfully processed (which is
  3204. * indicated by the return value 0, NGHTTP2_ERR_PAUSE or
  3205. * NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE), the number of processed
  3206. * input bytes is assigned to the |*readlen_ptr|.
  3207. *
  3208. * This function return 0 if it succeeds, or one of the negative error
  3209. * codes:
  3210. *
  3211. * NGHTTP2_ERR_CALLBACK_FAILURE
  3212. * The callback function failed.
  3213. * NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE
  3214. * The callback returns this error code, indicating that this
  3215. * stream should be RST_STREAMed.
  3216. * NGHTTP2_ERR_NOMEM
  3217. * Out of memory.
  3218. * NGHTTP2_ERR_PAUSE
  3219. * The callback function returned NGHTTP2_ERR_PAUSE
  3220. * NGHTTP2_ERR_HEADER_COMP
  3221. * Header decompression failed
  3222. */
  3223. static int inflate_header_block(nghttp2_session *session, nghttp2_frame *frame,
  3224. size_t *readlen_ptr, uint8_t *in, size_t inlen,
  3225. int final, int call_header_cb) {
  3226. ssize_t proclen;
  3227. int rv;
  3228. int inflate_flags;
  3229. nghttp2_hd_nv nv;
  3230. nghttp2_stream *stream;
  3231. nghttp2_stream *subject_stream;
  3232. int trailer = 0;
  3233. *readlen_ptr = 0;
  3234. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  3235. if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  3236. subject_stream = nghttp2_session_get_stream(
  3237. session, frame->push_promise.promised_stream_id);
  3238. } else {
  3239. subject_stream = stream;
  3240. trailer = session_trailer_headers(session, stream, frame);
  3241. }
  3242. DEBUGF("recv: decoding header block %zu bytes\n", inlen);
  3243. for (;;) {
  3244. inflate_flags = 0;
  3245. proclen = nghttp2_hd_inflate_hd_nv(&session->hd_inflater, &nv,
  3246. &inflate_flags, in, inlen, final);
  3247. if (nghttp2_is_fatal((int)proclen)) {
  3248. return (int)proclen;
  3249. }
  3250. if (proclen < 0) {
  3251. if (session->iframe.state == NGHTTP2_IB_READ_HEADER_BLOCK) {
  3252. if (subject_stream && subject_stream->state != NGHTTP2_STREAM_CLOSING) {
  3253. /* Adding RST_STREAM here is very important. It prevents
  3254. from invoking subsequent callbacks for the same stream
  3255. ID. */
  3256. rv = nghttp2_session_add_rst_stream(
  3257. session, subject_stream->stream_id, NGHTTP2_COMPRESSION_ERROR);
  3258. if (nghttp2_is_fatal(rv)) {
  3259. return rv;
  3260. }
  3261. }
  3262. }
  3263. rv =
  3264. nghttp2_session_terminate_session(session, NGHTTP2_COMPRESSION_ERROR);
  3265. if (nghttp2_is_fatal(rv)) {
  3266. return rv;
  3267. }
  3268. return NGHTTP2_ERR_HEADER_COMP;
  3269. }
  3270. in += proclen;
  3271. inlen -= (size_t)proclen;
  3272. *readlen_ptr += (size_t)proclen;
  3273. DEBUGF("recv: proclen=%zd\n", proclen);
  3274. if (call_header_cb && (inflate_flags & NGHTTP2_HD_INFLATE_EMIT)) {
  3275. rv = 0;
  3276. if (subject_stream) {
  3277. if (session_enforce_http_messaging(session)) {
  3278. rv = nghttp2_http_on_header(session, subject_stream, frame, &nv,
  3279. trailer);
  3280. if (rv == NGHTTP2_ERR_IGN_HTTP_HEADER) {
  3281. /* Don't overwrite rv here */
  3282. int rv2;
  3283. rv2 = session_call_on_invalid_header(session, frame, &nv);
  3284. if (rv2 == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  3285. rv = NGHTTP2_ERR_HTTP_HEADER;
  3286. } else {
  3287. if (rv2 != 0) {
  3288. return rv2;
  3289. }
  3290. /* header is ignored */
  3291. DEBUGF("recv: HTTP ignored: type=%u, id=%d, header %.*s: %.*s\n",
  3292. frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
  3293. nv.name->base, (int)nv.value->len, nv.value->base);
  3294. rv2 = session_call_error_callback(
  3295. session, NGHTTP2_ERR_HTTP_HEADER,
  3296. "Ignoring received invalid HTTP header field: frame type: "
  3297. "%u, stream: %d, name: [%.*s], value: [%.*s]",
  3298. frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
  3299. nv.name->base, (int)nv.value->len, nv.value->base);
  3300. if (nghttp2_is_fatal(rv2)) {
  3301. return rv2;
  3302. }
  3303. }
  3304. }
  3305. if (rv == NGHTTP2_ERR_HTTP_HEADER) {
  3306. DEBUGF("recv: HTTP error: type=%u, id=%d, header %.*s: %.*s\n",
  3307. frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
  3308. nv.name->base, (int)nv.value->len, nv.value->base);
  3309. rv = session_call_error_callback(
  3310. session, NGHTTP2_ERR_HTTP_HEADER,
  3311. "Invalid HTTP header field was received: frame type: "
  3312. "%u, stream: %d, name: [%.*s], value: [%.*s]",
  3313. frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
  3314. nv.name->base, (int)nv.value->len, nv.value->base);
  3315. if (nghttp2_is_fatal(rv)) {
  3316. return rv;
  3317. }
  3318. rv = session_handle_invalid_stream2(session,
  3319. subject_stream->stream_id,
  3320. frame, NGHTTP2_ERR_HTTP_HEADER);
  3321. if (nghttp2_is_fatal(rv)) {
  3322. return rv;
  3323. }
  3324. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  3325. }
  3326. }
  3327. if (rv == 0) {
  3328. rv = session_call_on_header(session, frame, &nv);
  3329. /* This handles NGHTTP2_ERR_PAUSE and
  3330. NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE as well */
  3331. if (rv != 0) {
  3332. return rv;
  3333. }
  3334. }
  3335. }
  3336. }
  3337. if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
  3338. nghttp2_hd_inflate_end_headers(&session->hd_inflater);
  3339. break;
  3340. }
  3341. if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
  3342. break;
  3343. }
  3344. }
  3345. return 0;
  3346. }
  3347. /*
  3348. * Call this function when HEADERS frame was completely received.
  3349. *
  3350. * This function returns 0 if it succeeds, or one of negative error
  3351. * codes:
  3352. *
  3353. * NGHTTP2_ERR_CALLBACK_FAILURE
  3354. * The callback function failed.
  3355. * NGHTTP2_ERR_NOMEM
  3356. * Out of memory.
  3357. */
  3358. static int session_end_stream_headers_received(nghttp2_session *session,
  3359. nghttp2_frame *frame,
  3360. nghttp2_stream *stream) {
  3361. int rv;
  3362. assert(frame->hd.type == NGHTTP2_HEADERS);
  3363. if (session->server && session_enforce_http_messaging(session) &&
  3364. frame->headers.cat == NGHTTP2_HCAT_REQUEST &&
  3365. (stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) &&
  3366. !(stream->flags & NGHTTP2_STREAM_FLAG_IGNORE_CLIENT_PRIORITIES) &&
  3367. (stream->http_flags & NGHTTP2_HTTP_FLAG_PRIORITY)) {
  3368. rv = session_update_stream_priority(session, stream, stream->http_extpri);
  3369. if (rv != 0) {
  3370. assert(nghttp2_is_fatal(rv));
  3371. return rv;
  3372. }
  3373. }
  3374. if ((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
  3375. return 0;
  3376. }
  3377. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
  3378. rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
  3379. if (nghttp2_is_fatal(rv)) {
  3380. return rv;
  3381. }
  3382. return 0;
  3383. }
  3384. static int session_after_header_block_received(nghttp2_session *session) {
  3385. int rv = 0;
  3386. nghttp2_frame *frame = &session->iframe.frame;
  3387. nghttp2_stream *stream;
  3388. /* We don't call on_frame_recv_callback if stream has been closed
  3389. already or being closed. */
  3390. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  3391. if (!stream || stream->state == NGHTTP2_STREAM_CLOSING) {
  3392. return 0;
  3393. }
  3394. if (session_enforce_http_messaging(session)) {
  3395. if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  3396. nghttp2_stream *subject_stream;
  3397. subject_stream = nghttp2_session_get_stream(
  3398. session, frame->push_promise.promised_stream_id);
  3399. if (subject_stream) {
  3400. rv = nghttp2_http_on_request_headers(subject_stream, frame);
  3401. }
  3402. } else {
  3403. assert(frame->hd.type == NGHTTP2_HEADERS);
  3404. switch (frame->headers.cat) {
  3405. case NGHTTP2_HCAT_REQUEST:
  3406. rv = nghttp2_http_on_request_headers(stream, frame);
  3407. break;
  3408. case NGHTTP2_HCAT_RESPONSE:
  3409. case NGHTTP2_HCAT_PUSH_RESPONSE:
  3410. rv = nghttp2_http_on_response_headers(stream);
  3411. break;
  3412. case NGHTTP2_HCAT_HEADERS:
  3413. if (stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) {
  3414. assert(!session->server);
  3415. rv = nghttp2_http_on_response_headers(stream);
  3416. } else {
  3417. rv = nghttp2_http_on_trailer_headers(stream, frame);
  3418. }
  3419. break;
  3420. default:
  3421. assert(0);
  3422. }
  3423. if (rv == 0 && (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
  3424. rv = nghttp2_http_on_remote_end_stream(stream);
  3425. }
  3426. }
  3427. if (rv != 0) {
  3428. int32_t stream_id;
  3429. if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  3430. stream_id = frame->push_promise.promised_stream_id;
  3431. } else {
  3432. stream_id = frame->hd.stream_id;
  3433. }
  3434. rv = session_handle_invalid_stream2(session, stream_id, frame,
  3435. NGHTTP2_ERR_HTTP_MESSAGING);
  3436. if (nghttp2_is_fatal(rv)) {
  3437. return rv;
  3438. }
  3439. if (frame->hd.type == NGHTTP2_HEADERS &&
  3440. (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
  3441. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
  3442. /* Don't call nghttp2_session_close_stream_if_shut_rdwr
  3443. because RST_STREAM has been submitted. */
  3444. }
  3445. return 0;
  3446. }
  3447. }
  3448. rv = session_call_on_frame_received(session, frame);
  3449. if (nghttp2_is_fatal(rv)) {
  3450. return rv;
  3451. }
  3452. if (frame->hd.type != NGHTTP2_HEADERS) {
  3453. return 0;
  3454. }
  3455. return session_end_stream_headers_received(session, frame, stream);
  3456. }
  3457. int nghttp2_session_on_request_headers_received(nghttp2_session *session,
  3458. nghttp2_frame *frame) {
  3459. int rv = 0;
  3460. nghttp2_stream *stream;
  3461. if (frame->hd.stream_id == 0) {
  3462. return session_inflate_handle_invalid_connection(
  3463. session, frame, NGHTTP2_ERR_PROTO, "request HEADERS: stream_id == 0");
  3464. }
  3465. /* If client receives idle stream from server, it is invalid
  3466. regardless stream ID is even or odd. This is because client is
  3467. not expected to receive request from server. */
  3468. if (!session->server) {
  3469. if (session_detect_idle_stream(session, frame->hd.stream_id)) {
  3470. return session_inflate_handle_invalid_connection(
  3471. session, frame, NGHTTP2_ERR_PROTO,
  3472. "request HEADERS: client received request");
  3473. }
  3474. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3475. }
  3476. assert(session->server);
  3477. if (!session_is_new_peer_stream_id(session, frame->hd.stream_id)) {
  3478. if (frame->hd.stream_id == 0 ||
  3479. nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
  3480. return session_inflate_handle_invalid_connection(
  3481. session, frame, NGHTTP2_ERR_PROTO,
  3482. "request HEADERS: invalid stream_id");
  3483. }
  3484. /* RFC 7540 says if an endpoint receives a HEADERS with invalid
  3485. * stream ID (e.g, numerically smaller than previous), it MUST
  3486. * issue connection error with error code PROTOCOL_ERROR. It is a
  3487. * bit hard to detect this, since we cannot remember all streams
  3488. * we observed so far.
  3489. *
  3490. * You might imagine this is really easy. But no. HTTP/2 is
  3491. * asynchronous protocol, and usually client and server do not
  3492. * share the complete picture of open/closed stream status. For
  3493. * example, after server sends RST_STREAM for a stream, client may
  3494. * send trailer HEADERS for that stream. If naive server detects
  3495. * that, and issued connection error, then it is a bug of server
  3496. * implementation since client is not wrong if it did not get
  3497. * RST_STREAM when it issued trailer HEADERS.
  3498. *
  3499. * At the moment, we are very conservative here. We only use
  3500. * connection error if stream ID refers idle stream, or we are
  3501. * sure that stream is half-closed(remote) or closed. Otherwise
  3502. * we just ignore HEADERS for now.
  3503. */
  3504. stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
  3505. if (stream && (stream->shut_flags & NGHTTP2_SHUT_RD)) {
  3506. return session_inflate_handle_invalid_connection(
  3507. session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
  3508. }
  3509. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3510. }
  3511. session->last_recv_stream_id = frame->hd.stream_id;
  3512. if (session_is_incoming_concurrent_streams_max(session)) {
  3513. return session_inflate_handle_invalid_connection(
  3514. session, frame, NGHTTP2_ERR_PROTO,
  3515. "request HEADERS: max concurrent streams exceeded");
  3516. }
  3517. if (!session_allow_incoming_new_stream(session)) {
  3518. /* We just ignore stream after GOAWAY was sent */
  3519. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3520. }
  3521. if (frame->headers.pri_spec.stream_id == frame->hd.stream_id) {
  3522. return session_inflate_handle_invalid_connection(
  3523. session, frame, NGHTTP2_ERR_PROTO, "request HEADERS: depend on itself");
  3524. }
  3525. if (session_is_incoming_concurrent_streams_pending_max(session)) {
  3526. return session_inflate_handle_invalid_stream(session, frame,
  3527. NGHTTP2_ERR_REFUSED_STREAM);
  3528. }
  3529. stream = nghttp2_session_open_stream(
  3530. session, frame->hd.stream_id, NGHTTP2_STREAM_FLAG_NONE,
  3531. &frame->headers.pri_spec, NGHTTP2_STREAM_OPENING, NULL);
  3532. if (!stream) {
  3533. return NGHTTP2_ERR_NOMEM;
  3534. }
  3535. rv = nghttp2_session_adjust_closed_stream(session);
  3536. if (nghttp2_is_fatal(rv)) {
  3537. return rv;
  3538. }
  3539. session->last_proc_stream_id = session->last_recv_stream_id;
  3540. rv = session_call_on_begin_headers(session, frame);
  3541. if (rv != 0) {
  3542. return rv;
  3543. }
  3544. return 0;
  3545. }
  3546. int nghttp2_session_on_response_headers_received(nghttp2_session *session,
  3547. nghttp2_frame *frame,
  3548. nghttp2_stream *stream) {
  3549. int rv;
  3550. /* This function is only called if stream->state ==
  3551. NGHTTP2_STREAM_OPENING and stream_id is local side initiated. */
  3552. assert(stream->state == NGHTTP2_STREAM_OPENING &&
  3553. nghttp2_session_is_my_stream_id(session, frame->hd.stream_id));
  3554. if (frame->hd.stream_id == 0) {
  3555. return session_inflate_handle_invalid_connection(
  3556. session, frame, NGHTTP2_ERR_PROTO, "response HEADERS: stream_id == 0");
  3557. }
  3558. if (stream->shut_flags & NGHTTP2_SHUT_RD) {
  3559. /* half closed (remote): from the spec:
  3560. If an endpoint receives additional frames for a stream that is
  3561. in this state it MUST respond with a stream error (Section
  3562. 5.4.2) of type STREAM_CLOSED.
  3563. We go further, and make it connection error.
  3564. */
  3565. return session_inflate_handle_invalid_connection(
  3566. session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
  3567. }
  3568. stream->state = NGHTTP2_STREAM_OPENED;
  3569. rv = session_call_on_begin_headers(session, frame);
  3570. if (rv != 0) {
  3571. return rv;
  3572. }
  3573. return 0;
  3574. }
  3575. int nghttp2_session_on_push_response_headers_received(nghttp2_session *session,
  3576. nghttp2_frame *frame,
  3577. nghttp2_stream *stream) {
  3578. int rv = 0;
  3579. assert(stream->state == NGHTTP2_STREAM_RESERVED);
  3580. if (frame->hd.stream_id == 0) {
  3581. return session_inflate_handle_invalid_connection(
  3582. session, frame, NGHTTP2_ERR_PROTO,
  3583. "push response HEADERS: stream_id == 0");
  3584. }
  3585. if (session->server) {
  3586. return session_inflate_handle_invalid_connection(
  3587. session, frame, NGHTTP2_ERR_PROTO,
  3588. "HEADERS: no HEADERS allowed from client in reserved state");
  3589. }
  3590. if (session_is_incoming_concurrent_streams_max(session)) {
  3591. return session_inflate_handle_invalid_connection(
  3592. session, frame, NGHTTP2_ERR_PROTO,
  3593. "push response HEADERS: max concurrent streams exceeded");
  3594. }
  3595. if (!session_allow_incoming_new_stream(session)) {
  3596. /* We don't accept new stream after GOAWAY was sent. */
  3597. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3598. }
  3599. if (session_is_incoming_concurrent_streams_pending_max(session)) {
  3600. return session_inflate_handle_invalid_stream(session, frame,
  3601. NGHTTP2_ERR_REFUSED_STREAM);
  3602. }
  3603. nghttp2_stream_promise_fulfilled(stream);
  3604. if (!nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
  3605. --session->num_incoming_reserved_streams;
  3606. }
  3607. ++session->num_incoming_streams;
  3608. rv = session_call_on_begin_headers(session, frame);
  3609. if (rv != 0) {
  3610. return rv;
  3611. }
  3612. return 0;
  3613. }
  3614. int nghttp2_session_on_headers_received(nghttp2_session *session,
  3615. nghttp2_frame *frame,
  3616. nghttp2_stream *stream) {
  3617. int rv = 0;
  3618. if (frame->hd.stream_id == 0) {
  3619. return session_inflate_handle_invalid_connection(
  3620. session, frame, NGHTTP2_ERR_PROTO, "HEADERS: stream_id == 0");
  3621. }
  3622. if ((stream->shut_flags & NGHTTP2_SHUT_RD)) {
  3623. /* half closed (remote): from the spec:
  3624. If an endpoint receives additional frames for a stream that is
  3625. in this state it MUST respond with a stream error (Section
  3626. 5.4.2) of type STREAM_CLOSED.
  3627. we go further, and make it connection error.
  3628. */
  3629. return session_inflate_handle_invalid_connection(
  3630. session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
  3631. }
  3632. if (nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
  3633. if (stream->state == NGHTTP2_STREAM_OPENED) {
  3634. rv = session_call_on_begin_headers(session, frame);
  3635. if (rv != 0) {
  3636. return rv;
  3637. }
  3638. return 0;
  3639. }
  3640. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3641. }
  3642. /* If this is remote peer initiated stream, it is OK unless it
  3643. has sent END_STREAM frame already. But if stream is in
  3644. NGHTTP2_STREAM_CLOSING, we discard the frame. This is a race
  3645. condition. */
  3646. if (stream->state != NGHTTP2_STREAM_CLOSING) {
  3647. rv = session_call_on_begin_headers(session, frame);
  3648. if (rv != 0) {
  3649. return rv;
  3650. }
  3651. return 0;
  3652. }
  3653. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  3654. }
  3655. static int session_process_headers_frame(nghttp2_session *session) {
  3656. nghttp2_inbound_frame *iframe = &session->iframe;
  3657. nghttp2_frame *frame = &iframe->frame;
  3658. nghttp2_stream *stream;
  3659. nghttp2_frame_unpack_headers_payload(&frame->headers, iframe->sbuf.pos);
  3660. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  3661. if (!stream) {
  3662. frame->headers.cat = NGHTTP2_HCAT_REQUEST;
  3663. return nghttp2_session_on_request_headers_received(session, frame);
  3664. }
  3665. if (stream->state == NGHTTP2_STREAM_RESERVED) {
  3666. frame->headers.cat = NGHTTP2_HCAT_PUSH_RESPONSE;
  3667. return nghttp2_session_on_push_response_headers_received(session, frame,
  3668. stream);
  3669. }
  3670. if (stream->state == NGHTTP2_STREAM_OPENING &&
  3671. nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
  3672. frame->headers.cat = NGHTTP2_HCAT_RESPONSE;
  3673. return nghttp2_session_on_response_headers_received(session, frame, stream);
  3674. }
  3675. frame->headers.cat = NGHTTP2_HCAT_HEADERS;
  3676. return nghttp2_session_on_headers_received(session, frame, stream);
  3677. }
  3678. int nghttp2_session_on_priority_received(nghttp2_session *session,
  3679. nghttp2_frame *frame) {
  3680. int rv;
  3681. nghttp2_stream *stream;
  3682. assert(!session_no_rfc7540_pri_no_fallback(session));
  3683. if (frame->hd.stream_id == 0) {
  3684. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  3685. "PRIORITY: stream_id == 0");
  3686. }
  3687. if (frame->priority.pri_spec.stream_id == frame->hd.stream_id) {
  3688. return nghttp2_session_terminate_session_with_reason(
  3689. session, NGHTTP2_PROTOCOL_ERROR, "depend on itself");
  3690. }
  3691. if (!session->server) {
  3692. /* Re-prioritization works only in server */
  3693. return session_call_on_frame_received(session, frame);
  3694. }
  3695. stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
  3696. if (!stream) {
  3697. /* PRIORITY against idle stream can create anchor node in
  3698. dependency tree. */
  3699. if (!session_detect_idle_stream(session, frame->hd.stream_id)) {
  3700. return 0;
  3701. }
  3702. stream = nghttp2_session_open_stream(
  3703. session, frame->hd.stream_id, NGHTTP2_STREAM_FLAG_NONE,
  3704. &frame->priority.pri_spec, NGHTTP2_STREAM_IDLE, NULL);
  3705. if (stream == NULL) {
  3706. return NGHTTP2_ERR_NOMEM;
  3707. }
  3708. rv = nghttp2_session_adjust_idle_stream(session);
  3709. if (nghttp2_is_fatal(rv)) {
  3710. return rv;
  3711. }
  3712. } else {
  3713. rv = nghttp2_session_reprioritize_stream(session, stream,
  3714. &frame->priority.pri_spec);
  3715. if (nghttp2_is_fatal(rv)) {
  3716. return rv;
  3717. }
  3718. rv = nghttp2_session_adjust_idle_stream(session);
  3719. if (nghttp2_is_fatal(rv)) {
  3720. return rv;
  3721. }
  3722. }
  3723. return session_call_on_frame_received(session, frame);
  3724. }
  3725. static int session_process_priority_frame(nghttp2_session *session) {
  3726. nghttp2_inbound_frame *iframe = &session->iframe;
  3727. nghttp2_frame *frame = &iframe->frame;
  3728. assert(!session_no_rfc7540_pri_no_fallback(session));
  3729. nghttp2_frame_unpack_priority_payload(&frame->priority, iframe->sbuf.pos);
  3730. return nghttp2_session_on_priority_received(session, frame);
  3731. }
  3732. static int session_update_stream_reset_ratelim(nghttp2_session *session) {
  3733. if (!session->server || (session->goaway_flags & NGHTTP2_GOAWAY_SUBMITTED)) {
  3734. return 0;
  3735. }
  3736. nghttp2_ratelim_update(&session->stream_reset_ratelim,
  3737. nghttp2_time_now_sec());
  3738. if (nghttp2_ratelim_drain(&session->stream_reset_ratelim, 1) == 0) {
  3739. return 0;
  3740. }
  3741. return nghttp2_session_add_goaway(session, session->last_recv_stream_id,
  3742. NGHTTP2_INTERNAL_ERROR, NULL, 0,
  3743. NGHTTP2_GOAWAY_AUX_NONE);
  3744. }
  3745. int nghttp2_session_on_rst_stream_received(nghttp2_session *session,
  3746. nghttp2_frame *frame) {
  3747. int rv;
  3748. nghttp2_stream *stream;
  3749. if (frame->hd.stream_id == 0) {
  3750. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  3751. "RST_STREAM: stream_id == 0");
  3752. }
  3753. if (session_detect_idle_stream(session, frame->hd.stream_id)) {
  3754. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  3755. "RST_STREAM: stream in idle");
  3756. }
  3757. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  3758. if (stream) {
  3759. /* We may use stream->shut_flags for strict error checking. */
  3760. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
  3761. }
  3762. rv = session_call_on_frame_received(session, frame);
  3763. if (rv != 0) {
  3764. return rv;
  3765. }
  3766. rv = nghttp2_session_close_stream(session, frame->hd.stream_id,
  3767. frame->rst_stream.error_code);
  3768. if (nghttp2_is_fatal(rv)) {
  3769. return rv;
  3770. }
  3771. return session_update_stream_reset_ratelim(session);
  3772. }
  3773. static int session_process_rst_stream_frame(nghttp2_session *session) {
  3774. nghttp2_inbound_frame *iframe = &session->iframe;
  3775. nghttp2_frame *frame = &iframe->frame;
  3776. nghttp2_frame_unpack_rst_stream_payload(&frame->rst_stream, iframe->sbuf.pos);
  3777. return nghttp2_session_on_rst_stream_received(session, frame);
  3778. }
  3779. static int update_remote_initial_window_size_func(void *entry, void *ptr) {
  3780. int rv;
  3781. nghttp2_update_window_size_arg *arg;
  3782. nghttp2_stream *stream;
  3783. arg = (nghttp2_update_window_size_arg *)ptr;
  3784. stream = (nghttp2_stream *)entry;
  3785. rv = nghttp2_stream_update_remote_initial_window_size(
  3786. stream, arg->new_window_size, arg->old_window_size);
  3787. if (rv != 0) {
  3788. return nghttp2_session_add_rst_stream(arg->session, stream->stream_id,
  3789. NGHTTP2_FLOW_CONTROL_ERROR);
  3790. }
  3791. /* If window size gets positive, push deferred DATA frame to
  3792. outbound queue. */
  3793. if (stream->remote_window_size > 0 &&
  3794. nghttp2_stream_check_deferred_by_flow_control(stream)) {
  3795. rv = session_resume_deferred_stream_item(
  3796. arg->session, stream, NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
  3797. if (nghttp2_is_fatal(rv)) {
  3798. return rv;
  3799. }
  3800. }
  3801. return 0;
  3802. }
  3803. /*
  3804. * Updates the remote initial window size of all active streams. If
  3805. * error occurs, all streams may not be updated.
  3806. *
  3807. * This function returns 0 if it succeeds, or one of the following
  3808. * negative error codes:
  3809. *
  3810. * NGHTTP2_ERR_NOMEM
  3811. * Out of memory.
  3812. */
  3813. static int
  3814. session_update_remote_initial_window_size(nghttp2_session *session,
  3815. int32_t new_initial_window_size) {
  3816. nghttp2_update_window_size_arg arg;
  3817. arg.session = session;
  3818. arg.new_window_size = new_initial_window_size;
  3819. arg.old_window_size = (int32_t)session->remote_settings.initial_window_size;
  3820. return nghttp2_map_each(&session->streams,
  3821. update_remote_initial_window_size_func, &arg);
  3822. }
  3823. static int update_local_initial_window_size_func(void *entry, void *ptr) {
  3824. int rv;
  3825. nghttp2_update_window_size_arg *arg;
  3826. nghttp2_stream *stream;
  3827. arg = (nghttp2_update_window_size_arg *)ptr;
  3828. stream = (nghttp2_stream *)entry;
  3829. rv = nghttp2_stream_update_local_initial_window_size(
  3830. stream, arg->new_window_size, arg->old_window_size);
  3831. if (rv != 0) {
  3832. return nghttp2_session_add_rst_stream(arg->session, stream->stream_id,
  3833. NGHTTP2_FLOW_CONTROL_ERROR);
  3834. }
  3835. if (stream->window_update_queued) {
  3836. return 0;
  3837. }
  3838. if (arg->session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
  3839. return session_update_stream_consumed_size(arg->session, stream, 0);
  3840. }
  3841. if (nghttp2_should_send_window_update(stream->local_window_size,
  3842. stream->recv_window_size)) {
  3843. rv = nghttp2_session_add_window_update(arg->session, NGHTTP2_FLAG_NONE,
  3844. stream->stream_id,
  3845. stream->recv_window_size);
  3846. if (rv != 0) {
  3847. return rv;
  3848. }
  3849. stream->recv_window_size = 0;
  3850. }
  3851. return 0;
  3852. }
  3853. /*
  3854. * Updates the local initial window size of all active streams. If
  3855. * error occurs, all streams may not be updated.
  3856. *
  3857. * This function returns 0 if it succeeds, or one of the following
  3858. * negative error codes:
  3859. *
  3860. * NGHTTP2_ERR_NOMEM
  3861. * Out of memory.
  3862. */
  3863. static int
  3864. session_update_local_initial_window_size(nghttp2_session *session,
  3865. int32_t new_initial_window_size,
  3866. int32_t old_initial_window_size) {
  3867. nghttp2_update_window_size_arg arg;
  3868. arg.session = session;
  3869. arg.new_window_size = new_initial_window_size;
  3870. arg.old_window_size = old_initial_window_size;
  3871. return nghttp2_map_each(&session->streams,
  3872. update_local_initial_window_size_func, &arg);
  3873. }
  3874. /*
  3875. * Apply SETTINGS values |iv| having |niv| elements to the local
  3876. * settings. We assumes that all values in |iv| is correct, since we
  3877. * validated them in nghttp2_session_add_settings() already.
  3878. *
  3879. * This function returns 0 if it succeeds, or one of the following
  3880. * negative error codes:
  3881. *
  3882. * NGHTTP2_ERR_HEADER_COMP
  3883. * The header table size is out of range
  3884. * NGHTTP2_ERR_NOMEM
  3885. * Out of memory
  3886. */
  3887. int nghttp2_session_update_local_settings(nghttp2_session *session,
  3888. nghttp2_settings_entry *iv,
  3889. size_t niv) {
  3890. int rv;
  3891. size_t i;
  3892. int32_t new_initial_window_size = -1;
  3893. uint32_t header_table_size = 0;
  3894. uint32_t min_header_table_size = UINT32_MAX;
  3895. uint8_t header_table_size_seen = 0;
  3896. /* For NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, use the value last
  3897. seen. For NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, use both minimum
  3898. value and last seen value. */
  3899. for (i = 0; i < niv; ++i) {
  3900. switch (iv[i].settings_id) {
  3901. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  3902. header_table_size_seen = 1;
  3903. header_table_size = iv[i].value;
  3904. min_header_table_size = nghttp2_min(min_header_table_size, iv[i].value);
  3905. break;
  3906. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  3907. new_initial_window_size = (int32_t)iv[i].value;
  3908. break;
  3909. }
  3910. }
  3911. if (header_table_size_seen) {
  3912. if (min_header_table_size < header_table_size) {
  3913. rv = nghttp2_hd_inflate_change_table_size(&session->hd_inflater,
  3914. min_header_table_size);
  3915. if (rv != 0) {
  3916. return rv;
  3917. }
  3918. }
  3919. rv = nghttp2_hd_inflate_change_table_size(&session->hd_inflater,
  3920. header_table_size);
  3921. if (rv != 0) {
  3922. return rv;
  3923. }
  3924. }
  3925. if (new_initial_window_size != -1) {
  3926. rv = session_update_local_initial_window_size(
  3927. session, new_initial_window_size,
  3928. (int32_t)session->local_settings.initial_window_size);
  3929. if (rv != 0) {
  3930. return rv;
  3931. }
  3932. }
  3933. for (i = 0; i < niv; ++i) {
  3934. switch (iv[i].settings_id) {
  3935. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  3936. session->local_settings.header_table_size = iv[i].value;
  3937. break;
  3938. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  3939. session->local_settings.enable_push = iv[i].value;
  3940. break;
  3941. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  3942. session->local_settings.max_concurrent_streams = iv[i].value;
  3943. break;
  3944. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  3945. session->local_settings.initial_window_size = iv[i].value;
  3946. break;
  3947. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  3948. session->local_settings.max_frame_size = iv[i].value;
  3949. break;
  3950. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  3951. session->local_settings.max_header_list_size = iv[i].value;
  3952. break;
  3953. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  3954. session->local_settings.enable_connect_protocol = iv[i].value;
  3955. break;
  3956. case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
  3957. session->local_settings.no_rfc7540_priorities = iv[i].value;
  3958. break;
  3959. }
  3960. }
  3961. return 0;
  3962. }
  3963. int nghttp2_session_on_settings_received(nghttp2_session *session,
  3964. nghttp2_frame *frame, int noack) {
  3965. int rv;
  3966. size_t i;
  3967. nghttp2_mem *mem;
  3968. nghttp2_inflight_settings *settings;
  3969. mem = &session->mem;
  3970. if (frame->hd.stream_id != 0) {
  3971. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  3972. "SETTINGS: stream_id != 0");
  3973. }
  3974. if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
  3975. if (frame->settings.niv != 0) {
  3976. return session_handle_invalid_connection(
  3977. session, frame, NGHTTP2_ERR_FRAME_SIZE_ERROR,
  3978. "SETTINGS: ACK and payload != 0");
  3979. }
  3980. settings = session->inflight_settings_head;
  3981. if (!settings) {
  3982. return session_handle_invalid_connection(
  3983. session, frame, NGHTTP2_ERR_PROTO, "SETTINGS: unexpected ACK");
  3984. }
  3985. rv = nghttp2_session_update_local_settings(session, settings->iv,
  3986. settings->niv);
  3987. session->inflight_settings_head = settings->next;
  3988. inflight_settings_del(settings, mem);
  3989. if (rv != 0) {
  3990. if (nghttp2_is_fatal(rv)) {
  3991. return rv;
  3992. }
  3993. return session_handle_invalid_connection(session, frame, rv, NULL);
  3994. }
  3995. return session_call_on_frame_received(session, frame);
  3996. }
  3997. if (!session->remote_settings_received) {
  3998. session->remote_settings.max_concurrent_streams =
  3999. NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
  4000. session->remote_settings_received = 1;
  4001. }
  4002. for (i = 0; i < frame->settings.niv; ++i) {
  4003. nghttp2_settings_entry *entry = &frame->settings.iv[i];
  4004. switch (entry->settings_id) {
  4005. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  4006. rv = nghttp2_hd_deflate_change_table_size(&session->hd_deflater,
  4007. entry->value);
  4008. if (rv != 0) {
  4009. if (nghttp2_is_fatal(rv)) {
  4010. return rv;
  4011. } else {
  4012. return session_handle_invalid_connection(
  4013. session, frame, NGHTTP2_ERR_HEADER_COMP, NULL);
  4014. }
  4015. }
  4016. session->remote_settings.header_table_size = entry->value;
  4017. break;
  4018. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  4019. if (entry->value != 0 && entry->value != 1) {
  4020. return session_handle_invalid_connection(
  4021. session, frame, NGHTTP2_ERR_PROTO,
  4022. "SETTINGS: invalid SETTINGS_ENBLE_PUSH");
  4023. }
  4024. if (!session->server && entry->value != 0) {
  4025. return session_handle_invalid_connection(
  4026. session, frame, NGHTTP2_ERR_PROTO,
  4027. "SETTINGS: server attempted to enable push");
  4028. }
  4029. session->remote_settings.enable_push = entry->value;
  4030. break;
  4031. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  4032. session->remote_settings.max_concurrent_streams = entry->value;
  4033. break;
  4034. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  4035. /* Update the initial window size of the all active streams */
  4036. /* Check that initial_window_size < (1u << 31) */
  4037. if (entry->value > NGHTTP2_MAX_WINDOW_SIZE) {
  4038. return session_handle_invalid_connection(
  4039. session, frame, NGHTTP2_ERR_FLOW_CONTROL,
  4040. "SETTINGS: too large SETTINGS_INITIAL_WINDOW_SIZE");
  4041. }
  4042. rv = session_update_remote_initial_window_size(session,
  4043. (int32_t)entry->value);
  4044. if (nghttp2_is_fatal(rv)) {
  4045. return rv;
  4046. }
  4047. if (rv != 0) {
  4048. return session_handle_invalid_connection(
  4049. session, frame, NGHTTP2_ERR_FLOW_CONTROL, NULL);
  4050. }
  4051. session->remote_settings.initial_window_size = entry->value;
  4052. break;
  4053. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  4054. if (entry->value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
  4055. entry->value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
  4056. return session_handle_invalid_connection(
  4057. session, frame, NGHTTP2_ERR_PROTO,
  4058. "SETTINGS: invalid SETTINGS_MAX_FRAME_SIZE");
  4059. }
  4060. session->remote_settings.max_frame_size = entry->value;
  4061. break;
  4062. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  4063. session->remote_settings.max_header_list_size = entry->value;
  4064. break;
  4065. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  4066. if (entry->value != 0 && entry->value != 1) {
  4067. return session_handle_invalid_connection(
  4068. session, frame, NGHTTP2_ERR_PROTO,
  4069. "SETTINGS: invalid SETTINGS_ENABLE_CONNECT_PROTOCOL");
  4070. }
  4071. if (!session->server &&
  4072. session->remote_settings.enable_connect_protocol &&
  4073. entry->value == 0) {
  4074. return session_handle_invalid_connection(
  4075. session, frame, NGHTTP2_ERR_PROTO,
  4076. "SETTINGS: server attempted to disable "
  4077. "SETTINGS_ENABLE_CONNECT_PROTOCOL");
  4078. }
  4079. session->remote_settings.enable_connect_protocol = entry->value;
  4080. break;
  4081. case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
  4082. if (entry->value != 0 && entry->value != 1) {
  4083. return session_handle_invalid_connection(
  4084. session, frame, NGHTTP2_ERR_PROTO,
  4085. "SETTINGS: invalid SETTINGS_NO_RFC7540_PRIORITIES");
  4086. }
  4087. if (session->remote_settings.no_rfc7540_priorities != UINT32_MAX &&
  4088. session->remote_settings.no_rfc7540_priorities != entry->value) {
  4089. return session_handle_invalid_connection(
  4090. session, frame, NGHTTP2_ERR_PROTO,
  4091. "SETTINGS: SETTINGS_NO_RFC7540_PRIORITIES cannot be changed");
  4092. }
  4093. session->remote_settings.no_rfc7540_priorities = entry->value;
  4094. break;
  4095. }
  4096. }
  4097. if (session->remote_settings.no_rfc7540_priorities == UINT32_MAX) {
  4098. session->remote_settings.no_rfc7540_priorities = 0;
  4099. if (session->server && session->pending_no_rfc7540_priorities &&
  4100. (session->opt_flags &
  4101. NGHTTP2_OPTMASK_SERVER_FALLBACK_RFC7540_PRIORITIES)) {
  4102. session->fallback_rfc7540_priorities = 1;
  4103. }
  4104. }
  4105. if (!noack && !session_is_closing(session)) {
  4106. rv = nghttp2_session_add_settings(session, NGHTTP2_FLAG_ACK, NULL, 0);
  4107. if (rv != 0) {
  4108. if (nghttp2_is_fatal(rv)) {
  4109. return rv;
  4110. }
  4111. return session_handle_invalid_connection(session, frame,
  4112. NGHTTP2_ERR_INTERNAL, NULL);
  4113. }
  4114. }
  4115. return session_call_on_frame_received(session, frame);
  4116. }
  4117. static int session_process_settings_frame(nghttp2_session *session) {
  4118. nghttp2_inbound_frame *iframe = &session->iframe;
  4119. nghttp2_frame *frame = &iframe->frame;
  4120. size_t i;
  4121. nghttp2_settings_entry min_header_size_entry;
  4122. if (iframe->max_niv) {
  4123. min_header_size_entry = iframe->iv[iframe->max_niv - 1];
  4124. if (min_header_size_entry.value < UINT32_MAX) {
  4125. /* If we have less value, then we must have
  4126. SETTINGS_HEADER_TABLE_SIZE in i < iframe->niv */
  4127. for (i = 0; i < iframe->niv; ++i) {
  4128. if (iframe->iv[i].settings_id == NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) {
  4129. break;
  4130. }
  4131. }
  4132. assert(i < iframe->niv);
  4133. if (min_header_size_entry.value != iframe->iv[i].value) {
  4134. iframe->iv[iframe->niv++] = iframe->iv[i];
  4135. iframe->iv[i] = min_header_size_entry;
  4136. }
  4137. }
  4138. }
  4139. nghttp2_frame_unpack_settings_payload(&frame->settings, iframe->iv,
  4140. iframe->niv);
  4141. iframe->iv = NULL;
  4142. iframe->niv = 0;
  4143. iframe->max_niv = 0;
  4144. return nghttp2_session_on_settings_received(session, frame, 0 /* ACK */);
  4145. }
  4146. int nghttp2_session_on_push_promise_received(nghttp2_session *session,
  4147. nghttp2_frame *frame) {
  4148. int rv;
  4149. nghttp2_stream *stream;
  4150. nghttp2_stream *promised_stream;
  4151. nghttp2_priority_spec pri_spec;
  4152. if (frame->hd.stream_id == 0) {
  4153. return session_inflate_handle_invalid_connection(
  4154. session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: stream_id == 0");
  4155. }
  4156. if (session->server || session->local_settings.enable_push == 0) {
  4157. return session_inflate_handle_invalid_connection(
  4158. session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: push disabled");
  4159. }
  4160. if (!nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
  4161. return session_inflate_handle_invalid_connection(
  4162. session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: invalid stream_id");
  4163. }
  4164. if (!session_allow_incoming_new_stream(session)) {
  4165. /* We just discard PUSH_PROMISE after GOAWAY was sent */
  4166. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  4167. }
  4168. if (!session_is_new_peer_stream_id(session,
  4169. frame->push_promise.promised_stream_id)) {
  4170. /* The spec says if an endpoint receives a PUSH_PROMISE with
  4171. illegal stream ID is subject to a connection error of type
  4172. PROTOCOL_ERROR. */
  4173. return session_inflate_handle_invalid_connection(
  4174. session, frame, NGHTTP2_ERR_PROTO,
  4175. "PUSH_PROMISE: invalid promised_stream_id");
  4176. }
  4177. if (session_detect_idle_stream(session, frame->hd.stream_id)) {
  4178. return session_inflate_handle_invalid_connection(
  4179. session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: stream in idle");
  4180. }
  4181. session->last_recv_stream_id = frame->push_promise.promised_stream_id;
  4182. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  4183. if (!stream || stream->state == NGHTTP2_STREAM_CLOSING ||
  4184. !session->pending_enable_push ||
  4185. session->num_incoming_reserved_streams >=
  4186. session->max_incoming_reserved_streams) {
  4187. /* Currently, client does not retain closed stream, so we don't
  4188. check NGHTTP2_SHUT_RD condition here. */
  4189. rv = nghttp2_session_add_rst_stream(
  4190. session, frame->push_promise.promised_stream_id, NGHTTP2_CANCEL);
  4191. if (rv != 0) {
  4192. return rv;
  4193. }
  4194. return NGHTTP2_ERR_IGN_HEADER_BLOCK;
  4195. }
  4196. if (stream->shut_flags & NGHTTP2_SHUT_RD) {
  4197. return session_inflate_handle_invalid_connection(
  4198. session, frame, NGHTTP2_ERR_STREAM_CLOSED,
  4199. "PUSH_PROMISE: stream closed");
  4200. }
  4201. nghttp2_priority_spec_init(&pri_spec, stream->stream_id,
  4202. NGHTTP2_DEFAULT_WEIGHT, 0);
  4203. promised_stream = nghttp2_session_open_stream(
  4204. session, frame->push_promise.promised_stream_id, NGHTTP2_STREAM_FLAG_NONE,
  4205. &pri_spec, NGHTTP2_STREAM_RESERVED, NULL);
  4206. if (!promised_stream) {
  4207. return NGHTTP2_ERR_NOMEM;
  4208. }
  4209. /* We don't call nghttp2_session_adjust_closed_stream(), since we
  4210. don't keep closed stream in client side */
  4211. session->last_proc_stream_id = session->last_recv_stream_id;
  4212. rv = session_call_on_begin_headers(session, frame);
  4213. if (rv != 0) {
  4214. return rv;
  4215. }
  4216. return 0;
  4217. }
  4218. static int session_process_push_promise_frame(nghttp2_session *session) {
  4219. nghttp2_inbound_frame *iframe = &session->iframe;
  4220. nghttp2_frame *frame = &iframe->frame;
  4221. nghttp2_frame_unpack_push_promise_payload(&frame->push_promise,
  4222. iframe->sbuf.pos);
  4223. return nghttp2_session_on_push_promise_received(session, frame);
  4224. }
  4225. int nghttp2_session_on_ping_received(nghttp2_session *session,
  4226. nghttp2_frame *frame) {
  4227. int rv = 0;
  4228. if (frame->hd.stream_id != 0) {
  4229. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  4230. "PING: stream_id != 0");
  4231. }
  4232. if ((session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_PING_ACK) == 0 &&
  4233. (frame->hd.flags & NGHTTP2_FLAG_ACK) == 0 &&
  4234. !session_is_closing(session)) {
  4235. /* Peer sent ping, so ping it back */
  4236. rv = nghttp2_session_add_ping(session, NGHTTP2_FLAG_ACK,
  4237. frame->ping.opaque_data);
  4238. if (rv != 0) {
  4239. return rv;
  4240. }
  4241. }
  4242. return session_call_on_frame_received(session, frame);
  4243. }
  4244. static int session_process_ping_frame(nghttp2_session *session) {
  4245. nghttp2_inbound_frame *iframe = &session->iframe;
  4246. nghttp2_frame *frame = &iframe->frame;
  4247. nghttp2_frame_unpack_ping_payload(&frame->ping, iframe->sbuf.pos);
  4248. return nghttp2_session_on_ping_received(session, frame);
  4249. }
  4250. int nghttp2_session_on_goaway_received(nghttp2_session *session,
  4251. nghttp2_frame *frame) {
  4252. int rv;
  4253. if (frame->hd.stream_id != 0) {
  4254. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  4255. "GOAWAY: stream_id != 0");
  4256. }
  4257. /* Spec says Endpoints MUST NOT increase the value they send in the
  4258. last stream identifier. */
  4259. if ((frame->goaway.last_stream_id > 0 &&
  4260. !nghttp2_session_is_my_stream_id(session,
  4261. frame->goaway.last_stream_id)) ||
  4262. session->remote_last_stream_id < frame->goaway.last_stream_id) {
  4263. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  4264. "GOAWAY: invalid last_stream_id");
  4265. }
  4266. session->goaway_flags |= NGHTTP2_GOAWAY_RECV;
  4267. session->remote_last_stream_id = frame->goaway.last_stream_id;
  4268. rv = session_call_on_frame_received(session, frame);
  4269. if (nghttp2_is_fatal(rv)) {
  4270. return rv;
  4271. }
  4272. return session_close_stream_on_goaway(session, frame->goaway.last_stream_id,
  4273. 0);
  4274. }
  4275. static int session_process_goaway_frame(nghttp2_session *session) {
  4276. nghttp2_inbound_frame *iframe = &session->iframe;
  4277. nghttp2_frame *frame = &iframe->frame;
  4278. nghttp2_frame_unpack_goaway_payload(&frame->goaway, iframe->sbuf.pos,
  4279. iframe->lbuf.pos,
  4280. nghttp2_buf_len(&iframe->lbuf));
  4281. nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
  4282. return nghttp2_session_on_goaway_received(session, frame);
  4283. }
  4284. static int
  4285. session_on_connection_window_update_received(nghttp2_session *session,
  4286. nghttp2_frame *frame) {
  4287. /* Handle connection-level flow control */
  4288. if (frame->window_update.window_size_increment == 0) {
  4289. return session_handle_invalid_connection(
  4290. session, frame, NGHTTP2_ERR_PROTO,
  4291. "WINDOW_UPDATE: window_size_increment == 0");
  4292. }
  4293. if (NGHTTP2_MAX_WINDOW_SIZE - frame->window_update.window_size_increment <
  4294. session->remote_window_size) {
  4295. return session_handle_invalid_connection(session, frame,
  4296. NGHTTP2_ERR_FLOW_CONTROL, NULL);
  4297. }
  4298. session->remote_window_size += frame->window_update.window_size_increment;
  4299. return session_call_on_frame_received(session, frame);
  4300. }
  4301. static int session_on_stream_window_update_received(nghttp2_session *session,
  4302. nghttp2_frame *frame) {
  4303. int rv;
  4304. nghttp2_stream *stream;
  4305. if (session_detect_idle_stream(session, frame->hd.stream_id)) {
  4306. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  4307. "WINDOW_UPDATE to idle stream");
  4308. }
  4309. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  4310. if (!stream) {
  4311. return 0;
  4312. }
  4313. if (state_reserved_remote(session, stream)) {
  4314. return session_handle_invalid_connection(
  4315. session, frame, NGHTTP2_ERR_PROTO, "WINDOW_UPADATE to reserved stream");
  4316. }
  4317. if (frame->window_update.window_size_increment == 0) {
  4318. return session_handle_invalid_connection(
  4319. session, frame, NGHTTP2_ERR_PROTO,
  4320. "WINDOW_UPDATE: window_size_increment == 0");
  4321. }
  4322. if (NGHTTP2_MAX_WINDOW_SIZE - frame->window_update.window_size_increment <
  4323. stream->remote_window_size) {
  4324. return session_handle_invalid_stream(session, frame,
  4325. NGHTTP2_ERR_FLOW_CONTROL);
  4326. }
  4327. stream->remote_window_size += frame->window_update.window_size_increment;
  4328. if (stream->remote_window_size > 0 &&
  4329. nghttp2_stream_check_deferred_by_flow_control(stream)) {
  4330. rv = session_resume_deferred_stream_item(
  4331. session, stream, NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
  4332. if (nghttp2_is_fatal(rv)) {
  4333. return rv;
  4334. }
  4335. }
  4336. return session_call_on_frame_received(session, frame);
  4337. }
  4338. int nghttp2_session_on_window_update_received(nghttp2_session *session,
  4339. nghttp2_frame *frame) {
  4340. if (frame->hd.stream_id == 0) {
  4341. return session_on_connection_window_update_received(session, frame);
  4342. } else {
  4343. return session_on_stream_window_update_received(session, frame);
  4344. }
  4345. }
  4346. static int session_process_window_update_frame(nghttp2_session *session) {
  4347. nghttp2_inbound_frame *iframe = &session->iframe;
  4348. nghttp2_frame *frame = &iframe->frame;
  4349. nghttp2_frame_unpack_window_update_payload(&frame->window_update,
  4350. iframe->sbuf.pos);
  4351. return nghttp2_session_on_window_update_received(session, frame);
  4352. }
  4353. int nghttp2_session_on_altsvc_received(nghttp2_session *session,
  4354. nghttp2_frame *frame) {
  4355. nghttp2_ext_altsvc *altsvc;
  4356. nghttp2_stream *stream;
  4357. altsvc = frame->ext.payload;
  4358. /* session->server case has been excluded */
  4359. if (frame->hd.stream_id == 0) {
  4360. if (altsvc->origin_len == 0) {
  4361. return session_call_on_invalid_frame_recv_callback(session, frame,
  4362. NGHTTP2_ERR_PROTO);
  4363. }
  4364. } else {
  4365. if (altsvc->origin_len > 0) {
  4366. return session_call_on_invalid_frame_recv_callback(session, frame,
  4367. NGHTTP2_ERR_PROTO);
  4368. }
  4369. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  4370. if (!stream) {
  4371. return 0;
  4372. }
  4373. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  4374. return 0;
  4375. }
  4376. }
  4377. if (altsvc->field_value_len == 0) {
  4378. return session_call_on_invalid_frame_recv_callback(session, frame,
  4379. NGHTTP2_ERR_PROTO);
  4380. }
  4381. return session_call_on_frame_received(session, frame);
  4382. }
  4383. int nghttp2_session_on_origin_received(nghttp2_session *session,
  4384. nghttp2_frame *frame) {
  4385. return session_call_on_frame_received(session, frame);
  4386. }
  4387. int nghttp2_session_on_priority_update_received(nghttp2_session *session,
  4388. nghttp2_frame *frame) {
  4389. nghttp2_ext_priority_update *priority_update;
  4390. nghttp2_stream *stream;
  4391. nghttp2_priority_spec pri_spec;
  4392. nghttp2_extpri extpri;
  4393. int rv;
  4394. assert(session->server);
  4395. priority_update = frame->ext.payload;
  4396. if (frame->hd.stream_id != 0) {
  4397. return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
  4398. "PRIORITY_UPDATE: stream_id == 0");
  4399. }
  4400. if (nghttp2_session_is_my_stream_id(session, priority_update->stream_id)) {
  4401. if (session_detect_idle_stream(session, priority_update->stream_id)) {
  4402. return session_handle_invalid_connection(
  4403. session, frame, NGHTTP2_ERR_PROTO,
  4404. "PRIORITY_UPDATE: prioritizing idle push is not allowed");
  4405. }
  4406. /* TODO Ignore priority signal to a push stream for now */
  4407. return session_call_on_frame_received(session, frame);
  4408. }
  4409. stream = nghttp2_session_get_stream_raw(session, priority_update->stream_id);
  4410. if (stream) {
  4411. /* Stream already exists. */
  4412. if (stream->flags & NGHTTP2_STREAM_FLAG_IGNORE_CLIENT_PRIORITIES) {
  4413. return session_call_on_frame_received(session, frame);
  4414. }
  4415. } else if (session_detect_idle_stream(session, priority_update->stream_id)) {
  4416. if (session->num_idle_streams + session->num_incoming_streams >=
  4417. session->local_settings.max_concurrent_streams) {
  4418. return session_handle_invalid_connection(
  4419. session, frame, NGHTTP2_ERR_PROTO,
  4420. "PRIORITY_UPDATE: max concurrent streams exceeded");
  4421. }
  4422. nghttp2_priority_spec_default_init(&pri_spec);
  4423. stream = nghttp2_session_open_stream(session, priority_update->stream_id,
  4424. NGHTTP2_FLAG_NONE, &pri_spec,
  4425. NGHTTP2_STREAM_IDLE, NULL);
  4426. if (!stream) {
  4427. return NGHTTP2_ERR_NOMEM;
  4428. }
  4429. } else {
  4430. return session_call_on_frame_received(session, frame);
  4431. }
  4432. extpri.urgency = NGHTTP2_EXTPRI_DEFAULT_URGENCY;
  4433. extpri.inc = 0;
  4434. rv = nghttp2_http_parse_priority(&extpri, priority_update->field_value,
  4435. priority_update->field_value_len);
  4436. if (rv != 0) {
  4437. /* Just ignore field_value if it cannot be parsed. */
  4438. return session_call_on_frame_received(session, frame);
  4439. }
  4440. rv = session_update_stream_priority(session, stream,
  4441. nghttp2_extpri_to_uint8(&extpri));
  4442. if (rv != 0) {
  4443. if (nghttp2_is_fatal(rv)) {
  4444. return rv;
  4445. }
  4446. }
  4447. return session_call_on_frame_received(session, frame);
  4448. }
  4449. static int session_process_altsvc_frame(nghttp2_session *session) {
  4450. nghttp2_inbound_frame *iframe = &session->iframe;
  4451. nghttp2_frame *frame = &iframe->frame;
  4452. nghttp2_frame_unpack_altsvc_payload(
  4453. &frame->ext, nghttp2_get_uint16(iframe->sbuf.pos), iframe->lbuf.pos,
  4454. nghttp2_buf_len(&iframe->lbuf));
  4455. /* nghttp2_frame_unpack_altsvc_payload steals buffer from
  4456. iframe->lbuf */
  4457. nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
  4458. return nghttp2_session_on_altsvc_received(session, frame);
  4459. }
  4460. static int session_process_origin_frame(nghttp2_session *session) {
  4461. nghttp2_inbound_frame *iframe = &session->iframe;
  4462. nghttp2_frame *frame = &iframe->frame;
  4463. nghttp2_mem *mem = &session->mem;
  4464. int rv;
  4465. rv = nghttp2_frame_unpack_origin_payload(&frame->ext, iframe->lbuf.pos,
  4466. nghttp2_buf_len(&iframe->lbuf), mem);
  4467. if (rv != 0) {
  4468. if (nghttp2_is_fatal(rv)) {
  4469. return rv;
  4470. }
  4471. /* Ignore ORIGIN frame which cannot be parsed. */
  4472. return 0;
  4473. }
  4474. return nghttp2_session_on_origin_received(session, frame);
  4475. }
  4476. static int session_process_priority_update_frame(nghttp2_session *session) {
  4477. nghttp2_inbound_frame *iframe = &session->iframe;
  4478. nghttp2_frame *frame = &iframe->frame;
  4479. nghttp2_frame_unpack_priority_update_payload(&frame->ext, iframe->sbuf.pos,
  4480. nghttp2_buf_len(&iframe->sbuf));
  4481. return nghttp2_session_on_priority_update_received(session, frame);
  4482. }
  4483. static int session_process_extension_frame(nghttp2_session *session) {
  4484. int rv;
  4485. nghttp2_inbound_frame *iframe = &session->iframe;
  4486. nghttp2_frame *frame = &iframe->frame;
  4487. rv = session_call_unpack_extension_callback(session);
  4488. if (nghttp2_is_fatal(rv)) {
  4489. return rv;
  4490. }
  4491. /* This handles the case where rv == NGHTTP2_ERR_CANCEL as well */
  4492. if (rv != 0) {
  4493. return 0;
  4494. }
  4495. return session_call_on_frame_received(session, frame);
  4496. }
  4497. int nghttp2_session_on_data_received(nghttp2_session *session,
  4498. nghttp2_frame *frame) {
  4499. int rv = 0;
  4500. nghttp2_stream *stream;
  4501. /* We don't call on_frame_recv_callback if stream has been closed
  4502. already or being closed. */
  4503. stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
  4504. if (!stream || stream->state == NGHTTP2_STREAM_CLOSING) {
  4505. /* This should be treated as stream error, but it results in lots
  4506. of RST_STREAM. So just ignore frame against nonexistent stream
  4507. for now. */
  4508. return 0;
  4509. }
  4510. if (session_enforce_http_messaging(session) &&
  4511. (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
  4512. if (nghttp2_http_on_remote_end_stream(stream) != 0) {
  4513. rv = nghttp2_session_add_rst_stream(session, stream->stream_id,
  4514. NGHTTP2_PROTOCOL_ERROR);
  4515. if (nghttp2_is_fatal(rv)) {
  4516. return rv;
  4517. }
  4518. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
  4519. /* Don't call nghttp2_session_close_stream_if_shut_rdwr because
  4520. RST_STREAM has been submitted. */
  4521. return 0;
  4522. }
  4523. }
  4524. rv = session_call_on_frame_received(session, frame);
  4525. if (nghttp2_is_fatal(rv)) {
  4526. return rv;
  4527. }
  4528. if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  4529. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
  4530. rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
  4531. if (nghttp2_is_fatal(rv)) {
  4532. return rv;
  4533. }
  4534. }
  4535. return 0;
  4536. }
  4537. /* For errors, this function only returns FATAL error. */
  4538. static int session_process_data_frame(nghttp2_session *session) {
  4539. int rv;
  4540. nghttp2_frame *public_data_frame = &session->iframe.frame;
  4541. rv = nghttp2_session_on_data_received(session, public_data_frame);
  4542. if (nghttp2_is_fatal(rv)) {
  4543. return rv;
  4544. }
  4545. return 0;
  4546. }
  4547. /*
  4548. * Now we have SETTINGS synchronization, flow control error can be
  4549. * detected strictly. If DATA frame is received with length > 0 and
  4550. * current received window size + delta length is strictly larger than
  4551. * local window size, it is subject to FLOW_CONTROL_ERROR, so return
  4552. * -1. Note that local_window_size is calculated after SETTINGS ACK is
  4553. * received from peer, so peer must honor this limit. If the resulting
  4554. * recv_window_size is strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
  4555. * return -1 too.
  4556. */
  4557. static int adjust_recv_window_size(int32_t *recv_window_size_ptr, size_t delta,
  4558. int32_t local_window_size) {
  4559. if (*recv_window_size_ptr > local_window_size - (int32_t)delta ||
  4560. *recv_window_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - (int32_t)delta) {
  4561. return -1;
  4562. }
  4563. *recv_window_size_ptr += (int32_t)delta;
  4564. return 0;
  4565. }
  4566. int nghttp2_session_update_recv_stream_window_size(nghttp2_session *session,
  4567. nghttp2_stream *stream,
  4568. size_t delta_size,
  4569. int send_window_update) {
  4570. int rv;
  4571. rv = adjust_recv_window_size(&stream->recv_window_size, delta_size,
  4572. stream->local_window_size);
  4573. if (rv != 0) {
  4574. return nghttp2_session_add_rst_stream(session, stream->stream_id,
  4575. NGHTTP2_FLOW_CONTROL_ERROR);
  4576. }
  4577. /* We don't have to send WINDOW_UPDATE if the data received is the
  4578. last chunk in the incoming stream. */
  4579. /* We have to use local_settings here because it is the constraint
  4580. the remote endpoint should honor. */
  4581. if (send_window_update &&
  4582. !(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) &&
  4583. stream->window_update_queued == 0 &&
  4584. nghttp2_should_send_window_update(stream->local_window_size,
  4585. stream->recv_window_size)) {
  4586. rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE,
  4587. stream->stream_id,
  4588. stream->recv_window_size);
  4589. if (rv != 0) {
  4590. return rv;
  4591. }
  4592. stream->recv_window_size = 0;
  4593. }
  4594. return 0;
  4595. }
  4596. int nghttp2_session_update_recv_connection_window_size(nghttp2_session *session,
  4597. size_t delta_size) {
  4598. int rv;
  4599. rv = adjust_recv_window_size(&session->recv_window_size, delta_size,
  4600. session->local_window_size);
  4601. if (rv != 0) {
  4602. return nghttp2_session_terminate_session(session,
  4603. NGHTTP2_FLOW_CONTROL_ERROR);
  4604. }
  4605. if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) &&
  4606. session->window_update_queued == 0 &&
  4607. nghttp2_should_send_window_update(session->local_window_size,
  4608. session->recv_window_size)) {
  4609. /* Use stream ID 0 to update connection-level flow control
  4610. window */
  4611. rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE, 0,
  4612. session->recv_window_size);
  4613. if (rv != 0) {
  4614. return rv;
  4615. }
  4616. session->recv_window_size = 0;
  4617. }
  4618. return 0;
  4619. }
  4620. static int session_update_consumed_size(nghttp2_session *session,
  4621. int32_t *consumed_size_ptr,
  4622. int32_t *recv_window_size_ptr,
  4623. uint8_t window_update_queued,
  4624. int32_t stream_id, size_t delta_size,
  4625. int32_t local_window_size) {
  4626. int32_t recv_size;
  4627. int rv;
  4628. if ((size_t)*consumed_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - delta_size) {
  4629. return nghttp2_session_terminate_session(session,
  4630. NGHTTP2_FLOW_CONTROL_ERROR);
  4631. }
  4632. *consumed_size_ptr += (int32_t)delta_size;
  4633. if (window_update_queued == 0) {
  4634. /* recv_window_size may be smaller than consumed_size, because it
  4635. may be decreased by negative value with
  4636. nghttp2_submit_window_update(). */
  4637. recv_size = nghttp2_min(*consumed_size_ptr, *recv_window_size_ptr);
  4638. if (nghttp2_should_send_window_update(local_window_size, recv_size)) {
  4639. rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE,
  4640. stream_id, recv_size);
  4641. if (rv != 0) {
  4642. return rv;
  4643. }
  4644. *recv_window_size_ptr -= recv_size;
  4645. *consumed_size_ptr -= recv_size;
  4646. }
  4647. }
  4648. return 0;
  4649. }
  4650. static int session_update_stream_consumed_size(nghttp2_session *session,
  4651. nghttp2_stream *stream,
  4652. size_t delta_size) {
  4653. return session_update_consumed_size(
  4654. session, &stream->consumed_size, &stream->recv_window_size,
  4655. stream->window_update_queued, stream->stream_id, delta_size,
  4656. stream->local_window_size);
  4657. }
  4658. static int session_update_connection_consumed_size(nghttp2_session *session,
  4659. size_t delta_size) {
  4660. return session_update_consumed_size(
  4661. session, &session->consumed_size, &session->recv_window_size,
  4662. session->window_update_queued, 0, delta_size, session->local_window_size);
  4663. }
  4664. /*
  4665. * Checks that we can receive the DATA frame for stream, which is
  4666. * indicated by |session->iframe.frame.hd.stream_id|. If it is a
  4667. * connection error situation, GOAWAY frame will be issued by this
  4668. * function.
  4669. *
  4670. * If the DATA frame is allowed, returns 0.
  4671. *
  4672. * This function returns 0 if it succeeds, or one of the following
  4673. * negative error codes:
  4674. *
  4675. * NGHTTP2_ERR_IGN_PAYLOAD
  4676. * The reception of DATA frame is connection error; or should be
  4677. * ignored.
  4678. * NGHTTP2_ERR_NOMEM
  4679. * Out of memory.
  4680. */
  4681. static int session_on_data_received_fail_fast(nghttp2_session *session) {
  4682. int rv;
  4683. nghttp2_stream *stream;
  4684. nghttp2_inbound_frame *iframe;
  4685. int32_t stream_id;
  4686. const char *failure_reason;
  4687. uint32_t error_code = NGHTTP2_PROTOCOL_ERROR;
  4688. iframe = &session->iframe;
  4689. stream_id = iframe->frame.hd.stream_id;
  4690. if (stream_id == 0) {
  4691. /* The spec says that if a DATA frame is received whose stream ID
  4692. is 0, the recipient MUST respond with a connection error of
  4693. type PROTOCOL_ERROR. */
  4694. failure_reason = "DATA: stream_id == 0";
  4695. goto fail;
  4696. }
  4697. if (session_detect_idle_stream(session, stream_id)) {
  4698. failure_reason = "DATA: stream in idle";
  4699. error_code = NGHTTP2_PROTOCOL_ERROR;
  4700. goto fail;
  4701. }
  4702. stream = nghttp2_session_get_stream(session, stream_id);
  4703. if (!stream) {
  4704. stream = nghttp2_session_get_stream_raw(session, stream_id);
  4705. if (stream && (stream->shut_flags & NGHTTP2_SHUT_RD)) {
  4706. failure_reason = "DATA: stream closed";
  4707. error_code = NGHTTP2_STREAM_CLOSED;
  4708. goto fail;
  4709. }
  4710. return NGHTTP2_ERR_IGN_PAYLOAD;
  4711. }
  4712. if (stream->shut_flags & NGHTTP2_SHUT_RD) {
  4713. failure_reason = "DATA: stream in half-closed(remote)";
  4714. error_code = NGHTTP2_STREAM_CLOSED;
  4715. goto fail;
  4716. }
  4717. if (nghttp2_session_is_my_stream_id(session, stream_id)) {
  4718. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  4719. return NGHTTP2_ERR_IGN_PAYLOAD;
  4720. }
  4721. if (stream->state != NGHTTP2_STREAM_OPENED) {
  4722. failure_reason = "DATA: stream not opened";
  4723. goto fail;
  4724. }
  4725. return 0;
  4726. }
  4727. if (stream->state == NGHTTP2_STREAM_RESERVED) {
  4728. failure_reason = "DATA: stream in reserved";
  4729. goto fail;
  4730. }
  4731. if (stream->state == NGHTTP2_STREAM_CLOSING) {
  4732. return NGHTTP2_ERR_IGN_PAYLOAD;
  4733. }
  4734. return 0;
  4735. fail:
  4736. rv = nghttp2_session_terminate_session_with_reason(session, error_code,
  4737. failure_reason);
  4738. if (nghttp2_is_fatal(rv)) {
  4739. return rv;
  4740. }
  4741. return NGHTTP2_ERR_IGN_PAYLOAD;
  4742. }
  4743. static size_t inbound_frame_payload_readlen(nghttp2_inbound_frame *iframe,
  4744. const uint8_t *in,
  4745. const uint8_t *last) {
  4746. return nghttp2_min((size_t)(last - in), iframe->payloadleft);
  4747. }
  4748. /*
  4749. * Resets iframe->sbuf and advance its mark pointer by |left| bytes.
  4750. */
  4751. static void inbound_frame_set_mark(nghttp2_inbound_frame *iframe, size_t left) {
  4752. nghttp2_buf_reset(&iframe->sbuf);
  4753. iframe->sbuf.mark += left;
  4754. }
  4755. static size_t inbound_frame_buf_read(nghttp2_inbound_frame *iframe,
  4756. const uint8_t *in, const uint8_t *last) {
  4757. size_t readlen;
  4758. readlen =
  4759. nghttp2_min((size_t)(last - in), nghttp2_buf_mark_avail(&iframe->sbuf));
  4760. iframe->sbuf.last = nghttp2_cpymem(iframe->sbuf.last, in, readlen);
  4761. return readlen;
  4762. }
  4763. /*
  4764. * Unpacks SETTINGS entry in iframe->sbuf.
  4765. */
  4766. static void inbound_frame_set_settings_entry(nghttp2_inbound_frame *iframe) {
  4767. nghttp2_settings_entry iv;
  4768. nghttp2_settings_entry *min_header_table_size_entry;
  4769. size_t i;
  4770. nghttp2_frame_unpack_settings_entry(&iv, iframe->sbuf.pos);
  4771. switch (iv.settings_id) {
  4772. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  4773. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  4774. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  4775. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  4776. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  4777. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  4778. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  4779. case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
  4780. break;
  4781. default:
  4782. DEBUGF("recv: unknown settings id=0x%02x\n", iv.settings_id);
  4783. iframe->iv[iframe->niv++] = iv;
  4784. return;
  4785. }
  4786. for (i = 0; i < iframe->niv; ++i) {
  4787. if (iframe->iv[i].settings_id == iv.settings_id) {
  4788. iframe->iv[i] = iv;
  4789. break;
  4790. }
  4791. }
  4792. if (i == iframe->niv) {
  4793. iframe->iv[iframe->niv++] = iv;
  4794. }
  4795. if (iv.settings_id == NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) {
  4796. /* Keep track of minimum value of SETTINGS_HEADER_TABLE_SIZE */
  4797. min_header_table_size_entry = &iframe->iv[iframe->max_niv - 1];
  4798. if (iv.value < min_header_table_size_entry->value) {
  4799. min_header_table_size_entry->value = iv.value;
  4800. }
  4801. }
  4802. }
  4803. /*
  4804. * Checks PADDED flags and set iframe->sbuf to read them accordingly.
  4805. * If padding is set, this function returns 1. If no padding is set,
  4806. * this function returns 0. On error, returns -1.
  4807. */
  4808. static int inbound_frame_handle_pad(nghttp2_inbound_frame *iframe,
  4809. nghttp2_frame_hd *hd) {
  4810. if (hd->flags & NGHTTP2_FLAG_PADDED) {
  4811. if (hd->length < 1) {
  4812. return -1;
  4813. }
  4814. inbound_frame_set_mark(iframe, 1);
  4815. return 1;
  4816. }
  4817. DEBUGF("recv: no padding in payload\n");
  4818. return 0;
  4819. }
  4820. /*
  4821. * Computes number of padding based on flags. This function returns
  4822. * the calculated length if it succeeds, or -1.
  4823. */
  4824. static ssize_t inbound_frame_compute_pad(nghttp2_inbound_frame *iframe) {
  4825. size_t padlen;
  4826. /* 1 for Pad Length field */
  4827. padlen = (size_t)(iframe->sbuf.pos[0] + 1);
  4828. DEBUGF("recv: padlen=%zu\n", padlen);
  4829. /* We cannot use iframe->frame.hd.length because of CONTINUATION */
  4830. if (padlen - 1 > iframe->payloadleft) {
  4831. return -1;
  4832. }
  4833. iframe->padlen = padlen;
  4834. return (ssize_t)padlen;
  4835. }
  4836. /*
  4837. * This function returns the effective payload length in the data of
  4838. * length |readlen| when the remaining payload is |payloadleft|. The
  4839. * |payloadleft| does not include |readlen|. If padding was started
  4840. * strictly before this data chunk, this function returns -1.
  4841. */
  4842. static ssize_t inbound_frame_effective_readlen(nghttp2_inbound_frame *iframe,
  4843. size_t payloadleft,
  4844. size_t readlen) {
  4845. size_t trail_padlen =
  4846. nghttp2_frame_trail_padlen(&iframe->frame, iframe->padlen);
  4847. if (trail_padlen > payloadleft) {
  4848. size_t padlen;
  4849. padlen = trail_padlen - payloadleft;
  4850. if (readlen < padlen) {
  4851. return -1;
  4852. }
  4853. return (ssize_t)(readlen - padlen);
  4854. }
  4855. return (ssize_t)(readlen);
  4856. }
  4857. static const uint8_t static_in[] = {0};
  4858. ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in,
  4859. size_t inlen) {
  4860. const uint8_t *first, *last;
  4861. nghttp2_inbound_frame *iframe = &session->iframe;
  4862. size_t readlen;
  4863. ssize_t padlen;
  4864. int rv;
  4865. int busy = 0;
  4866. nghttp2_frame_hd cont_hd;
  4867. nghttp2_stream *stream;
  4868. size_t pri_fieldlen;
  4869. nghttp2_mem *mem;
  4870. if (in == NULL) {
  4871. assert(inlen == 0);
  4872. in = static_in;
  4873. }
  4874. first = in;
  4875. last = in + inlen;
  4876. DEBUGF("recv: connection recv_window_size=%d, local_window=%d\n",
  4877. session->recv_window_size, session->local_window_size);
  4878. mem = &session->mem;
  4879. /* We may have idle streams more than we expect (e.g.,
  4880. nghttp2_session_change_stream_priority() or
  4881. nghttp2_session_create_idle_stream()). Adjust them here. */
  4882. rv = nghttp2_session_adjust_idle_stream(session);
  4883. if (nghttp2_is_fatal(rv)) {
  4884. return rv;
  4885. }
  4886. if (!nghttp2_session_want_read(session)) {
  4887. return (ssize_t)inlen;
  4888. }
  4889. for (;;) {
  4890. switch (iframe->state) {
  4891. case NGHTTP2_IB_READ_CLIENT_MAGIC:
  4892. readlen = nghttp2_min(inlen, iframe->payloadleft);
  4893. if (memcmp(&NGHTTP2_CLIENT_MAGIC[NGHTTP2_CLIENT_MAGIC_LEN -
  4894. iframe->payloadleft],
  4895. in, readlen) != 0) {
  4896. return NGHTTP2_ERR_BAD_CLIENT_MAGIC;
  4897. }
  4898. iframe->payloadleft -= readlen;
  4899. in += readlen;
  4900. if (iframe->payloadleft == 0) {
  4901. session_inbound_frame_reset(session);
  4902. iframe->state = NGHTTP2_IB_READ_FIRST_SETTINGS;
  4903. }
  4904. break;
  4905. case NGHTTP2_IB_READ_FIRST_SETTINGS:
  4906. DEBUGF("recv: [IB_READ_FIRST_SETTINGS]\n");
  4907. readlen = inbound_frame_buf_read(iframe, in, last);
  4908. in += readlen;
  4909. if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
  4910. return (ssize_t)(in - first);
  4911. }
  4912. if (iframe->sbuf.pos[3] != NGHTTP2_SETTINGS ||
  4913. (iframe->sbuf.pos[4] & NGHTTP2_FLAG_ACK)) {
  4914. rv = session_call_error_callback(
  4915. session, NGHTTP2_ERR_SETTINGS_EXPECTED,
  4916. "Remote peer returned unexpected data while we expected "
  4917. "SETTINGS frame. Perhaps, peer does not support HTTP/2 "
  4918. "properly.");
  4919. if (nghttp2_is_fatal(rv)) {
  4920. return rv;
  4921. }
  4922. rv = nghttp2_session_terminate_session_with_reason(
  4923. session, NGHTTP2_PROTOCOL_ERROR, "SETTINGS expected");
  4924. if (nghttp2_is_fatal(rv)) {
  4925. return rv;
  4926. }
  4927. return (ssize_t)inlen;
  4928. }
  4929. iframe->state = NGHTTP2_IB_READ_HEAD;
  4930. /* Fall through */
  4931. case NGHTTP2_IB_READ_HEAD: {
  4932. int on_begin_frame_called = 0;
  4933. DEBUGF("recv: [IB_READ_HEAD]\n");
  4934. readlen = inbound_frame_buf_read(iframe, in, last);
  4935. in += readlen;
  4936. if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
  4937. return (ssize_t)(in - first);
  4938. }
  4939. nghttp2_frame_unpack_frame_hd(&iframe->frame.hd, iframe->sbuf.pos);
  4940. iframe->payloadleft = iframe->frame.hd.length;
  4941. DEBUGF("recv: payloadlen=%zu, type=%u, flags=0x%02x, stream_id=%d\n",
  4942. iframe->frame.hd.length, iframe->frame.hd.type,
  4943. iframe->frame.hd.flags, iframe->frame.hd.stream_id);
  4944. if (iframe->frame.hd.length > session->local_settings.max_frame_size) {
  4945. DEBUGF("recv: length is too large %zu > %u\n", iframe->frame.hd.length,
  4946. session->local_settings.max_frame_size);
  4947. rv = nghttp2_session_terminate_session_with_reason(
  4948. session, NGHTTP2_FRAME_SIZE_ERROR, "too large frame size");
  4949. if (nghttp2_is_fatal(rv)) {
  4950. return rv;
  4951. }
  4952. return (ssize_t)inlen;
  4953. }
  4954. switch (iframe->frame.hd.type) {
  4955. case NGHTTP2_DATA: {
  4956. DEBUGF("recv: DATA\n");
  4957. iframe->frame.hd.flags &=
  4958. (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PADDED);
  4959. /* Check stream is open. If it is not open or closing,
  4960. ignore payload. */
  4961. busy = 1;
  4962. rv = session_on_data_received_fail_fast(session);
  4963. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  4964. return (ssize_t)inlen;
  4965. }
  4966. if (rv == NGHTTP2_ERR_IGN_PAYLOAD) {
  4967. DEBUGF("recv: DATA not allowed stream_id=%d\n",
  4968. iframe->frame.hd.stream_id);
  4969. iframe->state = NGHTTP2_IB_IGN_DATA;
  4970. break;
  4971. }
  4972. if (nghttp2_is_fatal(rv)) {
  4973. return rv;
  4974. }
  4975. rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
  4976. if (rv < 0) {
  4977. rv = nghttp2_session_terminate_session_with_reason(
  4978. session, NGHTTP2_PROTOCOL_ERROR,
  4979. "DATA: insufficient padding space");
  4980. if (nghttp2_is_fatal(rv)) {
  4981. return rv;
  4982. }
  4983. return (ssize_t)inlen;
  4984. }
  4985. if (rv == 1) {
  4986. iframe->state = NGHTTP2_IB_READ_PAD_DATA;
  4987. break;
  4988. }
  4989. iframe->state = NGHTTP2_IB_READ_DATA;
  4990. break;
  4991. }
  4992. case NGHTTP2_HEADERS:
  4993. DEBUGF("recv: HEADERS\n");
  4994. iframe->frame.hd.flags &=
  4995. (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS |
  4996. NGHTTP2_FLAG_PADDED | NGHTTP2_FLAG_PRIORITY);
  4997. rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
  4998. if (rv < 0) {
  4999. rv = nghttp2_session_terminate_session_with_reason(
  5000. session, NGHTTP2_PROTOCOL_ERROR,
  5001. "HEADERS: insufficient padding space");
  5002. if (nghttp2_is_fatal(rv)) {
  5003. return rv;
  5004. }
  5005. return (ssize_t)inlen;
  5006. }
  5007. if (rv == 1) {
  5008. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5009. break;
  5010. }
  5011. pri_fieldlen = nghttp2_frame_priority_len(iframe->frame.hd.flags);
  5012. if (pri_fieldlen > 0) {
  5013. if (iframe->payloadleft < pri_fieldlen) {
  5014. busy = 1;
  5015. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5016. break;
  5017. }
  5018. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5019. inbound_frame_set_mark(iframe, pri_fieldlen);
  5020. break;
  5021. }
  5022. /* Call on_begin_frame_callback here because
  5023. session_process_headers_frame() may call
  5024. on_begin_headers_callback */
  5025. rv = session_call_on_begin_frame(session, &iframe->frame.hd);
  5026. if (nghttp2_is_fatal(rv)) {
  5027. return rv;
  5028. }
  5029. on_begin_frame_called = 1;
  5030. rv = session_process_headers_frame(session);
  5031. if (nghttp2_is_fatal(rv)) {
  5032. return rv;
  5033. }
  5034. busy = 1;
  5035. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5036. return (ssize_t)inlen;
  5037. }
  5038. if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  5039. rv = nghttp2_session_add_rst_stream(
  5040. session, iframe->frame.hd.stream_id, NGHTTP2_INTERNAL_ERROR);
  5041. if (nghttp2_is_fatal(rv)) {
  5042. return rv;
  5043. }
  5044. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5045. break;
  5046. }
  5047. if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
  5048. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5049. break;
  5050. }
  5051. iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
  5052. break;
  5053. case NGHTTP2_PRIORITY:
  5054. DEBUGF("recv: PRIORITY\n");
  5055. iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
  5056. if (iframe->payloadleft != NGHTTP2_PRIORITY_SPECLEN) {
  5057. busy = 1;
  5058. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5059. break;
  5060. }
  5061. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5062. inbound_frame_set_mark(iframe, NGHTTP2_PRIORITY_SPECLEN);
  5063. break;
  5064. case NGHTTP2_RST_STREAM:
  5065. case NGHTTP2_WINDOW_UPDATE:
  5066. #ifdef DEBUGBUILD
  5067. switch (iframe->frame.hd.type) {
  5068. case NGHTTP2_RST_STREAM:
  5069. DEBUGF("recv: RST_STREAM\n");
  5070. break;
  5071. case NGHTTP2_WINDOW_UPDATE:
  5072. DEBUGF("recv: WINDOW_UPDATE\n");
  5073. break;
  5074. }
  5075. #endif /* DEBUGBUILD */
  5076. iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
  5077. if (iframe->payloadleft != 4) {
  5078. busy = 1;
  5079. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5080. break;
  5081. }
  5082. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5083. inbound_frame_set_mark(iframe, 4);
  5084. break;
  5085. case NGHTTP2_SETTINGS:
  5086. DEBUGF("recv: SETTINGS\n");
  5087. iframe->frame.hd.flags &= NGHTTP2_FLAG_ACK;
  5088. if ((iframe->frame.hd.length % NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) ||
  5089. ((iframe->frame.hd.flags & NGHTTP2_FLAG_ACK) &&
  5090. iframe->payloadleft > 0)) {
  5091. busy = 1;
  5092. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5093. break;
  5094. }
  5095. /* Check the settings flood counter early to be safe */
  5096. if (session->obq_flood_counter_ >= session->max_outbound_ack &&
  5097. !(iframe->frame.hd.flags & NGHTTP2_FLAG_ACK)) {
  5098. return NGHTTP2_ERR_FLOODED;
  5099. }
  5100. iframe->state = NGHTTP2_IB_READ_SETTINGS;
  5101. if (iframe->payloadleft) {
  5102. nghttp2_settings_entry *min_header_table_size_entry;
  5103. /* We allocate iv with additional one entry, to store the
  5104. minimum header table size. */
  5105. iframe->max_niv =
  5106. iframe->frame.hd.length / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH + 1;
  5107. if (iframe->max_niv - 1 > session->max_settings) {
  5108. rv = nghttp2_session_terminate_session_with_reason(
  5109. session, NGHTTP2_ENHANCE_YOUR_CALM,
  5110. "SETTINGS: too many setting entries");
  5111. if (nghttp2_is_fatal(rv)) {
  5112. return rv;
  5113. }
  5114. return (ssize_t)inlen;
  5115. }
  5116. iframe->iv = nghttp2_mem_malloc(mem, sizeof(nghttp2_settings_entry) *
  5117. iframe->max_niv);
  5118. if (!iframe->iv) {
  5119. return NGHTTP2_ERR_NOMEM;
  5120. }
  5121. min_header_table_size_entry = &iframe->iv[iframe->max_niv - 1];
  5122. min_header_table_size_entry->settings_id =
  5123. NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
  5124. min_header_table_size_entry->value = UINT32_MAX;
  5125. inbound_frame_set_mark(iframe, NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH);
  5126. break;
  5127. }
  5128. busy = 1;
  5129. inbound_frame_set_mark(iframe, 0);
  5130. break;
  5131. case NGHTTP2_PUSH_PROMISE:
  5132. DEBUGF("recv: PUSH_PROMISE\n");
  5133. iframe->frame.hd.flags &=
  5134. (NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PADDED);
  5135. rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
  5136. if (rv < 0) {
  5137. rv = nghttp2_session_terminate_session_with_reason(
  5138. session, NGHTTP2_PROTOCOL_ERROR,
  5139. "PUSH_PROMISE: insufficient padding space");
  5140. if (nghttp2_is_fatal(rv)) {
  5141. return rv;
  5142. }
  5143. return (ssize_t)inlen;
  5144. }
  5145. if (rv == 1) {
  5146. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5147. break;
  5148. }
  5149. if (iframe->payloadleft < 4) {
  5150. busy = 1;
  5151. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5152. break;
  5153. }
  5154. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5155. inbound_frame_set_mark(iframe, 4);
  5156. break;
  5157. case NGHTTP2_PING:
  5158. DEBUGF("recv: PING\n");
  5159. iframe->frame.hd.flags &= NGHTTP2_FLAG_ACK;
  5160. if (iframe->payloadleft != 8) {
  5161. busy = 1;
  5162. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5163. break;
  5164. }
  5165. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5166. inbound_frame_set_mark(iframe, 8);
  5167. break;
  5168. case NGHTTP2_GOAWAY:
  5169. DEBUGF("recv: GOAWAY\n");
  5170. iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
  5171. if (iframe->payloadleft < 8) {
  5172. busy = 1;
  5173. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5174. break;
  5175. }
  5176. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5177. inbound_frame_set_mark(iframe, 8);
  5178. break;
  5179. case NGHTTP2_CONTINUATION:
  5180. DEBUGF("recv: unexpected CONTINUATION\n");
  5181. /* Receiving CONTINUATION in this state are subject to
  5182. connection error of type PROTOCOL_ERROR */
  5183. rv = nghttp2_session_terminate_session_with_reason(
  5184. session, NGHTTP2_PROTOCOL_ERROR, "CONTINUATION: unexpected");
  5185. if (nghttp2_is_fatal(rv)) {
  5186. return rv;
  5187. }
  5188. return (ssize_t)inlen;
  5189. default:
  5190. DEBUGF("recv: extension frame\n");
  5191. if (check_ext_type_set(session->user_recv_ext_types,
  5192. iframe->frame.hd.type)) {
  5193. if (!session->callbacks.unpack_extension_callback) {
  5194. /* Silently ignore unknown frame type. */
  5195. busy = 1;
  5196. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5197. break;
  5198. }
  5199. busy = 1;
  5200. iframe->state = NGHTTP2_IB_READ_EXTENSION_PAYLOAD;
  5201. break;
  5202. } else {
  5203. switch (iframe->frame.hd.type) {
  5204. case NGHTTP2_ALTSVC:
  5205. if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ALTSVC) ==
  5206. 0) {
  5207. busy = 1;
  5208. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5209. break;
  5210. }
  5211. DEBUGF("recv: ALTSVC\n");
  5212. iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
  5213. iframe->frame.ext.payload = &iframe->ext_frame_payload.altsvc;
  5214. if (session->server) {
  5215. busy = 1;
  5216. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5217. break;
  5218. }
  5219. if (iframe->payloadleft < 2) {
  5220. busy = 1;
  5221. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5222. break;
  5223. }
  5224. busy = 1;
  5225. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5226. inbound_frame_set_mark(iframe, 2);
  5227. break;
  5228. case NGHTTP2_ORIGIN:
  5229. if (!(session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ORIGIN)) {
  5230. busy = 1;
  5231. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5232. break;
  5233. }
  5234. DEBUGF("recv: ORIGIN\n");
  5235. iframe->frame.ext.payload = &iframe->ext_frame_payload.origin;
  5236. if (session->server || iframe->frame.hd.stream_id ||
  5237. (iframe->frame.hd.flags & 0xf0)) {
  5238. busy = 1;
  5239. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5240. break;
  5241. }
  5242. iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
  5243. if (iframe->payloadleft) {
  5244. iframe->raw_lbuf = nghttp2_mem_malloc(mem, iframe->payloadleft);
  5245. if (iframe->raw_lbuf == NULL) {
  5246. return NGHTTP2_ERR_NOMEM;
  5247. }
  5248. nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf,
  5249. iframe->payloadleft);
  5250. } else {
  5251. busy = 1;
  5252. }
  5253. iframe->state = NGHTTP2_IB_READ_ORIGIN_PAYLOAD;
  5254. break;
  5255. case NGHTTP2_PRIORITY_UPDATE:
  5256. if ((session->builtin_recv_ext_types &
  5257. NGHTTP2_TYPEMASK_PRIORITY_UPDATE) == 0) {
  5258. busy = 1;
  5259. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5260. break;
  5261. }
  5262. DEBUGF("recv: PRIORITY_UPDATE\n");
  5263. iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
  5264. iframe->frame.ext.payload =
  5265. &iframe->ext_frame_payload.priority_update;
  5266. if (!session->server) {
  5267. rv = nghttp2_session_terminate_session_with_reason(
  5268. session, NGHTTP2_PROTOCOL_ERROR,
  5269. "PRIORITY_UPDATE is received from server");
  5270. if (nghttp2_is_fatal(rv)) {
  5271. return rv;
  5272. }
  5273. return (ssize_t)inlen;
  5274. }
  5275. if (iframe->payloadleft < 4) {
  5276. busy = 1;
  5277. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5278. break;
  5279. }
  5280. if (!session_no_rfc7540_pri_no_fallback(session) ||
  5281. iframe->payloadleft > sizeof(iframe->raw_sbuf)) {
  5282. busy = 1;
  5283. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5284. break;
  5285. }
  5286. busy = 1;
  5287. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5288. inbound_frame_set_mark(iframe, iframe->payloadleft);
  5289. break;
  5290. default:
  5291. busy = 1;
  5292. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5293. break;
  5294. }
  5295. }
  5296. }
  5297. if (!on_begin_frame_called) {
  5298. switch (iframe->state) {
  5299. case NGHTTP2_IB_IGN_HEADER_BLOCK:
  5300. case NGHTTP2_IB_IGN_PAYLOAD:
  5301. case NGHTTP2_IB_FRAME_SIZE_ERROR:
  5302. case NGHTTP2_IB_IGN_DATA:
  5303. case NGHTTP2_IB_IGN_ALL:
  5304. break;
  5305. default:
  5306. rv = session_call_on_begin_frame(session, &iframe->frame.hd);
  5307. if (nghttp2_is_fatal(rv)) {
  5308. return rv;
  5309. }
  5310. }
  5311. }
  5312. break;
  5313. }
  5314. case NGHTTP2_IB_READ_NBYTE:
  5315. DEBUGF("recv: [IB_READ_NBYTE]\n");
  5316. readlen = inbound_frame_buf_read(iframe, in, last);
  5317. in += readlen;
  5318. iframe->payloadleft -= readlen;
  5319. DEBUGF("recv: readlen=%zu, payloadleft=%zu, left=%zd\n", readlen,
  5320. iframe->payloadleft, nghttp2_buf_mark_avail(&iframe->sbuf));
  5321. if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
  5322. return (ssize_t)(in - first);
  5323. }
  5324. switch (iframe->frame.hd.type) {
  5325. case NGHTTP2_HEADERS:
  5326. if (iframe->padlen == 0 &&
  5327. (iframe->frame.hd.flags & NGHTTP2_FLAG_PADDED)) {
  5328. pri_fieldlen = nghttp2_frame_priority_len(iframe->frame.hd.flags);
  5329. padlen = inbound_frame_compute_pad(iframe);
  5330. if (padlen < 0 ||
  5331. (size_t)padlen + pri_fieldlen > 1 + iframe->payloadleft) {
  5332. rv = nghttp2_session_terminate_session_with_reason(
  5333. session, NGHTTP2_PROTOCOL_ERROR, "HEADERS: invalid padding");
  5334. if (nghttp2_is_fatal(rv)) {
  5335. return rv;
  5336. }
  5337. return (ssize_t)inlen;
  5338. }
  5339. iframe->frame.headers.padlen = (size_t)padlen;
  5340. if (pri_fieldlen > 0) {
  5341. if (iframe->payloadleft < pri_fieldlen) {
  5342. busy = 1;
  5343. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5344. break;
  5345. }
  5346. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5347. inbound_frame_set_mark(iframe, pri_fieldlen);
  5348. break;
  5349. } else {
  5350. /* Truncate buffers used for padding spec */
  5351. inbound_frame_set_mark(iframe, 0);
  5352. }
  5353. }
  5354. rv = session_process_headers_frame(session);
  5355. if (nghttp2_is_fatal(rv)) {
  5356. return rv;
  5357. }
  5358. busy = 1;
  5359. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5360. return (ssize_t)inlen;
  5361. }
  5362. if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  5363. rv = nghttp2_session_add_rst_stream(
  5364. session, iframe->frame.hd.stream_id, NGHTTP2_INTERNAL_ERROR);
  5365. if (nghttp2_is_fatal(rv)) {
  5366. return rv;
  5367. }
  5368. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5369. break;
  5370. }
  5371. if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
  5372. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5373. break;
  5374. }
  5375. iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
  5376. break;
  5377. case NGHTTP2_PRIORITY:
  5378. if (!session_no_rfc7540_pri_no_fallback(session) &&
  5379. session->remote_settings.no_rfc7540_priorities != 1) {
  5380. rv = session_process_priority_frame(session);
  5381. if (nghttp2_is_fatal(rv)) {
  5382. return rv;
  5383. }
  5384. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5385. return (ssize_t)inlen;
  5386. }
  5387. }
  5388. session_inbound_frame_reset(session);
  5389. break;
  5390. case NGHTTP2_RST_STREAM:
  5391. rv = session_process_rst_stream_frame(session);
  5392. if (nghttp2_is_fatal(rv)) {
  5393. return rv;
  5394. }
  5395. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5396. return (ssize_t)inlen;
  5397. }
  5398. session_inbound_frame_reset(session);
  5399. break;
  5400. case NGHTTP2_PUSH_PROMISE:
  5401. if (iframe->padlen == 0 &&
  5402. (iframe->frame.hd.flags & NGHTTP2_FLAG_PADDED)) {
  5403. padlen = inbound_frame_compute_pad(iframe);
  5404. if (padlen < 0 || (size_t)padlen + 4 /* promised stream id */
  5405. > 1 + iframe->payloadleft) {
  5406. rv = nghttp2_session_terminate_session_with_reason(
  5407. session, NGHTTP2_PROTOCOL_ERROR,
  5408. "PUSH_PROMISE: invalid padding");
  5409. if (nghttp2_is_fatal(rv)) {
  5410. return rv;
  5411. }
  5412. return (ssize_t)inlen;
  5413. }
  5414. iframe->frame.push_promise.padlen = (size_t)padlen;
  5415. if (iframe->payloadleft < 4) {
  5416. busy = 1;
  5417. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5418. break;
  5419. }
  5420. iframe->state = NGHTTP2_IB_READ_NBYTE;
  5421. inbound_frame_set_mark(iframe, 4);
  5422. break;
  5423. }
  5424. rv = session_process_push_promise_frame(session);
  5425. if (nghttp2_is_fatal(rv)) {
  5426. return rv;
  5427. }
  5428. busy = 1;
  5429. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5430. return (ssize_t)inlen;
  5431. }
  5432. if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  5433. rv = nghttp2_session_add_rst_stream(
  5434. session, iframe->frame.push_promise.promised_stream_id,
  5435. NGHTTP2_INTERNAL_ERROR);
  5436. if (nghttp2_is_fatal(rv)) {
  5437. return rv;
  5438. }
  5439. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5440. break;
  5441. }
  5442. if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
  5443. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5444. break;
  5445. }
  5446. iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
  5447. break;
  5448. case NGHTTP2_PING:
  5449. rv = session_process_ping_frame(session);
  5450. if (nghttp2_is_fatal(rv)) {
  5451. return rv;
  5452. }
  5453. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5454. return (ssize_t)inlen;
  5455. }
  5456. session_inbound_frame_reset(session);
  5457. break;
  5458. case NGHTTP2_GOAWAY: {
  5459. size_t debuglen;
  5460. /* 8 is Last-stream-ID + Error Code */
  5461. debuglen = iframe->frame.hd.length - 8;
  5462. if (debuglen > 0) {
  5463. iframe->raw_lbuf = nghttp2_mem_malloc(mem, debuglen);
  5464. if (iframe->raw_lbuf == NULL) {
  5465. return NGHTTP2_ERR_NOMEM;
  5466. }
  5467. nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf, debuglen);
  5468. }
  5469. busy = 1;
  5470. iframe->state = NGHTTP2_IB_READ_GOAWAY_DEBUG;
  5471. break;
  5472. }
  5473. case NGHTTP2_WINDOW_UPDATE:
  5474. rv = session_process_window_update_frame(session);
  5475. if (nghttp2_is_fatal(rv)) {
  5476. return rv;
  5477. }
  5478. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5479. return (ssize_t)inlen;
  5480. }
  5481. session_inbound_frame_reset(session);
  5482. break;
  5483. case NGHTTP2_ALTSVC: {
  5484. size_t origin_len;
  5485. origin_len = nghttp2_get_uint16(iframe->sbuf.pos);
  5486. DEBUGF("recv: origin_len=%zu\n", origin_len);
  5487. if (origin_len > iframe->payloadleft) {
  5488. busy = 1;
  5489. iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
  5490. break;
  5491. }
  5492. if (iframe->frame.hd.length > 2) {
  5493. iframe->raw_lbuf =
  5494. nghttp2_mem_malloc(mem, iframe->frame.hd.length - 2);
  5495. if (iframe->raw_lbuf == NULL) {
  5496. return NGHTTP2_ERR_NOMEM;
  5497. }
  5498. nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf,
  5499. iframe->frame.hd.length);
  5500. }
  5501. busy = 1;
  5502. iframe->state = NGHTTP2_IB_READ_ALTSVC_PAYLOAD;
  5503. break;
  5504. case NGHTTP2_PRIORITY_UPDATE:
  5505. DEBUGF("recv: prioritized_stream_id=%d\n",
  5506. nghttp2_get_uint32(iframe->sbuf.pos) & NGHTTP2_STREAM_ID_MASK);
  5507. rv = session_process_priority_update_frame(session);
  5508. if (nghttp2_is_fatal(rv)) {
  5509. return rv;
  5510. }
  5511. session_inbound_frame_reset(session);
  5512. break;
  5513. }
  5514. default:
  5515. /* This is unknown frame */
  5516. session_inbound_frame_reset(session);
  5517. break;
  5518. }
  5519. break;
  5520. case NGHTTP2_IB_READ_HEADER_BLOCK:
  5521. case NGHTTP2_IB_IGN_HEADER_BLOCK: {
  5522. ssize_t data_readlen;
  5523. size_t trail_padlen;
  5524. int final;
  5525. #ifdef DEBUGBUILD
  5526. if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
  5527. DEBUGF("recv: [IB_READ_HEADER_BLOCK]\n");
  5528. } else {
  5529. DEBUGF("recv: [IB_IGN_HEADER_BLOCK]\n");
  5530. }
  5531. #endif /* DEBUGBUILD */
  5532. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5533. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5534. iframe->payloadleft - readlen);
  5535. data_readlen = inbound_frame_effective_readlen(
  5536. iframe, iframe->payloadleft - readlen, readlen);
  5537. if (data_readlen == -1) {
  5538. /* everything is padding */
  5539. data_readlen = 0;
  5540. }
  5541. trail_padlen = nghttp2_frame_trail_padlen(&iframe->frame, iframe->padlen);
  5542. final = (iframe->frame.hd.flags & NGHTTP2_FLAG_END_HEADERS) &&
  5543. iframe->payloadleft - (size_t)data_readlen == trail_padlen;
  5544. if (data_readlen > 0 || (data_readlen == 0 && final)) {
  5545. size_t hd_proclen = 0;
  5546. DEBUGF("recv: block final=%d\n", final);
  5547. rv =
  5548. inflate_header_block(session, &iframe->frame, &hd_proclen,
  5549. (uint8_t *)in, (size_t)data_readlen, final,
  5550. iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK);
  5551. if (nghttp2_is_fatal(rv)) {
  5552. return rv;
  5553. }
  5554. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5555. return (ssize_t)inlen;
  5556. }
  5557. if (rv == NGHTTP2_ERR_PAUSE) {
  5558. in += hd_proclen;
  5559. iframe->payloadleft -= hd_proclen;
  5560. return (ssize_t)(in - first);
  5561. }
  5562. if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
  5563. /* The application says no more headers. We decompress the
  5564. rest of the header block but not invoke on_header_callback
  5565. and on_frame_recv_callback. */
  5566. in += hd_proclen;
  5567. iframe->payloadleft -= hd_proclen;
  5568. /* Use promised stream ID for PUSH_PROMISE */
  5569. rv = nghttp2_session_add_rst_stream(
  5570. session,
  5571. iframe->frame.hd.type == NGHTTP2_PUSH_PROMISE
  5572. ? iframe->frame.push_promise.promised_stream_id
  5573. : iframe->frame.hd.stream_id,
  5574. NGHTTP2_INTERNAL_ERROR);
  5575. if (nghttp2_is_fatal(rv)) {
  5576. return rv;
  5577. }
  5578. busy = 1;
  5579. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5580. break;
  5581. }
  5582. in += readlen;
  5583. iframe->payloadleft -= readlen;
  5584. if (rv == NGHTTP2_ERR_HEADER_COMP) {
  5585. /* GOAWAY is already issued */
  5586. if (iframe->payloadleft == 0) {
  5587. session_inbound_frame_reset(session);
  5588. } else {
  5589. busy = 1;
  5590. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5591. }
  5592. break;
  5593. }
  5594. } else {
  5595. in += readlen;
  5596. iframe->payloadleft -= readlen;
  5597. }
  5598. if (iframe->payloadleft) {
  5599. break;
  5600. }
  5601. if ((iframe->frame.hd.flags & NGHTTP2_FLAG_END_HEADERS) == 0) {
  5602. inbound_frame_set_mark(iframe, NGHTTP2_FRAME_HDLEN);
  5603. iframe->padlen = 0;
  5604. if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
  5605. iframe->state = NGHTTP2_IB_EXPECT_CONTINUATION;
  5606. } else {
  5607. iframe->state = NGHTTP2_IB_IGN_CONTINUATION;
  5608. }
  5609. } else {
  5610. if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
  5611. rv = session_after_header_block_received(session);
  5612. if (nghttp2_is_fatal(rv)) {
  5613. return rv;
  5614. }
  5615. }
  5616. session_inbound_frame_reset(session);
  5617. }
  5618. break;
  5619. }
  5620. case NGHTTP2_IB_IGN_PAYLOAD:
  5621. DEBUGF("recv: [IB_IGN_PAYLOAD]\n");
  5622. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5623. iframe->payloadleft -= readlen;
  5624. in += readlen;
  5625. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5626. iframe->payloadleft);
  5627. if (iframe->payloadleft) {
  5628. break;
  5629. }
  5630. switch (iframe->frame.hd.type) {
  5631. case NGHTTP2_HEADERS:
  5632. case NGHTTP2_PUSH_PROMISE:
  5633. case NGHTTP2_CONTINUATION:
  5634. /* Mark inflater bad so that we won't perform further decoding */
  5635. session->hd_inflater.ctx.bad = 1;
  5636. break;
  5637. default:
  5638. break;
  5639. }
  5640. session_inbound_frame_reset(session);
  5641. break;
  5642. case NGHTTP2_IB_FRAME_SIZE_ERROR:
  5643. DEBUGF("recv: [IB_FRAME_SIZE_ERROR]\n");
  5644. rv = session_handle_frame_size_error(session);
  5645. if (nghttp2_is_fatal(rv)) {
  5646. return rv;
  5647. }
  5648. assert(iframe->state == NGHTTP2_IB_IGN_ALL);
  5649. return (ssize_t)inlen;
  5650. case NGHTTP2_IB_READ_SETTINGS:
  5651. DEBUGF("recv: [IB_READ_SETTINGS]\n");
  5652. readlen = inbound_frame_buf_read(iframe, in, last);
  5653. iframe->payloadleft -= readlen;
  5654. in += readlen;
  5655. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5656. iframe->payloadleft);
  5657. if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
  5658. break;
  5659. }
  5660. if (readlen > 0) {
  5661. inbound_frame_set_settings_entry(iframe);
  5662. }
  5663. if (iframe->payloadleft) {
  5664. inbound_frame_set_mark(iframe, NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH);
  5665. break;
  5666. }
  5667. rv = session_process_settings_frame(session);
  5668. if (nghttp2_is_fatal(rv)) {
  5669. return rv;
  5670. }
  5671. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5672. return (ssize_t)inlen;
  5673. }
  5674. session_inbound_frame_reset(session);
  5675. break;
  5676. case NGHTTP2_IB_READ_GOAWAY_DEBUG:
  5677. DEBUGF("recv: [IB_READ_GOAWAY_DEBUG]\n");
  5678. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5679. if (readlen > 0) {
  5680. iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
  5681. iframe->payloadleft -= readlen;
  5682. in += readlen;
  5683. }
  5684. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5685. iframe->payloadleft);
  5686. if (iframe->payloadleft) {
  5687. assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
  5688. break;
  5689. }
  5690. rv = session_process_goaway_frame(session);
  5691. if (nghttp2_is_fatal(rv)) {
  5692. return rv;
  5693. }
  5694. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5695. return (ssize_t)inlen;
  5696. }
  5697. session_inbound_frame_reset(session);
  5698. break;
  5699. case NGHTTP2_IB_EXPECT_CONTINUATION:
  5700. case NGHTTP2_IB_IGN_CONTINUATION:
  5701. #ifdef DEBUGBUILD
  5702. if (iframe->state == NGHTTP2_IB_EXPECT_CONTINUATION) {
  5703. fprintf(stderr, "recv: [IB_EXPECT_CONTINUATION]\n");
  5704. } else {
  5705. fprintf(stderr, "recv: [IB_IGN_CONTINUATION]\n");
  5706. }
  5707. #endif /* DEBUGBUILD */
  5708. readlen = inbound_frame_buf_read(iframe, in, last);
  5709. in += readlen;
  5710. if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
  5711. return (ssize_t)(in - first);
  5712. }
  5713. nghttp2_frame_unpack_frame_hd(&cont_hd, iframe->sbuf.pos);
  5714. iframe->payloadleft = cont_hd.length;
  5715. DEBUGF("recv: payloadlen=%zu, type=%u, flags=0x%02x, stream_id=%d\n",
  5716. cont_hd.length, cont_hd.type, cont_hd.flags, cont_hd.stream_id);
  5717. if (cont_hd.type != NGHTTP2_CONTINUATION ||
  5718. cont_hd.stream_id != iframe->frame.hd.stream_id) {
  5719. DEBUGF("recv: expected stream_id=%d, type=%d, but got stream_id=%d, "
  5720. "type=%u\n",
  5721. iframe->frame.hd.stream_id, NGHTTP2_CONTINUATION,
  5722. cont_hd.stream_id, cont_hd.type);
  5723. rv = nghttp2_session_terminate_session_with_reason(
  5724. session, NGHTTP2_PROTOCOL_ERROR,
  5725. "unexpected non-CONTINUATION frame or stream_id is invalid");
  5726. if (nghttp2_is_fatal(rv)) {
  5727. return rv;
  5728. }
  5729. return (ssize_t)inlen;
  5730. }
  5731. /* CONTINUATION won't bear NGHTTP2_PADDED flag */
  5732. iframe->frame.hd.flags =
  5733. (uint8_t)(iframe->frame.hd.flags |
  5734. (cont_hd.flags & NGHTTP2_FLAG_END_HEADERS));
  5735. iframe->frame.hd.length += cont_hd.length;
  5736. busy = 1;
  5737. if (iframe->state == NGHTTP2_IB_EXPECT_CONTINUATION) {
  5738. iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
  5739. rv = session_call_on_begin_frame(session, &cont_hd);
  5740. if (nghttp2_is_fatal(rv)) {
  5741. return rv;
  5742. }
  5743. } else {
  5744. iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
  5745. }
  5746. break;
  5747. case NGHTTP2_IB_READ_PAD_DATA:
  5748. DEBUGF("recv: [IB_READ_PAD_DATA]\n");
  5749. readlen = inbound_frame_buf_read(iframe, in, last);
  5750. in += readlen;
  5751. iframe->payloadleft -= readlen;
  5752. DEBUGF("recv: readlen=%zu, payloadleft=%zu, left=%zu\n", readlen,
  5753. iframe->payloadleft, nghttp2_buf_mark_avail(&iframe->sbuf));
  5754. if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
  5755. return (ssize_t)(in - first);
  5756. }
  5757. /* Pad Length field is subject to flow control */
  5758. rv = nghttp2_session_update_recv_connection_window_size(session, readlen);
  5759. if (nghttp2_is_fatal(rv)) {
  5760. return rv;
  5761. }
  5762. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5763. return (ssize_t)inlen;
  5764. }
  5765. /* Pad Length field is consumed immediately */
  5766. rv =
  5767. nghttp2_session_consume(session, iframe->frame.hd.stream_id, readlen);
  5768. if (nghttp2_is_fatal(rv)) {
  5769. return rv;
  5770. }
  5771. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5772. return (ssize_t)inlen;
  5773. }
  5774. stream = nghttp2_session_get_stream(session, iframe->frame.hd.stream_id);
  5775. if (stream) {
  5776. rv = nghttp2_session_update_recv_stream_window_size(
  5777. session, stream, readlen,
  5778. iframe->payloadleft ||
  5779. (iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0);
  5780. if (nghttp2_is_fatal(rv)) {
  5781. return rv;
  5782. }
  5783. }
  5784. busy = 1;
  5785. padlen = inbound_frame_compute_pad(iframe);
  5786. if (padlen < 0) {
  5787. rv = nghttp2_session_terminate_session_with_reason(
  5788. session, NGHTTP2_PROTOCOL_ERROR, "DATA: invalid padding");
  5789. if (nghttp2_is_fatal(rv)) {
  5790. return rv;
  5791. }
  5792. return (ssize_t)inlen;
  5793. }
  5794. iframe->frame.data.padlen = (size_t)padlen;
  5795. iframe->state = NGHTTP2_IB_READ_DATA;
  5796. break;
  5797. case NGHTTP2_IB_READ_DATA:
  5798. stream = nghttp2_session_get_stream(session, iframe->frame.hd.stream_id);
  5799. if (!stream) {
  5800. busy = 1;
  5801. iframe->state = NGHTTP2_IB_IGN_DATA;
  5802. break;
  5803. }
  5804. DEBUGF("recv: [IB_READ_DATA]\n");
  5805. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5806. iframe->payloadleft -= readlen;
  5807. in += readlen;
  5808. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5809. iframe->payloadleft);
  5810. if (readlen > 0) {
  5811. ssize_t data_readlen;
  5812. rv = nghttp2_session_update_recv_connection_window_size(session,
  5813. readlen);
  5814. if (nghttp2_is_fatal(rv)) {
  5815. return rv;
  5816. }
  5817. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5818. return (ssize_t)inlen;
  5819. }
  5820. rv = nghttp2_session_update_recv_stream_window_size(
  5821. session, stream, readlen,
  5822. iframe->payloadleft ||
  5823. (iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0);
  5824. if (nghttp2_is_fatal(rv)) {
  5825. return rv;
  5826. }
  5827. data_readlen = inbound_frame_effective_readlen(
  5828. iframe, iframe->payloadleft, readlen);
  5829. if (data_readlen == -1) {
  5830. /* everything is padding */
  5831. data_readlen = 0;
  5832. }
  5833. padlen = (ssize_t)readlen - data_readlen;
  5834. if (padlen > 0) {
  5835. /* Padding is considered as "consumed" immediately */
  5836. rv = nghttp2_session_consume(session, iframe->frame.hd.stream_id,
  5837. (size_t)padlen);
  5838. if (nghttp2_is_fatal(rv)) {
  5839. return rv;
  5840. }
  5841. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5842. return (ssize_t)inlen;
  5843. }
  5844. }
  5845. DEBUGF("recv: data_readlen=%zd\n", data_readlen);
  5846. if (data_readlen > 0) {
  5847. if (session_enforce_http_messaging(session)) {
  5848. if (nghttp2_http_on_data_chunk(stream, (size_t)data_readlen) != 0) {
  5849. if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
  5850. /* Consume all data for connection immediately here */
  5851. rv = session_update_connection_consumed_size(
  5852. session, (size_t)data_readlen);
  5853. if (nghttp2_is_fatal(rv)) {
  5854. return rv;
  5855. }
  5856. if (iframe->state == NGHTTP2_IB_IGN_DATA) {
  5857. return (ssize_t)inlen;
  5858. }
  5859. }
  5860. rv = nghttp2_session_add_rst_stream(
  5861. session, iframe->frame.hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
  5862. if (nghttp2_is_fatal(rv)) {
  5863. return rv;
  5864. }
  5865. busy = 1;
  5866. iframe->state = NGHTTP2_IB_IGN_DATA;
  5867. break;
  5868. }
  5869. }
  5870. if (session->callbacks.on_data_chunk_recv_callback) {
  5871. rv = session->callbacks.on_data_chunk_recv_callback(
  5872. session, iframe->frame.hd.flags, iframe->frame.hd.stream_id,
  5873. in - readlen, (size_t)data_readlen, session->user_data);
  5874. if (rv == NGHTTP2_ERR_PAUSE) {
  5875. return (ssize_t)(in - first);
  5876. }
  5877. if (nghttp2_is_fatal(rv)) {
  5878. return NGHTTP2_ERR_CALLBACK_FAILURE;
  5879. }
  5880. }
  5881. }
  5882. }
  5883. if (iframe->payloadleft) {
  5884. break;
  5885. }
  5886. rv = session_process_data_frame(session);
  5887. if (nghttp2_is_fatal(rv)) {
  5888. return rv;
  5889. }
  5890. session_inbound_frame_reset(session);
  5891. break;
  5892. case NGHTTP2_IB_IGN_DATA:
  5893. DEBUGF("recv: [IB_IGN_DATA]\n");
  5894. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5895. iframe->payloadleft -= readlen;
  5896. in += readlen;
  5897. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5898. iframe->payloadleft);
  5899. if (readlen > 0) {
  5900. /* Update connection-level flow control window for ignored
  5901. DATA frame too */
  5902. rv = nghttp2_session_update_recv_connection_window_size(session,
  5903. readlen);
  5904. if (nghttp2_is_fatal(rv)) {
  5905. return rv;
  5906. }
  5907. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5908. return (ssize_t)inlen;
  5909. }
  5910. if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
  5911. /* Ignored DATA is considered as "consumed" immediately. */
  5912. rv = session_update_connection_consumed_size(session, readlen);
  5913. if (nghttp2_is_fatal(rv)) {
  5914. return rv;
  5915. }
  5916. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5917. return (ssize_t)inlen;
  5918. }
  5919. }
  5920. }
  5921. if (iframe->payloadleft) {
  5922. break;
  5923. }
  5924. session_inbound_frame_reset(session);
  5925. break;
  5926. case NGHTTP2_IB_IGN_ALL:
  5927. return (ssize_t)inlen;
  5928. case NGHTTP2_IB_READ_EXTENSION_PAYLOAD:
  5929. DEBUGF("recv: [IB_READ_EXTENSION_PAYLOAD]\n");
  5930. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5931. iframe->payloadleft -= readlen;
  5932. in += readlen;
  5933. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5934. iframe->payloadleft);
  5935. if (readlen > 0) {
  5936. rv = session_call_on_extension_chunk_recv_callback(
  5937. session, in - readlen, readlen);
  5938. if (nghttp2_is_fatal(rv)) {
  5939. return rv;
  5940. }
  5941. if (rv != 0) {
  5942. busy = 1;
  5943. iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
  5944. break;
  5945. }
  5946. }
  5947. if (iframe->payloadleft > 0) {
  5948. break;
  5949. }
  5950. rv = session_process_extension_frame(session);
  5951. if (nghttp2_is_fatal(rv)) {
  5952. return rv;
  5953. }
  5954. session_inbound_frame_reset(session);
  5955. break;
  5956. case NGHTTP2_IB_READ_ALTSVC_PAYLOAD:
  5957. DEBUGF("recv: [IB_READ_ALTSVC_PAYLOAD]\n");
  5958. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5959. if (readlen > 0) {
  5960. iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
  5961. iframe->payloadleft -= readlen;
  5962. in += readlen;
  5963. }
  5964. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5965. iframe->payloadleft);
  5966. if (iframe->payloadleft) {
  5967. assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
  5968. break;
  5969. }
  5970. rv = session_process_altsvc_frame(session);
  5971. if (nghttp2_is_fatal(rv)) {
  5972. return rv;
  5973. }
  5974. session_inbound_frame_reset(session);
  5975. break;
  5976. case NGHTTP2_IB_READ_ORIGIN_PAYLOAD:
  5977. DEBUGF("recv: [IB_READ_ORIGIN_PAYLOAD]\n");
  5978. readlen = inbound_frame_payload_readlen(iframe, in, last);
  5979. if (readlen > 0) {
  5980. iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
  5981. iframe->payloadleft -= readlen;
  5982. in += readlen;
  5983. }
  5984. DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
  5985. iframe->payloadleft);
  5986. if (iframe->payloadleft) {
  5987. assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
  5988. break;
  5989. }
  5990. rv = session_process_origin_frame(session);
  5991. if (nghttp2_is_fatal(rv)) {
  5992. return rv;
  5993. }
  5994. if (iframe->state == NGHTTP2_IB_IGN_ALL) {
  5995. return (ssize_t)inlen;
  5996. }
  5997. session_inbound_frame_reset(session);
  5998. break;
  5999. }
  6000. if (!busy && in == last) {
  6001. break;
  6002. }
  6003. busy = 0;
  6004. }
  6005. assert(in == last);
  6006. return (ssize_t)(in - first);
  6007. }
  6008. int nghttp2_session_recv(nghttp2_session *session) {
  6009. uint8_t buf[NGHTTP2_INBOUND_BUFFER_LENGTH];
  6010. while (1) {
  6011. ssize_t readlen;
  6012. readlen = session_recv(session, buf, sizeof(buf));
  6013. if (readlen > 0) {
  6014. ssize_t proclen = nghttp2_session_mem_recv(session, buf, (size_t)readlen);
  6015. if (proclen < 0) {
  6016. return (int)proclen;
  6017. }
  6018. assert(proclen == readlen);
  6019. } else if (readlen == 0 || readlen == NGHTTP2_ERR_WOULDBLOCK) {
  6020. return 0;
  6021. } else if (readlen == NGHTTP2_ERR_EOF) {
  6022. return NGHTTP2_ERR_EOF;
  6023. } else if (readlen < 0) {
  6024. return NGHTTP2_ERR_CALLBACK_FAILURE;
  6025. }
  6026. }
  6027. }
  6028. /*
  6029. * Returns the number of active streams, which includes streams in
  6030. * reserved state.
  6031. */
  6032. static size_t session_get_num_active_streams(nghttp2_session *session) {
  6033. return nghttp2_map_size(&session->streams) - session->num_closed_streams -
  6034. session->num_idle_streams;
  6035. }
  6036. int nghttp2_session_want_read(nghttp2_session *session) {
  6037. size_t num_active_streams;
  6038. /* If this flag is set, we don't want to read. The application
  6039. should drop the connection. */
  6040. if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_SENT) {
  6041. return 0;
  6042. }
  6043. num_active_streams = session_get_num_active_streams(session);
  6044. /* Unless termination GOAWAY is sent or received, we always want to
  6045. read incoming frames. */
  6046. if (num_active_streams > 0) {
  6047. return 1;
  6048. }
  6049. /* If there is no active streams and GOAWAY has been sent or
  6050. received, we are done with this session. */
  6051. return (session->goaway_flags &
  6052. (NGHTTP2_GOAWAY_SENT | NGHTTP2_GOAWAY_RECV)) == 0;
  6053. }
  6054. int nghttp2_session_want_write(nghttp2_session *session) {
  6055. /* If these flag is set, we don't want to write any data. The
  6056. application should drop the connection. */
  6057. if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_SENT) {
  6058. return 0;
  6059. }
  6060. /*
  6061. * Unless termination GOAWAY is sent or received, we want to write
  6062. * frames if there is pending ones. If pending frame is request/push
  6063. * response HEADERS and concurrent stream limit is reached, we don't
  6064. * want to write them.
  6065. */
  6066. return session->aob.item || nghttp2_outbound_queue_top(&session->ob_urgent) ||
  6067. nghttp2_outbound_queue_top(&session->ob_reg) ||
  6068. ((!nghttp2_pq_empty(&session->root.obq) ||
  6069. !session_sched_empty(session)) &&
  6070. session->remote_window_size > 0) ||
  6071. (nghttp2_outbound_queue_top(&session->ob_syn) &&
  6072. !session_is_outgoing_concurrent_streams_max(session));
  6073. }
  6074. int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
  6075. const uint8_t *opaque_data) {
  6076. int rv;
  6077. nghttp2_outbound_item *item;
  6078. nghttp2_frame *frame;
  6079. nghttp2_mem *mem;
  6080. mem = &session->mem;
  6081. if ((flags & NGHTTP2_FLAG_ACK) &&
  6082. session->obq_flood_counter_ >= session->max_outbound_ack) {
  6083. return NGHTTP2_ERR_FLOODED;
  6084. }
  6085. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  6086. if (item == NULL) {
  6087. return NGHTTP2_ERR_NOMEM;
  6088. }
  6089. nghttp2_outbound_item_init(item);
  6090. frame = &item->frame;
  6091. nghttp2_frame_ping_init(&frame->ping, flags, opaque_data);
  6092. rv = nghttp2_session_add_item(session, item);
  6093. if (rv != 0) {
  6094. nghttp2_frame_ping_free(&frame->ping);
  6095. nghttp2_mem_free(mem, item);
  6096. return rv;
  6097. }
  6098. if (flags & NGHTTP2_FLAG_ACK) {
  6099. ++session->obq_flood_counter_;
  6100. }
  6101. return 0;
  6102. }
  6103. int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id,
  6104. uint32_t error_code, const uint8_t *opaque_data,
  6105. size_t opaque_data_len, uint8_t aux_flags) {
  6106. int rv;
  6107. nghttp2_outbound_item *item;
  6108. nghttp2_frame *frame;
  6109. uint8_t *opaque_data_copy = NULL;
  6110. nghttp2_goaway_aux_data *aux_data;
  6111. nghttp2_mem *mem;
  6112. mem = &session->mem;
  6113. if (nghttp2_session_is_my_stream_id(session, last_stream_id)) {
  6114. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6115. }
  6116. if (opaque_data_len) {
  6117. if (opaque_data_len + 8 > NGHTTP2_MAX_PAYLOADLEN) {
  6118. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6119. }
  6120. opaque_data_copy = nghttp2_mem_malloc(mem, opaque_data_len);
  6121. if (opaque_data_copy == NULL) {
  6122. return NGHTTP2_ERR_NOMEM;
  6123. }
  6124. memcpy(opaque_data_copy, opaque_data, opaque_data_len);
  6125. }
  6126. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  6127. if (item == NULL) {
  6128. nghttp2_mem_free(mem, opaque_data_copy);
  6129. return NGHTTP2_ERR_NOMEM;
  6130. }
  6131. nghttp2_outbound_item_init(item);
  6132. frame = &item->frame;
  6133. /* last_stream_id must not be increased from the value previously
  6134. sent */
  6135. last_stream_id = nghttp2_min(last_stream_id, session->local_last_stream_id);
  6136. nghttp2_frame_goaway_init(&frame->goaway, last_stream_id, error_code,
  6137. opaque_data_copy, opaque_data_len);
  6138. aux_data = &item->aux_data.goaway;
  6139. aux_data->flags = aux_flags;
  6140. rv = nghttp2_session_add_item(session, item);
  6141. if (rv != 0) {
  6142. nghttp2_frame_goaway_free(&frame->goaway, mem);
  6143. nghttp2_mem_free(mem, item);
  6144. return rv;
  6145. }
  6146. session->goaway_flags |= NGHTTP2_GOAWAY_SUBMITTED;
  6147. return 0;
  6148. }
  6149. int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
  6150. int32_t stream_id,
  6151. int32_t window_size_increment) {
  6152. int rv;
  6153. nghttp2_outbound_item *item;
  6154. nghttp2_frame *frame;
  6155. nghttp2_mem *mem;
  6156. mem = &session->mem;
  6157. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  6158. if (item == NULL) {
  6159. return NGHTTP2_ERR_NOMEM;
  6160. }
  6161. nghttp2_outbound_item_init(item);
  6162. frame = &item->frame;
  6163. nghttp2_frame_window_update_init(&frame->window_update, flags, stream_id,
  6164. window_size_increment);
  6165. rv = nghttp2_session_add_item(session, item);
  6166. if (rv != 0) {
  6167. nghttp2_frame_window_update_free(&frame->window_update);
  6168. nghttp2_mem_free(mem, item);
  6169. return rv;
  6170. }
  6171. return 0;
  6172. }
  6173. static void
  6174. session_append_inflight_settings(nghttp2_session *session,
  6175. nghttp2_inflight_settings *settings) {
  6176. nghttp2_inflight_settings **i;
  6177. for (i = &session->inflight_settings_head; *i; i = &(*i)->next)
  6178. ;
  6179. *i = settings;
  6180. }
  6181. int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
  6182. const nghttp2_settings_entry *iv, size_t niv) {
  6183. nghttp2_outbound_item *item;
  6184. nghttp2_frame *frame;
  6185. nghttp2_settings_entry *iv_copy;
  6186. size_t i;
  6187. int rv;
  6188. nghttp2_mem *mem;
  6189. nghttp2_inflight_settings *inflight_settings = NULL;
  6190. uint8_t no_rfc7540_pri = session->pending_no_rfc7540_priorities;
  6191. mem = &session->mem;
  6192. if (flags & NGHTTP2_FLAG_ACK) {
  6193. if (niv != 0) {
  6194. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6195. }
  6196. if (session->obq_flood_counter_ >= session->max_outbound_ack) {
  6197. return NGHTTP2_ERR_FLOODED;
  6198. }
  6199. }
  6200. if (!nghttp2_iv_check(iv, niv)) {
  6201. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6202. }
  6203. for (i = 0; i < niv; ++i) {
  6204. if (iv[i].settings_id != NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES) {
  6205. continue;
  6206. }
  6207. if (no_rfc7540_pri == UINT8_MAX) {
  6208. no_rfc7540_pri = (uint8_t)iv[i].value;
  6209. continue;
  6210. }
  6211. if (iv[i].value != (uint32_t)no_rfc7540_pri) {
  6212. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6213. }
  6214. }
  6215. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  6216. if (item == NULL) {
  6217. return NGHTTP2_ERR_NOMEM;
  6218. }
  6219. if (niv > 0) {
  6220. iv_copy = nghttp2_frame_iv_copy(iv, niv, mem);
  6221. if (iv_copy == NULL) {
  6222. nghttp2_mem_free(mem, item);
  6223. return NGHTTP2_ERR_NOMEM;
  6224. }
  6225. } else {
  6226. iv_copy = NULL;
  6227. }
  6228. if ((flags & NGHTTP2_FLAG_ACK) == 0) {
  6229. rv = inflight_settings_new(&inflight_settings, iv, niv, mem);
  6230. if (rv != 0) {
  6231. assert(nghttp2_is_fatal(rv));
  6232. nghttp2_mem_free(mem, iv_copy);
  6233. nghttp2_mem_free(mem, item);
  6234. return rv;
  6235. }
  6236. }
  6237. nghttp2_outbound_item_init(item);
  6238. frame = &item->frame;
  6239. nghttp2_frame_settings_init(&frame->settings, flags, iv_copy, niv);
  6240. rv = nghttp2_session_add_item(session, item);
  6241. if (rv != 0) {
  6242. /* The only expected error is fatal one */
  6243. assert(nghttp2_is_fatal(rv));
  6244. inflight_settings_del(inflight_settings, mem);
  6245. nghttp2_frame_settings_free(&frame->settings, mem);
  6246. nghttp2_mem_free(mem, item);
  6247. return rv;
  6248. }
  6249. if (flags & NGHTTP2_FLAG_ACK) {
  6250. ++session->obq_flood_counter_;
  6251. } else {
  6252. session_append_inflight_settings(session, inflight_settings);
  6253. }
  6254. /* Extract NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS and ENABLE_PUSH
  6255. here. We use it to refuse the incoming stream and PUSH_PROMISE
  6256. with RST_STREAM. */
  6257. for (i = niv; i > 0; --i) {
  6258. if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS) {
  6259. session->pending_local_max_concurrent_stream = iv[i - 1].value;
  6260. break;
  6261. }
  6262. }
  6263. for (i = niv; i > 0; --i) {
  6264. if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_ENABLE_PUSH) {
  6265. session->pending_enable_push = (uint8_t)iv[i - 1].value;
  6266. break;
  6267. }
  6268. }
  6269. for (i = niv; i > 0; --i) {
  6270. if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL) {
  6271. session->pending_enable_connect_protocol = (uint8_t)iv[i - 1].value;
  6272. break;
  6273. }
  6274. }
  6275. if (no_rfc7540_pri == UINT8_MAX) {
  6276. session->pending_no_rfc7540_priorities = 0;
  6277. } else {
  6278. session->pending_no_rfc7540_priorities = no_rfc7540_pri;
  6279. }
  6280. return 0;
  6281. }
  6282. int nghttp2_session_pack_data(nghttp2_session *session, nghttp2_bufs *bufs,
  6283. size_t datamax, nghttp2_frame *frame,
  6284. nghttp2_data_aux_data *aux_data,
  6285. nghttp2_stream *stream) {
  6286. int rv;
  6287. uint32_t data_flags;
  6288. ssize_t payloadlen;
  6289. ssize_t padded_payloadlen;
  6290. nghttp2_buf *buf;
  6291. size_t max_payloadlen;
  6292. assert(bufs->head == bufs->cur);
  6293. buf = &bufs->cur->buf;
  6294. if (session->callbacks.read_length_callback) {
  6295. payloadlen = session->callbacks.read_length_callback(
  6296. session, frame->hd.type, stream->stream_id, session->remote_window_size,
  6297. stream->remote_window_size, session->remote_settings.max_frame_size,
  6298. session->user_data);
  6299. DEBUGF("send: read_length_callback=%zd\n", payloadlen);
  6300. payloadlen = nghttp2_session_enforce_flow_control_limits(session, stream,
  6301. payloadlen);
  6302. DEBUGF("send: read_length_callback after flow control=%zd\n", payloadlen);
  6303. if (payloadlen <= 0) {
  6304. return NGHTTP2_ERR_CALLBACK_FAILURE;
  6305. }
  6306. if ((size_t)payloadlen > nghttp2_buf_avail(buf)) {
  6307. /* Resize the current buffer(s). The reason why we do +1 for
  6308. buffer size is for possible padding field. */
  6309. rv = nghttp2_bufs_realloc(&session->aob.framebufs,
  6310. (size_t)(NGHTTP2_FRAME_HDLEN + 1 + payloadlen));
  6311. if (rv != 0) {
  6312. DEBUGF("send: realloc buffer failed rv=%d", rv);
  6313. /* If reallocation failed, old buffers are still in tact. So
  6314. use safe limit. */
  6315. payloadlen = (ssize_t)datamax;
  6316. DEBUGF("send: use safe limit payloadlen=%zd", payloadlen);
  6317. } else {
  6318. assert(&session->aob.framebufs == bufs);
  6319. buf = &bufs->cur->buf;
  6320. }
  6321. }
  6322. datamax = (size_t)payloadlen;
  6323. }
  6324. /* Current max DATA length is less then buffer chunk size */
  6325. assert(nghttp2_buf_avail(buf) >= datamax);
  6326. data_flags = NGHTTP2_DATA_FLAG_NONE;
  6327. payloadlen = aux_data->data_prd.read_callback(
  6328. session, frame->hd.stream_id, buf->pos, datamax, &data_flags,
  6329. &aux_data->data_prd.source, session->user_data);
  6330. if (payloadlen == NGHTTP2_ERR_DEFERRED ||
  6331. payloadlen == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE ||
  6332. payloadlen == NGHTTP2_ERR_PAUSE) {
  6333. DEBUGF("send: DATA postponed due to %s\n",
  6334. nghttp2_strerror((int)payloadlen));
  6335. return (int)payloadlen;
  6336. }
  6337. if (payloadlen < 0 || datamax < (size_t)payloadlen) {
  6338. /* This is the error code when callback is failed. */
  6339. return NGHTTP2_ERR_CALLBACK_FAILURE;
  6340. }
  6341. buf->last = buf->pos + payloadlen;
  6342. buf->pos -= NGHTTP2_FRAME_HDLEN;
  6343. /* Clear flags, because this may contain previous flags of previous
  6344. DATA */
  6345. frame->hd.flags = NGHTTP2_FLAG_NONE;
  6346. if (data_flags & NGHTTP2_DATA_FLAG_EOF) {
  6347. aux_data->eof = 1;
  6348. /* If NGHTTP2_DATA_FLAG_NO_END_STREAM is set, don't set
  6349. NGHTTP2_FLAG_END_STREAM */
  6350. if ((aux_data->flags & NGHTTP2_FLAG_END_STREAM) &&
  6351. (data_flags & NGHTTP2_DATA_FLAG_NO_END_STREAM) == 0) {
  6352. frame->hd.flags |= NGHTTP2_FLAG_END_STREAM;
  6353. }
  6354. }
  6355. if (data_flags & NGHTTP2_DATA_FLAG_NO_COPY) {
  6356. if (session->callbacks.send_data_callback == NULL) {
  6357. DEBUGF("NGHTTP2_DATA_FLAG_NO_COPY requires send_data_callback set\n");
  6358. return NGHTTP2_ERR_CALLBACK_FAILURE;
  6359. }
  6360. aux_data->no_copy = 1;
  6361. }
  6362. frame->hd.length = (size_t)payloadlen;
  6363. frame->data.padlen = 0;
  6364. max_payloadlen = nghttp2_min(datamax, frame->hd.length + NGHTTP2_MAX_PADLEN);
  6365. padded_payloadlen =
  6366. session_call_select_padding(session, frame, max_payloadlen);
  6367. if (nghttp2_is_fatal((int)padded_payloadlen)) {
  6368. return (int)padded_payloadlen;
  6369. }
  6370. frame->data.padlen = (size_t)(padded_payloadlen - payloadlen);
  6371. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  6372. nghttp2_frame_add_pad(bufs, &frame->hd, frame->data.padlen,
  6373. aux_data->no_copy);
  6374. session_reschedule_stream(session, stream);
  6375. if (frame->hd.length == 0 && (data_flags & NGHTTP2_DATA_FLAG_EOF) &&
  6376. (data_flags & NGHTTP2_DATA_FLAG_NO_END_STREAM)) {
  6377. /* DATA payload length is 0, and DATA frame does not bear
  6378. END_STREAM. In this case, there is no point to send 0 length
  6379. DATA frame. */
  6380. return NGHTTP2_ERR_CANCEL;
  6381. }
  6382. return 0;
  6383. }
  6384. void *nghttp2_session_get_stream_user_data(nghttp2_session *session,
  6385. int32_t stream_id) {
  6386. nghttp2_stream *stream;
  6387. stream = nghttp2_session_get_stream(session, stream_id);
  6388. if (stream) {
  6389. return stream->stream_user_data;
  6390. } else {
  6391. return NULL;
  6392. }
  6393. }
  6394. int nghttp2_session_set_stream_user_data(nghttp2_session *session,
  6395. int32_t stream_id,
  6396. void *stream_user_data) {
  6397. nghttp2_stream *stream;
  6398. nghttp2_frame *frame;
  6399. nghttp2_outbound_item *item;
  6400. stream = nghttp2_session_get_stream(session, stream_id);
  6401. if (stream) {
  6402. stream->stream_user_data = stream_user_data;
  6403. return 0;
  6404. }
  6405. if (session->server || !nghttp2_session_is_my_stream_id(session, stream_id) ||
  6406. !nghttp2_outbound_queue_top(&session->ob_syn)) {
  6407. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6408. }
  6409. frame = &nghttp2_outbound_queue_top(&session->ob_syn)->frame;
  6410. assert(frame->hd.type == NGHTTP2_HEADERS);
  6411. if (frame->hd.stream_id > stream_id ||
  6412. (uint32_t)stream_id >= session->next_stream_id) {
  6413. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6414. }
  6415. for (item = session->ob_syn.head; item; item = item->qnext) {
  6416. if (item->frame.hd.stream_id < stream_id) {
  6417. continue;
  6418. }
  6419. if (item->frame.hd.stream_id > stream_id) {
  6420. break;
  6421. }
  6422. item->aux_data.headers.stream_user_data = stream_user_data;
  6423. return 0;
  6424. }
  6425. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6426. }
  6427. int nghttp2_session_resume_data(nghttp2_session *session, int32_t stream_id) {
  6428. int rv;
  6429. nghttp2_stream *stream;
  6430. stream = nghttp2_session_get_stream(session, stream_id);
  6431. if (stream == NULL || !nghttp2_stream_check_deferred_item(stream)) {
  6432. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6433. }
  6434. rv = session_resume_deferred_stream_item(session, stream,
  6435. NGHTTP2_STREAM_FLAG_DEFERRED_USER);
  6436. if (nghttp2_is_fatal(rv)) {
  6437. return rv;
  6438. }
  6439. return 0;
  6440. }
  6441. size_t nghttp2_session_get_outbound_queue_size(nghttp2_session *session) {
  6442. return nghttp2_outbound_queue_size(&session->ob_urgent) +
  6443. nghttp2_outbound_queue_size(&session->ob_reg) +
  6444. nghttp2_outbound_queue_size(&session->ob_syn);
  6445. /* TODO account for item attached to stream */
  6446. }
  6447. int32_t
  6448. nghttp2_session_get_stream_effective_recv_data_length(nghttp2_session *session,
  6449. int32_t stream_id) {
  6450. nghttp2_stream *stream;
  6451. stream = nghttp2_session_get_stream(session, stream_id);
  6452. if (stream == NULL) {
  6453. return -1;
  6454. }
  6455. return stream->recv_window_size < 0 ? 0 : stream->recv_window_size;
  6456. }
  6457. int32_t
  6458. nghttp2_session_get_stream_effective_local_window_size(nghttp2_session *session,
  6459. int32_t stream_id) {
  6460. nghttp2_stream *stream;
  6461. stream = nghttp2_session_get_stream(session, stream_id);
  6462. if (stream == NULL) {
  6463. return -1;
  6464. }
  6465. return stream->local_window_size;
  6466. }
  6467. int32_t nghttp2_session_get_stream_local_window_size(nghttp2_session *session,
  6468. int32_t stream_id) {
  6469. nghttp2_stream *stream;
  6470. int32_t size;
  6471. stream = nghttp2_session_get_stream(session, stream_id);
  6472. if (stream == NULL) {
  6473. return -1;
  6474. }
  6475. size = stream->local_window_size - stream->recv_window_size;
  6476. /* size could be negative if local endpoint reduced
  6477. SETTINGS_INITIAL_WINDOW_SIZE */
  6478. if (size < 0) {
  6479. return 0;
  6480. }
  6481. return size;
  6482. }
  6483. int32_t
  6484. nghttp2_session_get_effective_recv_data_length(nghttp2_session *session) {
  6485. return session->recv_window_size < 0 ? 0 : session->recv_window_size;
  6486. }
  6487. int32_t
  6488. nghttp2_session_get_effective_local_window_size(nghttp2_session *session) {
  6489. return session->local_window_size;
  6490. }
  6491. int32_t nghttp2_session_get_local_window_size(nghttp2_session *session) {
  6492. return session->local_window_size - session->recv_window_size;
  6493. }
  6494. int32_t nghttp2_session_get_stream_remote_window_size(nghttp2_session *session,
  6495. int32_t stream_id) {
  6496. nghttp2_stream *stream;
  6497. stream = nghttp2_session_get_stream(session, stream_id);
  6498. if (stream == NULL) {
  6499. return -1;
  6500. }
  6501. /* stream->remote_window_size can be negative when
  6502. SETTINGS_INITIAL_WINDOW_SIZE is changed. */
  6503. return nghttp2_max(0, stream->remote_window_size);
  6504. }
  6505. int32_t nghttp2_session_get_remote_window_size(nghttp2_session *session) {
  6506. return session->remote_window_size;
  6507. }
  6508. uint32_t nghttp2_session_get_remote_settings(nghttp2_session *session,
  6509. nghttp2_settings_id id) {
  6510. switch (id) {
  6511. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  6512. return session->remote_settings.header_table_size;
  6513. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  6514. return session->remote_settings.enable_push;
  6515. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  6516. return session->remote_settings.max_concurrent_streams;
  6517. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  6518. return session->remote_settings.initial_window_size;
  6519. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  6520. return session->remote_settings.max_frame_size;
  6521. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  6522. return session->remote_settings.max_header_list_size;
  6523. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  6524. return session->remote_settings.enable_connect_protocol;
  6525. case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
  6526. return session->remote_settings.no_rfc7540_priorities;
  6527. }
  6528. assert(0);
  6529. abort(); /* if NDEBUG is set */
  6530. }
  6531. uint32_t nghttp2_session_get_local_settings(nghttp2_session *session,
  6532. nghttp2_settings_id id) {
  6533. switch (id) {
  6534. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  6535. return session->local_settings.header_table_size;
  6536. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  6537. return session->local_settings.enable_push;
  6538. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  6539. return session->local_settings.max_concurrent_streams;
  6540. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  6541. return session->local_settings.initial_window_size;
  6542. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  6543. return session->local_settings.max_frame_size;
  6544. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  6545. return session->local_settings.max_header_list_size;
  6546. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  6547. return session->local_settings.enable_connect_protocol;
  6548. case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
  6549. return session->local_settings.no_rfc7540_priorities;
  6550. }
  6551. assert(0);
  6552. abort(); /* if NDEBUG is set */
  6553. }
  6554. static int nghttp2_session_upgrade_internal(nghttp2_session *session,
  6555. const uint8_t *settings_payload,
  6556. size_t settings_payloadlen,
  6557. void *stream_user_data) {
  6558. nghttp2_stream *stream;
  6559. nghttp2_frame frame;
  6560. nghttp2_settings_entry *iv;
  6561. size_t niv;
  6562. int rv;
  6563. nghttp2_priority_spec pri_spec;
  6564. nghttp2_mem *mem;
  6565. mem = &session->mem;
  6566. if ((!session->server && session->next_stream_id != 1) ||
  6567. (session->server && session->last_recv_stream_id >= 1)) {
  6568. return NGHTTP2_ERR_PROTO;
  6569. }
  6570. if (settings_payloadlen % NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
  6571. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6572. }
  6573. /* SETTINGS frame contains too many settings */
  6574. if (settings_payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH >
  6575. session->max_settings) {
  6576. return NGHTTP2_ERR_TOO_MANY_SETTINGS;
  6577. }
  6578. rv = nghttp2_frame_unpack_settings_payload2(&iv, &niv, settings_payload,
  6579. settings_payloadlen, mem);
  6580. if (rv != 0) {
  6581. return rv;
  6582. }
  6583. if (session->server) {
  6584. nghttp2_frame_hd_init(&frame.hd, settings_payloadlen, NGHTTP2_SETTINGS,
  6585. NGHTTP2_FLAG_NONE, 0);
  6586. frame.settings.iv = iv;
  6587. frame.settings.niv = niv;
  6588. rv = nghttp2_session_on_settings_received(session, &frame, 1 /* No ACK */);
  6589. } else {
  6590. rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
  6591. }
  6592. nghttp2_mem_free(mem, iv);
  6593. if (rv != 0) {
  6594. return rv;
  6595. }
  6596. nghttp2_priority_spec_default_init(&pri_spec);
  6597. stream = nghttp2_session_open_stream(
  6598. session, 1, NGHTTP2_STREAM_FLAG_NONE, &pri_spec, NGHTTP2_STREAM_OPENING,
  6599. session->server ? NULL : stream_user_data);
  6600. if (stream == NULL) {
  6601. return NGHTTP2_ERR_NOMEM;
  6602. }
  6603. /* We don't call nghttp2_session_adjust_closed_stream(), since this
  6604. should be the first stream open. */
  6605. if (session->server) {
  6606. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
  6607. session->last_recv_stream_id = 1;
  6608. session->last_proc_stream_id = 1;
  6609. } else {
  6610. nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
  6611. session->last_sent_stream_id = 1;
  6612. session->next_stream_id += 2;
  6613. }
  6614. return 0;
  6615. }
  6616. int nghttp2_session_upgrade(nghttp2_session *session,
  6617. const uint8_t *settings_payload,
  6618. size_t settings_payloadlen,
  6619. void *stream_user_data) {
  6620. int rv;
  6621. nghttp2_stream *stream;
  6622. rv = nghttp2_session_upgrade_internal(session, settings_payload,
  6623. settings_payloadlen, stream_user_data);
  6624. if (rv != 0) {
  6625. return rv;
  6626. }
  6627. stream = nghttp2_session_get_stream(session, 1);
  6628. assert(stream);
  6629. /* We have no information about request header fields when Upgrade
  6630. was happened. So we don't know the request method here. If
  6631. request method is HEAD, we have a trouble because we may have
  6632. nonzero content-length header field in response headers, and we
  6633. will going to check it against the actual DATA frames, but we may
  6634. get mismatch because HEAD response body must be empty. Because
  6635. of this reason, nghttp2_session_upgrade() was deprecated in favor
  6636. of nghttp2_session_upgrade2(), which has |head_request| parameter
  6637. to indicate that request method is HEAD or not. */
  6638. stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND;
  6639. return 0;
  6640. }
  6641. int nghttp2_session_upgrade2(nghttp2_session *session,
  6642. const uint8_t *settings_payload,
  6643. size_t settings_payloadlen, int head_request,
  6644. void *stream_user_data) {
  6645. int rv;
  6646. nghttp2_stream *stream;
  6647. rv = nghttp2_session_upgrade_internal(session, settings_payload,
  6648. settings_payloadlen, stream_user_data);
  6649. if (rv != 0) {
  6650. return rv;
  6651. }
  6652. stream = nghttp2_session_get_stream(session, 1);
  6653. assert(stream);
  6654. if (head_request) {
  6655. stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_HEAD;
  6656. }
  6657. return 0;
  6658. }
  6659. int nghttp2_session_get_stream_local_close(nghttp2_session *session,
  6660. int32_t stream_id) {
  6661. nghttp2_stream *stream;
  6662. stream = nghttp2_session_get_stream(session, stream_id);
  6663. if (!stream) {
  6664. return -1;
  6665. }
  6666. return (stream->shut_flags & NGHTTP2_SHUT_WR) != 0;
  6667. }
  6668. int nghttp2_session_get_stream_remote_close(nghttp2_session *session,
  6669. int32_t stream_id) {
  6670. nghttp2_stream *stream;
  6671. stream = nghttp2_session_get_stream(session, stream_id);
  6672. if (!stream) {
  6673. return -1;
  6674. }
  6675. return (stream->shut_flags & NGHTTP2_SHUT_RD) != 0;
  6676. }
  6677. int nghttp2_session_consume(nghttp2_session *session, int32_t stream_id,
  6678. size_t size) {
  6679. int rv;
  6680. nghttp2_stream *stream;
  6681. if (stream_id == 0) {
  6682. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6683. }
  6684. if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
  6685. return NGHTTP2_ERR_INVALID_STATE;
  6686. }
  6687. rv = session_update_connection_consumed_size(session, size);
  6688. if (nghttp2_is_fatal(rv)) {
  6689. return rv;
  6690. }
  6691. stream = nghttp2_session_get_stream(session, stream_id);
  6692. if (!stream) {
  6693. return 0;
  6694. }
  6695. rv = session_update_stream_consumed_size(session, stream, size);
  6696. if (nghttp2_is_fatal(rv)) {
  6697. return rv;
  6698. }
  6699. return 0;
  6700. }
  6701. int nghttp2_session_consume_connection(nghttp2_session *session, size_t size) {
  6702. int rv;
  6703. if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
  6704. return NGHTTP2_ERR_INVALID_STATE;
  6705. }
  6706. rv = session_update_connection_consumed_size(session, size);
  6707. if (nghttp2_is_fatal(rv)) {
  6708. return rv;
  6709. }
  6710. return 0;
  6711. }
  6712. int nghttp2_session_consume_stream(nghttp2_session *session, int32_t stream_id,
  6713. size_t size) {
  6714. int rv;
  6715. nghttp2_stream *stream;
  6716. if (stream_id == 0) {
  6717. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6718. }
  6719. if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
  6720. return NGHTTP2_ERR_INVALID_STATE;
  6721. }
  6722. stream = nghttp2_session_get_stream(session, stream_id);
  6723. if (!stream) {
  6724. return 0;
  6725. }
  6726. rv = session_update_stream_consumed_size(session, stream, size);
  6727. if (nghttp2_is_fatal(rv)) {
  6728. return rv;
  6729. }
  6730. return 0;
  6731. }
  6732. int nghttp2_session_set_next_stream_id(nghttp2_session *session,
  6733. int32_t next_stream_id) {
  6734. if (next_stream_id <= 0 ||
  6735. session->next_stream_id > (uint32_t)next_stream_id) {
  6736. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6737. }
  6738. if (session->server) {
  6739. if (next_stream_id % 2) {
  6740. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6741. }
  6742. } else if (next_stream_id % 2 == 0) {
  6743. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6744. }
  6745. session->next_stream_id = (uint32_t)next_stream_id;
  6746. return 0;
  6747. }
  6748. uint32_t nghttp2_session_get_next_stream_id(nghttp2_session *session) {
  6749. return session->next_stream_id;
  6750. }
  6751. int32_t nghttp2_session_get_last_proc_stream_id(nghttp2_session *session) {
  6752. return session->last_proc_stream_id;
  6753. }
  6754. nghttp2_stream *nghttp2_session_find_stream(nghttp2_session *session,
  6755. int32_t stream_id) {
  6756. if (stream_id == 0) {
  6757. return &session->root;
  6758. }
  6759. return nghttp2_session_get_stream_raw(session, stream_id);
  6760. }
  6761. nghttp2_stream *nghttp2_session_get_root_stream(nghttp2_session *session) {
  6762. return &session->root;
  6763. }
  6764. int nghttp2_session_check_server_session(nghttp2_session *session) {
  6765. return session->server;
  6766. }
  6767. int nghttp2_session_change_stream_priority(
  6768. nghttp2_session *session, int32_t stream_id,
  6769. const nghttp2_priority_spec *pri_spec) {
  6770. int rv;
  6771. nghttp2_stream *stream;
  6772. nghttp2_priority_spec pri_spec_copy;
  6773. if (session->pending_no_rfc7540_priorities == 1) {
  6774. return 0;
  6775. }
  6776. if (stream_id == 0 || stream_id == pri_spec->stream_id) {
  6777. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6778. }
  6779. stream = nghttp2_session_get_stream_raw(session, stream_id);
  6780. if (!stream) {
  6781. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6782. }
  6783. pri_spec_copy = *pri_spec;
  6784. nghttp2_priority_spec_normalize_weight(&pri_spec_copy);
  6785. rv = nghttp2_session_reprioritize_stream(session, stream, &pri_spec_copy);
  6786. if (nghttp2_is_fatal(rv)) {
  6787. return rv;
  6788. }
  6789. /* We don't intentionally call nghttp2_session_adjust_idle_stream()
  6790. so that idle stream created by this function, and existing ones
  6791. are kept for application. We will adjust number of idle stream
  6792. in nghttp2_session_mem_send or nghttp2_session_mem_recv is
  6793. called. */
  6794. return 0;
  6795. }
  6796. int nghttp2_session_create_idle_stream(nghttp2_session *session,
  6797. int32_t stream_id,
  6798. const nghttp2_priority_spec *pri_spec) {
  6799. nghttp2_stream *stream;
  6800. nghttp2_priority_spec pri_spec_copy;
  6801. if (session->pending_no_rfc7540_priorities == 1) {
  6802. return 0;
  6803. }
  6804. if (stream_id == 0 || stream_id == pri_spec->stream_id ||
  6805. !session_detect_idle_stream(session, stream_id)) {
  6806. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6807. }
  6808. stream = nghttp2_session_get_stream_raw(session, stream_id);
  6809. if (stream) {
  6810. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6811. }
  6812. pri_spec_copy = *pri_spec;
  6813. nghttp2_priority_spec_normalize_weight(&pri_spec_copy);
  6814. stream =
  6815. nghttp2_session_open_stream(session, stream_id, NGHTTP2_STREAM_FLAG_NONE,
  6816. &pri_spec_copy, NGHTTP2_STREAM_IDLE, NULL);
  6817. if (!stream) {
  6818. return NGHTTP2_ERR_NOMEM;
  6819. }
  6820. /* We don't intentionally call nghttp2_session_adjust_idle_stream()
  6821. so that idle stream created by this function, and existing ones
  6822. are kept for application. We will adjust number of idle stream
  6823. in nghttp2_session_mem_send or nghttp2_session_mem_recv is
  6824. called. */
  6825. return 0;
  6826. }
  6827. size_t
  6828. nghttp2_session_get_hd_inflate_dynamic_table_size(nghttp2_session *session) {
  6829. return nghttp2_hd_inflate_get_dynamic_table_size(&session->hd_inflater);
  6830. }
  6831. size_t
  6832. nghttp2_session_get_hd_deflate_dynamic_table_size(nghttp2_session *session) {
  6833. return nghttp2_hd_deflate_get_dynamic_table_size(&session->hd_deflater);
  6834. }
  6835. void nghttp2_session_set_user_data(nghttp2_session *session, void *user_data) {
  6836. session->user_data = user_data;
  6837. }
  6838. int nghttp2_session_change_extpri_stream_priority(
  6839. nghttp2_session *session, int32_t stream_id,
  6840. const nghttp2_extpri *extpri_in, int ignore_client_signal) {
  6841. nghttp2_stream *stream;
  6842. nghttp2_extpri extpri = *extpri_in;
  6843. if (!session->server) {
  6844. return NGHTTP2_ERR_INVALID_STATE;
  6845. }
  6846. if (session->pending_no_rfc7540_priorities != 1) {
  6847. return 0;
  6848. }
  6849. if (stream_id == 0) {
  6850. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6851. }
  6852. stream = nghttp2_session_get_stream_raw(session, stream_id);
  6853. if (!stream) {
  6854. return NGHTTP2_ERR_INVALID_ARGUMENT;
  6855. }
  6856. if (extpri.urgency > NGHTTP2_EXTPRI_URGENCY_LOW) {
  6857. extpri.urgency = NGHTTP2_EXTPRI_URGENCY_LOW;
  6858. }
  6859. if (ignore_client_signal) {
  6860. stream->flags |= NGHTTP2_STREAM_FLAG_IGNORE_CLIENT_PRIORITIES;
  6861. }
  6862. return session_update_stream_priority(session, stream,
  6863. nghttp2_extpri_to_uint8(&extpri));
  6864. }