ASL
HttpServer Class Reference

Detailed Description

This class can be used to create application-specific HTTP servers.

An HTTP server is implemented by subclassing HttpServer and implementing its serve() method. That method will be invoked whenever a new HTTP request is received. The handle method receives an HttpRequest to read the request (its method, path, query, headers, body, etc.) and and an HttpResponse object to write its response to (including status code, headers and body). The function can call serveFile() to serve static files like a normal web server.

class RestServer : public HttpServer
{
public:
RestServer()
{
setRoot("/www");
bind(8000);
bindTLS(443); // this for TLS
useCert(cert, key); //
}
void serve(HttpRequest& request, HttpResponse& response)
{
if(request.is("GET", "/api/clients/ *"))
{
String clientID = request.suffix();
response.put( Var("id", clientID)
("info", getInfo(clientID) )
);
}
else if(request.is("GET", "/api/icon")
{
response.setHeader("Content-Type", "image/png");
response.put( File("images/icon" + request.query("id") + ".png").content() );
}
else // serve static files
{
serveFile(request, response);
}
}
};
virtual void serve(HttpRequest &request, HttpResponse &response)
Implement this function in a subclass to create the server behavior.
void setRoot(const String &root)
Sets the root directory from where files will be served by default.
void setCrossDomain(bool on)
Enbles or disables cross-domain (CORS) support.
Definition: HttpServer.h:91
bool bindTLS(const String &ip, int port)
Assigns an interface IP address and TCP port to listen for TLS connections.
bool bind(int port)
Assigns a TCP port to listen to.
Definition: SocketServer.h:85
bool useCert(const String &cert, const String &key)
Sets the certificate and private key for the TLS server in PEM format.

A server like that will respond to requests such as /api/clients/132337 or /api/icon?id=12.

Each request is handled in a separate thread. So, you should probably use mutexes for synchronization.

#include <HttpServer.h>

Inheritance diagram for HttpServer:
SocketServer

Public Member Functions

void setRoot (const String &root)
 Sets the root directory from where files will be served by default.
 
void addMimeType (const String &ext, const String &type)
 Adds a mime type for a given extension for files served.
 
virtual void serve (HttpRequest &request, HttpResponse &response)
 Implement this function in a subclass to create the server behavior.
 
void addMethod (const String &verb)
 Adds a method to the list of allowed methods.
 
void setCrossDomain (bool on)
 Enbles or disables cross-domain (CORS) support.
 
void serveFile (HttpRequest &request, HttpResponse &response)
 Serves a static file from the configured root folder.
 
void link (WebSocketServer &wsserver)
 Links this socket with the given WebSocket server to process incoming WebSocket connections.
 
- Public Member Functions inherited from SocketServer
void start (bool nonblocking=false)
 Makes the server start listening. More...
 
bool bindPath (const String &sname)
 Assigns a Unix local socket to listen to (where supported).
 
bool bind (int port)
 Assigns a TCP port to listen to.
 
bool bind (const String &ip, int port)
 Assigns an interface IP address and TCP port to listen to.
 
bool bindTLS (const String &ip, int port)
 Assigns an interface IP address and TCP port to listen for TLS connections.
 
bool bindTLS (int port)
 Assigns a TCP port for TLS connections.
 
bool useCert (const String &cert, const String &key)
 Sets the certificate and private key for the TLS server in PEM format.
 
void stop (bool sync=false)
 Requests the server to stop receiving connections, and waits for all clients to exit if sync is true.
 
void setSequential (bool on)
 Sets the mode of operation: sequential (connections will be handled in sequence) or concurrent ( connections will be handled in parallel by starting a new thread each time); this must be called before calling start() to start the server.
 
bool running () const
 Returns true if this server started and has not yet stopped or still has clients running.
 
String socketError () const
 Returns a string representation of the last socket error (after a failed bind or use cert...)
 

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