libmicrohttpd.texi 110 KB

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