public_server.c 90 KB

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