public_server.c 30 KB

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