1 · Introduction

What is libmicrohttpd?

GNU libmicrohttpd (MHD) is a small C library that lets an application act as an HTTP 1.1 server without forking a separate process or thread pool unless you ask it to. Its design emphasises minimal footprint, straightforward API, and license compatibility (LGPL‑2.1+) making it suitable for embedded devices, CLI utilities, desktop apps, or even high‑performance back‑ends. Key features include:

Note: As of April 2025 Ubuntu ships MHD 1.0.1, the first “1.x” release focusing on long‑term ABI stability.

2 · Core Concepts & Data Structures

Primary Handles

MHD exposes opaque pointers so your program never touches internal state directly:

Callback‑Centric Workflow

MHD adopts an event‑driven design. Your application registers function pointers that the library invokes when:

  1. A socket becomes readable/writable (internal poller model), or
  2. You call MHD_run() / MHD_run_from_select() in an external main‑loop integration.

3 · Minimal “Hello, HTTP!” Example

C Source

#include <microhttpd.h>
#include <string.h>

#define PORT 8080
static int
answer (void *cls, struct MHD_Connection *c,
        const char *url, const char *method,
        const char *ver, const char *u, size_t *s,
        void **con_cls)
{
  static const char page[] = "Hello World\n";
  struct MHD_Response *r =
      MHD_create_response_from_buffer (strlen (page),
                                       (void *) page,
                                       MHD_RESPMEM_PERSISTENT);
  int ret = MHD_queue_response (c, MHD_HTTP_OK, r);
  MHD_destroy_response (r);
  return ret;
}

int main ()
{
  struct MHD_Daemon *d =
      MHD_start_daemon (
        MHD_USE_SELECT_INTERNALLY,
        PORT,
        NULL, NULL,            // accept-policy cb
        &answer, NULL,         // request handler cb
        MHD_OPTION_END);
  getchar ();                 // wait for ⏎
  MHD_stop_daemon (d);
  return 0;
}

Compile: gcc hello.c -lmicrohttpd -o hello Run: ./hello and visit http://localhost:8080.

4 · Request Handling Lifecycle

Flow Chart

  1. MHD accepts TCP socket → creates MHD_Connection.
  2. Parses request line + headers.
  3. Invokes your MHD_AccessHandlerCallback once headers are complete.
  4. If request has a body and you return MHD_YES, MHD calls your MHD_ContentReaderCallback (or POST processor) chunk‑by‑chunk.
  5. You craft a MHD_Response and queue it.
  6. When finished, connection is either kept‑alive or closed depending on headers & flags.

5 · Building Responses

Creation Helpers

FunctionTypical Use‑Case
MHD_create_response_from_buffer Small static strings held in memory.
MHD_create_response_from_fd Efficient sendfile() of large files.
MHD_create_response_from_callback Generate/stream data on demand (e.g., DB rows).
MHD_create_response_from_iovec Zero‑copy scatter‑gather, new since 0.9.73.

Queuing & Headers

After constructing you attach it with MHD_queue_response(). Custom headers are applied using MHD_add_response_header().

6 · POST & Multipart Parsing

Incremental Parsing

For application/x-www-form-urlencoded and multipart/form-data bodies call MHD_create_post_processor() inside your access‑handler, then feed chunks to MHD_post_process(). This keeps memory usage O(chunkSize) instead of O(bodySize).

7 · TLS / HTTPS Support

Enabling TLS

Compile MHD against your preferred SSL backend (--with-ssl=gnutls|openssl|mbedtls). At runtime pass one of:

MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION    |
                  MHD_USE_TLS,      /* ← enable SSL */
                  443,
                  NULL, NULL,
                  &handler, NULL,
                  MHD_OPTION_HTTPS_MEM_KEY,  key_pem,
                  MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
                  MHD_OPTION_END);

The library automatically performs the TLS handshake before invoking your access callback. Example file‑server with TLS is shipped in https_fileserver_example.c.

8 · Threading & Event Loops

Built‑in Modes

9 · Performance Tuning Tips

Settings

10 · Security Considerations

Hardening Checklist

11 · Building & Installation

From Source

git clone https://git.gnunet.org/libmicrohttpd.git
cd libmicrohttpd
./bootstrap
./configure --prefix=/usr/local --enable-messages --with-ssl=openssl
make -j$(nproc)
sudo make install

Pkg‑config

Add pkg-config --cflags --libs libmicrohttpd to your build system. If you rely on CMake, use find_package(PkgConfig) ….

12 · Ecosystem & Alternatives

When to Choose MHD

Alternatives

LibraryNotes
libevent‑httpdPart of libevent; larger code base, no built‑in TLS.
civetweb / mongooseHeader‑only single‑file; BSD/MIT licensed; no LGPL obligations.
Boost.BeastModern C++; header‑only, but heavier compile times.

13 · Further Resources