CivetServer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #ifndef UNUSED_PARAMETER
  11. #define UNUSED_PARAMETER(x) (void)(x)
  12. #endif
  13. bool CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn)
  14. {
  15. UNUSED_PARAMETER(server);
  16. UNUSED_PARAMETER(conn);
  17. return false;
  18. }
  19. bool CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn)
  20. {
  21. UNUSED_PARAMETER(server);
  22. UNUSED_PARAMETER(conn);
  23. return false;
  24. }
  25. bool CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn)
  26. {
  27. UNUSED_PARAMETER(server);
  28. UNUSED_PARAMETER(conn);
  29. return false;
  30. }
  31. bool CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
  32. {
  33. UNUSED_PARAMETER(server);
  34. UNUSED_PARAMETER(conn);
  35. return false;
  36. }
  37. int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
  38. {
  39. struct mg_request_info *request_info = mg_get_request_info(conn);
  40. assert(request_info != NULL);
  41. CivetServer *me = (CivetServer*) (request_info->user_data);
  42. assert(me != NULL);
  43. CivetHandler *handler = (CivetHandler *)cbdata;
  44. if (handler) {
  45. if (strcmp(request_info->request_method, "GET") == 0) {
  46. return handler->handleGet(me, conn) ? 1 : 0;
  47. } else if (strcmp(request_info->request_method, "POST") == 0) {
  48. return handler->handlePost(me, conn) ? 1 : 0;
  49. } else if (strcmp(request_info->request_method, "PUT") == 0) {
  50. return handler->handlePut(me, conn) ? 1 : 0;
  51. } else if (strcmp(request_info->request_method, "DELETE") == 0) {
  52. return handler->handleDelete(me, conn) ? 1 : 0;
  53. }
  54. }
  55. return 0; // No handler found
  56. }
  57. CivetServer::CivetServer(const char **options,
  58. const struct mg_callbacks *_callbacks) :
  59. context(0), postData(0)
  60. {
  61. struct mg_callbacks callbacks;
  62. memset(&callbacks, 0, sizeof(callbacks));
  63. if (_callbacks) {
  64. callbacks = *_callbacks;
  65. userCloseHandler = _callbacks->connection_close;
  66. } else {
  67. userCloseHandler = NULL;
  68. }
  69. callbacks.connection_close = closeHandler;
  70. context = mg_start(&callbacks, this, options);
  71. }
  72. CivetServer::~CivetServer()
  73. {
  74. close();
  75. }
  76. void CivetServer::closeHandler(struct mg_connection *conn)
  77. {
  78. struct mg_request_info *request_info = mg_get_request_info(conn);
  79. assert(request_info != NULL);
  80. CivetServer *me = (CivetServer*) (request_info->user_data);
  81. assert(me != NULL);
  82. if (me->userCloseHandler) me->userCloseHandler(conn);
  83. if (me->postData) {
  84. delete [] (me->postData);
  85. me->postData = 0;
  86. }
  87. }
  88. void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
  89. {
  90. mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
  91. }
  92. void CivetServer::removeHandler(const std::string &uri)
  93. {
  94. mg_set_request_handler(context, uri.c_str(), NULL, NULL);
  95. }
  96. void CivetServer::close()
  97. {
  98. if (context) {
  99. mg_stop (context);
  100. context = 0;
  101. }
  102. }
  103. int CivetServer::getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue)
  104. {
  105. //Maximum cookie length as per microsoft is 4096. http://msdn.microsoft.com/en-us/library/ms178194.aspx
  106. char _cookieValue[4096];
  107. const char *cookie = mg_get_header(conn, "Cookie");
  108. int lRead = mg_get_cookie(cookie, cookieName.c_str(), _cookieValue, sizeof(_cookieValue));
  109. cookieValue.clear();
  110. cookieValue.append(_cookieValue);
  111. return lRead;
  112. }
  113. const char* CivetServer::getHeader(struct mg_connection *conn, const std::string &headerName)
  114. {
  115. return mg_get_header(conn, headerName.c_str());
  116. }
  117. void
  118. CivetServer::urlDecode(const char *src, std::string &dst, bool is_form_url_encoded)
  119. {
  120. urlDecode(src, strlen(src), dst, is_form_url_encoded);
  121. }
  122. void
  123. CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded)
  124. {
  125. int i, j, a, b;
  126. #define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
  127. dst.clear();
  128. for (i = j = 0; i < (int)src_len; i++, j++) {
  129. if (src[i] == '%' && i < (int)src_len - 2 &&
  130. isxdigit(* (const unsigned char *) (src + i + 1)) &&
  131. isxdigit(* (const unsigned char *) (src + i + 2))) {
  132. a = tolower(* (const unsigned char *) (src + i + 1));
  133. b = tolower(* (const unsigned char *) (src + i + 2));
  134. dst.push_back((char) ((HEXTOI(a) << 4) | HEXTOI(b)));
  135. i += 2;
  136. } else if (is_form_url_encoded && src[i] == '+') {
  137. dst.push_back(' ');
  138. } else {
  139. dst.push_back(src[i]);
  140. }
  141. }
  142. }
  143. bool
  144. CivetServer::getParam(struct mg_connection *conn, const char *name,
  145. std::string &dst, size_t occurrence)
  146. {
  147. const char *formParams = NULL;
  148. struct mg_request_info *ri = mg_get_request_info(conn);
  149. assert(ri != NULL);
  150. CivetServer *me = (CivetServer*) (ri->user_data);
  151. assert(me != NULL);
  152. if (me->postData != NULL) {
  153. formParams = me->postData;
  154. } else {
  155. const char * con_len_str = mg_get_header(conn, "Content-Length");
  156. if (con_len_str) {
  157. unsigned long con_len = atoi(con_len_str);
  158. if (con_len>0) {
  159. me->postData = new char[con_len];
  160. if (me->postData != NULL) {
  161. /* NULL check is not required according to current C++ standard */
  162. mg_read(conn, me->postData, con_len);
  163. formParams = me->postData;
  164. }
  165. }
  166. }
  167. }
  168. if (formParams == NULL) {
  169. // get requests do store html <form> field values in the http query_string
  170. formParams = ri->query_string;
  171. }
  172. if (formParams != NULL) {
  173. return getParam(formParams, strlen(formParams), name, dst, occurrence);
  174. }
  175. return false;
  176. }
  177. bool
  178. CivetServer::getParam(const char *data, size_t data_len, const char *name,
  179. std::string &dst, size_t occurrence)
  180. {
  181. const char *p, *e, *s;
  182. size_t name_len;
  183. dst.clear();
  184. if (data == NULL || name == NULL || data_len == 0) {
  185. return false;
  186. }
  187. name_len = strlen(name);
  188. e = data + data_len;
  189. // data is "var1=val1&var2=val2...". Find variable first
  190. for (p = data; p + name_len < e; p++) {
  191. if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
  192. !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) {
  193. // Point p to variable value
  194. p += name_len + 1;
  195. // Point s to the end of the value
  196. s = (const char *) memchr(p, '&', (size_t)(e - p));
  197. if (s == NULL) {
  198. s = e;
  199. }
  200. assert(s >= p);
  201. // Decode variable into destination buffer
  202. urlDecode(p, (int)(s - p), dst, true);
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. void
  209. CivetServer::urlEncode(const char *src, std::string &dst, bool append)
  210. {
  211. urlEncode(src, strlen(src), dst, append);
  212. }
  213. void
  214. CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst, bool append)
  215. {
  216. static const char *dont_escape = "._-$,;~()";
  217. static const char *hex = "0123456789abcdef";
  218. if (!append)
  219. dst.clear();
  220. for (; src_len > 0; src++, src_len--) {
  221. if (isalnum(*(const unsigned char *) src) ||
  222. strchr(dont_escape, * (const unsigned char *) src) != NULL) {
  223. dst.push_back(*src);
  224. } else {
  225. dst.push_back('%');
  226. dst.push_back(hex[(* (const unsigned char *) src) >> 4]);
  227. dst.push_back(hex[(* (const unsigned char *) src) & 0xf]);
  228. }
  229. }
  230. }