public_server.c 134 KB

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