libcex  1.0.0
Modern C++11 embedded webserver library
My Personal Index Page

Introduction

libcex - embeddeded C++11 webserver.

Requirements

Mandatory libraries

Optional dependencies

  • OpenSSL (for HTTPS)
  • libz (for deflate/gzip compression)

Installation

git clone https://github.com/patrickjane/libcex .
mkdir build
cd build
cmake ..

If cmake cannot find your OpenSSL installation, or you've installed in a non-standard location, you might want to add -DOPENSSL_ROOT_DIR=/path/to/ssl to the cmake call.

Usage

libcex is built around the concept of middleware functions which serve both for routing and also for request/response interaction and processing. A minimal example might look like:

#include <cex.hpp>
int main()
{
app.get([](cex::Request* req, cex::Response* res, std::function<void()> next)
{
res->end(200);
});
app.listen("127.0.0.1", 5555, true); // blocks
return 0;
}

The cex::Server class provides a set of functions to attach middlewares for various situations. Middleware functions can be installed for a given HTTP method (GET, POST) or for a given URL-path. Each middleware function receives 3 parameters:

  • The cex::Request object contaning everything about the incoming request
  • The cex::Response object which is used to create a response
  • A function pointer which shall be used to skip to the next middleware

All registered middleware functions are executed in the order they have been registered, for each request. A middleware function is only executed, when its specification (HTTP method, URL-path) matches the incoming request. Execution of middlewares stops as soon as one of the following happens:

  • The last registered middleware was executed
  • The next method is not called

The method cex::Response::end is used to send a response to the client. This can be just a statuscode, or also a payload. If no registered middleware calls