public_server.c 30 KB

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