public_server.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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 <stdarg.h>
  27. #include <time.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include "public_server.h"
  31. #include <civetweb.h>
  32. #if defined(_WIN32)
  33. #include <Windows.h>
  34. #define test_sleep(x) (Sleep(x * 1000))
  35. #else
  36. #include <unistd.h>
  37. #define test_sleep(x) (sleep(x))
  38. #endif
  39. /* This unit test file uses the excellent Check unit testing library.
  40. * The API documentation is available here:
  41. * http://check.sourceforge.net/doc/check_html/index.html
  42. */
  43. static const char *locate_ssl_cert(void)
  44. {
  45. return
  46. #ifdef _WIN32
  47. #ifdef LOCAL_TEST
  48. "resources\\ssl_cert.pem";
  49. #else
  50. /* Appveyor */
  51. "..\\..\\..\\resources\\ssl_cert.pem"; /* TODO: the different paths
  52. * used in the different test
  53. * system is an unsolved
  54. * problem */
  55. #endif
  56. #else
  57. #ifdef LOCAL_TEST
  58. "resources/ssl_cert.pem";
  59. #else
  60. /* Travis */
  61. "../../resources/ssl_cert.pem"; // TODO: fix path in CI test environment
  62. #endif
  63. #endif
  64. }
  65. static int wait_not_null(void *volatile *data)
  66. {
  67. int i;
  68. for (i = 0; i < 100; i++) {
  69. test_sleep(1);
  70. if (*data != NULL) {
  71. return 1;
  72. }
  73. }
  74. return 0;
  75. }
  76. START_TEST(test_the_test_environment)
  77. {
  78. char wd[300];
  79. char buf[500];
  80. FILE *f;
  81. struct stat st;
  82. int ret;
  83. const char *ssl_cert = locate_ssl_cert();
  84. memset(wd, 0, sizeof(wd));
  85. memset(buf, 0, sizeof(buf));
  86. /* Get the current working directory */
  87. #ifdef _WIN32
  88. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  89. wd[sizeof(wd) - 1] = 0;
  90. #else
  91. (void)getcwd(wd, sizeof(wd));
  92. wd[sizeof(wd) - 1] = 0;
  93. #endif
  94. /* Check the pem file */
  95. #ifdef _WIN32
  96. strcpy(buf, wd);
  97. strcat(buf, "\\");
  98. strcat(buf, ssl_cert);
  99. f = fopen(buf, "rb");
  100. #else
  101. strcpy(buf, wd);
  102. strcat(buf, "/");
  103. strcat(buf, ssl_cert);
  104. f = fopen(buf, "r");
  105. #endif
  106. if (f) {
  107. fclose(f);
  108. } else {
  109. fprintf(stderr, "%s not found", buf);
  110. }
  111. /* Check the test dir */
  112. #ifdef _WIN32
  113. strcpy(buf, wd);
  114. strcat(buf, "\\test");
  115. #else
  116. strcpy(buf, wd);
  117. strcat(buf, "/test");
  118. #endif
  119. memset(&st, 0, sizeof(st));
  120. ret = stat(buf, &st);
  121. if (ret) {
  122. fprintf(stderr, "%s not found", buf);
  123. }
  124. }
  125. END_TEST
  126. static void *threading_data;
  127. static void *test_thread_func_t(void *param)
  128. {
  129. ck_assert_ptr_eq(param, &threading_data);
  130. ck_assert_ptr_eq(threading_data, NULL);
  131. threading_data = &threading_data;
  132. return NULL;
  133. }
  134. START_TEST(test_threading)
  135. {
  136. int ok;
  137. threading_data = NULL;
  138. ok = mg_start_thread(test_thread_func_t, &threading_data);
  139. ck_assert_int_eq(ok, 0);
  140. wait_not_null(&threading_data);
  141. ck_assert_ptr_eq(threading_data, &threading_data);
  142. }
  143. END_TEST
  144. static int log_msg_func(const struct mg_connection *conn, const char *message)
  145. {
  146. struct mg_context *ctx;
  147. char *ud;
  148. ck_assert(conn != NULL);
  149. ctx = mg_get_context(conn);
  150. ck_assert(ctx != NULL);
  151. ud = (char *)mg_get_user_data(ctx);
  152. strncpy(ud, message, 255);
  153. ud[255] = 0;
  154. return 1;
  155. }
  156. START_TEST(test_mg_start_stop_http_server)
  157. {
  158. struct mg_context *ctx;
  159. const char *OPTIONS[] = {
  160. #if !defined(NO_FILES)
  161. "document_root",
  162. ".",
  163. #endif
  164. "listening_ports",
  165. "8080",
  166. NULL,
  167. };
  168. size_t ports_cnt;
  169. int ports[16];
  170. int ssl[16];
  171. struct mg_callbacks callbacks;
  172. char errmsg[256];
  173. memset(ports, 0, sizeof(ports));
  174. memset(ssl, 0, sizeof(ssl));
  175. memset(&callbacks, 0, sizeof(callbacks));
  176. memset(errmsg, 0, sizeof(errmsg));
  177. callbacks.log_message = log_msg_func;
  178. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  179. test_sleep(1);
  180. ck_assert_str_eq(errmsg, "");
  181. ck_assert(ctx != NULL);
  182. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  183. ck_assert_uint_eq(ports_cnt, 1);
  184. ck_assert_int_eq(ports[0], 8080);
  185. ck_assert_int_eq(ssl[0], 0);
  186. ck_assert_int_eq(ports[1], 0);
  187. ck_assert_int_eq(ssl[1], 0);
  188. test_sleep(1);
  189. mg_stop(ctx);
  190. }
  191. END_TEST
  192. START_TEST(test_mg_start_stop_https_server)
  193. {
  194. #ifndef NO_SSL
  195. struct mg_context *ctx;
  196. size_t ports_cnt;
  197. int ports[16];
  198. int ssl[16];
  199. struct mg_callbacks callbacks;
  200. char errmsg[256];
  201. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  202. int opt_idx = 0;
  203. const char *ssl_cert = locate_ssl_cert();
  204. ck_assert(ssl_cert != NULL);
  205. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  206. #if !defined(NO_FILES)
  207. OPTIONS[opt_idx++] = "document_root";
  208. OPTIONS[opt_idx++] = ".";
  209. #endif
  210. OPTIONS[opt_idx++] = "listening_ports";
  211. OPTIONS[opt_idx++] = "8080r,8443s";
  212. OPTIONS[opt_idx++] = "ssl_certificate";
  213. OPTIONS[opt_idx++] = ssl_cert;
  214. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  215. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  216. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  217. memset(ports, 0, sizeof(ports));
  218. memset(ssl, 0, sizeof(ssl));
  219. memset(&callbacks, 0, sizeof(callbacks));
  220. memset(errmsg, 0, sizeof(errmsg));
  221. callbacks.log_message = log_msg_func;
  222. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  223. test_sleep(1);
  224. ck_assert_str_eq(errmsg, "");
  225. ck_assert(ctx != NULL);
  226. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  227. ck_assert_uint_eq(ports_cnt, 2);
  228. ck_assert_int_eq(ports[0], 8080);
  229. ck_assert_int_eq(ssl[0], 0);
  230. ck_assert_int_eq(ports[1], 8443);
  231. ck_assert_int_eq(ssl[1], 1);
  232. ck_assert_int_eq(ports[2], 0);
  233. ck_assert_int_eq(ssl[2], 0);
  234. test_sleep(1);
  235. mg_stop(ctx);
  236. #endif
  237. }
  238. END_TEST
  239. static struct mg_context *g_ctx;
  240. static int request_test_handler(struct mg_connection *conn, void *cbdata)
  241. {
  242. int i;
  243. char chunk_data[32];
  244. const struct mg_request_info *ri;
  245. struct mg_context *ctx;
  246. void *ud, *cud;
  247. ctx = mg_get_context(conn);
  248. ud = mg_get_user_data(ctx);
  249. ri = mg_get_request_info(conn);
  250. ck_assert(ri != NULL);
  251. ck_assert(ctx == g_ctx);
  252. ck_assert(ud == &g_ctx);
  253. mg_set_user_connection_data(conn, (void *)6543);
  254. cud = mg_get_user_connection_data(conn);
  255. ck_assert_ptr_eq((void *)cud, (void *)6543);
  256. ck_assert_ptr_eq((void *)cbdata, (void *)7);
  257. strcpy(chunk_data, "123456789A123456789B123456789C");
  258. mg_printf(conn,
  259. "HTTP/1.1 200 OK\r\n"
  260. "Transfer-Encoding: chunked\r\n"
  261. "Content-Type: text/plain\r\n\r\n");
  262. for (i = 1; i <= 10; i++) {
  263. mg_printf(conn, "%x\r\n", i);
  264. mg_write(conn, chunk_data, (unsigned)i);
  265. mg_printf(conn, "\r\n");
  266. }
  267. mg_printf(conn, "0\r\n\r\n");
  268. return 1;
  269. }
  270. #ifdef USE_WEBSOCKET
  271. /****************************************************************************/
  272. /* WEBSOCKET SERVER */
  273. /****************************************************************************/
  274. static const char *websocket_welcome_msg = "websocket welcome\n";
  275. static const size_t websocket_welcome_msg_len =
  276. 18 /* strlen(websocket_welcome_msg) */;
  277. static const char *websocket_acknowledge_msg = "websocket msg ok\n";
  278. static const size_t websocket_acknowledge_msg_len =
  279. 17 /* strlen(websocket_acknowledge_msg) */;
  280. static const char *websocket_goodbye_msg = "websocket bye\n";
  281. static const size_t websocket_goodbye_msg_len =
  282. 14 /* strlen(websocket_goodbye_msg) */;
  283. static int websock_server_connect(const struct mg_connection *conn, void *udata)
  284. {
  285. (void)conn;
  286. ck_assert_ptr_eq((void *)udata, (void *)7531);
  287. printf("Server: Websocket connected\n");
  288. return 0; /* return 0 to accept every connection */
  289. }
  290. static void websock_server_ready(struct mg_connection *conn, void *udata)
  291. {
  292. ck_assert_ptr_eq((void *)udata, (void *)7531);
  293. printf("Server: Websocket ready\n");
  294. /* Send websocket welcome message */
  295. mg_lock_connection(conn);
  296. mg_websocket_write(conn,
  297. WEBSOCKET_OPCODE_TEXT,
  298. websocket_welcome_msg,
  299. websocket_welcome_msg_len);
  300. mg_unlock_connection(conn);
  301. printf("Server: Websocket ready X\n");
  302. }
  303. static int websock_server_data(struct mg_connection *conn,
  304. int bits,
  305. char *data,
  306. size_t data_len,
  307. void *udata)
  308. {
  309. (void)bits;
  310. ck_assert_ptr_eq((void *)udata, (void *)7531);
  311. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  312. if (data_len < 3 || 0 != memcmp(data, "bye", 3)) {
  313. /* Send websocket acknowledge message */
  314. mg_lock_connection(conn);
  315. mg_websocket_write(conn,
  316. WEBSOCKET_OPCODE_TEXT,
  317. websocket_acknowledge_msg,
  318. websocket_acknowledge_msg_len);
  319. mg_unlock_connection(conn);
  320. } else {
  321. /* Send websocket acknowledge message */
  322. mg_lock_connection(conn);
  323. mg_websocket_write(conn,
  324. WEBSOCKET_OPCODE_TEXT,
  325. websocket_goodbye_msg,
  326. websocket_goodbye_msg_len);
  327. mg_unlock_connection(conn);
  328. }
  329. return 1; /* return 1 to keep the connetion open */
  330. }
  331. static void websock_server_close(const struct mg_connection *conn, void *udata)
  332. {
  333. (void)conn;
  334. ck_assert_ptr_eq((void *)udata, (void *)7531);
  335. printf("Server: Close connection\n");
  336. /* Can not send a websocket goodbye message here - the connection is already
  337. * closed */
  338. }
  339. /****************************************************************************/
  340. /* WEBSOCKET CLIENT */
  341. /****************************************************************************/
  342. struct tclient_data {
  343. void *data;
  344. size_t len;
  345. int closed;
  346. };
  347. static int websocket_client_data_handler(struct mg_connection *conn,
  348. int flags,
  349. char *data,
  350. size_t data_len,
  351. void *user_data)
  352. {
  353. struct mg_context *ctx = mg_get_context(conn);
  354. struct tclient_data *pclient_data =
  355. (struct tclient_data *)mg_get_user_data(ctx);
  356. (void)user_data; /* TODO: check this */
  357. ck_assert(pclient_data != NULL);
  358. ck_assert_int_eq(flags, (int)(128 | 1));
  359. printf("Client received data from server: ");
  360. fwrite(data, 1, data_len, stdout);
  361. printf("\n");
  362. pclient_data->data = malloc(data_len);
  363. ck_assert(pclient_data->data != NULL);
  364. memcpy(pclient_data->data, data, data_len);
  365. pclient_data->len = data_len;
  366. return 1;
  367. }
  368. static void websocket_client_close_handler(const struct mg_connection *conn,
  369. void *user_data)
  370. {
  371. struct mg_context *ctx = mg_get_context(conn);
  372. struct tclient_data *pclient_data =
  373. (struct tclient_data *)mg_get_user_data(ctx);
  374. (void)user_data; /* TODO: check this */
  375. ck_assert(pclient_data != NULL);
  376. printf("Client: Close handler\n");
  377. pclient_data->closed++;
  378. }
  379. #endif
  380. START_TEST(test_request_handlers)
  381. {
  382. char ebuf[100];
  383. struct mg_context *ctx;
  384. struct mg_connection *client_conn;
  385. const struct mg_request_info *ri;
  386. char uri[64];
  387. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  388. const char *expected =
  389. "112123123412345123456123456712345678123456789123456789A";
  390. int i;
  391. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  392. #if defined(USE_IPV6) && defined(NO_SSL)
  393. const char *HTTP_PORT = "8084,[::]:8086";
  394. short ipv4_port = 8084;
  395. short ipv6_port = 8086;
  396. #elif !defined(USE_IPV6) && defined(NO_SSL)
  397. const char *HTTP_PORT = "8084";
  398. short ipv4_port = 8084;
  399. #elif defined(USE_IPV6) && !defined(NO_SSL)
  400. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  401. short ipv4_port = 8084;
  402. short ipv4s_port = 8094;
  403. short ipv4r_port = 8194;
  404. short ipv6_port = 8086;
  405. short ipv6s_port = 8096;
  406. short ipv6r_port = 8196;
  407. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  408. const char *HTTP_PORT = "8084,8194r,8094s";
  409. short ipv4_port = 8084;
  410. short ipv4s_port = 8094;
  411. short ipv4r_port = 8194;
  412. #endif
  413. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  414. const char *opt;
  415. FILE *f;
  416. const char *plain_file_content;
  417. const char *encoded_file_content;
  418. int opt_idx = 0;
  419. #if !defined(NO_SSL)
  420. const char *ssl_cert = locate_ssl_cert();
  421. #endif
  422. #if defined(USE_WEBSOCKET)
  423. struct tclient_data ws_client1_data = {NULL, 0, 0};
  424. struct tclient_data ws_client2_data = {NULL, 0, 0};
  425. struct tclient_data ws_client3_data = {NULL, 0, 0};
  426. struct mg_connection *ws_client1_conn = NULL;
  427. struct mg_connection *ws_client2_conn = NULL;
  428. struct mg_connection *ws_client3_conn = NULL;
  429. #endif
  430. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  431. OPTIONS[opt_idx++] = "listening_ports";
  432. OPTIONS[opt_idx++] = HTTP_PORT;
  433. #if !defined(NO_FILES)
  434. OPTIONS[opt_idx++] = "document_root";
  435. OPTIONS[opt_idx++] = ".";
  436. #endif
  437. #ifndef NO_SSL
  438. ck_assert(ssl_cert != NULL);
  439. OPTIONS[opt_idx++] = "ssl_certificate";
  440. OPTIONS[opt_idx++] = ssl_cert;
  441. #endif
  442. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  443. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  444. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  445. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  446. ck_assert(ctx != NULL);
  447. g_ctx = ctx;
  448. opt = mg_get_option(ctx, "listening_ports");
  449. ck_assert_str_eq(opt, HTTP_PORT);
  450. opt = mg_get_option(ctx, "cgi_environment");
  451. ck_assert_str_eq(opt, "");
  452. opt = mg_get_option(ctx, "unknown_option_name");
  453. ck_assert(opt == NULL);
  454. for (i = 0; i < 1000; i++) {
  455. sprintf(uri, "/U%u", i);
  456. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  457. }
  458. for (i = 500; i < 800; i++) {
  459. sprintf(uri, "/U%u", i);
  460. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  461. }
  462. for (i = 600; i >= 0; i--) {
  463. sprintf(uri, "/U%u", i);
  464. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  465. }
  466. for (i = 750; i <= 1000; i++) {
  467. sprintf(uri, "/U%u", i);
  468. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  469. }
  470. for (i = 5; i < 9; i++) {
  471. sprintf(uri, "/U%u", i);
  472. mg_set_request_handler(
  473. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  474. }
  475. #ifdef USE_WEBSOCKET
  476. mg_set_websocket_handler(ctx,
  477. "/websocket",
  478. websock_server_connect,
  479. websock_server_ready,
  480. websock_server_data,
  481. websock_server_close,
  482. (void *)7531);
  483. #endif
  484. /* Try to load non existing file */
  485. client_conn = mg_download("localhost",
  486. ipv4_port,
  487. 0,
  488. ebuf,
  489. sizeof(ebuf),
  490. "%s",
  491. "GET /file/not/found HTTP/1.0\r\n\r\n");
  492. ck_assert(client_conn != NULL);
  493. ri = mg_get_request_info(client_conn);
  494. ck_assert(ri != NULL);
  495. ck_assert_str_eq(ri->uri, "404");
  496. mg_close_connection(client_conn);
  497. /* Get data from callback */
  498. client_conn = mg_download(
  499. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  500. ck_assert(client_conn != NULL);
  501. ri = mg_get_request_info(client_conn);
  502. ck_assert(ri != NULL);
  503. ck_assert_str_eq(ri->uri, "200");
  504. i = mg_read(client_conn, buf, sizeof(buf));
  505. ck_assert_int_eq(i, (int)strlen(expected));
  506. buf[i] = 0;
  507. ck_assert_str_eq(buf, expected);
  508. mg_close_connection(client_conn);
  509. /* Get data from callback using http://127.0.0.1 */
  510. client_conn = mg_download(
  511. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  512. ck_assert(client_conn != NULL);
  513. ri = mg_get_request_info(client_conn);
  514. ck_assert(ri != NULL);
  515. ck_assert_str_eq(ri->uri, "200");
  516. i = mg_read(client_conn, buf, sizeof(buf));
  517. ck_assert_int_eq(i, (int)strlen(expected));
  518. buf[i] = 0;
  519. ck_assert_str_eq(buf, expected);
  520. mg_close_connection(client_conn);
  521. #if defined(USE_IPV6)
  522. /* Get data from callback using http://[::1] */
  523. client_conn =
  524. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  525. ck_assert(client_conn != NULL);
  526. ri = mg_get_request_info(client_conn);
  527. ck_assert(ri != NULL);
  528. ck_assert_str_eq(ri->uri, "200");
  529. i = mg_read(client_conn, buf, sizeof(buf));
  530. ck_assert_int_eq(i, (int)strlen(expected));
  531. buf[i] = 0;
  532. ck_assert_str_eq(buf, expected);
  533. mg_close_connection(client_conn);
  534. #endif
  535. #if !defined(NO_SSL)
  536. /* Get data from callback using https://127.0.0.1 */
  537. client_conn = mg_download(
  538. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  539. ck_assert(client_conn != NULL);
  540. ri = mg_get_request_info(client_conn);
  541. ck_assert(ri != NULL);
  542. ck_assert_str_eq(ri->uri, "200");
  543. i = mg_read(client_conn, buf, sizeof(buf));
  544. ck_assert_int_eq(i, (int)strlen(expected));
  545. buf[i] = 0;
  546. ck_assert_str_eq(buf, expected);
  547. mg_close_connection(client_conn);
  548. /* Get redirect from callback using http://127.0.0.1 */
  549. client_conn = mg_download(
  550. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  551. ck_assert(client_conn != NULL);
  552. ri = mg_get_request_info(client_conn);
  553. ck_assert(ri != NULL);
  554. ck_assert_str_eq(ri->uri, "302");
  555. i = mg_read(client_conn, buf, sizeof(buf));
  556. ck_assert_int_eq(i, -1);
  557. mg_close_connection(client_conn);
  558. #endif
  559. #if defined(USE_IPV6) && !defined(NO_SSL)
  560. /* Get data from callback using https://[::1] */
  561. client_conn =
  562. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  563. ck_assert(client_conn != NULL);
  564. ri = mg_get_request_info(client_conn);
  565. ck_assert(ri != NULL);
  566. ck_assert_str_eq(ri->uri, "200");
  567. i = mg_read(client_conn, buf, sizeof(buf));
  568. ck_assert_int_eq(i, (int)strlen(expected));
  569. buf[i] = 0;
  570. ck_assert_str_eq(buf, expected);
  571. mg_close_connection(client_conn);
  572. /* Get redirect from callback using http://127.0.0.1 */
  573. client_conn =
  574. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  575. ck_assert(client_conn != NULL);
  576. ri = mg_get_request_info(client_conn);
  577. ck_assert(ri != NULL);
  578. ck_assert_str_eq(ri->uri, "302");
  579. i = mg_read(client_conn, buf, sizeof(buf));
  580. ck_assert_int_eq(i, -1);
  581. mg_close_connection(client_conn);
  582. #endif
  583. /* It seems to be impossible to find out what the actual working
  584. * directory of the CI test environment is. Before breaking another
  585. * dozen of builds by trying blindly with different paths, just
  586. * create the file here */
  587. #ifdef _WIN32
  588. f = fopen("test.txt", "wb");
  589. #else
  590. f = fopen("test.txt", "w");
  591. #endif
  592. plain_file_content = "simple text file\n";
  593. fwrite(plain_file_content, 17, 1, f);
  594. fclose(f);
  595. #ifdef _WIN32
  596. f = fopen("test_gz.txt.gz", "wb");
  597. #else
  598. f = fopen("test_gz.txt.gz", "w");
  599. #endif
  600. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  601. "test_gz.txt"
  602. "\x00\x01\x11\x00\xee\xff"
  603. "zipped text file"
  604. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  605. fwrite(encoded_file_content, 1, 52, f);
  606. fclose(f);
  607. /* Get static data */
  608. client_conn = mg_download("localhost",
  609. ipv4_port,
  610. 0,
  611. ebuf,
  612. sizeof(ebuf),
  613. "%s",
  614. "GET /test.txt HTTP/1.0\r\n\r\n");
  615. ck_assert(client_conn != NULL);
  616. ri = mg_get_request_info(client_conn);
  617. ck_assert(ri != NULL);
  618. #if defined(NO_FILES)
  619. ck_assert_str_eq(ri->uri, "404");
  620. #else
  621. ck_assert_str_eq(ri->uri, "200");
  622. i = mg_read(client_conn, buf, sizeof(buf));
  623. ck_assert_int_eq(i, 17);
  624. if ((i >= 0) && (i < (int)sizeof(buf))) {
  625. buf[i] = 0;
  626. }
  627. ck_assert_str_eq(buf, plain_file_content);
  628. #endif
  629. mg_close_connection(client_conn);
  630. /* Get zipped static data - will not work if Accept-Encoding is not set */
  631. client_conn = mg_download("localhost",
  632. ipv4_port,
  633. 0,
  634. ebuf,
  635. sizeof(ebuf),
  636. "%s",
  637. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  638. ck_assert(client_conn != NULL);
  639. ri = mg_get_request_info(client_conn);
  640. ck_assert(ri != NULL);
  641. ck_assert_str_eq(ri->uri, "404");
  642. mg_close_connection(client_conn);
  643. /* Get zipped static data - with Accept-Encoding */
  644. client_conn = mg_download(
  645. "localhost",
  646. ipv4_port,
  647. 0,
  648. ebuf,
  649. sizeof(ebuf),
  650. "%s",
  651. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  652. ck_assert(client_conn != NULL);
  653. ri = mg_get_request_info(client_conn);
  654. ck_assert(ri != NULL);
  655. #if defined(NO_FILES)
  656. ck_assert_str_eq(ri->uri, "404");
  657. #else
  658. ck_assert_str_eq(ri->uri, "200");
  659. i = mg_read(client_conn, buf, sizeof(buf));
  660. ck_assert_int_eq(i, 52);
  661. if ((i >= 0) && (i < (int)sizeof(buf))) {
  662. buf[i] = 0;
  663. }
  664. ck_assert_int_eq(ri->content_length, 52);
  665. ck_assert_str_eq(buf, encoded_file_content);
  666. #endif
  667. mg_close_connection(client_conn);
  668. /* Get directory listing */
  669. client_conn = mg_download("localhost",
  670. ipv4_port,
  671. 0,
  672. ebuf,
  673. sizeof(ebuf),
  674. "%s",
  675. "GET / HTTP/1.0\r\n\r\n");
  676. ck_assert(client_conn != NULL);
  677. ri = mg_get_request_info(client_conn);
  678. ck_assert(ri != NULL);
  679. #if defined(NO_FILES)
  680. ck_assert_str_eq(ri->uri, "404");
  681. #else
  682. ck_assert_str_eq(ri->uri, "200");
  683. i = mg_read(client_conn, buf, sizeof(buf));
  684. ck_assert(i > 6);
  685. buf[6] = 0;
  686. ck_assert_str_eq(buf, "<html>");
  687. #endif
  688. mg_close_connection(client_conn);
  689. /* POST to static file (will not work) */
  690. client_conn = mg_download("localhost",
  691. ipv4_port,
  692. 0,
  693. ebuf,
  694. sizeof(ebuf),
  695. "%s",
  696. "POST /test.txt HTTP/1.0\r\n\r\n");
  697. ck_assert(client_conn != NULL);
  698. ri = mg_get_request_info(client_conn);
  699. ck_assert(ri != NULL);
  700. #if defined(NO_FILES)
  701. ck_assert_str_eq(ri->uri, "404");
  702. #else
  703. ck_assert_str_eq(ri->uri, "405");
  704. i = mg_read(client_conn, buf, sizeof(buf));
  705. ck_assert(i >= 29);
  706. buf[29] = 0;
  707. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  708. #endif
  709. mg_close_connection(client_conn);
  710. /* PUT to static file (will not work) */
  711. client_conn = mg_download("localhost",
  712. ipv4_port,
  713. 0,
  714. ebuf,
  715. sizeof(ebuf),
  716. "%s",
  717. "PUT /test.txt HTTP/1.0\r\n\r\n");
  718. ck_assert(client_conn != NULL);
  719. ri = mg_get_request_info(client_conn);
  720. ck_assert(ri != NULL);
  721. #if defined(NO_FILES)
  722. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  723. #else
  724. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  725. #endif
  726. mg_close_connection(client_conn);
  727. /* Websocket test */
  728. #ifdef USE_WEBSOCKET
  729. /* Then connect a first client */
  730. ws_client1_conn =
  731. mg_connect_websocket_client("localhost",
  732. ipv4_port,
  733. 0,
  734. ebuf,
  735. sizeof(ebuf),
  736. "/websocket",
  737. NULL,
  738. websocket_client_data_handler,
  739. websocket_client_close_handler,
  740. &ws_client1_data);
  741. ck_assert(ws_client1_conn != NULL);
  742. wait_not_null(
  743. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  744. ck_assert_int_eq(ws_client1_data.closed, 0);
  745. ck_assert_int_eq(ws_client2_data.closed, 0);
  746. ck_assert_int_eq(ws_client3_data.closed, 0);
  747. ck_assert(ws_client2_data.data == NULL);
  748. ck_assert_uint_eq(ws_client2_data.len, 0);
  749. ck_assert(ws_client1_data.data != NULL);
  750. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  751. ck_assert(!memcmp(ws_client1_data.data,
  752. websocket_welcome_msg,
  753. websocket_welcome_msg_len));
  754. free(ws_client1_data.data);
  755. ws_client1_data.data = NULL;
  756. ws_client1_data.len = 0;
  757. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data1", 5);
  758. wait_not_null(
  759. &(ws_client1_data
  760. .data)); /* Wait for the websocket acknowledge message */
  761. ck_assert_int_eq(ws_client1_data.closed, 0);
  762. ck_assert_int_eq(ws_client2_data.closed, 0);
  763. ck_assert(ws_client2_data.data == NULL);
  764. ck_assert_uint_eq(ws_client2_data.len, 0);
  765. ck_assert(ws_client1_data.data != NULL);
  766. ck_assert_uint_eq(ws_client1_data.len, websocket_acknowledge_msg_len);
  767. ck_assert(!memcmp(ws_client1_data.data,
  768. websocket_acknowledge_msg,
  769. websocket_acknowledge_msg_len));
  770. free(ws_client1_data.data);
  771. ws_client1_data.data = NULL;
  772. ws_client1_data.len = 0;
  773. /* Now connect a second client */
  774. #ifdef USE_IPV6
  775. ws_client2_conn =
  776. mg_connect_websocket_client("[::1]",
  777. ipv6_port,
  778. 0,
  779. ebuf,
  780. sizeof(ebuf),
  781. "/websocket",
  782. NULL,
  783. websocket_client_data_handler,
  784. websocket_client_close_handler,
  785. &ws_client2_data);
  786. #else
  787. ws_client2_conn =
  788. mg_connect_websocket_client("127.0.0.1",
  789. ipv4_port,
  790. 0,
  791. ebuf,
  792. sizeof(ebuf),
  793. "/websocket",
  794. NULL,
  795. websocket_client_data_handler,
  796. websocket_client_close_handler,
  797. &ws_client2_data);
  798. #endif
  799. ck_assert(ws_client2_conn != NULL);
  800. wait_not_null(
  801. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  802. ck_assert(ws_client1_data.closed == 0);
  803. ck_assert(ws_client2_data.closed == 0);
  804. ck_assert(ws_client1_data.data == NULL);
  805. ck_assert(ws_client1_data.len == 0);
  806. ck_assert(ws_client2_data.data != NULL);
  807. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  808. ck_assert(!memcmp(ws_client2_data.data,
  809. websocket_welcome_msg,
  810. websocket_welcome_msg_len));
  811. free(ws_client2_data.data);
  812. ws_client2_data.data = NULL;
  813. ws_client2_data.len = 0;
  814. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data2", 5);
  815. wait_not_null(
  816. &(ws_client1_data
  817. .data)); /* Wait for the websocket acknowledge message */
  818. ck_assert(ws_client1_data.closed == 0);
  819. ck_assert(ws_client2_data.closed == 0);
  820. ck_assert(ws_client2_data.data == NULL);
  821. ck_assert(ws_client2_data.len == 0);
  822. ck_assert(ws_client1_data.data != NULL);
  823. ck_assert(ws_client1_data.len == websocket_acknowledge_msg_len);
  824. ck_assert(!memcmp(ws_client1_data.data,
  825. websocket_acknowledge_msg,
  826. websocket_acknowledge_msg_len));
  827. free(ws_client1_data.data);
  828. ws_client1_data.data = NULL;
  829. ws_client1_data.len = 0;
  830. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  831. wait_not_null(
  832. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  833. ck_assert(ws_client1_data.closed == 0);
  834. ck_assert(ws_client2_data.closed == 0);
  835. ck_assert(ws_client2_data.data == NULL);
  836. ck_assert(ws_client2_data.len == 0);
  837. ck_assert(ws_client1_data.data != NULL);
  838. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  839. ck_assert(!memcmp(ws_client1_data.data,
  840. websocket_goodbye_msg,
  841. websocket_goodbye_msg_len));
  842. free(ws_client1_data.data);
  843. ws_client1_data.data = NULL;
  844. ws_client1_data.len = 0;
  845. mg_close_connection(ws_client1_conn);
  846. test_sleep(3); /* Won't get any message */
  847. ck_assert(ws_client1_data.closed == 1);
  848. ck_assert(ws_client2_data.closed == 0);
  849. ck_assert(ws_client1_data.data == NULL);
  850. ck_assert(ws_client1_data.len == 0);
  851. ck_assert(ws_client2_data.data == NULL);
  852. ck_assert(ws_client2_data.len == 0);
  853. mg_websocket_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  854. wait_not_null(
  855. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  856. ck_assert(ws_client1_data.closed == 1);
  857. ck_assert(ws_client2_data.closed == 0);
  858. ck_assert(ws_client1_data.data == NULL);
  859. ck_assert(ws_client1_data.len == 0);
  860. ck_assert(ws_client2_data.data != NULL);
  861. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  862. ck_assert(!memcmp(ws_client2_data.data,
  863. websocket_goodbye_msg,
  864. websocket_goodbye_msg_len));
  865. free(ws_client2_data.data);
  866. ws_client2_data.data = NULL;
  867. ws_client2_data.len = 0;
  868. mg_close_connection(ws_client2_conn);
  869. test_sleep(3); /* Won't get any message */
  870. ck_assert(ws_client1_data.closed == 1);
  871. ck_assert(ws_client2_data.closed == 1);
  872. ck_assert(ws_client1_data.data == NULL);
  873. ck_assert(ws_client1_data.len == 0);
  874. ck_assert(ws_client2_data.data == NULL);
  875. ck_assert(ws_client2_data.len == 0);
  876. /* Connect client 3 */
  877. ws_client3_conn =
  878. mg_connect_websocket_client("localhost",
  879. #if defined(NO_SSL)
  880. ipv4_port,
  881. 0,
  882. #else
  883. ipv4s_port,
  884. 1,
  885. #endif
  886. ebuf,
  887. sizeof(ebuf),
  888. "/websocket",
  889. NULL,
  890. websocket_client_data_handler,
  891. websocket_client_close_handler,
  892. &ws_client3_data);
  893. ck_assert(ws_client3_conn != NULL);
  894. wait_not_null(
  895. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  896. ck_assert(ws_client1_data.closed == 1);
  897. ck_assert(ws_client2_data.closed == 1);
  898. ck_assert(ws_client3_data.closed == 0);
  899. ck_assert(ws_client1_data.data == NULL);
  900. ck_assert(ws_client1_data.len == 0);
  901. ck_assert(ws_client2_data.data == NULL);
  902. ck_assert(ws_client2_data.len == 0);
  903. ck_assert(ws_client3_data.data != NULL);
  904. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  905. ck_assert(!memcmp(ws_client3_data.data,
  906. websocket_welcome_msg,
  907. websocket_welcome_msg_len));
  908. free(ws_client3_data.data);
  909. ws_client3_data.data = NULL;
  910. ws_client3_data.len = 0;
  911. #endif
  912. /* Close the server */
  913. g_ctx = NULL;
  914. mg_stop(ctx);
  915. #ifdef USE_WEBSOCKET
  916. for (i = 0; i < 100; i++) {
  917. test_sleep(1);
  918. if (ws_client3_data.closed != 0) {
  919. break;
  920. }
  921. }
  922. ck_assert_int_eq(ws_client3_data.closed, 1);
  923. #endif
  924. }
  925. END_TEST
  926. Suite *make_public_server_suite(void)
  927. {
  928. Suite *const suite = suite_create("PublicServer");
  929. TCase *const checktestenv = tcase_create("Check test environment");
  930. TCase *const startthreads = tcase_create("Start threads");
  931. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  932. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  933. TCase *const serverrequests = tcase_create("Server Requests");
  934. tcase_add_test(checktestenv, test_the_test_environment);
  935. tcase_set_timeout(checktestenv, civetweb_min_test_timeout);
  936. suite_add_tcase(suite, checktestenv);
  937. tcase_add_test(startthreads, test_threading);
  938. tcase_set_timeout(startthreads, civetweb_min_test_timeout);
  939. suite_add_tcase(suite, startthreads);
  940. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  941. tcase_set_timeout(startstophttp, civetweb_min_test_timeout);
  942. suite_add_tcase(suite, startstophttp);
  943. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  944. tcase_set_timeout(startstophttps, civetweb_min_test_timeout);
  945. suite_add_tcase(suite, startstophttps);
  946. tcase_add_test(serverrequests, test_request_handlers);
  947. tcase_set_timeout(serverrequests, 120);
  948. suite_add_tcase(suite, serverrequests);
  949. return suite;
  950. }
  951. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  952. /* Used to debug test cases without using the check framework */
  953. static int chk_ok = 0;
  954. static int chk_failed = 0;
  955. void main(void)
  956. {
  957. test_the_test_environment(0);
  958. test_threading(0);
  959. test_mg_start_stop_http_server(0);
  960. test_mg_start_stop_https_server(0);
  961. test_request_handlers(0);
  962. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  963. }
  964. void _ck_assert_failed(const char *file, int line, const char *expr, ...)
  965. {
  966. va_list va;
  967. va_start(va, expr);
  968. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  969. vfprintf(stderr, expr, va);
  970. fprintf(stderr, "\n\n");
  971. va_end(va);
  972. chk_failed++;
  973. }
  974. void _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  975. {
  976. va_list va;
  977. if (cond) {
  978. chk_ok++;
  979. return;
  980. }
  981. va_start(va, expr);
  982. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  983. vfprintf(stderr, expr, va);
  984. fprintf(stderr, "\n\n");
  985. va_end(va);
  986. chk_failed++;
  987. }
  988. void _mark_point(const char *file, int line)
  989. {
  990. chk_ok++;
  991. }
  992. void tcase_fn_start(const char *fname, const char *file, int line)
  993. {
  994. }
  995. void suite_add_tcase(Suite *s, TCase *tc){};
  996. void _tcase_add_test(TCase *tc,
  997. TFun tf,
  998. const char *fname,
  999. int _signal,
  1000. int allowed_exit_value,
  1001. int start,
  1002. int end){};
  1003. TCase *tcase_create(const char *name)
  1004. {
  1005. return NULL;
  1006. };
  1007. Suite *suite_create(const char *name)
  1008. {
  1009. return NULL;
  1010. };
  1011. void tcase_set_timeout(TCase *tc, double timeout){};
  1012. #endif