public_server.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  1. /* Copyright (c) 2015-2016 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");
  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. test_sleep(SLEEP_AFTER_MG_START);
  238. mark_point();
  239. return ctx;
  240. }
  241. static void
  242. test_mg_stop(struct mg_context *ctx)
  243. {
  244. mark_point();
  245. test_sleep(SLEEP_BEFORE_MG_STOP);
  246. mark_point();
  247. mg_stop(ctx);
  248. mark_point();
  249. test_sleep(SLEEP_AFTER_MG_STOP);
  250. mark_point();
  251. }
  252. START_TEST(test_mg_start_stop_http_server)
  253. {
  254. struct mg_context *ctx;
  255. const char *OPTIONS[] = {
  256. #if !defined(NO_FILES)
  257. "document_root",
  258. ".",
  259. #endif
  260. "listening_ports",
  261. "8080",
  262. NULL,
  263. };
  264. size_t ports_cnt;
  265. int ports[16];
  266. int ssl[16];
  267. struct mg_callbacks callbacks;
  268. char errmsg[256];
  269. struct mg_connection *client_conn;
  270. char client_err[256];
  271. const struct mg_request_info *client_ri;
  272. int client_res, ret;
  273. struct mg_server_ports portinfo[8];
  274. memset(ports, 0, sizeof(ports));
  275. memset(ssl, 0, sizeof(ssl));
  276. memset(portinfo, 0, sizeof(portinfo));
  277. memset(&callbacks, 0, sizeof(callbacks));
  278. memset(errmsg, 0, sizeof(errmsg));
  279. callbacks.log_message = log_msg_func;
  280. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  281. ck_assert_str_eq(errmsg, "");
  282. ck_assert(ctx != NULL);
  283. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  284. ck_assert_uint_eq(ports_cnt, 1);
  285. ck_assert_int_eq(ports[0], 8080);
  286. ck_assert_int_eq(ssl[0], 0);
  287. ck_assert_int_eq(ports[1], 0);
  288. ck_assert_int_eq(ssl[1], 0);
  289. ret = mg_get_server_ports(ctx, 0, portinfo);
  290. ck_assert_int_lt(ret, 0);
  291. ck_assert_int_eq(portinfo[0].protocol, 0);
  292. ck_assert_int_eq(portinfo[0].port, 0);
  293. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  294. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  295. ck_assert_int_eq(portinfo[1].protocol, 0);
  296. ck_assert_int_eq(portinfo[1].port, 0);
  297. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  298. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  299. ret = mg_get_server_ports(ctx, 4, portinfo);
  300. ck_assert_int_eq(ret, 1);
  301. ck_assert_int_eq(portinfo[0].protocol, 1);
  302. ck_assert_int_eq(portinfo[0].port, 8080);
  303. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  304. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  305. ck_assert_int_eq(portinfo[1].protocol, 0);
  306. ck_assert_int_eq(portinfo[1].port, 0);
  307. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  308. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  309. test_sleep(1);
  310. /* HTTP 1.0 GET request */
  311. memset(client_err, 0, sizeof(client_err));
  312. client_conn =
  313. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  314. ck_assert(client_conn != NULL);
  315. ck_assert_str_eq(client_err, "");
  316. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  317. client_res =
  318. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  319. ck_assert_int_ge(client_res, 0);
  320. ck_assert_str_eq(client_err, "");
  321. client_ri = mg_get_request_info(client_conn);
  322. ck_assert(client_ri != NULL);
  323. #if defined(NO_FILES)
  324. ck_assert_str_eq(client_ri->uri, "404");
  325. #else
  326. ck_assert_str_eq(client_ri->uri, "200");
  327. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  328. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  329. ck_assert_int_gt(client_res, 0);
  330. ck_assert_int_le(client_res, sizeof(client_err));
  331. #endif
  332. mg_close_connection(client_conn);
  333. test_sleep(1);
  334. /* HTTP 1.1 GET request */
  335. memset(client_err, 0, sizeof(client_err));
  336. client_conn =
  337. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  338. ck_assert(client_conn != NULL);
  339. ck_assert_str_eq(client_err, "");
  340. mg_printf(client_conn, "GET / HTTP/1.1\r\n");
  341. mg_printf(client_conn, "Host: localhost:8080\r\n");
  342. mg_printf(client_conn, "Connection: close\r\n\r\n");
  343. client_res =
  344. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  345. ck_assert_int_ge(client_res, 0);
  346. ck_assert_str_eq(client_err, "");
  347. client_ri = mg_get_request_info(client_conn);
  348. ck_assert(client_ri != NULL);
  349. #if defined(NO_FILES)
  350. ck_assert_str_eq(client_ri->uri, "404");
  351. #else
  352. ck_assert_str_eq(client_ri->uri, "200");
  353. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  354. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  355. ck_assert_int_gt(client_res, 0);
  356. ck_assert_int_le(client_res, sizeof(client_err));
  357. #endif
  358. mg_close_connection(client_conn);
  359. test_sleep(1);
  360. /* HTTP 1.7 GET request - this HTTP version does not exist */
  361. memset(client_err, 0, sizeof(client_err));
  362. client_conn =
  363. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  364. ck_assert(client_conn != NULL);
  365. ck_assert_str_eq(client_err, "");
  366. mg_printf(client_conn, "GET / HTTP/1.7\r\n");
  367. mg_printf(client_conn, "Host: localhost:8080\r\n");
  368. mg_printf(client_conn, "Connection: close\r\n\r\n");
  369. client_res =
  370. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  371. ck_assert_int_ge(client_res, 0);
  372. ck_assert_str_eq(client_err, "");
  373. client_ri = mg_get_request_info(client_conn);
  374. ck_assert(client_ri != NULL);
  375. /* Response must be 505 HTTP Version not supported */
  376. ck_assert_str_eq(client_ri->uri, "505");
  377. mg_close_connection(client_conn);
  378. test_sleep(1);
  379. /* HTTP request with multiline header.
  380. * Multiline header are obsolete with RFC 7230 section-3.2.4
  381. * and must return "400 Bad Request" */
  382. memset(client_err, 0, sizeof(client_err));
  383. client_conn =
  384. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  385. ck_assert(client_conn != NULL);
  386. ck_assert_str_eq(client_err, "");
  387. mg_printf(client_conn, "GET / HTTP/1.1\r\n");
  388. mg_printf(client_conn, "Host: localhost:8080\r\n");
  389. mg_printf(client_conn, "X-Obsolete-Header: something\r\nfor nothing\r\n");
  390. mg_printf(client_conn, "Connection: close\r\n\r\n");
  391. client_res =
  392. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  393. ck_assert_int_ge(client_res, 0);
  394. ck_assert_str_eq(client_err, "");
  395. client_ri = mg_get_request_info(client_conn);
  396. ck_assert(client_ri != NULL);
  397. /* Response must be 400 Bad Request */
  398. ck_assert_str_eq(client_ri->uri, "400");
  399. mg_close_connection(client_conn);
  400. test_sleep(1);
  401. /* End test */
  402. test_mg_stop(ctx);
  403. }
  404. END_TEST
  405. START_TEST(test_mg_start_stop_https_server)
  406. {
  407. #ifndef NO_SSL
  408. struct mg_context *ctx;
  409. size_t ports_cnt;
  410. int ports[16];
  411. int ssl[16];
  412. struct mg_callbacks callbacks;
  413. char errmsg[256];
  414. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  415. int opt_idx = 0;
  416. const char *ssl_cert = locate_ssl_cert();
  417. struct mg_connection *client_conn;
  418. char client_err[256];
  419. const struct mg_request_info *client_ri;
  420. int client_res, ret;
  421. struct mg_server_ports portinfo[8];
  422. ck_assert(ssl_cert != NULL);
  423. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  424. #if !defined(NO_FILES)
  425. OPTIONS[opt_idx++] = "document_root";
  426. OPTIONS[opt_idx++] = ".";
  427. #endif
  428. OPTIONS[opt_idx++] = "listening_ports";
  429. OPTIONS[opt_idx++] = "8080r,8443s";
  430. OPTIONS[opt_idx++] = "ssl_certificate";
  431. OPTIONS[opt_idx++] = ssl_cert;
  432. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  433. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  434. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  435. memset(ports, 0, sizeof(ports));
  436. memset(ssl, 0, sizeof(ssl));
  437. memset(portinfo, 0, sizeof(portinfo));
  438. memset(&callbacks, 0, sizeof(callbacks));
  439. memset(errmsg, 0, sizeof(errmsg));
  440. callbacks.log_message = log_msg_func;
  441. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  442. ck_assert_str_eq(errmsg, "");
  443. ck_assert(ctx != NULL);
  444. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  445. ck_assert_uint_eq(ports_cnt, 2);
  446. ck_assert_int_eq(ports[0], 8080);
  447. ck_assert_int_eq(ssl[0], 0);
  448. ck_assert_int_eq(ports[1], 8443);
  449. ck_assert_int_eq(ssl[1], 1);
  450. ck_assert_int_eq(ports[2], 0);
  451. ck_assert_int_eq(ssl[2], 0);
  452. ret = mg_get_server_ports(ctx, 0, portinfo);
  453. ck_assert_int_lt(ret, 0);
  454. ck_assert_int_eq(portinfo[0].protocol, 0);
  455. ck_assert_int_eq(portinfo[0].port, 0);
  456. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  457. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  458. ck_assert_int_eq(portinfo[1].protocol, 0);
  459. ck_assert_int_eq(portinfo[1].port, 0);
  460. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  461. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  462. ret = mg_get_server_ports(ctx, 4, portinfo);
  463. ck_assert_int_eq(ret, 2);
  464. ck_assert_int_eq(portinfo[0].protocol, 1);
  465. ck_assert_int_eq(portinfo[0].port, 8080);
  466. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  467. ck_assert_int_eq(portinfo[0].is_redirect, 1);
  468. ck_assert_int_eq(portinfo[1].protocol, 1);
  469. ck_assert_int_eq(portinfo[1].port, 8443);
  470. ck_assert_int_eq(portinfo[1].is_ssl, 1);
  471. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  472. ck_assert_int_eq(portinfo[2].protocol, 0);
  473. ck_assert_int_eq(portinfo[2].port, 0);
  474. ck_assert_int_eq(portinfo[2].is_ssl, 0);
  475. ck_assert_int_eq(portinfo[2].is_redirect, 0);
  476. test_sleep(1);
  477. memset(client_err, 0, sizeof(client_err));
  478. client_conn =
  479. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  480. ck_assert(client_conn != NULL);
  481. ck_assert_str_eq(client_err, "");
  482. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  483. client_res =
  484. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  485. ck_assert_int_ge(client_res, 0);
  486. ck_assert_str_eq(client_err, "");
  487. client_ri = mg_get_request_info(client_conn);
  488. ck_assert(client_ri != NULL);
  489. #if defined(NO_FILES)
  490. ck_assert_str_eq(client_ri->uri, "404");
  491. #else
  492. ck_assert_str_eq(client_ri->uri, "200");
  493. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  494. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  495. ck_assert_int_gt(client_res, 0);
  496. ck_assert_int_le(client_res, sizeof(client_err));
  497. #endif
  498. mg_close_connection(client_conn);
  499. test_sleep(1);
  500. test_mg_stop(ctx);
  501. #endif
  502. }
  503. END_TEST
  504. START_TEST(test_mg_server_and_client_tls)
  505. {
  506. #ifndef NO_SSL
  507. struct mg_context *ctx;
  508. int ports_cnt;
  509. struct mg_server_ports ports[16];
  510. struct mg_callbacks callbacks;
  511. char errmsg[256];
  512. struct mg_connection *client_conn;
  513. char client_err[256];
  514. const struct mg_request_info *client_ri;
  515. int client_res;
  516. struct mg_client_options client_options;
  517. const char *OPTIONS[32]; /* initializer list here is rejected by CI test */
  518. int opt_idx = 0;
  519. char server_cert[256];
  520. char client_cert[256];
  521. const char *res_dir = locate_resources();
  522. ck_assert(res_dir != NULL);
  523. strcpy(server_cert, res_dir);
  524. strcpy(client_cert, res_dir);
  525. #ifdef _WIN32
  526. strcat(server_cert, "cert\\server.pem");
  527. strcat(client_cert, "cert\\client.pem");
  528. #else
  529. strcat(server_cert, "cert/server.pem");
  530. strcat(client_cert, "cert/client.pem");
  531. #endif
  532. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  533. #if !defined(NO_FILES)
  534. OPTIONS[opt_idx++] = "document_root";
  535. OPTIONS[opt_idx++] = ".";
  536. #endif
  537. OPTIONS[opt_idx++] = "listening_ports";
  538. OPTIONS[opt_idx++] = "8080r,8443s";
  539. OPTIONS[opt_idx++] = "ssl_certificate";
  540. OPTIONS[opt_idx++] = server_cert;
  541. OPTIONS[opt_idx++] = "ssl_verify_peer";
  542. OPTIONS[opt_idx++] = "yes";
  543. OPTIONS[opt_idx++] = "ssl_ca_file";
  544. OPTIONS[opt_idx++] = client_cert;
  545. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  546. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  547. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  548. memset(ports, 0, sizeof(ports));
  549. memset(&callbacks, 0, sizeof(callbacks));
  550. memset(errmsg, 0, sizeof(errmsg));
  551. callbacks.log_message = log_msg_func;
  552. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  553. ck_assert_str_eq(errmsg, "");
  554. ck_assert(ctx != NULL);
  555. ports_cnt = mg_get_server_ports(ctx, 16, ports);
  556. ck_assert_int_eq(ports_cnt, 2);
  557. ck_assert_int_eq(ports[0].protocol, 1);
  558. ck_assert_int_eq(ports[0].port, 8080);
  559. ck_assert_int_eq(ports[0].is_ssl, 0);
  560. ck_assert_int_eq(ports[0].is_redirect, 1);
  561. ck_assert_int_eq(ports[1].protocol, 1);
  562. ck_assert_int_eq(ports[1].port, 8443);
  563. ck_assert_int_eq(ports[1].is_ssl, 1);
  564. ck_assert_int_eq(ports[1].is_redirect, 0);
  565. ck_assert_int_eq(ports[2].protocol, 0);
  566. ck_assert_int_eq(ports[2].port, 0);
  567. ck_assert_int_eq(ports[2].is_ssl, 0);
  568. ck_assert_int_eq(ports[2].is_redirect, 0);
  569. test_sleep(1);
  570. memset(client_err, 0, sizeof(client_err));
  571. client_conn =
  572. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  573. ck_assert(client_conn == NULL);
  574. ck_assert_str_ne(client_err, "");
  575. memset(client_err, 0, sizeof(client_err));
  576. memset(&client_options, 0, sizeof(client_options));
  577. client_options.host = "127.0.0.1";
  578. client_options.port = 8443;
  579. client_options.client_cert = client_cert;
  580. client_options.server_cert = server_cert;
  581. client_conn = mg_connect_client_secure(&client_options,
  582. client_err,
  583. sizeof(client_err));
  584. ck_assert(client_conn != NULL);
  585. ck_assert_str_eq(client_err, "");
  586. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  587. client_res =
  588. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  589. ck_assert_int_ge(client_res, 0);
  590. ck_assert_str_eq(client_err, "");
  591. client_ri = mg_get_request_info(client_conn);
  592. ck_assert(client_ri != NULL);
  593. #if defined(NO_FILES)
  594. ck_assert_str_eq(client_ri->uri, "404");
  595. #else
  596. ck_assert_str_eq(client_ri->uri, "200");
  597. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  598. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  599. ck_assert_int_gt(client_res, 0);
  600. ck_assert_int_le(client_res, sizeof(client_err));
  601. #endif
  602. mg_close_connection(client_conn);
  603. /* TODO: A client API using a client certificate is missing */
  604. test_sleep(1);
  605. test_mg_stop(ctx);
  606. #endif
  607. }
  608. END_TEST
  609. static struct mg_context *g_ctx;
  610. static int
  611. request_test_handler(struct mg_connection *conn, void *cbdata)
  612. {
  613. int i;
  614. char chunk_data[32];
  615. const struct mg_request_info *ri;
  616. struct mg_context *ctx;
  617. void *ud, *cud;
  618. ctx = mg_get_context(conn);
  619. ud = mg_get_user_data(ctx);
  620. ri = mg_get_request_info(conn);
  621. ck_assert(ri != NULL);
  622. ck_assert(ctx == g_ctx);
  623. ck_assert(ud == &g_ctx);
  624. mg_set_user_connection_data(conn, (void *)6543);
  625. cud = mg_get_user_connection_data(conn);
  626. ck_assert_ptr_eq((void *)cud, (void *)6543);
  627. ck_assert_ptr_eq((void *)cbdata, (void *)7);
  628. strcpy(chunk_data, "123456789A123456789B123456789C");
  629. mg_printf(conn,
  630. "HTTP/1.1 200 OK\r\n"
  631. "Transfer-Encoding: chunked\r\n"
  632. "Content-Type: text/plain\r\n\r\n");
  633. for (i = 1; i <= 10; i++) {
  634. mg_printf(conn, "%x\r\n", i);
  635. mg_write(conn, chunk_data, (unsigned)i);
  636. mg_printf(conn, "\r\n");
  637. }
  638. mg_printf(conn, "0\r\n\r\n");
  639. return 1;
  640. }
  641. #ifdef USE_WEBSOCKET
  642. /****************************************************************************/
  643. /* WEBSOCKET SERVER */
  644. /****************************************************************************/
  645. static const char *websocket_welcome_msg = "websocket welcome\n";
  646. static const size_t websocket_welcome_msg_len =
  647. 18 /* strlen(websocket_welcome_msg) */;
  648. static const char *websocket_goodbye_msg = "websocket bye\n";
  649. static const size_t websocket_goodbye_msg_len =
  650. 14 /* strlen(websocket_goodbye_msg) */;
  651. static int
  652. websock_server_connect(const struct mg_connection *conn, void *udata)
  653. {
  654. (void)conn;
  655. ck_assert_ptr_eq((void *)udata, (void *)7531);
  656. printf("Server: Websocket connected\n");
  657. return 0; /* return 0 to accept every connection */
  658. }
  659. static void
  660. websock_server_ready(struct mg_connection *conn, void *udata)
  661. {
  662. ck_assert_ptr_eq((void *)udata, (void *)7531);
  663. printf("Server: Websocket ready\n");
  664. /* Send websocket welcome message */
  665. mg_lock_connection(conn);
  666. mg_websocket_write(conn,
  667. WEBSOCKET_OPCODE_TEXT,
  668. websocket_welcome_msg,
  669. websocket_welcome_msg_len);
  670. mg_unlock_connection(conn);
  671. printf("Server: Websocket ready X\n");
  672. }
  673. static int
  674. websock_server_data(struct mg_connection *conn,
  675. int bits,
  676. char *data,
  677. size_t data_len,
  678. void *udata)
  679. {
  680. (void)bits;
  681. ck_assert_ptr_eq((void *)udata, (void *)7531);
  682. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  683. if (data_len == 3 && !memcmp(data, "bye", 3)) {
  684. /* Send websocket goodbye message */
  685. mg_lock_connection(conn);
  686. mg_websocket_write(conn,
  687. WEBSOCKET_OPCODE_TEXT,
  688. websocket_goodbye_msg,
  689. websocket_goodbye_msg_len);
  690. mg_unlock_connection(conn);
  691. } else if (data_len == 5 && !memcmp(data, "data1", 5)) {
  692. mg_lock_connection(conn);
  693. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok1", 3);
  694. mg_unlock_connection(conn);
  695. } else if (data_len == 5 && !memcmp(data, "data2", 5)) {
  696. mg_lock_connection(conn);
  697. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok 2", 4);
  698. mg_unlock_connection(conn);
  699. } else if (data_len == 5 && !memcmp(data, "data3", 5)) {
  700. mg_lock_connection(conn);
  701. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok - 3", 6);
  702. mg_unlock_connection(conn);
  703. } else {
  704. #if defined(__MINGW32__) || defined(__GNUC__)
  705. #pragma GCC diagnostic push
  706. #pragma GCC diagnostic ignored "-Wunreachable-code"
  707. #endif
  708. #ifdef __clang__
  709. #pragma clang diagnostic push
  710. #pragma clang diagnostic ignored "-Wunreachable-code"
  711. #endif
  712. ck_abort_msg("Got unexpected message from websocket client");
  713. return 0;
  714. #ifdef __clang__
  715. #pragma clang diagnostic pop
  716. #endif
  717. #if defined(__MINGW32__) || defined(__GNUC__)
  718. #pragma GCC diagnostic pop
  719. #endif
  720. }
  721. return 1; /* return 1 to keep the connetion open */
  722. }
  723. static void
  724. websock_server_close(const struct mg_connection *conn, void *udata)
  725. {
  726. (void)conn;
  727. ck_assert_ptr_eq((void *)udata, (void *)7531);
  728. printf("Server: Close connection\n");
  729. /* Can not send a websocket goodbye message here - the connection is already
  730. * closed */
  731. }
  732. /****************************************************************************/
  733. /* WEBSOCKET CLIENT */
  734. /****************************************************************************/
  735. struct tclient_data {
  736. void *data;
  737. size_t len;
  738. int closed;
  739. int clientId;
  740. };
  741. static int
  742. websocket_client_data_handler(struct mg_connection *conn,
  743. int flags,
  744. char *data,
  745. size_t data_len,
  746. void *user_data)
  747. {
  748. struct mg_context *ctx = mg_get_context(conn);
  749. struct tclient_data *pclient_data =
  750. (struct tclient_data *)mg_get_user_data(ctx);
  751. (void)user_data; /* TODO: check this */
  752. ck_assert(pclient_data != NULL);
  753. ck_assert_int_eq(flags, (int)(128 | 1));
  754. printf("Client %i received data from server: ", pclient_data->clientId);
  755. fwrite(data, 1, data_len, stdout);
  756. printf("\n");
  757. pclient_data->data = malloc(data_len);
  758. ck_assert(pclient_data->data != NULL);
  759. memcpy(pclient_data->data, data, data_len);
  760. pclient_data->len = data_len;
  761. return 1;
  762. }
  763. static void
  764. websocket_client_close_handler(const struct mg_connection *conn,
  765. void *user_data)
  766. {
  767. struct mg_context *ctx = mg_get_context(conn);
  768. struct tclient_data *pclient_data =
  769. (struct tclient_data *)mg_get_user_data(ctx);
  770. (void)user_data; /* TODO: check this */
  771. ck_assert(pclient_data != NULL);
  772. printf("Client %i: Close handler\n", pclient_data->clientId);
  773. pclient_data->closed++;
  774. }
  775. #endif
  776. START_TEST(test_request_handlers)
  777. {
  778. char ebuf[100];
  779. struct mg_context *ctx;
  780. struct mg_connection *client_conn;
  781. const struct mg_request_info *ri;
  782. char uri[64];
  783. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  784. const char *expected =
  785. "112123123412345123456123456712345678123456789123456789A";
  786. int i;
  787. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  788. #if defined(USE_IPV6) && defined(NO_SSL)
  789. const char *HTTP_PORT = "8084,[::]:8086";
  790. short ipv4_port = 8084;
  791. short ipv6_port = 8086;
  792. #elif !defined(USE_IPV6) && defined(NO_SSL)
  793. const char *HTTP_PORT = "8084";
  794. short ipv4_port = 8084;
  795. #elif defined(USE_IPV6) && !defined(NO_SSL)
  796. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  797. short ipv4_port = 8084;
  798. short ipv4s_port = 8094;
  799. short ipv4r_port = 8194;
  800. short ipv6_port = 8086;
  801. short ipv6s_port = 8096;
  802. short ipv6r_port = 8196;
  803. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  804. const char *HTTP_PORT = "8084,8194r,8094s";
  805. short ipv4_port = 8084;
  806. short ipv4s_port = 8094;
  807. short ipv4r_port = 8194;
  808. #endif
  809. const char *OPTIONS[16];
  810. const char *opt;
  811. FILE *f;
  812. const char *plain_file_content;
  813. const char *encoded_file_content;
  814. const char *cgi_script_content;
  815. const char *expected_cgi_result;
  816. int opt_idx = 0;
  817. #if !defined(NO_SSL)
  818. const char *ssl_cert = locate_ssl_cert();
  819. #endif
  820. #if defined(USE_WEBSOCKET)
  821. struct tclient_data ws_client1_data = {NULL, 0, 0, 1};
  822. struct tclient_data ws_client2_data = {NULL, 0, 0, 2};
  823. struct tclient_data ws_client3_data = {NULL, 0, 0, 3};
  824. struct tclient_data ws_client4_data = {NULL, 0, 0, 4};
  825. struct mg_connection *ws_client1_conn = NULL;
  826. struct mg_connection *ws_client2_conn = NULL;
  827. struct mg_connection *ws_client3_conn = NULL;
  828. struct mg_connection *ws_client4_conn = NULL;
  829. #endif
  830. char cmd_buf[256];
  831. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  832. OPTIONS[opt_idx++] = "listening_ports";
  833. OPTIONS[opt_idx++] = HTTP_PORT;
  834. OPTIONS[opt_idx++] = "authentication_domain";
  835. OPTIONS[opt_idx++] = "test.domain";
  836. #if !defined(NO_FILES)
  837. OPTIONS[opt_idx++] = "document_root";
  838. OPTIONS[opt_idx++] = ".";
  839. #endif
  840. #ifndef NO_SSL
  841. ck_assert(ssl_cert != NULL);
  842. OPTIONS[opt_idx++] = "ssl_certificate";
  843. OPTIONS[opt_idx++] = ssl_cert;
  844. #endif
  845. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  846. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  847. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  848. ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
  849. ck_assert(ctx != NULL);
  850. g_ctx = ctx;
  851. opt = mg_get_option(ctx, "listening_ports");
  852. ck_assert_str_eq(opt, HTTP_PORT);
  853. opt = mg_get_option(ctx, "cgi_environment");
  854. ck_assert_str_eq(opt, "");
  855. opt = mg_get_option(ctx, "unknown_option_name");
  856. ck_assert(opt == NULL);
  857. for (i = 0; i < 1000; i++) {
  858. sprintf(uri, "/U%u", i);
  859. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  860. }
  861. for (i = 500; i < 800; i++) {
  862. sprintf(uri, "/U%u", i);
  863. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  864. }
  865. for (i = 600; i >= 0; i--) {
  866. sprintf(uri, "/U%u", i);
  867. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  868. }
  869. for (i = 750; i <= 1000; i++) {
  870. sprintf(uri, "/U%u", i);
  871. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  872. }
  873. for (i = 5; i < 9; i++) {
  874. sprintf(uri, "/U%u", i);
  875. mg_set_request_handler(ctx,
  876. uri,
  877. request_test_handler,
  878. (void *)(ptrdiff_t)i);
  879. }
  880. #ifdef USE_WEBSOCKET
  881. mg_set_websocket_handler(ctx,
  882. "/websocket",
  883. websock_server_connect,
  884. websock_server_ready,
  885. websock_server_data,
  886. websock_server_close,
  887. (void *)7531);
  888. #endif
  889. /* Try to load non existing file */
  890. client_conn = mg_download("localhost",
  891. ipv4_port,
  892. 0,
  893. ebuf,
  894. sizeof(ebuf),
  895. "%s",
  896. "GET /file/not/found HTTP/1.0\r\n\r\n");
  897. ck_assert(client_conn != NULL);
  898. ri = mg_get_request_info(client_conn);
  899. ck_assert(ri != NULL);
  900. ck_assert_str_eq(ri->uri, "404");
  901. mg_close_connection(client_conn);
  902. /* Get data from callback */
  903. client_conn = mg_download(
  904. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  905. ck_assert(client_conn != NULL);
  906. ri = mg_get_request_info(client_conn);
  907. ck_assert(ri != NULL);
  908. ck_assert_str_eq(ri->uri, "200");
  909. i = mg_read(client_conn, buf, sizeof(buf));
  910. ck_assert_int_eq(i, (int)strlen(expected));
  911. buf[i] = 0;
  912. ck_assert_str_eq(buf, expected);
  913. mg_close_connection(client_conn);
  914. /* Get data from callback using http://127.0.0.1 */
  915. client_conn = mg_download(
  916. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  917. ck_assert(client_conn != NULL);
  918. ri = mg_get_request_info(client_conn);
  919. ck_assert(ri != NULL);
  920. ck_assert_str_eq(ri->uri, "200");
  921. i = mg_read(client_conn, buf, sizeof(buf));
  922. if ((i >= 0) && ((size_t)i < sizeof(buf))) {
  923. buf[i] = 0;
  924. } else {
  925. ck_abort_msg(
  926. "ERROR: test_request_handlers: read returned %i (>=0, <%i)",
  927. (int)i,
  928. (int)sizeof(buf));
  929. }
  930. ck_assert((int)i < (int)sizeof(buf));
  931. ck_assert(i > 0);
  932. ck_assert_int_eq(i, (int)strlen(expected));
  933. buf[i] = 0;
  934. ck_assert_str_eq(buf, expected);
  935. mg_close_connection(client_conn);
  936. #if defined(USE_IPV6)
  937. /* Get data from callback using http://[::1] */
  938. client_conn =
  939. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  940. ck_assert(client_conn != NULL);
  941. ri = mg_get_request_info(client_conn);
  942. ck_assert(ri != NULL);
  943. ck_assert_str_eq(ri->uri, "200");
  944. i = mg_read(client_conn, buf, sizeof(buf));
  945. ck_assert_int_eq(i, (int)strlen(expected));
  946. buf[i] = 0;
  947. ck_assert_str_eq(buf, expected);
  948. mg_close_connection(client_conn);
  949. #endif
  950. #if !defined(NO_SSL)
  951. /* Get data from callback using https://127.0.0.1 */
  952. client_conn = mg_download(
  953. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  954. ck_assert(client_conn != NULL);
  955. ri = mg_get_request_info(client_conn);
  956. ck_assert(ri != NULL);
  957. ck_assert_str_eq(ri->uri, "200");
  958. i = mg_read(client_conn, buf, sizeof(buf));
  959. ck_assert_int_eq(i, (int)strlen(expected));
  960. buf[i] = 0;
  961. ck_assert_str_eq(buf, expected);
  962. mg_close_connection(client_conn);
  963. /* Get redirect from callback using http://127.0.0.1 */
  964. client_conn = mg_download(
  965. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  966. ck_assert(client_conn != NULL);
  967. ri = mg_get_request_info(client_conn);
  968. ck_assert(ri != NULL);
  969. ck_assert_str_eq(ri->uri, "302");
  970. i = mg_read(client_conn, buf, sizeof(buf));
  971. ck_assert_int_eq(i, -1);
  972. mg_close_connection(client_conn);
  973. #endif
  974. #if defined(USE_IPV6) && !defined(NO_SSL)
  975. /* Get data from callback using https://[::1] */
  976. client_conn =
  977. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  978. ck_assert(client_conn != NULL);
  979. ri = mg_get_request_info(client_conn);
  980. ck_assert(ri != NULL);
  981. ck_assert_str_eq(ri->uri, "200");
  982. i = mg_read(client_conn, buf, sizeof(buf));
  983. ck_assert_int_eq(i, (int)strlen(expected));
  984. buf[i] = 0;
  985. ck_assert_str_eq(buf, expected);
  986. mg_close_connection(client_conn);
  987. /* Get redirect from callback using http://127.0.0.1 */
  988. client_conn =
  989. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  990. ck_assert(client_conn != NULL);
  991. ri = mg_get_request_info(client_conn);
  992. ck_assert(ri != NULL);
  993. ck_assert_str_eq(ri->uri, "302");
  994. i = mg_read(client_conn, buf, sizeof(buf));
  995. ck_assert_int_eq(i, -1);
  996. mg_close_connection(client_conn);
  997. #endif
  998. /* It seems to be impossible to find out what the actual working
  999. * directory of the CI test environment is. Before breaking another
  1000. * dozen of builds by trying blindly with different paths, just
  1001. * create the file here */
  1002. #ifdef _WIN32
  1003. f = fopen("test.txt", "wb");
  1004. #else
  1005. f = fopen("test.txt", "w");
  1006. #endif
  1007. plain_file_content = "simple text file\n";
  1008. fwrite(plain_file_content, 17, 1, f);
  1009. fclose(f);
  1010. #ifdef _WIN32
  1011. f = fopen("test_gz.txt.gz", "wb");
  1012. #else
  1013. f = fopen("test_gz.txt.gz", "w");
  1014. #endif
  1015. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  1016. "test_gz.txt"
  1017. "\x00\x01\x11\x00\xee\xff"
  1018. "zipped text file"
  1019. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  1020. fwrite(encoded_file_content, 1, 52, f);
  1021. fclose(f);
  1022. #ifdef _WIN32
  1023. f = fopen("test.cgi", "wb");
  1024. cgi_script_content = "#!test.cgi.cmd\r\n";
  1025. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1026. fclose(f);
  1027. f = fopen("test.cgi.cmd", "w");
  1028. cgi_script_content = "@echo off\r\n"
  1029. "echo Connection: close\r\n"
  1030. "echo Content-Type: text/plain\r\n"
  1031. "echo.\r\n"
  1032. "echo CGI test\r\n"
  1033. "\r\n";
  1034. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1035. fclose(f);
  1036. #else
  1037. f = fopen("test.cgi", "w");
  1038. cgi_script_content = "#!/bin/sh\n\n"
  1039. "printf \"Connection: close\\r\\n\"\n"
  1040. "printf \"Content-Type: text/plain\\r\\n\"\n"
  1041. "printf \"\\r\\n\"\n"
  1042. "printf \"CGI test\\r\\n\"\n"
  1043. "\n";
  1044. (void)fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1045. (void)fclose(f);
  1046. (void)system("chmod a+x test.cgi");
  1047. #endif
  1048. expected_cgi_result = "CGI test";
  1049. /* Get static data */
  1050. client_conn = mg_download("localhost",
  1051. ipv4_port,
  1052. 0,
  1053. ebuf,
  1054. sizeof(ebuf),
  1055. "%s",
  1056. "GET /test.txt HTTP/1.0\r\n\r\n");
  1057. ck_assert(client_conn != NULL);
  1058. ri = mg_get_request_info(client_conn);
  1059. ck_assert(ri != NULL);
  1060. #if defined(NO_FILES)
  1061. ck_assert_str_eq(ri->uri, "404");
  1062. #else
  1063. ck_assert_str_eq(ri->uri, "200");
  1064. i = mg_read(client_conn, buf, sizeof(buf));
  1065. ck_assert_int_eq(i, 17);
  1066. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1067. buf[i] = 0;
  1068. }
  1069. ck_assert_str_eq(buf, plain_file_content);
  1070. #endif
  1071. mg_close_connection(client_conn);
  1072. /* Test with CGI test executable */
  1073. #if defined(_WIN32)
  1074. sprintf(cmd_buf, "copy %s\\cgi_test.cgi cgi_test.exe", locate_test_exes());
  1075. #else
  1076. sprintf(cmd_buf, "cp %s/cgi_test.cgi cgi_test.cgi", locate_test_exes());
  1077. #endif
  1078. (void)system(cmd_buf);
  1079. #if !defined(NO_CGI) && !defined(NO_FILES) && !defined(_WIN32)
  1080. /* TODO: add test for windows, check with POST */
  1081. client_conn = mg_download(
  1082. "localhost",
  1083. ipv4_port,
  1084. 0,
  1085. ebuf,
  1086. sizeof(ebuf),
  1087. "%s",
  1088. "POST /cgi_test.cgi HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC");
  1089. ck_assert(client_conn != NULL);
  1090. ri = mg_get_request_info(client_conn);
  1091. ck_assert(ri != NULL);
  1092. ck_assert_str_eq(ri->uri, "200");
  1093. mg_close_connection(client_conn);
  1094. #endif
  1095. /* Get zipped static data - will not work if Accept-Encoding is not set */
  1096. client_conn = mg_download("localhost",
  1097. ipv4_port,
  1098. 0,
  1099. ebuf,
  1100. sizeof(ebuf),
  1101. "%s",
  1102. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  1103. ck_assert(client_conn != NULL);
  1104. ri = mg_get_request_info(client_conn);
  1105. ck_assert(ri != NULL);
  1106. ck_assert_str_eq(ri->uri, "404");
  1107. mg_close_connection(client_conn);
  1108. /* Get zipped static data - with Accept-Encoding */
  1109. client_conn = mg_download(
  1110. "localhost",
  1111. ipv4_port,
  1112. 0,
  1113. ebuf,
  1114. sizeof(ebuf),
  1115. "%s",
  1116. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  1117. ck_assert(client_conn != NULL);
  1118. ri = mg_get_request_info(client_conn);
  1119. ck_assert(ri != NULL);
  1120. #if defined(NO_FILES)
  1121. ck_assert_str_eq(ri->uri, "404");
  1122. #else
  1123. ck_assert_str_eq(ri->uri, "200");
  1124. i = mg_read(client_conn, buf, sizeof(buf));
  1125. ck_assert_int_eq(i, 52);
  1126. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1127. buf[i] = 0;
  1128. }
  1129. ck_assert_int_eq(ri->content_length, 52);
  1130. ck_assert_str_eq(buf, encoded_file_content);
  1131. #endif
  1132. mg_close_connection(client_conn);
  1133. /* Get CGI generated data */
  1134. #if !defined(NO_CGI)
  1135. client_conn = mg_download("localhost",
  1136. ipv4_port,
  1137. 0,
  1138. ebuf,
  1139. sizeof(ebuf),
  1140. "%s",
  1141. "GET /test.cgi HTTP/1.0\r\n\r\n");
  1142. ck_assert(client_conn != NULL);
  1143. ri = mg_get_request_info(client_conn);
  1144. ck_assert(ri != NULL);
  1145. #if defined(NO_FILES)
  1146. ck_assert_str_eq(ri->uri, "404");
  1147. (void)expected_cgi_result;
  1148. (void)cgi_script_content;
  1149. #else
  1150. i = mg_read(client_conn, buf, sizeof(buf));
  1151. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1152. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  1153. i--;
  1154. }
  1155. buf[i] = 0;
  1156. }
  1157. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  1158. ck_assert_str_eq(buf, expected_cgi_result);
  1159. ck_assert_str_eq(ri->uri, "200");
  1160. mg_close_connection(client_conn);
  1161. #endif
  1162. #else
  1163. (void)expected_cgi_result;
  1164. (void)cgi_script_content;
  1165. #endif
  1166. /* Get directory listing */
  1167. client_conn = mg_download("localhost",
  1168. ipv4_port,
  1169. 0,
  1170. ebuf,
  1171. sizeof(ebuf),
  1172. "%s",
  1173. "GET / HTTP/1.0\r\n\r\n");
  1174. ck_assert(client_conn != NULL);
  1175. ri = mg_get_request_info(client_conn);
  1176. ck_assert(ri != NULL);
  1177. #if defined(NO_FILES)
  1178. ck_assert_str_eq(ri->uri, "404");
  1179. #else
  1180. ck_assert_str_eq(ri->uri, "200");
  1181. i = mg_read(client_conn, buf, sizeof(buf));
  1182. ck_assert(i > 6);
  1183. buf[6] = 0;
  1184. ck_assert_str_eq(buf, "<html>");
  1185. #endif
  1186. mg_close_connection(client_conn);
  1187. /* POST to static file (will not work) */
  1188. client_conn = mg_download("localhost",
  1189. ipv4_port,
  1190. 0,
  1191. ebuf,
  1192. sizeof(ebuf),
  1193. "%s",
  1194. "POST /test.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. #if defined(NO_FILES)
  1199. ck_assert_str_eq(ri->uri, "404");
  1200. #else
  1201. ck_assert_str_eq(ri->uri, "405");
  1202. i = mg_read(client_conn, buf, sizeof(buf));
  1203. ck_assert(i >= 29);
  1204. buf[29] = 0;
  1205. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  1206. #endif
  1207. mg_close_connection(client_conn);
  1208. /* PUT to static file (will not work) */
  1209. client_conn = mg_download("localhost",
  1210. ipv4_port,
  1211. 0,
  1212. ebuf,
  1213. sizeof(ebuf),
  1214. "%s",
  1215. "PUT /test.txt HTTP/1.0\r\n\r\n");
  1216. ck_assert(client_conn != NULL);
  1217. ri = mg_get_request_info(client_conn);
  1218. ck_assert(ri != NULL);
  1219. #if defined(NO_FILES)
  1220. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  1221. #else
  1222. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  1223. #endif
  1224. mg_close_connection(client_conn);
  1225. /* Get data from callback using mg_connect_client instead of mg_download */
  1226. memset(ebuf, 0, sizeof(ebuf));
  1227. client_conn =
  1228. mg_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf));
  1229. ck_assert(client_conn != NULL);
  1230. ck_assert_str_eq(ebuf, "");
  1231. mg_printf(client_conn, "%s", request);
  1232. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1233. ck_assert_int_ge(i, 0);
  1234. ck_assert_str_eq(ebuf, "");
  1235. ri = mg_get_request_info(client_conn);
  1236. ck_assert(ri != NULL);
  1237. ck_assert_str_eq(ri->uri, "200");
  1238. i = mg_read(client_conn, buf, sizeof(buf));
  1239. ck_assert_int_eq(i, (int)strlen(expected));
  1240. buf[i] = 0;
  1241. ck_assert_str_eq(buf, expected);
  1242. mg_close_connection(client_conn);
  1243. /* Get data from callback using mg_connect_client and absolute URI */
  1244. memset(ebuf, 0, sizeof(ebuf));
  1245. client_conn =
  1246. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1247. ck_assert(client_conn != NULL);
  1248. ck_assert_str_eq(ebuf, "");
  1249. mg_printf(client_conn,
  1250. "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1251. ipv4_port);
  1252. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1253. ck_assert_int_ge(i, 0);
  1254. ck_assert_str_eq(ebuf, "");
  1255. ri = mg_get_request_info(client_conn);
  1256. ck_assert(ri != NULL);
  1257. ck_assert_str_eq(ri->uri, "200");
  1258. i = mg_read(client_conn, buf, sizeof(buf));
  1259. ck_assert_int_eq(i, (int)strlen(expected));
  1260. buf[i] = 0;
  1261. ck_assert_str_eq(buf, expected);
  1262. mg_close_connection(client_conn);
  1263. /* Get data from callback using mg_connect_client and absolute URI with a
  1264. * sub-domain */
  1265. memset(ebuf, 0, sizeof(ebuf));
  1266. client_conn =
  1267. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1268. ck_assert(client_conn != NULL);
  1269. ck_assert_str_eq(ebuf, "");
  1270. mg_printf(client_conn,
  1271. "GET http://subdomain.test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1272. ipv4_port);
  1273. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1274. ck_assert_int_ge(i, 0);
  1275. ck_assert_str_eq(ebuf, "");
  1276. ri = mg_get_request_info(client_conn);
  1277. ck_assert(ri != NULL);
  1278. ck_assert_str_eq(ri->uri, "200");
  1279. i = mg_read(client_conn, buf, sizeof(buf));
  1280. ck_assert_int_eq(i, (int)strlen(expected));
  1281. buf[i] = 0;
  1282. ck_assert_str_eq(buf, expected);
  1283. mg_close_connection(client_conn);
  1284. /* Websocket test */
  1285. #ifdef USE_WEBSOCKET
  1286. /* Then connect a first client */
  1287. ws_client1_conn =
  1288. mg_connect_websocket_client("localhost",
  1289. ipv4_port,
  1290. 0,
  1291. ebuf,
  1292. sizeof(ebuf),
  1293. "/websocket",
  1294. NULL,
  1295. websocket_client_data_handler,
  1296. websocket_client_close_handler,
  1297. &ws_client1_data);
  1298. ck_assert(ws_client1_conn != NULL);
  1299. wait_not_null(
  1300. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1301. ck_assert_int_eq(ws_client1_data.closed, 0);
  1302. ck_assert_int_eq(ws_client2_data.closed, 0);
  1303. ck_assert_int_eq(ws_client3_data.closed, 0);
  1304. ck_assert(ws_client2_data.data == NULL);
  1305. ck_assert_uint_eq(ws_client2_data.len, 0);
  1306. ck_assert(ws_client1_data.data != NULL);
  1307. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1308. ck_assert(!memcmp(ws_client1_data.data,
  1309. websocket_welcome_msg,
  1310. websocket_welcome_msg_len));
  1311. free(ws_client1_data.data);
  1312. ws_client1_data.data = NULL;
  1313. ws_client1_data.len = 0;
  1314. mg_websocket_client_write(ws_client1_conn,
  1315. WEBSOCKET_OPCODE_TEXT,
  1316. "data1",
  1317. 5);
  1318. wait_not_null(
  1319. &(ws_client1_data
  1320. .data)); /* Wait for the websocket acknowledge message */
  1321. ck_assert_int_eq(ws_client1_data.closed, 0);
  1322. ck_assert_int_eq(ws_client2_data.closed, 0);
  1323. ck_assert(ws_client2_data.data == NULL);
  1324. ck_assert_uint_eq(ws_client2_data.len, 0);
  1325. ck_assert(ws_client1_data.data != NULL);
  1326. ck_assert_uint_eq(ws_client1_data.len, 3);
  1327. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1328. free(ws_client1_data.data);
  1329. ws_client1_data.data = NULL;
  1330. ws_client1_data.len = 0;
  1331. /* Now connect a second client */
  1332. #ifdef USE_IPV6
  1333. ws_client2_conn =
  1334. mg_connect_websocket_client("[::1]",
  1335. ipv6_port,
  1336. 0,
  1337. ebuf,
  1338. sizeof(ebuf),
  1339. "/websocket",
  1340. NULL,
  1341. websocket_client_data_handler,
  1342. websocket_client_close_handler,
  1343. &ws_client2_data);
  1344. #else
  1345. ws_client2_conn =
  1346. mg_connect_websocket_client("127.0.0.1",
  1347. ipv4_port,
  1348. 0,
  1349. ebuf,
  1350. sizeof(ebuf),
  1351. "/websocket",
  1352. NULL,
  1353. websocket_client_data_handler,
  1354. websocket_client_close_handler,
  1355. &ws_client2_data);
  1356. #endif
  1357. ck_assert(ws_client2_conn != NULL);
  1358. wait_not_null(
  1359. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1360. ck_assert(ws_client1_data.closed == 0);
  1361. ck_assert(ws_client2_data.closed == 0);
  1362. ck_assert(ws_client1_data.data == NULL);
  1363. ck_assert(ws_client1_data.len == 0);
  1364. ck_assert(ws_client2_data.data != NULL);
  1365. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1366. ck_assert(!memcmp(ws_client2_data.data,
  1367. websocket_welcome_msg,
  1368. websocket_welcome_msg_len));
  1369. free(ws_client2_data.data);
  1370. ws_client2_data.data = NULL;
  1371. ws_client2_data.len = 0;
  1372. mg_websocket_client_write(ws_client1_conn,
  1373. WEBSOCKET_OPCODE_TEXT,
  1374. "data2",
  1375. 5);
  1376. wait_not_null(
  1377. &(ws_client1_data
  1378. .data)); /* Wait for the websocket acknowledge message */
  1379. ck_assert(ws_client1_data.closed == 0);
  1380. ck_assert(ws_client2_data.closed == 0);
  1381. ck_assert(ws_client2_data.data == NULL);
  1382. ck_assert(ws_client2_data.len == 0);
  1383. ck_assert(ws_client1_data.data != NULL);
  1384. ck_assert(ws_client1_data.len == 4);
  1385. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1386. free(ws_client1_data.data);
  1387. ws_client1_data.data = NULL;
  1388. ws_client1_data.len = 0;
  1389. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1390. wait_not_null(
  1391. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1392. ck_assert(ws_client1_data.closed == 0);
  1393. ck_assert(ws_client2_data.closed == 0);
  1394. ck_assert(ws_client2_data.data == NULL);
  1395. ck_assert(ws_client2_data.len == 0);
  1396. ck_assert(ws_client1_data.data != NULL);
  1397. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1398. ck_assert(!memcmp(ws_client1_data.data,
  1399. websocket_goodbye_msg,
  1400. websocket_goodbye_msg_len));
  1401. free(ws_client1_data.data);
  1402. ws_client1_data.data = NULL;
  1403. ws_client1_data.len = 0;
  1404. mg_close_connection(ws_client1_conn);
  1405. test_sleep(3); /* Won't get any message */
  1406. ck_assert(ws_client1_data.closed == 1);
  1407. ck_assert(ws_client2_data.closed == 0);
  1408. ck_assert(ws_client1_data.data == NULL);
  1409. ck_assert(ws_client1_data.len == 0);
  1410. ck_assert(ws_client2_data.data == NULL);
  1411. ck_assert(ws_client2_data.len == 0);
  1412. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1413. wait_not_null(
  1414. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1415. ck_assert(ws_client1_data.closed == 1);
  1416. ck_assert(ws_client2_data.closed == 0);
  1417. ck_assert(ws_client1_data.data == NULL);
  1418. ck_assert(ws_client1_data.len == 0);
  1419. ck_assert(ws_client2_data.data != NULL);
  1420. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1421. ck_assert(!memcmp(ws_client2_data.data,
  1422. websocket_goodbye_msg,
  1423. websocket_goodbye_msg_len));
  1424. free(ws_client2_data.data);
  1425. ws_client2_data.data = NULL;
  1426. ws_client2_data.len = 0;
  1427. mg_close_connection(ws_client2_conn);
  1428. test_sleep(3); /* Won't get any message */
  1429. ck_assert(ws_client1_data.closed == 1);
  1430. ck_assert(ws_client2_data.closed == 1);
  1431. ck_assert(ws_client1_data.data == NULL);
  1432. ck_assert(ws_client1_data.len == 0);
  1433. ck_assert(ws_client2_data.data == NULL);
  1434. ck_assert(ws_client2_data.len == 0);
  1435. /* Connect client 3 */
  1436. ws_client3_conn =
  1437. mg_connect_websocket_client("localhost",
  1438. #if defined(NO_SSL)
  1439. ipv4_port,
  1440. 0,
  1441. #else
  1442. ipv4s_port,
  1443. 1,
  1444. #endif
  1445. ebuf,
  1446. sizeof(ebuf),
  1447. "/websocket",
  1448. NULL,
  1449. websocket_client_data_handler,
  1450. websocket_client_close_handler,
  1451. &ws_client3_data);
  1452. ck_assert(ws_client3_conn != NULL);
  1453. wait_not_null(
  1454. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1455. ck_assert(ws_client1_data.closed == 1);
  1456. ck_assert(ws_client2_data.closed == 1);
  1457. ck_assert(ws_client3_data.closed == 0);
  1458. ck_assert(ws_client1_data.data == NULL);
  1459. ck_assert(ws_client1_data.len == 0);
  1460. ck_assert(ws_client2_data.data == NULL);
  1461. ck_assert(ws_client2_data.len == 0);
  1462. ck_assert(ws_client3_data.data != NULL);
  1463. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1464. ck_assert(!memcmp(ws_client3_data.data,
  1465. websocket_welcome_msg,
  1466. websocket_welcome_msg_len));
  1467. free(ws_client3_data.data);
  1468. ws_client3_data.data = NULL;
  1469. ws_client3_data.len = 0;
  1470. /* Disconnect client 3 */
  1471. ck_assert(ws_client3_data.closed == 0);
  1472. mg_close_connection(ws_client3_conn);
  1473. ck_assert(ws_client3_data.closed == 1);
  1474. /* Connect client 4 */
  1475. ws_client4_conn =
  1476. mg_connect_websocket_client("localhost",
  1477. #if defined(NO_SSL)
  1478. ipv4_port,
  1479. 0,
  1480. #else
  1481. ipv4s_port,
  1482. 1,
  1483. #endif
  1484. ebuf,
  1485. sizeof(ebuf),
  1486. "/websocket",
  1487. NULL,
  1488. websocket_client_data_handler,
  1489. websocket_client_close_handler,
  1490. &ws_client4_data);
  1491. ck_assert(ws_client4_conn != NULL);
  1492. wait_not_null(
  1493. &(ws_client4_data.data)); /* Wait for the websocket welcome message */
  1494. ck_assert(ws_client1_data.closed == 1);
  1495. ck_assert(ws_client2_data.closed == 1);
  1496. ck_assert(ws_client3_data.closed == 1);
  1497. ck_assert(ws_client4_data.closed == 0);
  1498. ck_assert(ws_client4_data.data != NULL);
  1499. ck_assert(ws_client4_data.len == websocket_welcome_msg_len);
  1500. ck_assert(!memcmp(ws_client4_data.data,
  1501. websocket_welcome_msg,
  1502. websocket_welcome_msg_len));
  1503. free(ws_client4_data.data);
  1504. ws_client4_data.data = NULL;
  1505. ws_client4_data.len = 0;
  1506. /* stop the server without closing this connection */
  1507. #endif
  1508. /* Close the server */
  1509. g_ctx = NULL;
  1510. test_mg_stop(ctx);
  1511. mark_point();
  1512. #ifdef USE_WEBSOCKET
  1513. for (i = 0; i < 100; i++) {
  1514. test_sleep(1);
  1515. if (ws_client3_data.closed != 0) {
  1516. mark_point();
  1517. break;
  1518. }
  1519. }
  1520. ck_assert_int_eq(ws_client4_data.closed, 1);
  1521. #endif
  1522. }
  1523. END_TEST
  1524. static int g_field_found_return = -999;
  1525. static int
  1526. field_found(const char *key,
  1527. const char *filename,
  1528. char *path,
  1529. size_t pathlen,
  1530. void *user_data)
  1531. {
  1532. ck_assert_ptr_ne(key, NULL);
  1533. ck_assert_ptr_ne(filename, NULL);
  1534. ck_assert_ptr_ne(path, NULL);
  1535. ck_assert_uint_gt(pathlen, 128);
  1536. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1537. ck_assert((g_field_found_return == FORM_FIELD_STORAGE_GET)
  1538. || (g_field_found_return == FORM_FIELD_STORAGE_STORE)
  1539. || (g_field_found_return == FORM_FIELD_STORAGE_SKIP)
  1540. || (g_field_found_return == FORM_FIELD_STORAGE_ABORT));
  1541. ck_assert_str_ne(key, "dontread");
  1542. if (!strcmp(key, "break_field_handler")) {
  1543. return FORM_FIELD_STORAGE_ABORT;
  1544. }
  1545. if (!strcmp(key, "continue_field_handler")) {
  1546. return FORM_FIELD_STORAGE_SKIP;
  1547. }
  1548. if (g_field_found_return == FORM_FIELD_STORAGE_STORE) {
  1549. strncpy(path, key, pathlen - 8);
  1550. strcat(path, ".txt");
  1551. }
  1552. return g_field_found_return;
  1553. }
  1554. static int g_field_step;
  1555. static int
  1556. field_get(const char *key,
  1557. const char *value_untruncated,
  1558. size_t valuelen,
  1559. void *user_data)
  1560. {
  1561. /* Copy the untruncated value, so string compare functions can be used. */
  1562. /* The check unit test library does not have build in memcmp functions. */
  1563. char *value = (char *)malloc(valuelen + 1);
  1564. ck_assert(value != NULL);
  1565. memcpy(value, value_untruncated, valuelen);
  1566. value[valuelen] = 0;
  1567. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1568. ck_assert_int_ge(g_field_step, 0);
  1569. ++g_field_step;
  1570. switch (g_field_step) {
  1571. case 1:
  1572. ck_assert_str_eq(key, "textin");
  1573. ck_assert_uint_eq(valuelen, 4);
  1574. ck_assert_str_eq(value, "text");
  1575. break;
  1576. case 2:
  1577. ck_assert_str_eq(key, "passwordin");
  1578. ck_assert_uint_eq(valuelen, 0);
  1579. ck_assert_str_eq(value, "");
  1580. break;
  1581. case 3:
  1582. ck_assert_str_eq(key, "radio1");
  1583. ck_assert_uint_eq(valuelen, 4);
  1584. ck_assert_str_eq(value, "val1");
  1585. break;
  1586. case 4:
  1587. ck_assert_str_eq(key, "radio2");
  1588. ck_assert_uint_eq(valuelen, 4);
  1589. ck_assert_str_eq(value, "val1");
  1590. break;
  1591. case 5:
  1592. ck_assert_str_eq(key, "check1");
  1593. ck_assert_uint_eq(valuelen, 4);
  1594. ck_assert_str_eq(value, "val1");
  1595. break;
  1596. case 6:
  1597. ck_assert_str_eq(key, "numberin");
  1598. ck_assert_uint_eq(valuelen, 1);
  1599. ck_assert_str_eq(value, "1");
  1600. break;
  1601. case 7:
  1602. ck_assert_str_eq(key, "datein");
  1603. ck_assert_uint_eq(valuelen, 8);
  1604. ck_assert_str_eq(value, "1.1.2016");
  1605. break;
  1606. case 8:
  1607. ck_assert_str_eq(key, "colorin");
  1608. ck_assert_uint_eq(valuelen, 7);
  1609. ck_assert_str_eq(value, "#80ff00");
  1610. break;
  1611. case 9:
  1612. ck_assert_str_eq(key, "rangein");
  1613. ck_assert_uint_eq(valuelen, 1);
  1614. ck_assert_str_eq(value, "3");
  1615. break;
  1616. case 10:
  1617. ck_assert_str_eq(key, "monthin");
  1618. ck_assert_uint_eq(valuelen, 0);
  1619. ck_assert_str_eq(value, "");
  1620. break;
  1621. case 11:
  1622. ck_assert_str_eq(key, "weekin");
  1623. ck_assert_uint_eq(valuelen, 0);
  1624. ck_assert_str_eq(value, "");
  1625. break;
  1626. case 12:
  1627. ck_assert_str_eq(key, "timein");
  1628. ck_assert_uint_eq(valuelen, 0);
  1629. ck_assert_str_eq(value, "");
  1630. break;
  1631. case 13:
  1632. ck_assert_str_eq(key, "datetimen");
  1633. ck_assert_uint_eq(valuelen, 0);
  1634. ck_assert_str_eq(value, "");
  1635. break;
  1636. case 14:
  1637. ck_assert_str_eq(key, "datetimelocalin");
  1638. ck_assert_uint_eq(valuelen, 0);
  1639. ck_assert_str_eq(value, "");
  1640. break;
  1641. case 15:
  1642. ck_assert_str_eq(key, "emailin");
  1643. ck_assert_uint_eq(valuelen, 0);
  1644. ck_assert_str_eq(value, "");
  1645. break;
  1646. case 16:
  1647. ck_assert_str_eq(key, "searchin");
  1648. ck_assert_uint_eq(valuelen, 0);
  1649. ck_assert_str_eq(value, "");
  1650. break;
  1651. case 17:
  1652. ck_assert_str_eq(key, "telin");
  1653. ck_assert_uint_eq(valuelen, 0);
  1654. ck_assert_str_eq(value, "");
  1655. break;
  1656. case 18:
  1657. ck_assert_str_eq(key, "urlin");
  1658. ck_assert_uint_eq(valuelen, 0);
  1659. ck_assert_str_eq(value, "");
  1660. break;
  1661. case 19:
  1662. ck_assert_str_eq(key, "filein");
  1663. ck_assert_uint_eq(valuelen, 0);
  1664. ck_assert_str_eq(value, "");
  1665. break;
  1666. case 20:
  1667. ck_assert_str_eq(key, "filesin");
  1668. ck_assert_uint_eq(valuelen, 0);
  1669. ck_assert_str_eq(value, "");
  1670. break;
  1671. case 21:
  1672. ck_assert_str_eq(key, "selectin");
  1673. ck_assert_uint_eq(valuelen, 4);
  1674. ck_assert_str_eq(value, "opt1");
  1675. break;
  1676. case 22:
  1677. ck_assert_str_eq(key, "message");
  1678. ck_assert_uint_eq(valuelen, 23);
  1679. ck_assert_str_eq(value, "Text area default text.");
  1680. break;
  1681. default:
  1682. ck_abort_msg("field_get called with g_field_step == %i",
  1683. (int)g_field_step);
  1684. }
  1685. free(value);
  1686. return 0;
  1687. }
  1688. static const char *myfile_content = "Content of myfile.txt\r\n";
  1689. static const int myfile_content_rep = 500;
  1690. static int
  1691. field_store(const char *path, long long file_size, void *user_data)
  1692. {
  1693. FILE *f;
  1694. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1695. ck_assert_int_ge(g_field_step, 100);
  1696. ++g_field_step;
  1697. switch (g_field_step) {
  1698. case 101:
  1699. ck_assert_str_eq(path, "storeme.txt");
  1700. ck_assert_int_eq(file_size, 9);
  1701. f = fopen(path, "r");
  1702. ck_assert_ptr_ne(f, NULL);
  1703. if (f) {
  1704. char buf[32] = {0};
  1705. int i = (int)fread(buf, 1, 31, f);
  1706. ck_assert_int_eq(i, 9);
  1707. fclose(f);
  1708. ck_assert_str_eq(buf, "storetest");
  1709. }
  1710. break;
  1711. case 102:
  1712. ck_assert_str_eq(path, "file2store.txt");
  1713. ck_assert_uint_eq(23, strlen(myfile_content));
  1714. ck_assert_int_eq(file_size, 23 * myfile_content_rep);
  1715. #ifdef _WIN32
  1716. f = fopen(path, "rb");
  1717. #else
  1718. f = fopen(path, "r");
  1719. #endif
  1720. ck_assert_ptr_ne(f, NULL);
  1721. if (f) {
  1722. char buf[32] = {0};
  1723. int r, i;
  1724. for (r = 0; r < myfile_content_rep; r++) {
  1725. i = (int)fread(buf, 1, 23, f);
  1726. ck_assert_int_eq(i, 23);
  1727. ck_assert_str_eq(buf, myfile_content);
  1728. }
  1729. i = (int)fread(buf, 1, 23, f);
  1730. ck_assert_int_eq(i, 0);
  1731. fclose(f);
  1732. }
  1733. break;
  1734. default:
  1735. ck_abort_msg("field_get called with g_field_step == %i",
  1736. (int)g_field_step);
  1737. }
  1738. return 0;
  1739. }
  1740. static int
  1741. FormGet(struct mg_connection *conn, void *cbdata)
  1742. {
  1743. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1744. int ret;
  1745. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  1746. (void)cbdata;
  1747. ck_assert(req_info != NULL);
  1748. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1749. fdh.user_data = (void *)&g_field_found_return;
  1750. /* Call the form handler */
  1751. g_field_step = 0;
  1752. g_field_found_return = FORM_FIELD_STORAGE_GET;
  1753. ret = mg_handle_form_request(conn, &fdh);
  1754. g_field_found_return = -888;
  1755. ck_assert_int_eq(ret, 22);
  1756. ck_assert_int_eq(g_field_step, 22);
  1757. mg_printf(conn, "%i\r\n", ret);
  1758. g_field_step = 1000;
  1759. return 1;
  1760. }
  1761. static int
  1762. FormStore(struct mg_connection *conn,
  1763. void *cbdata,
  1764. int ret_expected,
  1765. int field_step_expected)
  1766. {
  1767. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1768. int ret;
  1769. struct mg_form_data_handler fdh = {field_found,
  1770. field_get,
  1771. field_store,
  1772. NULL};
  1773. (void)cbdata;
  1774. ck_assert(req_info != NULL);
  1775. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1776. fdh.user_data = (void *)&g_field_found_return;
  1777. /* Call the form handler */
  1778. g_field_step = 100;
  1779. g_field_found_return = FORM_FIELD_STORAGE_STORE;
  1780. ret = mg_handle_form_request(conn, &fdh);
  1781. ck_assert_int_eq(ret, ret_expected);
  1782. ck_assert_int_eq(g_field_step, field_step_expected);
  1783. mg_printf(conn, "%i\r\n", ret);
  1784. g_field_step = 1000;
  1785. return 1;
  1786. }
  1787. static int
  1788. FormStore1(struct mg_connection *conn, void *cbdata)
  1789. {
  1790. return FormStore(conn, cbdata, 3, 101);
  1791. }
  1792. static int
  1793. FormStore2(struct mg_connection *conn, void *cbdata)
  1794. {
  1795. return FormStore(conn, cbdata, 4, 102);
  1796. }
  1797. static void
  1798. send_chunk_stringl(struct mg_connection *conn,
  1799. const char *chunk,
  1800. unsigned int chunk_len)
  1801. {
  1802. char lenbuf[16];
  1803. size_t lenbuf_len;
  1804. int ret;
  1805. /* First store the length information in a text buffer. */
  1806. sprintf(lenbuf, "%x\r\n", chunk_len);
  1807. lenbuf_len = strlen(lenbuf);
  1808. /* Then send length information, chunk and terminating \r\n. */
  1809. ret = mg_write(conn, lenbuf, lenbuf_len);
  1810. ck_assert_int_eq(ret, (int)lenbuf_len);
  1811. ret = mg_write(conn, chunk, chunk_len);
  1812. ck_assert_int_eq(ret, (int)chunk_len);
  1813. ret = mg_write(conn, "\r\n", 2);
  1814. ck_assert_int_eq(ret, 2);
  1815. }
  1816. static void
  1817. send_chunk_string(struct mg_connection *conn, const char *chunk)
  1818. {
  1819. send_chunk_stringl(conn, chunk, (unsigned int)strlen(chunk));
  1820. }
  1821. START_TEST(test_handle_form)
  1822. {
  1823. struct mg_context *ctx;
  1824. struct mg_connection *client_conn;
  1825. const struct mg_request_info *ri;
  1826. const char *OPTIONS[8];
  1827. const char *opt;
  1828. int opt_idx = 0;
  1829. char ebuf[100];
  1830. const char *multipart_body;
  1831. const char *boundary;
  1832. size_t body_len, body_sent, chunk_len;
  1833. int sleep_cnt;
  1834. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  1835. OPTIONS[opt_idx++] = "listening_ports";
  1836. OPTIONS[opt_idx++] = "8884";
  1837. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  1838. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  1839. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  1840. ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
  1841. ck_assert(ctx != NULL);
  1842. g_ctx = ctx;
  1843. opt = mg_get_option(ctx, "listening_ports");
  1844. ck_assert_str_eq(opt, "8884");
  1845. mg_set_request_handler(ctx, "/handle_form", FormGet, (void *)0);
  1846. mg_set_request_handler(ctx, "/handle_form_store", FormStore1, (void *)0);
  1847. mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, (void *)0);
  1848. test_sleep(1);
  1849. /* Handle form: "GET" */
  1850. client_conn = mg_download("localhost",
  1851. 8884,
  1852. 0,
  1853. ebuf,
  1854. sizeof(ebuf),
  1855. "%s",
  1856. "GET /handle_form"
  1857. "?textin=text&passwordin=&radio1=val1"
  1858. "&radio2=val1&check1=val1&numberin=1"
  1859. "&datein=1.1.2016&colorin=%2380ff00"
  1860. "&rangein=3&monthin=&weekin=&timein="
  1861. "&datetimen=&datetimelocalin=&emailin="
  1862. "&searchin=&telin=&urlin=&filein="
  1863. "&filesin=&selectin=opt1"
  1864. "&message=Text+area+default+text. "
  1865. "HTTP/1.0\r\n"
  1866. "Host: localhost:8884\r\n"
  1867. "Connection: close\r\n\r\n");
  1868. ck_assert(client_conn != NULL);
  1869. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1870. test_sleep(1);
  1871. if (g_field_step == 1000) {
  1872. break;
  1873. }
  1874. }
  1875. ri = mg_get_request_info(client_conn);
  1876. ck_assert(ri != NULL);
  1877. ck_assert_str_eq(ri->uri, "200");
  1878. mg_close_connection(client_conn);
  1879. /* Handle form: "POST x-www-form-urlencoded" */
  1880. client_conn =
  1881. mg_download("localhost",
  1882. 8884,
  1883. 0,
  1884. ebuf,
  1885. sizeof(ebuf),
  1886. "%s",
  1887. "POST /handle_form HTTP/1.1\r\n"
  1888. "Host: localhost:8884\r\n"
  1889. "Connection: close\r\n"
  1890. "Content-Type: application/x-www-form-urlencoded\r\n"
  1891. "Content-Length: 263\r\n"
  1892. "\r\n"
  1893. "textin=text&passwordin=&radio1=val1&radio2=val1"
  1894. "&check1=val1&numberin=1&datein=1.1.2016"
  1895. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  1896. "&timein=&datetimen=&datetimelocalin=&emailin="
  1897. "&searchin=&telin=&urlin=&filein=&filesin="
  1898. "&selectin=opt1&message=Text+area+default+text.");
  1899. ck_assert(client_conn != NULL);
  1900. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1901. test_sleep(1);
  1902. if (g_field_step == 1000) {
  1903. break;
  1904. }
  1905. }
  1906. ri = mg_get_request_info(client_conn);
  1907. ck_assert(ri != NULL);
  1908. ck_assert_str_eq(ri->uri, "200");
  1909. mg_close_connection(client_conn);
  1910. /* Handle form: "POST multipart/form-data" */
  1911. multipart_body =
  1912. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1913. "Content-Disposition: form-data; name=\"textin\"\r\n"
  1914. "\r\n"
  1915. "text\r\n"
  1916. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1917. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  1918. "\r\n"
  1919. "\r\n"
  1920. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1921. "Content-Disposition: form-data; name=\"radio1\"\r\n"
  1922. "\r\n"
  1923. "val1\r\n"
  1924. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1925. "Content-Disposition: form-data; name=\"radio2\"\r\n"
  1926. "\r\n"
  1927. "val1\r\n"
  1928. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1929. "Content-Disposition: form-data; name=\"check1\"\r\n"
  1930. "\r\n"
  1931. "val1\r\n"
  1932. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1933. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  1934. "\r\n"
  1935. "1\r\n"
  1936. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1937. "Content-Disposition: form-data; name=\"datein\"\r\n"
  1938. "\r\n"
  1939. "1.1.2016\r\n"
  1940. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1941. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  1942. "\r\n"
  1943. "#80ff00\r\n"
  1944. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1945. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  1946. "\r\n"
  1947. "3\r\n"
  1948. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1949. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  1950. "\r\n"
  1951. "\r\n"
  1952. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1953. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  1954. "\r\n"
  1955. "\r\n"
  1956. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1957. "Content-Disposition: form-data; name=\"timein\"\r\n"
  1958. "\r\n"
  1959. "\r\n"
  1960. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1961. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  1962. "\r\n"
  1963. "\r\n"
  1964. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1965. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  1966. "\r\n"
  1967. "\r\n"
  1968. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1969. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  1970. "\r\n"
  1971. "\r\n"
  1972. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1973. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  1974. "\r\n"
  1975. "\r\n"
  1976. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1977. "Content-Disposition: form-data; name=\"telin\"\r\n"
  1978. "\r\n"
  1979. "\r\n"
  1980. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1981. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  1982. "\r\n"
  1983. "\r\n"
  1984. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1985. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  1986. "Content-Type: application/octet-stream\r\n"
  1987. "\r\n"
  1988. "\r\n"
  1989. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1990. "Content-Disposition: form-data; name=\"filesin\"; filename=\"\"\r\n"
  1991. "Content-Type: application/octet-stream\r\n"
  1992. "\r\n"
  1993. "\r\n"
  1994. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1995. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  1996. "\r\n"
  1997. "opt1\r\n"
  1998. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1999. "Content-Disposition: form-data; name=\"message\"\r\n"
  2000. "\r\n"
  2001. "Text area default text.\r\n"
  2002. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  2003. body_len = strlen(multipart_body);
  2004. ck_assert_uint_eq(body_len, 2374); /* not required */
  2005. client_conn =
  2006. mg_download("localhost",
  2007. 8884,
  2008. 0,
  2009. ebuf,
  2010. sizeof(ebuf),
  2011. "POST /handle_form HTTP/1.1\r\n"
  2012. "Host: localhost:8884\r\n"
  2013. "Connection: close\r\n"
  2014. "Content-Type: multipart/form-data; "
  2015. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2016. "Content-Length: %u\r\n"
  2017. "\r\n%s",
  2018. (unsigned int)body_len,
  2019. multipart_body);
  2020. ck_assert(client_conn != NULL);
  2021. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2022. test_sleep(1);
  2023. if (g_field_step == 1000) {
  2024. break;
  2025. }
  2026. }
  2027. ri = mg_get_request_info(client_conn);
  2028. ck_assert(ri != NULL);
  2029. ck_assert_str_eq(ri->uri, "200");
  2030. mg_close_connection(client_conn);
  2031. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  2032. client_conn =
  2033. mg_download("localhost",
  2034. 8884,
  2035. 0,
  2036. ebuf,
  2037. sizeof(ebuf),
  2038. "%s",
  2039. "POST /handle_form HTTP/1.1\r\n"
  2040. "Host: localhost:8884\r\n"
  2041. "Connection: close\r\n"
  2042. "Content-Type: multipart/form-data; "
  2043. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2044. "Transfer-Encoding: chunked\r\n"
  2045. "\r\n");
  2046. ck_assert(client_conn != NULL);
  2047. body_len = strlen(multipart_body);
  2048. chunk_len = 1;
  2049. body_sent = 0;
  2050. while (body_len > body_sent) {
  2051. if (chunk_len > (body_len - body_sent)) {
  2052. chunk_len = body_len - body_sent;
  2053. }
  2054. ck_assert_int_gt((int)chunk_len, 0);
  2055. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2056. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2057. mg_printf(client_conn, "\r\n");
  2058. body_sent += chunk_len;
  2059. chunk_len = (chunk_len % 40) + 1;
  2060. }
  2061. mg_printf(client_conn, "0\r\n");
  2062. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2063. test_sleep(1);
  2064. if (g_field_step == 1000) {
  2065. break;
  2066. }
  2067. }
  2068. ri = mg_get_request_info(client_conn);
  2069. ck_assert(ri != NULL);
  2070. ck_assert_str_eq(ri->uri, "200");
  2071. mg_close_connection(client_conn);
  2072. /* Now test form_store */
  2073. /* First test with GET */
  2074. client_conn = mg_download("localhost",
  2075. 8884,
  2076. 0,
  2077. ebuf,
  2078. sizeof(ebuf),
  2079. "%s",
  2080. "GET /handle_form_store"
  2081. "?storeme=storetest"
  2082. "&continue_field_handler=ignore"
  2083. "&break_field_handler=abort"
  2084. "&dontread=xyz "
  2085. "HTTP/1.0\r\n"
  2086. "Host: localhost:8884\r\n"
  2087. "Connection: close\r\n\r\n");
  2088. ck_assert(client_conn != NULL);
  2089. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2090. test_sleep(1);
  2091. if (g_field_step == 1000) {
  2092. break;
  2093. }
  2094. }
  2095. ri = mg_get_request_info(client_conn);
  2096. ck_assert(ri != NULL);
  2097. ck_assert_str_eq(ri->uri, "200");
  2098. mg_close_connection(client_conn);
  2099. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  2100. client_conn =
  2101. mg_download("localhost",
  2102. 8884,
  2103. 0,
  2104. ebuf,
  2105. sizeof(ebuf),
  2106. "%s",
  2107. "POST /handle_form_store HTTP/1.0\r\n"
  2108. "Host: localhost:8884\r\n"
  2109. "Connection: close\r\n"
  2110. "Content-Type: application/x-www-form-urlencoded\r\n"
  2111. "Transfer-Encoding: chunked\r\n"
  2112. "\r\n");
  2113. ck_assert(client_conn != NULL);
  2114. send_chunk_string(client_conn, "storeme=store");
  2115. send_chunk_string(client_conn, "test&");
  2116. send_chunk_string(client_conn, "continue_field_handler=ignore");
  2117. send_chunk_string(client_conn, "&br");
  2118. test_sleep(1);
  2119. send_chunk_string(client_conn, "eak_field_handler=abort&");
  2120. send_chunk_string(client_conn, "dontread=xyz");
  2121. mg_printf(client_conn, "0\r\n");
  2122. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2123. test_sleep(1);
  2124. if (g_field_step == 1000) {
  2125. break;
  2126. }
  2127. }
  2128. ri = mg_get_request_info(client_conn);
  2129. ck_assert(ri != NULL);
  2130. ck_assert_str_eq(ri->uri, "200");
  2131. mg_close_connection(client_conn);
  2132. /* Handle form: "POST multipart/form-data", chunked, store */
  2133. client_conn =
  2134. mg_download("localhost",
  2135. 8884,
  2136. 0,
  2137. ebuf,
  2138. sizeof(ebuf),
  2139. "%s",
  2140. "POST /handle_form_store HTTP/1.0\r\n"
  2141. "Host: localhost:8884\r\n"
  2142. "Connection: close\r\n"
  2143. "Content-Type: multipart/form-data; "
  2144. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2145. "Transfer-Encoding: chunked\r\n"
  2146. "\r\n");
  2147. ck_assert(client_conn != NULL);
  2148. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2149. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  2150. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2151. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2152. send_chunk_string(client_conn, "\r\n");
  2153. send_chunk_string(client_conn, "storetest\r\n");
  2154. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2155. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2156. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2157. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  2158. send_chunk_string(client_conn, "\r\n");
  2159. send_chunk_string(client_conn, "ignore\r\n");
  2160. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2161. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2162. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2163. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2164. send_chunk_string(client_conn, "\r\n");
  2165. send_chunk_string(client_conn, "abort\r\n");
  2166. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2167. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2168. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2169. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2170. send_chunk_string(client_conn, "\r\n");
  2171. send_chunk_string(client_conn, "xyz\r\n");
  2172. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2173. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2174. mg_printf(client_conn, "0\r\n");
  2175. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2176. test_sleep(1);
  2177. if (g_field_step == 1000) {
  2178. break;
  2179. }
  2180. }
  2181. ri = mg_get_request_info(client_conn);
  2182. ck_assert(ri != NULL);
  2183. ck_assert_str_eq(ri->uri, "200");
  2184. mg_close_connection(client_conn);
  2185. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  2186. client_conn =
  2187. mg_download("localhost",
  2188. 8884,
  2189. 0,
  2190. ebuf,
  2191. sizeof(ebuf),
  2192. "%s",
  2193. "POST /handle_form_store2 HTTP/1.0\r\n"
  2194. "Host: localhost:8884\r\n"
  2195. "Connection: close\r\n"
  2196. "Content-Type: multipart/form-data; "
  2197. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2198. "Transfer-Encoding: chunked\r\n"
  2199. "\r\n");
  2200. ck_assert(client_conn != NULL);
  2201. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  2202. send_chunk_string(client_conn, boundary);
  2203. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2204. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2205. send_chunk_string(client_conn, "\r\n");
  2206. send_chunk_string(client_conn, "storetest\r\n");
  2207. send_chunk_string(client_conn, boundary);
  2208. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2209. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  2210. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  2211. send_chunk_string(client_conn, "Content-Type: ");
  2212. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2213. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2214. send_chunk_string(client_conn, "\r\n");
  2215. /* send some kilobyte of data */
  2216. /* sending megabytes to localhost does not allways work in CI test
  2217. * environments (depending on the network stack) */
  2218. body_sent = 0;
  2219. do {
  2220. send_chunk_string(client_conn, "ignore\r\n");
  2221. body_sent += 8;
  2222. /* send some strings that are almost boundaries */
  2223. for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
  2224. /* chunks from 1 byte to strlen(boundary)-1 */
  2225. send_chunk_stringl(client_conn, boundary, (unsigned int)chunk_len);
  2226. body_sent += chunk_len;
  2227. }
  2228. } while (body_sent < 8 * 1024);
  2229. send_chunk_string(client_conn, "\r\n");
  2230. send_chunk_string(client_conn, boundary);
  2231. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2232. send_chunk_string(client_conn, "name=\"file2store\";");
  2233. send_chunk_string(client_conn, "filename=\"myfile.txt\"\r\n");
  2234. send_chunk_string(client_conn, "Content-Type: ");
  2235. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2236. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2237. send_chunk_string(client_conn, "\r\n");
  2238. for (body_sent = 0; (int)body_sent < (int)myfile_content_rep; body_sent++) {
  2239. send_chunk_string(client_conn, myfile_content);
  2240. }
  2241. send_chunk_string(client_conn, "\r\n");
  2242. send_chunk_string(client_conn, boundary);
  2243. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2244. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2245. send_chunk_string(client_conn, "\r\n");
  2246. send_chunk_string(client_conn, "abort\r\n");
  2247. send_chunk_string(client_conn, boundary);
  2248. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2249. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2250. send_chunk_string(client_conn, "\r\n");
  2251. send_chunk_string(client_conn, "xyz\r\n");
  2252. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2253. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2254. mg_printf(client_conn, "0\r\n");
  2255. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2256. test_sleep(1);
  2257. if (g_field_step == 1000) {
  2258. break;
  2259. }
  2260. }
  2261. ri = mg_get_request_info(client_conn);
  2262. ck_assert(ri != NULL);
  2263. ck_assert_str_eq(ri->uri, "200");
  2264. mg_close_connection(client_conn);
  2265. /* Close the server */
  2266. g_ctx = NULL;
  2267. test_mg_stop(ctx);
  2268. mark_point();
  2269. }
  2270. END_TEST
  2271. START_TEST(test_http_auth)
  2272. {
  2273. #if !defined(NO_FILES)
  2274. const char *OPTIONS[] = {
  2275. "document_root",
  2276. ".",
  2277. "listening_ports",
  2278. "8080",
  2279. #if !defined(NO_CACHING)
  2280. "static_file_max_age",
  2281. "0",
  2282. #endif
  2283. NULL,
  2284. };
  2285. struct mg_context *ctx;
  2286. struct mg_connection *client_conn;
  2287. char client_err[256], nonce[256];
  2288. const struct mg_request_info *client_ri;
  2289. int client_res;
  2290. FILE *f;
  2291. const char *passwd_file = ".htpasswd";
  2292. const char *test_file = "test_http_auth.test_file.txt";
  2293. const char *test_content = "test_http_auth test_file content";
  2294. const char *domain;
  2295. const char *doc_root;
  2296. const char *auth_request;
  2297. const char *str;
  2298. size_t len;
  2299. int i;
  2300. char HA1[256], HA2[256], HA[256];
  2301. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2302. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2303. const char *nc = "00000001";
  2304. const char *cnonce = "6789ABCD";
  2305. /* Start with default options */
  2306. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2307. ck_assert(ctx != NULL);
  2308. domain = mg_get_option(ctx, "authentication_domain");
  2309. ck_assert(domain != NULL);
  2310. len = strlen(domain);
  2311. ck_assert_uint_gt(len, 0);
  2312. ck_assert_uint_lt(len, 64);
  2313. doc_root = mg_get_option(ctx, "document_root");
  2314. ck_assert_str_eq(doc_root, ".");
  2315. /* Create a default file in the document root */
  2316. f = fopen(test_file, "w");
  2317. if (f) {
  2318. fprintf(f, "%s", test_content);
  2319. fclose(f);
  2320. } else {
  2321. ck_abort_msg("Cannot create file %s", test_file);
  2322. }
  2323. remove(passwd_file);
  2324. /* Read file before a .htpasswd file has been created */
  2325. memset(client_err, 0, sizeof(client_err));
  2326. client_conn =
  2327. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2328. ck_assert(client_conn != NULL);
  2329. ck_assert_str_eq(client_err, "");
  2330. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2331. client_res =
  2332. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2333. ck_assert_int_ge(client_res, 0);
  2334. ck_assert_str_eq(client_err, "");
  2335. client_ri = mg_get_request_info(client_conn);
  2336. ck_assert(client_ri != NULL);
  2337. ck_assert_str_eq(client_ri->uri, "200");
  2338. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2339. ck_assert_int_gt(client_res, 0);
  2340. ck_assert_int_le(client_res, sizeof(client_err));
  2341. ck_assert_str_eq(client_err, test_content);
  2342. mg_close_connection(client_conn);
  2343. test_sleep(1);
  2344. /* Create a .htpasswd file */
  2345. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2346. ck_assert_int_eq(client_res, 1);
  2347. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2348. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2349. test_sleep(1);
  2350. /* Repeat test after .htpasswd is created */
  2351. memset(client_err, 0, sizeof(client_err));
  2352. client_conn =
  2353. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2354. ck_assert(client_conn != NULL);
  2355. ck_assert_str_eq(client_err, "");
  2356. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2357. client_res =
  2358. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2359. ck_assert_int_ge(client_res, 0);
  2360. ck_assert_str_eq(client_err, "");
  2361. client_ri = mg_get_request_info(client_conn);
  2362. ck_assert(client_ri != NULL);
  2363. ck_assert_str_eq(client_ri->uri, "401");
  2364. auth_request = NULL;
  2365. for (i = 0; i < client_ri->num_headers; i++) {
  2366. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2367. "WWW-Authenticate")) {
  2368. ck_assert_ptr_eq(auth_request, NULL);
  2369. auth_request = client_ri->http_headers[i].value;
  2370. ck_assert_ptr_ne(auth_request, NULL);
  2371. }
  2372. }
  2373. ck_assert_ptr_ne(auth_request, NULL);
  2374. str = "Digest qop=\"auth\", realm=\"";
  2375. len = strlen(str);
  2376. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2377. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2378. len += strlen(domain);
  2379. str = "\", nonce=\"";
  2380. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2381. len += strlen(str);
  2382. str = strchr(auth_request + len, '\"');
  2383. ck_assert_ptr_ne(str, NULL);
  2384. ck_assert_ptr_ne(str, auth_request + len);
  2385. /* nonce is from including (auth_request + len) to excluding (str) */
  2386. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2387. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2388. (ptrdiff_t)sizeof(nonce));
  2389. memset(nonce, 0, sizeof(nonce));
  2390. memcpy(nonce,
  2391. auth_request + len,
  2392. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2393. memset(HA1, 0, sizeof(HA1));
  2394. memset(HA2, 0, sizeof(HA2));
  2395. memset(HA, 0, sizeof(HA));
  2396. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2397. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2398. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2399. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2400. sprintf(HA2, "%s:/%s", "GET", test_file);
  2401. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2402. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2403. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2404. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2405. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2406. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2407. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2408. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2409. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2410. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2411. HA_md5_ret = mg_md5(HA_md5_buf,
  2412. HA1_md5_buf,
  2413. ":",
  2414. nonce,
  2415. ":",
  2416. nc,
  2417. ":",
  2418. cnonce,
  2419. ":",
  2420. "auth",
  2421. ":",
  2422. HA2_md5_buf,
  2423. NULL);
  2424. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2425. /* Retry with Authorization */
  2426. memset(client_err, 0, sizeof(client_err));
  2427. client_conn =
  2428. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2429. ck_assert(client_conn != NULL);
  2430. ck_assert_str_eq(client_err, "");
  2431. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2432. mg_printf(client_conn,
  2433. "Authorization: Digest "
  2434. "username=\"%s\", "
  2435. "realm=\"%s\", "
  2436. "nonce=\"%s\", "
  2437. "uri=\"/%s\", "
  2438. "qop=auth, "
  2439. "nc=%s, "
  2440. "cnonce=\"%s\", "
  2441. "response=\"%s\"\r\n\r\n",
  2442. "user",
  2443. domain,
  2444. nonce,
  2445. test_file,
  2446. nc,
  2447. cnonce,
  2448. HA_md5_buf);
  2449. client_res =
  2450. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2451. ck_assert_int_ge(client_res, 0);
  2452. ck_assert_str_eq(client_err, "");
  2453. client_ri = mg_get_request_info(client_conn);
  2454. ck_assert(client_ri != NULL);
  2455. ck_assert_str_eq(client_ri->uri, "200");
  2456. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2457. ck_assert_int_gt(client_res, 0);
  2458. ck_assert_int_le(client_res, sizeof(client_err));
  2459. ck_assert_str_eq(client_err, test_content);
  2460. mg_close_connection(client_conn);
  2461. test_sleep(1);
  2462. /* Remove the user from the .htpasswd file again */
  2463. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2464. ck_assert_int_eq(client_res, 1);
  2465. test_sleep(1);
  2466. /* Try to access the file again. Expected: 401 error */
  2467. memset(client_err, 0, sizeof(client_err));
  2468. client_conn =
  2469. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2470. ck_assert(client_conn != NULL);
  2471. ck_assert_str_eq(client_err, "");
  2472. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2473. client_res =
  2474. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2475. ck_assert_int_ge(client_res, 0);
  2476. ck_assert_str_eq(client_err, "");
  2477. client_ri = mg_get_request_info(client_conn);
  2478. ck_assert(client_ri != NULL);
  2479. ck_assert_str_eq(client_ri->uri, "401");
  2480. mg_close_connection(client_conn);
  2481. test_sleep(1);
  2482. /* Now remove the password file */
  2483. remove(passwd_file);
  2484. test_sleep(1);
  2485. /* Access to the file must work like before */
  2486. memset(client_err, 0, sizeof(client_err));
  2487. client_conn =
  2488. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2489. ck_assert(client_conn != NULL);
  2490. ck_assert_str_eq(client_err, "");
  2491. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2492. client_res =
  2493. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2494. ck_assert_int_ge(client_res, 0);
  2495. ck_assert_str_eq(client_err, "");
  2496. client_ri = mg_get_request_info(client_conn);
  2497. ck_assert(client_ri != NULL);
  2498. ck_assert_str_eq(client_ri->uri, "200");
  2499. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2500. ck_assert_int_gt(client_res, 0);
  2501. ck_assert_int_le(client_res, sizeof(client_err));
  2502. ck_assert_str_eq(client_err, test_content);
  2503. mg_close_connection(client_conn);
  2504. test_sleep(1);
  2505. /* Stop the server and clean up */
  2506. test_mg_stop(ctx);
  2507. remove(test_file);
  2508. #endif
  2509. }
  2510. END_TEST
  2511. START_TEST(test_keep_alive)
  2512. {
  2513. struct mg_context *ctx;
  2514. const char *OPTIONS[] =
  2515. { "listening_ports",
  2516. "8080",
  2517. "request_timeout_ms",
  2518. "10000",
  2519. "enable_keep_alive",
  2520. "yes",
  2521. #if !defined(NO_FILES)
  2522. "document_root",
  2523. ".",
  2524. "enable_directory_listing",
  2525. "no",
  2526. #endif
  2527. NULL };
  2528. struct mg_connection *client_conn;
  2529. char client_err[256];
  2530. const struct mg_request_info *client_ri;
  2531. int client_res, i;
  2532. const char *connection_header;
  2533. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2534. ck_assert(ctx != NULL);
  2535. /* HTTP 1.1 GET request */
  2536. memset(client_err, 0, sizeof(client_err));
  2537. client_conn =
  2538. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2539. ck_assert(client_conn != NULL);
  2540. ck_assert_str_eq(client_err, "");
  2541. mg_printf(client_conn,
  2542. "GET / HTTP/1.1\r\nHost: "
  2543. "localhost:8080\r\nConnection: keep-alive\r\n\r\n");
  2544. client_res =
  2545. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2546. ck_assert_int_ge(client_res, 0);
  2547. ck_assert_str_eq(client_err, "");
  2548. client_ri = mg_get_request_info(client_conn);
  2549. ck_assert(client_ri != NULL);
  2550. #if defined(NO_FILES)
  2551. ck_assert_str_eq(client_ri->uri, "404");
  2552. #else
  2553. ck_assert_str_eq(client_ri->uri, "403");
  2554. #endif
  2555. connection_header = 0;
  2556. for (i = 0; i < client_ri->num_headers; i++) {
  2557. if (!mg_strcasecmp(client_ri->http_headers[i].name, "Connection")) {
  2558. ck_assert_ptr_eq(connection_header, NULL);
  2559. connection_header = client_ri->http_headers[i].value;
  2560. ck_assert_ptr_ne(connection_header, NULL);
  2561. }
  2562. }
  2563. /* Error replies will close the connection, even if keep-alive is set. */
  2564. ck_assert_ptr_ne(connection_header, NULL);
  2565. ck_assert_str_eq(connection_header, "close");
  2566. mg_close_connection(client_conn);
  2567. test_sleep(1);
  2568. /* TODO: request a file and keep alive
  2569. * (will only work if NO_FILES is not set). */
  2570. /* Stop the server and clean up */
  2571. test_mg_stop(ctx);
  2572. }
  2573. END_TEST
  2574. Suite *
  2575. make_public_server_suite(void)
  2576. {
  2577. Suite *const suite = suite_create("PublicServer");
  2578. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  2579. TCase *const tcase_startthreads = tcase_create("Start threads");
  2580. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  2581. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  2582. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  2583. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  2584. TCase *const tcase_handle_form = tcase_create("Handle Form");
  2585. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  2586. TCase *const tcase_keep_alive = tcase_create("HTTP Keep Alive");
  2587. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  2588. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  2589. suite_add_tcase(suite, tcase_checktestenv);
  2590. tcase_add_test(tcase_startthreads, test_threading);
  2591. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  2592. suite_add_tcase(suite, tcase_startthreads);
  2593. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  2594. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  2595. suite_add_tcase(suite, tcase_startstophttp);
  2596. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  2597. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  2598. suite_add_tcase(suite, tcase_startstophttps);
  2599. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  2600. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  2601. suite_add_tcase(suite, tcase_serverandclienttls);
  2602. tcase_add_test(tcase_serverrequests, test_request_handlers);
  2603. tcase_set_timeout(tcase_serverrequests, 120);
  2604. suite_add_tcase(suite, tcase_serverrequests);
  2605. tcase_add_test(tcase_handle_form, test_handle_form);
  2606. tcase_set_timeout(tcase_handle_form, 300);
  2607. suite_add_tcase(suite, tcase_handle_form);
  2608. tcase_add_test(tcase_http_auth, test_http_auth);
  2609. tcase_set_timeout(tcase_http_auth, 60);
  2610. suite_add_tcase(suite, tcase_http_auth);
  2611. tcase_add_test(tcase_keep_alive, test_keep_alive);
  2612. tcase_set_timeout(tcase_keep_alive, 300);
  2613. suite_add_tcase(suite, tcase_keep_alive);
  2614. return suite;
  2615. }
  2616. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  2617. /* Used to debug test cases without using the check framework */
  2618. static int chk_ok = 0;
  2619. static int chk_failed = 0;
  2620. void
  2621. MAIN_PUBLIC_SERVER(void)
  2622. {
  2623. test_the_test_environment(0);
  2624. test_threading(0);
  2625. test_mg_start_stop_http_server(0);
  2626. test_mg_start_stop_https_server(0);
  2627. test_request_handlers(0);
  2628. test_mg_server_and_client_tls(0);
  2629. test_handle_form(0);
  2630. test_http_auth(0);
  2631. test_keep_alive(0);
  2632. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  2633. }
  2634. void
  2635. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  2636. {
  2637. va_list va;
  2638. va_start(va, expr);
  2639. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2640. vfprintf(stderr, expr, va);
  2641. fprintf(stderr, "\n\n");
  2642. va_end(va);
  2643. chk_failed++;
  2644. }
  2645. void
  2646. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  2647. {
  2648. va_list va;
  2649. if (cond) {
  2650. chk_ok++;
  2651. return;
  2652. }
  2653. va_start(va, expr);
  2654. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2655. vfprintf(stderr, expr, va);
  2656. fprintf(stderr, "\n\n");
  2657. va_end(va);
  2658. chk_failed++;
  2659. }
  2660. void
  2661. _mark_point(const char *file, int line)
  2662. {
  2663. chk_ok++;
  2664. }
  2665. void
  2666. tcase_fn_start(const char *fname, const char *file, int line)
  2667. {
  2668. }
  2669. void suite_add_tcase(Suite *s, TCase *tc){};
  2670. void _tcase_add_test(TCase *tc,
  2671. TFun tf,
  2672. const char *fname,
  2673. int _signal,
  2674. int allowed_exit_value,
  2675. int start,
  2676. int end){};
  2677. TCase *
  2678. tcase_create(const char *name)
  2679. {
  2680. return NULL;
  2681. };
  2682. Suite *
  2683. suite_create(const char *name)
  2684. {
  2685. return NULL;
  2686. };
  2687. void tcase_set_timeout(TCase *tc, double timeout){};
  2688. #endif