public_server.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867
  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(client_conn != NULL);
  352. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  376. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  402. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  423. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  540. ck_assert_str_eq(client_err, "");
  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(client_conn == NULL);
  634. ck_assert_str_ne(client_err, "");
  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(client_conn != NULL);
  645. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  1391. ck_assert_str_eq(ebuf, "");
  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(client_conn != NULL);
  1409. ck_assert_str_eq(ebuf, "");
  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(client_conn != NULL);
  1430. ck_assert_str_eq(ebuf, "");
  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(client_conn != NULL);
  2577. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  2603. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  2678. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  2715. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  2752. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  2771. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  2825. ck_assert_str_eq(client_err, "");
  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(client_conn != NULL);
  2948. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2949. client_res =
  2950. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2951. ck_assert_int_ge(client_res, 0);
  2952. ck_assert_str_eq(client_err, "");
  2953. client_ri = mg_get_request_info(client_conn);
  2954. ck_assert(client_ri != NULL);
  2955. ck_assert_str_eq(client_ri->local_uri, "404");
  2956. mg_close_connection(client_conn);
  2957. test_sleep(1);
  2958. /* Create an error.htm file */
  2959. f = fopen("error.htm", "wt");
  2960. ck_assert(f != NULL);
  2961. (void)fprintf(f, "err-all");
  2962. (void)fclose(f);
  2963. /* Ask for something not existing - should get error.htm */
  2964. memset(client_err, 0, sizeof(client_err));
  2965. client_conn =
  2966. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2967. ck_assert(client_conn != NULL);
  2968. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2969. client_res =
  2970. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2971. ck_assert_int_ge(client_res, 0);
  2972. ck_assert_str_eq(client_err, "");
  2973. client_ri = mg_get_request_info(client_conn);
  2974. ck_assert(client_ri != NULL);
  2975. ck_assert_str_eq(client_ri->local_uri, "200");
  2976. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2977. mg_close_connection(client_conn);
  2978. ck_assert_int_eq(client_res, 7);
  2979. client_err[8] = 0;
  2980. ck_assert_str_eq(client_err, "err-all");
  2981. test_sleep(1);
  2982. /* Create an error4xx.htm file */
  2983. f = fopen("error4xx.htm", "wt");
  2984. ck_assert(f != NULL);
  2985. (void)fprintf(f, "err-4xx");
  2986. (void)fclose(f);
  2987. /* Ask for something not existing - should get error4xx.htm */
  2988. memset(client_err, 0, sizeof(client_err));
  2989. client_conn =
  2990. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2991. ck_assert(client_conn != NULL);
  2992. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2993. client_res =
  2994. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2995. ck_assert_int_ge(client_res, 0);
  2996. ck_assert_str_eq(client_err, "");
  2997. client_ri = mg_get_request_info(client_conn);
  2998. ck_assert(client_ri != NULL);
  2999. ck_assert_str_eq(client_ri->local_uri, "200");
  3000. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3001. mg_close_connection(client_conn);
  3002. ck_assert_int_eq(client_res, 7);
  3003. client_err[8] = 0;
  3004. ck_assert_str_eq(client_err, "err-4xx");
  3005. test_sleep(1);
  3006. /* Create an error404.htm file */
  3007. f = fopen("error404.htm", "wt");
  3008. ck_assert(f != NULL);
  3009. (void)fprintf(f, "err-404");
  3010. (void)fclose(f);
  3011. /* Ask for something not existing - should get error404.htm */
  3012. memset(client_err, 0, sizeof(client_err));
  3013. client_conn =
  3014. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3015. ck_assert(client_conn != NULL);
  3016. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  3017. client_res =
  3018. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3019. ck_assert_int_ge(client_res, 0);
  3020. ck_assert_str_eq(client_err, "");
  3021. client_ri = mg_get_request_info(client_conn);
  3022. ck_assert(client_ri != NULL);
  3023. ck_assert_str_eq(client_ri->local_uri, "200");
  3024. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3025. mg_close_connection(client_conn);
  3026. ck_assert_int_eq(client_res, 7);
  3027. client_err[8] = 0;
  3028. ck_assert_str_eq(client_err, "err-404");
  3029. test_sleep(1);
  3030. /* Ask in a malformed way - should get error4xx.htm */
  3031. memset(client_err, 0, sizeof(client_err));
  3032. client_conn =
  3033. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3034. ck_assert(client_conn != NULL);
  3035. mg_printf(client_conn, "Gimme some file!\r\n\r\n");
  3036. client_res =
  3037. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3038. ck_assert_int_ge(client_res, 0);
  3039. ck_assert_str_eq(client_err, "");
  3040. client_ri = mg_get_request_info(client_conn);
  3041. ck_assert(client_ri != NULL);
  3042. ck_assert_str_eq(client_ri->local_uri, "200");
  3043. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3044. mg_close_connection(client_conn);
  3045. ck_assert_int_eq(client_res, 7);
  3046. client_err[8] = 0;
  3047. ck_assert_str_eq(client_err, "err-4xx");
  3048. test_sleep(1);
  3049. /* Remove all error files created by this test */
  3050. (void)remove("error.htm");
  3051. (void)remove("error4xx.htm");
  3052. (void)remove("error404.htm");
  3053. /* Stop the server */
  3054. test_mg_stop(ctx);
  3055. /* HTTP 1.1 GET request - must not work, since server is already stopped */
  3056. memset(client_err, 0, sizeof(client_err));
  3057. client_conn =
  3058. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3059. ck_assert(client_conn == NULL);
  3060. ck_assert_str_ne(client_err, "");
  3061. test_sleep(1);
  3062. mark_point();
  3063. }
  3064. END_TEST
  3065. START_TEST(test_error_log_file)
  3066. {
  3067. /* Server var */
  3068. struct mg_context *ctx;
  3069. const char *OPTIONS[32];
  3070. int opt_cnt = 0;
  3071. /* Client var */
  3072. struct mg_connection *client;
  3073. char client_err_buf[256];
  3074. char client_data_buf[256];
  3075. const struct mg_request_info *client_ri;
  3076. /* File content check var */
  3077. FILE *f;
  3078. char buf[1024];
  3079. int len, ok;
  3080. mark_point();
  3081. /* Set options and start server */
  3082. OPTIONS[opt_cnt++] = "listening_ports";
  3083. OPTIONS[opt_cnt++] = "8080";
  3084. OPTIONS[opt_cnt++] = "error_log_file";
  3085. OPTIONS[opt_cnt++] = "error.log";
  3086. OPTIONS[opt_cnt++] = "access_log_file";
  3087. OPTIONS[opt_cnt++] = "access.log";
  3088. #if !defined(NO_FILES)
  3089. OPTIONS[opt_cnt++] = "document_root";
  3090. OPTIONS[opt_cnt++] = ".";
  3091. #endif
  3092. OPTIONS[opt_cnt] = NULL;
  3093. ctx = test_mg_start(NULL, 0, OPTIONS);
  3094. ck_assert(ctx != NULL);
  3095. /* Remove log files (they may exist from previous incomplete runs of
  3096. * this test) */
  3097. (void)remove("error.log");
  3098. (void)remove("access.log");
  3099. /* connect client */
  3100. memset(client_err_buf, 0, sizeof(client_err_buf));
  3101. memset(client_data_buf, 0, sizeof(client_data_buf));
  3102. client = mg_download("127.0.0.1",
  3103. 8080,
  3104. 0,
  3105. client_err_buf,
  3106. sizeof(client_err_buf),
  3107. "GET /not_existing_file.ext HTTP/1.0\r\n\r\n");
  3108. ck_assert(ctx != NULL);
  3109. ck_assert_str_eq(client_err_buf, "");
  3110. client_ri = mg_get_request_info(client);
  3111. ck_assert(client_ri != NULL);
  3112. ck_assert_str_eq(client_ri->local_uri, "404");
  3113. /* Close the client connection */
  3114. mg_close_connection(client);
  3115. /* Stop the server */
  3116. test_mg_stop(ctx);
  3117. /* Check access.log */
  3118. memset(buf, 0, sizeof(buf));
  3119. f = fopen("access.log", "r");
  3120. ck_assert_msg(f != NULL, "Cannot open access log file");
  3121. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3122. (void)fclose(f);
  3123. ck_assert_msg(ok, "Cannot read access log file");
  3124. len = (int)strlen(buf);
  3125. ck_assert_int_gt(len, 0);
  3126. ok = (NULL != strstr(buf, "not_existing_file.ext"));
  3127. ck_assert_msg(ok, "Did not find uri in access log file");
  3128. ok = (NULL != strstr(buf, "404"));
  3129. ck_assert_msg(ok, "Did not find HTTP status code in access log file");
  3130. /* Check error.log */
  3131. memset(buf, 0, sizeof(buf));
  3132. f = fopen("error.log", "r");
  3133. if (f) {
  3134. (void)fgets(buf, sizeof(buf) - 1, f);
  3135. fclose(f);
  3136. }
  3137. ck_assert_msg(f == NULL,
  3138. "Should not create error log file on 404, but got [%s]",
  3139. buf);
  3140. /* Remove log files */
  3141. (void)remove("error.log");
  3142. (void)remove("access.log");
  3143. /* Start server with bad options */
  3144. ck_assert_str_eq(OPTIONS[0], "listening_ports");
  3145. OPTIONS[1] = "bad port syntax";
  3146. ctx = test_mg_start(NULL, 0, OPTIONS);
  3147. ck_assert_msg(
  3148. ctx == NULL,
  3149. "Should not be able to start server with bad port configuration");
  3150. /* Check access.log */
  3151. memset(buf, 0, sizeof(buf));
  3152. f = fopen("access.log", "r");
  3153. if (f) {
  3154. (void)fgets(buf, sizeof(buf) - 1, f);
  3155. fclose(f);
  3156. }
  3157. ck_assert_msg(
  3158. f == NULL,
  3159. "Should not create access log file if start fails, but got [%s]",
  3160. buf);
  3161. /* Check error.log */
  3162. memset(buf, 0, sizeof(buf));
  3163. f = fopen("error.log", "r");
  3164. ck_assert_msg(f != NULL, "Cannot open access log file");
  3165. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3166. (void)fclose(f);
  3167. ck_assert_msg(ok, "Cannot read access log file");
  3168. len = (int)strlen(buf);
  3169. ck_assert_int_gt(len, 0);
  3170. ok = (NULL != strstr(buf, "port"));
  3171. ck_assert_msg(ok, "Did not find port as error reason in error log file");
  3172. /* Remove log files */
  3173. (void)remove("error.log");
  3174. (void)remove("access.log");
  3175. mark_point();
  3176. }
  3177. END_TEST
  3178. static int
  3179. test_throttle_begin_request(struct mg_connection *conn)
  3180. {
  3181. const struct mg_request_info *ri;
  3182. long unsigned len = 1024 * 10;
  3183. const char *block = "0123456789";
  3184. unsigned long i, blocklen;
  3185. ck_assert(conn != NULL);
  3186. ri = mg_get_request_info(conn);
  3187. ck_assert(ri != NULL);
  3188. ck_assert_str_eq(ri->request_method, "GET");
  3189. ck_assert_str_eq(ri->request_uri, "/throttle");
  3190. ck_assert_str_eq(ri->local_uri, "/throttle");
  3191. ck_assert_str_eq(ri->http_version, "1.0");
  3192. ck_assert_str_eq(ri->query_string, "q");
  3193. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3194. mg_printf(conn,
  3195. "HTTP/1.1 200 OK\r\n"
  3196. "Content-Length: %lu\r\n"
  3197. "Connection: close\r\n\r\n",
  3198. len);
  3199. blocklen = (unsigned long)strlen(block);
  3200. for (i = 0; i < len; i += blocklen) {
  3201. mg_write(conn, block, blocklen);
  3202. }
  3203. mark_point();
  3204. return 987; /* Not a valid HTTP response code,
  3205. * but it should be written to the log and passed to
  3206. * end_request. */
  3207. }
  3208. static void
  3209. test_throttle_end_request(const struct mg_connection *conn,
  3210. int reply_status_code)
  3211. {
  3212. const struct mg_request_info *ri;
  3213. ck_assert(conn != NULL);
  3214. ri = mg_get_request_info(conn);
  3215. ck_assert(ri != NULL);
  3216. ck_assert_str_eq(ri->request_method, "GET");
  3217. ck_assert_str_eq(ri->request_uri, "/throttle");
  3218. ck_assert_str_eq(ri->local_uri, "/throttle");
  3219. ck_assert_str_eq(ri->http_version, "1.0");
  3220. ck_assert_str_eq(ri->query_string, "q");
  3221. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3222. ck_assert_int_eq(reply_status_code, 987);
  3223. }
  3224. START_TEST(test_throttle)
  3225. {
  3226. /* Server var */
  3227. struct mg_context *ctx;
  3228. struct mg_callbacks callbacks;
  3229. const char *OPTIONS[32];
  3230. int opt_cnt = 0;
  3231. /* Client var */
  3232. struct mg_connection *client;
  3233. char client_err_buf[256];
  3234. char client_data_buf[256];
  3235. const struct mg_request_info *client_ri;
  3236. /* timing test */
  3237. int r, data_read;
  3238. time_t t0, t1;
  3239. double dt;
  3240. mark_point();
  3241. /* Set options and start server */
  3242. #if !defined(NO_FILES)
  3243. OPTIONS[opt_cnt++] = "document_root";
  3244. OPTIONS[opt_cnt++] = ".";
  3245. #endif
  3246. OPTIONS[opt_cnt++] = "listening_ports";
  3247. OPTIONS[opt_cnt++] = "8080";
  3248. OPTIONS[opt_cnt++] = "throttle";
  3249. OPTIONS[opt_cnt++] = "*=1k";
  3250. OPTIONS[opt_cnt] = NULL;
  3251. memset(&callbacks, 0, sizeof(callbacks));
  3252. callbacks.begin_request = test_throttle_begin_request;
  3253. callbacks.end_request = test_throttle_end_request;
  3254. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3255. ck_assert(ctx != NULL);
  3256. /* connect client */
  3257. memset(client_err_buf, 0, sizeof(client_err_buf));
  3258. memset(client_data_buf, 0, sizeof(client_data_buf));
  3259. strcpy(client_err_buf, "reset-content");
  3260. client = mg_download("127.0.0.1",
  3261. 8080,
  3262. 0,
  3263. client_err_buf,
  3264. sizeof(client_err_buf),
  3265. "GET /throttle?q HTTP/1.0\r\n\r\n");
  3266. ck_assert(ctx != NULL);
  3267. ck_assert_str_eq(client_err_buf, "");
  3268. client_ri = mg_get_request_info(client);
  3269. ck_assert(client_ri != NULL);
  3270. ck_assert_str_eq(client_ri->local_uri, "200");
  3271. ck_assert_int_eq(client_ri->content_length, 1024 * 10);
  3272. data_read = 0;
  3273. t0 = time(NULL);
  3274. while (data_read < client_ri->content_length) {
  3275. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3276. ck_assert_int_ge(r, 0);
  3277. data_read += r;
  3278. }
  3279. t1 = time(NULL);
  3280. dt = difftime(t1, t0) * 1000.0; /* Elapsed time in ms - in most systems
  3281. * only with second resolution */
  3282. /* Time estimation: Data size is 10 kB, with 1 kB/s speed limit.
  3283. * The first block (1st kB) is transferred immediately, the second
  3284. * block (2nd kB) one second later, the third block (3rd kB) two
  3285. * seconds later, .. the last block (10th kB) nine seconds later.
  3286. * The resolution of time measurement using the "time" C library
  3287. * function is 1 second, so we should add +/- one second tolerance.
  3288. * Thus, download of 10 kB with 1 kB/s should not be faster than
  3289. * 8 seconds. */
  3290. /* Check if there are at least 8 seconds */
  3291. ck_assert_int_ge((int)dt, 8 * 1000);
  3292. /* Nothing left to read */
  3293. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3294. ck_assert_int_eq(r, 0);
  3295. /* Close the client connection */
  3296. mg_close_connection(client);
  3297. /* Stop the server */
  3298. test_mg_stop(ctx);
  3299. mark_point();
  3300. }
  3301. END_TEST
  3302. START_TEST(test_init_library)
  3303. {
  3304. unsigned f_avail, f_ret;
  3305. mark_point();
  3306. f_avail = mg_check_feature(0xFF);
  3307. f_ret = mg_init_library(f_avail);
  3308. ck_assert_uint_eq(f_ret, f_avail);
  3309. }
  3310. END_TEST
  3311. #define LARGE_FILE_SIZE (1024 * 1024 * 10)
  3312. static int
  3313. test_large_file_begin_request(struct mg_connection *conn)
  3314. {
  3315. const struct mg_request_info *ri;
  3316. long unsigned len = LARGE_FILE_SIZE;
  3317. const char *block = "0123456789";
  3318. uint64_t i;
  3319. size_t blocklen;
  3320. ck_assert(conn != NULL);
  3321. ri = mg_get_request_info(conn);
  3322. ck_assert(ri != NULL);
  3323. ck_assert_str_eq(ri->request_method, "GET");
  3324. ck_assert_str_eq(ri->http_version, "1.1");
  3325. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3326. ck_assert_ptr_eq(ri->query_string, NULL);
  3327. ck_assert_ptr_ne(ri->local_uri, NULL);
  3328. mg_printf(conn,
  3329. "HTTP/1.1 200 OK\r\n"
  3330. "Content-Length: %lu\r\n"
  3331. "Connection: close\r\n\r\n",
  3332. len);
  3333. blocklen = strlen(block);
  3334. for (i = 0; i < len; i += blocklen) {
  3335. mg_write(conn, block, blocklen);
  3336. }
  3337. mark_point();
  3338. return 200;
  3339. }
  3340. START_TEST(test_large_file)
  3341. {
  3342. /* Server var */
  3343. struct mg_context *ctx;
  3344. struct mg_callbacks callbacks;
  3345. const char *OPTIONS[32];
  3346. int opt_cnt = 0;
  3347. #if !defined(NO_SSL)
  3348. const char *ssl_cert = locate_ssl_cert();
  3349. #endif
  3350. char errmsg[256] = {0};
  3351. /* Client var */
  3352. struct mg_connection *client;
  3353. char client_err_buf[256];
  3354. char client_data_buf[256];
  3355. const struct mg_request_info *client_ri;
  3356. int64_t data_read;
  3357. int r;
  3358. int retry, retry_ok_cnt, retry_fail_cnt;
  3359. mark_point();
  3360. /* Set options and start server */
  3361. #if !defined(NO_FILES)
  3362. OPTIONS[opt_cnt++] = "document_root";
  3363. OPTIONS[opt_cnt++] = ".";
  3364. #endif
  3365. #if defined(NO_SSL)
  3366. OPTIONS[opt_cnt++] = "listening_ports";
  3367. OPTIONS[opt_cnt++] = "8080";
  3368. #else
  3369. OPTIONS[opt_cnt++] = "listening_ports";
  3370. OPTIONS[opt_cnt++] = "8443s";
  3371. OPTIONS[opt_cnt++] = "ssl_certificate";
  3372. OPTIONS[opt_cnt++] = ssl_cert;
  3373. #ifdef __MACH__
  3374. /* The Apple builds on Travis CI seem to have problems with TLS1.x
  3375. * Allow SSLv3 and TLS */
  3376. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3377. OPTIONS[opt_cnt++] = "2";
  3378. #else
  3379. /* The Linux builds on Travis CI work fine with TLS1.2 */
  3380. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3381. OPTIONS[opt_cnt++] = "4";
  3382. #endif
  3383. ck_assert(ssl_cert != NULL);
  3384. #endif
  3385. OPTIONS[opt_cnt] = NULL;
  3386. memset(&callbacks, 0, sizeof(callbacks));
  3387. callbacks.begin_request = test_large_file_begin_request;
  3388. callbacks.log_message = log_msg_func;
  3389. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  3390. ck_assert_str_eq(errmsg, "");
  3391. ck_assert(ctx != NULL);
  3392. /* Try downloading several times */
  3393. retry_ok_cnt = 0;
  3394. retry_fail_cnt = 0;
  3395. for (retry = 0; retry < 3; retry++) {
  3396. int fail = 0;
  3397. /* connect client */
  3398. memset(client_err_buf, 0, sizeof(client_err_buf));
  3399. memset(client_data_buf, 0, sizeof(client_data_buf));
  3400. client =
  3401. mg_download("127.0.0.1",
  3402. #if defined(NO_SSL)
  3403. 8080,
  3404. 0,
  3405. #else
  3406. 8443,
  3407. 1,
  3408. #endif
  3409. client_err_buf,
  3410. sizeof(client_err_buf),
  3411. "GET /large.file HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3412. ck_assert(client != NULL);
  3413. ck_assert_str_eq(client_err_buf, "");
  3414. client_ri = mg_get_request_info(client);
  3415. ck_assert(client_ri != NULL);
  3416. ck_assert_str_eq(client_ri->local_uri, "200");
  3417. ck_assert_int_eq(client_ri->content_length, LARGE_FILE_SIZE);
  3418. data_read = 0;
  3419. while (data_read < client_ri->content_length) {
  3420. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3421. if (r < 0) {
  3422. fail = 1;
  3423. break;
  3424. };
  3425. data_read += r;
  3426. }
  3427. /* Nothing left to read */
  3428. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3429. if (fail) {
  3430. ck_assert_int_eq(r, -1);
  3431. retry_fail_cnt++;
  3432. } else {
  3433. ck_assert_int_eq(r, 0);
  3434. retry_ok_cnt++;
  3435. }
  3436. /* Close the client connection */
  3437. mg_close_connection(client);
  3438. }
  3439. #if defined(_WIN32)
  3440. // TODO: Check this problem on AppVeyor
  3441. // ck_assert_int_le(retry_fail_cnt, 2);
  3442. // ck_assert_int_ge(retry_ok_cnt, 1);
  3443. #else
  3444. ck_assert_int_eq(retry_fail_cnt, 0);
  3445. ck_assert_int_eq(retry_ok_cnt, 3);
  3446. #endif
  3447. /* Stop the server */
  3448. test_mg_stop(ctx);
  3449. mark_point();
  3450. }
  3451. END_TEST
  3452. static int test_mg_store_body_con_len = 20000;
  3453. static int
  3454. test_mg_store_body_put_delete_handler(struct mg_connection *conn, void *ignored)
  3455. {
  3456. char path[4096] = {0};
  3457. const struct mg_request_info *info = mg_get_request_info(conn);
  3458. int64_t rc;
  3459. (void)ignored;
  3460. mark_point();
  3461. sprintf(path, "./%s", info->local_uri);
  3462. rc = mg_store_body(conn, path);
  3463. ck_assert_int_eq(test_mg_store_body_con_len, rc);
  3464. if (rc < 0) {
  3465. mg_printf(conn,
  3466. "HTTP/1.1 500 Internal Server Error\r\n"
  3467. "Content-Type:text/plain;charset=UTF-8\r\n"
  3468. "Connection:close\r\n\r\n"
  3469. "%s (ret: %ld)\n",
  3470. path,
  3471. (long)rc);
  3472. mg_close_connection(conn);
  3473. /* Debug output for tests */
  3474. printf("mg_store_body(%s) failed (ret: %ld)\n", path, (long)rc);
  3475. return 500;
  3476. }
  3477. mg_printf(conn,
  3478. "HTTP/1.1 200 OK\r\n"
  3479. "Content-Type:text/plain;charset=UTF-8\r\n"
  3480. "Connection:close\r\n\r\n"
  3481. "%s OK (%ld bytes saved)\n",
  3482. path,
  3483. (long)rc);
  3484. mg_close_connection(conn);
  3485. /* Debug output for tests */
  3486. printf("mg_store_body(%s) OK (%ld bytes)\n", path, (long)rc);
  3487. mark_point();
  3488. return 200;
  3489. }
  3490. static int
  3491. test_mg_store_body_begin_request_callback(struct mg_connection *conn)
  3492. {
  3493. const struct mg_request_info *info = mg_get_request_info(conn);
  3494. mark_point();
  3495. /* Debug output for tests */
  3496. printf("test_mg_store_body_begin_request_callback called (%s)\n",
  3497. info->request_method);
  3498. if ((strcmp(info->request_method, "PUT") == 0)
  3499. || (strcmp(info->request_method, "DELETE") == 0)) {
  3500. return test_mg_store_body_put_delete_handler(conn, NULL);
  3501. }
  3502. mark_point();
  3503. return 0;
  3504. }
  3505. START_TEST(test_mg_store_body)
  3506. {
  3507. /* Client data */
  3508. char client_err_buf[256];
  3509. char client_data_buf[1024];
  3510. struct mg_connection *client;
  3511. const struct mg_request_info *client_ri;
  3512. int r;
  3513. char check_data[256];
  3514. char *check_ptr;
  3515. char errmsg[256] = {0};
  3516. /* Server context handle */
  3517. struct mg_context *ctx;
  3518. struct mg_callbacks callbacks;
  3519. const char *options[] = {
  3520. #if !defined(NO_FILES)
  3521. "document_root",
  3522. ".",
  3523. #endif
  3524. #if !defined(NO_CACHING)
  3525. "static_file_max_age",
  3526. "0",
  3527. #endif
  3528. "listening_ports",
  3529. "127.0.0.1:8082",
  3530. "num_threads",
  3531. "1",
  3532. NULL
  3533. };
  3534. mark_point();
  3535. memset(&callbacks, 0, sizeof(callbacks));
  3536. callbacks.begin_request = test_mg_store_body_begin_request_callback;
  3537. callbacks.log_message = log_msg_func;
  3538. /* Initialize the library */
  3539. mg_init_library(0);
  3540. /* Start the server */
  3541. ctx = mg_start(&callbacks, (void *)errmsg, options);
  3542. ck_assert_str_eq(errmsg, "");
  3543. ck_assert(ctx != NULL);
  3544. /* Run the server for 15 seconds */
  3545. test_sleep(15);
  3546. /* Call a test client */
  3547. client = mg_connect_client(
  3548. "127.0.0.1", 8082, 0, client_err_buf, sizeof(client_err_buf));
  3549. ck_assert(client != NULL);
  3550. ck_assert_str_eq(client_err_buf, "");
  3551. mg_printf(client,
  3552. "PUT /%s HTTP/1.0\r\nContent-Length: %i\r\n\r\n",
  3553. "test_file_name.txt",
  3554. test_mg_store_body_con_len);
  3555. r = 0;
  3556. while (r < test_mg_store_body_con_len) {
  3557. int l = mg_write(client, "1234567890", 10);
  3558. ck_assert_int_eq(l, 10);
  3559. r += 10;
  3560. }
  3561. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3562. ck_assert_int_ge(r, 0);
  3563. ck_assert_str_eq(client_err_buf, "");
  3564. client_ri = mg_get_request_info(client);
  3565. ck_assert(client_ri != NULL);
  3566. /* Response must be 200 OK */
  3567. ck_assert_ptr_ne(client_ri->request_uri, NULL);
  3568. ck_assert_str_eq(client_ri->request_uri, "200");
  3569. /* Read PUT response */
  3570. r = mg_read(client, client_data_buf, sizeof(client_data_buf) - 1);
  3571. ck_assert_int_gt(r, 0);
  3572. client_data_buf[r] = 0;
  3573. sprintf(check_data, "(%i bytes saved)", test_mg_store_body_con_len);
  3574. check_ptr = strstr(client_data_buf, check_data);
  3575. ck_assert_ptr_ne(check_ptr, NULL);
  3576. mg_close_connection(client);
  3577. /* Run the server for 5 seconds */
  3578. test_sleep(5);
  3579. /* Stop the server */
  3580. test_mg_stop(ctx);
  3581. /* Un-initialize the library */
  3582. mg_exit_library();
  3583. mark_point();
  3584. }
  3585. END_TEST
  3586. #if defined(MG_USE_OPEN_FILE) && !defined(NO_FILES)
  3587. #define FILE_IN_MEM_SIZE (1024 * 100)
  3588. static char *file_in_mem_data;
  3589. static const char *
  3590. test_file_in_memory_open_file(const struct mg_connection *conn,
  3591. const char *file_path,
  3592. size_t *file_size)
  3593. {
  3594. (void)conn;
  3595. if (strcmp(file_path, "./file_in_mem") == 0) {
  3596. /* File is in memory */
  3597. *file_size = FILE_IN_MEM_SIZE;
  3598. return file_in_mem_data;
  3599. } else {
  3600. /* File is not in memory */
  3601. return NULL;
  3602. }
  3603. }
  3604. START_TEST(test_file_in_memory)
  3605. {
  3606. /* Server var */
  3607. struct mg_context *ctx;
  3608. struct mg_callbacks callbacks;
  3609. const char *OPTIONS[32];
  3610. int opt_cnt = 0;
  3611. #if !defined(NO_SSL)
  3612. const char *ssl_cert = locate_ssl_cert();
  3613. #endif
  3614. /* Client var */
  3615. struct mg_connection *client;
  3616. char client_err_buf[256];
  3617. char client_data_buf[256];
  3618. const struct mg_request_info *client_ri;
  3619. int64_t data_read;
  3620. int r, i;
  3621. /* Prepare test data */
  3622. file_in_mem_data = (char *)malloc(FILE_IN_MEM_SIZE);
  3623. ck_assert_ptr_ne(file_in_mem_data, NULL);
  3624. for (r = 0; r < FILE_IN_MEM_SIZE; r++) {
  3625. file_in_mem_data[r] = (char)(r);
  3626. }
  3627. /* Set options and start server */
  3628. OPTIONS[opt_cnt++] = "document_root";
  3629. OPTIONS[opt_cnt++] = ".";
  3630. #if defined(NO_SSL)
  3631. OPTIONS[opt_cnt++] = "listening_ports";
  3632. OPTIONS[opt_cnt++] = "8080";
  3633. #else
  3634. OPTIONS[opt_cnt++] = "listening_ports";
  3635. OPTIONS[opt_cnt++] = "8443s";
  3636. OPTIONS[opt_cnt++] = "ssl_certificate";
  3637. OPTIONS[opt_cnt++] = ssl_cert;
  3638. ck_assert(ssl_cert != NULL);
  3639. #endif
  3640. OPTIONS[opt_cnt] = NULL;
  3641. memset(&callbacks, 0, sizeof(callbacks));
  3642. callbacks.open_file = test_file_in_memory_open_file;
  3643. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3644. ck_assert(ctx != NULL);
  3645. /* connect client */
  3646. memset(client_err_buf, 0, sizeof(client_err_buf));
  3647. memset(client_data_buf, 0, sizeof(client_data_buf));
  3648. client =
  3649. mg_download("127.0.0.1",
  3650. #if defined(NO_SSL)
  3651. 8080,
  3652. 0,
  3653. #else
  3654. 8443,
  3655. 1,
  3656. #endif
  3657. client_err_buf,
  3658. sizeof(client_err_buf),
  3659. "GET /file_in_mem HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3660. ck_assert(client != NULL);
  3661. ck_assert_str_eq(client_err_buf, "");
  3662. client_ri = mg_get_request_info(client);
  3663. ck_assert(client_ri != NULL);
  3664. ck_assert_str_eq(client_ri->local_uri, "200");
  3665. ck_assert_int_eq(client_ri->content_length, FILE_IN_MEM_SIZE);
  3666. data_read = 0;
  3667. while (data_read < client_ri->content_length) {
  3668. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3669. if (r > 0) {
  3670. for (i = 0; i < r; i++) {
  3671. ck_assert_int_eq((int)client_data_buf[i],
  3672. (int)file_in_mem_data[data_read + i]);
  3673. }
  3674. data_read += r;
  3675. }
  3676. }
  3677. /* Nothing left to read */
  3678. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3679. ck_assert_int_eq(r, 0);
  3680. /* Close the client connection */
  3681. mg_close_connection(client);
  3682. /* Stop the server */
  3683. test_mg_stop(ctx);
  3684. /* Free test data */
  3685. free(file_in_mem_data);
  3686. file_in_mem_data = NULL;
  3687. }
  3688. END_TEST
  3689. #else /* defined(MG_USE_OPEN_FILE) */
  3690. START_TEST(test_file_in_memory)
  3691. {
  3692. mark_point();
  3693. }
  3694. END_TEST
  3695. #endif
  3696. static void
  3697. minimal_http_https_client_impl(const char *server,
  3698. uint16_t port,
  3699. int use_ssl,
  3700. const char *uri)
  3701. {
  3702. /* Client var */
  3703. struct mg_connection *client;
  3704. char client_err_buf[256];
  3705. char client_data_buf[256];
  3706. const struct mg_request_info *client_ri;
  3707. int64_t data_read;
  3708. int r;
  3709. mark_point();
  3710. client = mg_connect_client(
  3711. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  3712. ck_assert(client != NULL);
  3713. ck_assert_str_eq(client_err_buf, "");
  3714. mg_printf(client, "GET /%s HTTP/1.0\r\n\r\n", uri);
  3715. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3716. ck_assert_int_ge(r, 0);
  3717. ck_assert_str_eq(client_err_buf, "");
  3718. client_ri = mg_get_request_info(client);
  3719. ck_assert(client_ri != NULL);
  3720. /* e.g.: ck_assert_str_eq(client_ri->request_uri, "200"); */
  3721. ck_assert_ptr_ne(client_ri->request_uri, NULL);
  3722. r = (int)strlen(client_ri->request_uri);
  3723. ck_assert_int_eq(r, 3);
  3724. data_read = 0;
  3725. while (data_read < client_ri->content_length) {
  3726. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3727. if (r > 0) {
  3728. data_read += r;
  3729. }
  3730. }
  3731. /* Nothing left to read */
  3732. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3733. ck_assert_int_eq(r, 0);
  3734. mark_point();
  3735. mg_close_connection(client);
  3736. mark_point();
  3737. }
  3738. static void
  3739. minimal_http_client_impl(const char *server, uint16_t port, const char *uri)
  3740. {
  3741. minimal_http_https_client_impl(server, port, 0, uri);
  3742. }
  3743. static void
  3744. minimal_https_client_impl(const char *server, uint16_t port, const char *uri)
  3745. {
  3746. minimal_http_https_client_impl(server, port, 1, uri);
  3747. }
  3748. START_TEST(test_minimal_client)
  3749. {
  3750. mark_point();
  3751. /* Initialize the library */
  3752. mg_init_library(0);
  3753. mark_point();
  3754. /* Call a test client */
  3755. minimal_http_client_impl("192.30.253.113" /* www.github.com */,
  3756. 80,
  3757. "civetweb/civetweb/");
  3758. mark_point();
  3759. /* Un-initialize the library */
  3760. mg_exit_library();
  3761. mark_point();
  3762. }
  3763. END_TEST
  3764. static int
  3765. minimal_test_request_handler(struct mg_connection *conn, void *cbdata)
  3766. {
  3767. const char *msg = (const char *)cbdata;
  3768. unsigned long len = (unsigned long)strlen(msg);
  3769. mark_point();
  3770. mg_printf(conn,
  3771. "HTTP/1.1 200 OK\r\n"
  3772. "Content-Length: %lu\r\n"
  3773. "Content-Type: text/plain\r\n"
  3774. "Connection: close\r\n\r\n",
  3775. len);
  3776. mg_write(conn, msg, len);
  3777. mark_point();
  3778. return 200;
  3779. }
  3780. START_TEST(test_minimal_http_server_callback)
  3781. {
  3782. /* This test should ensure the minimum server example in
  3783. * docs/Embedding.md is still running. */
  3784. /* Server context handle */
  3785. struct mg_context *ctx;
  3786. mark_point();
  3787. /* Initialize the library */
  3788. mg_init_library(0);
  3789. /* Start the server */
  3790. ctx = test_mg_start(NULL, 0, NULL);
  3791. ck_assert(ctx != NULL);
  3792. /* Add some handler */
  3793. mg_set_request_handler(ctx,
  3794. "/hello",
  3795. minimal_test_request_handler,
  3796. (void *)"Hello world");
  3797. mg_set_request_handler(ctx,
  3798. "/8",
  3799. minimal_test_request_handler,
  3800. (void *)"Number eight");
  3801. /* Run the server for 15 seconds */
  3802. test_sleep(10);
  3803. /* Call a test client */
  3804. minimal_http_client_impl("127.0.0.1", 8080, "/hello");
  3805. /* Run the server for 15 seconds */
  3806. test_sleep(5);
  3807. /* Stop the server */
  3808. test_mg_stop(ctx);
  3809. /* Un-initialize the library */
  3810. mg_exit_library();
  3811. mark_point();
  3812. }
  3813. END_TEST
  3814. START_TEST(test_minimal_https_server_callback)
  3815. {
  3816. /* This test should show a HTTPS server with enhanced
  3817. * security settings.
  3818. *
  3819. * Articles:
  3820. * https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
  3821. *
  3822. * Scanners:
  3823. * https://securityheaders.io/
  3824. * https://www.htbridge.com/ssl/
  3825. * https://www.htbridge.com/websec/
  3826. * https://www.ssllabs.com/ssltest/
  3827. * https://www.qualys.com/forms/freescan/
  3828. */
  3829. /* Server context handle */
  3830. struct mg_context *ctx;
  3831. /* Server start parameters for HTTPS */
  3832. const char *OPTIONS[32];
  3833. int opt_idx = 0;
  3834. /* HTTPS port - required */
  3835. OPTIONS[opt_idx++] = "listening_ports";
  3836. OPTIONS[opt_idx++] = "8443s";
  3837. /* path to certificate file - required */
  3838. OPTIONS[opt_idx++] = "ssl_certificate";
  3839. OPTIONS[opt_idx++] = locate_ssl_cert();
  3840. /* set minimum SSL version to TLS 1.2 - recommended */
  3841. OPTIONS[opt_idx++] = "ssl_protocol_version";
  3842. OPTIONS[opt_idx++] = "4";
  3843. /* set some modern ciphers - recommended */
  3844. OPTIONS[opt_idx++] = "ssl_cipher_list";
  3845. OPTIONS[opt_idx++] = "ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS";
  3846. /* set "HTTPS only" header - recommended */
  3847. OPTIONS[opt_idx++] = "strict_transport_security_max_age";
  3848. OPTIONS[opt_idx++] = "31622400";
  3849. /* end of options - required */
  3850. OPTIONS[opt_idx] = NULL;
  3851. mark_point();
  3852. /* Initialize the library */
  3853. mg_init_library(0);
  3854. /* Start the server */
  3855. ctx = test_mg_start(NULL, 0, OPTIONS);
  3856. ck_assert(ctx != NULL);
  3857. /* Add some handler */
  3858. mg_set_request_handler(ctx,
  3859. "/hello",
  3860. minimal_test_request_handler,
  3861. (void *)"Hello world");
  3862. mg_set_request_handler(ctx,
  3863. "/8",
  3864. minimal_test_request_handler,
  3865. (void *)"Number eight");
  3866. /* Run the server for 15 seconds */
  3867. test_sleep(10);
  3868. /* Call a test client */
  3869. minimal_https_client_impl("127.0.0.1", 8443, "/hello");
  3870. /* Run the server for 15 seconds */
  3871. test_sleep(5);
  3872. /* Stop the server */
  3873. test_mg_stop(ctx);
  3874. /* Un-initialize the library */
  3875. mg_exit_library();
  3876. mark_point();
  3877. }
  3878. END_TEST
  3879. Suite *
  3880. make_public_server_suite(void)
  3881. {
  3882. Suite *const suite = suite_create("PublicServer");
  3883. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  3884. TCase *const tcase_initlib = tcase_create("Init library");
  3885. TCase *const tcase_startthreads = tcase_create("Start threads");
  3886. TCase *const tcase_minimal_svr = tcase_create("Minimal Server");
  3887. TCase *const tcase_minimal_cli = tcase_create("Minimal Client");
  3888. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  3889. TCase *const tcase_startstophttp_ipv6 =
  3890. tcase_create("Start Stop HTTP Server IPv6");
  3891. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  3892. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  3893. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  3894. TCase *const tcase_storebody = tcase_create("Store Body");
  3895. TCase *const tcase_handle_form = tcase_create("Handle Form");
  3896. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  3897. TCase *const tcase_keep_alive = tcase_create("HTTP Keep Alive");
  3898. TCase *const tcase_error_handling = tcase_create("Error handling");
  3899. TCase *const tcase_throttle = tcase_create("Limit speed");
  3900. TCase *const tcase_large_file = tcase_create("Large file");
  3901. TCase *const tcase_file_in_mem = tcase_create("File in memory");
  3902. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  3903. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  3904. suite_add_tcase(suite, tcase_checktestenv);
  3905. tcase_add_test(tcase_initlib, test_init_library);
  3906. tcase_set_timeout(tcase_initlib, civetweb_min_test_timeout);
  3907. suite_add_tcase(suite, tcase_initlib);
  3908. tcase_add_test(tcase_startthreads, test_threading);
  3909. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  3910. suite_add_tcase(suite, tcase_startthreads);
  3911. tcase_add_test(tcase_minimal_svr, test_minimal_http_server_callback);
  3912. tcase_add_test(tcase_minimal_svr, test_minimal_https_server_callback);
  3913. tcase_set_timeout(tcase_minimal_svr, civetweb_min_server_test_timeout);
  3914. suite_add_tcase(suite, tcase_minimal_svr);
  3915. tcase_add_test(tcase_minimal_cli, test_minimal_client);
  3916. tcase_set_timeout(tcase_minimal_cli, civetweb_min_server_test_timeout);
  3917. suite_add_tcase(suite, tcase_minimal_cli);
  3918. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  3919. tcase_set_timeout(tcase_startstophttp, civetweb_min_server_test_timeout);
  3920. suite_add_tcase(suite, tcase_startstophttp);
  3921. tcase_add_test(tcase_startstophttp_ipv6,
  3922. test_mg_start_stop_http_server_ipv6);
  3923. tcase_set_timeout(tcase_startstophttp_ipv6,
  3924. civetweb_min_server_test_timeout);
  3925. suite_add_tcase(suite, tcase_startstophttp_ipv6);
  3926. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  3927. tcase_set_timeout(tcase_startstophttps, civetweb_min_server_test_timeout);
  3928. suite_add_tcase(suite, tcase_startstophttps);
  3929. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  3930. tcase_set_timeout(tcase_serverandclienttls,
  3931. civetweb_min_server_test_timeout);
  3932. suite_add_tcase(suite, tcase_serverandclienttls);
  3933. tcase_add_test(tcase_serverrequests, test_request_handlers);
  3934. tcase_set_timeout(tcase_serverrequests, civetweb_mid_server_test_timeout);
  3935. suite_add_tcase(suite, tcase_serverrequests);
  3936. tcase_add_test(tcase_storebody, test_mg_store_body);
  3937. tcase_set_timeout(tcase_storebody, civetweb_mid_server_test_timeout);
  3938. suite_add_tcase(suite, tcase_storebody);
  3939. tcase_add_test(tcase_handle_form, test_handle_form);
  3940. tcase_set_timeout(tcase_handle_form, civetweb_mid_server_test_timeout);
  3941. suite_add_tcase(suite, tcase_handle_form);
  3942. tcase_add_test(tcase_http_auth, test_http_auth);
  3943. tcase_set_timeout(tcase_http_auth, civetweb_min_server_test_timeout);
  3944. suite_add_tcase(suite, tcase_http_auth);
  3945. tcase_add_test(tcase_keep_alive, test_keep_alive);
  3946. tcase_set_timeout(tcase_keep_alive, civetweb_mid_server_test_timeout);
  3947. suite_add_tcase(suite, tcase_keep_alive);
  3948. tcase_add_test(tcase_error_handling, test_error_handling);
  3949. tcase_add_test(tcase_error_handling, test_error_log_file);
  3950. tcase_set_timeout(tcase_error_handling, civetweb_mid_server_test_timeout);
  3951. suite_add_tcase(suite, tcase_error_handling);
  3952. tcase_add_test(tcase_throttle, test_throttle);
  3953. tcase_set_timeout(tcase_throttle, civetweb_mid_server_test_timeout);
  3954. suite_add_tcase(suite, tcase_throttle);
  3955. tcase_add_test(tcase_large_file, test_large_file);
  3956. tcase_set_timeout(tcase_large_file, civetweb_mid_server_test_timeout);
  3957. suite_add_tcase(suite, tcase_large_file);
  3958. tcase_add_test(tcase_file_in_mem, test_file_in_memory);
  3959. tcase_set_timeout(tcase_file_in_mem, civetweb_mid_server_test_timeout);
  3960. suite_add_tcase(suite, tcase_file_in_mem);
  3961. return suite;
  3962. }
  3963. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  3964. /* Used to debug test cases without using the check framework */
  3965. /* Build command for Linux:
  3966. gcc test/public_server.c src/civetweb.c -I include/ -I test/ -l pthread -l dl -D
  3967. LOCAL_TEST -D REPLACE_CHECK_FOR_LOCAL_DEBUGGING -D MAIN_PUBLIC_SERVER=main
  3968. */
  3969. static int chk_ok = 0;
  3970. static int chk_failed = 0;
  3971. void
  3972. MAIN_PUBLIC_SERVER(void)
  3973. {
  3974. unsigned f_avail = mg_check_feature(0xFF);
  3975. unsigned f_ret = mg_init_library(f_avail);
  3976. ck_assert_uint_eq(f_ret, f_avail);
  3977. test_the_test_environment(0);
  3978. test_threading(0);
  3979. test_minimal_client(0);
  3980. test_minimal_server_callback(0);
  3981. test_mg_start_stop_http_server(0);
  3982. test_mg_start_stop_https_server(0);
  3983. test_request_handlers(0);
  3984. test_mg_store_body(0);
  3985. test_mg_server_and_client_tls(0);
  3986. test_handle_form(0);
  3987. test_http_auth(0);
  3988. test_keep_alive(0);
  3989. test_error_handling(0);
  3990. test_error_log_file(0);
  3991. test_throttle(0);
  3992. test_large_file(0);
  3993. test_file_in_memory(0);
  3994. mg_exit_library();
  3995. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  3996. }
  3997. void
  3998. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  3999. {
  4000. va_list va;
  4001. va_start(va, expr);
  4002. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  4003. vfprintf(stderr, expr, va);
  4004. fprintf(stderr, "\n\n");
  4005. va_end(va);
  4006. chk_failed++;
  4007. }
  4008. void
  4009. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  4010. {
  4011. va_list va;
  4012. if (cond) {
  4013. chk_ok++;
  4014. return;
  4015. }
  4016. va_start(va, expr);
  4017. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  4018. vfprintf(stderr, expr, va);
  4019. fprintf(stderr, "\n\n");
  4020. va_end(va);
  4021. chk_failed++;
  4022. }
  4023. void
  4024. _mark_point(const char *file, int line)
  4025. {
  4026. chk_ok++;
  4027. }
  4028. void
  4029. tcase_fn_start(const char *fname, const char *file, int line)
  4030. {
  4031. }
  4032. void suite_add_tcase(Suite *s, TCase *tc){};
  4033. void _tcase_add_test(TCase *tc,
  4034. TFun tf,
  4035. const char *fname,
  4036. int _signal,
  4037. int allowed_exit_value,
  4038. int start,
  4039. int end){};
  4040. TCase *
  4041. tcase_create(const char *name)
  4042. {
  4043. return NULL;
  4044. };
  4045. Suite *
  4046. suite_create(const char *name)
  4047. {
  4048. return NULL;
  4049. };
  4050. void tcase_set_timeout(TCase *tc, double timeout){};
  4051. #endif