libmicrohttpd.texi 112 KB

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