libmicrohttpd.texi 172 KB

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