public_server.c 33 KB

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