public_server.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  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 int
  1520. field_store(const char *path, long long file_size, void *user_data)
  1521. {
  1522. FILE *f;
  1523. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1524. ck_assert_int_ge(g_field_step, 100);
  1525. ++g_field_step;
  1526. switch (g_field_step) {
  1527. case 101:
  1528. ck_assert_str_eq(path, "storeme.txt");
  1529. ck_assert_int_eq(file_size, 9);
  1530. f = fopen("storeme.txt", "r");
  1531. ck_assert_ptr_ne(f, NULL);
  1532. if (f) {
  1533. char buf[10] = {0};
  1534. int i = (int)fread(buf, 1, 9, f);
  1535. ck_assert_int_eq(i, 9);
  1536. fclose(f);
  1537. ck_assert_str_eq(buf, "storetest");
  1538. }
  1539. break;
  1540. case 102:
  1541. /* TODO: A file upload is only possible with multipart/form-data */
  1542. break;
  1543. default:
  1544. ck_abort_msg("field_get called with g_field_step == %i",
  1545. (int)g_field_step);
  1546. }
  1547. return 0;
  1548. }
  1549. static int
  1550. FormGet(struct mg_connection *conn, void *cbdata)
  1551. {
  1552. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1553. int ret;
  1554. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  1555. (void)cbdata;
  1556. ck_assert(req_info != NULL);
  1557. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1558. fdh.user_data = (void *)&g_field_found_return;
  1559. /* Call the form handler */
  1560. g_field_step = 0;
  1561. g_field_found_return = FORM_FIELD_STORAGE_GET;
  1562. ret = mg_handle_form_request(conn, &fdh);
  1563. g_field_found_return = -888;
  1564. ck_assert_int_eq(ret, 22);
  1565. ck_assert_int_eq(g_field_step, 22);
  1566. mg_printf(conn, "%i\r\n", ret);
  1567. return 1;
  1568. }
  1569. static int
  1570. FormStore(struct mg_connection *conn, void *cbdata)
  1571. {
  1572. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1573. int ret;
  1574. struct mg_form_data_handler fdh = {field_found,
  1575. field_get,
  1576. field_store,
  1577. NULL};
  1578. (void)cbdata;
  1579. ck_assert(req_info != NULL);
  1580. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1581. fdh.user_data = (void *)&g_field_found_return;
  1582. /* Call the form handler */
  1583. g_field_step = 100;
  1584. g_field_found_return = FORM_FIELD_STORAGE_STORE;
  1585. ret = mg_handle_form_request(conn, &fdh);
  1586. ck_assert_int_eq(ret, 3);
  1587. ck_assert_int_eq(g_field_step, 101);
  1588. mg_printf(conn, "%i\r\n", ret);
  1589. return 1;
  1590. }
  1591. static void
  1592. send_chunk_string(struct mg_connection *conn, const char *txt)
  1593. {
  1594. unsigned int chunk_len = (unsigned int)strlen(txt);
  1595. mg_printf(conn, "%x\r\n", chunk_len);
  1596. mg_write(conn, txt, chunk_len);
  1597. mg_printf(conn, "\r\n");
  1598. }
  1599. START_TEST(test_handle_form)
  1600. {
  1601. struct mg_context *ctx;
  1602. struct mg_connection *client_conn;
  1603. const struct mg_request_info *ri;
  1604. const char *OPTIONS[8];
  1605. const char *opt;
  1606. int opt_idx = 0;
  1607. char ebuf[100];
  1608. const char *multipart_body;
  1609. const char *boundary;
  1610. size_t body_len, body_sent, chunk_len;
  1611. int sleep_cnt;
  1612. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  1613. OPTIONS[opt_idx++] = "listening_ports";
  1614. OPTIONS[opt_idx++] = "8884";
  1615. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  1616. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  1617. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  1618. mark_point();
  1619. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  1620. ck_assert(ctx != NULL);
  1621. g_ctx = ctx;
  1622. opt = mg_get_option(ctx, "listening_ports");
  1623. ck_assert_str_eq(opt, "8884");
  1624. mg_set_request_handler(ctx, "/handle_form", FormGet, (void *)0);
  1625. mg_set_request_handler(ctx, "/handle_form_store", FormStore, (void *)0);
  1626. test_sleep(1);
  1627. /* Handle form: "GET" */
  1628. client_conn = mg_download("localhost",
  1629. 8884,
  1630. 0,
  1631. ebuf,
  1632. sizeof(ebuf),
  1633. "%s",
  1634. "GET /handle_form"
  1635. "?textin=text&passwordin=&radio1=val1"
  1636. "&radio2=val1&check1=val1&numberin=1"
  1637. "&datein=1.1.2016&colorin=%2380ff00"
  1638. "&rangein=3&monthin=&weekin=&timein="
  1639. "&datetimen=&datetimelocalin=&emailin="
  1640. "&searchin=&telin=&urlin=&filein="
  1641. "&filesin=&selectin=opt1"
  1642. "&message=Text+area+default+text. "
  1643. "HTTP/1.0\r\n"
  1644. "Host: localhost:8884\r\n"
  1645. "Connection: close\r\n\r\n");
  1646. ck_assert(client_conn != NULL);
  1647. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1648. test_sleep(1);
  1649. if (g_field_step == 22) {
  1650. break;
  1651. }
  1652. }
  1653. ri = mg_get_request_info(client_conn);
  1654. ck_assert(ri != NULL);
  1655. ck_assert_str_eq(ri->uri, "200");
  1656. mg_close_connection(client_conn);
  1657. /* Handle form: "POST x-www-form-urlencoded" */
  1658. client_conn =
  1659. mg_download("localhost",
  1660. 8884,
  1661. 0,
  1662. ebuf,
  1663. sizeof(ebuf),
  1664. "%s",
  1665. "POST /handle_form HTTP/1.1\r\n"
  1666. "Host: localhost:8884\r\n"
  1667. "Connection: close\r\n"
  1668. "Content-Type: application/x-www-form-urlencoded\r\n"
  1669. "Content-Length: 263\r\n"
  1670. "\r\n"
  1671. "textin=text&passwordin=&radio1=val1&radio2=val1"
  1672. "&check1=val1&numberin=1&datein=1.1.2016"
  1673. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  1674. "&timein=&datetimen=&datetimelocalin=&emailin="
  1675. "&searchin=&telin=&urlin=&filein=&filesin="
  1676. "&selectin=opt1&message=Text+area+default+text.");
  1677. ck_assert(client_conn != NULL);
  1678. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1679. test_sleep(1);
  1680. if (g_field_step == 22) {
  1681. break;
  1682. }
  1683. }
  1684. ri = mg_get_request_info(client_conn);
  1685. ck_assert(ri != NULL);
  1686. ck_assert_str_eq(ri->uri, "200");
  1687. mg_close_connection(client_conn);
  1688. /* Handle form: "POST multipart/form-data" */
  1689. multipart_body =
  1690. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1691. "Content-Disposition: form-data; name=\"textin\"\r\n"
  1692. "\r\n"
  1693. "text\r\n"
  1694. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1695. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  1696. "\r\n"
  1697. "\r\n"
  1698. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1699. "Content-Disposition: form-data; name=\"radio1\"\r\n"
  1700. "\r\n"
  1701. "val1\r\n"
  1702. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1703. "Content-Disposition: form-data; name=\"radio2\"\r\n"
  1704. "\r\n"
  1705. "val1\r\n"
  1706. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1707. "Content-Disposition: form-data; name=\"check1\"\r\n"
  1708. "\r\n"
  1709. "val1\r\n"
  1710. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1711. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  1712. "\r\n"
  1713. "1\r\n"
  1714. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1715. "Content-Disposition: form-data; name=\"datein\"\r\n"
  1716. "\r\n"
  1717. "1.1.2016\r\n"
  1718. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1719. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  1720. "\r\n"
  1721. "#80ff00\r\n"
  1722. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1723. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  1724. "\r\n"
  1725. "3\r\n"
  1726. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1727. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  1728. "\r\n"
  1729. "\r\n"
  1730. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1731. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  1732. "\r\n"
  1733. "\r\n"
  1734. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1735. "Content-Disposition: form-data; name=\"timein\"\r\n"
  1736. "\r\n"
  1737. "\r\n"
  1738. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1739. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  1740. "\r\n"
  1741. "\r\n"
  1742. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1743. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  1744. "\r\n"
  1745. "\r\n"
  1746. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1747. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  1748. "\r\n"
  1749. "\r\n"
  1750. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1751. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  1752. "\r\n"
  1753. "\r\n"
  1754. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1755. "Content-Disposition: form-data; name=\"telin\"\r\n"
  1756. "\r\n"
  1757. "\r\n"
  1758. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1759. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  1760. "\r\n"
  1761. "\r\n"
  1762. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1763. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  1764. "Content-Type: application/octet-stream\r\n"
  1765. "\r\n"
  1766. "\r\n"
  1767. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1768. "Content-Disposition: form-data; name=\"filesin\"; filename=\"\"\r\n"
  1769. "Content-Type: application/octet-stream\r\n"
  1770. "\r\n"
  1771. "\r\n"
  1772. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1773. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  1774. "\r\n"
  1775. "opt1\r\n"
  1776. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1777. "Content-Disposition: form-data; name=\"message\"\r\n"
  1778. "\r\n"
  1779. "Text area default text.\r\n"
  1780. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  1781. body_len = strlen(multipart_body);
  1782. ck_assert_uint_eq(body_len, 2374); /* not required */
  1783. client_conn =
  1784. mg_download("localhost",
  1785. 8884,
  1786. 0,
  1787. ebuf,
  1788. sizeof(ebuf),
  1789. "POST /handle_form HTTP/1.1\r\n"
  1790. "Host: localhost:8884\r\n"
  1791. "Connection: close\r\n"
  1792. "Content-Type: multipart/form-data; "
  1793. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  1794. "Content-Length: %u\r\n"
  1795. "\r\n%s",
  1796. (unsigned int)body_len,
  1797. multipart_body);
  1798. ck_assert(client_conn != NULL);
  1799. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1800. test_sleep(1);
  1801. if (g_field_step == 22) {
  1802. break;
  1803. }
  1804. }
  1805. ri = mg_get_request_info(client_conn);
  1806. ck_assert(ri != NULL);
  1807. ck_assert_str_eq(ri->uri, "200");
  1808. mg_close_connection(client_conn);
  1809. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  1810. client_conn =
  1811. mg_download("localhost",
  1812. 8884,
  1813. 0,
  1814. ebuf,
  1815. sizeof(ebuf),
  1816. "%s",
  1817. "POST /handle_form HTTP/1.1\r\n"
  1818. "Host: localhost:8884\r\n"
  1819. "Connection: close\r\n"
  1820. "Content-Type: multipart/form-data; "
  1821. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  1822. "Transfer-Encoding: chunked\r\n"
  1823. "\r\n");
  1824. ck_assert(client_conn != NULL);
  1825. body_len = strlen(multipart_body);
  1826. chunk_len = 1;
  1827. body_sent = 0;
  1828. while (body_len > body_sent) {
  1829. if (chunk_len > (body_len - body_sent)) {
  1830. chunk_len = body_len - body_sent;
  1831. }
  1832. ck_assert_int_gt((int)chunk_len, 0);
  1833. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  1834. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  1835. mg_printf(client_conn, "\r\n");
  1836. body_sent += chunk_len;
  1837. chunk_len = (chunk_len % 40) + 1;
  1838. }
  1839. mg_printf(client_conn, "0\r\n");
  1840. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1841. test_sleep(1);
  1842. if (g_field_step == 22) {
  1843. break;
  1844. }
  1845. }
  1846. ri = mg_get_request_info(client_conn);
  1847. ck_assert(ri != NULL);
  1848. ck_assert_str_eq(ri->uri, "200");
  1849. mg_close_connection(client_conn);
  1850. /* Now test form_store */
  1851. /* First test with GET */
  1852. client_conn = mg_download("localhost",
  1853. 8884,
  1854. 0,
  1855. ebuf,
  1856. sizeof(ebuf),
  1857. "%s",
  1858. "GET /handle_form_store"
  1859. "?storeme=storetest"
  1860. "&continue_field_handler=ignore"
  1861. "&break_field_handler=abort"
  1862. "&dontread=xyz "
  1863. "HTTP/1.0\r\n"
  1864. "Host: localhost:8884\r\n"
  1865. "Connection: close\r\n\r\n");
  1866. ck_assert(client_conn != NULL);
  1867. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1868. test_sleep(1);
  1869. if (g_field_step == 101) {
  1870. break;
  1871. }
  1872. }
  1873. ri = mg_get_request_info(client_conn);
  1874. ck_assert(ri != NULL);
  1875. ck_assert_str_eq(ri->uri, "200");
  1876. mg_close_connection(client_conn);
  1877. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  1878. client_conn =
  1879. mg_download("localhost",
  1880. 8884,
  1881. 0,
  1882. ebuf,
  1883. sizeof(ebuf),
  1884. "%s",
  1885. "POST /handle_form_store HTTP/1.0\r\n"
  1886. "Host: localhost:8884\r\n"
  1887. "Connection: close\r\n"
  1888. "Content-Type: application/x-www-form-urlencoded\r\n"
  1889. "Transfer-Encoding: chunked\r\n"
  1890. "\r\n");
  1891. ck_assert(client_conn != NULL);
  1892. send_chunk_string(client_conn, "storeme=store");
  1893. send_chunk_string(client_conn, "test&");
  1894. send_chunk_string(client_conn, "continue_field_handler=ignore");
  1895. send_chunk_string(client_conn, "&br");
  1896. test_sleep(1);
  1897. send_chunk_string(client_conn, "eak_field_handler=abort&");
  1898. send_chunk_string(client_conn, "dontread=xyz");
  1899. mg_printf(client_conn, "0\r\n");
  1900. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1901. test_sleep(1);
  1902. if (g_field_step == 101) {
  1903. break;
  1904. }
  1905. }
  1906. ri = mg_get_request_info(client_conn);
  1907. ck_assert(ri != NULL);
  1908. ck_assert_str_eq(ri->uri, "200");
  1909. mg_close_connection(client_conn);
  1910. /* Handle form: "POST multipart/form-data", chunked, store */
  1911. client_conn =
  1912. mg_download("localhost",
  1913. 8884,
  1914. 0,
  1915. ebuf,
  1916. sizeof(ebuf),
  1917. "%s",
  1918. "POST /handle_form_store HTTP/1.0\r\n"
  1919. "Host: localhost:8884\r\n"
  1920. "Connection: close\r\n"
  1921. "Content-Type: multipart/form-data; "
  1922. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  1923. "Transfer-Encoding: chunked\r\n"
  1924. "\r\n");
  1925. ck_assert(client_conn != NULL);
  1926. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  1927. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  1928. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  1929. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  1930. send_chunk_string(client_conn, "\r\n");
  1931. send_chunk_string(client_conn, "storetest\r\n");
  1932. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  1933. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  1934. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  1935. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  1936. send_chunk_string(client_conn, "\r\n");
  1937. send_chunk_string(client_conn, "ignore\r\n");
  1938. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  1939. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  1940. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  1941. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  1942. send_chunk_string(client_conn, "\r\n");
  1943. send_chunk_string(client_conn, "abort\r\n");
  1944. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  1945. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  1946. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  1947. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  1948. send_chunk_string(client_conn, "\r\n");
  1949. send_chunk_string(client_conn, "xyz\r\n");
  1950. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  1951. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  1952. mg_printf(client_conn, "0\r\n");
  1953. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1954. test_sleep(1);
  1955. if (g_field_step == 101) {
  1956. break;
  1957. }
  1958. }
  1959. ri = mg_get_request_info(client_conn);
  1960. ck_assert(ri != NULL);
  1961. ck_assert_str_eq(ri->uri, "200");
  1962. mg_close_connection(client_conn);
  1963. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  1964. client_conn =
  1965. mg_download("localhost",
  1966. 8884,
  1967. 0,
  1968. ebuf,
  1969. sizeof(ebuf),
  1970. "%s",
  1971. "POST /handle_form_store HTTP/1.0\r\n"
  1972. "Host: localhost:8884\r\n"
  1973. "Connection: close\r\n"
  1974. "Content-Type: multipart/form-data; "
  1975. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  1976. "Transfer-Encoding: chunked\r\n"
  1977. "\r\n");
  1978. ck_assert(client_conn != NULL);
  1979. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  1980. send_chunk_string(client_conn, boundary);
  1981. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  1982. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  1983. send_chunk_string(client_conn, "\r\n");
  1984. send_chunk_string(client_conn, "storetest\r\n");
  1985. send_chunk_string(client_conn, boundary);
  1986. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  1987. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  1988. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  1989. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  1990. send_chunk_string(client_conn, "Content-Type: ");
  1991. send_chunk_string(client_conn, "application/octet-stream\r\n");
  1992. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  1993. send_chunk_string(client_conn, "\r\n");
  1994. /* send some megabyte of data */
  1995. body_sent = 0;
  1996. do {
  1997. send_chunk_string(client_conn, "ignore\r\n");
  1998. body_sent += 8;
  1999. /* send some strings that are almost boundaries */
  2000. for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
  2001. /* chunks from 1 byte to strlen(boundary)-1 */
  2002. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2003. mg_write(client_conn, boundary, chunk_len);
  2004. mg_printf(client_conn, "\r\n");
  2005. body_sent += chunk_len;
  2006. }
  2007. } while (body_sent < 1024 * 1024);
  2008. send_chunk_string(client_conn, "\r\n");
  2009. send_chunk_string(client_conn, boundary);
  2010. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2011. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2012. send_chunk_string(client_conn, "\r\n");
  2013. send_chunk_string(client_conn, "abort\r\n");
  2014. send_chunk_string(client_conn, boundary);
  2015. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2016. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2017. send_chunk_string(client_conn, "\r\n");
  2018. send_chunk_string(client_conn, "xyz\r\n");
  2019. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2020. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2021. mg_printf(client_conn, "0\r\n");
  2022. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2023. test_sleep(1);
  2024. if (g_field_step == 101) {
  2025. break;
  2026. }
  2027. }
  2028. ri = mg_get_request_info(client_conn);
  2029. ck_assert(ri != NULL);
  2030. ck_assert_str_eq(ri->uri, "200");
  2031. mg_close_connection(client_conn);
  2032. /* Close the server */
  2033. g_ctx = NULL;
  2034. mg_stop(ctx);
  2035. mark_point();
  2036. }
  2037. END_TEST
  2038. START_TEST(test_http_auth)
  2039. {
  2040. #if !defined(NO_FILES)
  2041. const char *OPTIONS[] = {
  2042. "document_root",
  2043. ".",
  2044. "listening_ports",
  2045. "8080",
  2046. #if !defined(NO_CACHING)
  2047. "static_file_max_age",
  2048. "0",
  2049. #endif
  2050. NULL,
  2051. };
  2052. struct mg_context *ctx;
  2053. struct mg_connection *client_conn;
  2054. char client_err[256], nonce[256];
  2055. const struct mg_request_info *client_ri;
  2056. int client_res;
  2057. FILE *f;
  2058. const char *passwd_file = ".htpasswd";
  2059. const char *test_file = "test_http_auth.test_file.txt";
  2060. const char *test_content = "test_http_auth test_file content";
  2061. const char *domain;
  2062. const char *doc_root;
  2063. const char *auth_request;
  2064. const char *str;
  2065. size_t len;
  2066. int i;
  2067. char HA1[256], HA2[256], HA[256];
  2068. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2069. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2070. const char *nc = "00000001";
  2071. const char *cnonce = "6789ABCD";
  2072. /* Start with default options */
  2073. mark_point();
  2074. ctx = mg_start(NULL, NULL, OPTIONS);
  2075. test_sleep(1);
  2076. ck_assert(ctx != NULL);
  2077. domain = mg_get_option(ctx, "authentication_domain");
  2078. ck_assert(domain != NULL);
  2079. len = strlen(domain);
  2080. ck_assert_uint_gt(len, 0);
  2081. ck_assert_uint_lt(len, 64);
  2082. doc_root = mg_get_option(ctx, "document_root");
  2083. ck_assert_str_eq(doc_root, ".");
  2084. /* Create a default file in the document root */
  2085. f = fopen(test_file, "w");
  2086. if (f) {
  2087. fprintf(f, "%s", test_content);
  2088. fclose(f);
  2089. } else {
  2090. ck_abort_msg("Cannot create file %s", test_file);
  2091. }
  2092. remove(passwd_file);
  2093. /* Read file before a .htpasswd file has been created */
  2094. memset(client_err, 0, sizeof(client_err));
  2095. client_conn =
  2096. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2097. ck_assert(client_conn != NULL);
  2098. ck_assert_str_eq(client_err, "");
  2099. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2100. client_res =
  2101. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2102. ck_assert_int_ge(client_res, 0);
  2103. ck_assert_str_eq(client_err, "");
  2104. client_ri = mg_get_request_info(client_conn);
  2105. ck_assert(client_ri != NULL);
  2106. ck_assert_str_eq(client_ri->uri, "200");
  2107. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2108. ck_assert_int_gt(client_res, 0);
  2109. ck_assert_int_le(client_res, sizeof(client_err));
  2110. ck_assert_str_eq(client_err, test_content);
  2111. mg_close_connection(client_conn);
  2112. test_sleep(1);
  2113. /* Create a .htpasswd file */
  2114. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2115. ck_assert_int_eq(client_res, 1);
  2116. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2117. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2118. test_sleep(1);
  2119. /* Repeat test after .htpasswd is created */
  2120. memset(client_err, 0, sizeof(client_err));
  2121. client_conn =
  2122. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2123. ck_assert(client_conn != NULL);
  2124. ck_assert_str_eq(client_err, "");
  2125. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2126. client_res =
  2127. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2128. ck_assert_int_ge(client_res, 0);
  2129. ck_assert_str_eq(client_err, "");
  2130. client_ri = mg_get_request_info(client_conn);
  2131. ck_assert(client_ri != NULL);
  2132. ck_assert_str_eq(client_ri->uri, "401");
  2133. auth_request = NULL;
  2134. for (i = 0; i < client_ri->num_headers; i++) {
  2135. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2136. "WWW-Authenticate")) {
  2137. ck_assert_ptr_eq(auth_request, NULL);
  2138. auth_request = client_ri->http_headers[i].value;
  2139. ck_assert_ptr_ne(auth_request, NULL);
  2140. }
  2141. }
  2142. ck_assert_ptr_ne(auth_request, NULL);
  2143. str = "Digest qop=\"auth\", realm=\"";
  2144. len = strlen(str);
  2145. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2146. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2147. len += strlen(domain);
  2148. str = "\", nonce=\"";
  2149. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2150. len += strlen(str);
  2151. str = strchr(auth_request + len, '\"');
  2152. ck_assert_ptr_ne(str, NULL);
  2153. ck_assert_ptr_ne(str, auth_request + len);
  2154. /* nonce is from including (auth_request + len) to excluding (str) */
  2155. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2156. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2157. (ptrdiff_t)sizeof(nonce));
  2158. memset(nonce, 0, sizeof(nonce));
  2159. memcpy(nonce,
  2160. auth_request + len,
  2161. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2162. memset(HA1, 0, sizeof(HA1));
  2163. memset(HA2, 0, sizeof(HA2));
  2164. memset(HA, 0, sizeof(HA));
  2165. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2166. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2167. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2168. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2169. sprintf(HA2, "%s:/%s", "GET", test_file);
  2170. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2171. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2172. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2173. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2174. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2175. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2176. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2177. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2178. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2179. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2180. HA_md5_ret = mg_md5(HA_md5_buf,
  2181. HA1_md5_buf,
  2182. ":",
  2183. nonce,
  2184. ":",
  2185. nc,
  2186. ":",
  2187. cnonce,
  2188. ":",
  2189. "auth",
  2190. ":",
  2191. HA2_md5_buf,
  2192. NULL);
  2193. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2194. /* Retry with Authorization */
  2195. memset(client_err, 0, sizeof(client_err));
  2196. client_conn =
  2197. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2198. ck_assert(client_conn != NULL);
  2199. ck_assert_str_eq(client_err, "");
  2200. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2201. mg_printf(client_conn,
  2202. "Authorization: Digest "
  2203. "username=\"%s\", "
  2204. "realm=\"%s\", "
  2205. "nonce=\"%s\", "
  2206. "uri=\"/%s\", "
  2207. "qop=auth, "
  2208. "nc=%s, "
  2209. "cnonce=\"%s\", "
  2210. "response=\"%s\"\r\n\r\n",
  2211. "user",
  2212. domain,
  2213. nonce,
  2214. test_file,
  2215. nc,
  2216. cnonce,
  2217. HA_md5_buf);
  2218. client_res =
  2219. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2220. ck_assert_int_ge(client_res, 0);
  2221. ck_assert_str_eq(client_err, "");
  2222. client_ri = mg_get_request_info(client_conn);
  2223. ck_assert(client_ri != NULL);
  2224. ck_assert_str_eq(client_ri->uri, "200");
  2225. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2226. ck_assert_int_gt(client_res, 0);
  2227. ck_assert_int_le(client_res, sizeof(client_err));
  2228. ck_assert_str_eq(client_err, test_content);
  2229. mg_close_connection(client_conn);
  2230. test_sleep(1);
  2231. /* Remove the user from the .htpasswd file again */
  2232. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2233. ck_assert_int_eq(client_res, 1);
  2234. test_sleep(1);
  2235. /* Try to access the file again. Expected: 401 error */
  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\r\n", test_file);
  2242. client_res =
  2243. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2244. ck_assert_int_ge(client_res, 0);
  2245. ck_assert_str_eq(client_err, "");
  2246. client_ri = mg_get_request_info(client_conn);
  2247. ck_assert(client_ri != NULL);
  2248. ck_assert_str_eq(client_ri->uri, "401");
  2249. mg_close_connection(client_conn);
  2250. test_sleep(1);
  2251. /* Now remove the password file */
  2252. remove(passwd_file);
  2253. test_sleep(1);
  2254. /* Access to the file must work like before */
  2255. memset(client_err, 0, sizeof(client_err));
  2256. client_conn =
  2257. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2258. ck_assert(client_conn != NULL);
  2259. ck_assert_str_eq(client_err, "");
  2260. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2261. client_res =
  2262. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2263. ck_assert_int_ge(client_res, 0);
  2264. ck_assert_str_eq(client_err, "");
  2265. client_ri = mg_get_request_info(client_conn);
  2266. ck_assert(client_ri != NULL);
  2267. ck_assert_str_eq(client_ri->uri, "200");
  2268. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2269. ck_assert_int_gt(client_res, 0);
  2270. ck_assert_int_le(client_res, sizeof(client_err));
  2271. ck_assert_str_eq(client_err, test_content);
  2272. mg_close_connection(client_conn);
  2273. test_sleep(1);
  2274. /* Stop the server and clean up */
  2275. mg_stop(ctx);
  2276. remove(test_file);
  2277. #endif
  2278. }
  2279. END_TEST
  2280. Suite *
  2281. make_public_server_suite(void)
  2282. {
  2283. Suite *const suite = suite_create("PublicServer");
  2284. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  2285. TCase *const tcase_startthreads = tcase_create("Start threads");
  2286. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  2287. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  2288. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  2289. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  2290. TCase *const tcase_handle_form = tcase_create("Handle Form");
  2291. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  2292. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  2293. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  2294. suite_add_tcase(suite, tcase_checktestenv);
  2295. tcase_add_test(tcase_startthreads, test_threading);
  2296. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  2297. suite_add_tcase(suite, tcase_startthreads);
  2298. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  2299. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  2300. suite_add_tcase(suite, tcase_startstophttp);
  2301. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  2302. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  2303. suite_add_tcase(suite, tcase_startstophttps);
  2304. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  2305. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  2306. suite_add_tcase(suite, tcase_serverandclienttls);
  2307. tcase_add_test(tcase_serverrequests, test_request_handlers);
  2308. tcase_set_timeout(tcase_serverrequests, 120);
  2309. suite_add_tcase(suite, tcase_serverrequests);
  2310. tcase_add_test(tcase_handle_form, test_handle_form);
  2311. tcase_set_timeout(tcase_handle_form, 120);
  2312. suite_add_tcase(suite, tcase_handle_form);
  2313. tcase_add_test(tcase_http_auth, test_http_auth);
  2314. tcase_set_timeout(tcase_http_auth, 60);
  2315. suite_add_tcase(suite, tcase_http_auth);
  2316. return suite;
  2317. }
  2318. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  2319. /* Used to debug test cases without using the check framework */
  2320. static int chk_ok = 0;
  2321. static int chk_failed = 0;
  2322. void
  2323. MAIN_PUBLIC_SERVER(void)
  2324. {
  2325. /*
  2326. test_the_test_environment(0);
  2327. test_threading(0);
  2328. test_mg_start_stop_http_server(0);
  2329. test_mg_start_stop_https_server(0);
  2330. test_request_handlers(0);
  2331. test_mg_server_and_client_tls(0);
  2332. */
  2333. test_handle_form(0);
  2334. test_http_auth(0);
  2335. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  2336. }
  2337. void
  2338. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  2339. {
  2340. va_list va;
  2341. va_start(va, expr);
  2342. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2343. vfprintf(stderr, expr, va);
  2344. fprintf(stderr, "\n\n");
  2345. va_end(va);
  2346. chk_failed++;
  2347. }
  2348. void
  2349. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  2350. {
  2351. va_list va;
  2352. if (cond) {
  2353. chk_ok++;
  2354. return;
  2355. }
  2356. va_start(va, expr);
  2357. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2358. vfprintf(stderr, expr, va);
  2359. fprintf(stderr, "\n\n");
  2360. va_end(va);
  2361. chk_failed++;
  2362. }
  2363. void
  2364. _mark_point(const char *file, int line)
  2365. {
  2366. chk_ok++;
  2367. }
  2368. void
  2369. tcase_fn_start(const char *fname, const char *file, int line)
  2370. {
  2371. }
  2372. void suite_add_tcase(Suite *s, TCase *tc){};
  2373. void _tcase_add_test(TCase *tc,
  2374. TFun tf,
  2375. const char *fname,
  2376. int _signal,
  2377. int allowed_exit_value,
  2378. int start,
  2379. int end){};
  2380. TCase *
  2381. tcase_create(const char *name)
  2382. {
  2383. return NULL;
  2384. };
  2385. Suite *
  2386. suite_create(const char *name)
  2387. {
  2388. return NULL;
  2389. };
  2390. void tcase_set_timeout(TCase *tc, double timeout){};
  2391. #endif