libmicrohttpd.texi 112 KB

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