ASL
Loading...
Searching...
No Matches
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:
{
setCrossDomain(true);
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);
}
}
};
An Array is a contiguous and resizable array of any type of elements.
Definition Array.h:69
An HTTP request that servers can read from.
Definition Http.h:254
const String & suffix()
After a call to is() returns the part of the path that substitutes the * in the pattern.
Definition Http.h:345
bool is(const char *meth, const String &pat)
Returns true if the method is meth and the path matches pat.
Definition Http.h:331
An HTTP response that clients can read and servers write.
Definition Http.h:411
This class can be used to create application-specific HTTP servers.
Definition HttpServer.h:66
String represents a character string that behaves similarly to JavaScript strings in that it can be c...
Definition String.h:126
A Var is a type that can hold a value of one of several types, similarly to a var in JavaScript.
Definition Var.h:188

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>

Inherits 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.
 
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: