CivetServer.cpp 8.4 KB

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