CivetServer.cpp 9.4 KB

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