2
0

libmicrohttpd.texi 115 KB

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