libmicrohttpd.texi 111 KB

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