public_server.c 87 KB

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