Inspirel banner

Embedded HTTP Server

The idea is to treat the web browser as a programmable display device and use it as an alternative to traditional C++ GUI toolkit.
The "display" is programmed using standard web-development methods (like HTML+CSS+JavaScript), which also promotes the separation between presentation and application logic:

Embedded HTTP Server idea

Demo and Tutorial

The Embedded HTTP Server is a small and simple to use library with the following 6-step tutorial:

Example 1 - Static Files

Example 1 - Static Files

The left window presents complete program source code. One line of code is enough to serve static files, here there are just two of them, shown in the other two terminal windows. The GUI is top-right.

Example 2 - Dynamic Content

Example 2 - Dynamic Content

The program involves a function ("action") that generates dynamic HTML content, here displaying a single random number.

Note: a more realistic application of this scheme would fetch the static portions of output from other sources (storage, template engine, etc.) and AJAX is an alternative worth considering, too.

Example 3 - Dynamic Content With Parameters

Example 3 - Dynamic Content With Parameters

The program generates dynamic HTML content based on values of URL parameters.

Again, there are better options than to use HTML code in string literals.

Example 4 - Dynamic Elements With AJAX

Example 4 - Dynamic Elements With AJAX

The program generates a dynamic piece of data that is later reflected in the target page.

This looks more compact than example 2 and 3, and indeed promotes better separation between logic and presentation aspects. The C++ code is cleaner, but at the price of additional scripting for the browser side.

Example 5 - Forms

Example 5 - Forms

The program generates a dynamic response to the form (POST) request.

The data is transmitted using a separate channel, hence the signature of the greet action is more complex than in other examples.

Considerations about the use of HTML code in C++ strings apply here, too.

Example 6 - Server-Side Events

Example 6 - Server-Side Events

The program benefits from permanent connection to the browser, sending periodic updates that are then updating the target page.

This approach has extreme flexibility and allows to seamlessly update the display based on changing program state, while keeping the logic and presentation properly separated, at the price of some scripting support on the browser side.

API Reference

See the C++ API reference.

Limitations

The Embedded HTTP Server library was implemented under assumption of reasonably small number of clients. Internally, a separate thread is created for each client connection. This allows to keep the implementation straightforward, and provides some useful guarantees for the program code, too - but at the price of limited scalability. This approach is obvious in the basic scenario of a program and a single browser running on the same computer. The use of several browser windows is within the scope, too, and a single program serving users in a small local network is still a reasonable use case. But this library was not meant to support heavily loaded websites on a public network. Use full-featured web-server solutions for that.

Some antivirus applications, in particular configurations, are known to intercept SSE streams, treating them as downloads that deserve scanning and thus introducing delays in their delivery. Such set-ups can interfere with the natural responsiveness of SSE techniques.

Building and Packaging

The whole library contains just two header and two source files. Just add them to your project and don't bother making a separate library for it.

The package contains example programs from the tutorial above, with minimal Makefiles for Linux, Mac OS X and Windows (Visual Studio).

Licensing and Download

The Embedded HTML Server is available with the Boost Software License.

Package:

Purpose:

embedded_http_server-1.0.zip

Source package with reference docs and tutorial examples.