public_server.c 42 KB

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