public_server.c 134 KB

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