libmicrohttpd.texi 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261
  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--2017 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. Appendices
  64. * GNU-LGPL:: The GNU Lesser General Public License says how you
  65. can copy and share almost all of `libmicrohttpd'.
  66. * GNU GPL with eCos Extension:: The GNU General Public License with eCos extension says how you
  67. can copy and share some parts of `libmicrohttpd'.
  68. * GNU-FDL:: The GNU Free Documentation License says how you
  69. can copy and share the documentation of `libmicrohttpd'.
  70. Indices
  71. * Concept Index:: Index of concepts and programs.
  72. * Function and Data Index:: Index of functions, variables and data types.
  73. * Type Index:: Index of data types.
  74. @end menu
  75. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  76. @c ------------------------------------------------------------
  77. @node microhttpd-intro
  78. @chapter Introduction
  79. @noindent
  80. All symbols defined in the public API start with @code{MHD_}. MHD
  81. is a small HTTP daemon library. As such, it does not have any API
  82. for logging errors (you can only enable or disable logging to stderr).
  83. Also, it may not support all of the HTTP features directly, where
  84. applicable, portions of HTTP may have to be handled by clients of the
  85. library.
  86. The library is supposed to handle everything that it must handle
  87. (because the API would not allow clients to do this), such as basic
  88. connection management. However, detailed interpretations of headers,
  89. such as range requests, are left to the main application. In
  90. particular, if an application developer wants to support range
  91. requests, he needs to explicitly indicate support in responses and
  92. also explicitly parse the range header and generate a response (for
  93. example, using the @code{MHD_create_response_from_fd_at_offset} call
  94. to serve ranges from a file). MHD does understands headers that
  95. control connection management (specifically, @code{Connection: close}
  96. and @code{Expect: 100 continue} are understood and handled
  97. automatically). @code{Connection: upgrade} is supported by passing
  98. control over the socket (or something that behaves like the real
  99. socket in the case of TLS) to the application (after sending the
  100. desired HTTP response header).
  101. MHD largely ignores the semantics of the different HTTP methods,
  102. so clients are left to handle those. One exception is that MHD does
  103. understand @code{HEAD} and will only send the headers of the response
  104. and not the body, even if the client supplied a body. (In fact,
  105. clients do need to construct a response with the correct length, even
  106. for @code{HEAD} request.)
  107. MHD understands @code{POST} data and is able to decode certain
  108. formats (at the moment only @code{application/x-www-form-urlencoded}
  109. and @code{multipart/form-data}) using the post processor API. The
  110. data stream of a POST is also provided directly to the main
  111. application, so unsupported encodings could still be processed, just
  112. not conveniently by MHD.
  113. The header file defines various constants used by the HTTP protocol.
  114. This does not mean that MHD actually interprets all of these values.
  115. The provided constants are exported as a convenience for users of the
  116. library. MHD does not verify that transmitted HTTP headers are
  117. part of the standard specification; users of the library are free to
  118. define their own extensions of the HTTP standard and use those with
  119. MHD.
  120. All functions are guaranteed to be completely reentrant and
  121. thread-safe. MHD checks for allocation failures and tries to
  122. recover gracefully (for example, by closing the connection).
  123. Additionally, clients can specify resource limits on the overall
  124. number of connections, number of connections per IP address and memory
  125. used per connection to avoid resource exhaustion.
  126. @section Scope
  127. MHD is currently used in a wide range of implementations.
  128. Examples based on reports we've received from developers include:
  129. @itemize
  130. @item Embedded HTTP server on a cortex M3 (128 KB code space)
  131. @item Large-scale multimedia server (reportedly serving at the
  132. simulator limit of 7.5 GB/s)
  133. @item Administrative console (via HTTP/HTTPS) for network appliances
  134. @c If you have other interesting examples, please let us know
  135. @end itemize
  136. @section Thread modes and event loops
  137. @cindex poll
  138. @cindex epoll
  139. @cindex select
  140. MHD supports four basic thread modes and up to three event loop
  141. styles.
  142. The four basic thread modes are external sockets polling (MHD creates
  143. no threads, event loop is fully managed by the application), internal
  144. polling (MHD creates one thread for all connections), polling in
  145. thread pool (MHD creates a thread pool which is used to process all
  146. connections) and thread-per-connection (MHD creates one thread for
  147. listen sockets and then one thread per accepted connection).
  148. These thread modes are then combined with the evet loop styles
  149. (polling function type). MHD support select, poll and epoll. select
  150. is available on all platforms, epoll and poll may not be available on
  151. some platforms. Note that it is possible to combine MHD using epoll
  152. with an external select-based event loop.
  153. The default (if no other option is passed) is ``external select''.
  154. The highest performance can typically be obtained with a thread pool
  155. using @code{epoll}. Apache Benchmark (ab) was used to compare the
  156. performance of @code{select} and @code{epoll} when using a thread pool
  157. and a large number of connections. @ref{fig:performance} shows the
  158. resulting plot from the @code{benchmark.c} example, which measures the
  159. latency between an incoming request and the completion of the
  160. transmission of the response. In this setting, the @code{epoll}
  161. thread pool with four threads was able to handle more than 45,000
  162. connections per second on loopback (with Apache Benchmark running
  163. three processes on the same machine).
  164. @cindex performance
  165. @float Figure,fig:performance
  166. @image{libmicrohttpd_performance_data,400pt,300pt,Data,.png}
  167. @caption{Performance measurements for select vs. epoll (with thread-pool).}
  168. @end float
  169. Not all combinations of thread modes and event loop styles are
  170. supported. This is partially to keep the API simple, and partially
  171. because some combinations simply make no sense as others are strictly
  172. superior. Note that the choice of style depends first of all on the
  173. application logic, and then on the performance requirements.
  174. Applications that perform a blocking operation while handling a
  175. request within the callbacks from MHD must use a thread per
  176. connection. This is typically rather costly. Applications that do
  177. not support threads or that must run on embedded devices without
  178. thread-support must use the external mode. Using @code{epoll} is only
  179. supported on some platform, thus portable applications must at least
  180. have a fallback option available. @ref{tbl:supported} lists the sane
  181. combinations.
  182. @float Table,tbl:supported
  183. @multitable {@b{thread-per-connection}} {@b{select}} {@b{poll}} {@b{epoll}}
  184. @item @tab @b{select} @tab @b{poll} @tab @b{epoll}
  185. @item @b{external} @tab yes @tab no @tab yes
  186. @item @b{internal} @tab yes @tab yes @tab yes
  187. @item @b{thread pool} @tab yes @tab yes @tab yes
  188. @item @b{thread-per-connection} @tab yes @tab yes @tab no
  189. @end multitable
  190. @caption{Supported combinations of event styles and thread modes.}
  191. @end float
  192. @section Compiling GNU libmicrohttpd
  193. @cindex compilation
  194. @cindex embedded systems
  195. @cindex portability
  196. MHD uses the standard GNU system where the usual build process
  197. involves running
  198. @verbatim
  199. $ ./configure
  200. $ make
  201. $ make install
  202. @end verbatim
  203. MHD supports various options to be given to configure to tailor the
  204. binary to a specific situation. Note that some of these options will
  205. remove portions of the MHD code that are required for
  206. binary-compatibility. They should only be used on embedded systems
  207. with tight resource constraints and no concerns about library
  208. versioning. Standard distributions including MHD are expected to
  209. always ship with all features enabled, otherwise unexpected
  210. incompatibilities can arise!
  211. Here is a list of MHD-specific options that can be given to configure
  212. (canonical configure options such as ``--prefix'' are also supported, for a
  213. full list of options run ``./configure --help''):
  214. @table @code
  215. @item ``--disable-curl''
  216. disable running testcases using libcurl
  217. @item ``--disable-largefile''
  218. disable support for 64-bit files
  219. @item ``--disable-messages''
  220. disable logging of error messages (smaller binary size, not so much fun for debugging)
  221. @item ``--disable-https''
  222. 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)
  223. @item ``--disable-postprocessor''
  224. do not include the post processor API (results in binary incompatibility)
  225. @item ``--disable-dauth''
  226. do not include the authentication APIs (results in binary incompatibility)
  227. @item ``--disable-httpupgrade''
  228. do not build code for HTTP ``Upgrade'' (smaller binary size, binary incompatible library)
  229. @item ``--disable-epoll''
  230. do not include epoll support, even if it supported (minimally smaller binary size, good for portability testing)
  231. @item ``--enable-coverage''
  232. set flags for analysis of code-coverage with gcc/gcov (results in slow, large binaries)
  233. @item ``--with-threads=posix,w32,none,auto''
  234. 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''.
  235. @item ``--with-gcrypt=PATH''
  236. specifies path to libgcrypt installation
  237. @item ``--with-gnutls=PATH''
  238. specifies path to libgnutls installation
  239. @end table
  240. @section Validity of pointers
  241. MHD will give applications access to its internal data structures
  242. via pointers via arguments and return values from its API. This
  243. creates the question as to how long those pointers are assured to
  244. stay valid.
  245. Most MHD data structures are associated with the connection of an
  246. HTTP client. Thus, pointers associated with a connection are
  247. typically valid until the connection is finished, at which point
  248. MHD will call the @code{MHD_RequestCompletedCallback} if one is
  249. registered. Applications that have such a callback registered
  250. may assume that keys and values from the
  251. @code{MHD_KeyValueIterator}, return values from
  252. @code{MHD_lookup_connection_value} and the @code{url},
  253. @code{method} and @code{version} arguments to the
  254. @code{MHD_AccessHandlerCallback} will remain valid until the
  255. respective @code{MHD_RequestCompletedCallback} is invoked.
  256. In contrast, the @code{upload_data} argument of
  257. @code{MHD_RequestCompletedCallback} as well as all pointers
  258. from the @code{MHD_PostDataIterator} are only valid for the
  259. duration of the callback.
  260. Pointers returned from @code{MHD_get_response_header} are
  261. valid as long as the response itself is valid.
  262. @section Including the microhttpd.h header
  263. @cindex portability
  264. @cindex microhttpd.h
  265. Ideally, before including "microhttpd.h" you should add the necessary
  266. includes to define the @code{uint64_t}, @code{size_t}, @code{fd_set},
  267. @code{socklen_t} and @code{struct sockaddr} data types. Which
  268. specific headers are needed may depend on your platform and your build
  269. system might include some tests to provide you with the necessary
  270. conditional operations. For possible suggestions consult
  271. @code{platform.h} and @code{configure.ac} in the MHD distribution.
  272. Once you have ensured that you manually (!) included the right headers
  273. for your platform before "microhttpd.h", you should also add a line
  274. with @code{#define MHD_PLATFORM_H} which will prevent the
  275. "microhttpd.h" header from trying (and, depending on your platform,
  276. failing) to include the right headers.
  277. If you do not define MHD_PLATFORM_H, the "microhttpd.h" header will
  278. automatically include headers needed on GNU/Linux systems (possibly
  279. causing problems when porting to other platforms).
  280. @section SIGPIPE
  281. @cindex signals
  282. MHD does not install a signal handler for SIGPIPE. On platforms where
  283. this is possible (such as GNU/Linux), it disables SIGPIPE for its I/O
  284. operations (by passing MSG_NOSIGNAL or similar). On other platforms,
  285. SIGPIPE signals may be generated from network operations by MHD and
  286. will cause the process to die unless the developer explicitly installs
  287. a signal handler for SIGPIPE.
  288. Hence portable code using MHD must install a SIGPIPE handler or
  289. explicitly block the SIGPIPE signal. MHD does not do so in order to
  290. avoid messing with other parts of the application that may need to
  291. handle SIGPIPE in a particular way. You can make your application
  292. handle SIGPIPE by calling the following function in @code{main}:
  293. @verbatim
  294. static void
  295. catcher (int sig)
  296. {
  297. }
  298. static void
  299. ignore_sigpipe ()
  300. {
  301. struct sigaction oldsig;
  302. struct sigaction sig;
  303. sig.sa_handler = &catcher;
  304. sigemptyset (&sig.sa_mask);
  305. #ifdef SA_INTERRUPT
  306. sig.sa_flags = SA_INTERRUPT; /* SunOS */
  307. #else
  308. sig.sa_flags = SA_RESTART;
  309. #endif
  310. if (0 != sigaction (SIGPIPE, &sig, &oldsig))
  311. fprintf (stderr,
  312. "Failed to install SIGPIPE handler: %s\n", strerror (errno));
  313. }
  314. @end verbatim
  315. @section MHD_UNSIGNED_LONG_LONG
  316. @cindex long long
  317. @cindex MHD_LONG_LONG
  318. @cindex IAR
  319. @cindex ARM
  320. @cindex cortex m3
  321. @cindex embedded systems
  322. Some platforms do not support @code{long long}. Hence MHD defines a
  323. macro @code{MHD_UNSIGNED LONG_LONG} which will default to
  324. @code{unsigned long long}. For standard desktop operating systems,
  325. this is all you need to know.
  326. However, if your platform does not support @code{unsigned long long},
  327. you should change "platform.h" to define @code{MHD_LONG_LONG} and
  328. @code{MHD_UNSIGNED_LONG_LONG} to an appropriate alternative type and
  329. also define @code{MHD_LONG_LONG_PRINTF} and
  330. @code{MHD_UNSIGNED_LONG_LONG_PRINTF} to the corresponding format
  331. string for printing such a data type. Note that the ``signed''
  332. versions are deprecated. Also, for historical reasons,
  333. @code{MHD_LONG_LONG_PRINTF} is without the percent sign, whereas
  334. @code{MHD_UNSIGNED_LONG_LONG_PRINTF} is with the percent sign. Newly
  335. written code should only use the unsigned versions. However, you need
  336. to define both in "platform.h" if you need to change the definition
  337. for the specific platform.
  338. @section Portability to W32
  339. libmicrohttpd in general ported well to W32. Most libmicrohttpd features
  340. are supported. W32 do not support some functions, like epoll and
  341. corresponding MHD features are not available on W32.
  342. @section Portability to z/OS
  343. To compile MHD on z/OS, extract the archive and run
  344. @verbatim
  345. iconv -f UTF-8 -t IBM-1047 contrib/ascebc > /tmp/ascebc.sh
  346. chmod +x /tmp/ascebc.sh
  347. for n in `find * -type f`
  348. do
  349. /tmp/ascebc.sh $n
  350. done
  351. @end verbatim
  352. to convert all source files to EBCDIC. Note that you must run
  353. @code{configure} from the directory where the configure script is
  354. located. Otherwise, configure will fail to find the
  355. @code{contrib/xcc} script (which is a wrapper around the z/OS c89
  356. compiler).
  357. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  358. @c ------------------------------------------------------------
  359. @node microhttpd-const
  360. @chapter Constants
  361. @deftp {Enumeration} MHD_FLAG
  362. Options for the MHD daemon.
  363. Note that MHD will run automatically in background thread(s) only if
  364. @code{MHD_USE_INTERNAL_POLLING_THREAD} is used. Otherwise caller
  365. (application) must use @code{MHD_run} or @code{MHD_run_from_select} to
  366. have MHD processed network connections and data.
  367. Starting the daemon may also fail if a particular option is not
  368. implemented or not supported on the target platform (i.e. no support
  369. for @acronym{TLS}, threads or IPv6). TLS support generally depends on
  370. options given during MHD compilation.
  371. @table @code
  372. @item MHD_NO_FLAG
  373. No options selected.
  374. @item MHD_USE_ERROR_LOG
  375. If this flag is used, the library should print error messages and
  376. warnings to stderr (or to custom error printer if it's specified by
  377. options). Note that for this run-time option to have any effect, MHD
  378. needs to be compiled with messages enabled. This is done by default
  379. except you ran configure with the @code{--disable-messages} flag set.
  380. @item MHD_USE_DEBUG
  381. @cindex debugging
  382. Currently the same as @code{MHD_USE_ERROR_LOG}.
  383. @item MHD_USE_TLS
  384. @cindex TLS
  385. @cindex SSL
  386. Run in HTTPS-mode. If you specify @code{MHD_USE_TLS} and MHD was
  387. compiled without SSL support, @code{MHD_start_daemon} will return
  388. NULL.
  389. @item MHD_USE_THREAD_PER_CONNECTION
  390. Run using one thread per connection.
  391. @item MHD_USE_INTERNAL_POLLING_THREAD
  392. Run using an internal thread doing @code{SELECT}.
  393. @item MHD_USE_IPv6
  394. @cindex IPv6
  395. Run using the IPv6 protocol (otherwise, MHD will just support IPv4).
  396. If you specify @code{MHD_USE_IPV6} and the local platform does not
  397. support it, @code{MHD_start_daemon} will return NULL.
  398. If you want MHD to support IPv4 and IPv6 using a single socket, pass
  399. MHD_USE_DUAL_STACK, otherwise, if you only pass this option, MHD will
  400. try to bind to IPv6-only (resulting in no IPv4 support).
  401. @item MHD_USE_DUAL_STACK
  402. @cindex IPv6
  403. Use a single socket for IPv4 and IPv6. Note that this will mean
  404. that IPv4 addresses are returned by MHD in the IPv6-mapped format
  405. (the 'struct sockaddr_in6' format will be used for IPv4 and IPv6).
  406. @item MHD_USE_PEDANTIC_CHECKS
  407. @cindex deprecated
  408. Deprecated (use @code{MHD_OPTION_STRICT_FOR_CLIENT}).
  409. Be pedantic about the protocol.
  410. Specifically, at the moment, this flag causes MHD to reject HTTP
  411. 1.1 connections without a @code{Host} header. This is required by the
  412. standard, but of course in violation of the ``be as liberal as possible
  413. in what you accept'' norm. It is recommended to turn this @strong{ON}
  414. if you are testing clients against MHD, and @strong{OFF} in
  415. production.
  416. @item MHD_USE_POLL
  417. @cindex FD_SETSIZE
  418. @cindex poll
  419. @cindex select
  420. Use @code{poll()} instead of @code{select()}. This allows sockets with
  421. descriptors @code{>= FD_SETSIZE}. This option currently only works in
  422. conjunction with @code{MHD_USE_INTERNAL_POLLING_THREAD} (at this point).
  423. If you specify @code{MHD_USE_POLL} and the local platform does not
  424. support it, @code{MHD_start_daemon} will return NULL.
  425. @item MHD_USE_EPOLL
  426. @cindex FD_SETSIZE
  427. @cindex epoll
  428. @cindex select
  429. Use @code{epoll()} instead of @code{poll()} or @code{select()}. This
  430. allows sockets with descriptors @code{>= FD_SETSIZE}. This option is
  431. only available on some systems and does not work in conjunction with
  432. @code{MHD_USE_THREAD_PER_CONNECTION} (at this point). If you specify
  433. @code{MHD_USE_EPOLL} and the local platform does not support it,
  434. @code{MHD_start_daemon} will return NULL. Using @code{epoll()}
  435. instead of @code{select()} or @code{poll()} can in some situations
  436. result in significantly higher performance as the system call has
  437. fundamentally lower complexity (O(1) for @code{epoll()} vs. O(n) for
  438. @code{select()}/@code{poll()} where n is the number of open
  439. connections).
  440. @item MHD_USE_TURBO
  441. @cindex performance
  442. Enable optimizations to aggressively improve performance.
  443. Currently, the optimizations this option enables are based on
  444. opportunistic reads and writes. Bascially, MHD will simply try to
  445. read or write or accept on a socket before checking that the socket is
  446. ready for IO using the event loop mechanism. As the sockets are
  447. non-blocking, this may fail (at a loss of performance), but generally
  448. MHD does this in situations where the operation is likely to succeed,
  449. in which case performance is improved. Setting the flag should generally
  450. be safe (even though the code is slightly more experimental). You may
  451. want to benchmark your application to see if this makes any difference
  452. for you.
  453. @item MHD_USE_SUPPRESS_DATE_NO_CLOCK
  454. @cindex date
  455. @cindex clock
  456. @cindex embedded systems
  457. Suppress (automatically) adding the 'Date:' header to HTTP responses.
  458. This option should ONLY be used on systems that do not have a clock
  459. and that DO provide other mechanisms for cache control. See also
  460. RFC 2616, section 14.18 (exception 3).
  461. @item MHD_USE_NO_LISTEN_SOCKET
  462. @cindex listen
  463. @cindex proxy
  464. @cindex embedded systems
  465. Run the HTTP server without any listen socket. This option only makes
  466. sense if @code{MHD_add_connection} is going to be used exclusively to
  467. connect HTTP clients to the HTTP server. This option is incompatible
  468. with using a thread pool; if it is used,
  469. @code{MHD_OPTION_THREAD_POOL_SIZE} is ignored.
  470. @item MHD_USE_ITC
  471. @cindex quiesce
  472. Force MHD to use a signal inter-thread communication channel to notify
  473. the event loop (of threads) of our shutdown and other events. This is
  474. required if an application uses @code{MHD_USE_INTERNAL_POLLING_THREAD}
  475. and then performs @code{MHD_quiesce_daemon} (which eliminates our
  476. ability to signal termination via the listen socket). In these modes,
  477. @code{MHD_quiesce_daemon} will fail if this option was not set. Also,
  478. use of this option is automatic (as in, you do not even have to
  479. specify it), if @code{MHD_USE_NO_LISTEN_SOCKET} is specified. In
  480. "external" select mode, this option is always simply ignored.
  481. Using this option also guarantees that MHD will not call
  482. @code{shutdown()} on the listen socket, which means a parent
  483. process can continue to use the socket.
  484. @item MHD_ALLOW_SUSPEND_RESUME
  485. Enables using @code{MHD_suspend_connection} and
  486. @code{MHD_resume_connection}, as performing these calls requires some
  487. additional inter-thred communication channels to be created, and code
  488. not using these calls should not pay the cost.
  489. @item MHD_USE_TCP_FASTOPEN
  490. @cindex listen
  491. Enable TCP_FASTOPEN on the listen socket. TCP_FASTOPEN is currently
  492. supported on Linux >= 3.6. On other systems using this option with
  493. cause @code{MHD_start_daemon} to fail.
  494. @item MHD_ALLOW_UPGRADE
  495. @cindex upgrade
  496. This option must be set if you want to upgrade connections
  497. (via ``101 Switching Protocols'' responses). This requires MHD to
  498. allocate additional resources, and hence we require this
  499. special flag so we only use the resources that are really needed.
  500. @item MHD_USE_AUTO
  501. Automatically select best event loop style (polling function)
  502. depending on requested mode by other MHD flags and functions available
  503. on platform. If application doesn't have requirements for any
  504. specific polling function, it's recommended to use this flag. This
  505. flag is very convenient for multiplatform applications.
  506. @end table
  507. @end deftp
  508. @deftp {Enumeration} MHD_OPTION
  509. MHD options. Passed in the varargs portion of
  510. @code{MHD_start_daemon()}.
  511. @table @code
  512. @item MHD_OPTION_END
  513. No more options / last option. This is used to terminate the VARARGs
  514. list.
  515. @item MHD_OPTION_CONNECTION_MEMORY_LIMIT
  516. @cindex memory, limiting memory utilization
  517. Maximum memory size per connection (followed by a @code{size_t}). The
  518. default is 32 kB (32*1024 bytes) as defined by the internal constant
  519. @code{MHD_POOL_SIZE_DEFAULT}. Values above 128k are unlikely to
  520. result in much benefit, as half of the memory will be typically used
  521. for IO, and TCP buffers are unlikely to support window sizes above 64k
  522. on most systems.
  523. @item MHD_OPTION_CONNECTION_MEMORY_INCREMENT
  524. @cindex memory
  525. Increment to use for growing the read buffer (followed by a
  526. @code{size_t}). The default is 1024 (bytes). Increasing this value
  527. will make MHD use memory for reading more aggressively, which can
  528. reduce the number of @code{recvfrom} calls but may increase the number
  529. of @code{sendto} calls. The given value must fit within
  530. MHD_OPTION_CONNECTION_MEMORY_LIMIT.
  531. @item MHD_OPTION_CONNECTION_LIMIT
  532. @cindex connection, limiting number of connections
  533. Maximum number of concurrent connections to accept (followed by an
  534. @code{unsigned int}). The default is @code{FD_SETSIZE - 4} (the
  535. maximum number of file descriptors supported by @code{select} minus
  536. four for @code{stdin}, @code{stdout}, @code{stderr} and the server
  537. socket). In other words, the default is as large as possible.
  538. If the connection limit is reached, MHD's behavior depends a bit on
  539. other options. If @code{MHD_USE_ITC} was given, MHD
  540. will stop accepting connections on the listen socket. This will cause
  541. the operating system to queue connections (up to the @code{listen()}
  542. limit) above the connection limit. Those connections will be held
  543. until MHD is done processing at least one of the active connections.
  544. If @code{MHD_USE_ITC} is not set, then MHD will continue
  545. to @code{accept()} and immediately @code{close()} these connections.
  546. Note that if you set a low connection limit, you can easily get into
  547. trouble with browsers doing request pipelining. For example, if your
  548. connection limit is ``1'', a browser may open a first connection to
  549. access your ``index.html'' file, keep it open but use a second
  550. connection to retrieve CSS files, images and the like. In fact, modern
  551. browsers are typically by default configured for up to 15 parallel
  552. connections to a single server. If this happens, MHD will refuse to
  553. even accept the second connection until the first connection is
  554. closed --- which does not happen until timeout. As a result, the
  555. browser will fail to render the page and seem to hang. If you expect
  556. your server to operate close to the connection limit, you should
  557. first consider using a lower timeout value and also possibly add
  558. a ``Connection: close'' header to your response to ensure that
  559. request pipelining is not used and connections are closed immediately
  560. after the request has completed:
  561. @example
  562. MHD_add_response_header (response,
  563. MHD_HTTP_HEADER_CONNECTION,
  564. "close");
  565. @end example
  566. @item MHD_OPTION_CONNECTION_TIMEOUT
  567. @cindex timeout
  568. After how many seconds of inactivity should a connection automatically
  569. be timed out? (followed by an @code{unsigned int}; use zero for no
  570. timeout). The default is zero (no timeout).
  571. @item MHD_OPTION_NOTIFY_COMPLETED
  572. Register a function that should be called whenever a request has been
  573. completed (this can be used for application-specific clean up).
  574. Requests that have never been presented to the application (via
  575. @code{MHD_AccessHandlerCallback()}) will not result in
  576. notifications.
  577. This option should be followed by @strong{TWO} pointers. First a
  578. pointer to a function of type @code{MHD_RequestCompletedCallback()}
  579. and second a pointer to a closure to pass to the request completed
  580. callback. The second pointer maybe @code{NULL}.
  581. @item MHD_OPTION_NOTIFY_CONNECTION
  582. Register a function that should be called when the TCP connection to a
  583. client is opened or closed. Note that
  584. @code{MHD_OPTION_NOTIFY_COMPLETED} and the @code{con_cls} argument to
  585. the @code{MHD_AccessHandlerCallback} are per HTTP request (and there
  586. can be multiple HTTP requests per TCP connection). The registered
  587. callback is called twice per TCP connection, with
  588. @code{MHD_CONNECTION_NOTIFY_STARTED} and
  589. @code{MHD_CONNECTION_NOTIFY_CLOSED} respectively. An additional
  590. argument can be used to store TCP connection specific information,
  591. which can be retrieved using @code{MHD_CONNECTION_INFO_SOCKET_CONTEXT}
  592. during the lifetime of the TCP connection. The respective location is
  593. not the same as the HTTP-request-specific @code{con_cls} from the
  594. @code{MHD_AccessHandlerCallback}.
  595. This option should be followed by @strong{TWO} pointers. First a
  596. pointer to a function of type @code{MHD_NotifyConnectionCallback()}
  597. and second a pointer to a closure to pass to the request completed
  598. callback. The second pointer maybe @code{NULL}.
  599. @item MHD_OPTION_PER_IP_CONNECTION_LIMIT
  600. Limit on the number of (concurrent) connections made to the
  601. server from the same IP address. Can be used to prevent one
  602. IP from taking over all of the allowed connections. If the
  603. same IP tries to establish more than the specified number of
  604. connections, they will be immediately rejected. The option
  605. should be followed by an @code{unsigned int}. The default is
  606. zero, which means no limit on the number of connections
  607. from the same IP address.
  608. @item MHD_OPTION_LISTEN_BACKLOG_SIZE
  609. Set the size of the @code{listen()} back log queue of the TCP socket.
  610. Takes an @code{unsigned int} as the argument. Default is the
  611. platform-specific value of @code{SOMAXCONN}.
  612. @item MHD_OPTION_STRICT_FOR_CLIENT
  613. Specify how strict we should enforce the HTTP protocol.
  614. Takes an @code{int} as the argument. Default is zero.
  615. If set to 1, MHD will be strict about the protocol. Specifically, at
  616. the moment, this flag uses MHD to reject HTTP 1.1 connections without
  617. a "Host" header. This is required by the standard, but of course in
  618. violation of the "be as liberal as possible in what you accept" norm.
  619. It is recommended to set this to 1 if you are testing clients against
  620. MHD, and 0 in production.
  621. If set to -1 MHD will be permissive about the protocol, allowing
  622. slight deviations that are technically not allowed by the
  623. RFC. Specifically, at the moment, this flag causes MHD to allow spaces
  624. in header field names. This is disallowed by the standard.
  625. It is not recommended to set it to -1 on publicly available servers as
  626. it may potentially lower level of protection.
  627. @item MHD_OPTION_SOCK_ADDR
  628. @cindex bind, restricting bind
  629. Bind daemon to the supplied socket address. This option should be followed by a
  630. @code{struct sockaddr *}. If @code{MHD_USE_IPv6} is specified,
  631. the @code{struct sockaddr*} should point to a @code{struct sockaddr_in6},
  632. otherwise to a @code{struct sockaddr_in}. If this option is not specified,
  633. the daemon will listen to incoming connections from anywhere. If you use this
  634. option, the 'port' argument from @code{MHD_start_daemon} is ignored and the port
  635. from the given @code{struct sockaddr *} will be used instead.
  636. @item MHD_OPTION_URI_LOG_CALLBACK
  637. @cindex debugging
  638. @cindex logging
  639. @cindex query string
  640. Specify a function that should be called before parsing the URI from
  641. the client. The specified callback function can be used for processing
  642. the URI (including the options) before it is parsed. The URI after
  643. parsing will no longer contain the options, which maybe inconvenient for
  644. logging. This option should be followed by two arguments, the first
  645. one must be of the form
  646. @example
  647. void * my_logger(void * cls, const char * uri, struct MHD_Connection *con)
  648. @end example
  649. where the return value will be passed as
  650. @code{*con_cls} in calls to the @code{MHD_AccessHandlerCallback}
  651. when this request is processed later; returning a
  652. value of @code{NULL} has no special significance; (however,
  653. note that if you return non-@code{NULL}, you can no longer
  654. rely on the first call to the access handler having
  655. @code{NULL == *con_cls} on entry)
  656. @code{cls} will be set to the second argument following
  657. MHD_OPTION_URI_LOG_CALLBACK. Finally, @code{uri} will
  658. be the 0-terminated URI of the request.
  659. Note that during the time of this call, most of the connection's state
  660. is not initialized (as we have not yet parsed he headers). However,
  661. information about the connecting client (IP, socket) is available.
  662. @item MHD_OPTION_HTTPS_MEM_KEY
  663. @cindex SSL
  664. @cindex TLS
  665. Memory pointer to the private key to be used by the
  666. HTTPS daemon. This option should be followed by an
  667. "const char*" argument.
  668. This should be used in conjunction with 'MHD_OPTION_HTTPS_MEM_CERT'.
  669. @item MHD_OPTION_HTTPS_KEY_PASSWORD
  670. @cindex SSL
  671. @cindex TLS
  672. Memory pointer to the password that decrypts the
  673. private key to be used by the HTTPS daemon.
  674. This option should be followed by an
  675. "const char*" argument.
  676. This should be used in conjunction with 'MHD_OPTION_HTTPS_MEM_KEY'.
  677. The password (or passphrase) is only used immediately during
  678. @code{MHD_start_daemon()}. Thus, the application may want to
  679. erase it from memory afterwards for additional security.
  680. @item MHD_OPTION_HTTPS_MEM_CERT
  681. @cindex SSL
  682. @cindex TLS
  683. Memory pointer to the certificate to be used by the
  684. HTTPS daemon. This option should be followed by an
  685. "const char*" argument.
  686. This should be used in conjunction with 'MHD_OPTION_HTTPS_MEM_KEY'.
  687. @item MHD_OPTION_HTTPS_MEM_TRUST
  688. @cindex SSL
  689. @cindex TLS
  690. Memory pointer to the CA certificate to be used by the
  691. HTTPS daemon to authenticate and trust clients certificates.
  692. This option should be followed by an "const char*" argument.
  693. The presence of this option activates the request of certificate
  694. to the client. The request to the client is marked optional, and
  695. it is the responsibility of the server to check the presence
  696. of the certificate if needed.
  697. Note that most browsers will only present a client certificate
  698. only if they have one matching the specified CA, not sending
  699. any certificate otherwise.
  700. @item MHD_OPTION_HTTPS_CRED_TYPE
  701. @cindex SSL
  702. @cindex TLS
  703. Daemon credentials type. Either certificate or anonymous,
  704. this option should be followed by one of the values listed in
  705. "enum gnutls_credentials_type_t".
  706. @item MHD_OPTION_HTTPS_PRIORITIES
  707. @cindex SSL
  708. @cindex TLS
  709. @cindex cipher
  710. SSL/TLS protocol version and ciphers.
  711. This option must be followed by an "const char *" argument
  712. specifying the SSL/TLS protocol versions and ciphers that
  713. are acceptable for the application. The string is passed
  714. unchanged to gnutls_priority_init. If this option is not
  715. specified, ``NORMAL'' is used.
  716. @item MHD_OPTION_HTTPS_CERT_CALLBACK
  717. @cindex SSL
  718. @cindex TLS
  719. @cindex SNI
  720. Use a callback to determine which X.509 certificate should be used for
  721. a given HTTPS connection. This option should be followed by a
  722. argument of type "gnutls_certificate_retrieve_function2 *". This
  723. option provides an alternative to MHD_OPTION_HTTPS_MEM_KEY and
  724. MHD_OPTION_HTTPS_MEM_CERT. You must use this version if multiple
  725. domains are to be hosted at the same IP address using TLS's Server
  726. Name Indication (SNI) extension. In this case, the callback is
  727. expected to select the correct certificate based on the SNI
  728. information provided. The callback is expected to access the SNI data
  729. using gnutls_server_name_get(). Using this option requires GnuTLS 3.0
  730. or higher.
  731. @item MHD_OPTION_GNUTLS_PSK_CRED_HANDLER
  732. @cindex SSL
  733. @cindex TLS
  734. @cindex PSK
  735. Use pre-shared key for TLS credentials.
  736. Pass a pointer to callback of type
  737. @code{MHD_PskServerCredentialsCallback} and a closure.
  738. The function will be called to
  739. retrieve the shared key for a given username.
  740. @item MHD_OPTION_DIGEST_AUTH_RANDOM
  741. @cindex digest auth
  742. @cindex random
  743. Digest Authentication nonce's seed.
  744. This option should be followed by two arguments. First an integer of
  745. type "size_t" which specifies the size of the buffer pointed to by the
  746. second argument in bytes. Note that the application must ensure that
  747. the buffer of the second argument remains allocated and unmodified
  748. while the daemon is running. For security, you SHOULD provide a fresh
  749. random nonce when using MHD with Digest Authentication.
  750. @item MHD_OPTION_NONCE_NC_SIZE
  751. @cindex digest auth
  752. @cindex replay attack
  753. Size of an array of nonce and nonce counter map. This option must be
  754. followed by an "unsigned int" argument that have the size (number of
  755. elements) of a map of a nonce and a nonce-counter. If this option
  756. is not specified, a default value of 4 will be used (which might be
  757. too small for servers handling many requests). If you do not use
  758. digest authentication at all, you can specify a value of zero to
  759. save some memory.
  760. You should calculate the value of NC_SIZE based on the number of
  761. connections per second multiplied by your expected session duration
  762. plus a factor of about two for hash table collisions. For example, if
  763. you expect 100 digest-authenticated connections per second and the
  764. average user to stay on your site for 5 minutes, then you likely need
  765. a value of about 60000. On the other hand, if you can only expect
  766. only 10 digest-authenticated connections per second, tolerate browsers
  767. getting a fresh nonce for each request and expect a HTTP request
  768. latency of 250 ms, then a value of about 5 should be fine.
  769. @item MHD_OPTION_LISTEN_SOCKET
  770. @cindex systemd
  771. Listen socket to use. Pass a listen socket for MHD to use
  772. (systemd-style). If this option is used, MHD will not open its own
  773. listen socket(s). The argument passed must be of type "int" and refer
  774. to an existing socket that has been bound to a port and is listening.
  775. @item MHD_OPTION_EXTERNAL_LOGGER
  776. @cindex logging
  777. Use the given function for logging error messages.
  778. This option must be followed by two arguments; the
  779. first must be a pointer to a function
  780. of type 'void fun(void * arg, const char * fmt, va_list ap)'
  781. and the second a pointer of type 'void*' which will
  782. be passed as the "arg" argument to "fun".
  783. Note that MHD will not generate any log messages without
  784. the MHD_USE_ERROR_LOG flag set and if MHD was compiled
  785. with the "--disable-messages" flag.
  786. @item MHD_OPTION_THREAD_POOL_SIZE
  787. @cindex performance
  788. Number (unsigned int) of threads in thread pool. Enable
  789. thread pooling by setting this value to to something
  790. greater than 1. Currently, thread mode must be
  791. MHD_USE_INTERNAL_POLLING_THREAD if thread pooling is enabled
  792. (@code{MHD_start_daemon} returns @code{NULL} for an unsupported thread
  793. mode).
  794. @item MHD_OPTION_ARRAY
  795. @cindex options
  796. @cindex foreign-function interface
  797. This option can be used for initializing MHD using options from an
  798. array. A common use for this is writing an FFI for MHD. The actual
  799. options given are in an array of 'struct MHD_OptionItem', so this
  800. option requires a single argument of type 'struct MHD_OptionItem'.
  801. The array must be terminated with an entry @code{MHD_OPTION_END}.
  802. An example for code using MHD_OPTION_ARRAY is:
  803. @example
  804. struct MHD_OptionItem ops[] = @{
  805. @{ MHD_OPTION_CONNECTION_LIMIT, 100, NULL @},
  806. @{ MHD_OPTION_CONNECTION_TIMEOUT, 10, NULL @},
  807. @{ MHD_OPTION_END, 0, NULL @}
  808. @};
  809. d = MHD_start_daemon(0, 8080, NULL, NULL, dh, NULL,
  810. MHD_OPTION_ARRAY, ops,
  811. MHD_OPTION_END);
  812. @end example
  813. For options that expect a single pointer argument, the
  814. second member of the @code{struct MHD_OptionItem} is ignored.
  815. For options that expect two pointer arguments, the first
  816. argument must be cast to @code{intptr_t}.
  817. @item MHD_OPTION_UNESCAPE_CALLBACK
  818. @cindex internationalization
  819. @cindex escaping
  820. Specify a function that should be called for unescaping escape
  821. sequences in URIs and URI arguments. Note that this function will NOT
  822. be used by the MHD_PostProcessor. If this option is not specified,
  823. the default method will be used which decodes escape sequences of the
  824. form "%HH". This option should be followed by two arguments, the
  825. first one must be of the form
  826. @example
  827. size_t my_unescaper(void * cls, struct MHD_Connection *c, char *s)
  828. @end example
  829. where the return value must be @code{strlen(s)} and @code{s} should be
  830. updated. Note that the unescape function must not lengthen @code{s}
  831. (the result must be shorter than the input and still be 0-terminated).
  832. @code{cls} will be set to the second argument following
  833. MHD_OPTION_UNESCAPE_CALLBACK.
  834. @item MHD_OPTION_THREAD_STACK_SIZE
  835. @cindex stack
  836. @cindex thread
  837. @cindex pthread
  838. @cindex embedded systems
  839. Maximum stack size for threads created by MHD. This option must be
  840. followed by a @code{size_t}). Not specifying this option or using
  841. a value of zero means using the system default (which is likely to
  842. differ based on your platform).
  843. @item MHD_OPTION_TCP_FASTQUEUE_QUEUE_SIZE
  844. @cindex listen
  845. When the flag @code{MHD_USE_TCP_FASTOPEN} is used, this option sets the
  846. connection handshake queue size for the TCP FASTOPEN connections. Note
  847. that a TCP FASTOPEN connection handshake occupies more resources than a
  848. TCP handshake as the SYN packets also contain DATA which is kept in the
  849. associate state until handshake is completed. If this option is not
  850. given the queue size is set to a default value of 10. This option must
  851. be followed by a @code{unsigned int}.
  852. @item MHD_OPTION_HTTPS_MEM_DHPARAMS
  853. @cindex TLS
  854. @cindex SSL
  855. @cindex DH
  856. Memory pointer for the Diffie-Hellman parameters (dh.pem) to be used
  857. by the HTTPS daemon for key exchange. This option must be followed by
  858. a @code{const char *} argument. The argument would be a zero-terminated
  859. string with a PEM encoded PKCS3 DH parameters structure suitable
  860. for passing to @code{gnutls_dh_parms_import_pkcs3}.
  861. @item MHD_OPTION_LISTENING_ADDRESS_REUSE
  862. @cindex bind, restricting bind
  863. @cindex reusing listening address
  864. This option must be followed by a @code{unsigned int} argument.
  865. If this option is present and true (nonzero) parameter is given, allow reusing
  866. the address:port of the listening socket (using @code{SO_REUSEPORT} on most
  867. platforms, and @code{SO_REUSEADDR} on Windows). If a false (zero) parameter is
  868. given, disallow reusing the the address:port of the listening socket (this
  869. usually requires no special action, but @code{SO_EXCLUSIVEADDRUSE} is needed on
  870. Windows). If this option is not present @code{SO_REUSEADDR} is used on all
  871. platforms except Windows so reusing of address:port is disallowed.
  872. @end table
  873. @end deftp
  874. @deftp {C Struct} MHD_OptionItem
  875. Entry in an MHD_OPTION_ARRAY. See the @code{MHD_OPTION_ARRAY} option
  876. argument for its use.
  877. The @code{option} member is used to specify which option is specified
  878. in the array. The other members specify the respective argument.
  879. Note that for options taking only a single pointer, the
  880. @code{ptr_value} member should be set. For options taking two pointer
  881. arguments, the first pointer must be cast to @code{intptr_t} and both
  882. the @code{value} and the @code{ptr_value} members should be used to
  883. pass the two pointers.
  884. @end deftp
  885. @deftp {Enumeration} MHD_ValueKind
  886. The @code{MHD_ValueKind} specifies the source of the key-value pairs in
  887. the HTTP protocol.
  888. @table @code
  889. @item MHD_HEADER_KIND
  890. HTTP header.
  891. @item MHD_COOKIE_KIND
  892. @cindex cookie
  893. Cookies. Note that the original HTTP header containing the cookie(s)
  894. will still be available and intact.
  895. @item MHD_POSTDATA_KIND
  896. @cindex POST method
  897. @code{POST} data. This is available only if a content encoding
  898. supported by MHD is used (currently only @acronym{URL} encoding), and
  899. only if the posted content fits within the available memory pool. Note
  900. that in that case, the upload data given to the
  901. @code{MHD_AccessHandlerCallback()} will be empty (since it has
  902. already been processed).
  903. @item MHD_GET_ARGUMENT_KIND
  904. @code{GET} (URI) arguments.
  905. @item MHD_FOOTER_KIND
  906. HTTP footer (only for http 1.1 chunked encodings).
  907. @end table
  908. @end deftp
  909. @deftp {Enumeration} MHD_RequestTerminationCode
  910. The @code{MHD_RequestTerminationCode} specifies reasons why a request
  911. has been terminated (or completed).
  912. @table @code
  913. @item MHD_REQUEST_TERMINATED_COMPLETED_OK
  914. We finished sending the response.
  915. @item MHD_REQUEST_TERMINATED_WITH_ERROR
  916. Error handling the connection (resources exhausted, other side closed
  917. connection, application error accepting request, etc.)
  918. @item MHD_REQUEST_TERMINATED_TIMEOUT_REACHED
  919. No activity on the connection for the number of seconds specified using
  920. @code{MHD_OPTION_CONNECTION_TIMEOUT}.
  921. @item MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN
  922. We had to close the session since MHD was being shut down.
  923. @end table
  924. @end deftp
  925. @deftp {Enumeration} MHD_ResponseMemoryMode
  926. The @code{MHD_ResponeMemoryMode} specifies how MHD should treat
  927. the memory buffer given for the response in
  928. @code{MHD_create_response_from_buffer}.
  929. @table @code
  930. @item MHD_RESPMEM_PERSISTENT
  931. Buffer is a persistent (static/global) buffer that won't change
  932. for at least the lifetime of the response, MHD should just use
  933. it, not free it, not copy it, just keep an alias to it.
  934. @item MHD_RESPMEM_MUST_FREE
  935. Buffer is heap-allocated with @code{malloc} (or equivalent) and
  936. should be freed by MHD after processing the response has
  937. concluded (response reference counter reaches zero).
  938. @item MHD_RESPMEM_MUST_COPY
  939. Buffer is in transient memory, but not on the heap (for example,
  940. on the stack or non-malloc allocated) and only valid during the
  941. call to @code{MHD_create_response_from_buffer}. MHD must make its
  942. own private copy of the data for processing.
  943. @end table
  944. @end deftp
  945. @deftp {Enumeration} MHD_ResponseFlags
  946. Response-specific flags. Passed as an argument to
  947. @code{MHD_set_response_options()}.
  948. @table @code
  949. @item MHD_RF_NONE
  950. No special handling.
  951. @item MHD_RF_HTTP_VERSION_1_0_ONLY
  952. Only respond in conservative HTTP 1.0-mode. In particular,
  953. do not (automatically) sent "Connection" headers and always
  954. close the connection after generating the response.
  955. By default, MHD will respond using the same HTTP version which
  956. was set in the request. You can also set the
  957. @code{MHD_RF_HTTP_VERSION_1_0_RESPONSE} flag to force version 1.0
  958. in the response.
  959. @item MHD_RF_HTTP_VERSION_1_0_RESPONSE
  960. Only respond in HTTP 1.0-mode. Contrary to the
  961. @code{MHD_RF_HTTP_VERSION_1_0_ONLY} flag, the response's HTTP version will
  962. always be set to 1.0 and ``Connection'' headers are still supported.
  963. You can even combine this option with MHD_RF_HTTP_VERSION_1_0_ONLY to
  964. change the response's HTTP version while maintaining strict compliance
  965. with HTTP 1.0 regarding connection management.
  966. This solution is not perfect as this flag is set on the response which
  967. is created after header processing. So MHD will behave as a HTTP 1.1
  968. server until the response is queued. It means that an invalid HTTP 1.1
  969. request will fail even if the response is sent with HTTP 1.0 and the
  970. request would be valid if interpreted with this version. For example,
  971. this request will fail in strict mode:
  972. @verbatim
  973. GET / HTTP/1.1
  974. @end verbatim
  975. as the ``Host'' header is missing and is mandatory in HTTP 1.1, but it
  976. should succeed when interpreted with HTTP 1.0.
  977. @end table
  978. @end deftp
  979. @deftp {Enumeration} MHD_ResponseOptions
  980. Response-specific options. Passed in the varargs portion of
  981. @code{MHD_set_response_options()}.
  982. @table @code
  983. @item MHD_RO_END
  984. No more options / last option. This is used to terminate the VARARGs
  985. list.
  986. @end table
  987. @end deftp
  988. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  989. @c ------------------------------------------------------------
  990. @node microhttpd-struct
  991. @chapter Structures type definition
  992. @deftp {C Struct} MHD_Daemon
  993. Handle for the daemon (listening on a socket for HTTP traffic).
  994. @end deftp
  995. @deftp {C Struct} MHD_Connection
  996. Handle for a connection / HTTP request. With HTTP/1.1, multiple
  997. requests can be run over the same connection. However, MHD will only
  998. show one request per TCP connection to the client at any given time.
  999. @end deftp
  1000. @deftp {C Struct} MHD_Response
  1001. Handle for a response.
  1002. @end deftp
  1003. @deftp {C Struct} MHD_PostProcessor
  1004. @cindex POST method
  1005. Handle for @code{POST} processing.
  1006. @end deftp
  1007. @deftp {C Union} MHD_ConnectionInfo
  1008. Information about a connection.
  1009. @end deftp
  1010. @deftp {C Union} MHD_DaemonInfo
  1011. Information about an MHD daemon.
  1012. @end deftp
  1013. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1014. @c ------------------------------------------------------------
  1015. @node microhttpd-cb
  1016. @chapter Callback functions definition
  1017. @deftypefn {Function Pointer} int {*MHD_AcceptPolicyCallback} (void *cls, const struct sockaddr * addr, socklen_t addrlen)
  1018. Invoked in the context of a connection to allow or deny a client to
  1019. connect. This callback return @code{MHD_YES} if connection is allowed,
  1020. @code{MHD_NO} if not.
  1021. @table @var
  1022. @item cls
  1023. custom value selected at callback registration time;
  1024. @item addr
  1025. address information from the client;
  1026. @item addrlen
  1027. length of the address information.
  1028. @end table
  1029. @end deftypefn
  1030. @deftypefn {Function Pointer} int {*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)
  1031. Invoked in the context of a connection to answer a request from the
  1032. client. This callback must call MHD functions (example: the
  1033. @code{MHD_Response} ones) to provide content to give back to the client
  1034. and return an HTTP status code (i.e. @code{200} for OK, @code{404},
  1035. etc.).
  1036. @ref{microhttpd-post}, for details on how to code this callback.
  1037. Must return @code{MHD_YES} if the connection was handled successfully,
  1038. @code{MHD_NO} if the socket must be closed due to a serious error while
  1039. handling the request
  1040. @table @var
  1041. @item cls
  1042. custom value selected at callback registration time;
  1043. @item url
  1044. the URL requested by the client;
  1045. @item method
  1046. the HTTP method used by the client (@code{GET}, @code{PUT},
  1047. @code{DELETE}, @code{POST}, etc.);
  1048. @item version
  1049. the HTTP version string (i.e. @code{HTTP/1.1});
  1050. @item upload_data
  1051. the data being uploaded (excluding headers):
  1052. @cindex POST method
  1053. @cindex PUT method
  1054. @code{POST} data @strong{will} be made available
  1055. incrementally in @var{upload_data}; even if @code{POST}
  1056. data is available, the first time the callback is
  1057. invoked there won't be upload data, as this is done
  1058. just after MHD parses the headers. If supported by
  1059. the client and the HTTP version, the application can
  1060. at this point queue an error response to possibly
  1061. avoid the upload entirely. If no response is generated,
  1062. MHD will (if required) automatically send a 100 CONTINUE
  1063. reply to the client.
  1064. Afterwards, POST data will be passed to the callback
  1065. to be processed incrementally by the application. The
  1066. application may return @code{MHD_NO} to forcefully
  1067. terminate the TCP connection without generating a
  1068. proper HTTP response. Once all of the upload data has
  1069. been provided to the application, the application
  1070. will be called again with 0 bytes of upload data.
  1071. At this point, a response should be queued to complete
  1072. the handling of the request.
  1073. @item upload_data_size
  1074. set initially to the size of the @var{upload_data} provided; this
  1075. callback must update this value to the number of bytes @strong{NOT}
  1076. processed; unless external select is used, the callback maybe
  1077. required to process at least some data. If the callback fails to
  1078. process data in multi-threaded or internal-select mode and if the
  1079. read-buffer is already at the maximum size that MHD is willing to
  1080. use for reading (about half of the maximum amount of memory allowed
  1081. for the connection), then MHD will abort handling the connection
  1082. and return an internal server error to the client. In order to
  1083. avoid this, clients must be able to process upload data incrementally
  1084. and reduce the value of @code{upload_data_size}.
  1085. @item con_cls
  1086. reference to a pointer, initially set to @code{NULL}, that this callback can
  1087. set to some address and that will be preserved by MHD for future
  1088. calls for this request;
  1089. since the access handler may be called many times (i.e., for a
  1090. @code{PUT}/@code{POST} operation with plenty of upload data) this allows
  1091. the application to easily associate some request-specific state;
  1092. if necessary, this state can be cleaned up in the global
  1093. @code{MHD_RequestCompletedCallback} (which can be set with the
  1094. @code{MHD_OPTION_NOTIFY_COMPLETED}).
  1095. @end table
  1096. @end deftypefn
  1097. @deftypefn {Function Pointer} void {*MHD_RequestCompletedCallback} (void *cls, struct MHD_Connectionconnection, void **con_cls, enum MHD_RequestTerminationCode toe)
  1098. Signature of the callback used by MHD to notify the application about
  1099. completed requests.
  1100. @table @var
  1101. @item cls
  1102. custom value selected at callback registration time;
  1103. @item connection
  1104. connection handle;
  1105. @item con_cls
  1106. value as set by the last call to the
  1107. @code{MHD_AccessHandlerCallback};
  1108. @item toe
  1109. reason for request termination see @code{MHD_OPTION_NOTIFY_COMPLETED}.
  1110. @end table
  1111. @end deftypefn
  1112. @deftypefn {Function Pointer} int {*MHD_KeyValueIterator} (void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
  1113. Iterator over key-value pairs. This iterator can be used to iterate
  1114. over all of the cookies, headers, or @code{POST}-data fields of a
  1115. request, and also to iterate over the headers that have been added to a
  1116. response.
  1117. @table @var
  1118. @item cls
  1119. custom value specified when iteration was triggered;
  1120. @item kind
  1121. kind of the header we are looking at
  1122. @item key
  1123. key for the value, can be an empty string
  1124. @item value
  1125. value corresponding value, can be NULL
  1126. @end table
  1127. Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the
  1128. iteration.
  1129. @end deftypefn
  1130. @deftypefn {Function Pointer} int {*MHD_ContentReaderCallback} (void *cls, uint64_t pos, char *buf, size_t max)
  1131. Callback used by MHD in order to obtain content. The callback has to
  1132. copy at most @var{max} bytes of content into @var{buf}. The total
  1133. number of bytes that has been placed into @var{buf} should be returned.
  1134. Note that returning zero will cause MHD to try again.
  1135. Thus, returning zero should only be used in conjunction
  1136. with @code{MHD_suspend_connection()} to avoid busy waiting.
  1137. While usually the callback simply returns the number of bytes written
  1138. into @var{buf}, there are two special return value:
  1139. @code{MHD_CONTENT_READER_END_OF_STREAM} (-1) should be returned
  1140. for the regular end of transmission (with chunked encoding, MHD will then
  1141. terminate the chunk and send any HTTP footers that might be
  1142. present; without chunked encoding and given an unknown
  1143. response size, MHD will simply close the connection; note
  1144. that while returning @code{MHD_CONTENT_READER_END_OF_STREAM} is not technically
  1145. legal if a response size was specified, MHD accepts this
  1146. and treats it just as @code{MHD_CONTENT_READER_END_WITH_ERROR}.
  1147. @code{MHD_CONTENT_READER_END_WITH_ERROR} (-2) is used to indicate a server
  1148. error generating the response; this will cause MHD to simply
  1149. close the connection immediately. If a response size was
  1150. given or if chunked encoding is in use, this will indicate
  1151. an error to the client. Note, however, that if the client
  1152. does not know a response size and chunked encoding is not in
  1153. use, then clients will not be able to tell the difference between
  1154. @code{MHD_CONTENT_READER_END_WITH_ERROR} and
  1155. @code{MHD_CONTENT_READER_END_OF_STREAM}.
  1156. This is not a limitation of MHD but rather of the HTTP protocol.
  1157. @table @var
  1158. @item cls
  1159. custom value selected at callback registration time;
  1160. @item pos
  1161. position in the datastream to access; note that if an
  1162. @code{MHD_Response} object is re-used, it is possible for the same
  1163. content reader to be queried multiple times for the same data; however,
  1164. if an @code{MHD_Response} is not re-used, MHD guarantees that
  1165. @var{pos} will be the sum of all non-negative return values obtained
  1166. from the content reader so far.
  1167. @end table
  1168. Return @code{-1} on error (MHD will no longer try to read content and
  1169. instead close the connection with the client).
  1170. @end deftypefn
  1171. @deftypefn {Function Pointer} void {*MHD_ContentReaderFreeCallback} (void *cls)
  1172. This method is called by MHD if we are done with a content reader.
  1173. It should be used to free resources associated with the content reader.
  1174. @end deftypefn
  1175. @deftypefn {Function Pointer} int {*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)
  1176. Iterator over key-value pairs where the value maybe made available in
  1177. increments and/or may not be zero-terminated. Used for processing
  1178. @code{POST} data.
  1179. @table @var
  1180. @item cls
  1181. custom value selected at callback registration time;
  1182. @item kind
  1183. type of the value;
  1184. @item key
  1185. zero-terminated key for the value;
  1186. @item filename
  1187. name of the uploaded file, @code{NULL} if not known;
  1188. @item content_type
  1189. mime-type of the data, @code{NULL} if not known;
  1190. @item transfer_encoding
  1191. encoding of the data, @code{NULL} if not known;
  1192. @item data
  1193. pointer to size bytes of data at the specified offset;
  1194. @item off
  1195. offset of data in the overall value;
  1196. @item size
  1197. number of bytes in data available.
  1198. @end table
  1199. Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the
  1200. iteration.
  1201. @end deftypefn
  1202. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1203. @c ------------------------------------------------------------
  1204. @node microhttpd-init
  1205. @chapter Starting and stopping the server
  1206. @deftypefun {void} MHD_set_panic_func (MHD_PanicCallback cb, void *cls)
  1207. Set a handler for fatal errors.
  1208. @table @var
  1209. @item cb
  1210. function to call if MHD encounters a fatal internal error. If no handler was set explicitly, MHD will call @code{abort}.
  1211. @item cls
  1212. 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})
  1213. @end table
  1214. @end deftypefun
  1215. @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, ...)
  1216. Start a webserver on the given port.
  1217. @table @var
  1218. @item flags
  1219. OR-ed combination of @code{MHD_FLAG} values;
  1220. @item port
  1221. port to bind to;
  1222. @item apc
  1223. callback to call to check which clients will be allowed to connect; you
  1224. can pass @code{NULL} in which case connections from any @acronym{IP} will be
  1225. accepted;
  1226. @item apc_cls
  1227. extra argument to @var{apc};
  1228. @item dh
  1229. default handler for all URIs;
  1230. @item dh_cls
  1231. extra argument to @var{dh}.
  1232. @end table
  1233. Additional arguments are a list of options (type-value pairs,
  1234. terminated with @code{MHD_OPTION_END}). It is mandatory to use
  1235. @code{MHD_OPTION_END} as last argument, even when there are no
  1236. additional arguments.
  1237. Return @code{NULL} on error, handle to daemon on success.
  1238. @end deftypefun
  1239. @deftypefun int MHD_quiesce_daemon (struct MHD_Daemon *daemon)
  1240. @cindex quiesce
  1241. Stop accepting connections from the listening socket. Allows clients
  1242. to continue processing, but stops accepting new connections. Note
  1243. that the caller is responsible for closing the returned socket;
  1244. however, if MHD is run using threads (anything but external select
  1245. mode), it must not be closed until AFTER @code{MHD_stop_daemon} has
  1246. been called (as it is theoretically possible that an existing thread
  1247. is still using it).
  1248. This function is useful in the special case that a listen socket
  1249. is to be migrated to another process (i.e. a newer version of the
  1250. HTTP server) while existing connections should continue to be
  1251. processed until they are finished.
  1252. Return @code{-1} on error (daemon not listening), the handle to the
  1253. listen socket otherwise.
  1254. @end deftypefun
  1255. @deftypefun void MHD_stop_daemon (struct MHD_Daemon *daemon)
  1256. Shutdown an HTTP daemon.
  1257. @end deftypefun
  1258. @deftypefun int MHD_run (struct MHD_Daemon *daemon)
  1259. Run webserver operations (without blocking unless in client callbacks).
  1260. This method should be called by clients in combination with
  1261. @code{MHD_get_fdset()} if the client-controlled @code{select}-method is used.
  1262. @cindex select
  1263. @cindex poll
  1264. This function will work for external @code{poll} and @code{select} mode.
  1265. However, if using external @code{select} mode, you may want to
  1266. instead use @code{MHD_run_from_select}, as it is more efficient.
  1267. @table @var
  1268. @item daemon
  1269. daemon to process connections of
  1270. @end table
  1271. Return @code{MHD_YES} on success, @code{MHD_NO} if this daemon was not
  1272. started with the right options for this call.
  1273. @end deftypefun
  1274. @deftypefun int 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)
  1275. Run webserver operations given sets of ready socket handles.
  1276. @cindex select
  1277. This method should be called by clients in combination with
  1278. @code{MHD_get_fdset} if the client-controlled (external)
  1279. select method is used.
  1280. You can use this function instead of @code{MHD_run} if you called
  1281. @code{select} on the result from @code{MHD_get_fdset}. File descriptors in
  1282. the sets that are not controlled by MHD will be ignored. Calling
  1283. this function instead of @code{MHD_run} is more efficient as MHD will
  1284. not have to call @code{select} again to determine which operations are
  1285. ready.
  1286. @table @var
  1287. @item daemon
  1288. daemon to process connections of
  1289. @item read_fd_set
  1290. set of descriptors that must be ready for reading without blocking
  1291. @item write_fd_set
  1292. set of descriptors that must be ready for writing without blocking
  1293. @item except_fd_set
  1294. ignored, can be NULL
  1295. @end table
  1296. Return @code{MHD_YES} on success, @code{MHD_NO} on serious internal
  1297. errors.
  1298. @end deftypefun
  1299. @deftypefun void MHD_add_connection (struct MHD_Daemon *daemon, int client_socket, const struct sockaddr *addr, socklen_t addrlen)
  1300. Add another client connection to the set of connections
  1301. managed by MHD. This API is usually not needed (since
  1302. MHD will accept inbound connections on the server socket).
  1303. Use this API in special cases, for example if your HTTP
  1304. server is behind NAT and needs to connect out to the
  1305. HTTP client, or if you are building a proxy.
  1306. If you use this API in conjunction with a internal select or a thread
  1307. pool, you must set the option @code{MHD_USE_ITC} to
  1308. ensure that the freshly added connection is immediately processed by
  1309. MHD.
  1310. The given client socket will be managed (and closed!) by MHD after
  1311. this call and must no longer be used directly by the application
  1312. afterwards.
  1313. @table @var
  1314. @item daemon
  1315. daemon that manages the connection
  1316. @item client_socket
  1317. socket to manage (MHD will expect to receive an HTTP request from this socket next).
  1318. @item addr
  1319. IP address of the client
  1320. @item addrlen
  1321. number of bytes in addr
  1322. @end table
  1323. This function will return @code{MHD_YES} on success,
  1324. @code{MHD_NO} if this daemon could
  1325. not handle the connection (i.e. malloc failed, etc).
  1326. The socket will be closed in any case; 'errno' is set
  1327. to indicate further details about the error.
  1328. @end deftypefun
  1329. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1330. @c -----------------------------------------------------------
  1331. @node microhttpd-inspect
  1332. @chapter Implementing external @code{select}
  1333. @deftypefun int 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)
  1334. Obtain the @code{select()} sets for this daemon. The daemon's socket
  1335. is added to @var{read_fd_set}. The list of currently existent
  1336. connections is scanned and their file descriptors added to the correct
  1337. set.
  1338. When calling this function, FD_SETSIZE is assumed to be platform's
  1339. default. If you changed FD_SETSIZE for your application,
  1340. you should use @code{MHD_get_fdset2()} instead.
  1341. This function should only be called in when MHD is configured to use
  1342. external select with @code{select()} or with @code{epoll()}. In
  1343. the latter case, it will only add the single @code{epoll()} file
  1344. descriptor used by MHD to the sets.
  1345. After the call completed successfully: the variable referenced by
  1346. @var{max_fd} references the file descriptor with highest integer
  1347. identifier. The variable must be set to zero before invoking this
  1348. function.
  1349. Return @code{MHD_YES} on success, @code{MHD_NO} if: the arguments are
  1350. invalid (example: @code{NULL} pointers); this daemon was not started with
  1351. the right options for this call.
  1352. @end deftypefun
  1353. @deftypefun int 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)
  1354. Like @code{MHD_get_fdset()}, except that you can manually specify the value of FD_SETSIZE used by your application.
  1355. @end deftypefun
  1356. @deftypefun int MHD_get_timeout (struct MHD_Daemon *daemon, unsigned long long *timeout)
  1357. @cindex timeout
  1358. Obtain timeout value for select for this daemon (only needed if
  1359. connection timeout is used). The returned value is how many
  1360. milliseconds @code{select} should at most block, not the timeout value
  1361. set for connections. This function must not be called if the
  1362. @code{MHD_USE_THREAD_PER_CONNECTION} mode is in use (since then it is
  1363. not meaningful to ask for a timeout, after all, there is concurrenct
  1364. activity). The function must also not be called by user-code if
  1365. @code{MHD_USE_INTERNAL_POLLING_THREAD} is in use. In the latter case, the
  1366. behavior is undefined.
  1367. @table @var
  1368. @item daemon
  1369. which daemon to obtain the timeout from.
  1370. @item timeout
  1371. will be set to the timeout (in milliseconds).
  1372. @end table
  1373. Return @code{MHD_YES} on success, @code{MHD_NO} if timeouts are not used
  1374. (or no connections exist that would necessiate the use of a timeout
  1375. right now).
  1376. @end deftypefun
  1377. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1378. @c -----------------------------------------------------------
  1379. @node microhttpd-requests
  1380. @chapter Handling requests
  1381. @deftypefun int MHD_get_connection_values (struct MHD_Connection *connection, enum MHD_ValueKind kind, MHD_KeyValueIterator iterator, void *iterator_cls)
  1382. Get all the headers matching @var{kind} from the request. The @var{kind}
  1383. argument can be a bitmask, ORing the various header kinds that are
  1384. requested.
  1385. The @var{iterator} callback is invoked once for each header, with
  1386. @var{iterator_cls} as first argument. After version 0.9.19, the
  1387. headers are iterated in the same order as they were received from
  1388. the network; previous versions iterated over the headers in reverse
  1389. order.
  1390. @code{MHD_get_connection_values} returns the number of entries
  1391. iterated over; this can be less than the number of headers if, while
  1392. iterating, @var{iterator} returns @code{MHD_NO}.
  1393. @var{iterator} can be @code{NULL}: in this case this function just counts
  1394. and returns the number of headers.
  1395. In the case of @code{MHD_GET_ARGUMENT_KIND}, the @var{value} argument
  1396. will be @code{NULL} if the URL contained a key without an equals operator.
  1397. For example, for a HTTP request to the URL ``http://foo/bar?key'', the
  1398. @var{value} argument is @code{NULL}; in contrast, a HTTP request to the URL
  1399. ``http://foo/bar?key='', the @var{value} argument is the empty string.
  1400. The normal case is that the URL contains ``http://foo/bar?key=value''
  1401. in which case @var{value} would be the string ``value'' and @var{key}
  1402. would contain the string ``key''.
  1403. @end deftypefun
  1404. @deftypefun int MHD_set_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key, const char *value)
  1405. This function can be used to append an entry to
  1406. the list of HTTP headers of a connection (so that the
  1407. @code{MHD_get_connection_values function} will return
  1408. them -- and the MHD PostProcessor will also
  1409. see them). This maybe required in certain
  1410. situations (see Mantis #1399) where (broken)
  1411. HTTP implementations fail to supply values needed
  1412. by the post processor (or other parts of the
  1413. application).
  1414. This function MUST only be called from within
  1415. the MHD_AccessHandlerCallback (otherwise, access
  1416. maybe improperly synchronized). Furthermore,
  1417. the client must guarantee that the key and
  1418. value arguments are 0-terminated strings that
  1419. are NOT freed until the connection is closed.
  1420. (The easiest way to do this is by passing only
  1421. arguments to permanently allocated strings.).
  1422. @var{connection} is the connection for which
  1423. the entry for @var{key} of the given @var{kind}
  1424. should be set to the given @var{value}.
  1425. The function returns @code{MHD_NO} if the operation
  1426. could not be performed due to insufficient memory
  1427. and @code{MHD_YES} on success.
  1428. @end deftypefun
  1429. @deftypefun {const char *} MHD_lookup_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key)
  1430. Get a particular header value. If multiple values match the
  1431. @var{kind}, return one of them (the ``first'', whatever that means).
  1432. @var{key} must reference a zero-terminated ASCII-coded string
  1433. representing the header to look for: it is compared against the
  1434. headers using @code{strcasecmp()}, so case is ignored. A value of
  1435. @code{NULL} for @var{key} can be used to lookup 'trailing' values without a
  1436. key, for example if a URI is of the form
  1437. ``http://example.com/?trailer'', a @var{key} of @code{NULL} can be used to
  1438. access ``tailer" The function returns @code{NULL} if no matching item
  1439. was found.
  1440. @end deftypefun
  1441. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1442. @c ------------------------------------------------------------
  1443. @node microhttpd-responses
  1444. @chapter Building responses to requests
  1445. @noindent
  1446. Response objects handling by MHD is asynchronous with respect to the
  1447. application execution flow. Instances of the @code{MHD_Response}
  1448. structure are not associated to a daemon and neither to a client
  1449. connection: they are managed with reference counting.
  1450. In the simplest case: we allocate a new @code{MHD_Response} structure
  1451. for each response, we use it once and finally we destroy it.
  1452. MHD allows more efficient resources usages.
  1453. Example: we allocate a new @code{MHD_Response} structure for each
  1454. response @strong{kind}, we use it every time we have to give that
  1455. response and we finally destroy it only when the daemon shuts down.
  1456. @menu
  1457. * microhttpd-response enqueue:: Enqueuing a response.
  1458. * microhttpd-response create:: Creating a response object.
  1459. * microhttpd-response headers:: Adding headers to a response.
  1460. * microhttpd-response options:: Setting response options.
  1461. * microhttpd-response inspect:: Inspecting a response object.
  1462. * microhttpd-response upgrade:: Creating a response for protocol upgrades.
  1463. @end menu
  1464. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1465. @c ------------------------------------------------------------
  1466. @node microhttpd-response enqueue
  1467. @section Enqueuing a response
  1468. @deftypefun int MHD_queue_response (struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
  1469. Queue a response to be transmitted to the client as soon as possible
  1470. but only after MHD_AccessHandlerCallback returns. This function
  1471. checks that it is legal to queue a response at this time for the
  1472. given connection. It also increments the internal reference
  1473. counter for the response object (the counter will be decremented
  1474. automatically once the response has been transmitted).
  1475. @table @var
  1476. @item connection
  1477. the connection identifying the client;
  1478. @item status_code
  1479. HTTP status code (i.e. @code{200} for OK);
  1480. @item response
  1481. response to transmit.
  1482. @end table
  1483. Return @code{MHD_YES} on success or if message has been queued. Return
  1484. @code{MHD_NO}: if arguments are invalid (example: @code{NULL} pointer); on
  1485. error (i.e. reply already sent).
  1486. @end deftypefun
  1487. @deftypefun void MHD_destroy_response (struct MHD_Response *response)
  1488. Destroy a response object and associated resources (decrement the
  1489. reference counter). Note that MHD may keep some of the resources
  1490. around if the response is still in the queue for some clients, so the
  1491. memory may not necessarily be freed immediately.
  1492. @end deftypefun
  1493. An explanation of reference counting@footnote{Note to readers acquainted
  1494. to the Tcl API: reference counting on @code{MHD_Connection}
  1495. structures is handled in the same way as Tcl handles @code{Tcl_Obj}
  1496. structures through @code{Tcl_IncrRefCount()} and
  1497. @code{Tcl_DecrRefCount()}.}:
  1498. @enumerate
  1499. @item
  1500. a @code{MHD_Response} object is allocated:
  1501. @example
  1502. struct MHD_Response * response = MHD_create_response_from_buffer(...);
  1503. /* here: reference counter = 1 */
  1504. @end example
  1505. @item
  1506. the @code{MHD_Response} object is enqueued in a @code{MHD_Connection}:
  1507. @example
  1508. MHD_queue_response(connection, , response);
  1509. /* here: reference counter = 2 */
  1510. @end example
  1511. @item
  1512. the creator of the response object discharges responsibility for it:
  1513. @example
  1514. MHD_destroy_response(response);
  1515. /* here: reference counter = 1 */
  1516. @end example
  1517. @item
  1518. the daemon handles the connection sending the response's data to the
  1519. client then decrements the reference counter by calling
  1520. @code{MHD_destroy_response()}: the counter's value drops to zero and
  1521. the @code{MHD_Response} object is released.
  1522. @end enumerate
  1523. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1524. @c ------------------------------------------------------------
  1525. @node microhttpd-response create
  1526. @section Creating a response object
  1527. @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)
  1528. Create a response object. The response object can be extended with
  1529. header information and then it can be used any number of times.
  1530. @table @var
  1531. @item size
  1532. size of the data portion of the response, @code{-1} for unknown;
  1533. @item block_size
  1534. preferred block size for querying @var{crc} (advisory only, MHD may
  1535. still call @var{crc} using smaller chunks); this is essentially the
  1536. buffer size used for @acronym{IO}, clients should pick a value that is
  1537. appropriate for @acronym{IO} and memory performance requirements;
  1538. @item crc
  1539. callback to use to obtain response data;
  1540. @item crc_cls
  1541. extra argument to @var{crc};
  1542. @item crfc
  1543. callback to call to free @var{crc_cls} resources.
  1544. @end table
  1545. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  1546. @end deftypefun
  1547. @deftypefun {struct MHD_Response *} MHD_create_response_from_fd (uint64_t size, int fd)
  1548. Create a response object. The response object can be extended with
  1549. header information and then it can be used any number of times.
  1550. @table @var
  1551. @item size
  1552. size of the data portion of the response (should be smaller or equal to the
  1553. size of the file)
  1554. @item fd
  1555. file descriptor referring to a file on disk with the data; will be
  1556. closed when response is destroyed; note that 'fd' must be an actual
  1557. file descriptor (not a pipe or socket) since MHD might use 'sendfile'
  1558. or 'seek' on it. The descriptor should be in blocking-IO mode.
  1559. @end table
  1560. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  1561. @end deftypefun
  1562. @deftypefun {struct MHD_Response *} MHD_create_response_from_fd_at_offset (size_t size, int fd, off_t offset)
  1563. Create a response object. The response object can be extended with
  1564. header information and then it can be used any number of times.
  1565. Note that you need to be a bit careful about @code{off_t} when
  1566. writing this code. Depending on your platform, MHD is likely
  1567. to have been compiled with support for 64-bit files. When you
  1568. compile your own application, you must make sure that @code{off_t}
  1569. is also a 64-bit value. If not, your compiler may pass a 32-bit
  1570. value as @code{off_t}, which will result in 32-bits of garbage.
  1571. If you use the autotools, use the @code{AC_SYS_LARGEFILE} autoconf
  1572. macro and make sure to include the generated @file{config.h} file
  1573. before @file{microhttpd.h} to avoid problems. If you do not have a
  1574. build system and only want to run on a GNU/Linux system, you could
  1575. also use
  1576. @verbatim
  1577. #define _FILE_OFFSET_BITS 64
  1578. #include <sys/types.h>
  1579. #include <sys/stat.h>
  1580. #include <fcntl.h>
  1581. #include <microhttpd.h>
  1582. @end verbatim
  1583. to ensure 64-bit @code{off_t}. Note that if your operating system
  1584. does not support 64-bit files, MHD will be compiled with a 32-bit
  1585. @code{off_t} (in which case the above would be wrong).
  1586. @table @var
  1587. @item size
  1588. size of the data portion of the response (number of bytes to transmit from the
  1589. file starting at offset).
  1590. @item fd
  1591. file descriptor referring to a file on disk with the data; will be
  1592. closed when response is destroyed; note that 'fd' must be an actual
  1593. file descriptor (not a pipe or socket) since MHD might use 'sendfile'
  1594. or 'seek' on it. The descriptor should be in blocking-IO mode.
  1595. @item offset
  1596. offset to start reading from in the file
  1597. @end table
  1598. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  1599. @end deftypefun
  1600. @deftypefun {struct MHD_Response *} MHD_create_response_from_buffer (size_t size, void *data, enum MHD_ResponseMemoryMode mode)
  1601. Create a response object. The response object can be extended with
  1602. header information and then it can be used any number of times.
  1603. @table @var
  1604. @item size
  1605. size of the data portion of the response;
  1606. @item buffer
  1607. the data itself;
  1608. @item mode
  1609. memory management options for buffer; use
  1610. MHD_RESPMEM_PERSISTENT if the buffer is static/global memory,
  1611. use MHD_RESPMEM_MUST_FREE if the buffer is heap-allocated and
  1612. should be freed by MHD and MHD_RESPMEM_MUST_COPY if the
  1613. buffer is in transient memory (i.e. on the stack) and must
  1614. be copied by MHD;
  1615. @end table
  1616. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  1617. @end deftypefun
  1618. @deftypefun {struct MHD_Response *} MHD_create_response_from_buffer_with_free_callback (size_t size, void *data, MHD_ContentReaderFreeCallback crfc)
  1619. Create a response object. The buffer at the end must be free'd
  1620. by calling the @var{crfc} function.
  1621. @table @var
  1622. @item size
  1623. size of the data portion of the response;
  1624. @item buffer
  1625. the data itself;
  1626. @item crfc
  1627. function to call at the end to free memory allocated at @var{buffer}.
  1628. @end table
  1629. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  1630. @end deftypefun
  1631. @deftypefun {struct MHD_Response *} MHD_create_response_from_data (size_t size, void *data, int must_free, int must_copy)
  1632. Create a response object. The response object can be extended with
  1633. header information and then it can be used any number of times.
  1634. This function is deprecated, use @code{MHD_create_response_from_buffer} instead.
  1635. @table @var
  1636. @item size
  1637. size of the data portion of the response;
  1638. @item data
  1639. the data itself;
  1640. @item must_free
  1641. if true: MHD should free data when done;
  1642. @item must_copy
  1643. if true: MHD allocates a block of memory and use it to make a copy of
  1644. @var{data} embedded in the returned @code{MHD_Response} structure;
  1645. handling of the embedded memory is responsibility of MHD; @var{data}
  1646. can be released anytime after this call returns.
  1647. @end table
  1648. Return @code{NULL} on error (i.e. invalid arguments, out of memory).
  1649. @end deftypefun
  1650. Example: create a response from a statically allocated string:
  1651. @example
  1652. const char * data = "<html><body><p>Error!</p></body></html>";
  1653. struct MHD_Connection * connection = ...;
  1654. struct MHD_Response * response;
  1655. response = MHD_create_response_from_buffer (strlen(data), data,
  1656. MHD_RESPMEM_PERSISTENT);
  1657. MHD_queue_response(connection, 404, response);
  1658. MHD_destroy_response(response);
  1659. @end example
  1660. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1661. @c ------------------------------------------------------------
  1662. @node microhttpd-response headers
  1663. @section Adding headers to a response
  1664. @deftypefun int MHD_add_response_header (struct MHD_Response *response, const char *header, const char *content)
  1665. Add a header line to the response. The strings referenced by
  1666. @var{header} and @var{content} must be zero-terminated and they are
  1667. duplicated into memory blocks embedded in @var{response}.
  1668. Notice that the strings must not hold newlines, carriage returns or tab
  1669. chars.
  1670. MHD_add_response_header() prevents applications from setting a
  1671. ``Transfer-Encoding'' header to values other than ``identity'' or
  1672. ``chunked'' as other transfer encodings are not supported by MHD. Note
  1673. that usually MHD will pick the transfer encoding correctly
  1674. automatically, but applications can use the header to force a
  1675. particular behavior.
  1676. MHD_add_response_header() also prevents applications from setting a
  1677. ``Content-Length'' header. MHD will automatically set a correct
  1678. ``Content-Length'' header if it is possible and allowed.
  1679. Return @code{MHD_NO} on error (i.e. invalid header or content format or
  1680. memory allocation error).
  1681. @end deftypefun
  1682. @deftypefun int MHD_add_response_footer (struct MHD_Response *response, const char *footer, const char *content)
  1683. Add a footer line to the response. The strings referenced by
  1684. @var{footer} and @var{content} must be zero-terminated and they are
  1685. duplicated into memory blocks embedded in @var{response}.
  1686. Notice that the strings must not hold newlines, carriage returns or tab
  1687. chars. You can add response footers at any time before signalling the
  1688. end of the response to MHD (not just before calling 'MHD_queue_response').
  1689. Footers are useful for adding cryptographic checksums to the reply or to
  1690. signal errors encountered during data generation. This call was introduced
  1691. in MHD 0.9.3.
  1692. Return @code{MHD_NO} on error (i.e. invalid header or content format or
  1693. memory allocation error).
  1694. @end deftypefun
  1695. @deftypefun int MHD_del_response_header (struct MHD_Response *response, const char *header, const char *content)
  1696. Delete a header (or footer) line from the response. Return @code{MHD_NO} on error
  1697. (arguments are invalid or no such header known).
  1698. @end deftypefun
  1699. @c ------------------------------------------------------------
  1700. @node microhttpd-response options
  1701. @section Setting response options
  1702. @deftypefun int MHD_set_response_options (struct MHD_Response *response, enum MHD_ResponseFlags flags, ...)
  1703. Set special flags and options for a response.
  1704. Calling this functions sets the given flags and options for the response.
  1705. @table @var
  1706. @item response
  1707. which response should be modified;
  1708. @item flags
  1709. flags to set for the response;
  1710. @end table
  1711. Additional arguments are a list of options (type-value pairs,
  1712. terminated with @code{MHD_RO_END}). It is mandatory to use
  1713. @code{MHD_RO_END} as last argument, even when there are no
  1714. additional arguments.
  1715. Return @code{MHD_NO} on error, @code{MHD_YES} on success.
  1716. @end deftypefun
  1717. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1718. @c ------------------------------------------------------------
  1719. @node microhttpd-response inspect
  1720. @section Inspecting a response object
  1721. @deftypefun int MHD_get_response_headers (struct MHD_Response *response, MHD_KeyValueIterator iterator, void *iterator_cls)
  1722. Get all of the headers added to a response.
  1723. Invoke the @var{iterator} callback for each header in the response,
  1724. using @var{iterator_cls} as first argument. Return number of entries
  1725. iterated over. @var{iterator} can be @code{NULL}: in this case the function
  1726. just counts headers.
  1727. @var{iterator} should not modify the its key and value arguments, unless
  1728. we know what we are doing.
  1729. @end deftypefun
  1730. @deftypefun {const char *} MHD_get_response_header (struct MHD_Response *response, const char *key)
  1731. Find and return a pointer to the value of a particular header from the
  1732. response. @var{key} must reference a zero-terminated string
  1733. representing the header to look for. The search is case sensitive.
  1734. Return @code{NULL} if header does not exist or @var{key} is @code{NULL}.
  1735. We should not modify the value, unless we know what we are doing.
  1736. @end deftypefun
  1737. @c ------------------------------------------------------------
  1738. @node microhttpd-response upgrade
  1739. @section Creating a response for protocol upgrades
  1740. @cindex WebSockets
  1741. @cindex Upgrade
  1742. @cindex HTTP2
  1743. @cindex RFC2817
  1744. With RFC 2817 a mechanism to switch protocols within HTTP was
  1745. introduced. Here, a client sends a request with a ``Connection:
  1746. Upgrade'' header. The server responds with a ``101 Switching
  1747. Protocols'' response header, after which the two parties begin to
  1748. speak a different (non-HTTP) protocol over the TCP connection.
  1749. This mechanism is used for upgrading HTTP 1.1 connections to HTTP2 or
  1750. HTTPS, as well as for implementing WebSockets. Which protocol
  1751. upgrade is performed is negotiated between server and client in
  1752. additional headers, in particular the ``Upgrade'' header.
  1753. MHD supports switching protocols using this mechanism only if the
  1754. @code{MHD_ALLOW_SUSPEND_RESUME} flag has been set when starting
  1755. the daemon. If this flag has been set, applications can upgrade
  1756. a connection by queueing a response (using the
  1757. @code{MHD_HTTP_SWITCHING_PROTOCOLS} status code) which must
  1758. have been created with the following function:
  1759. @deftypefun int MHD_create_response_for_upgrade (MHD_UpgradeHandler upgrade_handler, void *upgrade_handler_cls)
  1760. Create a response suitable for switching protocols. Returns @code{MHD_YES} on success. @code{upgrade_handler} must not be @code{NULL}.
  1761. When creating this type of response, the ``Connection: Upgrade''
  1762. header will be set automatically for you. MHD requires that you
  1763. additionally set an ``Upgrade:'' header. The ``Upgrade'' header
  1764. must simply exist, the specific value is completely up to the
  1765. application.
  1766. @end deftypefun
  1767. The @code{upgrade_handler} argument to the above has the following type:
  1768. @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)
  1769. 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:
  1770. @table @var
  1771. @item cls
  1772. matches the @code{upgrade_handler_cls} that was given to @code{MHD_create_response_for_upgrade}
  1773. @item connection
  1774. identifies the connection that is being upgraded;
  1775. @item con_cls
  1776. last value left in `*con_cls` in the `MHD_AccessHandlerCallback`
  1777. @item extra_in
  1778. 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.
  1779. @item extra_in_size
  1780. number of bytes in @code{extra_in}
  1781. @item sock
  1782. 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.
  1783. @item urh
  1784. argument for calls to @code{MHD_upgrade_action}. Applications must eventually use this function to perform the @code{close()} action on the socket.
  1785. @end table
  1786. @end deftypefn
  1787. @deftypefun int MHD_upgrade_action (struct MHD_UpgradeResponseHandle *urh, enum MHD_UpgradeAction action, ...)
  1788. Perform special operations related to upgraded connections.
  1789. @table @var
  1790. @item urh
  1791. identifies the upgraded connection to perform an action on
  1792. @item action
  1793. specifies the action to perform; further arguments to the function depend on the specifics of the action.
  1794. @end table
  1795. @end deftypefun
  1796. @deftp {Enumeration} MHD_UpgradeAction
  1797. Set of actions to be performed on upgraded connections. Passed as an argument to
  1798. @code{MHD_upgrade_action()}.
  1799. @table @code
  1800. @item MHD_UPGRADE_ACTION_CLOSE
  1801. Closes the connection. Must be called once the application is done with the client. Takes no additional arguments.
  1802. @end table
  1803. @end deftp
  1804. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1805. @c ------------------------------------------------------------
  1806. @node microhttpd-flow
  1807. @chapter Flow control.
  1808. @noindent
  1809. Sometimes it may be possible that clients upload data faster
  1810. than an application can process it, or that an application
  1811. needs an extended period of time to generate a response.
  1812. If @code{MHD_USE_THREAD_PER_CONNECTION} is used, applications
  1813. can simply deal with this by performing their logic within the
  1814. thread and thus effectively blocking connection processing
  1815. by MHD. In all other modes, blocking logic must not be
  1816. placed within the callbacks invoked by MHD as this would also
  1817. block processing of other requests, as a single thread may be
  1818. responsible for tens of thousands of connections.
  1819. Instead, applications using thread modes other than
  1820. @code{MHD_USE_THREAD_PER_CONNECTION} should use the
  1821. following functions to perform flow control.
  1822. @deftypefun int MHD_suspend_connection (struct MHD_Connection *connection)
  1823. Suspend handling of network data for a given connection. This can
  1824. be used to dequeue a connection from MHD's event loop (external
  1825. select, internal select or thread pool; not applicable to
  1826. thread-per-connection!) for a while.
  1827. If you use this API in conjunction with a internal select or a
  1828. thread pool, you must set the option @code{MHD_ALLOW_SUSPEND_RESUME} to
  1829. ensure that a resumed connection is immediately processed by MHD.
  1830. Suspended connections continue to count against the total number of
  1831. connections allowed (per daemon, as well as per IP, if such limits
  1832. are set). Suspended connections will NOT time out; timeouts will
  1833. restart when the connection handling is resumed. While a
  1834. connection is suspended, MHD will not detect disconnects by the
  1835. client.
  1836. The only safe time to suspend a connection is from the
  1837. @code{MHD_AccessHandlerCallback} or from the respective
  1838. @code{MHD_ContentReaderCallback} (but in this case the
  1839. response object must not be shared among multiple
  1840. connections).
  1841. Finally, it is an API violation to call @code{MHD_stop_daemon} while
  1842. having suspended connections (this will at least create memory and
  1843. socket leaks or lead to undefined behavior). You must explicitly
  1844. resume all connections before stopping the daemon.
  1845. @table @var
  1846. @item connection
  1847. the connection to suspend
  1848. @end table
  1849. @end deftypefun
  1850. @deftypefun int MHD_resume_connection (struct MHD_Connection *connection)
  1851. Resume handling of network data for suspended connection. It is safe
  1852. to resume a suspended connection at any time. Calling this function
  1853. on a connection that was not previously suspended will result in
  1854. undefined behavior.
  1855. If you are using this function in ``external'' select mode, you must
  1856. make sure to run @code{MHD_run} afterwards (before again calling
  1857. @code{MHD_get_fdset}), as otherwise the change may not be reflected in
  1858. the set returned by @code{MHD_get_fdset} and you may end up with a
  1859. connection that is stuck until the next network activity.
  1860. You can check whether a connection is currently suspended using
  1861. @code{MHD_get_connection_info} by querying for
  1862. @code{MHD_CONNECTION_INFO_CONNECTION_SUSPENDED}.
  1863. @table @var
  1864. @item connection
  1865. the connection to resume
  1866. @end table
  1867. @end deftypefun
  1868. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1869. @c ------------------------------------------------------------
  1870. @node microhttpd-dauth
  1871. @chapter Utilizing Authentication
  1872. @noindent
  1873. MHD support three types of client authentication.
  1874. Basic authentication uses a simple authentication method based
  1875. on BASE64 algorithm. Username and password are exchanged in clear
  1876. between the client and the server, so this method must only be used
  1877. for non-sensitive content or when the session is protected with https.
  1878. When using basic authentication MHD will have access to the clear
  1879. password, possibly allowing to create a chained authentication
  1880. toward an external authentication server.
  1881. Digest authentication uses a one-way authentication method based
  1882. on MD5 hash algorithm. Only the hash will transit over the network,
  1883. hence protecting the user password. The nonce will prevent replay
  1884. attacks. This method is appropriate for general use, especially
  1885. when https is not used to encrypt the session.
  1886. Client certificate authentication uses a X.509 certificate from
  1887. the client. This is the strongest authentication mechanism but it
  1888. requires the use of HTTPS. Client certificate authentication can
  1889. be used simultaneously with Basic or Digest Authentication in order
  1890. to provide a two levels authentication (like for instance separate
  1891. machine and user authentication). A code example for using
  1892. client certificates is presented in the MHD tutorial.
  1893. @menu
  1894. * microhttpd-dauth basic:: Using Basic Authentication.
  1895. * microhttpd-dauth digest:: Using Digest Authentication.
  1896. @end menu
  1897. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1898. @c ------------------------------------------------------------
  1899. @node microhttpd-dauth basic
  1900. @section Using Basic Authentication
  1901. @deftypefun {void} MHD_free (void *ptr)
  1902. Free the memory given at @code{ptr}. Used to free data structures allocated by MHD. Calls @code{free(ptr)}.
  1903. @end deftypefun
  1904. @deftypefun {char *} MHD_basic_auth_get_username_password (struct MHD_Connection *connection, char** password)
  1905. Get the username and password from the basic authorization header sent by the client.
  1906. Return @code{NULL} if no username could be found, a pointer to the username if found.
  1907. If returned value is not @code{NULL}, the value must be @code{MHD_free()}'ed.
  1908. @var{password} reference a buffer to store the password. It can be @code{NULL}.
  1909. If returned value is not @code{NULL}, the value must be @code{MHD_free()}'ed.
  1910. @end deftypefun
  1911. @deftypefun {int} MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection, const char *realm, struct MHD_Response *response)
  1912. Queues a response to request basic authentication from the client.
  1913. Return @code{MHD_YES} if successful, otherwise @code{MHD_NO}.
  1914. @var{realm} must reference to a zero-terminated string representing the realm.
  1915. @var{response} a response structure to specify what shall be presented to the
  1916. client with a 401 HTTP status.
  1917. @end deftypefun
  1918. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1919. @c ------------------------------------------------------------
  1920. @node microhttpd-dauth digest
  1921. @section Using Digest Authentication
  1922. MHD supports MD5 (deprecated by IETF) and SHA-256 hash algorithms
  1923. for digest authentication. The @code{MHD_DigestAuthAlgorithm} enumeration
  1924. is used to specify which algorithm should be used.
  1925. @deftp {Enumeration} MHD_DigestAuthAlgorithm
  1926. Which digest algorithm should be used. Must be used consistently.
  1927. @table @code
  1928. @item MHD_DIGEST_ALG_AUTO
  1929. Have MHD pick an algorithm currently considered secure. For now defaults to SHA-256.
  1930. @item MHD_DIGEST_ALG_MD5
  1931. Force use of (deprecated, ancient, insecure) MD5.
  1932. @item MHD_DIGEST_ALG_SHA256
  1933. Force use of SHA-256.
  1934. @end table
  1935. @end deftp
  1936. @deftypefun {char *} MHD_digest_auth_get_username (struct MHD_Connection *connection)
  1937. Find and return a pointer to the username value from the request header.
  1938. Return @code{NULL} if the value is not found or header does not exist.
  1939. If returned value is not @code{NULL}, the value must be @code{MHD_free()}'ed.
  1940. @end deftypefun
  1941. @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)
  1942. Checks if the provided values in the WWW-Authenticate header are valid
  1943. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  1944. @var{realm} must reference to a zero-terminated string representing the realm.
  1945. @var{username} must reference to a zero-terminated string representing the username,
  1946. it is usually the returned value from MHD_digest_auth_get_username.
  1947. @var{password} must reference to a zero-terminated string representing the password,
  1948. most probably it will be the result of a lookup of the username against a local database.
  1949. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  1950. Most of the time it is sound to specify 300 seconds as its values.
  1951. @var{algo} which digest algorithm should we use.
  1952. @end deftypefun
  1953. @deftypefun int MHD_digest_auth_check (struct MHD_Connection *connection, const char *realm, const char *username, const char *password, unsigned int nonce_timeout)
  1954. Checks if the provided values in the WWW-Authenticate header are valid
  1955. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  1956. Deprecated, use @code{MHD_digest_auth_check2} instead.
  1957. @var{realm} must reference to a zero-terminated string representing the realm.
  1958. @var{username} must reference to a zero-terminated string representing the username,
  1959. it is usually the returned value from MHD_digest_auth_get_username.
  1960. @var{password} must reference to a zero-terminated string representing the password,
  1961. most probably it will be the result of a lookup of the username against a local database.
  1962. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  1963. Most of the time it is sound to specify 300 seconds as its values.
  1964. @end deftypefun
  1965. @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)
  1966. Checks if the provided values in the WWW-Authenticate header are valid
  1967. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  1968. @var{realm} must reference to a zero-terminated string representing the realm.
  1969. @var{username} must reference to a zero-terminated string representing the username,
  1970. it is usually the returned value from MHD_digest_auth_get_username.
  1971. @var{digest} pointer to the binary MD5 sum for the precalculated hash value ``userame:realm:password''. The size must match the selected @var{algo}!
  1972. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  1973. Most of the time it is sound to specify 300 seconds as its values.
  1974. @var{algo} digest authentication algorithm to use.
  1975. @end deftypefun
  1976. @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)
  1977. Checks if the provided values in the WWW-Authenticate header are valid
  1978. and sound according to RFC2716. If valid return @code{MHD_YES}, otherwise return @code{MHD_NO}.
  1979. Deprecated, use @code{MHD_digest_auth_check_digest2} instead.
  1980. @var{realm} must reference to a zero-terminated string representing the realm.
  1981. @var{username} must reference to a zero-terminated string representing the username,
  1982. it is usually the returned value from MHD_digest_auth_get_username.
  1983. @var{digest} pointer to the binary MD5 sum for the precalculated hash value ``userame:realm:password'' of @code{MHD_MD5_DIGEST_SIZE} bytes.
  1984. @var{nonce_timeout} is the amount of time in seconds for a nonce to be invalid.
  1985. Most of the time it is sound to specify 300 seconds as its values.
  1986. @end deftypefun
  1987. @deftypefun int 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)
  1988. Queues a response to request authentication from the client,
  1989. return @code{MHD_YES} if successful, otherwise @code{MHD_NO}.
  1990. @var{realm} must reference to a zero-terminated string representing the realm.
  1991. @var{opaque} must reference to a zero-terminated string representing a value
  1992. that gets passed to the client and expected to be passed again to the server
  1993. as-is. This value can be a hexadecimal or base64 string.
  1994. @var{response} a response structure to specify what shall be presented to the
  1995. client with a 401 HTTP status.
  1996. @var{signal_stale} a value that signals "stale=true" in the response header to
  1997. indicate the invalidity of the nonce and no need to ask for authentication
  1998. parameters and only a new nonce gets generated. @code{MHD_YES} to generate a new
  1999. nonce, @code{MHD_NO} to ask for authentication parameters.
  2000. @var{algo} which digest algorithm should we use. The same algorithm
  2001. must then be selected when checking digests received from clients!
  2002. @end deftypefun
  2003. @deftypefun int MHD_queue_auth_fail_response (struct MHD_Connection *connection, const char *realm, const char *opaque, struct MHD_Response *response, int signal_stale)
  2004. Queues a response to request authentication from the client,
  2005. return @code{MHD_YES} if successful, otherwise @code{MHD_NO}.
  2006. @var{realm} must reference to a zero-terminated string representing the realm.
  2007. @var{opaque} must reference to a zero-terminated string representing a value
  2008. that gets passed to the client and expected to be passed again to the server
  2009. as-is. This value can be a hexadecimal or base64 string.
  2010. @var{response} a response structure to specify what shall be presented to the
  2011. client with a 401 HTTP status.
  2012. @var{signal_stale} a value that signals "stale=true" in the response header to
  2013. indicate the invalidity of the nonce and no need to ask for authentication
  2014. parameters and only a new nonce gets generated. @code{MHD_YES} to generate a new
  2015. nonce, @code{MHD_NO} to ask for authentication parameters.
  2016. @end deftypefun
  2017. Example: handling digest authentication requests and responses.
  2018. @example
  2019. #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>"
  2020. #define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>"
  2021. #define OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
  2022. static int
  2023. ahc_echo (void *cls,
  2024. struct MHD_Connection *connection,
  2025. const char *url,
  2026. const char *method,
  2027. const char *version,
  2028. const char *upload_data, size_t *upload_data_size, void **ptr)
  2029. @{
  2030. struct MHD_Response *response;
  2031. char *username;
  2032. const char *password = "testpass";
  2033. const char *realm = "test@@example.com";
  2034. int ret;
  2035. username = MHD_digest_auth_get_username (connection);
  2036. if (username == NULL)
  2037. @{
  2038. response = MHD_create_response_from_buffer(strlen (DENIED),
  2039. DENIED,
  2040. MHD_RESPMEM_PERSISTENT);
  2041. ret = MHD_queue_auth_fail_response2 (connection,
  2042. realm,
  2043. OPAQUE,
  2044. response,
  2045. MHD_NO,
  2046. MHD_DIGEST_ALG_SHA256);
  2047. MHD_destroy_response(response);
  2048. return ret;
  2049. @}
  2050. ret = MHD_digest_auth_check2 (connection,
  2051. realm,
  2052. username,
  2053. password,
  2054. 300,
  2055. MHD_DIGEST_ALG_SHA256);
  2056. free(username);
  2057. if ( (ret == MHD_INVALID_NONCE) ||
  2058. (ret == MHD_NO) )
  2059. @{
  2060. response = MHD_create_response_from_buffer(strlen (DENIED),
  2061. DENIED,
  2062. MHD_RESPMEM_PERSISTENT);
  2063. if (NULL == response)
  2064. return MHD_NO;
  2065. ret = MHD_queue_auth_fail_response2 (connection,
  2066. realm,
  2067. OPAQUE,
  2068. response,
  2069. (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO,
  2070. MHD_DIGEST_ALG_SHA256);
  2071. MHD_destroy_response(response);
  2072. return ret;
  2073. @}
  2074. response = MHD_create_response_from_buffer (strlen(PAGE),
  2075. PAGE,
  2076. MHD_RESPMEM_PERSISTENT);
  2077. ret = MHD_queue_response (connection,
  2078. MHD_HTTP_OK,
  2079. response);
  2080. MHD_destroy_response(response);
  2081. return ret;
  2082. @}
  2083. @end example
  2084. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2085. @c ------------------------------------------------------------
  2086. @node microhttpd-post
  2087. @chapter Adding a @code{POST} processor
  2088. @cindex POST method
  2089. @menu
  2090. * microhttpd-post api:: Programming interface for the
  2091. @code{POST} processor.
  2092. @end menu
  2093. @noindent
  2094. MHD provides the post processor API to make it easier for applications to
  2095. parse the data of a client's @code{POST} request: the
  2096. @code{MHD_AccessHandlerCallback} will be invoked multiple times to
  2097. process data as it arrives; at each invocation a new chunk of data must
  2098. be processed. The arguments @var{upload_data} and @var{upload_data_size}
  2099. are used to reference the chunk of data.
  2100. When @code{MHD_AccessHandlerCallback} is invoked for a new connection:
  2101. its @code{*@var{con_cls}} argument is set to @code{NULL}. When @code{POST}
  2102. data comes in the upload buffer it is @strong{mandatory} to use the
  2103. @var{con_cls} to store a reference to per-connection data. The fact
  2104. that the pointer was initially @code{NULL} can be used to detect that
  2105. this is a new request.
  2106. One method to detect that a new connection was established is
  2107. to set @code{*con_cls} to an unused integer:
  2108. @example
  2109. int
  2110. access_handler (void *cls,
  2111. struct MHD_Connection * connection,
  2112. const char *url,
  2113. const char *method, const char *version,
  2114. const char *upload_data, size_t *upload_data_size,
  2115. void **con_cls)
  2116. @{
  2117. static int old_connection_marker;
  2118. int new_connection = (NULL == *con_cls);
  2119. if (new_connection)
  2120. @{
  2121. /* new connection with POST */
  2122. *con_cls = &old_connection_marker;
  2123. @}
  2124. ...
  2125. @}
  2126. @end example
  2127. @noindent
  2128. In contrast to the previous example, for @code{POST} requests in particular,
  2129. it is more common to use the value of @code{*con_cls} to keep track of
  2130. actual state used during processing, such as the post processor (or a
  2131. struct containing a post processor):
  2132. @example
  2133. int
  2134. access_handler (void *cls,
  2135. struct MHD_Connection * connection,
  2136. const char *url,
  2137. const char *method, const char *version,
  2138. const char *upload_data, size_t *upload_data_size,
  2139. void **con_cls)
  2140. @{
  2141. struct MHD_PostProcessor * pp = *con_cls;
  2142. if (pp == NULL)
  2143. @{
  2144. pp = MHD_create_post_processor(connection, ...);
  2145. *con_cls = pp;
  2146. return MHD_YES;
  2147. @}
  2148. if (*upload_data_size)
  2149. @{
  2150. MHD_post_process(pp, upload_data, *upload_data_size);
  2151. *upload_data_size = 0;
  2152. return MHD_YES;
  2153. @}
  2154. else
  2155. @{
  2156. MHD_destroy_post_processor(pp);
  2157. return MHD_queue_response(...);
  2158. @}
  2159. @}
  2160. @end example
  2161. Note that the callback from @code{MHD_OPTION_NOTIFY_COMPLETED}
  2162. should be used to destroy the post processor. This cannot be
  2163. done inside of the access handler since the connection may not
  2164. always terminate normally.
  2165. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2166. @c ------------------------------------------------------------
  2167. @node microhttpd-post api
  2168. @section Programming interface for the @code{POST} processor
  2169. @cindex POST method
  2170. @deftypefun {struct MHD_PostProcessor *} MHD_create_post_processor (struct MHD_Connection *connection, size_t buffer_size, MHD_PostDataIterator iterator, void *iterator_cls)
  2171. Create a PostProcessor. A PostProcessor can be used to (incrementally)
  2172. parse the data portion of a @code{POST} request.
  2173. @table @var
  2174. @item connection
  2175. the connection on which the @code{POST} is happening (used to determine
  2176. the @code{POST} format);
  2177. @item buffer_size
  2178. maximum number of bytes to use for internal buffering (used only for the
  2179. parsing, specifically the parsing of the keys). A tiny value (256-1024)
  2180. should be sufficient; do @strong{NOT} use a value smaller than 256;
  2181. for good performance, use 32k or 64k (i.e. 65536).
  2182. @item iterator
  2183. iterator to be called with the parsed data; must @strong{NOT} be
  2184. @code{NULL};
  2185. @item iterator_cls
  2186. custom value to be used as first argument to @var{iterator}.
  2187. @end table
  2188. Return @code{NULL} on error (out of memory, unsupported encoding), otherwise
  2189. a PP handle.
  2190. @end deftypefun
  2191. @deftypefun int MHD_post_process (struct MHD_PostProcessor *pp, const char *post_data, size_t post_data_len)
  2192. Parse and process @code{POST} data. Call this function when @code{POST}
  2193. data is available (usually during an @code{MHD_AccessHandlerCallback})
  2194. with the @var{upload_data} and @var{upload_data_size}. Whenever
  2195. possible, this will then cause calls to the
  2196. @code{MHD_IncrementalKeyValueIterator}.
  2197. @table @var
  2198. @item pp
  2199. the post processor;
  2200. @item post_data
  2201. @var{post_data_len} bytes of @code{POST} data;
  2202. @item post_data_len
  2203. length of @var{post_data}.
  2204. @end table
  2205. Return @code{MHD_YES} on success, @code{MHD_NO} on error
  2206. (out-of-memory, iterator aborted, parse error).
  2207. @end deftypefun
  2208. @deftypefun int MHD_destroy_post_processor (struct MHD_PostProcessor *pp)
  2209. Release PostProcessor resources. After this function is being called,
  2210. the PostProcessor is guaranteed to no longer call its iterator. There
  2211. is no special call to the iterator to indicate the end of the post processing
  2212. stream. After destroying the PostProcessor, the programmer should
  2213. perform any necessary work to complete the processing of the iterator.
  2214. Return @code{MHD_YES} if processing completed nicely, @code{MHD_NO}
  2215. if there were spurious characters or formatting problems with
  2216. the post request. It is common to ignore the return value
  2217. of this function.
  2218. @end deftypefun
  2219. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2220. @c ------------------------------------------------------------
  2221. @node microhttpd-info
  2222. @chapter Obtaining and modifying status information.
  2223. @menu
  2224. * microhttpd-info daemon:: State information about an MHD daemon
  2225. * microhttpd-info conn:: State information about a connection
  2226. * microhttpd-option conn:: Modify per-connection options
  2227. @end menu
  2228. @c ------------------------------------------------------------
  2229. @node microhttpd-info daemon
  2230. @section Obtaining state information about an MHD daemon
  2231. @deftypefun {const union MHD_DaemonInfo *} MHD_get_daemon_info (struct MHD_Daemon *daemon, enum MHD_DaemonInfoType infoType, ...)
  2232. Obtain information about the given daemon. This function
  2233. is currently not fully implemented.
  2234. @table @var
  2235. @item daemon
  2236. the daemon about which information is desired;
  2237. @item infoType
  2238. type of information that is desired
  2239. @item ...
  2240. additional arguments about the desired information (depending on
  2241. infoType)
  2242. @end table
  2243. Returns a union with the respective member (depending on
  2244. infoType) set to the desired information), or @code{NULL}
  2245. in case the desired information is not available or
  2246. applicable.
  2247. @end deftypefun
  2248. @deftp {Enumeration} MHD_DaemonInfoType
  2249. Values of this enum are used to specify what
  2250. information about a daemon is desired.
  2251. @table @code
  2252. @item MHD_DAEMON_INFO_KEY_SIZE
  2253. Request information about the key size for a particular cipher
  2254. algorithm. The cipher algorithm should be passed as an extra argument
  2255. (of type 'enum MHD_GNUTLS_CipherAlgorithm'). No longer supported,
  2256. using this value will cause @code{MHD_get_daemon_info} to return NULL.
  2257. @item MHD_DAEMON_INFO_MAC_KEY_SIZE
  2258. Request information about the key size for a particular cipher
  2259. algorithm. The cipher algorithm should be passed as an extra argument
  2260. (of type 'enum MHD_GNUTLS_HashAlgorithm'). No longer supported,
  2261. using this value will cause @code{MHD_get_daemon_info} to return NULL.
  2262. @item MHD_DAEMON_INFO_LISTEN_FD
  2263. @cindex listen
  2264. Request the file-descriptor number that MHD is using to listen to the
  2265. server socket. This can be useful if no port
  2266. was specified and a client needs to learn what port
  2267. is actually being used by MHD.
  2268. No extra arguments should be passed.
  2269. @item MHD_DAEMON_INFO_EPOLL_FD
  2270. @cindex epoll
  2271. Request the file-descriptor number that MHD is using for epoll. If
  2272. the build is not supporting epoll, NULL is returned; if we are using a
  2273. thread pool or this daemon was not started with
  2274. @code{MHD_USE_EPOLL}, (a pointer to) -1 is returned. If we are
  2275. using @code{MHD_USE_INTERNAL_POLLING_THREAD} or are in 'external' select mode, the
  2276. internal epoll FD is returned. This function must be used in external
  2277. select mode with epoll to obtain the FD to call epoll on. No extra
  2278. arguments should be passed.
  2279. @item MHD_DAEMON_INFO_CURRENT_CONNECTIONS
  2280. @cindex connection, limiting number of connections
  2281. Request the number of current connections handled by the daemon. No
  2282. extra arguments should be passed and a pointer to a @code{union
  2283. MHD_DaemonInfo} value is returned, with the @code{num_connections}
  2284. member of type @code{unsigned int} set to the number of active
  2285. connections.
  2286. Note that in multi-threaded or internal-select mode, the real number of current
  2287. connections may already be different when @code{MHD_get_daemon_info} returns.
  2288. The number of current connections can be used (even in multi-threaded and
  2289. internal-select mode) after @code{MHD_quiesce_daemon} to detect whether all
  2290. connections have been handled.
  2291. @end table
  2292. @end deftp
  2293. @c ------------------------------------------------------------
  2294. @node microhttpd-info conn
  2295. @section Obtaining state information about a connection
  2296. @deftypefun {const union MHD_ConnectionInfo *} MHD_get_connection_info (struct MHD_Connection *daemon, enum MHD_ConnectionInfoType infoType, ...)
  2297. Obtain information about the given connection.
  2298. @table @var
  2299. @item connection
  2300. the connection about which information is desired;
  2301. @item infoType
  2302. type of information that is desired
  2303. @item ...
  2304. additional arguments about the desired information (depending on
  2305. infoType)
  2306. @end table
  2307. Returns a union with the respective member (depending on
  2308. infoType) set to the desired information), or @code{NULL}
  2309. in case the desired information is not available or
  2310. applicable.
  2311. @end deftypefun
  2312. @deftp {Enumeration} MHD_ConnectionInfoType
  2313. Values of this enum are used to specify what information about a
  2314. connection is desired.
  2315. @table @code
  2316. @item MHD_CONNECTION_INFO_CIPHER_ALGO
  2317. What cipher algorithm is being used (HTTPS connections only).
  2318. @code{NULL} is returned for non-HTTPS connections.
  2319. Takes no extra arguments.
  2320. @item MHD_CONNECTION_INFO_PROTOCOL,
  2321. Allows finding out the TLS/SSL protocol used
  2322. (HTTPS connections only).
  2323. @code{NULL} is returned for non-HTTPS connections.
  2324. Takes no extra arguments.
  2325. @item MHD_CONNECTION_INFO_CLIENT_ADDRESS
  2326. Returns information about the address of the client. Returns
  2327. essentially a @code{struct sockaddr **} (since the API returns
  2328. a @code{union MHD_ConnectionInfo *} and that union contains
  2329. a @code{struct sockaddr *}).
  2330. Takes no extra arguments.
  2331. @item MHD_CONNECTION_INFO_GNUTLS_SESSION,
  2332. Takes no extra arguments. Allows access to the underlying GNUtls session,
  2333. including access to the underlying GNUtls client certificate
  2334. (HTTPS connections only). Takes no extra arguments.
  2335. @code{NULL} is returned for non-HTTPS connections.
  2336. Takes no extra arguments.
  2337. @item MHD_CONNECTION_INFO_GNUTLS_CLIENT_CERT,
  2338. Dysfunctional (never implemented, deprecated). Use
  2339. MHD_CONNECTION_INFO_GNUTLS_SESSION to get the @code{gnutls_session_t}
  2340. and then call @code{gnutls_certificate_get_peers()}.
  2341. @item MHD_CONNECTION_INFO_DAEMON
  2342. Returns information about @code{struct MHD_Daemon} which manages
  2343. this connection.
  2344. Takes no extra arguments.
  2345. @item MHD_CONNECTION_INFO_CONNECTION_FD
  2346. Returns the file descriptor (usually a TCP socket) associated with
  2347. this connection (in the ``connect-fd'' member of the returned struct).
  2348. Note that manipulating the descriptor directly can have problematic
  2349. consequences (as in, break HTTP). Applications might use this access
  2350. to manipulate TCP options, for example to set the ``TCP-NODELAY''
  2351. option for COMET-like applications. Note that MHD will set TCP-CORK
  2352. after sending the HTTP header and clear it after finishing the footers
  2353. automatically (if the platform supports it). As the connection
  2354. callbacks are invoked in between, those might be used to set different
  2355. values for TCP-CORK and TCP-NODELAY in the meantime.
  2356. Takes no extra arguments.
  2357. @item MHD_CONNECTION_INFO_CONNECTION_SUSPENDED
  2358. Returns pointer to an integer that is @code{MHD_YES} if the connection
  2359. is currently suspended (and thus can be safely resumed) and
  2360. @code{MHD_NO} otherwise.
  2361. Takes no extra arguments.
  2362. @item MHD_CONNECTION_INFO_SOCKET_CONTEXT
  2363. Returns the client-specific pointer to a @code{void *} that was
  2364. (possibly) set during a @code{MHD_NotifyConnectionCallback} when the
  2365. socket was first accepted. Note that this is NOT the same as the
  2366. @code{con_cls} argument of the @code{MHD_AccessHandlerCallback}. The
  2367. @code{con_cls} is fresh for each HTTP request, while the
  2368. @code{socket_context} is fresh for each socket.
  2369. Takes no extra arguments.
  2370. @item MHD_CONNECTION_INFO_CONNECTION_TIMEOUT
  2371. Returns pointer to an @code{unsigned int} that is the current timeout
  2372. used for the connection (in seconds, 0 for no timeout). Note that
  2373. while suspended connections will not timeout, the timeout value
  2374. returned for suspended connections will be the timeout that the
  2375. connection will use after it is resumed, and thus might not be zero.
  2376. Takes no extra arguments.
  2377. @item MHD_CONNECTION_INFO_REQUEST_HEADER_SIZE
  2378. @cindex performance
  2379. Returns pointer to an @code{size_t} that represents the size of the
  2380. HTTP header received from the client. Only valid after the first callback
  2381. to the access handler.
  2382. Takes no extra arguments.
  2383. @end table
  2384. @end deftp
  2385. @c ------------------------------------------------------------
  2386. @node microhttpd-option conn
  2387. @section Setting custom options for an individual connection
  2388. @cindex timeout
  2389. @deftypefun {int} MHD_set_connection_option (struct MHD_Connection *daemon, enum MHD_CONNECTION_OPTION option, ...)
  2390. Set a custom option for the given connection.
  2391. @table @var
  2392. @item connection
  2393. the connection for which an option should be set or modified;
  2394. @item option
  2395. option to set
  2396. @item ...
  2397. additional arguments for the option (depending on option)
  2398. @end table
  2399. Returns @code{MHD_YES} on success, @code{MHD_NO} for errors
  2400. (i.e. option argument invalid or option unknown).
  2401. @end deftypefun
  2402. @deftp {Enumeration} MHD_CONNECTION_OPTION
  2403. Values of this enum are used to specify which option for a
  2404. connection should be changed.
  2405. @table @code
  2406. @item MHD_CONNECTION_OPTION_TIMEOUT
  2407. Set a custom timeout for the given connection. Specified
  2408. as the number of seconds, given as an @code{unsigned int}. Use
  2409. zero for no timeout.
  2410. @end table
  2411. @end deftp
  2412. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2413. @c ------------------------------------------------------------
  2414. @node microhttpd-util
  2415. @chapter Utility functions.
  2416. @menu
  2417. * microhttpd-util feature:: Test supported MHD features
  2418. * microhttpd-util unescape:: Unescape strings
  2419. @end menu
  2420. @c ------------------------------------------------------------
  2421. @node microhttpd-util feature
  2422. @section Testing for supported MHD features
  2423. @deftp {Enumeration} MHD_FEATURE
  2424. Values of this enum are used to specify what
  2425. information about a daemon is desired.
  2426. @table @code
  2427. @item MHD_FEATURE_MESSAGES
  2428. Get whether messages are supported. If supported then in debug
  2429. mode messages can be printed to stderr or to external logger.
  2430. @item MHD_FEATURE_SSL
  2431. Get whether HTTPS is supported. If supported then flag
  2432. MHD_USE_SSL and options MHD_OPTION_HTTPS_MEM_KEY,
  2433. MHD_OPTION_HTTPS_MEM_CERT, MHD_OPTION_HTTPS_MEM_TRUST,
  2434. MHD_OPTION_HTTPS_MEM_DHPARAMS, MHD_OPTION_HTTPS_CRED_TYPE,
  2435. MHD_OPTION_HTTPS_PRIORITIES can be used.
  2436. @item MHD_FEATURE_HTTPS_CERT_CALLBACK
  2437. Get whether option #MHD_OPTION_HTTPS_CERT_CALLBACK is
  2438. supported.
  2439. @item MHD_FEATURE_IPv6
  2440. Get whether IPv6 is supported. If supported then flag
  2441. MHD_USE_IPv6 can be used.
  2442. @item MHD_FEATURE_IPv6_ONLY
  2443. Get whether IPv6 without IPv4 is supported. If not supported
  2444. then IPv4 is always enabled in IPv6 sockets and
  2445. flag MHD_USE_DUAL_STACK if always used when MHD_USE_IPv6 is
  2446. specified.
  2447. @item MHD_FEATURE_POLL
  2448. Get whether @code{poll()} is supported. If supported then flag
  2449. MHD_USE_POLL can be used.
  2450. @item MHD_FEATURE_EPOLL
  2451. Get whether @code{epoll()} is supported. If supported then Flags
  2452. MHD_USE_EPOLL and
  2453. MHD_USE_EPOLL_INTERNAL_THREAD can be used.
  2454. @item MHD_FEATURE_SHUTDOWN_LISTEN_SOCKET
  2455. Get whether shutdown on listen socket to signal other
  2456. threads is supported. If not supported flag
  2457. MHD_USE_ITC is automatically forced.
  2458. @item MHD_FEATURE_SOCKETPAIR
  2459. Get whether a @code{socketpair()} is used internally instead of
  2460. a @code{pipe()} to signal other threads.
  2461. @item MHD_FEATURE_TCP_FASTOPEN
  2462. Get whether TCP Fast Open is supported. If supported then
  2463. flag MHD_USE_TCP_FASTOPEN and option
  2464. MHD_OPTION_TCP_FASTOPEN_QUEUE_SIZE can be used.
  2465. @item MHD_FEATURE_BASIC_AUTH
  2466. Get whether HTTP Basic authorization is supported. If supported
  2467. then functions @code{MHD_basic_auth_get_username_password()} and
  2468. @code{MHD_queue_basic_auth_fail_response()} can be used.
  2469. @item MHD_FEATURE_DIGEST_AUTH
  2470. Get whether HTTP Digest authorization is supported. If
  2471. supported then options MHD_OPTION_DIGEST_AUTH_RANDOM,
  2472. MHD_OPTION_NONCE_NC_SIZE and functions @code{MHD_digest_auth_check()},
  2473. can be used.
  2474. @item MHD_FEATURE_POSTPROCESSOR
  2475. Get whether postprocessor is supported. If supported then
  2476. functions @code{MHD_create_post_processor()},
  2477. @code{MHD_post_process()}, @code{MHD_destroy_post_processor()}
  2478. can be used.
  2479. @item MHD_FEATURE_SENDFILE
  2480. Get whether @code{sendfile()} is supported.
  2481. @end table
  2482. @end deftp
  2483. @deftypefun {int} MHD_is_feature_supported (enum MHD_FEATURE feature)
  2484. Get information about supported MHD features. Indicate that MHD was
  2485. compiled with or without support for particular feature. Some features
  2486. require additional support by the kernel. However, kernel support is not
  2487. checked by this function.
  2488. @table @var
  2489. @item feature
  2490. type of requested information
  2491. @end table
  2492. Returns @code{MHD_YES} if the feature is supported,
  2493. and @code{MHD_NO} if not.
  2494. @end deftypefun
  2495. @c ------------------------------------------------------------
  2496. @node microhttpd-util unescape
  2497. @section Unescape strings
  2498. @deftypefun {size_t} MHD_http_unescape (char *val)
  2499. Process escape sequences ('%HH') Updates val in place; the result
  2500. should be UTF-8 encoded and cannot be larger than the input. The
  2501. result must also still be 0-terminated.
  2502. @table @var
  2503. @item val
  2504. value to unescape (modified in the process), must be
  2505. a 0-terminated UTF-8 string.
  2506. @end table
  2507. Returns length of the resulting val (@code{strlen(val)} may be
  2508. shorter afterwards due to elimination of escape sequences).
  2509. @end deftypefun
  2510. @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2511. @c **********************************************************
  2512. @c ******************* Appendices *************************
  2513. @c **********************************************************
  2514. @node GNU-LGPL
  2515. @unnumbered GNU-LGPL
  2516. @cindex license
  2517. @include lgpl.texi
  2518. @node GNU GPL with eCos Extension
  2519. @unnumbered GNU GPL with eCos Extension
  2520. @cindex license
  2521. @include ecos.texi
  2522. @node GNU-FDL
  2523. @unnumbered GNU-FDL
  2524. @cindex license
  2525. @include fdl-1.3.texi
  2526. @node Concept Index
  2527. @unnumbered Concept Index
  2528. @printindex cp
  2529. @node Function and Data Index
  2530. @unnumbered Function and Data Index
  2531. @printindex fn
  2532. @node Type Index
  2533. @unnumbered Type Index
  2534. @printindex tp
  2535. @bye