Server API

class HttpResource
#include <HttpResource.h>

Instances of this class are registered with an HttpServer for a specific URL.

Subclassed by HttpMultipartResource, WebsocketResource

Public Functions

inline virtual void shutdown(HttpServerConnection &connection)

Takes care to cleanup the connection.

Public Members

HttpResourceDelegate onUrlComplete = nullptr

URL is ready. Path and status code are available.

HttpServerConnectionBodyDelegate onBody = nullptr

resource wants to process the raw body data

HttpResourceDelegate onHeadersComplete = nullptr

headers are ready

HttpResourceDelegate onRequestComplete = nullptr

request is complete OR upgraded

HttpServerConnectionUpgradeDelegate onUpgrade = nullptr

request is upgraded and raw data is passed to it

class PluginRef : public LinkedObjectTemplate<PluginRef>
#include <HttpResource.h>
class HttpResourceTree : public ObjectMap<String, HttpResource>
#include <HttpResourceTree.h>

Class to map URL paths to classes which handle them.

Public Functions

inline HttpResource *setDefault(HttpResource *resource)

Set the default resource handler.

Parameters:

resource – The default resource handler

inline HttpResource *setDefault(const HttpResourceDelegate &onRequestComplete)

Set the default resource handler, identified by “*” wildcard.

Parameters:

onRequestComplete – The default resource handler

inline HttpResource *setDefault(const HttpPathDelegate &callback)

Set the default resource handler, identified by “*” wildcard.

inline HttpResource *getDefault()

Get the current default resource handler, if any.

Return values:

HttpResource*

HttpResource *set(const String &path, const HttpResourceDelegate &onRequestComplete)

Set a callback to handle the given path.

Note

Path should start with slash. Trailing slashes will be removed.

Note

Any existing handler for this path is replaced

Parameters:
  • path – URL path

  • onRequestComplete – Delegate to handle this path

Return values:

HttpResource* – The created resource object

template<class ...Tail>
inline HttpResource *set(const String &path, const HttpResourceDelegate &onRequestComplete, HttpResourcePlugin *plugin, Tail... plugins)

Set a callback to handle the given path, with one or more plugins.

Note

Path should start with slash. Trailing slashes will be removed.

Note

Any existing handler for this path is replaced

Parameters:
  • path – URL path

  • onRequestComplete – Delegate to handle this path

  • plugin – Plugins to register for the resource

Return values:

HttpResource* – The created resource object

HttpResource *set(String path, const HttpPathDelegate &callback)

Add a new path resource with a callback.

Note

Path should start with slash. Trailing slashes will be removed

Note

Any existing handler for this path is replaced

Parameters:
  • path – URL path

  • callback – The callback that will handle this path

template<class ...Tail>
inline HttpResource *set(const String &path, const HttpPathDelegate &callback, HttpResourcePlugin *plugin, Tail... plugins)

Add a new path resource with callback and one or more plugins.

Note

Path should start with slash. Trailing slashes will be removed

Note

Any existing handler for this path is replaced

Parameters:
  • path – URL path

  • callback – The callback that will handle this path

  • plugin – - optional resource plugin

Return values:

HttpResource* – The created resource object

inline void set(const K &key, V *value)

Set a key value.

Parameters:
  • key

  • value

struct HttpServerSettings
#include <HttpServer.h>

Public Members

uint16_t maxActiveConnections = 10

maximum number of concurrent requests..

uint16_t keepAliveSeconds = 0

default seconds to keep the connection alive before closing it

int minHeapSize = -1

min heap size that is required to accept connection, -1 means use server default

bool useDefaultBodyParsers = 1

if the default body parsers, as form-url-encoded, should be used

bool closeOnContentError = true

close the connection if a body parser or resource fails to parse the body content.

class HttpServer : public TcpServer
#include <HttpServer.h>

Public Functions

void configure(const HttpServerSettings &settings)

Allows changing the server configuration.

inline void setBodyParser(const String &contentType, HttpBodyParserDelegate parser)

Allows content-type specific parsing of the body based on content-type.

Parameters:
  • contentType – Can be full content-type like ‘application/json’, or ‘application/*’ or ‘*’. If there is exact match for the content-type wildcard content-types will not be used. There can be only one catch-all ‘*’ body parser and that will be the last registered

  • parser

inline void setBodyParser(MimeType mimeType, HttpBodyParserDelegate parser)

Allows content-type specific parsing of the body based on content-type.

Parameters:
  • mimeType

  • parser

Public Members

HttpResourceTree paths

Maps paths to resources which deal with incoming requests.