public_server.c 33 KB

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