CivetServer.cpp 9.0 KB

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