Artifactspublic-v0

curl

master
ply4 git calls28 ms306 KiB3 cachedprotocol v2
2222 lines67 KiBRaw
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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 https://curl.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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "first.h"
26/* sws.c: simple (silly?) web server
28 This code was originally graciously donated to the project by Juergen
29 Wilke. Thanks a bunch!
31 */
33#ifdef HAVE_NETINET_TCP_H
34#include <netinet/tcp.h> /* for TCP_NODELAY */
35#endif
37static bool use_gopher = FALSE;
38static bool is_proxy = FALSE;
40#define MAX_SLEEP_TIME_MS 250
42static long sws_prevtestno = -1; /* previous test number we served */
43static long sws_prevpartno = -1; /* previous part number we served */
44static bool sws_prevbounce = FALSE; /* instructs the server to override the
45 requested part number to
46 prevpartno + 1 when prevtestno and
47 current test are the same */
49#define RCMD_NORMALREQ 0 /* default request, use the tests file normally */
50#define RCMD_IDLE 1 /* told to sit idle */
51#define RCMD_STREAM 2 /* told to stream */
53struct sws_httprequest {
54 char reqbuf[2 * 1024 * 1024]; /* buffer area for the incoming request */
55 bool connect_request; /* if a CONNECT */
56 uint16_t connect_port; /* the port number CONNECT used */
57 size_t checkindex; /* where to start checking of the request */
58 size_t offset; /* size of the incoming request */
59 long testno; /* test number found in the request */
60 long partno; /* part number found in the request */
61 bool open; /* keep connection open info, as found in the request */
62 bool auth_req; /* authentication required, do not wait for body unless
63 there is an Authorization header */
64 bool auth; /* Authorization header present in the incoming request */
65 size_t cl; /* Content-Length of the incoming request */
66 bool digest; /* Authorization digest header found */
67 int delay; /* if non-zero, delay this number of msec after connect */
68 int writedelay; /* if non-zero, delay this number of milliseconds between
69 writes in the response */
70 int skip; /* if non-zero, the server is instructed to not read this
71 many bytes from a PUT/POST request. Ie the client sends N
72 bytes said in Content-Length, but the server only reads N
73 - skip bytes. */
74 int rcmd; /* doing a special command, see defines above */
75 int prot_version; /* HTTP version * 10 */
76 int callcount; /* times sws_ProcessRequest() gets called */
77 bool skipall; /* skip all incoming data */
78 bool noexpect; /* refuse Expect: (do not read the body) */
79 bool connmon; /* monitor the state of the connection, log disconnects */
80 bool upgrade; /* test case allows upgrade */
81 bool upgrade_request; /* upgrade request found and allowed */
82 bool close; /* similar to swsclose in response: close connection after
83 response is sent */
84 int done_processing;
85};
87#define MAX_SOCKETS 1024
89static curl_socket_t all_sockets[MAX_SOCKETS];
90static size_t num_sockets = 0;
92#define SWSVERSION "curl test suite HTTP server/0.1"
94#define REQUEST_DUMP "server.input"
95#define RESPONSE_DUMP "server.response"
97/* when told to run as proxy, we store the logs in different files so that
98 they can co-exist with the same program running as a "server" */
99#define REQUEST_PROXY_DUMP "proxy.input"
100#define RESPONSE_PROXY_DUMP "proxy.response"
102#define REQUEST_DUMP_FILENAME \
103 (is_proxy ? REQUEST_PROXY_DUMP : REQUEST_DUMP)
105/* file in which additional instructions may be found */
106const char *cmdfile = "log/server.cmd";
108/* very-big-path support */
109#define MAXDOCNAMELEN 140000
110#define MAXDOCNAMELEN_TXT "139999"
112#define REQUEST_KEYWORD_SIZE 256
113#define REQUEST_KEYWORD_SIZE_TXT "255"
115#define CMD_AUTH_REQUIRED "auth_required"
117/* 'idle' means that it accepts the request fine but never responds
118 any data. Keep the connection alive. */
119#define CMD_IDLE "idle"
121/* 'stream' means to send a never-ending stream of data */
122#define CMD_STREAM "stream"
124/* 'connection-monitor' outputs when a server/proxy connection gets
125 disconnected as for some cases it is important that it gets done at the
126 proper point */
127#define CMD_CONNECTIONMONITOR "connection-monitor"
129/* upgrade to http2/websocket/xxxx */
130#define CMD_UPGRADE "upgrade"
132/* close connection */
133#define CMD_SWSCLOSE "swsclose"
135/* deny Expect: requests */
136#define CMD_NOEXPECT "no-expect"
138#define END_OF_HEADERS "\r\n\r\n"
140static const char *end_of_headers = END_OF_HEADERS;
142/* sent as reply to a QUIT */
143static const char docquit_sws[] = "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
145/* send back this on 404 file not found */
146static const char doc404[] =
147 "HTTP/1.1 404 Not Found\r\n"
148 "Server: " SWSVERSION "\r\n"
149 "Connection: close\r\n"
150 "Content-Type: text/html"
151 END_OF_HEADERS
152 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
153 "<HTML><HEAD>\n"
154 "<TITLE>404 Not Found</TITLE>\n"
155 "</HEAD><BODY>\n"
156 "<H1>Not Found</H1>\n"
157 "The requested URL was not found on this server.\n"
158 "<P><HR><ADDRESS>" SWSVERSION "</ADDRESS>\n"
159 "</BODY></HTML>\n";
161/* This function returns a pointer to STATIC memory. It converts the given
162 * binary lump to a hex formatted string usable for output in logs or
163 * whatever.
164 */
165static char *data_to_hex(const char *data, size_t len)
167 static char buf[256 * 3];
168 size_t i;
169 char *optr = buf;
170 const char *iptr = data;
172 if(len > 255)
173 len = 255;
175 for(i = 0; i < len; i++) {
176 if((data[i] >= 0x20) && (data[i] < 0x7f))
177 *optr++ = *iptr++;
178 else {
179 snprintf(optr, 4, "%%%02x", (unsigned char)*iptr++);
180 optr += 3;
181 }
182 }
183 *optr = 0; /* in case no sprintf was used */
185 return buf;
188/* work around for handling trailing headers */
189static int already_recv_zeroed_chunk = FALSE;
191/* parse the file on disk that might have a test number for us */
192static int parse_cmdfile(struct sws_httprequest *req)
194 FILE *f = curlx_fopen(cmdfile, FOPEN_READTEXT);
195 if(f) {
196 int testnum = DOCNUMBER_NOTHING;
197 char buf[256];
198 while(fgets(buf, sizeof(buf), f)) {
199 if(sscanf(buf, "Testnum %d", &testnum) == 1) {
200 logmsg("[%s] cmdfile says testnum %d", cmdfile, testnum);
201 req->testno = testnum;
202 }
203 }
204 curlx_fclose(f);
205 }
206 return 0;
209/* based on the testno, parse the correct server commands */
210static int sws_parse_servercmd(struct sws_httprequest *req)
212 FILE *stream;
213 int error;
215 stream = test2fopen(req->testno, logdir);
216 req->close = FALSE;
217 req->connmon = FALSE;
219 if(!stream) {
220 char errbuf[STRERROR_LEN];
221 error = errno;
222 logmsg("fopen() failed with error (%d) %s",
223 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
224 logmsg(" Could not open test file %ld", req->testno);
225 req->open = FALSE; /* closes connection */
226 return 1; /* done */
227 }
228 else {
229 char *orgcmd = NULL;
230 const char *cmd = NULL;
231 size_t cmdsize = 0;
232 int num = 0;
234 /* get the custom server control "commands" */
235 error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
236 curlx_fclose(stream);
237 if(error) {
238 logmsg("getpart() failed with error (%d)", error);
239 req->open = FALSE; /* closes connection */
240 return 1; /* done */
241 }
243 cmd = orgcmd;
244 while(cmd && cmdsize) {
245 const char *check;
247 if(!strncmp(CMD_AUTH_REQUIRED, cmd, CURL_CSTRLEN(CMD_AUTH_REQUIRED))) {
248 logmsg("instructed to require authorization header");
249 req->auth_req = TRUE;
250 }
251 else if(!strncmp(CMD_IDLE, cmd, CURL_CSTRLEN(CMD_IDLE))) {
252 logmsg("instructed to idle");
253 req->rcmd = RCMD_IDLE;
254 req->open = TRUE;
255 }
256 else if(!strncmp(CMD_STREAM, cmd, CURL_CSTRLEN(CMD_STREAM))) {
257 logmsg("instructed to stream");
258 req->rcmd = RCMD_STREAM;
259 }
260 else if(!strncmp(CMD_CONNECTIONMONITOR, cmd,
261 CURL_CSTRLEN(CMD_CONNECTIONMONITOR))) {
262 logmsg("enabled connection monitoring");
263 req->connmon = TRUE;
264 }
265 else if(!strncmp(CMD_UPGRADE, cmd, CURL_CSTRLEN(CMD_UPGRADE))) {
266 logmsg("enabled upgrade");
267 req->upgrade = TRUE;
268 }
269 else if(!strncmp(CMD_SWSCLOSE, cmd, CURL_CSTRLEN(CMD_SWSCLOSE))) {
270 logmsg("swsclose: close this connection after response");
271 req->close = TRUE;
272 }
273 else if(sscanf(cmd, "skip: %d", &num) == 1) {
274 logmsg("instructed to skip this number of bytes %d", num);
275 req->skip = num;
276 }
277 else if(!strncmp(CMD_NOEXPECT, cmd, CURL_CSTRLEN(CMD_NOEXPECT))) {
278 logmsg("instructed to reject Expect: 100-continue");
279 req->noexpect = TRUE;
280 }
281 else if(sscanf(cmd, "delay: %d", &num) == 1) {
282 logmsg("instructed to delay %d msecs after connect", num);
283 req->delay = num;
284 }
285 else if(sscanf(cmd, "writedelay: %d", &num) == 1) {
286 logmsg("instructed to delay %d msecs between packets", num);
287 req->writedelay = num;
288 }
289 else {
290 logmsg("Unknown <servercmd> instruction found: %s", cmd);
291 }
292 /* try to deal with CRLF or LF */
293 check = strchr(cmd, '\r');
294 if(!check)
295 check = strchr(cmd, '\n');
297 if(check) {
298 /* get to the letter following the newline */
299 while((*check == '\r') || (*check == '\n'))
300 check++;
302 if(!*check)
303 /* if we reached a zero, get out */
304 break;
305 cmd = check;
306 }
307 else
308 break;
309 }
310 free(orgcmd);
311 }
313 return 0; /* OK! */
316static int sws_ProcessRequest(struct sws_httprequest *req)
318 const char *line = &req->reqbuf[req->checkindex];
319 bool chunked = FALSE;
320 static char request[REQUEST_KEYWORD_SIZE];
321 int prot_major = 0;
322 int prot_minor = 0;
323 const char *end = strstr(line, end_of_headers);
324 const char *pval;
325 curl_off_t num;
327 req->callcount++;
329 logmsg("Process %lu bytes request%s", (unsigned long)req->offset,
330 req->callcount > 1 ? " [CONTINUED]" : "");
332 /* try to figure out the request characteristics as soon as possible, but
333 only once! */
335 if(use_gopher &&
336 (req->testno == DOCNUMBER_NOTHING) &&
337 !strncmp("/verifiedserver", line, 15)) {
338 logmsg("Are-we-friendly question received");
339 req->testno = DOCNUMBER_WERULEZ;
340 return 1; /* done */
341 }
343 else if(req->testno == DOCNUMBER_NOTHING) {
344 const char *http;
345 bool fine = FALSE;
346 const char *httppath = NULL;
347 size_t npath = 0; /* httppath length */
349 if(sscanf(line, "%" REQUEST_KEYWORD_SIZE_TXT "s ", request) == 1) {
350 http = strstr(line + strlen(request), "HTTP/");
352 if(http && sscanf(http, "HTTP/%d.%d", &prot_major, &prot_minor) == 2) {
353 /* between the request keyword and HTTP/ there is a path */
354 httppath = line + strlen(request);
355 npath = http - httppath;
357 /* trim leading spaces */
358 while(npath && ISSPACE(*httppath)) {
359 httppath++;
360 npath--;
361 }
362 /* trim ending spaces */
363 while(npath && ISSPACE(httppath[npath - 1])) {
364 npath--;
365 }
366 if(npath)
367 fine = TRUE;
368 }
369 }
371 if(fine) {
372 const char *ptr;
374 req->prot_version = (prot_major * 10) + prot_minor;
376 /* find the last slash */
377 ptr = &httppath[npath];
378 while(ptr >= httppath) {
379 if(*ptr == '/')
380 break;
381 ptr--;
382 }
384 /* get the number after it */
385 if(*ptr == '/') {
386 if((npath + strlen(request)) < 400)
387 logmsg("Got request: %s %.*s HTTP/%d.%d",
388 request, (int)npath, httppath, prot_major, prot_minor);
389 else
390 logmsg("Got a *HUGE* request HTTP/%d.%d", prot_major, prot_minor);
392 if(!strncmp("/verifiedserver", ptr, 15)) {
393 logmsg("Are-we-friendly question received");
394 req->testno = DOCNUMBER_WERULEZ;
395 return 1; /* done */
396 }
398 if(!strncmp("/quit", ptr, 5)) {
399 logmsg("Request-to-quit received");
400 req->testno = DOCNUMBER_QUIT;
401 return 1; /* done */
402 }
404 ptr++; /* skip the slash */
406 pval = ptr;
407 if(!curlx_str_number(&pval, &num, INT_MAX))
408 req->testno = (long)num;
410 if(req->testno > 10000) {
411 req->partno = req->testno % 10000;
412 req->testno /= 10000;
413 }
414 else
415 req->partno = 0;
417 if(req->testno) {
418 logmsg("Serve test number %ld part %ld", req->testno, req->partno);
419 }
420 else {
421 logmsg("No test number in path");
422 req->testno = DOCNUMBER_NOTHING;
423 }
424 }
426 if(req->testno == DOCNUMBER_NOTHING) {
427 /* did not find any in the first scan, try alternative test case
428 number placements */
429 static char doc[MAXDOCNAMELEN];
430 if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
431 doc, &prot_major, &prot_minor) == 3) {
432 const char *portp = NULL;
434 logmsg("Received a CONNECT %s HTTP/%d.%d request",
435 doc, prot_major, prot_minor);
437 req->connect_request = TRUE;
439 if(req->prot_version == 10)
440 req->open = FALSE; /* HTTP 1.0 closes connection by default */
442 if(doc[0] == '[') {
443 const char *p = &doc[1];
444 curl_off_t part = 0;
445 /* scan through the hexgroups and store the value of the last group
446 in the 'part' variable and use as test case number!! */
447 while(*p && (ISXDIGIT(*p) || (*p == ':') || (*p == '.'))) {
448 const char *endp = p;
449 if(!curlx_str_hex(&endp, &part, 0xffff))
450 p = endp;
451 else
452 p++;
453 }
454 if(*p != ']')
455 logmsg("Invalid CONNECT IPv6 address format");
456 else if(*(p + 1) != ':')
457 logmsg("Invalid CONNECT IPv6 port format");
458 else
459 portp = p + 1;
461 req->testno = (long)part;
462 }
463 else
464 portp = strchr(doc, ':');
466 if(portp && (*(portp + 1) != '\0') && ISDIGIT(*(portp + 1))) {
467 pval = portp + 1;
468 if(curlx_str_number(&pval, &num, 0xffff) ||
469 (num <= 0) || (num > 65535))
470 logmsg("Invalid CONNECT port received");
471 else
472 req->connect_port = (uint16_t)num;
473 }
474 logmsg("Port number: %hu, test case number: %ld",
475 req->connect_port, req->testno);
476 }
477 }
479 if(req->testno == DOCNUMBER_NOTHING)
480 /* might get the test number */
481 parse_cmdfile(req);
483 if(req->testno == DOCNUMBER_NOTHING) {
484 logmsg("Did not find test number in PATH");
485 req->testno = DOCNUMBER_404;
486 }
487 else
488 sws_parse_servercmd(req);
489 }
490 else if((req->offset >= 3)) {
491 const unsigned char *l = (const unsigned char *)line;
492 logmsg("** Unusual request. Starts with %02x %02x %02x (%c%c%c)",
493 l[0], l[1], l[2], l[0], l[1], l[2]);
494 }
495 }
497 if(!end) {
498 /* we do not have a complete request yet! */
499 logmsg("request not complete yet");
500 return 0; /* not complete yet */
501 }
502 logmsg("- request found to be complete (%ld)", req->testno);
504 if(req->testno == DOCNUMBER_NOTHING) {
505 /* check for a Testno: header with the test case number */
506 const char *testno = strstr(line, "\nTestno: ");
507 if(testno) {
508 pval = &testno[9];
509 if(!curlx_str_number(&pval, &num, INT_MAX)) {
510 req->testno = (long)num;
511 logmsg("Found test number %ld in Testno: header!", req->testno);
512 }
513 else
514 logmsg("No Testno: number");
515 }
516 else {
517 logmsg("No Testno: header");
518 }
519 }
521 /* find and parse <servercmd> for this test */
522 sws_parse_servercmd(req);
524 if(use_gopher) {
525 /* when using gopher we cannot check the request until the entire
526 thing has been received */
527 const char *ptr;
529 /* find the last slash in the line */
530 ptr = strrchr(line, '/');
532 if(ptr) {
533 ptr++; /* skip the slash */
535 /* skip all non-numericals following the slash */
536 while(*ptr && !ISDIGIT(*ptr))
537 ptr++;
539 pval = ptr;
540 if(!curlx_str_number(&pval, &num, INT_MAX))
541 req->testno = (long)num;
543 if(req->testno > 10000) {
544 req->partno = req->testno % 10000;
545 req->testno /= 10000;
546 }
547 else
548 req->partno = 0;
550 logmsg("Requested GOPHER test number %ld part %ld",
551 req->testno, req->partno);
552 }
553 }
555 /* **** Persistence ****
556 *
557 * If the request is an HTTP/1.0 one, we close the connection unconditionally
558 * when we are done.
559 *
560 * If the request is an HTTP/1.1 one, we MUST check for a "Connection:"
561 * header that might say "close". If it does, we close a connection when
562 * this request is processed. Otherwise, we keep the connection alive for X
563 * seconds.
564 */
566 do {
567 if(got_exit_signal)
568 return 1; /* done */
570 if((req->cl == 0) && !CURL_STRNICMP("Content-Length:", line, 15)) {
571 /* If we do not ignore content-length, we read it and we read the whole
572 request including the body before we return. If we have been told to
573 ignore the content-length, we return as soon as all headers
574 have been received */
575 curl_off_t clen;
576 const char *p = line + CURL_CSTRLEN("Content-Length:");
577 if(curlx_str_numblanks(&p, &clen)) {
578 /* this assumes that a zero Content-Length is valid */
579 logmsg("Found invalid '%s' in the request", line);
580 req->open = FALSE; /* closes connection */
581 return 1; /* done */
582 }
583 if(req->skipall)
584 req->cl = 0;
585 else
586 req->cl = (size_t)clen - req->skip;
588 logmsg("Found Content-Length: %lu in the request", (unsigned long)clen);
589 if(req->skip)
590 logmsg("... but going to abort after %lu bytes",
591 (unsigned long)req->cl);
592 }
593 else if(!CURL_STRNICMP("Transfer-Encoding: chunked", line,
594 CURL_CSTRLEN("Transfer-Encoding: chunked"))) {
595 /* chunked data coming in */
596 chunked = TRUE;
597 }
598 else if(req->noexpect &&
599 !CURL_STRNICMP("Expect: 100-continue", line,
600 CURL_CSTRLEN("Expect: 100-continue"))) {
601 if(req->cl)
602 req->cl = 0;
603 req->skipall = TRUE;
604 logmsg("Found Expect: 100-continue, ignore body");
605 }
607 if(chunked) {
608 if(strstr(req->reqbuf, "\r\n0\r\n\r\n")) {
609 /* end of chunks reached */
610 return 1; /* done */
611 }
612 else if(strstr(req->reqbuf, "\r\n0\r\n")) {
613 const char *last_crlf_char = strstr(req->reqbuf, "\r\n\r\n");
614 while(TRUE) {
615 if(!strstr(last_crlf_char + 4, "\r\n\r\n"))
616 break;
617 last_crlf_char = strstr(last_crlf_char + 4, "\r\n\r\n");
618 }
619 if(last_crlf_char &&
620 last_crlf_char > strstr(req->reqbuf, "\r\n0\r\n"))
621 return 1;
622 already_recv_zeroed_chunk = TRUE;
623 return 0;
624 }
625 else if(already_recv_zeroed_chunk && strstr(req->reqbuf, "\r\n\r\n"))
626 return 1;
627 else
628 return 0; /* not done */
629 }
631 line = strchr(line, '\n');
632 if(line)
633 line++;
635 } while(line);
637 if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
638 req->auth = TRUE; /* Authorization: header present! */
639 if(req->auth_req)
640 logmsg("Authorization header found, as required");
641 }
643 if(strstr(req->reqbuf, "Authorization: Negotiate")) {
644 /* Negotiate iterations */
645 static long prev_testno = -1;
646 static long prev_partno = -1;
647 logmsg("Negotiate: prev_testno: %ld, prev_partno: %ld",
648 prev_testno, prev_partno);
649 if(req->testno != prev_testno) {
650 prev_testno = req->testno;
651 prev_partno = req->partno;
652 }
653 prev_partno += 1;
654 req->partno = prev_partno;
655 }
656 else if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
657 /* If the client is passing this Digest-header, we set the part number
658 to 1000. Not only to spice up the complexity of this, but to make
659 Digest stuff to work in the test suite. */
660 req->partno += 1000;
661 req->digest = TRUE; /* header found */
662 logmsg("Received Digest request, sending back data %ld", req->partno);
663 }
664 else if((req->partno >= 1000) &&
665 strstr(req->reqbuf, "Authorization: Basic")) {
666 /* If the client is passing this Basic-header and the part number is
667 already >=1000, we add 1 to the part number. This allows simple Basic
668 authentication negotiation to work in the test suite. */
669 req->partno += 1;
670 logmsg("Received Basic request, sending back data %ld", req->partno);
671 }
672 if(strstr(req->reqbuf, "Connection: close"))
673 req->open = FALSE; /* close connection after this request */
675 if(req->open &&
676 req->prot_version >= 11 &&
677 req->reqbuf + req->offset > end + strlen(end_of_headers) &&
678 !req->cl &&
679 (!strncmp(req->reqbuf, "GET", CURL_CSTRLEN("GET")) ||
680 !strncmp(req->reqbuf, "HEAD", CURL_CSTRLEN("HEAD")))) {
681 /* If we have a persistent connection, HTTP version >= 1.1
682 and GET/HEAD request, enable pipelining. */
683 req->checkindex = (end - req->reqbuf) + strlen(end_of_headers);
684 }
686 /* If authentication is required and no auth was provided, end now. This
687 makes the server NOT wait for PUT/POST data and you can then make the
688 test case send a rejection before any such data has been sent. Test case
689 154 uses this.*/
690 if(req->auth_req && !req->auth) {
691 logmsg("Return early due to auth requested but none provided");
692 return 1; /* done */
693 }
695 if(req->upgrade && strstr(req->reqbuf, "Upgrade:")) {
696 /* we allow upgrade and there was one! */
697 logmsg("Found Upgrade: in request and allow it");
698 req->upgrade_request = TRUE;
699 return 0; /* not done */
700 }
702 if(req->cl > 0) {
703 if(req->cl <= req->offset - (end - req->reqbuf) - strlen(end_of_headers))
704 return 1; /* done */
705 else
706 return 0; /* not complete yet */
707 }
709 return 1; /* done */
712/* returns -1 on failure */
713static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req)
715 ssize_t written;
716 size_t count;
717 const char *buffer;
718 char *ptr = NULL;
719 FILE *stream;
720 char *cmd = NULL;
721 size_t cmdsize = 0;
722 FILE *dump;
723 bool persistent = TRUE;
724 bool sendfailure = FALSE;
725 size_t responsesize;
726 int error = 0;
727 char errbuf[STRERROR_LEN];
728 static char weare[256];
729 char responsedump[256];
731 snprintf(responsedump, sizeof(responsedump), "%s/%s",
732 logdir, is_proxy ? RESPONSE_PROXY_DUMP : RESPONSE_DUMP);
734 switch(req->rcmd) {
735 default:
736 case RCMD_NORMALREQ:
737 break; /* continue with business as usual */
738 case RCMD_STREAM: {
739 static const char streamthis[] = "a string to stream 01234567890\n";
740 for(;;) {
741 written = swrite(sock, streamthis, CURL_CSTRLEN(streamthis));
742 if(got_exit_signal)
743 return -1;
744 if(written != (ssize_t)CURL_CSTRLEN(streamthis)) {
745 logmsg("Stopped streaming");
746 break;
747 }
748 }
749 return -1;
750 }
751 case RCMD_IDLE:
752 /* Do nothing. Sit idle. Pretend it rains. */
753 return 0;
754 }
756 req->open = FALSE;
758 if(req->testno < 0) {
759 size_t msglen;
760 char msgbuf[64];
762 switch(req->testno) {
763 case DOCNUMBER_QUIT:
764 logmsg("Replying to QUIT");
765 buffer = docquit_sws;
766 break;
767 case DOCNUMBER_WERULEZ:
768 /* we got a "friends?" question, reply back that we sure are */
769 logmsg("Identifying ourselves as friends");
770 snprintf(msgbuf, sizeof(msgbuf), "WE ROOLZ: %ld\r\n",
771 (long)our_getpid());
772 msglen = strlen(msgbuf);
773 if(use_gopher)
774 snprintf(weare, sizeof(weare), "%s", msgbuf);
775 else
776 snprintf(weare, sizeof(weare),
777 "HTTP/1.1 200 OK\r\nContent-Length: %u\r\n\r\n%s",
778 (unsigned int)msglen, msgbuf);
779 buffer = weare;
780 break;
781 case DOCNUMBER_404:
782 default:
783 logmsg("Replying with a 404");
784 buffer = doc404;
785 break;
786 }
788 count = strlen(buffer);
789 }
790 else {
791 char partbuf[80];
793 /* select the <data> tag for "normal" requests and the <connect> one
794 for CONNECT requests (within the <reply> section) */
795 const char *section = req->connect_request ? "connect" : "data";
797 if(req->partno)
798 snprintf(partbuf, sizeof(partbuf), "%s%ld", section, req->partno);
799 else
800 snprintf(partbuf, sizeof(partbuf), "%s", section);
802 logmsg("Send response test%ld section <%s>", req->testno, partbuf);
804 stream = test2fopen(req->testno, logdir);
805 if(!stream) {
806 error = errno;
807 logmsg("fopen() failed with error (%d) %s",
808 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
809 return 0;
810 }
811 else {
812 error = getpart(&ptr, &count, "reply", partbuf, stream);
813 curlx_fclose(stream);
814 if(error) {
815 logmsg("getpart() failed with error (%d)", error);
816 return 0;
817 }
818 buffer = ptr;
819 }
821 if(got_exit_signal) {
822 free(ptr);
823 return -1;
824 }
826 /* re-open the same file again */
827 stream = test2fopen(req->testno, logdir);
828 if(!stream) {
829 error = errno;
830 logmsg("fopen() failed with error (%d) %s",
831 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
832 free(ptr);
833 return 0;
834 }
835 else {
836 /* get the custom server control "commands" */
837 error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
838 curlx_fclose(stream);
839 if(error) {
840 logmsg("getpart() failed with error (%d)", error);
841 free(ptr);
842 return 0;
843 }
844 }
845 }
847 if(got_exit_signal) {
848 free(ptr);
849 free(cmd);
850 return -1;
851 }
853 /* If the word 'swsclose' is present anywhere in the reply chunk, the
854 connection is closed after the data has been sent to the requesting
855 client... */
856 if(strstr(buffer, "swsclose")) {
857 persistent = FALSE;
858 logmsg("connection close instruction \"swsclose\" found in response");
859 }
860 else if(!count) {
861 persistent = FALSE;
862 logmsg("connection closed because of empty response");
863 }
864 else if(req->close) {
865 persistent = FALSE;
866 logmsg("connection closed because of close instruction in servercmd");
867 }
868 if(strstr(buffer, "swsbounce")) {
869 sws_prevbounce = TRUE;
870 logmsg("enable \"swsbounce\" in the next request");
871 }
872 else
873 sws_prevbounce = FALSE;
875 dump = curlx_fopen(responsedump, "ab");
876 if(!dump) {
877 error = errno;
878 logmsg("fopen() failed with error (%d) %s",
879 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
880 logmsg(" [5] Error opening file '%s'", responsedump);
881 free(ptr);
882 free(cmd);
883 return -1;
884 }
886 responsesize = count;
887 do {
888 /* Ok, we send no more than N bytes at a time, to make sure that
889 larger chunks are split up so that the client needs to do multiple
890 recv() calls to get it and thus we exercise that code better */
891 size_t num = count;
892 if(num > 20)
893 num = 20;
895retry:
896 written = swrite(sock, buffer, num);
897 if(written < 0) {
898 if(SOCK_EAGAIN(SOCKERRNO)) {
899 curlx_wait_ms(10);
900 goto retry;
901 }
902 sendfailure = TRUE;
903 break;
904 }
906 /* write to file as well */
907 fwrite(buffer, 1, (size_t)written, dump);
909 count -= written;
910 buffer += written;
912 if(req->writedelay) {
913 int msecs_left = req->writedelay;
914 int intervals = msecs_left / MAX_SLEEP_TIME_MS;
915 if(msecs_left % MAX_SLEEP_TIME_MS)
916 intervals++;
917 logmsg("Pausing %d milliseconds after writing %ld bytes",
918 msecs_left, (long)written);
919 while((intervals > 0) && !got_exit_signal) {
920 int sleep_time = msecs_left > MAX_SLEEP_TIME_MS ?
921 MAX_SLEEP_TIME_MS : msecs_left;
922 intervals--;
923 curlx_wait_ms(sleep_time);
924 msecs_left -= sleep_time;
925 }
926 }
927 } while((count > 0) && !got_exit_signal);
929 if(curlx_fclose(dump))
930 logmsg("Error closing file %s error (%d) %s", responsedump,
931 errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));
933 if(got_exit_signal) {
934 free(ptr);
935 free(cmd);
936 return -1;
937 }
939 if(sendfailure) {
940 logmsg("Sending response failed. Only (%lu bytes) of (%lu bytes) "
941 "were sent",
942 (unsigned long)(responsesize - count), (unsigned long)responsesize);
943 sws_prevtestno = req->testno;
944 sws_prevpartno = req->partno;
945 free(ptr);
946 free(cmd);
947 return -1;
948 }
950 logmsg("Response sent (%lu bytes) and written to %s",
951 (unsigned long)responsesize, responsedump);
952 free(ptr);
954 if(cmdsize > 0) {
955 char command[32];
956 int quarters;
957 int num;
958 ptr = cmd;
959 do {
960 if(sscanf(ptr, "%31s %d", command, &num) == 2) {
961 if(!strcmp("wait", command)) {
962 logmsg("Told to sleep for %d seconds", num);
963 quarters = num * 4;
964 while((quarters > 0) && !got_exit_signal) {
965 quarters--;
966 if(curlx_wait_ms(250)) {
967 /* should not happen */
968 int sockerr = SOCKERRNO;
969 logmsg("curlx_wait_ms() failed with error (%d) %s",
970 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
971 break;
972 }
973 }
974 if(!quarters)
975 logmsg("Continuing after sleeping %d seconds", num);
976 }
977 else
978 logmsg("Unknown command in reply command section");
979 }
980 ptr = strchr(ptr, '\n');
981 if(ptr)
982 ptr++;
983 else
984 ptr = NULL;
985 } while(ptr && *ptr);
986 }
987 free(cmd);
988 req->open = use_gopher ? FALSE : persistent;
990 sws_prevtestno = req->testno;
991 sws_prevpartno = req->partno;
993 return 0;
996static void init_httprequest(struct sws_httprequest *req)
998 req->checkindex = 0;
999 req->offset = 0;
1000 req->testno = DOCNUMBER_NOTHING;
1001 req->partno = 0;
1002 req->connect_request = FALSE;
1003 req->open = TRUE;
1004 req->auth_req = FALSE;
1005 req->auth = FALSE;
1006 req->cl = 0;
1007 req->digest = FALSE;
1008 req->skip = 0;
1009 req->skipall = FALSE;
1010 req->noexpect = FALSE;
1011 req->delay = 0;
1012 req->writedelay = 0;
1013 req->rcmd = RCMD_NORMALREQ;
1014 req->prot_version = 0;
1015 req->callcount = 0;
1016 req->connect_port = 0;
1017 req->done_processing = 0;
1018 req->upgrade = 0;
1019 req->upgrade_request = 0;
1022/* returns 1 if the connection should be serviced again immediately, 0 if there
1023 is no data waiting, or < 0 if it should be closed */
1024static int sws_get_request(curl_socket_t sock, struct sws_httprequest *req)
1026 int fail = 0;
1027 char *reqbuf = req->reqbuf;
1028 ssize_t got = 0;
1029 int overflow = 0;
1031 if(req->upgrade_request) {
1032 /* upgraded connection, work it differently until end of connection */
1033 logmsg("Upgraded connection, this is no longer HTTP/1");
1034 sws_send_doc(sock, req);
1036 /* dump the request received so far to the external file */
1037 reqbuf[req->offset] = '\0';
1038 storerequest(reqbuf, req->offset, REQUEST_DUMP_FILENAME);
1039 req->offset = 0;
1041 /* read websocket traffic */
1042 if(req->open) {
1043 logmsg("wait for websocket traffic");
1044 do {
1045 got = sread(sock, reqbuf + req->offset,
1046 sizeof(req->reqbuf) - 1 - req->offset);
1047 if(got > 0) {
1048 req->offset += got;
1049 logmsg("Got %ld bytes from client", (long)got);
1052 if((got == -1) && SOCK_EAGAIN(SOCKERRNO)) {
1053 int rc;
1054 fd_set input;
1055 fd_set output;
1056 struct timeval timeout = { 0 };
1057 timeout.tv_sec = 1; /* 1000 ms */
1059 logmsg("Got EAGAIN from sread");
1060 FD_ZERO(&input);
1061 FD_ZERO(&output);
1062 got = 0;
1063 FD_SET(sock, &input);
1064 do {
1065 logmsg("Wait until readable");
1066 rc = select((int)sock + 1, &input, &output, NULL, &timeout);
1067 } while(rc < 0 && SOCKERRNO == SOCKEINTR && !got_exit_signal);
1068 logmsg("readable %d", rc);
1069 if(rc)
1070 got = 1;
1072 } while(got > 0);
1074 else {
1075 logmsg("NO wait for websocket traffic");
1077 if(req->offset) {
1078 logmsg("log the websocket traffic");
1079 /* dump the incoming websocket traffic to the external file */
1080 reqbuf[req->offset] = '\0';
1081 storerequest(reqbuf, req->offset, REQUEST_DUMP_FILENAME);
1082 req->offset = 0;
1084 init_httprequest(req);
1086 return -1;
1089 if(req->offset >= sizeof(req->reqbuf) - 1) {
1090 /* buffer is already full; do nothing */
1091 overflow = 1;
1093 else {
1094 if(req->skip)
1095 /* we are instructed to not read the entire thing, so we make sure to
1096 only read what we are supposed to and NOT read the entire thing the
1097 client wants to send! */
1098 got = sread(sock, reqbuf + req->offset, req->cl);
1099 else
1100 got = sread(sock, reqbuf + req->offset,
1101 sizeof(req->reqbuf) - 1 - req->offset);
1103 if(got_exit_signal)
1104 return -1;
1105 if(got == 0) {
1106 logmsg("Connection closed by client");
1107 fail = 1;
1109 else if(got < 0) {
1110 char errbuf[STRERROR_LEN];
1111 int sockerr = SOCKERRNO;
1112 if(SOCK_EAGAIN(sockerr)) {
1113 /* nothing to read at the moment */
1114 return 0;
1116 logmsg("recv() returned error (%d) %s",
1117 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1118 fail = 1;
1120 if(fail) {
1121 /* dump the request received so far to the external file */
1122 reqbuf[req->offset] = '\0';
1123 storerequest(reqbuf, req->offset, REQUEST_DUMP_FILENAME);
1124 return -1;
1127 logmsg("Read %ld bytes", (long)got);
1129 req->offset += (size_t)got;
1130 reqbuf[req->offset] = '\0';
1132 req->done_processing = sws_ProcessRequest(req);
1133 if(got_exit_signal)
1134 return -1;
1137 if(overflow || (req->offset == sizeof(req->reqbuf) - 1 && got > 0)) {
1138 logmsg("Request would overflow buffer, closing connection");
1139 /* dump request received so far to external file anyway */
1140 reqbuf[sizeof(req->reqbuf) - 1] = '\0';
1141 fail = 1;
1143 else if(req->offset > sizeof(req->reqbuf) - 1) {
1144 logmsg("Request buffer overflow, closing connection");
1145 /* dump request received so far to external file anyway */
1146 reqbuf[sizeof(req->reqbuf) - 1] = '\0';
1147 fail = 1;
1149 else
1150 reqbuf[req->offset] = '\0';
1152 /* at the end of a request dump it to an external file */
1153 if(fail || req->done_processing)
1154 storerequest(reqbuf, req->offset, REQUEST_DUMP_FILENAME);
1155 if(got_exit_signal)
1156 return -1;
1158 return fail ? -1 : 1;
1161static curl_socket_t connect_to(const char *ipaddr, uint16_t port)
1163 srvr_sockaddr_union_t serveraddr;
1164 curl_socket_t serverfd;
1165 int sockerr;
1166 char errbuf[STRERROR_LEN];
1167 int rc = 0;
1168 const char *op_br = "";
1169 const char *cl_br = "";
1171#ifdef USE_IPV6
1172 if(socket_domain == AF_INET6) {
1173 op_br = "[";
1174 cl_br = "]";
1176#endif
1178 if(!ipaddr)
1179 return CURL_SOCKET_BAD;
1181 logmsg("about to connect to %s%s%s:%hu", op_br, ipaddr, cl_br, port);
1183 serverfd = socket(socket_domain, SOCK_STREAM, 0);
1184 if(serverfd == CURL_SOCKET_BAD) {
1185 sockerr = SOCKERRNO;
1186 logmsg("Error creating socket for server connection (%d) %s",
1187 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1188 return CURL_SOCKET_BAD;
1191#if defined(TCP_NODELAY) && defined(CURL_TCP_NODELAY_SUPPORTED)
1192 if(socket_domain_is_ip()) {
1193 /* Disable the Nagle algorithm */
1194 curl_socklen_t flag = 1;
1195 if(setsockopt(serverfd, IPPROTO_TCP, TCP_NODELAY,
1196 (void *)&flag, sizeof(flag)))
1197 logmsg("====> TCP_NODELAY for server connection failed");
1199#endif
1201 /* We want to do the connect() in a non-blocking mode, since
1202 * Windows has an internal retry logic that may lead to long
1203 * timeouts if the peer is not listening. */
1204 if(curlx_nonblock(serverfd, TRUE)) {
1205 sockerr = SOCKERRNO;
1206 logmsg("curlx_nonblock(TRUE) failed with error (%d) %s",
1207 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1208 sclose(serverfd);
1209 return CURL_SOCKET_BAD;
1212 switch(socket_domain) {
1213 case AF_INET:
1214 memset(&serveraddr.sa4, 0, sizeof(serveraddr.sa4));
1215 serveraddr.sa4.sin_family = AF_INET;
1216 serveraddr.sa4.sin_port = htons(port);
1217 if(curlx_inet_pton(AF_INET, ipaddr, &serveraddr.sa4.sin_addr) < 1) {
1218 logmsg("Error inet_pton failed AF_INET conversion of '%s'", ipaddr);
1219 sclose(serverfd);
1220 return CURL_SOCKET_BAD;
1223 rc = connect(serverfd, &serveraddr.sa, sizeof(serveraddr.sa4));
1224 break;
1225#ifdef USE_IPV6
1226 case AF_INET6:
1227 memset(&serveraddr.sa6, 0, sizeof(serveraddr.sa6));
1228 serveraddr.sa6.sin6_family = AF_INET6;
1229 serveraddr.sa6.sin6_port = htons(port);
1230 if(curlx_inet_pton(AF_INET6, ipaddr, &serveraddr.sa6.sin6_addr) < 1) {
1231 logmsg("Error inet_pton failed AF_INET6 conversion of '%s'", ipaddr);
1232 sclose(serverfd);
1233 return CURL_SOCKET_BAD;
1236 rc = connect(serverfd, &serveraddr.sa, sizeof(serveraddr.sa6));
1237 break;
1238#endif /* USE_IPV6 */
1239#ifdef USE_UNIX_SOCKETS
1240 case AF_UNIX:
1241 logmsg("Proxying through Unix socket is not (yet?) supported.");
1242 return CURL_SOCKET_BAD;
1243#endif /* USE_UNIX_SOCKETS */
1246 if(got_exit_signal) {
1247 sclose(serverfd);
1248 return CURL_SOCKET_BAD;
1251 if(rc) {
1252 sockerr = SOCKERRNO;
1253 if((sockerr == SOCKEINPROGRESS) || SOCK_EAGAIN(sockerr)) {
1254 fd_set output;
1255 struct timeval timeout = { 0 };
1256 timeout.tv_sec = 1; /* 1000 ms */
1258 FD_ZERO(&output);
1259 FD_SET(serverfd, &output);
1260 while(1) {
1261 rc = select((int)serverfd + 1, NULL, &output, NULL, &timeout);
1262 sockerr = SOCKERRNO;
1263 if(rc < 0 && sockerr != SOCKEINTR)
1264 goto error;
1265 else if(rc > 0) {
1266 curl_socklen_t errSize = sizeof(sockerr);
1267 if(getsockopt(serverfd, SOL_SOCKET, SO_ERROR,
1268 (void *)&sockerr, &errSize))
1269 sockerr = SOCKERRNO;
1270 if((sockerr == 0) || (SOCKEISCONN == sockerr))
1271 goto success;
1272 else if((sockerr != SOCKEINPROGRESS) && !SOCK_EAGAIN(sockerr))
1273 goto error;
1275 else if(!rc) {
1276 logmsg("Timeout connecting to server port %hu", port);
1277 sclose(serverfd);
1278 return CURL_SOCKET_BAD;
1282error:
1283 logmsg("Error connecting to server port %hu (%d) %s", port,
1284 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1285 sclose(serverfd);
1286 return CURL_SOCKET_BAD;
1288success:
1289 logmsg("connected fine to %s%s%s:%hu, now tunnel",
1290 op_br, ipaddr, cl_br, port);
1292 if(curlx_nonblock(serverfd, FALSE)) {
1293 sockerr = SOCKERRNO;
1294 logmsg("curlx_nonblock(FALSE) failed with error (%d) %s",
1295 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1296 sclose(serverfd);
1297 return CURL_SOCKET_BAD;
1300 return serverfd;
1304 * A CONNECT has been received, a CONNECT response has been sent.
1306 * This function needs to connect to the server, and then pass data between
1307 * the client and the server back and forth until the connection is closed by
1308 * either end.
1310 * When doing FTP through a CONNECT proxy, we expect that the data connection
1311 * is setup while the first connect is still being kept up. Therefore we
1312 * must accept a new connection and deal with it appropriately.
1313 */
1315#define data_or_ctrl(x) ((x) ? "DATA" : "CTRL")
1317#define SWS_CTRL 0
1318#define SWS_DATA 1
1320static void http_connect(curl_socket_t *infdp,
1321 curl_socket_t rootfd,
1322 const char *ipaddr,
1323 uint16_t ipport,
1324 int keepalive_secs)
1326 curl_socket_t serverfd[2] = { CURL_SOCKET_BAD, CURL_SOCKET_BAD };
1327 curl_socket_t clientfd[2] = { CURL_SOCKET_BAD, CURL_SOCKET_BAD };
1328 ssize_t toc[2] = { 0, 0 }; /* number of bytes to client */
1329 ssize_t tos[2] = { 0, 0 }; /* number of bytes to server */
1330 char readclient[2][256];
1331 char readserver[2][256];
1332 bool poll_client_rd[2] = { TRUE, TRUE };
1333 bool poll_server_rd[2] = { TRUE, TRUE };
1334 bool poll_client_wr[2] = { TRUE, TRUE };
1335 bool poll_server_wr[2] = { TRUE, TRUE };
1336 bool primary = FALSE;
1337 bool secondary = FALSE;
1338 int max_tunnel_idx; /* SWS_CTRL or SWS_DATA */
1339 int loop;
1340 int i;
1341 int timeout_count = 0;
1343 /* primary tunnel client endpoint already connected */
1344 clientfd[SWS_CTRL] = *infdp;
1346 /* Sleep here to make sure the client reads CONNECT response's
1347 'end of headers' separate from the server data that follows.
1348 This is done to prevent triggering libcurl known bug #39. */
1349 for(loop = 2; (loop > 0) && !got_exit_signal; loop--)
1350 curlx_wait_ms(250);
1351 if(got_exit_signal)
1352 goto http_connect_cleanup;
1354 serverfd[SWS_CTRL] = connect_to(ipaddr, ipport);
1355 if(serverfd[SWS_CTRL] == CURL_SOCKET_BAD)
1356 goto http_connect_cleanup;
1358 /* Primary tunnel socket endpoints are now connected. Tunnel data back and
1359 forth over the primary tunnel until client or server breaks the primary
1360 tunnel, simultaneously allowing establishment, operation and teardown of
1361 a secondary tunnel that may be used for passive FTP data connection. */
1363 max_tunnel_idx = SWS_CTRL;
1364 primary = TRUE;
1366 while(!got_exit_signal) {
1368 fd_set input;
1369 fd_set output;
1370 ssize_t rc;
1371 curl_socket_t maxfd = (curl_socket_t)-1;
1372 struct timeval timeout = { 0 };
1373 timeout.tv_sec = 1; /* 1000 ms */
1375 FD_ZERO(&input);
1376 FD_ZERO(&output);
1378 if((clientfd[SWS_DATA] == CURL_SOCKET_BAD) &&
1379 (serverfd[SWS_DATA] == CURL_SOCKET_BAD) &&
1380 poll_client_rd[SWS_CTRL] && poll_client_wr[SWS_CTRL] &&
1381 poll_server_rd[SWS_CTRL] && poll_server_wr[SWS_CTRL]) {
1382 /* listener socket is monitored to allow client to establish
1383 secondary tunnel only when this tunnel is not established
1384 and primary one is fully operational */
1385 FD_SET(rootfd, &input);
1386 maxfd = rootfd;
1389 /* set tunnel sockets to wait for */
1390 for(i = 0; i <= max_tunnel_idx; i++) {
1391 /* client side socket monitoring */
1392 if(clientfd[i] != CURL_SOCKET_BAD) {
1393 if(poll_client_rd[i]) {
1394 /* unless told not to do so, monitor readability */
1395 FD_SET(clientfd[i], &input);
1396 if(clientfd[i] > maxfd)
1397 maxfd = clientfd[i];
1399 if(poll_client_wr[i] && toc[i]) {
1400 /* unless told not to do so, monitor writability
1401 if there is data ready to be sent to client */
1402 FD_SET(clientfd[i], &output);
1403 if(clientfd[i] > maxfd)
1404 maxfd = clientfd[i];
1407 /* server side socket monitoring */
1408 if(serverfd[i] != CURL_SOCKET_BAD) {
1409 if(poll_server_rd[i]) {
1410 /* unless told not to do so, monitor readability */
1411 FD_SET(serverfd[i], &input);
1412 if(serverfd[i] > maxfd)
1413 maxfd = serverfd[i];
1415 if(poll_server_wr[i] && tos[i]) {
1416 /* unless told not to do so, monitor writability
1417 if there is data ready to be sent to server */
1418 FD_SET(serverfd[i], &output);
1419 if(serverfd[i] > maxfd)
1420 maxfd = serverfd[i];
1424 if(got_exit_signal)
1425 break;
1427 do {
1428 rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
1429 } while(rc < 0 && SOCKERRNO == SOCKEINTR && !got_exit_signal);
1431 if(got_exit_signal)
1432 break;
1434 if(rc > 0) {
1435 /* socket action */
1436 bool tcp_fin_wr = FALSE;
1437 timeout_count = 0;
1439 /* ---------------------------------------------------------- */
1441 /* passive mode FTP may establish a secondary tunnel */
1442 if((clientfd[SWS_DATA] == CURL_SOCKET_BAD) &&
1443 (serverfd[SWS_DATA] == CURL_SOCKET_BAD) && FD_ISSET(rootfd, &input)) {
1444 /* a new connection on listener socket (most likely from client) */
1445 curl_socket_t datafd = accept(rootfd, NULL, NULL);
1446 if(datafd != CURL_SOCKET_BAD) {
1447 static struct sws_httprequest *req2;
1448 int err = 0;
1449 if(!req2) {
1450 req2 = malloc(sizeof(*req2));
1451 if(!req2)
1452 goto http_connect_cleanup; /* fail */
1454 memset(req2, 0, sizeof(*req2));
1455 logmsg("====> Client connect DATA");
1456#if defined(TCP_NODELAY) && defined(CURL_TCP_NODELAY_SUPPORTED)
1457 if(socket_domain_is_ip()) {
1458 /* Disable the Nagle algorithm */
1459 curl_socklen_t flag = 1;
1460 if(setsockopt(datafd, IPPROTO_TCP, TCP_NODELAY,
1461 (void *)&flag, sizeof(flag)))
1462 logmsg("====> TCP_NODELAY for client DATA connection failed");
1464#endif
1465 init_httprequest(req2);
1466 while(!req2->done_processing) {
1467 err = sws_get_request(datafd, req2);
1468 if(err < 0) {
1469 /* this socket must be closed, done or not */
1470 break;
1474 /* skip this and close the socket if err < 0 */
1475 if(err >= 0) {
1476 err = sws_send_doc(datafd, req2);
1477 if(!err && req2->connect_request) {
1478 /* sleep to prevent triggering libcurl known bug #39. */
1479 for(loop = 2; (loop > 0) && !got_exit_signal; loop--)
1480 curlx_wait_ms(250);
1481 if(!got_exit_signal) {
1482 /* connect to the server */
1483 serverfd[SWS_DATA] = connect_to(ipaddr, req2->connect_port);
1484 if(serverfd[SWS_DATA] != CURL_SOCKET_BAD) {
1485 /* secondary tunnel established, now we have two
1486 connections */
1487 poll_client_rd[SWS_DATA] = TRUE;
1488 poll_client_wr[SWS_DATA] = TRUE;
1489 poll_server_rd[SWS_DATA] = TRUE;
1490 poll_server_wr[SWS_DATA] = TRUE;
1491 max_tunnel_idx = SWS_DATA;
1492 secondary = TRUE;
1493 toc[SWS_DATA] = 0;
1494 tos[SWS_DATA] = 0;
1495 clientfd[SWS_DATA] = datafd;
1496 datafd = CURL_SOCKET_BAD;
1501 if(datafd != CURL_SOCKET_BAD) {
1502 /* secondary tunnel not established */
1503 shutdown(datafd, SHUT_RDWR);
1504 sclose(datafd);
1507 if(got_exit_signal)
1508 break;
1511 /* ---------------------------------------------------------- */
1513 /* react to tunnel endpoint readable/writable notifications */
1514 for(i = 0; i <= max_tunnel_idx; i++) {
1515 size_t len;
1516 if(clientfd[i] != CURL_SOCKET_BAD) {
1517 len = sizeof(readclient[i]) - tos[i];
1518 if(len && FD_ISSET(clientfd[i], &input)) {
1519 /* read from client */
1520 rc = sread(clientfd[i], &readclient[i][tos[i]], len);
1521 if(rc <= 0) {
1522 logmsg("[%s] got %ld, STOP READING client", data_or_ctrl(i),
1523 (long)rc);
1524 shutdown(clientfd[i], SHUT_RD);
1525 poll_client_rd[i] = FALSE;
1527 else {
1528 logmsg("[%s] READ %ld bytes from client", data_or_ctrl(i),
1529 (long)rc);
1530 logmsg("[%s] READ \"%s\"", data_or_ctrl(i),
1531 data_to_hex(&readclient[i][tos[i]], rc));
1532 tos[i] += rc;
1536 if(serverfd[i] != CURL_SOCKET_BAD) {
1537 len = sizeof(readserver[i]) - toc[i];
1538 if(len && FD_ISSET(serverfd[i], &input)) {
1539 /* read from server */
1540 rc = sread(serverfd[i], &readserver[i][toc[i]], len);
1541 if(rc <= 0) {
1542 logmsg("[%s] got %ld, STOP READING server", data_or_ctrl(i),
1543 (long)rc);
1544 shutdown(serverfd[i], SHUT_RD);
1545 poll_server_rd[i] = FALSE;
1547 else {
1548 logmsg("[%s] READ %ld bytes from server", data_or_ctrl(i),
1549 (long)rc);
1550 logmsg("[%s] READ \"%s\"", data_or_ctrl(i),
1551 data_to_hex(&readserver[i][toc[i]], rc));
1552 toc[i] += rc;
1556 if(clientfd[i] != CURL_SOCKET_BAD) {
1557 if(toc[i] && FD_ISSET(clientfd[i], &output)) {
1558 /* write to client */
1559 rc = swrite(clientfd[i], readserver[i], toc[i]);
1560 if(rc <= 0) {
1561 logmsg("[%s] got %ld, STOP WRITING client", data_or_ctrl(i),
1562 (long)rc);
1563 shutdown(clientfd[i], SHUT_WR);
1564 poll_client_wr[i] = FALSE;
1565 tcp_fin_wr = TRUE;
1567 else {
1568 logmsg("[%s] SENT %ld bytes to client", data_or_ctrl(i),
1569 (long)rc);
1570 logmsg("[%s] SENT \"%s\"", data_or_ctrl(i),
1571 data_to_hex(readserver[i], rc));
1572 if(toc[i] - rc)
1573 memmove(&readserver[i][0], &readserver[i][rc], toc[i] - rc);
1574 toc[i] -= rc;
1578 if(serverfd[i] != CURL_SOCKET_BAD) {
1579 if(tos[i] && FD_ISSET(serverfd[i], &output)) {
1580 /* write to server */
1581 rc = swrite(serverfd[i], readclient[i], tos[i]);
1582 if(rc <= 0) {
1583 logmsg("[%s] got %ld, STOP WRITING server", data_or_ctrl(i),
1584 (long)rc);
1585 shutdown(serverfd[i], SHUT_WR);
1586 poll_server_wr[i] = FALSE;
1587 tcp_fin_wr = TRUE;
1589 else {
1590 logmsg("[%s] SENT %ld bytes to server", data_or_ctrl(i),
1591 (long)rc);
1592 logmsg("[%s] SENT \"%s\"", data_or_ctrl(i),
1593 data_to_hex(readclient[i], rc));
1594 if(tos[i] - rc)
1595 memmove(&readclient[i][0], &readclient[i][rc], tos[i] - rc);
1596 tos[i] -= rc;
1601 if(got_exit_signal)
1602 break;
1604 /* ---------------------------------------------------------- */
1606 /* endpoint read/write disabling, endpoint closing and tunnel teardown */
1607 for(i = 0; i <= max_tunnel_idx; i++) {
1608 for(loop = 2; loop > 0; loop--) {
1609 /* loop twice to satisfy condition interdependencies without
1610 having to await select timeout or another socket event */
1611 if(clientfd[i] != CURL_SOCKET_BAD) {
1612 if(poll_client_rd[i] && !poll_server_wr[i]) {
1613 logmsg("[%s] DISABLED READING client", data_or_ctrl(i));
1614 shutdown(clientfd[i], SHUT_RD);
1615 poll_client_rd[i] = FALSE;
1617 if(poll_client_wr[i] && !poll_server_rd[i] && !toc[i]) {
1618 logmsg("[%s] DISABLED WRITING client", data_or_ctrl(i));
1619 shutdown(clientfd[i], SHUT_WR);
1620 poll_client_wr[i] = FALSE;
1621 tcp_fin_wr = TRUE;
1624 if(serverfd[i] != CURL_SOCKET_BAD) {
1625 if(poll_server_rd[i] && !poll_client_wr[i]) {
1626 logmsg("[%s] DISABLED READING server", data_or_ctrl(i));
1627 shutdown(serverfd[i], SHUT_RD);
1628 poll_server_rd[i] = FALSE;
1630 if(poll_server_wr[i] && !poll_client_rd[i] && !tos[i]) {
1631 logmsg("[%s] DISABLED WRITING server", data_or_ctrl(i));
1632 shutdown(serverfd[i], SHUT_WR);
1633 poll_server_wr[i] = FALSE;
1634 tcp_fin_wr = TRUE;
1640 if(tcp_fin_wr)
1641 /* allow kernel to place FIN bit packet on the wire */
1642 curlx_wait_ms(250);
1644 /* socket clearing */
1645 for(i = 0; i <= max_tunnel_idx; i++) {
1646 for(loop = 2; loop > 0; loop--) {
1647 if(clientfd[i] != CURL_SOCKET_BAD) {
1648 if(!poll_client_wr[i] && !poll_client_rd[i]) {
1649 logmsg("[%s] CLOSING client socket", data_or_ctrl(i));
1650 sclose(clientfd[i]);
1651 clientfd[i] = CURL_SOCKET_BAD;
1652 if(serverfd[i] == CURL_SOCKET_BAD) {
1653 logmsg("[%s] ENDING", data_or_ctrl(i));
1654 if(i == SWS_DATA)
1655 secondary = FALSE;
1656 else
1657 primary = FALSE;
1661 if(serverfd[i] != CURL_SOCKET_BAD) {
1662 if(!poll_server_wr[i] && !poll_server_rd[i]) {
1663 logmsg("[%s] CLOSING server socket", data_or_ctrl(i));
1664 sclose(serverfd[i]);
1665 serverfd[i] = CURL_SOCKET_BAD;
1666 if(clientfd[i] == CURL_SOCKET_BAD) {
1667 logmsg("[%s] ENDING", data_or_ctrl(i));
1668 if(i == SWS_DATA)
1669 secondary = FALSE;
1670 else
1671 primary = FALSE;
1678 /* ---------------------------------------------------------- */
1680 max_tunnel_idx = secondary ? SWS_DATA : SWS_CTRL;
1682 if(!primary)
1683 /* exit loop upon primary tunnel teardown */
1684 break;
1686 } /* (rc > 0) */
1687 else {
1688 timeout_count++;
1689 if(timeout_count > keepalive_secs) {
1690 logmsg("CONNECT proxy timeout after %d idle seconds!", timeout_count);
1691 break;
1696http_connect_cleanup:
1698 for(i = SWS_DATA; i >= SWS_CTRL; i--) {
1699 if(serverfd[i] != CURL_SOCKET_BAD) {
1700 logmsg("[%s] CLOSING server socket (cleanup)", data_or_ctrl(i));
1701 shutdown(serverfd[i], SHUT_RDWR);
1702 sclose(serverfd[i]);
1704 if(clientfd[i] != CURL_SOCKET_BAD) {
1705 logmsg("[%s] CLOSING client socket (cleanup)", data_or_ctrl(i));
1706 shutdown(clientfd[i], SHUT_RDWR);
1707 sclose(clientfd[i]);
1709 if((serverfd[i] != CURL_SOCKET_BAD) ||
1710 (clientfd[i] != CURL_SOCKET_BAD)) {
1711 logmsg("[%s] ABORTING", data_or_ctrl(i));
1715 *infdp = CURL_SOCKET_BAD;
1718static void http_upgrade(struct sws_httprequest *req)
1720 (void)req;
1721 logmsg("Upgraded to ... %d", (int)req->upgrade_request);
1722 /* left to implement */
1725/* returns 1 if the connection should be serviced again immediately, 0 if there
1726 is no data waiting, or < 0 if it should be closed */
1727static int service_connection(curl_socket_t *msgsock,
1728 struct sws_httprequest *req,
1729 curl_socket_t listensock,
1730 const char *connecthost,
1731 int keepalive_secs)
1733 if(got_exit_signal)
1734 return -1;
1736 while(!req->done_processing) {
1737 int rc = sws_get_request(*msgsock, req);
1738 if(rc <= 0) {
1739 /* Nothing further to read now, possibly because the socket was closed */
1740 return rc;
1744 if(sws_prevbounce) {
1745 /* bounce treatment requested */
1746 if(req->testno == sws_prevtestno) {
1747 req->partno = sws_prevpartno + 1;
1748 logmsg("BOUNCE part number to %ld", req->partno);
1750 else {
1751 sws_prevbounce = FALSE;
1752 sws_prevtestno = -1;
1753 sws_prevpartno = -1;
1757 sws_send_doc(*msgsock, req);
1758 if(got_exit_signal)
1759 return -1;
1761 if(req->testno < 0) {
1762 logmsg("special request received, no persistency");
1763 return -1;
1765 if(!req->open) {
1766 logmsg("instructed to close connection after server-reply");
1767 return -1;
1770 if(req->connect_request) {
1771 /* a CONNECT request, setup and talk the tunnel */
1772 if(!is_proxy) {
1773 logmsg("received CONNECT but not running as proxy!");
1774 return 1;
1776 else {
1777 http_connect(msgsock, listensock, connecthost, req->connect_port,
1778 keepalive_secs);
1779 return -1;
1783 if(req->upgrade_request) {
1784 /* an upgrade request, switch to another protocol here */
1785 http_upgrade(req);
1786 return 1;
1789 /* if we got a CONNECT, loop and get another request as well! */
1791 if(req->open) {
1792 logmsg("=> persistent connection request ended, awaits new request");
1793 return 1;
1795 else {
1796 logmsg("=> NOT a persistent connection, close close CLOSE");
1799 return -1;
1802static int test_sws(int argc, const char *argv[])
1804 curl_socket_t sock = CURL_SOCKET_BAD;
1805 int wrotepidfile = 0;
1806 int wroteportfile = 0;
1807#ifdef USE_UNIX_SOCKETS
1808 bool unlink_socket = FALSE;
1809#endif
1810 struct sws_httprequest *req = NULL;
1811 int rc = 0;
1812 int sockerr;
1813 char errbuf[STRERROR_LEN];
1814 int arg = 1;
1815 const char *connecthost = "127.0.0.1";
1816 char port_str[11];
1817 const char *location_str = port_str;
1818 int keepalive_secs = 5;
1819 const char *protocol_type = "HTTP";
1820 int result = 0;
1822 /* a default CONNECT port is pointless, but still ... */
1823 size_t socket_idx;
1825 pidname = ".http.pid";
1826 portname = ".http.port";
1827 serverlogfile = "log/sws.log";
1828 serverlogslocked = 0;
1829 server_port = 8999;
1831 while(argc > arg) {
1832 const char *opt;
1833 curl_off_t num;
1834 if(!strcmp("--version", argv[arg])) {
1835 puts("sws IPv4"
1836#ifdef USE_IPV6
1837 "/IPv6"
1838#endif
1839#ifdef USE_UNIX_SOCKETS
1840 "/unix"
1841#endif
1842 );
1843 return 0;
1845 else if(!strcmp("--pidfile", argv[arg])) {
1846 arg++;
1847 if(argc > arg)
1848 pidname = argv[arg++];
1850 else if(!strcmp("--portfile", argv[arg])) {
1851 arg++;
1852 if(argc > arg)
1853 portname = argv[arg++];
1855 else if(!strcmp("--logfile", argv[arg])) {
1856 arg++;
1857 if(argc > arg)
1858 serverlogfile = argv[arg++];
1860 else if(!strcmp("--logdir", argv[arg])) {
1861 arg++;
1862 if(argc > arg)
1863 logdir = argv[arg++];
1865 else if(!strcmp("--cmdfile", argv[arg])) {
1866 arg++;
1867 if(argc > arg)
1868 cmdfile = argv[arg++];
1870 else if(!strcmp("--gopher", argv[arg])) {
1871 arg++;
1872 use_gopher = TRUE;
1873 protocol_type = "GOPHER";
1874 end_of_headers = "\r\n"; /* gopher style is much simpler */
1876 else if(!strcmp("--ipv4", argv[arg])) {
1877 socket_type = "IPv4";
1878 socket_domain = AF_INET;
1879 location_str = port_str;
1880 arg++;
1882 else if(!strcmp("--ipv6", argv[arg])) {
1883#ifdef USE_IPV6
1884 socket_type = "IPv6";
1885 socket_domain = AF_INET6;
1886 location_str = port_str;
1887#endif
1888 arg++;
1890 else if(!strcmp("--unix-socket", argv[arg])) {
1891 arg++;
1892 if(argc > arg) {
1893#ifdef USE_UNIX_SOCKETS
1894 srvr_sockaddr_union_t me;
1895 server_unix_socket = argv[arg];
1896 if(strlen(server_unix_socket) >= sizeof(me.sau.sun_path)) {
1897 fprintf(stderr,
1898 "sws: socket path must be shorter than %u chars: %s\n",
1899 (unsigned int)sizeof(me.sau.sun_path), server_unix_socket);
1900 return 0;
1902 socket_type = "unix";
1903 socket_domain = AF_UNIX;
1904 location_str = server_unix_socket;
1905#endif
1906 arg++;
1909 else if(!strcmp("--port", argv[arg])) {
1910 arg++;
1911 if(argc > arg) {
1912 opt = argv[arg];
1913 if(curlx_str_number(&opt, &num, 0xffff)) {
1914 fprintf(stderr, "sws: invalid --port argument (%s)\n", argv[arg]);
1915 return 0;
1917 server_port = (uint16_t)num;
1918 arg++;
1921 else if(!strcmp("--srcdir", argv[arg])) {
1922 arg++;
1923 if(argc > arg) {
1924 srcpath = argv[arg];
1925 arg++;
1928 else if(!strcmp("--keepalive", argv[arg])) {
1929 arg++;
1930 if(argc > arg) {
1931 opt = argv[arg];
1932 if(curlx_str_number(&opt, &num, 0xffff)) {
1933 fprintf(stderr, "sws: invalid --keepalive argument (%s), must "
1934 "be a number of seconds\n", argv[arg]);
1935 return 0;
1937 keepalive_secs = (unsigned short)num;
1938 arg++;
1941 else if(!strcmp("--connect", argv[arg])) {
1942 /* The connect host IP number that the proxy connects to no matter
1943 what the client asks for, but also use this as a hint that we run as
1944 a proxy and do a few different internal choices */
1945 arg++;
1946 if(argc > arg) {
1947 connecthost = argv[arg];
1948 arg++;
1949 is_proxy = TRUE;
1950 logmsg("Run as proxy, CONNECT to host %s", connecthost);
1953 else {
1954 puts("Usage: sws [option]\n"
1955 " --version\n"
1956 " --logfile [file]\n"
1957 " --logdir [directory]\n"
1958 " --pidfile [file]\n"
1959 " --portfile [file]\n"
1960 " --ipv4\n"
1961 " --ipv6\n"
1962 " --unix-socket [file]\n"
1963 " --port [port]\n"
1964 " --srcdir [path]\n"
1965 " --connect [ip4-addr]\n"
1966 " --gopher");
1967 return 0;
1971 snprintf(loglockfile, sizeof(loglockfile), "%s/%s/sws-%s%s-%s.lock",
1972 logdir, SERVERLOGS_LOCKDIR, protocol_type,
1973 is_proxy ? "-proxy" : "", socket_type);
1975 install_signal_handlers(FALSE);
1977 req = calloc(1, sizeof(*req));
1978 if(!req)
1979 goto sws_cleanup;
1981 result = open_stream_sock(&sock, &server_port);
1982 if(result)
1983 goto sws_cleanup;
1985 all_sockets[0] = sock;
1986 num_sockets = 1;
1988#ifdef USE_UNIX_SOCKETS
1989 if(socket_domain != AF_UNIX)
1990#endif
1991 snprintf(port_str, sizeof(port_str), "port %hu", server_port);
1993 logmsg("Running %s %s version on %s",
1994 protocol_type, socket_type, location_str);
1996 /* start accepting connections */
1997 if(listen(sock, 50)) {
1998 sockerr = SOCKERRNO;
1999 logmsg("listen() failed with error (%d) %s",
2000 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
2001 goto sws_cleanup;
2004#ifdef USE_UNIX_SOCKETS
2005 /* listen succeeds, so let's assume a valid listening Unix socket */
2006 unlink_socket = TRUE;
2007#endif
2009 /*
2010 * As soon as this server writes its pid file the test harness attempts
2011 * to connect to this server and initiate its verification.
2012 */
2014 wrotepidfile = write_pidfile(pidname);
2015 if(!wrotepidfile)
2016 goto sws_cleanup;
2018 wroteportfile = write_portfile(portname, server_port);
2019 if(!wroteportfile)
2020 goto sws_cleanup;
2022 /* initialization of httprequest struct is done before sws_get_request(),
2023 but the pipelining struct field must be initialized previously to FALSE
2024 every time a new connection arrives. */
2026 init_httprequest(req);
2028 for(;;) {
2029 fd_set input;
2030 fd_set output;
2031 curl_socket_t maxfd = (curl_socket_t)-1;
2032 int active;
2033 struct timeval timeout = { 0 };
2034 timeout.tv_usec = 250000L; /* 250 ms */
2036 /* Clear out closed sockets */
2037 for(socket_idx = num_sockets - 1; socket_idx >= 1; --socket_idx) {
2038 if(all_sockets[socket_idx] == CURL_SOCKET_BAD) {
2039 char *dst = (char *)(all_sockets + socket_idx);
2040 char *src = (char *)(all_sockets + socket_idx + 1);
2041 char *end = (char *)(all_sockets + num_sockets);
2042 memmove(dst, src, end - src);
2043 num_sockets -= 1;
2047 if(got_exit_signal)
2048 goto sws_cleanup;
2050 /* Set up for select */
2051 FD_ZERO(&input);
2052 FD_ZERO(&output);
2054 for(socket_idx = 0; socket_idx < num_sockets; ++socket_idx) {
2055 /* Listen on all sockets */
2056 FD_SET(all_sockets[socket_idx], &input);
2057 if(all_sockets[socket_idx] > maxfd)
2058 maxfd = all_sockets[socket_idx];
2061 if(got_exit_signal)
2062 goto sws_cleanup;
2064 sockerr = 0;
2065 do {
2066 rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
2067 } while(rc < 0 && ((sockerr = SOCKERRNO) == SOCKEINTR &&
2068 !got_exit_signal));
2070 if(got_exit_signal)
2071 goto sws_cleanup;
2073 if(rc < 0) {
2074 logmsg("select() failed with error (%d) %s",
2075 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
2076 goto sws_cleanup;
2079 if(rc == 0) {
2080 /* Timed out - try again */
2081 continue;
2083 active = rc; /* a positive number */
2085 /* Check if the listening socket is ready to accept */
2086 if(FD_ISSET(all_sockets[0], &input)) {
2087 /* Service all queued connections */
2088 curl_socket_t msgsock;
2089 do {
2090 if(MAX_SOCKETS == num_sockets) {
2091 logmsg("Too many open sockets!");
2092 goto sws_cleanup;
2094 msgsock = accept_connection(sock);
2095 if(!msgsock)
2096 break;
2097 logmsg("accept_connection %ld returned %ld",
2098 (long)sock, (long)msgsock);
2099 if(msgsock == CURL_SOCKET_BAD)
2100 goto sws_cleanup;
2101 all_sockets[num_sockets] = msgsock;
2102 num_sockets += 1;
2103 if(req->delay)
2104 curlx_wait_ms(req->delay);
2105 } while(msgsock > 0);
2106 active--;
2109 /* Service all connections that are ready */
2110 for(socket_idx = 1; (socket_idx < num_sockets) && active; ++socket_idx) {
2111 if(FD_ISSET(all_sockets[socket_idx], &input)) {
2112 active--;
2113 if(got_exit_signal)
2114 goto sws_cleanup;
2116 /* Service this connection until it has nothing available */
2117 do {
2118 rc = service_connection(&all_sockets[socket_idx], req, sock,
2119 connecthost, keepalive_secs);
2120 if(got_exit_signal)
2121 goto sws_cleanup;
2123 if(rc < 0) {
2124 if(req->testno == DOCNUMBER_NOTHING) {
2125 /* The test number is unknown here, so connmon may be left over
2126 from a previous test case, for example when a client closes
2127 a connection without sending anything. Use the setting of
2128 the test case in server.cmd, or disable it without one. */
2129 req->connmon = FALSE;
2130 parse_cmdfile(req);
2131 if(req->testno != DOCNUMBER_NOTHING)
2132 sws_parse_servercmd(req);
2135 logmsg("====> Client disconnect %d", req->connmon);
2137 if(req->connmon) {
2138 static const char keepopen[] = "[DISCONNECT]\n";
2139 storerequest(keepopen, CURL_CSTRLEN(keepopen),
2140 REQUEST_DUMP_FILENAME);
2143 if(!req->open)
2144 /* When instructed to close connection after server-reply we
2145 wait a small amount of time before doing so. If this
2146 is not done client might get an ECONNRESET before reading
2147 a single byte of server-reply. */
2148 curlx_wait_ms(50);
2150 if(all_sockets[socket_idx] != CURL_SOCKET_BAD) {
2151 sclose(all_sockets[socket_idx]);
2152 all_sockets[socket_idx] = CURL_SOCKET_BAD;
2155 serverlogslocked -= 1;
2156 if(!serverlogslocked)
2157 clear_advisor_read_lock(loglockfile);
2159 if(req->testno == DOCNUMBER_QUIT)
2160 goto sws_cleanup;
2163 /* Reset the request, unless we are still in the middle of reading */
2164 if(rc && !req->upgrade_request)
2165 /* Note: resetting the HTTP request here can cause problems if:
2166 * 1) req->skipall is TRUE,
2167 * 2) the socket is still open, and
2168 * 3) (stale) data is still available (or about to be available)
2169 * on that socket
2170 * In that case, this loop runs once more and treat that stale
2171 * data (in service_connection()) as the first data received on
2172 * this new HTTP request and report "** Unusual request" (skipall
2173 * would have otherwise caused that data to be ignored). Normally,
2174 * that socket is closed by the client and there is no stale data
2175 * to cause this, but stranger things have happened
2176 * (see issue #11678).
2177 */
2178 init_httprequest(req);
2179 } while(rc > 0);
2183 if(got_exit_signal)
2184 goto sws_cleanup;
2187sws_cleanup:
2189 for(socket_idx = 1; socket_idx < num_sockets; ++socket_idx)
2190 if((all_sockets[socket_idx] != sock) &&
2191 (all_sockets[socket_idx] != CURL_SOCKET_BAD))
2192 sclose(all_sockets[socket_idx]);
2194 if(sock != CURL_SOCKET_BAD)
2195 sclose(sock);
2197#ifdef USE_UNIX_SOCKETS
2198 if(unlink_socket && socket_domain == AF_UNIX && server_unix_socket &&
2199 unlink(server_unix_socket))
2200 logmsg("unlink(%s): %d (%s)", server_unix_socket,
2201 errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));
2202#endif
2204 free(req);
2206 if(got_exit_signal)
2207 logmsg("signalled to die");
2209 if(wrotepidfile)
2210 unlink(pidname);
2211 if(wroteportfile)
2212 unlink(portname);
2214 if(serverlogslocked) {
2215 serverlogslocked = 0;
2216 clear_advisor_read_lock(loglockfile);
2219 restore_signal_handlers(FALSE);
2221 return result;
Diagnostics4 git calls · 28 ms
CallStatusSizeTime028 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20020 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=23
git.response.bytes=19978
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=1518b39a9268a71de4a55d9c53758b0b1854ebb8
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=19978
git.pkt.payload.max=19957
git.sideband.band1.bytes=19956
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e2ecf57cf977be
Request
3a8a5aea…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format