# &adminguide;

## Overview

This module implements an embedded HTTP server using libmicrohttpd.

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- none.

### External Libraries or Applications

The following libraries or applications must be installed before running &kamailio; with this module loaded:

- libmicrohttpd - libmicrohttpd library

## Parameters

### `listen_addr` (int)

IPv4 address to listen for HTTP connection. If not set, then it listens on all local addresses (port has to be specified by listen_port parameter).

Default value is "" (empty - not set).

#### Set `listen_addr` parameter

    ...
    modparam("microhttpd", "listen_addr", "127.0.0.1")
    ...

### `listen_port` (int)

Port to listen for HTTP connection.

Default value is 8280.

#### Set `listen_port` parameter

    ...
    modparam("microhttpd", "listen_port", 8284)
    ...

### `event_callback` (str)

The name of the function in the kemi configuration file (embedded scripting language such as Lua, Python, ...) to be executed instead of event_route[microhttpd:request] block.

The function has one string parameter with the value "microhttpd:request".

Default value is 'empty' (no function is executed for events).

#### Set `event_callback` parameter

    ...
    modparam("microhttpd", "event_callback", "ksr_microhttpd_event")
    ...
    -- event callback function implemented in Lua
    function ksr_microhttpd_event(evname)
    	KSR.info("===== microhttpd module triggered event: " .. evname .. "\n");
    	return 1;
    end
    ...

## Functions

### `mhttpd_reply(code, reason, ctype, body)`

Send back a reply with content-type and body.

#### `mhttpd_reply` usage

    ...
    event_route[microhttpd:request] {
        mhttpd_reply("200", "OK", "text/html",
            "<html><body>OK</body></html>");
    }
    ...

## Event Routes

### `microhttpd:request`

The event route is executed when a new HTTP request is received.

Inside it, the $mhttpd(...) group of variables is available, giving access to several attributes of the HTTP request, such as method, URL, data (body) or headers.

    ...
    ...
    loadmodule "microhttpd.so
    ...
    event_route[microhttpd:request] {
        xinfo("request: $mhttpd(method) - url: $mhttpd(url) - data: [$mhttpd(data)]\n");
        mhttpd_reply("200", "OK", "text/html",
            "<html><body>OK</body></html>");
    }
    ...