public_server.c 135 KB

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