CivetServer.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. bool CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn)
  38. {
  39. UNUSED_PARAMETER(server);
  40. UNUSED_PARAMETER(conn);
  41. return false;
  42. }
  43. int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
  44. {
  45. struct mg_request_info *request_info = mg_get_request_info(conn);
  46. assert(request_info != NULL);
  47. CivetServer *me = (CivetServer*) (request_info->user_data);
  48. assert(me != NULL);
  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. {
  72. struct mg_callbacks callbacks;
  73. memset(&callbacks, 0, sizeof(callbacks));
  74. if (_callbacks) {
  75. callbacks = *_callbacks;
  76. userCloseHandler = _callbacks->connection_close;
  77. } else {
  78. userCloseHandler = NULL;
  79. }
  80. callbacks.connection_close = closeHandler;
  81. context = mg_start(&callbacks, this, options);
  82. }
  83. CivetServer::~CivetServer()
  84. {
  85. close();
  86. }
  87. void CivetServer::closeHandler(struct mg_connection *conn)
  88. {
  89. struct mg_request_info *request_info = mg_get_request_info(conn);
  90. assert(request_info != NULL);
  91. CivetServer *me = (CivetServer*) (request_info->user_data);
  92. assert(me != NULL);
  93. if (me->userCloseHandler) me->userCloseHandler(conn);
  94. mg_lock_context(me->context);
  95. me->connections.erase(conn);
  96. mg_unlock_context(me->context);
  97. }
  98. void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
  99. {
  100. mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
  101. }
  102. void CivetServer::removeHandler(const std::string &uri)
  103. {
  104. mg_set_request_handler(context, uri.c_str(), NULL, NULL);
  105. }
  106. void CivetServer::close()
  107. {
  108. if (context) {
  109. mg_stop (context);
  110. context = 0;
  111. }
  112. }
  113. int CivetServer::getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue)
  114. {
  115. //Maximum cookie length as per microsoft is 4096. http://msdn.microsoft.com/en-us/library/ms178194.aspx
  116. char _cookieValue[4096];
  117. const char *cookie = mg_get_header(conn, "Cookie");
  118. int lRead = mg_get_cookie(cookie, cookieName.c_str(), _cookieValue, sizeof(_cookieValue));
  119. cookieValue.clear();
  120. cookieValue.append(_cookieValue);
  121. return lRead;
  122. }
  123. const char* CivetServer::getHeader(struct mg_connection *conn, const std::string &headerName)
  124. {
  125. return mg_get_header(conn, headerName.c_str());
  126. }
  127. void
  128. CivetServer::urlDecode(const char *src, std::string &dst, bool is_form_url_encoded)
  129. {
  130. urlDecode(src, strlen(src), dst, is_form_url_encoded);
  131. }
  132. void
  133. CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded)
  134. {
  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
  154. CivetServer::getParam(struct mg_connection *conn, const char *name,
  155. std::string &dst, size_t occurrence)
  156. {
  157. const char *formParams = NULL;
  158. struct mg_request_info *ri = mg_get_request_info(conn);
  159. assert(ri != NULL);
  160. CivetServer *me = (CivetServer*) (ri->user_data);
  161. assert(me != NULL);
  162. mg_lock_context(me->context);
  163. CivetConnection &conobj = me->connections[conn];
  164. mg_lock_connection(conn);
  165. mg_unlock_context(me->context);
  166. if (conobj.postData != NULL) {
  167. formParams = conobj.postData;
  168. } else {
  169. const char * con_len_str = mg_get_header(conn, "Content-Length");
  170. if (con_len_str) {
  171. unsigned long con_len = atoi(con_len_str);
  172. if (con_len>0) {
  173. // Add one extra character: in case the post-data is a text, it is required as 0-termination.
  174. // Do not increment con_len, since the 0 terminating is not part 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 query_string
  188. formParams = ri->query_string;
  189. }
  190. mg_unlock_connection(conn);
  191. if (formParams != NULL) {
  192. return getParam(formParams, strlen(formParams), name, dst, occurrence);
  193. }
  194. return false;
  195. }
  196. bool
  197. CivetServer::getParam(const char *data, size_t data_len, const char *name,
  198. std::string &dst, size_t occurrence)
  199. {
  200. const char *p, *e, *s;
  201. size_t name_len;
  202. dst.clear();
  203. if (data == NULL || name == NULL || data_len == 0) {
  204. return false;
  205. }
  206. name_len = strlen(name);
  207. e = data + data_len;
  208. // data is "var1=val1&var2=val2...". Find variable first
  209. for (p = data; p + name_len < e; p++) {
  210. if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
  211. !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) {
  212. // Point p to variable value
  213. p += name_len + 1;
  214. // Point s to the end of the value
  215. s = (const char *) memchr(p, '&', (size_t)(e - p));
  216. if (s == NULL) {
  217. s = e;
  218. }
  219. assert(s >= p);
  220. // Decode variable into destination buffer
  221. urlDecode(p, (int)(s - p), dst, true);
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. void
  228. CivetServer::urlEncode(const char *src, std::string &dst, bool append)
  229. {
  230. urlEncode(src, strlen(src), dst, append);
  231. }
  232. void
  233. CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst, bool append)
  234. {
  235. static const char *dont_escape = "._-$,;~()";
  236. static const char *hex = "0123456789abcdef";
  237. if (!append)
  238. dst.clear();
  239. for (; src_len > 0; src++, src_len--) {
  240. if (isalnum(*(const unsigned char *) src) ||
  241. strchr(dont_escape, * (const unsigned char *) src) != NULL) {
  242. dst.push_back(*src);
  243. } else {
  244. dst.push_back('%');
  245. dst.push_back(hex[(* (const unsigned char *) src) >> 4]);
  246. dst.push_back(hex[(* (const unsigned char *) src) & 0xf]);
  247. }
  248. }
  249. }
  250. CivetServer::CivetConnection::CivetConnection() {
  251. postData = NULL;
  252. postDataLen = 0;
  253. }
  254. CivetServer::CivetConnection::~CivetConnection() {
  255. free(postData);
  256. }