CivetServer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /* Copyright (c) 2013-2020 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 <assert.h>
  8. #include <stdexcept>
  9. #include <string.h>
  10. #ifndef UNUSED_PARAMETER
  11. #define UNUSED_PARAMETER(x) (void)(x)
  12. #endif
  13. #ifndef MAX_PARAM_BODY_LENGTH
  14. // Set a default limit for parameters in a form body: 2 MB
  15. #define MAX_PARAM_BODY_LENGTH (1024 * 1024 * 2)
  16. #endif
  17. bool
  18. CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn)
  19. {
  20. UNUSED_PARAMETER(server);
  21. UNUSED_PARAMETER(conn);
  22. return false;
  23. }
  24. bool
  25. CivetHandler::handleGet(CivetServer *server,
  26. struct mg_connection *conn,
  27. int *status_code)
  28. {
  29. UNUSED_PARAMETER(server);
  30. UNUSED_PARAMETER(conn);
  31. if (status_code) {
  32. *status_code = -1;
  33. }
  34. return false;
  35. }
  36. bool
  37. CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn)
  38. {
  39. UNUSED_PARAMETER(server);
  40. UNUSED_PARAMETER(conn);
  41. return false;
  42. }
  43. bool
  44. CivetHandler::handlePost(CivetServer *server,
  45. struct mg_connection *conn,
  46. int *status_code)
  47. {
  48. UNUSED_PARAMETER(server);
  49. UNUSED_PARAMETER(conn);
  50. if (status_code) {
  51. *status_code = -1;
  52. }
  53. return false;
  54. }
  55. bool
  56. CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn)
  57. {
  58. UNUSED_PARAMETER(server);
  59. UNUSED_PARAMETER(conn);
  60. return false;
  61. }
  62. bool
  63. CivetHandler::handleHead(CivetServer *server,
  64. struct mg_connection *conn,
  65. int *status_code)
  66. {
  67. UNUSED_PARAMETER(server);
  68. UNUSED_PARAMETER(conn);
  69. if (status_code) {
  70. *status_code = -1;
  71. }
  72. return false;
  73. }
  74. bool
  75. CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn)
  76. {
  77. UNUSED_PARAMETER(server);
  78. UNUSED_PARAMETER(conn);
  79. return false;
  80. }
  81. bool
  82. CivetHandler::handlePut(CivetServer *server,
  83. struct mg_connection *conn,
  84. int *status_code)
  85. {
  86. UNUSED_PARAMETER(server);
  87. UNUSED_PARAMETER(conn);
  88. if (status_code) {
  89. *status_code = -1;
  90. }
  91. return false;
  92. }
  93. bool
  94. CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn)
  95. {
  96. UNUSED_PARAMETER(server);
  97. UNUSED_PARAMETER(conn);
  98. return false;
  99. }
  100. bool
  101. CivetHandler::handlePatch(CivetServer *server,
  102. struct mg_connection *conn,
  103. int *status_code)
  104. {
  105. UNUSED_PARAMETER(server);
  106. UNUSED_PARAMETER(conn);
  107. if (status_code) {
  108. *status_code = -1;
  109. }
  110. return false;
  111. }
  112. bool
  113. CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
  114. {
  115. UNUSED_PARAMETER(server);
  116. UNUSED_PARAMETER(conn);
  117. return false;
  118. }
  119. bool
  120. CivetHandler::handleDelete(CivetServer *server,
  121. struct mg_connection *conn,
  122. int *status_code)
  123. {
  124. UNUSED_PARAMETER(server);
  125. UNUSED_PARAMETER(conn);
  126. if (status_code) {
  127. *status_code = -1;
  128. }
  129. return false;
  130. }
  131. bool
  132. CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn)
  133. {
  134. UNUSED_PARAMETER(server);
  135. UNUSED_PARAMETER(conn);
  136. return false;
  137. }
  138. bool
  139. CivetHandler::handleOptions(CivetServer *server,
  140. struct mg_connection *conn,
  141. int *status_code)
  142. {
  143. UNUSED_PARAMETER(server);
  144. UNUSED_PARAMETER(conn);
  145. if (status_code) {
  146. *status_code = -1;
  147. }
  148. return false;
  149. }
  150. bool
  151. CivetWebSocketHandler::handleConnection(CivetServer *server,
  152. const struct mg_connection *conn)
  153. {
  154. UNUSED_PARAMETER(server);
  155. UNUSED_PARAMETER(conn);
  156. return true;
  157. }
  158. void
  159. CivetWebSocketHandler::handleReadyState(CivetServer *server,
  160. struct mg_connection *conn)
  161. {
  162. UNUSED_PARAMETER(server);
  163. UNUSED_PARAMETER(conn);
  164. return;
  165. }
  166. bool
  167. CivetWebSocketHandler::handleData(CivetServer *server,
  168. struct mg_connection *conn,
  169. int bits,
  170. char *data,
  171. size_t data_len)
  172. {
  173. UNUSED_PARAMETER(server);
  174. UNUSED_PARAMETER(conn);
  175. UNUSED_PARAMETER(bits);
  176. UNUSED_PARAMETER(data);
  177. UNUSED_PARAMETER(data_len);
  178. return true;
  179. }
  180. void
  181. CivetWebSocketHandler::handleClose(CivetServer *server,
  182. const struct mg_connection *conn)
  183. {
  184. UNUSED_PARAMETER(server);
  185. UNUSED_PARAMETER(conn);
  186. return;
  187. }
  188. int
  189. CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
  190. {
  191. const struct mg_request_info *request_info = mg_get_request_info(conn);
  192. assert(request_info != NULL);
  193. CivetServer *me = (CivetServer *)(request_info->user_data);
  194. assert(me != NULL);
  195. int http_status_code = -1;
  196. bool status_ok = false;
  197. // Happens when a request hits the server before the context is saved
  198. if (me->context == NULL)
  199. return 0;
  200. mg_lock_context(me->context);
  201. me->connections[conn] = CivetConnection();
  202. mg_unlock_context(me->context);
  203. CivetHandler *handler = (CivetHandler *)cbdata;
  204. if (handler) {
  205. if (strcmp(request_info->request_method, "GET") == 0) {
  206. status_ok = handler->handleGet(me, conn, &http_status_code);
  207. if (http_status_code < 0) {
  208. status_ok = handler->handleGet(me, conn);
  209. }
  210. } else if (strcmp(request_info->request_method, "POST") == 0) {
  211. status_ok = handler->handlePost(me, conn, &http_status_code);
  212. if (http_status_code < 0) {
  213. status_ok = handler->handlePost(me, conn);
  214. }
  215. } else if (strcmp(request_info->request_method, "HEAD") == 0) {
  216. status_ok = handler->handleHead(me, conn, &http_status_code);
  217. if (http_status_code < 0) {
  218. status_ok = handler->handleHead(me, conn);
  219. }
  220. } else if (strcmp(request_info->request_method, "PUT") == 0) {
  221. status_ok = handler->handlePut(me, conn, &http_status_code);
  222. if (http_status_code < 0) {
  223. status_ok = handler->handlePut(me, conn);
  224. }
  225. } else if (strcmp(request_info->request_method, "DELETE") == 0) {
  226. status_ok = handler->handleDelete(me, conn, &http_status_code);
  227. if (http_status_code < 0) {
  228. status_ok = handler->handleDelete(me, conn);
  229. }
  230. } else if (strcmp(request_info->request_method, "OPTIONS") == 0) {
  231. status_ok = handler->handleOptions(me, conn, &http_status_code);
  232. if (http_status_code < 0) {
  233. status_ok = handler->handleOptions(me, conn);
  234. }
  235. } else if (strcmp(request_info->request_method, "PATCH") == 0) {
  236. status_ok = handler->handlePatch(me, conn, &http_status_code);
  237. if (http_status_code < 0) {
  238. status_ok = handler->handlePatch(me, conn);
  239. }
  240. }
  241. }
  242. if (http_status_code < 0) {
  243. http_status_code = status_ok ? 1 : 0;
  244. }
  245. return http_status_code;
  246. }
  247. int
  248. CivetServer::authHandler(struct mg_connection *conn, void *cbdata)
  249. {
  250. const struct mg_request_info *request_info = mg_get_request_info(conn);
  251. assert(request_info != NULL);
  252. CivetServer *me = (CivetServer *)(request_info->user_data);
  253. assert(me != NULL);
  254. // Happens when a request hits the server before the context is saved
  255. if (me->context == NULL)
  256. return 0;
  257. mg_lock_context(me->context);
  258. me->connections[conn] = CivetConnection();
  259. mg_unlock_context(me->context);
  260. CivetAuthHandler *handler = (CivetAuthHandler *)cbdata;
  261. if (handler) {
  262. return handler->authorize(me, conn) ? 1 : 0;
  263. }
  264. return 0; // No handler found
  265. }
  266. int
  267. CivetServer::webSocketConnectionHandler(const struct mg_connection *conn,
  268. void *cbdata)
  269. {
  270. const struct mg_request_info *request_info = mg_get_request_info(conn);
  271. assert(request_info != NULL);
  272. CivetServer *me = (CivetServer *)(request_info->user_data);
  273. assert(me != NULL);
  274. // Happens when a request hits the server before the context is saved
  275. if (me->context == NULL)
  276. return 0;
  277. CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata;
  278. if (handler) {
  279. return handler->handleConnection(me, conn) ? 0 : 1;
  280. }
  281. return 1; // No handler found, close connection
  282. }
  283. void
  284. CivetServer::webSocketReadyHandler(struct mg_connection *conn, void *cbdata)
  285. {
  286. const struct mg_request_info *request_info = mg_get_request_info(conn);
  287. assert(request_info != NULL);
  288. CivetServer *me = (CivetServer *)(request_info->user_data);
  289. assert(me != NULL);
  290. // Happens when a request hits the server before the context is saved
  291. if (me->context == NULL)
  292. return;
  293. CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata;
  294. if (handler) {
  295. handler->handleReadyState(me, conn);
  296. }
  297. }
  298. int
  299. CivetServer::webSocketDataHandler(struct mg_connection *conn,
  300. int bits,
  301. char *data,
  302. size_t data_len,
  303. void *cbdata)
  304. {
  305. const struct mg_request_info *request_info = mg_get_request_info(conn);
  306. assert(request_info != NULL);
  307. CivetServer *me = (CivetServer *)(request_info->user_data);
  308. assert(me != NULL);
  309. // Happens when a request hits the server before the context is saved
  310. if (me->context == NULL)
  311. return 0;
  312. CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata;
  313. if (handler) {
  314. return handler->handleData(me, conn, bits, data, data_len) ? 1 : 0;
  315. }
  316. return 1; // No handler found
  317. }
  318. void
  319. CivetServer::webSocketCloseHandler(const struct mg_connection *conn,
  320. void *cbdata)
  321. {
  322. const struct mg_request_info *request_info = mg_get_request_info(conn);
  323. assert(request_info != NULL);
  324. CivetServer *me = (CivetServer *)(request_info->user_data);
  325. assert(me != NULL);
  326. // Happens when a request hits the server before the context is saved
  327. if (me->context == NULL)
  328. return;
  329. CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata;
  330. if (handler) {
  331. handler->handleClose(me, conn);
  332. }
  333. }
  334. CivetCallbacks::CivetCallbacks()
  335. {
  336. memset(this, 0, sizeof(*this));
  337. }
  338. CivetServer::CivetServer(const char **options,
  339. const struct CivetCallbacks *_callbacks,
  340. const void *UserContextIn)
  341. : context(0)
  342. {
  343. struct CivetCallbacks callbacks;
  344. UserContext = UserContextIn;
  345. if (_callbacks) {
  346. callbacks = *_callbacks;
  347. userCloseHandler = _callbacks->connection_close;
  348. } else {
  349. userCloseHandler = NULL;
  350. }
  351. callbacks.connection_close = closeHandler;
  352. struct mg_init_data mg_start_init_data = {0};
  353. mg_start_init_data.callbacks = &callbacks;
  354. mg_start_init_data.user_data = this;
  355. mg_start_init_data.configuration_options = options;
  356. struct mg_error_data mg_start_error_data = {0};
  357. char errtxtbuf[256] = {0};
  358. mg_start_error_data.text = errtxtbuf;
  359. mg_start_error_data.text_buffer_size = sizeof(errtxtbuf);
  360. context = mg_start2(&mg_start_init_data, &mg_start_error_data);
  361. if (context == NULL) {
  362. std::string exceptionMsg =
  363. "null context when constructing CivetServer. "
  364. "Possible problem binding to port. Error: ";
  365. exceptionMsg += errtxtbuf;
  366. printf("CivetServer: %s\n", exceptionMsg.c_str());
  367. throw CivetException(exceptionMsg);
  368. }
  369. }
  370. CivetServer::CivetServer(const std::vector<std::string> &options,
  371. const struct CivetCallbacks *_callbacks,
  372. const void *UserContextIn)
  373. : context(0)
  374. {
  375. struct CivetCallbacks callbacks;
  376. UserContext = UserContextIn;
  377. if (_callbacks) {
  378. callbacks = *_callbacks;
  379. userCloseHandler = _callbacks->connection_close;
  380. } else {
  381. userCloseHandler = NULL;
  382. }
  383. callbacks.connection_close = closeHandler;
  384. std::vector<const char *> pointers(options.size() + 1);
  385. for (size_t i = 0; i < options.size(); i++) {
  386. pointers[i] = (options[i].c_str());
  387. }
  388. pointers.back() = NULL;
  389. struct mg_init_data mg_start_init_data = {0};
  390. mg_start_init_data.callbacks = &callbacks;
  391. mg_start_init_data.user_data = this;
  392. mg_start_init_data.configuration_options = &pointers[0];
  393. struct mg_error_data mg_start_error_data = {0};
  394. char errtxtbuf[256] = {0};
  395. mg_start_error_data.text = errtxtbuf;
  396. mg_start_error_data.text_buffer_size = sizeof(errtxtbuf);
  397. context = mg_start2(&mg_start_init_data, &mg_start_error_data);
  398. if (context == NULL) {
  399. std::string exceptionMsg =
  400. "null context when constructing CivetServer. "
  401. "Possible problem binding to port. Error: ";
  402. exceptionMsg += errtxtbuf;
  403. throw CivetException(exceptionMsg);
  404. }
  405. }
  406. CivetServer::~CivetServer()
  407. {
  408. close();
  409. }
  410. void
  411. CivetServer::closeHandler(const struct mg_connection *conn)
  412. {
  413. CivetServer *me = (CivetServer *)mg_get_user_data(mg_get_context(conn));
  414. assert(me != NULL);
  415. // Happens when a request hits the server before the context is saved
  416. if (me->context == NULL)
  417. return;
  418. if (me->userCloseHandler) {
  419. me->userCloseHandler(conn);
  420. }
  421. mg_lock_context(me->context);
  422. me->connections.erase(conn);
  423. mg_unlock_context(me->context);
  424. }
  425. void
  426. CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
  427. {
  428. mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
  429. }
  430. void
  431. CivetServer::addWebSocketHandler(const std::string &uri,
  432. CivetWebSocketHandler *handler)
  433. {
  434. mg_set_websocket_handler(context,
  435. uri.c_str(),
  436. webSocketConnectionHandler,
  437. webSocketReadyHandler,
  438. webSocketDataHandler,
  439. webSocketCloseHandler,
  440. handler);
  441. }
  442. void
  443. CivetServer::addAuthHandler(const std::string &uri, CivetAuthHandler *handler)
  444. {
  445. mg_set_auth_handler(context, uri.c_str(), authHandler, handler);
  446. }
  447. void
  448. CivetServer::removeHandler(const std::string &uri)
  449. {
  450. mg_set_request_handler(context, uri.c_str(), NULL, NULL);
  451. }
  452. void
  453. CivetServer::removeWebSocketHandler(const std::string &uri)
  454. {
  455. mg_set_websocket_handler(
  456. context, uri.c_str(), NULL, NULL, NULL, NULL, NULL);
  457. }
  458. void
  459. CivetServer::removeAuthHandler(const std::string &uri)
  460. {
  461. mg_set_auth_handler(context, uri.c_str(), NULL, NULL);
  462. }
  463. void
  464. CivetServer::close()
  465. {
  466. if (context) {
  467. mg_stop(context);
  468. context = 0;
  469. }
  470. }
  471. int
  472. CivetServer::getCookie(struct mg_connection *conn,
  473. const std::string &cookieName,
  474. std::string &cookieValue)
  475. {
  476. // Maximum cookie length as per microsoft is 4096.
  477. // http://msdn.microsoft.com/en-us/library/ms178194.aspx
  478. char _cookieValue[4096];
  479. const char *cookie = mg_get_header(conn, "Cookie");
  480. int lRead = mg_get_cookie(cookie,
  481. cookieName.c_str(),
  482. _cookieValue,
  483. sizeof(_cookieValue));
  484. cookieValue.clear();
  485. cookieValue.append(_cookieValue);
  486. return lRead;
  487. }
  488. const char *
  489. CivetServer::getHeader(struct mg_connection *conn,
  490. const std::string &headerName)
  491. {
  492. return mg_get_header(conn, headerName.c_str());
  493. }
  494. const char *
  495. CivetServer::getMethod(struct mg_connection *conn)
  496. {
  497. const struct mg_request_info *request_info = mg_get_request_info(conn);
  498. assert(request_info != NULL);
  499. return request_info->request_method;
  500. }
  501. void
  502. CivetServer::urlDecode(const char *src,
  503. std::string &dst,
  504. bool is_form_url_encoded)
  505. {
  506. urlDecode(src, strlen(src), dst, is_form_url_encoded);
  507. }
  508. void
  509. CivetServer::urlDecode(const char *src,
  510. size_t src_len,
  511. std::string &dst,
  512. bool is_form_url_encoded)
  513. {
  514. // assign enough buffer
  515. std::vector<char> buf(src_len + 1);
  516. int r = mg_url_decode(src,
  517. static_cast<int>(src_len),
  518. &buf[0],
  519. static_cast<int>(buf.size()),
  520. is_form_url_encoded);
  521. if (r < 0) {
  522. // never reach here
  523. throw std::out_of_range("");
  524. }
  525. // dst can contain NUL characters
  526. dst.assign(buf.begin(), buf.begin() + r);
  527. }
  528. bool
  529. CivetServer::getParam(struct mg_connection *conn,
  530. const char *name,
  531. std::string &dst,
  532. size_t occurrence)
  533. {
  534. const char *formParams = NULL;
  535. const char *queryString = NULL;
  536. const struct mg_request_info *ri = mg_get_request_info(conn);
  537. assert(ri != NULL);
  538. CivetServer *me = (CivetServer *)(ri->user_data);
  539. assert(me != NULL);
  540. mg_lock_context(me->context);
  541. CivetConnection &conobj = me->connections[conn];
  542. mg_unlock_context(me->context);
  543. mg_lock_connection(conn);
  544. if (conobj.postData.empty()) {
  545. // check if there is a request body
  546. for (;;) {
  547. char buf[2048];
  548. int r = mg_read(conn, buf, sizeof(buf));
  549. try {
  550. if (r == 0) {
  551. conobj.postData.push_back('\0');
  552. break;
  553. } else if ((r < 0)
  554. || ((conobj.postData.size() + r)
  555. > MAX_PARAM_BODY_LENGTH)) {
  556. conobj.postData.assign(1, '\0');
  557. break;
  558. }
  559. conobj.postData.insert(conobj.postData.end(), buf, buf + r);
  560. } catch (...) {
  561. conobj.postData.clear();
  562. break;
  563. }
  564. }
  565. }
  566. if (!conobj.postData.empty()) {
  567. // check if form parameter are already stored
  568. formParams = &conobj.postData[0];
  569. }
  570. if (ri->query_string != NULL) {
  571. // get requests do store html <form> field values in the http
  572. // query_string
  573. queryString = ri->query_string;
  574. }
  575. mg_unlock_connection(conn);
  576. bool get_param_success = false;
  577. if (formParams != NULL) {
  578. get_param_success =
  579. getParam(formParams, strlen(formParams), name, dst, occurrence);
  580. }
  581. if (!get_param_success && queryString != NULL) {
  582. get_param_success =
  583. getParam(queryString, strlen(queryString), name, dst, occurrence);
  584. }
  585. return get_param_success;
  586. }
  587. bool
  588. CivetServer::getParam(const char *data,
  589. size_t data_len,
  590. const char *name,
  591. std::string &dst,
  592. size_t occurrence)
  593. {
  594. char buf[256];
  595. int r = mg_get_var2(data, data_len, name, buf, sizeof(buf), occurrence);
  596. if (r >= 0) {
  597. // dst can contain NUL characters
  598. dst.assign(buf, r);
  599. return true;
  600. } else if (r == -2) {
  601. // more buffer
  602. std::vector<char> vbuf(sizeof(buf) * 2);
  603. for (;;) {
  604. r = mg_get_var2(
  605. data, data_len, name, &vbuf[0], vbuf.size(), occurrence);
  606. if (r >= 0) {
  607. dst.assign(vbuf.begin(), vbuf.begin() + r);
  608. return true;
  609. } else if (r != -2) {
  610. break;
  611. }
  612. // more buffer
  613. vbuf.resize(vbuf.size() * 2);
  614. }
  615. }
  616. dst.clear();
  617. return false;
  618. }
  619. std::string
  620. CivetServer::getPostData(struct mg_connection *conn)
  621. {
  622. mg_lock_connection(conn);
  623. std::string postdata;
  624. char buf[2048];
  625. int r = mg_read(conn, buf, sizeof(buf));
  626. while (r > 0) {
  627. postdata.append(buf, r);
  628. r = mg_read(conn, buf, sizeof(buf));
  629. }
  630. mg_unlock_connection(conn);
  631. return postdata;
  632. }
  633. void
  634. CivetServer::urlEncode(const char *src, std::string &dst, bool append)
  635. {
  636. urlEncode(src, strlen(src), dst, append);
  637. }
  638. void
  639. CivetServer::urlEncode(const char *src,
  640. size_t src_len,
  641. std::string &dst,
  642. bool append)
  643. {
  644. if (!append)
  645. dst.clear();
  646. for (; src_len > 0; src++, src_len--) {
  647. if (*src == '\0') {
  648. // src and dst can contain NUL characters without encoding
  649. dst.push_back(*src);
  650. } else {
  651. char buf[2] = {*src, '\0'};
  652. char dst_buf[4];
  653. if (mg_url_encode(buf, dst_buf, sizeof(dst_buf)) < 0) {
  654. // never reach here
  655. throw std::out_of_range("");
  656. }
  657. dst.append(dst_buf);
  658. }
  659. }
  660. }
  661. std::vector<int>
  662. CivetServer::getListeningPorts()
  663. {
  664. std::vector<struct mg_server_port> server_ports = getListeningPortsFull();
  665. std::vector<int> ports(server_ports.size());
  666. for (size_t i = 0; i < server_ports.size(); i++) {
  667. ports[i] = server_ports[i].port;
  668. }
  669. return ports;
  670. }
  671. std::vector<struct mg_server_port>
  672. CivetServer::getListeningPortsFull()
  673. {
  674. std::vector<struct mg_server_port> server_ports(8);
  675. for (;;) {
  676. int size = mg_get_server_ports(context,
  677. static_cast<int>(server_ports.size()),
  678. &server_ports[0]);
  679. if (size < static_cast<int>(server_ports.size())) {
  680. server_ports.resize(size < 0 ? 0 : size);
  681. break;
  682. }
  683. server_ports.resize(server_ports.size() * 2);
  684. }
  685. return server_ports;
  686. }