Embed.md 5.65 KB
Newer Older
1 2 3 4
# Mongoose Embedding Guide

Embedding Mongoose is done in two steps:

Sergey Lyubka's avatar
Sergey Lyubka committed
5
   1. Copy
6 7
    [mongoose.c](https://raw.github.com/cesanta/mongoose/master/mongoose.c) and
    [mongoose.h](https://raw.github.com/cesanta/mongoose/master/mongoose.h)
Sergey Lyubka's avatar
Sergey Lyubka committed
8
    to your application's source tree and include them in the build.
9 10 11 12 13 14 15
   2. Somewhere in the application code, call `mg_create_server()` to create
    a server, configure it with `mg_set_option()` and loop with
    `mg_poll_server()` until done. Call `mg_destroy_server()` to cleanup.

Here's a minimal application `app.c` that embeds mongoose:

    #include "mongoose.h"
Sergey Lyubka's avatar
Sergey Lyubka committed
16

17
    int main(void) {
18
      struct mg_server *server = mg_create_server(NULL, NULL);
Sergey Lyubka's avatar
Sergey Lyubka committed
19
      mg_set_option(server, "document_root", ".");      // Serve current directory
Sergey Lyubka's avatar
Sergey Lyubka committed
20
      mg_set_option(server, "listening_port", "8080");  // Open port 8080
Sergey Lyubka's avatar
Sergey Lyubka committed
21
      for (;;) mg_poll_server(server, 1000);            // Infinite loop, Ctrl-C to stop
22 23 24 25
      mg_destroy_server(&server);
      return 0;
    }

Sergey Lyubka's avatar
Sergey Lyubka committed
26
To compile it, put `mongoose.c`, `mongoose.h` and `app.c` into one
Sergey Lyubka's avatar
Sergey Lyubka committed
27 28
folder, start terminal on UNIX or Visual Studio command line prompt on Windows,
and run the following command:
29

30 31
    cc app.c mongoose.c -pthread -o app    # on Unix
    cl app.c mongoose.c /TC /MD            # on Windows
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

When run, this simple application opens port 8080 and serves static files,
CGI files and lists directory content in the current working directory.

Mongoose can call user-defined functions when certain URIs are requested.
These functions are _called uri handlers_.  `mg_add_uri_handler()` registers
an URI handler, and there is no restriction exist on the number of URI handlers.
Also, mongoose can call a user-defined function when it is about to send
HTTP error back to client. That function is called _http error handler_ and
can be registered by `mg_set_http_error_handler()`. Handlers are called
by Mongoose with `struct mg_connection *` pointer as a parameter, which
has all information about the request: HTTP headers, POST or websocket
data buffer, etcetera.

Let's extend our minimal application example and
create an URI that will be served by user's C code. The app will handle
`/hello` URI by showing a hello message. So, when app is run,
http://127.0.0.1:8080/hello will say hello, and here's the code:

    #include <string.h>
    #include "mongoose.h"

54 55 56 57 58 59 60 61 62
    static int event_handler(struct mg_connection *conn, enum mg_event ev) {
      if (ev == MG_AUTH) {
        return MG_TRUE;   // Authorize all requests
      } else if (ev == MG_REQ_BEGIN) {
        mg_printf_data(conn, "%s", "Hello world");
        return MG_TRUE;   // Mark as processed
      } else {
        return MG_FALSE;  // Rest of the events are not processed
      }
63 64 65
    }

    int main(void) {
66
      struct mg_server *server = mg_create_server(NULL, event_handler);
67 68
      mg_set_option(server, "document_root", ".");
      mg_set_option(server, "listening_port", "8080");
69 70 71 72

      for (;;) {
        mg_poll_server(server, 1000);  // Infinite loop, Ctrl-C to stop
      }
73
      mg_destroy_server(&server);
74

75 76 77
      return 0;
    }

78 79 80 81
Below is the list of compilation flags that enable or disable certain
features. By default, some features are enabled, and could be disabled
by setting appropriate `NO_*` flag. Features that are disabled by default
could be enabled by setting appropriate `USE_*` flag. Bare bones Mongoose
Sergey Lyubka's avatar
Sergey Lyubka committed
82 83
is quite small, about 30 kilobytes of compiled x86 code. Each feature adds
a couple of kilobytes to the executable size, and also has some runtime penalty.
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    -DMONGOOSE_NO_AUTH          Disable MD5 authorization support
    -DMONGOOSE_NO_CGI           Disable CGI support
    -DMONGOOSE_NO_DAV           Disable WebDAV support
                                (PUT, DELETE, MKCOL, PROPFIND methods)
    -DMONGOOSE_NO_DIRECTORY_LISTING  Disable directory listing
    -DMONGOOSE_NO_FILESYSTEM    Disables all file IO, serving from memory only
    -DMONGOOSE_NO_LOGGING       Disable access/error logging
    -DMONGOOSE_NO_THREADS
    -DMONGOOSE_NO_WEBSOCKET     Disable WebSocket support

    -DMONGOOSE_USE_IDLE_TIMEOUT_SECONDS=X Idle connection timeout, default is 30
    -DMONGOOSE_USE_IPV6         Enable IPv6 support
    -DMONGOOSE_USE_LUA          Enable Lua scripting
    -DMONGOOSE_USE_LUA_SQLITE3  Enable sqlite3 binding for Lua
    -DMONGOOSE_USE_SSL          Enable SSL
    -DMONGOOSE_USE_POST_SIZE_LIMIT=X      POST requests larger than X will be
                                          rejected, not set by default
    -DMONGOOSE_USE_EXTRA_HTTP_HEADERS=X   Append X to the HTTP headers
                                          for static files, empty by default
104 105 106
    -DMONGOOSE_USE_STACK_SIZE=X           Let mg_start_thread() use stack
                                          size X, default is OS default
    -DMONGOOSE_ENABLE_DEBUG     Enables debug messages on stdout, very noisy
107 108 109
    -DMONGOOSE_HEXDUMP=\"XXX\"  Enables hexdump of sent and received traffic
                                to the text files. XXX must be a prefix of the
                                IP address whose traffic must be hexdumped.
110

111 112 113
Mongoose source code contains a well-commented example code, listed below:

   * [hello.c](https://github.com/cesanta/mongoose/blob/master/examples/hello.c)
Sergey Lyubka's avatar
Sergey Lyubka committed
114
   a minimalistic hello world example
Sergey Lyubka's avatar
Sergey Lyubka committed
115
   * [post.c](https://github.com/cesanta/mongoose/blob/master/examples/post.c)
Sergey Lyubka's avatar
Sergey Lyubka committed
116
   shows how to handle form input
Sergey Lyubka's avatar
Sergey Lyubka committed
117 118
   * [upload.c](https://github.com/cesanta/mongoose/blob/master/examples/post.c)
   shows how to upload files
Sergey Lyubka's avatar
Sergey Lyubka committed
119 120
   * [websocket.c](https://github.com/cesanta/mongoose/blob/master/examples/websocket.c) demonstrates websocket usage
   * [auth.c](https://github.com/cesanta/mongoose/blob/master/examples/websocket.c) demonstrates API-controlled Digest authorization