public_server.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. 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. 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. static const char *websocket_welcome_msg = "websocket welcome\n";
  245. static const size_t websocket_welcome_msg_len =
  246. 18 /* strlen(websocket_welcome_msg) */;
  247. static const char *websocket_acknowledge_msg = "websocket msg ok\n";
  248. static const size_t websocket_acknowledge_msg_len =
  249. 17 /* strlen(websocket_acknowledge_msg) */;
  250. static const char *websocket_goodbye_msg = "websocket bye\n";
  251. static const size_t websocket_goodbye_msg_len =
  252. 14 /* strlen(websocket_goodbye_msg) */;
  253. static int websock_server_connect(const struct mg_connection *conn, void *udata)
  254. {
  255. (void)conn;
  256. ck_assert_int_eq((intmax_t)(void *)udata, 7531);
  257. printf("Server: Websocket connected\n");
  258. return 0; /* return 0 to accept every connection */
  259. }
  260. static void websock_server_ready(struct mg_connection *conn, void *udata)
  261. {
  262. ck_assert_int_eq((intmax_t)(void *)udata, 7531);
  263. printf("Server: Websocket ready\n");
  264. /* Send websocket welcome message */
  265. mg_lock_connection(conn);
  266. mg_websocket_write(conn,
  267. WEBSOCKET_OPCODE_TEXT,
  268. websocket_welcome_msg,
  269. websocket_welcome_msg_len);
  270. mg_unlock_connection(conn);
  271. }
  272. static int websock_server_data(struct mg_connection *conn,
  273. int bits,
  274. char *data,
  275. size_t data_len,
  276. void *udata)
  277. {
  278. (void)bits;
  279. ck_assert_int_eq((intmax_t)(void *)udata, 7531);
  280. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  281. if (data_len < 3 || 0 != memcmp(data, "bye", 3)) {
  282. /* Send websocket acknowledge message */
  283. mg_lock_connection(conn);
  284. mg_websocket_write(conn,
  285. WEBSOCKET_OPCODE_TEXT,
  286. websocket_acknowledge_msg,
  287. websocket_acknowledge_msg_len);
  288. mg_unlock_connection(conn);
  289. } else {
  290. /* Send websocket acknowledge message */
  291. mg_lock_connection(conn);
  292. mg_websocket_write(conn,
  293. WEBSOCKET_OPCODE_TEXT,
  294. websocket_goodbye_msg,
  295. websocket_goodbye_msg_len);
  296. mg_unlock_connection(conn);
  297. }
  298. return 1; /* return 1 to keep the connetion open */
  299. }
  300. static void websock_server_close(const struct mg_connection *conn, void *udata)
  301. {
  302. (void)conn;
  303. ck_assert_int_eq((intmax_t)(void *)udata, 7531);
  304. printf("Server: Close connection\n");
  305. /* Can not send a websocket goodbye message here - the connection is already
  306. * closed */
  307. }
  308. /****************************************************************************/
  309. /* WEBSOCKET CLIENT */
  310. /****************************************************************************/
  311. struct tclient_data {
  312. void *data;
  313. size_t len;
  314. int closed;
  315. };
  316. static int websocket_client_data_handler(struct mg_connection *conn,
  317. int flags,
  318. char *data,
  319. size_t data_len,
  320. void *user_data)
  321. {
  322. struct mg_context *ctx = mg_get_context(conn);
  323. struct tclient_data *pclient_data =
  324. (struct tclient_data *)mg_get_user_data(ctx);
  325. (void)user_data; /* TODO: check this */
  326. ck_assert(pclient_data != NULL);
  327. ck_assert_int_eq(flags, (int)(128 | 1));
  328. printf("Client received data from server: ");
  329. fwrite(data, 1, data_len, stdout);
  330. printf("\n");
  331. pclient_data->data = malloc(data_len);
  332. ck_assert(pclient_data->data != NULL);
  333. memcpy(pclient_data->data, data, data_len);
  334. pclient_data->len = data_len;
  335. return 1;
  336. }
  337. static void websocket_client_close_handler(const struct mg_connection *conn,
  338. void *user_data)
  339. {
  340. struct mg_context *ctx = mg_get_context(conn);
  341. struct tclient_data *pclient_data =
  342. (struct tclient_data *)mg_get_user_data(ctx);
  343. (void)user_data; /* TODO: check this */
  344. ck_assert(pclient_data != NULL);
  345. printf("Client: Close handler\n");
  346. pclient_data->closed++;
  347. }
  348. #endif
  349. START_TEST(test_request_handlers)
  350. {
  351. char ebuf[100];
  352. struct mg_context *ctx;
  353. struct mg_connection *client_conn;
  354. const struct mg_request_info *ri;
  355. char uri[64];
  356. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  357. const char *expected =
  358. "112123123412345123456123456712345678123456789123456789A";
  359. int i;
  360. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  361. #if defined(USE_IPV6) && defined(NO_SSL)
  362. const char *HTTP_PORT = "8084,[::]:8086";
  363. short ipv4_port = 8084;
  364. short ipv6_port = 8086;
  365. #elif !defined(USE_IPV6) && defined(NO_SSL)
  366. const char *HTTP_PORT = "8084";
  367. short ipv4_port = 8084;
  368. #elif defined(USE_IPV6) && !defined(NO_SSL)
  369. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  370. short ipv4_port = 8084;
  371. short ipv4s_port = 8094;
  372. short ipv4r_port = 8194;
  373. short ipv6_port = 8086;
  374. short ipv6s_port = 8096;
  375. short ipv6r_port = 8196;
  376. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  377. const char *HTTP_PORT = "8084,8194r,8094s";
  378. short ipv4_port = 8084;
  379. short ipv4s_port = 8094;
  380. short ipv4r_port = 8194;
  381. #endif
  382. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  383. const char *opt;
  384. FILE *f;
  385. int opt_idx = 0;
  386. const char *ssl_cert = locate_ssl_cert();
  387. #ifdef USE_WEBSOCKET
  388. struct tclient_data ws_client1_data = {NULL, 0, 0};
  389. struct tclient_data ws_client2_data = {NULL, 0, 0};
  390. struct tclient_data ws_client3_data = {NULL, 0, 0};
  391. struct mg_connection *ws_client1_conn = NULL;
  392. struct mg_connection *ws_client2_conn = NULL;
  393. struct mg_connection *ws_client3_conn = NULL;
  394. #endif
  395. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  396. OPTIONS[opt_idx++] = "listening_ports";
  397. OPTIONS[opt_idx++] = HTTP_PORT;
  398. #if !defined(NO_FILES)
  399. OPTIONS[opt_idx++] = "document_root";
  400. OPTIONS[opt_idx++] = ".";
  401. #endif
  402. #ifndef NO_SSL
  403. ck_assert(ssl_cert != NULL);
  404. OPTIONS[opt_idx++] = "ssl_certificate";
  405. OPTIONS[opt_idx++] = ssl_cert;
  406. #endif
  407. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  408. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  409. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  410. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  411. ck_assert(ctx != NULL);
  412. g_ctx = ctx;
  413. opt = mg_get_option(ctx, "listening_ports");
  414. ck_assert_str_eq(opt, HTTP_PORT);
  415. opt = mg_get_option(ctx, "cgi_environment");
  416. ck_assert_str_eq(opt, "");
  417. opt = mg_get_option(ctx, "unknown_option_name");
  418. ck_assert(opt == NULL);
  419. for (i = 0; i < 1000; i++) {
  420. sprintf(uri, "/U%u", i);
  421. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  422. }
  423. for (i = 500; i < 800; i++) {
  424. sprintf(uri, "/U%u", i);
  425. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  426. }
  427. for (i = 600; i >= 0; i--) {
  428. sprintf(uri, "/U%u", i);
  429. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  430. }
  431. for (i = 750; i <= 1000; i++) {
  432. sprintf(uri, "/U%u", i);
  433. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  434. }
  435. for (i = 5; i < 9; i++) {
  436. sprintf(uri, "/U%u", i);
  437. mg_set_request_handler(
  438. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  439. }
  440. #ifdef USE_WEBSOCKET
  441. mg_set_websocket_handler(ctx,
  442. "/websocket",
  443. websock_server_connect,
  444. websock_server_ready,
  445. websock_server_data,
  446. websock_server_close,
  447. (void *)7531);
  448. #endif
  449. /* Try to load non existing file */
  450. client_conn = mg_download("localhost",
  451. ipv4_port,
  452. 0,
  453. ebuf,
  454. sizeof(ebuf),
  455. "%s",
  456. "GET /file/not/found HTTP/1.0\r\n\r\n");
  457. ck_assert(client_conn != NULL);
  458. ri = mg_get_request_info(client_conn);
  459. ck_assert(ri != NULL);
  460. ck_assert_str_eq(ri->uri, "404");
  461. mg_close_connection(client_conn);
  462. /* Get data from callback */
  463. client_conn = mg_download(
  464. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  465. ck_assert(client_conn != NULL);
  466. ri = mg_get_request_info(client_conn);
  467. ck_assert(ri != NULL);
  468. ck_assert_str_eq(ri->uri, "200");
  469. i = mg_read(client_conn, buf, sizeof(buf));
  470. ck_assert_int_eq(i, (int)strlen(expected));
  471. buf[i] = 0;
  472. ck_assert_str_eq(buf, expected);
  473. mg_close_connection(client_conn);
  474. /* Get data from callback using http://127.0.0.1 */
  475. client_conn = mg_download(
  476. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  477. ck_assert(client_conn != NULL);
  478. ri = mg_get_request_info(client_conn);
  479. ck_assert(ri != NULL);
  480. ck_assert_str_eq(ri->uri, "200");
  481. i = mg_read(client_conn, buf, sizeof(buf));
  482. ck_assert_int_eq(i, (int)strlen(expected));
  483. buf[i] = 0;
  484. ck_assert_str_eq(buf, expected);
  485. mg_close_connection(client_conn);
  486. #if defined(USE_IPV6)
  487. /* Get data from callback using http://[::1] */
  488. client_conn =
  489. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  490. ck_assert(client_conn != NULL);
  491. ri = mg_get_request_info(client_conn);
  492. ck_assert(ri != NULL);
  493. ck_assert_str_eq(ri->uri, "200");
  494. i = mg_read(client_conn, buf, sizeof(buf));
  495. ck_assert_int_eq(i, (int)strlen(expected));
  496. buf[i] = 0;
  497. ck_assert_str_eq(buf, expected);
  498. mg_close_connection(client_conn);
  499. #endif
  500. #if !defined(NO_SSL)
  501. /* Get data from callback using https://127.0.0.1 */
  502. client_conn = mg_download(
  503. "127.0.0.1", ipv4s_port, 1, 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. /* Get redirect from callback using http://127.0.0.1 */
  514. client_conn = mg_download(
  515. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  516. ck_assert(client_conn != NULL);
  517. ri = mg_get_request_info(client_conn);
  518. ck_assert(ri != NULL);
  519. ck_assert_str_eq(ri->uri, "302");
  520. i = mg_read(client_conn, buf, sizeof(buf));
  521. ck_assert_int_eq(i, -1);
  522. mg_close_connection(client_conn);
  523. #endif
  524. #if defined(USE_IPV6) && !defined(NO_SSL)
  525. /* Get data from callback using https://[::1] */
  526. client_conn =
  527. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  528. ck_assert(client_conn != NULL);
  529. ri = mg_get_request_info(client_conn);
  530. ck_assert(ri != NULL);
  531. ck_assert_str_eq(ri->uri, "200");
  532. i = mg_read(client_conn, buf, sizeof(buf));
  533. ck_assert_int_eq(i, (int)strlen(expected));
  534. buf[i] = 0;
  535. ck_assert_str_eq(buf, expected);
  536. mg_close_connection(client_conn);
  537. /* Get redirect from callback using http://127.0.0.1 */
  538. client_conn =
  539. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  540. ck_assert(client_conn != NULL);
  541. ri = mg_get_request_info(client_conn);
  542. ck_assert(ri != NULL);
  543. ck_assert_str_eq(ri->uri, "302");
  544. i = mg_read(client_conn, buf, sizeof(buf));
  545. ck_assert_int_eq(i, -1);
  546. mg_close_connection(client_conn);
  547. #endif
  548. /* It seems to be impossible to find out what the actual working
  549. * directory of the CI test environment is. Before breaking another
  550. * dozen of builds by trying blindly with different paths, just
  551. * create the file here */
  552. #ifdef _WIN32
  553. f = fopen("test.txt", "wb");
  554. #else
  555. f = fopen("test.txt", "w");
  556. #endif
  557. fwrite("simple text file\n", 17, 1, f);
  558. fclose(f);
  559. /* Get static data */
  560. client_conn = mg_download("localhost",
  561. ipv4_port,
  562. 0,
  563. ebuf,
  564. sizeof(ebuf),
  565. "%s",
  566. "GET /test.txt HTTP/1.0\r\n\r\n");
  567. ck_assert(client_conn != NULL);
  568. ri = mg_get_request_info(client_conn);
  569. ck_assert(ri != NULL);
  570. #if defined(NO_FILES)
  571. ck_assert_str_eq(ri->uri, "404");
  572. #else
  573. ck_assert_str_eq(ri->uri, "200");
  574. i = mg_read(client_conn, buf, sizeof(buf));
  575. ck_assert_int_eq(i, 17);
  576. if ((i >= 0) && (i < (int)sizeof(buf))) {
  577. buf[i] = 0;
  578. }
  579. ck_assert_str_eq(buf, "simple text file\n");
  580. #endif
  581. mg_close_connection(client_conn);
  582. /* Get directory listing */
  583. client_conn = mg_download("localhost",
  584. ipv4_port,
  585. 0,
  586. ebuf,
  587. sizeof(ebuf),
  588. "%s",
  589. "GET / HTTP/1.0\r\n\r\n");
  590. ck_assert(client_conn != NULL);
  591. ri = mg_get_request_info(client_conn);
  592. ck_assert(ri != NULL);
  593. #if defined(NO_FILES)
  594. ck_assert_str_eq(ri->uri, "404");
  595. #else
  596. ck_assert_str_eq(ri->uri, "200");
  597. i = mg_read(client_conn, buf, sizeof(buf));
  598. ck_assert(i > 6);
  599. buf[6] = 0;
  600. ck_assert_str_eq(buf, "<html>");
  601. #endif
  602. mg_close_connection(client_conn);
  603. /* POST to static file (will not work) */
  604. client_conn = mg_download("localhost",
  605. ipv4_port,
  606. 0,
  607. ebuf,
  608. sizeof(ebuf),
  609. "%s",
  610. "POST /test.txt HTTP/1.0\r\n\r\n");
  611. ck_assert(client_conn != NULL);
  612. ri = mg_get_request_info(client_conn);
  613. ck_assert(ri != NULL);
  614. #if defined(NO_FILES)
  615. ck_assert_str_eq(ri->uri, "404");
  616. #else
  617. ck_assert_str_eq(ri->uri, "405");
  618. i = mg_read(client_conn, buf, sizeof(buf));
  619. ck_assert(i >= 29);
  620. buf[29] = 0;
  621. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  622. #endif
  623. mg_close_connection(client_conn);
  624. /* PUT to static file (will not work) */
  625. client_conn = mg_download("localhost",
  626. ipv4_port,
  627. 0,
  628. ebuf,
  629. sizeof(ebuf),
  630. "%s",
  631. "PUT /test.txt HTTP/1.0\r\n\r\n");
  632. ck_assert(client_conn != NULL);
  633. ri = mg_get_request_info(client_conn);
  634. ck_assert(ri != NULL);
  635. #if defined(NO_FILES)
  636. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  637. #else
  638. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  639. #endif
  640. mg_close_connection(client_conn);
  641. /* Websocket test */
  642. #ifdef USE_WEBSOCKET
  643. /* Then connect a first client */
  644. ws_client1_conn =
  645. mg_connect_websocket_client("localhost",
  646. ipv4_port,
  647. 0,
  648. ebuf,
  649. sizeof(ebuf),
  650. "/websocket",
  651. NULL,
  652. websocket_client_data_handler,
  653. websocket_client_close_handler,
  654. &ws_client1_data);
  655. ck_assert(ws_client1_conn != NULL);
  656. mg_Sleep(3); /* Should get the websocket welcome message */
  657. ck_assert_int_eq(ws_client1_data.closed, 0);
  658. ck_assert_int_eq(ws_client2_data.closed, 0);
  659. ck_assert_int_eq(ws_client3_data.closed, 0);
  660. ck_assert(ws_client2_data.data == NULL);
  661. ck_assert_uint_eq(ws_client2_data.len, 0);
  662. ck_assert(ws_client1_data.data != NULL);
  663. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  664. ck_assert(!memcmp(ws_client1_data.data,
  665. websocket_welcome_msg,
  666. websocket_welcome_msg_len));
  667. free(ws_client1_data.data);
  668. ws_client1_data.data = NULL;
  669. ws_client1_data.len = 0;
  670. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data1", 5);
  671. mg_Sleep(3); /* Should get the acknowledge message */
  672. ck_assert_int_eq(ws_client1_data.closed, 0);
  673. ck_assert_int_eq(ws_client2_data.closed, 0);
  674. ck_assert(ws_client2_data.data == NULL);
  675. ck_assert_uint_eq(ws_client2_data.len, 0);
  676. ck_assert(ws_client1_data.data != NULL);
  677. ck_assert_uint_eq(ws_client1_data.len, websocket_acknowledge_msg_len);
  678. ck_assert(!memcmp(ws_client1_data.data,
  679. websocket_acknowledge_msg,
  680. websocket_acknowledge_msg_len));
  681. free(ws_client1_data.data);
  682. ws_client1_data.data = NULL;
  683. ws_client1_data.len = 0;
  684. /* Now connect a second client */
  685. #ifdef USE_IPV6
  686. ws_client2_conn =
  687. mg_connect_websocket_client("[::1]",
  688. ipv6_port,
  689. 0,
  690. ebuf,
  691. sizeof(ebuf),
  692. "/websocket",
  693. NULL,
  694. websocket_client_data_handler,
  695. websocket_client_close_handler,
  696. &ws_client2_data);
  697. #else
  698. ws_client2_conn =
  699. mg_connect_websocket_client("127.0.0.1",
  700. ipv4_port,
  701. 0,
  702. ebuf,
  703. sizeof(ebuf),
  704. "/websocket",
  705. NULL,
  706. websocket_client_data_handler,
  707. websocket_client_close_handler,
  708. &ws_client2_data);
  709. #endif
  710. ck_assert(ws_client2_conn != NULL);
  711. mg_Sleep(3); /* Client 2 should get the websocket welcome message */
  712. ck_assert(ws_client1_data.closed == 0);
  713. ck_assert(ws_client2_data.closed == 0);
  714. ck_assert(ws_client1_data.data == NULL);
  715. ck_assert(ws_client1_data.len == 0);
  716. ck_assert(ws_client2_data.data != NULL);
  717. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  718. ck_assert(!memcmp(ws_client2_data.data,
  719. websocket_welcome_msg,
  720. websocket_welcome_msg_len));
  721. free(ws_client2_data.data);
  722. ws_client2_data.data = NULL;
  723. ws_client2_data.len = 0;
  724. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data2", 5);
  725. mg_Sleep(3); /* Should get the acknowledge message */
  726. ck_assert(ws_client1_data.closed == 0);
  727. ck_assert(ws_client2_data.closed == 0);
  728. ck_assert(ws_client2_data.data == NULL);
  729. ck_assert(ws_client2_data.len == 0);
  730. ck_assert(ws_client1_data.data != NULL);
  731. ck_assert(ws_client1_data.len == websocket_acknowledge_msg_len);
  732. ck_assert(!memcmp(ws_client1_data.data,
  733. websocket_acknowledge_msg,
  734. websocket_acknowledge_msg_len));
  735. free(ws_client1_data.data);
  736. ws_client1_data.data = NULL;
  737. ws_client1_data.len = 0;
  738. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  739. mg_Sleep(3); /* Should get the goodbye message */
  740. ck_assert(ws_client1_data.closed == 0);
  741. ck_assert(ws_client2_data.closed == 0);
  742. ck_assert(ws_client2_data.data == NULL);
  743. ck_assert(ws_client2_data.len == 0);
  744. ck_assert(ws_client1_data.data != NULL);
  745. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  746. ck_assert(!memcmp(ws_client1_data.data,
  747. websocket_goodbye_msg,
  748. websocket_goodbye_msg_len));
  749. free(ws_client1_data.data);
  750. ws_client1_data.data = NULL;
  751. ws_client1_data.len = 0;
  752. mg_close_connection(ws_client1_conn);
  753. mg_Sleep(3); /* Won't get any message */
  754. ck_assert(ws_client1_data.closed == 1);
  755. ck_assert(ws_client2_data.closed == 0);
  756. ck_assert(ws_client1_data.data == NULL);
  757. ck_assert(ws_client1_data.len == 0);
  758. ck_assert(ws_client2_data.data == NULL);
  759. ck_assert(ws_client2_data.len == 0);
  760. mg_websocket_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  761. mg_Sleep(3); /* Should get the goodbye message */
  762. ck_assert(ws_client1_data.closed == 1);
  763. ck_assert(ws_client2_data.closed == 0);
  764. ck_assert(ws_client1_data.data == NULL);
  765. ck_assert(ws_client1_data.len == 0);
  766. ck_assert(ws_client2_data.data != NULL);
  767. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  768. ck_assert(!memcmp(ws_client2_data.data,
  769. websocket_goodbye_msg,
  770. websocket_goodbye_msg_len));
  771. free(ws_client2_data.data);
  772. ws_client2_data.data = NULL;
  773. ws_client2_data.len = 0;
  774. mg_close_connection(ws_client2_conn);
  775. mg_Sleep(3); /* Won't get any message */
  776. ck_assert(ws_client1_data.closed == 1);
  777. ck_assert(ws_client2_data.closed == 1);
  778. ck_assert(ws_client1_data.data == NULL);
  779. ck_assert(ws_client1_data.len == 0);
  780. ck_assert(ws_client2_data.data == NULL);
  781. ck_assert(ws_client2_data.len == 0);
  782. /* Connect client 3 */
  783. ws_client3_conn =
  784. mg_connect_websocket_client("localhost",
  785. ipv4_port,
  786. 0,
  787. ebuf,
  788. sizeof(ebuf),
  789. "/websocket",
  790. NULL,
  791. websocket_client_data_handler,
  792. websocket_client_close_handler,
  793. &ws_client3_data);
  794. ck_assert(ws_client3_conn != NULL);
  795. mg_Sleep(3); /* Client 3 should get the websocket welcome message */
  796. ck_assert(ws_client1_data.closed == 1);
  797. ck_assert(ws_client2_data.closed == 1);
  798. ck_assert(ws_client3_data.closed == 0);
  799. ck_assert(ws_client1_data.data == NULL);
  800. ck_assert(ws_client1_data.len == 0);
  801. ck_assert(ws_client2_data.data == NULL);
  802. ck_assert(ws_client2_data.len == 0);
  803. ck_assert(ws_client3_data.data != NULL);
  804. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  805. ck_assert(!memcmp(ws_client3_data.data,
  806. websocket_welcome_msg,
  807. websocket_welcome_msg_len));
  808. free(ws_client3_data.data);
  809. ws_client3_data.data = NULL;
  810. ws_client3_data.len = 0;
  811. #endif
  812. /* Close the server */
  813. g_ctx = NULL;
  814. mg_stop(ctx);
  815. mg_Sleep(30);
  816. ck_assert_int_eq(ws_client3_data.closed, 1);
  817. }
  818. END_TEST
  819. Suite *make_public_server_suite(void)
  820. {
  821. Suite *const suite = suite_create("PublicServer");
  822. TCase *const checktestenv = tcase_create("Check test environment");
  823. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  824. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  825. TCase *const serverrequests = tcase_create("Server Requests");
  826. tcase_add_test(checktestenv, test_the_test_environment);
  827. suite_add_tcase(suite, checktestenv);
  828. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  829. suite_add_tcase(suite, startstophttp);
  830. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  831. suite_add_tcase(suite, startstophttps);
  832. tcase_add_test(serverrequests, test_request_handlers);
  833. suite_add_tcase(suite, serverrequests);
  834. return suite;
  835. }
  836. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  837. /* Used to debug test cases without using the check framework */
  838. static int chk_ok = 0;
  839. static int chk_failed = 0;
  840. void main(void)
  841. {
  842. test_the_test_environment(0);
  843. test_mg_start_stop_http_server(0);
  844. test_mg_start_stop_https_server(0);
  845. test_request_handlers(0);
  846. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  847. }
  848. void _ck_assert_failed(const char *file, int line, const char *expr, ...)
  849. {
  850. va_list va;
  851. va_start(va, expr);
  852. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  853. vfprintf(stderr, expr, va);
  854. fprintf(stderr, "\n\n");
  855. va_end(va);
  856. chk_failed++;
  857. }
  858. void _mark_point(const char *file, int line) { chk_ok++; }
  859. void tcase_fn_start(const char *fname, const char *file, int line) {}
  860. void suite_add_tcase(Suite *s, TCase *tc){};
  861. void _tcase_add_test(TCase *tc,
  862. TFun tf,
  863. const char *fname,
  864. int _signal,
  865. int allowed_exit_value,
  866. int start,
  867. int end){};
  868. TCase *tcase_create(const char *name) { return NULL; };
  869. Suite *suite_create(const char *name) { return NULL; };
  870. #endif