My Project
 All Classes Functions Variables Pages
HttpServer.h
1 /*
2  * HttpServer.h
3  *
4  * Created on: Aug 30, 2017
5  * Author: kolban
6  *
7  * Implementation of an HTTP server for the ESP32.
8  *
9  */
10 
11 #ifndef COMPONENTS_CPP_UTILS_HTTPSERVER_H_
12 #define COMPONENTS_CPP_UTILS_HTTPSERVER_H_
13 #include <stdint.h>
14 
15 #include <vector>
16 #include "SockServ.h"
17 #include "HttpRequest.h"
18 #include "HttpResponse.h"
19 #include "FreeRTOS.h"
20 #include <regex>
21 
22 class HttpServerTask;
23 
27 class PathHandler {
28  public:
30  std::string method, // The method in the request to be matched.
31  std::string pathPattern, // The pattern in the request to be matched
32  void (*pWebServerRequestHandler) // The handler function to be invoked upon a match.
33  (
34  HttpRequest* pHttpRequest,
35  HttpResponse* pHttpResponse)
36  );
38  std::string method, // The method in the request to be matched.
39  std::regex* pathPattern, // The pattern in the request to be matched (regex)
40  void (*pWebServerRequestHandler) // The handler function to be invoked upon a match.
41  (
42  HttpRequest* pHttpRequest,
43  HttpResponse* pHttpResponse)
44  );
45  bool match(std::string method, std::string path); // Does the request method and pattern match?
46  void invokePathHandler(HttpRequest* request, HttpResponse* response);
47  private:
48  std::string m_method;
49  std::regex* m_pRegex;
50  bool m_isRegex;
51  std::string m_textPattern;
52  void (*m_pRequestHandler)(HttpRequest* pHttpRequest, HttpResponse* pHttpResponse);
53 }; // PathHandler
54 
55 
56 class HttpServer {
57 public:
58  HttpServer();
59  virtual ~HttpServer();
60 
61  void addPathHandler(
62  std::string method,
63  std::string pathExpr,
64  void (*webServerRequestHandler)
65  (
66  HttpRequest* pHttpRequest,
67  HttpResponse* pHttpResponse)
68  );
69  void addPathHandler(
70  std::string method,
71  std::regex* pRegex,
72  void (*webServerRequestHandler)
73  (
74  HttpRequest* pHttpRequest,
75  HttpResponse* pHttpResponse)
76  );
77  uint32_t getClientTimeout(); // Get client's socket timeout
78  size_t getFileBufferSize(); // Get the current size of the file buffer.
79  uint16_t getPort(); // Get the port on which the Http server is listening.
80  std::string getRootPath(); // Get the root of the file system path.
81  bool getSSL(); // Are we using SSL?
82  void setClientTimeout(uint32_t timeout); // Set client's socket timeout
83  void setDirectoryListing(bool use); // Should we list the content of directories?
84  void setFileBufferSize(size_t fileBufferSize); // Set the size of the file buffer
85  void setRootPath(std::string path); // Set the root of the file system path.
86  void start(uint16_t portNumber, bool useSSL = false);
87  void stop(); // Stop a previously started server.
88 
89 private:
90  friend class HttpServerTask;
91  friend class WebSocket;
92  void listDirectory(std::string path, HttpResponse& response);
93  size_t m_fileBufferSize; // Size of the file buffer.
94  bool m_directoryListing; // Should we list directory content?
95  std::vector<PathHandler> m_pathHandlers; // Vector of path handlers.
96  uint16_t m_portNumber; // Port number on which server is listening.
97  std::string m_rootPath; // Root path into the file system.
98  Socket m_socket;
99  bool m_useSSL; // Is this server listening on an HTTPS port?
100  uint32_t m_clientTimeout; // Default Timeout
101  FreeRTOS::Semaphore m_semaphoreServerStarted = FreeRTOS::Semaphore("ServerStarted");
102 }; // HttpServer
103 
104 #endif /* COMPONENTS_CPP_UTILS_HTTPSERVER_H_ */
Encapsulate a socket.
Definition: Socket.h:62
void addPathHandler(std::string method, std::string pathExpr, void(*webServerRequestHandler)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse))
Register a handler for a path.
Definition: HttpServer.cpp:215
std::string getRootPath()
Get the current root path.
Definition: HttpServer.cpp:250
Handle path matching for an incoming HTTP request.
Definition: HttpServer.h:27
bool getSSL()
Return whether or not we are using SSL.
Definition: HttpServer.cpp:259
HttpServer()
Definition: HttpServer.cpp:32
uint16_t getPort()
Get the port number on which the HTTP Server is listening.
Definition: HttpServer.cpp:241
void setRootPath(std::string path)
Set the root path for URL file mapping.
Definition: HttpServer.cpp:371
void stop()
Shutdown the HTTP server.
Definition: HttpServer.cpp:412
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.
Definition: HttpServer.cpp:388
bool match(std::string method, std::string path)
Determine if the path matches.
Definition: HttpServer.cpp:472
Definition: WebSocket.h:60
void setClientTimeout(uint32_t timeout)
Set different socket timeout for new connections.
Definition: HttpServer.cpp:319
PathHandler(std::string method, std::string pathPattern, void(*pWebServerRequestHandler)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse))
Construct an instance of a PathHandler.
Definition: HttpServer.cpp:451
Definition: HttpServer.h:56
Be an HTTP server task. Here we define a Task that will be run when the HTTP server starts...
Definition: HttpServer.cpp:53
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...
Definition: HttpServer.cpp:232
uint32_t getClientTimeout()
Get current socket's timeout for new connections.
Definition: HttpServer.cpp:328
Definition: HttpRequest.h:19
Definition: FreeRTOS.h:31
void invokePathHandler(HttpRequest *request, HttpResponse *response)
Invoke the handler.
Definition: HttpServer.cpp:489
void setDirectoryListing(bool use)
Set whether or not we will list directories.
Definition: HttpServer.cpp:337
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...
Definition: HttpServer.cpp:349
Definition: HttpResponse.h:16