libcex  1.0.0
Modern C++11 embedded webserver library
Classes | Public Member Functions | Static Public Member Functions | List of all members
cex::Server Class Reference

Core class of the embedded webserver. Manages a single HTTP/HTTPS listener and performs routing as defined by the installed middlewares. More...

#include <core.hpp>

Classes

struct  Config
 Structure transporting all configuration options of the embedded server. Note that certain middlewares have additional config structs (e. g. cex::filesystem ) More...
 
struct  Context
 Internal helper struct for handling libevhtp callback functions. More...
 

Public Member Functions

 Server ()
 Constructs a new server with the default config.
 
 Server (Config &config)
 Constructs a new server with the given config. More...
 
int listen (bool block=true)
 Starts the server with listener on address and port specified in the server Config struct. More...
 
int listen (std::string address, int port, bool block=true)
 Starts the server with listener on the given address and port. More...
 
int stop ()
 Stops the listener. If it was started within a background thread, the background thread is terminated.
 
void reset ()
 Removes all attached middlewares.
 
void use (MiddlewareFunction func)
 Attaches a middleware function with no conditions. More...
 
void use (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function with no HTTP method specification and with the given path. More...
 
void get (MiddlewareFunction func)
 Attaches a middleware function for HTTP GET requests. More...
 
void get (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP GET method and the given path. More...
 
void put (MiddlewareFunction func)
 Attaches a middleware function for HTTP PUT requests. More...
 
void put (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP PUT method and the given path. More...
 
void post (MiddlewareFunction func)
 Attaches a middleware function for HTTP POST requests. More...
 
void post (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP POST method and the given path. More...
 
void head (MiddlewareFunction func)
 Attaches a middleware function for HTTP HEAD requests. More...
 
void head (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP HEAD method and the given path. More...
 
void del (MiddlewareFunction func)
 Attaches a middleware function for HTTP DEL requests. More...
 
void del (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP DEL method and the given path. More...
 
void connect (MiddlewareFunction func)
 Attaches a middleware function for HTTP CONNECT requests. More...
 
void connect (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP CONNECT method and the given path. More...
 
void options (MiddlewareFunction func)
 Attaches a middleware function for HTTP OPTIONS requests. More...
 
void options (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP OPTIONS method and the given path. More...
 
void trace (MiddlewareFunction func)
 Attaches a middleware function for HTTP TRACE requests. More...
 
void trace (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP TRACE method and the given path. More...
 
void patch (MiddlewareFunction func)
 Attaches a middleware function for HTTP PATCH requests. More...
 
void patch (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the HTTP PATCH method and the given path. More...
 
void mkcol (MiddlewareFunction func)
 Attaches a middleware function for WEBDAV MKCOL requests. More...
 
void mkcol (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the WEBDAV MKCOL method and the given path. More...
 
void copy (MiddlewareFunction func)
 Attaches a middleware function for WEBDAV COPY requests. More...
 
void copy (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the WEBDAV COPY method and the given path. More...
 
void move (MiddlewareFunction func)
 Attaches a middleware function for WEBDAV MOVE requests. More...
 
void move (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the WEBDAV MOVE method and the given path. More...
 
void propfind (MiddlewareFunction func)
 Attaches a middleware function for HTTP WEBDAV PROPFIND requests. More...
 
void propfind (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the WEBDAV PROPFIND method and the given path. More...
 
void proppatch (MiddlewareFunction func)
 Attaches a middleware function for WEBDAV PROPPATCH requests. More...
 
void proppatch (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the WEBDAV PROPPATCH method and the given path. More...
 
void lock (MiddlewareFunction func)
 Attaches a middleware function for WEBDAV LOCK requests. More...
 
void lock (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the WEBDAV LOCK method and the given path. More...
 
void unlock (MiddlewareFunction func)
 Attaches a middleware function for WEBDAV UNLOCK requests. More...
 
void unlock (const char *path, MiddlewareFunction func, int flags=Middleware::fMatchContain)
 Attaches a middleware function for the WEBDAV UNLOCK method and the given path. More...
 
void uploads (UploadFunction func)
 Attaches a special middleware function which receives request body data uploads in chunks. More...
 
void uploads (const char *path, UploadFunction func, Method method=methodPOST, int flags=Middleware::fMatchContain)
 Attaches an upload middleware function for the given HTTP method and URL path. More...
 

Static Public Member Functions

static MimeTypes * getMimeTypes ()
 
static void registerMimeType (const char *ext, const char *mime, bool binary)
 
static void libraryInit ()
 

Detailed Description

Core class of the embedded webserver. Manages a single HTTP/HTTPS listener and performs routing as defined by the installed middlewares.

Constructor & Destructor Documentation

◆ Server()

cex::Server::Server ( Config config)

Constructs a new server with the given config.

Parameters
configThe Config object which defines the server options/configuration

Member Function Documentation

◆ connect() [1/2]

void cex::Server::connect ( MiddlewareFunction  func)

Attaches a middleware function for HTTP CONNECT requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ connect() [2/2]

void cex::Server::connect ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP CONNECT method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ copy() [1/2]

void cex::Server::copy ( MiddlewareFunction  func)

Attaches a middleware function for WEBDAV COPY requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ copy() [2/2]

void cex::Server::copy ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the WEBDAV COPY method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ del() [1/2]

void cex::Server::del ( MiddlewareFunction  func)

Attaches a middleware function for HTTP DEL requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ del() [2/2]

void cex::Server::del ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP DEL method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ get() [1/2]

void cex::Server::get ( MiddlewareFunction  func)

Attaches a middleware function for HTTP GET requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ get() [2/2]

void cex::Server::get ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP GET method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ head() [1/2]

void cex::Server::head ( MiddlewareFunction  func)

Attaches a middleware function for HTTP HEAD requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ head() [2/2]

void cex::Server::head ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP HEAD method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ listen() [1/2]

int cex::Server::listen ( bool  block = true)

Starts the server with listener on address and port specified in the server Config struct.

Parameters
blockIf set to true, runs the listener/eventloop in the calling thread, thus blocking the caller. If set to false, spawns a new thread which runs the listener/eventloop, and returns immediately.

◆ listen() [2/2]

int cex::Server::listen ( std::string  address,
int  port,
bool  block = true 
)

Starts the server with listener on the given address and port.

Parameters
addressThe address to start the listener on (e.g. localhost or 10.0.2.14)
portThe port to start the listener on
blockIf set to true, runs the listener/eventloop in the calling thread, thus blocking the caller. If set to false, spawns a new thread which runs the listener/eventloop, and returns immediately.

◆ lock() [1/2]

void cex::Server::lock ( MiddlewareFunction  func)

Attaches a middleware function for WEBDAV LOCK requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ lock() [2/2]

void cex::Server::lock ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the WEBDAV LOCK method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ mkcol() [1/2]

void cex::Server::mkcol ( MiddlewareFunction  func)

Attaches a middleware function for WEBDAV MKCOL requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ mkcol() [2/2]

void cex::Server::mkcol ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the WEBDAV MKCOL method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ move() [1/2]

void cex::Server::move ( MiddlewareFunction  func)

Attaches a middleware function for WEBDAV MOVE requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ move() [2/2]

void cex::Server::move ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the WEBDAV MOVE method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ options() [1/2]

void cex::Server::options ( MiddlewareFunction  func)

Attaches a middleware function for HTTP OPTIONS requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ options() [2/2]

void cex::Server::options ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP OPTIONS method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ patch() [1/2]

void cex::Server::patch ( MiddlewareFunction  func)

Attaches a middleware function for HTTP PATCH requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ patch() [2/2]

void cex::Server::patch ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP PATCH method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ post() [1/2]

void cex::Server::post ( MiddlewareFunction  func)

Attaches a middleware function for HTTP POST requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ post() [2/2]

void cex::Server::post ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP POST method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ propfind() [1/2]

void cex::Server::propfind ( MiddlewareFunction  func)

Attaches a middleware function for HTTP WEBDAV PROPFIND requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ propfind() [2/2]

void cex::Server::propfind ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the WEBDAV PROPFIND method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ proppatch() [1/2]

void cex::Server::proppatch ( MiddlewareFunction  func)

Attaches a middleware function for WEBDAV PROPPATCH requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ proppatch() [2/2]

void cex::Server::proppatch ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the WEBDAV PROPPATCH method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ put() [1/2]

void cex::Server::put ( MiddlewareFunction  func)

Attaches a middleware function for HTTP PUT requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ put() [2/2]

void cex::Server::put ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP PUT method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ trace() [1/2]

void cex::Server::trace ( MiddlewareFunction  func)

Attaches a middleware function for HTTP TRACE requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ trace() [2/2]

void cex::Server::trace ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the HTTP TRACE method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ unlock() [1/2]

void cex::Server::unlock ( MiddlewareFunction  func)

Attaches a middleware function for WEBDAV UNLOCK requests.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ unlock() [2/2]

void cex::Server::unlock ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function for the WEBDAV UNLOCK method and the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ uploads() [1/2]

void cex::Server::uploads ( UploadFunction  func)

Attaches a special middleware function which receives request body data uploads in chunks.

The provided UploadFunction will be called repeatedly until the whole request body data is received. Processing will not continue to any subsequent middlewares until the upload is completed.

Parameters
funcThe UploadFunction which shall be called upon receiving request body data

◆ uploads() [2/2]

void cex::Server::uploads ( const char *  path,
UploadFunction  func,
Method  method = methodPOST,
int  flags = Middleware::fMatchContain 
)

Attaches an upload middleware function for the given HTTP method and URL path.

The provided UploadFunction will be called repeatedly for matching requests until the whole request body data is received. Processing will not continue to any subsequent middlewares until the upload is completed.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe UploadFunction which shall be called upon receiving request body data
flagsFlags controlling the URL matching behaviour (see Middleware)

◆ use() [1/2]

void cex::Server::use ( MiddlewareFunction  func)

Attaches a middleware function with no conditions.

The function will be called for every request.

Parameters
funcThe middleware function which shall be called

◆ use() [2/2]

void cex::Server::use ( const char *  path,
MiddlewareFunction  func,
int  flags = Middleware::fMatchContain 
)

Attaches a middleware function with no HTTP method specification and with the given path.

The function will be called when the URL of incoming requests match the path, regardless of HTTP method.

Parameters
pathThe URL path which shall be compared against the request URL
funcThe middleware function which shall be called
flagsFlags controlling the URL matching behaviour (see Middleware)

The documentation for this class was generated from the following files: