public_server.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  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 tclient_data ws_client4_data = {NULL, 0, 0};
  800. struct mg_connection *ws_client1_conn = NULL;
  801. struct mg_connection *ws_client2_conn = NULL;
  802. struct mg_connection *ws_client3_conn = NULL;
  803. struct mg_connection *ws_client4_conn = NULL;
  804. #endif
  805. char cmd_buf[256];
  806. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  807. OPTIONS[opt_idx++] = "listening_ports";
  808. OPTIONS[opt_idx++] = HTTP_PORT;
  809. OPTIONS[opt_idx++] = "authentication_domain";
  810. OPTIONS[opt_idx++] = "test.domain";
  811. #if !defined(NO_FILES)
  812. OPTIONS[opt_idx++] = "document_root";
  813. OPTIONS[opt_idx++] = ".";
  814. #endif
  815. #ifndef NO_SSL
  816. ck_assert(ssl_cert != NULL);
  817. OPTIONS[opt_idx++] = "ssl_certificate";
  818. OPTIONS[opt_idx++] = ssl_cert;
  819. #endif
  820. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  821. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  822. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  823. mark_point();
  824. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  825. ck_assert(ctx != NULL);
  826. g_ctx = ctx;
  827. opt = mg_get_option(ctx, "listening_ports");
  828. ck_assert_str_eq(opt, HTTP_PORT);
  829. opt = mg_get_option(ctx, "cgi_environment");
  830. ck_assert_str_eq(opt, "");
  831. opt = mg_get_option(ctx, "unknown_option_name");
  832. ck_assert(opt == NULL);
  833. for (i = 0; i < 1000; i++) {
  834. sprintf(uri, "/U%u", i);
  835. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  836. }
  837. for (i = 500; i < 800; i++) {
  838. sprintf(uri, "/U%u", i);
  839. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  840. }
  841. for (i = 600; i >= 0; i--) {
  842. sprintf(uri, "/U%u", i);
  843. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  844. }
  845. for (i = 750; i <= 1000; i++) {
  846. sprintf(uri, "/U%u", i);
  847. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  848. }
  849. for (i = 5; i < 9; i++) {
  850. sprintf(uri, "/U%u", i);
  851. mg_set_request_handler(ctx,
  852. uri,
  853. request_test_handler,
  854. (void *)(ptrdiff_t)i);
  855. }
  856. #ifdef USE_WEBSOCKET
  857. mg_set_websocket_handler(ctx,
  858. "/websocket",
  859. websock_server_connect,
  860. websock_server_ready,
  861. websock_server_data,
  862. websock_server_close,
  863. (void *)7531);
  864. #endif
  865. /* Try to load non existing file */
  866. client_conn = mg_download("localhost",
  867. ipv4_port,
  868. 0,
  869. ebuf,
  870. sizeof(ebuf),
  871. "%s",
  872. "GET /file/not/found HTTP/1.0\r\n\r\n");
  873. ck_assert(client_conn != NULL);
  874. ri = mg_get_request_info(client_conn);
  875. ck_assert(ri != NULL);
  876. ck_assert_str_eq(ri->uri, "404");
  877. mg_close_connection(client_conn);
  878. /* Get data from callback */
  879. client_conn = mg_download(
  880. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  881. ck_assert(client_conn != NULL);
  882. ri = mg_get_request_info(client_conn);
  883. ck_assert(ri != NULL);
  884. ck_assert_str_eq(ri->uri, "200");
  885. i = mg_read(client_conn, buf, sizeof(buf));
  886. ck_assert_int_eq(i, (int)strlen(expected));
  887. buf[i] = 0;
  888. ck_assert_str_eq(buf, expected);
  889. mg_close_connection(client_conn);
  890. /* Get data from callback using http://127.0.0.1 */
  891. client_conn = mg_download(
  892. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  893. ck_assert(client_conn != NULL);
  894. ri = mg_get_request_info(client_conn);
  895. ck_assert(ri != NULL);
  896. ck_assert_str_eq(ri->uri, "200");
  897. i = mg_read(client_conn, buf, sizeof(buf));
  898. if ((i >= 0) && ((size_t)i < sizeof(buf))) {
  899. buf[i] = 0;
  900. } else {
  901. ck_abort_msg(
  902. "ERROR: test_request_handlers: read returned %i (>=0, <%i)",
  903. (int)i,
  904. (int)sizeof(buf));
  905. }
  906. ck_assert((int)i < (int)sizeof(buf));
  907. ck_assert(i > 0);
  908. ck_assert_int_eq(i, (int)strlen(expected));
  909. buf[i] = 0;
  910. ck_assert_str_eq(buf, expected);
  911. mg_close_connection(client_conn);
  912. #if defined(USE_IPV6)
  913. /* Get data from callback using http://[::1] */
  914. client_conn =
  915. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  916. ck_assert(client_conn != NULL);
  917. ri = mg_get_request_info(client_conn);
  918. ck_assert(ri != NULL);
  919. ck_assert_str_eq(ri->uri, "200");
  920. i = mg_read(client_conn, buf, sizeof(buf));
  921. ck_assert_int_eq(i, (int)strlen(expected));
  922. buf[i] = 0;
  923. ck_assert_str_eq(buf, expected);
  924. mg_close_connection(client_conn);
  925. #endif
  926. #if !defined(NO_SSL)
  927. /* Get data from callback using https://127.0.0.1 */
  928. client_conn = mg_download(
  929. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  930. ck_assert(client_conn != NULL);
  931. ri = mg_get_request_info(client_conn);
  932. ck_assert(ri != NULL);
  933. ck_assert_str_eq(ri->uri, "200");
  934. i = mg_read(client_conn, buf, sizeof(buf));
  935. ck_assert_int_eq(i, (int)strlen(expected));
  936. buf[i] = 0;
  937. ck_assert_str_eq(buf, expected);
  938. mg_close_connection(client_conn);
  939. /* Get redirect from callback using http://127.0.0.1 */
  940. client_conn = mg_download(
  941. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  942. ck_assert(client_conn != NULL);
  943. ri = mg_get_request_info(client_conn);
  944. ck_assert(ri != NULL);
  945. ck_assert_str_eq(ri->uri, "302");
  946. i = mg_read(client_conn, buf, sizeof(buf));
  947. ck_assert_int_eq(i, -1);
  948. mg_close_connection(client_conn);
  949. #endif
  950. #if defined(USE_IPV6) && !defined(NO_SSL)
  951. /* Get data from callback using https://[::1] */
  952. client_conn =
  953. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  954. ck_assert(client_conn != NULL);
  955. ri = mg_get_request_info(client_conn);
  956. ck_assert(ri != NULL);
  957. ck_assert_str_eq(ri->uri, "200");
  958. i = mg_read(client_conn, buf, sizeof(buf));
  959. ck_assert_int_eq(i, (int)strlen(expected));
  960. buf[i] = 0;
  961. ck_assert_str_eq(buf, expected);
  962. mg_close_connection(client_conn);
  963. /* Get redirect from callback using http://127.0.0.1 */
  964. client_conn =
  965. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  966. ck_assert(client_conn != NULL);
  967. ri = mg_get_request_info(client_conn);
  968. ck_assert(ri != NULL);
  969. ck_assert_str_eq(ri->uri, "302");
  970. i = mg_read(client_conn, buf, sizeof(buf));
  971. ck_assert_int_eq(i, -1);
  972. mg_close_connection(client_conn);
  973. #endif
  974. /* It seems to be impossible to find out what the actual working
  975. * directory of the CI test environment is. Before breaking another
  976. * dozen of builds by trying blindly with different paths, just
  977. * create the file here */
  978. #ifdef _WIN32
  979. f = fopen("test.txt", "wb");
  980. #else
  981. f = fopen("test.txt", "w");
  982. #endif
  983. plain_file_content = "simple text file\n";
  984. fwrite(plain_file_content, 17, 1, f);
  985. fclose(f);
  986. #ifdef _WIN32
  987. f = fopen("test_gz.txt.gz", "wb");
  988. #else
  989. f = fopen("test_gz.txt.gz", "w");
  990. #endif
  991. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  992. "test_gz.txt"
  993. "\x00\x01\x11\x00\xee\xff"
  994. "zipped text file"
  995. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  996. fwrite(encoded_file_content, 1, 52, f);
  997. fclose(f);
  998. #ifdef _WIN32
  999. f = fopen("test.cgi", "wb");
  1000. cgi_script_content = "#!test.cgi.cmd\r\n";
  1001. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1002. fclose(f);
  1003. f = fopen("test.cgi.cmd", "w");
  1004. cgi_script_content = "@echo off\r\n"
  1005. "echo Connection: close\r\n"
  1006. "echo Content-Type: text/plain\r\n"
  1007. "echo.\r\n"
  1008. "echo CGI test\r\n"
  1009. "\r\n";
  1010. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1011. fclose(f);
  1012. #else
  1013. f = fopen("test.cgi", "w");
  1014. cgi_script_content = "#!/bin/sh\n\n"
  1015. "printf \"Connection: close\\r\\n\"\n"
  1016. "printf \"Content-Type: text/plain\\r\\n\"\n"
  1017. "printf \"\\r\\n\"\n"
  1018. "printf \"CGI test\\r\\n\"\n"
  1019. "\n";
  1020. (void)fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1021. (void)fclose(f);
  1022. (void)system("chmod a+x test.cgi");
  1023. #endif
  1024. expected_cgi_result = "CGI test";
  1025. /* Get static data */
  1026. client_conn = mg_download("localhost",
  1027. ipv4_port,
  1028. 0,
  1029. ebuf,
  1030. sizeof(ebuf),
  1031. "%s",
  1032. "GET /test.txt HTTP/1.0\r\n\r\n");
  1033. ck_assert(client_conn != NULL);
  1034. ri = mg_get_request_info(client_conn);
  1035. ck_assert(ri != NULL);
  1036. #if defined(NO_FILES)
  1037. ck_assert_str_eq(ri->uri, "404");
  1038. #else
  1039. ck_assert_str_eq(ri->uri, "200");
  1040. i = mg_read(client_conn, buf, sizeof(buf));
  1041. ck_assert_int_eq(i, 17);
  1042. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1043. buf[i] = 0;
  1044. }
  1045. ck_assert_str_eq(buf, plain_file_content);
  1046. #endif
  1047. mg_close_connection(client_conn);
  1048. /* Test with CGI test executable */
  1049. #if defined(_WIN32)
  1050. sprintf(cmd_buf, "copy %s\\cgi_test.cgi cgi_test.exe", locate_test_exes());
  1051. #else
  1052. sprintf(cmd_buf, "cp %s/cgi_test.cgi cgi_test.cgi", locate_test_exes());
  1053. #endif
  1054. (void)system(cmd_buf);
  1055. #if !defined(NO_CGI) && !defined(NO_FILES) && !defined(_WIN32)
  1056. /* TODO: add test for windows, check with POST */
  1057. client_conn = mg_download(
  1058. "localhost",
  1059. ipv4_port,
  1060. 0,
  1061. ebuf,
  1062. sizeof(ebuf),
  1063. "%s",
  1064. "POST /cgi_test.cgi HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC");
  1065. ck_assert(client_conn != NULL);
  1066. ri = mg_get_request_info(client_conn);
  1067. ck_assert(ri != NULL);
  1068. ck_assert_str_eq(ri->uri, "200");
  1069. mg_close_connection(client_conn);
  1070. #endif
  1071. /* Get zipped static data - will not work if Accept-Encoding is not set */
  1072. client_conn = mg_download("localhost",
  1073. ipv4_port,
  1074. 0,
  1075. ebuf,
  1076. sizeof(ebuf),
  1077. "%s",
  1078. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  1079. ck_assert(client_conn != NULL);
  1080. ri = mg_get_request_info(client_conn);
  1081. ck_assert(ri != NULL);
  1082. ck_assert_str_eq(ri->uri, "404");
  1083. mg_close_connection(client_conn);
  1084. /* Get zipped static data - with Accept-Encoding */
  1085. client_conn = mg_download(
  1086. "localhost",
  1087. ipv4_port,
  1088. 0,
  1089. ebuf,
  1090. sizeof(ebuf),
  1091. "%s",
  1092. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  1093. ck_assert(client_conn != NULL);
  1094. ri = mg_get_request_info(client_conn);
  1095. ck_assert(ri != NULL);
  1096. #if defined(NO_FILES)
  1097. ck_assert_str_eq(ri->uri, "404");
  1098. #else
  1099. ck_assert_str_eq(ri->uri, "200");
  1100. i = mg_read(client_conn, buf, sizeof(buf));
  1101. ck_assert_int_eq(i, 52);
  1102. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1103. buf[i] = 0;
  1104. }
  1105. ck_assert_int_eq(ri->content_length, 52);
  1106. ck_assert_str_eq(buf, encoded_file_content);
  1107. #endif
  1108. mg_close_connection(client_conn);
  1109. /* Get CGI generated data */
  1110. #if !defined(NO_CGI)
  1111. client_conn = mg_download("localhost",
  1112. ipv4_port,
  1113. 0,
  1114. ebuf,
  1115. sizeof(ebuf),
  1116. "%s",
  1117. "GET /test.cgi HTTP/1.0\r\n\r\n");
  1118. ck_assert(client_conn != NULL);
  1119. ri = mg_get_request_info(client_conn);
  1120. ck_assert(ri != NULL);
  1121. #if defined(NO_FILES)
  1122. ck_assert_str_eq(ri->uri, "404");
  1123. (void)expected_cgi_result;
  1124. (void)cgi_script_content;
  1125. #else
  1126. i = mg_read(client_conn, buf, sizeof(buf));
  1127. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1128. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  1129. i--;
  1130. }
  1131. buf[i] = 0;
  1132. }
  1133. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  1134. ck_assert_str_eq(buf, expected_cgi_result);
  1135. ck_assert_str_eq(ri->uri, "200");
  1136. mg_close_connection(client_conn);
  1137. #endif
  1138. #else
  1139. (void)expected_cgi_result;
  1140. (void)cgi_script_content;
  1141. #endif
  1142. /* Get directory listing */
  1143. client_conn = mg_download("localhost",
  1144. ipv4_port,
  1145. 0,
  1146. ebuf,
  1147. sizeof(ebuf),
  1148. "%s",
  1149. "GET / HTTP/1.0\r\n\r\n");
  1150. ck_assert(client_conn != NULL);
  1151. ri = mg_get_request_info(client_conn);
  1152. ck_assert(ri != NULL);
  1153. #if defined(NO_FILES)
  1154. ck_assert_str_eq(ri->uri, "404");
  1155. #else
  1156. ck_assert_str_eq(ri->uri, "200");
  1157. i = mg_read(client_conn, buf, sizeof(buf));
  1158. ck_assert(i > 6);
  1159. buf[6] = 0;
  1160. ck_assert_str_eq(buf, "<html>");
  1161. #endif
  1162. mg_close_connection(client_conn);
  1163. /* POST to static file (will not work) */
  1164. client_conn = mg_download("localhost",
  1165. ipv4_port,
  1166. 0,
  1167. ebuf,
  1168. sizeof(ebuf),
  1169. "%s",
  1170. "POST /test.txt HTTP/1.0\r\n\r\n");
  1171. ck_assert(client_conn != NULL);
  1172. ri = mg_get_request_info(client_conn);
  1173. ck_assert(ri != NULL);
  1174. #if defined(NO_FILES)
  1175. ck_assert_str_eq(ri->uri, "404");
  1176. #else
  1177. ck_assert_str_eq(ri->uri, "405");
  1178. i = mg_read(client_conn, buf, sizeof(buf));
  1179. ck_assert(i >= 29);
  1180. buf[29] = 0;
  1181. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  1182. #endif
  1183. mg_close_connection(client_conn);
  1184. /* PUT to static file (will not work) */
  1185. client_conn = mg_download("localhost",
  1186. ipv4_port,
  1187. 0,
  1188. ebuf,
  1189. sizeof(ebuf),
  1190. "%s",
  1191. "PUT /test.txt HTTP/1.0\r\n\r\n");
  1192. ck_assert(client_conn != NULL);
  1193. ri = mg_get_request_info(client_conn);
  1194. ck_assert(ri != NULL);
  1195. #if defined(NO_FILES)
  1196. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  1197. #else
  1198. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  1199. #endif
  1200. mg_close_connection(client_conn);
  1201. /* Get data from callback using mg_connect_client instead of mg_download */
  1202. memset(ebuf, 0, sizeof(ebuf));
  1203. client_conn =
  1204. mg_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf));
  1205. ck_assert(client_conn != NULL);
  1206. ck_assert_str_eq(ebuf, "");
  1207. mg_printf(client_conn, "%s", request);
  1208. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1209. ck_assert_int_ge(i, 0);
  1210. ck_assert_str_eq(ebuf, "");
  1211. ri = mg_get_request_info(client_conn);
  1212. ck_assert(ri != NULL);
  1213. ck_assert_str_eq(ri->uri, "200");
  1214. i = mg_read(client_conn, buf, sizeof(buf));
  1215. ck_assert_int_eq(i, (int)strlen(expected));
  1216. buf[i] = 0;
  1217. ck_assert_str_eq(buf, expected);
  1218. mg_close_connection(client_conn);
  1219. /* Get data from callback using mg_connect_client and absolute URI */
  1220. memset(ebuf, 0, sizeof(ebuf));
  1221. client_conn =
  1222. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1223. ck_assert(client_conn != NULL);
  1224. ck_assert_str_eq(ebuf, "");
  1225. mg_printf(client_conn,
  1226. "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1227. ipv4_port);
  1228. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1229. ck_assert_int_ge(i, 0);
  1230. ck_assert_str_eq(ebuf, "");
  1231. ri = mg_get_request_info(client_conn);
  1232. ck_assert(ri != NULL);
  1233. ck_assert_str_eq(ri->uri, "200");
  1234. i = mg_read(client_conn, buf, sizeof(buf));
  1235. ck_assert_int_eq(i, (int)strlen(expected));
  1236. buf[i] = 0;
  1237. ck_assert_str_eq(buf, expected);
  1238. mg_close_connection(client_conn);
  1239. /* Get data from callback using mg_connect_client and absolute URI with a
  1240. * sub-domain */
  1241. memset(ebuf, 0, sizeof(ebuf));
  1242. client_conn =
  1243. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1244. ck_assert(client_conn != NULL);
  1245. ck_assert_str_eq(ebuf, "");
  1246. mg_printf(client_conn,
  1247. "GET http://subdomain.test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1248. ipv4_port);
  1249. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1250. ck_assert_int_ge(i, 0);
  1251. ck_assert_str_eq(ebuf, "");
  1252. ri = mg_get_request_info(client_conn);
  1253. ck_assert(ri != NULL);
  1254. ck_assert_str_eq(ri->uri, "200");
  1255. i = mg_read(client_conn, buf, sizeof(buf));
  1256. ck_assert_int_eq(i, (int)strlen(expected));
  1257. buf[i] = 0;
  1258. ck_assert_str_eq(buf, expected);
  1259. mg_close_connection(client_conn);
  1260. /* Websocket test */
  1261. #ifdef USE_WEBSOCKET
  1262. /* Then connect a first client */
  1263. ws_client1_conn =
  1264. mg_connect_websocket_client("localhost",
  1265. ipv4_port,
  1266. 0,
  1267. ebuf,
  1268. sizeof(ebuf),
  1269. "/websocket",
  1270. NULL,
  1271. websocket_client_data_handler,
  1272. websocket_client_close_handler,
  1273. &ws_client1_data);
  1274. ck_assert(ws_client1_conn != NULL);
  1275. wait_not_null(
  1276. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1277. ck_assert_int_eq(ws_client1_data.closed, 0);
  1278. ck_assert_int_eq(ws_client2_data.closed, 0);
  1279. ck_assert_int_eq(ws_client3_data.closed, 0);
  1280. ck_assert(ws_client2_data.data == NULL);
  1281. ck_assert_uint_eq(ws_client2_data.len, 0);
  1282. ck_assert(ws_client1_data.data != NULL);
  1283. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1284. ck_assert(!memcmp(ws_client1_data.data,
  1285. websocket_welcome_msg,
  1286. websocket_welcome_msg_len));
  1287. free(ws_client1_data.data);
  1288. ws_client1_data.data = NULL;
  1289. ws_client1_data.len = 0;
  1290. mg_websocket_client_write(ws_client1_conn,
  1291. WEBSOCKET_OPCODE_TEXT,
  1292. "data1",
  1293. 5);
  1294. wait_not_null(
  1295. &(ws_client1_data
  1296. .data)); /* Wait for the websocket acknowledge message */
  1297. ck_assert_int_eq(ws_client1_data.closed, 0);
  1298. ck_assert_int_eq(ws_client2_data.closed, 0);
  1299. ck_assert(ws_client2_data.data == NULL);
  1300. ck_assert_uint_eq(ws_client2_data.len, 0);
  1301. ck_assert(ws_client1_data.data != NULL);
  1302. ck_assert_uint_eq(ws_client1_data.len, 3);
  1303. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1304. free(ws_client1_data.data);
  1305. ws_client1_data.data = NULL;
  1306. ws_client1_data.len = 0;
  1307. /* Now connect a second client */
  1308. #ifdef USE_IPV6
  1309. ws_client2_conn =
  1310. mg_connect_websocket_client("[::1]",
  1311. ipv6_port,
  1312. 0,
  1313. ebuf,
  1314. sizeof(ebuf),
  1315. "/websocket",
  1316. NULL,
  1317. websocket_client_data_handler,
  1318. websocket_client_close_handler,
  1319. &ws_client2_data);
  1320. #else
  1321. ws_client2_conn =
  1322. mg_connect_websocket_client("127.0.0.1",
  1323. ipv4_port,
  1324. 0,
  1325. ebuf,
  1326. sizeof(ebuf),
  1327. "/websocket",
  1328. NULL,
  1329. websocket_client_data_handler,
  1330. websocket_client_close_handler,
  1331. &ws_client2_data);
  1332. #endif
  1333. ck_assert(ws_client2_conn != NULL);
  1334. wait_not_null(
  1335. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1336. ck_assert(ws_client1_data.closed == 0);
  1337. ck_assert(ws_client2_data.closed == 0);
  1338. ck_assert(ws_client1_data.data == NULL);
  1339. ck_assert(ws_client1_data.len == 0);
  1340. ck_assert(ws_client2_data.data != NULL);
  1341. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1342. ck_assert(!memcmp(ws_client2_data.data,
  1343. websocket_welcome_msg,
  1344. websocket_welcome_msg_len));
  1345. free(ws_client2_data.data);
  1346. ws_client2_data.data = NULL;
  1347. ws_client2_data.len = 0;
  1348. mg_websocket_client_write(ws_client1_conn,
  1349. WEBSOCKET_OPCODE_TEXT,
  1350. "data2",
  1351. 5);
  1352. wait_not_null(
  1353. &(ws_client1_data
  1354. .data)); /* Wait for the websocket acknowledge message */
  1355. ck_assert(ws_client1_data.closed == 0);
  1356. ck_assert(ws_client2_data.closed == 0);
  1357. ck_assert(ws_client2_data.data == NULL);
  1358. ck_assert(ws_client2_data.len == 0);
  1359. ck_assert(ws_client1_data.data != NULL);
  1360. ck_assert(ws_client1_data.len == 4);
  1361. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1362. free(ws_client1_data.data);
  1363. ws_client1_data.data = NULL;
  1364. ws_client1_data.len = 0;
  1365. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1366. wait_not_null(
  1367. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1368. ck_assert(ws_client1_data.closed == 0);
  1369. ck_assert(ws_client2_data.closed == 0);
  1370. ck_assert(ws_client2_data.data == NULL);
  1371. ck_assert(ws_client2_data.len == 0);
  1372. ck_assert(ws_client1_data.data != NULL);
  1373. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1374. ck_assert(!memcmp(ws_client1_data.data,
  1375. websocket_goodbye_msg,
  1376. websocket_goodbye_msg_len));
  1377. free(ws_client1_data.data);
  1378. ws_client1_data.data = NULL;
  1379. ws_client1_data.len = 0;
  1380. mg_close_connection(ws_client1_conn);
  1381. test_sleep(3); /* Won't get any message */
  1382. ck_assert(ws_client1_data.closed == 1);
  1383. ck_assert(ws_client2_data.closed == 0);
  1384. ck_assert(ws_client1_data.data == NULL);
  1385. ck_assert(ws_client1_data.len == 0);
  1386. ck_assert(ws_client2_data.data == NULL);
  1387. ck_assert(ws_client2_data.len == 0);
  1388. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1389. wait_not_null(
  1390. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1391. ck_assert(ws_client1_data.closed == 1);
  1392. ck_assert(ws_client2_data.closed == 0);
  1393. ck_assert(ws_client1_data.data == NULL);
  1394. ck_assert(ws_client1_data.len == 0);
  1395. ck_assert(ws_client2_data.data != NULL);
  1396. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1397. ck_assert(!memcmp(ws_client2_data.data,
  1398. websocket_goodbye_msg,
  1399. websocket_goodbye_msg_len));
  1400. free(ws_client2_data.data);
  1401. ws_client2_data.data = NULL;
  1402. ws_client2_data.len = 0;
  1403. mg_close_connection(ws_client2_conn);
  1404. test_sleep(3); /* Won't get any message */
  1405. ck_assert(ws_client1_data.closed == 1);
  1406. ck_assert(ws_client2_data.closed == 1);
  1407. ck_assert(ws_client1_data.data == NULL);
  1408. ck_assert(ws_client1_data.len == 0);
  1409. ck_assert(ws_client2_data.data == NULL);
  1410. ck_assert(ws_client2_data.len == 0);
  1411. /* Connect client 3 */
  1412. ws_client3_conn =
  1413. mg_connect_websocket_client("localhost",
  1414. #if defined(NO_SSL)
  1415. ipv4_port,
  1416. 0,
  1417. #else
  1418. ipv4s_port,
  1419. 1,
  1420. #endif
  1421. ebuf,
  1422. sizeof(ebuf),
  1423. "/websocket",
  1424. NULL,
  1425. websocket_client_data_handler,
  1426. websocket_client_close_handler,
  1427. &ws_client3_data);
  1428. ck_assert(ws_client3_conn != NULL);
  1429. wait_not_null(
  1430. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1431. ck_assert(ws_client1_data.closed == 1);
  1432. ck_assert(ws_client2_data.closed == 1);
  1433. ck_assert(ws_client3_data.closed == 0);
  1434. ck_assert(ws_client1_data.data == NULL);
  1435. ck_assert(ws_client1_data.len == 0);
  1436. ck_assert(ws_client2_data.data == NULL);
  1437. ck_assert(ws_client2_data.len == 0);
  1438. ck_assert(ws_client3_data.data != NULL);
  1439. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1440. ck_assert(!memcmp(ws_client3_data.data,
  1441. websocket_welcome_msg,
  1442. websocket_welcome_msg_len));
  1443. free(ws_client3_data.data);
  1444. ws_client3_data.data = NULL;
  1445. ws_client3_data.len = 0;
  1446. /* Disconnect client 3 */
  1447. ck_assert(ws_client3_data.closed == 0);
  1448. mg_close_connection(ws_client3_conn);
  1449. ck_assert(ws_client3_data.closed == 1);
  1450. /* Connect client 4 */
  1451. ws_client4_conn =
  1452. mg_connect_websocket_client("localhost",
  1453. #if defined(NO_SSL)
  1454. ipv4_port,
  1455. 0,
  1456. #else
  1457. ipv4s_port,
  1458. 1,
  1459. #endif
  1460. ebuf,
  1461. sizeof(ebuf),
  1462. "/websocket",
  1463. NULL,
  1464. websocket_client_data_handler,
  1465. websocket_client_close_handler,
  1466. &ws_client4_data);
  1467. ck_assert(ws_client3_conn != NULL);
  1468. wait_not_null(
  1469. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1470. ck_assert(ws_client1_data.closed == 1);
  1471. ck_assert(ws_client2_data.closed == 1);
  1472. ck_assert(ws_client3_data.closed == 1);
  1473. ck_assert(ws_client4_data.closed == 0);
  1474. ck_assert(ws_client4_data.data != NULL);
  1475. ck_assert(ws_client4_data.len == websocket_welcome_msg_len);
  1476. ck_assert(!memcmp(ws_client4_data.data,
  1477. websocket_welcome_msg,
  1478. websocket_welcome_msg_len));
  1479. free(ws_client4_data.data);
  1480. ws_client4_data.data = NULL;
  1481. ws_client4_data.len = 0;
  1482. /* stop the server without closing this connection */
  1483. #endif
  1484. /* Close the server */
  1485. g_ctx = NULL;
  1486. mg_stop(ctx);
  1487. mark_point();
  1488. #ifdef USE_WEBSOCKET
  1489. for (i = 0; i < 100; i++) {
  1490. test_sleep(1);
  1491. if (ws_client3_data.closed != 0) {
  1492. mark_point();
  1493. break;
  1494. }
  1495. }
  1496. ck_assert_int_eq(ws_client4_data.closed, 1);
  1497. #endif
  1498. }
  1499. END_TEST
  1500. static int g_field_found_return = -999;
  1501. static int
  1502. field_found(const char *key,
  1503. const char *filename,
  1504. char *path,
  1505. size_t pathlen,
  1506. void *user_data)
  1507. {
  1508. ck_assert_ptr_ne(key, NULL);
  1509. ck_assert_ptr_ne(filename, NULL);
  1510. ck_assert_ptr_ne(path, NULL);
  1511. ck_assert_uint_gt(pathlen, 128);
  1512. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1513. ck_assert((g_field_found_return == FORM_FIELD_STORAGE_GET)
  1514. || (g_field_found_return == FORM_FIELD_STORAGE_STORE)
  1515. || (g_field_found_return == FORM_FIELD_STORAGE_SKIP)
  1516. || (g_field_found_return == FORM_FIELD_STORAGE_ABORT));
  1517. ck_assert_str_ne(key, "dontread");
  1518. if (!strcmp(key, "break_field_handler")) {
  1519. return FORM_FIELD_STORAGE_ABORT;
  1520. }
  1521. if (!strcmp(key, "continue_field_handler")) {
  1522. return FORM_FIELD_STORAGE_SKIP;
  1523. }
  1524. if (g_field_found_return == FORM_FIELD_STORAGE_STORE) {
  1525. strncpy(path, key, pathlen - 8);
  1526. strcat(path, ".txt");
  1527. }
  1528. return g_field_found_return;
  1529. }
  1530. static int g_field_step;
  1531. static int
  1532. field_get(const char *key,
  1533. const char *value_untruncated,
  1534. size_t valuelen,
  1535. void *user_data)
  1536. {
  1537. /* Copy the untruncated value, so string compare functions can be used. */
  1538. /* The check unit test library does not have build in memcmp functions. */
  1539. char *value = (char *)malloc(valuelen + 1);
  1540. ck_assert(value != NULL);
  1541. memcpy(value, value_untruncated, valuelen);
  1542. value[valuelen] = 0;
  1543. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1544. ck_assert_int_ge(g_field_step, 0);
  1545. ++g_field_step;
  1546. switch (g_field_step) {
  1547. case 1:
  1548. ck_assert_str_eq(key, "textin");
  1549. ck_assert_uint_eq(valuelen, 4);
  1550. ck_assert_str_eq(value, "text");
  1551. break;
  1552. case 2:
  1553. ck_assert_str_eq(key, "passwordin");
  1554. ck_assert_uint_eq(valuelen, 0);
  1555. ck_assert_str_eq(value, "");
  1556. break;
  1557. case 3:
  1558. ck_assert_str_eq(key, "radio1");
  1559. ck_assert_uint_eq(valuelen, 4);
  1560. ck_assert_str_eq(value, "val1");
  1561. break;
  1562. case 4:
  1563. ck_assert_str_eq(key, "radio2");
  1564. ck_assert_uint_eq(valuelen, 4);
  1565. ck_assert_str_eq(value, "val1");
  1566. break;
  1567. case 5:
  1568. ck_assert_str_eq(key, "check1");
  1569. ck_assert_uint_eq(valuelen, 4);
  1570. ck_assert_str_eq(value, "val1");
  1571. break;
  1572. case 6:
  1573. ck_assert_str_eq(key, "numberin");
  1574. ck_assert_uint_eq(valuelen, 1);
  1575. ck_assert_str_eq(value, "1");
  1576. break;
  1577. case 7:
  1578. ck_assert_str_eq(key, "datein");
  1579. ck_assert_uint_eq(valuelen, 8);
  1580. ck_assert_str_eq(value, "1.1.2016");
  1581. break;
  1582. case 8:
  1583. ck_assert_str_eq(key, "colorin");
  1584. ck_assert_uint_eq(valuelen, 7);
  1585. ck_assert_str_eq(value, "#80ff00");
  1586. break;
  1587. case 9:
  1588. ck_assert_str_eq(key, "rangein");
  1589. ck_assert_uint_eq(valuelen, 1);
  1590. ck_assert_str_eq(value, "3");
  1591. break;
  1592. case 10:
  1593. ck_assert_str_eq(key, "monthin");
  1594. ck_assert_uint_eq(valuelen, 0);
  1595. ck_assert_str_eq(value, "");
  1596. break;
  1597. case 11:
  1598. ck_assert_str_eq(key, "weekin");
  1599. ck_assert_uint_eq(valuelen, 0);
  1600. ck_assert_str_eq(value, "");
  1601. break;
  1602. case 12:
  1603. ck_assert_str_eq(key, "timein");
  1604. ck_assert_uint_eq(valuelen, 0);
  1605. ck_assert_str_eq(value, "");
  1606. break;
  1607. case 13:
  1608. ck_assert_str_eq(key, "datetimen");
  1609. ck_assert_uint_eq(valuelen, 0);
  1610. ck_assert_str_eq(value, "");
  1611. break;
  1612. case 14:
  1613. ck_assert_str_eq(key, "datetimelocalin");
  1614. ck_assert_uint_eq(valuelen, 0);
  1615. ck_assert_str_eq(value, "");
  1616. break;
  1617. case 15:
  1618. ck_assert_str_eq(key, "emailin");
  1619. ck_assert_uint_eq(valuelen, 0);
  1620. ck_assert_str_eq(value, "");
  1621. break;
  1622. case 16:
  1623. ck_assert_str_eq(key, "searchin");
  1624. ck_assert_uint_eq(valuelen, 0);
  1625. ck_assert_str_eq(value, "");
  1626. break;
  1627. case 17:
  1628. ck_assert_str_eq(key, "telin");
  1629. ck_assert_uint_eq(valuelen, 0);
  1630. ck_assert_str_eq(value, "");
  1631. break;
  1632. case 18:
  1633. ck_assert_str_eq(key, "urlin");
  1634. ck_assert_uint_eq(valuelen, 0);
  1635. ck_assert_str_eq(value, "");
  1636. break;
  1637. case 19:
  1638. ck_assert_str_eq(key, "filein");
  1639. ck_assert_uint_eq(valuelen, 0);
  1640. ck_assert_str_eq(value, "");
  1641. break;
  1642. case 20:
  1643. ck_assert_str_eq(key, "filesin");
  1644. ck_assert_uint_eq(valuelen, 0);
  1645. ck_assert_str_eq(value, "");
  1646. break;
  1647. case 21:
  1648. ck_assert_str_eq(key, "selectin");
  1649. ck_assert_uint_eq(valuelen, 4);
  1650. ck_assert_str_eq(value, "opt1");
  1651. break;
  1652. case 22:
  1653. ck_assert_str_eq(key, "message");
  1654. ck_assert_uint_eq(valuelen, 23);
  1655. ck_assert_str_eq(value, "Text area default text.");
  1656. break;
  1657. default:
  1658. ck_abort_msg("field_get called with g_field_step == %i",
  1659. (int)g_field_step);
  1660. }
  1661. free(value);
  1662. return 0;
  1663. }
  1664. static const char *myfile_content = "Content of myfile.txt\r\n";
  1665. static const int myfile_content_rep = 50000;
  1666. static int
  1667. field_store(const char *path, long long file_size, void *user_data)
  1668. {
  1669. FILE *f;
  1670. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1671. ck_assert_int_ge(g_field_step, 100);
  1672. ++g_field_step;
  1673. switch (g_field_step) {
  1674. case 101:
  1675. ck_assert_str_eq(path, "storeme.txt");
  1676. ck_assert_int_eq(file_size, 9);
  1677. f = fopen(path, "r");
  1678. ck_assert_ptr_ne(f, NULL);
  1679. if (f) {
  1680. char buf[32] = {0};
  1681. int i = (int)fread(buf, 1, 31, f);
  1682. ck_assert_int_eq(i, 9);
  1683. fclose(f);
  1684. ck_assert_str_eq(buf, "storetest");
  1685. }
  1686. break;
  1687. case 102:
  1688. ck_assert_str_eq(path, "file2store.txt");
  1689. ck_assert_uint_eq(23, strlen(myfile_content));
  1690. ck_assert_int_eq(file_size, 23 * myfile_content_rep);
  1691. #ifdef _WIN32
  1692. f = fopen(path, "rb");
  1693. #else
  1694. f = fopen(path, "r");
  1695. #endif
  1696. ck_assert_ptr_ne(f, NULL);
  1697. if (f) {
  1698. char buf[32] = {0};
  1699. int r, i;
  1700. for (r = 0; r < myfile_content_rep; r++) {
  1701. i = (int)fread(buf, 1, 23, f);
  1702. ck_assert_int_eq(i, 23);
  1703. ck_assert_str_eq(buf, myfile_content);
  1704. }
  1705. i = (int)fread(buf, 1, 23, f);
  1706. ck_assert_int_eq(i, 0);
  1707. fclose(f);
  1708. }
  1709. break;
  1710. default:
  1711. ck_abort_msg("field_get called with g_field_step == %i",
  1712. (int)g_field_step);
  1713. }
  1714. return 0;
  1715. }
  1716. static int
  1717. FormGet(struct mg_connection *conn, void *cbdata)
  1718. {
  1719. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1720. int ret;
  1721. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  1722. (void)cbdata;
  1723. ck_assert(req_info != NULL);
  1724. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1725. fdh.user_data = (void *)&g_field_found_return;
  1726. /* Call the form handler */
  1727. g_field_step = 0;
  1728. g_field_found_return = FORM_FIELD_STORAGE_GET;
  1729. ret = mg_handle_form_request(conn, &fdh);
  1730. g_field_found_return = -888;
  1731. ck_assert_int_eq(ret, 22);
  1732. ck_assert_int_eq(g_field_step, 22);
  1733. mg_printf(conn, "%i\r\n", ret);
  1734. g_field_step = 1000;
  1735. return 1;
  1736. }
  1737. static int
  1738. FormStore(struct mg_connection *conn,
  1739. void *cbdata,
  1740. int ret_expected,
  1741. int field_step_expected)
  1742. {
  1743. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1744. int ret;
  1745. struct mg_form_data_handler fdh = {field_found,
  1746. field_get,
  1747. field_store,
  1748. NULL};
  1749. (void)cbdata;
  1750. ck_assert(req_info != NULL);
  1751. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1752. fdh.user_data = (void *)&g_field_found_return;
  1753. /* Call the form handler */
  1754. g_field_step = 100;
  1755. g_field_found_return = FORM_FIELD_STORAGE_STORE;
  1756. ret = mg_handle_form_request(conn, &fdh);
  1757. ck_assert_int_eq(ret, ret_expected);
  1758. ck_assert_int_eq(g_field_step, field_step_expected);
  1759. mg_printf(conn, "%i\r\n", ret);
  1760. g_field_step = 1000;
  1761. return 1;
  1762. }
  1763. static int
  1764. FormStore1(struct mg_connection *conn, void *cbdata)
  1765. {
  1766. return FormStore(conn, cbdata, 3, 101);
  1767. }
  1768. static int
  1769. FormStore2(struct mg_connection *conn, void *cbdata)
  1770. {
  1771. return FormStore(conn, cbdata, 4, 102);
  1772. }
  1773. static void
  1774. send_chunk_string(struct mg_connection *conn, const char *txt)
  1775. {
  1776. unsigned int chunk_len = (unsigned int)strlen(txt);
  1777. mg_printf(conn, "%x\r\n", chunk_len);
  1778. mg_write(conn, txt, chunk_len);
  1779. mg_printf(conn, "\r\n");
  1780. }
  1781. START_TEST(test_handle_form)
  1782. {
  1783. struct mg_context *ctx;
  1784. struct mg_connection *client_conn;
  1785. const struct mg_request_info *ri;
  1786. const char *OPTIONS[8];
  1787. const char *opt;
  1788. int opt_idx = 0;
  1789. char ebuf[100];
  1790. const char *multipart_body;
  1791. const char *boundary;
  1792. size_t body_len, body_sent, chunk_len;
  1793. int sleep_cnt;
  1794. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  1795. OPTIONS[opt_idx++] = "listening_ports";
  1796. OPTIONS[opt_idx++] = "8884";
  1797. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  1798. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  1799. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  1800. mark_point();
  1801. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  1802. ck_assert(ctx != NULL);
  1803. g_ctx = ctx;
  1804. opt = mg_get_option(ctx, "listening_ports");
  1805. ck_assert_str_eq(opt, "8884");
  1806. mg_set_request_handler(ctx, "/handle_form", FormGet, (void *)0);
  1807. mg_set_request_handler(ctx, "/handle_form_store", FormStore1, (void *)0);
  1808. mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, (void *)0);
  1809. test_sleep(1);
  1810. /* Handle form: "GET" */
  1811. client_conn = mg_download("localhost",
  1812. 8884,
  1813. 0,
  1814. ebuf,
  1815. sizeof(ebuf),
  1816. "%s",
  1817. "GET /handle_form"
  1818. "?textin=text&passwordin=&radio1=val1"
  1819. "&radio2=val1&check1=val1&numberin=1"
  1820. "&datein=1.1.2016&colorin=%2380ff00"
  1821. "&rangein=3&monthin=&weekin=&timein="
  1822. "&datetimen=&datetimelocalin=&emailin="
  1823. "&searchin=&telin=&urlin=&filein="
  1824. "&filesin=&selectin=opt1"
  1825. "&message=Text+area+default+text. "
  1826. "HTTP/1.0\r\n"
  1827. "Host: localhost:8884\r\n"
  1828. "Connection: close\r\n\r\n");
  1829. ck_assert(client_conn != NULL);
  1830. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1831. test_sleep(1);
  1832. if (g_field_step == 1000) {
  1833. break;
  1834. }
  1835. }
  1836. ri = mg_get_request_info(client_conn);
  1837. ck_assert(ri != NULL);
  1838. ck_assert_str_eq(ri->uri, "200");
  1839. mg_close_connection(client_conn);
  1840. /* Handle form: "POST x-www-form-urlencoded" */
  1841. client_conn =
  1842. mg_download("localhost",
  1843. 8884,
  1844. 0,
  1845. ebuf,
  1846. sizeof(ebuf),
  1847. "%s",
  1848. "POST /handle_form HTTP/1.1\r\n"
  1849. "Host: localhost:8884\r\n"
  1850. "Connection: close\r\n"
  1851. "Content-Type: application/x-www-form-urlencoded\r\n"
  1852. "Content-Length: 263\r\n"
  1853. "\r\n"
  1854. "textin=text&passwordin=&radio1=val1&radio2=val1"
  1855. "&check1=val1&numberin=1&datein=1.1.2016"
  1856. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  1857. "&timein=&datetimen=&datetimelocalin=&emailin="
  1858. "&searchin=&telin=&urlin=&filein=&filesin="
  1859. "&selectin=opt1&message=Text+area+default+text.");
  1860. ck_assert(client_conn != NULL);
  1861. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1862. test_sleep(1);
  1863. if (g_field_step == 1000) {
  1864. break;
  1865. }
  1866. }
  1867. ri = mg_get_request_info(client_conn);
  1868. ck_assert(ri != NULL);
  1869. ck_assert_str_eq(ri->uri, "200");
  1870. mg_close_connection(client_conn);
  1871. /* Handle form: "POST multipart/form-data" */
  1872. multipart_body =
  1873. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1874. "Content-Disposition: form-data; name=\"textin\"\r\n"
  1875. "\r\n"
  1876. "text\r\n"
  1877. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1878. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  1879. "\r\n"
  1880. "\r\n"
  1881. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1882. "Content-Disposition: form-data; name=\"radio1\"\r\n"
  1883. "\r\n"
  1884. "val1\r\n"
  1885. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1886. "Content-Disposition: form-data; name=\"radio2\"\r\n"
  1887. "\r\n"
  1888. "val1\r\n"
  1889. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1890. "Content-Disposition: form-data; name=\"check1\"\r\n"
  1891. "\r\n"
  1892. "val1\r\n"
  1893. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1894. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  1895. "\r\n"
  1896. "1\r\n"
  1897. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1898. "Content-Disposition: form-data; name=\"datein\"\r\n"
  1899. "\r\n"
  1900. "1.1.2016\r\n"
  1901. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1902. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  1903. "\r\n"
  1904. "#80ff00\r\n"
  1905. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1906. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  1907. "\r\n"
  1908. "3\r\n"
  1909. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1910. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  1911. "\r\n"
  1912. "\r\n"
  1913. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1914. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  1915. "\r\n"
  1916. "\r\n"
  1917. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1918. "Content-Disposition: form-data; name=\"timein\"\r\n"
  1919. "\r\n"
  1920. "\r\n"
  1921. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1922. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  1923. "\r\n"
  1924. "\r\n"
  1925. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1926. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  1927. "\r\n"
  1928. "\r\n"
  1929. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1930. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  1931. "\r\n"
  1932. "\r\n"
  1933. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1934. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  1935. "\r\n"
  1936. "\r\n"
  1937. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1938. "Content-Disposition: form-data; name=\"telin\"\r\n"
  1939. "\r\n"
  1940. "\r\n"
  1941. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1942. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  1943. "\r\n"
  1944. "\r\n"
  1945. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1946. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  1947. "Content-Type: application/octet-stream\r\n"
  1948. "\r\n"
  1949. "\r\n"
  1950. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1951. "Content-Disposition: form-data; name=\"filesin\"; filename=\"\"\r\n"
  1952. "Content-Type: application/octet-stream\r\n"
  1953. "\r\n"
  1954. "\r\n"
  1955. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1956. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  1957. "\r\n"
  1958. "opt1\r\n"
  1959. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  1960. "Content-Disposition: form-data; name=\"message\"\r\n"
  1961. "\r\n"
  1962. "Text area default text.\r\n"
  1963. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  1964. body_len = strlen(multipart_body);
  1965. ck_assert_uint_eq(body_len, 2374); /* not required */
  1966. client_conn =
  1967. mg_download("localhost",
  1968. 8884,
  1969. 0,
  1970. ebuf,
  1971. sizeof(ebuf),
  1972. "POST /handle_form HTTP/1.1\r\n"
  1973. "Host: localhost:8884\r\n"
  1974. "Connection: close\r\n"
  1975. "Content-Type: multipart/form-data; "
  1976. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  1977. "Content-Length: %u\r\n"
  1978. "\r\n%s",
  1979. (unsigned int)body_len,
  1980. multipart_body);
  1981. ck_assert(client_conn != NULL);
  1982. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  1983. test_sleep(1);
  1984. if (g_field_step == 1000) {
  1985. break;
  1986. }
  1987. }
  1988. ri = mg_get_request_info(client_conn);
  1989. ck_assert(ri != NULL);
  1990. ck_assert_str_eq(ri->uri, "200");
  1991. mg_close_connection(client_conn);
  1992. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  1993. client_conn =
  1994. mg_download("localhost",
  1995. 8884,
  1996. 0,
  1997. ebuf,
  1998. sizeof(ebuf),
  1999. "%s",
  2000. "POST /handle_form HTTP/1.1\r\n"
  2001. "Host: localhost:8884\r\n"
  2002. "Connection: close\r\n"
  2003. "Content-Type: multipart/form-data; "
  2004. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2005. "Transfer-Encoding: chunked\r\n"
  2006. "\r\n");
  2007. ck_assert(client_conn != NULL);
  2008. body_len = strlen(multipart_body);
  2009. chunk_len = 1;
  2010. body_sent = 0;
  2011. while (body_len > body_sent) {
  2012. if (chunk_len > (body_len - body_sent)) {
  2013. chunk_len = body_len - body_sent;
  2014. }
  2015. ck_assert_int_gt((int)chunk_len, 0);
  2016. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2017. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2018. mg_printf(client_conn, "\r\n");
  2019. body_sent += chunk_len;
  2020. chunk_len = (chunk_len % 40) + 1;
  2021. }
  2022. mg_printf(client_conn, "0\r\n");
  2023. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2024. test_sleep(1);
  2025. if (g_field_step == 1000) {
  2026. break;
  2027. }
  2028. }
  2029. ri = mg_get_request_info(client_conn);
  2030. ck_assert(ri != NULL);
  2031. ck_assert_str_eq(ri->uri, "200");
  2032. mg_close_connection(client_conn);
  2033. /* Now test form_store */
  2034. /* First test with GET */
  2035. client_conn = mg_download("localhost",
  2036. 8884,
  2037. 0,
  2038. ebuf,
  2039. sizeof(ebuf),
  2040. "%s",
  2041. "GET /handle_form_store"
  2042. "?storeme=storetest"
  2043. "&continue_field_handler=ignore"
  2044. "&break_field_handler=abort"
  2045. "&dontread=xyz "
  2046. "HTTP/1.0\r\n"
  2047. "Host: localhost:8884\r\n"
  2048. "Connection: close\r\n\r\n");
  2049. ck_assert(client_conn != NULL);
  2050. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2051. test_sleep(1);
  2052. if (g_field_step == 1000) {
  2053. break;
  2054. }
  2055. }
  2056. ri = mg_get_request_info(client_conn);
  2057. ck_assert(ri != NULL);
  2058. ck_assert_str_eq(ri->uri, "200");
  2059. mg_close_connection(client_conn);
  2060. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  2061. client_conn =
  2062. mg_download("localhost",
  2063. 8884,
  2064. 0,
  2065. ebuf,
  2066. sizeof(ebuf),
  2067. "%s",
  2068. "POST /handle_form_store HTTP/1.0\r\n"
  2069. "Host: localhost:8884\r\n"
  2070. "Connection: close\r\n"
  2071. "Content-Type: application/x-www-form-urlencoded\r\n"
  2072. "Transfer-Encoding: chunked\r\n"
  2073. "\r\n");
  2074. ck_assert(client_conn != NULL);
  2075. send_chunk_string(client_conn, "storeme=store");
  2076. send_chunk_string(client_conn, "test&");
  2077. send_chunk_string(client_conn, "continue_field_handler=ignore");
  2078. send_chunk_string(client_conn, "&br");
  2079. test_sleep(1);
  2080. send_chunk_string(client_conn, "eak_field_handler=abort&");
  2081. send_chunk_string(client_conn, "dontread=xyz");
  2082. mg_printf(client_conn, "0\r\n");
  2083. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2084. test_sleep(1);
  2085. if (g_field_step == 1000) {
  2086. break;
  2087. }
  2088. }
  2089. ri = mg_get_request_info(client_conn);
  2090. ck_assert(ri != NULL);
  2091. ck_assert_str_eq(ri->uri, "200");
  2092. mg_close_connection(client_conn);
  2093. /* Handle form: "POST multipart/form-data", chunked, store */
  2094. client_conn =
  2095. mg_download("localhost",
  2096. 8884,
  2097. 0,
  2098. ebuf,
  2099. sizeof(ebuf),
  2100. "%s",
  2101. "POST /handle_form_store HTTP/1.0\r\n"
  2102. "Host: localhost:8884\r\n"
  2103. "Connection: close\r\n"
  2104. "Content-Type: multipart/form-data; "
  2105. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2106. "Transfer-Encoding: chunked\r\n"
  2107. "\r\n");
  2108. ck_assert(client_conn != NULL);
  2109. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2110. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  2111. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2112. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2113. send_chunk_string(client_conn, "\r\n");
  2114. send_chunk_string(client_conn, "storetest\r\n");
  2115. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2116. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2117. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2118. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  2119. send_chunk_string(client_conn, "\r\n");
  2120. send_chunk_string(client_conn, "ignore\r\n");
  2121. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2122. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2123. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2124. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2125. send_chunk_string(client_conn, "\r\n");
  2126. send_chunk_string(client_conn, "abort\r\n");
  2127. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2128. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2129. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2130. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2131. send_chunk_string(client_conn, "\r\n");
  2132. send_chunk_string(client_conn, "xyz\r\n");
  2133. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2134. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2135. mg_printf(client_conn, "0\r\n");
  2136. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2137. test_sleep(1);
  2138. if (g_field_step == 1000) {
  2139. break;
  2140. }
  2141. }
  2142. ri = mg_get_request_info(client_conn);
  2143. ck_assert(ri != NULL);
  2144. ck_assert_str_eq(ri->uri, "200");
  2145. mg_close_connection(client_conn);
  2146. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  2147. client_conn =
  2148. mg_download("localhost",
  2149. 8884,
  2150. 0,
  2151. ebuf,
  2152. sizeof(ebuf),
  2153. "%s",
  2154. "POST /handle_form_store2 HTTP/1.0\r\n"
  2155. "Host: localhost:8884\r\n"
  2156. "Connection: close\r\n"
  2157. "Content-Type: multipart/form-data; "
  2158. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2159. "Transfer-Encoding: chunked\r\n"
  2160. "\r\n");
  2161. ck_assert(client_conn != NULL);
  2162. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  2163. send_chunk_string(client_conn, boundary);
  2164. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2165. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2166. send_chunk_string(client_conn, "\r\n");
  2167. send_chunk_string(client_conn, "storetest\r\n");
  2168. send_chunk_string(client_conn, boundary);
  2169. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2170. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  2171. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  2172. send_chunk_string(client_conn, "Content-Type: ");
  2173. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2174. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2175. send_chunk_string(client_conn, "\r\n");
  2176. /* send some megabyte of data */
  2177. body_sent = 0;
  2178. do {
  2179. send_chunk_string(client_conn, "ignore\r\n");
  2180. body_sent += 8;
  2181. /* send some strings that are almost boundaries */
  2182. for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
  2183. /* chunks from 1 byte to strlen(boundary)-1 */
  2184. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2185. mg_write(client_conn, boundary, chunk_len);
  2186. mg_printf(client_conn, "\r\n");
  2187. body_sent += chunk_len;
  2188. }
  2189. } while (body_sent < 1024 * 1024);
  2190. send_chunk_string(client_conn, "\r\n");
  2191. send_chunk_string(client_conn, boundary);
  2192. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2193. send_chunk_string(client_conn, "name=\"file2store\";");
  2194. send_chunk_string(client_conn, "filename=\"myfile.txt\"\r\n");
  2195. send_chunk_string(client_conn, "Content-Type: ");
  2196. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2197. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2198. send_chunk_string(client_conn, "\r\n");
  2199. for (body_sent = 0; (int)body_sent < (int)myfile_content_rep; body_sent++) {
  2200. send_chunk_string(client_conn, myfile_content);
  2201. }
  2202. send_chunk_string(client_conn, "\r\n");
  2203. send_chunk_string(client_conn, boundary);
  2204. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2205. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2206. send_chunk_string(client_conn, "\r\n");
  2207. send_chunk_string(client_conn, "abort\r\n");
  2208. send_chunk_string(client_conn, boundary);
  2209. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2210. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2211. send_chunk_string(client_conn, "\r\n");
  2212. send_chunk_string(client_conn, "xyz\r\n");
  2213. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2214. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2215. mg_printf(client_conn, "0\r\n");
  2216. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2217. test_sleep(1);
  2218. if (g_field_step == 1000) {
  2219. break;
  2220. }
  2221. }
  2222. ri = mg_get_request_info(client_conn);
  2223. ck_assert(ri != NULL);
  2224. ck_assert_str_eq(ri->uri, "200");
  2225. mg_close_connection(client_conn);
  2226. /* Close the server */
  2227. g_ctx = NULL;
  2228. mg_stop(ctx);
  2229. mark_point();
  2230. }
  2231. END_TEST
  2232. START_TEST(test_http_auth)
  2233. {
  2234. #if !defined(NO_FILES)
  2235. const char *OPTIONS[] = {
  2236. "document_root",
  2237. ".",
  2238. "listening_ports",
  2239. "8080",
  2240. #if !defined(NO_CACHING)
  2241. "static_file_max_age",
  2242. "0",
  2243. #endif
  2244. NULL,
  2245. };
  2246. struct mg_context *ctx;
  2247. struct mg_connection *client_conn;
  2248. char client_err[256], nonce[256];
  2249. const struct mg_request_info *client_ri;
  2250. int client_res;
  2251. FILE *f;
  2252. const char *passwd_file = ".htpasswd";
  2253. const char *test_file = "test_http_auth.test_file.txt";
  2254. const char *test_content = "test_http_auth test_file content";
  2255. const char *domain;
  2256. const char *doc_root;
  2257. const char *auth_request;
  2258. const char *str;
  2259. size_t len;
  2260. int i;
  2261. char HA1[256], HA2[256], HA[256];
  2262. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2263. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2264. const char *nc = "00000001";
  2265. const char *cnonce = "6789ABCD";
  2266. /* Start with default options */
  2267. mark_point();
  2268. ctx = mg_start(NULL, NULL, OPTIONS);
  2269. test_sleep(1);
  2270. ck_assert(ctx != NULL);
  2271. domain = mg_get_option(ctx, "authentication_domain");
  2272. ck_assert(domain != NULL);
  2273. len = strlen(domain);
  2274. ck_assert_uint_gt(len, 0);
  2275. ck_assert_uint_lt(len, 64);
  2276. doc_root = mg_get_option(ctx, "document_root");
  2277. ck_assert_str_eq(doc_root, ".");
  2278. /* Create a default file in the document root */
  2279. f = fopen(test_file, "w");
  2280. if (f) {
  2281. fprintf(f, "%s", test_content);
  2282. fclose(f);
  2283. } else {
  2284. ck_abort_msg("Cannot create file %s", test_file);
  2285. }
  2286. remove(passwd_file);
  2287. /* Read file before a .htpasswd file has been created */
  2288. memset(client_err, 0, sizeof(client_err));
  2289. client_conn =
  2290. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2291. ck_assert(client_conn != NULL);
  2292. ck_assert_str_eq(client_err, "");
  2293. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2294. client_res =
  2295. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2296. ck_assert_int_ge(client_res, 0);
  2297. ck_assert_str_eq(client_err, "");
  2298. client_ri = mg_get_request_info(client_conn);
  2299. ck_assert(client_ri != NULL);
  2300. ck_assert_str_eq(client_ri->uri, "200");
  2301. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2302. ck_assert_int_gt(client_res, 0);
  2303. ck_assert_int_le(client_res, sizeof(client_err));
  2304. ck_assert_str_eq(client_err, test_content);
  2305. mg_close_connection(client_conn);
  2306. test_sleep(1);
  2307. /* Create a .htpasswd file */
  2308. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2309. ck_assert_int_eq(client_res, 1);
  2310. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2311. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2312. test_sleep(1);
  2313. /* Repeat test after .htpasswd is created */
  2314. memset(client_err, 0, sizeof(client_err));
  2315. client_conn =
  2316. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2317. ck_assert(client_conn != NULL);
  2318. ck_assert_str_eq(client_err, "");
  2319. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2320. client_res =
  2321. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2322. ck_assert_int_ge(client_res, 0);
  2323. ck_assert_str_eq(client_err, "");
  2324. client_ri = mg_get_request_info(client_conn);
  2325. ck_assert(client_ri != NULL);
  2326. ck_assert_str_eq(client_ri->uri, "401");
  2327. auth_request = NULL;
  2328. for (i = 0; i < client_ri->num_headers; i++) {
  2329. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2330. "WWW-Authenticate")) {
  2331. ck_assert_ptr_eq(auth_request, NULL);
  2332. auth_request = client_ri->http_headers[i].value;
  2333. ck_assert_ptr_ne(auth_request, NULL);
  2334. }
  2335. }
  2336. ck_assert_ptr_ne(auth_request, NULL);
  2337. str = "Digest qop=\"auth\", realm=\"";
  2338. len = strlen(str);
  2339. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2340. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2341. len += strlen(domain);
  2342. str = "\", nonce=\"";
  2343. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2344. len += strlen(str);
  2345. str = strchr(auth_request + len, '\"');
  2346. ck_assert_ptr_ne(str, NULL);
  2347. ck_assert_ptr_ne(str, auth_request + len);
  2348. /* nonce is from including (auth_request + len) to excluding (str) */
  2349. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2350. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2351. (ptrdiff_t)sizeof(nonce));
  2352. memset(nonce, 0, sizeof(nonce));
  2353. memcpy(nonce,
  2354. auth_request + len,
  2355. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2356. memset(HA1, 0, sizeof(HA1));
  2357. memset(HA2, 0, sizeof(HA2));
  2358. memset(HA, 0, sizeof(HA));
  2359. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2360. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2361. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2362. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2363. sprintf(HA2, "%s:/%s", "GET", test_file);
  2364. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2365. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2366. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2367. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2368. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2369. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2370. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2371. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2372. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2373. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2374. HA_md5_ret = mg_md5(HA_md5_buf,
  2375. HA1_md5_buf,
  2376. ":",
  2377. nonce,
  2378. ":",
  2379. nc,
  2380. ":",
  2381. cnonce,
  2382. ":",
  2383. "auth",
  2384. ":",
  2385. HA2_md5_buf,
  2386. NULL);
  2387. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2388. /* Retry with Authorization */
  2389. memset(client_err, 0, sizeof(client_err));
  2390. client_conn =
  2391. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2392. ck_assert(client_conn != NULL);
  2393. ck_assert_str_eq(client_err, "");
  2394. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2395. mg_printf(client_conn,
  2396. "Authorization: Digest "
  2397. "username=\"%s\", "
  2398. "realm=\"%s\", "
  2399. "nonce=\"%s\", "
  2400. "uri=\"/%s\", "
  2401. "qop=auth, "
  2402. "nc=%s, "
  2403. "cnonce=\"%s\", "
  2404. "response=\"%s\"\r\n\r\n",
  2405. "user",
  2406. domain,
  2407. nonce,
  2408. test_file,
  2409. nc,
  2410. cnonce,
  2411. HA_md5_buf);
  2412. client_res =
  2413. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2414. ck_assert_int_ge(client_res, 0);
  2415. ck_assert_str_eq(client_err, "");
  2416. client_ri = mg_get_request_info(client_conn);
  2417. ck_assert(client_ri != NULL);
  2418. ck_assert_str_eq(client_ri->uri, "200");
  2419. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2420. ck_assert_int_gt(client_res, 0);
  2421. ck_assert_int_le(client_res, sizeof(client_err));
  2422. ck_assert_str_eq(client_err, test_content);
  2423. mg_close_connection(client_conn);
  2424. test_sleep(1);
  2425. /* Remove the user from the .htpasswd file again */
  2426. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2427. ck_assert_int_eq(client_res, 1);
  2428. test_sleep(1);
  2429. /* Try to access the file again. Expected: 401 error */
  2430. memset(client_err, 0, sizeof(client_err));
  2431. client_conn =
  2432. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2433. ck_assert(client_conn != NULL);
  2434. ck_assert_str_eq(client_err, "");
  2435. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2436. client_res =
  2437. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2438. ck_assert_int_ge(client_res, 0);
  2439. ck_assert_str_eq(client_err, "");
  2440. client_ri = mg_get_request_info(client_conn);
  2441. ck_assert(client_ri != NULL);
  2442. ck_assert_str_eq(client_ri->uri, "401");
  2443. mg_close_connection(client_conn);
  2444. test_sleep(1);
  2445. /* Now remove the password file */
  2446. remove(passwd_file);
  2447. test_sleep(1);
  2448. /* Access to the file must work like before */
  2449. memset(client_err, 0, sizeof(client_err));
  2450. client_conn =
  2451. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2452. ck_assert(client_conn != NULL);
  2453. ck_assert_str_eq(client_err, "");
  2454. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2455. client_res =
  2456. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2457. ck_assert_int_ge(client_res, 0);
  2458. ck_assert_str_eq(client_err, "");
  2459. client_ri = mg_get_request_info(client_conn);
  2460. ck_assert(client_ri != NULL);
  2461. ck_assert_str_eq(client_ri->uri, "200");
  2462. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2463. ck_assert_int_gt(client_res, 0);
  2464. ck_assert_int_le(client_res, sizeof(client_err));
  2465. ck_assert_str_eq(client_err, test_content);
  2466. mg_close_connection(client_conn);
  2467. test_sleep(1);
  2468. /* Stop the server and clean up */
  2469. mg_stop(ctx);
  2470. remove(test_file);
  2471. #endif
  2472. }
  2473. END_TEST
  2474. START_TEST(test_keep_alive)
  2475. {
  2476. struct mg_context *ctx;
  2477. const char *OPTIONS[] =
  2478. { "listening_ports",
  2479. "8080",
  2480. "request_timeout_ms",
  2481. "10000",
  2482. "enable_keep_alive",
  2483. "yes",
  2484. #if !defined(NO_FILES)
  2485. "document_root",
  2486. ".",
  2487. "enable_directory_listing",
  2488. "no",
  2489. #endif
  2490. NULL };
  2491. struct mg_connection *client_conn;
  2492. char client_err[256];
  2493. const struct mg_request_info *client_ri;
  2494. int client_res, i;
  2495. const char *connection_header;
  2496. mark_point();
  2497. ctx = mg_start(NULL, NULL, OPTIONS);
  2498. ck_assert(ctx != NULL);
  2499. test_sleep(1);
  2500. /* HTTP 1.1 GET request */
  2501. memset(client_err, 0, sizeof(client_err));
  2502. client_conn =
  2503. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2504. ck_assert(client_conn != NULL);
  2505. ck_assert_str_eq(client_err, "");
  2506. mg_printf(client_conn,
  2507. "GET / HTTP/1.1\r\nHost: "
  2508. "localhost:8080\r\nConnection: keep-alive\r\n\r\n");
  2509. client_res =
  2510. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2511. ck_assert_int_ge(client_res, 0);
  2512. ck_assert_str_eq(client_err, "");
  2513. client_ri = mg_get_request_info(client_conn);
  2514. ck_assert(client_ri != NULL);
  2515. #if defined(NO_FILES)
  2516. ck_assert_str_eq(client_ri->uri, "404");
  2517. #else
  2518. ck_assert_str_eq(client_ri->uri, "403");
  2519. #endif
  2520. connection_header = 0;
  2521. for (i = 0; i < client_ri->num_headers; i++) {
  2522. if (!mg_strcasecmp(client_ri->http_headers[i].name, "Connection")) {
  2523. ck_assert_ptr_eq(connection_header, NULL);
  2524. connection_header = client_ri->http_headers[i].value;
  2525. ck_assert_ptr_ne(connection_header, NULL);
  2526. }
  2527. }
  2528. /* Error replies will close the connection, even if keep-alive is set. */
  2529. ck_assert_ptr_ne(connection_header, NULL);
  2530. ck_assert_str_eq(connection_header, "close");
  2531. mg_close_connection(client_conn);
  2532. test_sleep(1);
  2533. /* TODO: request a file and keep alive
  2534. * (will only work if NO_FILES is not set). */
  2535. /* Stop the server and clean up */
  2536. mg_stop(ctx);
  2537. }
  2538. END_TEST
  2539. Suite *
  2540. make_public_server_suite(void)
  2541. {
  2542. Suite *const suite = suite_create("PublicServer");
  2543. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  2544. TCase *const tcase_startthreads = tcase_create("Start threads");
  2545. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  2546. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  2547. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  2548. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  2549. TCase *const tcase_handle_form = tcase_create("Handle Form");
  2550. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  2551. TCase *const tcase_keep_alive = tcase_create("HTTP Keep Alive");
  2552. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  2553. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  2554. suite_add_tcase(suite, tcase_checktestenv);
  2555. tcase_add_test(tcase_startthreads, test_threading);
  2556. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  2557. suite_add_tcase(suite, tcase_startthreads);
  2558. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  2559. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  2560. suite_add_tcase(suite, tcase_startstophttp);
  2561. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  2562. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  2563. suite_add_tcase(suite, tcase_startstophttps);
  2564. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  2565. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  2566. suite_add_tcase(suite, tcase_serverandclienttls);
  2567. tcase_add_test(tcase_serverrequests, test_request_handlers);
  2568. tcase_set_timeout(tcase_serverrequests, 120);
  2569. suite_add_tcase(suite, tcase_serverrequests);
  2570. tcase_add_test(tcase_handle_form, test_handle_form);
  2571. tcase_set_timeout(tcase_handle_form, 300);
  2572. suite_add_tcase(suite, tcase_handle_form);
  2573. tcase_add_test(tcase_http_auth, test_http_auth);
  2574. tcase_set_timeout(tcase_http_auth, 60);
  2575. suite_add_tcase(suite, tcase_http_auth);
  2576. tcase_add_test(tcase_keep_alive, test_keep_alive);
  2577. tcase_set_timeout(tcase_keep_alive, 300);
  2578. suite_add_tcase(suite, tcase_keep_alive);
  2579. return suite;
  2580. }
  2581. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  2582. /* Used to debug test cases without using the check framework */
  2583. static int chk_ok = 0;
  2584. static int chk_failed = 0;
  2585. void
  2586. MAIN_PUBLIC_SERVER(void)
  2587. {
  2588. test_the_test_environment(0);
  2589. test_threading(0);
  2590. test_mg_start_stop_http_server(0);
  2591. // test_mg_start_stop_https_server(0);
  2592. test_request_handlers(0);
  2593. // test_mg_server_and_client_tls(0);
  2594. // test_handle_form(0);
  2595. // test_http_auth(0);
  2596. test_keep_alive(0);
  2597. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  2598. }
  2599. void
  2600. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  2601. {
  2602. va_list va;
  2603. va_start(va, expr);
  2604. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2605. vfprintf(stderr, expr, va);
  2606. fprintf(stderr, "\n\n");
  2607. va_end(va);
  2608. chk_failed++;
  2609. }
  2610. void
  2611. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  2612. {
  2613. va_list va;
  2614. if (cond) {
  2615. chk_ok++;
  2616. return;
  2617. }
  2618. va_start(va, expr);
  2619. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  2620. vfprintf(stderr, expr, va);
  2621. fprintf(stderr, "\n\n");
  2622. va_end(va);
  2623. chk_failed++;
  2624. }
  2625. void
  2626. _mark_point(const char *file, int line)
  2627. {
  2628. chk_ok++;
  2629. }
  2630. void
  2631. tcase_fn_start(const char *fname, const char *file, int line)
  2632. {
  2633. }
  2634. void suite_add_tcase(Suite *s, TCase *tc){};
  2635. void _tcase_add_test(TCase *tc,
  2636. TFun tf,
  2637. const char *fname,
  2638. int _signal,
  2639. int allowed_exit_value,
  2640. int start,
  2641. int end){};
  2642. TCase *
  2643. tcase_create(const char *name)
  2644. {
  2645. return NULL;
  2646. };
  2647. Suite *
  2648. suite_create(const char *name)
  2649. {
  2650. return NULL;
  2651. };
  2652. void tcase_set_timeout(TCase *tc, double timeout){};
  2653. #endif