libmicrohttpd.texi 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761
  1. \input texinfo
  2. @setfilename libmicrohttpd.info
  3. @documentencoding UTF-8
  4. @include version.texi
  5. @settitle The GNU libmicrohttpd Reference Manual
  6. @c Unify all the indices into concept index.
  7. @syncodeindex vr cp
  8. @syncodeindex ky cp
  9. @syncodeindex pg cp
  10. @copying
  11. This manual is for GNU libmicrohttpd
  12. (version @value{VERSION}, @value{UPDATED}), a library for embedding
  13. an HTTP(S) server into C applications.
  14. Copyright @copyright{} 2007--2019 Christian Grothoff
  15. @quotation
  16. Permission is granted to copy, distribute and/or modify this document
  17. under the terms of the GNU Free Documentation License, Version 1.3
  18. or any later version published by the Free Software Foundation;
  19. with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
  20. Texts. A copy of the license is included in the section entitled "GNU
  21. Free Documentation License".
  22. @end quotation
  23. @end copying
  24. @dircategory Software libraries
  25. @direntry
  26. * libmicrohttpd: (libmicrohttpd). Embedded HTTP server library.
  27. @end direntry
  28. @c
  29. @c Titlepage
  30. @c
  31. @titlepage
  32. @title The GNU libmicrohttpd Reference Manual
  33. @subtitle Version @value{VERSION}
  34. @subtitle @value{UPDATED}
  35. @author Marco Maggi (@email{marco.maggi-ipsu@@poste.it})
  36. @author Christian Grothoff (@email{christian@@grothoff.org})
  37. @page
  38. @vskip 0pt plus 1filll
  39. @insertcopying
  40. @end titlepage
  41. @summarycontents
  42. @contents
  43. @c ------------------------------------------------------------
  44. @ifnottex
  45. @node Top
  46. @top The GNU libmicrohttpd Library
  47. @insertcopying
  48. @end ifnottex
  49. @menu
  50. * microhttpd-intro:: Introduction.
  51. * microhttpd-const:: Constants.
  52. * microhttpd-struct:: Structures type definition.
  53. * microhttpd-cb:: Callback functions definition.
  54. * microhttpd-init:: Starting and stopping the server.
  55. * microhttpd-inspect:: Implementing external @code{select}.
  56. * microhttpd-requests:: Handling requests.
  57. * microhttpd-responses:: Building responses to requests.
  58. * microhttpd-flow:: Flow control.
  59. * microhttpd-dauth:: Utilizing Authentication.
  60. * microhttpd-post:: Adding a @code{POST} processor.
  61. * microhttpd-info:: Obtaining and modifying status information.
  62. * microhttpd-util:: Utilities.
  63. * microhttpd-websocket:: Websockets.
  64. Appendices
  65. * GNU-LGPL:: The GNU Lesser General Public License says how you
  66. can copy and share almost all of `libmicrohttpd'.
  67. * eCos License:: The eCos License says how you can copy and share some parts of `libmicrohttpd'.
  68. * GNU-GPL:: The GNU General Public License (with eCos extension) says how you can copy and share some parts of `libmicrohttpd'.
  69. * GNU-FDL:: The GNU Free Documentation License says how you
  70. can copy and share the documentation of `libmicrohttpd'.
  71. Indices
  72. * Concept Index:: Index of concepts and programs.
  73. * Function and Data Index:: Index of functions, variables and data types.
  74. * Type Index:: Index of data types.
  75. @end menu
  76. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  77. @c ------------------------------------------------------------
  78. @node microhttpd-intro
  79. @chapter Introduction
  80. @noindent
  81. All symbols defined in the public API start with @code{MHD_}. MHD
  82. is a small HTTP daemon library. As such, it does not have any API
  83. for logging errors (you can only enable or disable logging to stderr).
  84. Also, it may not support all of the HTTP features directly, where
  85. applicable, portions of HTTP may have to be handled by clients of the
  86. library.
  87. The library is supposed to handle everything that it must handle
  88. (because the API would not allow clients to do this), such as basic
  89. connection management. However, detailed interpretations of headers,
  90. such as range requests, are left to the main application. In
  91. particular, if an application developer wants to support range
  92. requests, he needs to explicitly indicate support in responses and
  93. also explicitly parse the range header and generate a response (for
  94. example, using the @code{MHD_create_response_from_fd_at_offset} call
  95. to serve ranges from a file). MHD does understands headers that
  96. control connection management (specifically, @code{Connection: close}
  97. and @code{Expect: 100 continue} are understood and handled
  98. automatically). @code{Connection: upgrade} is supported by passing
  99. control over the socket (or something that behaves like the real
  100. socket in the case of TLS) to the application (after sending the
  101. desired HTTP response header).
  102. MHD largely ignores the semantics of the different HTTP methods,
  103. so clients are left to handle those. One exception is that MHD does
  104. understand @code{HEAD} and will only send the headers of the response
  105. and not the body, even if the client supplied a body. (In fact,
  106. clients do need to construct a response with the correct length, even
  107. for @code{HEAD} request.)
  108. MHD understands @code{POST} data and is able to decode certain
  109. formats (at the moment only @code{application/x-www-form-urlencoded}
  110. and @code{multipart/form-data}) using the post processor API. The
  111. data stream of a POST is also provided directly to the main
  112. application, so unsupported encodings could still be processed, just
  113. not conveniently by MHD.
  114. The header file defines various constants used by the HTTP protocol.
  115. This does not mean that MHD actually interprets all of these values.
  116. The provided constants are exported as a convenience for users of the
  117. library. MHD does not verify that transmitted HTTP headers are
  118. part of the standard specification; users of the library are free to
  119. define their own extensions of the HTTP standard and use those with
  120. MHD.
  121. All functions are guaranteed to be completely reentrant and
  122. thread-safe. MHD checks for allocation failures and tries to
  123. recover gracefully (for example, by closing the connection).
  124. Additionally, clients can specify resource limits on the overall
  125. number of connections, number of connections per IP address and memory
  126. used per connection to avoid resource exhaustion.
  127. @section Scope
  128. MHD is currently used in a wide range of implementations.
  129. Examples based on reports we've received from developers include:
  130. @itemize
  131. @item Embedded HTTP server on a cortex M3 (128 KB code space)
  132. @item Large-scale multimedia server (reportedly serving at the
  133. simulator limit of 7.5 GB/s)
  134. @item Administrative console (via HTTP/HTTPS) for network appliances
  135. @c If you have other interesting examples, please let us know
  136. @end itemize
  137. @section Thread modes and event loops
  138. @cindex poll
  139. @cindex epoll
  140. @cindex select
  141. MHD supports four basic thread modes and up to three event loop
  142. styles.
  143. The four basic thread modes are external sockets polling (MHD creates
  144. no threads, event loop is fully managed by the application), internal
  145. polling (MHD creates one thread for all connections), polling in
  146. thread pool (MHD creates a thread pool which is used to process all
  147. connections) and thread-per-connection (MHD creates one thread for
  148. listen sockets and then one thread per accepted connection).
  149. These thread modes are then combined with the evet loop styles
  150. (polling function type). MHD support select, poll and epoll. select
  151. is available on all platforms, epoll and poll may not be available on
  152. some platforms. Note that it is possible to combine MHD using epoll
  153. with an external select-based event loop.
  154. The default (if no other option is passed) is ``external select''.
  155. The highest performance can typically be obtained with a thread pool
  156. using @code{epoll}. Apache Benchmark (ab) was used to compare the
  157. performance of @code{select} and @code{epoll} when using a thread pool
  158. and a large number of connections. @ref{fig:performance} shows the
  159. resulting plot from the @code{benchmark.c} example, which measures the
  160. latency between an incoming request and the completion of the
  161. transmission of the response. In this setting, the @code{epoll}
  162. thread pool with four threads was able to handle more than 45,000
  163. connections per second on loopback (with Apache Benchmark running
  164. three processes on the same machine).
  165. @cindex performance
  166. @float Figure,fig:performance
  167. @image{libmicrohttpd_performance_data,400pt,300pt,Data,.png}
  168. @caption{Performance measurements for select vs. epoll (with thread-pool).}
  169. @end float
  170. Not all combinations of thread modes and event loop styles are
  171. supported. This is partially to keep the API simple, and partially
  172. because some combinations simply make no sense as others are strictly
  173. superior. Note that the choice of style depends first of all on the
  174. application logic, and then on the performance requirements.
  175. Applications that perform a blocking operation while handling a
  176. request within the callbacks from MHD must use a thread per
  177. connection. This is typically rather costly. Applications that do
  178. not support threads or that must run on embedded devices without
  179. thread-support must use the external mode. Using @code{epoll} is only
  180. supported on some platform, thus portable applications must at least
  181. have a fallback option available. @ref{tbl:supported} lists the sane
  182. combinations.
  183. @float Table,tbl:supported
  184. @multitable {@b{thread-per-connection}} {@b{select}} {@b{poll}} {@b{epoll}}
  185. @item @tab @b{select} @tab @b{poll} @tab @b{epoll}
  186. @item @b{external} @tab yes @tab no @tab yes
  187. @item @b{internal} @tab yes @tab yes @tab yes
  188. @item @b{thread pool} @tab yes @tab yes @tab yes
  189. @item @b{thread-per-connection} @tab yes @tab yes @tab no
  190. @end multitable
  191. @caption{Supported combinations of event styles and thread modes.}
  192. @end float
  193. @section Compiling GNU libmicrohttpd
  194. @cindex compilation
  195. @cindex embedded systems
  196. @cindex portability
  197. MHD uses the standard GNU system where the usual build process
  198. involves running
  199. @verbatim
  200. $ ./configure
  201. $ make
  202. $ make install
  203. @end verbatim
  204. MHD supports various options to be given to configure to tailor the
  205. binary to a specific situation. Note that some of these options will
  206. remove portions of the MHD code that are required for
  207. binary-compatibility. They should only be used on embedded systems
  208. with tight resource constraints and no concerns about library
  209. versioning. Standard distributions including MHD are expected to
  210. always ship with all features enabled, otherwise unexpected
  211. incompatibilities can arise!
  212. Here is a list of MHD-specific options that can be given to configure
  213. (canonical configure options such as ``--prefix'' are also supported, for a
  214. full list of options run ``./configure --help''):
  215. @table @code
  216. @item ``--disable-curl''
  217. disable running testcases using libcurl
  218. @item ``--disable-largefile''
  219. disable support for 64-bit files
  220. @item ``--disable-messages''
  221. disable logging of error messages (smaller binary size, not so much fun for debugging)
  222. @item ``--disable-https''
  223. disable HTTPS support, even if GNUtls is found; this option must be used if eCOS license is desired as an option (in all cases the resulting binary falls under a GNU LGPL-only license)
  224. @item ``--disable-postprocessor''
  225. do not include the post processor API (results in binary incompatibility)
  226. @item ``--disable-dauth''
  227. do not include the authentication APIs (results in binary incompatibility)
  228. @item ``--disable-httpupgrade''
  229. do not build code for HTTP ``Upgrade'' (smaller binary size, binary incompatible library)
  230. @item ``--disable-epoll''
  231. do not include epoll support, even if it supported (minimally smaller binary size, good for portability testing)
  232. @item ``--enable-coverage''
  233. set flags for analysis of code-coverage with gcc/gcov (results in slow, large binaries)
  234. @item ``--with-threads=posix,w32,none,auto''
  235. sets threading library to use. With use ``none'' to not support threads. In this case, MHD will only support the ``external'' threading modes and not perform any locking of data structures! Use @code{MHD_is_feature_supported(MHD_FEATURE_THREADS)} to test if threads are available. Default is ``auto''.
  236. @item ``--with-gcrypt=PATH''
  237. specifies path to libgcrypt installation
  238. @item ``--with-gnutls=PATH''
  239. specifies path to libgnutls installation
  240. @end table
  241. To cross-compile MHD for Android, install the Android NDK and use:
  242. @verbatim
  243. ./configure --target=arm-linux-androideabi --host=arm-linux-androideabi --disable-doc --disable-examples
  244. make
  245. @end verbatim
  246. Similar build commands should work for cross-compilation to other platforms.
  247. Note that you may have to first cross-compile GnuTLS to get MHD with TLS support.
  248. @section Validity of pointers
  249. MHD will give applications access to its internal data structures
  250. via pointers via arguments and return values from its API. This
  251. creates the question as to how long those pointers are assured to
  252. stay valid.
  253. Most MHD data structures are associated with the connection of an
  254. HTTP client. Thus, pointers associated with a connection are
  255. typically valid until the connection is finished, at which point
  256. MHD will call the @code{MHD_RequestCompletedCallback} if one is
  257. registered. Applications that have such a callback registered
  258. may assume that keys and values from the
  259. @code{MHD_KeyValueIterator}, return values from
  260. @code{MHD_lookup_connection_value} and the @code{url},
  261. @code{method} and @code{version} arguments to the
  262. @code{MHD_AccessHandlerCallback} will remain valid until the
  263. respective @code{MHD_RequestCompletedCallback} is invoked.
  264. In contrast, the @code{upload_data} argument of
  265. @code{MHD_RequestCompletedCallback} as well as all pointers
  266. from the @code{MHD_PostDataIterator} are only valid for the
  267. duration of the callback.
  268. Pointers returned from @code{MHD_get_response_header} are
  269. valid as long as the response itself is valid.
  270. @section Including the microhttpd.h header
  271. @cindex portability
  272. @cindex microhttpd.h
  273. Ideally, before including "microhttpd.h" you should add the necessary
  274. includes to define the @code{va_list}, @code{size_t}, @code{ssize_t},
  275. @code{intptr_t}, @code{off_t}, @code{uint8_t}, @code{uint16_t},
  276. @code{int32_t}, @code{uint32_t}, @code{int64_t}, @code{uint64_t},
  277. @code{fd_set}, @code{socklen_t} and @code{struct sockaddr} data types.
  278. Which specific headers are needed may depend on your platform and your
  279. build system might include some tests to provide you with the
  280. necessary conditional operations. For possible suggestions consult
  281. @code{platform.h} and @code{configure.ac} in the MHD distribution.
  282. Once you have ensured that you manually (!) included the right headers
  283. for your platform before "microhttpd.h", you should also add a line
  284. with @code{#define MHD_PLATFORM_H} which will prevent the
  285. "microhttpd.h" header from trying (and, depending on your platform,
  286. failing) to include the right headers.
  287. If you do not define MHD_PLATFORM_H, the "microhttpd.h" header will
  288. automatically include headers needed on GNU/Linux systems (possibly
  289. causing problems when porting to other platforms).
  290. @section SIGPIPE
  291. @cindex signals
  292. MHD does not install a signal handler for SIGPIPE. On platforms where
  293. this is possible (such as GNU/Linux), it disables SIGPIPE for its I/O
  294. operations (by passing MSG_NOSIGNAL or similar). On other platforms,
  295. SIGPIPE signals may be generated from network operations by MHD and
  296. will cause the process to die unless the developer explicitly installs
  297. a signal handler for SIGPIPE.
  298. Hence portable code using MHD must install a SIGPIPE handler or
  299. explicitly block the SIGPIPE signal. MHD does not do so in order to
  300. avoid messing with other parts of the application that may need to
  301. handle SIGPIPE in a particular way. You can make your application
  302. handle SIGPIPE by calling the following function in @code{main}:
  303. @verbatim
  304. static void
  305. catcher (int sig)
  306. {
  307. }
  308. static void
  309. ignore_sigpipe ()
  310. {
  311. struct sigaction oldsig;
  312. struct sigaction sig;
  313. sig.sa_handler = &catcher;
  314. sigemptyset (&sig.sa_mask);
  315. #ifdef SA_INTERRUPT
  316. sig.sa_flags = SA_INTERRUPT; /* SunOS */
  317. #else
  318. sig.sa_flags = SA_RESTART;
  319. #endif
  320. if (0 != sigaction (SIGPIPE, &sig, &oldsig))
  321. fprintf (stderr,
  322. "Failed to install SIGPIPE handler: %s\n", strerror (errno));
  323. }
  324. @end verbatim
  325. @section MHD_UNSIGNED_LONG_LONG
  326. @cindex long long
  327. @cindex MHD_LONG_LONG
  328. @cindex IAR
  329. @cindex ARM
  330. @cindex cortex m3
  331. @cindex embedded systems
  332. Some platforms do not support @code{long long}. Hence MHD defines a
  333. macro @code{MHD_UNSIGNED LONG_LONG} which will default to
  334. @code{unsigned long long}. For standard desktop operating systems,
  335. this is all you need to know.
  336. However, if your platform does not support @code{unsigned long long},
  337. you should change "platform.h" to define @code{MHD_LONG_LONG} and
  338. @code{MHD_UNSIGNED_LONG_LONG} to an appropriate alternative type and
  339. also define @code{MHD_LONG_LONG_PRINTF} and
  340. @code{MHD_UNSIGNED_LONG_LONG_PRINTF} to the corresponding format
  341. string for printing such a data type. Note that the ``signed''
  342. versions are deprecated. Also, for historical reasons,
  343. @code{MHD_LONG_LONG_PRINTF} is without the percent sign, whereas
  344. @code{MHD_UNSIGNED_LONG_LONG_PRINTF} is with the percent sign. Newly
  345. written code should only use the unsigned versions. However, you need
  346. to define both in "platform.h" if you need to change the definition
  347. for the specific platform.
  348. @section Portability to W32
  349. libmicrohttpd in general ported well to W32. Most libmicrohttpd features
  350. are supported. W32 do not support some functions, like epoll and
  351. corresponding MHD features are not available on W32.
  352. @section Portability to z/OS
  353. To compile MHD on z/OS, extract the archive and run
  354. @verbatim
  355. iconv -f UTF-8 -t IBM-1047 contrib/ascebc > /tmp/ascebc.sh
  356. chmod +x /tmp/ascebc.sh
  357. for n in `find * -type f`
  358. do
  359. /tmp/ascebc.sh $n
  360. done
  361. @end verbatim
  362. to convert all source files to EBCDIC. Note that you must run
  363. @code{configure} from the directory where the configure script is
  364. located. Otherwise, configure will fail to find the
  365. @code{contrib/xcc} script (which is a wrapper around the z/OS c89
  366. compiler).
  367. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  368. @c ------------------------------------------------------------
  369. @node microhttpd-const
  370. @chapter Constants
  371. @deftp {Enumeration} MHD_FLAG
  372. Options for the MHD daemon.
  373. Note that MHD will run automatically in background thread(s) only if
  374. @code{MHD_USE_INTERNAL_POLLING_THREAD} is used. Otherwise caller
  375. (application) must use @code{MHD_run} or @code{MHD_run_from_select} to
  376. have MHD processed network connections and data.
  377. Starting the daemon may also fail if a particular option is not
  378. implemented or not supported on the target platform (i.e. no support
  379. for @acronym{TLS}, threads or IPv6). TLS support generally depends on
  380. options given during MHD compilation.
  381. @table @code
  382. @item MHD_NO_FLAG
  383. No options selected.
  384. @item MHD_USE_ERROR_LOG
  385. If this flag is used, the library should print error messages and
  386. warnings to stderr (or to custom error printer if it's specified by
  387. options). Note that for this run-time option to have any effect, MHD
  388. needs to be compiled with messages enabled. This is done by default
  389. except you ran configure with the @code{--disable-messages} flag set.
  390. @item MHD_USE_DEBUG
  391. @cindex debugging
  392. Currently the same as @code{MHD_USE_ERROR_LOG}.
  393. @item MHD_USE_TLS
  394. @cindex TLS
  395. @cindex SSL
  396. Run in HTTPS-mode. If you specify @code{MHD_USE_TLS} and MHD was
  397. compiled without SSL support, @code{MHD_start_daemon} will return
  398. NULL.
  399. @item MHD_USE_THREAD_PER_CONNECTION
  400. Run using one thread per connection.
  401. @item MHD_USE_INTERNAL_POLLING_THREAD
  402. Run using an internal thread doing @code{SELECT}.
  403. @item MHD_USE_IPv6
  404. @cindex IPv6
  405. Run using the IPv6 protocol (otherwise, MHD will just support IPv4).
  406. If you specify @code{MHD_USE_IPV6} and the local platform does not
  407. support it, @code{MHD_start_daemon} will return NULL.
  408. If you want MHD to support IPv4 and IPv6 using a single socket, pass
  409. MHD_USE_DUAL_STACK, otherwise, if you only pass this option, MHD will
  410. try to bind to IPv6-only (resulting in no IPv4 support).
  411. @item MHD_USE_DUAL_STACK
  412. @cindex IPv6
  413. Use a single socket for IPv4 and IPv6. Note that this will mean
  414. that IPv4 addresses are returned by MHD in the IPv6-mapped format
  415. (the 'struct sockaddr_in6' format will be used for IPv4 and IPv6).
  416. @item MHD_USE_PEDANTIC_CHECKS
  417. @cindex deprecated
  418. Deprecated (use @code{MHD_OPTION_STRICT_FOR_CLIENT}).
  419. Be pedantic about the protocol.
  420. Specifically, at the moment, this flag causes MHD to reject HTTP
  421. 1.1 connections without a @code{Host} header. This is required by the
  422. standard, but of course in violation of the ``be as liberal as possible
  423. in what you accept'' norm. It is recommended to turn this @strong{ON}
  424. if you are testing clients against MHD, and @strong{OFF} in
  425. production.
  426. @item MHD_USE_POLL
  427. @cindex FD_SETSIZE
  428. @cindex poll
  429. @cindex select
  430. Use @code{poll()} instead of @code{select()}. This allows sockets with
  431. descriptors @code{>= FD_SETSIZE}. This option currently only works in
  432. conjunction with @code{MHD_USE_INTERNAL_POLLING_THREAD} (at this point).
  433. If you specify @code{MHD_USE_POLL} and the local platform does not
  434. support it, @code{MHD_start_daemon} will return NULL.
  435. @item MHD_USE_EPOLL
  436. @cindex FD_SETSIZE
  437. @cindex epoll
  438. @cindex select
  439. Use @code{epoll()} instead of @code{poll()} or @code{select()}. This
  440. allows sockets with descriptors @code{>= FD_SETSIZE}. This option is
  441. only available on some systems and does not work in conjunction with
  442. @code{MHD_USE_THREAD_PER_CONNECTION} (at this point). If you specify
  443. @code{MHD_USE_EPOLL} and the local platform does not support it,
  444. @code{MHD_start_daemon} will return NULL. Using @code{epoll()}
  445. instead of @code{select()} or @code{poll()} can in some situations
  446. result in significantly higher performance as the system call has
  447. fundamentally lower complexity (O(1) for @code{epoll()} vs. O(n) for
  448. @code{select()}/@code{poll()} where n is the number of open
  449. connections).
  450. @item MHD_USE_TURBO
  451. @cindex performance
  452. Enable optimizations to aggressively improve performance.
  453. Currently, the optimizations this option enables are based on
  454. opportunistic reads and writes. Basically, MHD will simply try to
  455. read or write or accept on a socket before checking that the socket is
  456. ready for IO using the event loop mechanism. As the sockets are
  457. non-blocking, this may fail (at a loss of performance), but generally
  458. MHD does this in situations where the operation is likely to succeed,
  459. in which case performance is improved. Setting the flag should generally
  460. be safe (even though the code is slightly more experimental). You may
  461. want to benchmark your application to see if this makes any difference
  462. for you.
  463. @item MHD_USE_SUPPRESS_DATE_NO_CLOCK
  464. @cindex date
  465. @cindex clock
  466. @cindex embedded systems
  467. Suppress (automatically) adding the 'Date:' header to HTTP responses.
  468. This option should ONLY be used on systems that do not have a clock
  469. and that DO provide other mechanisms for cache control. See also
  470. RFC 2616, section 14.18 (exception 3).
  471. @item MHD_USE_NO_LISTEN_SOCKET
  472. @cindex listen
  473. @cindex proxy
  474. @cindex embedded systems
  475. Run the HTTP server without any listen socket. This option only makes
  476. sense if @code{MHD_add_connection} is going to be used exclusively to
  477. connect HTTP clients to the HTTP server. This option is incompatible
  478. with using a thread pool; if it is used,
  479. @code{MHD_OPTION_THREAD_POOL_SIZE} is ignored.
  480. @item MHD_USE_ITC
  481. @cindex quiesce
  482. Force MHD to use a signal inter-thread communication channel to notify
  483. the event loop (of threads) of our shutdown and other events. This is
  484. required if an application uses @code{MHD_USE_INTERNAL_POLLING_THREAD}
  485. and then performs @code{MHD_quiesce_daemon} (which eliminates our
  486. ability to signal termination via the listen socket). In these modes,
  487. @code{MHD_quiesce_daemon} will fail if this option was not set. Also,
  488. use of this option is automatic (as in, you do not even have to
  489. specify it), if @code{MHD_USE_NO_LISTEN_SOCKET} is specified. In
  490. "external" select mode, this option is always simply ignored.
  491. Using this option also guarantees that MHD will not call
  492. @code{shutdown()} on the listen socket, which means a parent
  493. process can continue to use the socket.
  494. @item MHD_ALLOW_SUSPEND_RESUME
  495. Enables using @code{MHD_suspend_connection} and
  496. @code{MHD_resume_connection}, as performing these calls requires some
  497. additional inter-thred communication channels to be created, and code
  498. not using these calls should not pay the cost.
  499. @item MHD_USE_TCP_FASTOPEN
  500. @cindex listen
  501. Enable TCP_FASTOPEN on the listen socket. TCP_FASTOPEN is currently
  502. supported on Linux >= 3.6. On other systems using this option with
  503. cause @code{MHD_start_daemon} to fail.
  504. @item MHD_ALLOW_UPGRADE
  505. @cindex upgrade
  506. This option must be set if you want to upgrade connections
  507. (via ``101 Switching Protocols'' responses). This requires MHD to
  508. allocate additional resources, and hence we require this
  509. special flag so we only use the resources that are really needed.
  510. @item MHD_USE_AUTO
  511. Automatically select best event loop style (polling function)
  512. depending on requested mode by other MHD flags and functions available
  513. on platform. If application doesn't have requirements for any
  514. specific polling function, it's recommended to use this flag. This
  515. flag is very convenient for multiplatform applications.
  516. @item MHD_USE_POST_HANDSHAKE_AUTH_SUPPORT
  517. Tell the TLS library to support post handshake client authentication.
  518. Only useful in combination with @code{MHD_USE_TLS}.
  519. This option will only work if the underlying TLS library
  520. supports it (i.e. GnuTLS after 3.6.3). If the TLS library
  521. does not support it, MHD may ignore the option and proceed
  522. without supporting this features.
  523. @item MHD_USE_INSECURE_TLS_EARLY_DATA
  524. Tell the TLS library to support TLS v1.3 early data (0-RTT) with the
  525. resulting security drawbacks. Only enable this if you really know what
  526. you are doing. MHD currently does NOT enforce that this only affects
  527. GET requests! You have been warned.
  528. This option will only work if the underlying TLS library
  529. supports it (i.e. GnuTLS after 3.6.3). If the TLS library
  530. does not support it, MHD may ignore the option and proceed
  531. without supporting this features.
  532. @end table
  533. @end deftp
  534. @deftp {Enumeration} MHD_OPTION
  535. MHD options. Passed in the varargs portion of
  536. @code{MHD_start_daemon()}.
  537. @table @code
  538. @item MHD_OPTION_END
  539. No more options / last option. This is used to terminate the VARARGs
  540. list.
  541. @item MHD_OPTION_CONNECTION_MEMORY_LIMIT
  542. @cindex memory, limiting memory utilization
  543. Maximum memory size per connection (followed by a @code{size_t}). The
  544. default is 32 kB (32*1024 bytes) as defined by the internal constant
  545. @code{MHD_POOL_SIZE_DEFAULT}. Values above 128k are unlikely to
  546. result in much benefit, as half of the memory will be typically used
  547. for IO, and TCP buffers are unlikely to support window sizes above 64k
  548. on most systems.
  549. @item MHD_OPTION_CONNECTION_MEMORY_INCREMENT
  550. @cindex memory
  551. Increment to use for growing the read buffer (followed by a
  552. @code{size_t}). The default is 1024 (bytes). Increasing this value
  553. will make MHD use memory for reading more aggressively, which can
  554. reduce the number of @code{recvfrom} calls but may increase the number
  555. of @code{sendto} calls. The given value must fit within
  556. MHD_OPTION_CONNECTION_MEMORY_LIMIT.
  557. @item MHD_OPTION_CONNECTION_LIMIT
  558. @cindex connection, limiting number of connections
  559. Maximum number of concurrent connections to accept (followed by an
  560. @code{unsigned int}). The default is @code{FD_SETSIZE - 4} (the
  561. maximum number of file descriptors supported by @code{select} minus
  562. four for @code{stdin}, @code{stdout}, @code{stderr} and the server
  563. socket). In other words, the default is as large as possible.
  564. If the connection limit is reached, MHD's behavior depends a bit on
  565. other options. If @code{MHD_USE_ITC} was given, MHD
  566. will stop accepting connections on the listen socket. This will cause
  567. the operating system to queue connections (up to the @code{listen()}
  568. limit) above the connection limit. Those connections will be held
  569. until MHD is done processing at least one of the active connections.
  570. If @code{MHD_USE_ITC} is not set, then MHD will continue
  571. to @code{accept()} and immediately @code{close()} these connections.
  572. Note that if you set a low connection limit, you can easily get into
  573. trouble with browsers doing request pipelining. For example, if your
  574. connection limit is ``1'', a browser may open a first connection to
  575. access your ``index.html'' file, keep it open but use a second
  576. connection to retrieve CSS files, images and the like. In fact, modern
  577. browsers are typically by default configured for up to 15 parallel
  578. connections to a single server. If this happens, MHD will refuse to
  579. even accept the second connection until the first connection is
  580. closed --- which does not happen until timeout. As a result, the
  581. browser will fail to render the page and seem to hang. If you expect
  582. your server to operate close to the connection limit, you should
  583. first consider using a lower timeout value and also possibly add
  584. a ``Connection: close'' header to your response to ensure that
  585. request pipelining is not used and connections are closed immediately
  586. after the request has completed:
  587. @example
  588. MHD_add_response_header (response,
  589. MHD_HTTP_HEADER_CONNECTION,
  590. "close");
  591. @end example
  592. @item MHD_OPTION_CONNECTION_TIMEOUT
  593. @cindex timeout
  594. After how many seconds of inactivity should a connection automatically
  595. be timed out? (followed by an @code{unsigned int}; use zero for no
  596. timeout). The default is zero (no timeout).
  597. @item MHD_OPTION_NOTIFY_COMPLETED
  598. Register a function that should be called whenever a request has been
  599. completed (this can be used for application-specific clean up).
  600. Requests that have never been presented to the application (via
  601. @code{MHD_AccessHandlerCallback()}) will not result in
  602. notifications.
  603. This option should be followed by @strong{TWO} pointers. First a
  604. pointer to a function of type @code{MHD_RequestCompletedCallback()}
  605. and second a pointer to a closure to pass to the request completed
  606. callback. The second pointer maybe @code{NULL}.
  607. @item MHD_OPTION_NOTIFY_CONNECTION
  608. Register a function that should be called when the TCP connection to a
  609. client is opened or closed. The registered callback is called twice per
  610. TCP connection, with @code{MHD_CONNECTION_NOTIFY_STARTED} and
  611. @code{MHD_CONNECTION_NOTIFY_CLOSED} respectively. An additional
  612. argument can be used to store TCP connection specific information,
  613. which can be retrieved using @code{MHD_CONNECTION_INFO_SOCKET_CONTEXT}
  614. during the lifetime of the TCP connection.
  615. Note @code{MHD_OPTION_NOTIFY_COMPLETED} and the @code{req_cls} argument
  616. to the @code{MHD_AccessHandlerCallback} are per HTTP request (and there
  617. can be multiple HTTP requests per TCP connection).
  618. This option should be followed by @strong{TWO} pointers. First a
  619. pointer to a function of type @code{MHD_NotifyConnectionCallback()}
  620. and second a pointer to a closure to pass to the request completed
  621. callback. The second pointer maybe @code{NULL}.
  622. @item MHD_OPTION_PER_IP_CONNECTION_LIMIT
  623. Limit on the number of (concurrent) connections made to the
  624. server from the same IP address. Can be used to prevent one
  625. IP from taking over all of the allowed connections. If the
  626. same IP tries to establish more than the specified number of
  627. connections, they will be immediately rejected. The option
  628. should be followed by an @code{unsigned int}. The default is
  629. zero, which means no limit on the number of connections
  630. from the same IP address.
  631. @item MHD_OPTION_LISTEN_BACKLOG_SIZE
  632. Set the size of the @code{listen()} back log queue of the TCP socket.
  633. Takes an @code{unsigned int} as the argument. Default is the
  634. platform-specific value of @code{SOMAXCONN}.
  635. @item MHD_OPTION_STRICT_FOR_CLIENT
  636. Specify how strict we should enforce the HTTP protocol.
  637. Takes an @code{int} as the argument. Default is zero.
  638. If set to 1, MHD will be strict about the protocol. Specifically, at
  639. the moment, this flag uses MHD to reject HTTP 1.1 connections without
  640. a "Host" header. This is required by the standard, but of course in
  641. violation of the "be as liberal as possible in what you accept" norm.
  642. It is recommended to set this to 1 if you are testing clients against
  643. MHD, and 0 in production.
  644. If set to -1 MHD will be permissive about the protocol, allowing
  645. slight deviations that are technically not allowed by the
  646. RFC. Specifically, at the moment, this flag causes MHD to allow spaces
  647. in header field names. This is disallowed by the standard.
  648. It is not recommended to set it to -1 on publicly available servers as
  649. it may potentially lower level of protection.
  650. @item MHD_OPTION_SERVER_INSANITY
  651. @cindex testing
  652. Allows the application to disable certain sanity precautions in MHD. With
  653. these, the client can break the HTTP protocol, so this should never be used in
  654. production. The options are, however, useful for testing HTTP clients against
  655. "broken" server implementations. This argument must be followed by an
  656. @code{unsigned int}, corresponding to an @code{enum MHD_DisableSanityCheck}.
  657. Right now, no sanity checks can be disabled.
  658. @item MHD_OPTION_SOCK_ADDR
  659. @cindex bind, restricting bind
  660. Bind daemon to the supplied socket address. This option should be followed by a
  661. @code{struct sockaddr *}. If @code{MHD_USE_IPv6} is specified,
  662. the @code{struct sockaddr*} should point to a @code{struct sockaddr_in6},
  663. otherwise to a @code{struct sockaddr_in}. If this option is not specified,
  664. the daemon will listen to incoming connections from anywhere. If you use this
  665. option, the 'port' argument from @code{MHD_start_daemon} is ignored and the port
  666. from the given @code{struct sockaddr *} will be used instead.
  667. @item MHD_OPTION_URI_LOG_CALLBACK
  668. @cindex debugging
  669. @cindex logging
  670. @cindex query string
  671. Specify a function that should be called before parsing the URI from
  672. the client. The specified callback function can be used for processing
  673. the URI (including the options) before it is parsed. The URI after
  674. parsing will no longer contain the options, which maybe inconvenient for
  675. logging. This option should be followed by two arguments, the first
  676. one must be of the form
  677. @example
  678. void * my_logger(void * cls, const char * uri, struct MHD_Connection *con)
  679. @end example
  680. where the return value will be passed as
  681. @code{*req_cls} in calls to the @code{MHD_AccessHandlerCallback}
  682. when this request is processed later; returning a
  683. value of @code{NULL} has no special significance; (however,
  684. note that if you return non-@code{NULL}, you can no longer
  685. rely on the first call to the access handler having
  686. @code{NULL == *req_cls} on entry)
  687. @code{cls} will be set to the second argument following
  688. MHD_OPTION_URI_LOG_CALLBACK. Finally, @code{uri} will
  689. be the 0-terminated URI of the request.
  690. Note that during the time of this call, most of the connection's state
  691. is not initialized (as we have not yet parsed he headers). However,
  692. information about the connecting client (IP, socket) is available.
  693. @item MHD_OPTION_HTTPS_MEM_KEY
  694. @cindex SSL
  695. @cindex TLS
  696. Memory pointer to the private key to be used by the
  697. HTTPS daemon. This option should be followed by an
  698. "const char*" argument.
  699. This should be used in conjunction with 'MHD_OPTION_HTTPS_MEM_CERT'.
  700. @item MHD_OPTION_HTTPS_KEY_PASSWORD
  701. @cindex SSL
  702. @cindex TLS
  703. Memory pointer to the password that decrypts the
  704. private key to be used by the HTTPS daemon.
  705. This option should be followed by an
  706. "const char*" argument.
  707. This should be used in conjunction with 'MHD_OPTION_HTTPS_MEM_KEY'.
  708. The password (or passphrase) is only used immediately during
  709. @code{MHD_start_daemon()}. Thus, the application may want to
  710. erase it from memory afterwards for additional security.
  711. @item MHD_OPTION_HTTPS_MEM_CERT
  712. @cindex SSL
  713. @cindex TLS
  714. Memory pointer to the certificate to be used by the
  715. HTTPS daemon. This option should be followed by an
  716. "const char*" argument.
  717. This should be used in conjunction with 'MHD_OPTION_HTTPS_MEM_KEY'.
  718. @item MHD_OPTION_HTTPS_MEM_TRUST
  719. @cindex SSL
  720. @cindex TLS
  721. Memory pointer to the CA certificate to be used by the
  722. HTTPS daemon to authenticate and trust clients certificates.
  723. This option should be followed by an "const char*" argument.
  724. The presence of this option activates the request of certificate
  725. to the client. The request to the client is marked optional, and
  726. it is the responsibility of the server to check the presence
  727. of the certificate if needed.
  728. Note that most browsers will only present a client certificate
  729. only if they have one matching the specified CA, not sending
  730. any certificate otherwise.
  731. @item MHD_OPTION_HTTPS_CRED_TYPE
  732. @cindex SSL
  733. @cindex TLS
  734. Daemon credentials type. Either certificate or anonymous,
  735. this option should be followed by one of the values listed in
  736. "enum gnutls_credentials_type_t".
  737. @item MHD_OPTION_HTTPS_PRIORITIES
  738. @cindex SSL
  739. @cindex TLS
  740. @cindex cipher
  741. SSL/TLS protocol version and ciphers.
  742. This option must be followed by an "const char *" argument
  743. specifying the SSL/TLS protocol versions and ciphers that
  744. are acceptable for the application. The string is passed
  745. unchanged to gnutls_priority_init. If this option is not
  746. specified, ``NORMAL'' is used.
  747. @item MHD_OPTION_HTTPS_CERT_CALLBACK
  748. @cindex SSL
  749. @cindex TLS
  750. @cindex SNI
  751. Use a callback to determine which X.509 certificate should be used for
  752. a given HTTPS connection. This option should be followed by a
  753. argument of type "gnutls_certificate_retrieve_function2 *". This
  754. option provides an alternative to MHD_OPTION_HTTPS_MEM_KEY and
  755. MHD_OPTION_HTTPS_MEM_CERT. You must use this version if multiple
  756. domains are to be hosted at the same IP address using TLS's Server
  757. Name Indication (SNI) extension. In this case, the callback is
  758. expected to select the correct certificate based on the SNI
  759. information provided. The callback is expected to access the SNI data
  760. using gnutls_server_name_get(). Using this option requires GnuTLS 3.0
  761. or higher.
  762. @item MHD_OPTION_HTTPS_CERT_CALLBACK2
  763. @cindex SSL
  764. @cindex TLS
  765. @cindex SNI
  766. @cindex OCSP
  767. Use a callback to determine which X.509 certificate should be
  768. used for a given HTTPS connection. This option should be
  769. followed by a argument of type `gnutls_certificate_retrieve_function3 *`.
  770. This option provides an
  771. alternative/extension to #MHD_OPTION_HTTPS_CERT_CALLBACK.
  772. You must use this version if you want to use OCSP stapling.
  773. Using this option requires GnuTLS 3.6.3 or higher.
  774. @item MHD_OPTION_GNUTLS_PSK_CRED_HANDLER
  775. @cindex SSL
  776. @cindex TLS
  777. @cindex PSK
  778. Use pre-shared key for TLS credentials.
  779. Pass a pointer to callback of type
  780. @code{MHD_PskServerCredentialsCallback} and a closure.
  781. The function will be called to
  782. retrieve the shared key for a given username.
  783. @item MHD_OPTION_DIGEST_AUTH_RANDOM
  784. @cindex digest auth
  785. @cindex random
  786. Digest Authentication nonce's seed.
  787. This option should be followed by two arguments. First an integer of
  788. type "size_t" which specifies the size of the buffer pointed to by the
  789. second argument in bytes. Note that the application must ensure that
  790. the buffer of the second argument remains allocated and unmodified
  791. while the daemon is running. For security, you SHOULD provide a fresh
  792. random nonce when using MHD with Digest Authentication.
  793. @item MHD_OPTION_NONCE_NC_SIZE
  794. @cindex digest auth
  795. @cindex replay attack
  796. Size of an array of nonce and nonce counter map. This option must be
  797. followed by an "unsigned int" argument that have the size (number of
  798. elements) of a map of a nonce and a nonce-counter. If this option
  799. is not specified, a default value of 4 will be used (which might be
  800. too small for servers handling many requests). If you do not use
  801. digest authentication at all, you can specify a value of zero to
  802. save some memory.
  803. You should calculate the value of NC_SIZE based on the number of
  804. connections per second multiplied by your expected session duration
  805. plus a factor of about two for hash table collisions. For example, if
  806. you expect 100 digest-authenticated connections per second and the
  807. average user to stay on your site for 5 minutes, then you likely need
  808. a value of about 60000. On the other hand, if you can only expect
  809. only 10 digest-authenticated connections per second, tolerate browsers
  810. getting a fresh nonce for each request and expect a HTTP request
  811. latency of 250 ms, then a value of about 5 should be fine.
  812. @item MHD_OPTION_LISTEN_SOCKET
  813. @cindex systemd
  814. Listen socket to use. Pass a listen socket for MHD to use
  815. (systemd-style). If this option is used, MHD will not open its own
  816. listen socket(s). The argument passed must be of type "int" and refer
  817. to an existing socket that has been bound to a port and is listening.
  818. @item MHD_OPTION_EXTERNAL_LOGGER
  819. @cindex logging
  820. Use the given function for logging error messages.
  821. This option must be followed by two arguments; the
  822. first must be a pointer to a function
  823. of type 'void fun(void * arg, const char * fmt, va_list ap)'
  824. and the second a pointer of type 'void*' which will
  825. be passed as the "arg" argument to "fun".
  826. Note that MHD will not generate any log messages without
  827. the MHD_USE_ERROR_LOG flag set and if MHD was compiled
  828. with the "--disable-messages" flag.
  829. @item MHD_OPTION_THREAD_POOL_SIZE
  830. @cindex performance
  831. Number (unsigned int) of threads in thread pool. Enable
  832. thread pooling by setting this value to to something
  833. greater than 1. Currently, thread mode must be
  834. MHD_USE_INTERNAL_POLLING_THREAD if thread pooling is enabled
  835. (@code{MHD_start_daemon} returns @code{NULL} for an unsupported thread
  836. mode).
  837. @item MHD_OPTION_ARRAY
  838. @cindex options
  839. @cindex foreign-function interface
  840. This option can be used for initializing MHD using options from an
  841. array. A common use for this is writing an FFI for MHD. The actual
  842. options given are in an array of 'struct MHD_OptionItem', so this
  843. option requires a single argument of type 'struct MHD_OptionItem'.
  844. The array must be terminated with an entry @code{MHD_OPTION_END}.
  845. An example for code using MHD_OPTION_ARRAY is:
  846. @example
  847. struct MHD_OptionItem ops[] = @{
  848. @{ MHD_OPTION_CONNECTION_LIMIT, 100, NULL @},
  849. @{ MHD_OPTION_CONNECTION_TIMEOUT, 10, NULL @},
  850. @{ MHD_OPTION_END, 0, NULL @}
  851. @};
  852. d = MHD_start_daemon(0, 8080, NULL, NULL, dh, NULL,
  853. MHD_OPTION_ARRAY, ops,
  854. MHD_OPTION_END);
  855. @end example
  856. For options that expect a single pointer argument, the
  857. second member of the @code{struct MHD_OptionItem} is ignored.
  858. For options that expect two pointer arguments, the first
  859. argument must be cast to @code{intptr_t}.
  860. @item MHD_OPTION_UNESCAPE_CALLBACK
  861. @cindex internationalization
  862. @cindex escaping
  863. Specify a function that should be called for unescaping escape
  864. sequences in URIs and URI arguments. Note that this function will NOT
  865. be used by the MHD_PostProcessor. If this option is not specified,
  866. the default method will be used which decodes escape sequences of the
  867. form "%HH". This option should be followed by two arguments, the
  868. first one must be of the form
  869. @example
  870. size_t my_unescaper(void * cls, struct MHD_Connection *c, char *s)
  871. @end example
  872. where the return value must be @code{strlen(s)} and @code{s} should be
  873. updated. Note that the unescape function must not lengthen @code{s}
  874. (the result must be shorter than the input and still be 0-terminated).
  875. @code{cls} will be set to the second argument following
  876. MHD_OPTION_UNESCAPE_CALLBACK.
  877. @item MHD_OPTION_THREAD_STACK_SIZE
  878. @cindex stack
  879. @cindex thread
  880. @cindex pthread
  881. @cindex embedded systems
  882. Maximum stack size for threads created by MHD. This option must be
  883. followed by a @code{size_t}). Not specifying this option or using
  884. a value of zero means using the system default (which is likely to
  885. differ based on your platform).
  886. @item MHD_OPTION_TCP_FASTQUEUE_QUEUE_SIZE
  887. @cindex listen
  888. When the flag @code{MHD_USE_TCP_FASTOPEN} is used, this option sets the
  889. connection handshake queue size for the TCP FASTOPEN connections. Note
  890. that a TCP FASTOPEN connection handshake occupies more resources than a
  891. TCP handshake as the SYN packets also contain DATA which is kept in the
  892. associate state until handshake is completed. If this option is not
  893. given the queue size is set to a default value of 10. This option must
  894. be followed by a @code{unsigned int}.
  895. @item MHD_OPTION_HTTPS_MEM_DHPARAMS
  896. @cindex TLS
  897. @cindex SSL
  898. @cindex DH
  899. Memory pointer for the Diffie-Hellman parameters (dh.pem) to be used
  900. by the HTTPS daemon for key exchange. This option must be followed by
  901. a @code{const char *} argument. The argument would be a zero-terminated
  902. string with a PEM encoded PKCS3 DH parameters structure suitable
  903. for passing to @code{gnutls_dh_parms_import_pkcs3}.
  904. @item MHD_OPTION_LISTENING_ADDRESS_REUSE
  905. @cindex bind, restricting bind
  906. @cindex reusing listening address
  907. This option must be followed by a @code{unsigned int} argument.
  908. If this option is present and true (nonzero) parameter is given, allow reusing
  909. the address:port of the listening socket (using @code{SO_REUSEPORT} on most
  910. platforms, and @code{SO_REUSEADDR} on Windows). If a false (zero) parameter is
  911. given, disallow reusing the the address:port of the listening socket (this
  912. usually requires no special action, but @code{SO_EXCLUSIVEADDRUSE} is needed on
  913. Windows). If this option is not present @code{SO_REUSEADDR} is used on all
  914. platforms except Windows so reusing of address:port is disallowed.
  915. @end table
  916. @end deftp
  917. @deftp {C Struct} MHD_OptionItem
  918. Entry in an MHD_OPTION_ARRAY. See the @code{MHD_OPTION_ARRAY} option
  919. argument for its use.
  920. The @code{option} member is used to specify which option is specified
  921. in the array. The other members specify the respective argument.
  922. Note that for options taking only a single pointer, the
  923. @code{ptr_value} member should be set. For options taking two pointer
  924. arguments, the first pointer must be cast to @code{intptr_t} and both
  925. the @code{value} and the @code{ptr_value} members should be used to
  926. pass the two pointers.
  927. @end deftp
  928. @deftp {Enumeration} MHD_ValueKind
  929. The @code{MHD_ValueKind} specifies the source of the key-value pairs in
  930. the HTTP protocol.
  931. @table @code
  932. @item MHD_HEADER_KIND
  933. HTTP header.
  934. @item MHD_COOKIE_KIND
  935. @cindex cookie
  936. Cookies. Note that the original HTTP header containing the cookie(s)
  937. will still be available and intact.
  938. @item MHD_POSTDATA_KIND
  939. @cindex POST method
  940. @code{POST} data. This is available only if a content encoding
  941. supported by MHD is used (currently only @acronym{URL} encoding), and
  942. only if the posted content fits within the available memory pool. Note
  943. that in that case, the upload data given to the
  944. @code{MHD_AccessHandlerCallback()} will be empty (since it has
  945. already been processed).
  946. @item MHD_GET_ARGUMENT_KIND
  947. @code{GET} (URI) arguments.
  948. @item MHD_FOOTER_KIND
  949. HTTP footer (only for http 1.1 chunked encodings).
  950. @end table
  951. @end deftp
  952. @deftp {Enumeration} MHD_RequestTerminationCode
  953. The @code{MHD_RequestTerminationCode} specifies reasons why a request
  954. has been terminated (or completed).
  955. @table @code
  956. @item MHD_REQUEST_TERMINATED_COMPLETED_OK
  957. We finished sending the response.
  958. @item MHD_REQUEST_TERMINATED_WITH_ERROR
  959. Error handling the connection (resources exhausted, other side closed
  960. connection, application error accepting request, etc.)
  961. @item MHD_REQUEST_TERMINATED_TIMEOUT_REACHED
  962. No activity on the connection for the number of seconds specified using
  963. @code{MHD_OPTION_CONNECTION_TIMEOUT}.
  964. @item MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN
  965. We had to close the session since MHD was being shut down.
  966. @end table
  967. @end deftp
  968. @deftp {Enumeration} MHD_ResponseMemoryMode
  969. The @code{MHD_ResponeMemoryMode} specifies how MHD should treat
  970. the memory buffer given for the response in
  971. @code{MHD_create_response_from_buffer}.
  972. @table @code
  973. @item MHD_RESPMEM_PERSISTENT
  974. Buffer is a persistent (static/global) buffer that won't change
  975. for at least the lifetime of the response, MHD should just use
  976. it, not free it, not copy it, just keep an alias to it.
  977. @item MHD_RESPMEM_MUST_FREE
  978. Buffer is heap-allocated with @code{malloc} (or equivalent) and
  979. should be freed by MHD after processing the response has
  980. concluded (response reference counter reaches zero).
  981. @item MHD_RESPMEM_MUST_COPY
  982. Buffer is in transient memory, but not on the heap (for example,
  983. on the stack or non-malloc allocated) and only valid during the
  984. call to @code{MHD_create_response_from_buffer}. MHD must make its
  985. own private copy of the data for processing.
  986. @end table
  987. @end deftp
  988. @deftp {Enumeration} MHD_ResponseFlags
  989. Response-specific flags. Passed as an argument to
  990. @code{MHD_set_response_options()}.
  991. @table @code
  992. @item MHD_RF_NONE
  993. No special handling.
  994. @item MHD_RF_HTTP_VERSION_1_0_ONLY
  995. Only respond in conservative HTTP 1.0-mode. In particular,
  996. do not (automatically) sent "Connection" headers and always
  997. close the connection after generating the response.
  998. By default, MHD will respond using the same HTTP version which
  999. was set in the request. You can also set the
  1000. @code{MHD_RF_HTTP_VERSION_1_0_RESPONSE} flag to force version 1.0
  1001. in the response.
  1002. @item MHD_RF_HTTP_VERSION_1_0_RESPONSE
  1003. Only respond in HTTP 1.0-mode. Contrary to the
  1004. @code{MHD_RF_HTTP_VERSION_1_0_ONLY} flag, the response's HTTP version will
  1005. always be set to 1.0 and ``Connection'' headers are still supported.
  1006. You can even combine this option with MHD_RF_HTTP_VERSION_1_0_ONLY to
  1007. change the response's HTTP version while maintaining strict compliance
  1008. with HTTP 1.0 regarding connection management.
  1009. This solution is not perfect as this flag is set on the response which
  1010. is created after header processing. So MHD will behave as a HTTP 1.1
  1011. server until the response is queued. It means that an invalid HTTP 1.1
  1012. request will fail even if the response is sent with HTTP 1.0 and the
  1013. request would be valid if interpreted with this version. For example,
  1014. this request will fail in strict mode:
  1015. @verbatim
  1016. GET / HTTP/1.1
  1017. @end verbatim
  1018. as the ``Host'' header is missing and is mandatory in HTTP 1.1, but it
  1019. should succeed when interpreted with HTTP 1.0.
  1020. @item MHD_RF_INSANITY_HEADER_CONTENT_LENGTH
  1021. Disable sanity check preventing clients from manually
  1022. setting the HTTP content length option.
  1023. @end table
  1024. @end deftp
  1025. @deftp {Enumeration} MHD_ResponseOptions
  1026. Response-specific options. Passed in the varargs portion of
  1027. @code{MHD_set_response_options()}.
  1028. @table @code
  1029. @item MHD_RO_END
  1030. No more options / last option. This is used to terminate the VARARGs
  1031. list.
  1032. @end table
  1033. @end deftp
  1034. @deftp {Enumeration} MHD_WEBSOCKET_FLAG
  1035. @cindex websocket
  1036. Options for the MHD websocket stream.
  1037. This is used for initialization of a websocket stream when calling
  1038. @code{MHD_websocket_stream_init} or @code{MHD_websocket_stream_init2} and
  1039. alters the behavior of the websocket stream.
  1040. Note that websocket streams are only available if you include the header file
  1041. @code{microhttpd_ws.h} and compiled @emph{libmicrohttpd} with websockets.
  1042. @table @code
  1043. @item MHD_WEBSOCKET_FLAG_SERVER
  1044. The websocket stream is initialized in server mode (default).
  1045. Thus all outgoing payload will not be masked.
  1046. All incoming payload must be masked.
  1047. This flag cannot be used together with @code{MHD_WEBSOCKET_FLAG_CLIENT}.
  1048. @item MHD_WEBSOCKET_FLAG_CLIENT
  1049. The websocket stream is initialized in client mode.
  1050. You will usually never use that mode in combination with @emph{libmicrohttpd},
  1051. because @emph{libmicrohttpd} provides a server and not a client.
  1052. In client mode all outgoing payload will be masked
  1053. (XOR-ed with random values).
  1054. All incoming payload must be unmasked.
  1055. If you use this mode, you must always call @code{MHD_websocket_stream_init2}
  1056. instead of @code{MHD_websocket_stream_init}, because you need
  1057. to pass a random number generator callback function for masking.
  1058. This flag cannot be used together with @code{MHD_WEBSOCKET_FLAG_SERVER}.
  1059. @item MHD_WEBSOCKET_FLAG_NO_FRAGMENTS
  1060. You don't want to get fragmented data while decoding (default).
  1061. Fragmented frames will be internally put together until
  1062. they are complete.
  1063. Whether or not data is fragmented is decided
  1064. by the sender of the data during encoding.
  1065. This cannot be used together with @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS}.
  1066. @item MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS
  1067. You want fragmented data, if it appears while decoding.
  1068. You will receive the content of the fragmented frame,
  1069. but if you are decoding text, you will never get an unfinished
  1070. UTF-8 sequence (if the sequence appears between two fragments).
  1071. Instead the text will end before the unfinished UTF-8 sequence.
  1072. With the next fragment, which finishes the UTF-8 sequence,
  1073. you will get the complete UTF-8 sequence.
  1074. This cannot be used together with @code{MHD_WEBSOCKET_FLAG_NO_FRAGMENTS}.
  1075. @item MHD_WEBSOCKET_FLAG_GENERATE_CLOSE_FRAMES_ON_ERROR
  1076. If the websocket stream becomes invalid during decoding due to
  1077. protocol errors, a matching close frame will automatically
  1078. be generated.
  1079. The close frame will be returned via the parameters
  1080. @code{payload} and @code{payload_len} of @code{MHD_websocket_decode} and
  1081. the return value is negative (a value of @code{enum MHD_WEBSOCKET_STATUS}).
  1082. The generated close frame must be freed by the caller
  1083. with @code{MHD_websocket_free}.
  1084. @end table
  1085. @end deftp
  1086. @deftp {Enumeration} MHD_WEBSOCKET_FRAGMENTATION
  1087. @cindex websocket
  1088. This enumeration is used to specify the fragmentation behavior
  1089. when encoding of data (text/binary) for a websocket stream.
  1090. This is used with @code{MHD_websocket_encode_text} or
  1091. @code{MHD_websocket_encode_binary}.
  1092. Note that websocket streams are only available if you include the header file
  1093. @code{microhttpd_ws.h} and compiled @emph{libmicrohttpd} with websockets.
  1094. @table @code
  1095. @item MHD_WEBSOCKET_FRAGMENTATION_NONE
  1096. You don't want to use fragmentation.
  1097. The encoded frame consists of only one frame.
  1098. @item MHD_WEBSOCKET_FRAGMENTATION_FIRST
  1099. You want to use fragmentation.
  1100. The encoded frame is the first frame of
  1101. a series of data frames of the same type
  1102. (text or binary).
  1103. You may send control frames (ping, pong or close)
  1104. between these data frames.
  1105. @item MHD_WEBSOCKET_FRAGMENTATION_FOLLOWING
  1106. You want to use fragmentation.
  1107. The encoded frame is not the first frame of
  1108. the series of data frames, but also not the last one.
  1109. You may send control frames (ping, pong or close)
  1110. between these data frames.
  1111. @item MHD_WEBSOCKET_FRAGMENTATION_LAST
  1112. You want to use fragmentation.
  1113. The encoded frame is the last frame of
  1114. the series of data frames, but also not the first one.
  1115. After this frame, you may send all types of frames again.
  1116. @end table
  1117. @end deftp
  1118. @deftp {Enumeration} MHD_WEBSOCKET_STATUS
  1119. @cindex websocket
  1120. This enumeration is used for the return value of almost
  1121. every websocket stream function.
  1122. Errors are negative and values equal to or above zero mean a success.
  1123. Positive values are only used by @code{MHD_websocket_decode}.
  1124. Note that websocket streams are only available if you include the header file
  1125. @code{microhttpd_ws.h} and compiled @emph{libmicrohttpd} with websockets.
  1126. @table @code
  1127. @item MHD_WEBSOCKET_STATUS_OK
  1128. The call succeeded.
  1129. Especially for @code{MHD_websocket_decode} this means that no error occurred,
  1130. but also no frame has been completed yet.
  1131. For other functions this means simply a success.
  1132. @item MHD_WEBSOCKET_STATUS_TEXT_FRAME
  1133. @code{MHD_websocket_decode} has decoded a text frame.
  1134. The parameters @code{payload} and @code{payload_len} are filled with
  1135. the decoded text (if any).
  1136. You must free the returned @code{payload} after use with
  1137. @code{MHD_websocket_free}.
  1138. @item MHD_WEBSOCKET_STATUS_BINARY_FRAME
  1139. @code{MHD_websocket_decode} has decoded a binary frame.
  1140. The parameters @code{payload} and @code{payload_len} are filled with
  1141. the decoded binary data (if any).
  1142. You must free the returned @code{payload} after use with
  1143. @code{MHD_websocket_free}.
  1144. @item MHD_WEBSOCKET_STATUS_CLOSE_FRAME
  1145. @code{MHD_websocket_decode} has decoded a close frame.
  1146. This means you must close the socket using @code{MHD_upgrade_action}
  1147. with @code{MHD_UPGRADE_ACTION_CLOSE}.
  1148. You may respond with a close frame before closing.
  1149. The parameters @code{payload} and @code{payload_len} are filled with
  1150. the close reason (if any).
  1151. The close reason starts with a two byte sequence of close code
  1152. in network byte order (see @code{enum MHD_WEBSOCKET_CLOSEREASON}).
  1153. After these two bytes a UTF-8 encoded close reason may follow.
  1154. You can call @code{MHD_websocket_split_close_reason} to split that
  1155. close reason.
  1156. You must free the returned @code{payload} after use with
  1157. @code{MHD_websocket_free}.
  1158. @item MHD_WEBSOCKET_STATUS_PING_FRAME
  1159. @code{MHD_websocket_decode} has decoded a ping frame.
  1160. You should respond to this with a pong frame.
  1161. The pong frame must contain the same binary data as
  1162. the corresponding ping frame (if it had any).
  1163. The parameters @code{payload} and @code{payload_len} are filled with
  1164. the binary ping data (if any).
  1165. You must free the returned @code{payload} after use with
  1166. @code{MHD_websocket_free}.
  1167. @item MHD_WEBSOCKET_STATUS_PONG_FRAME
  1168. @code{MHD_websocket_decode} has decoded a pong frame.
  1169. You should usually only receive pong frames if you sent
  1170. a ping frame before.
  1171. The binary data should be equal to your ping frame and can be
  1172. used to distinguish the response if you sent multiple ping frames.
  1173. The parameters @code{payload} and @code{payload_len} are filled with
  1174. the binary pong data (if any).
  1175. You must free the returned @code{payload} after use with
  1176. @code{MHD_websocket_free}.
  1177. @item MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT
  1178. @code{MHD_websocket_decode} has decoded a text frame fragment.
  1179. The parameters @code{payload} and @code{payload_len} are filled with
  1180. the decoded text (if any).
  1181. This is like @code{MHD_WEBSOCKET_STATUS_TEXT_FRAME}, but it can only
  1182. appear if you specified @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS} during
  1183. the call of @code{MHD_websocket_stream_init} or
  1184. @code{MHD_websocket_stream_init2}.
  1185. You must free the returned @code{payload} after use with
  1186. @code{MHD_websocket_free}.
  1187. @item MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT
  1188. @code{MHD_websocket_decode} has decoded a binary frame fragment.
  1189. The parameters @code{payload} and @code{payload_len} are filled with
  1190. the decoded binary data (if any).
  1191. This is like @code{MHD_WEBSOCKET_STATUS_BINARY_FRAME}, but it can only
  1192. appear if you specified @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS} during
  1193. the call of @code{MHD_websocket_stream_init} or
  1194. @code{MHD_websocket_stream_init2}.
  1195. You must free the returned @code{payload} after use with
  1196. @code{MHD_websocket_free}.
  1197. @item MHD_WEBSOCKET_STATUS_TEXT_NEXT_FRAGMENT
  1198. @code{MHD_websocket_decode} has decoded the next text frame fragment.
  1199. The parameters @code{payload} and @code{payload_len} are filled with
  1200. the decoded text (if any).
  1201. This is like @code{MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT}, but it appears
  1202. only after the first and before the last fragment of a series of fragments.
  1203. It can only appear if you specified @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS}
  1204. during the call of @code{MHD_websocket_stream_init} or
  1205. @code{MHD_websocket_stream_init2}.
  1206. You must free the returned @code{payload} after use with
  1207. @code{MHD_websocket_free}.
  1208. @item MHD_WEBSOCKET_STATUS_BINARY_NEXT_FRAGMENT
  1209. @code{MHD_websocket_decode} has decoded the next binary frame fragment.
  1210. The parameters @code{payload} and @code{payload_len} are filled with
  1211. the decoded binary data (if any).
  1212. This is like @code{MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT}, but it appears
  1213. only after the first and before the last fragment of a series of fragments.
  1214. It can only appear if you specified @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS}
  1215. during the call of @code{MHD_websocket_stream_init} or
  1216. @code{MHD_websocket_stream_init2}.
  1217. You must free the returned @code{payload} after use with
  1218. @code{MHD_websocket_free}.
  1219. @item MHD_WEBSOCKET_STATUS_TEXT_LAST_FRAGMENT
  1220. @code{MHD_websocket_decode} has decoded the last text frame fragment.
  1221. The parameters @code{payload} and @code{payload_len} are filled with
  1222. the decoded text (if any).
  1223. This is like @code{MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT}, but it appears
  1224. only for the last fragment of a series of fragments.
  1225. It can only appear if you specified @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS}
  1226. during the call of @code{MHD_websocket_stream_init} or
  1227. @code{MHD_websocket_stream_init2}.
  1228. You must free the returned @code{payload} after use with
  1229. @code{MHD_websocket_free}.
  1230. @item MHD_WEBSOCKET_STATUS_BINARY_LAST_FRAGMENT
  1231. @code{MHD_websocket_decode} has decoded the last binary frame fragment.
  1232. The parameters @code{payload} and @code{payload_len} are filled with
  1233. the decoded binary data (if any).
  1234. This is like @code{MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT}, but it appears
  1235. only for the last fragment of a series of fragments.
  1236. It can only appear if you specified @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS}
  1237. during the call of @code{MHD_websocket_stream_init} or
  1238. @code{MHD_websocket_stream_init2}.
  1239. You must free the returned @code{payload} after use with
  1240. @code{MHD_websocket_free}.
  1241. @item MHD_WEBSOCKET_STATUS_PROTOCOL_ERROR
  1242. The call failed and the stream is invalid now for decoding.
  1243. You must close the websocket now using @code{MHD_upgrade_action}
  1244. with @code{MHD_UPGRADE_ACTION_CLOSE}.
  1245. You may send a close frame before closing.
  1246. This is only used by @code{MHD_websocket_decode} and happens
  1247. if the stream contains errors (i. e. invalid byte data).
  1248. @item MHD_WEBSOCKET_STATUS_STREAM_BROKEN
  1249. You tried to decode something, but the stream has already
  1250. been marked invalid.
  1251. You must close the websocket now using @code{MHD_upgrade_action}
  1252. with @code{MHD_UPGRADE_ACTION_CLOSE}.
  1253. You may send a close frame before closing.
  1254. This is only used by @code{MHD_websocket_decode} and happens
  1255. if you call @code{MDM_websocket_decode} again after
  1256. has been invalidated.
  1257. You can call @code{MHD_websocket_stream_is_valid} at any time
  1258. to check whether a stream is invalid or not.
  1259. @item MHD_WEBSOCKET_STATUS_MEMORY_ERROR
  1260. A memory allocation failed. The stream remains valid.
  1261. If this occurred while decoding, the decoding could be
  1262. possible later if enough memory is available.
  1263. This could happen while decoding if you received a too big data frame.
  1264. You could try to specify max_payload_size during the call of
  1265. @code{MHD_websocket_stream_init} or @code{MHD_websocket_stream_init2} to
  1266. avoid this and close the websocket instead.
  1267. @item MHD_WEBSOCKET_STATUS_PARAMETER_ERROR
  1268. You passed invalid parameters during the function call
  1269. (i. e. a NULL pointer for a required parameter).
  1270. The stream remains valid.
  1271. @item MHD_WEBSOCKET_STATUS_MAXIMUM_SIZE_EXCEEDED
  1272. The maximum payload size has been exceeded.
  1273. If you got this return code from @code{MHD_websocket_decode} then
  1274. the stream becomes invalid and the websocket must be closed
  1275. using @code{MHD_upgrade_action} with @code{MHD_UPGRADE_ACTION_CLOSE}.
  1276. You may send a close frame before closing.
  1277. The maximum payload size is specified during the call of
  1278. @code{MHD_websocket_stream_init} or @code{MHD_websocket_stream_init2}.
  1279. This can also appear if you specified 0 as maximum payload size
  1280. when the message is greater than the maximum allocatable memory size
  1281. (i. e. more than 4 GiB on 32 bit systems).
  1282. If you got this return code from @code{MHD_websocket_encode_close},
  1283. @code{MHD_websocket_encode_ping} or @code{MHD_websocket_encode_pong} then
  1284. you passed to much payload data. The stream remains valid then.
  1285. @item MHD_WEBSOCKET_STATUS_UTF8_ENCODING_ERROR
  1286. An UTF-8 sequence is invalid.
  1287. If you got this return code from @code{MHD_websocket_decode} then
  1288. the stream becomes invalid and you must close the websocket
  1289. using @code{MHD_upgrade_action} with @code{MHD_UPGRADE_ACTION_CLOSE}.
  1290. You may send a close frame before closing.
  1291. If you got this from @code{MHD_websocket_encode_text} or
  1292. @code{MHD_websocket_encode_close} then you passed invalid UTF-8 text.
  1293. The stream remains valid then.
  1294. @item MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER
  1295. A check routine for the HTTP headers came to the conclusion that
  1296. the header value isn't valid for a websocket handshake request.
  1297. This value can only be returned from the following functions:
  1298. @code{MHD_websocket_check_http_version},
  1299. @code{MHD_websocket_check_connection_header},
  1300. @code{MHD_websocket_check_upgrade_header},
  1301. @code{MHD_websocket_check_version_header},
  1302. @code{MHD_websocket_create_accept_header}
  1303. @end table
  1304. @end deftp
  1305. @deftp {Enumeration} MHD_WEBSOCKET_CLOSEREASON
  1306. @cindex websocket
  1307. Enumeration of possible close reasons for websocket close frames.
  1308. The possible values are specified in RFC 6455 7.4.1
  1309. These close reasons here are the default set specified by RFC 6455,
  1310. but also other close reasons could be used.
  1311. The definition is for short:
  1312. @itemize @bullet
  1313. @item 0-999 are never used (if you pass 0 in
  1314. @code{MHD_websocket_encode_close} then no close reason is used).
  1315. @item 1000-2999 are specified by RFC 6455.
  1316. @item 3000-3999 are specified by libraries, etc. but must be registered by IANA.
  1317. @item 4000-4999 are reserved for private use.
  1318. @end itemize
  1319. Note that websocket streams are only available if you include the header file
  1320. @code{microhttpd_ws.h} and compiled @emph{libmicrohttpd} with websockets.
  1321. @table @code
  1322. @item MHD_WEBSOCKET_CLOSEREASON_NO_REASON
  1323. This value is used as placeholder for @code{MHD_websocket_encode_close}
  1324. to tell that you don't want to specify any reason.
  1325. If you use this value then no reason text may be used.
  1326. This value cannot be a result of decoding, because this value
  1327. is not a valid close reason for the websocket protocol.
  1328. @item MHD_WEBSOCKET_CLOSEREASON_REGULAR
  1329. You close the websocket because it fulfilled its purpose and shall
  1330. now be closed in a normal, planned way.
  1331. @item MHD_WEBSOCKET_CLOSEREASON_GOING_AWAY
  1332. You close the websocket because you are shutting down the server or
  1333. something similar.
  1334. @item MHD_WEBSOCKET_CLOSEREASON_PROTOCOL_ERROR
  1335. You close the websocket because a protocol error occurred
  1336. during decoding (i. e. invalid byte data).
  1337. @item MHD_WEBSOCKET_CLOSEREASON_UNSUPPORTED_DATATYPE
  1338. You close the websocket because you received data which you don't accept.
  1339. For example if you received a binary frame,
  1340. but your application only expects text frames.
  1341. @item MHD_WEBSOCKET_CLOSEREASON_MALFORMED_UTF8
  1342. You close the websocket because it contains malformed UTF-8.
  1343. The UTF-8 validity is automatically checked by @code{MHD_websocket_decode},
  1344. so you don't need to check it on your own.
  1345. UTF-8 is specified in RFC 3629.
  1346. @item MHD_WEBSOCKET_CLOSEREASON_POLICY_VIOLATED
  1347. You close the websocket because you received a frame which is too big
  1348. to process.
  1349. You can specify the maximum allowed payload size during the call of
  1350. @code{MHD_websocket_stream_init} or @code{MHD_websocket_stream_init2}.
  1351. @item MHD_WEBSOCKET_CLOSEREASON_MISSING_EXTENSION
  1352. This status code can be sent by the client if it
  1353. expected a specific extension, but this extension hasn't been negotiated.
  1354. @item MHD_WEBSOCKET_CLOSEREASON_UNEXPECTED_CONDITION
  1355. The server closes the websocket because it encountered
  1356. an unexpected condition that prevented it from fulfilling the request.
  1357. @end table
  1358. @end deftp
  1359. @deftp {Enumeration} MHD_WEBSOCKET_UTF8STEP
  1360. @cindex websocket
  1361. Enumeration of possible UTF-8 check steps for websocket functions
  1362. These values are used during the encoding of fragmented text frames
  1363. or for error analysis while encoding text frames.
  1364. Its values specify the next step of the UTF-8 check.
  1365. UTF-8 sequences consist of one to four bytes.
  1366. This enumeration just says how long the current UTF-8 sequence is
  1367. and what is the next expected byte.
  1368. Note that websocket streams are only available if you include the header file
  1369. @code{microhttpd_ws.h} and compiled @emph{libmicrohttpd} with websockets.
  1370. @table @code
  1371. @item MHD_WEBSOCKET_UTF8STEP_NORMAL
  1372. There is no open UTF-8 sequence.
  1373. The next byte must be 0x00-0x7F or 0xC2-0xF4.
  1374. @item MHD_WEBSOCKET_UTF8STEP_UTF2TAIL_1OF1
  1375. The second byte of a two byte UTF-8 sequence.
  1376. The first byte was 0xC2-0xDF.
  1377. The next byte must be 0x80-0xBF.
  1378. @item MHD_WEBSOCKET_UTF8STEP_UTF3TAIL1_1OF2
  1379. The second byte of a three byte UTF-8 sequence.
  1380. The first byte was 0xE0.
  1381. The next byte must be 0xA0-0xBF.
  1382. @item MHD_WEBSOCKET_UTF8STEP_UTF3TAIL2_1OF2
  1383. The second byte of a three byte UTF-8 sequence.
  1384. The first byte was 0xED.
  1385. The next byte must by 0x80-0x9F.
  1386. @item MHD_WEBSOCKET_UTF8STEP_UTF3TAIL_1OF2
  1387. The second byte of a three byte UTF-8 sequence.
  1388. The first byte was 0xE1-0xEC or 0xEE-0xEF.
  1389. The next byte must be 0x80-0xBF.
  1390. @item MHD_WEBSOCKET_UTF8STEP_UTF3TAIL_2OF2
  1391. The third byte of a three byte UTF-8 sequence.
  1392. The next byte must be 0x80-0xBF.
  1393. @item MHD_WEBSOCKET_UTF8STEP_UTF4TAIL1_1OF3
  1394. The second byte of a four byte UTF-8 sequence.
  1395. The first byte was 0xF0.
  1396. The next byte must be 0x90-0xBF.
  1397. @item MHD_WEBSOCKET_UTF8STEP_UTF4TAIL2_1OF3
  1398. The second byte of a four byte UTF-8 sequence.
  1399. The first byte was 0xF4.
  1400. The next byte must be 0x80-0x8F.
  1401. @item MHD_WEBSOCKET_UTF8STEP_UTF4TAIL_1OF3
  1402. The second byte of a four byte UTF-8 sequence.
  1403. The first byte was 0xF1-0xF3.
  1404. The next byte must be 0x80-0xBF.
  1405. @item MHD_WEBSOCKET_UTF8STEP_UTF4TAIL_2OF3
  1406. The third byte of a four byte UTF-8 sequence.
  1407. The next byte must be 0x80-0xBF.
  1408. @item MHD_WEBSOCKET_UTF8STEP_UTF4TAIL_3OF3
  1409. The fourth byte of a four byte UTF-8 sequence.
  1410. The next byte must be 0x80-0xBF.
  1411. @end table
  1412. @end deftp
  1413. @deftp {Enumeration} MHD_WEBSOCKET_VALIDITY
  1414. @cindex websocket
  1415. Enumeration of validity values of a websocket stream
  1416. These values are used for @code{MHD_websocket_stream_is_valid}
  1417. and specify the validity status.
  1418. Note that websocket streams are only available if you include the header file
  1419. @code{microhttpd_ws.h} and compiled @emph{libmicrohttpd} with websockets.
  1420. @table @code
  1421. @item MHD_WEBSOCKET_VALIDITY_INVALID
  1422. The stream is invalid.
  1423. It cannot be used for decoding anymore.
  1424. @item MHD_WEBSOCKET_VALIDITY_VALID
  1425. The stream is valid.
  1426. Decoding works as expected.
  1427. @item MHD_WEBSOCKET_VALIDITY_ONLY_VALID_FOR_CONTROL_FRAMES
  1428. The stream has received a close frame and
  1429. is partly invalid.
  1430. You can still use the stream for decoding,
  1431. but if a data frame is received an error will be reported.
  1432. After a close frame has been sent, no data frames
  1433. may follow from the sender of the close frame.
  1434. @end table
  1435. @end deftp
  1436. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1437. @c ------------------------------------------------------------
  1438. @node microhttpd-struct
  1439. @chapter Structures type definition
  1440. @deftp {C Struct} MHD_Daemon
  1441. Handle for the daemon (listening on a socket for HTTP traffic).
  1442. @end deftp
  1443. @deftp {C Struct} MHD_Connection
  1444. Handle for a connection / HTTP request. With HTTP/1.1, multiple
  1445. requests can be run over the same connection. However, MHD will only
  1446. show one request per TCP connection to the client at any given time.
  1447. @end deftp
  1448. @deftp {C Struct} MHD_Response
  1449. Handle for a response.
  1450. @end deftp
  1451. @deftp {C Struct} MHD_IoVec
  1452. An element of an array of memory buffers.
  1453. @end deftp
  1454. @deftp {C Struct} MHD_PostProcessor
  1455. @cindex POST method
  1456. Handle for @code{POST} processing.
  1457. @end deftp
  1458. @deftp {C Union} MHD_ConnectionInfo
  1459. Information about a connection.
  1460. @end deftp
  1461. @deftp {C Union} MHD_DaemonInfo
  1462. Information about an MHD daemon.
  1463. @end deftp
  1464. @deftp {C Struct} MHD_WebSocketStream
  1465. @cindex websocket
  1466. Information about a MHD websocket stream.
  1467. @end deftp
  1468. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1469. @c ------------------------------------------------------------
  1470. @node microhttpd-cb
  1471. @chapter Callback functions definition
  1472. @deftypefn {Function Pointer} enum MHD_Result {*MHD_AcceptPolicyCallback} (void *cls, const struct sockaddr * addr, socklen_t addrlen)
  1473. Invoked in the context of a connection to allow or deny a client to
  1474. connect. This callback return @code{MHD_YES} if connection is allowed,
  1475. @code{MHD_NO} if not.
  1476. @table @var
  1477. @item cls
  1478. custom value selected at callback registration time;
  1479. @item addr
  1480. address information from the client;
  1481. @item addrlen
  1482. length of the address information.
  1483. @end table
  1484. @end deftypefn
  1485. @deftypefn {Function Pointer} enum MHD_Result {*MHD_AccessHandlerCallback} (void *cls, struct MHD_Connection * connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **req_cls)
  1486. Invoked in the context of a connection to answer a request from the
  1487. client. This callback must call MHD functions (example: the
  1488. @code{MHD_Response} ones) to provide content to give back to the client
  1489. and return an HTTP status code (i.e. @code{200} for OK, @code{404},
  1490. etc.).
  1491. @ref{microhttpd-post}, for details on how to code this callback.
  1492. Must return @code{MHD_YES} if the connection was handled successfully,
  1493. @code{MHD_NO} if the socket must be closed due to a serious error while
  1494. handling the request
  1495. @table @var
  1496. @item cls
  1497. custom value selected at callback registration time;
  1498. @item url
  1499. the URL requested by the client;
  1500. @item method
  1501. the HTTP method used by the client (@code{GET}, @code{PUT},
  1502. @code{DELETE}, @code{POST}, etc.);
  1503. @item version
  1504. the HTTP version string (i.e. @code{HTTP/1.1});
  1505. @item upload_data
  1506. the data being uploaded (excluding headers):
  1507. @cindex POST method
  1508. @cindex PUT method
  1509. @code{POST} data @strong{will} be made available
  1510. incrementally in @var{upload_data}; even if @code{POST}
  1511. data is available, the first time the callback is
  1512. invoked there won't be upload data, as this is done
  1513. just after MHD parses the headers. If supported by
  1514. the client and the HTTP version, the application can
  1515. at this point queue an error response to possibly
  1516. avoid the upload entirely. If no response is generated,
  1517. MHD will (if required) automatically send a 100 CONTINUE
  1518. reply to the client.
  1519. Afterwards, POST data will be passed to the callback
  1520. to be processed incrementally by the application. The
  1521. application may return @code{MHD_NO} to forcefully
  1522. terminate the TCP connection without generating a
  1523. proper HTTP response. Once all of the upload data has
  1524. been provided to the application, the application
  1525. will be called again with 0 bytes of upload data.
  1526. At this point, a response should be queued to complete
  1527. the handling of the request.
  1528. @item upload_data_size
  1529. set initially to the size of the @var{upload_data} provided; this
  1530. callback must update this value to the number of bytes @strong{NOT}
  1531. processed; unless external select is used, the callback maybe
  1532. required to process at least some data. If the callback fails to
  1533. process data in multi-threaded or internal-select mode and if the
  1534. read-buffer is already at the maximum size that MHD is willing to
  1535. use for reading (about half of the maximum amount of memory allowed
  1536. for the connection), then MHD will abort handling the connection
  1537. and return an internal server error to the client. In order to
  1538. avoid this, clients must be able to process upload data incrementally
  1539. and reduce the value of @code{upload_data_size}.
  1540. @item req_cls
  1541. reference to a pointer, initially set to @code{NULL}, that this callback can
  1542. set to some address and that will be preserved by MHD for future
  1543. calls for this request;
  1544. since the access handler may be called many times (i.e., for a
  1545. @code{PUT}/@code{POST} operation with plenty of upload data) this allows
  1546. the application to easily associate some request-specific state;
  1547. if necessary, this state can be cleaned up in the global
  1548. @code{MHD_RequestCompletedCallback} (which can be set with the
  1549. @code{MHD_OPTION_NOTIFY_COMPLETED}).
  1550. @end table
  1551. @end deftypefn
  1552. @deftypefn {Function Pointer} void {*MHD_RequestCompletedCallback} (void *cls, struct MHD_Connectionconnection, void **req_cls, enum MHD_RequestTerminationCode toe)
  1553. Signature of the callback used by MHD to notify the application about
  1554. completed requests.
  1555. @table @var
  1556. @item cls
  1557. custom value selected at callback registration time;
  1558. @item connection
  1559. connection handle;
  1560. @item req_cls
  1561. value as set by the last call to the
  1562. @code{MHD_AccessHandlerCallback};
  1563. @item toe
  1564. reason for request termination see @code{MHD_OPTION_NOTIFY_COMPLETED}.
  1565. @end table
  1566. @end deftypefn
  1567. @deftypefn {Function Pointer} enum MHD_Result {*MHD_KeyValueIterator} (void *cls, enum MHD_ValueKind kind, const char *key, const char *value, size_t value_size)
  1568. Iterator over key-value pairs. This iterator can be used to iterate
  1569. over all of the cookies, headers, or @code{POST}-data fields of a
  1570. request, and also to iterate over the headers that have been added to a
  1571. response.
  1572. @table @var
  1573. @item cls
  1574. custom value specified when iteration was triggered;
  1575. @item kind
  1576. kind of the header we are looking at
  1577. @item key
  1578. key for the value, can be an empty string
  1579. @item value
  1580. value corresponding value, can be NULL
  1581. @item value_size
  1582. number of bytes in @code{value}. This argument was introduced in
  1583. @code{MHD_VERSION} 0x00096301 to allow applications to use binary
  1584. zeros in values. Applications using this argument must ensure that
  1585. they are using a sufficiently recent version of MHD, i.e. by testing
  1586. @code{MHD_get_version()} for values above or equal to 0.9.64.
  1587. Applications that do not need zeros in values and that want to compile
  1588. without warnings against newer versions of MHD should not declare this
  1589. argument and cast the function pointer argument to
  1590. @code{MHD_KeyValueIterator}.
  1591. @end table
  1592. Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the
  1593. iteration.
  1594. @end deftypefn
  1595. @deftypefn {Function Pointer} ssize_t {*MHD_ContentReaderCallback} (void *cls, uint64_t pos, char *buf, size_t max)
  1596. Callback used by MHD in order to obtain content. The callback has to
  1597. copy at most @var{max} bytes of content into @var{buf}. The total
  1598. number of bytes that has been placed into @var{buf} should be returned.
  1599. Note that returning zero will cause MHD to try again.
  1600. Thus, returning zero should only be used in conjunction
  1601. with @code{MHD_suspend_connection()} to avoid busy waiting.
  1602. While usually the callback simply returns the number of bytes written
  1603. into @var{buf}, there are two special return value:
  1604. @code{MHD_CONTENT_READER_END_OF_STREAM} (-1) should be returned
  1605. for the regular end of transmission (with chunked encoding, MHD will then
  1606. terminate the chunk and send any HTTP footers that might be
  1607. present; without chunked encoding and given an unknown
  1608. response size, MHD will simply close the connection; note
  1609. that while returning @code{MHD_CONTENT_READER_END_OF_STREAM} is not technically
  1610. legal if a response size was specified, MHD accepts this
  1611. and treats it just as @code{MHD_CONTENT_READER_END_WITH_ERROR}.
  1612. @code{MHD_CONTENT_READER_END_WITH_ERROR} (-2) is used to indicate a server
  1613. error generating the response; this will cause MHD to simply
  1614. close the connection immediately. If a response size was
  1615. given or if chunked encoding is in use, this will indicate
  1616. an error to the client. Note, however, that if the client
  1617. does not know a response size and chunked encoding is not in
  1618. use, then clients will not be able to tell the difference between
  1619. @code{MHD_CONTENT_READER_END_WITH_ERROR} and
  1620. @code{MHD_CONTENT_READER_END_OF_STREAM}.
  1621. This is not a limitation of MHD but rather of the HTTP protocol.
  1622. @table @var
  1623. @item cls
  1624. custom value selected at callback registration time;
  1625. @item pos
  1626. position in the datastream to access; note that if an
  1627. @code{MHD_Response} object is re-used, it is possible for the same
  1628. content reader to be queried multiple times for the same data; however,
  1629. if an @code{MHD_Response} is not re-used, MHD guarantees that
  1630. @var{pos} will be the sum of all non-negative return values obtained
  1631. from the content reader so far.
  1632. @end table
  1633. Return @code{-1} on error (MHD will no longer try to read content and
  1634. instead close the connection with the client).
  1635. @end deftypefn
  1636. @deftypefn {Function Pointer} void {*MHD_ContentReaderFreeCallback} (void *cls)
  1637. This method is called by MHD if we are done with a content reader.
  1638. It should be used to free resources associated with the content reader.
  1639. @end deftypefn
  1640. @deftypefn {Function Pointer} enum MHD_Result {*MHD_PostDataIterator} (void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size)
  1641. Iterator over key-value pairs where the value maybe made available in
  1642. increments and/or may not be zero-terminated. Used for processing
  1643. @code{POST} data.
  1644. @table @var
  1645. @item cls
  1646. custom value selected at callback registration time;
  1647. @item kind
  1648. type of the value;
  1649. @item key
  1650. zero-terminated key for the value;
  1651. @item filename
  1652. name of the uploaded file, @code{NULL} if not known;
  1653. @item content_type
  1654. mime-type of the data, @code{NULL} if not known;
  1655. @item transfer_encoding
  1656. encoding of the data, @code{NULL} if not known;
  1657. @item data
  1658. pointer to size bytes of data at the specified offset;
  1659. @item off
  1660. offset of data in the overall value;
  1661. @item size
  1662. number of bytes in data available.
  1663. @end table
  1664. Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the
  1665. iteration.
  1666. @end deftypefn
  1667. @deftypefn {Function Pointer} void* {*MHD_WebSocketMallocCallback} (size_t buf_len)
  1668. @cindex websocket
  1669. This callback function is used internally by many websocket functions
  1670. for allocating data.
  1671. By default @code{malloc} is used.
  1672. You can use your own allocation function with @code{MHD_websocket_stream_init2}
  1673. if you wish to.
  1674. This can be useful for operating systems like Windows
  1675. where @code{malloc}, @code{realloc} and @code{free} are compiler-dependent.
  1676. You can call the associated @code{malloc} callback of
  1677. a websocket stream with @code{MHD_websocket_malloc}.
  1678. @table @var
  1679. @item buf_len
  1680. size of the buffer to allocate in bytes.
  1681. @end table
  1682. Return the pointer of the allocated buffer or @code{NULL} on failure.
  1683. @end deftypefn
  1684. @deftypefn {Function Pointer} void* {*MHD_WebSocketReallocCallback} (void *buf, size_t new_buf_len)
  1685. @cindex websocket
  1686. This callback function is used internally by many websocket
  1687. functions for reallocating data.
  1688. By default @code{realloc} is used.
  1689. You can use your own reallocation function with
  1690. @code{MHD_websocket_stream_init2} if you wish to.
  1691. This can be useful for operating systems like Windows
  1692. where @code{malloc}, @code{realloc} and @code{free} are compiler-dependent.
  1693. You can call the associated @code{realloc} callback of
  1694. a websocket stream with @code{MHD_websocket_realloc}.
  1695. @table @var
  1696. @item buf
  1697. current buffer, may be @code{NULL};
  1698. @item new_buf_len
  1699. new size of the buffer in bytes.
  1700. @end table
  1701. Return the pointer of the reallocated buffer or @code{NULL} on failure.
  1702. On failure the old pointer must remain valid.
  1703. @end deftypefn
  1704. @deftypefn {Function Pointer} void {*MHD_WebSocketFreeCallback} (void *buf)
  1705. @cindex websocket
  1706. This callback function is used internally by many websocket
  1707. functions for freeing data.
  1708. By default @code{free} is used.
  1709. You can use your own free function with
  1710. @code{MHD_websocket_stream_init2} if you wish to.
  1711. This can be useful for operating systems like Windows
  1712. where @code{malloc}, @code{realloc} and @code{free} are compiler-dependent.
  1713. You can call the associated @code{free} callback of
  1714. a websocket stream with @code{MHD_websocket_free}.
  1715. @table @var
  1716. @item cls
  1717. current buffer to free, this may be @code{NULL} then nothing happens.
  1718. @end table
  1719. @end deftypefn
  1720. @deftypefn {Function Pointer} size_t {*MHD_WebSocketRandomNumberGenerator} (void *cls, void* buf, size_t buf_len)
  1721. @cindex websocket
  1722. This callback function is used for generating random numbers
  1723. for masking payload data in client mode.
  1724. If you use websockets in server mode with @emph{libmicrohttpd} then
  1725. you don't need a random number generator, because
  1726. the server doesn't mask its outgoing messages.
  1727. However if you wish to use a websocket stream in client mode,
  1728. you must pass this callback function to @code{MHD_websocket_stream_init2}.
  1729. @table @var
  1730. @item cls
  1731. closure specified in @code{MHD_websocket_stream_init2};
  1732. @item buf
  1733. buffer to fill with random values;
  1734. @item buf_len
  1735. size of buffer in bytes.
  1736. @end table
  1737. Return the number of generated random bytes.
  1738. The return value should usually equal to buf_len.
  1739. @end deftypefn
  1740. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1741. @c ------------------------------------------------------------
  1742. @node microhttpd-init
  1743. @chapter Starting and stopping the server
  1744. @deftypefun {void} MHD_set_panic_func (MHD_PanicCallback cb, void *cls)
  1745. Set a handler for fatal errors.
  1746. @table @var
  1747. @item cb
  1748. function to call if MHD encounters a fatal internal error. If no handler was set explicitly, MHD will call @code{abort}.
  1749. @item cls
  1750. closure argument for cb; the other arguments are the name of the source file, line number and a string describing the nature of the fatal error (which can be @code{NULL})
  1751. @end table
  1752. @end deftypefun
  1753. @deftypefun {struct MHD_Daemon *} MHD_start_daemon (unsigned int flags, unsigned short port, MHD_AcceptPolicyCallback apc, void *apc_cls, MHD_AccessHandlerCallback dh, void *dh_cls, ...)
  1754. Start a webserver on the given port.
  1755. @table @var
  1756. @item flags
  1757. OR-ed combination of @code{MHD_FLAG} values;
  1758. @item port
  1759. port to bind to;
  1760. @item apc
  1761. callback to call to check which clients will be allowed to connect; you
  1762. can pass @code{NULL} in which case connections from any @acronym{IP} will be
  1763. accepted;
  1764. @item apc_cls
  1765. extra argument to @var{apc};
  1766. @item dh
  1767. default handler for all URIs;
  1768. @item dh_cls
  1769. extra argument to @var{dh}.
  1770. @end table
  1771. Additional arguments are a list of options (type-value pairs,
  1772. terminated with @code{MHD_OPTION_END}). It is mandatory to use
  1773. @code{MHD_OPTION_END} as last argument, even when there are no
  1774. additional arguments.
  1775. Return @code{NULL} on error, handle to daemon on success.
  1776. @end deftypefun
  1777. @deftypefun MHD_socket MHD_quiesce_daemon (struct MHD_Daemon *daemon)
  1778. @cindex quiesce
  1779. Stop accepting connections from the listening socket. Allows clients
  1780. to continue processing, but stops accepting new connections. Note
  1781. that the caller is responsible for closing the returned socket;
  1782. however, if MHD is run using threads (anything but external select
  1783. mode), it must not be closed until AFTER @code{MHD_stop_daemon} has
  1784. been called (as it is theoretically possible that an existing thread
  1785. is still using it).
  1786. This function is useful in the special case that a listen socket
  1787. is to be migrated to another process (i.e. a newer version of the
  1788. HTTP server) while existing connections should continue to be
  1789. processed until they are finished.
  1790. Return @code{-1} on error (daemon not listening), the handle to the
  1791. listen socket otherwise.
  1792. @end deftypefun
  1793. @deftypefun void MHD_stop_daemon (struct MHD_Daemon *daemon)
  1794. Shutdown an HTTP daemon.
  1795. @end deftypefun
  1796. @deftypefun enum MHD_Result MHD_run (struct MHD_Daemon *daemon)
  1797. Run webserver operations (without blocking unless in client callbacks).
  1798. This method should be called by clients in combination with
  1799. @code{MHD_get_fdset()} if the client-controlled @code{select}-method is used.
  1800. @cindex select
  1801. @cindex poll
  1802. This function will work for external @code{poll} and @code{select} mode.
  1803. However, if using external @code{select} mode, you may want to
  1804. instead use @code{MHD_run_from_select}, as it is more efficient.
  1805. @table @var
  1806. @item daemon
  1807. daemon to process connections of
  1808. @end table
  1809. Return @code{MHD_YES} on success, @code{MHD_NO} if this daemon was not
  1810. started with the right options for this call.
  1811. @end deftypefun
  1812. @deftypefun enum MHD_Result MHD_run_from_select (struct MHD_Daemon *daemon, const fd_set *read_fd_set, const fd_set *write_fd_set, const fd_set *except_fd_set)
  1813. Run webserver operations given sets of ready socket handles.
  1814. @cindex select
  1815. This method should be called by clients in combination with
  1816. @code{MHD_get_fdset} if the client-controlled (external)
  1817. select method is used.
  1818. You can use this function instead of @code{MHD_run} if you called
  1819. @code{select} on the result from @code{MHD_get_fdset}. File descriptors in
  1820. the sets that are not controlled by MHD will be ignored. Calling
  1821. this function instead of @code{MHD_run} is more efficient as MHD will
  1822. not have to call @code{select} again to determine which operations are
  1823. ready.
  1824. @table @var
  1825. @item daemon
  1826. daemon to process connections of
  1827. @item read_fd_set
  1828. set of descriptors that must be ready for reading without blocking
  1829. @item write_fd_set
  1830. set of descriptors that must be ready for writing without blocking
  1831. @item except_fd_set
  1832. ignored, can be NULL
  1833. @end table
  1834. Return @code{MHD_YES} on success, @code{MHD_NO} on serious internal
  1835. errors.
  1836. @end deftypefun
  1837. @deftypefun void MHD_add_connection (struct MHD_Daemon *daemon, int client_socket, const struct sockaddr *addr, socklen_t addrlen)
  1838. Add another client connection to the set of connections
  1839. managed by MHD. This API is usually not needed (since
  1840. MHD will accept inbound connections on the server socket).
  1841. Use this API in special cases, for example if your HTTP
  1842. server is behind NAT and needs to connect out to the
  1843. HTTP client, or if you are building a proxy.
  1844. If you use this API in conjunction with a internal select or a thread
  1845. pool, you must set the option @code{MHD_USE_ITC} to
  1846. ensure that the freshly added connection is immediately processed by
  1847. MHD.
  1848. The given client socket will be managed (and closed!) by MHD after
  1849. this call and must no longer be used directly by the application
  1850. afterwards.
  1851. @table @var
  1852. @item daemon
  1853. daemon that manages the connection
  1854. @item client_socket
  1855. socket to manage (MHD will expect to receive an HTTP request from this socket next).
  1856. @item addr
  1857. IP address of the client
  1858. @item addrlen
  1859. number of bytes in addr
  1860. @end table
  1861. This function will return @code{MHD_YES} on success,
  1862. @code{MHD_NO} if this daemon could
  1863. not handle the connection (i.e. malloc failed, etc).
  1864. The socket will be closed in any case; 'errno' is set
  1865. to indicate further details about the error.
  1866. @end deftypefun
  1867. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1868. @c -----------------------------------------------------------
  1869. @node microhttpd-inspect
  1870. @chapter Implementing external @code{select}
  1871. @deftypefun enum MHD_Result MHD_get_fdset (struct MHD_Daemon *daemon, fd_set * read_fd_set, fd_set * write_fd_set, fd_set * except_fd_set, int *max_fd)
  1872. Obtain the @code{select()} sets for this daemon. The daemon's socket
  1873. is added to @var{read_fd_set}. The list of currently existent
  1874. connections is scanned and their file descriptors added to the correct
  1875. set.
  1876. When calling this function, FD_SETSIZE is assumed to be platform's
  1877. default. If you changed FD_SETSIZE for your application,
  1878. you should use @code{MHD_get_fdset2()} instead.
  1879. This function should only be called in when MHD is configured to use
  1880. external select with @code{select()} or with @code{epoll()}. In
  1881. the latter case, it will only add the single @code{epoll()} file
  1882. descriptor used by MHD to the sets.
  1883. After the call completed successfully: the variable referenced by
  1884. @var{max_fd} references the file descriptor with highest integer
  1885. identifier. The variable must be set to zero before invoking this
  1886. function.
  1887. Return @code{MHD_YES} on success, @code{MHD_NO} if: the arguments are
  1888. invalid (example: @code{NULL} pointers); this daemon was not started with
  1889. the right options for this call.
  1890. @end deftypefun
  1891. @deftypefun enum MHD_Result MHD_get_fdset2 (struct MHD_Daemon *daemon, fd_set * read_fd_set, fd_set * write_fd_set, fd_set * except_fd_set, int *max_fd, unsigned int fd_setsize)
  1892. Like @code{MHD_get_fdset()}, except that you can manually specify the value of FD_SETSIZE used by your application.
  1893. @end deftypefun
  1894. @deftypefun enum MHD_Result MHD_get_timeout (struct MHD_Daemon *daemon, unsigned long long *timeout)
  1895. @cindex timeout
  1896. Obtain timeout value for select for this daemon (only needed if
  1897. connection timeout is used). The returned value is how many
  1898. milliseconds @code{select} should at most block, not the timeout value
  1899. set for connections. This function must not be called if the
  1900. @code{MHD_USE_THREAD_PER_CONNECTION} mode is in use (since then it is
  1901. not meaningful to ask for a timeout, after all, there is concurrenct
  1902. activity). The function must also not be called by user-code if
  1903. @code{MHD_USE_INTERNAL_POLLING_THREAD} is in use. In the latter case, the
  1904. behavior is undefined.
  1905. @table @var
  1906. @item daemon
  1907. which daemon to obtain the timeout from.
  1908. @item timeout
  1909. will be set to the timeout (in milliseconds).
  1910. @end table
  1911. Return @code{MHD_YES} on success, @code{MHD_NO} if timeouts are not used
  1912. (or no connections exist that would necessitate the use of a timeout
  1913. right now).
  1914. @end deftypefun
  1915. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1916. @c -----------------------------------------------------------
  1917. @node microhttpd-requests
  1918. @chapter Handling requests
  1919. @deftypefun int MHD_get_connection_values (struct MHD_Connection *connection, enum MHD_ValueKind kind, MHD_KeyValueIterator iterator, void *iterator_cls)
  1920. Get all the headers matching @var{kind} from the request. The @var{kind}
  1921. argument can be a bitmask, ORing the various header kinds that are
  1922. requested.
  1923. The @var{iterator} callback is invoked once for each header, with
  1924. @var{iterator_cls} as first argument. After version 0.9.19, the
  1925. headers are iterated in the same order as they were received from
  1926. the network; previous versions iterated over the headers in reverse
  1927. order.
  1928. @code{MHD_get_connection_values} returns the number of entries
  1929. iterated over; this can be less than the number of headers if, while
  1930. iterating, @var{iterator} returns @code{MHD_NO}.
  1931. @var{iterator} can be @code{NULL}: in this case this function just counts
  1932. and returns the number of headers.
  1933. In the case of @code{MHD_GET_ARGUMENT_KIND}, the @var{value} argument
  1934. will be @code{NULL} if the URL contained a key without an equals operator.
  1935. For example, for a HTTP request to the URL ``http://foo/bar?key'', the
  1936. @var{value} argument is @code{NULL}; in contrast, a HTTP request to the URL
  1937. ``http://foo/bar?key='', the @var{value} argument is the empty string.
  1938. The normal case is that the URL contains ``http://foo/bar?key=value''
  1939. in which case @var{value} would be the string ``value'' and @var{key}
  1940. would contain the string ``key''.
  1941. @end deftypefun
  1942. @deftypefun enum MHD_Result MHD_set_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key, const char *value)
  1943. This function can be used to append an entry to
  1944. the list of HTTP headers of a connection (so that the
  1945. @code{MHD_get_connection_values function} will return
  1946. them -- and the MHD PostProcessor will also
  1947. see them). This maybe required in certain
  1948. situations (see Mantis #1399) where (broken)
  1949. HTTP implementations fail to supply values needed
  1950. by the post processor (or other parts of the
  1951. application).
  1952. This function MUST only be called from within
  1953. the MHD_AccessHandlerCallback (otherwise, access
  1954. maybe improperly synchronized). Furthermore,
  1955. the client must guarantee that the key and
  1956. value arguments are 0-terminated strings that
  1957. are NOT freed until the connection is closed.
  1958. (The easiest way to do this is by passing only
  1959. arguments to permanently allocated strings.).
  1960. @var{connection} is the connection for which
  1961. the entry for @var{key} of the given @var{kind}
  1962. should be set to the given @var{value}.
  1963. The function returns @code{MHD_NO} if the operation
  1964. could not be performed due to insufficient memory
  1965. and @code{MHD_YES} on success.
  1966. @end deftypefun
  1967. @deftypefun {const char *} MHD_lookup_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key)
  1968. Get a particular header value. If multiple values match the
  1969. @var{kind}, return one of them (the ``first'', whatever that means).
  1970. @var{key} must reference a zero-terminated ASCII-coded string
  1971. representing the header to look for: it is compared against the
  1972. headers using (basically) @code{strcasecmp()}, so case is ignored.
  1973. @end deftypefun
  1974. @deftypefun {const char *} MHD_lookup_connection_value_n (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key, size_t key_size, const char **value_ptr, size_t *value_size_ptr)
  1975. Get a particular header value. If multiple values match the
  1976. @var{kind}, return one of them (the ``first'', whatever that means).
  1977. @var{key} must reference an ASCII-coded string
  1978. representing the header to look for: it is compared against the
  1979. headers using (basically) @code{strncasecmp()}, so case is ignored.
  1980. The @var{value_ptr} is set to the address of the value found,
  1981. and @var{value_size_ptr} is set to the number of bytes in the
  1982. value.
  1983. @end deftypefun
  1984. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1985. @c ------------------------------------------------------------
  1986. @node microhttpd-responses
  1987. @chapter Building responses to requests
  1988. @noindent
  1989. Response objects handling by MHD is asynchronous with respect to the
  1990. application execution flow. Instances of the @code{MHD_Response}
  1991. structure are not associated to a daemon and neither to a client
  1992. connection: they are managed with reference counting.
  1993. In the simplest case: we allocate a new @code{MHD_Response} structure
  1994. for each response, we use it once and finally we destroy it.
  1995. MHD allows more efficient resources usages.
  1996. Example: we allocate a new @code{MHD_Response} structure for each
  1997. response @strong{kind}, we use it every time we have to give that
  1998. response and we finally destroy it only when the daemon shuts down.
  1999. @menu
  2000. * microhttpd-response enqueue:: Enqueuing a response.
  2001. * microhttpd-response create:: Creating a response object.
  2002. * microhttpd-response headers:: Adding headers to a response.
  2003. * microhttpd-response options:: Setting response options.
  2004. * microhttpd-response inspect:: Inspecting a response object.
  2005. * microhttpd-response upgrade:: Creating a response for protocol upgrades.
  2006. @end menu
  2007. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2008. @c ------------------------------------------------------------
  2009. @node microhttpd-response enqueue
  2010. @section Enqueuing a response
  2011. @deftypefun enum MHD_Result MHD_queue_response (struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
  2012. Queue a response to be transmitted to the client as soon as possible
  2013. but only after MHD_AccessHandlerCallback returns. This function
  2014. checks that it is legal to queue a response at this time for the
  2015. given connection. It also increments the internal reference
  2016. counter for the response object (the counter will be decremented
  2017. automatically once the response has been transmitted).
  2018. @table @var
  2019. @item connection
  2020. the connection identifying the client;
  2021. @item status_code
  2022. HTTP status code (i.e. @code{200} for OK);
  2023. @item response
  2024. response to transmit.
  2025. @end table
  2026. Return @code{MHD_YES} on success or if message has been queued. Return
  2027. @code{MHD_NO}: if arguments are invalid (example: @code{NULL} pointer); on
  2028. error (i.e. reply already sent).
  2029. @end deftypefun
  2030. @deftypefun void MHD_destroy_response (struct MHD_Response *response)
  2031. Destroy a response object and associated resources (decrement the
  2032. reference counter). Note that MHD may keep some of the resources
  2033. around if the response is still in the queue for some clients, so the
  2034. memory may not necessarily be freed immediately.
  2035. @end deftypefun
  2036. An explanation of reference counting@footnote{Note to readers acquainted
  2037. to the Tcl API: reference counting on @code{MHD_Connection}
  2038. structures is handled in the same way as Tcl handles @code{Tcl_Obj}
  2039. structures through @code{Tcl_IncrRefCount()} and
  2040. @code{Tcl_DecrRefCount()}.}:
  2041. @enumerate
  2042. @item
  2043. a @code{MHD_Response} object is allocated:
  2044. @example
  2045. struct MHD_Response * response = MHD_create_response_from_buffer(...);
  2046. /* here: reference counter = 1 */
  2047. @end example
  2048. @item
  2049. the @code{MHD_Response} object is enqueued in a @code{MHD_Connection}:
  2050. @example
  2051. MHD_queue_response(connection, , response);
  2052. /* here: reference counter = 2 */
  2053. @end example
  2054. @item
  2055. the creator of the response object discharges responsibility for it:
  2056. @example
  2057. MHD_destroy_response(response);
  2058. /* here: reference counter = 1 */
  2059. @end example
  2060. @item
  2061. the daemon handles the connection sending the response's data to the
  2062. client then decrements the reference counter by calling
  2063. @code{MHD_destroy_response()}: the counter's value drops to zero and
  2064. the @code{MHD_Response} object is released.
  2065. @end enumerate
  2066. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2067. @c ------------------------------------------------------------
  2068. @node microhttpd-response create
  2069. @section Creating a response object
  2070. @deftypefun {struct MHD_Response *} MHD_create_response_from_callback (uint64_t size, size_t block_size, MHD_ContentReaderCallback crc, void *crc_cls, MHD_ContentReaderFreeCallback crfc)
  2071. Create a response object. The response object can be extended with
  2072. header information and then it can be used any number of times.
  2073. @table @var
  2074. @item size
  2075. size of the data portion of the response, @code{-1} for unknown;
  2076. @item block_size
  2077. preferred block size for querying @var{crc} (advisory only, MHD may
  2078. still call @var{crc} using smaller chunks); this is essentially the
  2079. buffer size used for @acronym{IO}, clients should pick a value that is
  2080. appropriate for @acronym{IO} and memory performance requirements;
  2081. @item crc
  2082. callback to use to obtain response data;
  2083. @item crc_cls
  2084. extra argument to @var{crc};
  2085. @item crfc
  2086. callback to call to free @var{crc_cls} resources.
  2087. @end table
  2088. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  2089. @end deftypefun
  2090. @deftypefun {struct MHD_Response *} MHD_create_response_from_fd (uint64_t size, int fd)
  2091. Create a response object. The response object can be extended with
  2092. header information and then it can be used any number of times.
  2093. @table @var
  2094. @item size
  2095. size of the data portion of the response (should be smaller or equal to the
  2096. size of the file)
  2097. @item fd
  2098. file descriptor referring to a file on disk with the data; will be
  2099. closed when response is destroyed; note that 'fd' must be an actual
  2100. file descriptor (not a pipe or socket) since MHD might use 'sendfile'
  2101. or 'seek' on it. The descriptor should be in blocking-IO mode.
  2102. @end table
  2103. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  2104. @end deftypefun
  2105. @deftypefun {struct MHD_Response *} MHD_create_response_from_pipe (int fd)
  2106. Create a response object. The response object can be extended with
  2107. header information and then it can be used ONLY ONCE.
  2108. @table @var
  2109. @item fd
  2110. file descriptor of the read-end of the pipe; will be
  2111. closed when response is destroyed.
  2112. The descriptor should be in blocking-IO mode.
  2113. @end table
  2114. Return @code{NULL} on error (i.e. out of memory).
  2115. @end deftypefun
  2116. @deftypefun {struct MHD_Response *} MHD_create_response_from_fd_at_offset (size_t size, int fd, off_t offset)
  2117. Create a response object. The response object can be extended with
  2118. header information and then it can be used any number of times.
  2119. Note that you need to be a bit careful about @code{off_t} when
  2120. writing this code. Depending on your platform, MHD is likely
  2121. to have been compiled with support for 64-bit files. When you
  2122. compile your own application, you must make sure that @code{off_t}
  2123. is also a 64-bit value. If not, your compiler may pass a 32-bit
  2124. value as @code{off_t}, which will result in 32-bits of garbage.
  2125. If you use the autotools, use the @code{AC_SYS_LARGEFILE} autoconf
  2126. macro and make sure to include the generated @file{config.h} file
  2127. before @file{microhttpd.h} to avoid problems. If you do not have a
  2128. build system and only want to run on a GNU/Linux system, you could
  2129. also use
  2130. @verbatim
  2131. #define _FILE_OFFSET_BITS 64
  2132. #include <sys/types.h>
  2133. #include <sys/stat.h>
  2134. #include <fcntl.h>
  2135. #include <microhttpd.h>
  2136. @end verbatim
  2137. to ensure 64-bit @code{off_t}. Note that if your operating system
  2138. does not support 64-bit files, MHD will be compiled with a 32-bit
  2139. @code{off_t} (in which case the above would be wrong).
  2140. @table @var
  2141. @item size
  2142. size of the data portion of the response (number of bytes to transmit from the
  2143. file starting at offset).
  2144. @item fd
  2145. file descriptor referring to a file on disk with the data; will be
  2146. closed when response is destroyed; note that 'fd' must be an actual
  2147. file descriptor (not a pipe or socket) since MHD might use 'sendfile'
  2148. or 'seek' on it. The descriptor should be in blocking-IO mode.
  2149. @item offset
  2150. offset to start reading from in the file
  2151. @end table
  2152. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  2153. @end deftypefun
  2154. @deftypefun {struct MHD_Response *} MHD_create_response_from_buffer (size_t size, void *data, enum MHD_ResponseMemoryMode mode)
  2155. Create a response object. The response object can be extended with
  2156. header information and then it can be used any number of times.
  2157. @table @var
  2158. @item size
  2159. size of the data portion of the response;
  2160. @item buffer
  2161. the data itself;
  2162. @item mode
  2163. memory management options for buffer; use
  2164. MHD_RESPMEM_PERSISTENT if the buffer is static/global memory,
  2165. use MHD_RESPMEM_MUST_FREE if the buffer is heap-allocated and
  2166. should be freed by MHD and MHD_RESPMEM_MUST_COPY if the
  2167. buffer is in transient memory (i.e. on the stack) and must
  2168. be copied by MHD;
  2169. @end table
  2170. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  2171. @end deftypefun
  2172. @deftypefun {struct MHD_Response *} MHD_create_response_from_buffer_with_free_callback (size_t size, void *data, MHD_ContentReaderFreeCallback crfc)
  2173. Create a response object. The buffer at the end must be free'd
  2174. by calling the @var{crfc} function.
  2175. @table @var
  2176. @item size
  2177. size of the data portion of the response;
  2178. @item buffer
  2179. the data itself;
  2180. @item crfc
  2181. function to call at the end to free memory allocated at @var{buffer}.
  2182. @end table
  2183. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  2184. @end deftypefun
  2185. @deftypefun {struct MHD_Response *} MHD_create_response_from_data (size_t size, void *data, int must_free, int must_copy)
  2186. Create a response object. The response object can be extended with
  2187. header information and then it can be used any number of times.
  2188. This function is deprecated, use @code{MHD_create_response_from_buffer} instead.
  2189. @table @var
  2190. @item size
  2191. size of the data portion of the response;
  2192. @item data
  2193. the data itself;
  2194. @item must_free
  2195. if true: MHD should free data when done;
  2196. @item must_copy
  2197. if true: MHD allocates a block of memory and use it to make a copy of
  2198. @var{data} embedded in the returned @code{MHD_Response} structure;
  2199. handling of the embedded memory is responsibility of MHD; @var{data}
  2200. can be released anytime after this call returns.
  2201. @end table
  2202. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  2203. @end deftypefun
  2204. Example: create a response from a statically allocated string:
  2205. @example
  2206. const char * data = "<html><body><p>Error!</p></body></html>";
  2207. struct MHD_Connection * connection = ...;
  2208. struct MHD_Response * response;
  2209. response = MHD_create_response_from_buffer (strlen(data), data,
  2210. MHD_RESPMEM_PERSISTENT);
  2211. MHD_queue_response(connection, 404, response);
  2212. MHD_destroy_response(response);
  2213. @end example
  2214. @deftypefun {struct MHD_Response *} MHD_create_response_from_iovec (const struct MHD_IoVec *iov, int iovcnt, MHD_ContentReaderFreeCallback crfc, void *cls)
  2215. Create a response object from an array of memory buffers.
  2216. The response object can be extended with header information and then be used
  2217. any number of times.
  2218. @table @var
  2219. @item iov
  2220. the array for response data buffers, an internal copy of this will be made; however, note that the data pointed to by the @var{iov} is not copied and must be preserved unchanged at the given locations until the response is no longer in use and the @var{crfc} is called;
  2221. @item iovcnt
  2222. the number of elements in @var{iov};
  2223. @item crfc
  2224. the callback to call to free resources associated with @var{iov};
  2225. @item cls
  2226. the argument to @var{crfc};
  2227. @end table
  2228. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  2229. @end deftypefun
  2230. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2231. @c ------------------------------------------------------------
  2232. @node microhttpd-response headers
  2233. @section Adding headers to a response
  2234. @deftypefun enum MHD_Result MHD_add_response_header (struct MHD_Response *response, const char *header, const char *content)
  2235. Add a header line to the response. The strings referenced by
  2236. @var{header} and @var{content} must be zero-terminated and they are
  2237. duplicated into memory blocks embedded in @var{response}.
  2238. Notice that the strings must not hold newlines, carriage returns or tab
  2239. chars.
  2240. MHD_add_response_header() prevents applications from setting a
  2241. ``Transfer-Encoding'' header to values other than ``identity'' or
  2242. ``chunked'' as other transfer encodings are not supported by MHD. Note
  2243. that usually MHD will pick the transfer encoding correctly
  2244. automatically, but applications can use the header to force a
  2245. particular behavior.
  2246. MHD_add_response_header() also prevents applications from setting a
  2247. ``Content-Length'' header. MHD will automatically set a correct
  2248. ``Content-Length'' header if it is possible and allowed.
  2249. Return @code{MHD_NO} on error (i.e. invalid header or content format or
  2250. memory allocation error).
  2251. @end deftypefun
  2252. @deftypefun enum MHD_Result MHD_add_response_footer (struct MHD_Response *response, const char *footer, const char *content)
  2253. Add a footer line to the response. The strings referenced by
  2254. @var{footer} and @var{content} must be zero-terminated and they are
  2255. duplicated into memory blocks embedded in @var{response}.
  2256. Notice that the strings must not hold newlines, carriage returns or tab
  2257. chars. You can add response footers at any time before signalling the
  2258. end of the response to MHD (not just before calling 'MHD_queue_response').
  2259. Footers are useful for adding cryptographic checksums to the reply or to
  2260. signal errors encountered during data generation. This call was introduced
  2261. in MHD 0.9.3.
  2262. Return @code{MHD_NO} on error (i.e. invalid header or content format or
  2263. memory allocation error).
  2264. @end deftypefun
  2265. @deftypefun enum MHD_Result MHD_del_response_header (struct MHD_Response *response, const char *header, const char *content)
  2266. Delete a header (or footer) line from the response. Return @code{MHD_NO} on error
  2267. (arguments are invalid or no such header known).
  2268. @end deftypefun
  2269. @c ------------------------------------------------------------
  2270. @node microhttpd-response options
  2271. @section Setting response options
  2272. @deftypefun enum MHD_Result MHD_set_response_options (struct MHD_Response *response, enum MHD_ResponseFlags flags, ...)
  2273. Set special flags and options for a response.
  2274. Calling this functions sets the given flags and options for the response.
  2275. @table @var
  2276. @item response
  2277. which response should be modified;
  2278. @item flags
  2279. flags to set for the response;
  2280. @end table
  2281. Additional arguments are a list of options (type-value pairs,
  2282. terminated with @code{MHD_RO_END}). It is mandatory to use
  2283. @code{MHD_RO_END} as last argument, even when there are no
  2284. additional arguments.
  2285. Return @code{MHD_NO} on error, @code{MHD_YES} on success.
  2286. @end deftypefun
  2287. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2288. @c ------------------------------------------------------------
  2289. @node microhttpd-response inspect
  2290. @section Inspecting a response object
  2291. @deftypefun int MHD_get_response_headers (struct MHD_Response *response, MHD_KeyValueIterator iterator, void *iterator_cls)
  2292. Get all of the headers added to a response.
  2293. Invoke the @var{iterator} callback for each header in the response,
  2294. using @var{iterator_cls} as first argument. Return number of entries
  2295. iterated over. @var{iterator} can be @code{NULL}: in this case the function
  2296. just counts headers.
  2297. @var{iterator} should not modify the its key and value arguments, unless
  2298. we know what we are doing.
  2299. @end deftypefun
  2300. @deftypefun {const char *} MHD_get_response_header (struct MHD_Response *response, const char *key)
  2301. Find and return a pointer to the value of a particular header from the
  2302. response. @var{key} must reference a zero-terminated string
  2303. representing the header to look for. The search is case sensitive.
  2304. Return @code{NULL} if header does not exist or @var{key} is @code{NULL}.
  2305. We should not modify the value, unless we know what we are doing.
  2306. @end deftypefun
  2307. @c ------------------------------------------------------------
  2308. @node microhttpd-response upgrade
  2309. @section Creating a response for protocol upgrades
  2310. @cindex WebSockets
  2311. @cindex Upgrade
  2312. @cindex HTTP2
  2313. @cindex RFC2817
  2314. With RFC 2817 a mechanism to switch protocols within HTTP was
  2315. introduced. Here, a client sends a request with a ``Connection:
  2316. Upgrade'' header. The server responds with a ``101 Switching
  2317. Protocols'' response header, after which the two parties begin to
  2318. speak a different (non-HTTP) protocol over the TCP connection.
  2319. This mechanism is used for upgrading HTTP 1.1 connections to HTTP2 or
  2320. HTTPS, as well as for implementing WebSockets. Which protocol
  2321. upgrade is performed is negotiated between server and client in
  2322. additional headers, in particular the ``Upgrade'' header.
  2323. MHD supports switching protocols using this mechanism only if the
  2324. @code{MHD_ALLOW_SUSPEND_RESUME} flag has been set when starting
  2325. the daemon. If this flag has been set, applications can upgrade
  2326. a connection by queueing a response (using the
  2327. @code{MHD_HTTP_SWITCHING_PROTOCOLS} status code) which must
  2328. have been created with the following function:
  2329. @deftypefun enum MHD_Result MHD_create_response_for_upgrade (MHD_UpgradeHandler upgrade_handler, void *upgrade_handler_cls)
  2330. Create a response suitable for switching protocols. Returns @code{MHD_YES} on success. @code{upgrade_handler} must not be @code{NULL}.
  2331. When creating this type of response, the ``Connection: Upgrade''
  2332. header will be set automatically for you. MHD requires that you
  2333. additionally set an ``Upgrade:'' header. The ``Upgrade'' header
  2334. must simply exist, the specific value is completely up to the
  2335. application.
  2336. @end deftypefun
  2337. The @code{upgrade_handler} argument to the above has the following type:
  2338. @deftypefn {Function Pointer} void {*MHD_UpgradeHandler} (void *cls, struct MHD_Connection *connection, const char *extra_in, size_t extra_in_size, MHD_socket sock, struct MHD_UpgradeResponseHandle *urh)
  2339. This function will be called once MHD has transmitted the header of the response to the connection that is being upgraded. At this point, the application is expected to take over the socket @code{sock} and speak the non-HTTP protocol to which the connection was upgraded. MHD will no longer use the socket; this includes handling timeouts. The application must call @code{MHD_upgrade_action} with an upgrade action of @code{MHD_UPGRADE_ACTION_CLOSE} when it is done processing the connection to close the socket. The application must not call @code{MHD_stop_daemon} on the respective daemon as long as it is still handling the connection. The arguments given to the @code{upgrade_handler} have the following meaning:
  2340. @table @var
  2341. @item cls
  2342. matches the @code{upgrade_handler_cls} that was given to @code{MHD_create_response_for_upgrade}
  2343. @item connection
  2344. identifies the connection that is being upgraded;
  2345. @item req_cls
  2346. last value left in `*req_cls` in the `MHD_AccessHandlerCallback`
  2347. @item extra_in
  2348. buffer of bytes MHD read ``by accident'' from the socket already. This can happen if the client eagerly transmits more than just the HTTP request. The application should treat these as if it had read them from the socket.
  2349. @item extra_in_size
  2350. number of bytes in @code{extra_in}
  2351. @item sock
  2352. the socket which the application can now use directly for some bi-directional communication with the client. The application can henceforth use @code{recv()} and @code{send()} or @code{read()} and @code{write()} system calls on the socket. However, @code{ioctl()} and @code{setsockopt()} functions will not work as expected when using HTTPS. Such operations may be supported in the future via @code{MHD_upgrade_action}. Most importantly, the application must never call @code{close()} on this socket. Closing the socket must be done using @code{MHD_upgrade_action}. However, while close is forbidden, the application may call @code{shutdown()} on the socket.
  2353. @item urh
  2354. argument for calls to @code{MHD_upgrade_action}. Applications must eventually use this function to perform the @code{close()} action on the socket.
  2355. @end table
  2356. @end deftypefn
  2357. @deftypefun enum MHD_Result MHD_upgrade_action (struct MHD_UpgradeResponseHandle *urh, enum MHD_UpgradeAction action, ...)
  2358. Perform special operations related to upgraded connections.
  2359. @table @var
  2360. @item urh
  2361. identifies the upgraded connection to perform an action on
  2362. @item action
  2363. specifies the action to perform; further arguments to the function depend on the specifics of the action.
  2364. @end table
  2365. @end deftypefun
  2366. @deftp {Enumeration} MHD_UpgradeAction
  2367. Set of actions to be performed on upgraded connections. Passed as an argument to
  2368. @code{MHD_upgrade_action()}.
  2369. @table @code
  2370. @item MHD_UPGRADE_ACTION_CLOSE
  2371. Closes the connection. Must be called once the application is done with the client. Takes no additional arguments.
  2372. @item MHD_UPGRADE_ACTION_CORK_ON
  2373. Enable corking on the underlying socket.
  2374. @item MHD_UPGRADE_ACTION_CORK_OFF
  2375. Disable corking on the underlying socket.
  2376. @end table
  2377. @end deftp
  2378. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2379. @c ------------------------------------------------------------
  2380. @node microhttpd-flow
  2381. @chapter Flow control.
  2382. @noindent
  2383. Sometimes it may be possible that clients upload data faster
  2384. than an application can process it, or that an application
  2385. needs an extended period of time to generate a response.
  2386. If @code{MHD_USE_THREAD_PER_CONNECTION} is used, applications
  2387. can simply deal with this by performing their logic within the
  2388. thread and thus effectively blocking connection processing
  2389. by MHD. In all other modes, blocking logic must not be
  2390. placed within the callbacks invoked by MHD as this would also
  2391. block processing of other requests, as a single thread may be
  2392. responsible for tens of thousands of connections.
  2393. Instead, applications using thread modes other than
  2394. @code{MHD_USE_THREAD_PER_CONNECTION} should use the
  2395. following functions to perform flow control.
  2396. @deftypefun enum MHD_Result MHD_suspend_connection (struct MHD_Connection *connection)
  2397. Suspend handling of network data for a given connection. This can
  2398. be used to dequeue a connection from MHD's event loop (external
  2399. select, internal select or thread pool; not applicable to
  2400. thread-per-connection!) for a while.
  2401. If you use this API in conjunction with a internal select or a
  2402. thread pool, you must set the option @code{MHD_ALLOW_SUSPEND_RESUME} to
  2403. ensure that a resumed connection is immediately processed by MHD.
  2404. Suspended connections continue to count against the total number of
  2405. connections allowed (per daemon, as well as per IP, if such limits
  2406. are set). Suspended connections will NOT time out; timeouts will
  2407. restart when the connection handling is resumed. While a
  2408. connection is suspended, MHD will not detect disconnects by the
  2409. client.
  2410. The only safe time to suspend a connection is from the
  2411. @code{MHD_AccessHandlerCallback} or from the respective
  2412. @code{MHD_ContentReaderCallback} (but in this case the
  2413. response object must not be shared among multiple
  2414. connections).
  2415. When suspending from the @code{MHD_AccessHandlerCallback}
  2416. you MUST afterwards return @code{MHD_YES} from the access handler
  2417. callback (as MHD_NO would imply to both close and suspend
  2418. the connection, which is not allowed).
  2419. Finally, it is an API violation to call @code{MHD_stop_daemon} while
  2420. having suspended connections (this will at least create memory and
  2421. socket leaks or lead to undefined behavior). You must explicitly
  2422. resume all connections before stopping the daemon.
  2423. @table @var
  2424. @item connection
  2425. the connection to suspend
  2426. @end table
  2427. @end deftypefun
  2428. @deftypefun enum MHD_Result MHD_resume_connection (struct MHD_Connection *connection)
  2429. Resume handling of network data for suspended connection. It is safe
  2430. to resume a suspended connection at any time. Calling this function
  2431. on a connection that was not previously suspended will result in
  2432. undefined behavior.
  2433. If you are using this function in ``external'' select mode, you must
  2434. make sure to run @code{MHD_run} afterwards (before again calling
  2435. @code{MHD_get_fdset}), as otherwise the change may not be reflected in
  2436. the set returned by @code{MHD_get_fdset} and you may end up with a
  2437. connection that is stuck until the next network activity.
  2438. You can check whether a connection is currently suspended using
  2439. @code{MHD_get_connection_info} by querying for
  2440. @code{MHD_CONNECTION_INFO_CONNECTION_SUSPENDED}.
  2441. @table @var
  2442. @item connection
  2443. the connection to resume
  2444. @end table
  2445. @end deftypefun
  2446. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2447. @c ------------------------------------------------------------
  2448. @node microhttpd-dauth
  2449. @chapter Utilizing Authentication
  2450. @noindent
  2451. MHD support three types of client authentication.
  2452. Basic authentication uses a simple authentication method based
  2453. on BASE64 algorithm. Username and password are exchanged in clear
  2454. between the client and the server, so this method must only be used
  2455. for non-sensitive content or when the session is protected with https.
  2456. When using basic authentication MHD will have access to the clear
  2457. password, possibly allowing to create a chained authentication
  2458. toward an external authentication server.
  2459. Digest authentication uses a one-way authentication method based
  2460. on MD5 hash algorithm. Only the hash will transit over the network,
  2461. hence protecting the user password. The nonce will prevent replay
  2462. attacks. This method is appropriate for general use, especially
  2463. when https is not used to encrypt the session.
  2464. Client certificate authentication uses a X.509 certificate from
  2465. the client. This is the strongest authentication mechanism but it
  2466. requires the use of HTTPS. Client certificate authentication can
  2467. be used simultaneously with Basic or Digest Authentication in order
  2468. to provide a two levels authentication (like for instance separate
  2469. machine and user authentication). A code example for using
  2470. client certificates is presented in the MHD tutorial.
  2471. @menu
  2472. * microhttpd-dauth basic:: Using Basic Authentication.
  2473. * microhttpd-dauth digest:: Using Digest Authentication.
  2474. @end menu
  2475. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2476. @c ------------------------------------------------------------
  2477. @node microhttpd-dauth basic
  2478. @section Using Basic Authentication
  2479. @deftypefun {void} MHD_free (void *ptr)
  2480. Free the memory given at @code{ptr}. Used to free data structures allocated by MHD. Calls @code{free(ptr)}.
  2481. @end deftypefun
  2482. @deftypefun {char *} MHD_basic_auth_get_username_password3 (struct MHD_Connection *connection)
  2483. Get the username and password from the basic authorization header sent by the client.
  2484. Return @code{NULL} if no Basic Authorization header set by the client or if Base64
  2485. encoding is invalid; a pointer to the structure with username and password
  2486. if found values set by the client.
  2487. If returned value is not @code{NULL}, the value must be @code{MHD_free()}'ed.
  2488. @end deftypefun
  2489. @deftypefun {enum MHD_Result} MHD_queue_basic_auth_fail_response3 (struct MHD_Connection *connection, const char *realm, int prefer_utf8, struct MHD_Response *response)
  2490. Queues a response to request basic authentication from the client.
  2491. Return @code{MHD_YES} if successful, otherwise @code{MHD_NO}.
  2492. @var{realm} must reference to a zero-terminated string representing the realm.
  2493. @var{prefer_utf8} if set to @code{MHD_YES} then parameter @code{charset} with value
  2494. @code{UTF-8} will be added to the response authentication header which indicates
  2495. that UTF-8 encoding is preferred for username and password.
  2496. @var{response} a response structure to specify what shall be presented to the
  2497. client with a 401 HTTP status.
  2498. @end deftypefun
  2499. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2500. @c ------------------------------------------------------------
  2501. @node microhttpd-dauth digest
  2502. @section Using Digest Authentication
  2503. MHD supports MD5 (deprecated by IETF) and SHA-256 hash algorithms
  2504. for digest authentication. The @code{MHD_DigestAuthAlgorithm} enumeration
  2505. is used to specify which algorithm should be used.
  2506. @deftp {Enumeration} MHD_DigestAuthAlgorithm
  2507. Which digest algorithm should be used. Must be used consistently.
  2508. @table @code
  2509. @item MHD_DIGEST_ALG_AUTO
  2510. Have MHD pick an algorithm currently considered secure. For now defaults to SHA-256.
  2511. @item MHD_DIGEST_ALG_MD5
  2512. Force use of (deprecated, ancient, insecure) MD5.
  2513. @item MHD_DIGEST_ALG_SHA256
  2514. Force use of SHA-256.
  2515. @end table
  2516. @end deftp
  2517. @deftp {Enumeration} MHD_DigestAuthResult
  2518. The result of digest authentication of the client.
  2519. @table @code
  2520. @item MHD_DAUTH_OK
  2521. Authentication OK.
  2522. @item MHD_DAUTH_ERROR
  2523. General error, like ``out of memory''.
  2524. @item MHD_DAUTH_WRONG_HEADER
  2525. No ``Authorization'' header or wrong format of the header.
  2526. @item MHD_DAUTH_WRONG_USERNAME
  2527. Wrong ``username''.
  2528. @item MHD_DAUTH_WRONG_REALM
  2529. Wrong ``realm''.
  2530. @item MHD_DAUTH_WRONG_URI
  2531. Wrong ``URI'' (or URI parameters).
  2532. @item MHD_DAUTH_NONCE_STALE
  2533. The ``nonce'' is too old. Suggest the client to retry with the same username and
  2534. password to get the fresh ``nonce''.
  2535. The validity of the ``nonce'' may not be checked.
  2536. @item MHD_DAUTH_NONCE_WRONG
  2537. The ``nonce'' is wrong. May indicate an attack attempt.
  2538. @item MHD_DAUTH_RESPONSE_WRONG
  2539. The ``response'' is wrong. May indicate an attack attempt.
  2540. @end table
  2541. @end deftp
  2542. @deftypefun {char *} MHD_digest_auth_get_username (struct MHD_Connection *connection)
  2543. Find and return a pointer to the username value from the request header.
  2544. Return @code{NULL} if the value is not found or header does not exist.
  2545. If returned value is not @code{NULL}, the value must be @code{MHD_free()}'ed.
  2546. @end deftypefun
  2547. @deftypefun enum MHD_DigestAuthResult MHD_digest_auth_check3 (struct MHD_Connection *connection, const char *realm, const char *username, const char *password, unsigned int nonce_timeout, enum MHD_DigestAuthAlgorithm algo)
  2548. Checks if the provided values in the WWW-Authenticate header are valid
  2549. and sound according to RFC7616. If valid return @code{MHD_DAUTH_OK}, otherwise return the error code.
  2550. @var{realm} must reference to a zero-terminated string representing the realm.
  2551. @var{username} must reference to a zero-terminated string representing the username,
  2552. it is usually the returned value from MHD_digest_auth_get_username.
  2553. @var{password} must reference to a zero-terminated string representing the password,
  2554. most probably it will be the result of a lookup of the username against a local database.
  2555. @var{nonce_timeout} the nonce validity duration in seconds.
  2556. Most of the time it is sound to specify 300 seconds as its values.
  2557. @var{algo} which digest algorithm should we use.
  2558. @end deftypefun
  2559. @deftypefun int MHD_digest_auth_check2 (struct MHD_Connection *connection, const char *realm, const char *username, const char *password, unsigned int nonce_timeout, enum MHD_DigestAuthAlgorithm algo)
  2560. Checks if the provided values in the WWW-Authenticate header are valid
  2561. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  2562. @var{realm} must reference to a zero-terminated string representing the realm.
  2563. @var{username} must reference to a zero-terminated string representing the username,
  2564. it is usually the returned value from MHD_digest_auth_get_username.
  2565. @var{password} must reference to a zero-terminated string representing the password,
  2566. most probably it will be the result of a lookup of the username against a local database.
  2567. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  2568. Most of the time it is sound to specify 300 seconds as its values.
  2569. @var{algo} which digest algorithm should we use.
  2570. @end deftypefun
  2571. @deftypefun int MHD_digest_auth_check (struct MHD_Connection *connection, const char *realm, const char *username, const char *password, unsigned int nonce_timeout)
  2572. Checks if the provided values in the WWW-Authenticate header are valid
  2573. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  2574. Deprecated, use @code{MHD_digest_auth_check2} instead.
  2575. @var{realm} must reference to a zero-terminated string representing the realm.
  2576. @var{username} must reference to a zero-terminated string representing the username,
  2577. it is usually the returned value from MHD_digest_auth_get_username.
  2578. @var{password} must reference to a zero-terminated string representing the password,
  2579. most probably it will be the result of a lookup of the username against a local database.
  2580. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  2581. Most of the time it is sound to specify 300 seconds as its values.
  2582. @end deftypefun
  2583. @deftypefun enum MHD_DigestAuthResult MHD_digest_auth_check_digest3 (struct MHD_Connection *connection, const char *realm, const char *username, const uint8_t *digest, unsigned int nonce_timeout, enum MHD_DigestAuthAlgorithm algo)
  2584. Checks if the provided values in the WWW-Authenticate header are valid
  2585. and sound according to RFC7616. If valid return @code{MHD_DAUTH_OK}, otherwise return the error code.
  2586. @var{realm} must reference to a zero-terminated string representing the realm.
  2587. @var{username} must reference to a zero-terminated string representing the username,
  2588. it is usually the returned value from MHD_digest_auth_get_username.
  2589. @var{digest} the pointer to the binary digest for the precalculated hash value ``username:realm:password'' with specified @var{algo}.
  2590. @var{digest_size} the number of bytes in @var{digest} (the size must match @var{algo}!)
  2591. @var{nonce_timeout} the nonce validity duration in seconds.
  2592. Most of the time it is sound to specify 300 seconds as its values.
  2593. @var{algo} digest authentication algorithm to use.
  2594. @end deftypefun
  2595. @deftypefun int MHD_digest_auth_check_digest2 (struct MHD_Connection *connection, const char *realm, const char *username, const uint8_t *digest, unsigned int nonce_timeout, enum MHD_DigestAuthAlgorithm algo)
  2596. Checks if the provided values in the WWW-Authenticate header are valid
  2597. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  2598. @var{realm} must reference to a zero-terminated string representing the realm.
  2599. @var{username} must reference to a zero-terminated string representing the username,
  2600. it is usually the returned value from MHD_digest_auth_get_username.
  2601. @var{digest} pointer to the binary MD5 sum for the precalculated hash value ``userame:realm:password''. The size must match the selected @var{algo}!
  2602. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  2603. Most of the time it is sound to specify 300 seconds as its values.
  2604. @var{algo} digest authentication algorithm to use.
  2605. @end deftypefun
  2606. @deftypefun int MHD_digest_auth_check_digest (struct MHD_Connection *connection, const char *realm, const char *username, const unsigned char digest[MHD_MD5_DIGEST_SIZE], unsigned int nonce_timeout)
  2607. Checks if the provided values in the WWW-Authenticate header are valid
  2608. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  2609. Deprecated, use @code{MHD_digest_auth_check_digest2} instead.
  2610. @var{realm} must reference to a zero-terminated string representing the realm.
  2611. @var{username} must reference to a zero-terminated string representing the username,
  2612. it is usually the returned value from MHD_digest_auth_get_username.
  2613. @var{digest} pointer to the binary MD5 sum for the precalculated hash value ``userame:realm:password'' of @code{MHD_MD5_DIGEST_SIZE} bytes.
  2614. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  2615. Most of the time it is sound to specify 300 seconds as its values.
  2616. @end deftypefun
  2617. @deftypefun enum MHD_Result MHD_queue_auth_fail_response2 (struct MHD_Connection *connection, const char *realm, const char *opaque, struct MHD_Response *response, int signal_stale, enum MHD_DigestAuthAlgorithm algo)
  2618. Queues a response to request authentication from the client,
  2619. return @code{MHD_YES} if successful, otherwise @code{MHD_NO}.
  2620. @var{realm} must reference to a zero-terminated string representing the realm.
  2621. @var{opaque} must reference to a zero-terminated string representing a value
  2622. that gets passed to the client and expected to be passed again to the server
  2623. as-is. This value can be a hexadecimal or base64 string.
  2624. @var{response} a response structure to specify what shall be presented to the
  2625. client with a 401 HTTP status.
  2626. @var{signal_stale} a value that signals "stale=true" in the response header to
  2627. indicate the invalidity of the nonce and no need to ask for authentication
  2628. parameters and only a new nonce gets generated. @code{MHD_YES} to generate a new
  2629. nonce, @code{MHD_NO} to ask for authentication parameters.
  2630. @var{algo} which digest algorithm should we use. The same algorithm
  2631. must then be selected when checking digests received from clients!
  2632. @end deftypefun
  2633. @deftypefun enum MHD_Result MHD_queue_auth_fail_response (struct MHD_Connection *connection, const char *realm, const char *opaque, struct MHD_Response *response, int signal_stale)
  2634. Queues a response to request authentication from the client,
  2635. return @code{MHD_YES} if successful, otherwise @code{MHD_NO}.
  2636. @var{realm} must reference to a zero-terminated string representing the realm.
  2637. @var{opaque} must reference to a zero-terminated string representing a value
  2638. that gets passed to the client and expected to be passed again to the server
  2639. as-is. This value can be a hexadecimal or base64 string.
  2640. @var{response} a response structure to specify what shall be presented to the
  2641. client with a 401 HTTP status.
  2642. @var{signal_stale} a value that signals "stale=true" in the response header to
  2643. indicate the invalidity of the nonce and no need to ask for authentication
  2644. parameters and only a new nonce gets generated. @code{MHD_YES} to generate a new
  2645. nonce, @code{MHD_NO} to ask for authentication parameters.
  2646. @end deftypefun
  2647. Example: handling digest authentication requests and responses.
  2648. @example
  2649. #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>"
  2650. #define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>"
  2651. #define OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
  2652. static int
  2653. ahc_echo (void *cls,
  2654. struct MHD_Connection *connection,
  2655. const char *url,
  2656. const char *method,
  2657. const char *version,
  2658. const char *upload_data, size_t *upload_data_size, void **ptr)
  2659. @{
  2660. struct MHD_Response *response;
  2661. char *username;
  2662. const char *password = "testpass";
  2663. const char *realm = "test@@example.com";
  2664. int ret;
  2665. static int already_called_marker;
  2666. if (&already_called_marker != *req_cls)
  2667. @{ /* Called for the first time, request not fully read yet */
  2668. *req_cls = &already_called_marker;
  2669. /* Wait for complete request */
  2670. return MHD_YES;
  2671. @}
  2672. username = MHD_digest_auth_get_username (connection);
  2673. if (username == NULL)
  2674. @{
  2675. response = MHD_create_response_from_buffer(strlen (DENIED),
  2676. DENIED,
  2677. MHD_RESPMEM_PERSISTENT);
  2678. ret = MHD_queue_auth_fail_response2 (connection,
  2679. realm,
  2680. OPAQUE,
  2681. response,
  2682. MHD_NO,
  2683. MHD_DIGEST_ALG_SHA256);
  2684. MHD_destroy_response(response);
  2685. return ret;
  2686. @}
  2687. ret = MHD_digest_auth_check2 (connection,
  2688. realm,
  2689. username,
  2690. password,
  2691. 300,
  2692. MHD_DIGEST_ALG_SHA256);
  2693. MHD_free(username);
  2694. if ( (ret == MHD_INVALID_NONCE) ||
  2695. (ret == MHD_NO) )
  2696. @{
  2697. response = MHD_create_response_from_buffer(strlen (DENIED),
  2698. DENIED,
  2699. MHD_RESPMEM_PERSISTENT);
  2700. if (NULL == response)
  2701. return MHD_NO;
  2702. ret = MHD_queue_auth_fail_response2 (connection,
  2703. realm,
  2704. OPAQUE,
  2705. response,
  2706. (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO,
  2707. MHD_DIGEST_ALG_SHA256);
  2708. MHD_destroy_response(response);
  2709. return ret;
  2710. @}
  2711. response = MHD_create_response_from_buffer (strlen(PAGE),
  2712. PAGE,
  2713. MHD_RESPMEM_PERSISTENT);
  2714. ret = MHD_queue_response (connection,
  2715. MHD_HTTP_OK,
  2716. response);
  2717. MHD_destroy_response(response);
  2718. return ret;
  2719. @}
  2720. @end example
  2721. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2722. @c ------------------------------------------------------------
  2723. @node microhttpd-post
  2724. @chapter Adding a @code{POST} processor
  2725. @cindex POST method
  2726. @menu
  2727. * microhttpd-post api:: Programming interface for the
  2728. @code{POST} processor.
  2729. @end menu
  2730. @noindent
  2731. MHD provides the post processor API to make it easier for applications to
  2732. parse the data of a client's @code{POST} request: the
  2733. @code{MHD_AccessHandlerCallback} will be invoked multiple times to
  2734. process data as it arrives; at each invocation a new chunk of data must
  2735. be processed. The arguments @var{upload_data} and @var{upload_data_size}
  2736. are used to reference the chunk of data.
  2737. When @code{MHD_AccessHandlerCallback} is invoked for a new request:
  2738. its @code{*@var{req_cls}} argument is set to @code{NULL}. When @code{POST}
  2739. data comes in the upload buffer it is @strong{mandatory} to use the
  2740. @var{req_cls} to store a reference to per-request data. The fact
  2741. that the pointer was initially @code{NULL} can be used to detect that
  2742. this is a new request.
  2743. One method to detect that a new request was started is
  2744. to set @code{*req_cls} to an unused integer:
  2745. @example
  2746. int
  2747. access_handler (void *cls,
  2748. struct MHD_Connection * connection,
  2749. const char *url,
  2750. const char *method, const char *version,
  2751. const char *upload_data, size_t *upload_data_size,
  2752. void **req_cls)
  2753. @{
  2754. static int old_connection_marker;
  2755. int new_connection = (NULL == *req_cls);
  2756. if (new_connection)
  2757. @{
  2758. /* new connection with POST */
  2759. *req_cls = &old_connection_marker;
  2760. @}
  2761. ...
  2762. @}
  2763. @end example
  2764. @noindent
  2765. In contrast to the previous example, for @code{POST} requests in particular,
  2766. it is more common to use the value of @code{*req_cls} to keep track of
  2767. actual state used during processing, such as the post processor (or a
  2768. struct containing a post processor):
  2769. @example
  2770. int
  2771. access_handler (void *cls,
  2772. struct MHD_Connection * connection,
  2773. const char *url,
  2774. const char *method, const char *version,
  2775. const char *upload_data, size_t *upload_data_size,
  2776. void **req_cls)
  2777. @{
  2778. struct MHD_PostProcessor * pp = *req_cls;
  2779. if (pp == NULL)
  2780. @{
  2781. pp = MHD_create_post_processor(connection, ...);
  2782. *req_cls = pp;
  2783. return MHD_YES;
  2784. @}
  2785. if (*upload_data_size)
  2786. @{
  2787. MHD_post_process(pp, upload_data, *upload_data_size);
  2788. *upload_data_size = 0;
  2789. return MHD_YES;
  2790. @}
  2791. else
  2792. @{
  2793. MHD_destroy_post_processor(pp);
  2794. return MHD_queue_response(...);
  2795. @}
  2796. @}
  2797. @end example
  2798. Note that the callback from @code{MHD_OPTION_NOTIFY_COMPLETED}
  2799. should be used to destroy the post processor. This cannot be
  2800. done inside of the access handler since the connection may not
  2801. always terminate normally.
  2802. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2803. @c ------------------------------------------------------------
  2804. @node microhttpd-post api
  2805. @section Programming interface for the @code{POST} processor
  2806. @cindex POST method
  2807. @deftypefun {struct MHD_PostProcessor *} MHD_create_post_processor (struct MHD_Connection *connection, size_t buffer_size, MHD_PostDataIterator iterator, void *iterator_cls)
  2808. Create a PostProcessor. A PostProcessor can be used to (incrementally)
  2809. parse the data portion of a @code{POST} request.
  2810. @table @var
  2811. @item connection
  2812. the connection on which the @code{POST} is happening (used to determine
  2813. the @code{POST} format);
  2814. @item buffer_size
  2815. maximum number of bytes to use for internal buffering (used only for the
  2816. parsing, specifically the parsing of the keys). A tiny value (256-1024)
  2817. should be sufficient; do @strong{NOT} use a value smaller than 256;
  2818. for good performance, use 32k or 64k (i.e. 65536).
  2819. @item iterator
  2820. iterator to be called with the parsed data; must @strong{NOT} be
  2821. @code{NULL};
  2822. @item iterator_cls
  2823. custom value to be used as first argument to @var{iterator}.
  2824. @end table
  2825. Return @code{NULL} on error (out of memory, unsupported encoding), otherwise
  2826. a PP handle.
  2827. @end deftypefun
  2828. @deftypefun enum MHD_Result MHD_post_process (struct MHD_PostProcessor *pp, const char *post_data, size_t post_data_len)
  2829. Parse and process @code{POST} data. Call this function when @code{POST}
  2830. data is available (usually during an @code{MHD_AccessHandlerCallback})
  2831. with the @var{upload_data} and @var{upload_data_size}. Whenever
  2832. possible, this will then cause calls to the
  2833. @code{MHD_IncrementalKeyValueIterator}.
  2834. @table @var
  2835. @item pp
  2836. the post processor;
  2837. @item post_data
  2838. @var{post_data_len} bytes of @code{POST} data;
  2839. @item post_data_len
  2840. length of @var{post_data}.
  2841. @end table
  2842. Return @code{MHD_YES} on success, @code{MHD_NO} on error
  2843. (out-of-memory, iterator aborted, parse error).
  2844. @end deftypefun
  2845. @deftypefun enum MHD_Result MHD_destroy_post_processor (struct MHD_PostProcessor *pp)
  2846. Release PostProcessor resources. After this function is being called,
  2847. the PostProcessor is guaranteed to no longer call its iterator. There
  2848. is no special call to the iterator to indicate the end of the post processing
  2849. stream. After destroying the PostProcessor, the programmer should
  2850. perform any necessary work to complete the processing of the iterator.
  2851. Return @code{MHD_YES} if processing completed nicely, @code{MHD_NO}
  2852. if there were spurious characters or formatting problems with
  2853. the post request. It is common to ignore the return value
  2854. of this function.
  2855. @end deftypefun
  2856. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2857. @c ------------------------------------------------------------
  2858. @node microhttpd-info
  2859. @chapter Obtaining and modifying status information.
  2860. @menu
  2861. * microhttpd-info daemon:: State information about an MHD daemon
  2862. * microhttpd-info conn:: State information about a connection
  2863. * microhttpd-option conn:: Modify per-connection options
  2864. @end menu
  2865. @c ------------------------------------------------------------
  2866. @node microhttpd-info daemon
  2867. @section Obtaining state information about an MHD daemon
  2868. @deftypefun {const union MHD_DaemonInfo *} MHD_get_daemon_info (struct MHD_Daemon *daemon, enum MHD_DaemonInfoType infoType, ...)
  2869. Obtain information about the given daemon. This function
  2870. is currently not fully implemented.
  2871. @table @var
  2872. @item daemon
  2873. the daemon about which information is desired;
  2874. @item infoType
  2875. type of information that is desired
  2876. @item ...
  2877. additional arguments about the desired information (depending on
  2878. infoType)
  2879. @end table
  2880. Returns a union with the respective member (depending on
  2881. infoType) set to the desired information), or @code{NULL}
  2882. in case the desired information is not available or
  2883. applicable.
  2884. @end deftypefun
  2885. @deftp {Enumeration} MHD_DaemonInfoType
  2886. Values of this enum are used to specify what
  2887. information about a daemon is desired.
  2888. @table @code
  2889. @item MHD_DAEMON_INFO_KEY_SIZE
  2890. Request information about the key size for a particular cipher
  2891. algorithm. The cipher algorithm should be passed as an extra argument
  2892. (of type 'enum MHD_GNUTLS_CipherAlgorithm'). No longer supported,
  2893. using this value will cause @code{MHD_get_daemon_info} to return NULL.
  2894. @item MHD_DAEMON_INFO_MAC_KEY_SIZE
  2895. Request information about the key size for a particular cipher
  2896. algorithm. The cipher algorithm should be passed as an extra argument
  2897. (of type 'enum MHD_GNUTLS_HashAlgorithm'). No longer supported,
  2898. using this value will cause @code{MHD_get_daemon_info} to return NULL.
  2899. @item MHD_DAEMON_INFO_LISTEN_FD
  2900. @cindex listen
  2901. Request the file-descriptor number that MHD is using to listen to the
  2902. server socket. This can be useful if no port
  2903. was specified and a client needs to learn what port
  2904. is actually being used by MHD.
  2905. No extra arguments should be passed.
  2906. @item MHD_DAEMON_INFO_EPOLL_FD
  2907. @cindex epoll
  2908. Request the file-descriptor number that MHD is using for epoll. If
  2909. the build is not supporting epoll, NULL is returned; if we are using a
  2910. thread pool or this daemon was not started with
  2911. @code{MHD_USE_EPOLL}, (a pointer to) -1 is returned. If we are
  2912. using @code{MHD_USE_INTERNAL_POLLING_THREAD} or are in 'external' select mode, the
  2913. internal epoll FD is returned. This function must be used in external
  2914. select mode with epoll to obtain the FD to call epoll on. No extra
  2915. arguments should be passed.
  2916. @item MHD_DAEMON_INFO_CURRENT_CONNECTIONS
  2917. @cindex connection, limiting number of connections
  2918. Request the number of current connections handled by the daemon. No
  2919. extra arguments should be passed and a pointer to a @code{union
  2920. MHD_DaemonInfo} value is returned, with the @code{num_connections}
  2921. member of type @code{unsigned int} set to the number of active
  2922. connections.
  2923. Note that in multi-threaded or internal-select mode, the real number of current
  2924. connections may already be different when @code{MHD_get_daemon_info} returns.
  2925. The number of current connections can be used (even in multi-threaded and
  2926. internal-select mode) after @code{MHD_quiesce_daemon} to detect whether all
  2927. connections have been handled.
  2928. @end table
  2929. @end deftp
  2930. @c ------------------------------------------------------------
  2931. @node microhttpd-info conn
  2932. @section Obtaining state information about a connection
  2933. @deftypefun {const union MHD_ConnectionInfo *} MHD_get_connection_info (struct MHD_Connection *connection, enum MHD_ConnectionInfoType infoType, ...)
  2934. Obtain information about the given connection.
  2935. @table @var
  2936. @item connection
  2937. the connection about which information is desired;
  2938. @item infoType
  2939. type of information that is desired
  2940. @item ...
  2941. additional arguments about the desired information (depending on
  2942. infoType)
  2943. @end table
  2944. Returns a union with the respective member (depending on
  2945. infoType) set to the desired information), or @code{NULL}
  2946. in case the desired information is not available or
  2947. applicable.
  2948. @end deftypefun
  2949. @deftp {Enumeration} MHD_ConnectionInfoType
  2950. Values of this enum are used to specify what information about a
  2951. connection is desired.
  2952. @table @code
  2953. @item MHD_CONNECTION_INFO_CIPHER_ALGO
  2954. What cipher algorithm is being used (HTTPS connections only).
  2955. @code{NULL} is returned for non-HTTPS connections.
  2956. Takes no extra arguments.
  2957. @item MHD_CONNECTION_INFO_PROTOCOL,
  2958. Allows finding out the TLS/SSL protocol used
  2959. (HTTPS connections only).
  2960. @code{NULL} is returned for non-HTTPS connections.
  2961. Takes no extra arguments.
  2962. @item MHD_CONNECTION_INFO_CLIENT_ADDRESS
  2963. Returns information about the address of the client. Returns
  2964. essentially a @code{struct sockaddr **} (since the API returns
  2965. a @code{union MHD_ConnectionInfo *} and that union contains
  2966. a @code{struct sockaddr *}).
  2967. Takes no extra arguments.
  2968. @item MHD_CONNECTION_INFO_GNUTLS_SESSION,
  2969. Takes no extra arguments. Allows access to the underlying GNUtls session,
  2970. including access to the underlying GNUtls client certificate
  2971. (HTTPS connections only). Takes no extra arguments.
  2972. @code{NULL} is returned for non-HTTPS connections.
  2973. Takes no extra arguments.
  2974. @item MHD_CONNECTION_INFO_GNUTLS_CLIENT_CERT,
  2975. Dysfunctional (never implemented, deprecated). Use
  2976. MHD_CONNECTION_INFO_GNUTLS_SESSION to get the @code{gnutls_session_t}
  2977. and then call @code{gnutls_certificate_get_peers()}.
  2978. @item MHD_CONNECTION_INFO_DAEMON
  2979. Returns information about @code{struct MHD_Daemon} which manages
  2980. this connection.
  2981. Takes no extra arguments.
  2982. @item MHD_CONNECTION_INFO_CONNECTION_FD
  2983. Returns the file descriptor (usually a TCP socket) associated with
  2984. this connection (in the ``connect-fd'' member of the returned struct).
  2985. Note that manipulating the descriptor directly can have problematic
  2986. consequences (as in, break HTTP). Applications might use this access
  2987. to manipulate TCP options, for example to set the ``TCP-NODELAY''
  2988. option for COMET-like applications. Note that MHD will set TCP-CORK
  2989. after sending the HTTP header and clear it after finishing the footers
  2990. automatically (if the platform supports it). As the connection
  2991. callbacks are invoked in between, those might be used to set different
  2992. values for TCP-CORK and TCP-NODELAY in the meantime.
  2993. Takes no extra arguments.
  2994. @item MHD_CONNECTION_INFO_CONNECTION_SUSPENDED
  2995. Returns pointer to an integer that is @code{MHD_YES} if the connection
  2996. is currently suspended (and thus can be safely resumed) and
  2997. @code{MHD_NO} otherwise.
  2998. Takes no extra arguments.
  2999. @item MHD_CONNECTION_INFO_SOCKET_CONTEXT
  3000. Returns the client-specific pointer to a @code{void *} that was
  3001. (possibly) set during a @code{MHD_NotifyConnectionCallback} when the
  3002. socket was first accepted. Note that this is NOT the same as the
  3003. @code{req_cls} argument of the @code{MHD_AccessHandlerCallback}. The
  3004. @code{req_cls} is fresh for each HTTP request, while the
  3005. @code{socket_context} is fresh for each socket.
  3006. Takes no extra arguments.
  3007. @item MHD_CONNECTION_INFO_CONNECTION_TIMEOUT
  3008. Returns pointer to an @code{unsigned int} that is the current timeout
  3009. used for the connection (in seconds, 0 for no timeout). Note that
  3010. while suspended connections will not timeout, the timeout value
  3011. returned for suspended connections will be the timeout that the
  3012. connection will use after it is resumed, and thus might not be zero.
  3013. Takes no extra arguments.
  3014. @item MHD_CONNECTION_INFO_REQUEST_HEADER_SIZE
  3015. @cindex performance
  3016. Returns pointer to an @code{size_t} that represents the size of the
  3017. HTTP header received from the client. Only valid after the first callback
  3018. to the access handler.
  3019. Takes no extra arguments.
  3020. @item MHD_CONNECTION_INFO_HTTP_STATUS
  3021. Returns the HTTP status code of the response that was
  3022. queued. Returns NULL if no response was queued yet.
  3023. Takes no extra arguments.
  3024. @end table
  3025. @end deftp
  3026. @c ------------------------------------------------------------
  3027. @node microhttpd-option conn
  3028. @section Setting custom options for an individual connection
  3029. @cindex timeout
  3030. @deftypefun {int} MHD_set_connection_option (struct MHD_Connection *daemon, enum MHD_CONNECTION_OPTION option, ...)
  3031. Set a custom option for the given connection.
  3032. @table @var
  3033. @item connection
  3034. the connection for which an option should be set or modified;
  3035. @item option
  3036. option to set
  3037. @item ...
  3038. additional arguments for the option (depending on option)
  3039. @end table
  3040. Returns @code{MHD_YES} on success, @code{MHD_NO} for errors
  3041. (i.e. option argument invalid or option unknown).
  3042. @end deftypefun
  3043. @deftp {Enumeration} MHD_CONNECTION_OPTION
  3044. Values of this enum are used to specify which option for a
  3045. connection should be changed.
  3046. @table @code
  3047. @item MHD_CONNECTION_OPTION_TIMEOUT
  3048. Set a custom timeout for the given connection. Specified
  3049. as the number of seconds, given as an @code{unsigned int}. Use
  3050. zero for no timeout.
  3051. @end table
  3052. @end deftp
  3053. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3054. @c ------------------------------------------------------------
  3055. @node microhttpd-util
  3056. @chapter Utility functions.
  3057. @menu
  3058. * microhttpd-util feature:: Test supported MHD features
  3059. * microhttpd-util unescape:: Unescape strings
  3060. @end menu
  3061. @c ------------------------------------------------------------
  3062. @node microhttpd-util feature
  3063. @section Testing for supported MHD features
  3064. @deftp {Enumeration} MHD_FEATURE
  3065. Values of this enum are used to specify what
  3066. information about a daemon is desired.
  3067. @table @code
  3068. @item MHD_FEATURE_MESSAGES
  3069. Get whether messages are supported. If supported then in debug
  3070. mode messages can be printed to stderr or to external logger.
  3071. @item MHD_FEATURE_SSL
  3072. Get whether HTTPS is supported. If supported then flag
  3073. MHD_USE_SSL and options MHD_OPTION_HTTPS_MEM_KEY,
  3074. MHD_OPTION_HTTPS_MEM_CERT, MHD_OPTION_HTTPS_MEM_TRUST,
  3075. MHD_OPTION_HTTPS_MEM_DHPARAMS, MHD_OPTION_HTTPS_CRED_TYPE,
  3076. MHD_OPTION_HTTPS_PRIORITIES can be used.
  3077. @item MHD_FEATURE_HTTPS_CERT_CALLBACK
  3078. Get whether option #MHD_OPTION_HTTPS_CERT_CALLBACK is
  3079. supported.
  3080. @item MHD_FEATURE_IPv6
  3081. Get whether IPv6 is supported. If supported then flag
  3082. MHD_USE_IPv6 can be used.
  3083. @item MHD_FEATURE_IPv6_ONLY
  3084. Get whether IPv6 without IPv4 is supported. If not supported
  3085. then IPv4 is always enabled in IPv6 sockets and
  3086. flag MHD_USE_DUAL_STACK if always used when MHD_USE_IPv6 is
  3087. specified.
  3088. @item MHD_FEATURE_POLL
  3089. Get whether @code{poll()} is supported. If supported then flag
  3090. MHD_USE_POLL can be used.
  3091. @item MHD_FEATURE_EPOLL
  3092. Get whether @code{epoll()} is supported. If supported then Flags
  3093. MHD_USE_EPOLL and
  3094. MHD_USE_EPOLL_INTERNAL_THREAD can be used.
  3095. @item MHD_FEATURE_SHUTDOWN_LISTEN_SOCKET
  3096. Get whether shutdown on listen socket to signal other
  3097. threads is supported. If not supported flag
  3098. MHD_USE_ITC is automatically forced.
  3099. @item MHD_FEATURE_SOCKETPAIR
  3100. Get whether a @code{socketpair()} is used internally instead of
  3101. a @code{pipe()} to signal other threads.
  3102. @item MHD_FEATURE_TCP_FASTOPEN
  3103. Get whether TCP Fast Open is supported. If supported then
  3104. flag MHD_USE_TCP_FASTOPEN and option
  3105. MHD_OPTION_TCP_FASTOPEN_QUEUE_SIZE can be used.
  3106. @item MHD_FEATURE_BASIC_AUTH
  3107. Get whether HTTP Basic authorization is supported. If supported
  3108. then functions @code{MHD_basic_auth_get_username_password()} and
  3109. @code{MHD_queue_basic_auth_fail_response()} can be used.
  3110. @item MHD_FEATURE_DIGEST_AUTH
  3111. Get whether HTTP Digest authorization is supported. If
  3112. supported then options MHD_OPTION_DIGEST_AUTH_RANDOM,
  3113. MHD_OPTION_NONCE_NC_SIZE and functions @code{MHD_digest_auth_check()},
  3114. can be used.
  3115. @item MHD_FEATURE_POSTPROCESSOR
  3116. Get whether postprocessor is supported. If supported then
  3117. functions @code{MHD_create_post_processor()},
  3118. @code{MHD_post_process()}, @code{MHD_destroy_post_processor()}
  3119. can be used.
  3120. @item MHD_FEATURE_SENDFILE
  3121. Get whether @code{sendfile()} is supported.
  3122. @end table
  3123. @end deftp
  3124. @deftypefun {int} MHD_is_feature_supported (enum MHD_FEATURE feature)
  3125. Get information about supported MHD features. Indicate that MHD was
  3126. compiled with or without support for particular feature. Some features
  3127. require additional support by the kernel. However, kernel support is not
  3128. checked by this function.
  3129. @table @var
  3130. @item feature
  3131. type of requested information
  3132. @end table
  3133. Returns @code{MHD_YES} if the feature is supported,
  3134. and @code{MHD_NO} if not.
  3135. @end deftypefun
  3136. @c ------------------------------------------------------------
  3137. @node microhttpd-util unescape
  3138. @section Unescape strings
  3139. @deftypefun {size_t} MHD_http_unescape (char *val)
  3140. Process escape sequences ('%HH') Updates val in place; the result
  3141. should be UTF-8 encoded and cannot be larger than the input. The
  3142. result must also still be 0-terminated.
  3143. @table @var
  3144. @item val
  3145. value to unescape (modified in the process), must be
  3146. a 0-terminated UTF-8 string.
  3147. @end table
  3148. Returns length of the resulting val (@code{strlen(val)} may be
  3149. shorter afterwards due to elimination of escape sequences).
  3150. @end deftypefun
  3151. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3152. @c ------------------------------------------------------------
  3153. @node microhttpd-websocket
  3154. @chapter Websocket functions.
  3155. @noindent
  3156. Websocket functions provide what you need to use an upgraded connection
  3157. as a websocket.
  3158. These functions are only available if you include the header file
  3159. @code{microhttpd_ws.h} and compiled @emph{libmicrohttpd} with websockets.
  3160. @menu
  3161. * microhttpd-websocket handshake:: Websocket handshake functions
  3162. * microhttpd-websocket stream:: Websocket stream functions
  3163. * microhttpd-websocket decode:: Websocket decode functions
  3164. * microhttpd-websocket encode:: Websocket encode functions
  3165. * microhttpd-websocket memory:: Websocket memory functions
  3166. @end menu
  3167. @c ------------------------------------------------------------
  3168. @node microhttpd-websocket handshake
  3169. @section Websocket handshake functions
  3170. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_check_http_version (const char* http_version)
  3171. @cindex websocket
  3172. Checks the HTTP version of the incoming request.
  3173. Websocket requests are only allowed for HTTP/1.1 or above.
  3174. @table @var
  3175. @item http_version
  3176. The value of the @code{version} parameter of your
  3177. @code{access_handler} callback.
  3178. If you pass @code{NULL} then this is handled like a not
  3179. matching HTTP version.
  3180. @end table
  3181. Returns 0 when the HTTP version is
  3182. valid for a websocket request and
  3183. a value less than zero when the HTTP version isn't
  3184. valid for a websocket request.
  3185. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3186. @end deftypefun
  3187. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_check_connection_header (const char* connection_header)
  3188. @cindex websocket
  3189. Checks the value of the @code{Connection} HTTP request header.
  3190. Websocket requests require the token @code{Upgrade} in
  3191. the @code{Connection} HTTP request header.
  3192. @table @var
  3193. @item connection_header
  3194. Value of the @code{Connection} request header.
  3195. You can get this request header value by passing
  3196. @code{MHD_HTTP_HEADER_CONNECTION} to
  3197. @code{MHD_lookup_connection_value()}.
  3198. If you pass @code{NULL} then this is handled like a not
  3199. matching @code{Connection} header value.
  3200. @end table
  3201. Returns 0 when the @code{Connection} header is
  3202. valid for a websocket request and
  3203. a value less than zero when the @code{Connection} header isn't
  3204. valid for a websocket request.
  3205. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3206. @end deftypefun
  3207. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_check_upgrade_header (const char* upgrade_header)
  3208. @cindex websocket
  3209. Checks the value of the @code{Upgrade} HTTP request header.
  3210. Websocket requests require the value @code{websocket} in
  3211. the @code{Upgrade} HTTP request header.
  3212. @table @var
  3213. @item upgrade_header
  3214. Value of the @code{Upgrade} request header.
  3215. You can get this request header value by passing
  3216. @code{MHD_HTTP_HEADER_UPGRADE} to
  3217. @code{MHD_lookup_connection_value()}.
  3218. If you pass @code{NULL} then this is handled like a not
  3219. matching @code{Upgrade} header value.
  3220. @end table
  3221. Returns 0 when the @code{Upgrade} header is
  3222. valid for a websocket request and
  3223. a value less than zero when the @code{Upgrade} header isn't
  3224. valid for a websocket request.
  3225. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3226. @end deftypefun
  3227. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_check_version_header (const char* version_header)
  3228. @cindex websocket
  3229. Checks the value of the @code{Sec-WebSocket-Version} HTTP request header.
  3230. Websocket requests require the value @code{13} in
  3231. the @code{Sec-WebSocket-Version} HTTP request header.
  3232. @table @var
  3233. @item version_header
  3234. Value of the @code{Sec-WebSocket-Version} request header.
  3235. You can get this request header value by passing
  3236. @code{MHD_HTTP_HEADER_SEC_WEBSOCKET_VERSION} to
  3237. @code{MHD_lookup_connection_value()}.
  3238. If you pass @code{NULL} then this is handled like a not
  3239. matching @code{Sec-WebSocket-Version} header value.
  3240. @end table
  3241. Returns 0 when the @code{Sec-WebSocket-Version} header is
  3242. valid for a websocket request and
  3243. a value less than zero when the @code{Sec-WebSocket-Version} header isn't
  3244. valid for a websocket request.
  3245. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3246. @end deftypefun
  3247. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_create_accept_header (const char* sec_websocket_key, char* sec_websocket_accept)
  3248. @cindex websocket
  3249. Checks the value of the @code{Sec-WebSocket-Key}
  3250. HTTP request header and generates the value for
  3251. the @code{Sec-WebSocket-Accept} HTTP response header.
  3252. The generated value must be sent to the client.
  3253. @table @var
  3254. @item sec_websocket_key
  3255. Value of the @code{Sec-WebSocket-Key} request header.
  3256. You can get this request header value by passing
  3257. @code{MHD_HTTP_HEADER_SEC_WEBSOCKET_KEY} to
  3258. @code{MHD_lookup_connection_value()}.
  3259. If you pass @code{NULL} then this is handled like a not
  3260. matching @code{Sec-WebSocket-Key} header value.
  3261. @item sec_websocket_accept
  3262. Response buffer, which will receive
  3263. the generated value for the @code{Sec-WebSocket-Accept}
  3264. HTTP response header.
  3265. This buffer must be at least 29 bytes long and
  3266. will contain the response value plus a terminating @code{NUL}
  3267. character on success.
  3268. Must not be @code{NULL}.
  3269. You can add this HTTP header to your response by passing
  3270. @code{MHD_HTTP_HEADER_SEC_WEBSOCKET_ACCEPT} to
  3271. @code{MHD_add_response_header()}.
  3272. @end table
  3273. Returns 0 when the @code{Sec-WebSocket-Key} header was
  3274. not empty and a result value for the @code{Sec-WebSocket-Accept}
  3275. was calculated.
  3276. A value less than zero is returned when the @code{Sec-WebSocket-Key}
  3277. header isn't valid for a websocket request or when any
  3278. error occurred.
  3279. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3280. @end deftypefun
  3281. @c ------------------------------------------------------------
  3282. @node microhttpd-websocket stream
  3283. @section Websocket stream functions
  3284. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_stream_init (struct MHD_WebSocketStream **ws, int flags, size_t max_payload_size)
  3285. @cindex websocket
  3286. Creates a new websocket stream, used for decoding/encoding.
  3287. @table @var
  3288. @item ws
  3289. pointer a variable to fill with the newly created
  3290. @code{struct MHD_WebSocketStream},
  3291. receives @code{NULL} on error. May not be @code{NULL}.
  3292. If not required anymore, free the created websocket stream with
  3293. @code{MHD_websocket_stream_free()}.
  3294. @item flags
  3295. combination of @code{enum MHD_WEBSOCKET_FLAG} values to
  3296. modify the behavior of the websocket stream.
  3297. @item max_payload_size
  3298. maximum size for incoming payload data in bytes. Use 0 to allow each size.
  3299. @end table
  3300. Returns 0 on success, negative values on error.
  3301. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3302. @end deftypefun
  3303. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_stream_init2 (struct MHD_WebSocketStream **ws, int flags, size_t max_payload_size, MHD_WebSocketMallocCallback callback_malloc, MHD_WebSocketReallocCallback callback_realloc, MHD_WebSocketFreeCallback callback_free, void* cls_rng, MHD_WebSocketRandomNumberGenerator callback_rng)
  3304. @cindex websocket
  3305. Creates a new websocket stream, used for decoding/encoding,
  3306. but with custom memory functions for malloc, realloc and free.
  3307. Also a random number generator can be specified for client mode.
  3308. @table @var
  3309. @item ws
  3310. pointer a variable to fill with the newly created
  3311. @code{struct MHD_WebSocketStream},
  3312. receives @code{NULL} on error. Must not be @code{NULL}.
  3313. If not required anymore, free the created websocket stream with
  3314. @code{MHD_websocket_stream_free}.
  3315. @item flags
  3316. combination of @code{enum MHD_WEBSOCKET_FLAG} values to
  3317. modify the behavior of the websocket stream.
  3318. @item max_payload_size
  3319. maximum size for incoming payload data in bytes. Use 0 to allow each size.
  3320. @item callback_malloc
  3321. callback function for allocating memory. Must not be @code{NULL}.
  3322. The shorter @code{MHD_websocket_stream_init()} passes a reference to @code{malloc} here.
  3323. @item callback_realloc
  3324. callback function for reallocating memory. Must not be @code{NULL}.
  3325. The shorter @code{MHD_websocket_stream_init()} passes a reference to @code{realloc} here.
  3326. @item callback_free
  3327. callback function for freeing memory. Must not be @code{NULL}.
  3328. The shorter @code{MHD_websocket_stream_init()} passes a reference to @code{free} here.
  3329. @item cls_rng
  3330. closure for the random number generator.
  3331. This is only required when
  3332. @code{MHD_WEBSOCKET_FLAG_CLIENT} is passed in @code{flags}.
  3333. The given value is passed to the random number generator callback.
  3334. May be @code{NULL} if not needed.
  3335. Should be @code{NULL} when you are not using @code{MHD_WEBSOCKET_FLAG_CLIENT}.
  3336. The shorter @code{MHD_websocket_stream_init} passes @code{NULL} here.
  3337. @item callback_rng
  3338. callback function for a secure random number generator.
  3339. This is only required when @code{MHD_WEBSOCKET_FLAG_CLIENT} is
  3340. passed in @code{flags} and must not be @code{NULL} then.
  3341. Should be @code{NULL} otherwise.
  3342. The shorter @code{MHD_websocket_stream_init()} passes @code{NULL} here.
  3343. @end table
  3344. Returns 0 on success, negative values on error.
  3345. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3346. @end deftypefun
  3347. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_stream_free (struct MHD_WebSocketStream *ws)
  3348. @cindex websocket
  3349. Frees a previously allocated websocket stream
  3350. @table @var
  3351. @item ws
  3352. websocket stream to free, this value may be @code{NULL}.
  3353. @end table
  3354. Returns 0 on success, negative values on error.
  3355. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3356. @end deftypefun
  3357. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_stream_invalidate (struct MHD_WebSocketStream *ws)
  3358. @cindex websocket
  3359. Invalidates a websocket stream.
  3360. After invalidation a websocket stream cannot be used for decoding anymore.
  3361. Encoding is still possible.
  3362. @table @var
  3363. @item ws
  3364. websocket stream to invalidate.
  3365. @end table
  3366. Returns 0 on success, negative values on error.
  3367. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3368. @end deftypefun
  3369. @deftypefun {enum MHD_WEBSOCKET_VALIDITY} MHD_websocket_stream_is_valid (struct MHD_WebSocketStream *ws)
  3370. @cindex websocket
  3371. Queries whether a websocket stream is valid.
  3372. Invalidated websocket streams cannot be used for decoding anymore.
  3373. Encoding is still possible.
  3374. @table @var
  3375. @item ws
  3376. websocket stream to invalidate.
  3377. @end table
  3378. Returns 0 if invalid, 1 if valid for all types or
  3379. 2 if valid only for control frames.
  3380. Can be compared with @code{enum MHD_WEBSOCKET_VALIDITY}.
  3381. @end deftypefun
  3382. @c ------------------------------------------------------------
  3383. @node microhttpd-websocket decode
  3384. @section Websocket decode functions
  3385. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_decode (struct MHD_WebSocketStream* ws, const char* streambuf, size_t streambuf_len, size_t* streambuf_read_len, char** payload, size_t* payload_len)
  3386. @cindex websocket
  3387. Decodes a byte sequence for a websocket stream.
  3388. Decoding is done until either a frame is complete or
  3389. the end of the byte sequence is reached.
  3390. @table @var
  3391. @item ws
  3392. websocket stream for decoding.
  3393. @item streambuf
  3394. byte sequence for decoding.
  3395. This is what you typically received via @code{recv()}.
  3396. @item streambuf_len
  3397. length of the byte sequence in parameter @code{streambuf}.
  3398. @item streambuf_read_len
  3399. pointer to a variable, which receives the number of bytes,
  3400. that has been processed by this call.
  3401. This value may be less than the value of @code{streambuf_len} when
  3402. a frame is decoded before the end of the buffer is reached.
  3403. The remaining bytes of @code{buf} must be passed to
  3404. the next call of this function.
  3405. @item payload
  3406. pointer to a variable, which receives the allocated buffer with the payload
  3407. data of the decoded frame. Must not be @code{NULL}.
  3408. If no decoded data is available or an error occurred @code{NULL} is returned.
  3409. When the returned value is not @code{NULL} then the buffer contains always
  3410. @code{payload_len} bytes plus one terminating @code{NUL} character
  3411. (regardless of the frame type).
  3412. The caller must free this buffer using @code{MHD_websocket_free()}.
  3413. If you passed the flag @code{MHD_WEBSOCKET_FLAG_GENERATE_CLOSE_FRAMES_ON_ERROR}
  3414. upon creation of the websocket stream and a decoding error occurred
  3415. (function return value less than 0), then this buffer contains
  3416. a generated close frame, which must be sent via the socket to the recipient.
  3417. If you passed the flag @code{MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS}
  3418. upon creation of the websocket stream then
  3419. this payload may only be a part of the complete message.
  3420. Only complete UTF-8 sequences are returned for fragmented text frames.
  3421. If necessary the UTF-8 sequence will be completed with the next text fragment.
  3422. @item payload_len
  3423. pointer to a variable, which receives length of the result
  3424. @code{payload} buffer in bytes.
  3425. Must not be @code{NULL}.
  3426. This receives 0 when no data is available, when the decoded payload
  3427. has a length of zero or when an error occurred.
  3428. @end table
  3429. Returns a value greater than zero when a frame is complete.
  3430. Compare with @code{enum MHD_WEBSOCKET_STATUS} to distinguish the frame type.
  3431. Returns 0 when the call succeeded, but no frame is available.
  3432. Returns a value less than zero on errors.
  3433. @end deftypefun
  3434. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_split_close_reason (const char* payload, size_t payload_len, unsigned short* reason_code, const char** reason_utf8, size_t* reason_utf8_len)
  3435. @cindex websocket
  3436. Splits the payload of a decoded close frame.
  3437. @table @var
  3438. @item payload
  3439. payload of the close frame.
  3440. This parameter may only be @code{NULL} if @code{payload_len} is 0.
  3441. @item payload_len
  3442. length of @code{payload}.
  3443. @item reason_code
  3444. pointer to a variable, which receives the numeric close reason.
  3445. If there was no close reason, this is 0.
  3446. This value can be compared with @code{enum MHD_WEBSOCKET_CLOSEREASON}.
  3447. May be @code{NULL}.
  3448. @item reason_utf8
  3449. pointer to a variable, which receives the literal close reason.
  3450. If there was no literal close reason, this will be @code{NULL}.
  3451. May be @code{NULL}.
  3452. Please note that no memory is allocated in this function.
  3453. If not @code{NULL} the returned value of this parameter
  3454. points to a position in the specified @code{payload}.
  3455. @item reason_utf8_len
  3456. pointer to a variable, which receives the length of the literal close reason.
  3457. If there was no literal close reason, this is 0.
  3458. May be @code{NULL}.
  3459. @end table
  3460. Returns 0 on success or a value less than zero on errors.
  3461. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3462. @end deftypefun
  3463. @c ------------------------------------------------------------
  3464. @node microhttpd-websocket encode
  3465. @section Websocket encode functions
  3466. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_encode_text (struct MHD_WebSocketStream* ws, const char* payload_utf8, size_t payload_utf8_len, int fragmentation, char** frame, size_t* frame_len, int* utf8_step)
  3467. @cindex websocket
  3468. Encodes an UTF-8 encoded text into websocket text frame
  3469. @table @var
  3470. @item ws
  3471. websocket stream;
  3472. @item payload_utf8
  3473. text to send. This must be UTF-8 encoded.
  3474. If you don't want UTF-8 then send a binary frame
  3475. with @code{MHD_websocket_encode_binary()} instead.
  3476. May be be @code{NULL} if @code{payload_utf8_len} is 0,
  3477. must not be @code{NULL} otherwise.
  3478. @item payload_utf8_len
  3479. length of @code{payload_utf8} in bytes.
  3480. @item fragmentation
  3481. A value of @code{enum MHD_WEBSOCKET_FRAGMENTATION}
  3482. to specify the fragmentation behavior.
  3483. Specify @code{MHD_WEBSOCKET_FRAGMENTATION_NONE} or just 0
  3484. if you don't want to use fragmentation (default).
  3485. @item frame
  3486. pointer to a variable, which receives a buffer with the encoded text frame.
  3487. Must not be @code{NULL}.
  3488. The buffer contains what you typically send via @code{send()} to the recipient.
  3489. If no encoded data is available the variable receives @code{NULL}.
  3490. If the variable is not @code{NULL} then the buffer contains always
  3491. @code{frame_len} bytes plus one terminating @code{NUL} character.
  3492. The caller must free this buffer using @code{MHD_websocket_free()}.
  3493. @item frame_len
  3494. pointer to a variable, which receives the length of the encoded frame in bytes.
  3495. Must not be @code{NULL}.
  3496. @item utf8_step
  3497. If fragmentation is used (the parameter @code{fragmentation} is not 0)
  3498. then is parameter is required and must not be @code{NULL}.
  3499. If no fragmentation is used, this parameter is optional and
  3500. should be @code{NULL}.
  3501. This parameter is a pointer to a variable which contains the last check status
  3502. of the UTF-8 sequence. It is required to continue a previous
  3503. UTF-8 sequence check when fragmentation is used, because a UTF-8 sequence
  3504. could be split upon fragments.
  3505. @code{enum MHD_WEBSOCKET_UTF8STEP} is used for this value.
  3506. If you start a new fragment using
  3507. @code{MHD_WEBSOCKET_FRAGMENTATION_NONE} or
  3508. @code{MHD_WEBSOCKET_FRAGMENTATION_FIRST} the old value of this variable
  3509. will be discarded and the value of this variable will be initialized
  3510. to @code{MHD_WEBSOCKET_UTF8STEP_NORMAL}.
  3511. On all other fragmentation modes the previous value of the pointed variable
  3512. will be used to continue the UTF-8 sequence check.
  3513. @end table
  3514. Returns 0 on success or a value less than zero on errors.
  3515. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3516. @end deftypefun
  3517. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_encode_binary (struct MHD_WebSocketStream* ws, const char* payload, size_t payload_len, int fragmentation, char** frame, size_t* frame_len)
  3518. @cindex websocket
  3519. Encodes binary data into websocket binary frame
  3520. @table @var
  3521. @item ws
  3522. websocket stream;
  3523. @item payload
  3524. binary data to send.
  3525. May be be @code{NULL} if @code{payload_len} is 0,
  3526. must not be @code{NULL} otherwise.
  3527. @item payload_len
  3528. length of @code{payload} in bytes.
  3529. @item fragmentation
  3530. A value of @code{enum MHD_WEBSOCKET_FRAGMENTATION}
  3531. to specify the fragmentation behavior.
  3532. Specify @code{MHD_WEBSOCKET_FRAGMENTATION_NONE} or just 0
  3533. if you don't want to use fragmentation (default).
  3534. @item frame
  3535. pointer to a variable, which receives a buffer with the encoded binary frame.
  3536. Must not be @code{NULL}.
  3537. The buffer contains what you typically send via @code{send()} to the recipient.
  3538. If no encoded data is available the variable receives @code{NULL}.
  3539. If the variable is not @code{NULL} then the buffer contains always
  3540. @code{frame_len} bytes plus one terminating @code{NUL} character.
  3541. The caller must free this buffer using @code{MHD_websocket_free()}.
  3542. @item frame_len
  3543. pointer to a variable, which receives the length of the encoded frame in bytes.
  3544. Must not be @code{NULL}.
  3545. @end table
  3546. Returns 0 on success or a value less than zero on errors.
  3547. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3548. @end deftypefun
  3549. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_encode_ping (struct MHD_WebSocketStream* ws, const char* payload, size_t payload_len, char** frame, size_t* frame_len)
  3550. @cindex websocket
  3551. Encodes a websocket ping frame.
  3552. Ping frames are used to check whether a recipient is still available
  3553. and what latency the websocket connection has.
  3554. @table @var
  3555. @item ws
  3556. websocket stream;
  3557. @item payload
  3558. binary ping data to send.
  3559. May be @code{NULL} if @code{payload_len} is 0.
  3560. @item payload_len
  3561. length of @code{payload} in bytes.
  3562. This may not exceed 125 bytes.
  3563. @item frame
  3564. pointer to a variable, which receives a buffer with the encoded ping frame.
  3565. Must not be @code{NULL}.
  3566. The buffer contains what you typically send via @code{send()} to the recipient.
  3567. If no encoded data is available the variable receives @code{NULL}.
  3568. If the variable is not @code{NULL} then the buffer contains always
  3569. @code{frame_len} bytes plus one terminating @code{NUL} character.
  3570. The caller must free this buffer using @code{MHD_websocket_free()}.
  3571. @item frame_len
  3572. pointer to a variable, which receives the length of the encoded frame in bytes.
  3573. Must not be @code{NULL}.
  3574. @end table
  3575. Returns 0 on success or a value less than zero on errors.
  3576. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3577. @end deftypefun
  3578. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_encode_pong (struct MHD_WebSocketStream* ws, const char* payload, size_t payload_len, char** frame, size_t* frame_len)
  3579. @cindex websocket
  3580. Encodes a websocket pong frame.
  3581. Pong frames are used to answer a previously received websocket ping frame.
  3582. @table @var
  3583. @item ws
  3584. websocket stream;
  3585. @item payload
  3586. binary pong data to send, which should be
  3587. the decoded payload from the received ping frame.
  3588. May be @code{NULL} if @code{payload_len} is 0.
  3589. @item payload_len
  3590. length of @code{payload} in bytes.
  3591. This may not exceed 125 bytes.
  3592. @item frame
  3593. pointer to a variable, which receives a buffer with the encoded pong frame.
  3594. Must not be @code{NULL}.
  3595. The buffer contains what you typically send via @code{send()} to the recipient.
  3596. If no encoded data is available the variable receives @code{NULL}.
  3597. If the variable is not @code{NULL} then the buffer contains always
  3598. @code{frame_len} bytes plus one terminating @code{NUL} character.
  3599. The caller must free this buffer using @code{MHD_websocket_free()}.
  3600. @item frame_len
  3601. pointer to a variable, which receives the length of the encoded frame in bytes.
  3602. Must not be @code{NULL}.
  3603. @end table
  3604. Returns 0 on success or a value less than zero on errors.
  3605. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3606. @end deftypefun
  3607. @deftypefun {enum MHD_WEBSOCKET_STATUS} MHD_websocket_encode_close (struct MHD_WebSocketStream* ws, unsigned short reason_code, const char* reason_utf8, size_t reason_utf8_len, char** frame, size_t* frame_len)
  3608. @cindex websocket
  3609. Encodes a websocket close frame.
  3610. Close frames are used to close a websocket connection in a formal way.
  3611. @table @var
  3612. @item ws
  3613. websocket stream;
  3614. @item reason_code
  3615. reason for close.
  3616. You can use @code{enum MHD_WEBSOCKET_CLOSEREASON} for typical reasons,
  3617. but you are not limited to these values.
  3618. The allowed values are specified in RFC 6455 7.4.
  3619. If you don't want to enter a reason, you can specify
  3620. @code{MHD_WEBSOCKET_CLOSEREASON_NO_REASON} (or just 0) then
  3621. no reason is encoded.
  3622. @item reason_utf8
  3623. An UTF-8 encoded text reason why the connection is closed.
  3624. This may be @code{NULL} if @code{reason_utf8_len} is 0.
  3625. This must be @code{NULL} if @code{reason_code} equals to zero
  3626. (@code{MHD_WEBSOCKET_CLOSEREASON_NO_REASON}).
  3627. @item reason_utf8_len
  3628. length of the UTF-8 encoded text reason in bytes.
  3629. This may not exceed 123 bytes.
  3630. @item frame
  3631. pointer to a variable, which receives a buffer with the encoded close frame.
  3632. Must not be @code{NULL}.
  3633. The buffer contains what you typically send via @code{send()} to the recipient.
  3634. If no encoded data is available the variable receives @code{NULL}.
  3635. If the variable is not @code{NULL} then the buffer contains always
  3636. @code{frame_len} bytes plus one terminating @code{NUL} character.
  3637. The caller must free this buffer using @code{MHD_websocket_free()}.
  3638. @item frame_len
  3639. pointer to a variable, which receives the length of the encoded frame in bytes.
  3640. Must not be @code{NULL}.
  3641. @end table
  3642. Returns 0 on success or a value less than zero on errors.
  3643. Can be compared with @code{enum MHD_WEBSOCKET_STATUS}.
  3644. @end deftypefun
  3645. @c ------------------------------------------------------------
  3646. @node microhttpd-websocket memory
  3647. @section Websocket memory functions
  3648. @deftypefun {void*} MHD_websocket_malloc (struct MHD_WebSocketStream* ws, size_t buf_len)
  3649. @cindex websocket
  3650. Allocates memory with the associated @code{malloc()} function
  3651. of the websocket stream.
  3652. The memory allocation function could be different for a websocket stream if
  3653. @code{MHD_websocket_stream_init2()} has been used for initialization.
  3654. @table @var
  3655. @item ws
  3656. websocket stream;
  3657. @item buf_len
  3658. size of the buffer to allocate in bytes.
  3659. @end table
  3660. Returns the pointer of the allocated buffer or @code{NULL} on failure.
  3661. @end deftypefun
  3662. @deftypefun {void*} MHD_websocket_realloc (struct MHD_WebSocketStream* ws, void* buf, size_t new_buf_len)
  3663. @cindex websocket
  3664. Reallocates memory with the associated @code{realloc()} function
  3665. of the websocket stream.
  3666. The memory reallocation function could be different for a websocket stream if
  3667. @code{MHD_websocket_stream_init2()} has been used for initialization.
  3668. @table @var
  3669. @item ws
  3670. websocket stream;
  3671. @item buf
  3672. current buffer, may be @code{NULL};
  3673. @item new_buf_len
  3674. new size of the buffer in bytes.
  3675. @end table
  3676. Return the pointer of the reallocated buffer or @code{NULL} on failure.
  3677. On failure the old pointer remains valid.
  3678. @end deftypefun
  3679. @deftypefun {void} MHD_websocket_free (struct MHD_WebSocketStream* ws, void* buf)
  3680. @cindex websocket
  3681. Frees memory with the associated @code{free()} function
  3682. of the websocket stream.
  3683. The memory free function could be different for a websocket stream if
  3684. @code{MHD_websocket_stream_init2()} has been used for initialization.
  3685. @table @var
  3686. @item ws
  3687. websocket stream;
  3688. @item buf
  3689. buffer to free, this may be @code{NULL} then nothing happens.
  3690. @end table
  3691. @end deftypefun
  3692. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3693. @c **********************************************************
  3694. @c ******************* Appendices *************************
  3695. @c **********************************************************
  3696. @node GNU-LGPL
  3697. @unnumbered GNU-LGPL
  3698. @cindex license
  3699. @include lgpl.texi
  3700. @node eCos License
  3701. @unnumbered eCos License
  3702. @cindex license
  3703. @include ecos.texi
  3704. @node GNU-GPL
  3705. @unnumbered GNU General Public License
  3706. @cindex license
  3707. @include gpl-2.0.texi
  3708. @node GNU-FDL
  3709. @unnumbered GNU-FDL
  3710. @cindex license
  3711. @include fdl-1.3.texi
  3712. @node Concept Index
  3713. @unnumbered Concept Index
  3714. @printindex cp
  3715. @node Function and Data Index
  3716. @unnumbered Function and Data Index
  3717. @printindex fn
  3718. @node Type Index
  3719. @unnumbered Type Index
  3720. @printindex tp
  3721. @bye