CivetServer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 = {};
  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 = {};
  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. throw CivetException(exceptionMsg);
  367. }
  368. }
  369. CivetServer::CivetServer(const std::vector<std::string> &options,
  370. const struct CivetCallbacks *_callbacks,
  371. const void *UserContextIn)
  372. : context(0)
  373. {
  374. struct CivetCallbacks callbacks;
  375. UserContext = UserContextIn;
  376. if (_callbacks) {
  377. callbacks = *_callbacks;
  378. userCloseHandler = _callbacks->connection_close;
  379. } else {
  380. userCloseHandler = NULL;
  381. }
  382. callbacks.connection_close = closeHandler;
  383. std::vector<const char *> pointers(options.size() + 1);
  384. for (size_t i = 0; i < options.size(); i++) {
  385. pointers[i] = (options[i].c_str());
  386. }
  387. pointers.back() = NULL;
  388. struct mg_init_data mg_start_init_data = {};
  389. mg_start_init_data.callbacks = &callbacks;
  390. mg_start_init_data.user_data = this;
  391. mg_start_init_data.configuration_options = &pointers[0];
  392. struct mg_error_data mg_start_error_data = {};
  393. char errtxtbuf[256] = {0};
  394. mg_start_error_data.text = errtxtbuf;
  395. mg_start_error_data.text_buffer_size = sizeof(errtxtbuf);
  396. context = mg_start2(&mg_start_init_data, &mg_start_error_data);
  397. if (context == NULL) {
  398. std::string exceptionMsg =
  399. "null context when constructing CivetServer. "
  400. "Possible problem binding to port. Error: ";
  401. exceptionMsg += errtxtbuf;
  402. throw CivetException(exceptionMsg);
  403. }
  404. }
  405. CivetServer::~CivetServer()
  406. {
  407. close();
  408. }
  409. void
  410. CivetServer::closeHandler(const struct mg_connection *conn)
  411. {
  412. CivetServer *me = (CivetServer *)mg_get_user_data(mg_get_context(conn));
  413. assert(me != NULL);
  414. // Happens when a request hits the server before the context is saved
  415. if (me->context == NULL)
  416. return;
  417. if (me->userCloseHandler) {
  418. me->userCloseHandler(conn);
  419. }
  420. mg_lock_context(me->context);
  421. me->connections.erase(conn);
  422. mg_unlock_context(me->context);
  423. }
  424. void
  425. CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
  426. {
  427. mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
  428. }
  429. void
  430. CivetServer::addWebSocketHandler(const std::string &uri,
  431. CivetWebSocketHandler *handler)
  432. {
  433. mg_set_websocket_handler(context,
  434. uri.c_str(),
  435. webSocketConnectionHandler,
  436. webSocketReadyHandler,
  437. webSocketDataHandler,
  438. webSocketCloseHandler,
  439. handler);
  440. }
  441. void
  442. CivetServer::addAuthHandler(const std::string &uri, CivetAuthHandler *handler)
  443. {
  444. mg_set_auth_handler(context, uri.c_str(), authHandler, handler);
  445. }
  446. void
  447. CivetServer::removeHandler(const std::string &uri)
  448. {
  449. mg_set_request_handler(context, uri.c_str(), NULL, NULL);
  450. }
  451. void
  452. CivetServer::removeWebSocketHandler(const std::string &uri)
  453. {
  454. mg_set_websocket_handler(
  455. context, uri.c_str(), NULL, NULL, NULL, NULL, NULL);
  456. }
  457. void
  458. CivetServer::removeAuthHandler(const std::string &uri)
  459. {
  460. mg_set_auth_handler(context, uri.c_str(), NULL, NULL);
  461. }
  462. void
  463. CivetServer::close()
  464. {
  465. if (context) {
  466. mg_stop(context);
  467. context = 0;
  468. }
  469. }
  470. int
  471. CivetServer::getCookie(struct mg_connection *conn,
  472. const std::string &cookieName,
  473. std::string &cookieValue)
  474. {
  475. // Maximum cookie length as per microsoft is 4096.
  476. // http://msdn.microsoft.com/en-us/library/ms178194.aspx
  477. char _cookieValue[4096];
  478. const char *cookie = mg_get_header(conn, "Cookie");
  479. int lRead = mg_get_cookie(cookie,
  480. cookieName.c_str(),
  481. _cookieValue,
  482. sizeof(_cookieValue));
  483. cookieValue.clear();
  484. if (lRead >= 0) {
  485. cookieValue.append(_cookieValue);
  486. }
  487. return lRead;
  488. }
  489. const char *
  490. CivetServer::getHeader(struct mg_connection *conn,
  491. const std::string &headerName)
  492. {
  493. return mg_get_header(conn, headerName.c_str());
  494. }
  495. const char *
  496. CivetServer::getMethod(struct mg_connection *conn)
  497. {
  498. const struct mg_request_info *request_info = mg_get_request_info(conn);
  499. assert(request_info != NULL);
  500. return request_info->request_method;
  501. }
  502. void
  503. CivetServer::urlDecode(const char *src,
  504. std::string &dst,
  505. bool is_form_url_encoded)
  506. {
  507. urlDecode(src, strlen(src), dst, is_form_url_encoded);
  508. }
  509. void
  510. CivetServer::urlDecode(const char *src,
  511. size_t src_len,
  512. std::string &dst,
  513. bool is_form_url_encoded)
  514. {
  515. // assign enough buffer
  516. std::vector<char> buf(src_len + 1);
  517. int r = mg_url_decode(src,
  518. static_cast<int>(src_len),
  519. &buf[0],
  520. static_cast<int>(buf.size()),
  521. is_form_url_encoded);
  522. if (r < 0) {
  523. // never reach here
  524. throw std::out_of_range("");
  525. }
  526. // dst can contain NUL characters
  527. dst.assign(buf.begin(), buf.begin() + r);
  528. }
  529. bool
  530. CivetServer::getParam(struct mg_connection *conn,
  531. const char *name,
  532. std::string &dst,
  533. size_t occurrence)
  534. {
  535. const char *formParams = NULL;
  536. const char *queryString = NULL;
  537. const struct mg_request_info *ri = mg_get_request_info(conn);
  538. assert(ri != NULL);
  539. CivetServer *me = (CivetServer *)(ri->user_data);
  540. assert(me != NULL);
  541. mg_lock_context(me->context);
  542. CivetConnection &conobj = me->connections[conn];
  543. mg_unlock_context(me->context);
  544. mg_lock_connection(conn);
  545. if (conobj.postData.empty()) {
  546. // check if there is a request body
  547. for (;;) {
  548. char buf[2048];
  549. int r = mg_read(conn, buf, sizeof(buf));
  550. try {
  551. if (r == 0) {
  552. conobj.postData.push_back('\0');
  553. break;
  554. } else if ((r < 0)
  555. || ((conobj.postData.size() + r)
  556. > MAX_PARAM_BODY_LENGTH)) {
  557. conobj.postData.assign(1, '\0');
  558. break;
  559. }
  560. conobj.postData.insert(conobj.postData.end(), buf, buf + r);
  561. } catch (...) {
  562. conobj.postData.clear();
  563. break;
  564. }
  565. }
  566. }
  567. if (!conobj.postData.empty()) {
  568. // check if form parameter are already stored
  569. formParams = &conobj.postData[0];
  570. }
  571. if (ri->query_string != NULL) {
  572. // get requests do store html <form> field values in the http
  573. // query_string
  574. queryString = ri->query_string;
  575. }
  576. mg_unlock_connection(conn);
  577. bool get_param_success = false;
  578. if (formParams != NULL) {
  579. get_param_success =
  580. getParam(formParams, strlen(formParams), name, dst, occurrence);
  581. }
  582. if (!get_param_success && queryString != NULL) {
  583. get_param_success =
  584. getParam(queryString, strlen(queryString), name, dst, occurrence);
  585. }
  586. return get_param_success;
  587. }
  588. bool
  589. CivetServer::getParam(const char *data,
  590. size_t data_len,
  591. const char *name,
  592. std::string &dst,
  593. size_t occurrence)
  594. {
  595. char buf[256];
  596. int r = mg_get_var2(data, data_len, name, buf, sizeof(buf), occurrence);
  597. if (r >= 0) {
  598. // dst can contain NUL characters
  599. dst.assign(buf, r);
  600. return true;
  601. } else if (r == -2) {
  602. // more buffer
  603. std::vector<char> vbuf(sizeof(buf) * 2);
  604. for (;;) {
  605. r = mg_get_var2(
  606. data, data_len, name, &vbuf[0], vbuf.size(), occurrence);
  607. if (r >= 0) {
  608. dst.assign(vbuf.begin(), vbuf.begin() + r);
  609. return true;
  610. } else if (r != -2) {
  611. break;
  612. }
  613. // more buffer
  614. vbuf.resize(vbuf.size() * 2);
  615. }
  616. }
  617. dst.clear();
  618. return false;
  619. }
  620. std::string
  621. CivetServer::getPostData(struct mg_connection *conn)
  622. {
  623. mg_lock_connection(conn);
  624. std::string postdata;
  625. char buf[2048];
  626. int r = mg_read(conn, buf, sizeof(buf));
  627. while (r > 0) {
  628. postdata.append(buf, r);
  629. r = mg_read(conn, buf, sizeof(buf));
  630. }
  631. mg_unlock_connection(conn);
  632. return postdata;
  633. }
  634. void
  635. CivetServer::urlEncode(const char *src, std::string &dst, bool append)
  636. {
  637. urlEncode(src, strlen(src), dst, append);
  638. }
  639. void
  640. CivetServer::urlEncode(const char *src,
  641. size_t src_len,
  642. std::string &dst,
  643. bool append)
  644. {
  645. if (!append)
  646. dst.clear();
  647. for (; src_len > 0; src++, src_len--) {
  648. if (*src == '\0') {
  649. // src and dst can contain NUL characters without encoding
  650. dst.push_back(*src);
  651. } else {
  652. char buf[2] = {*src, '\0'};
  653. char dst_buf[4];
  654. if (mg_url_encode(buf, dst_buf, sizeof(dst_buf)) < 0) {
  655. // never reach here
  656. throw std::out_of_range("");
  657. }
  658. dst.append(dst_buf);
  659. }
  660. }
  661. }
  662. std::vector<int>
  663. CivetServer::getListeningPorts()
  664. {
  665. std::vector<struct mg_server_port> server_ports = getListeningPortsFull();
  666. std::vector<int> ports(server_ports.size());
  667. for (size_t i = 0; i < server_ports.size(); i++) {
  668. ports[i] = server_ports[i].port;
  669. }
  670. return ports;
  671. }
  672. std::vector<struct mg_server_port>
  673. CivetServer::getListeningPortsFull()
  674. {
  675. std::vector<struct mg_server_port> server_ports(8);
  676. for (;;) {
  677. int size = mg_get_server_ports(context,
  678. static_cast<int>(server_ports.size()),
  679. &server_ports[0]);
  680. if (size < static_cast<int>(server_ports.size())) {
  681. server_ports.resize(size < 0 ? 0 : size);
  682. break;
  683. }
  684. server_ports.resize(server_ports.size() * 2);
  685. }
  686. return server_ports;
  687. }