public_server.c 135 KB

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