sws.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* sws.c: simple (silly?) web server
  23. This code was originally graciously donated to the project by Juergen
  24. Wilke. Thanks a bunch!
  25. */
  26. #define CURL_NO_OLDIES
  27. #include "setup.h" /* portability help from the lib directory */
  28. #ifdef HAVE_SIGNAL_H
  29. #include <signal.h>
  30. #endif
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34. #ifdef HAVE_SYS_SOCKET_H
  35. #include <sys/socket.h>
  36. #endif
  37. #ifdef HAVE_NETINET_IN_H
  38. #include <netinet/in.h>
  39. #endif
  40. #ifdef HAVE_ARPA_INET_H
  41. #include <arpa/inet.h>
  42. #endif
  43. #ifdef HAVE_NETDB_H
  44. #include <netdb.h>
  45. #endif
  46. #ifdef HAVE_NETINET_TCP_H
  47. #include <netinet/tcp.h> /* for TCP_NODELAY */
  48. #endif
  49. #define ENABLE_CURLX_PRINTF
  50. /* make the curlx header define all printf() functions to use the curlx_*
  51. versions instead */
  52. #include "curlx.h" /* from the private lib dir */
  53. #include "getpart.h"
  54. #include "util.h"
  55. #include "server_sockaddr.h"
  56. /* include memdebug.h last */
  57. #include "memdebug.h"
  58. #if !defined(CURL_SWS_FORK_ENABLED) && defined(HAVE_FORK)
  59. /*
  60. * The normal sws build for the plain standard curl test suite has no use for
  61. * fork(), but if you feel wild and crazy and want to setup some more exotic
  62. * tests. Define this and run...
  63. */
  64. #define CURL_SWS_FORK_ENABLED
  65. #endif
  66. #ifdef ENABLE_IPV6
  67. static bool use_ipv6 = FALSE;
  68. #endif
  69. static bool use_gopher = FALSE;
  70. static const char *ipv_inuse = "IPv4";
  71. static int serverlogslocked = 0;
  72. #define REQBUFSIZ 150000
  73. #define REQBUFSIZ_TXT "149999"
  74. static long prevtestno=-1; /* previous test number we served */
  75. static long prevpartno=-1; /* previous part number we served */
  76. static bool prevbounce=FALSE; /* instructs the server to increase the part
  77. number for a test in case the identical
  78. testno+partno request shows up again */
  79. #define RCMD_NORMALREQ 0 /* default request, use the tests file normally */
  80. #define RCMD_IDLE 1 /* told to sit idle */
  81. #define RCMD_STREAM 2 /* told to stream */
  82. struct httprequest {
  83. char reqbuf[REQBUFSIZ]; /* buffer area for the incoming request */
  84. size_t checkindex; /* where to start checking of the request */
  85. size_t offset; /* size of the incoming request */
  86. long testno; /* test number found in the request */
  87. long partno; /* part number found in the request */
  88. bool open; /* keep connection open info, as found in the request */
  89. bool auth_req; /* authentication required, don't wait for body unless
  90. there's an Authorization header */
  91. bool auth; /* Authorization header present in the incoming request */
  92. size_t cl; /* Content-Length of the incoming request */
  93. bool digest; /* Authorization digest header found */
  94. bool ntlm; /* Authorization ntlm header found */
  95. int writedelay; /* if non-zero, delay this number of seconds between
  96. writes in the response */
  97. int pipe; /* if non-zero, expect this many requests to do a "piped"
  98. request/response */
  99. int skip; /* if non-zero, the server is instructed to not read this
  100. many bytes from a PUT/POST request. Ie the client sends N
  101. bytes said in Content-Length, but the server only reads N
  102. - skip bytes. */
  103. int rcmd; /* doing a special command, see defines above */
  104. int prot_version; /* HTTP version * 10 */
  105. bool pipelining; /* true if request is pipelined */
  106. };
  107. static int ProcessRequest(struct httprequest *req);
  108. static void storerequest(char *reqbuf, size_t totalsize);
  109. #define DEFAULT_PORT 8999
  110. #ifndef DEFAULT_LOGFILE
  111. #define DEFAULT_LOGFILE "log/sws.log"
  112. #endif
  113. const char *serverlogfile = DEFAULT_LOGFILE;
  114. #define SWSVERSION "cURL test suite HTTP server/0.1"
  115. #define REQUEST_DUMP "log/server.input"
  116. #define RESPONSE_DUMP "log/server.response"
  117. /* very-big-path support */
  118. #define MAXDOCNAMELEN 140000
  119. #define MAXDOCNAMELEN_TXT "139999"
  120. #define REQUEST_KEYWORD_SIZE 256
  121. #define REQUEST_KEYWORD_SIZE_TXT "255"
  122. #define CMD_AUTH_REQUIRED "auth_required"
  123. /* 'idle' means that it will accept the request fine but never respond
  124. any data. Just keep the connection alive. */
  125. #define CMD_IDLE "idle"
  126. /* 'stream' means to send a never-ending stream of data */
  127. #define CMD_STREAM "stream"
  128. #define END_OF_HEADERS "\r\n\r\n"
  129. enum {
  130. DOCNUMBER_NOTHING = -7,
  131. DOCNUMBER_QUIT = -6,
  132. DOCNUMBER_BADCONNECT = -5,
  133. DOCNUMBER_INTERNAL= -4,
  134. DOCNUMBER_CONNECT = -3,
  135. DOCNUMBER_WERULEZ = -2,
  136. DOCNUMBER_404 = -1
  137. };
  138. static const char *end_of_headers = END_OF_HEADERS;
  139. /* sent as reply to a QUIT */
  140. static const char *docquit =
  141. "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
  142. /* sent as reply to a CONNECT */
  143. static const char *docconnect =
  144. "HTTP/1.1 200 Mighty fine indeed" END_OF_HEADERS;
  145. /* sent as reply to a "bad" CONNECT */
  146. static const char *docbadconnect =
  147. "HTTP/1.1 501 Forbidden you fool" END_OF_HEADERS;
  148. /* send back this on 404 file not found */
  149. static const char *doc404 = "HTTP/1.1 404 Not Found\r\n"
  150. "Server: " SWSVERSION "\r\n"
  151. "Connection: close\r\n"
  152. "Content-Type: text/html"
  153. END_OF_HEADERS
  154. "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  155. "<HTML><HEAD>\n"
  156. "<TITLE>404 Not Found</TITLE>\n"
  157. "</HEAD><BODY>\n"
  158. "<H1>Not Found</H1>\n"
  159. "The requested URL was not found on this server.\n"
  160. "<P><HR><ADDRESS>" SWSVERSION "</ADDRESS>\n" "</BODY></HTML>\n";
  161. /* do-nothing macro replacement for systems which lack siginterrupt() */
  162. #ifndef HAVE_SIGINTERRUPT
  163. #define siginterrupt(x,y) do {} while(0)
  164. #endif
  165. /* vars used to keep around previous signal handlers */
  166. typedef RETSIGTYPE (*SIGHANDLER_T)(int);
  167. #ifdef SIGHUP
  168. static SIGHANDLER_T old_sighup_handler = SIG_ERR;
  169. #endif
  170. #ifdef SIGPIPE
  171. static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
  172. #endif
  173. #ifdef SIGALRM
  174. static SIGHANDLER_T old_sigalrm_handler = SIG_ERR;
  175. #endif
  176. #ifdef SIGINT
  177. static SIGHANDLER_T old_sigint_handler = SIG_ERR;
  178. #endif
  179. #ifdef SIGTERM
  180. static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
  181. #endif
  182. /* var which if set indicates that the program should finish execution */
  183. SIG_ATOMIC_T got_exit_signal = 0;
  184. /* if next is set indicates the first signal handled in exit_signal_handler */
  185. static volatile int exit_signal = 0;
  186. /* signal handler that will be triggered to indicate that the program
  187. should finish its execution in a controlled manner as soon as possible.
  188. The first time this is called it will set got_exit_signal to one and
  189. store in exit_signal the signal that triggered its execution. */
  190. static RETSIGTYPE exit_signal_handler(int signum)
  191. {
  192. int old_errno = ERRNO;
  193. if(got_exit_signal == 0) {
  194. got_exit_signal = 1;
  195. exit_signal = signum;
  196. }
  197. (void)signal(signum, exit_signal_handler);
  198. SET_ERRNO(old_errno);
  199. }
  200. static void install_signal_handlers(void)
  201. {
  202. #ifdef SIGHUP
  203. /* ignore SIGHUP signal */
  204. if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
  205. logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
  206. #endif
  207. #ifdef SIGPIPE
  208. /* ignore SIGPIPE signal */
  209. if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
  210. logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
  211. #endif
  212. #ifdef SIGALRM
  213. /* ignore SIGALRM signal */
  214. if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
  215. logmsg("cannot install SIGALRM handler: %s", strerror(ERRNO));
  216. #endif
  217. #ifdef SIGINT
  218. /* handle SIGINT signal with our exit_signal_handler */
  219. if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
  220. logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
  221. else
  222. siginterrupt(SIGINT, 1);
  223. #endif
  224. #ifdef SIGTERM
  225. /* handle SIGTERM signal with our exit_signal_handler */
  226. if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
  227. logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
  228. else
  229. siginterrupt(SIGTERM, 1);
  230. #endif
  231. }
  232. static void restore_signal_handlers(void)
  233. {
  234. #ifdef SIGHUP
  235. if(SIG_ERR != old_sighup_handler)
  236. (void)signal(SIGHUP, old_sighup_handler);
  237. #endif
  238. #ifdef SIGPIPE
  239. if(SIG_ERR != old_sigpipe_handler)
  240. (void)signal(SIGPIPE, old_sigpipe_handler);
  241. #endif
  242. #ifdef SIGALRM
  243. if(SIG_ERR != old_sigalrm_handler)
  244. (void)signal(SIGALRM, old_sigalrm_handler);
  245. #endif
  246. #ifdef SIGINT
  247. if(SIG_ERR != old_sigint_handler)
  248. (void)signal(SIGINT, old_sigint_handler);
  249. #endif
  250. #ifdef SIGTERM
  251. if(SIG_ERR != old_sigterm_handler)
  252. (void)signal(SIGTERM, old_sigterm_handler);
  253. #endif
  254. }
  255. static int ProcessRequest(struct httprequest *req)
  256. {
  257. char *line=&req->reqbuf[req->checkindex];
  258. bool chunked = FALSE;
  259. static char request[REQUEST_KEYWORD_SIZE];
  260. static char doc[MAXDOCNAMELEN];
  261. char logbuf[256];
  262. int prot_major, prot_minor;
  263. char *end;
  264. int error;
  265. end = strstr(line, end_of_headers);
  266. logmsg("ProcessRequest() called");
  267. /* try to figure out the request characteristics as soon as possible, but
  268. only once! */
  269. if(use_gopher &&
  270. (req->testno == DOCNUMBER_NOTHING) &&
  271. !strncmp("/verifiedserver", line, 15)) {
  272. logmsg("Are-we-friendly question received");
  273. req->testno = DOCNUMBER_WERULEZ;
  274. return 1; /* done */
  275. }
  276. else if((req->testno == DOCNUMBER_NOTHING) &&
  277. sscanf(line,
  278. "%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
  279. request,
  280. doc,
  281. &prot_major,
  282. &prot_minor) == 4) {
  283. char *ptr;
  284. req->prot_version = prot_major*10 + prot_minor;
  285. /* find the last slash */
  286. ptr = strrchr(doc, '/');
  287. /* get the number after it */
  288. if(ptr) {
  289. FILE *stream;
  290. char *filename;
  291. if((strlen(doc) + strlen(request)) < 200)
  292. sprintf(logbuf, "Got request: %s %s HTTP/%d.%d",
  293. request, doc, prot_major, prot_minor);
  294. else
  295. sprintf(logbuf, "Got a *HUGE* request HTTP/%d.%d",
  296. prot_major, prot_minor);
  297. logmsg("%s", logbuf);
  298. if(!strncmp("/verifiedserver", ptr, 15)) {
  299. logmsg("Are-we-friendly question received");
  300. req->testno = DOCNUMBER_WERULEZ;
  301. return 1; /* done */
  302. }
  303. if(!strncmp("/quit", ptr, 5)) {
  304. logmsg("Request-to-quit received");
  305. req->testno = DOCNUMBER_QUIT;
  306. return 1; /* done */
  307. }
  308. ptr++; /* skip the slash */
  309. /* skip all non-numericals following the slash */
  310. while(*ptr && !ISDIGIT(*ptr))
  311. ptr++;
  312. req->testno = strtol(ptr, &ptr, 10);
  313. if(req->testno > 10000) {
  314. req->partno = req->testno % 10000;
  315. req->testno /= 10000;
  316. }
  317. else
  318. req->partno = 0;
  319. sprintf(logbuf, "Requested test number %ld part %ld",
  320. req->testno, req->partno);
  321. logmsg("%s", logbuf);
  322. filename = test2file(req->testno);
  323. stream=fopen(filename, "rb");
  324. if(!stream) {
  325. error = ERRNO;
  326. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  327. logmsg("Error opening file: %s", filename);
  328. logmsg("Couldn't open test file %ld", req->testno);
  329. req->open = FALSE; /* closes connection */
  330. return 1; /* done */
  331. }
  332. else {
  333. char *cmd = NULL;
  334. size_t cmdsize = 0;
  335. int num=0;
  336. /* get the custom server control "commands" */
  337. error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream);
  338. fclose(stream);
  339. if(error) {
  340. logmsg("getpart() failed with error: %d", error);
  341. req->open = FALSE; /* closes connection */
  342. return 1; /* done */
  343. }
  344. if(cmdsize) {
  345. logmsg("Found a reply-servercmd section!");
  346. if(!strncmp(CMD_AUTH_REQUIRED, cmd, strlen(CMD_AUTH_REQUIRED))) {
  347. logmsg("instructed to require authorization header");
  348. req->auth_req = TRUE;
  349. }
  350. else if(!strncmp(CMD_IDLE, cmd, strlen(CMD_IDLE))) {
  351. logmsg("instructed to idle");
  352. req->rcmd = RCMD_IDLE;
  353. req->open = TRUE;
  354. }
  355. else if(!strncmp(CMD_STREAM, cmd, strlen(CMD_STREAM))) {
  356. logmsg("instructed to stream");
  357. req->rcmd = RCMD_STREAM;
  358. }
  359. else if(1 == sscanf(cmd, "pipe: %d", &num)) {
  360. logmsg("instructed to allow a pipe size of %d", num);
  361. if(num < 0)
  362. logmsg("negative pipe size ignored");
  363. else if(num > 0)
  364. req->pipe = num-1; /* decrease by one since we don't count the
  365. first request in this number */
  366. }
  367. else if(1 == sscanf(cmd, "skip: %d", &num)) {
  368. logmsg("instructed to skip this number of bytes %d", num);
  369. req->skip = num;
  370. }
  371. else if(1 == sscanf(cmd, "writedelay: %d", &num)) {
  372. logmsg("instructed to delay %d secs between packets", num);
  373. req->writedelay = num;
  374. }
  375. else {
  376. logmsg("funny instruction found: %s", cmd);
  377. }
  378. }
  379. if(cmd)
  380. free(cmd);
  381. }
  382. }
  383. else {
  384. if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
  385. doc, &prot_major, &prot_minor) == 3) {
  386. sprintf(logbuf, "Received a CONNECT %s HTTP/%d.%d request",
  387. doc, prot_major, prot_minor);
  388. logmsg("%s", logbuf);
  389. if(req->prot_version == 10)
  390. req->open = FALSE; /* HTTP 1.0 closes connection by default */
  391. if(!strncmp(doc, "bad", 3))
  392. /* if the host name starts with bad, we fake an error here */
  393. req->testno = DOCNUMBER_BADCONNECT;
  394. else if(!strncmp(doc, "test", 4)) {
  395. /* if the host name starts with test, the port number used in the
  396. CONNECT line will be used as test number! */
  397. char *portp = strchr(doc, ':');
  398. if(portp && (*(portp+1) != '\0') && ISDIGIT(*(portp+1)))
  399. req->testno = strtol(portp+1, NULL, 10);
  400. else
  401. req->testno = DOCNUMBER_CONNECT;
  402. }
  403. else
  404. req->testno = DOCNUMBER_CONNECT;
  405. }
  406. else {
  407. logmsg("Did not find test number in PATH");
  408. req->testno = DOCNUMBER_404;
  409. }
  410. }
  411. }
  412. if(!end) {
  413. /* we don't have a complete request yet! */
  414. logmsg("ProcessRequest returned without a complete request");
  415. return 0; /* not complete yet */
  416. }
  417. logmsg("ProcessRequest found a complete request");
  418. if(use_gopher) {
  419. /* when using gopher we cannot check the request until the entire
  420. thing has been received */
  421. char *ptr;
  422. /* find the last slash in the line */
  423. ptr = strrchr(line, '/');
  424. if(ptr) {
  425. ptr++; /* skip the slash */
  426. /* skip all non-numericals following the slash */
  427. while(*ptr && !ISDIGIT(*ptr))
  428. ptr++;
  429. req->testno = strtol(ptr, &ptr, 10);
  430. if(req->testno > 10000) {
  431. req->partno = req->testno % 10000;
  432. req->testno /= 10000;
  433. }
  434. else
  435. req->partno = 0;
  436. sprintf(logbuf, "Requested GOPHER test number %ld part %ld",
  437. req->testno, req->partno);
  438. logmsg("%s", logbuf);
  439. }
  440. }
  441. if(req->pipe)
  442. /* we do have a full set, advance the checkindex to after the end of the
  443. headers, for the pipelining case mostly */
  444. req->checkindex += (end - line) + strlen(end_of_headers);
  445. /* **** Persistence ****
  446. *
  447. * If the request is a HTTP/1.0 one, we close the connection unconditionally
  448. * when we're done.
  449. *
  450. * If the request is a HTTP/1.1 one, we MUST check for a "Connection:"
  451. * header that might say "close". If it does, we close a connection when
  452. * this request is processed. Otherwise, we keep the connection alive for X
  453. * seconds.
  454. */
  455. do {
  456. if(got_exit_signal)
  457. return 1; /* done */
  458. if((req->cl==0) && curlx_strnequal("Content-Length:", line, 15)) {
  459. /* If we don't ignore content-length, we read it and we read the whole
  460. request including the body before we return. If we've been told to
  461. ignore the content-length, we will return as soon as all headers
  462. have been received */
  463. char *endptr;
  464. char *ptr = line + 15;
  465. unsigned long clen = 0;
  466. while(*ptr && ISSPACE(*ptr))
  467. ptr++;
  468. endptr = ptr;
  469. SET_ERRNO(0);
  470. clen = strtoul(ptr, &endptr, 10);
  471. if((ptr == endptr) || !ISSPACE(*endptr) || (ERANGE == ERRNO)) {
  472. /* this assumes that a zero Content-Length is valid */
  473. logmsg("Found invalid Content-Length: (%s) in the request", ptr);
  474. req->open = FALSE; /* closes connection */
  475. return 1; /* done */
  476. }
  477. req->cl = clen - req->skip;
  478. logmsg("Found Content-Length: %lu in the request", clen);
  479. if(req->skip)
  480. logmsg("... but will abort after %zu bytes", req->cl);
  481. break;
  482. }
  483. else if(curlx_strnequal("Transfer-Encoding: chunked", line,
  484. strlen("Transfer-Encoding: chunked"))) {
  485. /* chunked data coming in */
  486. chunked = TRUE;
  487. }
  488. if(chunked) {
  489. if(strstr(req->reqbuf, "\r\n0\r\n\r\n"))
  490. /* end of chunks reached */
  491. return 1; /* done */
  492. else
  493. return 0; /* not done */
  494. }
  495. line = strchr(line, '\n');
  496. if(line)
  497. line++;
  498. } while(line);
  499. if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
  500. req->auth = TRUE; /* Authorization: header present! */
  501. if(req->auth_req)
  502. logmsg("Authorization header found, as required");
  503. }
  504. if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
  505. /* If the client is passing this Digest-header, we set the part number
  506. to 1000. Not only to spice up the complexity of this, but to make
  507. Digest stuff to work in the test suite. */
  508. req->partno += 1000;
  509. req->digest = TRUE; /* header found */
  510. logmsg("Received Digest request, sending back data %ld", req->partno);
  511. }
  512. else if(!req->ntlm &&
  513. strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAD")) {
  514. /* If the client is passing this type-3 NTLM header */
  515. req->partno += 1002;
  516. req->ntlm = TRUE; /* NTLM found */
  517. logmsg("Received NTLM type-3, sending back data %ld", req->partno);
  518. if(req->cl) {
  519. logmsg(" Expecting %zu POSTed bytes", req->cl);
  520. }
  521. }
  522. else if(!req->ntlm &&
  523. strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAB")) {
  524. /* If the client is passing this type-1 NTLM header */
  525. req->partno += 1001;
  526. req->ntlm = TRUE; /* NTLM found */
  527. logmsg("Received NTLM type-1, sending back data %ld", req->partno);
  528. }
  529. else if((req->partno >= 1000) && strstr(req->reqbuf, "Authorization: Basic")) {
  530. /* If the client is passing this Basic-header and the part number is already
  531. >=1000, we add 1 to the part number. This allows simple Basic authentication
  532. negotiation to work in the test suite. */
  533. req->partno += 1;
  534. logmsg("Received Basic request, sending back data %ld", req->partno);
  535. }
  536. if(strstr(req->reqbuf, "Connection: close"))
  537. req->open = FALSE; /* close connection after this request */
  538. if(!req->pipe &&
  539. req->open &&
  540. req->prot_version >= 11 &&
  541. end &&
  542. req->reqbuf + req->offset > end + strlen(end_of_headers) &&
  543. (!strncmp(req->reqbuf, "GET", strlen("GET")) ||
  544. !strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
  545. /* If we have a persistent connection, HTTP version >= 1.1
  546. and GET/HEAD request, enable pipelining. */
  547. req->checkindex = (end - req->reqbuf) + strlen(end_of_headers);
  548. req->pipelining = TRUE;
  549. }
  550. while(req->pipe) {
  551. if(got_exit_signal)
  552. return 1; /* done */
  553. /* scan for more header ends within this chunk */
  554. line = &req->reqbuf[req->checkindex];
  555. end = strstr(line, end_of_headers);
  556. if(!end)
  557. break;
  558. req->checkindex += (end - line) + strlen(end_of_headers);
  559. req->pipe--;
  560. }
  561. /* If authentication is required and no auth was provided, end now. This
  562. makes the server NOT wait for PUT/POST data and you can then make the
  563. test case send a rejection before any such data has been sent. Test case
  564. 154 uses this.*/
  565. if(req->auth_req && !req->auth)
  566. return 1; /* done */
  567. if(req->cl > 0) {
  568. if(req->cl <= req->offset - (end - req->reqbuf) - strlen(end_of_headers))
  569. return 1; /* done */
  570. else
  571. return 0; /* not complete yet */
  572. }
  573. return 1; /* done */
  574. }
  575. /* store the entire request in a file */
  576. static void storerequest(char *reqbuf, size_t totalsize)
  577. {
  578. int res;
  579. int error = 0;
  580. size_t written;
  581. size_t writeleft;
  582. FILE *dump;
  583. if (reqbuf == NULL)
  584. return;
  585. if (totalsize == 0)
  586. return;
  587. do {
  588. dump = fopen(REQUEST_DUMP, "ab");
  589. } while ((dump == NULL) && ((error = ERRNO) == EINTR));
  590. if (dump == NULL) {
  591. logmsg("Error opening file %s error: %d %s",
  592. REQUEST_DUMP, error, strerror(error));
  593. logmsg("Failed to write request input to " REQUEST_DUMP);
  594. return;
  595. }
  596. writeleft = totalsize;
  597. do {
  598. written = fwrite(&reqbuf[totalsize-writeleft],
  599. 1, writeleft, dump);
  600. if(got_exit_signal)
  601. goto storerequest_cleanup;
  602. if(written > 0)
  603. writeleft -= written;
  604. } while ((writeleft > 0) && ((error = ERRNO) == EINTR));
  605. if(writeleft == 0)
  606. logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize);
  607. else if(writeleft > 0) {
  608. logmsg("Error writing file %s error: %d %s",
  609. REQUEST_DUMP, error, strerror(error));
  610. logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s",
  611. totalsize-writeleft, totalsize, REQUEST_DUMP);
  612. }
  613. storerequest_cleanup:
  614. do {
  615. res = fclose(dump);
  616. } while(res && ((error = ERRNO) == EINTR));
  617. if(res)
  618. logmsg("Error closing file %s error: %d %s",
  619. REQUEST_DUMP, error, strerror(error));
  620. }
  621. /* return 0 on success, non-zero on failure */
  622. static int get_request(curl_socket_t sock, struct httprequest *req)
  623. {
  624. int error;
  625. int fail = 0;
  626. int done_processing = 0;
  627. char *reqbuf = req->reqbuf;
  628. ssize_t got = 0;
  629. char *pipereq = NULL;
  630. size_t pipereq_length = 0;
  631. if(req->pipelining) {
  632. pipereq = reqbuf + req->checkindex;
  633. pipereq_length = req->offset - req->checkindex;
  634. }
  635. /*** Init the httprequest structure properly for the upcoming request ***/
  636. req->checkindex = 0;
  637. req->offset = 0;
  638. req->testno = DOCNUMBER_NOTHING;
  639. req->partno = 0;
  640. req->open = TRUE;
  641. req->auth_req = FALSE;
  642. req->auth = FALSE;
  643. req->cl = 0;
  644. req->digest = FALSE;
  645. req->ntlm = FALSE;
  646. req->pipe = 0;
  647. req->skip = 0;
  648. req->writedelay = 0;
  649. req->rcmd = RCMD_NORMALREQ;
  650. req->prot_version = 0;
  651. req->pipelining = FALSE;
  652. /*** end of httprequest init ***/
  653. while(!done_processing && (req->offset < REQBUFSIZ-1)) {
  654. if(pipereq_length && pipereq) {
  655. memmove(reqbuf, pipereq, pipereq_length);
  656. got = curlx_uztosz(pipereq_length);
  657. pipereq_length = 0;
  658. }
  659. else {
  660. if(req->skip)
  661. /* we are instructed to not read the entire thing, so we make sure to only
  662. read what we're supposed to and NOT read the enire thing the client
  663. wants to send! */
  664. got = sread(sock, reqbuf + req->offset, req->cl);
  665. else
  666. got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset);
  667. }
  668. if(got_exit_signal)
  669. return 1;
  670. if(got == 0) {
  671. logmsg("Connection closed by client");
  672. fail = 1;
  673. }
  674. else if(got < 0) {
  675. error = SOCKERRNO;
  676. logmsg("recv() returned error: (%d) %s", error, strerror(error));
  677. fail = 1;
  678. }
  679. if(fail) {
  680. /* dump the request received so far to the external file */
  681. reqbuf[req->offset] = '\0';
  682. storerequest(reqbuf, req->offset);
  683. return 1;
  684. }
  685. logmsg("Read %zd bytes", got);
  686. req->offset += (size_t)got;
  687. reqbuf[req->offset] = '\0';
  688. done_processing = ProcessRequest(req);
  689. if(got_exit_signal)
  690. return 1;
  691. if(done_processing && req->pipe) {
  692. logmsg("Waiting for another piped request");
  693. done_processing = 0;
  694. req->pipe--;
  695. }
  696. }
  697. if((req->offset == REQBUFSIZ-1) && (got > 0)) {
  698. logmsg("Request would overflow buffer, closing connection");
  699. /* dump request received so far to external file anyway */
  700. reqbuf[REQBUFSIZ-1] = '\0';
  701. fail = 1;
  702. }
  703. else if(req->offset > REQBUFSIZ-1) {
  704. logmsg("Request buffer overflow, closing connection");
  705. /* dump request received so far to external file anyway */
  706. reqbuf[REQBUFSIZ-1] = '\0';
  707. fail = 1;
  708. }
  709. else
  710. reqbuf[req->offset] = '\0';
  711. /* dump the request to an external file */
  712. storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset);
  713. if(got_exit_signal)
  714. return 1;
  715. return fail; /* return 0 on success */
  716. }
  717. /* returns -1 on failure */
  718. static int send_doc(curl_socket_t sock, struct httprequest *req)
  719. {
  720. ssize_t written;
  721. size_t count;
  722. const char *buffer;
  723. char *ptr=NULL;
  724. FILE *stream;
  725. char *cmd=NULL;
  726. size_t cmdsize=0;
  727. FILE *dump;
  728. bool persistant = TRUE;
  729. bool sendfailure = FALSE;
  730. size_t responsesize;
  731. int error = 0;
  732. int res;
  733. static char weare[256];
  734. char partbuf[80]="data";
  735. logmsg("Send response number %ld part %ld", req->testno, req->partno);
  736. switch(req->rcmd) {
  737. default:
  738. case RCMD_NORMALREQ:
  739. break; /* continue with business as usual */
  740. case RCMD_STREAM:
  741. #define STREAMTHIS "a string to stream 01234567890\n"
  742. count = strlen(STREAMTHIS);
  743. for (;;) {
  744. written = swrite(sock, STREAMTHIS, count);
  745. if(got_exit_signal)
  746. return -1;
  747. if(written != (ssize_t)count) {
  748. logmsg("Stopped streaming");
  749. break;
  750. }
  751. }
  752. return -1;
  753. case RCMD_IDLE:
  754. /* Do nothing. Sit idle. Pretend it rains. */
  755. return 0;
  756. }
  757. req->open = FALSE;
  758. if(req->testno < 0) {
  759. size_t msglen;
  760. char msgbuf[64];
  761. switch(req->testno) {
  762. case DOCNUMBER_QUIT:
  763. logmsg("Replying to QUIT");
  764. buffer = docquit;
  765. break;
  766. case DOCNUMBER_WERULEZ:
  767. /* we got a "friends?" question, reply back that we sure are */
  768. logmsg("Identifying ourselves as friends");
  769. sprintf(msgbuf, "WE ROOLZ: %ld\r\n", (long)getpid());
  770. msglen = strlen(msgbuf);
  771. if(use_gopher)
  772. sprintf(weare, "%s", msgbuf);
  773. else
  774. sprintf(weare, "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
  775. msglen, msgbuf);
  776. buffer = weare;
  777. break;
  778. case DOCNUMBER_INTERNAL:
  779. logmsg("Bailing out due to internal error");
  780. return -1;
  781. case DOCNUMBER_CONNECT:
  782. logmsg("Replying to CONNECT");
  783. buffer = docconnect;
  784. break;
  785. case DOCNUMBER_BADCONNECT:
  786. logmsg("Replying to a bad CONNECT");
  787. buffer = docbadconnect;
  788. break;
  789. case DOCNUMBER_404:
  790. default:
  791. logmsg("Replying to with a 404");
  792. buffer = doc404;
  793. break;
  794. }
  795. count = strlen(buffer);
  796. }
  797. else {
  798. char *filename = test2file(req->testno);
  799. if(0 != req->partno)
  800. sprintf(partbuf, "data%ld", req->partno);
  801. stream=fopen(filename, "rb");
  802. if(!stream) {
  803. error = ERRNO;
  804. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  805. logmsg("Error opening file: %s", filename);
  806. logmsg("Couldn't open test file");
  807. return 0;
  808. }
  809. else {
  810. error = getpart(&ptr, &count, "reply", partbuf, stream);
  811. fclose(stream);
  812. if(error) {
  813. logmsg("getpart() failed with error: %d", error);
  814. return 0;
  815. }
  816. buffer = ptr;
  817. }
  818. if(got_exit_signal) {
  819. if(ptr)
  820. free(ptr);
  821. return -1;
  822. }
  823. /* re-open the same file again */
  824. stream=fopen(filename, "rb");
  825. if(!stream) {
  826. error = ERRNO;
  827. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  828. logmsg("Error opening file: %s", filename);
  829. logmsg("Couldn't open test file");
  830. if(ptr)
  831. free(ptr);
  832. return 0;
  833. }
  834. else {
  835. /* get the custom server control "commands" */
  836. error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
  837. fclose(stream);
  838. if(error) {
  839. logmsg("getpart() failed with error: %d", error);
  840. if(ptr)
  841. free(ptr);
  842. return 0;
  843. }
  844. }
  845. }
  846. if(got_exit_signal) {
  847. if(ptr)
  848. free(ptr);
  849. if(cmd)
  850. free(cmd);
  851. return -1;
  852. }
  853. /* If the word 'swsclose' is present anywhere in the reply chunk, the
  854. connection will be closed after the data has been sent to the requesting
  855. client... */
  856. if(strstr(buffer, "swsclose") || !count) {
  857. persistant = FALSE;
  858. logmsg("connection close instruction \"swsclose\" found in response");
  859. }
  860. if(strstr(buffer, "swsbounce")) {
  861. prevbounce = TRUE;
  862. logmsg("enable \"swsbounce\" in the next request");
  863. }
  864. else
  865. prevbounce = FALSE;
  866. dump = fopen(RESPONSE_DUMP, "ab");
  867. if(!dump) {
  868. error = ERRNO;
  869. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  870. logmsg("Error opening file: %s", RESPONSE_DUMP);
  871. logmsg("couldn't create logfile: " RESPONSE_DUMP);
  872. if(ptr)
  873. free(ptr);
  874. if(cmd)
  875. free(cmd);
  876. return -1;
  877. }
  878. responsesize = count;
  879. do {
  880. /* Ok, we send no more than 200 bytes at a time, just to make sure that
  881. larger chunks are split up so that the client will need to do multiple
  882. recv() calls to get it and thus we exercise that code better */
  883. size_t num = count;
  884. if(num > 200)
  885. num = 200;
  886. written = swrite(sock, buffer, num);
  887. if (written < 0) {
  888. sendfailure = TRUE;
  889. break;
  890. }
  891. else {
  892. logmsg("Sent off %zd bytes", written);
  893. }
  894. if (req->writedelay) {
  895. logmsg("Pausing %d seconds", req->writedelay);
  896. sleep(req->writedelay);
  897. }
  898. /* write to file as well */
  899. fwrite(buffer, 1, (size_t)written, dump);
  900. if(got_exit_signal)
  901. break;
  902. count -= written;
  903. buffer += written;
  904. } while(count>0);
  905. do {
  906. res = fclose(dump);
  907. } while(res && ((error = ERRNO) == EINTR));
  908. if(res)
  909. logmsg("Error closing file %s error: %d %s",
  910. RESPONSE_DUMP, error, strerror(error));
  911. if(got_exit_signal) {
  912. if(ptr)
  913. free(ptr);
  914. if(cmd)
  915. free(cmd);
  916. return -1;
  917. }
  918. if(sendfailure) {
  919. logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) were sent",
  920. responsesize-count, responsesize);
  921. if(ptr)
  922. free(ptr);
  923. if(cmd)
  924. free(cmd);
  925. return -1;
  926. }
  927. logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP,
  928. responsesize);
  929. if(ptr)
  930. free(ptr);
  931. if(cmdsize > 0 ) {
  932. char command[32];
  933. int quarters;
  934. int num;
  935. ptr=cmd;
  936. do {
  937. if(2 == sscanf(ptr, "%31s %d", command, &num)) {
  938. if(!strcmp("wait", command)) {
  939. logmsg("Told to sleep for %d seconds", num);
  940. quarters = num * 4;
  941. while(quarters > 0) {
  942. quarters--;
  943. res = wait_ms(250);
  944. if(got_exit_signal)
  945. break;
  946. if(res) {
  947. /* should not happen */
  948. error = SOCKERRNO;
  949. logmsg("wait_ms() failed with error: (%d) %s",
  950. error, strerror(error));
  951. break;
  952. }
  953. }
  954. if(!quarters)
  955. logmsg("Continuing after sleeping %d seconds", num);
  956. }
  957. else
  958. logmsg("Unknown command in reply command section");
  959. }
  960. ptr = strchr(ptr, '\n');
  961. if(ptr)
  962. ptr++;
  963. else
  964. ptr = NULL;
  965. } while(ptr && *ptr);
  966. }
  967. if(cmd)
  968. free(cmd);
  969. req->open = use_gopher?FALSE:persistant;
  970. prevtestno = req->testno;
  971. prevpartno = req->partno;
  972. return 0;
  973. }
  974. int main(int argc, char *argv[])
  975. {
  976. srvr_sockaddr_union_t me;
  977. curl_socket_t sock = CURL_SOCKET_BAD;
  978. curl_socket_t msgsock = CURL_SOCKET_BAD;
  979. int wrotepidfile = 0;
  980. int flag;
  981. unsigned short port = DEFAULT_PORT;
  982. char *pidname= (char *)".http.pid";
  983. struct httprequest req;
  984. int rc;
  985. int error;
  986. int arg=1;
  987. long pid;
  988. #ifdef CURL_SWS_FORK_ENABLED
  989. bool use_fork = FALSE;
  990. #endif
  991. while(argc>arg) {
  992. if(!strcmp("--version", argv[arg])) {
  993. printf("sws IPv4%s"
  994. #ifdef CURL_SWS_FORK_ENABLED
  995. " FORK"
  996. #endif
  997. "\n"
  998. ,
  999. #ifdef ENABLE_IPV6
  1000. "/IPv6"
  1001. #else
  1002. ""
  1003. #endif
  1004. );
  1005. return 0;
  1006. }
  1007. else if(!strcmp("--pidfile", argv[arg])) {
  1008. arg++;
  1009. if(argc>arg)
  1010. pidname = argv[arg++];
  1011. }
  1012. else if(!strcmp("--logfile", argv[arg])) {
  1013. arg++;
  1014. if(argc>arg)
  1015. serverlogfile = argv[arg++];
  1016. }
  1017. else if(!strcmp("--gopher", argv[arg])) {
  1018. arg++;
  1019. use_gopher = TRUE;
  1020. end_of_headers = "\r\n"; /* gopher style is much simpler */
  1021. }
  1022. else if(!strcmp("--ipv4", argv[arg])) {
  1023. #ifdef ENABLE_IPV6
  1024. ipv_inuse = "IPv4";
  1025. use_ipv6 = FALSE;
  1026. #endif
  1027. arg++;
  1028. }
  1029. else if(!strcmp("--ipv6", argv[arg])) {
  1030. #ifdef ENABLE_IPV6
  1031. ipv_inuse = "IPv6";
  1032. use_ipv6 = TRUE;
  1033. #endif
  1034. arg++;
  1035. }
  1036. #ifdef CURL_SWS_FORK_ENABLED
  1037. else if(!strcmp("--fork", argv[arg])) {
  1038. use_fork=TRUE;
  1039. arg++;
  1040. }
  1041. #endif
  1042. else if(!strcmp("--port", argv[arg])) {
  1043. arg++;
  1044. if(argc>arg) {
  1045. char *endptr;
  1046. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  1047. if((endptr != argv[arg] + strlen(argv[arg])) ||
  1048. (ulnum < 1025UL) || (ulnum > 65535UL)) {
  1049. fprintf(stderr, "sws: invalid --port argument (%s)\n",
  1050. argv[arg]);
  1051. return 0;
  1052. }
  1053. port = curlx_ultous(ulnum);
  1054. arg++;
  1055. }
  1056. }
  1057. else if(!strcmp("--srcdir", argv[arg])) {
  1058. arg++;
  1059. if(argc>arg) {
  1060. path = argv[arg];
  1061. arg++;
  1062. }
  1063. }
  1064. else {
  1065. puts("Usage: sws [option]\n"
  1066. " --version\n"
  1067. " --logfile [file]\n"
  1068. " --pidfile [file]\n"
  1069. " --ipv4\n"
  1070. " --ipv6\n"
  1071. " --port [port]\n"
  1072. " --srcdir [path]\n"
  1073. " --gopher\n"
  1074. " --fork");
  1075. return 0;
  1076. }
  1077. }
  1078. #ifdef WIN32
  1079. win32_init();
  1080. atexit(win32_cleanup);
  1081. #endif
  1082. install_signal_handlers();
  1083. pid = (long)getpid();
  1084. #ifdef ENABLE_IPV6
  1085. if(!use_ipv6)
  1086. #endif
  1087. sock = socket(AF_INET, SOCK_STREAM, 0);
  1088. #ifdef ENABLE_IPV6
  1089. else
  1090. sock = socket(AF_INET6, SOCK_STREAM, 0);
  1091. #endif
  1092. if(CURL_SOCKET_BAD == sock) {
  1093. error = SOCKERRNO;
  1094. logmsg("Error creating socket: (%d) %s",
  1095. error, strerror(error));
  1096. goto sws_cleanup;
  1097. }
  1098. flag = 1;
  1099. if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  1100. (void *)&flag, sizeof(flag))) {
  1101. error = SOCKERRNO;
  1102. logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
  1103. error, strerror(error));
  1104. goto sws_cleanup;
  1105. }
  1106. #ifdef ENABLE_IPV6
  1107. if(!use_ipv6) {
  1108. #endif
  1109. memset(&me.sa4, 0, sizeof(me.sa4));
  1110. me.sa4.sin_family = AF_INET;
  1111. me.sa4.sin_addr.s_addr = INADDR_ANY;
  1112. me.sa4.sin_port = htons(port);
  1113. rc = bind(sock, &me.sa, sizeof(me.sa4));
  1114. #ifdef ENABLE_IPV6
  1115. }
  1116. else {
  1117. memset(&me.sa6, 0, sizeof(me.sa6));
  1118. me.sa6.sin6_family = AF_INET6;
  1119. me.sa6.sin6_addr = in6addr_any;
  1120. me.sa6.sin6_port = htons(port);
  1121. rc = bind(sock, &me.sa, sizeof(me.sa6));
  1122. }
  1123. #endif /* ENABLE_IPV6 */
  1124. if(0 != rc) {
  1125. error = SOCKERRNO;
  1126. logmsg("Error binding socket on port %hu: (%d) %s",
  1127. port, error, strerror(error));
  1128. goto sws_cleanup;
  1129. }
  1130. logmsg("Running %s %s version on port %d",
  1131. use_gopher?"GOPHER":"HTTP", ipv_inuse, (int)port);
  1132. /* start accepting connections */
  1133. rc = listen(sock, 5);
  1134. if(0 != rc) {
  1135. error = SOCKERRNO;
  1136. logmsg("listen() failed with error: (%d) %s",
  1137. error, strerror(error));
  1138. goto sws_cleanup;
  1139. }
  1140. /*
  1141. ** As soon as this server writes its pid file the test harness will
  1142. ** attempt to connect to this server and initiate its verification.
  1143. */
  1144. wrotepidfile = write_pidfile(pidname);
  1145. if(!wrotepidfile)
  1146. goto sws_cleanup;
  1147. for (;;) {
  1148. msgsock = accept(sock, NULL, NULL);
  1149. if(got_exit_signal)
  1150. break;
  1151. if (CURL_SOCKET_BAD == msgsock) {
  1152. error = SOCKERRNO;
  1153. logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
  1154. error, strerror(error));
  1155. break;
  1156. }
  1157. /*
  1158. ** As soon as this server acepts a connection from the test harness it
  1159. ** must set the server logs advisor read lock to indicate that server
  1160. ** logs should not be read until this lock is removed by this server.
  1161. */
  1162. set_advisor_read_lock(SERVERLOGS_LOCK);
  1163. serverlogslocked = 1;
  1164. #ifdef CURL_SWS_FORK_ENABLED
  1165. if(use_fork) {
  1166. /* The fork enabled version just forks off the child and don't care
  1167. about it anymore, so don't assume otherwise. Beware and don't do
  1168. this at home. */
  1169. rc = fork();
  1170. if(-1 == rc) {
  1171. printf("MAJOR ERROR: fork() failed!\n");
  1172. break;
  1173. }
  1174. }
  1175. else
  1176. /* not a fork, just set rc so the following proceeds nicely */
  1177. rc = 0;
  1178. /* 0 is returned to the child */
  1179. if(0 == rc) {
  1180. #endif
  1181. logmsg("====> Client connect");
  1182. #ifdef TCP_NODELAY
  1183. /*
  1184. * Disable the Nagle algorithm to make it easier to send out a large
  1185. * response in many small segments to torture the clients more.
  1186. */
  1187. flag = 1;
  1188. if (setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
  1189. (void *)&flag, sizeof(flag)) == -1) {
  1190. logmsg("====> TCP_NODELAY failed");
  1191. }
  1192. #endif
  1193. /* initialization of httprequest struct is done in get_request(), but due
  1194. to pipelining treatment the pipelining struct field must be initialized
  1195. previously to FALSE every time a new connection arrives. */
  1196. req.pipelining = FALSE;
  1197. do {
  1198. if(got_exit_signal)
  1199. break;
  1200. if(get_request(msgsock, &req))
  1201. /* non-zero means error, break out of loop */
  1202. break;
  1203. if(prevbounce) {
  1204. /* bounce treatment requested */
  1205. if((req.testno == prevtestno) &&
  1206. (req.partno == prevpartno)) {
  1207. req.partno++;
  1208. logmsg("BOUNCE part number to %ld", req.partno);
  1209. }
  1210. else {
  1211. prevbounce = FALSE;
  1212. prevtestno = -1;
  1213. prevpartno = -1;
  1214. }
  1215. }
  1216. send_doc(msgsock, &req);
  1217. if(got_exit_signal)
  1218. break;
  1219. if((req.testno < 0) && (req.testno != DOCNUMBER_CONNECT)) {
  1220. logmsg("special request received, no persistency");
  1221. break;
  1222. }
  1223. if(!req.open) {
  1224. logmsg("instructed to close connection after server-reply");
  1225. break;
  1226. }
  1227. if(req.open)
  1228. logmsg("=> persistant connection request ended, awaits new request");
  1229. /* if we got a CONNECT, loop and get another request as well! */
  1230. } while(req.open || (req.testno == DOCNUMBER_CONNECT));
  1231. if(got_exit_signal)
  1232. break;
  1233. logmsg("====> Client disconnect");
  1234. sclose(msgsock);
  1235. msgsock = CURL_SOCKET_BAD;
  1236. if(serverlogslocked) {
  1237. serverlogslocked = 0;
  1238. clear_advisor_read_lock(SERVERLOGS_LOCK);
  1239. }
  1240. if (req.testno == DOCNUMBER_QUIT)
  1241. break;
  1242. #ifdef CURL_SWS_FORK_ENABLED
  1243. }
  1244. #endif
  1245. }
  1246. sws_cleanup:
  1247. if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
  1248. sclose(msgsock);
  1249. if(sock != CURL_SOCKET_BAD)
  1250. sclose(sock);
  1251. if(got_exit_signal)
  1252. logmsg("signalled to die");
  1253. if(wrotepidfile)
  1254. unlink(pidname);
  1255. if(serverlogslocked) {
  1256. serverlogslocked = 0;
  1257. clear_advisor_read_lock(SERVERLOGS_LOCK);
  1258. }
  1259. restore_signal_handlers();
  1260. if(got_exit_signal) {
  1261. logmsg("========> %s sws (port: %d pid: %ld) exits with signal (%d)",
  1262. ipv_inuse, (int)port, pid, exit_signal);
  1263. /*
  1264. * To properly set the return status of the process we
  1265. * must raise the same signal SIGINT or SIGTERM that we
  1266. * caught and let the old handler take care of it.
  1267. */
  1268. raise(exit_signal);
  1269. }
  1270. logmsg("========> sws quits");
  1271. return 0;
  1272. }