public_server.c 81 KB

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