public_server.c 136 KB

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