public_server.c 127 KB

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