public_server.c 135 KB

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