My Project
 All Classes Functions Variables Pages
WebServer.h
1 /*
2  * WebServer.h
3  *
4  * Created on: May 19, 2017
5  * Author: kolban
6  */
7 
8 #ifndef CPP_UTILS_WEBSERVER_H_
9 #define CPP_UTILS_WEBSERVER_H_
10 #include <string>
11 #include <vector>
12 #include <regex>
13 #include <map>
14 #include "sdkconfig.h"
15 
16 #ifdef CONFIG_MONGOOSE_PRESENT
17 #include <mongoose.h>
18 
19 #define MAX_CHUNK_LENGTH 4090 // 4 kilobytes
20 
21 class WebServer;
22 
28 class WebServer {
29 public:
33  class HTTPRequest {
34  public:
35  HTTPRequest(struct http_message* message);
36  const char* getMethod() const;
37  const char* getPath() const;
38  const char* getBody() const;
39  size_t getMethodLen() const;
40  size_t getPathLen() const;
41  size_t getBodyLen() const;
42  std::map<std::string, std::string> getQuery() const;
43  std::vector<std::string> pathSplit() const;
44 
45  private:
46  struct http_message* m_message;
47 
48  }; // HTTPRequest
49 
53  class HTTPResponse {
54  public:
55  HTTPResponse(struct mg_connection* nc);
56  void addHeader(const std::string& name, const std::string& value);
57  void addHeader(std::string&& name, std::string&& value);
58  void setStatus(int status);
59  void setHeaders(const std::map<std::string, std::string>& headers);
60  void setHeaders(std::map<std::string, std::string>&& headers);
61  void sendData(const std::string& data);
62  void sendData(const uint8_t* pData, size_t length);
63  void sendData(const char* pData, size_t length);
64  const std::string& getRootPath() const;
65  void setRootPath(const std::string& path);
66  void setRootPath(std::string&& path);
67  void sendChunkHead();
68  void sendChunk(const char* pData, size_t length);
69  void closeConnection();
70 
71  private:
72  struct mg_connection* m_nc;
73  std::string m_rootPath;
74  int m_status;
75  std::map<std::string, std::string> m_headers;
76  bool m_dataSent;
77  std::string buildHeaders();
78 
79  }; // HTTPResponse
80 
102  class HTTPMultiPart {
103  public:
104  virtual ~HTTPMultiPart() = default;
105  virtual void begin(const std::string& varName, const std::string& fileName);
106  virtual void end();
107  virtual void data(const std::string& data);
108  virtual void multipartEnd();
109  virtual void multipartStart();
110 
111  }; // HTTPMultiPart
112 
149  class HTTPMultiPartFactory {
150  public:
155  virtual HTTPMultiPart* newInstance() = 0;
156 
157  };
158 
163  class PathHandler {
164  public:
165  PathHandler(const std::string& method, const std::string& pathPattern, void (*webServerRequestHandler) (WebServer::HTTPRequest* pHttpRequest, WebServer::HTTPResponse* pHttpResponse));
166  PathHandler(std::string&& method, const std::string& pathPattern, void (*webServerRequestHandler) (WebServer::HTTPRequest* pHttpRequest, WebServer::HTTPResponse* pHttpResponse));
167  bool match(const char* method, size_t method_len, const char* path);
168  void invoke(HTTPRequest* request, HTTPResponse* response);
169 
170  private:
171  std::string m_method;
172  std::regex m_pattern;
173  void (*m_requestHandler)(WebServer::HTTPRequest* pHttpRequest, WebServer::HTTPResponse* pHttpResponse);
174 
175  }; // PathHandler
176 
180  class WebSocketHandler {
181  public:
182  void onCreated();
183  virtual void onMessage(const std::string& message);
184  void onClosed();
185  void sendData(const std::string& message);
186  void sendData(const uint8_t* data, uint32_t size);
187  void close();
188 
189  private:
190  struct mg_connection* m_mgConnection;
191 
192  };
193 
194  class WebSocketHandlerFactory {
195  public:
196  virtual WebSocketHandler* newInstance() = 0;
197 
198  };
199 
200  WebServer();
201  virtual ~WebServer();
202  void addPathHandler(const std::string& method, const std::string& pathExpr, void (*webServerRequestHandler) (WebServer::HTTPRequest* pHttpRequest, WebServer::HTTPResponse* pHttpResponse));
203  void addPathHandler(std::string&& method, const std::string& pathExpr, void (*webServerRequestHandler) (WebServer::HTTPRequest* pHttpRequest, WebServer::HTTPResponse* pHttpResponse));
204  const std::string& getRootPath();
205  void setMultiPartFactory(HTTPMultiPartFactory* pMultiPartFactory);
206  void setRootPath(const std::string& path);
207  void setRootPath(std::string&& path);
208  void setWebSocketHandlerFactory(WebSocketHandlerFactory* pWebSocketHandlerFactory);
209  void start(unsigned short port = 80);
210  void processRequest(struct mg_connection* mgConnection, struct http_message* message);
211  void continueConnection(struct mg_connection* mgConnection);
212  HTTPMultiPartFactory* m_pMultiPartFactory;
213  WebSocketHandlerFactory* m_pWebSocketHandlerFactory;
214 
215 private:
216  std::string m_rootPath;
217  std::vector<PathHandler> m_pathHandlers;
218  std::map<sock_t, FILE*> unfinishedConnection;
219 
220 };
221 
222 #endif // CONFIG_MONGOOSE_PRESENT
223 #endif /* CPP_UTILS_WEBSERVER_H_ */
Handle path matching for an incoming HTTP request.
Definition: HttpServer.h:27
Definition: WebSocket.h:47