public_server.c 89 KB

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