Skip to content

Web Server design and required packages

Peter Corke edited this page Sep 8, 2018 · 2 revisions

STL supports a simple web server that allows MATLAB code to respond to GET (and soon PUT) requests, and format variables into the body of web pages using templates.

To implement this we need a lightweight/embedded web server library (in C) and an HTML templating library (in C).

Chosen tools

Tiny web server

A number of suitable packages exist but I've chosen libmicrohttpd. It's quite complete and well documented though the coding style for examples is quite idiosyncratic. It runs fine on MacOS and Linux.

The key capabilities are:

  • Launch the daemon, in its own thread or main thread
  • Register a page request callback that is invoked on every page request
  • The callback is passed the URL, method, version, and can access the GET and POST arguments

The callback invokes the registered MATLAB function to handle the page request. To skirt around the problem of passing libmicrohttpd struct pointers to MATLAB and back again, the interface uses functions, called from the MATLAB code, to get this information. A consequence is that the web server is not reentrant, but for the low-traffic envisaged a single threaded HTTP server ensures serial access. The alternative would require a more complex codegen command.

HTML templating engine

Once again there are a lot of choices but I've chosen libctemplate. My second choice would be Mustache because it's closer to the template language Jinja used by Flask. However libctemplate is simple, sufficient and works well.

The template engine takes the name of a template file, a list of name/value pairs for substitution and writes to a file. The name/value pair list is kept in a libctemplate structure but their is a convenient API to add name/value pairs to the list, and these are extracted from a passed in MATLAB structure.

The output is to a FILE pointer, but a string is required to be passed to libmicrohttp. Originally I thought I'd have to hack the code but then I discovered fmemopen() which creates a memory-based file that is used for ctemplate's output.

JSON engine

MATLAB Coder does not support code generation for jsonencode/decode. No choice made yet but this one looks promising, single file, and active.

Installing the required 3rd party packages

We will assume that the packages above are installed in a folder called contrib. It will have two subfolders: lib and include which are needed for the codegen build.

Installing libmicrohttpd

We will build from source and do a local install. Note that the actual version (0.9.59 as per below) will change over time, so update the version as apppropriate Get the source code and unpack it

% wget https://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-latest.tar.gz
% tar zxvf libmicrohttpd-latest.tar.gz
% cd libmicrohttpd-0.9.59
% ./configure --prefix=ABSOLUTE_PATH_TO_CONTRIB
% make install

Installing ctemplate

Get the source code and unpack it

% wget https://sourceforge.net/projects/libctemplate/files/latest/download -O libctemplate.tar
% tar xf libctemplate.tar
% cd ctemplate-1.0
% make
% cp libctemplate.a PATH_TO_CONTRIB
% cp template.h PATH_TO_CONTRIB