public_server.c 131 KB

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