public_server.c 129 KB

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