BellHTTPServer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "BellHTTPServer.h"
  2. #include <string.h> // for memcpy
  3. #include <cassert> // for assert
  4. #include <exception> // for exception
  5. #include <mutex> // for scoped_lock
  6. #include <regex> // for sregex_token_iterator, regex
  7. #include "BellLogger.h" // for AbstractLogger, BELL_LOG, bell
  8. #include "CivetServer.h" // for CivetServer, CivetWebSocketHandler
  9. #include "civetweb.h" // for mg_get_request_info, mg_printf, mg_set_user...
  10. using namespace bell;
  11. class WebSocketHandler : public CivetWebSocketHandler {
  12. public:
  13. BellHTTPServer::WSDataHandler dataHandler;
  14. BellHTTPServer::WSStateHandler stateHandler;
  15. WebSocketHandler(BellHTTPServer::WSDataHandler dataHandler,
  16. BellHTTPServer::WSStateHandler stateHandler) {
  17. this->dataHandler = dataHandler;
  18. this->stateHandler = stateHandler;
  19. }
  20. virtual bool handleConnection(CivetServer* server,
  21. struct mg_connection* conn) {
  22. this->stateHandler(conn, BellHTTPServer::WSState::CONNECTED);
  23. return true;
  24. }
  25. virtual void handleReadyState(CivetServer* server,
  26. struct mg_connection* conn) {
  27. this->stateHandler(conn, BellHTTPServer::WSState::READY);
  28. }
  29. virtual bool handleData(CivetServer* server, struct mg_connection* conn,
  30. int flags, char* data, size_t data_len) {
  31. if ((flags & 0xf) == MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE) {
  32. // Received close message from client. Close the connection.
  33. this->stateHandler(conn, BellHTTPServer::WSState::CLOSED);
  34. return false;
  35. }
  36. this->dataHandler(conn, data, data_len);
  37. return true;
  38. }
  39. virtual void handleClose(CivetServer* server, struct mg_connection* conn) {
  40. stateHandler(conn, BellHTTPServer::WSState::CLOSED);
  41. }
  42. };
  43. std::vector<std::string> BellHTTPServer::Router::split(
  44. const std::string str, const std::string regex_str) {
  45. std::regex regexz(regex_str);
  46. return {std::sregex_token_iterator(str.begin(), str.end(), regexz, -1),
  47. std::sregex_token_iterator()};
  48. }
  49. void BellHTTPServer::Router::insert(const std::string& route,
  50. HTTPHandler& value) {
  51. auto parts = split(route, "/");
  52. auto currentNode = &root;
  53. for (int index = 0; index < parts.size(); index++) {
  54. auto part = parts[index];
  55. if (part[0] == ':') {
  56. currentNode->isParam = true;
  57. currentNode->paramName = part.substr(1);
  58. part = "";
  59. } else if (part[0] == '*') {
  60. currentNode->isCatchAll = true;
  61. currentNode->value = value;
  62. return;
  63. }
  64. if (!currentNode->children.count(part)) {
  65. currentNode->children[part] = std::make_unique<RouterNode>();
  66. }
  67. currentNode = currentNode->children[part].get();
  68. }
  69. currentNode->value = value;
  70. }
  71. BellHTTPServer::Router::HandlerAndParams BellHTTPServer::Router::find(
  72. const std::string& route) {
  73. auto parts = split(route, "/");
  74. auto currentNode = &root;
  75. std::unordered_map<std::string, std::string> params;
  76. for (int index = 0; index < parts.size(); index++) {
  77. auto part = parts[index];
  78. if (currentNode->children.count(part)) {
  79. currentNode = currentNode->children[part].get();
  80. } else if (currentNode->isParam) {
  81. params[currentNode->paramName] = part;
  82. if (currentNode->children.count("")) {
  83. currentNode = currentNode->children[""].get();
  84. } else {
  85. return {nullptr, Params()};
  86. }
  87. } else if (currentNode->isCatchAll) {
  88. params["**"] = '*';
  89. return {currentNode->value, params};
  90. } else {
  91. return {nullptr, Params()};
  92. }
  93. }
  94. if (currentNode->value != nullptr) {
  95. return {currentNode->value, params};
  96. }
  97. return {nullptr, Params()};
  98. }
  99. bool BellHTTPServer::handleGet(CivetServer* server,
  100. struct mg_connection* conn) {
  101. std::scoped_lock lock(this->responseMutex);
  102. auto requestInfo = mg_get_request_info(conn);
  103. auto handler = getRequestsRouter.find(requestInfo->local_uri);
  104. if (handler.first == nullptr) {
  105. if (this->notFoundHandler != nullptr) {
  106. this->notFoundHandler(conn);
  107. return true;
  108. }
  109. return false;
  110. }
  111. mg_set_user_connection_data(conn, &handler.second);
  112. try {
  113. auto reply = handler.first(conn);
  114. if (reply->body == nullptr) {
  115. return true;
  116. }
  117. mg_printf(
  118. conn,
  119. "HTTP/1.1 %d OK\r\nContent-Type: "
  120. "%s\r\nAccess-Control-Allow-Origin: *\r\nConnection: close\r\n\r\n",
  121. reply->status, reply->headers["Content-Type"].c_str());
  122. mg_write(conn, reply->body, reply->bodySize);
  123. return true;
  124. } catch (std::exception& e) {
  125. BELL_LOG(error, "HttpServer", "Exception occured in handler: %s", e.what());
  126. return false;
  127. }
  128. }
  129. bool BellHTTPServer::handlePost(CivetServer* server,
  130. struct mg_connection* conn) {
  131. std::scoped_lock lock(this->responseMutex);
  132. auto requestInfo = mg_get_request_info(conn);
  133. auto handler = postRequestsRouter.find(requestInfo->local_uri);
  134. if (handler.first == nullptr) {
  135. return false;
  136. }
  137. mg_set_user_connection_data(conn, &handler.second);
  138. try {
  139. auto reply = handler.first(conn);
  140. if (reply->body == nullptr) {
  141. return true;
  142. }
  143. mg_printf(
  144. conn,
  145. "HTTP/1.1 %d OK\r\nContent-Type: "
  146. "%s\r\nAccess-Control-Allow-Origin: *\r\nConnection: close\r\n\r\n",
  147. reply->status, reply->headers["Content-Type"].c_str());
  148. mg_write(conn, reply->body, reply->bodySize);
  149. return true;
  150. } catch (std::exception& e) {
  151. BELL_LOG(error, "HttpServer", "Exception occured in handler: %s", e.what());
  152. return false;
  153. }
  154. }
  155. BellHTTPServer::BellHTTPServer(int serverPort) {
  156. mg_init_library(0);
  157. BELL_LOG(info, "HttpServer", "Server listening on port %d", serverPort);
  158. this->serverPort = serverPort;
  159. auto port = std::to_string(this->serverPort);
  160. civetWebOptions.push_back("listening_ports");
  161. civetWebOptions.push_back(port);
  162. server = std::make_unique<CivetServer>(civetWebOptions);
  163. }
  164. std::unique_ptr<BellHTTPServer::HTTPResponse> BellHTTPServer::makeJsonResponse(
  165. const std::string& json, int status) {
  166. auto response = std::make_unique<BellHTTPServer::HTTPResponse>();
  167. response->body = (uint8_t*)malloc(json.size());
  168. response->bodySize = json.size();
  169. response->headers["Content-Type"] = "application/json";
  170. response->status = status;
  171. memcpy(response->body, json.c_str(), json.size());
  172. return response;
  173. }
  174. std::unique_ptr<BellHTTPServer::HTTPResponse>
  175. BellHTTPServer::makeEmptyResponse() {
  176. auto response = std::make_unique<BellHTTPServer::HTTPResponse>();
  177. return response;
  178. }
  179. void BellHTTPServer::registerGet(const std::string& url,
  180. BellHTTPServer::HTTPHandler handler) {
  181. server->addHandler(url, this);
  182. getRequestsRouter.insert(url, handler);
  183. }
  184. void BellHTTPServer::registerPost(const std::string& url,
  185. BellHTTPServer::HTTPHandler handler) {
  186. server->addHandler(url, this);
  187. postRequestsRouter.insert(url, handler);
  188. }
  189. void BellHTTPServer::registerWS(const std::string& url,
  190. BellHTTPServer::WSDataHandler dataHandler,
  191. BellHTTPServer::WSStateHandler stateHandler) {
  192. server->addWebSocketHandler(url,
  193. new WebSocketHandler(dataHandler, stateHandler));
  194. }
  195. void BellHTTPServer::registerNotFound(HTTPHandler handler) {
  196. this->notFoundHandler = handler;
  197. }
  198. std::unordered_map<std::string, std::string> BellHTTPServer::extractParams(
  199. struct mg_connection* conn) {
  200. void* data = mg_get_user_connection_data(conn);
  201. assert(data != nullptr);
  202. std::unordered_map<std::string, std::string>& params =
  203. *(std::unordered_map<std::string, std::string>*)data;
  204. return params;
  205. }