6 * Uncomment this to log all communications with the Citadel server
12 #include "webserver.h"
14 extern int DisableGzip;
17 * register the timeout
18 * signum signalhandler number
21 RETSIGTYPE timeout(int signum)
23 lprintf(1, "Connection timed out; unable to reach citserver\n");
24 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
29 * Connect a unix domain socket
30 * sockpath where to open a unix domain socket
32 int uds_connectsock(char *sockpath)
34 struct sockaddr_un addr;
37 memset(&addr, 0, sizeof(addr));
38 addr.sun_family = AF_UNIX;
39 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
41 s = socket(AF_UNIX, SOCK_STREAM, 0);
43 lprintf(1, "Can't create socket[%s]: %s\n",
49 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
50 lprintf(1, "Can't connect [%s]: %s\n",
62 * Connect a TCP/IP socket
63 * host the host to connect to
64 * service the service on the host to call
66 int tcp_connectsock(char *host, char *service)
72 struct sockaddr_in sin;
75 memset(&sin, 0, sizeof(sin));
76 sin.sin_family = AF_INET;
78 pse = getservbyname(service, "tcp");
80 sin.sin_port = pse->s_port;
81 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
82 lprintf(1, "Can't get %s service entry\n", service);
85 phe = gethostbyname(host);
87 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
88 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
89 lprintf(1, "Can't get %s host entry: %s\n",
90 host, strerror(errno));
93 if ((ppe = getprotobyname("tcp")) == 0) {
94 lprintf(1, "Can't get TCP protocol entry: %s\n",
99 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
101 lprintf(1, "Can't create socket: %s\n", strerror(errno));
105 fdflags = fcntl(s, F_GETFL);
107 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
108 host, service, strerror(errno));
109 fdflags = fdflags | O_NONBLOCK;
110 if (fcntl(s, F_SETFD, fdflags) < 0)
111 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
112 host, service, strerror(errno));
114 signal(SIGALRM, timeout);
117 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
118 lprintf(1, "Can't connect to %s.%s: %s\n",
119 host, service, strerror(errno));
124 signal(SIGALRM, SIG_IGN);
126 fdflags = fcntl(s, F_GETFL);
128 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
129 host, service, strerror(errno));
130 fdflags = fdflags | O_NONBLOCK;
131 if (fcntl(s, F_SETFD, fdflags) < 0)
132 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
133 host, service, strerror(errno));
140 * input string from pipe
142 int serv_getln(char *strbuf, int bufsize)
148 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
149 len = StrLength(WCC->MigrateReadLineBuf);
152 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
153 FlushStrBuf(WCC->MigrateReadLineBuf);
156 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
162 int StrBuf_ServGetln(StrBuf *buf)
165 const char *ErrStr = NULL;
168 rc = StrBufTCP_read_buffered_line_fast(buf,
176 lprintf(1, "Server connection broken: %s\n",
179 WCC->serv_sock = (-1);
186 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
192 rc = StrBufReadBLOBBuffered(buf,
202 lprintf(1, "Server connection broken: %s\n",
205 WCC->serv_sock = (-1);
212 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
219 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &Err);
222 lprintf(1, "Server connection broken: %s\n",
225 WCC->serv_sock = (-1);
233 * send binary to server
234 * buf the buffer to write to citadel server
235 * nbytes how many bytes to send to citadel server
237 void serv_write(const char *buf, int nbytes)
240 int bytes_written = 0;
243 FlushStrBuf(WCC->ReadBuf);
245 while (bytes_written < nbytes) {
246 retval = write(WCC->serv_sock, &buf[bytes_written],
247 nbytes - bytes_written);
249 lprintf(1, "Server connection broken: %s\n",
251 close(WCC->serv_sock);
252 WCC->serv_sock = (-1);
257 bytes_written = bytes_written + retval;
263 * send line to server
264 * string the line to send to the citadel server
266 void serv_puts(const char *string)
270 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
272 FlushStrBuf(WCC->ReadBuf);
275 serv_write(string, strlen(string));
280 * send line to server
281 * string the line to send to the citadel server
283 void serv_putbuf(const StrBuf *string)
287 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
289 FlushStrBuf(WCC->ReadBuf);
292 serv_write(ChrPtr(string), StrLength(string));
298 * convenience function to send stuff to the server
299 * format the formatstring
300 * ... the entities to insert into format
302 void serv_printf(const char *format,...)
309 FlushStrBuf(WCC->ReadBuf);
312 va_start(arg_ptr, format);
313 vsnprintf(buf, sizeof buf, format, arg_ptr);
319 serv_write(buf, len);
321 lprintf(9, "<%s", buf);
328 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
330 const char *Error, *pch, *pchs;
331 int rlen, len, retval = 0;
336 if (StrLength(Hdr->ReadBuf) > 0) {
337 pchs = ChrPtr(Hdr->ReadBuf);
338 pch = strchr(pchs, '\n');
342 if (len > 0 && (*(pch - 1) == '\r') )
344 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
345 StrBufCutLeft(Hdr->ReadBuf, len + 1);
350 while (retval == 0) {
352 pchs = ChrPtr(Hdr->ReadBuf);
354 pch = strchr(pchs, '\n');
356 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
357 pchs = ChrPtr(Hdr->ReadBuf);
358 pch = strchr(pchs, '\n');
367 if ((retval > 0) && (pch != NULL)) {
370 if (len > 0 && (*(pch - 1) == '\r') )
372 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
373 StrBufCutLeft(Hdr->ReadBuf, len + 1);
382 return StrBufTCP_read_buffered_line_fast(Target,
392 * This is a generic function to set up a master socket for listening on
393 * a TCP port. The server shuts down if the bind fails.
395 * ip_addr IP address to bind
396 * port_number port number to bind
397 * queue_len number of incoming connections to allow in the queue
399 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
401 struct sockaddr_in sin;
404 memset(&sin, 0, sizeof(sin));
405 sin.sin_family = AF_INET;
406 if (ip_addr == NULL) {
407 sin.sin_addr.s_addr = INADDR_ANY;
409 sin.sin_addr.s_addr = inet_addr(ip_addr);
412 if (sin.sin_addr.s_addr == INADDR_NONE) {
413 sin.sin_addr.s_addr = INADDR_ANY;
416 if (port_number == 0) {
417 lprintf(1, "Cannot start: no port number specified.\n");
420 sin.sin_port = htons((u_short) port_number);
422 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
424 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
427 /* Set some socket options that make sense. */
429 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
432 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
433 there should be a preceding F_GETFL
434 and a bitwise OR with the previous
438 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
439 lprintf(1, "Can't bind: %s\n", strerror(errno));
442 if (listen(s, queue_len) < 0) {
443 lprintf(1, "Can't listen: %s\n", strerror(errno));
452 * Create a Unix domain socket and listen on it
453 * sockpath - file name of the unix domain socket
454 * queue_len - Number of incoming connections to allow in the queue
456 int ig_uds_server(char *sockpath, int queue_len)
458 struct sockaddr_un addr;
461 int actual_queue_len;
463 actual_queue_len = queue_len;
464 if (actual_queue_len < 5) actual_queue_len = 5;
466 i = unlink(sockpath);
467 if ((i != 0) && (errno != ENOENT)) {
468 lprintf(1, "webcit: can't unlink %s: %s\n",
469 sockpath, strerror(errno));
473 memset(&addr, 0, sizeof(addr));
474 addr.sun_family = AF_UNIX;
475 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
477 s = socket(AF_UNIX, SOCK_STREAM, 0);
479 lprintf(1, "webcit: Can't create a socket: %s\n",
484 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
485 lprintf(1, "webcit: Can't bind: %s\n",
490 if (listen(s, actual_queue_len) < 0) {
491 lprintf(1, "webcit: Can't listen: %s\n",
496 chmod(sockpath, 0777);
504 * Read data from the client socket.
506 * sock socket fd to read from
507 * buf buffer to read into
508 * bytes number of bytes to read
509 * timeout Number of seconds to wait before timing out
511 * Possible return values:
512 * 1 Requested number of bytes has been read.
513 * 0 Request timed out.
514 * -1 Connection is broken, or other error.
516 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
523 long bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
524 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
526 FlushStrBuf(Hdr->ReadBuf);
528 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes) &&
530 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
532 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
534 write(2, "\033[32m", 5);
535 write(2, buf, bytes);
536 write(2, "\033[30m", 5);
541 lprintf(2, "client_read_ssl() failed\n");
547 retval = StrBufReadBLOBBuffered(Target,
556 lprintf(2, "client_read() failed: %s\n",
562 write(2, "\033[32m", 5);
563 write(2, buf, bytes);
564 write(2, "\033[30m", 5);
571 * Begin buffering HTTP output so we can transmit it all in one write operation later.
573 void begin_burst(void)
575 if (WC->WBuf == NULL) {
576 WC->WBuf = NewStrBufPlain(NULL, 32768);
582 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
587 const char *ptr, *eptr;
593 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok) && CompressBuffer(WCC->WBuf))
595 hprintf("Content-encoding: gzip\r\n");
598 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
600 ptr = ChrPtr(WCC->HBuf);
601 count = StrLength(WCC->HBuf);
606 client_write_ssl(WCC->HBuf);
607 client_write_ssl(WCC->WBuf);
615 write(2, "\033[34m", 5);
616 write(2, ptr, StrLength(WCC->WBuf));
617 write(2, "\033[30m", 5);
619 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
622 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
624 FD_SET(WCC->Hdr->http_sock, &wset);
625 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
626 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
631 if ((res = write(WCC->Hdr->http_sock,
634 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
642 ptr = ChrPtr(WCC->WBuf);
643 count = StrLength(WCC->WBuf);
648 write(2, "\033[34m", 5);
649 write(2, ptr, StrLength(WCC->WBuf));
650 write(2, "\033[30m", 5);
654 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
656 FD_SET(WCC->Hdr->http_sock, &wset);
657 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
658 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
663 if ((res = write(WCC->Hdr->http_sock,
666 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
674 return StrLength(WCC->WBuf);
679 * lingering_close() a`la Apache. see
680 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
682 int lingering_close(int fd)
687 struct timeval tv, start;
689 gettimeofday(&start, NULL);
693 gettimeofday(&tv, NULL);
694 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
695 tv.tv_usec = start.tv_usec - tv.tv_usec;
696 if (tv.tv_usec < 0) {
698 tv.tv_usec += 1000000;
702 i = select(fd + 1, &set, NULL, NULL, &tv);
703 } while (i == -1 && errno == EINTR);
708 i = read(fd, buf, sizeof buf);
709 } while (i != 0 && (i != -1 || errno == EINTR));
715 HttpNewModule_TCPSOCKETS
716 (ParsedHttpHdrs *httpreq)
719 httpreq->ReadBuf = NewStrBuf();
723 HttpDetachModule_TCPSOCKETS
724 (ParsedHttpHdrs *httpreq)
727 FlushStrBuf(httpreq->ReadBuf);
728 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
732 HttpDestroyModule_TCPSOCKETS
733 (ParsedHttpHdrs *httpreq)
736 FreeStrBuf(&httpreq->ReadBuf);
741 SessionNewModule_TCPSOCKETS
744 sess->CLineBuf = NewStrBuf();
745 sess->MigrateReadLineBuf = NewStrBuf();
749 SessionDestroyModule_TCPSOCKETS
752 FreeStrBuf(&sess->CLineBuf);
753 FreeStrBuf(&sess->ReadBuf);
754 FreeStrBuf(&sess->MigrateReadLineBuf);
755 if (sess->serv_sock > 0)
756 close(sess->serv_sock);