public_server.c 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000
  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. MG_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. MG_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, MG_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, MG_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, MG_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. MG_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. MG_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 | MG_WEBSOCKET_OPCODE_BINARY))
  867. || (flags == (int)(128 | MG_WEBSOCKET_OPCODE_TEXT)));
  868. if (flags == (int)(128 | MG_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. MG_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. MG_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,
  1567. MG_WEBSOCKET_OPCODE_TEXT,
  1568. "bye",
  1569. 3);
  1570. wait_not_null(
  1571. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1572. ck_assert(ws_client1_data.closed == 0);
  1573. ck_assert(ws_client2_data.closed == 0);
  1574. ck_assert(ws_client2_data.data == NULL);
  1575. ck_assert(ws_client2_data.len == 0);
  1576. ck_assert(ws_client1_data.data != NULL);
  1577. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1578. ck_assert(!memcmp(ws_client1_data.data,
  1579. websocket_goodbye_msg,
  1580. websocket_goodbye_msg_len));
  1581. free(ws_client1_data.data);
  1582. ws_client1_data.data = NULL;
  1583. ws_client1_data.len = 0;
  1584. ck_assert(ws_client1_data.closed == 0); /* Not closed */
  1585. mg_close_connection(ws_client1_conn);
  1586. test_sleep(3); /* Won't get any message */
  1587. ck_assert(ws_client1_data.closed == 1); /* Closed */
  1588. ck_assert(ws_client2_data.closed == 0);
  1589. ck_assert(ws_client1_data.data == NULL);
  1590. ck_assert(ws_client1_data.len == 0);
  1591. ck_assert(ws_client2_data.data == NULL);
  1592. ck_assert(ws_client2_data.len == 0);
  1593. mg_websocket_client_write(ws_client2_conn,
  1594. MG_WEBSOCKET_OPCODE_TEXT,
  1595. "bye",
  1596. 3);
  1597. wait_not_null(
  1598. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1599. ck_assert(ws_client1_data.closed == 1);
  1600. ck_assert(ws_client2_data.closed == 0);
  1601. ck_assert(ws_client1_data.data == NULL);
  1602. ck_assert(ws_client1_data.len == 0);
  1603. ck_assert(ws_client2_data.data != NULL);
  1604. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1605. ck_assert(!memcmp(ws_client2_data.data,
  1606. websocket_goodbye_msg,
  1607. websocket_goodbye_msg_len));
  1608. free(ws_client2_data.data);
  1609. ws_client2_data.data = NULL;
  1610. ws_client2_data.len = 0;
  1611. mg_close_connection(ws_client2_conn);
  1612. test_sleep(3); /* Won't get any message */
  1613. ck_assert(ws_client1_data.closed == 1);
  1614. ck_assert(ws_client2_data.closed == 1);
  1615. ck_assert(ws_client1_data.data == NULL);
  1616. ck_assert(ws_client1_data.len == 0);
  1617. ck_assert(ws_client2_data.data == NULL);
  1618. ck_assert(ws_client2_data.len == 0);
  1619. /* Connect client 3 */
  1620. ws_client3_conn =
  1621. mg_connect_websocket_client("localhost",
  1622. #if defined(NO_SSL)
  1623. ipv4_port,
  1624. 0,
  1625. #else
  1626. ipv4s_port,
  1627. 1,
  1628. #endif
  1629. ebuf,
  1630. sizeof(ebuf),
  1631. "/websocket",
  1632. NULL,
  1633. websocket_client_data_handler,
  1634. websocket_client_close_handler,
  1635. &ws_client3_data);
  1636. ck_assert(ws_client3_conn != NULL);
  1637. wait_not_null(
  1638. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1639. ck_assert(ws_client1_data.closed == 1);
  1640. ck_assert(ws_client2_data.closed == 1);
  1641. ck_assert(ws_client3_data.closed == 0);
  1642. ck_assert(ws_client1_data.data == NULL);
  1643. ck_assert(ws_client1_data.len == 0);
  1644. ck_assert(ws_client2_data.data == NULL);
  1645. ck_assert(ws_client2_data.len == 0);
  1646. ck_assert(ws_client3_data.data != NULL);
  1647. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1648. ck_assert(!memcmp(ws_client3_data.data,
  1649. websocket_welcome_msg,
  1650. websocket_welcome_msg_len));
  1651. free(ws_client3_data.data);
  1652. ws_client3_data.data = NULL;
  1653. ws_client3_data.len = 0;
  1654. /* Write long data (16 bit size header) */
  1655. mg_websocket_client_write(ws_client3_conn,
  1656. MG_WEBSOCKET_OPCODE_BINARY,
  1657. long_ws_buf,
  1658. long_ws_buf_len_16);
  1659. /* Wait for the response */
  1660. wait_not_null(&(ws_client3_data.data));
  1661. ck_assert_int_eq((int)ws_client3_data.len, (int)long_ws_buf_len_16);
  1662. ck_assert(!memcmp(ws_client3_data.data, long_ws_buf, long_ws_buf_len_16));
  1663. free(ws_client3_data.data);
  1664. ws_client3_data.data = NULL;
  1665. ws_client3_data.len = 0;
  1666. /* Write long data (64 bit size header) */
  1667. mg_websocket_client_write(ws_client3_conn,
  1668. MG_WEBSOCKET_OPCODE_BINARY,
  1669. long_ws_buf,
  1670. long_ws_buf_len_64);
  1671. /* Wait for the response */
  1672. wait_not_null(&(ws_client3_data.data));
  1673. ck_assert_int_eq((int)ws_client3_data.len, (int)long_ws_buf_len_64);
  1674. ck_assert(!memcmp(ws_client3_data.data, long_ws_buf, long_ws_buf_len_64));
  1675. free(ws_client3_data.data);
  1676. ws_client3_data.data = NULL;
  1677. ws_client3_data.len = 0;
  1678. /* Disconnect client 3 */
  1679. ck_assert(ws_client3_data.closed == 0);
  1680. mg_close_connection(ws_client3_conn);
  1681. ck_assert(ws_client3_data.closed == 1);
  1682. /* Connect client 4 */
  1683. ws_client4_conn =
  1684. mg_connect_websocket_client("localhost",
  1685. #if defined(NO_SSL)
  1686. ipv4_port,
  1687. 0,
  1688. #else
  1689. ipv4s_port,
  1690. 1,
  1691. #endif
  1692. ebuf,
  1693. sizeof(ebuf),
  1694. "/websocket",
  1695. NULL,
  1696. websocket_client_data_handler,
  1697. websocket_client_close_handler,
  1698. &ws_client4_data);
  1699. ck_assert(ws_client4_conn != NULL);
  1700. wait_not_null(
  1701. &(ws_client4_data.data)); /* Wait for the websocket welcome message */
  1702. ck_assert(ws_client1_data.closed == 1);
  1703. ck_assert(ws_client2_data.closed == 1);
  1704. ck_assert(ws_client3_data.closed == 1);
  1705. ck_assert(ws_client4_data.closed == 0);
  1706. ck_assert(ws_client4_data.data != NULL);
  1707. ck_assert(ws_client4_data.len == websocket_welcome_msg_len);
  1708. ck_assert(!memcmp(ws_client4_data.data,
  1709. websocket_welcome_msg,
  1710. websocket_welcome_msg_len));
  1711. free(ws_client4_data.data);
  1712. ws_client4_data.data = NULL;
  1713. ws_client4_data.len = 0;
  1714. /* stop the server without closing this connection */
  1715. #endif
  1716. /* Close the server */
  1717. g_ctx = NULL;
  1718. test_mg_stop(ctx);
  1719. mark_point();
  1720. #ifdef USE_WEBSOCKET
  1721. for (i = 0; i < 100; i++) {
  1722. test_sleep(1);
  1723. if (ws_client3_data.closed != 0) {
  1724. mark_point();
  1725. break;
  1726. }
  1727. }
  1728. ck_assert_int_eq(ws_client4_data.closed, 1);
  1729. /* Free data in ws_client4_conn */
  1730. mg_close_connection(ws_client4_conn);
  1731. #endif
  1732. mark_point();
  1733. }
  1734. END_TEST
  1735. static int g_field_found_return = -999;
  1736. static int
  1737. field_found(const char *key,
  1738. const char *filename,
  1739. char *path,
  1740. size_t pathlen,
  1741. void *user_data)
  1742. {
  1743. ck_assert_ptr_ne(key, NULL);
  1744. ck_assert_ptr_ne(filename, NULL);
  1745. ck_assert_ptr_ne(path, NULL);
  1746. ck_assert_uint_gt(pathlen, 128);
  1747. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1748. ck_assert((g_field_found_return == MG_FORM_FIELD_STORAGE_GET)
  1749. || (g_field_found_return == MG_FORM_FIELD_STORAGE_STORE)
  1750. || (g_field_found_return == MG_FORM_FIELD_STORAGE_SKIP)
  1751. || (g_field_found_return == MG_FORM_FIELD_STORAGE_ABORT));
  1752. ck_assert_str_ne(key, "dontread");
  1753. if (!strcmp(key, "break_field_handler")) {
  1754. return MG_FORM_FIELD_STORAGE_ABORT;
  1755. }
  1756. if (!strcmp(key, "continue_field_handler")) {
  1757. return MG_FORM_FIELD_STORAGE_SKIP;
  1758. }
  1759. if (g_field_found_return == MG_FORM_FIELD_STORAGE_STORE) {
  1760. strncpy(path, key, pathlen - 8);
  1761. strcat(path, ".txt");
  1762. }
  1763. mark_point();
  1764. return g_field_found_return;
  1765. }
  1766. static int g_field_step;
  1767. static int
  1768. field_get(const char *key,
  1769. const char *value_untruncated,
  1770. size_t valuelen,
  1771. void *user_data)
  1772. {
  1773. /* Copy the untruncated value, so string compare functions can be used. */
  1774. /* The check unit test library does not have build in memcmp functions. */
  1775. char *value = (char *)malloc(valuelen + 1);
  1776. ck_assert(value != NULL);
  1777. memcpy(value, value_untruncated, valuelen);
  1778. value[valuelen] = 0;
  1779. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1780. ck_assert_int_ge(g_field_step, 0);
  1781. ++g_field_step;
  1782. switch (g_field_step) {
  1783. case 1:
  1784. ck_assert_str_eq(key, "textin");
  1785. ck_assert_uint_eq(valuelen, 4);
  1786. ck_assert_str_eq(value, "text");
  1787. break;
  1788. case 2:
  1789. ck_assert_str_eq(key, "passwordin");
  1790. ck_assert_uint_eq(valuelen, 0);
  1791. ck_assert_str_eq(value, "");
  1792. break;
  1793. case 3:
  1794. ck_assert_str_eq(key, "radio1");
  1795. ck_assert_uint_eq(valuelen, 4);
  1796. ck_assert_str_eq(value, "val1");
  1797. break;
  1798. case 4:
  1799. ck_assert_str_eq(key, "radio2");
  1800. ck_assert_uint_eq(valuelen, 4);
  1801. ck_assert_str_eq(value, "val1");
  1802. break;
  1803. case 5:
  1804. ck_assert_str_eq(key, "check1");
  1805. ck_assert_uint_eq(valuelen, 4);
  1806. ck_assert_str_eq(value, "val1");
  1807. break;
  1808. case 6:
  1809. ck_assert_str_eq(key, "numberin");
  1810. ck_assert_uint_eq(valuelen, 1);
  1811. ck_assert_str_eq(value, "1");
  1812. break;
  1813. case 7:
  1814. ck_assert_str_eq(key, "datein");
  1815. ck_assert_uint_eq(valuelen, 8);
  1816. ck_assert_str_eq(value, "1.1.2016");
  1817. break;
  1818. case 8:
  1819. ck_assert_str_eq(key, "colorin");
  1820. ck_assert_uint_eq(valuelen, 7);
  1821. ck_assert_str_eq(value, "#80ff00");
  1822. break;
  1823. case 9:
  1824. ck_assert_str_eq(key, "rangein");
  1825. ck_assert_uint_eq(valuelen, 1);
  1826. ck_assert_str_eq(value, "3");
  1827. break;
  1828. case 10:
  1829. ck_assert_str_eq(key, "monthin");
  1830. ck_assert_uint_eq(valuelen, 0);
  1831. ck_assert_str_eq(value, "");
  1832. break;
  1833. case 11:
  1834. ck_assert_str_eq(key, "weekin");
  1835. ck_assert_uint_eq(valuelen, 0);
  1836. ck_assert_str_eq(value, "");
  1837. break;
  1838. case 12:
  1839. ck_assert_str_eq(key, "timein");
  1840. ck_assert_uint_eq(valuelen, 0);
  1841. ck_assert_str_eq(value, "");
  1842. break;
  1843. case 13:
  1844. ck_assert_str_eq(key, "datetimen");
  1845. ck_assert_uint_eq(valuelen, 0);
  1846. ck_assert_str_eq(value, "");
  1847. break;
  1848. case 14:
  1849. ck_assert_str_eq(key, "datetimelocalin");
  1850. ck_assert_uint_eq(valuelen, 0);
  1851. ck_assert_str_eq(value, "");
  1852. break;
  1853. case 15:
  1854. ck_assert_str_eq(key, "emailin");
  1855. ck_assert_uint_eq(valuelen, 0);
  1856. ck_assert_str_eq(value, "");
  1857. break;
  1858. case 16:
  1859. ck_assert_str_eq(key, "searchin");
  1860. ck_assert_uint_eq(valuelen, 0);
  1861. ck_assert_str_eq(value, "");
  1862. break;
  1863. case 17:
  1864. ck_assert_str_eq(key, "telin");
  1865. ck_assert_uint_eq(valuelen, 0);
  1866. ck_assert_str_eq(value, "");
  1867. break;
  1868. case 18:
  1869. ck_assert_str_eq(key, "urlin");
  1870. ck_assert_uint_eq(valuelen, 0);
  1871. ck_assert_str_eq(value, "");
  1872. break;
  1873. case 19:
  1874. ck_assert_str_eq(key, "filein");
  1875. ck_assert_uint_eq(valuelen, 0);
  1876. ck_assert_str_eq(value, "");
  1877. break;
  1878. case 20:
  1879. ck_assert_str_eq(key, "filesin");
  1880. ck_assert_uint_eq(valuelen, 0);
  1881. ck_assert_str_eq(value, "");
  1882. break;
  1883. case 21:
  1884. ck_assert_str_eq(key, "selectin");
  1885. ck_assert_uint_eq(valuelen, 4);
  1886. ck_assert_str_eq(value, "opt1");
  1887. break;
  1888. case 22:
  1889. ck_assert_str_eq(key, "message");
  1890. ck_assert_uint_eq(valuelen, 23);
  1891. ck_assert_str_eq(value, "Text area default text.");
  1892. break;
  1893. default:
  1894. ck_abort_msg("field_get called with g_field_step == %i",
  1895. (int)g_field_step);
  1896. }
  1897. free(value);
  1898. mark_point();
  1899. return 0;
  1900. }
  1901. static const char *myfile_content = "Content of myfile.txt\r\n";
  1902. static const int myfile_content_rep = 500;
  1903. static int
  1904. field_store(const char *path, long long file_size, void *user_data)
  1905. {
  1906. FILE *f;
  1907. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1908. ck_assert_int_ge(g_field_step, 100);
  1909. ++g_field_step;
  1910. switch (g_field_step) {
  1911. case 101:
  1912. ck_assert_str_eq(path, "storeme.txt");
  1913. ck_assert_int_eq(file_size, 9);
  1914. f = fopen(path, "r");
  1915. ck_assert_ptr_ne(f, NULL);
  1916. if (f) {
  1917. char buf[32] = {0};
  1918. int i = (int)fread(buf, 1, 31, f);
  1919. ck_assert_int_eq(i, 9);
  1920. fclose(f);
  1921. ck_assert_str_eq(buf, "storetest");
  1922. }
  1923. break;
  1924. case 102:
  1925. ck_assert_str_eq(path, "file2store.txt");
  1926. ck_assert_uint_eq(23, strlen(myfile_content));
  1927. ck_assert_int_eq(file_size, 23 * myfile_content_rep);
  1928. #ifdef _WIN32
  1929. f = fopen(path, "rb");
  1930. #else
  1931. f = fopen(path, "r");
  1932. #endif
  1933. ck_assert_ptr_ne(f, NULL);
  1934. if (f) {
  1935. char buf[32] = {0};
  1936. int r, i;
  1937. for (r = 0; r < myfile_content_rep; r++) {
  1938. i = (int)fread(buf, 1, 23, f);
  1939. ck_assert_int_eq(i, 23);
  1940. ck_assert_str_eq(buf, myfile_content);
  1941. }
  1942. i = (int)fread(buf, 1, 23, f);
  1943. ck_assert_int_eq(i, 0);
  1944. fclose(f);
  1945. }
  1946. break;
  1947. default:
  1948. ck_abort_msg("field_get called with g_field_step == %i",
  1949. (int)g_field_step);
  1950. }
  1951. mark_point();
  1952. return 0;
  1953. }
  1954. static int
  1955. FormGet(struct mg_connection *conn, void *cbdata)
  1956. {
  1957. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1958. int ret;
  1959. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  1960. (void)cbdata;
  1961. ck_assert(req_info != NULL);
  1962. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1963. fdh.user_data = (void *)&g_field_found_return;
  1964. /* Call the form handler */
  1965. g_field_step = 0;
  1966. g_field_found_return = MG_FORM_FIELD_STORAGE_GET;
  1967. ret = mg_handle_form_request(conn, &fdh);
  1968. g_field_found_return = -888;
  1969. ck_assert_int_eq(ret, 22);
  1970. ck_assert_int_eq(g_field_step, 22);
  1971. mg_printf(conn, "%i\r\n", ret);
  1972. g_field_step = 1000;
  1973. mark_point();
  1974. return 1;
  1975. }
  1976. static int
  1977. FormStore(struct mg_connection *conn,
  1978. void *cbdata,
  1979. int ret_expected,
  1980. int field_step_expected)
  1981. {
  1982. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1983. int ret;
  1984. struct mg_form_data_handler fdh = {field_found,
  1985. field_get,
  1986. field_store,
  1987. NULL};
  1988. (void)cbdata;
  1989. ck_assert(req_info != NULL);
  1990. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1991. fdh.user_data = (void *)&g_field_found_return;
  1992. /* Call the form handler */
  1993. g_field_step = 100;
  1994. g_field_found_return = MG_FORM_FIELD_STORAGE_STORE;
  1995. ret = mg_handle_form_request(conn, &fdh);
  1996. ck_assert_int_eq(ret, ret_expected);
  1997. ck_assert_int_eq(g_field_step, field_step_expected);
  1998. mg_printf(conn, "%i\r\n", ret);
  1999. g_field_step = 1000;
  2000. mark_point();
  2001. return 1;
  2002. }
  2003. static int
  2004. FormStore1(struct mg_connection *conn, void *cbdata)
  2005. {
  2006. mark_point();
  2007. return FormStore(conn, cbdata, 3, 101);
  2008. }
  2009. static int
  2010. FormStore2(struct mg_connection *conn, void *cbdata)
  2011. {
  2012. mark_point();
  2013. return FormStore(conn, cbdata, 4, 102);
  2014. }
  2015. static void
  2016. send_chunk_stringl(struct mg_connection *conn,
  2017. const char *chunk,
  2018. unsigned int chunk_len)
  2019. {
  2020. char lenbuf[16];
  2021. size_t lenbuf_len;
  2022. int ret;
  2023. mark_point();
  2024. /* First store the length information in a text buffer. */
  2025. sprintf(lenbuf, "%x\r\n", chunk_len);
  2026. lenbuf_len = strlen(lenbuf);
  2027. /* Then send length information, chunk and terminating \r\n. */
  2028. ret = mg_write(conn, lenbuf, lenbuf_len);
  2029. ck_assert_int_eq(ret, (int)lenbuf_len);
  2030. ret = mg_write(conn, chunk, chunk_len);
  2031. ck_assert_int_eq(ret, (int)chunk_len);
  2032. ret = mg_write(conn, "\r\n", 2);
  2033. ck_assert_int_eq(ret, 2);
  2034. }
  2035. static void
  2036. send_chunk_string(struct mg_connection *conn, const char *chunk)
  2037. {
  2038. mark_point();
  2039. send_chunk_stringl(conn, chunk, (unsigned int)strlen(chunk));
  2040. mark_point();
  2041. }
  2042. START_TEST(test_handle_form)
  2043. {
  2044. struct mg_context *ctx;
  2045. struct mg_connection *client_conn;
  2046. const struct mg_request_info *ri;
  2047. const char *OPTIONS[8];
  2048. const char *opt;
  2049. int opt_idx = 0;
  2050. char ebuf[100];
  2051. const char *multipart_body;
  2052. const char *boundary;
  2053. size_t body_len, body_sent, chunk_len;
  2054. int sleep_cnt;
  2055. mark_point();
  2056. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  2057. OPTIONS[opt_idx++] = "listening_ports";
  2058. OPTIONS[opt_idx++] = "8884";
  2059. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  2060. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  2061. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  2062. ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
  2063. ck_assert(ctx != NULL);
  2064. g_ctx = ctx;
  2065. opt = mg_get_option(ctx, "listening_ports");
  2066. ck_assert_str_eq(opt, "8884");
  2067. mg_set_request_handler(ctx, "/handle_form", FormGet, NULL);
  2068. mg_set_request_handler(ctx, "/handle_form_store", FormStore1, NULL);
  2069. mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, NULL);
  2070. test_sleep(1);
  2071. /* Handle form: "GET" */
  2072. client_conn = mg_download("localhost",
  2073. 8884,
  2074. 0,
  2075. ebuf,
  2076. sizeof(ebuf),
  2077. "%s",
  2078. "GET /handle_form"
  2079. "?textin=text&passwordin=&radio1=val1"
  2080. "&radio2=val1&check1=val1&numberin=1"
  2081. "&datein=1.1.2016&colorin=%2380ff00"
  2082. "&rangein=3&monthin=&weekin=&timein="
  2083. "&datetimen=&datetimelocalin=&emailin="
  2084. "&searchin=&telin=&urlin=&filein="
  2085. "&filesin=&selectin=opt1"
  2086. "&message=Text+area+default+text. "
  2087. "HTTP/1.0\r\n"
  2088. "Host: localhost:8884\r\n"
  2089. "Connection: close\r\n\r\n");
  2090. ck_assert(client_conn != NULL);
  2091. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2092. test_sleep(1);
  2093. if (g_field_step == 1000) {
  2094. break;
  2095. }
  2096. }
  2097. ri = mg_get_request_info(client_conn);
  2098. ck_assert(ri != NULL);
  2099. ck_assert_str_eq(ri->local_uri, "200");
  2100. mg_close_connection(client_conn);
  2101. /* Handle form: "POST x-www-form-urlencoded" */
  2102. client_conn =
  2103. mg_download("localhost",
  2104. 8884,
  2105. 0,
  2106. ebuf,
  2107. sizeof(ebuf),
  2108. "%s",
  2109. "POST /handle_form HTTP/1.1\r\n"
  2110. "Host: localhost:8884\r\n"
  2111. "Connection: close\r\n"
  2112. "Content-Type: application/x-www-form-urlencoded\r\n"
  2113. "Content-Length: 263\r\n"
  2114. "\r\n"
  2115. "textin=text&passwordin=&radio1=val1&radio2=val1"
  2116. "&check1=val1&numberin=1&datein=1.1.2016"
  2117. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  2118. "&timein=&datetimen=&datetimelocalin=&emailin="
  2119. "&searchin=&telin=&urlin=&filein=&filesin="
  2120. "&selectin=opt1&message=Text+area+default+text.");
  2121. ck_assert(client_conn != NULL);
  2122. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2123. test_sleep(1);
  2124. if (g_field_step == 1000) {
  2125. break;
  2126. }
  2127. }
  2128. ri = mg_get_request_info(client_conn);
  2129. ck_assert(ri != NULL);
  2130. ck_assert_str_eq(ri->local_uri, "200");
  2131. mg_close_connection(client_conn);
  2132. /* Handle form: "POST multipart/form-data" */
  2133. multipart_body =
  2134. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2135. "Content-Disposition: form-data; name=\"textin\"\r\n"
  2136. "\r\n"
  2137. "text\r\n"
  2138. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2139. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  2140. "\r\n"
  2141. "\r\n"
  2142. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2143. "Content-Disposition: form-data; name=\"radio1\"\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=radio2\r\n"
  2148. "\r\n"
  2149. "val1\r\n"
  2150. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2151. "Content-Disposition: form-data; name=\"check1\"\r\n"
  2152. "\r\n"
  2153. "val1\r\n"
  2154. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2155. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  2156. "\r\n"
  2157. "1\r\n"
  2158. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2159. "Content-Disposition: form-data; name=\"datein\"\r\n"
  2160. "\r\n"
  2161. "1.1.2016\r\n"
  2162. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2163. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  2164. "\r\n"
  2165. "#80ff00\r\n"
  2166. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2167. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  2168. "\r\n"
  2169. "3\r\n"
  2170. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2171. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  2172. "\r\n"
  2173. "\r\n"
  2174. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2175. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  2176. "\r\n"
  2177. "\r\n"
  2178. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2179. "Content-Disposition: form-data; name=\"timein\"\r\n"
  2180. "\r\n"
  2181. "\r\n"
  2182. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2183. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  2184. "\r\n"
  2185. "\r\n"
  2186. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2187. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  2188. "\r\n"
  2189. "\r\n"
  2190. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2191. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  2192. "\r\n"
  2193. "\r\n"
  2194. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2195. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  2196. "\r\n"
  2197. "\r\n"
  2198. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2199. "Content-Disposition: form-data; name=\"telin\"\r\n"
  2200. "\r\n"
  2201. "\r\n"
  2202. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2203. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  2204. "\r\n"
  2205. "\r\n"
  2206. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2207. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  2208. "Content-Type: application/octet-stream\r\n"
  2209. "\r\n"
  2210. "\r\n"
  2211. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2212. "Content-Disposition: form-data; name=filesin; filename=\r\n"
  2213. "Content-Type: application/octet-stream\r\n"
  2214. "\r\n"
  2215. "\r\n"
  2216. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2217. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  2218. "\r\n"
  2219. "opt1\r\n"
  2220. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2221. "Content-Disposition: form-data; name=\"message\"\r\n"
  2222. "\r\n"
  2223. "Text area default text.\r\n"
  2224. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  2225. body_len = strlen(multipart_body);
  2226. ck_assert_uint_eq(body_len, 2368); /* not required */
  2227. client_conn =
  2228. mg_download("localhost",
  2229. 8884,
  2230. 0,
  2231. ebuf,
  2232. sizeof(ebuf),
  2233. "POST /handle_form HTTP/1.1\r\n"
  2234. "Host: localhost:8884\r\n"
  2235. "Connection: close\r\n"
  2236. "Content-Type: multipart/form-data; "
  2237. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2238. "Content-Length: %u\r\n"
  2239. "\r\n%s",
  2240. (unsigned int)body_len,
  2241. multipart_body);
  2242. ck_assert(client_conn != NULL);
  2243. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2244. test_sleep(1);
  2245. if (g_field_step == 1000) {
  2246. break;
  2247. }
  2248. }
  2249. ri = mg_get_request_info(client_conn);
  2250. ck_assert(ri != NULL);
  2251. ck_assert_str_eq(ri->local_uri, "200");
  2252. mg_close_connection(client_conn);
  2253. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  2254. client_conn =
  2255. mg_download("localhost",
  2256. 8884,
  2257. 0,
  2258. ebuf,
  2259. sizeof(ebuf),
  2260. "%s",
  2261. "POST /handle_form HTTP/1.1\r\n"
  2262. "Host: localhost:8884\r\n"
  2263. "Connection: close\r\n"
  2264. "Content-Type: multipart/form-data; "
  2265. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2266. "Transfer-Encoding: chunked\r\n"
  2267. "\r\n");
  2268. ck_assert(client_conn != NULL);
  2269. body_len = strlen(multipart_body);
  2270. chunk_len = 1;
  2271. body_sent = 0;
  2272. while (body_len > body_sent) {
  2273. if (chunk_len > (body_len - body_sent)) {
  2274. chunk_len = body_len - body_sent;
  2275. }
  2276. ck_assert_int_gt((int)chunk_len, 0);
  2277. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2278. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2279. mg_printf(client_conn, "\r\n");
  2280. body_sent += chunk_len;
  2281. chunk_len = (chunk_len % 40) + 1;
  2282. }
  2283. mg_printf(client_conn, "0\r\n");
  2284. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2285. test_sleep(1);
  2286. if (g_field_step == 1000) {
  2287. break;
  2288. }
  2289. }
  2290. ri = mg_get_request_info(client_conn);
  2291. ck_assert(ri != NULL);
  2292. ck_assert_str_eq(ri->local_uri, "200");
  2293. mg_close_connection(client_conn);
  2294. /* Handle form: "POST multipart/form-data" with chunked transfer
  2295. * encoding, using a quoted boundary string */
  2296. client_conn = mg_download(
  2297. "localhost",
  2298. 8884,
  2299. 0,
  2300. ebuf,
  2301. sizeof(ebuf),
  2302. "%s",
  2303. "POST /handle_form HTTP/1.1\r\n"
  2304. "Host: localhost:8884\r\n"
  2305. "Connection: close\r\n"
  2306. "Content-Type: multipart/form-data; "
  2307. "boundary=\"multipart-form-data-boundary--see-RFC-2388\"\r\n"
  2308. "Transfer-Encoding: chunked\r\n"
  2309. "\r\n");
  2310. ck_assert(client_conn != NULL);
  2311. body_len = strlen(multipart_body);
  2312. chunk_len = 1;
  2313. body_sent = 0;
  2314. while (body_len > body_sent) {
  2315. if (chunk_len > (body_len - body_sent)) {
  2316. chunk_len = body_len - body_sent;
  2317. }
  2318. ck_assert_int_gt((int)chunk_len, 0);
  2319. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2320. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2321. mg_printf(client_conn, "\r\n");
  2322. body_sent += chunk_len;
  2323. chunk_len = (chunk_len % 40) + 1;
  2324. }
  2325. mg_printf(client_conn, "0\r\n");
  2326. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2327. test_sleep(1);
  2328. if (g_field_step == 1000) {
  2329. break;
  2330. }
  2331. }
  2332. ri = mg_get_request_info(client_conn);
  2333. ck_assert(ri != NULL);
  2334. ck_assert_str_eq(ri->local_uri, "200");
  2335. mg_close_connection(client_conn);
  2336. /* Now test form_store */
  2337. /* First test with GET */
  2338. client_conn = mg_download("localhost",
  2339. 8884,
  2340. 0,
  2341. ebuf,
  2342. sizeof(ebuf),
  2343. "%s",
  2344. "GET /handle_form_store"
  2345. "?storeme=storetest"
  2346. "&continue_field_handler=ignore"
  2347. "&break_field_handler=abort"
  2348. "&dontread=xyz "
  2349. "HTTP/1.0\r\n"
  2350. "Host: localhost:8884\r\n"
  2351. "Connection: close\r\n\r\n");
  2352. ck_assert(client_conn != NULL);
  2353. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2354. test_sleep(1);
  2355. if (g_field_step == 1000) {
  2356. break;
  2357. }
  2358. }
  2359. ri = mg_get_request_info(client_conn);
  2360. ck_assert(ri != NULL);
  2361. ck_assert_str_eq(ri->local_uri, "200");
  2362. mg_close_connection(client_conn);
  2363. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  2364. client_conn =
  2365. mg_download("localhost",
  2366. 8884,
  2367. 0,
  2368. ebuf,
  2369. sizeof(ebuf),
  2370. "%s",
  2371. "POST /handle_form_store HTTP/1.0\r\n"
  2372. "Host: localhost:8884\r\n"
  2373. "Connection: close\r\n"
  2374. "Content-Type: application/x-www-form-urlencoded\r\n"
  2375. "Transfer-Encoding: chunked\r\n"
  2376. "\r\n");
  2377. ck_assert(client_conn != NULL);
  2378. send_chunk_string(client_conn, "storeme=store");
  2379. send_chunk_string(client_conn, "test&");
  2380. send_chunk_string(client_conn, "continue_field_handler=ignore");
  2381. send_chunk_string(client_conn, "&br");
  2382. test_sleep(1);
  2383. send_chunk_string(client_conn, "eak_field_handler=abort&");
  2384. send_chunk_string(client_conn, "dontread=xyz");
  2385. mg_printf(client_conn, "0\r\n");
  2386. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2387. test_sleep(1);
  2388. if (g_field_step == 1000) {
  2389. break;
  2390. }
  2391. }
  2392. ri = mg_get_request_info(client_conn);
  2393. ck_assert(ri != NULL);
  2394. ck_assert_str_eq(ri->local_uri, "200");
  2395. mg_close_connection(client_conn);
  2396. /* Handle form: "POST multipart/form-data", chunked, store */
  2397. client_conn =
  2398. mg_download("localhost",
  2399. 8884,
  2400. 0,
  2401. ebuf,
  2402. sizeof(ebuf),
  2403. "%s",
  2404. "POST /handle_form_store HTTP/1.0\r\n"
  2405. "Host: localhost:8884\r\n"
  2406. "Connection: close\r\n"
  2407. "Content-Type: multipart/form-data; "
  2408. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2409. "Transfer-Encoding: chunked\r\n"
  2410. "\r\n");
  2411. ck_assert(client_conn != NULL);
  2412. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2413. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  2414. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2415. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2416. send_chunk_string(client_conn, "\r\n");
  2417. send_chunk_string(client_conn, "storetest\r\n");
  2418. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2419. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2420. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2421. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  2422. send_chunk_string(client_conn, "\r\n");
  2423. send_chunk_string(client_conn, "ignore\r\n");
  2424. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2425. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2426. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2427. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2428. send_chunk_string(client_conn, "\r\n");
  2429. send_chunk_string(client_conn, "abort\r\n");
  2430. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2431. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2432. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2433. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2434. send_chunk_string(client_conn, "\r\n");
  2435. send_chunk_string(client_conn, "xyz\r\n");
  2436. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2437. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2438. mg_printf(client_conn, "0\r\n");
  2439. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2440. test_sleep(1);
  2441. if (g_field_step == 1000) {
  2442. break;
  2443. }
  2444. }
  2445. ri = mg_get_request_info(client_conn);
  2446. ck_assert(ri != NULL);
  2447. ck_assert_str_eq(ri->local_uri, "200");
  2448. mg_close_connection(client_conn);
  2449. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  2450. client_conn =
  2451. mg_download("localhost",
  2452. 8884,
  2453. 0,
  2454. ebuf,
  2455. sizeof(ebuf),
  2456. "%s",
  2457. "POST /handle_form_store2 HTTP/1.0\r\n"
  2458. "Host: localhost:8884\r\n"
  2459. "Connection: close\r\n"
  2460. "Content-Type: multipart/form-data; "
  2461. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2462. "Transfer-Encoding: chunked\r\n"
  2463. "\r\n");
  2464. ck_assert(client_conn != NULL);
  2465. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  2466. send_chunk_string(client_conn, boundary);
  2467. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2468. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2469. send_chunk_string(client_conn, "\r\n");
  2470. send_chunk_string(client_conn, "storetest\r\n");
  2471. send_chunk_string(client_conn, boundary);
  2472. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2473. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  2474. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  2475. send_chunk_string(client_conn, "Content-Type: ");
  2476. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2477. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2478. send_chunk_string(client_conn, "\r\n");
  2479. /* send some kilobyte of data */
  2480. /* sending megabytes to localhost does not allways work in CI test
  2481. * environments (depending on the network stack) */
  2482. body_sent = 0;
  2483. do {
  2484. send_chunk_string(client_conn, "ignore\r\n");
  2485. body_sent += 8;
  2486. /* send some strings that are almost boundaries */
  2487. for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
  2488. /* chunks from 1 byte to strlen(boundary)-1 */
  2489. send_chunk_stringl(client_conn, boundary, (unsigned int)chunk_len);
  2490. body_sent += chunk_len;
  2491. }
  2492. } while (body_sent < 8 * 1024);
  2493. send_chunk_string(client_conn, "\r\n");
  2494. send_chunk_string(client_conn, boundary);
  2495. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2496. send_chunk_string(client_conn, "name=\"file2store\";");
  2497. send_chunk_string(client_conn, "filename=\"myfile.txt\"\r\n");
  2498. send_chunk_string(client_conn, "Content-Type: ");
  2499. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2500. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2501. send_chunk_string(client_conn, "\r\n");
  2502. for (body_sent = 0; (int)body_sent < (int)myfile_content_rep; body_sent++) {
  2503. send_chunk_string(client_conn, myfile_content);
  2504. }
  2505. send_chunk_string(client_conn, "\r\n");
  2506. send_chunk_string(client_conn, boundary);
  2507. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2508. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2509. send_chunk_string(client_conn, "\r\n");
  2510. send_chunk_string(client_conn, "abort\r\n");
  2511. send_chunk_string(client_conn, boundary);
  2512. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2513. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2514. send_chunk_string(client_conn, "\r\n");
  2515. send_chunk_string(client_conn, "xyz\r\n");
  2516. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2517. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2518. mg_printf(client_conn, "0\r\n");
  2519. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2520. test_sleep(1);
  2521. if (g_field_step == 1000) {
  2522. break;
  2523. }
  2524. }
  2525. ri = mg_get_request_info(client_conn);
  2526. ck_assert(ri != NULL);
  2527. ck_assert_str_eq(ri->local_uri, "200");
  2528. mg_close_connection(client_conn);
  2529. /* Close the server */
  2530. g_ctx = NULL;
  2531. test_mg_stop(ctx);
  2532. mark_point();
  2533. }
  2534. END_TEST
  2535. START_TEST(test_http_auth)
  2536. {
  2537. #if !defined(NO_FILES)
  2538. const char *OPTIONS[] = {
  2539. "document_root",
  2540. ".",
  2541. "listening_ports",
  2542. "8080",
  2543. #if !defined(NO_CACHING)
  2544. "static_file_max_age",
  2545. "0",
  2546. #endif
  2547. "put_delete_auth_file",
  2548. "put_delete_auth_file.csv",
  2549. NULL,
  2550. };
  2551. struct mg_context *ctx;
  2552. struct mg_connection *client_conn;
  2553. char client_err[256], nonce[256];
  2554. const struct mg_request_info *client_ri;
  2555. int client_res;
  2556. FILE *f;
  2557. const char *passwd_file = ".htpasswd";
  2558. const char *test_file = "test_http_auth.test_file.txt";
  2559. const char *test_content = "test_http_auth test_file content";
  2560. const char *domain;
  2561. const char *doc_root;
  2562. const char *auth_request;
  2563. const char *str;
  2564. size_t len;
  2565. int i;
  2566. char HA1[256], HA2[256], HA[256];
  2567. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2568. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2569. const char *nc = "00000001";
  2570. const char *cnonce = "6789ABCD";
  2571. mark_point();
  2572. /* Start with default options */
  2573. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2574. ck_assert(ctx != NULL);
  2575. domain = mg_get_option(ctx, "authentication_domain");
  2576. ck_assert(domain != NULL);
  2577. len = strlen(domain);
  2578. ck_assert_uint_gt(len, 0);
  2579. ck_assert_uint_lt(len, 64);
  2580. doc_root = mg_get_option(ctx, "document_root");
  2581. ck_assert_str_eq(doc_root, ".");
  2582. /* Create a default file in the document root */
  2583. f = fopen(test_file, "w");
  2584. if (f) {
  2585. fprintf(f, "%s", test_content);
  2586. fclose(f);
  2587. } else {
  2588. ck_abort_msg("Cannot create file %s", test_file);
  2589. }
  2590. (void)remove(passwd_file);
  2591. (void)remove("put_delete_auth_file.csv");
  2592. client_res = mg_modify_passwords_file("put_delete_auth_file.csv",
  2593. domain,
  2594. "admin",
  2595. "adminpass");
  2596. ck_assert_int_eq(client_res, 1);
  2597. /* Read file before a .htpasswd file has been created */
  2598. memset(client_err, 0, sizeof(client_err));
  2599. client_conn =
  2600. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2601. ck_assert_str_eq(client_err, "");
  2602. ck_assert(client_conn != NULL);
  2603. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2604. client_res =
  2605. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2606. ck_assert_int_ge(client_res, 0);
  2607. ck_assert_str_eq(client_err, "");
  2608. client_ri = mg_get_request_info(client_conn);
  2609. ck_assert(client_ri != NULL);
  2610. ck_assert_str_eq(client_ri->local_uri, "200");
  2611. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2612. ck_assert_int_gt(client_res, 0);
  2613. ck_assert_int_le(client_res, sizeof(client_err));
  2614. ck_assert_str_eq(client_err, test_content);
  2615. mg_close_connection(client_conn);
  2616. test_sleep(1);
  2617. /* Create a .htpasswd file */
  2618. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2619. ck_assert_int_eq(client_res, 1);
  2620. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2621. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2622. test_sleep(1);
  2623. /* Repeat test after .htpasswd is created */
  2624. memset(client_err, 0, sizeof(client_err));
  2625. client_conn =
  2626. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2627. ck_assert_str_eq(client_err, "");
  2628. ck_assert(client_conn != NULL);
  2629. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2630. client_res =
  2631. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2632. ck_assert_int_ge(client_res, 0);
  2633. ck_assert_str_eq(client_err, "");
  2634. client_ri = mg_get_request_info(client_conn);
  2635. ck_assert(client_ri != NULL);
  2636. ck_assert_str_eq(client_ri->local_uri, "401");
  2637. auth_request = NULL;
  2638. for (i = 0; i < client_ri->num_headers; i++) {
  2639. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2640. "WWW-Authenticate")) {
  2641. ck_assert_ptr_eq(auth_request, NULL);
  2642. auth_request = client_ri->http_headers[i].value;
  2643. ck_assert_ptr_ne(auth_request, NULL);
  2644. }
  2645. }
  2646. ck_assert_ptr_ne(auth_request, NULL);
  2647. str = "Digest qop=\"auth\", realm=\"";
  2648. len = strlen(str);
  2649. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2650. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2651. len += strlen(domain);
  2652. str = "\", nonce=\"";
  2653. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2654. len += strlen(str);
  2655. str = strchr(auth_request + len, '\"');
  2656. ck_assert_ptr_ne(str, NULL);
  2657. ck_assert_ptr_ne(str, auth_request + len);
  2658. /* nonce is from including (auth_request + len) to excluding (str) */
  2659. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2660. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2661. (ptrdiff_t)sizeof(nonce));
  2662. memset(nonce, 0, sizeof(nonce));
  2663. memcpy(nonce,
  2664. auth_request + len,
  2665. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2666. memset(HA1, 0, sizeof(HA1));
  2667. memset(HA2, 0, sizeof(HA2));
  2668. memset(HA, 0, sizeof(HA));
  2669. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2670. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2671. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2672. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2673. sprintf(HA2, "%s:/%s", "GET", test_file);
  2674. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2675. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2676. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2677. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2678. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2679. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2680. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2681. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2682. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2683. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2684. HA_md5_ret = mg_md5(HA_md5_buf,
  2685. HA1_md5_buf,
  2686. ":",
  2687. nonce,
  2688. ":",
  2689. nc,
  2690. ":",
  2691. cnonce,
  2692. ":",
  2693. "auth",
  2694. ":",
  2695. HA2_md5_buf,
  2696. NULL);
  2697. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2698. mg_close_connection(client_conn);
  2699. /* Retry with authorization */
  2700. memset(client_err, 0, sizeof(client_err));
  2701. client_conn =
  2702. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2703. ck_assert_str_eq(client_err, "");
  2704. ck_assert(client_conn != NULL);
  2705. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2706. mg_printf(client_conn,
  2707. "Authorization: Digest "
  2708. "username=\"%s\", "
  2709. "realm=\"%s\", "
  2710. "nonce=\"%s\", "
  2711. "uri=\"/%s\", "
  2712. "qop=auth, "
  2713. "nc=%s, "
  2714. "cnonce=\"%s\", "
  2715. "response=\"%s\"\r\n\r\n",
  2716. "user",
  2717. domain,
  2718. nonce,
  2719. test_file,
  2720. nc,
  2721. cnonce,
  2722. HA_md5_buf);
  2723. client_res =
  2724. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2725. ck_assert_int_ge(client_res, 0);
  2726. ck_assert_str_eq(client_err, "");
  2727. client_ri = mg_get_request_info(client_conn);
  2728. ck_assert(client_ri != NULL);
  2729. ck_assert_str_eq(client_ri->local_uri, "200");
  2730. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2731. ck_assert_int_gt(client_res, 0);
  2732. ck_assert_int_le(client_res, sizeof(client_err));
  2733. ck_assert_str_eq(client_err, test_content);
  2734. mg_close_connection(client_conn);
  2735. test_sleep(1);
  2736. /* Retry DELETE with authorization of a user not authorized for DELETE */
  2737. memset(client_err, 0, sizeof(client_err));
  2738. client_conn =
  2739. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2740. ck_assert_str_eq(client_err, "");
  2741. ck_assert(client_conn != NULL);
  2742. mg_printf(client_conn, "DELETE /%s HTTP/1.0\r\n", test_file);
  2743. mg_printf(client_conn,
  2744. "Authorization: Digest "
  2745. "username=\"%s\", "
  2746. "realm=\"%s\", "
  2747. "nonce=\"%s\", "
  2748. "uri=\"/%s\", "
  2749. "qop=auth, "
  2750. "nc=%s, "
  2751. "cnonce=\"%s\", "
  2752. "response=\"%s\"\r\n\r\n",
  2753. "user",
  2754. domain,
  2755. nonce,
  2756. test_file,
  2757. nc,
  2758. cnonce,
  2759. HA_md5_buf);
  2760. client_res =
  2761. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2762. ck_assert_int_ge(client_res, 0);
  2763. ck_assert_str_eq(client_err, "");
  2764. client_ri = mg_get_request_info(client_conn);
  2765. ck_assert(client_ri != NULL);
  2766. ck_assert_str_eq(client_ri->local_uri, "401");
  2767. mg_close_connection(client_conn);
  2768. test_sleep(1);
  2769. /* Remove the user from the .htpasswd file again */
  2770. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2771. ck_assert_int_eq(client_res, 1);
  2772. test_sleep(1);
  2773. /* Try to access the file again. Expected: 401 error */
  2774. memset(client_err, 0, sizeof(client_err));
  2775. client_conn =
  2776. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2777. ck_assert_str_eq(client_err, "");
  2778. ck_assert(client_conn != NULL);
  2779. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2780. client_res =
  2781. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2782. ck_assert_int_ge(client_res, 0);
  2783. ck_assert_str_eq(client_err, "");
  2784. client_ri = mg_get_request_info(client_conn);
  2785. ck_assert(client_ri != NULL);
  2786. ck_assert_str_eq(client_ri->local_uri, "401");
  2787. mg_close_connection(client_conn);
  2788. test_sleep(1);
  2789. /* Now remove the password file */
  2790. (void)remove(passwd_file);
  2791. test_sleep(1);
  2792. /* Access to the file must work like before */
  2793. memset(client_err, 0, sizeof(client_err));
  2794. client_conn =
  2795. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2796. ck_assert_str_eq(client_err, "");
  2797. ck_assert(client_conn != NULL);
  2798. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2799. client_res =
  2800. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2801. ck_assert_int_ge(client_res, 0);
  2802. ck_assert_str_eq(client_err, "");
  2803. client_ri = mg_get_request_info(client_conn);
  2804. ck_assert(client_ri != NULL);
  2805. ck_assert_str_eq(client_ri->local_uri, "200");
  2806. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2807. ck_assert_int_gt(client_res, 0);
  2808. ck_assert_int_le(client_res, sizeof(client_err));
  2809. ck_assert_str_eq(client_err, test_content);
  2810. mg_close_connection(client_conn);
  2811. test_sleep(1);
  2812. /* Stop the server and clean up */
  2813. test_mg_stop(ctx);
  2814. (void)remove(test_file);
  2815. (void)remove(passwd_file);
  2816. (void)remove("put_delete_auth_file.csv");
  2817. #endif
  2818. mark_point();
  2819. }
  2820. END_TEST
  2821. START_TEST(test_keep_alive)
  2822. {
  2823. struct mg_context *ctx;
  2824. const char *OPTIONS[] =
  2825. { "listening_ports",
  2826. "8081",
  2827. "request_timeout_ms",
  2828. "10000",
  2829. "enable_keep_alive",
  2830. "yes",
  2831. #if !defined(NO_FILES)
  2832. "document_root",
  2833. ".",
  2834. "enable_directory_listing",
  2835. "no",
  2836. #endif
  2837. NULL };
  2838. struct mg_connection *client_conn;
  2839. char client_err[256];
  2840. const struct mg_request_info *client_ri;
  2841. int client_res, i;
  2842. const char *connection_header;
  2843. mark_point();
  2844. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2845. ck_assert(ctx != NULL);
  2846. /* HTTP 1.1 GET request */
  2847. memset(client_err, 0, sizeof(client_err));
  2848. client_conn =
  2849. mg_connect_client("127.0.0.1", 8081, 0, client_err, sizeof(client_err));
  2850. ck_assert_str_eq(client_err, "");
  2851. ck_assert(client_conn != NULL);
  2852. mg_printf(client_conn,
  2853. "GET / HTTP/1.1\r\nHost: "
  2854. "localhost:8081\r\nConnection: keep-alive\r\n\r\n");
  2855. client_res =
  2856. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2857. ck_assert_int_ge(client_res, 0);
  2858. ck_assert_str_eq(client_err, "");
  2859. client_ri = mg_get_request_info(client_conn);
  2860. ck_assert(client_ri != NULL);
  2861. #if defined(NO_FILES)
  2862. ck_assert_str_eq(client_ri->local_uri, "404");
  2863. #else
  2864. ck_assert_str_eq(client_ri->local_uri, "403");
  2865. #endif
  2866. connection_header = 0;
  2867. for (i = 0; i < client_ri->num_headers; i++) {
  2868. if (!mg_strcasecmp(client_ri->http_headers[i].name, "Connection")) {
  2869. ck_assert_ptr_eq(connection_header, NULL);
  2870. connection_header = client_ri->http_headers[i].value;
  2871. ck_assert_ptr_ne(connection_header, NULL);
  2872. }
  2873. }
  2874. /* Error replies will close the connection, even if keep-alive is set. */
  2875. ck_assert_ptr_ne(connection_header, NULL);
  2876. ck_assert_str_eq(connection_header, "close");
  2877. mg_close_connection(client_conn);
  2878. test_sleep(1);
  2879. /* TODO: request a file and keep alive
  2880. * (will only work if NO_FILES is not set). */
  2881. /* Stop the server and clean up */
  2882. test_mg_stop(ctx);
  2883. mark_point();
  2884. }
  2885. END_TEST
  2886. START_TEST(test_error_handling)
  2887. {
  2888. struct mg_context *ctx;
  2889. FILE *f;
  2890. char bad_thread_num[32] = "badnumber";
  2891. struct mg_callbacks callbacks;
  2892. char errmsg[256];
  2893. struct mg_connection *client_conn;
  2894. char client_err[256];
  2895. const struct mg_request_info *client_ri;
  2896. int client_res, i;
  2897. const char *OPTIONS[32];
  2898. int opt_cnt = 0;
  2899. mark_point();
  2900. #if !defined(NO_FILES)
  2901. OPTIONS[opt_cnt++] = "document_root";
  2902. OPTIONS[opt_cnt++] = ".";
  2903. #endif
  2904. OPTIONS[opt_cnt++] = "error_pages";
  2905. OPTIONS[opt_cnt++] = "./";
  2906. OPTIONS[opt_cnt++] = "listening_ports";
  2907. OPTIONS[opt_cnt++] = "8080";
  2908. OPTIONS[opt_cnt++] = "num_threads";
  2909. OPTIONS[opt_cnt++] = bad_thread_num;
  2910. OPTIONS[opt_cnt++] = "unknown_option";
  2911. OPTIONS[opt_cnt++] = "unknown_option_value";
  2912. OPTIONS[opt_cnt] = NULL;
  2913. memset(&callbacks, 0, sizeof(callbacks));
  2914. callbacks.log_message = log_msg_func;
  2915. /* test with unknown option */
  2916. memset(errmsg, 0, sizeof(errmsg));
  2917. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2918. /* Details of errmsg may vary, but it may not be empty */
  2919. ck_assert_str_ne(errmsg, "");
  2920. ck_assert(ctx == NULL);
  2921. ck_assert_str_eq(errmsg, "Invalid option: unknown_option");
  2922. /* Remove invalid option */
  2923. for (i = 0; OPTIONS[i]; i++) {
  2924. if (strstr(OPTIONS[i], "unknown_option")) {
  2925. OPTIONS[i] = 0;
  2926. }
  2927. }
  2928. /* Test with bad num_thread option */
  2929. memset(errmsg, 0, sizeof(errmsg));
  2930. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2931. /* Details of errmsg may vary, but it may not be empty */
  2932. ck_assert_str_ne(errmsg, "");
  2933. ck_assert(ctx == NULL);
  2934. ck_assert_str_eq(errmsg, "Invalid number of worker threads");
  2935. /* Set to a number - but use a number above the limit */
  2936. #ifdef MAX_WORKER_THREADS
  2937. sprintf(bad_thread_num, "%u", MAX_WORKER_THREADS + 1);
  2938. #else
  2939. sprintf(bad_thread_num, "%lu", 1000000000lu);
  2940. #endif
  2941. /* Test with bad num_thread option */
  2942. memset(errmsg, 0, sizeof(errmsg));
  2943. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2944. /* Details of errmsg may vary, but it may not be empty */
  2945. ck_assert_str_ne(errmsg, "");
  2946. ck_assert(ctx == NULL);
  2947. ck_assert_str_eq(errmsg, "Too many worker threads");
  2948. /* HTTP 1.0 GET request - server is not running */
  2949. memset(client_err, 0, sizeof(client_err));
  2950. client_conn =
  2951. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2952. ck_assert(client_conn == NULL);
  2953. /* Error message detail may vary - it may not be empty ans should contain
  2954. * some information "connect" failed */
  2955. ck_assert_str_ne(client_err, "");
  2956. ck_assert(strstr(client_err, "connect"));
  2957. /* This time start the server with a valid configuration */
  2958. sprintf(bad_thread_num, "%i", 1);
  2959. memset(errmsg, 0, sizeof(errmsg));
  2960. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2961. ck_assert_str_eq(errmsg, "");
  2962. ck_assert(ctx != NULL);
  2963. /* Server is running now */
  2964. test_sleep(1);
  2965. /* Remove error files (in case they exist) */
  2966. (void)remove("error.htm");
  2967. (void)remove("error4xx.htm");
  2968. (void)remove("error404.htm");
  2969. /* Ask for something not existing - should get default 404 */
  2970. memset(client_err, 0, sizeof(client_err));
  2971. client_conn =
  2972. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2973. ck_assert_str_eq(client_err, "");
  2974. ck_assert(client_conn != NULL);
  2975. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2976. client_res =
  2977. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2978. ck_assert_int_ge(client_res, 0);
  2979. ck_assert_str_eq(client_err, "");
  2980. client_ri = mg_get_request_info(client_conn);
  2981. ck_assert(client_ri != NULL);
  2982. ck_assert_str_eq(client_ri->local_uri, "404");
  2983. mg_close_connection(client_conn);
  2984. test_sleep(1);
  2985. /* Create an error.htm file */
  2986. f = fopen("error.htm", "wt");
  2987. ck_assert(f != NULL);
  2988. (void)fprintf(f, "err-all");
  2989. (void)fclose(f);
  2990. /* Ask for something not existing - should get error.htm */
  2991. memset(client_err, 0, sizeof(client_err));
  2992. client_conn =
  2993. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2994. ck_assert_str_eq(client_err, "");
  2995. ck_assert(client_conn != NULL);
  2996. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2997. client_res =
  2998. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2999. ck_assert_int_ge(client_res, 0);
  3000. ck_assert_str_eq(client_err, "");
  3001. client_ri = mg_get_request_info(client_conn);
  3002. ck_assert(client_ri != NULL);
  3003. ck_assert_str_eq(client_ri->local_uri, "200");
  3004. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3005. mg_close_connection(client_conn);
  3006. ck_assert_int_eq(client_res, 7);
  3007. client_err[8] = 0;
  3008. ck_assert_str_eq(client_err, "err-all");
  3009. test_sleep(1);
  3010. /* Create an error4xx.htm file */
  3011. f = fopen("error4xx.htm", "wt");
  3012. ck_assert(f != NULL);
  3013. (void)fprintf(f, "err-4xx");
  3014. (void)fclose(f);
  3015. /* Ask for something not existing - should get error4xx.htm */
  3016. memset(client_err, 0, sizeof(client_err));
  3017. client_conn =
  3018. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3019. ck_assert_str_eq(client_err, "");
  3020. ck_assert(client_conn != NULL);
  3021. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  3022. client_res =
  3023. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3024. ck_assert_int_ge(client_res, 0);
  3025. ck_assert_str_eq(client_err, "");
  3026. client_ri = mg_get_request_info(client_conn);
  3027. ck_assert(client_ri != NULL);
  3028. ck_assert_str_eq(client_ri->local_uri, "200");
  3029. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3030. mg_close_connection(client_conn);
  3031. ck_assert_int_eq(client_res, 7);
  3032. client_err[8] = 0;
  3033. ck_assert_str_eq(client_err, "err-4xx");
  3034. test_sleep(1);
  3035. /* Create an error404.htm file */
  3036. f = fopen("error404.htm", "wt");
  3037. ck_assert(f != NULL);
  3038. (void)fprintf(f, "err-404");
  3039. (void)fclose(f);
  3040. /* Ask for something not existing - should get error404.htm */
  3041. memset(client_err, 0, sizeof(client_err));
  3042. client_conn =
  3043. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3044. ck_assert_str_eq(client_err, "");
  3045. ck_assert(client_conn != NULL);
  3046. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  3047. client_res =
  3048. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3049. ck_assert_int_ge(client_res, 0);
  3050. ck_assert_str_eq(client_err, "");
  3051. client_ri = mg_get_request_info(client_conn);
  3052. ck_assert(client_ri != NULL);
  3053. ck_assert_str_eq(client_ri->local_uri, "200");
  3054. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3055. mg_close_connection(client_conn);
  3056. ck_assert_int_eq(client_res, 7);
  3057. client_err[8] = 0;
  3058. ck_assert_str_eq(client_err, "err-404");
  3059. test_sleep(1);
  3060. /* Ask in a malformed way - should get error4xx.htm */
  3061. memset(client_err, 0, sizeof(client_err));
  3062. client_conn =
  3063. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3064. ck_assert_str_eq(client_err, "");
  3065. ck_assert(client_conn != NULL);
  3066. mg_printf(client_conn, "Gimme some file!\r\n\r\n");
  3067. client_res =
  3068. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3069. ck_assert_int_ge(client_res, 0);
  3070. ck_assert_str_eq(client_err, "");
  3071. client_ri = mg_get_request_info(client_conn);
  3072. ck_assert(client_ri != NULL);
  3073. ck_assert_str_eq(client_ri->local_uri, "200");
  3074. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3075. mg_close_connection(client_conn);
  3076. ck_assert_int_eq(client_res, 7);
  3077. client_err[8] = 0;
  3078. ck_assert_str_eq(client_err, "err-4xx");
  3079. test_sleep(1);
  3080. /* Remove all error files created by this test */
  3081. (void)remove("error.htm");
  3082. (void)remove("error4xx.htm");
  3083. (void)remove("error404.htm");
  3084. /* Stop the server */
  3085. test_mg_stop(ctx);
  3086. /* HTTP 1.1 GET request - must not work, since server is already stopped */
  3087. memset(client_err, 0, sizeof(client_err));
  3088. client_conn =
  3089. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3090. ck_assert(client_conn == NULL);
  3091. ck_assert_str_ne(client_err, "");
  3092. test_sleep(1);
  3093. mark_point();
  3094. }
  3095. END_TEST
  3096. START_TEST(test_error_log_file)
  3097. {
  3098. /* Server var */
  3099. struct mg_context *ctx;
  3100. const char *OPTIONS[32];
  3101. int opt_cnt = 0;
  3102. /* Client var */
  3103. struct mg_connection *client;
  3104. char client_err_buf[256];
  3105. char client_data_buf[256];
  3106. const struct mg_request_info *client_ri;
  3107. /* File content check var */
  3108. FILE *f;
  3109. char buf[1024];
  3110. int len, ok;
  3111. mark_point();
  3112. /* Set options and start server */
  3113. OPTIONS[opt_cnt++] = "listening_ports";
  3114. OPTIONS[opt_cnt++] = "8080";
  3115. OPTIONS[opt_cnt++] = "error_log_file";
  3116. OPTIONS[opt_cnt++] = "error.log";
  3117. OPTIONS[opt_cnt++] = "access_log_file";
  3118. OPTIONS[opt_cnt++] = "access.log";
  3119. #if !defined(NO_FILES)
  3120. OPTIONS[opt_cnt++] = "document_root";
  3121. OPTIONS[opt_cnt++] = ".";
  3122. #endif
  3123. OPTIONS[opt_cnt] = NULL;
  3124. ctx = test_mg_start(NULL, 0, OPTIONS);
  3125. ck_assert(ctx != NULL);
  3126. /* Remove log files (they may exist from previous incomplete runs of
  3127. * this test) */
  3128. (void)remove("error.log");
  3129. (void)remove("access.log");
  3130. /* connect client */
  3131. memset(client_err_buf, 0, sizeof(client_err_buf));
  3132. memset(client_data_buf, 0, sizeof(client_data_buf));
  3133. client = mg_download("127.0.0.1",
  3134. 8080,
  3135. 0,
  3136. client_err_buf,
  3137. sizeof(client_err_buf),
  3138. "GET /not_existing_file.ext HTTP/1.0\r\n\r\n");
  3139. ck_assert(ctx != NULL);
  3140. ck_assert_str_eq(client_err_buf, "");
  3141. client_ri = mg_get_request_info(client);
  3142. ck_assert(client_ri != NULL);
  3143. ck_assert_str_eq(client_ri->local_uri, "404");
  3144. /* Close the client connection */
  3145. mg_close_connection(client);
  3146. /* Stop the server */
  3147. test_mg_stop(ctx);
  3148. /* Check access.log */
  3149. memset(buf, 0, sizeof(buf));
  3150. f = fopen("access.log", "r");
  3151. ck_assert_msg(f != NULL, "Cannot open access log file");
  3152. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3153. (void)fclose(f);
  3154. ck_assert_msg(ok, "Cannot read access log file");
  3155. len = (int)strlen(buf);
  3156. ck_assert_int_gt(len, 0);
  3157. ok = (NULL != strstr(buf, "not_existing_file.ext"));
  3158. ck_assert_msg(ok, "Did not find uri in access log file");
  3159. ok = (NULL != strstr(buf, "404"));
  3160. ck_assert_msg(ok, "Did not find HTTP status code in access log file");
  3161. /* Check error.log */
  3162. memset(buf, 0, sizeof(buf));
  3163. f = fopen("error.log", "r");
  3164. if (f) {
  3165. (void)fgets(buf, sizeof(buf) - 1, f);
  3166. fclose(f);
  3167. }
  3168. ck_assert_msg(f == NULL,
  3169. "Should not create error log file on 404, but got [%s]",
  3170. buf);
  3171. /* Remove log files */
  3172. (void)remove("error.log");
  3173. (void)remove("access.log");
  3174. /* Start server with bad options */
  3175. ck_assert_str_eq(OPTIONS[0], "listening_ports");
  3176. OPTIONS[1] = "bad !"; /* no r or s in string */
  3177. ctx = test_mg_start(NULL, 0, OPTIONS);
  3178. ck_assert_msg(
  3179. ctx == NULL,
  3180. "Should not be able to start server with bad port configuration");
  3181. /* Check access.log */
  3182. memset(buf, 0, sizeof(buf));
  3183. f = fopen("access.log", "r");
  3184. if (f) {
  3185. (void)fgets(buf, sizeof(buf) - 1, f);
  3186. fclose(f);
  3187. }
  3188. ck_assert_msg(
  3189. f == NULL,
  3190. "Should not create access log file if start fails, but got [%s]",
  3191. buf);
  3192. /* Check error.log */
  3193. memset(buf, 0, sizeof(buf));
  3194. f = fopen("error.log", "r");
  3195. ck_assert_msg(f != NULL, "Cannot open access log file");
  3196. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3197. (void)fclose(f);
  3198. ck_assert_msg(ok, "Cannot read access log file");
  3199. len = (int)strlen(buf);
  3200. ck_assert_int_gt(len, 0);
  3201. ok = (NULL != strstr(buf, "port"));
  3202. ck_assert_msg(ok, "Did not find port as error reason in error log file");
  3203. /* Remove log files */
  3204. (void)remove("error.log");
  3205. (void)remove("access.log");
  3206. mark_point();
  3207. }
  3208. END_TEST
  3209. static int
  3210. test_throttle_begin_request(struct mg_connection *conn)
  3211. {
  3212. const struct mg_request_info *ri;
  3213. long unsigned len = 1024 * 10;
  3214. const char *block = "0123456789";
  3215. unsigned long i, blocklen;
  3216. ck_assert(conn != NULL);
  3217. ri = mg_get_request_info(conn);
  3218. ck_assert(ri != NULL);
  3219. ck_assert_str_eq(ri->request_method, "GET");
  3220. ck_assert_str_eq(ri->request_uri, "/throttle");
  3221. ck_assert_str_eq(ri->local_uri, "/throttle");
  3222. ck_assert_str_eq(ri->http_version, "1.0");
  3223. ck_assert_str_eq(ri->query_string, "q");
  3224. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3225. mg_printf(conn,
  3226. "HTTP/1.1 200 OK\r\n"
  3227. "Content-Length: %lu\r\n"
  3228. "Connection: close\r\n\r\n",
  3229. len);
  3230. blocklen = (unsigned long)strlen(block);
  3231. for (i = 0; i < len; i += blocklen) {
  3232. mg_write(conn, block, blocklen);
  3233. }
  3234. mark_point();
  3235. return 987; /* Not a valid HTTP response code,
  3236. * but it should be written to the log and passed to
  3237. * end_request. */
  3238. }
  3239. static void
  3240. test_throttle_end_request(const struct mg_connection *conn,
  3241. int reply_status_code)
  3242. {
  3243. const struct mg_request_info *ri;
  3244. ck_assert(conn != NULL);
  3245. ri = mg_get_request_info(conn);
  3246. ck_assert(ri != NULL);
  3247. ck_assert_str_eq(ri->request_method, "GET");
  3248. ck_assert_str_eq(ri->request_uri, "/throttle");
  3249. ck_assert_str_eq(ri->local_uri, "/throttle");
  3250. ck_assert_str_eq(ri->http_version, "1.0");
  3251. ck_assert_str_eq(ri->query_string, "q");
  3252. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3253. ck_assert_int_eq(reply_status_code, 987);
  3254. }
  3255. START_TEST(test_throttle)
  3256. {
  3257. /* Server var */
  3258. struct mg_context *ctx;
  3259. struct mg_callbacks callbacks;
  3260. const char *OPTIONS[32];
  3261. int opt_cnt = 0;
  3262. /* Client var */
  3263. struct mg_connection *client;
  3264. char client_err_buf[256];
  3265. char client_data_buf[256];
  3266. const struct mg_request_info *client_ri;
  3267. /* timing test */
  3268. int r, data_read;
  3269. time_t t0, t1;
  3270. double dt;
  3271. mark_point();
  3272. /* Set options and start server */
  3273. #if !defined(NO_FILES)
  3274. OPTIONS[opt_cnt++] = "document_root";
  3275. OPTIONS[opt_cnt++] = ".";
  3276. #endif
  3277. OPTIONS[opt_cnt++] = "listening_ports";
  3278. OPTIONS[opt_cnt++] = "8080";
  3279. OPTIONS[opt_cnt++] = "throttle";
  3280. OPTIONS[opt_cnt++] = "*=1k";
  3281. OPTIONS[opt_cnt] = NULL;
  3282. memset(&callbacks, 0, sizeof(callbacks));
  3283. callbacks.begin_request = test_throttle_begin_request;
  3284. callbacks.end_request = test_throttle_end_request;
  3285. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3286. ck_assert(ctx != NULL);
  3287. /* connect client */
  3288. memset(client_err_buf, 0, sizeof(client_err_buf));
  3289. memset(client_data_buf, 0, sizeof(client_data_buf));
  3290. strcpy(client_err_buf, "reset-content");
  3291. client = mg_download("127.0.0.1",
  3292. 8080,
  3293. 0,
  3294. client_err_buf,
  3295. sizeof(client_err_buf),
  3296. "GET /throttle?q HTTP/1.0\r\n\r\n");
  3297. ck_assert(ctx != NULL);
  3298. ck_assert_str_eq(client_err_buf, "");
  3299. client_ri = mg_get_request_info(client);
  3300. ck_assert(client_ri != NULL);
  3301. ck_assert_str_eq(client_ri->local_uri, "200");
  3302. ck_assert_int_eq(client_ri->content_length, 1024 * 10);
  3303. data_read = 0;
  3304. t0 = time(NULL);
  3305. while (data_read < client_ri->content_length) {
  3306. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3307. ck_assert_int_ge(r, 0);
  3308. data_read += r;
  3309. }
  3310. t1 = time(NULL);
  3311. dt = difftime(t1, t0) * 1000.0; /* Elapsed time in ms - in most systems
  3312. * only with second resolution */
  3313. /* Time estimation: Data size is 10 kB, with 1 kB/s speed limit.
  3314. * The first block (1st kB) is transferred immediately, the second
  3315. * block (2nd kB) one second later, the third block (3rd kB) two
  3316. * seconds later, .. the last block (10th kB) nine seconds later.
  3317. * The resolution of time measurement using the "time" C library
  3318. * function is 1 second, so we should add +/- one second tolerance.
  3319. * Thus, download of 10 kB with 1 kB/s should not be faster than
  3320. * 8 seconds. */
  3321. /* Check if there are at least 8 seconds */
  3322. ck_assert_int_ge((int)dt, 8 * 1000);
  3323. /* Nothing left to read */
  3324. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3325. ck_assert_int_eq(r, 0);
  3326. /* Close the client connection */
  3327. mg_close_connection(client);
  3328. /* Stop the server */
  3329. test_mg_stop(ctx);
  3330. mark_point();
  3331. }
  3332. END_TEST
  3333. START_TEST(test_init_library)
  3334. {
  3335. unsigned f_avail, f_ret;
  3336. mark_point();
  3337. f_avail = mg_check_feature(0xFF);
  3338. f_ret = mg_init_library(f_avail);
  3339. ck_assert_uint_eq(f_ret, f_avail);
  3340. }
  3341. END_TEST
  3342. #define LARGE_FILE_SIZE (1024 * 1024 * 10)
  3343. static int
  3344. test_large_file_begin_request(struct mg_connection *conn)
  3345. {
  3346. const struct mg_request_info *ri;
  3347. long unsigned len = LARGE_FILE_SIZE;
  3348. const char *block = "0123456789";
  3349. uint64_t i;
  3350. size_t blocklen;
  3351. ck_assert(conn != NULL);
  3352. ri = mg_get_request_info(conn);
  3353. ck_assert(ri != NULL);
  3354. ck_assert_str_eq(ri->request_method, "GET");
  3355. ck_assert_str_eq(ri->http_version, "1.1");
  3356. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3357. ck_assert_ptr_eq(ri->query_string, NULL);
  3358. ck_assert_ptr_ne(ri->local_uri, NULL);
  3359. mg_printf(conn,
  3360. "HTTP/1.1 200 OK\r\n"
  3361. "Content-Length: %lu\r\n"
  3362. "Connection: close\r\n\r\n",
  3363. len);
  3364. blocklen = strlen(block);
  3365. for (i = 0; i < len; i += blocklen) {
  3366. mg_write(conn, block, blocklen);
  3367. }
  3368. mark_point();
  3369. return 200;
  3370. }
  3371. START_TEST(test_large_file)
  3372. {
  3373. /* Server var */
  3374. struct mg_context *ctx;
  3375. struct mg_callbacks callbacks;
  3376. const char *OPTIONS[32];
  3377. int opt_cnt = 0;
  3378. #if !defined(NO_SSL)
  3379. const char *ssl_cert = locate_ssl_cert();
  3380. #endif
  3381. char errmsg[256] = {0};
  3382. /* Client var */
  3383. struct mg_connection *client;
  3384. char client_err_buf[256];
  3385. char client_data_buf[256];
  3386. const struct mg_request_info *client_ri;
  3387. int64_t data_read;
  3388. int r;
  3389. int retry, retry_ok_cnt, retry_fail_cnt;
  3390. mark_point();
  3391. /* Set options and start server */
  3392. #if !defined(NO_FILES)
  3393. OPTIONS[opt_cnt++] = "document_root";
  3394. OPTIONS[opt_cnt++] = ".";
  3395. #endif
  3396. #if defined(NO_SSL)
  3397. OPTIONS[opt_cnt++] = "listening_ports";
  3398. OPTIONS[opt_cnt++] = "8080";
  3399. #else
  3400. OPTIONS[opt_cnt++] = "listening_ports";
  3401. OPTIONS[opt_cnt++] = "8443s";
  3402. OPTIONS[opt_cnt++] = "ssl_certificate";
  3403. OPTIONS[opt_cnt++] = ssl_cert;
  3404. #ifdef __MACH__
  3405. /* The Apple builds on Travis CI seem to have problems with TLS1.x
  3406. * Allow SSLv3 and TLS */
  3407. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3408. OPTIONS[opt_cnt++] = "2";
  3409. #else
  3410. /* The Linux builds on Travis CI work fine with TLS1.2 */
  3411. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3412. OPTIONS[opt_cnt++] = "4";
  3413. #endif
  3414. ck_assert(ssl_cert != NULL);
  3415. #endif
  3416. OPTIONS[opt_cnt] = NULL;
  3417. memset(&callbacks, 0, sizeof(callbacks));
  3418. callbacks.begin_request = test_large_file_begin_request;
  3419. callbacks.log_message = log_msg_func;
  3420. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  3421. ck_assert_str_eq(errmsg, "");
  3422. ck_assert(ctx != NULL);
  3423. /* Try downloading several times */
  3424. retry_ok_cnt = 0;
  3425. retry_fail_cnt = 0;
  3426. for (retry = 0; retry < 3; retry++) {
  3427. int fail = 0;
  3428. /* connect client */
  3429. memset(client_err_buf, 0, sizeof(client_err_buf));
  3430. memset(client_data_buf, 0, sizeof(client_data_buf));
  3431. client =
  3432. mg_download("127.0.0.1",
  3433. #if defined(NO_SSL)
  3434. 8080,
  3435. 0,
  3436. #else
  3437. 8443,
  3438. 1,
  3439. #endif
  3440. client_err_buf,
  3441. sizeof(client_err_buf),
  3442. "GET /large.file HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3443. ck_assert(client != NULL);
  3444. ck_assert_str_eq(client_err_buf, "");
  3445. client_ri = mg_get_request_info(client);
  3446. ck_assert(client_ri != NULL);
  3447. ck_assert_str_eq(client_ri->local_uri, "200");
  3448. ck_assert_int_eq(client_ri->content_length, LARGE_FILE_SIZE);
  3449. data_read = 0;
  3450. while (data_read < client_ri->content_length) {
  3451. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3452. if (r < 0) {
  3453. fail = 1;
  3454. break;
  3455. };
  3456. data_read += r;
  3457. }
  3458. /* Nothing left to read */
  3459. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3460. if (fail) {
  3461. ck_assert_int_eq(r, -1);
  3462. retry_fail_cnt++;
  3463. } else {
  3464. ck_assert_int_eq(r, 0);
  3465. retry_ok_cnt++;
  3466. }
  3467. /* Close the client connection */
  3468. mg_close_connection(client);
  3469. }
  3470. #if defined(_WIN32)
  3471. // TODO: Check this problem on AppVeyor
  3472. // ck_assert_int_le(retry_fail_cnt, 2);
  3473. // ck_assert_int_ge(retry_ok_cnt, 1);
  3474. #else
  3475. ck_assert_int_eq(retry_fail_cnt, 0);
  3476. ck_assert_int_eq(retry_ok_cnt, 3);
  3477. #endif
  3478. /* Stop the server */
  3479. test_mg_stop(ctx);
  3480. mark_point();
  3481. }
  3482. END_TEST
  3483. static int test_mg_store_body_con_len = 20000;
  3484. static int
  3485. test_mg_store_body_put_delete_handler(struct mg_connection *conn, void *ignored)
  3486. {
  3487. char path[4096] = {0};
  3488. const struct mg_request_info *info = mg_get_request_info(conn);
  3489. int64_t rc;
  3490. (void)ignored;
  3491. mark_point();
  3492. sprintf(path, "./%s", info->local_uri);
  3493. rc = mg_store_body(conn, path);
  3494. ck_assert_int_eq(test_mg_store_body_con_len, rc);
  3495. if (rc < 0) {
  3496. mg_printf(conn,
  3497. "HTTP/1.1 500 Internal Server Error\r\n"
  3498. "Content-Type:text/plain;charset=UTF-8\r\n"
  3499. "Connection:close\r\n\r\n"
  3500. "%s (ret: %ld)\n",
  3501. path,
  3502. (long)rc);
  3503. mg_close_connection(conn);
  3504. /* Debug output for tests */
  3505. printf("mg_store_body(%s) failed (ret: %ld)\n", path, (long)rc);
  3506. return 500;
  3507. }
  3508. mg_printf(conn,
  3509. "HTTP/1.1 200 OK\r\n"
  3510. "Content-Type:text/plain;charset=UTF-8\r\n"
  3511. "Connection:close\r\n\r\n"
  3512. "%s OK (%ld bytes saved)\n",
  3513. path,
  3514. (long)rc);
  3515. mg_close_connection(conn);
  3516. /* Debug output for tests */
  3517. printf("mg_store_body(%s) OK (%ld bytes)\n", path, (long)rc);
  3518. mark_point();
  3519. return 200;
  3520. }
  3521. static int
  3522. test_mg_store_body_begin_request_callback(struct mg_connection *conn)
  3523. {
  3524. const struct mg_request_info *info = mg_get_request_info(conn);
  3525. mark_point();
  3526. /* Debug output for tests */
  3527. printf("test_mg_store_body_begin_request_callback called (%s)\n",
  3528. info->request_method);
  3529. if ((strcmp(info->request_method, "PUT") == 0)
  3530. || (strcmp(info->request_method, "DELETE") == 0)) {
  3531. return test_mg_store_body_put_delete_handler(conn, NULL);
  3532. }
  3533. mark_point();
  3534. return 0;
  3535. }
  3536. START_TEST(test_mg_store_body)
  3537. {
  3538. /* Client data */
  3539. char client_err_buf[256];
  3540. char client_data_buf[1024];
  3541. struct mg_connection *client;
  3542. const struct mg_request_info *client_ri;
  3543. int r;
  3544. char check_data[256];
  3545. char *check_ptr;
  3546. char errmsg[256] = {0};
  3547. /* Server context handle */
  3548. struct mg_context *ctx;
  3549. struct mg_callbacks callbacks;
  3550. const char *options[] = {
  3551. #if !defined(NO_FILES)
  3552. "document_root",
  3553. ".",
  3554. #endif
  3555. #if !defined(NO_CACHING)
  3556. "static_file_max_age",
  3557. "0",
  3558. #endif
  3559. "listening_ports",
  3560. "127.0.0.1:8082",
  3561. "num_threads",
  3562. "1",
  3563. NULL
  3564. };
  3565. mark_point();
  3566. memset(&callbacks, 0, sizeof(callbacks));
  3567. callbacks.begin_request = test_mg_store_body_begin_request_callback;
  3568. callbacks.log_message = log_msg_func;
  3569. /* Initialize the library */
  3570. mg_init_library(0);
  3571. /* Start the server */
  3572. ctx = mg_start(&callbacks, (void *)errmsg, options);
  3573. ck_assert_str_eq(errmsg, "");
  3574. ck_assert(ctx != NULL);
  3575. /* Run the server for 15 seconds */
  3576. test_sleep(15);
  3577. /* Call a test client */
  3578. client = mg_connect_client(
  3579. "127.0.0.1", 8082, 0, client_err_buf, sizeof(client_err_buf));
  3580. ck_assert_str_eq(client_err_buf, "");
  3581. ck_assert(client != NULL);
  3582. mg_printf(client,
  3583. "PUT /%s HTTP/1.0\r\nContent-Length: %i\r\n\r\n",
  3584. "test_file_name.txt",
  3585. test_mg_store_body_con_len);
  3586. r = 0;
  3587. while (r < test_mg_store_body_con_len) {
  3588. int l = mg_write(client, "1234567890", 10);
  3589. ck_assert_int_eq(l, 10);
  3590. r += 10;
  3591. }
  3592. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3593. ck_assert_int_ge(r, 0);
  3594. ck_assert_str_eq(client_err_buf, "");
  3595. client_ri = mg_get_request_info(client);
  3596. ck_assert(client_ri != NULL);
  3597. /* Response must be 200 OK */
  3598. ck_assert_ptr_ne(client_ri->request_uri, NULL);
  3599. ck_assert_str_eq(client_ri->request_uri, "200");
  3600. /* Read PUT response */
  3601. r = mg_read(client, client_data_buf, sizeof(client_data_buf) - 1);
  3602. ck_assert_int_gt(r, 0);
  3603. client_data_buf[r] = 0;
  3604. sprintf(check_data, "(%i bytes saved)", test_mg_store_body_con_len);
  3605. check_ptr = strstr(client_data_buf, check_data);
  3606. ck_assert_ptr_ne(check_ptr, NULL);
  3607. mg_close_connection(client);
  3608. /* Run the server for 5 seconds */
  3609. test_sleep(5);
  3610. /* Stop the server */
  3611. test_mg_stop(ctx);
  3612. /* Un-initialize the library */
  3613. mg_exit_library();
  3614. mark_point();
  3615. }
  3616. END_TEST
  3617. #if defined(MG_USE_OPEN_FILE) && !defined(NO_FILES)
  3618. #define FILE_IN_MEM_SIZE (1024 * 100)
  3619. static char *file_in_mem_data;
  3620. static const char *
  3621. test_file_in_memory_open_file(const struct mg_connection *conn,
  3622. const char *file_path,
  3623. size_t *file_size)
  3624. {
  3625. (void)conn;
  3626. if (strcmp(file_path, "./file_in_mem") == 0) {
  3627. /* File is in memory */
  3628. *file_size = FILE_IN_MEM_SIZE;
  3629. return file_in_mem_data;
  3630. } else {
  3631. /* File is not in memory */
  3632. return NULL;
  3633. }
  3634. }
  3635. START_TEST(test_file_in_memory)
  3636. {
  3637. /* Server var */
  3638. struct mg_context *ctx;
  3639. struct mg_callbacks callbacks;
  3640. const char *OPTIONS[32];
  3641. int opt_cnt = 0;
  3642. #if !defined(NO_SSL)
  3643. const char *ssl_cert = locate_ssl_cert();
  3644. #endif
  3645. /* Client var */
  3646. struct mg_connection *client;
  3647. char client_err_buf[256];
  3648. char client_data_buf[256];
  3649. const struct mg_request_info *client_ri;
  3650. int64_t data_read;
  3651. int r, i;
  3652. /* Prepare test data */
  3653. file_in_mem_data = (char *)malloc(FILE_IN_MEM_SIZE);
  3654. ck_assert_ptr_ne(file_in_mem_data, NULL);
  3655. for (r = 0; r < FILE_IN_MEM_SIZE; r++) {
  3656. file_in_mem_data[r] = (char)(r);
  3657. }
  3658. /* Set options and start server */
  3659. OPTIONS[opt_cnt++] = "document_root";
  3660. OPTIONS[opt_cnt++] = ".";
  3661. #if defined(NO_SSL)
  3662. OPTIONS[opt_cnt++] = "listening_ports";
  3663. OPTIONS[opt_cnt++] = "8080";
  3664. #else
  3665. OPTIONS[opt_cnt++] = "listening_ports";
  3666. OPTIONS[opt_cnt++] = "8443s";
  3667. OPTIONS[opt_cnt++] = "ssl_certificate";
  3668. OPTIONS[opt_cnt++] = ssl_cert;
  3669. ck_assert(ssl_cert != NULL);
  3670. #endif
  3671. OPTIONS[opt_cnt] = NULL;
  3672. memset(&callbacks, 0, sizeof(callbacks));
  3673. callbacks.open_file = test_file_in_memory_open_file;
  3674. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3675. ck_assert(ctx != NULL);
  3676. /* connect client */
  3677. memset(client_err_buf, 0, sizeof(client_err_buf));
  3678. memset(client_data_buf, 0, sizeof(client_data_buf));
  3679. client =
  3680. mg_download("127.0.0.1",
  3681. #if defined(NO_SSL)
  3682. 8080,
  3683. 0,
  3684. #else
  3685. 8443,
  3686. 1,
  3687. #endif
  3688. client_err_buf,
  3689. sizeof(client_err_buf),
  3690. "GET /file_in_mem HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3691. ck_assert(client != NULL);
  3692. ck_assert_str_eq(client_err_buf, "");
  3693. client_ri = mg_get_request_info(client);
  3694. ck_assert(client_ri != NULL);
  3695. ck_assert_str_eq(client_ri->local_uri, "200");
  3696. ck_assert_int_eq(client_ri->content_length, FILE_IN_MEM_SIZE);
  3697. data_read = 0;
  3698. while (data_read < client_ri->content_length) {
  3699. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3700. if (r > 0) {
  3701. for (i = 0; i < r; i++) {
  3702. ck_assert_int_eq((int)client_data_buf[i],
  3703. (int)file_in_mem_data[data_read + i]);
  3704. }
  3705. data_read += r;
  3706. }
  3707. }
  3708. /* Nothing left to read */
  3709. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3710. ck_assert_int_eq(r, 0);
  3711. /* Close the client connection */
  3712. mg_close_connection(client);
  3713. /* Stop the server */
  3714. test_mg_stop(ctx);
  3715. /* Free test data */
  3716. free(file_in_mem_data);
  3717. file_in_mem_data = NULL;
  3718. }
  3719. END_TEST
  3720. #else /* defined(MG_USE_OPEN_FILE) */
  3721. START_TEST(test_file_in_memory)
  3722. {
  3723. mark_point();
  3724. }
  3725. END_TEST
  3726. #endif
  3727. static void
  3728. minimal_http_https_client_impl(const char *server,
  3729. uint16_t port,
  3730. int use_ssl,
  3731. const char *uri)
  3732. {
  3733. /* Client var */
  3734. struct mg_connection *client;
  3735. char client_err_buf[256];
  3736. char client_data_buf[256];
  3737. const struct mg_request_info *client_ri;
  3738. int64_t data_read;
  3739. int r;
  3740. mark_point();
  3741. client = mg_connect_client(
  3742. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  3743. ck_assert_str_eq(client_err_buf, "");
  3744. ck_assert(client != NULL);
  3745. mg_printf(client, "GET /%s HTTP/1.0\r\n\r\n", uri);
  3746. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3747. ck_assert_int_ge(r, 0);
  3748. ck_assert_str_eq(client_err_buf, "");
  3749. client_ri = mg_get_request_info(client);
  3750. ck_assert(client_ri != NULL);
  3751. /* e.g.: ck_assert_str_eq(client_ri->request_uri, "200"); */
  3752. ck_assert_ptr_ne(client_ri->request_uri, NULL);
  3753. r = (int)strlen(client_ri->request_uri);
  3754. ck_assert_int_eq(r, 3);
  3755. data_read = 0;
  3756. while (data_read < client_ri->content_length) {
  3757. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3758. if (r > 0) {
  3759. data_read += r;
  3760. }
  3761. }
  3762. /* Nothing left to read */
  3763. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3764. ck_assert_int_eq(r, 0);
  3765. mark_point();
  3766. mg_close_connection(client);
  3767. mark_point();
  3768. }
  3769. static void
  3770. minimal_http_client_impl(const char *server, uint16_t port, const char *uri)
  3771. {
  3772. minimal_http_https_client_impl(server, port, 0, uri);
  3773. }
  3774. #if !defined(NO_SSL)
  3775. static void
  3776. minimal_https_client_impl(const char *server, uint16_t port, const char *uri)
  3777. {
  3778. minimal_http_https_client_impl(server, port, 1, uri);
  3779. }
  3780. #endif
  3781. START_TEST(test_minimal_client)
  3782. {
  3783. mark_point();
  3784. /* Initialize the library */
  3785. mg_init_library(0);
  3786. mark_point();
  3787. /* Call a test client */
  3788. minimal_http_client_impl("192.30.253.113" /* www.github.com */,
  3789. 80,
  3790. "civetweb/civetweb/");
  3791. mark_point();
  3792. /* Un-initialize the library */
  3793. mg_exit_library();
  3794. mark_point();
  3795. }
  3796. END_TEST
  3797. START_TEST(test_minimal_tls_client)
  3798. {
  3799. mark_point();
  3800. #if !defined(NO_SSL)
  3801. /* Initialize the library */
  3802. mg_init_library(2);
  3803. mark_point();
  3804. /* Call a test client */
  3805. minimal_https_client_impl("192.30.253.113" /* www.github.com */,
  3806. 443,
  3807. "civetweb/civetweb/");
  3808. mark_point();
  3809. /* Un-initialize the library */
  3810. mg_exit_library();
  3811. #endif
  3812. mark_point();
  3813. }
  3814. END_TEST
  3815. static int
  3816. minimal_test_request_handler(struct mg_connection *conn, void *cbdata)
  3817. {
  3818. const char *msg = (const char *)cbdata;
  3819. unsigned long len = (unsigned long)strlen(msg);
  3820. mark_point();
  3821. mg_printf(conn,
  3822. "HTTP/1.1 200 OK\r\n"
  3823. "Content-Length: %lu\r\n"
  3824. "Content-Type: text/plain\r\n"
  3825. "Connection: close\r\n\r\n",
  3826. len);
  3827. mg_write(conn, msg, len);
  3828. mark_point();
  3829. return 200;
  3830. }
  3831. START_TEST(test_minimal_http_server_callback)
  3832. {
  3833. /* This test should ensure the minimum server example in
  3834. * docs/Embedding.md is still running. */
  3835. /* Server context handle */
  3836. struct mg_context *ctx;
  3837. mark_point();
  3838. /* Initialize the library */
  3839. mg_init_library(0);
  3840. /* Start the server */
  3841. ctx = test_mg_start(NULL, 0, NULL);
  3842. ck_assert(ctx != NULL);
  3843. /* Add some handler */
  3844. mg_set_request_handler(ctx,
  3845. "/hello",
  3846. minimal_test_request_handler,
  3847. (void *)"Hello world");
  3848. mg_set_request_handler(ctx,
  3849. "/8",
  3850. minimal_test_request_handler,
  3851. (void *)"Number eight");
  3852. /* Run the server for 15 seconds */
  3853. test_sleep(10);
  3854. /* Call a test client */
  3855. minimal_http_client_impl("127.0.0.1", 8080, "/hello");
  3856. /* Run the server for 15 seconds */
  3857. test_sleep(5);
  3858. /* Stop the server */
  3859. test_mg_stop(ctx);
  3860. /* Un-initialize the library */
  3861. mg_exit_library();
  3862. mark_point();
  3863. }
  3864. END_TEST
  3865. START_TEST(test_minimal_https_server_callback)
  3866. {
  3867. #if !defined(NO_SSL)
  3868. /* This test should show a HTTPS server with enhanced
  3869. * security settings.
  3870. *
  3871. * Articles:
  3872. * https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
  3873. *
  3874. * Scanners:
  3875. * https://securityheaders.io/
  3876. * https://www.htbridge.com/ssl/
  3877. * https://www.htbridge.com/websec/
  3878. * https://www.ssllabs.com/ssltest/
  3879. * https://www.qualys.com/forms/freescan/
  3880. */
  3881. /* Server context handle */
  3882. struct mg_context *ctx;
  3883. /* Server start parameters for HTTPS */
  3884. const char *OPTIONS[32];
  3885. int opt_idx = 0;
  3886. /* HTTPS port - required */
  3887. OPTIONS[opt_idx++] = "listening_ports";
  3888. OPTIONS[opt_idx++] = "8443s";
  3889. /* path to certificate file - required */
  3890. OPTIONS[opt_idx++] = "ssl_certificate";
  3891. OPTIONS[opt_idx++] = locate_ssl_cert();
  3892. #if defined(LOCAL_TEST) || defined(_WIN32)
  3893. /* Do not set this on Travis CI, since the build containers
  3894. * contain older SSL libraries */
  3895. /* set minimum SSL version to TLS 1.2 - recommended */
  3896. OPTIONS[opt_idx++] = "ssl_protocol_version";
  3897. OPTIONS[opt_idx++] = "4";
  3898. /* set some modern ciphers - recommended */
  3899. OPTIONS[opt_idx++] = "ssl_cipher_list";
  3900. OPTIONS[opt_idx++] = "ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS";
  3901. #endif
  3902. /* set "HTTPS only" header - recommended */
  3903. OPTIONS[opt_idx++] = "strict_transport_security_max_age";
  3904. OPTIONS[opt_idx++] = "31622400";
  3905. /* end of options - required */
  3906. OPTIONS[opt_idx] = NULL;
  3907. mark_point();
  3908. /* Initialize the library */
  3909. mg_init_library(0);
  3910. /* Start the server */
  3911. ctx = test_mg_start(NULL, 0, OPTIONS);
  3912. ck_assert(ctx != NULL);
  3913. /* Add some handler */
  3914. mg_set_request_handler(ctx,
  3915. "/hello",
  3916. minimal_test_request_handler,
  3917. (void *)"Hello world");
  3918. mg_set_request_handler(ctx,
  3919. "/8",
  3920. minimal_test_request_handler,
  3921. (void *)"Number eight");
  3922. /* Run the server for 15 seconds */
  3923. test_sleep(10);
  3924. /* Call a test client */
  3925. minimal_https_client_impl("127.0.0.1", 8443, "/hello");
  3926. /* Run the server for 15 seconds */
  3927. test_sleep(5);
  3928. /* Stop the server */
  3929. test_mg_stop(ctx);
  3930. /* Un-initialize the library */
  3931. mg_exit_library();
  3932. #endif
  3933. mark_point();
  3934. }
  3935. END_TEST
  3936. #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
  3937. Suite *
  3938. make_public_server_suite(void)
  3939. {
  3940. Suite *const suite = suite_create("PublicServer");
  3941. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  3942. TCase *const tcase_initlib = tcase_create("Init library");
  3943. TCase *const tcase_startthreads = tcase_create("Start threads");
  3944. TCase *const tcase_minimal_http_svr = tcase_create("Minimal HTTP Server");
  3945. TCase *const tcase_minimal_https_svr = tcase_create("Minimal HTTPS Server");
  3946. TCase *const tcase_minimal_http_cli = tcase_create("Minimal HTTP Client");
  3947. TCase *const tcase_minimal_https_cli = tcase_create("Minimal HTTPS Client");
  3948. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  3949. TCase *const tcase_startstophttp_ipv6 =
  3950. tcase_create("Start Stop HTTP Server IPv6");
  3951. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  3952. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  3953. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  3954. TCase *const tcase_storebody = tcase_create("Store Body");
  3955. TCase *const tcase_handle_form = tcase_create("Handle Form");
  3956. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  3957. TCase *const tcase_keep_alive = tcase_create("HTTP Keep Alive");
  3958. TCase *const tcase_error_handling = tcase_create("Error handling");
  3959. TCase *const tcase_error_log = tcase_create("Error logging");
  3960. TCase *const tcase_throttle = tcase_create("Limit speed");
  3961. TCase *const tcase_large_file = tcase_create("Large file");
  3962. TCase *const tcase_file_in_mem = tcase_create("File in memory");
  3963. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  3964. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  3965. suite_add_tcase(suite, tcase_checktestenv);
  3966. tcase_add_test(tcase_initlib, test_init_library);
  3967. tcase_set_timeout(tcase_initlib, civetweb_min_test_timeout);
  3968. suite_add_tcase(suite, tcase_initlib);
  3969. tcase_add_test(tcase_startthreads, test_threading);
  3970. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  3971. suite_add_tcase(suite, tcase_startthreads);
  3972. tcase_add_test(tcase_minimal_http_svr, test_minimal_http_server_callback);
  3973. tcase_set_timeout(tcase_minimal_http_svr, civetweb_min_server_test_timeout);
  3974. suite_add_tcase(suite, tcase_minimal_http_svr);
  3975. tcase_add_test(tcase_minimal_https_svr, test_minimal_https_server_callback);
  3976. tcase_set_timeout(tcase_minimal_https_svr,
  3977. civetweb_min_server_test_timeout);
  3978. suite_add_tcase(suite, tcase_minimal_https_svr);
  3979. tcase_add_test(tcase_minimal_http_cli, test_minimal_client);
  3980. tcase_set_timeout(tcase_minimal_http_cli, civetweb_min_server_test_timeout);
  3981. suite_add_tcase(suite, tcase_minimal_http_cli);
  3982. tcase_add_test(tcase_minimal_https_cli, test_minimal_tls_client);
  3983. tcase_set_timeout(tcase_minimal_https_cli,
  3984. civetweb_min_server_test_timeout);
  3985. suite_add_tcase(suite, tcase_minimal_https_cli);
  3986. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  3987. tcase_set_timeout(tcase_startstophttp, civetweb_min_server_test_timeout);
  3988. suite_add_tcase(suite, tcase_startstophttp);
  3989. tcase_add_test(tcase_startstophttp_ipv6,
  3990. test_mg_start_stop_http_server_ipv6);
  3991. tcase_set_timeout(tcase_startstophttp_ipv6,
  3992. civetweb_min_server_test_timeout);
  3993. suite_add_tcase(suite, tcase_startstophttp_ipv6);
  3994. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  3995. tcase_set_timeout(tcase_startstophttps, civetweb_min_server_test_timeout);
  3996. suite_add_tcase(suite, tcase_startstophttps);
  3997. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  3998. tcase_set_timeout(tcase_serverandclienttls,
  3999. civetweb_min_server_test_timeout);
  4000. suite_add_tcase(suite, tcase_serverandclienttls);
  4001. tcase_add_test(tcase_serverrequests, test_request_handlers);
  4002. tcase_set_timeout(tcase_serverrequests, civetweb_mid_server_test_timeout);
  4003. suite_add_tcase(suite, tcase_serverrequests);
  4004. tcase_add_test(tcase_storebody, test_mg_store_body);
  4005. tcase_set_timeout(tcase_storebody, civetweb_mid_server_test_timeout);
  4006. suite_add_tcase(suite, tcase_storebody);
  4007. tcase_add_test(tcase_handle_form, test_handle_form);
  4008. tcase_set_timeout(tcase_handle_form, civetweb_mid_server_test_timeout);
  4009. suite_add_tcase(suite, tcase_handle_form);
  4010. tcase_add_test(tcase_http_auth, test_http_auth);
  4011. tcase_set_timeout(tcase_http_auth, civetweb_min_server_test_timeout);
  4012. suite_add_tcase(suite, tcase_http_auth);
  4013. tcase_add_test(tcase_keep_alive, test_keep_alive);
  4014. tcase_set_timeout(tcase_keep_alive, civetweb_mid_server_test_timeout);
  4015. suite_add_tcase(suite, tcase_keep_alive);
  4016. tcase_add_test(tcase_error_handling, test_error_handling);
  4017. tcase_set_timeout(tcase_error_handling, civetweb_mid_server_test_timeout);
  4018. suite_add_tcase(suite, tcase_error_handling);
  4019. tcase_add_test(tcase_error_log, test_error_log_file);
  4020. tcase_set_timeout(tcase_error_log, civetweb_mid_server_test_timeout);
  4021. suite_add_tcase(suite, tcase_error_log);
  4022. tcase_add_test(tcase_throttle, test_throttle);
  4023. tcase_set_timeout(tcase_throttle, civetweb_mid_server_test_timeout);
  4024. suite_add_tcase(suite, tcase_throttle);
  4025. tcase_add_test(tcase_large_file, test_large_file);
  4026. tcase_set_timeout(tcase_large_file, civetweb_mid_server_test_timeout);
  4027. suite_add_tcase(suite, tcase_large_file);
  4028. tcase_add_test(tcase_file_in_mem, test_file_in_memory);
  4029. tcase_set_timeout(tcase_file_in_mem, civetweb_mid_server_test_timeout);
  4030. suite_add_tcase(suite, tcase_file_in_mem);
  4031. return suite;
  4032. }
  4033. #endif
  4034. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  4035. /* Used to debug test cases without using the check framework */
  4036. /* Build command for Linux:
  4037. gcc test/public_server.c src/civetweb.c -I include/ -I test/ -l pthread -l dl -D
  4038. LOCAL_TEST -D REPLACE_CHECK_FOR_LOCAL_DEBUGGING -D MAIN_PUBLIC_SERVER=main
  4039. */
  4040. static int chk_ok = 0;
  4041. static int chk_failed = 0;
  4042. void
  4043. MAIN_PUBLIC_SERVER(void)
  4044. {
  4045. unsigned f_avail = mg_check_feature(0xFF);
  4046. unsigned f_ret = mg_init_library(f_avail);
  4047. ck_assert_uint_eq(f_ret, f_avail);
  4048. test_the_test_environment(0);
  4049. test_threading(0);
  4050. test_minimal_client(0);
  4051. test_mg_start_stop_http_server(0);
  4052. test_mg_start_stop_https_server(0);
  4053. test_request_handlers(0);
  4054. test_mg_store_body(0);
  4055. test_mg_server_and_client_tls(0);
  4056. test_handle_form(0);
  4057. test_http_auth(0);
  4058. test_keep_alive(0);
  4059. test_error_handling(0);
  4060. test_error_log_file(0);
  4061. test_throttle(0);
  4062. test_large_file(0);
  4063. test_file_in_memory(0);
  4064. mg_exit_library();
  4065. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  4066. }
  4067. void
  4068. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  4069. {
  4070. va_list va;
  4071. va_start(va, expr);
  4072. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  4073. vfprintf(stderr, expr, va);
  4074. fprintf(stderr, "\n\n");
  4075. va_end(va);
  4076. chk_failed++;
  4077. }
  4078. void
  4079. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  4080. {
  4081. va_list va;
  4082. if (cond) {
  4083. chk_ok++;
  4084. return;
  4085. }
  4086. va_start(va, expr);
  4087. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  4088. vfprintf(stderr, expr, va);
  4089. fprintf(stderr, "\n\n");
  4090. va_end(va);
  4091. chk_failed++;
  4092. }
  4093. void
  4094. _mark_point(const char *file, int line)
  4095. {
  4096. chk_ok++;
  4097. }
  4098. void
  4099. tcase_fn_start(const char *fname, const char *file, int line)
  4100. {
  4101. }
  4102. void suite_add_tcase(Suite *s, TCase *tc){};
  4103. void _tcase_add_test(TCase *tc,
  4104. TFun tf,
  4105. const char *fname,
  4106. int _signal,
  4107. int allowed_exit_value,
  4108. int start,
  4109. int end){};
  4110. TCase *
  4111. tcase_create(const char *name)
  4112. {
  4113. return NULL;
  4114. };
  4115. Suite *
  4116. suite_create(const char *name)
  4117. {
  4118. return NULL;
  4119. };
  4120. void tcase_set_timeout(TCase *tc, double timeout){};
  4121. #endif