CivetServer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Copyright (c) 2013-2014 the Civetweb developers
  2. * Copyright (c) 2013 No Face Press, LLC
  3. *
  4. * License http://opensource.org/licenses/mit-license.php MIT License
  5. */
  6. #include "CivetServer.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <stdexcept>
  11. #ifndef UNUSED_PARAMETER
  12. #define UNUSED_PARAMETER(x) (void)(x)
  13. #endif
  14. bool CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn) {
  15. UNUSED_PARAMETER(server);
  16. UNUSED_PARAMETER(conn);
  17. return false;
  18. }
  19. bool CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn) {
  20. UNUSED_PARAMETER(server);
  21. UNUSED_PARAMETER(conn);
  22. return false;
  23. }
  24. bool CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn) {
  25. UNUSED_PARAMETER(server);
  26. UNUSED_PARAMETER(conn);
  27. return false;
  28. }
  29. bool CivetHandler::handleDelete(CivetServer *server,
  30. struct mg_connection *conn) {
  31. UNUSED_PARAMETER(server);
  32. UNUSED_PARAMETER(conn);
  33. return false;
  34. }
  35. bool CivetHandler::handleOptions(CivetServer *server,
  36. struct mg_connection *conn) {
  37. UNUSED_PARAMETER(server);
  38. UNUSED_PARAMETER(conn);
  39. return false;
  40. }
  41. int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata) {
  42. const struct mg_request_info *request_info = mg_get_request_info(conn);
  43. assert(request_info != NULL);
  44. CivetServer *me = (CivetServer *)(request_info->user_data);
  45. assert(me != NULL);
  46. // Happens when a request hits the server before the context is saved
  47. if (me->context == NULL)
  48. return 0;
  49. mg_lock_context(me->context);
  50. me->connections[conn] = CivetConnection();
  51. mg_unlock_context(me->context);
  52. CivetHandler *handler = (CivetHandler *)cbdata;
  53. if (handler) {
  54. if (strcmp(request_info->request_method, "GET") == 0) {
  55. return handler->handleGet(me, conn) ? 1 : 0;
  56. } else if (strcmp(request_info->request_method, "POST") == 0) {
  57. return handler->handlePost(me, conn) ? 1 : 0;
  58. } else if (strcmp(request_info->request_method, "PUT") == 0) {
  59. return handler->handlePut(me, conn) ? 1 : 0;
  60. } else if (strcmp(request_info->request_method, "DELETE") == 0) {
  61. return handler->handleDelete(me, conn) ? 1 : 0;
  62. } else if (strcmp(request_info->request_method, "OPTIONS") == 0) {
  63. return handler->handleOptions(me, conn) ? 1 : 0;
  64. }
  65. }
  66. return 0; // No handler found
  67. }
  68. CivetServer::CivetServer(const char **options,
  69. const struct mg_callbacks *_callbacks)
  70. : context(0) {
  71. struct mg_callbacks callbacks;
  72. memset(&callbacks, 0, sizeof(callbacks));
  73. if (_callbacks) {
  74. callbacks = *_callbacks;
  75. userCloseHandler = _callbacks->connection_close;
  76. } else {
  77. userCloseHandler = NULL;
  78. }
  79. callbacks.connection_close = closeHandler;
  80. context = mg_start(&callbacks, this, options);
  81. if (context == NULL)
  82. throw CivetException("null context when constructing CivetServer. "
  83. "Possible problem binding to port.");
  84. }
  85. CivetServer::~CivetServer() { close(); }
  86. void CivetServer::closeHandler(const struct mg_connection *conn) {
  87. const struct mg_request_info *request_info = mg_get_request_info(conn);
  88. assert(request_info != NULL);
  89. CivetServer *me = (CivetServer *)(request_info->user_data);
  90. assert(me != NULL);
  91. // Happens when a request hits the server before the context is saved
  92. if (me->context == NULL)
  93. return;
  94. if (me->userCloseHandler)
  95. me->userCloseHandler(conn);
  96. mg_lock_context(me->context);
  97. me->connections.erase(const_cast<struct mg_connection *>(conn));
  98. mg_unlock_context(me->context);
  99. }
  100. void CivetServer::addHandler(const std::string &uri, CivetHandler *handler) {
  101. mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
  102. }
  103. void CivetServer::removeHandler(const std::string &uri) {
  104. mg_set_request_handler(context, uri.c_str(), NULL, NULL);
  105. }
  106. void CivetServer::close() {
  107. if (context) {
  108. mg_stop(context);
  109. context = 0;
  110. }
  111. }
  112. int CivetServer::getCookie(struct mg_connection *conn,
  113. const std::string &cookieName,
  114. std::string &cookieValue) {
  115. // Maximum cookie length as per microsoft is 4096.
  116. // http://msdn.microsoft.com/en-us/library/ms178194.aspx
  117. char _cookieValue[4096];
  118. const char *cookie = mg_get_header(conn, "Cookie");
  119. int lRead = mg_get_cookie(cookie, cookieName.c_str(), _cookieValue,
  120. sizeof(_cookieValue));
  121. cookieValue.clear();
  122. cookieValue.append(_cookieValue);
  123. return lRead;
  124. }
  125. const char *CivetServer::getHeader(struct mg_connection *conn,
  126. const std::string &headerName) {
  127. return mg_get_header(conn, headerName.c_str());
  128. }
  129. void CivetServer::urlDecode(const char *src, std::string &dst,
  130. bool is_form_url_encoded) {
  131. urlDecode(src, strlen(src), dst, is_form_url_encoded);
  132. }
  133. void CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst,
  134. bool is_form_url_encoded) {
  135. int i, j, a, b;
  136. #define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
  137. dst.clear();
  138. for (i = j = 0; i < (int)src_len; i++, j++) {
  139. if (i < (int)src_len - 2 && src[i] == '%' &&
  140. isxdigit(*(const unsigned char *)(src + i + 1)) &&
  141. isxdigit(*(const unsigned char *)(src + i + 2))) {
  142. a = tolower(*(const unsigned char *)(src + i + 1));
  143. b = tolower(*(const unsigned char *)(src + i + 2));
  144. dst.push_back((char)((HEXTOI(a) << 4) | HEXTOI(b)));
  145. i += 2;
  146. } else if (is_form_url_encoded && src[i] == '+') {
  147. dst.push_back(' ');
  148. } else {
  149. dst.push_back(src[i]);
  150. }
  151. }
  152. }
  153. bool CivetServer::getParam(struct mg_connection *conn, const char *name,
  154. std::string &dst, size_t occurrence) {
  155. const char *formParams = NULL;
  156. const struct mg_request_info *ri = mg_get_request_info(conn);
  157. assert(ri != NULL);
  158. CivetServer *me = (CivetServer *)(ri->user_data);
  159. assert(me != NULL);
  160. mg_lock_context(me->context);
  161. CivetConnection &conobj = me->connections[conn];
  162. mg_lock_connection(conn);
  163. mg_unlock_context(me->context);
  164. if (conobj.postData != NULL) {
  165. formParams = conobj.postData;
  166. } else {
  167. const char *con_len_str = mg_get_header(conn, "Content-Length");
  168. if (con_len_str) {
  169. unsigned long con_len = atoi(con_len_str);
  170. if (con_len > 0) {
  171. // Add one extra character: in case the post-data is a text, it
  172. // is required as 0-termination.
  173. // Do not increment con_len, since the 0 terminating is not part
  174. // of the content (text or binary).
  175. conobj.postData = (char *)malloc(con_len + 1);
  176. if (conobj.postData != NULL) {
  177. // malloc may fail for huge requests
  178. mg_read(conn, conobj.postData, con_len);
  179. conobj.postData[con_len] = 0;
  180. formParams = conobj.postData;
  181. conobj.postDataLen = con_len;
  182. }
  183. }
  184. }
  185. }
  186. if (formParams == NULL) {
  187. // get requests do store html <form> field values in the http
  188. // query_string
  189. formParams = ri->query_string;
  190. }
  191. mg_unlock_connection(conn);
  192. if (formParams != NULL) {
  193. return getParam(formParams, strlen(formParams), name, dst, occurrence);
  194. }
  195. return false;
  196. }
  197. bool CivetServer::getParam(const char *data, size_t data_len, const char *name,
  198. std::string &dst, size_t occurrence) {
  199. const char *p, *e, *s;
  200. size_t name_len;
  201. dst.clear();
  202. if (data == NULL || name == NULL || data_len == 0) {
  203. return false;
  204. }
  205. name_len = strlen(name);
  206. e = data + data_len;
  207. // data is "var1=val1&var2=val2...". Find variable first
  208. for (p = data; p + name_len < e; p++) {
  209. if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
  210. !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) {
  211. // Point p to variable value
  212. p += name_len + 1;
  213. // Point s to the end of the value
  214. s = (const char *)memchr(p, '&', (size_t)(e - p));
  215. if (s == NULL) {
  216. s = e;
  217. }
  218. assert(s >= p);
  219. // Decode variable into destination buffer
  220. urlDecode(p, (int)(s - p), dst, true);
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. void CivetServer::urlEncode(const char *src, std::string &dst, bool append) {
  227. urlEncode(src, strlen(src), dst, append);
  228. }
  229. void CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst,
  230. bool append) {
  231. static const char *dont_escape = "._-$,;~()";
  232. static const char *hex = "0123456789abcdef";
  233. if (!append)
  234. dst.clear();
  235. for (; src_len > 0; src++, src_len--) {
  236. if (isalnum(*(const unsigned char *)src) ||
  237. strchr(dont_escape, *(const unsigned char *)src) != NULL) {
  238. dst.push_back(*src);
  239. } else {
  240. dst.push_back('%');
  241. dst.push_back(hex[(*(const unsigned char *)src) >> 4]);
  242. dst.push_back(hex[(*(const unsigned char *)src) & 0xf]);
  243. }
  244. }
  245. }
  246. std::vector<int> CivetServer::getListeningPorts() {
  247. std::vector<int> ports(10);
  248. std::vector<int> ssl(10);
  249. size_t size = mg_get_ports(context, ports.size(), &ports[0], &ssl[0]);
  250. ports.resize(size);
  251. ssl.resize(size);
  252. return ports;
  253. }
  254. CivetServer::CivetConnection::CivetConnection() {
  255. postData = NULL;
  256. postDataLen = 0;
  257. }
  258. CivetServer::CivetConnection::~CivetConnection() { free(postData); }