public_server.c 134 KB

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