CivetServer.cpp 8.9 KB

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