public_server.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* Copyright (c) 2015 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #ifdef _MSC_VER
  22. #define _CRT_SECURE_NO_WARNINGS
  23. #endif
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <time.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include "public_server.h"
  30. #include <civetweb.h>
  31. #if defined(_WIN32)
  32. #include <Windows.h>
  33. #define mg_Sleep(x) (Sleep(x * 1000))
  34. #else
  35. #include <unistd.h>
  36. #define mg_Sleep(x) (sleep(x))
  37. #endif
  38. /* This unit test file uses the excellent Check unit testing library.
  39. * The API documentation is available here:
  40. * http://check.sourceforge.net/doc/check_html/index.html
  41. */
  42. START_TEST(test_the_test_environment)
  43. {
  44. char wd[300];
  45. char buf[500];
  46. FILE *f;
  47. struct stat st;
  48. int ret;
  49. memset(wd, 0, sizeof(wd));
  50. memset(buf, 0, sizeof(buf));
  51. /* Get the current working directory */
  52. #ifdef _WIN32
  53. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  54. wd[sizeof(wd) - 1] = 0;
  55. #else
  56. (void)getcwd(wd, sizeof(wd));
  57. wd[sizeof(wd) - 1] = 0;
  58. #endif
  59. /* Check the pem file */
  60. #ifdef _WIN32
  61. strcpy(buf, wd);
  62. strcat(buf, "..\\..\\resources\\ssl_cert.pem");
  63. f = fopen(buf, "rb");
  64. #else
  65. strcpy(buf, wd);
  66. strcat(buf, "../../resources/ssl_cert.pem");
  67. f = fopen(buf, "r");
  68. #endif
  69. if (f) {
  70. fclose(f);
  71. } else {
  72. ck_abort_msg("%s not found", buf);
  73. }
  74. /* Check the test dir */
  75. #ifdef _WIN32
  76. strcpy(buf, wd);
  77. strcat(buf, "\\test");
  78. #else
  79. strcpy(buf, wd);
  80. strcat(buf, "/test");
  81. #endif
  82. memset(&st, 0, sizeof(st));
  83. ret = stat(buf, &st);
  84. if (!ret) {
  85. fclose(f);
  86. } else {
  87. ck_abort_msg("%s not found", buf);
  88. }
  89. }
  90. END_TEST
  91. static int log_msg_func(const struct mg_connection *conn, const char *message)
  92. {
  93. struct mg_context *ctx;
  94. char *ud;
  95. ck_assert(conn != NULL);
  96. ctx = mg_get_context(conn);
  97. ck_assert(ctx != NULL);
  98. ud = (char *)mg_get_user_data(ctx);
  99. strncpy(ud, message, 255);
  100. ud[255] = 0;
  101. return 1;
  102. }
  103. START_TEST(test_mg_start_stop_http_server)
  104. {
  105. struct mg_context *ctx;
  106. const char *OPTIONS[] = {
  107. "document_root", ".", "listening_ports", "8080", NULL,
  108. };
  109. size_t ports_cnt;
  110. int ports[16];
  111. int ssl[16];
  112. struct mg_callbacks callbacks;
  113. char errmsg[256];
  114. memset(ports, 0, sizeof(ports));
  115. memset(ssl, 0, sizeof(ssl));
  116. memset(&callbacks, 0, sizeof(callbacks));
  117. memset(errmsg, 0, sizeof(errmsg));
  118. callbacks.log_message = log_msg_func;
  119. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  120. mg_Sleep(1);
  121. ck_assert_str_eq(errmsg, "");
  122. ck_assert(ctx != NULL);
  123. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  124. ck_assert_uint_eq(ports_cnt, 1);
  125. ck_assert_int_eq(ports[0], 8080);
  126. ck_assert_int_eq(ssl[0], 0);
  127. ck_assert_int_eq(ports[1], 0);
  128. ck_assert_int_eq(ssl[1], 0);
  129. mg_Sleep(1);
  130. mg_stop(ctx);
  131. }
  132. END_TEST
  133. START_TEST(test_mg_start_stop_https_server)
  134. {
  135. struct mg_context *ctx;
  136. const char *OPTIONS[] = {
  137. "document_root",
  138. ".",
  139. "listening_ports",
  140. "8080,8443s",
  141. "ssl_certificate",
  142. "../../resources/ssl_cert.pem", // TODO: fix path in CI test environment
  143. NULL,
  144. };
  145. size_t ports_cnt;
  146. int ports[16];
  147. int ssl[16];
  148. struct mg_callbacks callbacks;
  149. char errmsg[256];
  150. memset(ports, 0, sizeof(ports));
  151. memset(ssl, 0, sizeof(ssl));
  152. memset(&callbacks, 0, sizeof(callbacks));
  153. memset(errmsg, 0, sizeof(errmsg));
  154. callbacks.log_message = log_msg_func;
  155. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  156. mg_Sleep(1);
  157. ck_assert_str_eq(errmsg, "");
  158. ck_assert(ctx != NULL);
  159. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  160. ck_assert_uint_eq(ports_cnt, 2);
  161. ck_assert_int_eq(ports[0], 8080);
  162. ck_assert_int_eq(ssl[0], 0);
  163. ck_assert_int_eq(ports[1], 8443);
  164. ck_assert_int_eq(ssl[1], 1);
  165. ck_assert_int_eq(ports[2], 0);
  166. ck_assert_int_eq(ssl[2], 0);
  167. mg_Sleep(1);
  168. mg_stop(ctx);
  169. }
  170. END_TEST
  171. static struct mg_context *g_ctx;
  172. static int request_test_handler(struct mg_connection *conn, void *cbdata)
  173. {
  174. int i;
  175. char chunk_data[32];
  176. const struct mg_request_info *ri;
  177. struct mg_context *ctx;
  178. void *ud, *cud;
  179. ctx = mg_get_context(conn);
  180. ud = mg_get_user_data(ctx);
  181. ri = mg_get_request_info(conn);
  182. ck_assert(ri != NULL);
  183. ck_assert(ctx == g_ctx);
  184. ck_assert(ud == &g_ctx);
  185. mg_set_user_connection_data(conn, (void *)6543);
  186. cud = mg_get_user_connection_data(conn);
  187. ck_assert(cud == (void *)6543);
  188. ck_assert(cbdata == (void *)7);
  189. strcpy(chunk_data, "123456789A123456789B123456789C");
  190. mg_printf(conn,
  191. "HTTP/1.1 200 OK\r\n"
  192. "Transfer-Encoding: chunked\r\n"
  193. "Content-Type: text/plain\r\n\r\n");
  194. for (i = 1; i <= 10; i++) {
  195. mg_printf(conn, "%x\r\n", i);
  196. mg_write(conn, chunk_data, (unsigned)i);
  197. mg_printf(conn, "\r\n");
  198. }
  199. mg_printf(conn, "0\r\n\r\n");
  200. return 1;
  201. }
  202. START_TEST(test_request_handlers)
  203. {
  204. char ebuf[100];
  205. struct mg_context *ctx;
  206. struct mg_connection *conn;
  207. const struct mg_request_info *ri;
  208. char uri[64];
  209. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  210. const char *expected =
  211. "112123123412345123456123456712345678123456789123456789A";
  212. int i;
  213. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  214. #if defined(USE_IPV6) && defined(NO_SSL)
  215. const char *HTTP_PORT = "8084,[::]:8086";
  216. short ipv4_port = 8084;
  217. short ipv6_port = 8086;
  218. #elif !defined(USE_IPV6) && defined(NO_SSL)
  219. const char *HTTP_PORT = "8084";
  220. short ipv4_port = 8084;
  221. #elif defined(USE_IPV6) && !defined(NO_SSL)
  222. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  223. short ipv4_port = 8084;
  224. short ipv4s_port = 8094;
  225. short ipv4r_port = 8194;
  226. short ipv6_port = 8086;
  227. short ipv6s_port = 8096;
  228. short ipv6r_port = 8196;
  229. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  230. const char *HTTP_PORT = "8084,8194r,8094s";
  231. short ipv4_port = 8084;
  232. short ipv4s_port = 8094;
  233. short ipv4r_port = 8194;
  234. #endif
  235. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  236. const char *opt;
  237. FILE *f;
  238. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  239. OPTIONS[0] = "listening_ports";
  240. OPTIONS[1] = HTTP_PORT;
  241. OPTIONS[2] = "document_root";
  242. OPTIONS[3] = ".";
  243. #ifndef NO_SSL
  244. OPTIONS[4] = "ssl_certificate";
  245. OPTIONS[5] = "../../resources/ssl_cert.pem";
  246. #endif
  247. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  248. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  249. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  250. ck_assert(ctx != NULL);
  251. g_ctx = ctx;
  252. opt = mg_get_option(ctx, "listening_ports");
  253. ck_assert_str_eq(opt, HTTP_PORT);
  254. opt = mg_get_option(ctx, "cgi_environment");
  255. ck_assert_str_eq(opt, "");
  256. opt = mg_get_option(ctx, "unknown_option_name");
  257. ck_assert(opt == NULL);
  258. for (i = 0; i < 1000; i++) {
  259. sprintf(uri, "/U%u", i);
  260. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  261. }
  262. for (i = 500; i < 800; i++) {
  263. sprintf(uri, "/U%u", i);
  264. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  265. }
  266. for (i = 600; i >= 0; i--) {
  267. sprintf(uri, "/U%u", i);
  268. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  269. }
  270. for (i = 750; i <= 1000; i++) {
  271. sprintf(uri, "/U%u", i);
  272. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  273. }
  274. for (i = 5; i < 9; i++) {
  275. sprintf(uri, "/U%u", i);
  276. mg_set_request_handler(
  277. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  278. }
  279. /* Try to load non existing file */
  280. conn = mg_download("localhost",
  281. ipv4_port,
  282. 0,
  283. ebuf,
  284. sizeof(ebuf),
  285. "%s",
  286. "GET /file/not/found HTTP/1.0\r\n\r\n");
  287. ck_assert(conn != NULL);
  288. ri = mg_get_request_info(conn);
  289. ck_assert(ri != NULL);
  290. ck_assert_str_eq(ri->uri, "404");
  291. mg_close_connection(conn);
  292. /* Get data from callback */
  293. conn = mg_download(
  294. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  295. ck_assert(conn != NULL);
  296. ri = mg_get_request_info(conn);
  297. ck_assert(ri != NULL);
  298. ck_assert_str_eq(ri->uri, "200");
  299. i = mg_read(conn, buf, sizeof(buf));
  300. ck_assert_int_eq(i, (int)strlen(expected));
  301. buf[i] = 0;
  302. ck_assert_str_eq(buf, expected);
  303. mg_close_connection(conn);
  304. /* Get data from callback using http://127.0.0.1 */
  305. conn = mg_download(
  306. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  307. ck_assert(conn != NULL);
  308. ri = mg_get_request_info(conn);
  309. ck_assert(ri != NULL);
  310. ck_assert_str_eq(ri->uri, "200");
  311. i = mg_read(conn, buf, sizeof(buf));
  312. ck_assert_int_eq(i, (int)strlen(expected));
  313. buf[i] = 0;
  314. ck_assert_str_eq(buf, expected);
  315. mg_close_connection(conn);
  316. #if defined(USE_IPV6)
  317. /* Get data from callback using http://[::1] */
  318. conn =
  319. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  320. ck_assert(conn != NULL);
  321. ri = mg_get_request_info(conn);
  322. ck_assert(ri != NULL);
  323. ck_assert_str_eq(ri->uri, "200");
  324. i = mg_read(conn, buf, sizeof(buf));
  325. ck_assert_int_eq(i, (int)strlen(expected));
  326. buf[i] = 0;
  327. ck_assert_str_eq(buf, expected);
  328. mg_close_connection(conn);
  329. #endif
  330. #if !defined(NO_SSL)
  331. /* Get data from callback using https://127.0.0.1 */
  332. conn = mg_download(
  333. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  334. ck_assert(conn != NULL);
  335. ri = mg_get_request_info(conn);
  336. ck_assert(ri != NULL);
  337. ck_assert_str_eq(ri->uri, "200");
  338. i = mg_read(conn, buf, sizeof(buf));
  339. ck_assert_int_eq(i, (int)strlen(expected));
  340. buf[i] = 0;
  341. ck_assert_str_eq(buf, expected);
  342. mg_close_connection(conn);
  343. /* Get redirect from callback using http://127.0.0.1 */
  344. conn = mg_download(
  345. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  346. ck_assert(conn != NULL);
  347. ri = mg_get_request_info(conn);
  348. ck_assert(ri != NULL);
  349. ck_assert_str_eq(ri->uri, "302");
  350. i = mg_read(conn, buf, sizeof(buf));
  351. ck_assert_int_eq(i, -1);
  352. mg_close_connection(conn);
  353. #endif
  354. #if defined(USE_IPV6) && !defined(NO_SSL)
  355. /* Get data from callback using https://[::1] */
  356. conn =
  357. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  358. ck_assert(conn != NULL);
  359. ri = mg_get_request_info(conn);
  360. ck_assert(ri != NULL);
  361. ck_assert_str_eq(ri->uri, "200");
  362. i = mg_read(conn, buf, sizeof(buf));
  363. ck_assert_int_eq(i, (int)strlen(expected));
  364. buf[i] = 0;
  365. ck_assert_str_eq(buf, expected);
  366. mg_close_connection(conn);
  367. /* Get redirect from callback using http://127.0.0.1 */
  368. conn =
  369. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  370. ck_assert(conn != NULL);
  371. ri = mg_get_request_info(conn);
  372. ck_assert(ri != NULL);
  373. ck_assert_str_eq(ri->uri, "302");
  374. i = mg_read(conn, buf, sizeof(buf));
  375. ck_assert_int_eq(i, -1);
  376. mg_close_connection(conn);
  377. #endif
  378. /* It seems to be impossible to find out what the actual working
  379. * directory of the CI test environment is. Before breaking another
  380. * dozen of builds by trying blindly with different paths, just
  381. * create the file here */
  382. #ifdef _WIN32
  383. f = fopen("test.txt", "wb");
  384. #else
  385. f = fopen("test.txt", "w");
  386. #endif
  387. fwrite("simple text file\n", 17, 1, f);
  388. fclose(f);
  389. /* Get static data */
  390. conn = mg_download("localhost",
  391. ipv4_port,
  392. 0,
  393. ebuf,
  394. sizeof(ebuf),
  395. "%s",
  396. "GET /test.txt HTTP/1.0\r\n\r\n");
  397. ck_assert(conn != NULL);
  398. ri = mg_get_request_info(conn);
  399. ck_assert(ri != NULL);
  400. ck_assert_str_eq(ri->uri, "200");
  401. i = mg_read(conn, buf, sizeof(buf));
  402. ck_assert_int_eq(i, 17);
  403. if ((i >= 0) && (i < (int)sizeof(buf))) {
  404. buf[i] = 0;
  405. }
  406. ck_assert_str_eq(buf, "simple text file\n");
  407. mg_close_connection(conn);
  408. /* Get directory listing */
  409. conn = mg_download("localhost",
  410. ipv4_port,
  411. 0,
  412. ebuf,
  413. sizeof(ebuf),
  414. "%s",
  415. "GET / HTTP/1.0\r\n\r\n");
  416. ck_assert(conn != NULL);
  417. ri = mg_get_request_info(conn);
  418. ck_assert(ri != NULL);
  419. ck_assert_str_eq(ri->uri, "200");
  420. i = mg_read(conn, buf, sizeof(buf));
  421. ck_assert(i > 6);
  422. buf[6] = 0;
  423. ck_assert_str_eq(buf, "<html>");
  424. mg_close_connection(conn);
  425. /* POST to static file (will not work) */
  426. conn = mg_download("localhost",
  427. ipv4_port,
  428. 0,
  429. ebuf,
  430. sizeof(ebuf),
  431. "%s",
  432. "POST /test.txt HTTP/1.0\r\n\r\n");
  433. ck_assert(conn != NULL);
  434. ri = mg_get_request_info(conn);
  435. ck_assert(ri != NULL);
  436. ck_assert_str_eq(ri->uri, "405");
  437. i = mg_read(conn, buf, sizeof(buf));
  438. ck_assert(i >= 29);
  439. buf[29] = 0;
  440. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  441. mg_close_connection(conn);
  442. /* PUT to static file (will not work) */
  443. conn = mg_download("localhost",
  444. ipv4_port,
  445. 0,
  446. ebuf,
  447. sizeof(ebuf),
  448. "%s",
  449. "PUT /test.txt HTTP/1.0\r\n\r\n");
  450. ck_assert(conn != NULL);
  451. ri = mg_get_request_info(conn);
  452. ck_assert(ri != NULL);
  453. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  454. mg_close_connection(conn);
  455. /* TODO: Test websockets */
  456. /* Close the server */
  457. g_ctx = NULL;
  458. mg_stop(ctx);
  459. mg_Sleep(1);
  460. }
  461. END_TEST
  462. Suite *make_public_server_suite(void)
  463. {
  464. Suite *const suite = suite_create("PublicServer");
  465. TCase *const checktestenv = tcase_create("Check test environment");
  466. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  467. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  468. TCase *const serverrequests = tcase_create("Server Requests");
  469. tcase_add_test(checktestenv, test_the_test_environment);
  470. suite_add_tcase(suite, checktestenv);
  471. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  472. suite_add_tcase(suite, startstophttp);
  473. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  474. suite_add_tcase(suite, startstophttps);
  475. tcase_add_test(serverrequests, test_request_handlers);
  476. suite_add_tcase(suite, serverrequests);
  477. return suite;
  478. }
  479. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  480. /* Used to debug test cases without using the check framework */
  481. static int chk_ok = 0;
  482. static int chk_failed = 0;
  483. void main(void)
  484. {
  485. test_the_test_environment(0);
  486. test_mg_start_stop_http_server(0);
  487. test_mg_start_stop_https_server(0);
  488. test_request_handlers(0);
  489. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  490. }
  491. void _ck_assert_failed(const char *file, int line, const char *expr, ...)
  492. {
  493. va_list va;
  494. va_start(va, expr);
  495. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  496. vfprintf(stderr, expr, va);
  497. va_end(va);
  498. chk_failed++;
  499. }
  500. void _mark_point(const char *file, int line) { chk_ok++; }
  501. void tcase_fn_start(const char *fname, const char *file, int line) {}
  502. void suite_add_tcase(Suite *s, TCase *tc){};
  503. void _tcase_add_test(TCase *tc,
  504. TFun tf,
  505. const char *fname,
  506. int _signal,
  507. int allowed_exit_value,
  508. int start,
  509. int end){};
  510. TCase *tcase_create(const char *name) { return NULL; };
  511. Suite *suite_create(const char *name) { return NULL; };
  512. #endif