My Project
 All Classes Functions Variables Pages
Public Member Functions | Friends | List of all members
HttpServer Class Reference

Public Member Functions

 HttpServer ()
 
void addPathHandler (std::string method, std::string pathExpr, void(*webServerRequestHandler)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse))
 Register a handler for a path. More...
 
void addPathHandler (std::string method, std::regex *pRegex, void(*webServerRequestHandler)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse))
 Register a handler for a path. More...
 
uint32_t getClientTimeout ()
 Get current socket's timeout for new connections. More...
 
size_t getFileBufferSize ()
 Get the size of the file buffer. When serving up a file from the file system, we can't afford to read the whole file into RAM before sending it. As such, we must read the file in chunks. The buffer size is the size of a chunk to be transmitted before the next chunk is read. More...
 
uint16_t getPort ()
 Get the port number on which the HTTP Server is listening. More...
 
std::string getRootPath ()
 Get the current root path. More...
 
bool getSSL ()
 Return whether or not we are using SSL. More...
 
void setClientTimeout (uint32_t timeout)
 Set different socket timeout for new connections. More...
 
void setDirectoryListing (bool use)
 Set whether or not we will list directories. More...
 
void setFileBufferSize (size_t fileBufferSize)
 Set the size of the file buffer. When serving up a file from the file system, we can't afford to read the whole file into RAM before sending it. As such, we must read the file in chunks. The buffer size is the size of a chunk to be transmitted before the next chunk is read. More...
 
void setRootPath (std::string path)
 Set the root path for URL file mapping. More...
 
void start (uint16_t portNumber, bool useSSL=false)
 Start the HTTP server listening. We start an instance of the HTTP server listening. A new task is spawned to perform this work in the back ground. More...
 
void stop ()
 Shutdown the HTTP server.
 

Friends

class HttpServerTask
 
class WebSocket
 

Constructor & Destructor Documentation

HttpServer::HttpServer ( )

Constructor for HTTP Server

Member Function Documentation

void HttpServer::addPathHandler ( std::string  method,
std::string  path,
void(*)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse)  handler 
)

Register a handler for a path.

When a browser request arrives, the request will contain a method (GET, POST, etc) and a path to be accessed. Using this method we can register a regular expression and, if the incoming method and path match the expression, the corresponding handler will be called.

Example:

static void handle_REST_WiFi(WebServer::HttpRequest *pRequest, WebServer::HttpResponse *pResponse) {
...
}
webServer.addPathHandler("GET", "/ESP32/WiFi", handle_REST_WiFi);
Parameters
[in]methodThe method being used for access ("GET", "POST" etc).
[in]pathThe plain path being accessed.
[in]handlerThe callback function to be invoked when a request arrives.
void HttpServer::addPathHandler ( std::string  method,
std::regex *  pathExpr,
void(*)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse)  handler 
)

Register a handler for a path.

When a browser request arrives, the request will contain a method (GET, POST, etc) and a path to be accessed. Using this method we can register a regular expression and, if the incoming method and path match the expression, the corresponding handler will be called.

Example:

static void handle_REST_WiFi(WebServer::HttpRequest *pRequest, WebServer::HttpResponse *pResponse) {
...
}
webServer.addPathHandler("GET", "\\/ESP32\\/WiFi", handle_REST_WiFi);
Parameters
[in]methodThe method being used for access ("GET", "POST" etc).
[in]pathExprThe path being accessed.
[in]handlerThe callback function to be invoked when a request arrives.
uint32_t HttpServer::getClientTimeout ( )

Get current socket's timeout for new connections.

Parameters
[in]useSet to true to enable directory listing.
size_t HttpServer::getFileBufferSize ( )

Get the size of the file buffer. When serving up a file from the file system, we can't afford to read the whole file into RAM before sending it. As such, we must read the file in chunks. The buffer size is the size of a chunk to be transmitted before the next chunk is read.

Returns
The file buffer size.
uint16_t HttpServer::getPort ( )

Get the port number on which the HTTP Server is listening.

Returns
The port number on which the HTTP server is listening.
std::string HttpServer::getRootPath ( )

Get the current root path.

Returns
The current root path.
bool HttpServer::getSSL ( )

Return whether or not we are using SSL.

Returns
True if we are using SSL.
void HttpServer::setClientTimeout ( uint32_t  timeout)

Set different socket timeout for new connections.

Parameters
[in]useSet to true to enable directory listing.
void HttpServer::setDirectoryListing ( bool  use)

Set whether or not we will list directories.

Parameters
[in]useSet to true to enable directory listing.
void HttpServer::setFileBufferSize ( size_t  fileBufferSize)

Set the size of the file buffer. When serving up a file from the file system, we can't afford to read the whole file into RAM before sending it. As such, we must read the file in chunks. The buffer size is the size of a chunk to be transmitted before the next chunk is read.

Parameters
[in]fileBufferSizeHow large should the file buffer size be?
void HttpServer::setRootPath ( std::string  path)

Set the root path for URL file mapping.

When a browser requests a file, it uses the address form:

http://<host>:<port>/<path>

The path part can be considered the path to where the file should be retrieved on the file system available to the web server. Typically, we want a directory structure on the file system to host the web served files and not expose the whole file system. Using this method we specify the root directory from which the files will be served.

Parameters
[in]pathThe root path on the file system.
Returns
N/A.
void HttpServer::start ( uint16_t  portNumber,
bool  useSSL = false 
)

Start the HTTP server listening. We start an instance of the HTTP server listening. A new task is spawned to perform this work in the back ground.

Parameters
[in]portNumberThe port number on which the HTTP server should listen.
[in]useSSLShould we use SSL?

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