libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

introduction.inc (15653B)


      1 @noindent
      2 
      3 GNU libmicrohttpd (MHD) is a small HTTP daemon library implementing
      4 the server-side functionality of the HTTP protocol.  MHD is an
      5 official GNU package and part of the GNU project, bringing you
      6 Free/Libre Software since 1983. MHD is released under the GNU
      7 Lesser General Public License (LGPLv3 or later)  and gives you
      8 the freedoms to run, copy, distribute, study, change and improve
      9 the software for yourself and others. MHD without TLS support
     10 is also dual-licensed under eCos license (the GPLv3+ with the
     11 eCOS exception).
     12 
     13 Many of MHD's features are optional and can be turned off using
     14 compiler flags. Features include:
     15 
     16 @itemize @bullet
     17 @item Supports HTTP 1.0, HTTP/1.1 and (soon) HTTP2
     18 @item Basic and digest authentication
     19 @item TLS via OpenSSL or libgnutls (mbedTLS planned)
     20 @item Websockets
     21 @item Different threading modes (select, poll, epoll, thread-per-connection, thread-pool)
     22 @item Stream processing (arbitrary size uploads)
     23 @item Chunked encoding, HTTP pipelining
     24 @item Limiting connections per client IP address
     25 @item Use of @code{sendfile()}
     26 @end itemize
     27 
     28 This manual is for libmicrohttpd2, a major new release of the
     29 library. It is still not battle-tested (compared to the previous)
     30 version, and we would be excited to hear back from you about
     31 real-world use!  The previous implementation of MHD is used in a wide
     32 range of implementations.  Examples based on reports we've received
     33 from developers include:
     34 
     35 @itemize
     36 @item Embedded HTTP server on a cortex M3 (128 KB code space)
     37 @item Large-scale multimedia server (reportedly serving at the
     38       simulator limit of 7.5 GB/s)
     39 @item Administrative console (via HTTP/HTTPS) for network appliances
     40 @item Streaming Earth observation data from NASA to the Internet
     41 @end itemize
     42 
     43 The remainder of this chapter will give a high-level overview of
     44 the library.
     45 
     46 @menu
     47 * libmicrohttpd-introduction-limitations::  Library limitations.
     48 * libmicrohttpd-introduction-threadmodes::  Thread modes and event loops.
     49 * libmicrohttpd-introduction-compiling::    Compiling libmicrohttpd.
     50 * libmicrohttpd-introduction-pointers::     Using MHD data.
     51 * libmicrohttpd-introduction-sigpipe::      SIGPIPE.
     52 * libmicrohttpd-introduction-w32::          Portability to W32.
     53 * libmicrohttpd-introduction-zos::          Portability to z/OS.
     54 @end menu
     55 
     56 
     57 @node libmicrohttpd-introduction-limitations
     58 @section Limitations
     59 
     60 MHD is supposed to handle everything that it must handle (because the
     61 API would not allow clients to do this), such as basic connection
     62 management. However, some detailed interpretations of HTTP headers, such as
     63 range requests, are deliberately left to the main application.  In particular,
     64 if an application developer wants to support range requests, he needs
     65 to explicitly indicate support in responses and also explicitly parse
     66 the range header and generate a suitable response.
     67 @ref{libmicrohttpd2-requests,,Accessing client request data} explains how applications can learn
     68 everything about incoming HTTP requests.
     69 
     70 MHD does understands HTTP headers that control connection management
     71 (specifically, @code{Connection: close} and @code{Expect: 100
     72 continue} are understood and handled automatically).
     73 @code{Connection: upgrade} is supported by passing control over
     74 the communication to the application (after sending the HTTP
     75 upgrade response header).
     76 @ref{libmicrohttpd-upgrade,Upgrading HTTP connections} explains how to implement connection
     77 upgrades (for example, to implement WebSockets).
     78 
     79 MHD largely leaves implementing the semantics of the different HTTP
     80 methods to the application.  One exception is that MHD does understand
     81 @code{HEAD} and will only send the headers of the response and not the
     82 body, even if the client supplied a body.  (In fact, applications do
     83 need to construct a response with the correct length, even for
     84 @code{HEAD} request.)
     85 
     86 MHD understands @code{POST} data and is able to decode certain formats
     87 (@code{application/x-www-form-urlencoded}, @code{multipart/form-data}
     88 and @code{text/plain}) using the post processor API.  The data stream
     89 of a POST can also be provided directly to the main application, thus
     90 unsupported encodings could still be processed using custom
     91 application logic.  @ref{libmicrohttpd2-postprocessor,,Parsing POST
     92 data} explains MHD functions for dealing with POST data in more
     93 detail.
     94 
     95 The @code{microhttp2.h} header file defines various constants used by
     96 the HTTP protocol.  This does not mean that MHD actually interprets
     97 all of these values.  The provided constants are exported as a
     98 convenience for users of the library.  MHD does not require that all
     99 transmitted HTTP headers are part of the standard specification;
    100 applications are free to define their own custom headers and use those
    101 with MHD.  Even setting standard headers incorrectly (for example, to test
    102 clients) is possible via the use of options to override sanity checks.
    103 
    104 All symbols defined in the public API start with @code{MHD_}.  All
    105 functions are guaranteed to be completely reentrant and thread-safe.
    106 MHD checks for allocation failures and tries to recover gracefully
    107 (for example, by closing the connection).  Additionally, clients can
    108 specify resource limits on the overall number of connections, number
    109 of connections per IP address and memory used per connection to avoid
    110 resource exhaustion.  @ref{libmicrohttpd2-doptions,,Configuring your
    111 HTTP daemon} explains the MHD API for configuring the various settings
    112 supported for the daemon.
    113 
    114 
    115 @node libmicrohttpd-introduction-threadmodes
    116 @section Thread modes and event loops
    117 @cindex poll
    118 @cindex epoll
    119 @cindex mode
    120 @cindex threads
    121 @cindex select
    122 
    123 MHD supports four basic thread modes and up to three event loop
    124 styles.
    125 
    126 The four basic thread modes are external sockets polling (MHD creates
    127 no threads, event loop is fully managed by the application), internal
    128 polling (MHD creates one thread for all connections), polling in
    129 thread pool (MHD creates a thread pool which is used to process all
    130 connections) and thread-per-connection (MHD creates one thread for
    131 listen sockets and then one thread per accepted connection).
    132 
    133 These thread modes are then combined with the evet loop styles
    134 (polling function type).  MHD supports @code{select}, @code{poll} and
    135 @code{epoll}. @code{select} is available on all platforms,
    136 @code{epoll} and @code{poll} may not be available on some platforms.
    137 Note that it is possible to combine MHD using @code{epoll} with an
    138 external @code{select}-based event loop.
    139 
    140 The default (if no other option is passed) is ``external periodic''
    141 where the application must periodically call on MHD to give it a
    142 chance to do its work.  For simple applications, the recommended mode
    143 is an external event loop where MHD tells the application which events
    144 to watch for and when the application needs to call the library.  The
    145 highest performance can typically be obtained with a thread pool using
    146 @code{epoll}.
    147 
    148 @c Apache Benchmark (ab) was used to compare the
    149 @c performance of @code{select} and @code{epoll} when using a thread pool
    150 @c and a large number of connections.
    151 
    152 Not all combinations of thread modes and event loop styles are
    153 supported.  This is partially to keep the API simple, and partially
    154 because some combinations simply make no sense as others are strictly
    155 superior.  Note that the choice of style depends first of all on the
    156 application logic, and then on the performance requirements.
    157 Applications that perform a blocking operation while handling a
    158 request within the callbacks from MHD must use a thread per
    159 connection.  This is typically rather costly.  Applications that do
    160 not support threads or that must run on embedded devices without
    161 thread-support must use the external mode.  Using @code{epoll} is only
    162 supported by some platforms, thus portable applications must at least
    163 have a fallback option available.  @ref{tbl:supported} lists the sane
    164 combinations.
    165 
    166 @float Table,tbl:supported
    167 @multitable {@b{thread-per-connection}} {@b{select}} {@b{poll}} {@b{epoll}}
    168 @item                           @tab @b{select} @tab @b{poll} @tab @b{epoll}
    169 @item @b{external}              @tab  yes       @tab yes      @tab yes
    170 @item @b{internal}              @tab  yes       @tab yes      @tab yes
    171 @item @b{thread pool}           @tab  yes       @tab yes      @tab yes
    172 @item @b{thread-per-connection} @tab  no        @tab no       @tab no @c not yet implemented
    173 @end multitable
    174 @caption{Supported combinations of event styles and thread modes.}
    175 @end float
    176 
    177 In practice, you rarely need to worry about selecting the event
    178 loop style: MHD offers the "automatic" setting, which will pick
    179 the fastest mode available on your platform automatically.
    180 
    181 @ref{libmicrohttpd2-external,,Using external event loops} for details
    182 on how to use MHD with an external event loop.
    183 
    184 @node libmicrohttpd-introduction-compiling
    185 @section Compiling GNU libmicrohttpd
    186 @cindex compilation
    187 @cindex embedded systems
    188 @cindex portability
    189 
    190 MHD uses the standard GNU system where the usual build process
    191 involves running:
    192 
    193 @verbatim
    194 $ ./configure
    195 $ make
    196 $ make install
    197 @end verbatim
    198 
    199 In terms of dependencies, MHD can optionally use OpenSSL or
    200 libgnutls for TLS support.  MHD has no hard dependencies
    201 other than libc, and tries to impose minimal requirements
    202 on the C standard library as well.
    203 
    204 MHD supports various options to be given to configure to tailor the
    205 binary to a specific situation.  Note that some of these options will
    206 remove portions of the MHD code that are required for
    207 binary-compatibility.  They should only be used on embedded systems
    208 with tight resource constraints and no concerns about library
    209 versioning.  Standard distributions including MHD are expected to
    210 always ship with all features enabled and with at least one TLS
    211 backend, otherwise unexpected incompatibilities can arise. In
    212 particular, the libtool versioning supported by MHD developers
    213 assumes that you enable all of the optional features.
    214 
    215 Below is a list of MHD-specific options that can be given to
    216 configure:@footnote{Canonical configure options such
    217 as @code{--prefix} are also supported; for a
    218 full list of options run @code{./configure --help}}
    219 
    220 @c FIXME: check with current configure options!
    221 @table @code
    222 @item ``--disable-curl''
    223 disable running testcases using libcurl
    224 
    225 @item ``--disable-largefile''
    226 disable support for 64-bit files
    227 
    228 @item ``--disable-messages''
    229 disable logging of error messages (smaller binary size, not so much fun for debugging)
    230 
    231 @item ``--disable-https''
    232 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)
    233 
    234 @item ``--disable-postprocessor''
    235 do not include the post processor API (results in binary incompatibility)
    236 
    237 @item ``--disable-dauth''
    238 do not include the authentication APIs (results in binary incompatibility)
    239 
    240 @item ``--disable-httpupgrade''
    241 do not build code for HTTP ``Upgrade'' (smaller binary size, binary incompatible library)
    242 
    243 @item ``--disable-epoll''
    244 do not include epoll support, even if it supported (minimally smaller binary size, good for portability testing)
    245 
    246 @item ``--enable-coverage''
    247 set flags for analysis of code-coverage with gcc/gcov (results in slow, large binaries)
    248 
    249 @item ``--with-threads=posix,w32,none,auto''
    250 sets threading library to use. With use ``none'' to not support threads. In this case, MHD will only support the ``external'' threading modes and not perform any locking of data structures! Use @code{MHD_is_feature_supported(MHD_FEATURE_THREADS)} to test if threads are available. Default is ``auto''.
    251 
    252 @item ``--with-gnutls=PATH''
    253 specifies path to libgnutls installation
    254 
    255 @end table
    256 
    257 
    258 @node libmicrohttpd-introduction-pointers
    259 @section Applications using data provided by MHD
    260 
    261 @cindex lifetime
    262 @subsection Data lifetime
    263 
    264 MHD will give applications access to its internal data structures via
    265 arguments and return values pointing to memory managed by MHD.  This
    266 creates the question as to how long those pointers are assured to stay
    267 valid.
    268 
    269 Most MHD data structures are associated with an HTTP session,
    270 connection, request or response.  Thus, pointers associated with a
    271 session, connection, request or response are typically valid until the
    272 respective session, connection, request or response are completed.
    273 This is in particular true for all values obtained via the
    274 introspection API.
    275 @ref{libmicrohttpd2-introspection,,introspection APIs} has more
    276 details on the introspection API.
    277 
    278 Otherwise, values can generally only be assumed to be valid during the
    279 callback where MHD provided a value to the application. Finally,
    280 unless stated otherwise, the application does not need to guarantee
    281 that values it passes into MHD APIs remain valid after the call to an
    282 MHD API returns.
    283 
    284 @cindex string
    285 @subsection Strings
    286 
    287 MHD will frequently make strings available to applications.
    288 In this case, MHD will return a @code{struct MHD_String} or
    289 a @code{struct MHD_StringNullable}.
    290 
    291 @anchor{MHD_String}
    292 @deftp {C Struct} MHD_String len cstr
    293 Represents a string, the pointer is never @code{NULL}.
    294 @table @var
    295 @item @code{size_t} len
    296 Number of characters in @var{str}, not counting 0-termination;
    297 
    298 @item @code{const char *} cstr
    299 0-terminated C-string. Guaranteed to not be @code{NULL}.
    300 @end table
    301 @end deftp
    302 
    303 @anchor{MHD_StringNullable}
    304 @deftp {C Struct} MHD_StringNullable len cstr
    305 Represents a string that could also be @code{NULL}.
    306 @table @var
    307 @item @code{size_t} len
    308 Number of characters in @var{str}, not counting 0-termination;
    309 
    310 @c FIXME: really? or is the pointer to the Nullable NULL!?
    311 @item @code{const char *} cstr
    312 0-terminated C-string. Could be @code{NULL} in some cases.
    313 @end table
    314 @end deftp
    315 
    316 @cindex boolean
    317 @subsection Booleans
    318 
    319 @deftp {Enumeration} MHD_Bool
    320 MHD's representation of a boolean choice.
    321 Values include @code{MHD_NO} (0) and @code{MHD_YES} (1).
    322 @end deftp
    323 
    324 
    325 @node libmicrohttpd-introduction-sigpipe
    326 @section SIGPIPE
    327 @cindex signals
    328 
    329 MHD does not install a signal handler for SIGPIPE.  On platforms where
    330 this is possible (such as GNU/Linux), it disables SIGPIPE for its I/O
    331 operations (by passing @code{MSG_NOSIGNAL} or similar flags).  On
    332 platforms where this is not possible, SIGPIPE signals may be generated
    333 from network operations by MHD. This will cause the process to die
    334 unless the application explicitly installs a signal handler for SIGPIPE.
    335 
    336 Hence portable code using MHD @strong{must} install a SIGPIPE handler
    337 @emph{or} explicitly block the SIGPIPE signal.  MHD does not do so to
    338 avoid messing with other parts of the application that may need to
    339 handle SIGPIPE in a particular way.  You can make your application
    340 handle SIGPIPE by calling the following function in @code{main()}:
    341 
    342 @example
    343 @verbatiminclude examples/sigpipe.c
    344 @end example
    345 
    346 
    347 @section Portability to Android
    348 
    349 To cross-compile MHD for Android, install the Android NDK and use:
    350 @verbatim
    351 $ ./configure \
    352     --target=arm-linux-androideabi \
    353     --host=arm-linux-androideabi \
    354     --disable-doc \
    355     --disable-examples
    356 $ make
    357 @end verbatim
    358 
    359 Similar build commands should work for cross-compilation to other platforms.
    360 Note that you may have to first cross-compile the respective TLS library
    361 to get MHD with TLS support.
    362 
    363 @node libmicrohttpd-introduction-w32
    364 @section Portability to W32
    365 
    366 MHD in general ported well to W32. Most MHD features are
    367 supported. W32 do not support some functions, like @code{epoll} and
    368 corresponding MHD features are thus not available on W32.
    369 
    370 @node libmicrohttpd-introduction-zos
    371 @section Portability to z/OS
    372 
    373 It has been a long time since the developers had access to z/OS.
    374 While in principle the code should work, we would appreciate
    375 feedback from the z/OS community on the current version.
    376 
    377 
    378