public_server.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  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 <string.h>
  30. #include <time.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include "public_server.h"
  34. #include <civetweb.h>
  35. #if defined(_WIN32)
  36. #include <Windows.h>
  37. #define test_sleep(x) (Sleep((x)*1000))
  38. #else
  39. #include <unistd.h>
  40. #define test_sleep(x) (sleep(x))
  41. #endif
  42. /* This unit test file uses the excellent Check unit testing library.
  43. * The API documentation is available here:
  44. * http://check.sourceforge.net/doc/check_html/index.html
  45. */
  46. static const char *
  47. locate_path(const char *a_path)
  48. {
  49. static char r_path[256];
  50. #ifdef _WIN32
  51. #ifdef LOCAL_TEST
  52. sprintf(r_path, "%s\\", a_path);
  53. #else
  54. /* Appveyor */
  55. sprintf(r_path, "..\\..\\..\\%s\\", a_path);
  56. /* TODO: the different paths
  57. * used in the different test
  58. * system is an unsolved
  59. * problem. */
  60. #endif
  61. #else
  62. #ifdef LOCAL_TEST
  63. sprintf(r_path, "%s/", a_path);
  64. #else
  65. /* Travis */
  66. sprintf(r_path,
  67. "../../%s/",
  68. a_path); // TODO: fix path in CI test environment
  69. #endif
  70. #endif
  71. return r_path;
  72. }
  73. #define locate_resources() locate_path("resources")
  74. #define locate_test_exes() locate_path("output")
  75. static const char *
  76. locate_ssl_cert(void)
  77. {
  78. static char cert_path[256];
  79. const char *res = locate_resources();
  80. size_t l;
  81. ck_assert(res != NULL);
  82. l = strlen(res);
  83. ck_assert_uint_gt(l, 0);
  84. ck_assert_uint_lt(l, 100); /* assume there is enough space left in our
  85. typical 255 character string buffers */
  86. strcpy(cert_path, res);
  87. strcat(cert_path, "ssl_cert.pem");
  88. return cert_path;
  89. }
  90. static int
  91. wait_not_null(void *volatile *data)
  92. {
  93. int i;
  94. for (i = 0; i < 100; i++) {
  95. mark_point();
  96. test_sleep(1);
  97. if (*data != NULL) {
  98. mark_point();
  99. return 1;
  100. }
  101. }
  102. #if defined(__MINGW32__) || defined(__GNUC__)
  103. #pragma GCC diagnostic push
  104. #pragma GCC diagnostic ignored "-Wunreachable-code"
  105. #endif
  106. #ifdef __clang__
  107. #pragma clang diagnostic push
  108. #pragma clang diagnostic ignored "-Wunreachable-code"
  109. #endif
  110. ck_abort_msg("wait_not_null failed");
  111. return 0;
  112. #ifdef __clang__
  113. #pragma clang diagnostic pop
  114. #endif
  115. #if defined(__MINGW32__) || defined(__GNUC__)
  116. #pragma GCC diagnostic pop
  117. #endif
  118. }
  119. START_TEST(test_the_test_environment)
  120. {
  121. char wd[300];
  122. char buf[500];
  123. FILE *f;
  124. struct stat st;
  125. int ret;
  126. const char *ssl_cert = locate_ssl_cert();
  127. memset(wd, 0, sizeof(wd));
  128. memset(buf, 0, sizeof(buf));
  129. /* Get the current working directory */
  130. #ifdef _WIN32
  131. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  132. wd[sizeof(wd) - 1] = 0;
  133. #else
  134. (void)getcwd(wd, sizeof(wd));
  135. wd[sizeof(wd) - 1] = 0;
  136. #endif
  137. /* Check the pem file */
  138. #ifdef _WIN32
  139. strcpy(buf, wd);
  140. strcat(buf, "\\");
  141. strcat(buf, ssl_cert);
  142. f = fopen(buf, "rb");
  143. #else
  144. strcpy(buf, wd);
  145. strcat(buf, "/");
  146. strcat(buf, ssl_cert);
  147. f = fopen(buf, "r");
  148. #endif
  149. if (f) {
  150. fclose(f);
  151. } else {
  152. fprintf(stderr, "%s not found", buf);
  153. }
  154. /* Check the test dir */
  155. #ifdef _WIN32
  156. strcpy(buf, wd);
  157. strcat(buf, "\\test");
  158. #else
  159. strcpy(buf, wd);
  160. strcat(buf, "/test");
  161. #endif
  162. memset(&st, 0, sizeof(st));
  163. ret = stat(buf, &st);
  164. if (ret) {
  165. fprintf(stderr, "%s not found", buf);
  166. }
  167. #ifdef _WIN32
  168. /* Try to copy the files required for AppVeyor */
  169. #if defined(_WIN64) || defined(__MINGW64__)
  170. system("cmd /c copy C:\\OpenSSL-Win64\\libeay32.dll libeay32.dll");
  171. system("cmd /c copy C:\\OpenSSL-Win64\\libssl32.dll libssl32.dll");
  172. system("cmd /c copy C:\\OpenSSL-Win64\\ssleay32.dll ssleay32.dll");
  173. system("cmd /c copy C:\\OpenSSL-Win64\\libeay32.dll libeay64.dll");
  174. system("cmd /c copy C:\\OpenSSL-Win64\\libssl32.dll libssl64.dll");
  175. system("cmd /c copy C:\\OpenSSL-Win64\\ssleay32.dll ssleay64.dll");
  176. #else
  177. system("cmd /c copy C:\\OpenSSL-Win32\\libeay32.dll libeay32.dll");
  178. system("cmd /c copy C:\\OpenSSL-Win32\\libssl32.dll libssl32.dll");
  179. system("cmd /c copy C:\\OpenSSL-Win32\\ssleay32.dll ssleay32.dll");
  180. #endif
  181. #endif
  182. }
  183. END_TEST
  184. static void *threading_data;
  185. static void *
  186. test_thread_func_t(void *param)
  187. {
  188. ck_assert_ptr_eq(param, &threading_data);
  189. ck_assert_ptr_eq(threading_data, NULL);
  190. threading_data = &threading_data;
  191. return NULL;
  192. }
  193. START_TEST(test_threading)
  194. {
  195. int ok;
  196. threading_data = NULL;
  197. ok = mg_start_thread(test_thread_func_t, &threading_data);
  198. ck_assert_int_eq(ok, 0);
  199. wait_not_null(&threading_data);
  200. ck_assert_ptr_eq(threading_data, &threading_data);
  201. }
  202. END_TEST
  203. static int
  204. log_msg_func(const struct mg_connection *conn, const char *message)
  205. {
  206. struct mg_context *ctx;
  207. char *ud;
  208. ck_assert(conn != NULL);
  209. ctx = mg_get_context(conn);
  210. ck_assert(ctx != NULL);
  211. ud = (char *)mg_get_user_data(ctx);
  212. strncpy(ud, message, 255);
  213. ud[255] = 0;
  214. return 1;
  215. }
  216. START_TEST(test_mg_start_stop_http_server)
  217. {
  218. struct mg_context *ctx;
  219. const char *OPTIONS[] = {
  220. #if !defined(NO_FILES)
  221. "document_root",
  222. ".",
  223. #endif
  224. "listening_ports",
  225. "8080",
  226. NULL,
  227. };
  228. size_t ports_cnt;
  229. int ports[16];
  230. int ssl[16];
  231. struct mg_callbacks callbacks;
  232. char errmsg[256];
  233. struct mg_connection *client_conn;
  234. char client_err[256];
  235. const struct mg_request_info *client_ri;
  236. int client_res, ret;
  237. struct mg_server_ports portinfo[8];
  238. memset(ports, 0, sizeof(ports));
  239. memset(ssl, 0, sizeof(ssl));
  240. memset(portinfo, 0, sizeof(portinfo));
  241. memset(&callbacks, 0, sizeof(callbacks));
  242. memset(errmsg, 0, sizeof(errmsg));
  243. callbacks.log_message = log_msg_func;
  244. mark_point();
  245. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  246. test_sleep(1);
  247. ck_assert_str_eq(errmsg, "");
  248. ck_assert(ctx != NULL);
  249. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  250. ck_assert_uint_eq(ports_cnt, 1);
  251. ck_assert_int_eq(ports[0], 8080);
  252. ck_assert_int_eq(ssl[0], 0);
  253. ck_assert_int_eq(ports[1], 0);
  254. ck_assert_int_eq(ssl[1], 0);
  255. ret = mg_get_server_ports(ctx, 0, portinfo);
  256. ck_assert_int_lt(ret, 0);
  257. ck_assert_int_eq(portinfo[0].protocol, 0);
  258. ck_assert_int_eq(portinfo[0].port, 0);
  259. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  260. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  261. ck_assert_int_eq(portinfo[1].protocol, 0);
  262. ck_assert_int_eq(portinfo[1].port, 0);
  263. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  264. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  265. ret = mg_get_server_ports(ctx, 4, portinfo);
  266. ck_assert_int_eq(ret, 1);
  267. ck_assert_int_eq(portinfo[0].protocol, 1);
  268. ck_assert_int_eq(portinfo[0].port, 8080);
  269. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  270. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  271. ck_assert_int_eq(portinfo[1].protocol, 0);
  272. ck_assert_int_eq(portinfo[1].port, 0);
  273. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  274. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  275. test_sleep(1);
  276. /* HTTP 1.0 GET request */
  277. memset(client_err, 0, sizeof(client_err));
  278. client_conn =
  279. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  280. ck_assert(client_conn != NULL);
  281. ck_assert_str_eq(client_err, "");
  282. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  283. client_res =
  284. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  285. ck_assert_int_ge(client_res, 0);
  286. ck_assert_str_eq(client_err, "");
  287. client_ri = mg_get_request_info(client_conn);
  288. ck_assert(client_ri != NULL);
  289. #if defined(NO_FILES)
  290. ck_assert_str_eq(client_ri->uri, "404");
  291. #else
  292. ck_assert_str_eq(client_ri->uri, "200");
  293. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  294. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  295. ck_assert_int_gt(client_res, 0);
  296. ck_assert_int_le(client_res, sizeof(client_err));
  297. #endif
  298. mg_close_connection(client_conn);
  299. test_sleep(1);
  300. /* HTTP 1.1 GET request */
  301. memset(client_err, 0, sizeof(client_err));
  302. client_conn =
  303. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  304. ck_assert(client_conn != NULL);
  305. ck_assert_str_eq(client_err, "");
  306. mg_printf(client_conn, "GET / HTTP/1.1\r\n");
  307. mg_printf(client_conn, "Host: localhost:8080\r\n");
  308. mg_printf(client_conn, "Connection: close\r\n\r\n");
  309. client_res =
  310. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  311. ck_assert_int_ge(client_res, 0);
  312. ck_assert_str_eq(client_err, "");
  313. client_ri = mg_get_request_info(client_conn);
  314. ck_assert(client_ri != NULL);
  315. #if defined(NO_FILES)
  316. ck_assert_str_eq(client_ri->uri, "404");
  317. #else
  318. ck_assert_str_eq(client_ri->uri, "200");
  319. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  320. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  321. ck_assert_int_gt(client_res, 0);
  322. ck_assert_int_le(client_res, sizeof(client_err));
  323. #endif
  324. mg_close_connection(client_conn);
  325. test_sleep(1);
  326. /* HTTP 1.7 GET request - this HTTP version does not exist */
  327. memset(client_err, 0, sizeof(client_err));
  328. client_conn =
  329. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  330. ck_assert(client_conn != NULL);
  331. ck_assert_str_eq(client_err, "");
  332. mg_printf(client_conn, "GET / HTTP/1.7\r\n");
  333. mg_printf(client_conn, "Host: localhost:8080\r\n");
  334. mg_printf(client_conn, "Connection: close\r\n\r\n");
  335. client_res =
  336. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  337. ck_assert_int_ge(client_res, 0);
  338. ck_assert_str_eq(client_err, "");
  339. client_ri = mg_get_request_info(client_conn);
  340. ck_assert(client_ri != NULL);
  341. /* Response must be 505 HTTP Version not supported */
  342. ck_assert_str_eq(client_ri->uri, "505");
  343. mg_close_connection(client_conn);
  344. test_sleep(1);
  345. /* End test */
  346. mg_stop(ctx);
  347. }
  348. END_TEST
  349. START_TEST(test_mg_start_stop_https_server)
  350. {
  351. #ifndef NO_SSL
  352. struct mg_context *ctx;
  353. size_t ports_cnt;
  354. int ports[16];
  355. int ssl[16];
  356. struct mg_callbacks callbacks;
  357. char errmsg[256];
  358. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  359. int opt_idx = 0;
  360. const char *ssl_cert = locate_ssl_cert();
  361. struct mg_connection *client_conn;
  362. char client_err[256];
  363. const struct mg_request_info *client_ri;
  364. int client_res, ret;
  365. struct mg_server_ports portinfo[8];
  366. ck_assert(ssl_cert != NULL);
  367. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  368. #if !defined(NO_FILES)
  369. OPTIONS[opt_idx++] = "document_root";
  370. OPTIONS[opt_idx++] = ".";
  371. #endif
  372. OPTIONS[opt_idx++] = "listening_ports";
  373. OPTIONS[opt_idx++] = "8080r,8443s";
  374. OPTIONS[opt_idx++] = "ssl_certificate";
  375. OPTIONS[opt_idx++] = ssl_cert;
  376. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  377. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  378. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  379. memset(ports, 0, sizeof(ports));
  380. memset(ssl, 0, sizeof(ssl));
  381. memset(portinfo, 0, sizeof(portinfo));
  382. memset(&callbacks, 0, sizeof(callbacks));
  383. memset(errmsg, 0, sizeof(errmsg));
  384. callbacks.log_message = log_msg_func;
  385. mark_point();
  386. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  387. test_sleep(1);
  388. ck_assert_str_eq(errmsg, "");
  389. ck_assert(ctx != NULL);
  390. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  391. ck_assert_uint_eq(ports_cnt, 2);
  392. ck_assert_int_eq(ports[0], 8080);
  393. ck_assert_int_eq(ssl[0], 0);
  394. ck_assert_int_eq(ports[1], 8443);
  395. ck_assert_int_eq(ssl[1], 1);
  396. ck_assert_int_eq(ports[2], 0);
  397. ck_assert_int_eq(ssl[2], 0);
  398. ret = mg_get_server_ports(ctx, 0, portinfo);
  399. ck_assert_int_lt(ret, 0);
  400. ck_assert_int_eq(portinfo[0].protocol, 0);
  401. ck_assert_int_eq(portinfo[0].port, 0);
  402. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  403. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  404. ck_assert_int_eq(portinfo[1].protocol, 0);
  405. ck_assert_int_eq(portinfo[1].port, 0);
  406. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  407. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  408. ret = mg_get_server_ports(ctx, 4, portinfo);
  409. ck_assert_int_eq(ret, 2);
  410. ck_assert_int_eq(portinfo[0].protocol, 1);
  411. ck_assert_int_eq(portinfo[0].port, 8080);
  412. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  413. ck_assert_int_eq(portinfo[0].is_redirect, 1);
  414. ck_assert_int_eq(portinfo[1].protocol, 1);
  415. ck_assert_int_eq(portinfo[1].port, 8443);
  416. ck_assert_int_eq(portinfo[1].is_ssl, 1);
  417. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  418. ck_assert_int_eq(portinfo[2].protocol, 0);
  419. ck_assert_int_eq(portinfo[2].port, 0);
  420. ck_assert_int_eq(portinfo[2].is_ssl, 0);
  421. ck_assert_int_eq(portinfo[2].is_redirect, 0);
  422. test_sleep(1);
  423. memset(client_err, 0, sizeof(client_err));
  424. client_conn =
  425. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  426. ck_assert(client_conn != NULL);
  427. ck_assert_str_eq(client_err, "");
  428. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  429. client_res =
  430. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  431. ck_assert_int_ge(client_res, 0);
  432. ck_assert_str_eq(client_err, "");
  433. client_ri = mg_get_request_info(client_conn);
  434. ck_assert(client_ri != NULL);
  435. #if defined(NO_FILES)
  436. ck_assert_str_eq(client_ri->uri, "404");
  437. #else
  438. ck_assert_str_eq(client_ri->uri, "200");
  439. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  440. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  441. ck_assert_int_gt(client_res, 0);
  442. ck_assert_int_le(client_res, sizeof(client_err));
  443. #endif
  444. mg_close_connection(client_conn);
  445. test_sleep(1);
  446. mg_stop(ctx);
  447. #endif
  448. }
  449. END_TEST
  450. START_TEST(test_mg_server_and_client_tls)
  451. {
  452. #ifndef NO_SSL
  453. struct mg_context *ctx;
  454. int ports_cnt;
  455. struct mg_server_ports ports[16];
  456. struct mg_callbacks callbacks;
  457. char errmsg[256];
  458. struct mg_connection *client_conn;
  459. char client_err[256];
  460. const struct mg_request_info *client_ri;
  461. int client_res;
  462. struct mg_client_options client_options;
  463. const char *OPTIONS[32]; /* initializer list here is rejected by CI test */
  464. int opt_idx = 0;
  465. char server_cert[256];
  466. char client_cert[256];
  467. const char *res_dir = locate_resources();
  468. ck_assert(res_dir != NULL);
  469. strcpy(server_cert, res_dir);
  470. strcpy(client_cert, res_dir);
  471. #ifdef _WIN32
  472. strcat(server_cert, "cert\\server.pem");
  473. strcat(client_cert, "cert\\client.pem");
  474. #else
  475. strcat(server_cert, "cert/server.pem");
  476. strcat(client_cert, "cert/client.pem");
  477. #endif
  478. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  479. #if !defined(NO_FILES)
  480. OPTIONS[opt_idx++] = "document_root";
  481. OPTIONS[opt_idx++] = ".";
  482. #endif
  483. OPTIONS[opt_idx++] = "listening_ports";
  484. OPTIONS[opt_idx++] = "8080r,8443s";
  485. OPTIONS[opt_idx++] = "ssl_certificate";
  486. OPTIONS[opt_idx++] = server_cert;
  487. OPTIONS[opt_idx++] = "ssl_verify_peer";
  488. OPTIONS[opt_idx++] = "yes";
  489. OPTIONS[opt_idx++] = "ssl_ca_file";
  490. OPTIONS[opt_idx++] = client_cert;
  491. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  492. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  493. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  494. memset(ports, 0, sizeof(ports));
  495. memset(&callbacks, 0, sizeof(callbacks));
  496. memset(errmsg, 0, sizeof(errmsg));
  497. callbacks.log_message = log_msg_func;
  498. mark_point();
  499. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  500. test_sleep(1);
  501. ck_assert_str_eq(errmsg, "");
  502. ck_assert(ctx != NULL);
  503. ports_cnt = mg_get_server_ports(ctx, 16, ports);
  504. ck_assert_int_eq(ports_cnt, 2);
  505. ck_assert_int_eq(ports[0].protocol, 1);
  506. ck_assert_int_eq(ports[0].port, 8080);
  507. ck_assert_int_eq(ports[0].is_ssl, 0);
  508. ck_assert_int_eq(ports[0].is_redirect, 1);
  509. ck_assert_int_eq(ports[1].protocol, 1);
  510. ck_assert_int_eq(ports[1].port, 8443);
  511. ck_assert_int_eq(ports[1].is_ssl, 1);
  512. ck_assert_int_eq(ports[1].is_redirect, 0);
  513. ck_assert_int_eq(ports[2].protocol, 0);
  514. ck_assert_int_eq(ports[2].port, 0);
  515. ck_assert_int_eq(ports[2].is_ssl, 0);
  516. ck_assert_int_eq(ports[2].is_redirect, 0);
  517. test_sleep(1);
  518. memset(client_err, 0, sizeof(client_err));
  519. client_conn =
  520. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  521. ck_assert(client_conn == NULL);
  522. ck_assert_str_ne(client_err, "");
  523. memset(client_err, 0, sizeof(client_err));
  524. memset(&client_options, 0, sizeof(client_options));
  525. client_options.host = "127.0.0.1";
  526. client_options.port = 8443;
  527. client_options.client_cert = client_cert;
  528. client_options.server_cert = server_cert;
  529. client_conn = mg_connect_client_secure(&client_options,
  530. client_err,
  531. sizeof(client_err));
  532. ck_assert(client_conn != NULL);
  533. ck_assert_str_eq(client_err, "");
  534. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  535. client_res =
  536. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  537. ck_assert_int_ge(client_res, 0);
  538. ck_assert_str_eq(client_err, "");
  539. client_ri = mg_get_request_info(client_conn);
  540. ck_assert(client_ri != NULL);
  541. #if defined(NO_FILES)
  542. ck_assert_str_eq(client_ri->uri, "404");
  543. #else
  544. ck_assert_str_eq(client_ri->uri, "200");
  545. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  546. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  547. ck_assert_int_gt(client_res, 0);
  548. ck_assert_int_le(client_res, sizeof(client_err));
  549. #endif
  550. mg_close_connection(client_conn);
  551. /* TODO: A client API using a client certificate is missing */
  552. test_sleep(1);
  553. mg_stop(ctx);
  554. #endif
  555. }
  556. END_TEST
  557. static struct mg_context *g_ctx;
  558. static int
  559. request_test_handler(struct mg_connection *conn, void *cbdata)
  560. {
  561. int i;
  562. char chunk_data[32];
  563. const struct mg_request_info *ri;
  564. struct mg_context *ctx;
  565. void *ud, *cud;
  566. ctx = mg_get_context(conn);
  567. ud = mg_get_user_data(ctx);
  568. ri = mg_get_request_info(conn);
  569. ck_assert(ri != NULL);
  570. ck_assert(ctx == g_ctx);
  571. ck_assert(ud == &g_ctx);
  572. mg_set_user_connection_data(conn, (void *)6543);
  573. cud = mg_get_user_connection_data(conn);
  574. ck_assert_ptr_eq((void *)cud, (void *)6543);
  575. ck_assert_ptr_eq((void *)cbdata, (void *)7);
  576. strcpy(chunk_data, "123456789A123456789B123456789C");
  577. mg_printf(conn,
  578. "HTTP/1.1 200 OK\r\n"
  579. "Transfer-Encoding: chunked\r\n"
  580. "Content-Type: text/plain\r\n\r\n");
  581. for (i = 1; i <= 10; i++) {
  582. mg_printf(conn, "%x\r\n", i);
  583. mg_write(conn, chunk_data, (unsigned)i);
  584. mg_printf(conn, "\r\n");
  585. }
  586. mg_printf(conn, "0\r\n\r\n");
  587. return 1;
  588. }
  589. #ifdef USE_WEBSOCKET
  590. /****************************************************************************/
  591. /* WEBSOCKET SERVER */
  592. /****************************************************************************/
  593. static const char *websocket_welcome_msg = "websocket welcome\n";
  594. static const size_t websocket_welcome_msg_len =
  595. 18 /* strlen(websocket_welcome_msg) */;
  596. static const char *websocket_goodbye_msg = "websocket bye\n";
  597. static const size_t websocket_goodbye_msg_len =
  598. 14 /* strlen(websocket_goodbye_msg) */;
  599. static int
  600. websock_server_connect(const struct mg_connection *conn, void *udata)
  601. {
  602. (void)conn;
  603. ck_assert_ptr_eq((void *)udata, (void *)7531);
  604. printf("Server: Websocket connected\n");
  605. return 0; /* return 0 to accept every connection */
  606. }
  607. static void
  608. websock_server_ready(struct mg_connection *conn, void *udata)
  609. {
  610. ck_assert_ptr_eq((void *)udata, (void *)7531);
  611. printf("Server: Websocket ready\n");
  612. /* Send websocket welcome message */
  613. mg_lock_connection(conn);
  614. mg_websocket_write(conn,
  615. WEBSOCKET_OPCODE_TEXT,
  616. websocket_welcome_msg,
  617. websocket_welcome_msg_len);
  618. mg_unlock_connection(conn);
  619. printf("Server: Websocket ready X\n");
  620. }
  621. static int
  622. websock_server_data(struct mg_connection *conn,
  623. int bits,
  624. char *data,
  625. size_t data_len,
  626. void *udata)
  627. {
  628. (void)bits;
  629. ck_assert_ptr_eq((void *)udata, (void *)7531);
  630. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  631. if (data_len == 3 && !memcmp(data, "bye", 3)) {
  632. /* Send websocket goodbye message */
  633. mg_lock_connection(conn);
  634. mg_websocket_write(conn,
  635. WEBSOCKET_OPCODE_TEXT,
  636. websocket_goodbye_msg,
  637. websocket_goodbye_msg_len);
  638. mg_unlock_connection(conn);
  639. } else if (data_len == 5 && !memcmp(data, "data1", 5)) {
  640. mg_lock_connection(conn);
  641. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok1", 3);
  642. mg_unlock_connection(conn);
  643. } else if (data_len == 5 && !memcmp(data, "data2", 5)) {
  644. mg_lock_connection(conn);
  645. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok 2", 4);
  646. mg_unlock_connection(conn);
  647. } else if (data_len == 5 && !memcmp(data, "data3", 5)) {
  648. mg_lock_connection(conn);
  649. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok - 3", 6);
  650. mg_unlock_connection(conn);
  651. } else {
  652. #if defined(__MINGW32__) || defined(__GNUC__)
  653. #pragma GCC diagnostic push
  654. #pragma GCC diagnostic ignored "-Wunreachable-code"
  655. #endif
  656. #ifdef __clang__
  657. #pragma clang diagnostic push
  658. #pragma clang diagnostic ignored "-Wunreachable-code"
  659. #endif
  660. ck_abort_msg("Got unexpected message from websocket client");
  661. return 0;
  662. #ifdef __clang__
  663. #pragma clang diagnostic pop
  664. #endif
  665. #if defined(__MINGW32__) || defined(__GNUC__)
  666. #pragma GCC diagnostic pop
  667. #endif
  668. }
  669. return 1; /* return 1 to keep the connetion open */
  670. }
  671. static void
  672. websock_server_close(const struct mg_connection *conn, void *udata)
  673. {
  674. (void)conn;
  675. ck_assert_ptr_eq((void *)udata, (void *)7531);
  676. printf("Server: Close connection\n");
  677. /* Can not send a websocket goodbye message here - the connection is already
  678. * closed */
  679. }
  680. /****************************************************************************/
  681. /* WEBSOCKET CLIENT */
  682. /****************************************************************************/
  683. struct tclient_data {
  684. void *data;
  685. size_t len;
  686. int closed;
  687. };
  688. static int
  689. websocket_client_data_handler(struct mg_connection *conn,
  690. int flags,
  691. char *data,
  692. size_t data_len,
  693. void *user_data)
  694. {
  695. struct mg_context *ctx = mg_get_context(conn);
  696. struct tclient_data *pclient_data =
  697. (struct tclient_data *)mg_get_user_data(ctx);
  698. (void)user_data; /* TODO: check this */
  699. ck_assert(pclient_data != NULL);
  700. ck_assert_int_eq(flags, (int)(128 | 1));
  701. printf("Client received data from server: ");
  702. fwrite(data, 1, data_len, stdout);
  703. printf("\n");
  704. pclient_data->data = malloc(data_len);
  705. ck_assert(pclient_data->data != NULL);
  706. memcpy(pclient_data->data, data, data_len);
  707. pclient_data->len = data_len;
  708. return 1;
  709. }
  710. static void
  711. websocket_client_close_handler(const struct mg_connection *conn,
  712. void *user_data)
  713. {
  714. struct mg_context *ctx = mg_get_context(conn);
  715. struct tclient_data *pclient_data =
  716. (struct tclient_data *)mg_get_user_data(ctx);
  717. (void)user_data; /* TODO: check this */
  718. ck_assert(pclient_data != NULL);
  719. printf("Client: Close handler\n");
  720. pclient_data->closed++;
  721. }
  722. #endif
  723. START_TEST(test_request_handlers)
  724. {
  725. char ebuf[100];
  726. struct mg_context *ctx;
  727. struct mg_connection *client_conn;
  728. const struct mg_request_info *ri;
  729. char uri[64];
  730. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  731. const char *expected =
  732. "112123123412345123456123456712345678123456789123456789A";
  733. int i;
  734. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  735. #if defined(USE_IPV6) && defined(NO_SSL)
  736. const char *HTTP_PORT = "8084,[::]:8086";
  737. short ipv4_port = 8084;
  738. short ipv6_port = 8086;
  739. #elif !defined(USE_IPV6) && defined(NO_SSL)
  740. const char *HTTP_PORT = "8084";
  741. short ipv4_port = 8084;
  742. #elif defined(USE_IPV6) && !defined(NO_SSL)
  743. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  744. short ipv4_port = 8084;
  745. short ipv4s_port = 8094;
  746. short ipv4r_port = 8194;
  747. short ipv6_port = 8086;
  748. short ipv6s_port = 8096;
  749. short ipv6r_port = 8196;
  750. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  751. const char *HTTP_PORT = "8084,8194r,8094s";
  752. short ipv4_port = 8084;
  753. short ipv4s_port = 8094;
  754. short ipv4r_port = 8194;
  755. #endif
  756. const char *OPTIONS[16];
  757. const char *opt;
  758. FILE *f;
  759. const char *plain_file_content;
  760. const char *encoded_file_content;
  761. const char *cgi_script_content;
  762. const char *expected_cgi_result;
  763. int opt_idx = 0;
  764. #if !defined(NO_SSL)
  765. const char *ssl_cert = locate_ssl_cert();
  766. #endif
  767. #if defined(USE_WEBSOCKET)
  768. struct tclient_data ws_client1_data = {NULL, 0, 0};
  769. struct tclient_data ws_client2_data = {NULL, 0, 0};
  770. struct tclient_data ws_client3_data = {NULL, 0, 0};
  771. struct mg_connection *ws_client1_conn = NULL;
  772. struct mg_connection *ws_client2_conn = NULL;
  773. struct mg_connection *ws_client3_conn = NULL;
  774. #endif
  775. char cmd_buf[256];
  776. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  777. OPTIONS[opt_idx++] = "listening_ports";
  778. OPTIONS[opt_idx++] = HTTP_PORT;
  779. OPTIONS[opt_idx++] = "authentication_domain";
  780. OPTIONS[opt_idx++] = "test.domain";
  781. #if !defined(NO_FILES)
  782. OPTIONS[opt_idx++] = "document_root";
  783. OPTIONS[opt_idx++] = ".";
  784. #endif
  785. #ifndef NO_SSL
  786. ck_assert(ssl_cert != NULL);
  787. OPTIONS[opt_idx++] = "ssl_certificate";
  788. OPTIONS[opt_idx++] = ssl_cert;
  789. #endif
  790. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  791. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  792. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  793. mark_point();
  794. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  795. ck_assert(ctx != NULL);
  796. g_ctx = ctx;
  797. opt = mg_get_option(ctx, "listening_ports");
  798. ck_assert_str_eq(opt, HTTP_PORT);
  799. opt = mg_get_option(ctx, "cgi_environment");
  800. ck_assert_str_eq(opt, "");
  801. opt = mg_get_option(ctx, "unknown_option_name");
  802. ck_assert(opt == NULL);
  803. for (i = 0; i < 1000; i++) {
  804. sprintf(uri, "/U%u", i);
  805. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  806. }
  807. for (i = 500; i < 800; i++) {
  808. sprintf(uri, "/U%u", i);
  809. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  810. }
  811. for (i = 600; i >= 0; i--) {
  812. sprintf(uri, "/U%u", i);
  813. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  814. }
  815. for (i = 750; i <= 1000; i++) {
  816. sprintf(uri, "/U%u", i);
  817. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  818. }
  819. for (i = 5; i < 9; i++) {
  820. sprintf(uri, "/U%u", i);
  821. mg_set_request_handler(ctx,
  822. uri,
  823. request_test_handler,
  824. (void *)(ptrdiff_t)i);
  825. }
  826. #ifdef USE_WEBSOCKET
  827. mg_set_websocket_handler(ctx,
  828. "/websocket",
  829. websock_server_connect,
  830. websock_server_ready,
  831. websock_server_data,
  832. websock_server_close,
  833. (void *)7531);
  834. #endif
  835. /* Try to load non existing file */
  836. client_conn = mg_download("localhost",
  837. ipv4_port,
  838. 0,
  839. ebuf,
  840. sizeof(ebuf),
  841. "%s",
  842. "GET /file/not/found HTTP/1.0\r\n\r\n");
  843. ck_assert(client_conn != NULL);
  844. ri = mg_get_request_info(client_conn);
  845. ck_assert(ri != NULL);
  846. ck_assert_str_eq(ri->uri, "404");
  847. mg_close_connection(client_conn);
  848. /* Get data from callback */
  849. client_conn = mg_download(
  850. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  851. ck_assert(client_conn != NULL);
  852. ri = mg_get_request_info(client_conn);
  853. ck_assert(ri != NULL);
  854. ck_assert_str_eq(ri->uri, "200");
  855. i = mg_read(client_conn, buf, sizeof(buf));
  856. ck_assert_int_eq(i, (int)strlen(expected));
  857. buf[i] = 0;
  858. ck_assert_str_eq(buf, expected);
  859. mg_close_connection(client_conn);
  860. /* Get data from callback using http://127.0.0.1 */
  861. client_conn = mg_download(
  862. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  863. ck_assert(client_conn != NULL);
  864. ri = mg_get_request_info(client_conn);
  865. ck_assert(ri != NULL);
  866. ck_assert_str_eq(ri->uri, "200");
  867. i = mg_read(client_conn, buf, sizeof(buf));
  868. if ((i >= 0) && ((size_t)i < sizeof(buf))) {
  869. buf[i] = 0;
  870. } else {
  871. ck_abort_msg(
  872. "ERROR: test_request_handlers: read returned %i (>=0, <%i)",
  873. (int)i,
  874. (int)sizeof(buf));
  875. }
  876. ck_assert((int)i < (int)sizeof(buf));
  877. ck_assert(i > 0);
  878. ck_assert_int_eq(i, (int)strlen(expected));
  879. buf[i] = 0;
  880. ck_assert_str_eq(buf, expected);
  881. mg_close_connection(client_conn);
  882. #if defined(USE_IPV6)
  883. /* Get data from callback using http://[::1] */
  884. client_conn =
  885. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  886. ck_assert(client_conn != NULL);
  887. ri = mg_get_request_info(client_conn);
  888. ck_assert(ri != NULL);
  889. ck_assert_str_eq(ri->uri, "200");
  890. i = mg_read(client_conn, buf, sizeof(buf));
  891. ck_assert_int_eq(i, (int)strlen(expected));
  892. buf[i] = 0;
  893. ck_assert_str_eq(buf, expected);
  894. mg_close_connection(client_conn);
  895. #endif
  896. #if !defined(NO_SSL)
  897. /* Get data from callback using https://127.0.0.1 */
  898. client_conn = mg_download(
  899. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  900. ck_assert(client_conn != NULL);
  901. ri = mg_get_request_info(client_conn);
  902. ck_assert(ri != NULL);
  903. ck_assert_str_eq(ri->uri, "200");
  904. i = mg_read(client_conn, buf, sizeof(buf));
  905. ck_assert_int_eq(i, (int)strlen(expected));
  906. buf[i] = 0;
  907. ck_assert_str_eq(buf, expected);
  908. mg_close_connection(client_conn);
  909. /* Get redirect from callback using http://127.0.0.1 */
  910. client_conn = mg_download(
  911. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  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, "302");
  916. i = mg_read(client_conn, buf, sizeof(buf));
  917. ck_assert_int_eq(i, -1);
  918. mg_close_connection(client_conn);
  919. #endif
  920. #if defined(USE_IPV6) && !defined(NO_SSL)
  921. /* Get data from callback using https://[::1] */
  922. client_conn =
  923. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  924. ck_assert(client_conn != NULL);
  925. ri = mg_get_request_info(client_conn);
  926. ck_assert(ri != NULL);
  927. ck_assert_str_eq(ri->uri, "200");
  928. i = mg_read(client_conn, buf, sizeof(buf));
  929. ck_assert_int_eq(i, (int)strlen(expected));
  930. buf[i] = 0;
  931. ck_assert_str_eq(buf, expected);
  932. mg_close_connection(client_conn);
  933. /* Get redirect from callback using http://127.0.0.1 */
  934. client_conn =
  935. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  936. ck_assert(client_conn != NULL);
  937. ri = mg_get_request_info(client_conn);
  938. ck_assert(ri != NULL);
  939. ck_assert_str_eq(ri->uri, "302");
  940. i = mg_read(client_conn, buf, sizeof(buf));
  941. ck_assert_int_eq(i, -1);
  942. mg_close_connection(client_conn);
  943. #endif
  944. /* It seems to be impossible to find out what the actual working
  945. * directory of the CI test environment is. Before breaking another
  946. * dozen of builds by trying blindly with different paths, just
  947. * create the file here */
  948. #ifdef _WIN32
  949. f = fopen("test.txt", "wb");
  950. #else
  951. f = fopen("test.txt", "w");
  952. #endif
  953. plain_file_content = "simple text file\n";
  954. fwrite(plain_file_content, 17, 1, f);
  955. fclose(f);
  956. #ifdef _WIN32
  957. f = fopen("test_gz.txt.gz", "wb");
  958. #else
  959. f = fopen("test_gz.txt.gz", "w");
  960. #endif
  961. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  962. "test_gz.txt"
  963. "\x00\x01\x11\x00\xee\xff"
  964. "zipped text file"
  965. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  966. fwrite(encoded_file_content, 1, 52, f);
  967. fclose(f);
  968. #ifdef _WIN32
  969. f = fopen("test.cgi", "wb");
  970. cgi_script_content = "#!test.cgi.cmd\r\n";
  971. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  972. fclose(f);
  973. f = fopen("test.cgi.cmd", "w");
  974. cgi_script_content = "@echo off\r\n"
  975. "echo Connection: close\r\n"
  976. "echo Content-Type: text/plain\r\n"
  977. "echo.\r\n"
  978. "echo CGI test\r\n"
  979. "\r\n";
  980. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  981. fclose(f);
  982. #else
  983. f = fopen("test.cgi", "w");
  984. cgi_script_content = "#!/bin/sh\n\n"
  985. "printf \"Connection: close\\r\\n\"\n"
  986. "printf \"Content-Type: text/plain\\r\\n\"\n"
  987. "printf \"\\r\\n\"\n"
  988. "printf \"CGI test\\r\\n\"\n"
  989. "\n";
  990. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  991. fclose(f);
  992. system("chmod a+x test.cgi");
  993. #endif
  994. expected_cgi_result = "CGI test";
  995. /* Get static data */
  996. client_conn = mg_download("localhost",
  997. ipv4_port,
  998. 0,
  999. ebuf,
  1000. sizeof(ebuf),
  1001. "%s",
  1002. "GET /test.txt HTTP/1.0\r\n\r\n");
  1003. ck_assert(client_conn != NULL);
  1004. ri = mg_get_request_info(client_conn);
  1005. ck_assert(ri != NULL);
  1006. #if defined(NO_FILES)
  1007. ck_assert_str_eq(ri->uri, "404");
  1008. #else
  1009. ck_assert_str_eq(ri->uri, "200");
  1010. i = mg_read(client_conn, buf, sizeof(buf));
  1011. ck_assert_int_eq(i, 17);
  1012. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1013. buf[i] = 0;
  1014. }
  1015. ck_assert_str_eq(buf, plain_file_content);
  1016. #endif
  1017. mg_close_connection(client_conn);
  1018. /* Test with CGI test executable */
  1019. #if defined(_WIN32)
  1020. sprintf(cmd_buf, "copy %s\\cgi_test.cgi cgi_test.exe", locate_test_exes());
  1021. #else
  1022. sprintf(cmd_buf, "cp %s/cgi_test.cgi cgi_test.cgi", locate_test_exes());
  1023. #endif
  1024. system(cmd_buf);
  1025. #if !defined(NO_CGI) && !defined(NO_FILES) && !defined(_WIN32)
  1026. /* TODO: add test for windows, check with POST */
  1027. client_conn = mg_download(
  1028. "localhost",
  1029. ipv4_port,
  1030. 0,
  1031. ebuf,
  1032. sizeof(ebuf),
  1033. "%s",
  1034. "POST /cgi_test.cgi HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC");
  1035. ck_assert(client_conn != NULL);
  1036. ri = mg_get_request_info(client_conn);
  1037. ck_assert(ri != NULL);
  1038. ck_assert_str_eq(ri->uri, "200");
  1039. mg_close_connection(client_conn);
  1040. #endif
  1041. /* Get zipped static data - will not work if Accept-Encoding is not set */
  1042. client_conn = mg_download("localhost",
  1043. ipv4_port,
  1044. 0,
  1045. ebuf,
  1046. sizeof(ebuf),
  1047. "%s",
  1048. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  1049. ck_assert(client_conn != NULL);
  1050. ri = mg_get_request_info(client_conn);
  1051. ck_assert(ri != NULL);
  1052. ck_assert_str_eq(ri->uri, "404");
  1053. mg_close_connection(client_conn);
  1054. /* Get zipped static data - with Accept-Encoding */
  1055. client_conn = mg_download(
  1056. "localhost",
  1057. ipv4_port,
  1058. 0,
  1059. ebuf,
  1060. sizeof(ebuf),
  1061. "%s",
  1062. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  1063. ck_assert(client_conn != NULL);
  1064. ri = mg_get_request_info(client_conn);
  1065. ck_assert(ri != NULL);
  1066. #if defined(NO_FILES)
  1067. ck_assert_str_eq(ri->uri, "404");
  1068. #else
  1069. ck_assert_str_eq(ri->uri, "200");
  1070. i = mg_read(client_conn, buf, sizeof(buf));
  1071. ck_assert_int_eq(i, 52);
  1072. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1073. buf[i] = 0;
  1074. }
  1075. ck_assert_int_eq(ri->content_length, 52);
  1076. ck_assert_str_eq(buf, encoded_file_content);
  1077. #endif
  1078. mg_close_connection(client_conn);
  1079. /* Get CGI generated data */
  1080. #if !defined(NO_CGI)
  1081. client_conn = mg_download("localhost",
  1082. ipv4_port,
  1083. 0,
  1084. ebuf,
  1085. sizeof(ebuf),
  1086. "%s",
  1087. "GET /test.cgi HTTP/1.0\r\n\r\n");
  1088. ck_assert(client_conn != NULL);
  1089. ri = mg_get_request_info(client_conn);
  1090. ck_assert(ri != NULL);
  1091. #if defined(NO_FILES)
  1092. ck_assert_str_eq(ri->uri, "404");
  1093. (void)expected_cgi_result;
  1094. (void)cgi_script_content;
  1095. #else
  1096. i = mg_read(client_conn, buf, sizeof(buf));
  1097. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1098. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  1099. i--;
  1100. }
  1101. buf[i] = 0;
  1102. }
  1103. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  1104. ck_assert_str_eq(buf, expected_cgi_result);
  1105. ck_assert_str_eq(ri->uri, "200");
  1106. mg_close_connection(client_conn);
  1107. #endif
  1108. #else
  1109. (void)expected_cgi_result;
  1110. (void)cgi_script_content;
  1111. #endif
  1112. /* Get directory listing */
  1113. client_conn = mg_download("localhost",
  1114. ipv4_port,
  1115. 0,
  1116. ebuf,
  1117. sizeof(ebuf),
  1118. "%s",
  1119. "GET / HTTP/1.0\r\n\r\n");
  1120. ck_assert(client_conn != NULL);
  1121. ri = mg_get_request_info(client_conn);
  1122. ck_assert(ri != NULL);
  1123. #if defined(NO_FILES)
  1124. ck_assert_str_eq(ri->uri, "404");
  1125. #else
  1126. ck_assert_str_eq(ri->uri, "200");
  1127. i = mg_read(client_conn, buf, sizeof(buf));
  1128. ck_assert(i > 6);
  1129. buf[6] = 0;
  1130. ck_assert_str_eq(buf, "<html>");
  1131. #endif
  1132. mg_close_connection(client_conn);
  1133. /* POST to static file (will not work) */
  1134. client_conn = mg_download("localhost",
  1135. ipv4_port,
  1136. 0,
  1137. ebuf,
  1138. sizeof(ebuf),
  1139. "%s",
  1140. "POST /test.txt HTTP/1.0\r\n\r\n");
  1141. ck_assert(client_conn != NULL);
  1142. ri = mg_get_request_info(client_conn);
  1143. ck_assert(ri != NULL);
  1144. #if defined(NO_FILES)
  1145. ck_assert_str_eq(ri->uri, "404");
  1146. #else
  1147. ck_assert_str_eq(ri->uri, "405");
  1148. i = mg_read(client_conn, buf, sizeof(buf));
  1149. ck_assert(i >= 29);
  1150. buf[29] = 0;
  1151. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  1152. #endif
  1153. mg_close_connection(client_conn);
  1154. /* PUT to static file (will not work) */
  1155. client_conn = mg_download("localhost",
  1156. ipv4_port,
  1157. 0,
  1158. ebuf,
  1159. sizeof(ebuf),
  1160. "%s",
  1161. "PUT /test.txt HTTP/1.0\r\n\r\n");
  1162. ck_assert(client_conn != NULL);
  1163. ri = mg_get_request_info(client_conn);
  1164. ck_assert(ri != NULL);
  1165. #if defined(NO_FILES)
  1166. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  1167. #else
  1168. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  1169. #endif
  1170. mg_close_connection(client_conn);
  1171. /* Get data from callback using mg_connect_client instead of mg_download */
  1172. memset(ebuf, 0, sizeof(ebuf));
  1173. client_conn =
  1174. mg_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf));
  1175. ck_assert(client_conn != NULL);
  1176. ck_assert_str_eq(ebuf, "");
  1177. mg_printf(client_conn, "%s", request);
  1178. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1179. ck_assert_int_ge(i, 0);
  1180. ck_assert_str_eq(ebuf, "");
  1181. ri = mg_get_request_info(client_conn);
  1182. ck_assert(ri != NULL);
  1183. ck_assert_str_eq(ri->uri, "200");
  1184. i = mg_read(client_conn, buf, sizeof(buf));
  1185. ck_assert_int_eq(i, (int)strlen(expected));
  1186. buf[i] = 0;
  1187. ck_assert_str_eq(buf, expected);
  1188. mg_close_connection(client_conn);
  1189. /* Get data from callback using mg_connect_client and absolute URI */
  1190. memset(ebuf, 0, sizeof(ebuf));
  1191. client_conn =
  1192. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1193. ck_assert(client_conn != NULL);
  1194. ck_assert_str_eq(ebuf, "");
  1195. mg_printf(client_conn,
  1196. "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1197. ipv4_port);
  1198. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1199. ck_assert_int_ge(i, 0);
  1200. ck_assert_str_eq(ebuf, "");
  1201. ri = mg_get_request_info(client_conn);
  1202. ck_assert(ri != NULL);
  1203. ck_assert_str_eq(ri->uri, "200");
  1204. i = mg_read(client_conn, buf, sizeof(buf));
  1205. ck_assert_int_eq(i, (int)strlen(expected));
  1206. buf[i] = 0;
  1207. ck_assert_str_eq(buf, expected);
  1208. mg_close_connection(client_conn);
  1209. /* Websocket test */
  1210. #ifdef USE_WEBSOCKET
  1211. /* Then connect a first client */
  1212. ws_client1_conn =
  1213. mg_connect_websocket_client("localhost",
  1214. ipv4_port,
  1215. 0,
  1216. ebuf,
  1217. sizeof(ebuf),
  1218. "/websocket",
  1219. NULL,
  1220. websocket_client_data_handler,
  1221. websocket_client_close_handler,
  1222. &ws_client1_data);
  1223. ck_assert(ws_client1_conn != NULL);
  1224. wait_not_null(
  1225. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1226. ck_assert_int_eq(ws_client1_data.closed, 0);
  1227. ck_assert_int_eq(ws_client2_data.closed, 0);
  1228. ck_assert_int_eq(ws_client3_data.closed, 0);
  1229. ck_assert(ws_client2_data.data == NULL);
  1230. ck_assert_uint_eq(ws_client2_data.len, 0);
  1231. ck_assert(ws_client1_data.data != NULL);
  1232. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1233. ck_assert(!memcmp(ws_client1_data.data,
  1234. websocket_welcome_msg,
  1235. websocket_welcome_msg_len));
  1236. free(ws_client1_data.data);
  1237. ws_client1_data.data = NULL;
  1238. ws_client1_data.len = 0;
  1239. mg_websocket_client_write(ws_client1_conn,
  1240. WEBSOCKET_OPCODE_TEXT,
  1241. "data1",
  1242. 5);
  1243. wait_not_null(
  1244. &(ws_client1_data
  1245. .data)); /* Wait for the websocket acknowledge message */
  1246. ck_assert_int_eq(ws_client1_data.closed, 0);
  1247. ck_assert_int_eq(ws_client2_data.closed, 0);
  1248. ck_assert(ws_client2_data.data == NULL);
  1249. ck_assert_uint_eq(ws_client2_data.len, 0);
  1250. ck_assert(ws_client1_data.data != NULL);
  1251. ck_assert_uint_eq(ws_client1_data.len, 3);
  1252. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1253. free(ws_client1_data.data);
  1254. ws_client1_data.data = NULL;
  1255. ws_client1_data.len = 0;
  1256. /* Now connect a second client */
  1257. #ifdef USE_IPV6
  1258. ws_client2_conn =
  1259. mg_connect_websocket_client("[::1]",
  1260. ipv6_port,
  1261. 0,
  1262. ebuf,
  1263. sizeof(ebuf),
  1264. "/websocket",
  1265. NULL,
  1266. websocket_client_data_handler,
  1267. websocket_client_close_handler,
  1268. &ws_client2_data);
  1269. #else
  1270. ws_client2_conn =
  1271. mg_connect_websocket_client("127.0.0.1",
  1272. ipv4_port,
  1273. 0,
  1274. ebuf,
  1275. sizeof(ebuf),
  1276. "/websocket",
  1277. NULL,
  1278. websocket_client_data_handler,
  1279. websocket_client_close_handler,
  1280. &ws_client2_data);
  1281. #endif
  1282. ck_assert(ws_client2_conn != NULL);
  1283. wait_not_null(
  1284. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1285. ck_assert(ws_client1_data.closed == 0);
  1286. ck_assert(ws_client2_data.closed == 0);
  1287. ck_assert(ws_client1_data.data == NULL);
  1288. ck_assert(ws_client1_data.len == 0);
  1289. ck_assert(ws_client2_data.data != NULL);
  1290. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1291. ck_assert(!memcmp(ws_client2_data.data,
  1292. websocket_welcome_msg,
  1293. websocket_welcome_msg_len));
  1294. free(ws_client2_data.data);
  1295. ws_client2_data.data = NULL;
  1296. ws_client2_data.len = 0;
  1297. mg_websocket_client_write(ws_client1_conn,
  1298. WEBSOCKET_OPCODE_TEXT,
  1299. "data2",
  1300. 5);
  1301. wait_not_null(
  1302. &(ws_client1_data
  1303. .data)); /* Wait for the websocket acknowledge message */
  1304. ck_assert(ws_client1_data.closed == 0);
  1305. ck_assert(ws_client2_data.closed == 0);
  1306. ck_assert(ws_client2_data.data == NULL);
  1307. ck_assert(ws_client2_data.len == 0);
  1308. ck_assert(ws_client1_data.data != NULL);
  1309. ck_assert(ws_client1_data.len == 4);
  1310. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1311. free(ws_client1_data.data);
  1312. ws_client1_data.data = NULL;
  1313. ws_client1_data.len = 0;
  1314. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1315. wait_not_null(
  1316. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1317. ck_assert(ws_client1_data.closed == 0);
  1318. ck_assert(ws_client2_data.closed == 0);
  1319. ck_assert(ws_client2_data.data == NULL);
  1320. ck_assert(ws_client2_data.len == 0);
  1321. ck_assert(ws_client1_data.data != NULL);
  1322. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1323. ck_assert(!memcmp(ws_client1_data.data,
  1324. websocket_goodbye_msg,
  1325. websocket_goodbye_msg_len));
  1326. free(ws_client1_data.data);
  1327. ws_client1_data.data = NULL;
  1328. ws_client1_data.len = 0;
  1329. mg_close_connection(ws_client1_conn);
  1330. test_sleep(3); /* Won't get any message */
  1331. ck_assert(ws_client1_data.closed == 1);
  1332. ck_assert(ws_client2_data.closed == 0);
  1333. ck_assert(ws_client1_data.data == NULL);
  1334. ck_assert(ws_client1_data.len == 0);
  1335. ck_assert(ws_client2_data.data == NULL);
  1336. ck_assert(ws_client2_data.len == 0);
  1337. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1338. wait_not_null(
  1339. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1340. ck_assert(ws_client1_data.closed == 1);
  1341. ck_assert(ws_client2_data.closed == 0);
  1342. ck_assert(ws_client1_data.data == NULL);
  1343. ck_assert(ws_client1_data.len == 0);
  1344. ck_assert(ws_client2_data.data != NULL);
  1345. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1346. ck_assert(!memcmp(ws_client2_data.data,
  1347. websocket_goodbye_msg,
  1348. websocket_goodbye_msg_len));
  1349. free(ws_client2_data.data);
  1350. ws_client2_data.data = NULL;
  1351. ws_client2_data.len = 0;
  1352. mg_close_connection(ws_client2_conn);
  1353. test_sleep(3); /* Won't get any message */
  1354. ck_assert(ws_client1_data.closed == 1);
  1355. ck_assert(ws_client2_data.closed == 1);
  1356. ck_assert(ws_client1_data.data == NULL);
  1357. ck_assert(ws_client1_data.len == 0);
  1358. ck_assert(ws_client2_data.data == NULL);
  1359. ck_assert(ws_client2_data.len == 0);
  1360. /* Connect client 3 */
  1361. ws_client3_conn =
  1362. mg_connect_websocket_client("localhost",
  1363. #if defined(NO_SSL)
  1364. ipv4_port,
  1365. 0,
  1366. #else
  1367. ipv4s_port,
  1368. 1,
  1369. #endif
  1370. ebuf,
  1371. sizeof(ebuf),
  1372. "/websocket",
  1373. NULL,
  1374. websocket_client_data_handler,
  1375. websocket_client_close_handler,
  1376. &ws_client3_data);
  1377. ck_assert(ws_client3_conn != NULL);
  1378. wait_not_null(
  1379. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1380. ck_assert(ws_client1_data.closed == 1);
  1381. ck_assert(ws_client2_data.closed == 1);
  1382. ck_assert(ws_client3_data.closed == 0);
  1383. ck_assert(ws_client1_data.data == NULL);
  1384. ck_assert(ws_client1_data.len == 0);
  1385. ck_assert(ws_client2_data.data == NULL);
  1386. ck_assert(ws_client2_data.len == 0);
  1387. ck_assert(ws_client3_data.data != NULL);
  1388. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1389. ck_assert(!memcmp(ws_client3_data.data,
  1390. websocket_welcome_msg,
  1391. websocket_welcome_msg_len));
  1392. free(ws_client3_data.data);
  1393. ws_client3_data.data = NULL;
  1394. ws_client3_data.len = 0;
  1395. #endif
  1396. /* Close the server */
  1397. g_ctx = NULL;
  1398. mg_stop(ctx);
  1399. mark_point();
  1400. #ifdef USE_WEBSOCKET
  1401. for (i = 0; i < 100; i++) {
  1402. test_sleep(1);
  1403. if (ws_client3_data.closed != 0) {
  1404. mark_point();
  1405. break;
  1406. }
  1407. }
  1408. ck_assert_int_eq(ws_client3_data.closed, 1);
  1409. #endif
  1410. }
  1411. END_TEST
  1412. static int g_field_found_return = -999;
  1413. static int
  1414. field_found(const char *key,
  1415. const char *filename,
  1416. char *path,
  1417. size_t pathlen,
  1418. void *user_data)
  1419. {
  1420. ck_assert_ptr_ne(key, NULL);
  1421. ck_assert_ptr_ne(filename, NULL);
  1422. ck_assert_ptr_ne(path, NULL);
  1423. ck_assert_uint_gt(pathlen, 128);
  1424. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1425. ck_assert((g_field_found_return == FORM_FIELD_STORAGE_GET)
  1426. || (g_field_found_return == FORM_FIELD_STORAGE_STORE)
  1427. || (g_field_found_return == FORM_FIELD_STORAGE_SKIP)
  1428. || (g_field_found_return == FORM_FIELD_STORAGE_ABORT));
  1429. ck_assert_str_ne(key, "dontread");
  1430. if (!strcmp(key, "break_field_handler")) {
  1431. return FORM_FIELD_STORAGE_ABORT;
  1432. }
  1433. if (!strcmp(key, "continue_field_handler")) {
  1434. return FORM_FIELD_STORAGE_SKIP;
  1435. }
  1436. if (g_field_found_return == FORM_FIELD_STORAGE_STORE) {
  1437. strncpy(path, key, pathlen - 8);
  1438. strcat(path, ".txt");
  1439. }
  1440. return g_field_found_return;
  1441. }
  1442. static int g_field_step;
  1443. static int
  1444. field_get(const char *key, const char *value, size_t valuelen, void *user_data)
  1445. {
  1446. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1447. ck_assert_int_ge(g_field_step, 0);
  1448. ++g_field_step;
  1449. switch (g_field_step) {
  1450. case 1:
  1451. ck_assert_str_eq(key, "textin");
  1452. ck_assert_uint_eq(valuelen, 4);
  1453. ck_assert_str_eq(value, "text");
  1454. break;
  1455. case 2:
  1456. ck_assert_str_eq(key, "passwordin");
  1457. ck_assert_uint_eq(valuelen, 0);
  1458. ck_assert_str_eq(value, "");
  1459. break;
  1460. case 3:
  1461. ck_assert_str_eq(key, "radio1");
  1462. ck_assert_uint_eq(valuelen, 4);
  1463. ck_assert_str_eq(value, "val1");
  1464. break;
  1465. case 4:
  1466. ck_assert_str_eq(key, "radio2");
  1467. ck_assert_uint_eq(valuelen, 4);
  1468. ck_assert_str_eq(value, "val1");
  1469. break;
  1470. case 5:
  1471. ck_assert_str_eq(key, "check1");
  1472. ck_assert_uint_eq(valuelen, 4);
  1473. ck_assert_str_eq(value, "val1");
  1474. break;
  1475. case 6:
  1476. ck_assert_str_eq(key, "numberin");
  1477. ck_assert_uint_eq(valuelen, 1);
  1478. ck_assert_str_eq(value, "1");
  1479. break;
  1480. case 7:
  1481. ck_assert_str_eq(key, "datein");
  1482. ck_assert_uint_eq(valuelen, 8);
  1483. ck_assert_str_eq(value, "1.1.2016");
  1484. break;
  1485. case 8:
  1486. ck_assert_str_eq(key, "colorin");
  1487. ck_assert_uint_eq(valuelen, 7);
  1488. ck_assert_str_eq(value, "#80ff00");
  1489. break;
  1490. case 9:
  1491. ck_assert_str_eq(key, "rangein");
  1492. ck_assert_uint_eq(valuelen, 1);
  1493. ck_assert_str_eq(value, "3");
  1494. break;
  1495. case 10:
  1496. ck_assert_str_eq(key, "monthin");
  1497. ck_assert_uint_eq(valuelen, 0);
  1498. ck_assert_str_eq(value, "");
  1499. break;
  1500. case 11:
  1501. ck_assert_str_eq(key, "weekin");
  1502. ck_assert_uint_eq(valuelen, 0);
  1503. ck_assert_str_eq(value, "");
  1504. break;
  1505. case 12:
  1506. ck_assert_str_eq(key, "timein");
  1507. ck_assert_uint_eq(valuelen, 0);
  1508. ck_assert_str_eq(value, "");
  1509. break;
  1510. case 13:
  1511. ck_assert_str_eq(key, "datetimen");
  1512. ck_assert_uint_eq(valuelen, 0);
  1513. ck_assert_str_eq(value, "");
  1514. break;
  1515. case 14:
  1516. ck_assert_str_eq(key, "datetimelocalin");
  1517. ck_assert_uint_eq(valuelen, 0);
  1518. ck_assert_str_eq(value, "");
  1519. break;
  1520. case 15:
  1521. ck_assert_str_eq(key, "emailin");
  1522. ck_assert_uint_eq(valuelen, 0);
  1523. ck_assert_str_eq(value, "");
  1524. break;
  1525. case 16:
  1526. ck_assert_str_eq(key, "searchin");
  1527. ck_assert_uint_eq(valuelen, 0);
  1528. ck_assert_str_eq(value, "");
  1529. break;
  1530. case 17:
  1531. ck_assert_str_eq(key, "telin");
  1532. ck_assert_uint_eq(valuelen, 0);
  1533. ck_assert_str_eq(value, "");
  1534. break;
  1535. case 18:
  1536. ck_assert_str_eq(key, "urlin");
  1537. ck_assert_uint_eq(valuelen, 0);
  1538. ck_assert_str_eq(value, "");
  1539. break;
  1540. case 19:
  1541. ck_assert_str_eq(key, "filein");
  1542. ck_assert_uint_eq(valuelen, 0);
  1543. ck_assert_str_eq(value, "");
  1544. break;
  1545. case 20:
  1546. ck_assert_str_eq(key, "filesin");
  1547. ck_assert_uint_eq(valuelen, 0);
  1548. ck_assert_str_eq(value, "");
  1549. break;
  1550. case 21:
  1551. ck_assert_str_eq(key, "selectin");
  1552. ck_assert_uint_eq(valuelen, 4);
  1553. ck_assert_str_eq(value, "opt1");
  1554. break;
  1555. case 22:
  1556. ck_assert_str_eq(key, "message");
  1557. ck_assert_uint_eq(valuelen, 23);
  1558. ck_assert_str_eq(value, "Text area default text.");
  1559. break;
  1560. default:
  1561. ck_abort_msg("field_get called with g_field_step == %i",
  1562. (int)g_field_step);
  1563. }
  1564. return 0;
  1565. }
  1566. static const char *myfile_content = "Content of myfile.txt\r\n";
  1567. static const int myfile_content_rep = 50000;
  1568. static int
  1569. field_store(const char *path, long long file_size, void *user_data)
  1570. {
  1571. FILE *f;
  1572. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1573. ck_assert_int_ge(g_field_step, 100);
  1574. ++g_field_step;
  1575. switch (g_field_step) {
  1576. case 101:
  1577. ck_assert_str_eq(path, "storeme.txt");
  1578. ck_assert_int_eq(file_size, 9);
  1579. f = fopen(path, "r");
  1580. ck_assert_ptr_ne(f, NULL);
  1581. if (f) {
  1582. char buf[32] = {0};
  1583. int i = (int)fread(buf, 1, 31, f);
  1584. ck_assert_int_eq(i, 9);
  1585. fclose(f);
  1586. ck_assert_str_eq(buf, "storetest");
  1587. }
  1588. break;
  1589. case 102:
  1590. ck_assert_str_eq(path, "file2store.txt");
  1591. ck_assert_uint_eq(23, strlen(myfile_content));
  1592. ck_assert_int_eq(file_size, 23 * myfile_content_rep);
  1593. #ifdef _WIN32
  1594. f = fopen(path, "rb");
  1595. #else
  1596. f = fopen(path, "r");
  1597. #endif
  1598. ck_assert_ptr_ne(f, NULL);
  1599. if (f) {
  1600. char buf[32] = {0};
  1601. int r, i;
  1602. for (r = 0; r < myfile_content_rep; r++) {
  1603. i = (int)fread(buf, 1, 23, f);
  1604. ck_assert_int_eq(i, 23);
  1605. ck_assert_str_eq(buf, myfile_content);
  1606. }
  1607. i = (int)fread(buf, 1, 23, f);
  1608. ck_assert_int_eq(i, 0);
  1609. fclose(f);
  1610. }
  1611. break;
  1612. default:
  1613. ck_abort_msg("field_get called with g_field_step == %i",
  1614. (int)g_field_step);
  1615. }
  1616. return 0;
  1617. }
  1618. static int
  1619. FormGet(struct mg_connection *conn, void *cbdata)
  1620. {
  1621. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1622. int ret;
  1623. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  1624. (void)cbdata;
  1625. ck_assert(req_info != NULL);
  1626. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1627. fdh.user_data = (void *)&g_field_found_return;
  1628. /* Call the form handler */
  1629. g_field_step = 0;
  1630. g_field_found_return = FORM_FIELD_STORAGE_GET;
  1631. ret = mg_handle_form_request(conn, &fdh);
  1632. g_field_found_return = -888;
  1633. ck_assert_int_eq(ret, 22);
  1634. ck_assert_int_eq(g_field_step, 22);
  1635. mg_printf(conn, "%i\r\n", ret);
  1636. g_field_step = 1000;
  1637. return 1;
  1638. }
  1639. static int
  1640. FormStore(struct mg_connection *conn,
  1641. void *cbdata,
  1642. int ret_expected,
  1643. int field_step_expected)
  1644. {
  1645. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1646. int ret;
  1647. struct mg_form_data_handler fdh = {field_found,
  1648. field_get,
  1649. field_store,
  1650. NULL};
  1651. (void)cbdata;
  1652. ck_assert(req_info != NULL);
  1653. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1654. fdh.user_data = (void *)&g_field_found_return;
  1655. /* Call the form handler */
  1656. g_field_step = 100;
  1657. g_field_found_return = FORM_FIELD_STORAGE_STORE;
  1658. ret = mg_handle_form_request(conn, &fdh);
  1659. ck_assert_int_eq(ret, ret_expected);
  1660. ck_assert_int_eq(g_field_step, field_step_expected);
  1661. mg_printf(conn, "%i\r\n", ret);
  1662. g_field_step = 1000;
  1663. return 1;
  1664. }
  1665. static int
  1666. FormStore1(struct mg_connection *conn, void *cbdata)
  1667. {
  1668. return FormStore(conn, cbdata, 3, 101);
  1669. }
  1670. static int
  1671. FormStore2(struct mg_connection *conn, void *cbdata)
  1672. {
  1673. return FormStore(conn, cbdata, 4, 102);
  1674. }
  1675. static void
  1676. send_chunk_string(struct mg_connection *conn, const char *txt)
  1677. {
  1678. unsigned int chunk_len = (unsigned int)strlen(txt);
  1679. mg_printf(conn, "%x\r\n", chunk_len);
  1680. mg_write(conn, txt, chunk_len);
  1681. mg_printf(conn, "\r\n");
  1682. }
  1683. START_TEST(test_handle_form)
  1684. {
  1685. struct mg_context *ctx;
  1686. struct mg_connection *client_conn;
  1687. const struct mg_request_info *ri;
  1688. const char *OPTIONS[8];
  1689. const char *opt;
  1690. int opt_idx = 0;
  1691. char ebuf[100];
  1692. const char *multipart_body;
  1693. const char *boundary;
  1694. size_t body_len, body_sent, chunk_len;
  1695. int sleep_cnt;
  1696. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  1697. OPTIONS[opt_idx++] = "listening_ports";
  1698. OPTIONS[opt_idx++] = "8884";
  1699. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  1700. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  1701. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  1702. mark_point();
  1703. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  1704. ck_assert(ctx != NULL);
  1705. g_ctx = ctx;
  1706. opt = mg_get_option(ctx, "listening_ports");
  1707. ck_assert_str_eq(opt, "8884");
  1708. mg_set_request_handler(ctx, "/handle_form", FormGet, (void *)0);
  1709. mg_set_request_handler(ctx, "/handle_form_store", FormStore1, (void *)0);
  1710. mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, (void *)0);
  1711. test_sleep(1);
  1712. /* Handle form: "GET" */
  1713. client_conn = mg_download("localhost",
  1714. 8884,
  1715. 0,
  1716. ebuf,
  1717. sizeof(ebuf),
  1718. "%s",
  1719. "GET /handle_form"
  1720. "?textin=text&passwordin=&radio1=val1"
  1721. "&radio2=val1&check1=val1&numberin=1"
  1722. "&datein=1.1.2016&colorin=%2380ff00"
  1723. "&rangein=3&monthin=&weekin=&timein="
  1724. "&datetimen=&datetimelocalin=&emailin="
  1725. "&searchin=&telin=&urlin=&filein="
  1726. "&filesin=&selectin=opt1"
  1727. "&message=Text+area+default+text. "
  1728. "HTTP/1.0\r\n"
  1729. "Host: localhost:8884\r\n"
  1730. "Connection: close\r\n\r\n");
  1731. ck_assert(client_conn != NULL);
  1732. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1733. test_sleep(1);
  1734. if (g_field_step == 1000) {
  1735. break;
  1736. }
  1737. }
  1738. ri = mg_get_request_info(client_conn);
  1739. ck_assert(ri != NULL);
  1740. ck_assert_str_eq(ri->uri, "200");
  1741. mg_close_connection(client_conn);
  1742. /* Handle form: "POST x-www-form-urlencoded" */
  1743. client_conn =
  1744. mg_download("localhost",
  1745. 8884,
  1746. 0,
  1747. ebuf,
  1748. sizeof(ebuf),
  1749. "%s",
  1750. "POST /handle_form HTTP/1.1\r\n"
  1751. "Host: localhost:8884\r\n"
  1752. "Connection: close\r\n"
  1753. "Content-Type: application/x-www-form-urlencoded\r\n"
  1754. "Content-Length: 263\r\n"
  1755. "\r\n"
  1756. "textin=text&passwordin=&radio1=val1&radio2=val1"
  1757. "&check1=val1&numberin=1&datein=1.1.2016"
  1758. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  1759. "&timein=&datetimen=&datetimelocalin=&emailin="
  1760. "&searchin=&telin=&urlin=&filein=&filesin="
  1761. "&selectin=opt1&message=Text+area+default+text.");
  1762. ck_assert(client_conn != NULL);
  1763. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1764. test_sleep(1);
  1765. if (g_field_step == 1000) {
  1766. break;
  1767. }
  1768. }
  1769. ri = mg_get_request_info(client_conn);
  1770. ck_assert(ri != NULL);
  1771. ck_assert_str_eq(ri->uri, "200");
  1772. mg_close_connection(client_conn);
  1773. /* Handle form: "POST multipart/form-data" */
  1774. multipart_body =
  1775. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1776. "Content-Disposition: form-data; name=\"textin\"\r\n"
  1777. "\r\n"
  1778. "text\r\n"
  1779. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1780. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  1781. "\r\n"
  1782. "\r\n"
  1783. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1784. "Content-Disposition: form-data; name=\"radio1\"\r\n"
  1785. "\r\n"
  1786. "val1\r\n"
  1787. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1788. "Content-Disposition: form-data; name=\"radio2\"\r\n"
  1789. "\r\n"
  1790. "val1\r\n"
  1791. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1792. "Content-Disposition: form-data; name=\"check1\"\r\n"
  1793. "\r\n"
  1794. "val1\r\n"
  1795. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1796. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  1797. "\r\n"
  1798. "1\r\n"
  1799. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1800. "Content-Disposition: form-data; name=\"datein\"\r\n"
  1801. "\r\n"
  1802. "1.1.2016\r\n"
  1803. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1804. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  1805. "\r\n"
  1806. "#80ff00\r\n"
  1807. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1808. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  1809. "\r\n"
  1810. "3\r\n"
  1811. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1812. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  1813. "\r\n"
  1814. "\r\n"
  1815. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1816. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  1817. "\r\n"
  1818. "\r\n"
  1819. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1820. "Content-Disposition: form-data; name=\"timein\"\r\n"
  1821. "\r\n"
  1822. "\r\n"
  1823. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1824. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  1825. "\r\n"
  1826. "\r\n"
  1827. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1828. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  1829. "\r\n"
  1830. "\r\n"
  1831. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1832. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  1833. "\r\n"
  1834. "\r\n"
  1835. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1836. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  1837. "\r\n"
  1838. "\r\n"
  1839. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1840. "Content-Disposition: form-data; name=\"telin\"\r\n"
  1841. "\r\n"
  1842. "\r\n"
  1843. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1844. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  1845. "\r\n"
  1846. "\r\n"
  1847. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1848. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  1849. "Content-Type: application/octet-stream\r\n"
  1850. "\r\n"
  1851. "\r\n"
  1852. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1853. "Content-Disposition: form-data; name=\"filesin\"; filename=\"\"\r\n"
  1854. "Content-Type: application/octet-stream\r\n"
  1855. "\r\n"
  1856. "\r\n"
  1857. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1858. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  1859. "\r\n"
  1860. "opt1\r\n"
  1861. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1862. "Content-Disposition: form-data; name=\"message\"\r\n"
  1863. "\r\n"
  1864. "Text area default text.\r\n"
  1865. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  1866. body_len = strlen(multipart_body);
  1867. ck_assert_uint_eq(body_len, 2374); /* not required */
  1868. client_conn =
  1869. mg_download("localhost",
  1870. 8884,
  1871. 0,
  1872. ebuf,
  1873. sizeof(ebuf),
  1874. "POST /handle_form HTTP/1.1\r\n"
  1875. "Host: localhost:8884\r\n"
  1876. "Connection: close\r\n"
  1877. "Content-Type: multipart/form-data; "
  1878. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  1879. "Content-Length: %u\r\n"
  1880. "\r\n%s",
  1881. (unsigned int)body_len,
  1882. multipart_body);
  1883. ck_assert(client_conn != NULL);
  1884. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1885. test_sleep(1);
  1886. if (g_field_step == 1000) {
  1887. break;
  1888. }
  1889. }
  1890. ri = mg_get_request_info(client_conn);
  1891. ck_assert(ri != NULL);
  1892. ck_assert_str_eq(ri->uri, "200");
  1893. mg_close_connection(client_conn);
  1894. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  1895. client_conn =
  1896. mg_download("localhost",
  1897. 8884,
  1898. 0,
  1899. ebuf,
  1900. sizeof(ebuf),
  1901. "%s",
  1902. "POST /handle_form HTTP/1.1\r\n"
  1903. "Host: localhost:8884\r\n"
  1904. "Connection: close\r\n"
  1905. "Content-Type: multipart/form-data; "
  1906. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  1907. "Transfer-Encoding: chunked\r\n"
  1908. "\r\n");
  1909. ck_assert(client_conn != NULL);
  1910. body_len = strlen(multipart_body);
  1911. chunk_len = 1;
  1912. body_sent = 0;
  1913. while (body_len > body_sent) {
  1914. if (chunk_len > (body_len - body_sent)) {
  1915. chunk_len = body_len - body_sent;
  1916. }
  1917. ck_assert_int_gt((int)chunk_len, 0);
  1918. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  1919. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  1920. mg_printf(client_conn, "\r\n");
  1921. body_sent += chunk_len;
  1922. chunk_len = (chunk_len % 40) + 1;
  1923. }
  1924. mg_printf(client_conn, "0\r\n");
  1925. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1926. test_sleep(1);
  1927. if (g_field_step == 1000) {
  1928. break;
  1929. }
  1930. }
  1931. ri = mg_get_request_info(client_conn);
  1932. ck_assert(ri != NULL);
  1933. ck_assert_str_eq(ri->uri, "200");
  1934. mg_close_connection(client_conn);
  1935. /* Now test form_store */
  1936. /* First test with GET */
  1937. client_conn = mg_download("localhost",
  1938. 8884,
  1939. 0,
  1940. ebuf,
  1941. sizeof(ebuf),
  1942. "%s",
  1943. "GET /handle_form_store"
  1944. "?storeme=storetest"
  1945. "&continue_field_handler=ignore"
  1946. "&break_field_handler=abort"
  1947. "&dontread=xyz "
  1948. "HTTP/1.0\r\n"
  1949. "Host: localhost:8884\r\n"
  1950. "Connection: close\r\n\r\n");
  1951. ck_assert(client_conn != NULL);
  1952. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1953. test_sleep(1);
  1954. if (g_field_step == 1000) {
  1955. break;
  1956. }
  1957. }
  1958. ri = mg_get_request_info(client_conn);
  1959. ck_assert(ri != NULL);
  1960. ck_assert_str_eq(ri->uri, "200");
  1961. mg_close_connection(client_conn);
  1962. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  1963. client_conn =
  1964. mg_download("localhost",
  1965. 8884,
  1966. 0,
  1967. ebuf,
  1968. sizeof(ebuf),
  1969. "%s",
  1970. "POST /handle_form_store HTTP/1.0\r\n"
  1971. "Host: localhost:8884\r\n"
  1972. "Connection: close\r\n"
  1973. "Content-Type: application/x-www-form-urlencoded\r\n"
  1974. "Transfer-Encoding: chunked\r\n"
  1975. "\r\n");
  1976. ck_assert(client_conn != NULL);
  1977. send_chunk_string(client_conn, "storeme=store");
  1978. send_chunk_string(client_conn, "test&");
  1979. send_chunk_string(client_conn, "continue_field_handler=ignore");
  1980. send_chunk_string(client_conn, "&br");
  1981. test_sleep(1);
  1982. send_chunk_string(client_conn, "eak_field_handler=abort&");
  1983. send_chunk_string(client_conn, "dontread=xyz");
  1984. mg_printf(client_conn, "0\r\n");
  1985. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1986. test_sleep(1);
  1987. if (g_field_step == 1000) {
  1988. break;
  1989. }
  1990. }
  1991. ri = mg_get_request_info(client_conn);
  1992. ck_assert(ri != NULL);
  1993. ck_assert_str_eq(ri->uri, "200");
  1994. mg_close_connection(client_conn);
  1995. /* Handle form: "POST multipart/form-data", chunked, store */
  1996. client_conn =
  1997. mg_download("localhost",
  1998. 8884,
  1999. 0,
  2000. ebuf,
  2001. sizeof(ebuf),
  2002. "%s",
  2003. "POST /handle_form_store HTTP/1.0\r\n"
  2004. "Host: localhost:8884\r\n"
  2005. "Connection: close\r\n"
  2006. "Content-Type: multipart/form-data; "
  2007. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2008. "Transfer-Encoding: chunked\r\n"
  2009. "\r\n");
  2010. ck_assert(client_conn != NULL);
  2011. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2012. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  2013. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2014. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2015. send_chunk_string(client_conn, "\r\n");
  2016. send_chunk_string(client_conn, "storetest\r\n");
  2017. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2018. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2019. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2020. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  2021. send_chunk_string(client_conn, "\r\n");
  2022. send_chunk_string(client_conn, "ignore\r\n");
  2023. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2024. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2025. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2026. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2027. send_chunk_string(client_conn, "\r\n");
  2028. send_chunk_string(client_conn, "abort\r\n");
  2029. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2030. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2031. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2032. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2033. send_chunk_string(client_conn, "\r\n");
  2034. send_chunk_string(client_conn, "xyz\r\n");
  2035. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2036. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2037. mg_printf(client_conn, "0\r\n");
  2038. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2039. test_sleep(1);
  2040. if (g_field_step == 1000) {
  2041. break;
  2042. }
  2043. }
  2044. ri = mg_get_request_info(client_conn);
  2045. ck_assert(ri != NULL);
  2046. ck_assert_str_eq(ri->uri, "200");
  2047. mg_close_connection(client_conn);
  2048. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  2049. client_conn =
  2050. mg_download("localhost",
  2051. 8884,
  2052. 0,
  2053. ebuf,
  2054. sizeof(ebuf),
  2055. "%s",
  2056. "POST /handle_form_store2 HTTP/1.0\r\n"
  2057. "Host: localhost:8884\r\n"
  2058. "Connection: close\r\n"
  2059. "Content-Type: multipart/form-data; "
  2060. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2061. "Transfer-Encoding: chunked\r\n"
  2062. "\r\n");
  2063. ck_assert(client_conn != NULL);
  2064. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  2065. send_chunk_string(client_conn, boundary);
  2066. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2067. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2068. send_chunk_string(client_conn, "\r\n");
  2069. send_chunk_string(client_conn, "storetest\r\n");
  2070. send_chunk_string(client_conn, boundary);
  2071. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2072. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2073. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  2074. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  2075. send_chunk_string(client_conn, "Content-Type: ");
  2076. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2077. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2078. send_chunk_string(client_conn, "\r\n");
  2079. /* send some megabyte of data */
  2080. body_sent = 0;
  2081. do {
  2082. send_chunk_string(client_conn, "ignore\r\n");
  2083. body_sent += 8;
  2084. /* send some strings that are almost boundaries */
  2085. for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
  2086. /* chunks from 1 byte to strlen(boundary)-1 */
  2087. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2088. mg_write(client_conn, boundary, chunk_len);
  2089. mg_printf(client_conn, "\r\n");
  2090. body_sent += chunk_len;
  2091. }
  2092. } while (body_sent < 1024 * 1024);
  2093. send_chunk_string(client_conn, "\r\n");
  2094. send_chunk_string(client_conn, boundary);
  2095. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2096. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2097. send_chunk_string(client_conn, "name=\"file2store\";");
  2098. send_chunk_string(client_conn, "filename=\"myfile.txt\"\r\n");
  2099. send_chunk_string(client_conn, "Content-Type: ");
  2100. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2101. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2102. send_chunk_string(client_conn, "\r\n");
  2103. for (body_sent = 0; (int)body_sent < (int)myfile_content_rep; body_sent++) {
  2104. send_chunk_string(client_conn, myfile_content);
  2105. }
  2106. send_chunk_string(client_conn, "\r\n");
  2107. send_chunk_string(client_conn, boundary);
  2108. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2109. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2110. send_chunk_string(client_conn, "\r\n");
  2111. send_chunk_string(client_conn, "abort\r\n");
  2112. send_chunk_string(client_conn, boundary);
  2113. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2114. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2115. send_chunk_string(client_conn, "\r\n");
  2116. send_chunk_string(client_conn, "xyz\r\n");
  2117. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2118. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2119. mg_printf(client_conn, "0\r\n");
  2120. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2121. test_sleep(1);
  2122. if (g_field_step == 1000) {
  2123. break;
  2124. }
  2125. }
  2126. ri = mg_get_request_info(client_conn);
  2127. ck_assert(ri != NULL);
  2128. ck_assert_str_eq(ri->uri, "200");
  2129. mg_close_connection(client_conn);
  2130. /* Close the server */
  2131. g_ctx = NULL;
  2132. mg_stop(ctx);
  2133. mark_point();
  2134. }
  2135. END_TEST
  2136. START_TEST(test_http_auth)
  2137. {
  2138. #if !defined(NO_FILES)
  2139. const char *OPTIONS[] = {
  2140. "document_root",
  2141. ".",
  2142. "listening_ports",
  2143. "8080",
  2144. #if !defined(NO_CACHING)
  2145. "static_file_max_age",
  2146. "0",
  2147. #endif
  2148. NULL,
  2149. };
  2150. struct mg_context *ctx;
  2151. struct mg_connection *client_conn;
  2152. char client_err[256], nonce[256];
  2153. const struct mg_request_info *client_ri;
  2154. int client_res;
  2155. FILE *f;
  2156. const char *passwd_file = ".htpasswd";
  2157. const char *test_file = "test_http_auth.test_file.txt";
  2158. const char *test_content = "test_http_auth test_file content";
  2159. const char *domain;
  2160. const char *doc_root;
  2161. const char *auth_request;
  2162. const char *str;
  2163. size_t len;
  2164. int i;
  2165. char HA1[256], HA2[256], HA[256];
  2166. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2167. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2168. const char *nc = "00000001";
  2169. const char *cnonce = "6789ABCD";
  2170. /* Start with default options */
  2171. mark_point();
  2172. ctx = mg_start(NULL, NULL, OPTIONS);
  2173. test_sleep(1);
  2174. ck_assert(ctx != NULL);
  2175. domain = mg_get_option(ctx, "authentication_domain");
  2176. ck_assert(domain != NULL);
  2177. len = strlen(domain);
  2178. ck_assert_uint_gt(len, 0);
  2179. ck_assert_uint_lt(len, 64);
  2180. doc_root = mg_get_option(ctx, "document_root");
  2181. ck_assert_str_eq(doc_root, ".");
  2182. /* Create a default file in the document root */
  2183. f = fopen(test_file, "w");
  2184. if (f) {
  2185. fprintf(f, "%s", test_content);
  2186. fclose(f);
  2187. } else {
  2188. ck_abort_msg("Cannot create file %s", test_file);
  2189. }
  2190. remove(passwd_file);
  2191. /* Read file before a .htpasswd file has been created */
  2192. memset(client_err, 0, sizeof(client_err));
  2193. client_conn =
  2194. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2195. ck_assert(client_conn != NULL);
  2196. ck_assert_str_eq(client_err, "");
  2197. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2198. client_res =
  2199. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2200. ck_assert_int_ge(client_res, 0);
  2201. ck_assert_str_eq(client_err, "");
  2202. client_ri = mg_get_request_info(client_conn);
  2203. ck_assert(client_ri != NULL);
  2204. ck_assert_str_eq(client_ri->uri, "200");
  2205. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2206. ck_assert_int_gt(client_res, 0);
  2207. ck_assert_int_le(client_res, sizeof(client_err));
  2208. ck_assert_str_eq(client_err, test_content);
  2209. mg_close_connection(client_conn);
  2210. test_sleep(1);
  2211. /* Create a .htpasswd file */
  2212. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2213. ck_assert_int_eq(client_res, 1);
  2214. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2215. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2216. test_sleep(1);
  2217. /* Repeat test after .htpasswd is created */
  2218. memset(client_err, 0, sizeof(client_err));
  2219. client_conn =
  2220. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2221. ck_assert(client_conn != NULL);
  2222. ck_assert_str_eq(client_err, "");
  2223. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2224. client_res =
  2225. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2226. ck_assert_int_ge(client_res, 0);
  2227. ck_assert_str_eq(client_err, "");
  2228. client_ri = mg_get_request_info(client_conn);
  2229. ck_assert(client_ri != NULL);
  2230. ck_assert_str_eq(client_ri->uri, "401");
  2231. auth_request = NULL;
  2232. for (i = 0; i < client_ri->num_headers; i++) {
  2233. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2234. "WWW-Authenticate")) {
  2235. ck_assert_ptr_eq(auth_request, NULL);
  2236. auth_request = client_ri->http_headers[i].value;
  2237. ck_assert_ptr_ne(auth_request, NULL);
  2238. }
  2239. }
  2240. ck_assert_ptr_ne(auth_request, NULL);
  2241. str = "Digest qop=\"auth\", realm=\"";
  2242. len = strlen(str);
  2243. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2244. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2245. len += strlen(domain);
  2246. str = "\", nonce=\"";
  2247. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2248. len += strlen(str);
  2249. str = strchr(auth_request + len, '\"');
  2250. ck_assert_ptr_ne(str, NULL);
  2251. ck_assert_ptr_ne(str, auth_request + len);
  2252. /* nonce is from including (auth_request + len) to excluding (str) */
  2253. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2254. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2255. (ptrdiff_t)sizeof(nonce));
  2256. memset(nonce, 0, sizeof(nonce));
  2257. memcpy(nonce,
  2258. auth_request + len,
  2259. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2260. memset(HA1, 0, sizeof(HA1));
  2261. memset(HA2, 0, sizeof(HA2));
  2262. memset(HA, 0, sizeof(HA));
  2263. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2264. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2265. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2266. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2267. sprintf(HA2, "%s:/%s", "GET", test_file);
  2268. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2269. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2270. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2271. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2272. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2273. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2274. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2275. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2276. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2277. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2278. HA_md5_ret = mg_md5(HA_md5_buf,
  2279. HA1_md5_buf,
  2280. ":",
  2281. nonce,
  2282. ":",
  2283. nc,
  2284. ":",
  2285. cnonce,
  2286. ":",
  2287. "auth",
  2288. ":",
  2289. HA2_md5_buf,
  2290. NULL);
  2291. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2292. /* Retry with Authorization */
  2293. memset(client_err, 0, sizeof(client_err));
  2294. client_conn =
  2295. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2296. ck_assert(client_conn != NULL);
  2297. ck_assert_str_eq(client_err, "");
  2298. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2299. mg_printf(client_conn,
  2300. "Authorization: Digest "
  2301. "username=\"%s\", "
  2302. "realm=\"%s\", "
  2303. "nonce=\"%s\", "
  2304. "uri=\"/%s\", "
  2305. "qop=auth, "
  2306. "nc=%s, "
  2307. "cnonce=\"%s\", "
  2308. "response=\"%s\"\r\n\r\n",
  2309. "user",
  2310. domain,
  2311. nonce,
  2312. test_file,
  2313. nc,
  2314. cnonce,
  2315. HA_md5_buf);
  2316. client_res =
  2317. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2318. ck_assert_int_ge(client_res, 0);
  2319. ck_assert_str_eq(client_err, "");
  2320. client_ri = mg_get_request_info(client_conn);
  2321. ck_assert(client_ri != NULL);
  2322. ck_assert_str_eq(client_ri->uri, "200");
  2323. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2324. ck_assert_int_gt(client_res, 0);
  2325. ck_assert_int_le(client_res, sizeof(client_err));
  2326. ck_assert_str_eq(client_err, test_content);
  2327. mg_close_connection(client_conn);
  2328. test_sleep(1);
  2329. /* Remove the user from the .htpasswd file again */
  2330. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2331. ck_assert_int_eq(client_res, 1);
  2332. test_sleep(1);
  2333. /* Try to access the file again. Expected: 401 error */
  2334. memset(client_err, 0, sizeof(client_err));
  2335. client_conn =
  2336. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2337. ck_assert(client_conn != NULL);
  2338. ck_assert_str_eq(client_err, "");
  2339. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2340. client_res =
  2341. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2342. ck_assert_int_ge(client_res, 0);
  2343. ck_assert_str_eq(client_err, "");
  2344. client_ri = mg_get_request_info(client_conn);
  2345. ck_assert(client_ri != NULL);
  2346. ck_assert_str_eq(client_ri->uri, "401");
  2347. mg_close_connection(client_conn);
  2348. test_sleep(1);
  2349. /* Now remove the password file */
  2350. remove(passwd_file);
  2351. test_sleep(1);
  2352. /* Access to the file must work like before */
  2353. memset(client_err, 0, sizeof(client_err));
  2354. client_conn =
  2355. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2356. ck_assert(client_conn != NULL);
  2357. ck_assert_str_eq(client_err, "");
  2358. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2359. client_res =
  2360. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2361. ck_assert_int_ge(client_res, 0);
  2362. ck_assert_str_eq(client_err, "");
  2363. client_ri = mg_get_request_info(client_conn);
  2364. ck_assert(client_ri != NULL);
  2365. ck_assert_str_eq(client_ri->uri, "200");
  2366. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2367. ck_assert_int_gt(client_res, 0);
  2368. ck_assert_int_le(client_res, sizeof(client_err));
  2369. ck_assert_str_eq(client_err, test_content);
  2370. mg_close_connection(client_conn);
  2371. test_sleep(1);
  2372. /* Stop the server and clean up */
  2373. mg_stop(ctx);
  2374. remove(test_file);
  2375. #endif
  2376. }
  2377. END_TEST
  2378. Suite *
  2379. make_public_server_suite(void)
  2380. {
  2381. Suite *const suite = suite_create("PublicServer");
  2382. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  2383. TCase *const tcase_startthreads = tcase_create("Start threads");
  2384. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  2385. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  2386. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  2387. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  2388. TCase *const tcase_handle_form = tcase_create("Handle Form");
  2389. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  2390. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  2391. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  2392. suite_add_tcase(suite, tcase_checktestenv);
  2393. tcase_add_test(tcase_startthreads, test_threading);
  2394. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  2395. suite_add_tcase(suite, tcase_startthreads);
  2396. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  2397. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  2398. suite_add_tcase(suite, tcase_startstophttp);
  2399. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  2400. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  2401. suite_add_tcase(suite, tcase_startstophttps);
  2402. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  2403. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  2404. suite_add_tcase(suite, tcase_serverandclienttls);
  2405. tcase_add_test(tcase_serverrequests, test_request_handlers);
  2406. tcase_set_timeout(tcase_serverrequests, 120);
  2407. suite_add_tcase(suite, tcase_serverrequests);
  2408. tcase_add_test(tcase_handle_form, test_handle_form);
  2409. tcase_set_timeout(tcase_handle_form, 300);
  2410. suite_add_tcase(suite, tcase_handle_form);
  2411. tcase_add_test(tcase_http_auth, test_http_auth);
  2412. tcase_set_timeout(tcase_http_auth, 60);
  2413. suite_add_tcase(suite, tcase_http_auth);
  2414. return suite;
  2415. }
  2416. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  2417. /* Used to debug test cases without using the check framework */
  2418. static int chk_ok = 0;
  2419. static int chk_failed = 0;
  2420. void
  2421. MAIN_PUBLIC_SERVER(void)
  2422. {
  2423. /*
  2424. test_the_test_environment(0);
  2425. test_threading(0);
  2426. */
  2427. test_mg_start_stop_http_server(0);
  2428. /*
  2429. test_mg_start_stop_https_server(0);
  2430. test_request_handlers(0);
  2431. test_mg_server_and_client_tls(0);
  2432. test_handle_form(0);
  2433. test_http_auth(0);
  2434. */
  2435. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  2436. }
  2437. void
  2438. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  2439. {
  2440. va_list va;
  2441. va_start(va, expr);
  2442. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2443. vfprintf(stderr, expr, va);
  2444. fprintf(stderr, "\n\n");
  2445. va_end(va);
  2446. chk_failed++;
  2447. }
  2448. void
  2449. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  2450. {
  2451. va_list va;
  2452. if (cond) {
  2453. chk_ok++;
  2454. return;
  2455. }
  2456. va_start(va, expr);
  2457. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2458. vfprintf(stderr, expr, va);
  2459. fprintf(stderr, "\n\n");
  2460. va_end(va);
  2461. chk_failed++;
  2462. }
  2463. void
  2464. _mark_point(const char *file, int line)
  2465. {
  2466. chk_ok++;
  2467. }
  2468. void
  2469. tcase_fn_start(const char *fname, const char *file, int line)
  2470. {
  2471. }
  2472. void suite_add_tcase(Suite *s, TCase *tc){};
  2473. void _tcase_add_test(TCase *tc,
  2474. TFun tf,
  2475. const char *fname,
  2476. int _signal,
  2477. int allowed_exit_value,
  2478. int start,
  2479. int end){};
  2480. TCase *
  2481. tcase_create(const char *name)
  2482. {
  2483. return NULL;
  2484. };
  2485. Suite *
  2486. suite_create(const char *name)
  2487. {
  2488. return NULL;
  2489. };
  2490. void tcase_set_timeout(TCase *tc, double timeout){};
  2491. #endif