public_server.c 135 KB

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