public_server.c 88 KB

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