public_server.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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. #ifndef _WIN32
  291. /* TODO(critical): Why is mg_lock_connection here a deadlock in Linux,
  292. * while it is OK for Windows? */
  293. #define mg_lock_connection(conn)
  294. #define mg_unlock_connection(conn)
  295. #endif
  296. static void websock_server_ready(struct mg_connection *conn, void *udata)
  297. {
  298. ck_assert_ptr_eq((void *)udata, (void *)7531);
  299. printf("Server: Websocket ready\n");
  300. /* Send websocket welcome message */
  301. mg_lock_connection(conn);
  302. mg_websocket_write(conn,
  303. WEBSOCKET_OPCODE_TEXT,
  304. websocket_welcome_msg,
  305. websocket_welcome_msg_len);
  306. mg_unlock_connection(conn);
  307. printf("Server: Websocket ready X\n");
  308. }
  309. static int websock_server_data(struct mg_connection *conn,
  310. int bits,
  311. char *data,
  312. size_t data_len,
  313. void *udata)
  314. {
  315. (void)bits;
  316. ck_assert_ptr_eq((void *)udata, (void *)7531);
  317. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  318. if (data_len < 3 || 0 != memcmp(data, "bye", 3)) {
  319. /* Send websocket acknowledge message */
  320. mg_lock_connection(conn);
  321. mg_websocket_write(conn,
  322. WEBSOCKET_OPCODE_TEXT,
  323. websocket_acknowledge_msg,
  324. websocket_acknowledge_msg_len);
  325. mg_unlock_connection(conn);
  326. } else {
  327. /* Send websocket acknowledge message */
  328. mg_lock_connection(conn);
  329. mg_websocket_write(conn,
  330. WEBSOCKET_OPCODE_TEXT,
  331. websocket_goodbye_msg,
  332. websocket_goodbye_msg_len);
  333. mg_unlock_connection(conn);
  334. }
  335. return 1; /* return 1 to keep the connetion open */
  336. }
  337. static void websock_server_close(const struct mg_connection *conn, void *udata)
  338. {
  339. (void)conn;
  340. ck_assert_ptr_eq((void *)udata, (void *)7531);
  341. printf("Server: Close connection\n");
  342. /* Can not send a websocket goodbye message here - the connection is already
  343. * closed */
  344. }
  345. /****************************************************************************/
  346. /* WEBSOCKET CLIENT */
  347. /****************************************************************************/
  348. struct tclient_data {
  349. void *data;
  350. size_t len;
  351. int closed;
  352. };
  353. static int websocket_client_data_handler(struct mg_connection *conn,
  354. int flags,
  355. char *data,
  356. size_t data_len,
  357. void *user_data)
  358. {
  359. struct mg_context *ctx = mg_get_context(conn);
  360. struct tclient_data *pclient_data =
  361. (struct tclient_data *)mg_get_user_data(ctx);
  362. (void)user_data; /* TODO: check this */
  363. ck_assert(pclient_data != NULL);
  364. ck_assert_int_eq(flags, (int)(128 | 1));
  365. printf("Client received data from server: ");
  366. fwrite(data, 1, data_len, stdout);
  367. printf("\n");
  368. pclient_data->data = malloc(data_len);
  369. ck_assert(pclient_data->data != NULL);
  370. memcpy(pclient_data->data, data, data_len);
  371. pclient_data->len = data_len;
  372. return 1;
  373. }
  374. static void websocket_client_close_handler(const struct mg_connection *conn,
  375. void *user_data)
  376. {
  377. struct mg_context *ctx = mg_get_context(conn);
  378. struct tclient_data *pclient_data =
  379. (struct tclient_data *)mg_get_user_data(ctx);
  380. (void)user_data; /* TODO: check this */
  381. ck_assert(pclient_data != NULL);
  382. printf("Client: Close handler\n");
  383. pclient_data->closed++;
  384. }
  385. #endif
  386. START_TEST(test_request_handlers)
  387. {
  388. char ebuf[100];
  389. struct mg_context *ctx;
  390. struct mg_connection *client_conn;
  391. const struct mg_request_info *ri;
  392. char uri[64];
  393. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  394. const char *expected =
  395. "112123123412345123456123456712345678123456789123456789A";
  396. int i;
  397. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  398. #if defined(USE_IPV6) && defined(NO_SSL)
  399. const char *HTTP_PORT = "8084,[::]:8086";
  400. short ipv4_port = 8084;
  401. short ipv6_port = 8086;
  402. #elif !defined(USE_IPV6) && defined(NO_SSL)
  403. const char *HTTP_PORT = "8084";
  404. short ipv4_port = 8084;
  405. #elif defined(USE_IPV6) && !defined(NO_SSL)
  406. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  407. short ipv4_port = 8084;
  408. short ipv4s_port = 8094;
  409. short ipv4r_port = 8194;
  410. short ipv6_port = 8086;
  411. short ipv6s_port = 8096;
  412. short ipv6r_port = 8196;
  413. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  414. const char *HTTP_PORT = "8084,8194r,8094s";
  415. short ipv4_port = 8084;
  416. short ipv4s_port = 8094;
  417. short ipv4r_port = 8194;
  418. #endif
  419. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  420. const char *opt;
  421. FILE *f;
  422. const char *plain_file_content;
  423. const char *encoded_file_content;
  424. int opt_idx = 0;
  425. #if !defined(NO_SSL)
  426. const char *ssl_cert = locate_ssl_cert();
  427. #endif
  428. #if defined(USE_WEBSOCKET)
  429. struct tclient_data ws_client1_data = {NULL, 0, 0};
  430. struct tclient_data ws_client2_data = {NULL, 0, 0};
  431. struct tclient_data ws_client3_data = {NULL, 0, 0};
  432. struct mg_connection *ws_client1_conn = NULL;
  433. struct mg_connection *ws_client2_conn = NULL;
  434. struct mg_connection *ws_client3_conn = NULL;
  435. #endif
  436. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  437. OPTIONS[opt_idx++] = "listening_ports";
  438. OPTIONS[opt_idx++] = HTTP_PORT;
  439. #if !defined(NO_FILES)
  440. OPTIONS[opt_idx++] = "document_root";
  441. OPTIONS[opt_idx++] = ".";
  442. #endif
  443. #ifndef NO_SSL
  444. ck_assert(ssl_cert != NULL);
  445. OPTIONS[opt_idx++] = "ssl_certificate";
  446. OPTIONS[opt_idx++] = ssl_cert;
  447. #endif
  448. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  449. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  450. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  451. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  452. ck_assert(ctx != NULL);
  453. g_ctx = ctx;
  454. opt = mg_get_option(ctx, "listening_ports");
  455. ck_assert_str_eq(opt, HTTP_PORT);
  456. opt = mg_get_option(ctx, "cgi_environment");
  457. ck_assert_str_eq(opt, "");
  458. opt = mg_get_option(ctx, "unknown_option_name");
  459. ck_assert(opt == NULL);
  460. for (i = 0; i < 1000; i++) {
  461. sprintf(uri, "/U%u", i);
  462. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  463. }
  464. for (i = 500; i < 800; i++) {
  465. sprintf(uri, "/U%u", i);
  466. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  467. }
  468. for (i = 600; i >= 0; i--) {
  469. sprintf(uri, "/U%u", i);
  470. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  471. }
  472. for (i = 750; i <= 1000; i++) {
  473. sprintf(uri, "/U%u", i);
  474. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  475. }
  476. for (i = 5; i < 9; i++) {
  477. sprintf(uri, "/U%u", i);
  478. mg_set_request_handler(
  479. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  480. }
  481. #ifdef USE_WEBSOCKET
  482. mg_set_websocket_handler(ctx,
  483. "/websocket",
  484. websock_server_connect,
  485. websock_server_ready,
  486. websock_server_data,
  487. websock_server_close,
  488. (void *)7531);
  489. #endif
  490. /* Try to load non existing file */
  491. client_conn = mg_download("localhost",
  492. ipv4_port,
  493. 0,
  494. ebuf,
  495. sizeof(ebuf),
  496. "%s",
  497. "GET /file/not/found HTTP/1.0\r\n\r\n");
  498. ck_assert(client_conn != NULL);
  499. ri = mg_get_request_info(client_conn);
  500. ck_assert(ri != NULL);
  501. ck_assert_str_eq(ri->uri, "404");
  502. mg_close_connection(client_conn);
  503. /* Get data from callback */
  504. client_conn = mg_download(
  505. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  506. ck_assert(client_conn != NULL);
  507. ri = mg_get_request_info(client_conn);
  508. ck_assert(ri != NULL);
  509. ck_assert_str_eq(ri->uri, "200");
  510. i = mg_read(client_conn, buf, sizeof(buf));
  511. ck_assert_int_eq(i, (int)strlen(expected));
  512. buf[i] = 0;
  513. ck_assert_str_eq(buf, expected);
  514. mg_close_connection(client_conn);
  515. /* Get data from callback using http://127.0.0.1 */
  516. client_conn = mg_download(
  517. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  518. ck_assert(client_conn != NULL);
  519. ri = mg_get_request_info(client_conn);
  520. ck_assert(ri != NULL);
  521. ck_assert_str_eq(ri->uri, "200");
  522. i = mg_read(client_conn, buf, sizeof(buf));
  523. ck_assert_int_eq(i, (int)strlen(expected));
  524. buf[i] = 0;
  525. ck_assert_str_eq(buf, expected);
  526. mg_close_connection(client_conn);
  527. #if defined(USE_IPV6)
  528. /* Get data from callback using http://[::1] */
  529. client_conn =
  530. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  531. ck_assert(client_conn != NULL);
  532. ri = mg_get_request_info(client_conn);
  533. ck_assert(ri != NULL);
  534. ck_assert_str_eq(ri->uri, "200");
  535. i = mg_read(client_conn, buf, sizeof(buf));
  536. ck_assert_int_eq(i, (int)strlen(expected));
  537. buf[i] = 0;
  538. ck_assert_str_eq(buf, expected);
  539. mg_close_connection(client_conn);
  540. #endif
  541. #if !defined(NO_SSL)
  542. /* Get data from callback using https://127.0.0.1 */
  543. client_conn = mg_download(
  544. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  545. ck_assert(client_conn != NULL);
  546. ri = mg_get_request_info(client_conn);
  547. ck_assert(ri != NULL);
  548. ck_assert_str_eq(ri->uri, "200");
  549. i = mg_read(client_conn, buf, sizeof(buf));
  550. ck_assert_int_eq(i, (int)strlen(expected));
  551. buf[i] = 0;
  552. ck_assert_str_eq(buf, expected);
  553. mg_close_connection(client_conn);
  554. /* Get redirect from callback using http://127.0.0.1 */
  555. client_conn = mg_download(
  556. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  557. ck_assert(client_conn != NULL);
  558. ri = mg_get_request_info(client_conn);
  559. ck_assert(ri != NULL);
  560. ck_assert_str_eq(ri->uri, "302");
  561. i = mg_read(client_conn, buf, sizeof(buf));
  562. ck_assert_int_eq(i, -1);
  563. mg_close_connection(client_conn);
  564. #endif
  565. #if defined(USE_IPV6) && !defined(NO_SSL)
  566. /* Get data from callback using https://[::1] */
  567. client_conn =
  568. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  569. ck_assert(client_conn != NULL);
  570. ri = mg_get_request_info(client_conn);
  571. ck_assert(ri != NULL);
  572. ck_assert_str_eq(ri->uri, "200");
  573. i = mg_read(client_conn, buf, sizeof(buf));
  574. ck_assert_int_eq(i, (int)strlen(expected));
  575. buf[i] = 0;
  576. ck_assert_str_eq(buf, expected);
  577. mg_close_connection(client_conn);
  578. /* Get redirect from callback using http://127.0.0.1 */
  579. client_conn =
  580. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  581. ck_assert(client_conn != NULL);
  582. ri = mg_get_request_info(client_conn);
  583. ck_assert(ri != NULL);
  584. ck_assert_str_eq(ri->uri, "302");
  585. i = mg_read(client_conn, buf, sizeof(buf));
  586. ck_assert_int_eq(i, -1);
  587. mg_close_connection(client_conn);
  588. #endif
  589. /* It seems to be impossible to find out what the actual working
  590. * directory of the CI test environment is. Before breaking another
  591. * dozen of builds by trying blindly with different paths, just
  592. * create the file here */
  593. #ifdef _WIN32
  594. f = fopen("test.txt", "wb");
  595. #else
  596. f = fopen("test.txt", "w");
  597. #endif
  598. plain_file_content = "simple text file\n";
  599. fwrite(plain_file_content, 17, 1, f);
  600. fclose(f);
  601. #ifdef _WIN32
  602. f = fopen("test_gz.txt.gz", "wb");
  603. #else
  604. f = fopen("test_gz.txt.gz", "w");
  605. #endif
  606. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  607. "test_gz.txt"
  608. "\x00\x01\x11\x00\xee\xff"
  609. "zipped text file"
  610. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  611. fwrite(encoded_file_content, 1, 52, f);
  612. fclose(f);
  613. /* Get static data */
  614. client_conn = mg_download("localhost",
  615. ipv4_port,
  616. 0,
  617. ebuf,
  618. sizeof(ebuf),
  619. "%s",
  620. "GET /test.txt HTTP/1.0\r\n\r\n");
  621. ck_assert(client_conn != NULL);
  622. ri = mg_get_request_info(client_conn);
  623. ck_assert(ri != NULL);
  624. #if defined(NO_FILES)
  625. ck_assert_str_eq(ri->uri, "404");
  626. #else
  627. ck_assert_str_eq(ri->uri, "200");
  628. i = mg_read(client_conn, buf, sizeof(buf));
  629. ck_assert_int_eq(i, 17);
  630. if ((i >= 0) && (i < (int)sizeof(buf))) {
  631. buf[i] = 0;
  632. }
  633. ck_assert_str_eq(buf, plain_file_content);
  634. #endif
  635. mg_close_connection(client_conn);
  636. /* Get zipped static data - will not work if Accept-Encoding is not set */
  637. client_conn = mg_download("localhost",
  638. ipv4_port,
  639. 0,
  640. ebuf,
  641. sizeof(ebuf),
  642. "%s",
  643. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  644. ck_assert(client_conn != NULL);
  645. ri = mg_get_request_info(client_conn);
  646. ck_assert(ri != NULL);
  647. ck_assert_str_eq(ri->uri, "404");
  648. mg_close_connection(client_conn);
  649. /* Get zipped static data - with Accept-Encoding */
  650. client_conn = mg_download(
  651. "localhost",
  652. ipv4_port,
  653. 0,
  654. ebuf,
  655. sizeof(ebuf),
  656. "%s",
  657. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  658. ck_assert(client_conn != NULL);
  659. ri = mg_get_request_info(client_conn);
  660. ck_assert(ri != NULL);
  661. #if defined(NO_FILES)
  662. ck_assert_str_eq(ri->uri, "404");
  663. #else
  664. ck_assert_str_eq(ri->uri, "200");
  665. i = mg_read(client_conn, buf, sizeof(buf));
  666. ck_assert_int_eq(i, 52);
  667. if ((i >= 0) && (i < (int)sizeof(buf))) {
  668. buf[i] = 0;
  669. }
  670. ck_assert_int_eq(ri->content_length, 52);
  671. ck_assert_str_eq(buf, encoded_file_content);
  672. #endif
  673. mg_close_connection(client_conn);
  674. /* Get directory listing */
  675. client_conn = mg_download("localhost",
  676. ipv4_port,
  677. 0,
  678. ebuf,
  679. sizeof(ebuf),
  680. "%s",
  681. "GET / HTTP/1.0\r\n\r\n");
  682. ck_assert(client_conn != NULL);
  683. ri = mg_get_request_info(client_conn);
  684. ck_assert(ri != NULL);
  685. #if defined(NO_FILES)
  686. ck_assert_str_eq(ri->uri, "404");
  687. #else
  688. ck_assert_str_eq(ri->uri, "200");
  689. i = mg_read(client_conn, buf, sizeof(buf));
  690. ck_assert(i > 6);
  691. buf[6] = 0;
  692. ck_assert_str_eq(buf, "<html>");
  693. #endif
  694. mg_close_connection(client_conn);
  695. /* POST to static file (will not work) */
  696. client_conn = mg_download("localhost",
  697. ipv4_port,
  698. 0,
  699. ebuf,
  700. sizeof(ebuf),
  701. "%s",
  702. "POST /test.txt HTTP/1.0\r\n\r\n");
  703. ck_assert(client_conn != NULL);
  704. ri = mg_get_request_info(client_conn);
  705. ck_assert(ri != NULL);
  706. #if defined(NO_FILES)
  707. ck_assert_str_eq(ri->uri, "404");
  708. #else
  709. ck_assert_str_eq(ri->uri, "405");
  710. i = mg_read(client_conn, buf, sizeof(buf));
  711. ck_assert(i >= 29);
  712. buf[29] = 0;
  713. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  714. #endif
  715. mg_close_connection(client_conn);
  716. /* PUT to static file (will not work) */
  717. client_conn = mg_download("localhost",
  718. ipv4_port,
  719. 0,
  720. ebuf,
  721. sizeof(ebuf),
  722. "%s",
  723. "PUT /test.txt HTTP/1.0\r\n\r\n");
  724. ck_assert(client_conn != NULL);
  725. ri = mg_get_request_info(client_conn);
  726. ck_assert(ri != NULL);
  727. #if defined(NO_FILES)
  728. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  729. #else
  730. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  731. #endif
  732. mg_close_connection(client_conn);
  733. /* Websocket test */
  734. #ifdef USE_WEBSOCKET
  735. /* Then connect a first client */
  736. ws_client1_conn =
  737. mg_connect_websocket_client("localhost",
  738. ipv4_port,
  739. 0,
  740. ebuf,
  741. sizeof(ebuf),
  742. "/websocket",
  743. NULL,
  744. websocket_client_data_handler,
  745. websocket_client_close_handler,
  746. &ws_client1_data);
  747. ck_assert(ws_client1_conn != NULL);
  748. wait_not_null(
  749. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  750. ck_assert_int_eq(ws_client1_data.closed, 0);
  751. ck_assert_int_eq(ws_client2_data.closed, 0);
  752. ck_assert_int_eq(ws_client3_data.closed, 0);
  753. ck_assert(ws_client2_data.data == NULL);
  754. ck_assert_uint_eq(ws_client2_data.len, 0);
  755. ck_assert(ws_client1_data.data != NULL);
  756. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  757. ck_assert(!memcmp(ws_client1_data.data,
  758. websocket_welcome_msg,
  759. websocket_welcome_msg_len));
  760. free(ws_client1_data.data);
  761. ws_client1_data.data = NULL;
  762. ws_client1_data.len = 0;
  763. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data1", 5);
  764. wait_not_null(
  765. &(ws_client1_data
  766. .data)); /* Wait for the websocket acknowledge message */
  767. ck_assert_int_eq(ws_client1_data.closed, 0);
  768. ck_assert_int_eq(ws_client2_data.closed, 0);
  769. ck_assert(ws_client2_data.data == NULL);
  770. ck_assert_uint_eq(ws_client2_data.len, 0);
  771. ck_assert(ws_client1_data.data != NULL);
  772. ck_assert_uint_eq(ws_client1_data.len, websocket_acknowledge_msg_len);
  773. ck_assert(!memcmp(ws_client1_data.data,
  774. websocket_acknowledge_msg,
  775. websocket_acknowledge_msg_len));
  776. free(ws_client1_data.data);
  777. ws_client1_data.data = NULL;
  778. ws_client1_data.len = 0;
  779. /* Now connect a second client */
  780. #ifdef USE_IPV6
  781. ws_client2_conn =
  782. mg_connect_websocket_client("[::1]",
  783. ipv6_port,
  784. 0,
  785. ebuf,
  786. sizeof(ebuf),
  787. "/websocket",
  788. NULL,
  789. websocket_client_data_handler,
  790. websocket_client_close_handler,
  791. &ws_client2_data);
  792. #else
  793. ws_client2_conn =
  794. mg_connect_websocket_client("127.0.0.1",
  795. ipv4_port,
  796. 0,
  797. ebuf,
  798. sizeof(ebuf),
  799. "/websocket",
  800. NULL,
  801. websocket_client_data_handler,
  802. websocket_client_close_handler,
  803. &ws_client2_data);
  804. #endif
  805. ck_assert(ws_client2_conn != NULL);
  806. wait_not_null(
  807. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  808. ck_assert(ws_client1_data.closed == 0);
  809. ck_assert(ws_client2_data.closed == 0);
  810. ck_assert(ws_client1_data.data == NULL);
  811. ck_assert(ws_client1_data.len == 0);
  812. ck_assert(ws_client2_data.data != NULL);
  813. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  814. ck_assert(!memcmp(ws_client2_data.data,
  815. websocket_welcome_msg,
  816. websocket_welcome_msg_len));
  817. free(ws_client2_data.data);
  818. ws_client2_data.data = NULL;
  819. ws_client2_data.len = 0;
  820. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data2", 5);
  821. wait_not_null(
  822. &(ws_client1_data
  823. .data)); /* Wait for the websocket acknowledge message */
  824. ck_assert(ws_client1_data.closed == 0);
  825. ck_assert(ws_client2_data.closed == 0);
  826. ck_assert(ws_client2_data.data == NULL);
  827. ck_assert(ws_client2_data.len == 0);
  828. ck_assert(ws_client1_data.data != NULL);
  829. ck_assert(ws_client1_data.len == websocket_acknowledge_msg_len);
  830. ck_assert(!memcmp(ws_client1_data.data,
  831. websocket_acknowledge_msg,
  832. websocket_acknowledge_msg_len));
  833. free(ws_client1_data.data);
  834. ws_client1_data.data = NULL;
  835. ws_client1_data.len = 0;
  836. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  837. wait_not_null(
  838. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  839. ck_assert(ws_client1_data.closed == 0);
  840. ck_assert(ws_client2_data.closed == 0);
  841. ck_assert(ws_client2_data.data == NULL);
  842. ck_assert(ws_client2_data.len == 0);
  843. ck_assert(ws_client1_data.data != NULL);
  844. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  845. ck_assert(!memcmp(ws_client1_data.data,
  846. websocket_goodbye_msg,
  847. websocket_goodbye_msg_len));
  848. free(ws_client1_data.data);
  849. ws_client1_data.data = NULL;
  850. ws_client1_data.len = 0;
  851. mg_close_connection(ws_client1_conn);
  852. test_sleep(3); /* Won't get any message */
  853. ck_assert(ws_client1_data.closed == 1);
  854. ck_assert(ws_client2_data.closed == 0);
  855. ck_assert(ws_client1_data.data == NULL);
  856. ck_assert(ws_client1_data.len == 0);
  857. ck_assert(ws_client2_data.data == NULL);
  858. ck_assert(ws_client2_data.len == 0);
  859. mg_websocket_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  860. wait_not_null(
  861. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  862. ck_assert(ws_client1_data.closed == 1);
  863. ck_assert(ws_client2_data.closed == 0);
  864. ck_assert(ws_client1_data.data == NULL);
  865. ck_assert(ws_client1_data.len == 0);
  866. ck_assert(ws_client2_data.data != NULL);
  867. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  868. ck_assert(!memcmp(ws_client2_data.data,
  869. websocket_goodbye_msg,
  870. websocket_goodbye_msg_len));
  871. free(ws_client2_data.data);
  872. ws_client2_data.data = NULL;
  873. ws_client2_data.len = 0;
  874. mg_close_connection(ws_client2_conn);
  875. test_sleep(3); /* Won't get any message */
  876. ck_assert(ws_client1_data.closed == 1);
  877. ck_assert(ws_client2_data.closed == 1);
  878. ck_assert(ws_client1_data.data == NULL);
  879. ck_assert(ws_client1_data.len == 0);
  880. ck_assert(ws_client2_data.data == NULL);
  881. ck_assert(ws_client2_data.len == 0);
  882. /* Connect client 3 */
  883. ws_client3_conn =
  884. mg_connect_websocket_client("localhost",
  885. #if defined(NO_SSL)
  886. ipv4_port,
  887. 0,
  888. #else
  889. ipv4s_port,
  890. 1,
  891. #endif
  892. ebuf,
  893. sizeof(ebuf),
  894. "/websocket",
  895. NULL,
  896. websocket_client_data_handler,
  897. websocket_client_close_handler,
  898. &ws_client3_data);
  899. ck_assert(ws_client3_conn != NULL);
  900. wait_not_null(
  901. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  902. ck_assert(ws_client1_data.closed == 1);
  903. ck_assert(ws_client2_data.closed == 1);
  904. ck_assert(ws_client3_data.closed == 0);
  905. ck_assert(ws_client1_data.data == NULL);
  906. ck_assert(ws_client1_data.len == 0);
  907. ck_assert(ws_client2_data.data == NULL);
  908. ck_assert(ws_client2_data.len == 0);
  909. ck_assert(ws_client3_data.data != NULL);
  910. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  911. ck_assert(!memcmp(ws_client3_data.data,
  912. websocket_welcome_msg,
  913. websocket_welcome_msg_len));
  914. free(ws_client3_data.data);
  915. ws_client3_data.data = NULL;
  916. ws_client3_data.len = 0;
  917. #endif
  918. /* Close the server */
  919. g_ctx = NULL;
  920. mg_stop(ctx);
  921. #ifdef USE_WEBSOCKET
  922. for (i = 0; i < 100; i++) {
  923. test_sleep(1);
  924. if (ws_client3_data.closed != 0) {
  925. break;
  926. }
  927. }
  928. ck_assert_int_eq(ws_client3_data.closed, 1);
  929. #endif
  930. }
  931. END_TEST
  932. Suite *make_public_server_suite(void)
  933. {
  934. Suite *const suite = suite_create("PublicServer");
  935. TCase *const checktestenv = tcase_create("Check test environment");
  936. TCase *const startthreads = tcase_create("Start threads");
  937. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  938. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  939. TCase *const serverrequests = tcase_create("Server Requests");
  940. tcase_add_test(checktestenv, test_the_test_environment);
  941. suite_add_tcase(suite, checktestenv);
  942. tcase_add_test(startthreads, test_threading);
  943. suite_add_tcase(suite, startthreads);
  944. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  945. tcase_set_timeout(startstophttp, 10);
  946. suite_add_tcase(suite, startstophttp);
  947. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  948. tcase_set_timeout(startstophttps, 10);
  949. suite_add_tcase(suite, startstophttps);
  950. tcase_add_test(serverrequests, test_request_handlers);
  951. tcase_set_timeout(serverrequests, 120);
  952. suite_add_tcase(suite, serverrequests);
  953. return suite;
  954. }
  955. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  956. /* Used to debug test cases without using the check framework */
  957. static int chk_ok = 0;
  958. static int chk_failed = 0;
  959. void main(void)
  960. {
  961. test_the_test_environment(0);
  962. test_threading(0);
  963. test_mg_start_stop_http_server(0);
  964. test_mg_start_stop_https_server(0);
  965. test_request_handlers(0);
  966. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  967. }
  968. void _ck_assert_failed(const char *file, int line, const char *expr, ...)
  969. {
  970. va_list va;
  971. va_start(va, expr);
  972. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  973. vfprintf(stderr, expr, va);
  974. fprintf(stderr, "\n\n");
  975. va_end(va);
  976. chk_failed++;
  977. }
  978. void _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  979. {
  980. va_list va;
  981. if (cond) {
  982. chk_ok++;
  983. return;
  984. }
  985. va_start(va, expr);
  986. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  987. vfprintf(stderr, expr, va);
  988. fprintf(stderr, "\n\n");
  989. va_end(va);
  990. chk_failed++;
  991. }
  992. void _mark_point(const char *file, int line) { chk_ok++; }
  993. void tcase_fn_start(const char *fname, const char *file, int line) {}
  994. void suite_add_tcase(Suite *s, TCase *tc){};
  995. void _tcase_add_test(TCase *tc,
  996. TFun tf,
  997. const char *fname,
  998. int _signal,
  999. int allowed_exit_value,
  1000. int start,
  1001. int end){};
  1002. TCase *tcase_create(const char *name) { return NULL; };
  1003. Suite *suite_create(const char *name) { return NULL; };
  1004. void tcase_set_timeout(TCase *tc, double timeout){};
  1005. #endif