public_server.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. /* Copyright (c) 2015-2016 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. #ifndef _CRT_SECURE_NO_WARNINGS
  23. #define _CRT_SECURE_NO_WARNINGS
  24. #endif
  25. #endif
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #include <time.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include "public_server.h"
  33. #include <civetweb.h>
  34. #if defined(_WIN32)
  35. #include <Windows.h>
  36. #define test_sleep(x) (Sleep((x)*1000))
  37. #else
  38. #include <unistd.h>
  39. #define test_sleep(x) (sleep(x))
  40. #endif
  41. /* This unit test file uses the excellent Check unit testing library.
  42. * The API documentation is available here:
  43. * http://check.sourceforge.net/doc/check_html/index.html
  44. */
  45. static const char *
  46. locate_resources(void)
  47. {
  48. return
  49. #ifdef _WIN32
  50. #ifdef LOCAL_TEST
  51. "resources\\";
  52. #else
  53. /* Appveyor */
  54. "..\\..\\..\\resources\\"; /* TODO: the different paths
  55. * used in the different test
  56. * system is an unsolved
  57. * problem */
  58. #endif
  59. #else
  60. #ifdef LOCAL_TEST
  61. "resources/";
  62. #else
  63. /* Travis */
  64. "../../resources/"; // TODO: fix path in CI test environment
  65. #endif
  66. #endif
  67. }
  68. static const char *
  69. locate_ssl_cert(void)
  70. {
  71. static char cert_path[256];
  72. const char *res = locate_resources();
  73. size_t l;
  74. ck_assert(res != NULL);
  75. l = strlen(res);
  76. ck_assert_uint_gt(l, 0);
  77. ck_assert_uint_lt(l, 100); /* assume there is enough space left in our
  78. typical 255 character string buffers */
  79. strcpy(cert_path, res);
  80. strcat(cert_path, "ssl_cert.pem");
  81. return cert_path;
  82. }
  83. static int
  84. wait_not_null(void *volatile *data)
  85. {
  86. int i;
  87. for (i = 0; i < 100; i++) {
  88. test_sleep(1);
  89. if (*data != NULL) {
  90. return 1;
  91. }
  92. }
  93. #if defined(__MINGW32__) || defined(__GNUC__)
  94. #pragma GCC diagnostic push
  95. #pragma GCC diagnostic ignored "-Wunreachable-code"
  96. #endif
  97. #ifdef __clang__
  98. #pragma clang diagnostic push
  99. #pragma clang diagnostic ignored "-Wunreachable-code"
  100. #endif
  101. ck_abort_msg("wait_not_null failed");
  102. return 0;
  103. #ifdef __clang__
  104. #pragma clang diagnostic pop
  105. #endif
  106. #if defined(__MINGW32__) || defined(__GNUC__)
  107. #pragma GCC diagnostic pop
  108. #endif
  109. }
  110. START_TEST(test_the_test_environment)
  111. {
  112. char wd[300];
  113. char buf[500];
  114. FILE *f;
  115. struct stat st;
  116. int ret;
  117. const char *ssl_cert = locate_ssl_cert();
  118. memset(wd, 0, sizeof(wd));
  119. memset(buf, 0, sizeof(buf));
  120. /* Get the current working directory */
  121. #ifdef _WIN32
  122. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  123. wd[sizeof(wd) - 1] = 0;
  124. #else
  125. (void)getcwd(wd, sizeof(wd));
  126. wd[sizeof(wd) - 1] = 0;
  127. #endif
  128. /* Check the pem file */
  129. #ifdef _WIN32
  130. strcpy(buf, wd);
  131. strcat(buf, "\\");
  132. strcat(buf, ssl_cert);
  133. f = fopen(buf, "rb");
  134. #else
  135. strcpy(buf, wd);
  136. strcat(buf, "/");
  137. strcat(buf, ssl_cert);
  138. f = fopen(buf, "r");
  139. #endif
  140. if (f) {
  141. fclose(f);
  142. } else {
  143. fprintf(stderr, "%s not found", buf);
  144. }
  145. /* Check the test dir */
  146. #ifdef _WIN32
  147. strcpy(buf, wd);
  148. strcat(buf, "\\test");
  149. #else
  150. strcpy(buf, wd);
  151. strcat(buf, "/test");
  152. #endif
  153. memset(&st, 0, sizeof(st));
  154. ret = stat(buf, &st);
  155. if (ret) {
  156. fprintf(stderr, "%s not found", buf);
  157. }
  158. }
  159. END_TEST
  160. static void *threading_data;
  161. static void *
  162. test_thread_func_t(void *param)
  163. {
  164. ck_assert_ptr_eq(param, &threading_data);
  165. ck_assert_ptr_eq(threading_data, NULL);
  166. threading_data = &threading_data;
  167. return NULL;
  168. }
  169. START_TEST(test_threading)
  170. {
  171. int ok;
  172. threading_data = NULL;
  173. ok = mg_start_thread(test_thread_func_t, &threading_data);
  174. ck_assert_int_eq(ok, 0);
  175. wait_not_null(&threading_data);
  176. ck_assert_ptr_eq(threading_data, &threading_data);
  177. }
  178. END_TEST
  179. static int
  180. log_msg_func(const struct mg_connection *conn, const char *message)
  181. {
  182. struct mg_context *ctx;
  183. char *ud;
  184. ck_assert(conn != NULL);
  185. ctx = mg_get_context(conn);
  186. ck_assert(ctx != NULL);
  187. ud = (char *)mg_get_user_data(ctx);
  188. strncpy(ud, message, 255);
  189. ud[255] = 0;
  190. return 1;
  191. }
  192. START_TEST(test_mg_start_stop_http_server)
  193. {
  194. struct mg_context *ctx;
  195. const char *OPTIONS[] = {
  196. #if !defined(NO_FILES)
  197. "document_root",
  198. ".",
  199. #endif
  200. "listening_ports",
  201. "8080",
  202. NULL,
  203. };
  204. size_t ports_cnt;
  205. int ports[16];
  206. int ssl[16];
  207. struct mg_callbacks callbacks;
  208. char errmsg[256];
  209. struct mg_connection *client_conn;
  210. char client_err[256];
  211. const struct mg_request_info *client_ri;
  212. int client_res;
  213. memset(ports, 0, sizeof(ports));
  214. memset(ssl, 0, sizeof(ssl));
  215. memset(&callbacks, 0, sizeof(callbacks));
  216. memset(errmsg, 0, sizeof(errmsg));
  217. callbacks.log_message = log_msg_func;
  218. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  219. mark_point();
  220. test_sleep(1);
  221. ck_assert_str_eq(errmsg, "");
  222. ck_assert(ctx != NULL);
  223. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  224. ck_assert_uint_eq(ports_cnt, 1);
  225. ck_assert_int_eq(ports[0], 8080);
  226. ck_assert_int_eq(ssl[0], 0);
  227. ck_assert_int_eq(ports[1], 0);
  228. ck_assert_int_eq(ssl[1], 0);
  229. test_sleep(1);
  230. mark_point();
  231. memset(client_err, 0, sizeof(client_err));
  232. client_conn =
  233. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  234. ck_assert(client_conn != NULL);
  235. ck_assert_str_eq(client_err, "");
  236. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  237. client_res =
  238. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  239. ck_assert_int_ge(client_res, 0);
  240. ck_assert_str_eq(client_err, "");
  241. client_ri = mg_get_request_info(client_conn);
  242. ck_assert(client_ri != NULL);
  243. #if defined(NO_FILES)
  244. ck_assert_str_eq(client_ri->uri, "404");
  245. #else
  246. ck_assert_str_eq(client_ri->uri, "200");
  247. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  248. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  249. ck_assert_int_gt(client_res, 0);
  250. ck_assert_int_le(client_res, sizeof(client_err));
  251. #endif
  252. mg_close_connection(client_conn);
  253. mark_point();
  254. test_sleep(1);
  255. mark_point();
  256. mg_stop(ctx);
  257. }
  258. END_TEST
  259. START_TEST(test_mg_start_stop_https_server)
  260. {
  261. #ifndef NO_SSL
  262. struct mg_context *ctx;
  263. size_t ports_cnt;
  264. int ports[16];
  265. int ssl[16];
  266. struct mg_callbacks callbacks;
  267. char errmsg[256];
  268. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  269. int opt_idx = 0;
  270. const char *ssl_cert = locate_ssl_cert();
  271. struct mg_connection *client_conn;
  272. char client_err[256];
  273. const struct mg_request_info *client_ri;
  274. int client_res;
  275. ck_assert(ssl_cert != NULL);
  276. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  277. #if !defined(NO_FILES)
  278. OPTIONS[opt_idx++] = "document_root";
  279. OPTIONS[opt_idx++] = ".";
  280. #endif
  281. OPTIONS[opt_idx++] = "listening_ports";
  282. OPTIONS[opt_idx++] = "8080r,8443s";
  283. OPTIONS[opt_idx++] = "ssl_certificate";
  284. OPTIONS[opt_idx++] = ssl_cert;
  285. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  286. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  287. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  288. memset(ports, 0, sizeof(ports));
  289. memset(ssl, 0, sizeof(ssl));
  290. memset(&callbacks, 0, sizeof(callbacks));
  291. memset(errmsg, 0, sizeof(errmsg));
  292. callbacks.log_message = log_msg_func;
  293. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  294. mark_point();
  295. test_sleep(1);
  296. ck_assert_str_eq(errmsg, "");
  297. ck_assert(ctx != NULL);
  298. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  299. ck_assert_uint_eq(ports_cnt, 2);
  300. ck_assert_int_eq(ports[0], 8080);
  301. ck_assert_int_eq(ssl[0], 0);
  302. ck_assert_int_eq(ports[1], 8443);
  303. ck_assert_int_eq(ssl[1], 1);
  304. ck_assert_int_eq(ports[2], 0);
  305. ck_assert_int_eq(ssl[2], 0);
  306. test_sleep(1);
  307. mark_point();
  308. memset(client_err, 0, sizeof(client_err));
  309. client_conn =
  310. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  311. ck_assert(client_conn != NULL);
  312. ck_assert_str_eq(client_err, "");
  313. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  314. client_res =
  315. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  316. ck_assert_int_ge(client_res, 0);
  317. ck_assert_str_eq(client_err, "");
  318. client_ri = mg_get_request_info(client_conn);
  319. ck_assert(client_ri != NULL);
  320. #if defined(NO_FILES)
  321. ck_assert_str_eq(client_ri->uri, "404");
  322. #else
  323. ck_assert_str_eq(client_ri->uri, "200");
  324. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  325. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  326. ck_assert_int_gt(client_res, 0);
  327. ck_assert_int_le(client_res, sizeof(client_err));
  328. #endif
  329. mg_close_connection(client_conn);
  330. mark_point();
  331. test_sleep(1);
  332. mark_point();
  333. mg_stop(ctx);
  334. #endif
  335. }
  336. END_TEST
  337. START_TEST(test_mg_server_and_client_tls)
  338. {
  339. #ifndef NO_SSL
  340. struct mg_context *ctx;
  341. int ports_cnt;
  342. struct mg_server_ports ports[16];
  343. struct mg_callbacks callbacks;
  344. char errmsg[256];
  345. struct mg_connection *client_conn;
  346. char client_err[256];
  347. const struct mg_request_info *client_ri;
  348. int client_res;
  349. struct mg_client_options client_options;
  350. const char *OPTIONS[32]; /* initializer list here is rejected by CI test */
  351. int opt_idx = 0;
  352. char server_cert[256];
  353. char client_cert[256];
  354. const char *res_dir = locate_resources();
  355. ck_assert(res_dir != NULL);
  356. strcpy(server_cert, res_dir);
  357. strcpy(client_cert, res_dir);
  358. #ifdef _WIN32
  359. strcat(server_cert, "cert\\server.pem");
  360. strcat(client_cert, "cert\\client.pem");
  361. #else
  362. strcat(server_cert, "cert/server.pem");
  363. strcat(client_cert, "cert/client.pem");
  364. #endif
  365. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  366. #if !defined(NO_FILES)
  367. OPTIONS[opt_idx++] = "document_root";
  368. OPTIONS[opt_idx++] = ".";
  369. #endif
  370. OPTIONS[opt_idx++] = "listening_ports";
  371. OPTIONS[opt_idx++] = "8080r,8443s";
  372. OPTIONS[opt_idx++] = "ssl_certificate";
  373. OPTIONS[opt_idx++] = server_cert;
  374. OPTIONS[opt_idx++] = "ssl_verify_peer";
  375. OPTIONS[opt_idx++] = "yes";
  376. OPTIONS[opt_idx++] = "ssl_ca_file";
  377. OPTIONS[opt_idx++] = client_cert;
  378. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  379. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  380. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  381. memset(ports, 0, sizeof(ports));
  382. memset(&callbacks, 0, sizeof(callbacks));
  383. memset(errmsg, 0, sizeof(errmsg));
  384. callbacks.log_message = log_msg_func;
  385. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  386. mark_point();
  387. test_sleep(1);
  388. ck_assert_str_eq(errmsg, "");
  389. ck_assert(ctx != NULL);
  390. ports_cnt = mg_get_server_ports(ctx, 16, ports);
  391. ck_assert_int_eq(ports_cnt, 2);
  392. ck_assert_int_eq(ports[0].protocol, 1);
  393. ck_assert_int_eq(ports[0].port, 8080);
  394. ck_assert_int_eq(ports[0].is_ssl, 0);
  395. ck_assert_int_eq(ports[0].is_redirect, 1);
  396. ck_assert_int_eq(ports[1].protocol, 1);
  397. ck_assert_int_eq(ports[1].port, 8443);
  398. ck_assert_int_eq(ports[1].is_ssl, 1);
  399. ck_assert_int_eq(ports[1].is_redirect, 0);
  400. ck_assert_int_eq(ports[2].protocol, 0);
  401. ck_assert_int_eq(ports[2].port, 0);
  402. ck_assert_int_eq(ports[2].is_ssl, 0);
  403. ck_assert_int_eq(ports[2].is_redirect, 0);
  404. test_sleep(1);
  405. mark_point();
  406. memset(client_err, 0, sizeof(client_err));
  407. client_conn =
  408. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  409. ck_assert(client_conn == NULL);
  410. ck_assert_str_ne(client_err, "");
  411. memset(client_err, 0, sizeof(client_err));
  412. memset(&client_options, 0, sizeof(client_options));
  413. client_options.host = "127.0.0.1";
  414. client_options.port = 8443;
  415. client_options.client_cert = client_cert;
  416. client_options.server_cert = server_cert;
  417. client_conn = mg_connect_client_secure(&client_options,
  418. client_err,
  419. sizeof(client_err));
  420. ck_assert(client_conn != NULL);
  421. ck_assert_str_eq(client_err, "");
  422. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  423. client_res =
  424. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  425. ck_assert_int_ge(client_res, 0);
  426. ck_assert_str_eq(client_err, "");
  427. client_ri = mg_get_request_info(client_conn);
  428. ck_assert(client_ri != NULL);
  429. #if defined(NO_FILES)
  430. ck_assert_str_eq(client_ri->uri, "404");
  431. #else
  432. ck_assert_str_eq(client_ri->uri, "200");
  433. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  434. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  435. ck_assert_int_gt(client_res, 0);
  436. ck_assert_int_le(client_res, sizeof(client_err));
  437. #endif
  438. mg_close_connection(client_conn);
  439. /* TODO: A client API using a client certificate is missing */
  440. mark_point();
  441. test_sleep(1);
  442. mark_point();
  443. mg_stop(ctx);
  444. #endif
  445. }
  446. END_TEST
  447. static struct mg_context *g_ctx;
  448. static int
  449. request_test_handler(struct mg_connection *conn, void *cbdata)
  450. {
  451. int i;
  452. char chunk_data[32];
  453. const struct mg_request_info *ri;
  454. struct mg_context *ctx;
  455. void *ud, *cud;
  456. ctx = mg_get_context(conn);
  457. ud = mg_get_user_data(ctx);
  458. ri = mg_get_request_info(conn);
  459. ck_assert(ri != NULL);
  460. ck_assert(ctx == g_ctx);
  461. ck_assert(ud == &g_ctx);
  462. mg_set_user_connection_data(conn, (void *)6543);
  463. cud = mg_get_user_connection_data(conn);
  464. ck_assert_ptr_eq((void *)cud, (void *)6543);
  465. ck_assert_ptr_eq((void *)cbdata, (void *)7);
  466. strcpy(chunk_data, "123456789A123456789B123456789C");
  467. mg_printf(conn,
  468. "HTTP/1.1 200 OK\r\n"
  469. "Transfer-Encoding: chunked\r\n"
  470. "Content-Type: text/plain\r\n\r\n");
  471. for (i = 1; i <= 10; i++) {
  472. mg_printf(conn, "%x\r\n", i);
  473. mg_write(conn, chunk_data, (unsigned)i);
  474. mg_printf(conn, "\r\n");
  475. }
  476. mg_printf(conn, "0\r\n\r\n");
  477. return 1;
  478. }
  479. #ifdef USE_WEBSOCKET
  480. /****************************************************************************/
  481. /* WEBSOCKET SERVER */
  482. /****************************************************************************/
  483. static const char *websocket_welcome_msg = "websocket welcome\n";
  484. static const size_t websocket_welcome_msg_len =
  485. 18 /* strlen(websocket_welcome_msg) */;
  486. static const char *websocket_goodbye_msg = "websocket bye\n";
  487. static const size_t websocket_goodbye_msg_len =
  488. 14 /* strlen(websocket_goodbye_msg) */;
  489. static int
  490. websock_server_connect(const struct mg_connection *conn, void *udata)
  491. {
  492. (void)conn;
  493. ck_assert_ptr_eq((void *)udata, (void *)7531);
  494. printf("Server: Websocket connected\n");
  495. return 0; /* return 0 to accept every connection */
  496. }
  497. static void
  498. websock_server_ready(struct mg_connection *conn, void *udata)
  499. {
  500. ck_assert_ptr_eq((void *)udata, (void *)7531);
  501. printf("Server: Websocket ready\n");
  502. /* Send websocket welcome message */
  503. mg_lock_connection(conn);
  504. mg_websocket_write(conn,
  505. WEBSOCKET_OPCODE_TEXT,
  506. websocket_welcome_msg,
  507. websocket_welcome_msg_len);
  508. mg_unlock_connection(conn);
  509. printf("Server: Websocket ready X\n");
  510. }
  511. static int
  512. websock_server_data(struct mg_connection *conn,
  513. int bits,
  514. char *data,
  515. size_t data_len,
  516. void *udata)
  517. {
  518. (void)bits;
  519. ck_assert_ptr_eq((void *)udata, (void *)7531);
  520. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  521. if (data_len == 3 && !memcmp(data, "bye", 3)) {
  522. /* Send websocket goodbye message */
  523. mg_lock_connection(conn);
  524. mg_websocket_write(conn,
  525. WEBSOCKET_OPCODE_TEXT,
  526. websocket_goodbye_msg,
  527. websocket_goodbye_msg_len);
  528. mg_unlock_connection(conn);
  529. } else if (data_len == 5 && !memcmp(data, "data1", 5)) {
  530. mg_lock_connection(conn);
  531. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok1", 3);
  532. mg_unlock_connection(conn);
  533. } else if (data_len == 5 && !memcmp(data, "data2", 5)) {
  534. mg_lock_connection(conn);
  535. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok 2", 4);
  536. mg_unlock_connection(conn);
  537. } else if (data_len == 5 && !memcmp(data, "data3", 5)) {
  538. mg_lock_connection(conn);
  539. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok - 3", 6);
  540. mg_unlock_connection(conn);
  541. } else {
  542. #if defined(__MINGW32__) || defined(__GNUC__)
  543. #pragma GCC diagnostic push
  544. #pragma GCC diagnostic ignored "-Wunreachable-code"
  545. #endif
  546. #ifdef __clang__
  547. #pragma clang diagnostic push
  548. #pragma clang diagnostic ignored "-Wunreachable-code"
  549. #endif
  550. ck_abort_msg("Got unexpected message from websocket client");
  551. return 0;
  552. #ifdef __clang__
  553. #pragma clang diagnostic pop
  554. #endif
  555. #if defined(__MINGW32__) || defined(__GNUC__)
  556. #pragma GCC diagnostic pop
  557. #endif
  558. }
  559. return 1; /* return 1 to keep the connetion open */
  560. }
  561. static void
  562. websock_server_close(const struct mg_connection *conn, void *udata)
  563. {
  564. (void)conn;
  565. ck_assert_ptr_eq((void *)udata, (void *)7531);
  566. printf("Server: Close connection\n");
  567. /* Can not send a websocket goodbye message here - the connection is already
  568. * closed */
  569. }
  570. /****************************************************************************/
  571. /* WEBSOCKET CLIENT */
  572. /****************************************************************************/
  573. struct tclient_data {
  574. void *data;
  575. size_t len;
  576. int closed;
  577. };
  578. static int
  579. websocket_client_data_handler(struct mg_connection *conn,
  580. int flags,
  581. char *data,
  582. size_t data_len,
  583. void *user_data)
  584. {
  585. struct mg_context *ctx = mg_get_context(conn);
  586. struct tclient_data *pclient_data =
  587. (struct tclient_data *)mg_get_user_data(ctx);
  588. (void)user_data; /* TODO: check this */
  589. ck_assert(pclient_data != NULL);
  590. ck_assert_int_eq(flags, (int)(128 | 1));
  591. printf("Client received data from server: ");
  592. fwrite(data, 1, data_len, stdout);
  593. printf("\n");
  594. pclient_data->data = malloc(data_len);
  595. ck_assert(pclient_data->data != NULL);
  596. memcpy(pclient_data->data, data, data_len);
  597. pclient_data->len = data_len;
  598. return 1;
  599. }
  600. static void
  601. websocket_client_close_handler(const struct mg_connection *conn,
  602. void *user_data)
  603. {
  604. struct mg_context *ctx = mg_get_context(conn);
  605. struct tclient_data *pclient_data =
  606. (struct tclient_data *)mg_get_user_data(ctx);
  607. (void)user_data; /* TODO: check this */
  608. ck_assert(pclient_data != NULL);
  609. printf("Client: Close handler\n");
  610. pclient_data->closed++;
  611. }
  612. #endif
  613. START_TEST(test_request_handlers)
  614. {
  615. char ebuf[100];
  616. struct mg_context *ctx;
  617. struct mg_connection *client_conn;
  618. const struct mg_request_info *ri;
  619. char uri[64];
  620. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  621. const char *expected =
  622. "112123123412345123456123456712345678123456789123456789A";
  623. int i;
  624. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  625. #if defined(USE_IPV6) && defined(NO_SSL)
  626. const char *HTTP_PORT = "8084,[::]:8086";
  627. short ipv4_port = 8084;
  628. short ipv6_port = 8086;
  629. #elif !defined(USE_IPV6) && defined(NO_SSL)
  630. const char *HTTP_PORT = "8084";
  631. short ipv4_port = 8084;
  632. #elif defined(USE_IPV6) && !defined(NO_SSL)
  633. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  634. short ipv4_port = 8084;
  635. short ipv4s_port = 8094;
  636. short ipv4r_port = 8194;
  637. short ipv6_port = 8086;
  638. short ipv6s_port = 8096;
  639. short ipv6r_port = 8196;
  640. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  641. const char *HTTP_PORT = "8084,8194r,8094s";
  642. short ipv4_port = 8084;
  643. short ipv4s_port = 8094;
  644. short ipv4r_port = 8194;
  645. #endif
  646. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  647. const char *opt;
  648. FILE *f;
  649. const char *plain_file_content;
  650. const char *encoded_file_content;
  651. const char *cgi_script_content;
  652. const char *expected_cgi_result;
  653. int opt_idx = 0;
  654. #if !defined(NO_SSL)
  655. const char *ssl_cert = locate_ssl_cert();
  656. #endif
  657. #if defined(USE_WEBSOCKET)
  658. struct tclient_data ws_client1_data = {NULL, 0, 0};
  659. struct tclient_data ws_client2_data = {NULL, 0, 0};
  660. struct tclient_data ws_client3_data = {NULL, 0, 0};
  661. struct mg_connection *ws_client1_conn = NULL;
  662. struct mg_connection *ws_client2_conn = NULL;
  663. struct mg_connection *ws_client3_conn = NULL;
  664. #endif
  665. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  666. OPTIONS[opt_idx++] = "listening_ports";
  667. OPTIONS[opt_idx++] = HTTP_PORT;
  668. #if !defined(NO_FILES)
  669. OPTIONS[opt_idx++] = "document_root";
  670. OPTIONS[opt_idx++] = ".";
  671. #endif
  672. #ifndef NO_SSL
  673. ck_assert(ssl_cert != NULL);
  674. OPTIONS[opt_idx++] = "ssl_certificate";
  675. OPTIONS[opt_idx++] = ssl_cert;
  676. #endif
  677. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  678. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  679. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  680. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  681. ck_assert(ctx != NULL);
  682. g_ctx = ctx;
  683. opt = mg_get_option(ctx, "listening_ports");
  684. ck_assert_str_eq(opt, HTTP_PORT);
  685. opt = mg_get_option(ctx, "cgi_environment");
  686. ck_assert_str_eq(opt, "");
  687. opt = mg_get_option(ctx, "unknown_option_name");
  688. ck_assert(opt == NULL);
  689. for (i = 0; i < 1000; i++) {
  690. sprintf(uri, "/U%u", i);
  691. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  692. }
  693. for (i = 500; i < 800; i++) {
  694. sprintf(uri, "/U%u", i);
  695. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  696. }
  697. for (i = 600; i >= 0; i--) {
  698. sprintf(uri, "/U%u", i);
  699. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  700. }
  701. for (i = 750; i <= 1000; i++) {
  702. sprintf(uri, "/U%u", i);
  703. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  704. }
  705. for (i = 5; i < 9; i++) {
  706. sprintf(uri, "/U%u", i);
  707. mg_set_request_handler(ctx,
  708. uri,
  709. request_test_handler,
  710. (void *)(ptrdiff_t)i);
  711. }
  712. #ifdef USE_WEBSOCKET
  713. mg_set_websocket_handler(ctx,
  714. "/websocket",
  715. websock_server_connect,
  716. websock_server_ready,
  717. websock_server_data,
  718. websock_server_close,
  719. (void *)7531);
  720. #endif
  721. /* Try to load non existing file */
  722. client_conn = mg_download("localhost",
  723. ipv4_port,
  724. 0,
  725. ebuf,
  726. sizeof(ebuf),
  727. "%s",
  728. "GET /file/not/found HTTP/1.0\r\n\r\n");
  729. ck_assert(client_conn != NULL);
  730. ri = mg_get_request_info(client_conn);
  731. ck_assert(ri != NULL);
  732. ck_assert_str_eq(ri->uri, "404");
  733. mg_close_connection(client_conn);
  734. /* Get data from callback */
  735. client_conn = mg_download(
  736. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  737. ck_assert(client_conn != NULL);
  738. ri = mg_get_request_info(client_conn);
  739. ck_assert(ri != NULL);
  740. ck_assert_str_eq(ri->uri, "200");
  741. i = mg_read(client_conn, buf, sizeof(buf));
  742. ck_assert_int_eq(i, (int)strlen(expected));
  743. buf[i] = 0;
  744. ck_assert_str_eq(buf, expected);
  745. mg_close_connection(client_conn);
  746. /* Get data from callback using http://127.0.0.1 */
  747. client_conn = mg_download(
  748. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  749. ck_assert(client_conn != NULL);
  750. ri = mg_get_request_info(client_conn);
  751. ck_assert(ri != NULL);
  752. ck_assert_str_eq(ri->uri, "200");
  753. i = mg_read(client_conn, buf, sizeof(buf));
  754. if ((i >= 0) && ((size_t)i < sizeof(buf))) {
  755. buf[i] = 0;
  756. } else {
  757. ck_abort_msg(
  758. "ERROR: test_request_handlers: read returned %i (>=0, <%i)",
  759. (int)i,
  760. (int)sizeof(buf));
  761. }
  762. ck_assert((int)i < (int)sizeof(buf));
  763. ck_assert(i > 0);
  764. ck_assert_int_eq(i, (int)strlen(expected));
  765. buf[i] = 0;
  766. ck_assert_str_eq(buf, expected);
  767. mg_close_connection(client_conn);
  768. #if defined(USE_IPV6)
  769. /* Get data from callback using http://[::1] */
  770. client_conn =
  771. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  772. ck_assert(client_conn != NULL);
  773. ri = mg_get_request_info(client_conn);
  774. ck_assert(ri != NULL);
  775. ck_assert_str_eq(ri->uri, "200");
  776. i = mg_read(client_conn, buf, sizeof(buf));
  777. ck_assert_int_eq(i, (int)strlen(expected));
  778. buf[i] = 0;
  779. ck_assert_str_eq(buf, expected);
  780. mg_close_connection(client_conn);
  781. #endif
  782. #if !defined(NO_SSL)
  783. /* Get data from callback using https://127.0.0.1 */
  784. client_conn = mg_download(
  785. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  786. ck_assert(client_conn != NULL);
  787. ri = mg_get_request_info(client_conn);
  788. ck_assert(ri != NULL);
  789. ck_assert_str_eq(ri->uri, "200");
  790. i = mg_read(client_conn, buf, sizeof(buf));
  791. ck_assert_int_eq(i, (int)strlen(expected));
  792. buf[i] = 0;
  793. ck_assert_str_eq(buf, expected);
  794. mg_close_connection(client_conn);
  795. /* Get redirect from callback using http://127.0.0.1 */
  796. client_conn = mg_download(
  797. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  798. ck_assert(client_conn != NULL);
  799. ri = mg_get_request_info(client_conn);
  800. ck_assert(ri != NULL);
  801. ck_assert_str_eq(ri->uri, "302");
  802. i = mg_read(client_conn, buf, sizeof(buf));
  803. ck_assert_int_eq(i, -1);
  804. mg_close_connection(client_conn);
  805. #endif
  806. #if defined(USE_IPV6) && !defined(NO_SSL)
  807. /* Get data from callback using https://[::1] */
  808. client_conn =
  809. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  810. ck_assert(client_conn != NULL);
  811. ri = mg_get_request_info(client_conn);
  812. ck_assert(ri != NULL);
  813. ck_assert_str_eq(ri->uri, "200");
  814. i = mg_read(client_conn, buf, sizeof(buf));
  815. ck_assert_int_eq(i, (int)strlen(expected));
  816. buf[i] = 0;
  817. ck_assert_str_eq(buf, expected);
  818. mg_close_connection(client_conn);
  819. /* Get redirect from callback using http://127.0.0.1 */
  820. client_conn =
  821. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  822. ck_assert(client_conn != NULL);
  823. ri = mg_get_request_info(client_conn);
  824. ck_assert(ri != NULL);
  825. ck_assert_str_eq(ri->uri, "302");
  826. i = mg_read(client_conn, buf, sizeof(buf));
  827. ck_assert_int_eq(i, -1);
  828. mg_close_connection(client_conn);
  829. #endif
  830. /* It seems to be impossible to find out what the actual working
  831. * directory of the CI test environment is. Before breaking another
  832. * dozen of builds by trying blindly with different paths, just
  833. * create the file here */
  834. #ifdef _WIN32
  835. f = fopen("test.txt", "wb");
  836. #else
  837. f = fopen("test.txt", "w");
  838. #endif
  839. plain_file_content = "simple text file\n";
  840. fwrite(plain_file_content, 17, 1, f);
  841. fclose(f);
  842. #ifdef _WIN32
  843. f = fopen("test_gz.txt.gz", "wb");
  844. #else
  845. f = fopen("test_gz.txt.gz", "w");
  846. #endif
  847. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  848. "test_gz.txt"
  849. "\x00\x01\x11\x00\xee\xff"
  850. "zipped text file"
  851. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  852. fwrite(encoded_file_content, 1, 52, f);
  853. fclose(f);
  854. #ifdef _WIN32
  855. f = fopen("test.cgi", "wb");
  856. cgi_script_content = "#!test.cgi.cmd\r\n";
  857. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  858. fclose(f);
  859. f = fopen("test.cgi.cmd", "w");
  860. cgi_script_content = "@echo off\r\n"
  861. "echo Connection: close\r\n"
  862. "echo Content-Type: text/plain\r\n"
  863. "echo.\r\n"
  864. "echo CGI test\r\n"
  865. "\r\n";
  866. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  867. fclose(f);
  868. #else
  869. f = fopen("test.cgi", "w");
  870. cgi_script_content = "#!/bin/sh\n\n"
  871. "printf \"Connection: close\\r\\n\"\n"
  872. "printf \"Content-Type: text/plain\\r\\n\"\n"
  873. "printf \"\\r\\n\"\n"
  874. "printf \"CGI test\\r\\n\"\n"
  875. "\n";
  876. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  877. fclose(f);
  878. system("chmod a+x test.cgi");
  879. #endif
  880. expected_cgi_result = "CGI test";
  881. /* Get static data */
  882. client_conn = mg_download("localhost",
  883. ipv4_port,
  884. 0,
  885. ebuf,
  886. sizeof(ebuf),
  887. "%s",
  888. "GET /test.txt HTTP/1.0\r\n\r\n");
  889. ck_assert(client_conn != NULL);
  890. ri = mg_get_request_info(client_conn);
  891. ck_assert(ri != NULL);
  892. #if defined(NO_FILES)
  893. ck_assert_str_eq(ri->uri, "404");
  894. #else
  895. ck_assert_str_eq(ri->uri, "200");
  896. i = mg_read(client_conn, buf, sizeof(buf));
  897. ck_assert_int_eq(i, 17);
  898. if ((i >= 0) && (i < (int)sizeof(buf))) {
  899. buf[i] = 0;
  900. }
  901. ck_assert_str_eq(buf, plain_file_content);
  902. #endif
  903. mg_close_connection(client_conn);
  904. /* Get zipped static data - will not work if Accept-Encoding is not set */
  905. client_conn = mg_download("localhost",
  906. ipv4_port,
  907. 0,
  908. ebuf,
  909. sizeof(ebuf),
  910. "%s",
  911. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  912. ck_assert(client_conn != NULL);
  913. ri = mg_get_request_info(client_conn);
  914. ck_assert(ri != NULL);
  915. ck_assert_str_eq(ri->uri, "404");
  916. mg_close_connection(client_conn);
  917. /* Get zipped static data - with Accept-Encoding */
  918. client_conn = mg_download(
  919. "localhost",
  920. ipv4_port,
  921. 0,
  922. ebuf,
  923. sizeof(ebuf),
  924. "%s",
  925. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  926. ck_assert(client_conn != NULL);
  927. ri = mg_get_request_info(client_conn);
  928. ck_assert(ri != NULL);
  929. #if defined(NO_FILES)
  930. ck_assert_str_eq(ri->uri, "404");
  931. #else
  932. ck_assert_str_eq(ri->uri, "200");
  933. i = mg_read(client_conn, buf, sizeof(buf));
  934. ck_assert_int_eq(i, 52);
  935. if ((i >= 0) && (i < (int)sizeof(buf))) {
  936. buf[i] = 0;
  937. }
  938. ck_assert_int_eq(ri->content_length, 52);
  939. ck_assert_str_eq(buf, encoded_file_content);
  940. #endif
  941. mg_close_connection(client_conn);
  942. /* Get CGI generated data */
  943. #if !defined(NO_CGI)
  944. client_conn = mg_download("localhost",
  945. ipv4_port,
  946. 0,
  947. ebuf,
  948. sizeof(ebuf),
  949. "%s",
  950. "GET /test.cgi HTTP/1.0\r\n\r\n");
  951. ck_assert(client_conn != NULL);
  952. ri = mg_get_request_info(client_conn);
  953. ck_assert(ri != NULL);
  954. #if defined(NO_FILES)
  955. ck_assert_str_eq(ri->uri, "404");
  956. (void)expected_cgi_result;
  957. (void)cgi_script_content;
  958. #else
  959. i = mg_read(client_conn, buf, sizeof(buf));
  960. if ((i >= 0) && (i < (int)sizeof(buf))) {
  961. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  962. i--;
  963. }
  964. buf[i] = 0;
  965. }
  966. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  967. ck_assert_str_eq(buf, expected_cgi_result);
  968. ck_assert_str_eq(ri->uri, "200");
  969. mg_close_connection(client_conn);
  970. #endif
  971. #else
  972. (void)expected_cgi_result;
  973. (void)cgi_script_content;
  974. #endif
  975. /* Get directory listing */
  976. client_conn = mg_download("localhost",
  977. ipv4_port,
  978. 0,
  979. ebuf,
  980. sizeof(ebuf),
  981. "%s",
  982. "GET / HTTP/1.0\r\n\r\n");
  983. ck_assert(client_conn != NULL);
  984. ri = mg_get_request_info(client_conn);
  985. ck_assert(ri != NULL);
  986. #if defined(NO_FILES)
  987. ck_assert_str_eq(ri->uri, "404");
  988. #else
  989. ck_assert_str_eq(ri->uri, "200");
  990. i = mg_read(client_conn, buf, sizeof(buf));
  991. ck_assert(i > 6);
  992. buf[6] = 0;
  993. ck_assert_str_eq(buf, "<html>");
  994. #endif
  995. mg_close_connection(client_conn);
  996. /* POST to static file (will not work) */
  997. client_conn = mg_download("localhost",
  998. ipv4_port,
  999. 0,
  1000. ebuf,
  1001. sizeof(ebuf),
  1002. "%s",
  1003. "POST /test.txt HTTP/1.0\r\n\r\n");
  1004. ck_assert(client_conn != NULL);
  1005. ri = mg_get_request_info(client_conn);
  1006. ck_assert(ri != NULL);
  1007. #if defined(NO_FILES)
  1008. ck_assert_str_eq(ri->uri, "404");
  1009. #else
  1010. ck_assert_str_eq(ri->uri, "405");
  1011. i = mg_read(client_conn, buf, sizeof(buf));
  1012. ck_assert(i >= 29);
  1013. buf[29] = 0;
  1014. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  1015. #endif
  1016. mg_close_connection(client_conn);
  1017. /* PUT to static file (will not work) */
  1018. client_conn = mg_download("localhost",
  1019. ipv4_port,
  1020. 0,
  1021. ebuf,
  1022. sizeof(ebuf),
  1023. "%s",
  1024. "PUT /test.txt HTTP/1.0\r\n\r\n");
  1025. ck_assert(client_conn != NULL);
  1026. ri = mg_get_request_info(client_conn);
  1027. ck_assert(ri != NULL);
  1028. #if defined(NO_FILES)
  1029. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  1030. #else
  1031. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  1032. #endif
  1033. mg_close_connection(client_conn);
  1034. /* Websocket test */
  1035. #ifdef USE_WEBSOCKET
  1036. /* Then connect a first client */
  1037. ws_client1_conn =
  1038. mg_connect_websocket_client("localhost",
  1039. ipv4_port,
  1040. 0,
  1041. ebuf,
  1042. sizeof(ebuf),
  1043. "/websocket",
  1044. NULL,
  1045. websocket_client_data_handler,
  1046. websocket_client_close_handler,
  1047. &ws_client1_data);
  1048. ck_assert(ws_client1_conn != NULL);
  1049. wait_not_null(
  1050. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1051. ck_assert_int_eq(ws_client1_data.closed, 0);
  1052. ck_assert_int_eq(ws_client2_data.closed, 0);
  1053. ck_assert_int_eq(ws_client3_data.closed, 0);
  1054. ck_assert(ws_client2_data.data == NULL);
  1055. ck_assert_uint_eq(ws_client2_data.len, 0);
  1056. ck_assert(ws_client1_data.data != NULL);
  1057. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1058. ck_assert(!memcmp(ws_client1_data.data,
  1059. websocket_welcome_msg,
  1060. websocket_welcome_msg_len));
  1061. free(ws_client1_data.data);
  1062. ws_client1_data.data = NULL;
  1063. ws_client1_data.len = 0;
  1064. mg_websocket_client_write(ws_client1_conn,
  1065. WEBSOCKET_OPCODE_TEXT,
  1066. "data1",
  1067. 5);
  1068. mark_point();
  1069. wait_not_null(
  1070. &(ws_client1_data
  1071. .data)); /* Wait for the websocket acknowledge message */
  1072. ck_assert_int_eq(ws_client1_data.closed, 0);
  1073. ck_assert_int_eq(ws_client2_data.closed, 0);
  1074. ck_assert(ws_client2_data.data == NULL);
  1075. ck_assert_uint_eq(ws_client2_data.len, 0);
  1076. ck_assert(ws_client1_data.data != NULL);
  1077. ck_assert_uint_eq(ws_client1_data.len, 3);
  1078. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1079. free(ws_client1_data.data);
  1080. ws_client1_data.data = NULL;
  1081. ws_client1_data.len = 0;
  1082. /* Now connect a second client */
  1083. #ifdef USE_IPV6
  1084. ws_client2_conn =
  1085. mg_connect_websocket_client("[::1]",
  1086. ipv6_port,
  1087. 0,
  1088. ebuf,
  1089. sizeof(ebuf),
  1090. "/websocket",
  1091. NULL,
  1092. websocket_client_data_handler,
  1093. websocket_client_close_handler,
  1094. &ws_client2_data);
  1095. #else
  1096. ws_client2_conn =
  1097. mg_connect_websocket_client("127.0.0.1",
  1098. ipv4_port,
  1099. 0,
  1100. ebuf,
  1101. sizeof(ebuf),
  1102. "/websocket",
  1103. NULL,
  1104. websocket_client_data_handler,
  1105. websocket_client_close_handler,
  1106. &ws_client2_data);
  1107. #endif
  1108. ck_assert(ws_client2_conn != NULL);
  1109. wait_not_null(
  1110. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1111. ck_assert(ws_client1_data.closed == 0);
  1112. ck_assert(ws_client2_data.closed == 0);
  1113. ck_assert(ws_client1_data.data == NULL);
  1114. ck_assert(ws_client1_data.len == 0);
  1115. ck_assert(ws_client2_data.data != NULL);
  1116. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1117. ck_assert(!memcmp(ws_client2_data.data,
  1118. websocket_welcome_msg,
  1119. websocket_welcome_msg_len));
  1120. free(ws_client2_data.data);
  1121. ws_client2_data.data = NULL;
  1122. ws_client2_data.len = 0;
  1123. mg_websocket_client_write(ws_client1_conn,
  1124. WEBSOCKET_OPCODE_TEXT,
  1125. "data2",
  1126. 5);
  1127. mark_point();
  1128. wait_not_null(
  1129. &(ws_client1_data
  1130. .data)); /* Wait for the websocket acknowledge message */
  1131. ck_assert(ws_client1_data.closed == 0);
  1132. ck_assert(ws_client2_data.closed == 0);
  1133. ck_assert(ws_client2_data.data == NULL);
  1134. ck_assert(ws_client2_data.len == 0);
  1135. ck_assert(ws_client1_data.data != NULL);
  1136. ck_assert(ws_client1_data.len == 4);
  1137. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1138. free(ws_client1_data.data);
  1139. ws_client1_data.data = NULL;
  1140. ws_client1_data.len = 0;
  1141. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1142. mark_point();
  1143. wait_not_null(
  1144. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1145. ck_assert(ws_client1_data.closed == 0);
  1146. ck_assert(ws_client2_data.closed == 0);
  1147. ck_assert(ws_client2_data.data == NULL);
  1148. ck_assert(ws_client2_data.len == 0);
  1149. ck_assert(ws_client1_data.data != NULL);
  1150. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1151. ck_assert(!memcmp(ws_client1_data.data,
  1152. websocket_goodbye_msg,
  1153. websocket_goodbye_msg_len));
  1154. free(ws_client1_data.data);
  1155. ws_client1_data.data = NULL;
  1156. ws_client1_data.len = 0;
  1157. mg_close_connection(ws_client1_conn);
  1158. mark_point();
  1159. test_sleep(3); /* Won't get any message */
  1160. ck_assert(ws_client1_data.closed == 1);
  1161. ck_assert(ws_client2_data.closed == 0);
  1162. ck_assert(ws_client1_data.data == NULL);
  1163. ck_assert(ws_client1_data.len == 0);
  1164. ck_assert(ws_client2_data.data == NULL);
  1165. ck_assert(ws_client2_data.len == 0);
  1166. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1167. mark_point();
  1168. wait_not_null(
  1169. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1170. ck_assert(ws_client1_data.closed == 1);
  1171. ck_assert(ws_client2_data.closed == 0);
  1172. ck_assert(ws_client1_data.data == NULL);
  1173. ck_assert(ws_client1_data.len == 0);
  1174. ck_assert(ws_client2_data.data != NULL);
  1175. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1176. ck_assert(!memcmp(ws_client2_data.data,
  1177. websocket_goodbye_msg,
  1178. websocket_goodbye_msg_len));
  1179. free(ws_client2_data.data);
  1180. ws_client2_data.data = NULL;
  1181. ws_client2_data.len = 0;
  1182. mg_close_connection(ws_client2_conn);
  1183. mark_point();
  1184. test_sleep(3); /* Won't get any message */
  1185. ck_assert(ws_client1_data.closed == 1);
  1186. ck_assert(ws_client2_data.closed == 1);
  1187. ck_assert(ws_client1_data.data == NULL);
  1188. ck_assert(ws_client1_data.len == 0);
  1189. ck_assert(ws_client2_data.data == NULL);
  1190. ck_assert(ws_client2_data.len == 0);
  1191. /* Connect client 3 */
  1192. ws_client3_conn =
  1193. mg_connect_websocket_client("localhost",
  1194. #if defined(NO_SSL)
  1195. ipv4_port,
  1196. 0,
  1197. #else
  1198. ipv4s_port,
  1199. 1,
  1200. #endif
  1201. ebuf,
  1202. sizeof(ebuf),
  1203. "/websocket",
  1204. NULL,
  1205. websocket_client_data_handler,
  1206. websocket_client_close_handler,
  1207. &ws_client3_data);
  1208. ck_assert(ws_client3_conn != NULL);
  1209. wait_not_null(
  1210. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1211. ck_assert(ws_client1_data.closed == 1);
  1212. ck_assert(ws_client2_data.closed == 1);
  1213. ck_assert(ws_client3_data.closed == 0);
  1214. ck_assert(ws_client1_data.data == NULL);
  1215. ck_assert(ws_client1_data.len == 0);
  1216. ck_assert(ws_client2_data.data == NULL);
  1217. ck_assert(ws_client2_data.len == 0);
  1218. ck_assert(ws_client3_data.data != NULL);
  1219. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1220. ck_assert(!memcmp(ws_client3_data.data,
  1221. websocket_welcome_msg,
  1222. websocket_welcome_msg_len));
  1223. free(ws_client3_data.data);
  1224. ws_client3_data.data = NULL;
  1225. ws_client3_data.len = 0;
  1226. #endif
  1227. /* Close the server */
  1228. g_ctx = NULL;
  1229. mg_stop(ctx);
  1230. mark_point();
  1231. #ifdef USE_WEBSOCKET
  1232. for (i = 0; i < 100; i++) {
  1233. test_sleep(1);
  1234. if (ws_client3_data.closed != 0) {
  1235. mark_point();
  1236. break;
  1237. }
  1238. }
  1239. ck_assert_int_eq(ws_client3_data.closed, 1);
  1240. #endif
  1241. }
  1242. END_TEST
  1243. Suite *
  1244. make_public_server_suite(void)
  1245. {
  1246. Suite *const suite = suite_create("PublicServer");
  1247. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  1248. TCase *const tcase_startthreads = tcase_create("Start threads");
  1249. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  1250. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  1251. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  1252. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  1253. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  1254. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  1255. suite_add_tcase(suite, tcase_checktestenv);
  1256. tcase_add_test(tcase_startthreads, test_threading);
  1257. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  1258. suite_add_tcase(suite, tcase_startthreads);
  1259. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  1260. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  1261. suite_add_tcase(suite, tcase_startstophttp);
  1262. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  1263. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  1264. suite_add_tcase(suite, tcase_startstophttps);
  1265. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  1266. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  1267. suite_add_tcase(suite, tcase_serverandclienttls);
  1268. tcase_add_test(tcase_serverrequests, test_request_handlers);
  1269. tcase_set_timeout(tcase_serverrequests, 120);
  1270. suite_add_tcase(suite, tcase_serverrequests);
  1271. return suite;
  1272. }
  1273. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  1274. /* Used to debug test cases without using the check framework */
  1275. static int chk_ok = 0;
  1276. static int chk_failed = 0;
  1277. void
  1278. xmain(void)
  1279. {
  1280. test_the_test_environment(0);
  1281. test_threading(0);
  1282. test_mg_start_stop_http_server(0);
  1283. test_mg_start_stop_https_server(0);
  1284. test_request_handlers(0);
  1285. test_mg_server_and_client_tls(0);
  1286. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  1287. }
  1288. void
  1289. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  1290. {
  1291. va_list va;
  1292. va_start(va, expr);
  1293. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1294. vfprintf(stderr, expr, va);
  1295. fprintf(stderr, "\n\n");
  1296. va_end(va);
  1297. chk_failed++;
  1298. }
  1299. void
  1300. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  1301. {
  1302. va_list va;
  1303. if (cond) {
  1304. chk_ok++;
  1305. return;
  1306. }
  1307. va_start(va, expr);
  1308. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1309. vfprintf(stderr, expr, va);
  1310. fprintf(stderr, "\n\n");
  1311. va_end(va);
  1312. chk_failed++;
  1313. }
  1314. void
  1315. _mark_point(const char *file, int line)
  1316. {
  1317. chk_ok++;
  1318. }
  1319. void
  1320. tcase_fn_start(const char *fname, const char *file, int line)
  1321. {
  1322. }
  1323. void suite_add_tcase(Suite *s, TCase *tc){};
  1324. void _tcase_add_test(TCase *tc,
  1325. TFun tf,
  1326. const char *fname,
  1327. int _signal,
  1328. int allowed_exit_value,
  1329. int start,
  1330. int end){};
  1331. TCase *
  1332. tcase_create(const char *name)
  1333. {
  1334. return NULL;
  1335. };
  1336. Suite *
  1337. suite_create(const char *name)
  1338. {
  1339. return NULL;
  1340. };
  1341. void tcase_set_timeout(TCase *tc, double timeout){};
  1342. #endif