nghttp2.h 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #ifndef NGHTTP2_H
  5. #define NGHTTP2_H
  6. #ifdef IOTX_HTTP2_DEBUG
  7. #define DEBUGBUILD
  8. #endif
  9. /* Define WIN32 when build target is Win32 API (borrowed from
  10. libcurl) */
  11. #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  12. #define WIN32
  13. #define ssize_t unsigned int
  14. #endif
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <stdlib.h>
  19. #if defined(_MSC_VER) && (_MSC_VER < 1800)
  20. /* MSVC < 2013 does not have inttypes.h because it is not C99
  21. compliant. See compiler macros and version number in
  22. https://sourceforge.net/p/predef/wiki/Compilers/ */
  23. #include <stdint.h>
  24. #else /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
  25. #include <inttypes.h>
  26. #endif /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
  27. #include <sys/types.h>
  28. #include <stdarg.h>
  29. #include <nghttp2ver.h>
  30. #ifdef NGHTTP2_STATICLIB
  31. #define NGHTTP2_EXTERN
  32. #elif defined(WIN32__)
  33. #ifdef BUILDING_NGHTTP2
  34. #define NGHTTP2_EXTERN __declspec(dllexport)
  35. #else /* !BUILDING_NGHTTP2 */
  36. #define NGHTTP2_EXTERN __declspec(dllimport)
  37. #endif /* !BUILDING_NGHTTP2 */
  38. #else /* !defined(WIN32) */
  39. #ifdef BUILDING_NGHTTP2
  40. #define NGHTTP2_EXTERN __attribute__((visibility("default")))
  41. #else /* !BUILDING_NGHTTP2 */
  42. #define NGHTTP2_EXTERN
  43. #endif /* !BUILDING_NGHTTP2 */
  44. #endif /* !defined(WIN32) */
  45. /**
  46. * @macro
  47. *
  48. * The protocol version identification string of this library
  49. * supports. This identifier is used if HTTP/2 is used over TLS.
  50. */
  51. #define NGHTTP2_PROTO_VERSION_ID "h2"
  52. /**
  53. * @macro
  54. *
  55. * The length of :macro:`NGHTTP2_PROTO_VERSION_ID`.
  56. */
  57. #define NGHTTP2_PROTO_VERSION_ID_LEN 2
  58. /**
  59. * @macro
  60. *
  61. * The serialized form of ALPN protocol identifier this library
  62. * supports. Notice that first byte is the length of following
  63. * protocol identifier. This is the same wire format of `TLS ALPN
  64. * extension <https://tools.ietf.org/html/rfc7301>`_. This is useful
  65. * to process incoming ALPN tokens in wire format.
  66. */
  67. #define NGHTTP2_PROTO_ALPN "\x2h2"
  68. /**
  69. * @macro
  70. *
  71. * The length of :macro:`NGHTTP2_PROTO_ALPN`.
  72. */
  73. #define NGHTTP2_PROTO_ALPN_LEN (sizeof(NGHTTP2_PROTO_ALPN) - 1)
  74. /**
  75. * @macro
  76. *
  77. * The protocol version identification string of this library
  78. * supports. This identifier is used if HTTP/2 is used over cleartext
  79. * TCP.
  80. */
  81. #define NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "h2c"
  82. /**
  83. * @macro
  84. *
  85. * The length of :macro:`NGHTTP2_CLEARTEXT_PROTO_VERSION_ID`.
  86. */
  87. #define NGHTTP2_CLEARTEXT_PROTO_VERSION_ID_LEN 3
  88. struct nghttp2_session;
  89. /**
  90. * @struct
  91. *
  92. * The primary structure to hold the resources needed for a HTTP/2
  93. * session. The details of this structure are intentionally hidden
  94. * from the public API.
  95. */
  96. typedef struct nghttp2_session nghttp2_session;
  97. /**
  98. * @macro
  99. *
  100. * The age of :type:`nghttp2_info`
  101. */
  102. #define NGHTTP2_VERSION_AGE 1
  103. #ifndef HTTP2_RECV_BUFFER_LENGHT
  104. #define HTTP2_RECV_BUFFER_LENGHT 16384
  105. #endif
  106. /**
  107. * @struct
  108. *
  109. * This struct is what `nghttp2_version()` returns. It holds
  110. * information about the particular nghttp2 version.
  111. */
  112. typedef struct {
  113. /**
  114. * Age of this struct. This instance of nghttp2 sets it to
  115. * :macro:`NGHTTP2_VERSION_AGE` but a future version may bump it and
  116. * add more struct fields at the bottom
  117. */
  118. int age;
  119. /**
  120. * the :macro:`NGHTTP2_VERSION_NUM` number (since age ==1)
  121. */
  122. int version_num;
  123. /**
  124. * points to the :macro:`NGHTTP2_VERSION` string (since age ==1)
  125. */
  126. const char *version_str;
  127. /**
  128. * points to the :macro:`NGHTTP2_PROTO_VERSION_ID` string this
  129. * instance implements (since age ==1)
  130. */
  131. const char *proto_str;
  132. /* -------- the above fields all exist when age == 1 */
  133. } nghttp2_info;
  134. /**
  135. * @macro
  136. *
  137. * The default weight of stream dependency.
  138. */
  139. #define NGHTTP2_DEFAULT_WEIGHT 16
  140. /**
  141. * @macro
  142. *
  143. * The maximum weight of stream dependency.
  144. */
  145. #define NGHTTP2_MAX_WEIGHT 256
  146. /**
  147. * @macro
  148. *
  149. * The minimum weight of stream dependency.
  150. */
  151. #define NGHTTP2_MIN_WEIGHT 1
  152. /**
  153. * @macro
  154. *
  155. * The maximum window size
  156. */
  157. #define NGHTTP2_MAX_WINDOW_SIZE ((int32_t)((1U << 31) - 1))
  158. /**
  159. * @macro
  160. *
  161. * The initial window size for stream level flow control.
  162. */
  163. #define NGHTTP2_INITIAL_WINDOW_SIZE ((1 << 24) - 1)
  164. /**
  165. * @macro
  166. *
  167. * The initial window size for connection level flow control.
  168. */
  169. #define NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE ((1 << 24) - 1)
  170. /**
  171. * @macro
  172. *
  173. * The default header table size.
  174. */
  175. #define NGHTTP2_DEFAULT_HEADER_TABLE_SIZE (1 << 12)
  176. /**
  177. * @macro
  178. *
  179. * The client magic string, which is the first 24 bytes byte string of
  180. * client connection preface.
  181. */
  182. #define NGHTTP2_CLIENT_MAGIC "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  183. /**
  184. * @macro
  185. *
  186. * The length of :macro:`NGHTTP2_CLIENT_MAGIC`.
  187. */
  188. #define NGHTTP2_CLIENT_MAGIC_LEN 24
  189. /**
  190. * @enum
  191. *
  192. * Error codes used in this library. The code range is [-999, -500],
  193. * inclusive. The following values are defined:
  194. */
  195. typedef enum {
  196. /**
  197. * Invalid argument passed.
  198. */
  199. NGHTTP2_ERR_INVALID_ARGUMENT = -501,
  200. /**
  201. * Out of buffer space.
  202. */
  203. NGHTTP2_ERR_BUFFER_ERROR = -502,
  204. /**
  205. * The specified protocol version is not supported.
  206. */
  207. NGHTTP2_ERR_UNSUPPORTED_VERSION = -503,
  208. /**
  209. * Used as a return value from :type:`nghttp2_send_callback`,
  210. * :type:`nghttp2_recv_callback` and
  211. * :type:`nghttp2_send_data_callback` to indicate that the operation
  212. * would block.
  213. */
  214. NGHTTP2_ERR_WOULDBLOCK = -504,
  215. /**
  216. * General protocol error
  217. */
  218. NGHTTP2_ERR_PROTO = -505,
  219. /**
  220. * The frame is invalid.
  221. */
  222. NGHTTP2_ERR_INVALID_FRAME = -506,
  223. /**
  224. * The peer performed a shutdown on the connection.
  225. */
  226. NGHTTP2_ERR_EOF = -507,
  227. /**
  228. * Used as a return value from
  229. * :func:`nghttp2_data_source_read_callback` to indicate that data
  230. * transfer is postponed. See
  231. * :func:`nghttp2_data_source_read_callback` for details.
  232. */
  233. NGHTTP2_ERR_DEFERRED = -508,
  234. /**
  235. * Stream ID has reached the maximum value. Therefore no stream ID
  236. * is available.
  237. */
  238. NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE = -509,
  239. /**
  240. * The stream is already closed; or the stream ID is invalid.
  241. */
  242. NGHTTP2_ERR_STREAM_CLOSED = -510,
  243. /**
  244. * RST_STREAM has been added to the outbound queue. The stream is
  245. * in closing state.
  246. */
  247. NGHTTP2_ERR_STREAM_CLOSING = -511,
  248. /**
  249. * The transmission is not allowed for this stream (e.g., a frame
  250. * with END_STREAM flag set has already sent).
  251. */
  252. NGHTTP2_ERR_STREAM_SHUT_WR = -512,
  253. /**
  254. * The stream ID is invalid.
  255. */
  256. NGHTTP2_ERR_INVALID_STREAM_ID = -513,
  257. /**
  258. * The state of the stream is not valid (e.g., DATA cannot be sent
  259. * to the stream if response HEADERS has not been sent).
  260. */
  261. NGHTTP2_ERR_INVALID_STREAM_STATE = -514,
  262. /**
  263. * Another DATA frame has already been deferred.
  264. */
  265. NGHTTP2_ERR_DEFERRED_DATA_EXIST = -515,
  266. /**
  267. * Starting new stream is not allowed (e.g., GOAWAY has been sent
  268. * and/or received).
  269. */
  270. NGHTTP2_ERR_START_STREAM_NOT_ALLOWED = -516,
  271. /**
  272. * GOAWAY has already been sent.
  273. */
  274. NGHTTP2_ERR_GOAWAY_ALREADY_SENT = -517,
  275. /**
  276. * The received frame contains the invalid header block (e.g., There
  277. * are duplicate header names; or the header names are not encoded
  278. * in US-ASCII character set and not lower cased; or the header name
  279. * is zero-length string; or the header value contains multiple
  280. * in-sequence NUL bytes).
  281. */
  282. NGHTTP2_ERR_INVALID_HEADER_BLOCK = -518,
  283. /**
  284. * Indicates that the context is not suitable to perform the
  285. * requested operation.
  286. */
  287. NGHTTP2_ERR_INVALID_STATE = -519,
  288. /**
  289. * The user callback function failed due to the temporal error.
  290. */
  291. NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE = -521,
  292. /**
  293. * The length of the frame is invalid, either too large or too small.
  294. */
  295. NGHTTP2_ERR_FRAME_SIZE_ERROR = -522,
  296. /**
  297. * Header block inflate/deflate error.
  298. */
  299. NGHTTP2_ERR_HEADER_COMP = -523,
  300. /**
  301. * Flow control error
  302. */
  303. NGHTTP2_ERR_FLOW_CONTROL = -524,
  304. /**
  305. * Insufficient buffer size given to function.
  306. */
  307. NGHTTP2_ERR_INSUFF_BUFSIZE = -525,
  308. /**
  309. * Callback was paused by the application
  310. */
  311. NGHTTP2_ERR_PAUSE = -526,
  312. /**
  313. * There are too many in-flight SETTING frame and no more
  314. * transmission of SETTINGS is allowed.
  315. */
  316. NGHTTP2_ERR_TOO_MANY_INFLIGHT_SETTINGS = -527,
  317. /**
  318. * The server push is disabled.
  319. */
  320. NGHTTP2_ERR_PUSH_DISABLED = -528,
  321. /**
  322. * DATA or HEADERS frame for a given stream has been already
  323. * submitted and has not been fully processed yet. Application
  324. * should wait for the transmission of the previously submitted
  325. * frame before submitting another.
  326. */
  327. NGHTTP2_ERR_DATA_EXIST = -529,
  328. /**
  329. * The current session is closing due to a connection error or
  330. * `nghttp2_session_terminate_session()` is called.
  331. */
  332. NGHTTP2_ERR_SESSION_CLOSING = -530,
  333. /**
  334. * Invalid HTTP header field was received and stream is going to be
  335. * closed.
  336. */
  337. NGHTTP2_ERR_HTTP_HEADER = -531,
  338. /**
  339. * Violation in HTTP messaging rule.
  340. */
  341. NGHTTP2_ERR_HTTP_MESSAGING = -532,
  342. /**
  343. * Stream was refused.
  344. */
  345. NGHTTP2_ERR_REFUSED_STREAM = -533,
  346. /**
  347. * Unexpected internal error, but recovered.
  348. */
  349. NGHTTP2_ERR_INTERNAL = -534,
  350. /**
  351. * Indicates that a processing was canceled.
  352. */
  353. NGHTTP2_ERR_CANCEL = -535,
  354. /**
  355. * When a local endpoint expects to receive SETTINGS frame, it
  356. * receives an other type of frame.
  357. */
  358. NGHTTP2_ERR_SETTINGS_EXPECTED = -536,
  359. /**
  360. * The errors < :enum:`NGHTTP2_ERR_FATAL` mean that the library is
  361. * under unexpected condition and processing was terminated (e.g.,
  362. * out of memory). If application receives this error code, it must
  363. * stop using that :type:`nghttp2_session` object and only allowed
  364. * operation for that object is deallocate it using
  365. * `nghttp2_session_del()`.
  366. */
  367. NGHTTP2_ERR_FATAL = -900,
  368. /**
  369. * Out of memory. This is a fatal error.
  370. */
  371. NGHTTP2_ERR_NOMEM = -901,
  372. /**
  373. * The user callback function failed. This is a fatal error.
  374. */
  375. NGHTTP2_ERR_CALLBACK_FAILURE = -902,
  376. /**
  377. * Invalid client magic (see :macro:`NGHTTP2_CLIENT_MAGIC`) was
  378. * received and further processing is not possible.
  379. */
  380. NGHTTP2_ERR_BAD_CLIENT_MAGIC = -903,
  381. /**
  382. * Possible flooding by peer was detected in this HTTP/2 session.
  383. * Flooding is measured by how many PING and SETTINGS frames with
  384. * ACK flag set are queued for transmission. These frames are
  385. * response for the peer initiated frames, and peer can cause memory
  386. * exhaustion on server side to send these frames forever and does
  387. * not read network.
  388. */
  389. NGHTTP2_ERR_FLOODED = -904
  390. } nghttp2_error;
  391. /**
  392. * @struct
  393. *
  394. * The object representing single contiguous buffer.
  395. */
  396. typedef struct {
  397. /**
  398. * The pointer to the buffer.
  399. */
  400. uint8_t *base;
  401. /**
  402. * The length of the buffer.
  403. */
  404. size_t len;
  405. } nghttp2_vec;
  406. struct nghttp2_rcbuf;
  407. /**
  408. * @struct
  409. *
  410. * The object representing reference counted buffer. The details of
  411. * this structure are intentionally hidden from the public API.
  412. */
  413. typedef struct nghttp2_rcbuf nghttp2_rcbuf;
  414. /**
  415. * @function
  416. *
  417. * Increments the reference count of |rcbuf| by 1.
  418. */
  419. NGHTTP2_EXTERN void nghttp2_rcbuf_incref(nghttp2_rcbuf *rcbuf);
  420. /**
  421. * @function
  422. *
  423. * Decrements the reference count of |rcbuf| by 1. If the reference
  424. * count becomes zero, the object pointed by |rcbuf| will be freed.
  425. * In this case, application must not use |rcbuf| again.
  426. */
  427. NGHTTP2_EXTERN void nghttp2_rcbuf_decref(nghttp2_rcbuf *rcbuf);
  428. /**
  429. * @function
  430. *
  431. * Returns the underlying buffer managed by |rcbuf|.
  432. */
  433. NGHTTP2_EXTERN nghttp2_vec nghttp2_rcbuf_get_buf(nghttp2_rcbuf *rcbuf);
  434. /**
  435. * @function
  436. *
  437. * Returns nonzero if the underlying buffer is statically allocated,
  438. * and 0 otherwise. This can be useful for language bindings that wish
  439. * to avoid creating duplicate strings for these buffers.
  440. */
  441. NGHTTP2_EXTERN int nghttp2_rcbuf_is_static(const nghttp2_rcbuf *rcbuf);
  442. /**
  443. * @enum
  444. *
  445. * The flags for header field name/value pair.
  446. */
  447. typedef enum {
  448. /**
  449. * No flag set.
  450. */
  451. NGHTTP2_NV_FLAG_NONE = 0,
  452. /**
  453. * Indicates that this name/value pair must not be indexed ("Literal
  454. * Header Field never Indexed" representation must be used in HPACK
  455. * encoding). Other implementation calls this bit as "sensitive".
  456. */
  457. NGHTTP2_NV_FLAG_NO_INDEX = 0x01,
  458. /**
  459. * This flag is set solely by application. If this flag is set, the
  460. * library does not make a copy of header field name. This could
  461. * improve performance.
  462. */
  463. NGHTTP2_NV_FLAG_NO_COPY_NAME = 0x02,
  464. /**
  465. * This flag is set solely by application. If this flag is set, the
  466. * library does not make a copy of header field value. This could
  467. * improve performance.
  468. */
  469. NGHTTP2_NV_FLAG_NO_COPY_VALUE = 0x04
  470. } nghttp2_nv_flag;
  471. /**
  472. * @struct
  473. *
  474. * The name/value pair, which mainly used to represent header fields.
  475. */
  476. typedef struct {
  477. /**
  478. * The |name| byte string. If this struct is presented from library
  479. * (e.g., :type:`nghttp2_on_frame_recv_callback`), |name| is
  480. * guaranteed to be NULL-terminated. For some callbacks
  481. * (:type:`nghttp2_before_frame_send_callback`,
  482. * :type:`nghttp2_on_frame_send_callback`, and
  483. * :type:`nghttp2_on_frame_not_send_callback`), it may not be
  484. * NULL-terminated if header field is passed from application with
  485. * the flag :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`). When application
  486. * is constructing this struct, |name| is not required to be
  487. * NULL-terminated.
  488. */
  489. uint8_t *name;
  490. /**
  491. * The |value| byte string. If this struct is presented from
  492. * library (e.g., :type:`nghttp2_on_frame_recv_callback`), |value|
  493. * is guaranteed to be NULL-terminated. For some callbacks
  494. * (:type:`nghttp2_before_frame_send_callback`,
  495. * :type:`nghttp2_on_frame_send_callback`, and
  496. * :type:`nghttp2_on_frame_not_send_callback`), it may not be
  497. * NULL-terminated if header field is passed from application with
  498. * the flag :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE`). When
  499. * application is constructing this struct, |value| is not required
  500. * to be NULL-terminated.
  501. */
  502. uint8_t *value;
  503. /**
  504. * The length of the |name|, excluding terminating NULL.
  505. */
  506. size_t namelen;
  507. /**
  508. * The length of the |value|, excluding terminating NULL.
  509. */
  510. size_t valuelen;
  511. /**
  512. * Bitwise OR of one or more of :type:`nghttp2_nv_flag`.
  513. */
  514. uint8_t flags;
  515. } nghttp2_nv;
  516. /**
  517. * @enum
  518. *
  519. * The frame types in HTTP/2 specification.
  520. */
  521. typedef enum {
  522. /**
  523. * The DATA frame.
  524. */
  525. NGHTTP2_DATA = 0,
  526. /**
  527. * The HEADERS frame.
  528. */
  529. NGHTTP2_HEADERS = 0x01,
  530. /**
  531. * The PRIORITY frame.
  532. */
  533. NGHTTP2_PRIORITY = 0x02,
  534. /**
  535. * The RST_STREAM frame.
  536. */
  537. NGHTTP2_RST_STREAM = 0x03,
  538. /**
  539. * The SETTINGS frame.
  540. */
  541. NGHTTP2_SETTINGS = 0x04,
  542. /**
  543. * The PUSH_PROMISE frame.
  544. */
  545. NGHTTP2_PUSH_PROMISE = 0x05,
  546. /**
  547. * The PING frame.
  548. */
  549. NGHTTP2_PING = 0x06,
  550. /**
  551. * The GOAWAY frame.
  552. */
  553. NGHTTP2_GOAWAY = 0x07,
  554. /**
  555. * The WINDOW_UPDATE frame.
  556. */
  557. NGHTTP2_WINDOW_UPDATE = 0x08,
  558. /**
  559. * The CONTINUATION frame. This frame type won't be passed to any
  560. * callbacks because the library processes this frame type and its
  561. * preceding HEADERS/PUSH_PROMISE as a single frame.
  562. */
  563. NGHTTP2_CONTINUATION = 0x09,
  564. /**
  565. * The ALTSVC frame, which is defined in `RFC 7383
  566. * <https://tools.ietf.org/html/rfc7838#section-4>`_.
  567. */
  568. NGHTTP2_ALTSVC = 0x0a
  569. } nghttp2_frame_type;
  570. /**
  571. * @enum
  572. *
  573. * The flags for HTTP/2 frames. This enum defines all flags for all
  574. * frames.
  575. */
  576. typedef enum {
  577. /**
  578. * No flag set.
  579. */
  580. NGHTTP2_FLAG_NONE = 0,
  581. /**
  582. * The END_STREAM flag.
  583. */
  584. NGHTTP2_FLAG_END_STREAM = 0x01,
  585. /**
  586. * The END_HEADERS flag.
  587. */
  588. NGHTTP2_FLAG_END_HEADERS = 0x04,
  589. /**
  590. * The ACK flag.
  591. */
  592. NGHTTP2_FLAG_ACK = 0x01,
  593. /**
  594. * The PADDED flag.
  595. */
  596. NGHTTP2_FLAG_PADDED = 0x08,
  597. /**
  598. * The PRIORITY flag.
  599. */
  600. NGHTTP2_FLAG_PRIORITY = 0x20
  601. } nghttp2_flag;
  602. /**
  603. * @enum
  604. * The SETTINGS ID.
  605. */
  606. typedef enum {
  607. /**
  608. * SETTINGS_HEADER_TABLE_SIZE
  609. */
  610. NGHTTP2_SETTINGS_HEADER_TABLE_SIZE = 0x01,
  611. /**
  612. * SETTINGS_ENABLE_PUSH
  613. */
  614. NGHTTP2_SETTINGS_ENABLE_PUSH = 0x02,
  615. /**
  616. * SETTINGS_MAX_CONCURRENT_STREAMS
  617. */
  618. NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS = 0x03,
  619. /**
  620. * SETTINGS_INITIAL_WINDOW_SIZE
  621. */
  622. NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE = 0x04,
  623. /**
  624. * SETTINGS_MAX_FRAME_SIZE
  625. */
  626. NGHTTP2_SETTINGS_MAX_FRAME_SIZE = 0x05,
  627. /**
  628. * SETTINGS_MAX_HEADER_LIST_SIZE
  629. */
  630. NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE = 0x06
  631. } nghttp2_settings_id;
  632. /* Note: If we add SETTINGS, update the capacity of
  633. NGHTTP2_INBOUND_NUM_IV as well */
  634. /**
  635. * @macro
  636. *
  637. * .. warning::
  638. *
  639. * Deprecated. The initial max concurrent streams is 0xffffffffu.
  640. *
  641. * Default maximum number of incoming concurrent streams. Use
  642. * `nghttp2_submit_settings()` with
  643. * :enum:`NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS` to change the
  644. * maximum number of incoming concurrent streams.
  645. *
  646. * .. note::
  647. *
  648. * The maximum number of outgoing concurrent streams is 100 by
  649. * default.
  650. */
  651. #define NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS ((1U << 31) - 1)
  652. /**
  653. * @enum
  654. * The status codes for the RST_STREAM and GOAWAY frames.
  655. */
  656. typedef enum {
  657. /**
  658. * No errors.
  659. */
  660. NGHTTP2_NO_ERROR = 0x00,
  661. /**
  662. * PROTOCOL_ERROR
  663. */
  664. NGHTTP2_PROTOCOL_ERROR = 0x01,
  665. /**
  666. * INTERNAL_ERROR
  667. */
  668. NGHTTP2_INTERNAL_ERROR = 0x02,
  669. /**
  670. * FLOW_CONTROL_ERROR
  671. */
  672. NGHTTP2_FLOW_CONTROL_ERROR = 0x03,
  673. /**
  674. * SETTINGS_TIMEOUT
  675. */
  676. NGHTTP2_SETTINGS_TIMEOUT = 0x04,
  677. /**
  678. * STREAM_CLOSED
  679. */
  680. NGHTTP2_STREAM_CLOSED = 0x05,
  681. /**
  682. * FRAME_SIZE_ERROR
  683. */
  684. NGHTTP2_FRAME_SIZE_ERROR = 0x06,
  685. /**
  686. * REFUSED_STREAM
  687. */
  688. NGHTTP2_REFUSED_STREAM = 0x07,
  689. /**
  690. * CANCEL
  691. */
  692. NGHTTP2_CANCEL = 0x08,
  693. /**
  694. * COMPRESSION_ERROR
  695. */
  696. NGHTTP2_COMPRESSION_ERROR = 0x09,
  697. /**
  698. * CONNECT_ERROR
  699. */
  700. NGHTTP2_CONNECT_ERROR = 0x0a,
  701. /**
  702. * ENHANCE_YOUR_CALM
  703. */
  704. NGHTTP2_ENHANCE_YOUR_CALM = 0x0b,
  705. /**
  706. * INADEQUATE_SECURITY
  707. */
  708. NGHTTP2_INADEQUATE_SECURITY = 0x0c,
  709. /**
  710. * HTTP_1_1_REQUIRED
  711. */
  712. NGHTTP2_HTTP_1_1_REQUIRED = 0x0d
  713. } nghttp2_error_code;
  714. /**
  715. * @struct
  716. * The frame header.
  717. */
  718. typedef struct {
  719. /**
  720. * The length field of this frame, excluding frame header.
  721. */
  722. size_t length;
  723. /**
  724. * The stream identifier (aka, stream ID)
  725. */
  726. int32_t stream_id;
  727. /**
  728. * The type of this frame. See `nghttp2_frame_type`.
  729. */
  730. uint8_t type;
  731. /**
  732. * The flags.
  733. */
  734. uint8_t flags;
  735. /**
  736. * Reserved bit in frame header. Currently, this is always set to 0
  737. * and application should not expect something useful in here.
  738. */
  739. uint8_t reserved;
  740. } nghttp2_frame_hd;
  741. /**
  742. * @union
  743. *
  744. * This union represents the some kind of data source passed to
  745. * :type:`nghttp2_data_source_read_callback`.
  746. */
  747. typedef struct {
  748. /**
  749. * The integer field, suitable for a file descriptor.
  750. */
  751. int fd;
  752. /**
  753. * data length.
  754. */
  755. int len;
  756. /**
  757. * The pointer to an arbitrary object.
  758. */
  759. void *ptr;
  760. } nghttp2_data_source;
  761. /**
  762. * @enum
  763. *
  764. * The flags used to set in |data_flags| output parameter in
  765. * :type:`nghttp2_data_source_read_callback`.
  766. */
  767. typedef enum {
  768. /**
  769. * No flag set.
  770. */
  771. NGHTTP2_DATA_FLAG_NONE = 0,
  772. /**
  773. * Indicates EOF was sensed.
  774. */
  775. NGHTTP2_DATA_FLAG_EOF = 0x01,
  776. /**
  777. * Indicates that END_STREAM flag must not be set even if
  778. * NGHTTP2_DATA_FLAG_EOF is set. Usually this flag is used to send
  779. * trailer fields with `nghttp2_submit_request()` or
  780. * `nghttp2_submit_response()`.
  781. */
  782. NGHTTP2_DATA_FLAG_NO_END_STREAM = 0x02,
  783. /**
  784. * Indicates that application will send complete DATA frame in
  785. * :type:`nghttp2_send_data_callback`.
  786. */
  787. NGHTTP2_DATA_FLAG_NO_COPY = 0x04
  788. } nghttp2_data_flag;
  789. /**
  790. * @functypedef
  791. *
  792. * Callback function invoked when the library wants to read data from
  793. * the |source|. The read data is sent in the stream |stream_id|.
  794. * The implementation of this function must read at most |length|
  795. * bytes of data from |source| (or possibly other places) and store
  796. * them in |buf| and return number of data stored in |buf|. If EOF is
  797. * reached, set :enum:`NGHTTP2_DATA_FLAG_EOF` flag in |*data_flags|.
  798. *
  799. * Sometime it is desirable to avoid copying data into |buf| and let
  800. * application to send data directly. To achieve this, set
  801. * :enum:`NGHTTP2_DATA_FLAG_NO_COPY` to |*data_flags| (and possibly
  802. * other flags, just like when we do copy), and return the number of
  803. * bytes to send without copying data into |buf|. The library, seeing
  804. * :enum:`NGHTTP2_DATA_FLAG_NO_COPY`, will invoke
  805. * :type:`nghttp2_send_data_callback`. The application must send
  806. * complete DATA frame in that callback.
  807. *
  808. * If this callback is set by `nghttp2_submit_request()`,
  809. * `nghttp2_submit_response()` or `nghttp2_submit_headers()` and
  810. * `nghttp2_submit_data()` with flag parameter
  811. * :enum:`NGHTTP2_FLAG_END_STREAM` set, and
  812. * :enum:`NGHTTP2_DATA_FLAG_EOF` flag is set to |*data_flags|, DATA
  813. * frame will have END_STREAM flag set. Usually, this is expected
  814. * behaviour and all are fine. One exception is send trailer fields.
  815. * You cannot send trailer fields after sending frame with END_STREAM
  816. * set. To avoid this problem, one can set
  817. * :enum:`NGHTTP2_DATA_FLAG_NO_END_STREAM` along with
  818. * :enum:`NGHTTP2_DATA_FLAG_EOF` to signal the library not to set
  819. * END_STREAM in DATA frame. Then application can use
  820. * `nghttp2_submit_trailer()` to send trailer fields.
  821. * `nghttp2_submit_trailer()` can be called inside this callback.
  822. *
  823. * If the application wants to postpone DATA frames (e.g.,
  824. * asynchronous I/O, or reading data blocks for long time), it is
  825. * achieved by returning :enum:`NGHTTP2_ERR_DEFERRED` without reading
  826. * any data in this invocation. The library removes DATA frame from
  827. * the outgoing queue temporarily. To move back deferred DATA frame
  828. * to outgoing queue, call `nghttp2_session_resume_data()`.
  829. *
  830. * By default, |length| is limited to 16KiB at maximum. If peer
  831. * allows larger frames, application can enlarge transmission buffer
  832. * size. See :type:`nghttp2_data_source_read_length_callback` for
  833. * more details.
  834. *
  835. * If the application just wants to return from
  836. * `nghttp2_session_send()` or `nghttp2_session_mem_send()` without
  837. * sending anything, return :enum:`NGHTTP2_ERR_PAUSE`.
  838. *
  839. * In case of error, there are 2 choices. Returning
  840. * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close the stream
  841. * by issuing RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`. If a
  842. * different error code is desirable, use
  843. * `nghttp2_submit_rst_stream()` with a desired error code and then
  844. * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. Returning
  845. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will signal the entire session
  846. * failure.
  847. */
  848. typedef ssize_t (*nghttp2_data_source_read_callback)(
  849. nghttp2_session *session, int32_t stream_id, uint8_t *buf, size_t length,
  850. uint32_t *data_flags, nghttp2_data_source *source, void *user_data);
  851. /**
  852. * @struct
  853. *
  854. * This struct represents the data source and the way to read a chunk
  855. * of data from it.
  856. */
  857. typedef struct {
  858. /**
  859. * The data source.
  860. */
  861. nghttp2_data_source source;
  862. /**
  863. * The callback function to read a chunk of data from the |source|.
  864. */
  865. nghttp2_data_source_read_callback read_callback;
  866. } nghttp2_data_provider;
  867. /**
  868. * @struct
  869. *
  870. * The DATA frame. The received data is delivered via
  871. * :type:`nghttp2_on_data_chunk_recv_callback`.
  872. */
  873. typedef struct {
  874. nghttp2_frame_hd hd;
  875. /**
  876. * The length of the padding in this frame. This includes PAD_HIGH
  877. * and PAD_LOW.
  878. */
  879. size_t padlen;
  880. } nghttp2_data;
  881. /**
  882. * @enum
  883. *
  884. * The category of HEADERS, which indicates the role of the frame. In
  885. * HTTP/2 spec, request, response, push response and other arbitrary
  886. * headers (e.g., trailer fields) are all called just HEADERS. To
  887. * give the application the role of incoming HEADERS frame, we define
  888. * several categories.
  889. */
  890. typedef enum {
  891. /**
  892. * The HEADERS frame is opening new stream, which is analogous to
  893. * SYN_STREAM in SPDY.
  894. */
  895. NGHTTP2_HCAT_REQUEST = 0,
  896. /**
  897. * The HEADERS frame is the first response headers, which is
  898. * analogous to SYN_REPLY in SPDY.
  899. */
  900. NGHTTP2_HCAT_RESPONSE = 1,
  901. /**
  902. * The HEADERS frame is the first headers sent against reserved
  903. * stream.
  904. */
  905. NGHTTP2_HCAT_PUSH_RESPONSE = 2,
  906. /**
  907. * The HEADERS frame which does not apply for the above categories,
  908. * which is analogous to HEADERS in SPDY. If non-final response
  909. * (e.g., status 1xx) is used, final response HEADERS frame will be
  910. * categorized here.
  911. */
  912. NGHTTP2_HCAT_HEADERS = 3
  913. } nghttp2_headers_category;
  914. /**
  915. * @struct
  916. *
  917. * The structure to specify stream dependency.
  918. */
  919. typedef struct {
  920. /**
  921. * The stream ID of the stream to depend on. Specifying 0 makes
  922. * stream not depend any other stream.
  923. */
  924. int32_t stream_id;
  925. /**
  926. * The weight of this dependency.
  927. */
  928. int32_t weight;
  929. /**
  930. * nonzero means exclusive dependency
  931. */
  932. uint8_t exclusive;
  933. } nghttp2_priority_spec;
  934. /**
  935. * @struct
  936. *
  937. * The HEADERS frame. It has the following members:
  938. */
  939. typedef struct {
  940. /**
  941. * The frame header.
  942. */
  943. nghttp2_frame_hd hd;
  944. /**
  945. * The length of the padding in this frame. This includes PAD_HIGH
  946. * and PAD_LOW.
  947. */
  948. size_t padlen;
  949. /**
  950. * The priority specification
  951. */
  952. nghttp2_priority_spec pri_spec;
  953. /**
  954. * The name/value pairs.
  955. */
  956. nghttp2_nv *nva;
  957. /**
  958. * The number of name/value pairs in |nva|.
  959. */
  960. size_t nvlen;
  961. /**
  962. * The category of this HEADERS frame.
  963. */
  964. nghttp2_headers_category cat;
  965. } nghttp2_headers;
  966. /**
  967. * @struct
  968. *
  969. * The PRIORITY frame. It has the following members:
  970. */
  971. typedef struct {
  972. /**
  973. * The frame header.
  974. */
  975. nghttp2_frame_hd hd;
  976. /**
  977. * The priority specification.
  978. */
  979. nghttp2_priority_spec pri_spec;
  980. } nghttp2_priority;
  981. /**
  982. * @struct
  983. *
  984. * The RST_STREAM frame. It has the following members:
  985. */
  986. typedef struct {
  987. /**
  988. * The frame header.
  989. */
  990. nghttp2_frame_hd hd;
  991. /**
  992. * The error code. See :type:`nghttp2_error_code`.
  993. */
  994. uint32_t error_code;
  995. } nghttp2_rst_stream;
  996. /**
  997. * @struct
  998. *
  999. * The SETTINGS ID/Value pair. It has the following members:
  1000. */
  1001. typedef struct {
  1002. /**
  1003. * The SETTINGS ID. See :type:`nghttp2_settings_id`.
  1004. */
  1005. int32_t settings_id;
  1006. /**
  1007. * The value of this entry.
  1008. */
  1009. uint32_t value;
  1010. } nghttp2_settings_entry;
  1011. /**
  1012. * @struct
  1013. *
  1014. * The SETTINGS frame. It has the following members:
  1015. */
  1016. typedef struct {
  1017. /**
  1018. * The frame header.
  1019. */
  1020. nghttp2_frame_hd hd;
  1021. /**
  1022. * The number of SETTINGS ID/Value pairs in |iv|.
  1023. */
  1024. size_t niv;
  1025. /**
  1026. * The pointer to the array of SETTINGS ID/Value pair.
  1027. */
  1028. nghttp2_settings_entry *iv;
  1029. } nghttp2_settings;
  1030. /**
  1031. * @struct
  1032. *
  1033. * The PUSH_PROMISE frame. It has the following members:
  1034. */
  1035. typedef struct {
  1036. /**
  1037. * The frame header.
  1038. */
  1039. nghttp2_frame_hd hd;
  1040. /**
  1041. * The length of the padding in this frame. This includes PAD_HIGH
  1042. * and PAD_LOW.
  1043. */
  1044. size_t padlen;
  1045. /**
  1046. * The name/value pairs.
  1047. */
  1048. nghttp2_nv *nva;
  1049. /**
  1050. * The number of name/value pairs in |nva|.
  1051. */
  1052. size_t nvlen;
  1053. /**
  1054. * The promised stream ID
  1055. */
  1056. int32_t promised_stream_id;
  1057. /**
  1058. * Reserved bit. Currently this is always set to 0 and application
  1059. * should not expect something useful in here.
  1060. */
  1061. uint8_t reserved;
  1062. } nghttp2_push_promise;
  1063. /**
  1064. * @struct
  1065. *
  1066. * The PING frame. It has the following members:
  1067. */
  1068. typedef struct {
  1069. /**
  1070. * The frame header.
  1071. */
  1072. nghttp2_frame_hd hd;
  1073. /**
  1074. * The opaque data
  1075. */
  1076. uint8_t opaque_data[8];
  1077. } nghttp2_ping;
  1078. /**
  1079. * @struct
  1080. *
  1081. * The GOAWAY frame. It has the following members:
  1082. */
  1083. typedef struct {
  1084. /**
  1085. * The frame header.
  1086. */
  1087. nghttp2_frame_hd hd;
  1088. /**
  1089. * The last stream stream ID.
  1090. */
  1091. int32_t last_stream_id;
  1092. /**
  1093. * The error code. See :type:`nghttp2_error_code`.
  1094. */
  1095. uint32_t error_code;
  1096. /**
  1097. * The additional debug data
  1098. */
  1099. uint8_t *opaque_data;
  1100. /**
  1101. * The length of |opaque_data| member.
  1102. */
  1103. size_t opaque_data_len;
  1104. /**
  1105. * Reserved bit. Currently this is always set to 0 and application
  1106. * should not expect something useful in here.
  1107. */
  1108. uint8_t reserved;
  1109. } nghttp2_goaway;
  1110. /**
  1111. * @struct
  1112. *
  1113. * The WINDOW_UPDATE frame. It has the following members:
  1114. */
  1115. typedef struct {
  1116. /**
  1117. * The frame header.
  1118. */
  1119. nghttp2_frame_hd hd;
  1120. /**
  1121. * The window size increment.
  1122. */
  1123. int32_t window_size_increment;
  1124. /**
  1125. * Reserved bit. Currently this is always set to 0 and application
  1126. * should not expect something useful in here.
  1127. */
  1128. uint8_t reserved;
  1129. } nghttp2_window_update;
  1130. /**
  1131. * @struct
  1132. *
  1133. * The extension frame. It has following members:
  1134. */
  1135. typedef struct {
  1136. /**
  1137. * The frame header.
  1138. */
  1139. nghttp2_frame_hd hd;
  1140. /**
  1141. * The pointer to extension payload. The exact pointer type is
  1142. * determined by hd.type.
  1143. *
  1144. * Currently, no extension is supported. This is a place holder for
  1145. * the future extensions.
  1146. */
  1147. void *payload;
  1148. } nghttp2_extension;
  1149. /**
  1150. * @union
  1151. *
  1152. * This union includes all frames to pass them to various function
  1153. * calls as nghttp2_frame type. The CONTINUATION frame is omitted
  1154. * from here because the library deals with it internally.
  1155. */
  1156. typedef union {
  1157. /**
  1158. * The frame header, which is convenient to inspect frame header.
  1159. */
  1160. nghttp2_frame_hd hd;
  1161. /**
  1162. * The DATA frame.
  1163. */
  1164. nghttp2_data data;
  1165. /**
  1166. * The HEADERS frame.
  1167. */
  1168. nghttp2_headers headers;
  1169. /**
  1170. * The PRIORITY frame.
  1171. */
  1172. nghttp2_priority priority;
  1173. /**
  1174. * The RST_STREAM frame.
  1175. */
  1176. nghttp2_rst_stream rst_stream;
  1177. /**
  1178. * The SETTINGS frame.
  1179. */
  1180. nghttp2_settings settings;
  1181. /**
  1182. * The PUSH_PROMISE frame.
  1183. */
  1184. nghttp2_push_promise push_promise;
  1185. /**
  1186. * The PING frame.
  1187. */
  1188. nghttp2_ping ping;
  1189. /**
  1190. * The GOAWAY frame.
  1191. */
  1192. nghttp2_goaway goaway;
  1193. /**
  1194. * The WINDOW_UPDATE frame.
  1195. */
  1196. nghttp2_window_update window_update;
  1197. /**
  1198. * The extension frame.
  1199. */
  1200. nghttp2_extension ext;
  1201. } nghttp2_frame;
  1202. /**
  1203. * @functypedef
  1204. *
  1205. * Callback function invoked when |session| wants to send data to the
  1206. * remote peer. The implementation of this function must send at most
  1207. * |length| bytes of data stored in |data|. The |flags| is currently
  1208. * not used and always 0. It must return the number of bytes sent if
  1209. * it succeeds. If it cannot send any single byte without blocking,
  1210. * it must return :enum:`NGHTTP2_ERR_WOULDBLOCK`. For other errors,
  1211. * it must return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. The
  1212. * |user_data| pointer is the third argument passed in to the call to
  1213. * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
  1214. *
  1215. * This callback is required if the application uses
  1216. * `nghttp2_session_send()` to send data to the remote endpoint. If
  1217. * the application uses solely `nghttp2_session_mem_send()` instead,
  1218. * this callback function is unnecessary.
  1219. *
  1220. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1221. * `nghttp2_session_callbacks_set_send_callback()`.
  1222. *
  1223. * .. note::
  1224. *
  1225. * The |length| may be very small. If that is the case, and
  1226. * application disables Nagle algorithm (``TCP_NODELAY``), then just
  1227. * writing |data| to the network stack leads to very small packet,
  1228. * and it is very inefficient. An application should be responsible
  1229. * to buffer up small chunks of data as necessary to avoid this
  1230. * situation.
  1231. */
  1232. typedef ssize_t (*nghttp2_send_callback)(nghttp2_session *session,
  1233. const uint8_t *data, size_t length,
  1234. int flags, void *user_data);
  1235. /**
  1236. * @functypedef
  1237. *
  1238. * Callback function invoked when :enum:`NGHTTP2_DATA_FLAG_NO_COPY` is
  1239. * used in :type:`nghttp2_data_source_read_callback` to send complete
  1240. * DATA frame.
  1241. *
  1242. * The |frame| is a DATA frame to send. The |framehd| is the
  1243. * serialized frame header (9 bytes). The |length| is the length of
  1244. * application data to send (this does not include padding). The
  1245. * |source| is the same pointer passed to
  1246. * :type:`nghttp2_data_source_read_callback`.
  1247. *
  1248. * The application first must send frame header |framehd| of length 9
  1249. * bytes. If ``frame->data.padlen > 0``, send 1 byte of value
  1250. * ``frame->data.padlen - 1``. Then send exactly |length| bytes of
  1251. * application data. Finally, if ``frame->data.padlen > 1``, send
  1252. * ``frame->data.padlen - 1`` bytes of zero as padding.
  1253. *
  1254. * The application has to send complete DATA frame in this callback.
  1255. * If all data were written successfully, return 0.
  1256. *
  1257. * If it cannot send any data at all, just return
  1258. * :enum:`NGHTTP2_ERR_WOULDBLOCK`; the library will call this callback
  1259. * with the same parameters later (It is recommended to send complete
  1260. * DATA frame at once in this function to deal with error; if partial
  1261. * frame data has already sent, it is impossible to send another data
  1262. * in that state, and all we can do is tear down connection). When
  1263. * data is fully processed, but application wants to make
  1264. * `nghttp2_session_mem_send()` or `nghttp2_session_send()` return
  1265. * immediately without processing next frames, return
  1266. * :enum:`NGHTTP2_ERR_PAUSE`. If application decided to reset this
  1267. * stream, return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`, then
  1268. * the library will send RST_STREAM with INTERNAL_ERROR as error code.
  1269. * The application can also return
  1270. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`, which will result in
  1271. * connection closure. Returning any other value is treated as
  1272. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned.
  1273. */
  1274. typedef int (*nghttp2_send_data_callback)(nghttp2_session *session,
  1275. nghttp2_frame *frame,
  1276. const uint8_t *framehd, size_t length,
  1277. nghttp2_data_source *source,
  1278. void *user_data);
  1279. /**
  1280. * @functypedef
  1281. *
  1282. * Callback function invoked when |session| wants to receive data from
  1283. * the remote peer. The implementation of this function must read at
  1284. * most |length| bytes of data and store it in |buf|. The |flags| is
  1285. * currently not used and always 0. It must return the number of
  1286. * bytes written in |buf| if it succeeds. If it cannot read any
  1287. * single byte without blocking, it must return
  1288. * :enum:`NGHTTP2_ERR_WOULDBLOCK`. If it gets EOF before it reads any
  1289. * single byte, it must return :enum:`NGHTTP2_ERR_EOF`. For other
  1290. * errors, it must return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1291. * Returning 0 is treated as :enum:`NGHTTP2_ERR_WOULDBLOCK`. The
  1292. * |user_data| pointer is the third argument passed in to the call to
  1293. * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
  1294. *
  1295. * This callback is required if the application uses
  1296. * `nghttp2_session_recv()` to receive data from the remote endpoint.
  1297. * If the application uses solely `nghttp2_session_mem_recv()`
  1298. * instead, this callback function is unnecessary.
  1299. *
  1300. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1301. * `nghttp2_session_callbacks_set_recv_callback()`.
  1302. */
  1303. typedef ssize_t (*nghttp2_recv_callback)(nghttp2_session *session, uint8_t *buf,
  1304. size_t length, int flags,
  1305. void *user_data);
  1306. /**
  1307. * @functypedef
  1308. *
  1309. * Callback function invoked by `nghttp2_session_recv()` and
  1310. * `nghttp2_session_mem_recv()` when a frame is received. The
  1311. * |user_data| pointer is the third argument passed in to the call to
  1312. * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
  1313. *
  1314. * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen``
  1315. * member of their data structure are always ``NULL`` and 0
  1316. * respectively. The header name/value pairs are emitted via
  1317. * :type:`nghttp2_on_header_callback`.
  1318. *
  1319. * For HEADERS, PUSH_PROMISE and DATA frames, this callback may be
  1320. * called after stream is closed (see
  1321. * :type:`nghttp2_on_stream_close_callback`). The application should
  1322. * check that stream is still alive using its own stream management or
  1323. * :func:`nghttp2_session_get_stream_user_data()`.
  1324. *
  1325. * Only HEADERS and DATA frame can signal the end of incoming data.
  1326. * If ``frame->hd.flags & NGHTTP2_FLAG_END_STREAM`` is nonzero, the
  1327. * |frame| is the last frame from the remote peer in this stream.
  1328. *
  1329. * This callback won't be called for CONTINUATION frames.
  1330. * HEADERS/PUSH_PROMISE + CONTINUATIONs are treated as single frame.
  1331. *
  1332. * The implementation of this function must return 0 if it succeeds.
  1333. * If nonzero value is returned, it is treated as fatal error and
  1334. * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
  1335. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1336. *
  1337. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1338. * `nghttp2_session_callbacks_set_on_frame_recv_callback()`.
  1339. */
  1340. typedef int (*nghttp2_on_frame_recv_callback)(nghttp2_session *session,
  1341. const nghttp2_frame *frame,
  1342. void *user_data);
  1343. /**
  1344. * @functypedef
  1345. *
  1346. * Callback function invoked by `nghttp2_session_recv()` and
  1347. * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is
  1348. * received. The error is indicated by the |lib_error_code|, which is
  1349. * one of the values defined in :type:`nghttp2_error`. When this
  1350. * callback function is invoked, the library automatically submits
  1351. * either RST_STREAM or GOAWAY frame. The |user_data| pointer is the
  1352. * third argument passed in to the call to
  1353. * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
  1354. *
  1355. * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen``
  1356. * member of their data structure are always ``NULL`` and 0
  1357. * respectively.
  1358. *
  1359. * The implementation of this function must return 0 if it succeeds.
  1360. * If nonzero is returned, it is treated as fatal error and
  1361. * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
  1362. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1363. *
  1364. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1365. * `nghttp2_session_callbacks_set_on_invalid_frame_recv_callback()`.
  1366. */
  1367. typedef int (*nghttp2_on_invalid_frame_recv_callback)(
  1368. nghttp2_session *session, const nghttp2_frame *frame, int lib_error_code,
  1369. void *user_data);
  1370. /**
  1371. * @functypedef
  1372. *
  1373. * Callback function invoked when a chunk of data in DATA frame is
  1374. * received. The |stream_id| is the stream ID this DATA frame belongs
  1375. * to. The |flags| is the flags of DATA frame which this data chunk
  1376. * is contained. ``(flags & NGHTTP2_FLAG_END_STREAM) != 0`` does not
  1377. * necessarily mean this chunk of data is the last one in the stream.
  1378. * You should use :type:`nghttp2_on_frame_recv_callback` to know all
  1379. * data frames are received. The |user_data| pointer is the third
  1380. * argument passed in to the call to `nghttp2_session_client_new()` or
  1381. * `nghttp2_session_server_new()`.
  1382. *
  1383. * If the application uses `nghttp2_session_mem_recv()`, it can return
  1384. * :enum:`NGHTTP2_ERR_PAUSE` to make `nghttp2_session_mem_recv()`
  1385. * return without processing further input bytes. The memory by
  1386. * pointed by the |data| is retained until
  1387. * `nghttp2_session_mem_recv()` or `nghttp2_session_recv()` is called.
  1388. * The application must retain the input bytes which was used to
  1389. * produce the |data| parameter, because it may refer to the memory
  1390. * region included in the input bytes.
  1391. *
  1392. * The implementation of this function must return 0 if it succeeds.
  1393. * If nonzero is returned, it is treated as fatal error, and
  1394. * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
  1395. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1396. *
  1397. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1398. * `nghttp2_session_callbacks_set_on_data_chunk_recv_callback()`.
  1399. */
  1400. typedef int (*nghttp2_on_data_chunk_recv_callback)(nghttp2_session *session,
  1401. uint8_t flags,
  1402. int32_t stream_id,
  1403. const uint8_t *data,
  1404. size_t len, void *user_data);
  1405. /**
  1406. * @functypedef
  1407. *
  1408. * Callback function invoked just before the non-DATA frame |frame| is
  1409. * sent. The |user_data| pointer is the third argument passed in to
  1410. * the call to `nghttp2_session_client_new()` or
  1411. * `nghttp2_session_server_new()`.
  1412. *
  1413. * The implementation of this function must return 0 if it succeeds.
  1414. * It can also return :enum:`NGHTTP2_ERR_CANCEL` to cancel the
  1415. * transmission of the given frame.
  1416. *
  1417. * If there is a fatal error while executing this callback, the
  1418. * implementation should return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`,
  1419. * which makes `nghttp2_session_send()` and
  1420. * `nghttp2_session_mem_send()` functions immediately return
  1421. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1422. *
  1423. * If the other value is returned, it is treated as if
  1424. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned. But the
  1425. * implementation should not rely on this since the library may define
  1426. * new return value to extend its capability.
  1427. *
  1428. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1429. * `nghttp2_session_callbacks_set_before_frame_send_callback()`.
  1430. */
  1431. typedef int (*nghttp2_before_frame_send_callback)(nghttp2_session *session,
  1432. const nghttp2_frame *frame,
  1433. void *user_data);
  1434. /**
  1435. * @functypedef
  1436. *
  1437. * Callback function invoked after the frame |frame| is sent. The
  1438. * |user_data| pointer is the third argument passed in to the call to
  1439. * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
  1440. *
  1441. * The implementation of this function must return 0 if it succeeds.
  1442. * If nonzero is returned, it is treated as fatal error and
  1443. * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
  1444. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1445. *
  1446. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1447. * `nghttp2_session_callbacks_set_on_frame_send_callback()`.
  1448. */
  1449. typedef int (*nghttp2_on_frame_send_callback)(nghttp2_session *session,
  1450. const nghttp2_frame *frame,
  1451. void *user_data);
  1452. /**
  1453. * @functypedef
  1454. *
  1455. * Callback function invoked after the non-DATA frame |frame| is not
  1456. * sent because of the error. The error is indicated by the
  1457. * |lib_error_code|, which is one of the values defined in
  1458. * :type:`nghttp2_error`. The |user_data| pointer is the third
  1459. * argument passed in to the call to `nghttp2_session_client_new()` or
  1460. * `nghttp2_session_server_new()`.
  1461. *
  1462. * The implementation of this function must return 0 if it succeeds.
  1463. * If nonzero is returned, it is treated as fatal error and
  1464. * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
  1465. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1466. *
  1467. * `nghttp2_session_get_stream_user_data()` can be used to get
  1468. * associated data.
  1469. *
  1470. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1471. * `nghttp2_session_callbacks_set_on_frame_not_send_callback()`.
  1472. */
  1473. typedef int (*nghttp2_on_frame_not_send_callback)(nghttp2_session *session,
  1474. const nghttp2_frame *frame,
  1475. int lib_error_code,
  1476. void *user_data);
  1477. /**
  1478. * @functypedef
  1479. *
  1480. * Callback function invoked when the stream |stream_id| is closed.
  1481. * The reason of closure is indicated by the |error_code|. The
  1482. * |error_code| is usually one of :enum:`nghttp2_error_code`, but that
  1483. * is not guaranteed. The stream_user_data, which was specified in
  1484. * `nghttp2_submit_request()` or `nghttp2_submit_headers()`, is still
  1485. * available in this function. The |user_data| pointer is the third
  1486. * argument passed in to the call to `nghttp2_session_client_new()` or
  1487. * `nghttp2_session_server_new()`.
  1488. *
  1489. * This function is also called for a stream in reserved state.
  1490. *
  1491. * The implementation of this function must return 0 if it succeeds.
  1492. * If nonzero is returned, it is treated as fatal error and
  1493. * `nghttp2_session_recv()`, `nghttp2_session_mem_recv()`,
  1494. * `nghttp2_session_send()`, and `nghttp2_session_mem_send()`
  1495. * functions immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1496. *
  1497. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1498. * `nghttp2_session_callbacks_set_on_stream_close_callback()`.
  1499. */
  1500. typedef int (*nghttp2_on_stream_close_callback)(nghttp2_session *session,
  1501. int32_t stream_id,
  1502. uint32_t error_code,
  1503. void *user_data);
  1504. /**
  1505. * @functypedef
  1506. *
  1507. * Callback function invoked when the reception of header block in
  1508. * HEADERS or PUSH_PROMISE is started. Each header name/value pair
  1509. * will be emitted by :type:`nghttp2_on_header_callback`.
  1510. *
  1511. * The ``frame->hd.flags`` may not have
  1512. * :enum:`NGHTTP2_FLAG_END_HEADERS` flag set, which indicates that one
  1513. * or more CONTINUATION frames are involved. But the application does
  1514. * not need to care about that because the header name/value pairs are
  1515. * emitted transparently regardless of CONTINUATION frames.
  1516. *
  1517. * The server applications probably create an object to store
  1518. * information about new stream if ``frame->hd.type ==
  1519. * NGHTTP2_HEADERS`` and ``frame->headers.cat ==
  1520. * NGHTTP2_HCAT_REQUEST``. If |session| is configured as server side,
  1521. * ``frame->headers.cat`` is either ``NGHTTP2_HCAT_REQUEST``
  1522. * containing request headers or ``NGHTTP2_HCAT_HEADERS`` containing
  1523. * trailer fields and never get PUSH_PROMISE in this callback.
  1524. *
  1525. * For the client applications, ``frame->hd.type`` is either
  1526. * ``NGHTTP2_HEADERS`` or ``NGHTTP2_PUSH_PROMISE``. In case of
  1527. * ``NGHTTP2_HEADERS``, ``frame->headers.cat ==
  1528. * NGHTTP2_HCAT_RESPONSE`` means that it is the first response
  1529. * headers, but it may be non-final response which is indicated by 1xx
  1530. * status code. In this case, there may be zero or more HEADERS frame
  1531. * with ``frame->headers.cat == NGHTTP2_HCAT_HEADERS`` which has
  1532. * non-final response code and finally client gets exactly one HEADERS
  1533. * frame with ``frame->headers.cat == NGHTTP2_HCAT_HEADERS``
  1534. * containing final response headers (non-1xx status code). The
  1535. * trailer fields also has ``frame->headers.cat ==
  1536. * NGHTTP2_HCAT_HEADERS`` which does not contain any status code.
  1537. *
  1538. * Returning :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close
  1539. * the stream (promised stream if frame is PUSH_PROMISE) by issuing
  1540. * RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`. In this case,
  1541. * :type:`nghttp2_on_header_callback` and
  1542. * :type:`nghttp2_on_frame_recv_callback` will not be invoked. If a
  1543. * different error code is desirable, use
  1544. * `nghttp2_submit_rst_stream()` with a desired error code and then
  1545. * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. Again, use
  1546. * ``frame->push_promise.promised_stream_id`` as stream_id parameter
  1547. * in `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE.
  1548. *
  1549. * The implementation of this function must return 0 if it succeeds.
  1550. * It can return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` to
  1551. * reset the stream (promised stream if frame is PUSH_PROMISE). For
  1552. * critical errors, it must return
  1553. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If the other value is
  1554. * returned, it is treated as if :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
  1555. * is returned. If :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned,
  1556. * `nghttp2_session_mem_recv()` function will immediately return
  1557. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1558. *
  1559. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1560. * `nghttp2_session_callbacks_set_on_begin_headers_callback()`.
  1561. */
  1562. typedef int (*nghttp2_on_begin_headers_callback)(nghttp2_session *session,
  1563. const nghttp2_frame *frame,
  1564. void *user_data);
  1565. /**
  1566. * @functypedef
  1567. *
  1568. * Callback function invoked when a header name/value pair is received
  1569. * for the |frame|. The |name| of length |namelen| is header name.
  1570. * The |value| of length |valuelen| is header value. The |flags| is
  1571. * bitwise OR of one or more of :type:`nghttp2_nv_flag`.
  1572. *
  1573. * If :enum:`NGHTTP2_NV_FLAG_NO_INDEX` is set in |flags|, the receiver
  1574. * must not index this name/value pair when forwarding it to the next
  1575. * hop. More specifically, "Literal Header Field never Indexed"
  1576. * representation must be used in HPACK encoding.
  1577. *
  1578. * When this callback is invoked, ``frame->hd.type`` is either
  1579. * :enum:`NGHTTP2_HEADERS` or :enum:`NGHTTP2_PUSH_PROMISE`. After all
  1580. * header name/value pairs are processed with this callback, and no
  1581. * error has been detected, :type:`nghttp2_on_frame_recv_callback`
  1582. * will be invoked. If there is an error in decompression,
  1583. * :type:`nghttp2_on_frame_recv_callback` for the |frame| will not be
  1584. * invoked.
  1585. *
  1586. * Both |name| and |value| are guaranteed to be NULL-terminated. The
  1587. * |namelen| and |valuelen| do not include terminal NULL. If
  1588. * `nghttp2_option_set_no_http_messaging()` is used with nonzero
  1589. * value, NULL character may be included in |name| or |value| before
  1590. * terminating NULL.
  1591. *
  1592. * Please note that unless `nghttp2_option_set_no_http_messaging()` is
  1593. * used, nghttp2 library does perform validation against the |name|
  1594. * and the |value| using `nghttp2_check_header_name()` and
  1595. * `nghttp2_check_header_value()`. In addition to this, nghttp2
  1596. * performs validation based on HTTP Messaging rule, which is briefly
  1597. * explained in :ref:`http-messaging` section.
  1598. *
  1599. * If the application uses `nghttp2_session_mem_recv()`, it can return
  1600. * :enum:`NGHTTP2_ERR_PAUSE` to make `nghttp2_session_mem_recv()`
  1601. * return without processing further input bytes. The memory pointed
  1602. * by |frame|, |name| and |value| parameters are retained until
  1603. * `nghttp2_session_mem_recv()` or `nghttp2_session_recv()` is called.
  1604. * The application must retain the input bytes which was used to
  1605. * produce these parameters, because it may refer to the memory region
  1606. * included in the input bytes.
  1607. *
  1608. * Returning :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close
  1609. * the stream (promised stream if frame is PUSH_PROMISE) by issuing
  1610. * RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`. In this case,
  1611. * :type:`nghttp2_on_header_callback` and
  1612. * :type:`nghttp2_on_frame_recv_callback` will not be invoked. If a
  1613. * different error code is desirable, use
  1614. * `nghttp2_submit_rst_stream()` with a desired error code and then
  1615. * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. Again, use
  1616. * ``frame->push_promise.promised_stream_id`` as stream_id parameter
  1617. * in `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE.
  1618. *
  1619. * The implementation of this function must return 0 if it succeeds.
  1620. * It may return :enum:`NGHTTP2_ERR_PAUSE` or
  1621. * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. For other critical
  1622. * failures, it must return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If
  1623. * the other nonzero value is returned, it is treated as
  1624. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If
  1625. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned,
  1626. * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
  1627. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1628. *
  1629. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1630. * `nghttp2_session_callbacks_set_on_header_callback()`.
  1631. *
  1632. * .. warning::
  1633. *
  1634. * Application should properly limit the total buffer size to store
  1635. * incoming header fields. Without it, peer may send large number
  1636. * of header fields or large header fields to cause out of memory in
  1637. * local endpoint. Due to how HPACK works, peer can do this
  1638. * effectively without using much memory on their own.
  1639. */
  1640. typedef int (*nghttp2_on_header_callback)(nghttp2_session *session,
  1641. const nghttp2_frame *frame,
  1642. const uint8_t *name, size_t namelen,
  1643. const uint8_t *value, size_t valuelen,
  1644. uint8_t flags, void *user_data);
  1645. /**
  1646. * @functypedef
  1647. *
  1648. * Callback function invoked when a header name/value pair is received
  1649. * for the |frame|. The |name| is header name. The |value| is header
  1650. * value. The |flags| is bitwise OR of one or more of
  1651. * :type:`nghttp2_nv_flag`.
  1652. *
  1653. * This callback behaves like :type:`nghttp2_on_header_callback`,
  1654. * except that |name| and |value| are stored in reference counted
  1655. * buffer. If application wishes to keep these references without
  1656. * copying them, use `nghttp2_rcbuf_incref()` to increment their
  1657. * reference count. It is the application's responsibility to call
  1658. * `nghttp2_rcbuf_decref()` if they called `nghttp2_rcbuf_incref()` so
  1659. * as not to leak memory. If the |session| is created by
  1660. * `nghttp2_session_server_new3()` or `nghttp2_session_client_new3()`,
  1661. * the function to free memory is the one belongs to the mem
  1662. * parameter. As long as this free function alives, |name| and
  1663. * |value| can live after |session| was destroyed.
  1664. */
  1665. typedef int (*nghttp2_on_header_callback2)(nghttp2_session *session,
  1666. const nghttp2_frame *frame,
  1667. nghttp2_rcbuf *name,
  1668. nghttp2_rcbuf *value, uint8_t flags,
  1669. void *user_data);
  1670. /**
  1671. * @functypedef
  1672. *
  1673. * Callback function invoked when a invalid header name/value pair is
  1674. * received for the |frame|.
  1675. *
  1676. * The parameter and behaviour are similar to
  1677. * :type:`nghttp2_on_header_callback`. The difference is that this
  1678. * callback is only invoked when a invalid header name/value pair is
  1679. * received which is treated as stream error if this callback is not
  1680. * set. Only invalid regular header field are passed to this
  1681. * callback. In other words, invalid pseudo header field is not
  1682. * passed to this callback. Also header fields which includes upper
  1683. * cased latter are also treated as error without passing them to this
  1684. * callback.
  1685. *
  1686. * This callback is only considered if HTTP messaging validation is
  1687. * turned on (which is on by default, see
  1688. * `nghttp2_option_set_no_http_messaging()`).
  1689. *
  1690. * With this callback, application inspects the incoming invalid
  1691. * field, and it also can reset stream from this callback by returning
  1692. * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. By default, the
  1693. * error code is :enum:`NGHTTP2_PROTOCOL_ERROR`. To change the error
  1694. * code, call `nghttp2_submit_rst_stream()` with the error code of
  1695. * choice in addition to returning
  1696. * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
  1697. *
  1698. * If 0 is returned, the header field is ignored, and the stream is
  1699. * not reset.
  1700. */
  1701. typedef int (*nghttp2_on_invalid_header_callback)(
  1702. nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name,
  1703. size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags,
  1704. void *user_data);
  1705. /**
  1706. * @functypedef
  1707. *
  1708. * Callback function invoked when a invalid header name/value pair is
  1709. * received for the |frame|.
  1710. *
  1711. * The parameter and behaviour are similar to
  1712. * :type:`nghttp2_on_header_callback2`. The difference is that this
  1713. * callback is only invoked when a invalid header name/value pair is
  1714. * received which is silently ignored if this callback is not set.
  1715. * Only invalid regular header field are passed to this callback. In
  1716. * other words, invalid pseudo header field is not passed to this
  1717. * callback. Also header fields which includes upper cased latter are
  1718. * also treated as error without passing them to this callback.
  1719. *
  1720. * This callback is only considered if HTTP messaging validation is
  1721. * turned on (which is on by default, see
  1722. * `nghttp2_option_set_no_http_messaging()`).
  1723. *
  1724. * With this callback, application inspects the incoming invalid
  1725. * field, and it also can reset stream from this callback by returning
  1726. * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. By default, the
  1727. * error code is :enum:`NGHTTP2_INTERNAL_ERROR`. To change the error
  1728. * code, call `nghttp2_submit_rst_stream()` with the error code of
  1729. * choice in addition to returning
  1730. * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
  1731. */
  1732. typedef int (*nghttp2_on_invalid_header_callback2)(
  1733. nghttp2_session *session, const nghttp2_frame *frame, nghttp2_rcbuf *name,
  1734. nghttp2_rcbuf *value, uint8_t flags, void *user_data);
  1735. /**
  1736. * @functypedef
  1737. *
  1738. * Callback function invoked when the library asks application how
  1739. * many padding bytes are required for the transmission of the
  1740. * |frame|. The application must choose the total length of payload
  1741. * including padded bytes in range [frame->hd.length, max_payloadlen],
  1742. * inclusive. Choosing number not in this range will be treated as
  1743. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. Returning
  1744. * ``frame->hd.length`` means no padding is added. Returning
  1745. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will make
  1746. * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
  1747. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1748. *
  1749. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1750. * `nghttp2_session_callbacks_set_select_padding_callback()`.
  1751. */
  1752. typedef ssize_t (*nghttp2_select_padding_callback)(nghttp2_session *session,
  1753. const nghttp2_frame *frame,
  1754. size_t max_payloadlen,
  1755. void *user_data);
  1756. /**
  1757. * @functypedef
  1758. *
  1759. * Callback function invoked when library wants to get max length of
  1760. * data to send data to the remote peer. The implementation of this
  1761. * function should return a value in the following range. [1,
  1762. * min(|session_remote_window_size|, |stream_remote_window_size|,
  1763. * |remote_max_frame_size|)]. If a value greater than this range is
  1764. * returned than the max allow value will be used. Returning a value
  1765. * smaller than this range is treated as
  1766. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. The |frame_type| is provided
  1767. * for future extensibility and identifies the type of frame (see
  1768. * :type:`nghttp2_frame_type`) for which to get the length for.
  1769. * Currently supported frame types are: :enum:`NGHTTP2_DATA`.
  1770. *
  1771. * This callback can be used to control the length in bytes for which
  1772. * :type:`nghttp2_data_source_read_callback` is allowed to send to the
  1773. * remote endpoint. This callback is optional. Returning
  1774. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will signal the entire session
  1775. * failure.
  1776. *
  1777. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1778. * `nghttp2_session_callbacks_set_data_source_read_length_callback()`.
  1779. */
  1780. typedef ssize_t (*nghttp2_data_source_read_length_callback)(
  1781. nghttp2_session *session, uint8_t frame_type, int32_t stream_id,
  1782. int32_t session_remote_window_size, int32_t stream_remote_window_size,
  1783. uint32_t remote_max_frame_size, void *user_data);
  1784. /**
  1785. * @functypedef
  1786. *
  1787. * Callback function invoked when a frame header is received. The
  1788. * |hd| points to received frame header.
  1789. *
  1790. * Unlike :type:`nghttp2_on_frame_recv_callback`, this callback will
  1791. * also be called when frame header of CONTINUATION frame is received.
  1792. *
  1793. * If both :type:`nghttp2_on_begin_frame_callback` and
  1794. * :type:`nghttp2_on_begin_headers_callback` are set and HEADERS or
  1795. * PUSH_PROMISE is received, :type:`nghttp2_on_begin_frame_callback`
  1796. * will be called first.
  1797. *
  1798. * The implementation of this function must return 0 if it succeeds.
  1799. * If nonzero value is returned, it is treated as fatal error and
  1800. * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
  1801. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1802. *
  1803. * To set this callback to :type:`nghttp2_session_callbacks`, use
  1804. * `nghttp2_session_callbacks_set_on_begin_frame_callback()`.
  1805. */
  1806. typedef int (*nghttp2_on_begin_frame_callback)(nghttp2_session *session,
  1807. const nghttp2_frame_hd *hd,
  1808. void *user_data);
  1809. /**
  1810. * @functypedef
  1811. *
  1812. * Callback function invoked when chunk of extension frame payload is
  1813. * received. The |hd| points to frame header. The received
  1814. * chunk is |data| of length |len|.
  1815. *
  1816. * The implementation of this function must return 0 if it succeeds.
  1817. *
  1818. * To abort processing this extension frame, return
  1819. * :enum:`NGHTTP2_ERR_CANCEL`.
  1820. *
  1821. * If fatal error occurred, application should return
  1822. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. In this case,
  1823. * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
  1824. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If the
  1825. * other values are returned, currently they are treated as
  1826. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1827. */
  1828. typedef int (*nghttp2_on_extension_chunk_recv_callback)(
  1829. nghttp2_session *session, const nghttp2_frame_hd *hd, const uint8_t *data,
  1830. size_t len, void *user_data);
  1831. /**
  1832. * @functypedef
  1833. *
  1834. * Callback function invoked when library asks the application to
  1835. * unpack extension payload from its wire format. The extension
  1836. * payload has been passed to the application using
  1837. * :type:`nghttp2_on_extension_chunk_recv_callback`. The frame header
  1838. * is already unpacked by the library and provided as |hd|.
  1839. *
  1840. * To receive extension frames, the application must tell desired
  1841. * extension frame type to the library using
  1842. * `nghttp2_option_set_user_recv_extension_type()`.
  1843. *
  1844. * The implementation of this function may store the pointer to the
  1845. * created object as a result of unpacking in |*payload|, and returns
  1846. * 0. The pointer stored in |*payload| is opaque to the library, and
  1847. * the library does not own its pointer. |*payload| is initialized as
  1848. * ``NULL``. The |*payload| is available as ``frame->ext.payload`` in
  1849. * :type:`nghttp2_on_frame_recv_callback`. Therefore if application
  1850. * can free that memory inside :type:`nghttp2_on_frame_recv_callback`
  1851. * callback. Of course, application has a liberty not ot use
  1852. * |*payload|, and do its own mechanism to process extension frames.
  1853. *
  1854. * To abort processing this extension frame, return
  1855. * :enum:`NGHTTP2_ERR_CANCEL`.
  1856. *
  1857. * If fatal error occurred, application should return
  1858. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. In this case,
  1859. * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
  1860. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If the
  1861. * other values are returned, currently they are treated as
  1862. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1863. */
  1864. typedef int (*nghttp2_unpack_extension_callback)(nghttp2_session *session,
  1865. void **payload,
  1866. const nghttp2_frame_hd *hd,
  1867. void *user_data);
  1868. /**
  1869. * @functypedef
  1870. *
  1871. * Callback function invoked when library asks the application to pack
  1872. * extension payload in its wire format. The frame header will be
  1873. * packed by library. Application must pack payload only.
  1874. * ``frame->ext.payload`` is the object passed to
  1875. * `nghttp2_submit_extension()` as payload parameter. Application
  1876. * must pack extension payload to the |buf| of its capacity |len|
  1877. * bytes. The |len| is at least 16KiB.
  1878. *
  1879. * The implementation of this function should return the number of
  1880. * bytes written into |buf| when it succeeds.
  1881. *
  1882. * To abort processing this extension frame, return
  1883. * :enum:`NGHTTP2_ERR_CANCEL`, and
  1884. * :type:`nghttp2_on_frame_not_send_callback` will be invoked.
  1885. *
  1886. * If fatal error occurred, application should return
  1887. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. In this case,
  1888. * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
  1889. * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If the
  1890. * other values are returned, currently they are treated as
  1891. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If the return value is
  1892. * strictly larger than |len|, it is treated as
  1893. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
  1894. */
  1895. typedef ssize_t (*nghttp2_pack_extension_callback)(nghttp2_session *session,
  1896. uint8_t *buf, size_t len,
  1897. const nghttp2_frame *frame,
  1898. void *user_data);
  1899. /**
  1900. * @functypedef
  1901. *
  1902. * Callback function invoked when library provides the error message
  1903. * intended for human consumption. This callback is solely for
  1904. * debugging purpose. The |msg| is typically NULL-terminated string
  1905. * of length |len|. |len| does not include the sentinel NULL
  1906. * character.
  1907. *
  1908. * This function is deprecated. The new application should use
  1909. * :type:`nghttp2_error_callback2`.
  1910. *
  1911. * The format of error message may change between nghttp2 library
  1912. * versions. The application should not depend on the particular
  1913. * format.
  1914. *
  1915. * Normally, application should return 0 from this callback. If fatal
  1916. * error occurred while doing something in this callback, application
  1917. * should return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. In this case,
  1918. * library will return immediately with return value
  1919. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. Currently, if nonzero value
  1920. * is returned from this callback, they are treated as
  1921. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`, but application should not
  1922. * rely on this details.
  1923. */
  1924. typedef int (*nghttp2_error_callback)(nghttp2_session *session, const char *msg,
  1925. size_t len, void *user_data);
  1926. /**
  1927. * @functypedef
  1928. *
  1929. * Callback function invoked when library provides the error code, and
  1930. * message. This callback is solely for debugging purpose.
  1931. * |lib_error_code| is one of error code defined in
  1932. * :enum:`nghttp2_error`. The |msg| is typically NULL-terminated
  1933. * string of length |len|, and intended for human consumption. |len|
  1934. * does not include the sentinel NULL character.
  1935. *
  1936. * The format of error message may change between nghttp2 library
  1937. * versions. The application should not depend on the particular
  1938. * format.
  1939. *
  1940. * Normally, application should return 0 from this callback. If fatal
  1941. * error occurred while doing something in this callback, application
  1942. * should return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. In this case,
  1943. * library will return immediately with return value
  1944. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. Currently, if nonzero value
  1945. * is returned from this callback, they are treated as
  1946. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`, but application should not
  1947. * rely on this details.
  1948. */
  1949. typedef int (*nghttp2_error_callback2)(nghttp2_session *session,
  1950. int lib_error_code, const char *msg,
  1951. size_t len, void *user_data);
  1952. struct nghttp2_session_callbacks;
  1953. /**
  1954. * @struct
  1955. *
  1956. * Callback functions for :type:`nghttp2_session`. The details of
  1957. * this structure are intentionally hidden from the public API.
  1958. */
  1959. typedef struct nghttp2_session_callbacks nghttp2_session_callbacks;
  1960. /**
  1961. * @function
  1962. *
  1963. * Initializes |*callbacks_ptr| with NULL values.
  1964. *
  1965. * The initialized object can be used when initializing multiple
  1966. * :type:`nghttp2_session` objects.
  1967. *
  1968. * When the application finished using this object, it can use
  1969. * `nghttp2_session_callbacks_del()` to free its memory.
  1970. *
  1971. * This function returns 0 if it succeeds, or one of the following
  1972. * negative error codes:
  1973. *
  1974. * :enum:`NGHTTP2_ERR_NOMEM`
  1975. * Out of memory.
  1976. */
  1977. NGHTTP2_EXTERN int
  1978. nghttp2_session_callbacks_new(nghttp2_session_callbacks **callbacks_ptr);
  1979. /**
  1980. * @function
  1981. *
  1982. * Frees any resources allocated for |callbacks|. If |callbacks| is
  1983. * ``NULL``, this function does nothing.
  1984. */
  1985. NGHTTP2_EXTERN void
  1986. nghttp2_session_callbacks_del(nghttp2_session_callbacks *callbacks);
  1987. /**
  1988. * @function
  1989. *
  1990. * Sets callback function invoked when a session wants to send data to
  1991. * the remote peer. This callback is not necessary if the application
  1992. * uses solely `nghttp2_session_mem_send()` to serialize data to
  1993. * transmit.
  1994. */
  1995. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_send_callback(
  1996. nghttp2_session_callbacks *cbs, nghttp2_send_callback send_callback);
  1997. /**
  1998. * @function
  1999. *
  2000. * Sets callback function invoked when the a session wants to receive
  2001. * data from the remote peer. This callback is not necessary if the
  2002. * application uses solely `nghttp2_session_mem_recv()` to process
  2003. * received data.
  2004. */
  2005. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_recv_callback(
  2006. nghttp2_session_callbacks *cbs, nghttp2_recv_callback recv_callback);
  2007. /**
  2008. * @function
  2009. *
  2010. * Sets callback function invoked by `nghttp2_session_recv()` and
  2011. * `nghttp2_session_mem_recv()` when a frame is received.
  2012. */
  2013. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_recv_callback(
  2014. nghttp2_session_callbacks *cbs,
  2015. nghttp2_on_frame_recv_callback on_frame_recv_callback);
  2016. /**
  2017. * @function
  2018. *
  2019. * Sets callback function invoked by `nghttp2_session_recv()` and
  2020. * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is
  2021. * received.
  2022. */
  2023. NGHTTP2_EXTERN void
  2024. nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
  2025. nghttp2_session_callbacks *cbs,
  2026. nghttp2_on_invalid_frame_recv_callback on_invalid_frame_recv_callback);
  2027. /**
  2028. * @function
  2029. *
  2030. * Sets callback function invoked when a chunk of data in DATA frame
  2031. * is received.
  2032. */
  2033. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
  2034. nghttp2_session_callbacks *cbs,
  2035. nghttp2_on_data_chunk_recv_callback on_data_chunk_recv_callback);
  2036. /**
  2037. * @function
  2038. *
  2039. * Sets callback function invoked before a non-DATA frame is sent.
  2040. */
  2041. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_before_frame_send_callback(
  2042. nghttp2_session_callbacks *cbs,
  2043. nghttp2_before_frame_send_callback before_frame_send_callback);
  2044. /**
  2045. * @function
  2046. *
  2047. * Sets callback function invoked after a frame is sent.
  2048. */
  2049. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_send_callback(
  2050. nghttp2_session_callbacks *cbs,
  2051. nghttp2_on_frame_send_callback on_frame_send_callback);
  2052. /**
  2053. * @function
  2054. *
  2055. * Sets callback function invoked when a non-DATA frame is not sent
  2056. * because of an error.
  2057. */
  2058. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_not_send_callback(
  2059. nghttp2_session_callbacks *cbs,
  2060. nghttp2_on_frame_not_send_callback on_frame_not_send_callback);
  2061. /**
  2062. * @function
  2063. *
  2064. * Sets callback function invoked when the stream is closed.
  2065. */
  2066. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_stream_close_callback(
  2067. nghttp2_session_callbacks *cbs,
  2068. nghttp2_on_stream_close_callback on_stream_close_callback);
  2069. /**
  2070. * @function
  2071. *
  2072. * Sets callback function invoked when the reception of header block
  2073. * in HEADERS or PUSH_PROMISE is started.
  2074. */
  2075. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_begin_headers_callback(
  2076. nghttp2_session_callbacks *cbs,
  2077. nghttp2_on_begin_headers_callback on_begin_headers_callback);
  2078. /**
  2079. * @function
  2080. *
  2081. * Sets callback function invoked when a header name/value pair is
  2082. * received. If both
  2083. * `nghttp2_session_callbacks_set_on_header_callback()` and
  2084. * `nghttp2_session_callbacks_set_on_header_callback2()` are used to
  2085. * set callbacks, the latter has the precedence.
  2086. */
  2087. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_header_callback(
  2088. nghttp2_session_callbacks *cbs,
  2089. nghttp2_on_header_callback on_header_callback);
  2090. /**
  2091. * @function
  2092. *
  2093. * Sets callback function invoked when a header name/value pair is
  2094. * received.
  2095. */
  2096. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_header_callback2(
  2097. nghttp2_session_callbacks *cbs,
  2098. nghttp2_on_header_callback2 on_header_callback2);
  2099. /**
  2100. * @function
  2101. *
  2102. * Sets callback function invoked when a invalid header name/value
  2103. * pair is received. If both
  2104. * `nghttp2_session_callbacks_set_on_invalid_header_callback()` and
  2105. * `nghttp2_session_callbacks_set_on_invalid_header_callback2()` are
  2106. * used to set callbacks, the latter takes the precedence.
  2107. */
  2108. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_invalid_header_callback(
  2109. nghttp2_session_callbacks *cbs,
  2110. nghttp2_on_invalid_header_callback on_invalid_header_callback);
  2111. /**
  2112. * @function
  2113. *
  2114. * Sets callback function invoked when a invalid header name/value
  2115. * pair is received.
  2116. */
  2117. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_invalid_header_callback2(
  2118. nghttp2_session_callbacks *cbs,
  2119. nghttp2_on_invalid_header_callback2 on_invalid_header_callback2);
  2120. /**
  2121. * @function
  2122. *
  2123. * Sets callback function invoked when the library asks application
  2124. * how many padding bytes are required for the transmission of the
  2125. * given frame.
  2126. */
  2127. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_select_padding_callback(
  2128. nghttp2_session_callbacks *cbs,
  2129. nghttp2_select_padding_callback select_padding_callback);
  2130. /**
  2131. * @function
  2132. *
  2133. * Sets callback function determine the length allowed in
  2134. * :type:`nghttp2_data_source_read_callback`.
  2135. */
  2136. NGHTTP2_EXTERN void
  2137. nghttp2_session_callbacks_set_data_source_read_length_callback(
  2138. nghttp2_session_callbacks *cbs,
  2139. nghttp2_data_source_read_length_callback data_source_read_length_callback);
  2140. /**
  2141. * @function
  2142. *
  2143. * Sets callback function invoked when a frame header is received.
  2144. */
  2145. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_begin_frame_callback(
  2146. nghttp2_session_callbacks *cbs,
  2147. nghttp2_on_begin_frame_callback on_begin_frame_callback);
  2148. /**
  2149. * @function
  2150. *
  2151. * Sets callback function invoked when
  2152. * :enum:`NGHTTP2_DATA_FLAG_NO_COPY` is used in
  2153. * :type:`nghttp2_data_source_read_callback` to avoid data copy.
  2154. */
  2155. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_send_data_callback(
  2156. nghttp2_session_callbacks *cbs,
  2157. nghttp2_send_data_callback send_data_callback);
  2158. /**
  2159. * @function
  2160. *
  2161. * Sets callback function invoked when the library asks the
  2162. * application to pack extension frame payload in wire format.
  2163. */
  2164. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_pack_extension_callback(
  2165. nghttp2_session_callbacks *cbs,
  2166. nghttp2_pack_extension_callback pack_extension_callback);
  2167. /**
  2168. * @function
  2169. *
  2170. * Sets callback function invoked when the library asks the
  2171. * application to unpack extension frame payload from wire format.
  2172. */
  2173. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_unpack_extension_callback(
  2174. nghttp2_session_callbacks *cbs,
  2175. nghttp2_unpack_extension_callback unpack_extension_callback);
  2176. /**
  2177. * @function
  2178. *
  2179. * Sets callback function invoked when chunk of extension frame
  2180. * payload is received.
  2181. */
  2182. NGHTTP2_EXTERN void
  2183. nghttp2_session_callbacks_set_on_extension_chunk_recv_callback(
  2184. nghttp2_session_callbacks *cbs,
  2185. nghttp2_on_extension_chunk_recv_callback on_extension_chunk_recv_callback);
  2186. /**
  2187. * @function
  2188. *
  2189. * Sets callback function invoked when library tells error message to
  2190. * the application.
  2191. *
  2192. * This function is deprecated. The new application should use
  2193. * `nghttp2_session_callbacks_set_error_callback2()`.
  2194. *
  2195. * If both :type:`nghttp2_error_callback` and
  2196. * :type:`nghttp2_error_callback2` are set, the latter takes
  2197. * precedence.
  2198. */
  2199. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_error_callback(
  2200. nghttp2_session_callbacks *cbs, nghttp2_error_callback error_callback);
  2201. /**
  2202. * @function
  2203. *
  2204. * Sets callback function invoked when library tells error code, and
  2205. * message to the application.
  2206. *
  2207. * If both :type:`nghttp2_error_callback` and
  2208. * :type:`nghttp2_error_callback2` are set, the latter takes
  2209. * precedence.
  2210. */
  2211. NGHTTP2_EXTERN void nghttp2_session_callbacks_set_error_callback2(
  2212. nghttp2_session_callbacks *cbs, nghttp2_error_callback2 error_callback2);
  2213. /**
  2214. * @functypedef
  2215. *
  2216. * Custom memory allocator to replace malloc(). The |mem_user_data|
  2217. * is the mem_user_data member of :type:`nghttp2_mem` structure.
  2218. */
  2219. typedef void *(*nghttp2_malloc)(size_t size, void *mem_user_data);
  2220. /**
  2221. * @functypedef
  2222. *
  2223. * Custom memory allocator to replace free(). The |mem_user_data| is
  2224. * the mem_user_data member of :type:`nghttp2_mem` structure.
  2225. */
  2226. typedef void (*nghttp2_free)(void *ptr, void *mem_user_data);
  2227. /**
  2228. * @functypedef
  2229. *
  2230. * Custom memory allocator to replace calloc(). The |mem_user_data|
  2231. * is the mem_user_data member of :type:`nghttp2_mem` structure.
  2232. */
  2233. typedef void *(*nghttp2_calloc)(size_t nmemb, size_t size, void *mem_user_data);
  2234. /**
  2235. * @functypedef
  2236. *
  2237. * Custom memory allocator to replace realloc(). The |mem_user_data|
  2238. * is the mem_user_data member of :type:`nghttp2_mem` structure.
  2239. */
  2240. typedef void *(*nghttp2_realloc)(void *ptr, size_t size, void *mem_user_data);
  2241. /**
  2242. * @struct
  2243. *
  2244. * Custom memory allocator functions and user defined pointer. The
  2245. * |mem_user_data| member is passed to each allocator function. This
  2246. * can be used, for example, to achieve per-session memory pool.
  2247. *
  2248. * In the following example code, ``my_malloc``, ``my_free``,
  2249. * ``my_calloc`` and ``my_realloc`` are the replacement of the
  2250. * standard allocators ``malloc``, ``free``, ``calloc`` and
  2251. * ``realloc`` respectively::
  2252. *
  2253. * void *my_malloc_cb(size_t size, void *mem_user_data) {
  2254. * return my_malloc(size);
  2255. * }
  2256. *
  2257. * void my_free_cb(void *ptr, void *mem_user_data) { my_free(ptr); }
  2258. *
  2259. * void *my_calloc_cb(size_t nmemb, size_t size, void *mem_user_data) {
  2260. * return my_calloc(nmemb, size);
  2261. * }
  2262. *
  2263. * void *my_realloc_cb(void *ptr, size_t size, void *mem_user_data) {
  2264. * return my_realloc(ptr, size);
  2265. * }
  2266. *
  2267. * void session_new() {
  2268. * nghttp2_session *session;
  2269. * nghttp2_session_callbacks *callbacks;
  2270. * nghttp2_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb,
  2271. * my_realloc_cb};
  2272. *
  2273. * ...
  2274. *
  2275. * nghttp2_session_client_new3(&session, callbacks, NULL, NULL, &mem);
  2276. *
  2277. * ...
  2278. * }
  2279. */
  2280. typedef struct {
  2281. /**
  2282. * An arbitrary user supplied data. This is passed to each
  2283. * allocator function.
  2284. */
  2285. void *mem_user_data;
  2286. /**
  2287. * Custom allocator function to replace malloc().
  2288. */
  2289. nghttp2_malloc malloc;
  2290. /**
  2291. * Custom allocator function to replace free().
  2292. */
  2293. nghttp2_free free;
  2294. /**
  2295. * Custom allocator function to replace calloc().
  2296. */
  2297. nghttp2_calloc calloc;
  2298. /**
  2299. * Custom allocator function to replace realloc().
  2300. */
  2301. nghttp2_realloc realloc;
  2302. } nghttp2_mem;
  2303. struct nghttp2_option;
  2304. /**
  2305. * @struct
  2306. *
  2307. * Configuration options for :type:`nghttp2_session`. The details of
  2308. * this structure are intentionally hidden from the public API.
  2309. */
  2310. typedef struct nghttp2_option nghttp2_option;
  2311. /**
  2312. * @function
  2313. *
  2314. * Initializes |*option_ptr| with default values.
  2315. *
  2316. * When the application finished using this object, it can use
  2317. * `nghttp2_option_del()` to free its memory.
  2318. *
  2319. * This function returns 0 if it succeeds, or one of the following
  2320. * negative error codes:
  2321. *
  2322. * :enum:`NGHTTP2_ERR_NOMEM`
  2323. * Out of memory.
  2324. */
  2325. NGHTTP2_EXTERN int nghttp2_option_new(nghttp2_option **option_ptr);
  2326. /**
  2327. * @function
  2328. *
  2329. * Frees any resources allocated for |option|. If |option| is
  2330. * ``NULL``, this function does nothing.
  2331. */
  2332. NGHTTP2_EXTERN void nghttp2_option_del(nghttp2_option *option);
  2333. /**
  2334. * @function
  2335. *
  2336. * This option prevents the library from sending WINDOW_UPDATE for a
  2337. * connection automatically. If this option is set to nonzero, the
  2338. * library won't send WINDOW_UPDATE for DATA until application calls
  2339. * `nghttp2_session_consume()` to indicate the consumed amount of
  2340. * data. Don't use `nghttp2_submit_window_update()` for this purpose.
  2341. * By default, this option is set to zero.
  2342. */
  2343. NGHTTP2_EXTERN void
  2344. nghttp2_option_set_no_auto_window_update(nghttp2_option *option, int val);
  2345. /**
  2346. * @function
  2347. *
  2348. * This option sets the SETTINGS_MAX_CONCURRENT_STREAMS value of
  2349. * remote endpoint as if it is received in SETTINGS frame. Without
  2350. * specifying this option, before the local endpoint receives
  2351. * SETTINGS_MAX_CONCURRENT_STREAMS in SETTINGS frame from remote
  2352. * endpoint, SETTINGS_MAX_CONCURRENT_STREAMS is unlimited. This may
  2353. * cause problem if local endpoint submits lots of requests initially
  2354. * and sending them at once to the remote peer may lead to the
  2355. * rejection of some requests. Specifying this option to the sensible
  2356. * value, say 100, may avoid this kind of issue. This value will be
  2357. * overwritten if the local endpoint receives
  2358. * SETTINGS_MAX_CONCURRENT_STREAMS from the remote endpoint.
  2359. */
  2360. NGHTTP2_EXTERN void
  2361. nghttp2_option_set_peer_max_concurrent_streams(nghttp2_option *option,
  2362. uint32_t val);
  2363. /**
  2364. * @function
  2365. *
  2366. * By default, nghttp2 library, if configured as server, requires
  2367. * first 24 bytes of client magic byte string (MAGIC). In most cases,
  2368. * this will simplify the implementation of server. But sometimes
  2369. * server may want to detect the application protocol based on first
  2370. * few bytes on clear text communication.
  2371. *
  2372. * If this option is used with nonzero |val|, nghttp2 library does not
  2373. * handle MAGIC. It still checks following SETTINGS frame. This
  2374. * means that applications should deal with MAGIC by themselves.
  2375. *
  2376. * If this option is not used or used with zero value, if MAGIC does
  2377. * not match :macro:`NGHTTP2_CLIENT_MAGIC`, `nghttp2_session_recv()`
  2378. * and `nghttp2_session_mem_recv()` will return error
  2379. * :enum:`NGHTTP2_ERR_BAD_CLIENT_MAGIC`, which is fatal error.
  2380. */
  2381. NGHTTP2_EXTERN void
  2382. nghttp2_option_set_no_recv_client_magic(nghttp2_option *option, int val);
  2383. /**
  2384. * @function
  2385. *
  2386. * By default, nghttp2 library enforces subset of HTTP Messaging rules
  2387. * described in `HTTP/2 specification, section 8
  2388. * <https://tools.ietf.org/html/rfc7540#section-8>`_. See
  2389. * :ref:`http-messaging` section for details. For those applications
  2390. * who use nghttp2 library as non-HTTP use, give nonzero to |val| to
  2391. * disable this enforcement. Please note that disabling this feature
  2392. * does not change the fundamental client and server model of HTTP.
  2393. * That is, even if the validation is disabled, only client can send
  2394. * requests.
  2395. */
  2396. NGHTTP2_EXTERN void nghttp2_option_set_no_http_messaging(nghttp2_option *option,
  2397. int val);
  2398. /**
  2399. * @function
  2400. *
  2401. * RFC 7540 does not enforce any limit on the number of incoming
  2402. * reserved streams (in RFC 7540 terms, streams in reserved (remote)
  2403. * state). This only affects client side, since only server can push
  2404. * streams. Malicious server can push arbitrary number of streams,
  2405. * and make client's memory exhausted. This option can set the
  2406. * maximum number of such incoming streams to avoid possible memory
  2407. * exhaustion. If this option is set, and pushed streams are
  2408. * automatically closed on reception, without calling user provided
  2409. * callback, if they exceed the given limit. The default value is
  2410. * 200. If session is configured as server side, this option has no
  2411. * effect. Server can control the number of streams to push.
  2412. */
  2413. NGHTTP2_EXTERN void
  2414. nghttp2_option_set_max_reserved_remote_streams(nghttp2_option *option,
  2415. uint32_t val);
  2416. /**
  2417. * @function
  2418. *
  2419. * Sets extension frame type the application is willing to handle with
  2420. * user defined callbacks (see
  2421. * :type:`nghttp2_on_extension_chunk_recv_callback` and
  2422. * :type:`nghttp2_unpack_extension_callback`). The |type| is
  2423. * extension frame type, and must be strictly greater than 0x9.
  2424. * Otherwise, this function does nothing. The application can call
  2425. * this function multiple times to set more than one frame type to
  2426. * receive. The application does not have to call this function if it
  2427. * just sends extension frames.
  2428. */
  2429. NGHTTP2_EXTERN void
  2430. nghttp2_option_set_user_recv_extension_type(nghttp2_option *option,
  2431. uint8_t type);
  2432. /**
  2433. * @function
  2434. *
  2435. * Sets extension frame type the application is willing to receive
  2436. * using builtin handler. The |type| is the extension frame type to
  2437. * receive, and must be strictly greater than 0x9. Otherwise, this
  2438. * function does nothing. The application can call this function
  2439. * multiple times to set more than one frame type to receive. The
  2440. * application does not have to call this function if it just sends
  2441. * extension frames.
  2442. *
  2443. * If same frame type is passed to both
  2444. * `nghttp2_option_set_builtin_recv_extension_type()` and
  2445. * `nghttp2_option_set_user_recv_extension_type()`, the latter takes
  2446. * precedence.
  2447. */
  2448. NGHTTP2_EXTERN void
  2449. nghttp2_option_set_builtin_recv_extension_type(nghttp2_option *option,
  2450. uint8_t type);
  2451. /**
  2452. * @function
  2453. *
  2454. * This option prevents the library from sending PING frame with ACK
  2455. * flag set automatically when PING frame without ACK flag set is
  2456. * received. If this option is set to nonzero, the library won't send
  2457. * PING frame with ACK flag set in the response for incoming PING
  2458. * frame. The application can send PING frame with ACK flag set using
  2459. * `nghttp2_submit_ping()` with :enum:`NGHTTP2_FLAG_ACK` as flags
  2460. * parameter.
  2461. */
  2462. NGHTTP2_EXTERN void nghttp2_option_set_no_auto_ping_ack(nghttp2_option *option,
  2463. int val);
  2464. /**
  2465. * @function
  2466. *
  2467. * This option sets the maximum length of header block (a set of
  2468. * header fields per one HEADERS frame) to send. The length of a
  2469. * given set of header fields is calculated using
  2470. * `nghttp2_hd_deflate_bound()`. The default value is 64KiB. If
  2471. * application attempts to send header fields larger than this limit,
  2472. * the transmission of the frame fails with error code
  2473. * :enum:`NGHTTP2_ERR_FRAME_SIZE_ERROR`.
  2474. */
  2475. NGHTTP2_EXTERN void
  2476. nghttp2_option_set_max_send_header_block_length(nghttp2_option *option,
  2477. size_t val);
  2478. /**
  2479. * @function
  2480. *
  2481. * This option sets the maximum dynamic table size for deflating
  2482. * header fields. The default value is 4KiB. In HTTP/2, receiver of
  2483. * deflated header block can specify maximum dynamic table size. The
  2484. * actual maximum size is the minimum of the size receiver specified
  2485. * and this option value.
  2486. */
  2487. NGHTTP2_EXTERN void
  2488. nghttp2_option_set_max_deflate_dynamic_table_size(nghttp2_option *option,
  2489. size_t val);
  2490. /**
  2491. * @function
  2492. *
  2493. * This option prevents the library from retaining closed streams to
  2494. * maintain the priority tree. If this option is set to nonzero,
  2495. * applications can discard closed stream completely to save memory.
  2496. */
  2497. NGHTTP2_EXTERN void nghttp2_option_set_no_closed_streams(nghttp2_option *option,
  2498. int val);
  2499. /**
  2500. * @function
  2501. *
  2502. * Initializes |*session_ptr| for client use. The all members of
  2503. * |callbacks| are copied to |*session_ptr|. Therefore |*session_ptr|
  2504. * does not store |callbacks|. The |user_data| is an arbitrary user
  2505. * supplied data, which will be passed to the callback functions.
  2506. *
  2507. * The :type:`nghttp2_send_callback` must be specified. If the
  2508. * application code uses `nghttp2_session_recv()`, the
  2509. * :type:`nghttp2_recv_callback` must be specified. The other members
  2510. * of |callbacks| can be ``NULL``.
  2511. *
  2512. * If this function fails, |*session_ptr| is left untouched.
  2513. *
  2514. * This function returns 0 if it succeeds, or one of the following
  2515. * negative error codes:
  2516. *
  2517. * :enum:`NGHTTP2_ERR_NOMEM`
  2518. * Out of memory.
  2519. */
  2520. NGHTTP2_EXTERN int
  2521. nghttp2_session_client_new(nghttp2_session **session_ptr,
  2522. const nghttp2_session_callbacks *callbacks,
  2523. void *user_data);
  2524. /**
  2525. * @function
  2526. *
  2527. * Initializes |*session_ptr| for server use. The all members of
  2528. * |callbacks| are copied to |*session_ptr|. Therefore |*session_ptr|
  2529. * does not store |callbacks|. The |user_data| is an arbitrary user
  2530. * supplied data, which will be passed to the callback functions.
  2531. *
  2532. * The :type:`nghttp2_send_callback` must be specified. If the
  2533. * application code uses `nghttp2_session_recv()`, the
  2534. * :type:`nghttp2_recv_callback` must be specified. The other members
  2535. * of |callbacks| can be ``NULL``.
  2536. *
  2537. * If this function fails, |*session_ptr| is left untouched.
  2538. *
  2539. * This function returns 0 if it succeeds, or one of the following
  2540. * negative error codes:
  2541. *
  2542. * :enum:`NGHTTP2_ERR_NOMEM`
  2543. * Out of memory.
  2544. */
  2545. NGHTTP2_EXTERN int
  2546. nghttp2_session_server_new(nghttp2_session **session_ptr,
  2547. const nghttp2_session_callbacks *callbacks,
  2548. void *user_data);
  2549. /**
  2550. * @function
  2551. *
  2552. * Like `nghttp2_session_client_new()`, but with additional options
  2553. * specified in the |option|.
  2554. *
  2555. * The |option| can be ``NULL`` and the call is equivalent to
  2556. * `nghttp2_session_client_new()`.
  2557. *
  2558. * This function does not take ownership |option|. The application is
  2559. * responsible for freeing |option| if it finishes using the object.
  2560. *
  2561. * The library code does not refer to |option| after this function
  2562. * returns.
  2563. *
  2564. * This function returns 0 if it succeeds, or one of the following
  2565. * negative error codes:
  2566. *
  2567. * :enum:`NGHTTP2_ERR_NOMEM`
  2568. * Out of memory.
  2569. */
  2570. NGHTTP2_EXTERN int
  2571. nghttp2_session_client_new2(nghttp2_session **session_ptr,
  2572. const nghttp2_session_callbacks *callbacks,
  2573. void *user_data, const nghttp2_option *option);
  2574. /**
  2575. * @function
  2576. *
  2577. * Like `nghttp2_session_server_new()`, but with additional options
  2578. * specified in the |option|.
  2579. *
  2580. * The |option| can be ``NULL`` and the call is equivalent to
  2581. * `nghttp2_session_server_new()`.
  2582. *
  2583. * This function does not take ownership |option|. The application is
  2584. * responsible for freeing |option| if it finishes using the object.
  2585. *
  2586. * The library code does not refer to |option| after this function
  2587. * returns.
  2588. *
  2589. * This function returns 0 if it succeeds, or one of the following
  2590. * negative error codes:
  2591. *
  2592. * :enum:`NGHTTP2_ERR_NOMEM`
  2593. * Out of memory.
  2594. */
  2595. NGHTTP2_EXTERN int
  2596. nghttp2_session_server_new2(nghttp2_session **session_ptr,
  2597. const nghttp2_session_callbacks *callbacks,
  2598. void *user_data, const nghttp2_option *option);
  2599. /**
  2600. * @function
  2601. *
  2602. * Like `nghttp2_session_client_new2()`, but with additional custom
  2603. * memory allocator specified in the |mem|.
  2604. *
  2605. * The |mem| can be ``NULL`` and the call is equivalent to
  2606. * `nghttp2_session_client_new2()`.
  2607. *
  2608. * This function does not take ownership |mem|. The application is
  2609. * responsible for freeing |mem|.
  2610. *
  2611. * The library code does not refer to |mem| pointer after this
  2612. * function returns, so the application can safely free it.
  2613. *
  2614. * This function returns 0 if it succeeds, or one of the following
  2615. * negative error codes:
  2616. *
  2617. * :enum:`NGHTTP2_ERR_NOMEM`
  2618. * Out of memory.
  2619. */
  2620. NGHTTP2_EXTERN int nghttp2_session_client_new3(
  2621. nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks,
  2622. void *user_data, const nghttp2_option *option, nghttp2_mem *mem);
  2623. /**
  2624. * @function
  2625. *
  2626. * Like `nghttp2_session_server_new2()`, but with additional custom
  2627. * memory allocator specified in the |mem|.
  2628. *
  2629. * The |mem| can be ``NULL`` and the call is equivalent to
  2630. * `nghttp2_session_server_new2()`.
  2631. *
  2632. * This function does not take ownership |mem|. The application is
  2633. * responsible for freeing |mem|.
  2634. *
  2635. * The library code does not refer to |mem| pointer after this
  2636. * function returns, so the application can safely free it.
  2637. *
  2638. * This function returns 0 if it succeeds, or one of the following
  2639. * negative error codes:
  2640. *
  2641. * :enum:`NGHTTP2_ERR_NOMEM`
  2642. * Out of memory.
  2643. */
  2644. NGHTTP2_EXTERN int nghttp2_session_server_new3(
  2645. nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks,
  2646. void *user_data, const nghttp2_option *option, nghttp2_mem *mem);
  2647. /**
  2648. * @function
  2649. *
  2650. * Frees any resources allocated for |session|. If |session| is
  2651. * ``NULL``, this function does nothing.
  2652. */
  2653. NGHTTP2_EXTERN void nghttp2_session_del(nghttp2_session *session);
  2654. /**
  2655. * @function
  2656. *
  2657. * Sends pending frames to the remote peer.
  2658. *
  2659. * This function retrieves the highest prioritized frame from the
  2660. * outbound queue and sends it to the remote peer. It does this as
  2661. * many as possible until the user callback
  2662. * :type:`nghttp2_send_callback` returns
  2663. * :enum:`NGHTTP2_ERR_WOULDBLOCK` or the outbound queue becomes empty.
  2664. * This function calls several callback functions which are passed
  2665. * when initializing the |session|. Here is the simple time chart
  2666. * which tells when each callback is invoked:
  2667. *
  2668. * 1. Get the next frame to send from outbound queue.
  2669. *
  2670. * 2. Prepare transmission of the frame.
  2671. *
  2672. * 3. If the control frame cannot be sent because some preconditions
  2673. * are not met (e.g., request HEADERS cannot be sent after GOAWAY),
  2674. * :type:`nghttp2_on_frame_not_send_callback` is invoked. Abort
  2675. * the following steps.
  2676. *
  2677. * 4. If the frame is HEADERS, PUSH_PROMISE or DATA,
  2678. * :type:`nghttp2_select_padding_callback` is invoked.
  2679. *
  2680. * 5. If the frame is request HEADERS, the stream is opened here.
  2681. *
  2682. * 6. :type:`nghttp2_before_frame_send_callback` is invoked.
  2683. *
  2684. * 7. If :enum:`NGHTTP2_ERR_CANCEL` is returned from
  2685. * :type:`nghttp2_before_frame_send_callback`, the current frame
  2686. * transmission is canceled, and
  2687. * :type:`nghttp2_on_frame_not_send_callback` is invoked. Abort
  2688. * the following steps.
  2689. *
  2690. * 8. :type:`nghttp2_send_callback` is invoked one or more times to
  2691. * send the frame.
  2692. *
  2693. * 9. :type:`nghttp2_on_frame_send_callback` is invoked.
  2694. *
  2695. * 10. If the transmission of the frame triggers closure of the
  2696. * stream, the stream is closed and
  2697. * :type:`nghttp2_on_stream_close_callback` is invoked.
  2698. *
  2699. * This function returns 0 if it succeeds, or one of the following
  2700. * negative error codes:
  2701. *
  2702. * :enum:`NGHTTP2_ERR_NOMEM`
  2703. * Out of memory.
  2704. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
  2705. * The callback function failed.
  2706. */
  2707. NGHTTP2_EXTERN int nghttp2_session_send(nghttp2_session *session);
  2708. /**
  2709. * @function
  2710. *
  2711. * Returns the serialized data to send.
  2712. *
  2713. * This function behaves like `nghttp2_session_send()` except that it
  2714. * does not use :type:`nghttp2_send_callback` to transmit data.
  2715. * Instead, it assigns the pointer to the serialized data to the
  2716. * |*data_ptr| and returns its length. The other callbacks are called
  2717. * in the same way as they are in `nghttp2_session_send()`.
  2718. *
  2719. * If no data is available to send, this function returns 0.
  2720. *
  2721. * This function may not return all serialized data in one invocation.
  2722. * To get all data, call this function repeatedly until it returns 0
  2723. * or one of negative error codes.
  2724. *
  2725. * The assigned |*data_ptr| is valid until the next call of
  2726. * `nghttp2_session_mem_send()` or `nghttp2_session_send()`.
  2727. *
  2728. * The caller must send all data before sending the next chunk of
  2729. * data.
  2730. *
  2731. * This function returns the length of the data pointed by the
  2732. * |*data_ptr| if it succeeds, or one of the following negative error
  2733. * codes:
  2734. *
  2735. * :enum:`NGHTTP2_ERR_NOMEM`
  2736. * Out of memory.
  2737. *
  2738. * .. note::
  2739. *
  2740. * This function may produce very small byte string. If that is the
  2741. * case, and application disables Nagle algorithm (``TCP_NODELAY``),
  2742. * then writing this small chunk leads to very small packet, and it
  2743. * is very inefficient. An application should be responsible to
  2744. * buffer up small chunks of data as necessary to avoid this
  2745. * situation.
  2746. */
  2747. NGHTTP2_EXTERN ssize_t nghttp2_session_mem_send(nghttp2_session *session,
  2748. const uint8_t **data_ptr);
  2749. /**
  2750. * @function
  2751. *
  2752. * Receives frames from the remote peer.
  2753. *
  2754. * This function receives as many frames as possible until the user
  2755. * callback :type:`nghttp2_recv_callback` returns
  2756. * :enum:`NGHTTP2_ERR_WOULDBLOCK`. This function calls several
  2757. * callback functions which are passed when initializing the
  2758. * |session|. Here is the simple time chart which tells when each
  2759. * callback is invoked:
  2760. *
  2761. * 1. :type:`nghttp2_recv_callback` is invoked one or more times to
  2762. * receive frame header.
  2763. *
  2764. * 2. When frame header is received,
  2765. * :type:`nghttp2_on_begin_frame_callback` is invoked.
  2766. *
  2767. * 3. If the frame is DATA frame:
  2768. *
  2769. * 1. :type:`nghttp2_recv_callback` is invoked to receive DATA
  2770. * payload. For each chunk of data,
  2771. * :type:`nghttp2_on_data_chunk_recv_callback` is invoked.
  2772. *
  2773. * 2. If one DATA frame is completely received,
  2774. * :type:`nghttp2_on_frame_recv_callback` is invoked. If the
  2775. * reception of the frame triggers the closure of the stream,
  2776. * :type:`nghttp2_on_stream_close_callback` is invoked.
  2777. *
  2778. * 4. If the frame is the control frame:
  2779. *
  2780. * 1. :type:`nghttp2_recv_callback` is invoked one or more times to
  2781. * receive whole frame.
  2782. *
  2783. * 2. If the received frame is valid, then following actions are
  2784. * taken. If the frame is either HEADERS or PUSH_PROMISE,
  2785. * :type:`nghttp2_on_begin_headers_callback` is invoked. Then
  2786. * :type:`nghttp2_on_header_callback` is invoked for each header
  2787. * name/value pair. For invalid header field,
  2788. * :type:`nghttp2_on_invalid_header_callback` is called. After
  2789. * all name/value pairs are emitted successfully,
  2790. * :type:`nghttp2_on_frame_recv_callback` is invoked. For other
  2791. * frames, :type:`nghttp2_on_frame_recv_callback` is invoked.
  2792. * If the reception of the frame triggers the closure of the
  2793. * stream, :type:`nghttp2_on_stream_close_callback` is invoked.
  2794. *
  2795. * 3. If the received frame is unpacked but is interpreted as
  2796. * invalid, :type:`nghttp2_on_invalid_frame_recv_callback` is
  2797. * invoked.
  2798. *
  2799. * This function returns 0 if it succeeds, or one of the following
  2800. * negative error codes:
  2801. *
  2802. * :enum:`NGHTTP2_ERR_EOF`
  2803. * The remote peer did shutdown on the connection.
  2804. * :enum:`NGHTTP2_ERR_NOMEM`
  2805. * Out of memory.
  2806. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
  2807. * The callback function failed.
  2808. * :enum:`NGHTTP2_ERR_BAD_CLIENT_MAGIC`
  2809. * Invalid client magic was detected. This error only returns
  2810. * when |session| was configured as server and
  2811. * `nghttp2_option_set_no_recv_client_magic()` is not used with
  2812. * nonzero value.
  2813. * :enum:`NGHTTP2_ERR_FLOODED`
  2814. * Flooding was detected in this HTTP/2 session, and it must be
  2815. * closed. This is most likely caused by misbehaviour of peer.
  2816. */
  2817. NGHTTP2_EXTERN int nghttp2_session_recv(nghttp2_session *session);
  2818. /**
  2819. * @function
  2820. *
  2821. * Processes data |in| as an input from the remote endpoint. The
  2822. * |inlen| indicates the number of bytes in the |in|.
  2823. *
  2824. * This function behaves like `nghttp2_session_recv()` except that it
  2825. * does not use :type:`nghttp2_recv_callback` to receive data; the
  2826. * |in| is the only data for the invocation of this function. If all
  2827. * bytes are processed, this function returns. The other callbacks
  2828. * are called in the same way as they are in `nghttp2_session_recv()`.
  2829. *
  2830. * In the current implementation, this function always tries to
  2831. * processes all input data unless either an error occurs or
  2832. * :enum:`NGHTTP2_ERR_PAUSE` is returned from
  2833. * :type:`nghttp2_on_header_callback` or
  2834. * :type:`nghttp2_on_data_chunk_recv_callback`. If
  2835. * :enum:`NGHTTP2_ERR_PAUSE` is used, the return value includes the
  2836. * number of bytes which was used to produce the data or frame for the
  2837. * callback.
  2838. *
  2839. * This function returns the number of processed bytes, or one of the
  2840. * following negative error codes:
  2841. *
  2842. * :enum:`NGHTTP2_ERR_NOMEM`
  2843. * Out of memory.
  2844. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
  2845. * The callback function failed.
  2846. * :enum:`NGHTTP2_ERR_BAD_CLIENT_MAGIC`
  2847. * Invalid client magic was detected. This error only returns
  2848. * when |session| was configured as server and
  2849. * `nghttp2_option_set_no_recv_client_magic()` is not used with
  2850. * nonzero value.
  2851. * :enum:`NGHTTP2_ERR_FLOODED`
  2852. * Flooding was detected in this HTTP/2 session, and it must be
  2853. * closed. This is most likely caused by misbehaviour of peer.
  2854. */
  2855. NGHTTP2_EXTERN ssize_t nghttp2_session_mem_recv(nghttp2_session *session,
  2856. const uint8_t *in,
  2857. size_t inlen);
  2858. /**
  2859. * @function
  2860. *
  2861. * Puts back previously deferred DATA frame in the stream |stream_id|
  2862. * to the outbound queue.
  2863. *
  2864. * This function returns 0 if it succeeds, or one of the following
  2865. * negative error codes:
  2866. *
  2867. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  2868. * The stream does not exist; or no deferred data exist.
  2869. * :enum:`NGHTTP2_ERR_NOMEM`
  2870. * Out of memory.
  2871. */
  2872. NGHTTP2_EXTERN int nghttp2_session_resume_data(nghttp2_session *session,
  2873. int32_t stream_id);
  2874. /**
  2875. * @function
  2876. *
  2877. * Returns nonzero value if |session| wants to receive data from the
  2878. * remote peer.
  2879. *
  2880. * If both `nghttp2_session_want_read()` and
  2881. * `nghttp2_session_want_write()` return 0, the application should
  2882. * drop the connection.
  2883. */
  2884. NGHTTP2_EXTERN int nghttp2_session_want_read(nghttp2_session *session);
  2885. /**
  2886. * @function
  2887. *
  2888. * Returns nonzero value if |session| wants to send data to the remote
  2889. * peer.
  2890. *
  2891. * If both `nghttp2_session_want_read()` and
  2892. * `nghttp2_session_want_write()` return 0, the application should
  2893. * drop the connection.
  2894. */
  2895. NGHTTP2_EXTERN int nghttp2_session_want_write(nghttp2_session *session);
  2896. /**
  2897. * @function
  2898. *
  2899. * Returns stream_user_data for the stream |stream_id|. The
  2900. * stream_user_data is provided by `nghttp2_submit_request()`,
  2901. * `nghttp2_submit_headers()` or
  2902. * `nghttp2_session_set_stream_user_data()`. Unless it is set using
  2903. * `nghttp2_session_set_stream_user_data()`, if the stream is
  2904. * initiated by the remote endpoint, stream_user_data is always
  2905. * ``NULL``. If the stream does not exist, this function returns
  2906. * ``NULL``.
  2907. */
  2908. NGHTTP2_EXTERN void *
  2909. nghttp2_session_get_stream_user_data(nghttp2_session *session,
  2910. int32_t stream_id);
  2911. /**
  2912. * @function
  2913. *
  2914. * Sets the |stream_user_data| to the stream denoted by the
  2915. * |stream_id|. If a stream user data is already set to the stream,
  2916. * it is replaced with the |stream_user_data|. It is valid to specify
  2917. * ``NULL`` in the |stream_user_data|, which nullifies the associated
  2918. * data pointer.
  2919. *
  2920. * It is valid to set the |stream_user_data| to the stream reserved by
  2921. * PUSH_PROMISE frame.
  2922. *
  2923. * This function returns 0 if it succeeds, or one of following
  2924. * negative error codes:
  2925. *
  2926. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  2927. * The stream does not exist
  2928. */
  2929. NGHTTP2_EXTERN int
  2930. nghttp2_session_set_stream_user_data(nghttp2_session *session,
  2931. int32_t stream_id, void *stream_user_data);
  2932. /**
  2933. * @function
  2934. *
  2935. * Sets |user_data| to |session|, overwriting the existing user data
  2936. * specified in `nghttp2_session_client_new()`, or
  2937. * `nghttp2_session_server_new()`.
  2938. */
  2939. NGHTTP2_EXTERN void nghttp2_session_set_user_data(nghttp2_session *session,
  2940. void *user_data);
  2941. /**
  2942. * @function
  2943. *
  2944. * Returns the number of frames in the outbound queue. This does not
  2945. * include the deferred DATA frames.
  2946. */
  2947. NGHTTP2_EXTERN size_t
  2948. nghttp2_session_get_outbound_queue_size(nghttp2_session *session);
  2949. /**
  2950. * @function
  2951. *
  2952. * Returns the number of DATA payload in bytes received without
  2953. * WINDOW_UPDATE transmission for the stream |stream_id|. The local
  2954. * (receive) window size can be adjusted by
  2955. * `nghttp2_submit_window_update()`. This function takes into account
  2956. * that and returns effective data length. In particular, if the
  2957. * local window size is reduced by submitting negative
  2958. * window_size_increment with `nghttp2_submit_window_update()`, this
  2959. * function returns the number of bytes less than actually received.
  2960. *
  2961. * This function returns -1 if it fails.
  2962. */
  2963. NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_effective_recv_data_length(
  2964. nghttp2_session *session, int32_t stream_id);
  2965. /**
  2966. * @function
  2967. *
  2968. * Returns the local (receive) window size for the stream |stream_id|.
  2969. * The local window size can be adjusted by
  2970. * `nghttp2_submit_window_update()`. This function takes into account
  2971. * that and returns effective window size.
  2972. *
  2973. * This function does not take into account the amount of received
  2974. * data from the remote endpoint. Use
  2975. * `nghttp2_session_get_stream_local_window_size()` to know the amount
  2976. * of data the remote endpoint can send without receiving stream level
  2977. * WINDOW_UPDATE frame. Note that each stream is still subject to the
  2978. * connection level flow control.
  2979. *
  2980. * This function returns -1 if it fails.
  2981. */
  2982. NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_effective_local_window_size(
  2983. nghttp2_session *session, int32_t stream_id);
  2984. /**
  2985. * @function
  2986. *
  2987. * Returns the amount of flow-controlled payload (e.g., DATA) that the
  2988. * remote endpoint can send without receiving stream level
  2989. * WINDOW_UPDATE frame. It is also subject to the connection level
  2990. * flow control. So the actual amount of data to send is
  2991. * min(`nghttp2_session_get_stream_local_window_size()`,
  2992. * `nghttp2_session_get_local_window_size()`).
  2993. *
  2994. * This function returns -1 if it fails.
  2995. */
  2996. NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_local_window_size(
  2997. nghttp2_session *session, int32_t stream_id);
  2998. /**
  2999. * @function
  3000. *
  3001. * Returns the number of DATA payload in bytes received without
  3002. * WINDOW_UPDATE transmission for a connection. The local (receive)
  3003. * window size can be adjusted by `nghttp2_submit_window_update()`.
  3004. * This function takes into account that and returns effective data
  3005. * length. In particular, if the local window size is reduced by
  3006. * submitting negative window_size_increment with
  3007. * `nghttp2_submit_window_update()`, this function returns the number
  3008. * of bytes less than actually received.
  3009. *
  3010. * This function returns -1 if it fails.
  3011. */
  3012. NGHTTP2_EXTERN int32_t
  3013. nghttp2_session_get_effective_recv_data_length(nghttp2_session *session);
  3014. /**
  3015. * @function
  3016. *
  3017. * Returns the local (receive) window size for a connection. The
  3018. * local window size can be adjusted by
  3019. * `nghttp2_submit_window_update()`. This function takes into account
  3020. * that and returns effective window size.
  3021. *
  3022. * This function does not take into account the amount of received
  3023. * data from the remote endpoint. Use
  3024. * `nghttp2_session_get_local_window_size()` to know the amount of
  3025. * data the remote endpoint can send without receiving
  3026. * connection-level WINDOW_UPDATE frame. Note that each stream is
  3027. * still subject to the stream level flow control.
  3028. *
  3029. * This function returns -1 if it fails.
  3030. */
  3031. NGHTTP2_EXTERN int32_t
  3032. nghttp2_session_get_effective_local_window_size(nghttp2_session *session);
  3033. /**
  3034. * @function
  3035. *
  3036. * Returns the amount of flow-controlled payload (e.g., DATA) that the
  3037. * remote endpoint can send without receiving connection level
  3038. * WINDOW_UPDATE frame. Note that each stream is still subject to the
  3039. * stream level flow control (see
  3040. * `nghttp2_session_get_stream_local_window_size()`).
  3041. *
  3042. * This function returns -1 if it fails.
  3043. */
  3044. NGHTTP2_EXTERN int32_t
  3045. nghttp2_session_get_local_window_size(nghttp2_session *session);
  3046. /**
  3047. * @function
  3048. *
  3049. * Returns the remote window size for a given stream |stream_id|.
  3050. *
  3051. * This is the amount of flow-controlled payload (e.g., DATA) that the
  3052. * local endpoint can send without stream level WINDOW_UPDATE. There
  3053. * is also connection level flow control, so the effective size of
  3054. * payload that the local endpoint can actually send is
  3055. * min(`nghttp2_session_get_stream_remote_window_size()`,
  3056. * `nghttp2_session_get_remote_window_size()`).
  3057. *
  3058. * This function returns -1 if it fails.
  3059. */
  3060. NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_remote_window_size(
  3061. nghttp2_session *session, int32_t stream_id);
  3062. /**
  3063. * @function
  3064. *
  3065. * Returns the remote window size for a connection.
  3066. *
  3067. * This function always succeeds.
  3068. */
  3069. NGHTTP2_EXTERN int32_t
  3070. nghttp2_session_get_remote_window_size(nghttp2_session *session);
  3071. /**
  3072. * @function
  3073. *
  3074. * Returns 1 if local peer half closed the given stream |stream_id|.
  3075. * Returns 0 if it did not. Returns -1 if no such stream exists.
  3076. */
  3077. NGHTTP2_EXTERN int
  3078. nghttp2_session_get_stream_local_close(nghttp2_session *session,
  3079. int32_t stream_id);
  3080. /**
  3081. * @function
  3082. *
  3083. * Returns 1 if remote peer half closed the given stream |stream_id|.
  3084. * Returns 0 if it did not. Returns -1 if no such stream exists.
  3085. */
  3086. NGHTTP2_EXTERN int
  3087. nghttp2_session_get_stream_remote_close(nghttp2_session *session,
  3088. int32_t stream_id);
  3089. /**
  3090. * @function
  3091. *
  3092. * Returns the current dynamic table size of HPACK inflater, including
  3093. * the overhead 32 bytes per entry described in RFC 7541.
  3094. */
  3095. NGHTTP2_EXTERN size_t
  3096. nghttp2_session_get_hd_inflate_dynamic_table_size(nghttp2_session *session);
  3097. /**
  3098. * @function
  3099. *
  3100. * Returns the current dynamic table size of HPACK deflater including
  3101. * the overhead 32 bytes per entry described in RFC 7541.
  3102. */
  3103. NGHTTP2_EXTERN size_t
  3104. nghttp2_session_get_hd_deflate_dynamic_table_size(nghttp2_session *session);
  3105. /**
  3106. * @function
  3107. *
  3108. * Signals the session so that the connection should be terminated.
  3109. *
  3110. * The last stream ID is the minimum value between the stream ID of a
  3111. * stream for which :type:`nghttp2_on_frame_recv_callback` was called
  3112. * most recently and the last stream ID we have sent to the peer
  3113. * previously.
  3114. *
  3115. * The |error_code| is the error code of this GOAWAY frame. The
  3116. * pre-defined error code is one of :enum:`nghttp2_error_code`.
  3117. *
  3118. * After the transmission, both `nghttp2_session_want_read()` and
  3119. * `nghttp2_session_want_write()` return 0.
  3120. *
  3121. * This function should be called when the connection should be
  3122. * terminated after sending GOAWAY. If the remaining streams should
  3123. * be processed after GOAWAY, use `nghttp2_submit_goaway()` instead.
  3124. *
  3125. * This function returns 0 if it succeeds, or one of the following
  3126. * negative error codes:
  3127. *
  3128. * :enum:`NGHTTP2_ERR_NOMEM`
  3129. * Out of memory.
  3130. */
  3131. NGHTTP2_EXTERN int nghttp2_session_terminate_session(nghttp2_session *session,
  3132. uint32_t error_code);
  3133. /**
  3134. * @function
  3135. *
  3136. * Signals the session so that the connection should be terminated.
  3137. *
  3138. * This function behaves like `nghttp2_session_terminate_session()`,
  3139. * but the last stream ID can be specified by the application for fine
  3140. * grained control of stream. The HTTP/2 specification does not allow
  3141. * last_stream_id to be increased. So the actual value sent as
  3142. * last_stream_id is the minimum value between the given
  3143. * |last_stream_id| and the last_stream_id we have previously sent to
  3144. * the peer.
  3145. *
  3146. * The |last_stream_id| is peer's stream ID or 0. So if |session| is
  3147. * initialized as client, |last_stream_id| must be even or 0. If
  3148. * |session| is initialized as server, |last_stream_id| must be odd or
  3149. * 0.
  3150. *
  3151. * This function returns 0 if it succeeds, or one of the following
  3152. * negative error codes:
  3153. *
  3154. * :enum:`NGHTTP2_ERR_NOMEM`
  3155. * Out of memory.
  3156. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3157. * The |last_stream_id| is invalid.
  3158. */
  3159. NGHTTP2_EXTERN int nghttp2_session_terminate_session2(nghttp2_session *session,
  3160. int32_t last_stream_id,
  3161. uint32_t error_code);
  3162. /**
  3163. * @function
  3164. *
  3165. * Signals to the client that the server started graceful shutdown
  3166. * procedure.
  3167. *
  3168. * This function is only usable for server. If this function is
  3169. * called with client side session, this function returns
  3170. * :enum:`NGHTTP2_ERR_INVALID_STATE`.
  3171. *
  3172. * To gracefully shutdown HTTP/2 session, server should call this
  3173. * function to send GOAWAY with last_stream_id (1u << 31) - 1. And
  3174. * after some delay (e.g., 1 RTT), send another GOAWAY with the stream
  3175. * ID that the server has some processing using
  3176. * `nghttp2_submit_goaway()`. See also
  3177. * `nghttp2_session_get_last_proc_stream_id()`.
  3178. *
  3179. * Unlike `nghttp2_submit_goaway()`, this function just sends GOAWAY
  3180. * and does nothing more. This is a mere indication to the client
  3181. * that session shutdown is imminent. The application should call
  3182. * `nghttp2_submit_goaway()` with appropriate last_stream_id after
  3183. * this call.
  3184. *
  3185. * If one or more GOAWAY frame have been already sent by either
  3186. * `nghttp2_submit_goaway()` or `nghttp2_session_terminate_session()`,
  3187. * this function has no effect.
  3188. *
  3189. * This function returns 0 if it succeeds, or one of the following
  3190. * negative error codes:
  3191. *
  3192. * :enum:`NGHTTP2_ERR_NOMEM`
  3193. * Out of memory.
  3194. * :enum:`NGHTTP2_ERR_INVALID_STATE`
  3195. * The |session| is initialized as client.
  3196. */
  3197. NGHTTP2_EXTERN int nghttp2_submit_shutdown_notice(nghttp2_session *session);
  3198. /**
  3199. * @function
  3200. *
  3201. * Returns the value of SETTINGS |id| notified by a remote endpoint.
  3202. * The |id| must be one of values defined in
  3203. * :enum:`nghttp2_settings_id`.
  3204. */
  3205. NGHTTP2_EXTERN uint32_t nghttp2_session_get_remote_settings(
  3206. nghttp2_session *session, nghttp2_settings_id id);
  3207. /**
  3208. * @function
  3209. *
  3210. * Returns the value of SETTINGS |id| of local endpoint acknowledged
  3211. * by the remote endpoint. The |id| must be one of the values defined
  3212. * in :enum:`nghttp2_settings_id`.
  3213. */
  3214. NGHTTP2_EXTERN uint32_t nghttp2_session_get_local_settings(
  3215. nghttp2_session *session, nghttp2_settings_id id);
  3216. /**
  3217. * @function
  3218. *
  3219. * Tells the |session| that next stream ID is |next_stream_id|. The
  3220. * |next_stream_id| must be equal or greater than the value returned
  3221. * by `nghttp2_session_get_next_stream_id()`.
  3222. *
  3223. * This function returns 0 if it succeeds, or one of the following
  3224. * negative error codes:
  3225. *
  3226. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3227. * The |next_stream_id| is strictly less than the value
  3228. * `nghttp2_session_get_next_stream_id()` returns; or
  3229. * |next_stream_id| is invalid (e.g., even integer for client, or
  3230. * odd integer for server).
  3231. */
  3232. NGHTTP2_EXTERN int nghttp2_session_set_next_stream_id(nghttp2_session *session,
  3233. int32_t next_stream_id);
  3234. /**
  3235. * @function
  3236. *
  3237. * Returns the next outgoing stream ID. Notice that return type is
  3238. * uint32_t. If we run out of stream ID for this session, this
  3239. * function returns 1 << 31.
  3240. */
  3241. NGHTTP2_EXTERN uint32_t
  3242. nghttp2_session_get_next_stream_id(nghttp2_session *session);
  3243. /**
  3244. * @function
  3245. *
  3246. * Tells the |session| that |size| bytes for a stream denoted by
  3247. * |stream_id| were consumed by application and are ready to
  3248. * WINDOW_UPDATE. The consumed bytes are counted towards both
  3249. * connection and stream level WINDOW_UPDATE (see
  3250. * `nghttp2_session_consume_connection()` and
  3251. * `nghttp2_session_consume_stream()` to update consumption
  3252. * independently). This function is intended to be used without
  3253. * automatic window update (see
  3254. * `nghttp2_option_set_no_auto_window_update()`).
  3255. *
  3256. * This function returns 0 if it succeeds, or one of the following
  3257. * negative error codes:
  3258. *
  3259. * :enum:`NGHTTP2_ERR_NOMEM`
  3260. * Out of memory.
  3261. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3262. * The |stream_id| is 0.
  3263. * :enum:`NGHTTP2_ERR_INVALID_STATE`
  3264. * Automatic WINDOW_UPDATE is not disabled.
  3265. */
  3266. NGHTTP2_EXTERN int nghttp2_session_consume(nghttp2_session *session,
  3267. int32_t stream_id, size_t size);
  3268. /**
  3269. * @function
  3270. *
  3271. * Like `nghttp2_session_consume()`, but this only tells library that
  3272. * |size| bytes were consumed only for connection level. Note that
  3273. * HTTP/2 maintains connection and stream level flow control windows
  3274. * independently.
  3275. *
  3276. * This function returns 0 if it succeeds, or one of the following
  3277. * negative error codes:
  3278. *
  3279. * :enum:`NGHTTP2_ERR_NOMEM`
  3280. * Out of memory.
  3281. * :enum:`NGHTTP2_ERR_INVALID_STATE`
  3282. * Automatic WINDOW_UPDATE is not disabled.
  3283. */
  3284. NGHTTP2_EXTERN int nghttp2_session_consume_connection(nghttp2_session *session,
  3285. size_t size);
  3286. /**
  3287. * @function
  3288. *
  3289. * Like `nghttp2_session_consume()`, but this only tells library that
  3290. * |size| bytes were consumed only for stream denoted by |stream_id|.
  3291. * Note that HTTP/2 maintains connection and stream level flow control
  3292. * windows independently.
  3293. *
  3294. * This function returns 0 if it succeeds, or one of the following
  3295. * negative error codes:
  3296. *
  3297. * :enum:`NGHTTP2_ERR_NOMEM`
  3298. * Out of memory.
  3299. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3300. * The |stream_id| is 0.
  3301. * :enum:`NGHTTP2_ERR_INVALID_STATE`
  3302. * Automatic WINDOW_UPDATE is not disabled.
  3303. */
  3304. NGHTTP2_EXTERN int nghttp2_session_consume_stream(nghttp2_session *session,
  3305. int32_t stream_id,
  3306. size_t size);
  3307. /**
  3308. * @function
  3309. *
  3310. * Changes priority of existing stream denoted by |stream_id|. The
  3311. * new priority specification is |pri_spec|.
  3312. *
  3313. * The priority is changed silently and instantly, and no PRIORITY
  3314. * frame will be sent to notify the peer of this change. This
  3315. * function may be useful for server to change the priority of pushed
  3316. * stream.
  3317. *
  3318. * If |session| is initialized as server, and ``pri_spec->stream_id``
  3319. * points to the idle stream, the idle stream is created if it does
  3320. * not exist. The created idle stream will depend on root stream
  3321. * (stream 0) with weight 16.
  3322. *
  3323. * Otherwise, if stream denoted by ``pri_spec->stream_id`` is not
  3324. * found, we use default priority instead of given |pri_spec|. That
  3325. * is make stream depend on root stream with weight 16.
  3326. *
  3327. * This function returns 0 if it succeeds, or one of the following
  3328. * negative error codes:
  3329. *
  3330. * :enum:`NGHTTP2_ERR_NOMEM`
  3331. * Out of memory.
  3332. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3333. * Attempted to depend on itself; or no stream exist for the given
  3334. * |stream_id|; or |stream_id| is 0
  3335. */
  3336. NGHTTP2_EXTERN int
  3337. nghttp2_session_change_stream_priority(nghttp2_session *session,
  3338. int32_t stream_id,
  3339. const nghttp2_priority_spec *pri_spec);
  3340. /**
  3341. * @function
  3342. *
  3343. * Creates idle stream with the given |stream_id|, and priority
  3344. * |pri_spec|.
  3345. *
  3346. * The stream creation is done without sending PRIORITY frame, which
  3347. * means that peer does not know about the existence of this idle
  3348. * stream in the local endpoint.
  3349. *
  3350. * RFC 7540 does not disallow the use of creation of idle stream with
  3351. * odd or even stream ID regardless of client or server. So this
  3352. * function can create odd or even stream ID regardless of client or
  3353. * server. But probably it is a bit safer to use the stream ID the
  3354. * local endpoint can initiate (in other words, use odd stream ID for
  3355. * client, and even stream ID for server), to avoid potential
  3356. * collision from peer's instruction. Also we can use
  3357. * `nghttp2_session_set_next_stream_id()` to avoid to open created
  3358. * idle streams accidentally if we follow this recommendation.
  3359. *
  3360. * If |session| is initialized as server, and ``pri_spec->stream_id``
  3361. * points to the idle stream, the idle stream is created if it does
  3362. * not exist. The created idle stream will depend on root stream
  3363. * (stream 0) with weight 16.
  3364. *
  3365. * Otherwise, if stream denoted by ``pri_spec->stream_id`` is not
  3366. * found, we use default priority instead of given |pri_spec|. That
  3367. * is make stream depend on root stream with weight 16.
  3368. *
  3369. * This function returns 0 if it succeeds, or one of the following
  3370. * negative error codes:
  3371. *
  3372. * :enum:`NGHTTP2_ERR_NOMEM`
  3373. * Out of memory.
  3374. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3375. * Attempted to depend on itself; or stream denoted by |stream_id|
  3376. * already exists; or |stream_id| cannot be used to create idle
  3377. * stream (in other words, local endpoint has already opened
  3378. * stream ID greater than or equal to the given stream ID; or
  3379. * |stream_id| is 0
  3380. */
  3381. NGHTTP2_EXTERN int
  3382. nghttp2_session_create_idle_stream(nghttp2_session *session, int32_t stream_id,
  3383. const nghttp2_priority_spec *pri_spec);
  3384. /**
  3385. * @function
  3386. *
  3387. * Performs post-process of HTTP Upgrade request. This function can
  3388. * be called from both client and server, but the behavior is very
  3389. * different in each other.
  3390. *
  3391. * .. warning::
  3392. *
  3393. * This function is deprecated in favor of
  3394. * `nghttp2_session_upgrade2()`, because this function lacks the
  3395. * parameter to tell the library the request method used in the
  3396. * original HTTP request. This information is required for client
  3397. * to validate actual response body length against content-length
  3398. * header field (see `nghttp2_option_set_no_http_messaging()`). If
  3399. * HEAD is used in request, the length of response body must be 0
  3400. * regardless of value included in content-length header field.
  3401. *
  3402. * If called from client side, the |settings_payload| must be the
  3403. * value sent in ``HTTP2-Settings`` header field and must be decoded
  3404. * by base64url decoder. The |settings_payloadlen| is the length of
  3405. * |settings_payload|. The |settings_payload| is unpacked and its
  3406. * setting values will be submitted using `nghttp2_submit_settings()`.
  3407. * This means that the client application code does not need to submit
  3408. * SETTINGS by itself. The stream with stream ID=1 is opened and the
  3409. * |stream_user_data| is used for its stream_user_data. The opened
  3410. * stream becomes half-closed (local) state.
  3411. *
  3412. * If called from server side, the |settings_payload| must be the
  3413. * value received in ``HTTP2-Settings`` header field and must be
  3414. * decoded by base64url decoder. The |settings_payloadlen| is the
  3415. * length of |settings_payload|. It is treated as if the SETTINGS
  3416. * frame with that payload is received. Thus, callback functions for
  3417. * the reception of SETTINGS frame will be invoked. The stream with
  3418. * stream ID=1 is opened. The |stream_user_data| is ignored. The
  3419. * opened stream becomes half-closed (remote).
  3420. *
  3421. * This function returns 0 if it succeeds, or one of the following
  3422. * negative error codes:
  3423. *
  3424. * :enum:`NGHTTP2_ERR_NOMEM`
  3425. * Out of memory.
  3426. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3427. * The |settings_payload| is badly formed.
  3428. * :enum:`NGHTTP2_ERR_PROTO`
  3429. * The stream ID 1 is already used or closed; or is not available.
  3430. */
  3431. NGHTTP2_EXTERN int nghttp2_session_upgrade(nghttp2_session *session,
  3432. const uint8_t *settings_payload,
  3433. size_t settings_payloadlen,
  3434. void *stream_user_data);
  3435. /**
  3436. * @function
  3437. *
  3438. * Performs post-process of HTTP Upgrade request. This function can
  3439. * be called from both client and server, but the behavior is very
  3440. * different in each other.
  3441. *
  3442. * If called from client side, the |settings_payload| must be the
  3443. * value sent in ``HTTP2-Settings`` header field and must be decoded
  3444. * by base64url decoder. The |settings_payloadlen| is the length of
  3445. * |settings_payload|. The |settings_payload| is unpacked and its
  3446. * setting values will be submitted using `nghttp2_submit_settings()`.
  3447. * This means that the client application code does not need to submit
  3448. * SETTINGS by itself. The stream with stream ID=1 is opened and the
  3449. * |stream_user_data| is used for its stream_user_data. The opened
  3450. * stream becomes half-closed (local) state.
  3451. *
  3452. * If called from server side, the |settings_payload| must be the
  3453. * value received in ``HTTP2-Settings`` header field and must be
  3454. * decoded by base64url decoder. The |settings_payloadlen| is the
  3455. * length of |settings_payload|. It is treated as if the SETTINGS
  3456. * frame with that payload is received. Thus, callback functions for
  3457. * the reception of SETTINGS frame will be invoked. The stream with
  3458. * stream ID=1 is opened. The |stream_user_data| is ignored. The
  3459. * opened stream becomes half-closed (remote).
  3460. *
  3461. * If the request method is HEAD, pass nonzero value to
  3462. * |head_request|. Otherwise, pass 0.
  3463. *
  3464. * This function returns 0 if it succeeds, or one of the following
  3465. * negative error codes:
  3466. *
  3467. * :enum:`NGHTTP2_ERR_NOMEM`
  3468. * Out of memory.
  3469. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3470. * The |settings_payload| is badly formed.
  3471. * :enum:`NGHTTP2_ERR_PROTO`
  3472. * The stream ID 1 is already used or closed; or is not available.
  3473. */
  3474. NGHTTP2_EXTERN int nghttp2_session_upgrade2(nghttp2_session *session,
  3475. const uint8_t *settings_payload,
  3476. size_t settings_payloadlen,
  3477. int head_request,
  3478. void *stream_user_data);
  3479. /**
  3480. * @function
  3481. *
  3482. * Serializes the SETTINGS values |iv| in the |buf|. The size of the
  3483. * |buf| is specified by |buflen|. The number of entries in the |iv|
  3484. * array is given by |niv|. The required space in |buf| for the |niv|
  3485. * entries is ``6*niv`` bytes and if the given buffer is too small, an
  3486. * error is returned. This function is used mainly for creating a
  3487. * SETTINGS payload to be sent with the ``HTTP2-Settings`` header
  3488. * field in an HTTP Upgrade request. The data written in |buf| is NOT
  3489. * base64url encoded and the application is responsible for encoding.
  3490. *
  3491. * This function returns the number of bytes written in |buf|, or one
  3492. * of the following negative error codes:
  3493. *
  3494. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3495. * The |iv| contains duplicate settings ID or invalid value.
  3496. *
  3497. * :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`
  3498. * The provided |buflen| size is too small to hold the output.
  3499. */
  3500. NGHTTP2_EXTERN ssize_t nghttp2_pack_settings_payload(
  3501. uint8_t *buf, size_t buflen, const nghttp2_settings_entry *iv, size_t niv);
  3502. /**
  3503. * @function
  3504. *
  3505. * Returns string describing the |lib_error_code|. The
  3506. * |lib_error_code| must be one of the :enum:`nghttp2_error`.
  3507. */
  3508. NGHTTP2_EXTERN const char *nghttp2_strerror(int lib_error_code);
  3509. /**
  3510. * @function
  3511. *
  3512. * Returns string representation of HTTP/2 error code |error_code|
  3513. * (e.g., ``PROTOCOL_ERROR`` is returned if ``error_code ==
  3514. * NGHTTP2_PROTOCOL_ERROR``). If string representation is unknown for
  3515. * given |error_code|, this function returns string ``unknown``.
  3516. */
  3517. NGHTTP2_EXTERN const char *nghttp2_http2_strerror(uint32_t error_code);
  3518. /**
  3519. * @function
  3520. *
  3521. * Initializes |pri_spec| with the |stream_id| of the stream to depend
  3522. * on with |weight| and its exclusive flag. If |exclusive| is
  3523. * nonzero, exclusive flag is set.
  3524. *
  3525. * The |weight| must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
  3526. * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive.
  3527. */
  3528. NGHTTP2_EXTERN void nghttp2_priority_spec_init(nghttp2_priority_spec *pri_spec,
  3529. int32_t stream_id,
  3530. int32_t weight, int exclusive);
  3531. /**
  3532. * @function
  3533. *
  3534. * Initializes |pri_spec| with the default values. The default values
  3535. * are: stream_id = 0, weight = :macro:`NGHTTP2_DEFAULT_WEIGHT` and
  3536. * exclusive = 0.
  3537. */
  3538. NGHTTP2_EXTERN void
  3539. nghttp2_priority_spec_default_init(nghttp2_priority_spec *pri_spec);
  3540. /**
  3541. * @function
  3542. *
  3543. * Returns nonzero if the |pri_spec| is filled with default values.
  3544. */
  3545. NGHTTP2_EXTERN int
  3546. nghttp2_priority_spec_check_default(const nghttp2_priority_spec *pri_spec);
  3547. /**
  3548. * @function
  3549. *
  3550. * Submits HEADERS frame and optionally one or more DATA frames.
  3551. *
  3552. * The |pri_spec| is priority specification of this request. ``NULL``
  3553. * means the default priority (see
  3554. * `nghttp2_priority_spec_default_init()`). To specify the priority,
  3555. * use `nghttp2_priority_spec_init()`. If |pri_spec| is not ``NULL``,
  3556. * this function will copy its data members.
  3557. *
  3558. * The ``pri_spec->weight`` must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
  3559. * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive. If ``pri_spec->weight`` is
  3560. * strictly less than :enum:`NGHTTP2_MIN_WEIGHT`, it becomes
  3561. * :enum:`NGHTTP2_MIN_WEIGHT`. If it is strictly greater than
  3562. * :enum:`NGHTTP2_MAX_WEIGHT`, it becomes :enum:`NGHTTP2_MAX_WEIGHT`.
  3563. *
  3564. * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
  3565. * |nvlen| elements. The application is responsible to include
  3566. * required pseudo-header fields (header field whose name starts with
  3567. * ":") in |nva| and must place pseudo-headers before regular header
  3568. * fields.
  3569. *
  3570. * This function creates copies of all name/value pairs in |nva|. It
  3571. * also lower-cases all names in |nva|. The order of elements in
  3572. * |nva| is preserved. For header fields with
  3573. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
  3574. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
  3575. * and value are not copied respectively. With
  3576. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
  3577. * pass header field name in lowercase. The application should
  3578. * maintain the references to them until
  3579. * :type:`nghttp2_on_frame_send_callback` or
  3580. * :type:`nghttp2_on_frame_not_send_callback` is called.
  3581. *
  3582. * HTTP/2 specification has requirement about header fields in the
  3583. * request HEADERS. See the specification for more details.
  3584. *
  3585. * If |data_prd| is not ``NULL``, it provides data which will be sent
  3586. * in subsequent DATA frames. In this case, a method that allows
  3587. * request message bodies
  3588. * (https://tools.ietf.org/html/rfc7231#section-4) must be specified
  3589. * with ``:method`` key in |nva| (e.g. ``POST``). This function does
  3590. * not take ownership of the |data_prd|. The function copies the
  3591. * members of the |data_prd|. If |data_prd| is ``NULL``, HEADERS have
  3592. * END_STREAM set. The |stream_user_data| is data associated to the
  3593. * stream opened by this request and can be an arbitrary pointer,
  3594. * which can be retrieved later by
  3595. * `nghttp2_session_get_stream_user_data()`.
  3596. *
  3597. * This function returns assigned stream ID if it succeeds, or one of
  3598. * the following negative error codes:
  3599. *
  3600. * :enum:`NGHTTP2_ERR_NOMEM`
  3601. * Out of memory.
  3602. * :enum:`NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
  3603. * No stream ID is available because maximum stream ID was
  3604. * reached.
  3605. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3606. * Trying to depend on itself (new stream ID equals
  3607. * ``pri_spec->stream_id``).
  3608. * :enum:`NGHTTP2_ERR_PROTO`
  3609. * The |session| is server session.
  3610. *
  3611. * .. warning::
  3612. *
  3613. * This function returns assigned stream ID if it succeeds. But
  3614. * that stream is not opened yet. The application must not submit
  3615. * frame to that stream ID before
  3616. * :type:`nghttp2_before_frame_send_callback` is called for this
  3617. * frame.
  3618. *
  3619. */
  3620. NGHTTP2_EXTERN int32_t nghttp2_submit_request(
  3621. nghttp2_session *session, const nghttp2_priority_spec *pri_spec,
  3622. const nghttp2_nv *nva, size_t nvlen, const nghttp2_data_provider *data_prd,
  3623. void *stream_user_data);
  3624. /**
  3625. * @function
  3626. *
  3627. * Submits response HEADERS frame and optionally one or more DATA
  3628. * frames against the stream |stream_id|.
  3629. *
  3630. * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
  3631. * |nvlen| elements. The application is responsible to include
  3632. * required pseudo-header fields (header field whose name starts with
  3633. * ":") in |nva| and must place pseudo-headers before regular header
  3634. * fields.
  3635. *
  3636. * This function creates copies of all name/value pairs in |nva|. It
  3637. * also lower-cases all names in |nva|. The order of elements in
  3638. * |nva| is preserved. For header fields with
  3639. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
  3640. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
  3641. * and value are not copied respectively. With
  3642. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
  3643. * pass header field name in lowercase. The application should
  3644. * maintain the references to them until
  3645. * :type:`nghttp2_on_frame_send_callback` or
  3646. * :type:`nghttp2_on_frame_not_send_callback` is called.
  3647. *
  3648. * HTTP/2 specification has requirement about header fields in the
  3649. * response HEADERS. See the specification for more details.
  3650. *
  3651. * If |data_prd| is not ``NULL``, it provides data which will be sent
  3652. * in subsequent DATA frames. This function does not take ownership
  3653. * of the |data_prd|. The function copies the members of the
  3654. * |data_prd|. If |data_prd| is ``NULL``, HEADERS will have
  3655. * END_STREAM flag set.
  3656. *
  3657. * This method can be used as normal HTTP response and push response.
  3658. * When pushing a resource using this function, the |session| must be
  3659. * configured using `nghttp2_session_server_new()` or its variants and
  3660. * the target stream denoted by the |stream_id| must be reserved using
  3661. * `nghttp2_submit_push_promise()`.
  3662. *
  3663. * To send non-final response headers (e.g., HTTP status 101), don't
  3664. * use this function because this function half-closes the outbound
  3665. * stream. Instead, use `nghttp2_submit_headers()` for this purpose.
  3666. *
  3667. * This function returns 0 if it succeeds, or one of the following
  3668. * negative error codes:
  3669. *
  3670. * :enum:`NGHTTP2_ERR_NOMEM`
  3671. * Out of memory.
  3672. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3673. * The |stream_id| is 0.
  3674. * :enum:`NGHTTP2_ERR_DATA_EXIST`
  3675. * DATA or HEADERS has been already submitted and not fully
  3676. * processed yet. Normally, this does not happen, but when
  3677. * application wrongly calls `nghttp2_submit_response()` twice,
  3678. * this may happen.
  3679. * :enum:`NGHTTP2_ERR_PROTO`
  3680. * The |session| is client session.
  3681. *
  3682. * .. warning::
  3683. *
  3684. * Calling this function twice for the same stream ID may lead to
  3685. * program crash. It is generally considered to a programming error
  3686. * to commit response twice.
  3687. */
  3688. NGHTTP2_EXTERN int
  3689. nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
  3690. const nghttp2_nv *nva, size_t nvlen,
  3691. const nghttp2_data_provider *data_prd);
  3692. /**
  3693. * @function
  3694. *
  3695. * Submits trailer fields HEADERS against the stream |stream_id|.
  3696. *
  3697. * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
  3698. * |nvlen| elements. The application must not include pseudo-header
  3699. * fields (headers whose names starts with ":") in |nva|.
  3700. *
  3701. * This function creates copies of all name/value pairs in |nva|. It
  3702. * also lower-cases all names in |nva|. The order of elements in
  3703. * |nva| is preserved. For header fields with
  3704. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
  3705. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
  3706. * and value are not copied respectively. With
  3707. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
  3708. * pass header field name in lowercase. The application should
  3709. * maintain the references to them until
  3710. * :type:`nghttp2_on_frame_send_callback` or
  3711. * :type:`nghttp2_on_frame_not_send_callback` is called.
  3712. *
  3713. * For server, trailer fields must follow response HEADERS or response
  3714. * DATA without END_STREAM flat set. The library does not enforce
  3715. * this requirement, and applications should do this for themselves.
  3716. * If `nghttp2_submit_trailer()` is called before any response HEADERS
  3717. * submission (usually by `nghttp2_submit_response()`), the content of
  3718. * |nva| will be sent as response headers, which will result in error.
  3719. *
  3720. * This function has the same effect with `nghttp2_submit_headers()`,
  3721. * with flags = :enum:`NGHTTP2_FLAG_END_STREAM` and both pri_spec and
  3722. * stream_user_data to NULL.
  3723. *
  3724. * To submit trailer fields after `nghttp2_submit_response()` is
  3725. * called, the application has to specify
  3726. * :type:`nghttp2_data_provider` to `nghttp2_submit_response()`.
  3727. * Inside of :type:`nghttp2_data_source_read_callback`, when setting
  3728. * :enum:`NGHTTP2_DATA_FLAG_EOF`, also set
  3729. * :enum:`NGHTTP2_DATA_FLAG_NO_END_STREAM`. After that, the
  3730. * application can send trailer fields using
  3731. * `nghttp2_submit_trailer()`. `nghttp2_submit_trailer()` can be used
  3732. * inside :type:`nghttp2_data_source_read_callback`.
  3733. *
  3734. * This function returns 0 if it succeeds and |stream_id| is -1.
  3735. * Otherwise, this function returns 0 if it succeeds, or one of the
  3736. * following negative error codes:
  3737. *
  3738. * :enum:`NGHTTP2_ERR_NOMEM`
  3739. * Out of memory.
  3740. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3741. * The |stream_id| is 0.
  3742. */
  3743. NGHTTP2_EXTERN int nghttp2_submit_trailer(nghttp2_session *session,
  3744. int32_t stream_id,
  3745. const nghttp2_nv *nva, size_t nvlen);
  3746. /**
  3747. * @function
  3748. *
  3749. * Submits HEADERS frame. The |flags| is bitwise OR of the
  3750. * following values:
  3751. *
  3752. * * :enum:`NGHTTP2_FLAG_END_STREAM`
  3753. *
  3754. * If |flags| includes :enum:`NGHTTP2_FLAG_END_STREAM`, this frame has
  3755. * END_STREAM flag set.
  3756. *
  3757. * The library handles the CONTINUATION frame internally and it
  3758. * correctly sets END_HEADERS to the last sequence of the PUSH_PROMISE
  3759. * or CONTINUATION frame.
  3760. *
  3761. * If the |stream_id| is -1, this frame is assumed as request (i.e.,
  3762. * request HEADERS frame which opens new stream). In this case, the
  3763. * assigned stream ID will be returned. Otherwise, specify stream ID
  3764. * in |stream_id|.
  3765. *
  3766. * The |pri_spec| is priority specification of this request. ``NULL``
  3767. * means the default priority (see
  3768. * `nghttp2_priority_spec_default_init()`). To specify the priority,
  3769. * use `nghttp2_priority_spec_init()`. If |pri_spec| is not ``NULL``,
  3770. * this function will copy its data members.
  3771. *
  3772. * The ``pri_spec->weight`` must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
  3773. * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive. If ``pri_spec->weight`` is
  3774. * strictly less than :enum:`NGHTTP2_MIN_WEIGHT`, it becomes
  3775. * :enum:`NGHTTP2_MIN_WEIGHT`. If it is strictly greater than
  3776. * :enum:`NGHTTP2_MAX_WEIGHT`, it becomes :enum:`NGHTTP2_MAX_WEIGHT`.
  3777. *
  3778. * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
  3779. * |nvlen| elements. The application is responsible to include
  3780. * required pseudo-header fields (header field whose name starts with
  3781. * ":") in |nva| and must place pseudo-headers before regular header
  3782. * fields.
  3783. *
  3784. * This function creates copies of all name/value pairs in |nva|. It
  3785. * also lower-cases all names in |nva|. The order of elements in
  3786. * |nva| is preserved. For header fields with
  3787. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
  3788. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
  3789. * and value are not copied respectively. With
  3790. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
  3791. * pass header field name in lowercase. The application should
  3792. * maintain the references to them until
  3793. * :type:`nghttp2_on_frame_send_callback` or
  3794. * :type:`nghttp2_on_frame_not_send_callback` is called.
  3795. *
  3796. * The |stream_user_data| is a pointer to an arbitrary data which is
  3797. * associated to the stream this frame will open. Therefore it is
  3798. * only used if this frame opens streams, in other words, it changes
  3799. * stream state from idle or reserved to open.
  3800. *
  3801. * This function is low-level in a sense that the application code can
  3802. * specify flags directly. For usual HTTP request,
  3803. * `nghttp2_submit_request()` is useful. Likewise, for HTTP response,
  3804. * prefer `nghttp2_submit_response()`.
  3805. *
  3806. * This function returns newly assigned stream ID if it succeeds and
  3807. * |stream_id| is -1. Otherwise, this function returns 0 if it
  3808. * succeeds, or one of the following negative error codes:
  3809. *
  3810. * :enum:`NGHTTP2_ERR_NOMEM`
  3811. * Out of memory.
  3812. * :enum:`NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
  3813. * No stream ID is available because maximum stream ID was
  3814. * reached.
  3815. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3816. * The |stream_id| is 0; or trying to depend on itself (stream ID
  3817. * equals ``pri_spec->stream_id``).
  3818. * :enum:`NGHTTP2_ERR_DATA_EXIST`
  3819. * DATA or HEADERS has been already submitted and not fully
  3820. * processed yet. This happens if stream denoted by |stream_id|
  3821. * is in reserved state.
  3822. * :enum:`NGHTTP2_ERR_PROTO`
  3823. * The |stream_id| is -1, and |session| is server session.
  3824. *
  3825. * .. warning::
  3826. *
  3827. * This function returns assigned stream ID if it succeeds and
  3828. * |stream_id| is -1. But that stream is not opened yet. The
  3829. * application must not submit frame to that stream ID before
  3830. * :type:`nghttp2_before_frame_send_callback` is called for this
  3831. * frame.
  3832. *
  3833. */
  3834. NGHTTP2_EXTERN int32_t nghttp2_submit_headers(
  3835. nghttp2_session *session, uint8_t flags, int32_t stream_id,
  3836. const nghttp2_priority_spec *pri_spec, const nghttp2_nv *nva, size_t nvlen,
  3837. void *stream_user_data);
  3838. /**
  3839. * @function
  3840. *
  3841. * Submits one or more DATA frames to the stream |stream_id|. The
  3842. * data to be sent are provided by |data_prd|. If |flags| contains
  3843. * :enum:`NGHTTP2_FLAG_END_STREAM`, the last DATA frame has END_STREAM
  3844. * flag set.
  3845. *
  3846. * This function does not take ownership of the |data_prd|. The
  3847. * function copies the members of the |data_prd|.
  3848. *
  3849. * This function returns 0 if it succeeds, or one of the following
  3850. * negative error codes:
  3851. *
  3852. * :enum:`NGHTTP2_ERR_NOMEM`
  3853. * Out of memory.
  3854. * :enum:`NGHTTP2_ERR_DATA_EXIST`
  3855. * DATA or HEADERS has been already submitted and not fully
  3856. * processed yet.
  3857. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3858. * The |stream_id| is 0.
  3859. * :enum:`NGHTTP2_ERR_STREAM_CLOSED`
  3860. * The stream was already closed; or the |stream_id| is invalid.
  3861. *
  3862. * .. note::
  3863. *
  3864. * Currently, only one DATA or HEADERS is allowed for a stream at a
  3865. * time. Submitting these frames more than once before first DATA
  3866. * or HEADERS is finished results in :enum:`NGHTTP2_ERR_DATA_EXIST`
  3867. * error code. The earliest callback which tells that previous
  3868. * frame is done is :type:`nghttp2_on_frame_send_callback`. In side
  3869. * that callback, new data can be submitted using
  3870. * `nghttp2_submit_data()`. Of course, all data except for last one
  3871. * must not have :enum:`NGHTTP2_FLAG_END_STREAM` flag set in
  3872. * |flags|. This sounds a bit complicated, and we recommend to use
  3873. * `nghttp2_submit_request()` and `nghttp2_submit_response()` to
  3874. * avoid this cascading issue. The experience shows that for HTTP
  3875. * use, these two functions are enough to implement both client and
  3876. * server.
  3877. */
  3878. NGHTTP2_EXTERN int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
  3879. int32_t stream_id,
  3880. const nghttp2_data_provider *data_prd);
  3881. /**
  3882. * @function
  3883. *
  3884. * Submits PRIORITY frame to change the priority of stream |stream_id|
  3885. * to the priority specification |pri_spec|.
  3886. *
  3887. * The |flags| is currently ignored and should be
  3888. * :enum:`NGHTTP2_FLAG_NONE`.
  3889. *
  3890. * The |pri_spec| is priority specification of this request. ``NULL``
  3891. * is not allowed for this function. To specify the priority, use
  3892. * `nghttp2_priority_spec_init()`. This function will copy its data
  3893. * members.
  3894. *
  3895. * The ``pri_spec->weight`` must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
  3896. * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive. If ``pri_spec->weight`` is
  3897. * strictly less than :enum:`NGHTTP2_MIN_WEIGHT`, it becomes
  3898. * :enum:`NGHTTP2_MIN_WEIGHT`. If it is strictly greater than
  3899. * :enum:`NGHTTP2_MAX_WEIGHT`, it becomes :enum:`NGHTTP2_MAX_WEIGHT`.
  3900. *
  3901. * This function returns 0 if it succeeds, or one of the following
  3902. * negative error codes:
  3903. *
  3904. * :enum:`NGHTTP2_ERR_NOMEM`
  3905. * Out of memory.
  3906. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3907. * The |stream_id| is 0; or the |pri_spec| is NULL; or trying to
  3908. * depend on itself.
  3909. */
  3910. NGHTTP2_EXTERN int
  3911. nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
  3912. int32_t stream_id,
  3913. const nghttp2_priority_spec *pri_spec);
  3914. /**
  3915. * @function
  3916. *
  3917. * Submits RST_STREAM frame to cancel/reject the stream |stream_id|
  3918. * with the error code |error_code|.
  3919. *
  3920. * The pre-defined error code is one of :enum:`nghttp2_error_code`.
  3921. *
  3922. * The |flags| is currently ignored and should be
  3923. * :enum:`NGHTTP2_FLAG_NONE`.
  3924. *
  3925. * This function returns 0 if it succeeds, or one of the following
  3926. * negative error codes:
  3927. *
  3928. * :enum:`NGHTTP2_ERR_NOMEM`
  3929. * Out of memory.
  3930. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3931. * The |stream_id| is 0.
  3932. */
  3933. NGHTTP2_EXTERN int nghttp2_submit_rst_stream(nghttp2_session *session,
  3934. uint8_t flags, int32_t stream_id,
  3935. uint32_t error_code);
  3936. /**
  3937. * @function
  3938. *
  3939. * Stores local settings and submits SETTINGS frame. The |iv| is the
  3940. * pointer to the array of :type:`nghttp2_settings_entry`. The |niv|
  3941. * indicates the number of :type:`nghttp2_settings_entry`.
  3942. *
  3943. * The |flags| is currently ignored and should be
  3944. * :enum:`NGHTTP2_FLAG_NONE`.
  3945. *
  3946. * This function does not take ownership of the |iv|. This function
  3947. * copies all the elements in the |iv|.
  3948. *
  3949. * While updating individual stream's local window size, if the window
  3950. * size becomes strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
  3951. * RST_STREAM is issued against such a stream.
  3952. *
  3953. * SETTINGS with :enum:`NGHTTP2_FLAG_ACK` is automatically submitted
  3954. * by the library and application could not send it at its will.
  3955. *
  3956. * This function returns 0 if it succeeds, or one of the following
  3957. * negative error codes:
  3958. *
  3959. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  3960. * The |iv| contains invalid value (e.g., initial window size
  3961. * strictly greater than (1 << 31) - 1.
  3962. * :enum:`NGHTTP2_ERR_NOMEM`
  3963. * Out of memory.
  3964. */
  3965. NGHTTP2_EXTERN int nghttp2_submit_settings(nghttp2_session *session,
  3966. uint8_t flags,
  3967. const nghttp2_settings_entry *iv,
  3968. size_t niv);
  3969. /**
  3970. * @function
  3971. *
  3972. * Submits PUSH_PROMISE frame.
  3973. *
  3974. * The |flags| is currently ignored. The library handles the
  3975. * CONTINUATION frame internally and it correctly sets END_HEADERS to
  3976. * the last sequence of the PUSH_PROMISE or CONTINUATION frame.
  3977. *
  3978. * The |stream_id| must be client initiated stream ID.
  3979. *
  3980. * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
  3981. * |nvlen| elements. The application is responsible to include
  3982. * required pseudo-header fields (header field whose name starts with
  3983. * ":") in |nva| and must place pseudo-headers before regular header
  3984. * fields.
  3985. *
  3986. * This function creates copies of all name/value pairs in |nva|. It
  3987. * also lower-cases all names in |nva|. The order of elements in
  3988. * |nva| is preserved. For header fields with
  3989. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
  3990. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
  3991. * and value are not copied respectively. With
  3992. * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
  3993. * pass header field name in lowercase. The application should
  3994. * maintain the references to them until
  3995. * :type:`nghttp2_on_frame_send_callback` or
  3996. * :type:`nghttp2_on_frame_not_send_callback` is called.
  3997. *
  3998. * The |promised_stream_user_data| is a pointer to an arbitrary data
  3999. * which is associated to the promised stream this frame will open and
  4000. * make it in reserved state. It is available using
  4001. * `nghttp2_session_get_stream_user_data()`. The application can
  4002. * access it in :type:`nghttp2_before_frame_send_callback` and
  4003. * :type:`nghttp2_on_frame_send_callback` of this frame.
  4004. *
  4005. * The client side is not allowed to use this function.
  4006. *
  4007. * To submit response headers and data, use
  4008. * `nghttp2_submit_response()`.
  4009. *
  4010. * This function returns assigned promised stream ID if it succeeds,
  4011. * or one of the following negative error codes:
  4012. *
  4013. * :enum:`NGHTTP2_ERR_NOMEM`
  4014. * Out of memory.
  4015. * :enum:`NGHTTP2_ERR_PROTO`
  4016. * This function was invoked when |session| is initialized as
  4017. * client.
  4018. * :enum:`NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
  4019. * No stream ID is available because maximum stream ID was
  4020. * reached.
  4021. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  4022. * The |stream_id| is 0; The |stream_id| does not designate stream
  4023. * that peer initiated.
  4024. * :enum:`NGHTTP2_ERR_STREAM_CLOSED`
  4025. * The stream was already closed; or the |stream_id| is invalid.
  4026. *
  4027. * .. warning::
  4028. *
  4029. * This function returns assigned promised stream ID if it succeeds.
  4030. * As of 1.16.0, stream object for pushed resource is created when
  4031. * this function succeeds. In that case, the application can submit
  4032. * push response for the promised frame.
  4033. *
  4034. * In 1.15.0 or prior versions, pushed stream is not opened yet when
  4035. * this function succeeds. The application must not submit frame to
  4036. * that stream ID before :type:`nghttp2_before_frame_send_callback`
  4037. * is called for this frame.
  4038. *
  4039. */
  4040. NGHTTP2_EXTERN int32_t nghttp2_submit_push_promise(
  4041. nghttp2_session *session, uint8_t flags, int32_t stream_id,
  4042. const nghttp2_nv *nva, size_t nvlen, void *promised_stream_user_data);
  4043. /**
  4044. * @function
  4045. *
  4046. * Submits PING frame. You don't have to send PING back when you
  4047. * received PING frame. The library automatically submits PING frame
  4048. * in this case.
  4049. *
  4050. * The |flags| is bitwise OR of 0 or more of the following value.
  4051. *
  4052. * * :enum:`NGHTTP2_FLAG_ACK`
  4053. *
  4054. * Unless `nghttp2_option_set_no_auto_ping_ack()` is used, the |flags|
  4055. * should be :enum:`NGHTTP2_FLAG_NONE`.
  4056. *
  4057. * If the |opaque_data| is non ``NULL``, then it should point to the 8
  4058. * bytes array of memory to specify opaque data to send with PING
  4059. * frame. If the |opaque_data| is ``NULL``, zero-cleared 8 bytes will
  4060. * be sent as opaque data.
  4061. *
  4062. * This function returns 0 if it succeeds, or one of the following
  4063. * negative error codes:
  4064. *
  4065. * :enum:`NGHTTP2_ERR_NOMEM`
  4066. * Out of memory.
  4067. */
  4068. NGHTTP2_EXTERN int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
  4069. const uint8_t *opaque_data);
  4070. /**
  4071. * @function
  4072. *
  4073. * Submits GOAWAY frame with the last stream ID |last_stream_id| and
  4074. * the error code |error_code|.
  4075. *
  4076. * The pre-defined error code is one of :enum:`nghttp2_error_code`.
  4077. *
  4078. * The |flags| is currently ignored and should be
  4079. * :enum:`NGHTTP2_FLAG_NONE`.
  4080. *
  4081. * The |last_stream_id| is peer's stream ID or 0. So if |session| is
  4082. * initialized as client, |last_stream_id| must be even or 0. If
  4083. * |session| is initialized as server, |last_stream_id| must be odd or
  4084. * 0.
  4085. *
  4086. * The HTTP/2 specification says last_stream_id must not be increased
  4087. * from the value previously sent. So the actual value sent as
  4088. * last_stream_id is the minimum value between the given
  4089. * |last_stream_id| and the last_stream_id previously sent to the
  4090. * peer.
  4091. *
  4092. * If the |opaque_data| is not ``NULL`` and |opaque_data_len| is not
  4093. * zero, those data will be sent as additional debug data. The
  4094. * library makes a copy of the memory region pointed by |opaque_data|
  4095. * with the length |opaque_data_len|, so the caller does not need to
  4096. * keep this memory after the return of this function. If the
  4097. * |opaque_data_len| is 0, the |opaque_data| could be ``NULL``.
  4098. *
  4099. * After successful transmission of GOAWAY, following things happen.
  4100. * All incoming streams having strictly more than |last_stream_id| are
  4101. * closed. All incoming HEADERS which starts new stream are simply
  4102. * ignored. After all active streams are handled, both
  4103. * `nghttp2_session_want_read()` and `nghttp2_session_want_write()`
  4104. * return 0 and the application can close session.
  4105. *
  4106. * This function returns 0 if it succeeds, or one of the following
  4107. * negative error codes:
  4108. *
  4109. * :enum:`NGHTTP2_ERR_NOMEM`
  4110. * Out of memory.
  4111. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  4112. * The |opaque_data_len| is too large; the |last_stream_id| is
  4113. * invalid.
  4114. */
  4115. NGHTTP2_EXTERN int nghttp2_submit_goaway(nghttp2_session *session,
  4116. uint8_t flags, int32_t last_stream_id,
  4117. uint32_t error_code,
  4118. const uint8_t *opaque_data,
  4119. size_t opaque_data_len);
  4120. /**
  4121. * @function
  4122. *
  4123. * Returns the last stream ID of a stream for which
  4124. * :type:`nghttp2_on_frame_recv_callback` was invoked most recently.
  4125. * The returned value can be used as last_stream_id parameter for
  4126. * `nghttp2_submit_goaway()` and
  4127. * `nghttp2_session_terminate_session2()`.
  4128. *
  4129. * This function always succeeds.
  4130. */
  4131. NGHTTP2_EXTERN int32_t
  4132. nghttp2_session_get_last_proc_stream_id(nghttp2_session *session);
  4133. /**
  4134. * @function
  4135. *
  4136. * Returns nonzero if new request can be sent from local endpoint.
  4137. *
  4138. * This function return 0 if request is not allowed for this session.
  4139. * There are several reasons why request is not allowed. Some of the
  4140. * reasons are: session is server; stream ID has been spent; GOAWAY
  4141. * has been sent or received.
  4142. *
  4143. * The application can call `nghttp2_submit_request()` without
  4144. * consulting this function. In that case, `nghttp2_submit_request()`
  4145. * may return error. Or, request is failed to sent, and
  4146. * :type:`nghttp2_on_stream_close_callback` is called.
  4147. */
  4148. NGHTTP2_EXTERN int
  4149. nghttp2_session_check_request_allowed(nghttp2_session *session);
  4150. /**
  4151. * @function
  4152. *
  4153. * Returns nonzero if |session| is initialized as server side session.
  4154. */
  4155. NGHTTP2_EXTERN int
  4156. nghttp2_session_check_server_session(nghttp2_session *session);
  4157. /**
  4158. * @function
  4159. *
  4160. * Submits WINDOW_UPDATE frame.
  4161. *
  4162. * The |flags| is currently ignored and should be
  4163. * :enum:`NGHTTP2_FLAG_NONE`.
  4164. *
  4165. * The |stream_id| is the stream ID to send this WINDOW_UPDATE. To
  4166. * send connection level WINDOW_UPDATE, specify 0 to |stream_id|.
  4167. *
  4168. * If the |window_size_increment| is positive, the WINDOW_UPDATE with
  4169. * that value as window_size_increment is queued. If the
  4170. * |window_size_increment| is larger than the received bytes from the
  4171. * remote endpoint, the local window size is increased by that
  4172. * difference. If the sole purpose is to increase the local window
  4173. * size, consider to use `nghttp2_session_set_local_window_size()`.
  4174. *
  4175. * If the |window_size_increment| is negative, the local window size
  4176. * is decreased by -|window_size_increment|. If automatic
  4177. * WINDOW_UPDATE is enabled
  4178. * (`nghttp2_option_set_no_auto_window_update()`), and the library
  4179. * decided that the WINDOW_UPDATE should be submitted, then
  4180. * WINDOW_UPDATE is queued with the current received bytes count. If
  4181. * the sole purpose is to decrease the local window size, consider to
  4182. * use `nghttp2_session_set_local_window_size()`.
  4183. *
  4184. * If the |window_size_increment| is 0, the function does nothing and
  4185. * returns 0.
  4186. *
  4187. * This function returns 0 if it succeeds, or one of the following
  4188. * negative error codes:
  4189. *
  4190. * :enum:`NGHTTP2_ERR_FLOW_CONTROL`
  4191. * The local window size overflow or gets negative.
  4192. * :enum:`NGHTTP2_ERR_NOMEM`
  4193. * Out of memory.
  4194. */
  4195. NGHTTP2_EXTERN int nghttp2_submit_window_update(nghttp2_session *session,
  4196. uint8_t flags,
  4197. int32_t stream_id,
  4198. int32_t window_size_increment);
  4199. /**
  4200. * @function
  4201. *
  4202. * Set local window size (local endpoints's window size) to the given
  4203. * |window_size| for the given stream denoted by |stream_id|. To
  4204. * change connection level window size, specify 0 to |stream_id|. To
  4205. * increase window size, this function may submit WINDOW_UPDATE frame
  4206. * to transmission queue.
  4207. *
  4208. * The |flags| is currently ignored and should be
  4209. * :enum:`NGHTTP2_FLAG_NONE`.
  4210. *
  4211. * This sounds similar to `nghttp2_submit_window_update()`, but there
  4212. * are 2 differences. The first difference is that this function
  4213. * takes the absolute value of window size to set, rather than the
  4214. * delta. To change the window size, this may be easier to use since
  4215. * the application just declares the intended window size, rather than
  4216. * calculating delta. The second difference is that
  4217. * `nghttp2_submit_window_update()` affects the received bytes count
  4218. * which has not acked yet. By the specification of
  4219. * `nghttp2_submit_window_update()`, to strictly increase the local
  4220. * window size, we have to submit delta including all received bytes
  4221. * count, which might not be desirable in some cases. On the other
  4222. * hand, this function does not affect the received bytes count. It
  4223. * just sets the local window size to the given value.
  4224. *
  4225. * This function returns 0 if it succeeds, or one of the following
  4226. * negative error codes:
  4227. *
  4228. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  4229. * The |stream_id| is negative.
  4230. * :enum:`NGHTTP2_ERR_NOMEM`
  4231. * Out of memory.
  4232. */
  4233. NGHTTP2_EXTERN int
  4234. nghttp2_session_set_local_window_size(nghttp2_session *session, uint8_t flags,
  4235. int32_t stream_id, int32_t window_size);
  4236. /**
  4237. * @function
  4238. *
  4239. * Submits extension frame.
  4240. *
  4241. * Application can pass arbitrary frame flags and stream ID in |flags|
  4242. * and |stream_id| respectively. The |payload| is opaque pointer, and
  4243. * it can be accessible though ``frame->ext.payload`` in
  4244. * :type:`nghttp2_pack_extension_callback`. The library will not own
  4245. * passed |payload| pointer.
  4246. *
  4247. * The application must set :type:`nghttp2_pack_extension_callback`
  4248. * using `nghttp2_session_callbacks_set_pack_extension_callback()`.
  4249. *
  4250. * The application should retain the memory pointed by |payload| until
  4251. * the transmission of extension frame is done (which is indicated by
  4252. * :type:`nghttp2_on_frame_send_callback`), or transmission fails
  4253. * (which is indicated by :type:`nghttp2_on_frame_not_send_callback`).
  4254. * If application does not touch this memory region after packing it
  4255. * into a wire format, application can free it inside
  4256. * :type:`nghttp2_pack_extension_callback`.
  4257. *
  4258. * The standard HTTP/2 frame cannot be sent with this function, so
  4259. * |type| must be strictly grater than 0x9. Otherwise, this function
  4260. * will fail with error code :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`.
  4261. *
  4262. * This function returns 0 if it succeeds, or one of the following
  4263. * negative error codes:
  4264. *
  4265. * :enum:`NGHTTP2_ERR_INVALID_STATE`
  4266. * If :type:`nghttp2_pack_extension_callback` is not set.
  4267. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  4268. * If |type| specifies standard HTTP/2 frame type. The frame
  4269. * types in the rage [0x0, 0x9], both inclusive, are standard
  4270. * HTTP/2 frame type, and cannot be sent using this function.
  4271. * :enum:`NGHTTP2_ERR_NOMEM`
  4272. * Out of memory
  4273. */
  4274. NGHTTP2_EXTERN int nghttp2_submit_extension(nghttp2_session *session,
  4275. uint8_t type, uint8_t flags,
  4276. int32_t stream_id, void *payload);
  4277. /**
  4278. * @struct
  4279. *
  4280. * The payload of ALTSVC frame. ALTSVC frame is a non-critical
  4281. * extension to HTTP/2. If this frame is received, and
  4282. * `nghttp2_option_set_user_recv_extension_type()` is not set, and
  4283. * `nghttp2_option_set_builtin_recv_extension_type()` is set for
  4284. * :enum:`NGHTTP2_ALTSVC`, ``nghttp2_extension.payload`` will point to
  4285. * this struct.
  4286. *
  4287. * It has the following members:
  4288. */
  4289. typedef struct {
  4290. /**
  4291. * The pointer to origin which this alternative service is
  4292. * associated with. This is not necessarily NULL-terminated.
  4293. */
  4294. uint8_t *origin;
  4295. /**
  4296. * The length of the |origin|.
  4297. */
  4298. size_t origin_len;
  4299. /**
  4300. * The pointer to Alt-Svc field value contained in ALTSVC frame.
  4301. * This is not necessarily NULL-terminated.
  4302. */
  4303. uint8_t *field_value;
  4304. /**
  4305. * The length of the |field_value|.
  4306. */
  4307. size_t field_value_len;
  4308. } nghttp2_ext_altsvc;
  4309. /**
  4310. * @function
  4311. *
  4312. * Submits ALTSVC frame.
  4313. *
  4314. * ALTSVC frame is a non-critical extension to HTTP/2, and defined in
  4315. * is defined in `RFC 7383
  4316. * <https://tools.ietf.org/html/rfc7838#section-4>`_.
  4317. *
  4318. * The |flags| is currently ignored and should be
  4319. * :enum:`NGHTTP2_FLAG_NONE`.
  4320. *
  4321. * The |origin| points to the origin this alternative service is
  4322. * associated with. The |origin_len| is the length of the origin. If
  4323. * |stream_id| is 0, the origin must be specified. If |stream_id| is
  4324. * not zero, the origin must be empty (in other words, |origin_len|
  4325. * must be 0).
  4326. *
  4327. * The ALTSVC frame is only usable from server side. If this function
  4328. * is invoked with client side session, this function returns
  4329. * :enum:`NGHTTP2_ERR_INVALID_STATE`.
  4330. *
  4331. * This function returns 0 if it succeeds, or one of the following
  4332. * negative error codes:
  4333. *
  4334. * :enum:`NGHTTP2_ERR_NOMEM`
  4335. * Out of memory
  4336. * :enum:`NGHTTP2_ERR_INVALID_STATE`
  4337. * The function is called from client side session
  4338. * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
  4339. * The sum of |origin_len| and |field_value_len| is larger than
  4340. * 16382; or |origin_len| is 0 while |stream_id| is 0; or
  4341. * |origin_len| is not 0 while |stream_id| is not 0.
  4342. */
  4343. NGHTTP2_EXTERN int nghttp2_submit_altsvc(nghttp2_session *session,
  4344. uint8_t flags, int32_t stream_id,
  4345. const uint8_t *origin,
  4346. size_t origin_len,
  4347. const uint8_t *field_value,
  4348. size_t field_value_len);
  4349. /**
  4350. * @function
  4351. *
  4352. * Compares ``lhs->name`` of length ``lhs->namelen`` bytes and
  4353. * ``rhs->name`` of length ``rhs->namelen`` bytes. Returns negative
  4354. * integer if ``lhs->name`` is found to be less than ``rhs->name``; or
  4355. * returns positive integer if ``lhs->name`` is found to be greater
  4356. * than ``rhs->name``; or returns 0 otherwise.
  4357. */
  4358. NGHTTP2_EXTERN int nghttp2_nv_compare_name(const nghttp2_nv *lhs,
  4359. const nghttp2_nv *rhs);
  4360. /**
  4361. * @function
  4362. *
  4363. * A helper function for dealing with NPN in client side or ALPN in
  4364. * server side. The |in| contains peer's protocol list in preferable
  4365. * order. The format of |in| is length-prefixed and not
  4366. * null-terminated. For example, ``h2`` and
  4367. * ``http/1.1`` stored in |in| like this::
  4368. *
  4369. * in[0] = 2
  4370. * in[1..2] = "h2"
  4371. * in[3] = 8
  4372. * in[4..11] = "http/1.1"
  4373. * inlen = 12
  4374. *
  4375. * The selection algorithm is as follows:
  4376. *
  4377. * 1. If peer's list contains HTTP/2 protocol the library supports,
  4378. * it is selected and returns 1. The following step is not taken.
  4379. *
  4380. * 2. If peer's list contains ``http/1.1``, this function selects
  4381. * ``http/1.1`` and returns 0. The following step is not taken.
  4382. *
  4383. * 3. This function selects nothing and returns -1 (So called
  4384. * non-overlap case). In this case, |out| and |outlen| are left
  4385. * untouched.
  4386. *
  4387. * Selecting ``h2`` means that ``h2`` is written into |*out| and its
  4388. * length (which is 2) is assigned to |*outlen|.
  4389. *
  4390. * For ALPN, refer to https://tools.ietf.org/html/rfc7301
  4391. *
  4392. * See http://technotes.googlecode.com/git/nextprotoneg.html for more
  4393. * details about NPN.
  4394. *
  4395. * For NPN, to use this method you should do something like::
  4396. *
  4397. * static int select_next_proto_cb(SSL* ssl,
  4398. * unsigned char **out,
  4399. * unsigned char *outlen,
  4400. * const unsigned char *in,
  4401. * unsigned int inlen,
  4402. * void *arg)
  4403. * {
  4404. * int rv;
  4405. * rv = nghttp2_select_next_protocol(out, outlen, in, inlen);
  4406. * if (rv == -1) {
  4407. * return SSL_TLSEXT_ERR_NOACK;
  4408. * }
  4409. * if (rv == 1) {
  4410. * ((MyType*)arg)->http2_selected = 1;
  4411. * }
  4412. * return SSL_TLSEXT_ERR_OK;
  4413. * }
  4414. * ...
  4415. * SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, my_obj);
  4416. *
  4417. */
  4418. NGHTTP2_EXTERN int nghttp2_select_next_protocol(unsigned char **out,
  4419. unsigned char *outlen,
  4420. const unsigned char *in,
  4421. unsigned int inlen);
  4422. /**
  4423. * @function
  4424. *
  4425. * Returns a pointer to a nghttp2_info struct with version information
  4426. * about the run-time library in use. The |least_version| argument
  4427. * can be set to a 24 bit numerical value for the least accepted
  4428. * version number and if the condition is not met, this function will
  4429. * return a ``NULL``. Pass in 0 to skip the version checking.
  4430. */
  4431. NGHTTP2_EXTERN nghttp2_info *nghttp2_version(int least_version);
  4432. /**
  4433. * @function
  4434. *
  4435. * Returns nonzero if the :type:`nghttp2_error` library error code
  4436. * |lib_error| is fatal.
  4437. */
  4438. NGHTTP2_EXTERN int nghttp2_is_fatal(int lib_error_code);
  4439. /**
  4440. * @function
  4441. *
  4442. * Returns nonzero if HTTP header field name |name| of length |len| is
  4443. * valid according to http://tools.ietf.org/html/rfc7230#section-3.2
  4444. *
  4445. * Because this is a header field name in HTTP2, the upper cased alphabet
  4446. * is treated as error.
  4447. */
  4448. NGHTTP2_EXTERN int nghttp2_check_header_name(const uint8_t *name, size_t len);
  4449. /**
  4450. * @function
  4451. *
  4452. * Returns nonzero if HTTP header field value |value| of length |len|
  4453. * is valid according to
  4454. * http://tools.ietf.org/html/rfc7230#section-3.2
  4455. */
  4456. NGHTTP2_EXTERN int nghttp2_check_header_value(const uint8_t *value, size_t len);
  4457. /* HPACK API */
  4458. struct nghttp2_hd_deflater;
  4459. /**
  4460. * @struct
  4461. *
  4462. * HPACK deflater object.
  4463. */
  4464. typedef struct nghttp2_hd_deflater nghttp2_hd_deflater;
  4465. /**
  4466. * @function
  4467. *
  4468. * Initializes |*deflater_ptr| for deflating name/values pairs.
  4469. *
  4470. * The |max_deflate_dynamic_table_size| is the upper bound of header
  4471. * table size the deflater will use.
  4472. *
  4473. * If this function fails, |*deflater_ptr| is left untouched.
  4474. *
  4475. * This function returns 0 if it succeeds, or one of the following
  4476. * negative error codes:
  4477. *
  4478. * :enum:`NGHTTP2_ERR_NOMEM`
  4479. * Out of memory.
  4480. */
  4481. NGHTTP2_EXTERN int
  4482. nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
  4483. size_t max_deflate_dynamic_table_size);
  4484. /**
  4485. * @function
  4486. *
  4487. * Like `nghttp2_hd_deflate_new()`, but with additional custom memory
  4488. * allocator specified in the |mem|.
  4489. *
  4490. * The |mem| can be ``NULL`` and the call is equivalent to
  4491. * `nghttp2_hd_deflate_new()`.
  4492. *
  4493. * This function does not take ownership |mem|. The application is
  4494. * responsible for freeing |mem|.
  4495. *
  4496. * The library code does not refer to |mem| pointer after this
  4497. * function returns, so the application can safely free it.
  4498. */
  4499. NGHTTP2_EXTERN int
  4500. nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr,
  4501. size_t max_deflate_dynamic_table_size,
  4502. nghttp2_mem *mem);
  4503. /**
  4504. * @function
  4505. *
  4506. * Deallocates any resources allocated for |deflater|.
  4507. */
  4508. NGHTTP2_EXTERN void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater);
  4509. /**
  4510. * @function
  4511. *
  4512. * Changes header table size of the |deflater| to
  4513. * |settings_max_dynamic_table_size| bytes. This may trigger eviction
  4514. * in the dynamic table.
  4515. *
  4516. * The |settings_max_dynamic_table_size| should be the value received
  4517. * in SETTINGS_HEADER_TABLE_SIZE.
  4518. *
  4519. * The deflater never uses more memory than
  4520. * ``max_deflate_dynamic_table_size`` bytes specified in
  4521. * `nghttp2_hd_deflate_new()`. Therefore, if
  4522. * |settings_max_dynamic_table_size| >
  4523. * ``max_deflate_dynamic_table_size``, resulting maximum table size
  4524. * becomes ``max_deflate_dynamic_table_size``.
  4525. *
  4526. * This function returns 0 if it succeeds, or one of the following
  4527. * negative error codes:
  4528. *
  4529. * :enum:`NGHTTP2_ERR_NOMEM`
  4530. * Out of memory.
  4531. */
  4532. NGHTTP2_EXTERN int
  4533. nghttp2_hd_deflate_change_table_size(nghttp2_hd_deflater *deflater,
  4534. size_t settings_max_dynamic_table_size);
  4535. /**
  4536. * @function
  4537. *
  4538. * Deflates the |nva|, which has the |nvlen| name/value pairs, into
  4539. * the |buf| of length |buflen|.
  4540. *
  4541. * If |buf| is not large enough to store the deflated header block,
  4542. * this function fails with :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`. The
  4543. * caller should use `nghttp2_hd_deflate_bound()` to know the upper
  4544. * bound of buffer size required to deflate given header name/value
  4545. * pairs.
  4546. *
  4547. * Once this function fails, subsequent call of this function always
  4548. * returns :enum:`NGHTTP2_ERR_HEADER_COMP`.
  4549. *
  4550. * After this function returns, it is safe to delete the |nva|.
  4551. *
  4552. * This function returns the number of bytes written to |buf| if it
  4553. * succeeds, or one of the following negative error codes:
  4554. *
  4555. * :enum:`NGHTTP2_ERR_NOMEM`
  4556. * Out of memory.
  4557. * :enum:`NGHTTP2_ERR_HEADER_COMP`
  4558. * Deflation process has failed.
  4559. * :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`
  4560. * The provided |buflen| size is too small to hold the output.
  4561. */
  4562. NGHTTP2_EXTERN ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater,
  4563. uint8_t *buf, size_t buflen,
  4564. const nghttp2_nv *nva,
  4565. size_t nvlen);
  4566. /**
  4567. * @function
  4568. *
  4569. * Deflates the |nva|, which has the |nvlen| name/value pairs, into
  4570. * the |veclen| size of buf vector |vec|. The each size of buffer
  4571. * must be set in len field of :type:`nghttp2_vec`. If and only if
  4572. * one chunk is filled up completely, next chunk will be used. If
  4573. * |vec| is not large enough to store the deflated header block, this
  4574. * function fails with :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`. The caller
  4575. * should use `nghttp2_hd_deflate_bound()` to know the upper bound of
  4576. * buffer size required to deflate given header name/value pairs.
  4577. *
  4578. * Once this function fails, subsequent call of this function always
  4579. * returns :enum:`NGHTTP2_ERR_HEADER_COMP`.
  4580. *
  4581. * After this function returns, it is safe to delete the |nva|.
  4582. *
  4583. * This function returns the number of bytes written to |vec| if it
  4584. * succeeds, or one of the following negative error codes:
  4585. *
  4586. * :enum:`NGHTTP2_ERR_NOMEM`
  4587. * Out of memory.
  4588. * :enum:`NGHTTP2_ERR_HEADER_COMP`
  4589. * Deflation process has failed.
  4590. * :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`
  4591. * The provided |buflen| size is too small to hold the output.
  4592. */
  4593. NGHTTP2_EXTERN ssize_t nghttp2_hd_deflate_hd_vec(nghttp2_hd_deflater *deflater,
  4594. const nghttp2_vec *vec,
  4595. size_t veclen,
  4596. const nghttp2_nv *nva,
  4597. size_t nvlen);
  4598. /**
  4599. * @function
  4600. *
  4601. * Returns an upper bound on the compressed size after deflation of
  4602. * |nva| of length |nvlen|.
  4603. */
  4604. NGHTTP2_EXTERN size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater,
  4605. const nghttp2_nv *nva,
  4606. size_t nvlen);
  4607. /**
  4608. * @function
  4609. *
  4610. * Returns the number of entries that header table of |deflater|
  4611. * contains. This is the sum of the number of static table and
  4612. * dynamic table, so the return value is at least 61.
  4613. */
  4614. NGHTTP2_EXTERN
  4615. size_t nghttp2_hd_deflate_get_num_table_entries(nghttp2_hd_deflater *deflater);
  4616. /**
  4617. * @function
  4618. *
  4619. * Returns the table entry denoted by |idx| from header table of
  4620. * |deflater|. The |idx| is 1-based, and idx=1 returns first entry of
  4621. * static table. idx=62 returns first entry of dynamic table if it
  4622. * exists. Specifying idx=0 is error, and this function returns NULL.
  4623. * If |idx| is strictly greater than the number of entries the tables
  4624. * contain, this function returns NULL.
  4625. */
  4626. NGHTTP2_EXTERN
  4627. const nghttp2_nv *
  4628. nghttp2_hd_deflate_get_table_entry(nghttp2_hd_deflater *deflater, size_t idx);
  4629. /**
  4630. * @function
  4631. *
  4632. * Returns the used dynamic table size, including the overhead 32
  4633. * bytes per entry described in RFC 7541.
  4634. */
  4635. NGHTTP2_EXTERN
  4636. size_t nghttp2_hd_deflate_get_dynamic_table_size(nghttp2_hd_deflater *deflater);
  4637. /**
  4638. * @function
  4639. *
  4640. * Returns the maximum dynamic table size.
  4641. */
  4642. NGHTTP2_EXTERN
  4643. size_t
  4644. nghttp2_hd_deflate_get_max_dynamic_table_size(nghttp2_hd_deflater *deflater);
  4645. struct nghttp2_hd_inflater;
  4646. /**
  4647. * @struct
  4648. *
  4649. * HPACK inflater object.
  4650. */
  4651. typedef struct nghttp2_hd_inflater nghttp2_hd_inflater;
  4652. /**
  4653. * @function
  4654. *
  4655. * Initializes |*inflater_ptr| for inflating name/values pairs.
  4656. *
  4657. * If this function fails, |*inflater_ptr| is left untouched.
  4658. *
  4659. * This function returns 0 if it succeeds, or one of the following
  4660. * negative error codes:
  4661. *
  4662. * :enum:`NGHTTP2_ERR_NOMEM`
  4663. * Out of memory.
  4664. */
  4665. NGHTTP2_EXTERN int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr);
  4666. /**
  4667. * @function
  4668. *
  4669. * Like `nghttp2_hd_inflate_new()`, but with additional custom memory
  4670. * allocator specified in the |mem|.
  4671. *
  4672. * The |mem| can be ``NULL`` and the call is equivalent to
  4673. * `nghttp2_hd_inflate_new()`.
  4674. *
  4675. * This function does not take ownership |mem|. The application is
  4676. * responsible for freeing |mem|.
  4677. *
  4678. * The library code does not refer to |mem| pointer after this
  4679. * function returns, so the application can safely free it.
  4680. */
  4681. NGHTTP2_EXTERN int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr,
  4682. nghttp2_mem *mem);
  4683. /**
  4684. * @function
  4685. *
  4686. * Deallocates any resources allocated for |inflater|.
  4687. */
  4688. NGHTTP2_EXTERN void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater);
  4689. /**
  4690. * @function
  4691. *
  4692. * Changes header table size in the |inflater|. This may trigger
  4693. * eviction in the dynamic table.
  4694. *
  4695. * The |settings_max_dynamic_table_size| should be the value
  4696. * transmitted in SETTINGS_HEADER_TABLE_SIZE.
  4697. *
  4698. * This function must not be called while header block is being
  4699. * inflated. In other words, this function must be called after
  4700. * initialization of |inflater|, but before calling
  4701. * `nghttp2_hd_inflate_hd2()`, or after
  4702. * `nghttp2_hd_inflate_end_headers()`. Otherwise,
  4703. * `NGHTTP2_ERR_INVALID_STATE` was returned.
  4704. *
  4705. * This function returns 0 if it succeeds, or one of the following
  4706. * negative error codes:
  4707. *
  4708. * :enum:`NGHTTP2_ERR_NOMEM`
  4709. * Out of memory.
  4710. * :enum:`NGHTTP2_ERR_INVALID_STATE`
  4711. * The function is called while header block is being inflated.
  4712. * Probably, application missed to call
  4713. * `nghttp2_hd_inflate_end_headers()`.
  4714. */
  4715. NGHTTP2_EXTERN int
  4716. nghttp2_hd_inflate_change_table_size(nghttp2_hd_inflater *inflater,
  4717. size_t settings_max_dynamic_table_size);
  4718. /**
  4719. * @enum
  4720. *
  4721. * The flags for header inflation.
  4722. */
  4723. typedef enum {
  4724. /**
  4725. * No flag set.
  4726. */
  4727. NGHTTP2_HD_INFLATE_NONE = 0,
  4728. /**
  4729. * Indicates all headers were inflated.
  4730. */
  4731. NGHTTP2_HD_INFLATE_FINAL = 0x01,
  4732. /**
  4733. * Indicates a header was emitted.
  4734. */
  4735. NGHTTP2_HD_INFLATE_EMIT = 0x02
  4736. } nghttp2_hd_inflate_flag;
  4737. /**
  4738. * @function
  4739. *
  4740. * .. warning::
  4741. *
  4742. * Deprecated. Use `nghttp2_hd_inflate_hd2()` instead.
  4743. *
  4744. * Inflates name/value block stored in |in| with length |inlen|. This
  4745. * function performs decompression. For each successful emission of
  4746. * header name/value pair, :enum:`NGHTTP2_HD_INFLATE_EMIT` is set in
  4747. * |*inflate_flags| and name/value pair is assigned to the |nv_out|
  4748. * and the function returns. The caller must not free the members of
  4749. * |nv_out|.
  4750. *
  4751. * The |nv_out| may include pointers to the memory region in the |in|.
  4752. * The caller must retain the |in| while the |nv_out| is used.
  4753. *
  4754. * The application should call this function repeatedly until the
  4755. * ``(*inflate_flags) & NGHTTP2_HD_INFLATE_FINAL`` is nonzero and
  4756. * return value is non-negative. This means the all input values are
  4757. * processed successfully. Then the application must call
  4758. * `nghttp2_hd_inflate_end_headers()` to prepare for the next header
  4759. * block input.
  4760. *
  4761. * The caller can feed complete compressed header block. It also can
  4762. * feed it in several chunks. The caller must set |in_final| to
  4763. * nonzero if the given input is the last block of the compressed
  4764. * header.
  4765. *
  4766. * This function returns the number of bytes processed if it succeeds,
  4767. * or one of the following negative error codes:
  4768. *
  4769. * :enum:`NGHTTP2_ERR_NOMEM`
  4770. * Out of memory.
  4771. * :enum:`NGHTTP2_ERR_HEADER_COMP`
  4772. * Inflation process has failed.
  4773. * :enum:`NGHTTP2_ERR_BUFFER_ERROR`
  4774. * The header field name or value is too large.
  4775. *
  4776. * Example follows::
  4777. *
  4778. * int inflate_header_block(nghttp2_hd_inflater *hd_inflater,
  4779. * uint8_t *in, size_t inlen, int final)
  4780. * {
  4781. * ssize_t rv;
  4782. *
  4783. * for(;;) {
  4784. * nghttp2_nv nv;
  4785. * int inflate_flags = 0;
  4786. *
  4787. * rv = nghttp2_hd_inflate_hd(hd_inflater, &nv, &inflate_flags,
  4788. * in, inlen, final);
  4789. *
  4790. * if(rv < 0) {
  4791. * fprintf(stderr, "inflate failed with error code %zd", rv);
  4792. * return -1;
  4793. * }
  4794. *
  4795. * in += rv;
  4796. * inlen -= rv;
  4797. *
  4798. * if(inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
  4799. * fwrite(nv.name, nv.namelen, 1, stderr);
  4800. * fprintf(stderr, ": ");
  4801. * fwrite(nv.value, nv.valuelen, 1, stderr);
  4802. * fprintf(stderr, "\n");
  4803. * }
  4804. * if(inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
  4805. * nghttp2_hd_inflate_end_headers(hd_inflater);
  4806. * break;
  4807. * }
  4808. * if((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 &&
  4809. * inlen == 0) {
  4810. * break;
  4811. * }
  4812. * }
  4813. *
  4814. * return 0;
  4815. * }
  4816. *
  4817. */
  4818. NGHTTP2_EXTERN ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater,
  4819. nghttp2_nv *nv_out,
  4820. int *inflate_flags, uint8_t *in,
  4821. size_t inlen, int in_final);
  4822. /**
  4823. * @function
  4824. *
  4825. * Inflates name/value block stored in |in| with length |inlen|. This
  4826. * function performs decompression. For each successful emission of
  4827. * header name/value pair, :enum:`NGHTTP2_HD_INFLATE_EMIT` is set in
  4828. * |*inflate_flags| and name/value pair is assigned to the |nv_out|
  4829. * and the function returns. The caller must not free the members of
  4830. * |nv_out|.
  4831. *
  4832. * The |nv_out| may include pointers to the memory region in the |in|.
  4833. * The caller must retain the |in| while the |nv_out| is used.
  4834. *
  4835. * The application should call this function repeatedly until the
  4836. * ``(*inflate_flags) & NGHTTP2_HD_INFLATE_FINAL`` is nonzero and
  4837. * return value is non-negative. If that happens, all given input
  4838. * data (|inlen| bytes) are processed successfully. Then the
  4839. * application must call `nghttp2_hd_inflate_end_headers()` to prepare
  4840. * for the next header block input.
  4841. *
  4842. * In other words, if |in_final| is nonzero, and this function returns
  4843. * |inlen|, you can assert that :enum:`NGHTTP2_HD_INFLATE_FINAL` is
  4844. * set in |*inflate_flags|.
  4845. *
  4846. * The caller can feed complete compressed header block. It also can
  4847. * feed it in several chunks. The caller must set |in_final| to
  4848. * nonzero if the given input is the last block of the compressed
  4849. * header.
  4850. *
  4851. * This function returns the number of bytes processed if it succeeds,
  4852. * or one of the following negative error codes:
  4853. *
  4854. * :enum:`NGHTTP2_ERR_NOMEM`
  4855. * Out of memory.
  4856. * :enum:`NGHTTP2_ERR_HEADER_COMP`
  4857. * Inflation process has failed.
  4858. * :enum:`NGHTTP2_ERR_BUFFER_ERROR`
  4859. * The header field name or value is too large.
  4860. *
  4861. * Example follows::
  4862. *
  4863. * int inflate_header_block(nghttp2_hd_inflater *hd_inflater,
  4864. * uint8_t *in, size_t inlen, int final)
  4865. * {
  4866. * ssize_t rv;
  4867. *
  4868. * for(;;) {
  4869. * nghttp2_nv nv;
  4870. * int inflate_flags = 0;
  4871. *
  4872. * rv = nghttp2_hd_inflate_hd2(hd_inflater, &nv, &inflate_flags,
  4873. * in, inlen, final);
  4874. *
  4875. * if(rv < 0) {
  4876. * fprintf(stderr, "inflate failed with error code %zd", rv);
  4877. * return -1;
  4878. * }
  4879. *
  4880. * in += rv;
  4881. * inlen -= rv;
  4882. *
  4883. * if(inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
  4884. * fwrite(nv.name, nv.namelen, 1, stderr);
  4885. * fprintf(stderr, ": ");
  4886. * fwrite(nv.value, nv.valuelen, 1, stderr);
  4887. * fprintf(stderr, "\n");
  4888. * }
  4889. * if(inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
  4890. * nghttp2_hd_inflate_end_headers(hd_inflater);
  4891. * break;
  4892. * }
  4893. * if((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 &&
  4894. * inlen == 0) {
  4895. * break;
  4896. * }
  4897. * }
  4898. *
  4899. * return 0;
  4900. * }
  4901. *
  4902. */
  4903. NGHTTP2_EXTERN ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater,
  4904. nghttp2_nv *nv_out,
  4905. int *inflate_flags,
  4906. const uint8_t *in, size_t inlen,
  4907. int in_final);
  4908. /**
  4909. * @function
  4910. *
  4911. * Signals the end of decompression for one header block.
  4912. *
  4913. * This function returns 0 if it succeeds. Currently this function
  4914. * always succeeds.
  4915. */
  4916. NGHTTP2_EXTERN int
  4917. nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater);
  4918. /**
  4919. * @function
  4920. *
  4921. * Returns the number of entries that header table of |inflater|
  4922. * contains. This is the sum of the number of static table and
  4923. * dynamic table, so the return value is at least 61.
  4924. */
  4925. NGHTTP2_EXTERN
  4926. size_t nghttp2_hd_inflate_get_num_table_entries(nghttp2_hd_inflater *inflater);
  4927. /**
  4928. * @function
  4929. *
  4930. * Returns the table entry denoted by |idx| from header table of
  4931. * |inflater|. The |idx| is 1-based, and idx=1 returns first entry of
  4932. * static table. idx=62 returns first entry of dynamic table if it
  4933. * exists. Specifying idx=0 is error, and this function returns NULL.
  4934. * If |idx| is strictly greater than the number of entries the tables
  4935. * contain, this function returns NULL.
  4936. */
  4937. NGHTTP2_EXTERN
  4938. const nghttp2_nv *
  4939. nghttp2_hd_inflate_get_table_entry(nghttp2_hd_inflater *inflater, size_t idx);
  4940. /**
  4941. * @function
  4942. *
  4943. * Returns the used dynamic table size, including the overhead 32
  4944. * bytes per entry described in RFC 7541.
  4945. */
  4946. NGHTTP2_EXTERN
  4947. size_t nghttp2_hd_inflate_get_dynamic_table_size(nghttp2_hd_inflater *inflater);
  4948. /**
  4949. * @function
  4950. *
  4951. * Returns the maximum dynamic table size.
  4952. */
  4953. NGHTTP2_EXTERN
  4954. size_t
  4955. nghttp2_hd_inflate_get_max_dynamic_table_size(nghttp2_hd_inflater *inflater);
  4956. struct nghttp2_stream;
  4957. /**
  4958. * @struct
  4959. *
  4960. * The structure to represent HTTP/2 stream. The details of this
  4961. * structure are intentionally hidden from the public API.
  4962. */
  4963. typedef struct nghttp2_stream nghttp2_stream;
  4964. /**
  4965. * @function
  4966. *
  4967. * Returns pointer to :type:`nghttp2_stream` object denoted by
  4968. * |stream_id|. If stream was not found, returns NULL.
  4969. *
  4970. * Returns imaginary root stream (see
  4971. * `nghttp2_session_get_root_stream()`) if 0 is given in |stream_id|.
  4972. *
  4973. * Unless |stream_id| == 0, the returned pointer is valid until next
  4974. * call of `nghttp2_session_send()`, `nghttp2_session_mem_send()`,
  4975. * `nghttp2_session_recv()`, and `nghttp2_session_mem_recv()`.
  4976. */
  4977. NGHTTP2_EXTERN nghttp2_stream *
  4978. nghttp2_session_find_stream(nghttp2_session *session, int32_t stream_id);
  4979. /**
  4980. * @enum
  4981. *
  4982. * State of stream as described in RFC 7540.
  4983. */
  4984. typedef enum {
  4985. /**
  4986. * idle state.
  4987. */
  4988. NGHTTP2_STREAM_STATE_IDLE = 1,
  4989. /**
  4990. * open state.
  4991. */
  4992. NGHTTP2_STREAM_STATE_OPEN,
  4993. /**
  4994. * reserved (local) state.
  4995. */
  4996. NGHTTP2_STREAM_STATE_RESERVED_LOCAL,
  4997. /**
  4998. * reserved (remote) state.
  4999. */
  5000. NGHTTP2_STREAM_STATE_RESERVED_REMOTE,
  5001. /**
  5002. * half closed (local) state.
  5003. */
  5004. NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL,
  5005. /**
  5006. * half closed (remote) state.
  5007. */
  5008. NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE,
  5009. /**
  5010. * closed state.
  5011. */
  5012. NGHTTP2_STREAM_STATE_CLOSED
  5013. } nghttp2_stream_proto_state;
  5014. /**
  5015. * @function
  5016. *
  5017. * Returns state of |stream|. The root stream retrieved by
  5018. * `nghttp2_session_get_root_stream()` will have stream state
  5019. * :enum:`NGHTTP2_STREAM_STATE_IDLE`.
  5020. */
  5021. NGHTTP2_EXTERN nghttp2_stream_proto_state
  5022. nghttp2_stream_get_state(nghttp2_stream *stream);
  5023. /**
  5024. * @function
  5025. *
  5026. * Returns root of dependency tree, which is imaginary stream with
  5027. * stream ID 0. The returned pointer is valid until |session| is
  5028. * freed by `nghttp2_session_del()`.
  5029. */
  5030. NGHTTP2_EXTERN nghttp2_stream *
  5031. nghttp2_session_get_root_stream(nghttp2_session *session);
  5032. /**
  5033. * @function
  5034. *
  5035. * Returns the parent stream of |stream| in dependency tree. Returns
  5036. * NULL if there is no such stream.
  5037. */
  5038. NGHTTP2_EXTERN nghttp2_stream *
  5039. nghttp2_stream_get_parent(nghttp2_stream *stream);
  5040. NGHTTP2_EXTERN int32_t nghttp2_stream_get_stream_id(nghttp2_stream *stream);
  5041. /**
  5042. * @function
  5043. *
  5044. * Returns the next sibling stream of |stream| in dependency tree.
  5045. * Returns NULL if there is no such stream.
  5046. */
  5047. NGHTTP2_EXTERN nghttp2_stream *
  5048. nghttp2_stream_get_next_sibling(nghttp2_stream *stream);
  5049. /**
  5050. * @function
  5051. *
  5052. * Returns the previous sibling stream of |stream| in dependency tree.
  5053. * Returns NULL if there is no such stream.
  5054. */
  5055. NGHTTP2_EXTERN nghttp2_stream *
  5056. nghttp2_stream_get_previous_sibling(nghttp2_stream *stream);
  5057. /**
  5058. * @function
  5059. *
  5060. * Returns the first child stream of |stream| in dependency tree.
  5061. * Returns NULL if there is no such stream.
  5062. */
  5063. NGHTTP2_EXTERN nghttp2_stream *
  5064. nghttp2_stream_get_first_child(nghttp2_stream *stream);
  5065. /**
  5066. * @function
  5067. *
  5068. * Returns dependency weight to the parent stream of |stream|.
  5069. */
  5070. NGHTTP2_EXTERN int32_t nghttp2_stream_get_weight(nghttp2_stream *stream);
  5071. /**
  5072. * @function
  5073. *
  5074. * Returns the sum of the weight for |stream|'s children.
  5075. */
  5076. NGHTTP2_EXTERN int32_t
  5077. nghttp2_stream_get_sum_dependency_weight(nghttp2_stream *stream);
  5078. /**
  5079. * @functypedef
  5080. *
  5081. * Callback function invoked when the library outputs debug logging.
  5082. * The function is called with arguments suitable for ``vfprintf(3)``
  5083. *
  5084. * The debug output is only enabled if the library is built with
  5085. * ``DEBUGBUILD`` macro defined.
  5086. */
  5087. typedef void (*nghttp2_debug_vprintf_callback)(const char *format,
  5088. va_list args);
  5089. /**
  5090. * @function
  5091. *
  5092. * Sets a debug output callback called by the library when built with
  5093. * ``DEBUGBUILD`` macro defined. If this option is not used, debug
  5094. * log is written into standard error output.
  5095. *
  5096. * For builds without ``DEBUGBUILD`` macro defined, this function is
  5097. * noop.
  5098. *
  5099. * Note that building with ``DEBUGBUILD`` may cause significant
  5100. * performance penalty to libnghttp2 because of extra processing. It
  5101. * should be used for debugging purpose only.
  5102. *
  5103. * .. Warning::
  5104. *
  5105. * Building with ``DEBUGBUILD`` may cause significant performance
  5106. * penalty to libnghttp2 because of extra processing. It should be
  5107. * used for debugging purpose only. We write this two times because
  5108. * this is important.
  5109. */
  5110. NGHTTP2_EXTERN void nghttp2_set_debug_vprintf_callback(
  5111. nghttp2_debug_vprintf_callback debug_vprintf_callback);
  5112. #ifdef __cplusplus
  5113. }
  5114. #endif
  5115. #endif /* NGHTTP2_H */