public_server.c 135 KB

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