CivetServer.cpp 9.0 KB

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