CivetServer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2013 No Face Press, LLC
  3. * License http://opensource.org/licenses/mit-license.php MIT License
  4. */
  5. #include "CivetServer.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #ifndef UNUSED_PARAMETER
  9. #define UNUSED_PARAMETER(x) (void)(x)
  10. #endif
  11. bool CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn) {
  12. UNUSED_PARAMETER(server);
  13. UNUSED_PARAMETER(conn);
  14. return false;
  15. }
  16. bool CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn) {
  17. UNUSED_PARAMETER(server);
  18. UNUSED_PARAMETER(conn);
  19. return false;
  20. }
  21. bool CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn) {
  22. UNUSED_PARAMETER(server);
  23. UNUSED_PARAMETER(conn);
  24. return false;
  25. }
  26. bool CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn) {
  27. UNUSED_PARAMETER(server);
  28. UNUSED_PARAMETER(conn);
  29. return false;
  30. }
  31. int CivetServer::begin_request_callback(struct mg_connection *conn) {
  32. struct mg_request_info *request_info = mg_get_request_info(conn);
  33. if (!request_info->user_data)
  34. return 0;
  35. CivetServer *me = (CivetServer*) (request_info->user_data);
  36. if (me->handleRequest(conn)) {
  37. return 1; // Mark as processed
  38. }
  39. return 0;
  40. }
  41. bool CivetServer::handleRequest(struct mg_connection *conn) {
  42. struct mg_request_info *request_info = mg_get_request_info(conn);
  43. CivetHandler *handler = getHandler(request_info->uri);
  44. if (handler) {
  45. if (strcmp(request_info->request_method, "GET") == 0) {
  46. return handler->handleGet(this, conn);
  47. } else if (strcmp(request_info->request_method, "POST") == 0) {
  48. return handler->handlePost(this, conn);
  49. } else if (strcmp(request_info->request_method, "PUT") == 0) {
  50. return !handler->handlePost(this, conn);
  51. } else if (strcmp(request_info->request_method, "DELETE") == 0) {
  52. return !handler->handlePost(this, conn);
  53. }
  54. }
  55. return false; // No handler found
  56. }
  57. CivetServer::CivetServer(const char **options,
  58. const struct mg_callbacks *_callbacks) :
  59. context(0) {
  60. struct mg_callbacks callbacks;
  61. if (_callbacks) {
  62. memcpy(&callbacks, _callbacks, sizeof(callbacks));
  63. } else {
  64. memset(&callbacks, 0, sizeof(callbacks));
  65. }
  66. callbacks.begin_request = &begin_request_callback;
  67. context = mg_start(&callbacks, this, options);
  68. }
  69. CivetServer::~CivetServer() {
  70. close();
  71. }
  72. CivetHandler *CivetServer::getHandler(const char *uri, unsigned urilen) const {
  73. for (unsigned index = 0; index < uris.size(); index++) {
  74. const std::string &handlerURI = uris[index];
  75. // first try for an exact match
  76. if (handlerURI == uri) {
  77. return handlers[index];
  78. }
  79. // next try for a partial match
  80. // we will accept uri/something
  81. if (handlerURI.length() < urilen
  82. && uri[handlerURI.length()] == '/'
  83. && handlerURI.compare(0, handlerURI.length(), uri, handlerURI.length()) == 0) {
  84. return handlers[index];
  85. }
  86. }
  87. return 0; // none found
  88. }
  89. void CivetServer::addHandler(const std::string &uri, CivetHandler *handler) {
  90. int index = getIndex(uri);
  91. if (index < 0) {
  92. uris.push_back(uri);
  93. handlers.push_back(handler);
  94. } else if (handlers[index] != handler) {
  95. delete handlers[index];
  96. handlers[index] = handler;
  97. }
  98. }
  99. void CivetServer::removeHandler(const std::string &uri) {
  100. int index = getIndex(uri);
  101. if (index >= 0) {
  102. uris.erase(uris.begin() + index, uris.begin() + index + 1);
  103. handlers.erase(handlers.begin() + index, handlers.begin() + index + 1);
  104. }
  105. }
  106. int CivetServer::getIndex(const std::string &uri) const {
  107. for (unsigned index = 0; index < uris.size(); index++) {
  108. if (uris[index].compare(uri) == 0)
  109. return index;
  110. }
  111. return -1;
  112. }
  113. void CivetServer::close() {
  114. if (context) {
  115. mg_stop (context);
  116. context = 0;
  117. }
  118. for (int i = handlers.size() - 1; i >= 0; i--) {
  119. delete handlers[i];
  120. }
  121. handlers.clear();
  122. uris.clear();
  123. }