public_server.c 134 KB

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