libmicrohttpd.texi 104 KB

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