public_server.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182
  1. /* Copyright (c) 2015-2017 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. #define SLEEP_BEFORE_MG_START (1)
  43. #define SLEEP_AFTER_MG_START (3)
  44. #define SLEEP_BEFORE_MG_STOP (1)
  45. #define SLEEP_AFTER_MG_STOP (5)
  46. /* This unit test file uses the excellent Check unit testing library.
  47. * The API documentation is available here:
  48. * http://check.sourceforge.net/doc/check_html/index.html
  49. */
  50. #if defined(__MINGW32__) || defined(__GNUC__)
  51. /* Return codes of the tested functions are evaluated. Checking all other
  52. * functions, used only to prepare the test environment seems redundant.
  53. * If they fail, the test fails anyway. */
  54. #pragma GCC diagnostic ignored "-Wunused-result"
  55. #endif
  56. static const char *
  57. locate_path(const char *a_path)
  58. {
  59. static char r_path[256];
  60. #ifdef _WIN32
  61. #ifdef LOCAL_TEST
  62. sprintf(r_path, "%s\\", a_path);
  63. #else
  64. /* Appveyor */
  65. sprintf(r_path, "..\\..\\..\\%s\\", a_path);
  66. /* TODO: the different paths
  67. * used in the different test
  68. * system is an unsolved
  69. * problem. */
  70. #endif
  71. #else
  72. #ifdef LOCAL_TEST
  73. sprintf(r_path, "%s/", a_path);
  74. #else
  75. /* Travis */
  76. sprintf(r_path,
  77. "../../%s/",
  78. a_path); // TODO: fix path in CI test environment
  79. #endif
  80. #endif
  81. return r_path;
  82. }
  83. #define locate_resources() locate_path("resources")
  84. #define locate_test_exes() locate_path("output")
  85. static const char *
  86. locate_ssl_cert(void)
  87. {
  88. static char cert_path[256];
  89. const char *res = locate_resources();
  90. size_t l;
  91. ck_assert(res != NULL);
  92. l = strlen(res);
  93. ck_assert_uint_gt(l, 0);
  94. ck_assert_uint_lt(l, 100); /* assume there is enough space left in our
  95. typical 255 character string buffers */
  96. strcpy(cert_path, res);
  97. strcat(cert_path, "ssl_cert.pem");
  98. return cert_path;
  99. }
  100. static int
  101. wait_not_null(void *volatile *data)
  102. {
  103. int i;
  104. for (i = 0; i < 100; i++) {
  105. mark_point();
  106. test_sleep(1);
  107. if (*data != NULL) {
  108. mark_point();
  109. return 1;
  110. }
  111. }
  112. #if defined(__MINGW32__) || defined(__GNUC__)
  113. #pragma GCC diagnostic push
  114. #pragma GCC diagnostic ignored "-Wunreachable-code"
  115. #endif
  116. #ifdef __clang__
  117. #pragma clang diagnostic push
  118. #pragma clang diagnostic ignored "-Wunreachable-code"
  119. #endif
  120. ck_abort_msg("wait_not_null failed");
  121. return 0;
  122. #ifdef __clang__
  123. #pragma clang diagnostic pop
  124. #endif
  125. #if defined(__MINGW32__) || defined(__GNUC__)
  126. #pragma GCC diagnostic pop
  127. #endif
  128. }
  129. START_TEST(test_the_test_environment)
  130. {
  131. char wd[300];
  132. char buf[500];
  133. FILE *f;
  134. struct stat st;
  135. int ret;
  136. const char *ssl_cert = locate_ssl_cert();
  137. memset(wd, 0, sizeof(wd));
  138. memset(buf, 0, sizeof(buf));
  139. /* Get the current working directory */
  140. #ifdef _WIN32
  141. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  142. wd[sizeof(wd) - 1] = 0;
  143. #else
  144. (void)getcwd(wd, sizeof(wd));
  145. wd[sizeof(wd) - 1] = 0;
  146. #endif
  147. /* Check the pem file */
  148. #ifdef _WIN32
  149. strcpy(buf, wd);
  150. strcat(buf, "\\");
  151. strcat(buf, ssl_cert);
  152. f = fopen(buf, "rb");
  153. #else
  154. strcpy(buf, wd);
  155. strcat(buf, "/");
  156. strcat(buf, ssl_cert);
  157. f = fopen(buf, "r");
  158. #endif
  159. if (f) {
  160. fclose(f);
  161. } else {
  162. fprintf(stderr, "%s not found", buf);
  163. }
  164. /* Check the test dir */
  165. #ifdef _WIN32
  166. strcpy(buf, wd);
  167. strcat(buf, "\\test");
  168. #else
  169. strcpy(buf, wd);
  170. strcat(buf, "/test");
  171. #endif
  172. memset(&st, 0, sizeof(st));
  173. ret = stat(buf, &st);
  174. if (ret) {
  175. fprintf(stderr, "%s not found", buf);
  176. }
  177. #ifdef _WIN32
  178. /* Try to copy the files required for AppVeyor */
  179. #if defined(_WIN64) || defined(__MINGW64__)
  180. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libeay32.dll libeay32.dll");
  181. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libssl32.dll libssl32.dll");
  182. (void)system("cmd /c copy C:\\OpenSSL-Win64\\ssleay32.dll ssleay32.dll");
  183. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libeay32.dll libeay64.dll");
  184. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libssl32.dll libssl64.dll");
  185. (void)system("cmd /c copy C:\\OpenSSL-Win64\\ssleay32.dll ssleay64.dll");
  186. #else
  187. (void)system("cmd /c copy C:\\OpenSSL-Win32\\libeay32.dll libeay32.dll");
  188. (void)system("cmd /c copy C:\\OpenSSL-Win32\\libssl32.dll libssl32.dll");
  189. (void)system("cmd /c copy C:\\OpenSSL-Win32\\ssleay32.dll ssleay32.dll");
  190. #endif
  191. #endif
  192. }
  193. END_TEST
  194. static void *threading_data;
  195. static void *
  196. test_thread_func_t(void *param)
  197. {
  198. ck_assert_ptr_eq(param, &threading_data);
  199. ck_assert_ptr_eq(threading_data, NULL);
  200. threading_data = &threading_data;
  201. return NULL;
  202. }
  203. START_TEST(test_threading)
  204. {
  205. int ok;
  206. threading_data = NULL;
  207. ok = mg_start_thread(test_thread_func_t, &threading_data);
  208. ck_assert_int_eq(ok, 0);
  209. wait_not_null(&threading_data);
  210. ck_assert_ptr_eq(threading_data, &threading_data);
  211. }
  212. END_TEST
  213. static int
  214. log_msg_func(const struct mg_connection *conn, const char *message)
  215. {
  216. struct mg_context *ctx;
  217. char *ud;
  218. ck_assert(conn != NULL);
  219. ctx = mg_get_context(conn);
  220. ck_assert(ctx != NULL);
  221. ud = (char *)mg_get_user_data(ctx);
  222. strncpy(ud, message, 255);
  223. ud[255] = 0;
  224. return 1;
  225. }
  226. static struct mg_context *
  227. test_mg_start(const struct mg_callbacks *callbacks,
  228. void *user_data,
  229. const char **configuration_options)
  230. {
  231. struct mg_context *ctx;
  232. mark_point();
  233. test_sleep(SLEEP_BEFORE_MG_START);
  234. mark_point();
  235. ctx = mg_start(callbacks, user_data, configuration_options);
  236. mark_point();
  237. if (ctx) {
  238. /* Give the server some time to start in the test VM */
  239. /* Don't need to do this if mg_start failed */
  240. test_sleep(SLEEP_AFTER_MG_START);
  241. }
  242. mark_point();
  243. return ctx;
  244. }
  245. static void
  246. test_mg_stop(struct mg_context *ctx)
  247. {
  248. mark_point();
  249. test_sleep(SLEEP_BEFORE_MG_STOP);
  250. mark_point();
  251. mg_stop(ctx);
  252. mark_point();
  253. test_sleep(SLEEP_AFTER_MG_STOP);
  254. mark_point();
  255. }
  256. START_TEST(test_mg_start_stop_http_server)
  257. {
  258. struct mg_context *ctx;
  259. const char *OPTIONS[] = {
  260. #if !defined(NO_FILES)
  261. "document_root",
  262. ".",
  263. #endif
  264. "listening_ports",
  265. #if defined(USE_IPV6)
  266. "+8080",
  267. #else
  268. "8080",
  269. #endif
  270. NULL,
  271. };
  272. size_t ports_cnt;
  273. int ports[16];
  274. int ssl[16];
  275. struct mg_callbacks callbacks;
  276. char errmsg[256];
  277. struct mg_connection *client_conn;
  278. char client_err[256];
  279. const struct mg_request_info *client_ri;
  280. int client_res, ret;
  281. struct mg_server_ports portinfo[8];
  282. memset(ports, 0, sizeof(ports));
  283. memset(ssl, 0, sizeof(ssl));
  284. memset(portinfo, 0, sizeof(portinfo));
  285. memset(&callbacks, 0, sizeof(callbacks));
  286. memset(errmsg, 0, sizeof(errmsg));
  287. callbacks.log_message = log_msg_func;
  288. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  289. ck_assert_str_eq(errmsg, "");
  290. ck_assert(ctx != NULL);
  291. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  292. ck_assert_uint_eq(ports_cnt, 1);
  293. ck_assert_int_eq(ports[0], 8080);
  294. ck_assert_int_eq(ssl[0], 0);
  295. ck_assert_int_eq(ports[1], 0);
  296. ck_assert_int_eq(ssl[1], 0);
  297. ret = mg_get_server_ports(ctx, 0, portinfo);
  298. ck_assert_int_lt(ret, 0);
  299. ck_assert_int_eq(portinfo[0].protocol, 0);
  300. ck_assert_int_eq(portinfo[0].port, 0);
  301. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  302. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  303. ck_assert_int_eq(portinfo[1].protocol, 0);
  304. ck_assert_int_eq(portinfo[1].port, 0);
  305. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  306. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  307. ret = mg_get_server_ports(ctx, 4, portinfo);
  308. ck_assert_int_eq(ret, 1);
  309. #if defined(USE_IPV6)
  310. ck_assert_int_eq(portinfo[0].protocol, 3);
  311. #else
  312. ck_assert_int_eq(portinfo[0].protocol, 1);
  313. #endif
  314. ck_assert_int_eq(portinfo[0].port, 8080);
  315. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  316. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  317. ck_assert_int_eq(portinfo[1].protocol, 0);
  318. ck_assert_int_eq(portinfo[1].port, 0);
  319. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  320. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  321. test_sleep(1);
  322. /* HTTP 1.0 GET request */
  323. memset(client_err, 0, sizeof(client_err));
  324. client_conn =
  325. mg_connect_client("localhost", 8080, 0, client_err, sizeof(client_err));
  326. ck_assert(client_conn != NULL);
  327. ck_assert_str_eq(client_err, "");
  328. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  329. client_res =
  330. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  331. ck_assert_int_ge(client_res, 0);
  332. ck_assert_str_eq(client_err, "");
  333. client_ri = mg_get_request_info(client_conn);
  334. ck_assert(client_ri != NULL);
  335. #if defined(NO_FILES)
  336. ck_assert_str_eq(client_ri->uri, "404");
  337. #else
  338. ck_assert_str_eq(client_ri->uri, "200");
  339. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  340. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  341. ck_assert_int_gt(client_res, 0);
  342. ck_assert_int_le(client_res, sizeof(client_err));
  343. #endif
  344. mg_close_connection(client_conn);
  345. test_sleep(1);
  346. /* HTTP 1.1 GET request */
  347. memset(client_err, 0, sizeof(client_err));
  348. client_conn =
  349. mg_connect_client("localhost", 8080, 0, client_err, sizeof(client_err));
  350. ck_assert(client_conn != NULL);
  351. ck_assert_str_eq(client_err, "");
  352. mg_printf(client_conn, "GET / HTTP/1.1\r\n");
  353. mg_printf(client_conn, "Host: localhost:8080\r\n");
  354. mg_printf(client_conn, "Connection: close\r\n\r\n");
  355. client_res =
  356. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  357. ck_assert_int_ge(client_res, 0);
  358. ck_assert_str_eq(client_err, "");
  359. client_ri = mg_get_request_info(client_conn);
  360. ck_assert(client_ri != NULL);
  361. #if defined(NO_FILES)
  362. ck_assert_str_eq(client_ri->uri, "404");
  363. #else
  364. ck_assert_str_eq(client_ri->uri, "200");
  365. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  366. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  367. ck_assert_int_gt(client_res, 0);
  368. ck_assert_int_le(client_res, sizeof(client_err));
  369. #endif
  370. mg_close_connection(client_conn);
  371. test_sleep(1);
  372. /* HTTP 1.7 GET request - this HTTP version does not exist */
  373. memset(client_err, 0, sizeof(client_err));
  374. client_conn =
  375. mg_connect_client("localhost", 8080, 0, client_err, sizeof(client_err));
  376. ck_assert(client_conn != NULL);
  377. ck_assert_str_eq(client_err, "");
  378. mg_printf(client_conn, "GET / HTTP/1.7\r\n");
  379. mg_printf(client_conn, "Host: localhost:8080\r\n");
  380. mg_printf(client_conn, "Connection: close\r\n\r\n");
  381. client_res =
  382. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  383. ck_assert_int_ge(client_res, 0);
  384. ck_assert_str_eq(client_err, "");
  385. client_ri = mg_get_request_info(client_conn);
  386. ck_assert(client_ri != NULL);
  387. /* Response must be 505 HTTP Version not supported */
  388. ck_assert_str_eq(client_ri->uri, "505");
  389. mg_close_connection(client_conn);
  390. test_sleep(1);
  391. /* HTTP request with multiline header.
  392. * Multiline header are obsolete with RFC 7230 section-3.2.4
  393. * and must return "400 Bad Request" */
  394. memset(client_err, 0, sizeof(client_err));
  395. client_conn =
  396. mg_connect_client("localhost", 8080, 0, client_err, sizeof(client_err));
  397. ck_assert(client_conn != NULL);
  398. ck_assert_str_eq(client_err, "");
  399. mg_printf(client_conn, "GET / HTTP/1.1\r\n");
  400. mg_printf(client_conn, "Host: localhost:8080\r\n");
  401. mg_printf(client_conn, "X-Obsolete-Header: something\r\nfor nothing\r\n");
  402. mg_printf(client_conn, "Connection: close\r\n\r\n");
  403. client_res =
  404. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  405. ck_assert_int_ge(client_res, 0);
  406. ck_assert_str_eq(client_err, "");
  407. client_ri = mg_get_request_info(client_conn);
  408. ck_assert(client_ri != NULL);
  409. /* Response must be 400 Bad Request */
  410. ck_assert_str_eq(client_ri->uri, "400");
  411. mg_close_connection(client_conn);
  412. test_sleep(1);
  413. /* End test */
  414. test_mg_stop(ctx);
  415. }
  416. END_TEST
  417. START_TEST(test_mg_start_stop_https_server)
  418. {
  419. #ifndef NO_SSL
  420. struct mg_context *ctx;
  421. size_t ports_cnt;
  422. int ports[16];
  423. int ssl[16];
  424. struct mg_callbacks callbacks;
  425. char errmsg[256];
  426. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  427. int opt_idx = 0;
  428. const char *ssl_cert = locate_ssl_cert();
  429. struct mg_connection *client_conn;
  430. char client_err[256];
  431. const struct mg_request_info *client_ri;
  432. int client_res, ret;
  433. struct mg_server_ports portinfo[8];
  434. ck_assert(ssl_cert != NULL);
  435. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  436. #if !defined(NO_FILES)
  437. OPTIONS[opt_idx++] = "document_root";
  438. OPTIONS[opt_idx++] = ".";
  439. #endif
  440. OPTIONS[opt_idx++] = "listening_ports";
  441. OPTIONS[opt_idx++] = "8080r,8443s";
  442. OPTIONS[opt_idx++] = "ssl_certificate";
  443. OPTIONS[opt_idx++] = ssl_cert;
  444. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  445. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  446. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  447. memset(ports, 0, sizeof(ports));
  448. memset(ssl, 0, sizeof(ssl));
  449. memset(portinfo, 0, sizeof(portinfo));
  450. memset(&callbacks, 0, sizeof(callbacks));
  451. memset(errmsg, 0, sizeof(errmsg));
  452. callbacks.log_message = log_msg_func;
  453. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  454. ck_assert_str_eq(errmsg, "");
  455. ck_assert(ctx != NULL);
  456. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  457. ck_assert_uint_eq(ports_cnt, 2);
  458. ck_assert_int_eq(ports[0], 8080);
  459. ck_assert_int_eq(ssl[0], 0);
  460. ck_assert_int_eq(ports[1], 8443);
  461. ck_assert_int_eq(ssl[1], 1);
  462. ck_assert_int_eq(ports[2], 0);
  463. ck_assert_int_eq(ssl[2], 0);
  464. ret = mg_get_server_ports(ctx, 0, portinfo);
  465. ck_assert_int_lt(ret, 0);
  466. ck_assert_int_eq(portinfo[0].protocol, 0);
  467. ck_assert_int_eq(portinfo[0].port, 0);
  468. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  469. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  470. ck_assert_int_eq(portinfo[1].protocol, 0);
  471. ck_assert_int_eq(portinfo[1].port, 0);
  472. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  473. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  474. ret = mg_get_server_ports(ctx, 4, portinfo);
  475. ck_assert_int_eq(ret, 2);
  476. ck_assert_int_eq(portinfo[0].protocol, 1);
  477. ck_assert_int_eq(portinfo[0].port, 8080);
  478. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  479. ck_assert_int_eq(portinfo[0].is_redirect, 1);
  480. ck_assert_int_eq(portinfo[1].protocol, 1);
  481. ck_assert_int_eq(portinfo[1].port, 8443);
  482. ck_assert_int_eq(portinfo[1].is_ssl, 1);
  483. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  484. ck_assert_int_eq(portinfo[2].protocol, 0);
  485. ck_assert_int_eq(portinfo[2].port, 0);
  486. ck_assert_int_eq(portinfo[2].is_ssl, 0);
  487. ck_assert_int_eq(portinfo[2].is_redirect, 0);
  488. test_sleep(1);
  489. memset(client_err, 0, sizeof(client_err));
  490. client_conn =
  491. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  492. ck_assert(client_conn != NULL);
  493. ck_assert_str_eq(client_err, "");
  494. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  495. client_res =
  496. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  497. ck_assert_int_ge(client_res, 0);
  498. ck_assert_str_eq(client_err, "");
  499. client_ri = mg_get_request_info(client_conn);
  500. ck_assert(client_ri != NULL);
  501. #if defined(NO_FILES)
  502. ck_assert_str_eq(client_ri->uri, "404");
  503. #else
  504. ck_assert_str_eq(client_ri->uri, "200");
  505. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  506. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  507. ck_assert_int_gt(client_res, 0);
  508. ck_assert_int_le(client_res, sizeof(client_err));
  509. #endif
  510. mg_close_connection(client_conn);
  511. test_sleep(1);
  512. test_mg_stop(ctx);
  513. #endif
  514. }
  515. END_TEST
  516. START_TEST(test_mg_server_and_client_tls)
  517. {
  518. #ifndef NO_SSL
  519. struct mg_context *ctx;
  520. int ports_cnt;
  521. struct mg_server_ports ports[16];
  522. struct mg_callbacks callbacks;
  523. char errmsg[256];
  524. struct mg_connection *client_conn;
  525. char client_err[256];
  526. const struct mg_request_info *client_ri;
  527. int client_res;
  528. struct mg_client_options client_options;
  529. const char *OPTIONS[32]; /* initializer list here is rejected by CI test */
  530. int opt_idx = 0;
  531. char server_cert[256];
  532. char client_cert[256];
  533. const char *res_dir = locate_resources();
  534. ck_assert(res_dir != NULL);
  535. strcpy(server_cert, res_dir);
  536. strcpy(client_cert, res_dir);
  537. #ifdef _WIN32
  538. strcat(server_cert, "cert\\server.pem");
  539. strcat(client_cert, "cert\\client.pem");
  540. #else
  541. strcat(server_cert, "cert/server.pem");
  542. strcat(client_cert, "cert/client.pem");
  543. #endif
  544. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  545. #if !defined(NO_FILES)
  546. OPTIONS[opt_idx++] = "document_root";
  547. OPTIONS[opt_idx++] = ".";
  548. #endif
  549. OPTIONS[opt_idx++] = "listening_ports";
  550. OPTIONS[opt_idx++] = "8080r,8443s";
  551. OPTIONS[opt_idx++] = "ssl_certificate";
  552. OPTIONS[opt_idx++] = server_cert;
  553. OPTIONS[opt_idx++] = "ssl_verify_peer";
  554. OPTIONS[opt_idx++] = "yes";
  555. OPTIONS[opt_idx++] = "ssl_ca_file";
  556. OPTIONS[opt_idx++] = client_cert;
  557. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  558. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  559. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  560. memset(ports, 0, sizeof(ports));
  561. memset(&callbacks, 0, sizeof(callbacks));
  562. memset(errmsg, 0, sizeof(errmsg));
  563. callbacks.log_message = log_msg_func;
  564. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  565. ck_assert_str_eq(errmsg, "");
  566. ck_assert(ctx != NULL);
  567. ports_cnt = mg_get_server_ports(ctx, 16, ports);
  568. ck_assert_int_eq(ports_cnt, 2);
  569. ck_assert_int_eq(ports[0].protocol, 1);
  570. ck_assert_int_eq(ports[0].port, 8080);
  571. ck_assert_int_eq(ports[0].is_ssl, 0);
  572. ck_assert_int_eq(ports[0].is_redirect, 1);
  573. ck_assert_int_eq(ports[1].protocol, 1);
  574. ck_assert_int_eq(ports[1].port, 8443);
  575. ck_assert_int_eq(ports[1].is_ssl, 1);
  576. ck_assert_int_eq(ports[1].is_redirect, 0);
  577. ck_assert_int_eq(ports[2].protocol, 0);
  578. ck_assert_int_eq(ports[2].port, 0);
  579. ck_assert_int_eq(ports[2].is_ssl, 0);
  580. ck_assert_int_eq(ports[2].is_redirect, 0);
  581. test_sleep(1);
  582. memset(client_err, 0, sizeof(client_err));
  583. client_conn =
  584. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  585. ck_assert(client_conn == NULL);
  586. ck_assert_str_ne(client_err, "");
  587. memset(client_err, 0, sizeof(client_err));
  588. memset(&client_options, 0, sizeof(client_options));
  589. client_options.host = "127.0.0.1";
  590. client_options.port = 8443;
  591. client_options.client_cert = client_cert;
  592. client_options.server_cert = server_cert;
  593. client_conn = mg_connect_client_secure(&client_options,
  594. client_err,
  595. sizeof(client_err));
  596. ck_assert(client_conn != NULL);
  597. ck_assert_str_eq(client_err, "");
  598. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  599. client_res =
  600. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  601. ck_assert_int_ge(client_res, 0);
  602. ck_assert_str_eq(client_err, "");
  603. client_ri = mg_get_request_info(client_conn);
  604. ck_assert(client_ri != NULL);
  605. #if defined(NO_FILES)
  606. ck_assert_str_eq(client_ri->uri, "404");
  607. #else
  608. ck_assert_str_eq(client_ri->uri, "200");
  609. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  610. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  611. ck_assert_int_gt(client_res, 0);
  612. ck_assert_int_le(client_res, sizeof(client_err));
  613. #endif
  614. mg_close_connection(client_conn);
  615. /* TODO: A client API using a client certificate is missing */
  616. test_sleep(1);
  617. test_mg_stop(ctx);
  618. #endif
  619. }
  620. END_TEST
  621. static struct mg_context *g_ctx;
  622. static int
  623. request_test_handler(struct mg_connection *conn, void *cbdata)
  624. {
  625. int i;
  626. char chunk_data[32];
  627. const struct mg_request_info *ri;
  628. struct mg_context *ctx;
  629. void *ud, *cud;
  630. ctx = mg_get_context(conn);
  631. ud = mg_get_user_data(ctx);
  632. ri = mg_get_request_info(conn);
  633. ck_assert(ri != NULL);
  634. ck_assert(ctx == g_ctx);
  635. ck_assert(ud == &g_ctx);
  636. mg_set_user_connection_data(conn, (void *)6543);
  637. cud = mg_get_user_connection_data(conn);
  638. ck_assert_ptr_eq((void *)cud, (void *)6543);
  639. ck_assert_ptr_eq((void *)cbdata, (void *)7);
  640. strcpy(chunk_data, "123456789A123456789B123456789C");
  641. mg_printf(conn,
  642. "HTTP/1.1 200 OK\r\n"
  643. "Transfer-Encoding: chunked\r\n"
  644. "Content-Type: text/plain\r\n\r\n");
  645. for (i = 1; i <= 10; i++) {
  646. mg_printf(conn, "%x\r\n", i);
  647. mg_write(conn, chunk_data, (unsigned)i);
  648. mg_printf(conn, "\r\n");
  649. }
  650. mg_printf(conn, "0\r\n\r\n");
  651. return 1;
  652. }
  653. #ifdef USE_WEBSOCKET
  654. /****************************************************************************/
  655. /* WEBSOCKET SERVER */
  656. /****************************************************************************/
  657. static const char *websocket_welcome_msg = "websocket welcome\n";
  658. static const size_t websocket_welcome_msg_len =
  659. 18 /* strlen(websocket_welcome_msg) */;
  660. static const char *websocket_goodbye_msg = "websocket bye\n";
  661. static const size_t websocket_goodbye_msg_len =
  662. 14 /* strlen(websocket_goodbye_msg) */;
  663. static int
  664. websock_server_connect(const struct mg_connection *conn, void *udata)
  665. {
  666. (void)conn;
  667. ck_assert_ptr_eq((void *)udata, (void *)7531);
  668. printf("Server: Websocket connected\n");
  669. return 0; /* return 0 to accept every connection */
  670. }
  671. static void
  672. websock_server_ready(struct mg_connection *conn, void *udata)
  673. {
  674. ck_assert_ptr_eq((void *)udata, (void *)7531);
  675. printf("Server: Websocket ready\n");
  676. /* Send websocket welcome message */
  677. mg_lock_connection(conn);
  678. mg_websocket_write(conn,
  679. WEBSOCKET_OPCODE_TEXT,
  680. websocket_welcome_msg,
  681. websocket_welcome_msg_len);
  682. mg_unlock_connection(conn);
  683. printf("Server: Websocket ready X\n");
  684. }
  685. #define long_ws_buf_len (500)
  686. static char long_ws_buf[long_ws_buf_len];
  687. static int
  688. websock_server_data(struct mg_connection *conn,
  689. int bits,
  690. char *data,
  691. size_t data_len,
  692. void *udata)
  693. {
  694. (void)bits;
  695. ck_assert_ptr_eq((void *)udata, (void *)7531);
  696. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  697. if (data_len == 3 && !memcmp(data, "bye", 3)) {
  698. /* Send websocket goodbye message */
  699. mg_lock_connection(conn);
  700. mg_websocket_write(conn,
  701. WEBSOCKET_OPCODE_TEXT,
  702. websocket_goodbye_msg,
  703. websocket_goodbye_msg_len);
  704. mg_unlock_connection(conn);
  705. } else if (data_len == 5 && !memcmp(data, "data1", 5)) {
  706. mg_lock_connection(conn);
  707. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok1", 3);
  708. mg_unlock_connection(conn);
  709. } else if (data_len == 5 && !memcmp(data, "data2", 5)) {
  710. mg_lock_connection(conn);
  711. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok 2", 4);
  712. mg_unlock_connection(conn);
  713. } else if (data_len == 5 && !memcmp(data, "data3", 5)) {
  714. mg_lock_connection(conn);
  715. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok - 3", 6);
  716. mg_unlock_connection(conn);
  717. } else if (data_len == long_ws_buf_len
  718. && !memcmp(data, long_ws_buf, long_ws_buf_len)) {
  719. mg_lock_connection(conn);
  720. mg_websocket_write(conn,
  721. WEBSOCKET_OPCODE_BINARY,
  722. long_ws_buf,
  723. long_ws_buf_len);
  724. mg_unlock_connection(conn);
  725. } else {
  726. #if defined(__MINGW32__) || defined(__GNUC__)
  727. #pragma GCC diagnostic push
  728. #pragma GCC diagnostic ignored "-Wunreachable-code"
  729. #endif
  730. #ifdef __clang__
  731. #pragma clang diagnostic push
  732. #pragma clang diagnostic ignored "-Wunreachable-code"
  733. #endif
  734. ck_abort_msg("Got unexpected message from websocket client");
  735. return 0;
  736. #ifdef __clang__
  737. #pragma clang diagnostic pop
  738. #endif
  739. #if defined(__MINGW32__) || defined(__GNUC__)
  740. #pragma GCC diagnostic pop
  741. #endif
  742. }
  743. return 1; /* return 1 to keep the connetion open */
  744. }
  745. static void
  746. websock_server_close(const struct mg_connection *conn, void *udata)
  747. {
  748. (void)conn;
  749. ck_assert_ptr_eq((void *)udata, (void *)7531);
  750. printf("Server: Close connection\n");
  751. /* Can not send a websocket goodbye message here - the connection is already
  752. * closed */
  753. }
  754. /****************************************************************************/
  755. /* WEBSOCKET CLIENT */
  756. /****************************************************************************/
  757. struct tclient_data {
  758. void *data;
  759. size_t len;
  760. int closed;
  761. int clientId;
  762. };
  763. static int
  764. websocket_client_data_handler(struct mg_connection *conn,
  765. int flags,
  766. char *data,
  767. size_t data_len,
  768. void *user_data)
  769. {
  770. struct mg_context *ctx = mg_get_context(conn);
  771. struct tclient_data *pclient_data =
  772. (struct tclient_data *)mg_get_user_data(ctx);
  773. (void)user_data; /* TODO: check this */
  774. ck_assert(pclient_data != NULL);
  775. ck_assert_int_eq(flags, (int)(128 | 1));
  776. printf("Client %i received data from server: ", pclient_data->clientId);
  777. fwrite(data, 1, data_len, stdout);
  778. printf("\n");
  779. pclient_data->data = malloc(data_len);
  780. ck_assert(pclient_data->data != NULL);
  781. memcpy(pclient_data->data, data, data_len);
  782. pclient_data->len = data_len;
  783. return 1;
  784. }
  785. static void
  786. websocket_client_close_handler(const struct mg_connection *conn,
  787. void *user_data)
  788. {
  789. struct mg_context *ctx = mg_get_context(conn);
  790. struct tclient_data *pclient_data =
  791. (struct tclient_data *)mg_get_user_data(ctx);
  792. (void)user_data; /* TODO: check this */
  793. ck_assert(pclient_data != NULL);
  794. printf("Client %i: Close handler\n", pclient_data->clientId);
  795. pclient_data->closed++;
  796. }
  797. #endif
  798. START_TEST(test_request_handlers)
  799. {
  800. char ebuf[100];
  801. struct mg_context *ctx;
  802. struct mg_connection *client_conn;
  803. const struct mg_request_info *ri;
  804. char uri[64];
  805. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  806. const char *expected =
  807. "112123123412345123456123456712345678123456789123456789A";
  808. int i;
  809. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  810. #if defined(USE_IPV6) && defined(NO_SSL)
  811. const char *HTTP_PORT = "8084,[::]:8086";
  812. short ipv4_port = 8084;
  813. short ipv6_port = 8086;
  814. #elif !defined(USE_IPV6) && defined(NO_SSL)
  815. const char *HTTP_PORT = "8084";
  816. short ipv4_port = 8084;
  817. #elif defined(USE_IPV6) && !defined(NO_SSL)
  818. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  819. short ipv4_port = 8084;
  820. short ipv4s_port = 8094;
  821. short ipv4r_port = 8194;
  822. short ipv6_port = 8086;
  823. short ipv6s_port = 8096;
  824. short ipv6r_port = 8196;
  825. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  826. const char *HTTP_PORT = "8084,8194r,8094s";
  827. short ipv4_port = 8084;
  828. short ipv4s_port = 8094;
  829. short ipv4r_port = 8194;
  830. #endif
  831. const char *OPTIONS[16];
  832. const char *opt;
  833. FILE *f;
  834. const char *plain_file_content;
  835. const char *encoded_file_content;
  836. const char *cgi_script_content;
  837. const char *expected_cgi_result;
  838. int opt_idx = 0;
  839. #if !defined(NO_SSL)
  840. const char *ssl_cert = locate_ssl_cert();
  841. #endif
  842. #if defined(USE_WEBSOCKET)
  843. struct tclient_data ws_client1_data = {NULL, 0, 0, 1};
  844. struct tclient_data ws_client2_data = {NULL, 0, 0, 2};
  845. struct tclient_data ws_client3_data = {NULL, 0, 0, 3};
  846. struct tclient_data ws_client4_data = {NULL, 0, 0, 4};
  847. struct mg_connection *ws_client1_conn = NULL;
  848. struct mg_connection *ws_client2_conn = NULL;
  849. struct mg_connection *ws_client3_conn = NULL;
  850. struct mg_connection *ws_client4_conn = NULL;
  851. #endif
  852. char cmd_buf[256];
  853. char *cgi_env_opt;
  854. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  855. OPTIONS[opt_idx++] = "listening_ports";
  856. OPTIONS[opt_idx++] = HTTP_PORT;
  857. OPTIONS[opt_idx++] = "authentication_domain";
  858. OPTIONS[opt_idx++] = "test.domain";
  859. #if !defined(NO_FILES)
  860. OPTIONS[opt_idx++] = "document_root";
  861. OPTIONS[opt_idx++] = ".";
  862. #endif
  863. #ifndef NO_SSL
  864. ck_assert(ssl_cert != NULL);
  865. OPTIONS[opt_idx++] = "ssl_certificate";
  866. OPTIONS[opt_idx++] = ssl_cert;
  867. #endif
  868. OPTIONS[opt_idx++] = "cgi_environment";
  869. cgi_env_opt = (char *)calloc(1, 4096 /* CGI_ENVIRONMENT_SIZE */);
  870. ck_assert_ptr_ne(cgi_env_opt, NULL);
  871. cgi_env_opt[0] = 'x';
  872. cgi_env_opt[1] = '=';
  873. memset(cgi_env_opt + 2, 'y', 4090); /* Add large env field, so the server
  874. * must reallocate buffers. */
  875. OPTIONS[opt_idx++] = cgi_env_opt;
  876. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  877. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  878. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  879. ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
  880. free(cgi_env_opt);
  881. ck_assert(ctx != NULL);
  882. g_ctx = ctx;
  883. opt = mg_get_option(ctx, "listening_ports");
  884. ck_assert_str_eq(opt, HTTP_PORT);
  885. opt = mg_get_option(ctx, "cgi_environment");
  886. ck_assert_str_eq(opt, "");
  887. opt = mg_get_option(ctx, "unknown_option_name");
  888. ck_assert(opt == NULL);
  889. for (i = 0; i < 1000; i++) {
  890. sprintf(uri, "/U%u", i);
  891. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  892. }
  893. for (i = 500; i < 800; i++) {
  894. sprintf(uri, "/U%u", i);
  895. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  896. }
  897. for (i = 600; i >= 0; i--) {
  898. sprintf(uri, "/U%u", i);
  899. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  900. }
  901. for (i = 750; i <= 1000; i++) {
  902. sprintf(uri, "/U%u", i);
  903. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  904. }
  905. for (i = 5; i < 9; i++) {
  906. sprintf(uri, "/U%u", i);
  907. mg_set_request_handler(ctx,
  908. uri,
  909. request_test_handler,
  910. (void *)(ptrdiff_t)i);
  911. }
  912. #ifdef USE_WEBSOCKET
  913. mg_set_websocket_handler(ctx,
  914. "/websocket",
  915. websock_server_connect,
  916. websock_server_ready,
  917. websock_server_data,
  918. websock_server_close,
  919. (void *)7531);
  920. #endif
  921. /* Try to load non existing file */
  922. client_conn = mg_download("localhost",
  923. ipv4_port,
  924. 0,
  925. ebuf,
  926. sizeof(ebuf),
  927. "%s",
  928. "GET /file/not/found HTTP/1.0\r\n\r\n");
  929. ck_assert(client_conn != NULL);
  930. ri = mg_get_request_info(client_conn);
  931. ck_assert(ri != NULL);
  932. ck_assert_str_eq(ri->uri, "404");
  933. mg_close_connection(client_conn);
  934. /* Get data from callback */
  935. client_conn = mg_download(
  936. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  937. ck_assert(client_conn != NULL);
  938. ri = mg_get_request_info(client_conn);
  939. ck_assert(ri != NULL);
  940. ck_assert_str_eq(ri->uri, "200");
  941. i = mg_read(client_conn, buf, sizeof(buf));
  942. ck_assert_int_eq(i, (int)strlen(expected));
  943. buf[i] = 0;
  944. ck_assert_str_eq(buf, expected);
  945. mg_close_connection(client_conn);
  946. /* Get data from callback using http://127.0.0.1 */
  947. client_conn = mg_download(
  948. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  949. ck_assert(client_conn != NULL);
  950. ri = mg_get_request_info(client_conn);
  951. ck_assert(ri != NULL);
  952. ck_assert_str_eq(ri->uri, "200");
  953. i = mg_read(client_conn, buf, sizeof(buf));
  954. if ((i >= 0) && ((size_t)i < sizeof(buf))) {
  955. buf[i] = 0;
  956. } else {
  957. ck_abort_msg(
  958. "ERROR: test_request_handlers: read returned %i (>=0, <%i)",
  959. (int)i,
  960. (int)sizeof(buf));
  961. }
  962. ck_assert((int)i < (int)sizeof(buf));
  963. ck_assert(i > 0);
  964. ck_assert_int_eq(i, (int)strlen(expected));
  965. buf[i] = 0;
  966. ck_assert_str_eq(buf, expected);
  967. mg_close_connection(client_conn);
  968. #if defined(USE_IPV6)
  969. /* Get data from callback using http://[::1] */
  970. client_conn =
  971. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  972. ck_assert(client_conn != NULL);
  973. ri = mg_get_request_info(client_conn);
  974. ck_assert(ri != NULL);
  975. ck_assert_str_eq(ri->uri, "200");
  976. i = mg_read(client_conn, buf, sizeof(buf));
  977. ck_assert_int_eq(i, (int)strlen(expected));
  978. buf[i] = 0;
  979. ck_assert_str_eq(buf, expected);
  980. mg_close_connection(client_conn);
  981. #endif
  982. #if !defined(NO_SSL)
  983. /* Get data from callback using https://127.0.0.1 */
  984. client_conn = mg_download(
  985. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  986. ck_assert(client_conn != NULL);
  987. ri = mg_get_request_info(client_conn);
  988. ck_assert(ri != NULL);
  989. ck_assert_str_eq(ri->uri, "200");
  990. i = mg_read(client_conn, buf, sizeof(buf));
  991. ck_assert_int_eq(i, (int)strlen(expected));
  992. buf[i] = 0;
  993. ck_assert_str_eq(buf, expected);
  994. mg_close_connection(client_conn);
  995. /* Get redirect from callback using http://127.0.0.1 */
  996. client_conn = mg_download(
  997. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  998. ck_assert(client_conn != NULL);
  999. ri = mg_get_request_info(client_conn);
  1000. ck_assert(ri != NULL);
  1001. ck_assert_str_eq(ri->uri, "302");
  1002. i = mg_read(client_conn, buf, sizeof(buf));
  1003. ck_assert_int_eq(i, -1);
  1004. mg_close_connection(client_conn);
  1005. #endif
  1006. #if defined(USE_IPV6) && !defined(NO_SSL)
  1007. /* Get data from callback using https://[::1] */
  1008. client_conn =
  1009. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  1010. ck_assert(client_conn != NULL);
  1011. ri = mg_get_request_info(client_conn);
  1012. ck_assert(ri != NULL);
  1013. ck_assert_str_eq(ri->uri, "200");
  1014. i = mg_read(client_conn, buf, sizeof(buf));
  1015. ck_assert_int_eq(i, (int)strlen(expected));
  1016. buf[i] = 0;
  1017. ck_assert_str_eq(buf, expected);
  1018. mg_close_connection(client_conn);
  1019. /* Get redirect from callback using http://127.0.0.1 */
  1020. client_conn =
  1021. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  1022. ck_assert(client_conn != NULL);
  1023. ri = mg_get_request_info(client_conn);
  1024. ck_assert(ri != NULL);
  1025. ck_assert_str_eq(ri->uri, "302");
  1026. i = mg_read(client_conn, buf, sizeof(buf));
  1027. ck_assert_int_eq(i, -1);
  1028. mg_close_connection(client_conn);
  1029. #endif
  1030. /* It seems to be impossible to find out what the actual working
  1031. * directory of the CI test environment is. Before breaking another
  1032. * dozen of builds by trying blindly with different paths, just
  1033. * create the file here */
  1034. #ifdef _WIN32
  1035. f = fopen("test.txt", "wb");
  1036. #else
  1037. f = fopen("test.txt", "w");
  1038. #endif
  1039. plain_file_content = "simple text file\n";
  1040. fwrite(plain_file_content, 17, 1, f);
  1041. fclose(f);
  1042. #ifdef _WIN32
  1043. f = fopen("test_gz.txt.gz", "wb");
  1044. #else
  1045. f = fopen("test_gz.txt.gz", "w");
  1046. #endif
  1047. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  1048. "test_gz.txt"
  1049. "\x00\x01\x11\x00\xee\xff"
  1050. "zipped text file"
  1051. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  1052. fwrite(encoded_file_content, 1, 52, f);
  1053. fclose(f);
  1054. #ifdef _WIN32
  1055. f = fopen("test.cgi", "wb");
  1056. cgi_script_content = "#!test.cgi.cmd\r\n";
  1057. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1058. fclose(f);
  1059. f = fopen("test.cgi.cmd", "w");
  1060. cgi_script_content = "@echo off\r\n"
  1061. "echo Connection: close\r\n"
  1062. "echo Content-Type: text/plain\r\n"
  1063. "echo.\r\n"
  1064. "echo CGI test\r\n"
  1065. "\r\n";
  1066. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1067. fclose(f);
  1068. #else
  1069. f = fopen("test.cgi", "w");
  1070. cgi_script_content = "#!/bin/sh\n\n"
  1071. "printf \"Connection: close\\r\\n\"\n"
  1072. "printf \"Content-Type: text/plain\\r\\n\"\n"
  1073. "printf \"\\r\\n\"\n"
  1074. "printf \"CGI test\\r\\n\"\n"
  1075. "\n";
  1076. (void)fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1077. (void)fclose(f);
  1078. (void)system("chmod a+x test.cgi");
  1079. #endif
  1080. expected_cgi_result = "CGI test";
  1081. /* Get static data */
  1082. client_conn = mg_download("localhost",
  1083. ipv4_port,
  1084. 0,
  1085. ebuf,
  1086. sizeof(ebuf),
  1087. "%s",
  1088. "GET /test.txt HTTP/1.0\r\n\r\n");
  1089. ck_assert(client_conn != NULL);
  1090. ri = mg_get_request_info(client_conn);
  1091. ck_assert(ri != NULL);
  1092. #if defined(NO_FILES)
  1093. ck_assert_str_eq(ri->uri, "404");
  1094. #else
  1095. ck_assert_str_eq(ri->uri, "200");
  1096. i = mg_read(client_conn, buf, sizeof(buf));
  1097. ck_assert_int_eq(i, 17);
  1098. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1099. buf[i] = 0;
  1100. }
  1101. ck_assert_str_eq(buf, plain_file_content);
  1102. #endif
  1103. mg_close_connection(client_conn);
  1104. /* Test with CGI test executable */
  1105. #if defined(_WIN32)
  1106. sprintf(cmd_buf, "copy %s\\cgi_test.cgi cgi_test.exe", locate_test_exes());
  1107. #else
  1108. sprintf(cmd_buf, "cp %s/cgi_test.cgi cgi_test.cgi", locate_test_exes());
  1109. #endif
  1110. (void)system(cmd_buf);
  1111. #if !defined(NO_CGI) && !defined(NO_FILES) && !defined(_WIN32)
  1112. /* TODO: add test for windows, check with POST */
  1113. client_conn = mg_download(
  1114. "localhost",
  1115. ipv4_port,
  1116. 0,
  1117. ebuf,
  1118. sizeof(ebuf),
  1119. "%s",
  1120. "POST /cgi_test.cgi/x/y.z HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC");
  1121. ck_assert(client_conn != NULL);
  1122. ri = mg_get_request_info(client_conn);
  1123. ck_assert(ri != NULL);
  1124. ck_assert_str_eq(ri->uri, "200");
  1125. mg_close_connection(client_conn);
  1126. #endif
  1127. /* Get zipped static data - will not work if Accept-Encoding is not set */
  1128. client_conn = mg_download("localhost",
  1129. ipv4_port,
  1130. 0,
  1131. ebuf,
  1132. sizeof(ebuf),
  1133. "%s",
  1134. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  1135. ck_assert(client_conn != NULL);
  1136. ri = mg_get_request_info(client_conn);
  1137. ck_assert(ri != NULL);
  1138. ck_assert_str_eq(ri->uri, "404");
  1139. mg_close_connection(client_conn);
  1140. /* Get zipped static data - with Accept-Encoding */
  1141. client_conn = mg_download(
  1142. "localhost",
  1143. ipv4_port,
  1144. 0,
  1145. ebuf,
  1146. sizeof(ebuf),
  1147. "%s",
  1148. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  1149. ck_assert(client_conn != NULL);
  1150. ri = mg_get_request_info(client_conn);
  1151. ck_assert(ri != NULL);
  1152. #if defined(NO_FILES)
  1153. ck_assert_str_eq(ri->uri, "404");
  1154. #else
  1155. ck_assert_str_eq(ri->uri, "200");
  1156. i = mg_read(client_conn, buf, sizeof(buf));
  1157. ck_assert_int_eq(i, 52);
  1158. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1159. buf[i] = 0;
  1160. }
  1161. ck_assert_int_eq(ri->content_length, 52);
  1162. ck_assert_str_eq(buf, encoded_file_content);
  1163. #endif
  1164. mg_close_connection(client_conn);
  1165. /* Get CGI generated data */
  1166. #if !defined(NO_CGI)
  1167. client_conn = mg_download("localhost",
  1168. ipv4_port,
  1169. 0,
  1170. ebuf,
  1171. sizeof(ebuf),
  1172. "%s",
  1173. "GET /test.cgi HTTP/1.0\r\n\r\n");
  1174. ck_assert(client_conn != NULL);
  1175. ri = mg_get_request_info(client_conn);
  1176. ck_assert(ri != NULL);
  1177. #if defined(NO_FILES)
  1178. ck_assert_str_eq(ri->uri, "404");
  1179. (void)expected_cgi_result;
  1180. (void)cgi_script_content;
  1181. #else
  1182. i = mg_read(client_conn, buf, sizeof(buf));
  1183. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1184. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  1185. i--;
  1186. }
  1187. buf[i] = 0;
  1188. }
  1189. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  1190. ck_assert_str_eq(buf, expected_cgi_result);
  1191. ck_assert_str_eq(ri->uri, "200");
  1192. mg_close_connection(client_conn);
  1193. #endif
  1194. #else
  1195. (void)expected_cgi_result;
  1196. (void)cgi_script_content;
  1197. #endif
  1198. /* Get directory listing */
  1199. client_conn = mg_download("localhost",
  1200. ipv4_port,
  1201. 0,
  1202. ebuf,
  1203. sizeof(ebuf),
  1204. "%s",
  1205. "GET / HTTP/1.0\r\n\r\n");
  1206. ck_assert(client_conn != NULL);
  1207. ri = mg_get_request_info(client_conn);
  1208. ck_assert(ri != NULL);
  1209. #if defined(NO_FILES)
  1210. ck_assert_str_eq(ri->uri, "404");
  1211. #else
  1212. ck_assert_str_eq(ri->uri, "200");
  1213. i = mg_read(client_conn, buf, sizeof(buf));
  1214. ck_assert(i > 6);
  1215. buf[6] = 0;
  1216. ck_assert_str_eq(buf, "<html>");
  1217. #endif
  1218. mg_close_connection(client_conn);
  1219. /* POST to static file (will not work) */
  1220. client_conn = mg_download("localhost",
  1221. ipv4_port,
  1222. 0,
  1223. ebuf,
  1224. sizeof(ebuf),
  1225. "%s",
  1226. "POST /test.txt HTTP/1.0\r\n\r\n");
  1227. ck_assert(client_conn != NULL);
  1228. ri = mg_get_request_info(client_conn);
  1229. ck_assert(ri != NULL);
  1230. #if defined(NO_FILES)
  1231. ck_assert_str_eq(ri->uri, "404");
  1232. #else
  1233. ck_assert_str_eq(ri->uri, "405");
  1234. i = mg_read(client_conn, buf, sizeof(buf));
  1235. ck_assert(i >= 29);
  1236. buf[29] = 0;
  1237. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  1238. #endif
  1239. mg_close_connection(client_conn);
  1240. /* PUT to static file (will not work) */
  1241. client_conn = mg_download("localhost",
  1242. ipv4_port,
  1243. 0,
  1244. ebuf,
  1245. sizeof(ebuf),
  1246. "%s",
  1247. "PUT /test.txt HTTP/1.0\r\n\r\n");
  1248. ck_assert(client_conn != NULL);
  1249. ri = mg_get_request_info(client_conn);
  1250. ck_assert(ri != NULL);
  1251. #if defined(NO_FILES)
  1252. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  1253. #else
  1254. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  1255. #endif
  1256. mg_close_connection(client_conn);
  1257. /* Get data from callback using mg_connect_client instead of mg_download */
  1258. memset(ebuf, 0, sizeof(ebuf));
  1259. client_conn =
  1260. mg_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf));
  1261. ck_assert(client_conn != NULL);
  1262. ck_assert_str_eq(ebuf, "");
  1263. mg_printf(client_conn, "%s", request);
  1264. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1265. ck_assert_int_ge(i, 0);
  1266. ck_assert_str_eq(ebuf, "");
  1267. ri = mg_get_request_info(client_conn);
  1268. ck_assert(ri != NULL);
  1269. ck_assert_str_eq(ri->uri, "200");
  1270. i = mg_read(client_conn, buf, sizeof(buf));
  1271. ck_assert_int_eq(i, (int)strlen(expected));
  1272. buf[i] = 0;
  1273. ck_assert_str_eq(buf, expected);
  1274. mg_close_connection(client_conn);
  1275. /* Get data from callback using mg_connect_client and absolute URI */
  1276. memset(ebuf, 0, sizeof(ebuf));
  1277. client_conn =
  1278. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1279. ck_assert(client_conn != NULL);
  1280. ck_assert_str_eq(ebuf, "");
  1281. mg_printf(client_conn,
  1282. "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1283. ipv4_port);
  1284. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1285. ck_assert_int_ge(i, 0);
  1286. ck_assert_str_eq(ebuf, "");
  1287. ri = mg_get_request_info(client_conn);
  1288. ck_assert(ri != NULL);
  1289. ck_assert_str_eq(ri->uri, "200");
  1290. i = mg_read(client_conn, buf, sizeof(buf));
  1291. ck_assert_int_eq(i, (int)strlen(expected));
  1292. buf[i] = 0;
  1293. ck_assert_str_eq(buf, expected);
  1294. mg_close_connection(client_conn);
  1295. /* Get data from callback using mg_connect_client and absolute URI with a
  1296. * sub-domain */
  1297. memset(ebuf, 0, sizeof(ebuf));
  1298. client_conn =
  1299. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1300. ck_assert(client_conn != NULL);
  1301. ck_assert_str_eq(ebuf, "");
  1302. mg_printf(client_conn,
  1303. "GET http://subdomain.test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1304. ipv4_port);
  1305. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1306. ck_assert_int_ge(i, 0);
  1307. ck_assert_str_eq(ebuf, "");
  1308. ri = mg_get_request_info(client_conn);
  1309. ck_assert(ri != NULL);
  1310. ck_assert_str_eq(ri->uri, "200");
  1311. i = mg_read(client_conn, buf, sizeof(buf));
  1312. ck_assert_int_eq(i, (int)strlen(expected));
  1313. buf[i] = 0;
  1314. ck_assert_str_eq(buf, expected);
  1315. mg_close_connection(client_conn);
  1316. /* Websocket test */
  1317. #ifdef USE_WEBSOCKET
  1318. /* Then connect a first client */
  1319. ws_client1_conn =
  1320. mg_connect_websocket_client("localhost",
  1321. ipv4_port,
  1322. 0,
  1323. ebuf,
  1324. sizeof(ebuf),
  1325. "/websocket",
  1326. NULL,
  1327. websocket_client_data_handler,
  1328. websocket_client_close_handler,
  1329. &ws_client1_data);
  1330. ck_assert(ws_client1_conn != NULL);
  1331. wait_not_null(
  1332. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1333. ck_assert_int_eq(ws_client1_data.closed, 0);
  1334. ck_assert_int_eq(ws_client2_data.closed, 0);
  1335. ck_assert_int_eq(ws_client3_data.closed, 0);
  1336. ck_assert(ws_client2_data.data == NULL);
  1337. ck_assert_uint_eq(ws_client2_data.len, 0);
  1338. ck_assert(ws_client1_data.data != NULL);
  1339. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1340. ck_assert(!memcmp(ws_client1_data.data,
  1341. websocket_welcome_msg,
  1342. websocket_welcome_msg_len));
  1343. free(ws_client1_data.data);
  1344. ws_client1_data.data = NULL;
  1345. ws_client1_data.len = 0;
  1346. mg_websocket_client_write(ws_client1_conn,
  1347. WEBSOCKET_OPCODE_TEXT,
  1348. "data1",
  1349. 5);
  1350. wait_not_null(
  1351. &(ws_client1_data
  1352. .data)); /* Wait for the websocket acknowledge message */
  1353. ck_assert_int_eq(ws_client1_data.closed, 0);
  1354. ck_assert_int_eq(ws_client2_data.closed, 0);
  1355. ck_assert(ws_client2_data.data == NULL);
  1356. ck_assert_uint_eq(ws_client2_data.len, 0);
  1357. ck_assert(ws_client1_data.data != NULL);
  1358. ck_assert_uint_eq(ws_client1_data.len, 3);
  1359. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1360. free(ws_client1_data.data);
  1361. ws_client1_data.data = NULL;
  1362. ws_client1_data.len = 0;
  1363. /* Now connect a second client */
  1364. #ifdef USE_IPV6
  1365. ws_client2_conn =
  1366. mg_connect_websocket_client("[::1]",
  1367. ipv6_port,
  1368. 0,
  1369. ebuf,
  1370. sizeof(ebuf),
  1371. "/websocket",
  1372. NULL,
  1373. websocket_client_data_handler,
  1374. websocket_client_close_handler,
  1375. &ws_client2_data);
  1376. #else
  1377. ws_client2_conn =
  1378. mg_connect_websocket_client("127.0.0.1",
  1379. ipv4_port,
  1380. 0,
  1381. ebuf,
  1382. sizeof(ebuf),
  1383. "/websocket",
  1384. NULL,
  1385. websocket_client_data_handler,
  1386. websocket_client_close_handler,
  1387. &ws_client2_data);
  1388. #endif
  1389. ck_assert(ws_client2_conn != NULL);
  1390. wait_not_null(
  1391. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1392. ck_assert(ws_client1_data.closed == 0);
  1393. ck_assert(ws_client2_data.closed == 0);
  1394. ck_assert(ws_client1_data.data == NULL);
  1395. ck_assert(ws_client1_data.len == 0);
  1396. ck_assert(ws_client2_data.data != NULL);
  1397. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1398. ck_assert(!memcmp(ws_client2_data.data,
  1399. websocket_welcome_msg,
  1400. websocket_welcome_msg_len));
  1401. free(ws_client2_data.data);
  1402. ws_client2_data.data = NULL;
  1403. ws_client2_data.len = 0;
  1404. mg_websocket_client_write(ws_client1_conn,
  1405. WEBSOCKET_OPCODE_TEXT,
  1406. "data2",
  1407. 5);
  1408. wait_not_null(
  1409. &(ws_client1_data
  1410. .data)); /* Wait for the websocket acknowledge message */
  1411. ck_assert(ws_client1_data.closed == 0);
  1412. ck_assert(ws_client2_data.closed == 0);
  1413. ck_assert(ws_client2_data.data == NULL);
  1414. ck_assert(ws_client2_data.len == 0);
  1415. ck_assert(ws_client1_data.data != NULL);
  1416. ck_assert(ws_client1_data.len == 4);
  1417. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1418. free(ws_client1_data.data);
  1419. ws_client1_data.data = NULL;
  1420. ws_client1_data.len = 0;
  1421. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1422. wait_not_null(
  1423. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1424. ck_assert(ws_client1_data.closed == 0);
  1425. ck_assert(ws_client2_data.closed == 0);
  1426. ck_assert(ws_client2_data.data == NULL);
  1427. ck_assert(ws_client2_data.len == 0);
  1428. ck_assert(ws_client1_data.data != NULL);
  1429. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1430. ck_assert(!memcmp(ws_client1_data.data,
  1431. websocket_goodbye_msg,
  1432. websocket_goodbye_msg_len));
  1433. free(ws_client1_data.data);
  1434. ws_client1_data.data = NULL;
  1435. ws_client1_data.len = 0;
  1436. mg_close_connection(ws_client1_conn);
  1437. test_sleep(3); /* Won't get any message */
  1438. ck_assert(ws_client1_data.closed == 1);
  1439. ck_assert(ws_client2_data.closed == 0);
  1440. ck_assert(ws_client1_data.data == NULL);
  1441. ck_assert(ws_client1_data.len == 0);
  1442. ck_assert(ws_client2_data.data == NULL);
  1443. ck_assert(ws_client2_data.len == 0);
  1444. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1445. wait_not_null(
  1446. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1447. ck_assert(ws_client1_data.closed == 1);
  1448. ck_assert(ws_client2_data.closed == 0);
  1449. ck_assert(ws_client1_data.data == NULL);
  1450. ck_assert(ws_client1_data.len == 0);
  1451. ck_assert(ws_client2_data.data != NULL);
  1452. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1453. ck_assert(!memcmp(ws_client2_data.data,
  1454. websocket_goodbye_msg,
  1455. websocket_goodbye_msg_len));
  1456. free(ws_client2_data.data);
  1457. ws_client2_data.data = NULL;
  1458. ws_client2_data.len = 0;
  1459. mg_close_connection(ws_client2_conn);
  1460. test_sleep(3); /* Won't get any message */
  1461. ck_assert(ws_client1_data.closed == 1);
  1462. ck_assert(ws_client2_data.closed == 1);
  1463. ck_assert(ws_client1_data.data == NULL);
  1464. ck_assert(ws_client1_data.len == 0);
  1465. ck_assert(ws_client2_data.data == NULL);
  1466. ck_assert(ws_client2_data.len == 0);
  1467. /* Connect client 3 */
  1468. ws_client3_conn =
  1469. mg_connect_websocket_client("localhost",
  1470. #if defined(NO_SSL)
  1471. ipv4_port,
  1472. 0,
  1473. #else
  1474. ipv4s_port,
  1475. 1,
  1476. #endif
  1477. ebuf,
  1478. sizeof(ebuf),
  1479. "/websocket",
  1480. NULL,
  1481. websocket_client_data_handler,
  1482. websocket_client_close_handler,
  1483. &ws_client3_data);
  1484. ck_assert(ws_client3_conn != NULL);
  1485. wait_not_null(
  1486. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1487. ck_assert(ws_client1_data.closed == 1);
  1488. ck_assert(ws_client2_data.closed == 1);
  1489. ck_assert(ws_client3_data.closed == 0);
  1490. ck_assert(ws_client1_data.data == NULL);
  1491. ck_assert(ws_client1_data.len == 0);
  1492. ck_assert(ws_client2_data.data == NULL);
  1493. ck_assert(ws_client2_data.len == 0);
  1494. ck_assert(ws_client3_data.data != NULL);
  1495. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1496. ck_assert(!memcmp(ws_client3_data.data,
  1497. websocket_welcome_msg,
  1498. websocket_welcome_msg_len));
  1499. free(ws_client3_data.data);
  1500. ws_client3_data.data = NULL;
  1501. ws_client3_data.len = 0;
  1502. /* Write long data */
  1503. mg_websocket_client_write(ws_client3_conn,
  1504. WEBSOCKET_OPCODE_BINARY,
  1505. long_ws_buf,
  1506. long_ws_buf_len);
  1507. /* Disconnect client 3 */
  1508. ck_assert(ws_client3_data.closed == 0);
  1509. mg_close_connection(ws_client3_conn);
  1510. ck_assert(ws_client3_data.closed == 1);
  1511. /* Connect client 4 */
  1512. ws_client4_conn =
  1513. mg_connect_websocket_client("localhost",
  1514. #if defined(NO_SSL)
  1515. ipv4_port,
  1516. 0,
  1517. #else
  1518. ipv4s_port,
  1519. 1,
  1520. #endif
  1521. ebuf,
  1522. sizeof(ebuf),
  1523. "/websocket",
  1524. NULL,
  1525. websocket_client_data_handler,
  1526. websocket_client_close_handler,
  1527. &ws_client4_data);
  1528. ck_assert(ws_client4_conn != NULL);
  1529. wait_not_null(
  1530. &(ws_client4_data.data)); /* Wait for the websocket welcome message */
  1531. ck_assert(ws_client1_data.closed == 1);
  1532. ck_assert(ws_client2_data.closed == 1);
  1533. ck_assert(ws_client3_data.closed == 1);
  1534. ck_assert(ws_client4_data.closed == 0);
  1535. ck_assert(ws_client4_data.data != NULL);
  1536. ck_assert(ws_client4_data.len == websocket_welcome_msg_len);
  1537. ck_assert(!memcmp(ws_client4_data.data,
  1538. websocket_welcome_msg,
  1539. websocket_welcome_msg_len));
  1540. free(ws_client4_data.data);
  1541. ws_client4_data.data = NULL;
  1542. ws_client4_data.len = 0;
  1543. /* stop the server without closing this connection */
  1544. #endif
  1545. /* Close the server */
  1546. g_ctx = NULL;
  1547. test_mg_stop(ctx);
  1548. mark_point();
  1549. #ifdef USE_WEBSOCKET
  1550. for (i = 0; i < 100; i++) {
  1551. test_sleep(1);
  1552. if (ws_client3_data.closed != 0) {
  1553. mark_point();
  1554. break;
  1555. }
  1556. }
  1557. ck_assert_int_eq(ws_client4_data.closed, 1);
  1558. #endif
  1559. }
  1560. END_TEST
  1561. static int g_field_found_return = -999;
  1562. static int
  1563. field_found(const char *key,
  1564. const char *filename,
  1565. char *path,
  1566. size_t pathlen,
  1567. void *user_data)
  1568. {
  1569. ck_assert_ptr_ne(key, NULL);
  1570. ck_assert_ptr_ne(filename, NULL);
  1571. ck_assert_ptr_ne(path, NULL);
  1572. ck_assert_uint_gt(pathlen, 128);
  1573. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1574. ck_assert((g_field_found_return == FORM_FIELD_STORAGE_GET)
  1575. || (g_field_found_return == FORM_FIELD_STORAGE_STORE)
  1576. || (g_field_found_return == FORM_FIELD_STORAGE_SKIP)
  1577. || (g_field_found_return == FORM_FIELD_STORAGE_ABORT));
  1578. ck_assert_str_ne(key, "dontread");
  1579. if (!strcmp(key, "break_field_handler")) {
  1580. return FORM_FIELD_STORAGE_ABORT;
  1581. }
  1582. if (!strcmp(key, "continue_field_handler")) {
  1583. return FORM_FIELD_STORAGE_SKIP;
  1584. }
  1585. if (g_field_found_return == FORM_FIELD_STORAGE_STORE) {
  1586. strncpy(path, key, pathlen - 8);
  1587. strcat(path, ".txt");
  1588. }
  1589. return g_field_found_return;
  1590. }
  1591. static int g_field_step;
  1592. static int
  1593. field_get(const char *key,
  1594. const char *value_untruncated,
  1595. size_t valuelen,
  1596. void *user_data)
  1597. {
  1598. /* Copy the untruncated value, so string compare functions can be used. */
  1599. /* The check unit test library does not have build in memcmp functions. */
  1600. char *value = (char *)malloc(valuelen + 1);
  1601. ck_assert(value != NULL);
  1602. memcpy(value, value_untruncated, valuelen);
  1603. value[valuelen] = 0;
  1604. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1605. ck_assert_int_ge(g_field_step, 0);
  1606. ++g_field_step;
  1607. switch (g_field_step) {
  1608. case 1:
  1609. ck_assert_str_eq(key, "textin");
  1610. ck_assert_uint_eq(valuelen, 4);
  1611. ck_assert_str_eq(value, "text");
  1612. break;
  1613. case 2:
  1614. ck_assert_str_eq(key, "passwordin");
  1615. ck_assert_uint_eq(valuelen, 0);
  1616. ck_assert_str_eq(value, "");
  1617. break;
  1618. case 3:
  1619. ck_assert_str_eq(key, "radio1");
  1620. ck_assert_uint_eq(valuelen, 4);
  1621. ck_assert_str_eq(value, "val1");
  1622. break;
  1623. case 4:
  1624. ck_assert_str_eq(key, "radio2");
  1625. ck_assert_uint_eq(valuelen, 4);
  1626. ck_assert_str_eq(value, "val1");
  1627. break;
  1628. case 5:
  1629. ck_assert_str_eq(key, "check1");
  1630. ck_assert_uint_eq(valuelen, 4);
  1631. ck_assert_str_eq(value, "val1");
  1632. break;
  1633. case 6:
  1634. ck_assert_str_eq(key, "numberin");
  1635. ck_assert_uint_eq(valuelen, 1);
  1636. ck_assert_str_eq(value, "1");
  1637. break;
  1638. case 7:
  1639. ck_assert_str_eq(key, "datein");
  1640. ck_assert_uint_eq(valuelen, 8);
  1641. ck_assert_str_eq(value, "1.1.2016");
  1642. break;
  1643. case 8:
  1644. ck_assert_str_eq(key, "colorin");
  1645. ck_assert_uint_eq(valuelen, 7);
  1646. ck_assert_str_eq(value, "#80ff00");
  1647. break;
  1648. case 9:
  1649. ck_assert_str_eq(key, "rangein");
  1650. ck_assert_uint_eq(valuelen, 1);
  1651. ck_assert_str_eq(value, "3");
  1652. break;
  1653. case 10:
  1654. ck_assert_str_eq(key, "monthin");
  1655. ck_assert_uint_eq(valuelen, 0);
  1656. ck_assert_str_eq(value, "");
  1657. break;
  1658. case 11:
  1659. ck_assert_str_eq(key, "weekin");
  1660. ck_assert_uint_eq(valuelen, 0);
  1661. ck_assert_str_eq(value, "");
  1662. break;
  1663. case 12:
  1664. ck_assert_str_eq(key, "timein");
  1665. ck_assert_uint_eq(valuelen, 0);
  1666. ck_assert_str_eq(value, "");
  1667. break;
  1668. case 13:
  1669. ck_assert_str_eq(key, "datetimen");
  1670. ck_assert_uint_eq(valuelen, 0);
  1671. ck_assert_str_eq(value, "");
  1672. break;
  1673. case 14:
  1674. ck_assert_str_eq(key, "datetimelocalin");
  1675. ck_assert_uint_eq(valuelen, 0);
  1676. ck_assert_str_eq(value, "");
  1677. break;
  1678. case 15:
  1679. ck_assert_str_eq(key, "emailin");
  1680. ck_assert_uint_eq(valuelen, 0);
  1681. ck_assert_str_eq(value, "");
  1682. break;
  1683. case 16:
  1684. ck_assert_str_eq(key, "searchin");
  1685. ck_assert_uint_eq(valuelen, 0);
  1686. ck_assert_str_eq(value, "");
  1687. break;
  1688. case 17:
  1689. ck_assert_str_eq(key, "telin");
  1690. ck_assert_uint_eq(valuelen, 0);
  1691. ck_assert_str_eq(value, "");
  1692. break;
  1693. case 18:
  1694. ck_assert_str_eq(key, "urlin");
  1695. ck_assert_uint_eq(valuelen, 0);
  1696. ck_assert_str_eq(value, "");
  1697. break;
  1698. case 19:
  1699. ck_assert_str_eq(key, "filein");
  1700. ck_assert_uint_eq(valuelen, 0);
  1701. ck_assert_str_eq(value, "");
  1702. break;
  1703. case 20:
  1704. ck_assert_str_eq(key, "filesin");
  1705. ck_assert_uint_eq(valuelen, 0);
  1706. ck_assert_str_eq(value, "");
  1707. break;
  1708. case 21:
  1709. ck_assert_str_eq(key, "selectin");
  1710. ck_assert_uint_eq(valuelen, 4);
  1711. ck_assert_str_eq(value, "opt1");
  1712. break;
  1713. case 22:
  1714. ck_assert_str_eq(key, "message");
  1715. ck_assert_uint_eq(valuelen, 23);
  1716. ck_assert_str_eq(value, "Text area default text.");
  1717. break;
  1718. default:
  1719. ck_abort_msg("field_get called with g_field_step == %i",
  1720. (int)g_field_step);
  1721. }
  1722. free(value);
  1723. return 0;
  1724. }
  1725. static const char *myfile_content = "Content of myfile.txt\r\n";
  1726. static const int myfile_content_rep = 500;
  1727. static int
  1728. field_store(const char *path, long long file_size, void *user_data)
  1729. {
  1730. FILE *f;
  1731. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1732. ck_assert_int_ge(g_field_step, 100);
  1733. ++g_field_step;
  1734. switch (g_field_step) {
  1735. case 101:
  1736. ck_assert_str_eq(path, "storeme.txt");
  1737. ck_assert_int_eq(file_size, 9);
  1738. f = fopen(path, "r");
  1739. ck_assert_ptr_ne(f, NULL);
  1740. if (f) {
  1741. char buf[32] = {0};
  1742. int i = (int)fread(buf, 1, 31, f);
  1743. ck_assert_int_eq(i, 9);
  1744. fclose(f);
  1745. ck_assert_str_eq(buf, "storetest");
  1746. }
  1747. break;
  1748. case 102:
  1749. ck_assert_str_eq(path, "file2store.txt");
  1750. ck_assert_uint_eq(23, strlen(myfile_content));
  1751. ck_assert_int_eq(file_size, 23 * myfile_content_rep);
  1752. #ifdef _WIN32
  1753. f = fopen(path, "rb");
  1754. #else
  1755. f = fopen(path, "r");
  1756. #endif
  1757. ck_assert_ptr_ne(f, NULL);
  1758. if (f) {
  1759. char buf[32] = {0};
  1760. int r, i;
  1761. for (r = 0; r < myfile_content_rep; r++) {
  1762. i = (int)fread(buf, 1, 23, f);
  1763. ck_assert_int_eq(i, 23);
  1764. ck_assert_str_eq(buf, myfile_content);
  1765. }
  1766. i = (int)fread(buf, 1, 23, f);
  1767. ck_assert_int_eq(i, 0);
  1768. fclose(f);
  1769. }
  1770. break;
  1771. default:
  1772. ck_abort_msg("field_get called with g_field_step == %i",
  1773. (int)g_field_step);
  1774. }
  1775. return 0;
  1776. }
  1777. static int
  1778. FormGet(struct mg_connection *conn, void *cbdata)
  1779. {
  1780. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1781. int ret;
  1782. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  1783. (void)cbdata;
  1784. ck_assert(req_info != NULL);
  1785. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1786. fdh.user_data = (void *)&g_field_found_return;
  1787. /* Call the form handler */
  1788. g_field_step = 0;
  1789. g_field_found_return = FORM_FIELD_STORAGE_GET;
  1790. ret = mg_handle_form_request(conn, &fdh);
  1791. g_field_found_return = -888;
  1792. ck_assert_int_eq(ret, 22);
  1793. ck_assert_int_eq(g_field_step, 22);
  1794. mg_printf(conn, "%i\r\n", ret);
  1795. g_field_step = 1000;
  1796. return 1;
  1797. }
  1798. static int
  1799. FormStore(struct mg_connection *conn,
  1800. void *cbdata,
  1801. int ret_expected,
  1802. int field_step_expected)
  1803. {
  1804. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1805. int ret;
  1806. struct mg_form_data_handler fdh = {field_found,
  1807. field_get,
  1808. field_store,
  1809. NULL};
  1810. (void)cbdata;
  1811. ck_assert(req_info != NULL);
  1812. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1813. fdh.user_data = (void *)&g_field_found_return;
  1814. /* Call the form handler */
  1815. g_field_step = 100;
  1816. g_field_found_return = FORM_FIELD_STORAGE_STORE;
  1817. ret = mg_handle_form_request(conn, &fdh);
  1818. ck_assert_int_eq(ret, ret_expected);
  1819. ck_assert_int_eq(g_field_step, field_step_expected);
  1820. mg_printf(conn, "%i\r\n", ret);
  1821. g_field_step = 1000;
  1822. return 1;
  1823. }
  1824. static int
  1825. FormStore1(struct mg_connection *conn, void *cbdata)
  1826. {
  1827. return FormStore(conn, cbdata, 3, 101);
  1828. }
  1829. static int
  1830. FormStore2(struct mg_connection *conn, void *cbdata)
  1831. {
  1832. return FormStore(conn, cbdata, 4, 102);
  1833. }
  1834. static void
  1835. send_chunk_stringl(struct mg_connection *conn,
  1836. const char *chunk,
  1837. unsigned int chunk_len)
  1838. {
  1839. char lenbuf[16];
  1840. size_t lenbuf_len;
  1841. int ret;
  1842. /* First store the length information in a text buffer. */
  1843. sprintf(lenbuf, "%x\r\n", chunk_len);
  1844. lenbuf_len = strlen(lenbuf);
  1845. /* Then send length information, chunk and terminating \r\n. */
  1846. ret = mg_write(conn, lenbuf, lenbuf_len);
  1847. ck_assert_int_eq(ret, (int)lenbuf_len);
  1848. ret = mg_write(conn, chunk, chunk_len);
  1849. ck_assert_int_eq(ret, (int)chunk_len);
  1850. ret = mg_write(conn, "\r\n", 2);
  1851. ck_assert_int_eq(ret, 2);
  1852. }
  1853. static void
  1854. send_chunk_string(struct mg_connection *conn, const char *chunk)
  1855. {
  1856. send_chunk_stringl(conn, chunk, (unsigned int)strlen(chunk));
  1857. }
  1858. START_TEST(test_handle_form)
  1859. {
  1860. struct mg_context *ctx;
  1861. struct mg_connection *client_conn;
  1862. const struct mg_request_info *ri;
  1863. const char *OPTIONS[8];
  1864. const char *opt;
  1865. int opt_idx = 0;
  1866. char ebuf[100];
  1867. const char *multipart_body;
  1868. const char *boundary;
  1869. size_t body_len, body_sent, chunk_len;
  1870. int sleep_cnt;
  1871. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  1872. OPTIONS[opt_idx++] = "listening_ports";
  1873. OPTIONS[opt_idx++] = "8884";
  1874. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  1875. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  1876. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  1877. ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
  1878. ck_assert(ctx != NULL);
  1879. g_ctx = ctx;
  1880. opt = mg_get_option(ctx, "listening_ports");
  1881. ck_assert_str_eq(opt, "8884");
  1882. mg_set_request_handler(ctx, "/handle_form", FormGet, (void *)0);
  1883. mg_set_request_handler(ctx, "/handle_form_store", FormStore1, (void *)0);
  1884. mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, (void *)0);
  1885. test_sleep(1);
  1886. /* Handle form: "GET" */
  1887. client_conn = mg_download("localhost",
  1888. 8884,
  1889. 0,
  1890. ebuf,
  1891. sizeof(ebuf),
  1892. "%s",
  1893. "GET /handle_form"
  1894. "?textin=text&passwordin=&radio1=val1"
  1895. "&radio2=val1&check1=val1&numberin=1"
  1896. "&datein=1.1.2016&colorin=%2380ff00"
  1897. "&rangein=3&monthin=&weekin=&timein="
  1898. "&datetimen=&datetimelocalin=&emailin="
  1899. "&searchin=&telin=&urlin=&filein="
  1900. "&filesin=&selectin=opt1"
  1901. "&message=Text+area+default+text. "
  1902. "HTTP/1.0\r\n"
  1903. "Host: localhost:8884\r\n"
  1904. "Connection: close\r\n\r\n");
  1905. ck_assert(client_conn != NULL);
  1906. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1907. test_sleep(1);
  1908. if (g_field_step == 1000) {
  1909. break;
  1910. }
  1911. }
  1912. ri = mg_get_request_info(client_conn);
  1913. ck_assert(ri != NULL);
  1914. ck_assert_str_eq(ri->uri, "200");
  1915. mg_close_connection(client_conn);
  1916. /* Handle form: "POST x-www-form-urlencoded" */
  1917. client_conn =
  1918. mg_download("localhost",
  1919. 8884,
  1920. 0,
  1921. ebuf,
  1922. sizeof(ebuf),
  1923. "%s",
  1924. "POST /handle_form HTTP/1.1\r\n"
  1925. "Host: localhost:8884\r\n"
  1926. "Connection: close\r\n"
  1927. "Content-Type: application/x-www-form-urlencoded\r\n"
  1928. "Content-Length: 263\r\n"
  1929. "\r\n"
  1930. "textin=text&passwordin=&radio1=val1&radio2=val1"
  1931. "&check1=val1&numberin=1&datein=1.1.2016"
  1932. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  1933. "&timein=&datetimen=&datetimelocalin=&emailin="
  1934. "&searchin=&telin=&urlin=&filein=&filesin="
  1935. "&selectin=opt1&message=Text+area+default+text.");
  1936. ck_assert(client_conn != NULL);
  1937. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1938. test_sleep(1);
  1939. if (g_field_step == 1000) {
  1940. break;
  1941. }
  1942. }
  1943. ri = mg_get_request_info(client_conn);
  1944. ck_assert(ri != NULL);
  1945. ck_assert_str_eq(ri->uri, "200");
  1946. mg_close_connection(client_conn);
  1947. /* Handle form: "POST multipart/form-data" */
  1948. multipart_body =
  1949. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1950. "Content-Disposition: form-data; name=\"textin\"\r\n"
  1951. "\r\n"
  1952. "text\r\n"
  1953. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1954. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  1955. "\r\n"
  1956. "\r\n"
  1957. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1958. "Content-Disposition: form-data; name=\"radio1\"\r\n"
  1959. "\r\n"
  1960. "val1\r\n"
  1961. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1962. "Content-Disposition: form-data; name=\"radio2\"\r\n"
  1963. "\r\n"
  1964. "val1\r\n"
  1965. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1966. "Content-Disposition: form-data; name=\"check1\"\r\n"
  1967. "\r\n"
  1968. "val1\r\n"
  1969. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1970. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  1971. "\r\n"
  1972. "1\r\n"
  1973. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1974. "Content-Disposition: form-data; name=\"datein\"\r\n"
  1975. "\r\n"
  1976. "1.1.2016\r\n"
  1977. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1978. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  1979. "\r\n"
  1980. "#80ff00\r\n"
  1981. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1982. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  1983. "\r\n"
  1984. "3\r\n"
  1985. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1986. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  1987. "\r\n"
  1988. "\r\n"
  1989. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1990. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  1991. "\r\n"
  1992. "\r\n"
  1993. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1994. "Content-Disposition: form-data; name=\"timein\"\r\n"
  1995. "\r\n"
  1996. "\r\n"
  1997. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1998. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  1999. "\r\n"
  2000. "\r\n"
  2001. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2002. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  2003. "\r\n"
  2004. "\r\n"
  2005. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2006. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  2007. "\r\n"
  2008. "\r\n"
  2009. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2010. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  2011. "\r\n"
  2012. "\r\n"
  2013. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2014. "Content-Disposition: form-data; name=\"telin\"\r\n"
  2015. "\r\n"
  2016. "\r\n"
  2017. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2018. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  2019. "\r\n"
  2020. "\r\n"
  2021. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2022. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  2023. "Content-Type: application/octet-stream\r\n"
  2024. "\r\n"
  2025. "\r\n"
  2026. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2027. "Content-Disposition: form-data; name=\"filesin\"; filename=\"\"\r\n"
  2028. "Content-Type: application/octet-stream\r\n"
  2029. "\r\n"
  2030. "\r\n"
  2031. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2032. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  2033. "\r\n"
  2034. "opt1\r\n"
  2035. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2036. "Content-Disposition: form-data; name=\"message\"\r\n"
  2037. "\r\n"
  2038. "Text area default text.\r\n"
  2039. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  2040. body_len = strlen(multipart_body);
  2041. ck_assert_uint_eq(body_len, 2374); /* not required */
  2042. client_conn =
  2043. mg_download("localhost",
  2044. 8884,
  2045. 0,
  2046. ebuf,
  2047. sizeof(ebuf),
  2048. "POST /handle_form HTTP/1.1\r\n"
  2049. "Host: localhost:8884\r\n"
  2050. "Connection: close\r\n"
  2051. "Content-Type: multipart/form-data; "
  2052. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2053. "Content-Length: %u\r\n"
  2054. "\r\n%s",
  2055. (unsigned int)body_len,
  2056. multipart_body);
  2057. ck_assert(client_conn != NULL);
  2058. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2059. test_sleep(1);
  2060. if (g_field_step == 1000) {
  2061. break;
  2062. }
  2063. }
  2064. ri = mg_get_request_info(client_conn);
  2065. ck_assert(ri != NULL);
  2066. ck_assert_str_eq(ri->uri, "200");
  2067. mg_close_connection(client_conn);
  2068. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  2069. client_conn =
  2070. mg_download("localhost",
  2071. 8884,
  2072. 0,
  2073. ebuf,
  2074. sizeof(ebuf),
  2075. "%s",
  2076. "POST /handle_form HTTP/1.1\r\n"
  2077. "Host: localhost:8884\r\n"
  2078. "Connection: close\r\n"
  2079. "Content-Type: multipart/form-data; "
  2080. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2081. "Transfer-Encoding: chunked\r\n"
  2082. "\r\n");
  2083. ck_assert(client_conn != NULL);
  2084. body_len = strlen(multipart_body);
  2085. chunk_len = 1;
  2086. body_sent = 0;
  2087. while (body_len > body_sent) {
  2088. if (chunk_len > (body_len - body_sent)) {
  2089. chunk_len = body_len - body_sent;
  2090. }
  2091. ck_assert_int_gt((int)chunk_len, 0);
  2092. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2093. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2094. mg_printf(client_conn, "\r\n");
  2095. body_sent += chunk_len;
  2096. chunk_len = (chunk_len % 40) + 1;
  2097. }
  2098. mg_printf(client_conn, "0\r\n");
  2099. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2100. test_sleep(1);
  2101. if (g_field_step == 1000) {
  2102. break;
  2103. }
  2104. }
  2105. ri = mg_get_request_info(client_conn);
  2106. ck_assert(ri != NULL);
  2107. ck_assert_str_eq(ri->uri, "200");
  2108. mg_close_connection(client_conn);
  2109. /* Now test form_store */
  2110. /* First test with GET */
  2111. client_conn = mg_download("localhost",
  2112. 8884,
  2113. 0,
  2114. ebuf,
  2115. sizeof(ebuf),
  2116. "%s",
  2117. "GET /handle_form_store"
  2118. "?storeme=storetest"
  2119. "&continue_field_handler=ignore"
  2120. "&break_field_handler=abort"
  2121. "&dontread=xyz "
  2122. "HTTP/1.0\r\n"
  2123. "Host: localhost:8884\r\n"
  2124. "Connection: close\r\n\r\n");
  2125. ck_assert(client_conn != NULL);
  2126. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2127. test_sleep(1);
  2128. if (g_field_step == 1000) {
  2129. break;
  2130. }
  2131. }
  2132. ri = mg_get_request_info(client_conn);
  2133. ck_assert(ri != NULL);
  2134. ck_assert_str_eq(ri->uri, "200");
  2135. mg_close_connection(client_conn);
  2136. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  2137. client_conn =
  2138. mg_download("localhost",
  2139. 8884,
  2140. 0,
  2141. ebuf,
  2142. sizeof(ebuf),
  2143. "%s",
  2144. "POST /handle_form_store HTTP/1.0\r\n"
  2145. "Host: localhost:8884\r\n"
  2146. "Connection: close\r\n"
  2147. "Content-Type: application/x-www-form-urlencoded\r\n"
  2148. "Transfer-Encoding: chunked\r\n"
  2149. "\r\n");
  2150. ck_assert(client_conn != NULL);
  2151. send_chunk_string(client_conn, "storeme=store");
  2152. send_chunk_string(client_conn, "test&");
  2153. send_chunk_string(client_conn, "continue_field_handler=ignore");
  2154. send_chunk_string(client_conn, "&br");
  2155. test_sleep(1);
  2156. send_chunk_string(client_conn, "eak_field_handler=abort&");
  2157. send_chunk_string(client_conn, "dontread=xyz");
  2158. mg_printf(client_conn, "0\r\n");
  2159. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2160. test_sleep(1);
  2161. if (g_field_step == 1000) {
  2162. break;
  2163. }
  2164. }
  2165. ri = mg_get_request_info(client_conn);
  2166. ck_assert(ri != NULL);
  2167. ck_assert_str_eq(ri->uri, "200");
  2168. mg_close_connection(client_conn);
  2169. /* Handle form: "POST multipart/form-data", chunked, store */
  2170. client_conn =
  2171. mg_download("localhost",
  2172. 8884,
  2173. 0,
  2174. ebuf,
  2175. sizeof(ebuf),
  2176. "%s",
  2177. "POST /handle_form_store HTTP/1.0\r\n"
  2178. "Host: localhost:8884\r\n"
  2179. "Connection: close\r\n"
  2180. "Content-Type: multipart/form-data; "
  2181. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2182. "Transfer-Encoding: chunked\r\n"
  2183. "\r\n");
  2184. ck_assert(client_conn != NULL);
  2185. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2186. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  2187. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2188. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2189. send_chunk_string(client_conn, "\r\n");
  2190. send_chunk_string(client_conn, "storetest\r\n");
  2191. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2192. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2193. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2194. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  2195. send_chunk_string(client_conn, "\r\n");
  2196. send_chunk_string(client_conn, "ignore\r\n");
  2197. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2198. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2199. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2200. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2201. send_chunk_string(client_conn, "\r\n");
  2202. send_chunk_string(client_conn, "abort\r\n");
  2203. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2204. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2205. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2206. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2207. send_chunk_string(client_conn, "\r\n");
  2208. send_chunk_string(client_conn, "xyz\r\n");
  2209. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2210. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2211. mg_printf(client_conn, "0\r\n");
  2212. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2213. test_sleep(1);
  2214. if (g_field_step == 1000) {
  2215. break;
  2216. }
  2217. }
  2218. ri = mg_get_request_info(client_conn);
  2219. ck_assert(ri != NULL);
  2220. ck_assert_str_eq(ri->uri, "200");
  2221. mg_close_connection(client_conn);
  2222. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  2223. client_conn =
  2224. mg_download("localhost",
  2225. 8884,
  2226. 0,
  2227. ebuf,
  2228. sizeof(ebuf),
  2229. "%s",
  2230. "POST /handle_form_store2 HTTP/1.0\r\n"
  2231. "Host: localhost:8884\r\n"
  2232. "Connection: close\r\n"
  2233. "Content-Type: multipart/form-data; "
  2234. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2235. "Transfer-Encoding: chunked\r\n"
  2236. "\r\n");
  2237. ck_assert(client_conn != NULL);
  2238. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  2239. send_chunk_string(client_conn, boundary);
  2240. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2241. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2242. send_chunk_string(client_conn, "\r\n");
  2243. send_chunk_string(client_conn, "storetest\r\n");
  2244. send_chunk_string(client_conn, boundary);
  2245. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2246. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  2247. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  2248. send_chunk_string(client_conn, "Content-Type: ");
  2249. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2250. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2251. send_chunk_string(client_conn, "\r\n");
  2252. /* send some kilobyte of data */
  2253. /* sending megabytes to localhost does not allways work in CI test
  2254. * environments (depending on the network stack) */
  2255. body_sent = 0;
  2256. do {
  2257. send_chunk_string(client_conn, "ignore\r\n");
  2258. body_sent += 8;
  2259. /* send some strings that are almost boundaries */
  2260. for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
  2261. /* chunks from 1 byte to strlen(boundary)-1 */
  2262. send_chunk_stringl(client_conn, boundary, (unsigned int)chunk_len);
  2263. body_sent += chunk_len;
  2264. }
  2265. } while (body_sent < 8 * 1024);
  2266. send_chunk_string(client_conn, "\r\n");
  2267. send_chunk_string(client_conn, boundary);
  2268. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2269. send_chunk_string(client_conn, "name=\"file2store\";");
  2270. send_chunk_string(client_conn, "filename=\"myfile.txt\"\r\n");
  2271. send_chunk_string(client_conn, "Content-Type: ");
  2272. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2273. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2274. send_chunk_string(client_conn, "\r\n");
  2275. for (body_sent = 0; (int)body_sent < (int)myfile_content_rep; body_sent++) {
  2276. send_chunk_string(client_conn, myfile_content);
  2277. }
  2278. send_chunk_string(client_conn, "\r\n");
  2279. send_chunk_string(client_conn, boundary);
  2280. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2281. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2282. send_chunk_string(client_conn, "\r\n");
  2283. send_chunk_string(client_conn, "abort\r\n");
  2284. send_chunk_string(client_conn, boundary);
  2285. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2286. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2287. send_chunk_string(client_conn, "\r\n");
  2288. send_chunk_string(client_conn, "xyz\r\n");
  2289. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2290. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2291. mg_printf(client_conn, "0\r\n");
  2292. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2293. test_sleep(1);
  2294. if (g_field_step == 1000) {
  2295. break;
  2296. }
  2297. }
  2298. ri = mg_get_request_info(client_conn);
  2299. ck_assert(ri != NULL);
  2300. ck_assert_str_eq(ri->uri, "200");
  2301. mg_close_connection(client_conn);
  2302. /* Close the server */
  2303. g_ctx = NULL;
  2304. test_mg_stop(ctx);
  2305. mark_point();
  2306. }
  2307. END_TEST
  2308. START_TEST(test_http_auth)
  2309. {
  2310. #if !defined(NO_FILES)
  2311. const char *OPTIONS[] = {
  2312. "document_root",
  2313. ".",
  2314. "listening_ports",
  2315. "8080",
  2316. #if !defined(NO_CACHING)
  2317. "static_file_max_age",
  2318. "0",
  2319. #endif
  2320. NULL,
  2321. };
  2322. struct mg_context *ctx;
  2323. struct mg_connection *client_conn;
  2324. char client_err[256], nonce[256];
  2325. const struct mg_request_info *client_ri;
  2326. int client_res;
  2327. FILE *f;
  2328. const char *passwd_file = ".htpasswd";
  2329. const char *test_file = "test_http_auth.test_file.txt";
  2330. const char *test_content = "test_http_auth test_file content";
  2331. const char *domain;
  2332. const char *doc_root;
  2333. const char *auth_request;
  2334. const char *str;
  2335. size_t len;
  2336. int i;
  2337. char HA1[256], HA2[256], HA[256];
  2338. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2339. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2340. const char *nc = "00000001";
  2341. const char *cnonce = "6789ABCD";
  2342. /* Start with default options */
  2343. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2344. ck_assert(ctx != NULL);
  2345. domain = mg_get_option(ctx, "authentication_domain");
  2346. ck_assert(domain != NULL);
  2347. len = strlen(domain);
  2348. ck_assert_uint_gt(len, 0);
  2349. ck_assert_uint_lt(len, 64);
  2350. doc_root = mg_get_option(ctx, "document_root");
  2351. ck_assert_str_eq(doc_root, ".");
  2352. /* Create a default file in the document root */
  2353. f = fopen(test_file, "w");
  2354. if (f) {
  2355. fprintf(f, "%s", test_content);
  2356. fclose(f);
  2357. } else {
  2358. ck_abort_msg("Cannot create file %s", test_file);
  2359. }
  2360. (void)remove(passwd_file);
  2361. /* Read file before a .htpasswd file has been created */
  2362. memset(client_err, 0, sizeof(client_err));
  2363. client_conn =
  2364. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2365. ck_assert(client_conn != NULL);
  2366. ck_assert_str_eq(client_err, "");
  2367. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2368. client_res =
  2369. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2370. ck_assert_int_ge(client_res, 0);
  2371. ck_assert_str_eq(client_err, "");
  2372. client_ri = mg_get_request_info(client_conn);
  2373. ck_assert(client_ri != NULL);
  2374. ck_assert_str_eq(client_ri->uri, "200");
  2375. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2376. ck_assert_int_gt(client_res, 0);
  2377. ck_assert_int_le(client_res, sizeof(client_err));
  2378. ck_assert_str_eq(client_err, test_content);
  2379. mg_close_connection(client_conn);
  2380. test_sleep(1);
  2381. /* Create a .htpasswd file */
  2382. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2383. ck_assert_int_eq(client_res, 1);
  2384. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2385. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2386. test_sleep(1);
  2387. /* Repeat test after .htpasswd is created */
  2388. memset(client_err, 0, sizeof(client_err));
  2389. client_conn =
  2390. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2391. ck_assert(client_conn != NULL);
  2392. ck_assert_str_eq(client_err, "");
  2393. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2394. client_res =
  2395. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2396. ck_assert_int_ge(client_res, 0);
  2397. ck_assert_str_eq(client_err, "");
  2398. client_ri = mg_get_request_info(client_conn);
  2399. ck_assert(client_ri != NULL);
  2400. ck_assert_str_eq(client_ri->uri, "401");
  2401. auth_request = NULL;
  2402. for (i = 0; i < client_ri->num_headers; i++) {
  2403. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2404. "WWW-Authenticate")) {
  2405. ck_assert_ptr_eq(auth_request, NULL);
  2406. auth_request = client_ri->http_headers[i].value;
  2407. ck_assert_ptr_ne(auth_request, NULL);
  2408. }
  2409. }
  2410. ck_assert_ptr_ne(auth_request, NULL);
  2411. str = "Digest qop=\"auth\", realm=\"";
  2412. len = strlen(str);
  2413. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2414. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2415. len += strlen(domain);
  2416. str = "\", nonce=\"";
  2417. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2418. len += strlen(str);
  2419. str = strchr(auth_request + len, '\"');
  2420. ck_assert_ptr_ne(str, NULL);
  2421. ck_assert_ptr_ne(str, auth_request + len);
  2422. /* nonce is from including (auth_request + len) to excluding (str) */
  2423. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2424. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2425. (ptrdiff_t)sizeof(nonce));
  2426. memset(nonce, 0, sizeof(nonce));
  2427. memcpy(nonce,
  2428. auth_request + len,
  2429. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2430. memset(HA1, 0, sizeof(HA1));
  2431. memset(HA2, 0, sizeof(HA2));
  2432. memset(HA, 0, sizeof(HA));
  2433. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2434. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2435. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2436. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2437. sprintf(HA2, "%s:/%s", "GET", test_file);
  2438. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2439. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2440. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2441. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2442. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2443. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2444. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2445. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2446. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2447. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2448. HA_md5_ret = mg_md5(HA_md5_buf,
  2449. HA1_md5_buf,
  2450. ":",
  2451. nonce,
  2452. ":",
  2453. nc,
  2454. ":",
  2455. cnonce,
  2456. ":",
  2457. "auth",
  2458. ":",
  2459. HA2_md5_buf,
  2460. NULL);
  2461. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2462. /* Retry with Authorization */
  2463. memset(client_err, 0, sizeof(client_err));
  2464. client_conn =
  2465. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2466. ck_assert(client_conn != NULL);
  2467. ck_assert_str_eq(client_err, "");
  2468. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2469. mg_printf(client_conn,
  2470. "Authorization: Digest "
  2471. "username=\"%s\", "
  2472. "realm=\"%s\", "
  2473. "nonce=\"%s\", "
  2474. "uri=\"/%s\", "
  2475. "qop=auth, "
  2476. "nc=%s, "
  2477. "cnonce=\"%s\", "
  2478. "response=\"%s\"\r\n\r\n",
  2479. "user",
  2480. domain,
  2481. nonce,
  2482. test_file,
  2483. nc,
  2484. cnonce,
  2485. HA_md5_buf);
  2486. client_res =
  2487. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2488. ck_assert_int_ge(client_res, 0);
  2489. ck_assert_str_eq(client_err, "");
  2490. client_ri = mg_get_request_info(client_conn);
  2491. ck_assert(client_ri != NULL);
  2492. ck_assert_str_eq(client_ri->uri, "200");
  2493. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2494. ck_assert_int_gt(client_res, 0);
  2495. ck_assert_int_le(client_res, sizeof(client_err));
  2496. ck_assert_str_eq(client_err, test_content);
  2497. mg_close_connection(client_conn);
  2498. test_sleep(1);
  2499. /* Remove the user from the .htpasswd file again */
  2500. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2501. ck_assert_int_eq(client_res, 1);
  2502. test_sleep(1);
  2503. /* Try to access the file again. Expected: 401 error */
  2504. memset(client_err, 0, sizeof(client_err));
  2505. client_conn =
  2506. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2507. ck_assert(client_conn != NULL);
  2508. ck_assert_str_eq(client_err, "");
  2509. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2510. client_res =
  2511. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2512. ck_assert_int_ge(client_res, 0);
  2513. ck_assert_str_eq(client_err, "");
  2514. client_ri = mg_get_request_info(client_conn);
  2515. ck_assert(client_ri != NULL);
  2516. ck_assert_str_eq(client_ri->uri, "401");
  2517. mg_close_connection(client_conn);
  2518. test_sleep(1);
  2519. /* Now remove the password file */
  2520. (void)remove(passwd_file);
  2521. test_sleep(1);
  2522. /* Access to the file must work like before */
  2523. memset(client_err, 0, sizeof(client_err));
  2524. client_conn =
  2525. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2526. ck_assert(client_conn != NULL);
  2527. ck_assert_str_eq(client_err, "");
  2528. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2529. client_res =
  2530. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2531. ck_assert_int_ge(client_res, 0);
  2532. ck_assert_str_eq(client_err, "");
  2533. client_ri = mg_get_request_info(client_conn);
  2534. ck_assert(client_ri != NULL);
  2535. ck_assert_str_eq(client_ri->uri, "200");
  2536. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2537. ck_assert_int_gt(client_res, 0);
  2538. ck_assert_int_le(client_res, sizeof(client_err));
  2539. ck_assert_str_eq(client_err, test_content);
  2540. mg_close_connection(client_conn);
  2541. test_sleep(1);
  2542. /* Stop the server and clean up */
  2543. test_mg_stop(ctx);
  2544. (void)remove(test_file);
  2545. (void)remove(passwd_file);
  2546. #endif
  2547. }
  2548. END_TEST
  2549. START_TEST(test_keep_alive)
  2550. {
  2551. struct mg_context *ctx;
  2552. const char *OPTIONS[] =
  2553. { "listening_ports",
  2554. "8080",
  2555. "request_timeout_ms",
  2556. "10000",
  2557. "enable_keep_alive",
  2558. "yes",
  2559. #if !defined(NO_FILES)
  2560. "document_root",
  2561. ".",
  2562. "enable_directory_listing",
  2563. "no",
  2564. #endif
  2565. NULL };
  2566. struct mg_connection *client_conn;
  2567. char client_err[256];
  2568. const struct mg_request_info *client_ri;
  2569. int client_res, i;
  2570. const char *connection_header;
  2571. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2572. ck_assert(ctx != NULL);
  2573. /* HTTP 1.1 GET request */
  2574. memset(client_err, 0, sizeof(client_err));
  2575. client_conn =
  2576. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2577. ck_assert(client_conn != NULL);
  2578. ck_assert_str_eq(client_err, "");
  2579. mg_printf(client_conn,
  2580. "GET / HTTP/1.1\r\nHost: "
  2581. "localhost:8080\r\nConnection: keep-alive\r\n\r\n");
  2582. client_res =
  2583. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2584. ck_assert_int_ge(client_res, 0);
  2585. ck_assert_str_eq(client_err, "");
  2586. client_ri = mg_get_request_info(client_conn);
  2587. ck_assert(client_ri != NULL);
  2588. #if defined(NO_FILES)
  2589. ck_assert_str_eq(client_ri->uri, "404");
  2590. #else
  2591. ck_assert_str_eq(client_ri->uri, "403");
  2592. #endif
  2593. connection_header = 0;
  2594. for (i = 0; i < client_ri->num_headers; i++) {
  2595. if (!mg_strcasecmp(client_ri->http_headers[i].name, "Connection")) {
  2596. ck_assert_ptr_eq(connection_header, NULL);
  2597. connection_header = client_ri->http_headers[i].value;
  2598. ck_assert_ptr_ne(connection_header, NULL);
  2599. }
  2600. }
  2601. /* Error replies will close the connection, even if keep-alive is set. */
  2602. ck_assert_ptr_ne(connection_header, NULL);
  2603. ck_assert_str_eq(connection_header, "close");
  2604. mg_close_connection(client_conn);
  2605. test_sleep(1);
  2606. /* TODO: request a file and keep alive
  2607. * (will only work if NO_FILES is not set). */
  2608. /* Stop the server and clean up */
  2609. test_mg_stop(ctx);
  2610. }
  2611. END_TEST
  2612. START_TEST(test_error_handling)
  2613. {
  2614. struct mg_context *ctx;
  2615. FILE *f;
  2616. char bad_thread_num[32] = "badnumber";
  2617. struct mg_callbacks callbacks;
  2618. char errmsg[256];
  2619. struct mg_connection *client_conn;
  2620. char client_err[256];
  2621. const struct mg_request_info *client_ri;
  2622. int client_res, i;
  2623. const char *OPTIONS[32];
  2624. int opt_cnt = 0;
  2625. #if !defined(NO_FILES)
  2626. OPTIONS[opt_cnt++] = "document_root";
  2627. OPTIONS[opt_cnt++] = ".";
  2628. #endif
  2629. OPTIONS[opt_cnt++] = "error_pages";
  2630. OPTIONS[opt_cnt++] = "./";
  2631. OPTIONS[opt_cnt++] = "listening_ports";
  2632. OPTIONS[opt_cnt++] = "8080";
  2633. OPTIONS[opt_cnt++] = "num_threads";
  2634. OPTIONS[opt_cnt++] = bad_thread_num;
  2635. OPTIONS[opt_cnt++] = "unknown_option";
  2636. OPTIONS[opt_cnt++] = "unknown_option_value";
  2637. OPTIONS[opt_cnt] = NULL;
  2638. memset(&callbacks, 0, sizeof(callbacks));
  2639. callbacks.log_message = log_msg_func;
  2640. /* test with unknown option */
  2641. memset(errmsg, 0, sizeof(errmsg));
  2642. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2643. /* Details of errmsg may vary, but it may not be empty */
  2644. ck_assert_str_ne(errmsg, "");
  2645. ck_assert(ctx == NULL);
  2646. ck_assert_str_eq(errmsg, "Invalid option: unknown_option");
  2647. /* Remove invalid option */
  2648. for (i = 0; OPTIONS[i]; i++) {
  2649. if (strstr(OPTIONS[i], "unknown_option")) {
  2650. OPTIONS[i] = 0;
  2651. }
  2652. }
  2653. /* Test with bad num_thread option */
  2654. memset(errmsg, 0, sizeof(errmsg));
  2655. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2656. /* Details of errmsg may vary, but it may not be empty */
  2657. ck_assert_str_ne(errmsg, "");
  2658. ck_assert(ctx == NULL);
  2659. ck_assert_str_eq(errmsg, "Invalid number of worker threads");
  2660. /* Set to a number - but use a number above the limit */
  2661. #ifdef MAX_WORKER_THREADS
  2662. sprintf(bad_thread_num, "%u", MAX_WORKER_THREADS + 1);
  2663. #else
  2664. sprintf(bad_thread_num, "%lu", 1000000000lu);
  2665. #endif
  2666. /* Test with bad num_thread option */
  2667. memset(errmsg, 0, sizeof(errmsg));
  2668. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2669. /* Details of errmsg may vary, but it may not be empty */
  2670. ck_assert_str_ne(errmsg, "");
  2671. ck_assert(ctx == NULL);
  2672. ck_assert_str_eq(errmsg, "Too many worker threads");
  2673. /* HTTP 1.0 GET request - server is not running */
  2674. memset(client_err, 0, sizeof(client_err));
  2675. client_conn =
  2676. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2677. ck_assert(client_conn == NULL);
  2678. /* Error message detail may vary - it may not be empty ans should contain
  2679. * some information "connect" failed */
  2680. ck_assert_str_ne(client_err, "");
  2681. ck_assert(strstr(client_err, "connect"));
  2682. /* This time start the server with a valid configuration */
  2683. sprintf(bad_thread_num, "%i", 1);
  2684. memset(errmsg, 0, sizeof(errmsg));
  2685. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2686. ck_assert_str_eq(errmsg, "");
  2687. ck_assert(ctx != NULL);
  2688. /* Server is running now */
  2689. test_sleep(1);
  2690. /* Remove error files (in case they exist) */
  2691. (void)remove("error.htm");
  2692. (void)remove("error4xx.htm");
  2693. (void)remove("error404.htm");
  2694. /* Ask for something not existing - should get default 404 */
  2695. memset(client_err, 0, sizeof(client_err));
  2696. client_conn =
  2697. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2698. ck_assert(client_conn != NULL);
  2699. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2700. client_res =
  2701. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2702. ck_assert_int_ge(client_res, 0);
  2703. ck_assert_str_eq(client_err, "");
  2704. client_ri = mg_get_request_info(client_conn);
  2705. ck_assert(client_ri != NULL);
  2706. ck_assert_str_eq(client_ri->uri, "404");
  2707. mg_close_connection(client_conn);
  2708. test_sleep(1);
  2709. /* Create an error.htm file */
  2710. f = fopen("error.htm", "wt");
  2711. ck_assert(f != NULL);
  2712. (void)fprintf(f, "err-all");
  2713. (void)fclose(f);
  2714. /* Ask for something not existing - should get error.htm */
  2715. memset(client_err, 0, sizeof(client_err));
  2716. client_conn =
  2717. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2718. ck_assert(client_conn != NULL);
  2719. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2720. client_res =
  2721. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2722. ck_assert_int_ge(client_res, 0);
  2723. ck_assert_str_eq(client_err, "");
  2724. client_ri = mg_get_request_info(client_conn);
  2725. ck_assert(client_ri != NULL);
  2726. ck_assert_str_eq(client_ri->uri, "200");
  2727. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2728. mg_close_connection(client_conn);
  2729. ck_assert_int_eq(client_res, 7);
  2730. client_err[8] = 0;
  2731. ck_assert_str_eq(client_err, "err-all");
  2732. test_sleep(1);
  2733. /* Create an error4xx.htm file */
  2734. f = fopen("error4xx.htm", "wt");
  2735. ck_assert(f != NULL);
  2736. (void)fprintf(f, "err-4xx");
  2737. (void)fclose(f);
  2738. /* Ask for something not existing - should get error4xx.htm */
  2739. memset(client_err, 0, sizeof(client_err));
  2740. client_conn =
  2741. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2742. ck_assert(client_conn != NULL);
  2743. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2744. client_res =
  2745. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2746. ck_assert_int_ge(client_res, 0);
  2747. ck_assert_str_eq(client_err, "");
  2748. client_ri = mg_get_request_info(client_conn);
  2749. ck_assert(client_ri != NULL);
  2750. ck_assert_str_eq(client_ri->uri, "200");
  2751. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2752. mg_close_connection(client_conn);
  2753. ck_assert_int_eq(client_res, 7);
  2754. client_err[8] = 0;
  2755. ck_assert_str_eq(client_err, "err-4xx");
  2756. test_sleep(1);
  2757. /* Create an error404.htm file */
  2758. f = fopen("error404.htm", "wt");
  2759. ck_assert(f != NULL);
  2760. (void)fprintf(f, "err-404");
  2761. (void)fclose(f);
  2762. /* Ask for something not existing - should get error404.htm */
  2763. memset(client_err, 0, sizeof(client_err));
  2764. client_conn =
  2765. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2766. ck_assert(client_conn != NULL);
  2767. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2768. client_res =
  2769. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2770. ck_assert_int_ge(client_res, 0);
  2771. ck_assert_str_eq(client_err, "");
  2772. client_ri = mg_get_request_info(client_conn);
  2773. ck_assert(client_ri != NULL);
  2774. ck_assert_str_eq(client_ri->uri, "200");
  2775. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2776. mg_close_connection(client_conn);
  2777. ck_assert_int_eq(client_res, 7);
  2778. client_err[8] = 0;
  2779. ck_assert_str_eq(client_err, "err-404");
  2780. test_sleep(1);
  2781. /* Ask in a malformed way - should get error4xx.htm */
  2782. memset(client_err, 0, sizeof(client_err));
  2783. client_conn =
  2784. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2785. ck_assert(client_conn != NULL);
  2786. mg_printf(client_conn, "Gimme some file!\r\n\r\n");
  2787. client_res =
  2788. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2789. ck_assert_int_ge(client_res, 0);
  2790. ck_assert_str_eq(client_err, "");
  2791. client_ri = mg_get_request_info(client_conn);
  2792. ck_assert(client_ri != NULL);
  2793. ck_assert_str_eq(client_ri->uri, "200");
  2794. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2795. mg_close_connection(client_conn);
  2796. ck_assert_int_eq(client_res, 7);
  2797. client_err[8] = 0;
  2798. ck_assert_str_eq(client_err, "err-4xx");
  2799. test_sleep(1);
  2800. /* Remove all error files created by this test */
  2801. (void)remove("error.htm");
  2802. (void)remove("error4xx.htm");
  2803. (void)remove("error404.htm");
  2804. /* Stop the server */
  2805. test_mg_stop(ctx);
  2806. /* HTTP 1.1 GET request - must not work, since server is already stopped */
  2807. memset(client_err, 0, sizeof(client_err));
  2808. client_conn =
  2809. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2810. ck_assert(client_conn == NULL);
  2811. ck_assert_str_ne(client_err, "");
  2812. test_sleep(1);
  2813. }
  2814. END_TEST
  2815. START_TEST(test_error_log_file)
  2816. {
  2817. /* Server var */
  2818. struct mg_context *ctx;
  2819. const char *OPTIONS[32];
  2820. int opt_cnt = 0;
  2821. /* Client var */
  2822. struct mg_connection *client;
  2823. char client_err_buf[256];
  2824. char client_data_buf[256];
  2825. const struct mg_request_info *client_ri;
  2826. /* File content check var */
  2827. FILE *f;
  2828. char buf[1024];
  2829. int len, ok;
  2830. /* Set options and start server */
  2831. OPTIONS[opt_cnt++] = "listening_ports";
  2832. OPTIONS[opt_cnt++] = "8080";
  2833. OPTIONS[opt_cnt++] = "error_log_file";
  2834. OPTIONS[opt_cnt++] = "error.log";
  2835. OPTIONS[opt_cnt++] = "access_log_file";
  2836. OPTIONS[opt_cnt++] = "access.log";
  2837. #if !defined(NO_FILES)
  2838. OPTIONS[opt_cnt++] = "document_root";
  2839. OPTIONS[opt_cnt++] = ".";
  2840. #endif
  2841. OPTIONS[opt_cnt] = NULL;
  2842. ctx = test_mg_start(NULL, 0, OPTIONS);
  2843. ck_assert(ctx != NULL);
  2844. /* Remove log files (they may exist from previous incomplete runs of
  2845. * this test) */
  2846. (void)remove("error.log");
  2847. (void)remove("access.log");
  2848. /* connect client */
  2849. memset(client_err_buf, 0, sizeof(client_err_buf));
  2850. memset(client_data_buf, 0, sizeof(client_data_buf));
  2851. client = mg_download("127.0.0.1",
  2852. 8080,
  2853. 0,
  2854. client_err_buf,
  2855. sizeof(client_err_buf),
  2856. "GET /not_existing_file.ext HTTP/1.0\r\n\r\n");
  2857. ck_assert(ctx != NULL);
  2858. ck_assert_str_eq(client_err_buf, "");
  2859. client_ri = mg_get_request_info(client);
  2860. ck_assert(client_ri != NULL);
  2861. ck_assert_str_eq(client_ri->uri, "404");
  2862. /* Close the client connection */
  2863. mg_close_connection(client);
  2864. /* Stop the server */
  2865. test_mg_stop(ctx);
  2866. /* Check access.log */
  2867. memset(buf, 0, sizeof(buf));
  2868. f = fopen("access.log", "r");
  2869. ck_assert_msg(f != NULL, "Cannot open access log file");
  2870. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  2871. (void)fclose(f);
  2872. ck_assert_msg(ok, "Cannot read access log file");
  2873. len = (int)strlen(buf);
  2874. ck_assert_int_gt(len, 0);
  2875. ok = (NULL != strstr(buf, "not_existing_file.ext"));
  2876. ck_assert_msg(ok, "Did not find uri in access log file");
  2877. ok = (NULL != strstr(buf, "404"));
  2878. ck_assert_msg(ok, "Did not find HTTP status code in access log file");
  2879. /* Check error.log */
  2880. memset(buf, 0, sizeof(buf));
  2881. f = fopen("error.log", "r");
  2882. if (f) {
  2883. (void)fgets(buf, sizeof(buf) - 1, f);
  2884. fclose(f);
  2885. }
  2886. ck_assert_msg(f == NULL,
  2887. "Should not create error log file on 404, but got [%s]",
  2888. buf);
  2889. /* Remove log files */
  2890. (void)remove("error.log");
  2891. (void)remove("access.log");
  2892. /* Start server with bad options */
  2893. ck_assert_str_eq(OPTIONS[0], "listening_ports");
  2894. OPTIONS[1] = "bad port syntax";
  2895. ctx = test_mg_start(NULL, 0, OPTIONS);
  2896. ck_assert_msg(
  2897. ctx == NULL,
  2898. "Should not be able to start server with bad port configuration");
  2899. /* Check access.log */
  2900. memset(buf, 0, sizeof(buf));
  2901. f = fopen("access.log", "r");
  2902. if (f) {
  2903. (void)fgets(buf, sizeof(buf) - 1, f);
  2904. fclose(f);
  2905. }
  2906. ck_assert_msg(
  2907. f == NULL,
  2908. "Should not create access log file if start fails, but got [%s]",
  2909. buf);
  2910. /* Check error.log */
  2911. memset(buf, 0, sizeof(buf));
  2912. f = fopen("error.log", "r");
  2913. ck_assert_msg(f != NULL, "Cannot open access log file");
  2914. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  2915. (void)fclose(f);
  2916. ck_assert_msg(ok, "Cannot read access log file");
  2917. len = (int)strlen(buf);
  2918. ck_assert_int_gt(len, 0);
  2919. ok = (NULL != strstr(buf, "port"));
  2920. ck_assert_msg(ok, "Did not find port as error reason in error log file");
  2921. /* Remove log files */
  2922. (void)remove("error.log");
  2923. (void)remove("access.log");
  2924. }
  2925. END_TEST
  2926. static int
  2927. test_throttle_begin_request(struct mg_connection *conn)
  2928. {
  2929. const struct mg_request_info *ri;
  2930. long unsigned len = 1024 * 10;
  2931. const char *block = "0123456789";
  2932. unsigned long i, blocklen;
  2933. ck_assert(conn != NULL);
  2934. ri = mg_get_request_info(conn);
  2935. ck_assert(ri != NULL);
  2936. ck_assert_str_eq(ri->request_method, "GET");
  2937. ck_assert_str_eq(ri->request_uri, "/throttle");
  2938. ck_assert_str_eq(ri->local_uri, "/throttle");
  2939. ck_assert_str_eq(ri->http_version, "1.0");
  2940. ck_assert_str_eq(ri->query_string, "q");
  2941. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  2942. mg_printf(conn,
  2943. "HTTP/1.1 200 OK\r\n"
  2944. "Content-Length: %lu\r\n"
  2945. "Connection: close\r\n\r\n",
  2946. len);
  2947. blocklen = (unsigned long)strlen(block);
  2948. for (i = 0; i < len; i += blocklen) {
  2949. mg_write(conn, block, blocklen);
  2950. }
  2951. return 987; /* Not a valid HTTP response code,
  2952. * but it should be written to the log and passed to
  2953. * end_request. */
  2954. }
  2955. static void
  2956. test_throttle_end_request(const struct mg_connection *conn,
  2957. int reply_status_code)
  2958. {
  2959. const struct mg_request_info *ri;
  2960. ck_assert(conn != NULL);
  2961. ri = mg_get_request_info(conn);
  2962. ck_assert(ri != NULL);
  2963. ck_assert_str_eq(ri->request_method, "GET");
  2964. ck_assert_str_eq(ri->request_uri, "/throttle");
  2965. ck_assert_str_eq(ri->local_uri, "/throttle");
  2966. ck_assert_str_eq(ri->http_version, "1.0");
  2967. ck_assert_str_eq(ri->query_string, "q");
  2968. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  2969. ck_assert_int_eq(reply_status_code, 987);
  2970. }
  2971. START_TEST(test_throttle)
  2972. {
  2973. /* Server var */
  2974. struct mg_context *ctx;
  2975. struct mg_callbacks callbacks;
  2976. const char *OPTIONS[32];
  2977. int opt_cnt = 0;
  2978. /* Client var */
  2979. struct mg_connection *client;
  2980. char client_err_buf[256];
  2981. char client_data_buf[256];
  2982. const struct mg_request_info *client_ri;
  2983. /* timing test */
  2984. int r, data_read;
  2985. time_t t0, t1;
  2986. double dt;
  2987. /* Set options and start server */
  2988. #if !defined(NO_FILES)
  2989. OPTIONS[opt_cnt++] = "document_root";
  2990. OPTIONS[opt_cnt++] = ".";
  2991. #endif
  2992. OPTIONS[opt_cnt++] = "listening_ports";
  2993. OPTIONS[opt_cnt++] = "8080";
  2994. OPTIONS[opt_cnt++] = "throttle";
  2995. OPTIONS[opt_cnt++] = "*=1k";
  2996. OPTIONS[opt_cnt] = NULL;
  2997. memset(&callbacks, 0, sizeof(callbacks));
  2998. callbacks.begin_request = test_throttle_begin_request;
  2999. callbacks.end_request = test_throttle_end_request;
  3000. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3001. ck_assert(ctx != NULL);
  3002. /* connect client */
  3003. memset(client_err_buf, 0, sizeof(client_err_buf));
  3004. memset(client_data_buf, 0, sizeof(client_data_buf));
  3005. strcpy(client_err_buf, "reset-content");
  3006. client = mg_download("127.0.0.1",
  3007. 8080,
  3008. 0,
  3009. client_err_buf,
  3010. sizeof(client_err_buf),
  3011. "GET /throttle?q HTTP/1.0\r\n\r\n");
  3012. ck_assert(ctx != NULL);
  3013. ck_assert_str_eq(client_err_buf, "");
  3014. client_ri = mg_get_request_info(client);
  3015. ck_assert(client_ri != NULL);
  3016. ck_assert_str_eq(client_ri->uri, "200");
  3017. ck_assert_int_eq(client_ri->content_length, 1024 * 10);
  3018. data_read = 0;
  3019. t0 = time(NULL);
  3020. while (data_read < client_ri->content_length) {
  3021. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3022. ck_assert_int_ge(r, 0);
  3023. data_read += r;
  3024. }
  3025. t1 = time(NULL);
  3026. dt = difftime(t1, t0) * 1000.0; /* Elapsed time in ms - in most systems
  3027. * only with second resolution */
  3028. /* Time estimation: Data size is 10 kB, with 1 kB/s speed limit.
  3029. * The first block (1st kB) is transferred immediately, the second
  3030. * block (2nd kB) one second later, the third block (3rd kB) two
  3031. * seconds later, .. the last block (10th kB) nine seconds later.
  3032. * The resolution of time measurement using the "time" C library
  3033. * function is 1 second, so we should add +/- one second tolerance.
  3034. * Thus, download of 10 kB with 1 kB/s should not be faster than
  3035. * 8 seconds. */
  3036. /* Check if there are at least 8 seconds */
  3037. ck_assert_int_ge((int)dt, 8 * 1000);
  3038. /* Nothing left to read */
  3039. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3040. ck_assert_int_eq(r, 0);
  3041. /* Close the client connection */
  3042. mg_close_connection(client);
  3043. /* Stop the server */
  3044. test_mg_stop(ctx);
  3045. }
  3046. END_TEST
  3047. START_TEST(test_init_library)
  3048. {
  3049. unsigned f_avail = mg_check_feature(0xFF);
  3050. unsigned f_ret = mg_init_library(f_avail);
  3051. ck_assert_uint_eq(f_ret, f_avail);
  3052. }
  3053. END_TEST
  3054. #define LARGE_FILE_SIZE (1024 * 1024 * 10)
  3055. static int
  3056. test_large_file_begin_request(struct mg_connection *conn)
  3057. {
  3058. const struct mg_request_info *ri;
  3059. long unsigned len = LARGE_FILE_SIZE;
  3060. const char *block = "0123456789";
  3061. uint64_t i;
  3062. size_t blocklen;
  3063. ck_assert(conn != NULL);
  3064. ri = mg_get_request_info(conn);
  3065. ck_assert(ri != NULL);
  3066. ck_assert_str_eq(ri->request_method, "GET");
  3067. ck_assert_str_eq(ri->http_version, "1.1");
  3068. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3069. ck_assert_ptr_eq(ri->query_string, NULL);
  3070. ck_assert_ptr_ne(ri->local_uri, NULL);
  3071. mg_printf(conn,
  3072. "HTTP/1.1 200 OK\r\n"
  3073. "Content-Length: %lu\r\n"
  3074. "Connection: close\r\n\r\n",
  3075. len);
  3076. blocklen = strlen(block);
  3077. for (i = 0; i < len; i += blocklen) {
  3078. mg_write(conn, block, blocklen);
  3079. }
  3080. return 200;
  3081. }
  3082. START_TEST(test_large_file)
  3083. {
  3084. /* Server var */
  3085. struct mg_context *ctx;
  3086. struct mg_callbacks callbacks;
  3087. const char *OPTIONS[32];
  3088. int opt_cnt = 0;
  3089. #if !defined(NO_SSL)
  3090. const char *ssl_cert = locate_ssl_cert();
  3091. #endif
  3092. /* Client var */
  3093. struct mg_connection *client;
  3094. char client_err_buf[256];
  3095. char client_data_buf[256];
  3096. const struct mg_request_info *client_ri;
  3097. int64_t data_read;
  3098. int r;
  3099. /* Set options and start server */
  3100. #if !defined(NO_FILES)
  3101. OPTIONS[opt_cnt++] = "document_root";
  3102. OPTIONS[opt_cnt++] = ".";
  3103. #endif
  3104. #if defined(NO_SSL)
  3105. OPTIONS[opt_cnt++] = "listening_ports";
  3106. OPTIONS[opt_cnt++] = "8080";
  3107. #else
  3108. OPTIONS[opt_cnt++] = "listening_ports";
  3109. OPTIONS[opt_cnt++] = "8443s";
  3110. OPTIONS[opt_cnt++] = "ssl_certificate";
  3111. OPTIONS[opt_cnt++] = ssl_cert;
  3112. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3113. OPTIONS[opt_cnt++] = "4";
  3114. ck_assert(ssl_cert != NULL);
  3115. #endif
  3116. OPTIONS[opt_cnt] = NULL;
  3117. memset(&callbacks, 0, sizeof(callbacks));
  3118. callbacks.begin_request = test_large_file_begin_request;
  3119. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3120. ck_assert(ctx != NULL);
  3121. /* connect client */
  3122. memset(client_err_buf, 0, sizeof(client_err_buf));
  3123. memset(client_data_buf, 0, sizeof(client_data_buf));
  3124. client = mg_download("127.0.0.1",
  3125. #if defined(NO_SSL)
  3126. 8080,
  3127. 0,
  3128. #else
  3129. 8443,
  3130. 1,
  3131. #endif
  3132. client_err_buf,
  3133. sizeof(client_err_buf),
  3134. "GET /large.file HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3135. ck_assert(client != NULL);
  3136. ck_assert_str_eq(client_err_buf, "");
  3137. client_ri = mg_get_request_info(client);
  3138. ck_assert(client_ri != NULL);
  3139. ck_assert_str_eq(client_ri->uri, "200");
  3140. ck_assert_int_eq(client_ri->content_length, LARGE_FILE_SIZE);
  3141. data_read = 0;
  3142. while (data_read < client_ri->content_length) {
  3143. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3144. ck_assert_int_ge(r, 0);
  3145. data_read += r;
  3146. }
  3147. /* Nothing left to read */
  3148. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3149. ck_assert_int_eq(r, 0);
  3150. /* Close the client connection */
  3151. mg_close_connection(client);
  3152. /* Stop the server */
  3153. test_mg_stop(ctx);
  3154. }
  3155. END_TEST
  3156. #if !defined(NO_FILES)
  3157. #define FILE_IN_MEM_SIZE (1024 * 100)
  3158. static char *file_in_mem_data;
  3159. static const char *
  3160. test_file_in_memory_open_file(const struct mg_connection *conn,
  3161. const char *file_path,
  3162. size_t *file_size)
  3163. {
  3164. (void)conn;
  3165. if (strcmp(file_path, "./file_in_mem") == 0) {
  3166. /* File is in memory */
  3167. *file_size = FILE_IN_MEM_SIZE;
  3168. return file_in_mem_data;
  3169. } else {
  3170. /* File is not in memory */
  3171. return NULL;
  3172. }
  3173. }
  3174. #endif
  3175. START_TEST(test_file_in_memory)
  3176. {
  3177. #if !defined(NO_FILES)
  3178. /* Server var */
  3179. struct mg_context *ctx;
  3180. struct mg_callbacks callbacks;
  3181. const char *OPTIONS[32];
  3182. int opt_cnt = 0;
  3183. #if !defined(NO_SSL)
  3184. const char *ssl_cert = locate_ssl_cert();
  3185. #endif
  3186. /* Client var */
  3187. struct mg_connection *client;
  3188. char client_err_buf[256];
  3189. char client_data_buf[256];
  3190. const struct mg_request_info *client_ri;
  3191. int64_t data_read;
  3192. int r, i;
  3193. /* Prepare test data */
  3194. file_in_mem_data = (char *)malloc(FILE_IN_MEM_SIZE);
  3195. ck_assert_ptr_ne(file_in_mem_data, NULL);
  3196. for (r = 0; r < FILE_IN_MEM_SIZE; r++) {
  3197. file_in_mem_data[r] = (char)(r);
  3198. }
  3199. /* Set options and start server */
  3200. OPTIONS[opt_cnt++] = "document_root";
  3201. OPTIONS[opt_cnt++] = ".";
  3202. #if defined(NO_SSL)
  3203. OPTIONS[opt_cnt++] = "listening_ports";
  3204. OPTIONS[opt_cnt++] = "8080";
  3205. #else
  3206. OPTIONS[opt_cnt++] = "listening_ports";
  3207. OPTIONS[opt_cnt++] = "8443s";
  3208. OPTIONS[opt_cnt++] = "ssl_certificate";
  3209. OPTIONS[opt_cnt++] = ssl_cert;
  3210. ck_assert(ssl_cert != NULL);
  3211. #endif
  3212. OPTIONS[opt_cnt] = NULL;
  3213. memset(&callbacks, 0, sizeof(callbacks));
  3214. callbacks.open_file = test_file_in_memory_open_file;
  3215. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3216. ck_assert(ctx != NULL);
  3217. /* connect client */
  3218. memset(client_err_buf, 0, sizeof(client_err_buf));
  3219. memset(client_data_buf, 0, sizeof(client_data_buf));
  3220. client =
  3221. mg_download("127.0.0.1",
  3222. #if defined(NO_SSL)
  3223. 8080,
  3224. 0,
  3225. #else
  3226. 8443,
  3227. 1,
  3228. #endif
  3229. client_err_buf,
  3230. sizeof(client_err_buf),
  3231. "GET /file_in_mem HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3232. ck_assert(client != NULL);
  3233. ck_assert_str_eq(client_err_buf, "");
  3234. client_ri = mg_get_request_info(client);
  3235. ck_assert(client_ri != NULL);
  3236. ck_assert_str_eq(client_ri->uri, "200");
  3237. ck_assert_int_eq(client_ri->content_length, FILE_IN_MEM_SIZE);
  3238. data_read = 0;
  3239. while (data_read < client_ri->content_length) {
  3240. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3241. if (r > 0) {
  3242. for (i = 0; i < r; i++) {
  3243. ck_assert_int_eq((int)client_data_buf[i],
  3244. (int)file_in_mem_data[data_read + i]);
  3245. }
  3246. data_read += r;
  3247. }
  3248. }
  3249. /* Nothing left to read */
  3250. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3251. ck_assert_int_eq(r, 0);
  3252. /* Close the client connection */
  3253. mg_close_connection(client);
  3254. /* Stop the server */
  3255. test_mg_stop(ctx);
  3256. /* Free test data */
  3257. free(file_in_mem_data);
  3258. file_in_mem_data = NULL;
  3259. #else
  3260. /* This test is not meaningful, if NO_FILES is set */
  3261. ck_assert_uint_eq(mg_check_feature(1), 0);
  3262. #endif
  3263. }
  3264. END_TEST
  3265. static void
  3266. minimal_http_client_impl(const char *server, uint16_t port, const char *uri)
  3267. {
  3268. /* Client var */
  3269. struct mg_connection *client;
  3270. char client_err_buf[256];
  3271. char client_data_buf[256];
  3272. const struct mg_request_info *client_ri;
  3273. int64_t data_read;
  3274. int r;
  3275. client = mg_connect_client(
  3276. server, port, 0, client_err_buf, sizeof(client_err_buf));
  3277. ck_assert(client != NULL);
  3278. ck_assert_str_eq(client_err_buf, "");
  3279. mg_printf(client, "GET /%s HTTP/1.0\r\n\r\n", uri);
  3280. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3281. ck_assert_int_ge(r, 0);
  3282. ck_assert_str_eq(client_err_buf, "");
  3283. client_ri = mg_get_request_info(client);
  3284. ck_assert(client_ri != NULL);
  3285. /* e.g.: ck_assert_str_eq(client_ri->uri, "200"); */
  3286. r = (int)strlen(client_ri->uri);
  3287. ck_assert_int_eq(r, 3);
  3288. data_read = 0;
  3289. while (data_read < client_ri->content_length) {
  3290. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3291. if (r > 0) {
  3292. data_read += r;
  3293. }
  3294. }
  3295. /* Nothing left to read */
  3296. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3297. ck_assert_int_eq(r, 0);
  3298. mg_close_connection(client);
  3299. }
  3300. START_TEST(test_minimal_client)
  3301. {
  3302. /* Initialize the library */
  3303. mg_init_library(0);
  3304. /* Call a test client */
  3305. minimal_http_client_impl("192.30.253.113" /* www.github.com */,
  3306. 80,
  3307. "civetweb/civetweb/");
  3308. /* Un-initialize the library */
  3309. mg_exit_library();
  3310. }
  3311. END_TEST
  3312. static int
  3313. minimal_test_request_handler(struct mg_connection *conn, void *cbdata)
  3314. {
  3315. const char *msg = (const char *)cbdata;
  3316. unsigned long len = (unsigned long)strlen(msg);
  3317. mg_printf(conn,
  3318. "HTTP/1.1 200 OK\r\n"
  3319. "Content-Length: %lu\r\n"
  3320. "Content-Type: text/plain\r\n"
  3321. "Connection: close\r\n\r\n",
  3322. len);
  3323. mg_write(conn, msg, len);
  3324. return 200;
  3325. }
  3326. START_TEST(test_minimal_server_callback)
  3327. {
  3328. /* This test should ensure the minimum server example in
  3329. * docs/Embedding.md is still running. */
  3330. /* Server context handle */
  3331. struct mg_context *ctx;
  3332. /* Initialize the library */
  3333. mg_init_library(0);
  3334. /* Start the server */
  3335. ctx = test_mg_start(NULL, 0, NULL);
  3336. ck_assert(ctx != NULL);
  3337. /* Add some handler */
  3338. mg_set_request_handler(ctx,
  3339. "/hello",
  3340. minimal_test_request_handler,
  3341. (void *)"Hello world");
  3342. mg_set_request_handler(ctx,
  3343. "/8",
  3344. minimal_test_request_handler,
  3345. (void *)"Number eight");
  3346. /* Run the server for 15 seconds */
  3347. test_sleep(10);
  3348. /* Call a test client */
  3349. minimal_http_client_impl("127.0.0.1", 8080, "/hello");
  3350. /* Run the server for 15 seconds */
  3351. test_sleep(5);
  3352. /* Stop the server */
  3353. test_mg_stop(ctx);
  3354. /* Un-initialize the library */
  3355. mg_exit_library();
  3356. }
  3357. END_TEST
  3358. Suite *
  3359. make_public_server_suite(void)
  3360. {
  3361. Suite *const suite = suite_create("PublicServer");
  3362. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  3363. TCase *const tcase_initlib = tcase_create("Init library");
  3364. TCase *const tcase_startthreads = tcase_create("Start threads");
  3365. TCase *const tcase_minimal = tcase_create("Minimal");
  3366. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  3367. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  3368. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  3369. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  3370. TCase *const tcase_handle_form = tcase_create("Handle Form");
  3371. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  3372. TCase *const tcase_keep_alive = tcase_create("HTTP Keep Alive");
  3373. TCase *const tcase_error_handling = tcase_create("Error handling");
  3374. TCase *const tcase_throttle = tcase_create("Limit speed");
  3375. TCase *const tcase_large_file = tcase_create("Large file");
  3376. TCase *const tcase_file_in_mem = tcase_create("File in memory");
  3377. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  3378. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  3379. suite_add_tcase(suite, tcase_checktestenv);
  3380. tcase_add_test(tcase_initlib, test_init_library);
  3381. tcase_set_timeout(tcase_initlib, civetweb_min_test_timeout);
  3382. suite_add_tcase(suite, tcase_initlib);
  3383. tcase_add_test(tcase_startthreads, test_threading);
  3384. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  3385. suite_add_tcase(suite, tcase_startthreads);
  3386. tcase_add_test(tcase_minimal, test_minimal_client);
  3387. tcase_add_test(tcase_minimal, test_minimal_server_callback);
  3388. tcase_set_timeout(tcase_minimal, civetweb_min_test_timeout);
  3389. suite_add_tcase(suite, tcase_minimal);
  3390. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  3391. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  3392. suite_add_tcase(suite, tcase_startstophttp);
  3393. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  3394. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  3395. suite_add_tcase(suite, tcase_startstophttps);
  3396. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  3397. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  3398. suite_add_tcase(suite, tcase_serverandclienttls);
  3399. tcase_add_test(tcase_serverrequests, test_request_handlers);
  3400. tcase_set_timeout(tcase_serverrequests, 120);
  3401. suite_add_tcase(suite, tcase_serverrequests);
  3402. tcase_add_test(tcase_handle_form, test_handle_form);
  3403. tcase_set_timeout(tcase_handle_form, 300);
  3404. suite_add_tcase(suite, tcase_handle_form);
  3405. tcase_add_test(tcase_http_auth, test_http_auth);
  3406. tcase_set_timeout(tcase_http_auth, 60);
  3407. suite_add_tcase(suite, tcase_http_auth);
  3408. tcase_add_test(tcase_keep_alive, test_keep_alive);
  3409. tcase_set_timeout(tcase_keep_alive, 300);
  3410. suite_add_tcase(suite, tcase_keep_alive);
  3411. tcase_add_test(tcase_error_handling, test_error_handling);
  3412. tcase_add_test(tcase_error_handling, test_error_log_file);
  3413. tcase_set_timeout(tcase_error_handling, 300);
  3414. suite_add_tcase(suite, tcase_error_handling);
  3415. tcase_add_test(tcase_throttle, test_throttle);
  3416. tcase_set_timeout(tcase_throttle, 300);
  3417. suite_add_tcase(suite, tcase_throttle);
  3418. tcase_add_test(tcase_large_file, test_large_file);
  3419. tcase_set_timeout(tcase_large_file, 600);
  3420. suite_add_tcase(suite, tcase_large_file);
  3421. tcase_add_test(tcase_file_in_mem, test_file_in_memory);
  3422. tcase_set_timeout(tcase_file_in_mem, 300);
  3423. suite_add_tcase(suite, tcase_file_in_mem);
  3424. return suite;
  3425. }
  3426. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  3427. /* Used to debug test cases without using the check framework */
  3428. static int chk_ok = 0;
  3429. static int chk_failed = 0;
  3430. void
  3431. MAIN_PUBLIC_SERVER(void)
  3432. {
  3433. unsigned f_avail = mg_check_feature(0xFF);
  3434. unsigned f_ret = mg_init_library(f_avail);
  3435. ck_assert_uint_eq(f_ret, f_avail);
  3436. test_the_test_environment(0);
  3437. test_threading(0);
  3438. test_minimal_client(0);
  3439. test_minimal_server_callback(0);
  3440. test_mg_start_stop_http_server(0);
  3441. test_mg_start_stop_https_server(0);
  3442. test_request_handlers(0);
  3443. test_mg_server_and_client_tls(0);
  3444. test_handle_form(0);
  3445. test_http_auth(0);
  3446. test_keep_alive(0);
  3447. test_error_handling(0);
  3448. test_error_log_file(0);
  3449. test_throttle(0);
  3450. test_large_file(0);
  3451. test_file_in_memory(0);
  3452. mg_exit_library();
  3453. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  3454. }
  3455. void
  3456. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  3457. {
  3458. va_list va;
  3459. va_start(va, expr);
  3460. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  3461. vfprintf(stderr, expr, va);
  3462. fprintf(stderr, "\n\n");
  3463. va_end(va);
  3464. chk_failed++;
  3465. }
  3466. void
  3467. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  3468. {
  3469. va_list va;
  3470. if (cond) {
  3471. chk_ok++;
  3472. return;
  3473. }
  3474. va_start(va, expr);
  3475. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  3476. vfprintf(stderr, expr, va);
  3477. fprintf(stderr, "\n\n");
  3478. va_end(va);
  3479. chk_failed++;
  3480. }
  3481. void
  3482. _mark_point(const char *file, int line)
  3483. {
  3484. chk_ok++;
  3485. }
  3486. void
  3487. tcase_fn_start(const char *fname, const char *file, int line)
  3488. {
  3489. }
  3490. void suite_add_tcase(Suite *s, TCase *tc){};
  3491. void _tcase_add_test(TCase *tc,
  3492. TFun tf,
  3493. const char *fname,
  3494. int _signal,
  3495. int allowed_exit_value,
  3496. int start,
  3497. int end){};
  3498. TCase *
  3499. tcase_create(const char *name)
  3500. {
  3501. return NULL;
  3502. };
  3503. Suite *
  3504. suite_create(const char *name)
  3505. {
  3506. return NULL;
  3507. };
  3508. void tcase_set_timeout(TCase *tc, double timeout){};
  3509. #endif