6 * Uncomment this to log all communications with the Citadel server
12 #include "webserver.h"
14 extern int DisableGzip;
15 long MaxRead = -1; /* should we do READ scattered or all at once? */
18 * register the timeout
20 RETSIGTYPE timeout(int signum)
22 lprintf(1, "Connection timed out; unable to reach citserver\n");
23 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
28 * Connect a unix domain socket
29 * sockpath where to open a unix domain socket
31 int uds_connectsock(char *sockpath)
33 struct sockaddr_un addr;
36 memset(&addr, 0, sizeof(addr));
37 addr.sun_family = AF_UNIX;
38 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
40 s = socket(AF_UNIX, SOCK_STREAM, 0);
42 lprintf(1, "Can't create socket[%s]: %s\n",
48 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
49 lprintf(1, "Can't connect [%s]: %s\n",
61 * Connect a TCP/IP socket
62 * host the host to connect to
63 * service the service on the host to call
65 int tcp_connectsock(char *host, char *service)
71 struct sockaddr_in sin;
74 memset(&sin, 0, sizeof(sin));
75 sin.sin_family = AF_INET;
77 pse = getservbyname(service, "tcp");
79 sin.sin_port = pse->s_port;
80 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
81 lprintf(1, "Can't get %s service entry\n", service);
84 phe = gethostbyname(host);
86 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
87 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
88 lprintf(1, "Can't get %s host entry: %s\n",
89 host, strerror(errno));
92 if ((ppe = getprotobyname("tcp")) == 0) {
93 lprintf(1, "Can't get TCP protocol entry: %s\n",
98 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
100 lprintf(1, "Can't create socket: %s\n", strerror(errno));
104 fdflags = fcntl(s, F_GETFL);
106 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
107 host, service, strerror(errno));
108 fdflags = fdflags | O_NONBLOCK;
109 if (fcntl(s, F_SETFD, fdflags) < 0)
110 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
111 host, service, strerror(errno));
113 signal(SIGALRM, timeout);
116 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
117 lprintf(1, "Can't connect to %s.%s: %s\n",
118 host, service, strerror(errno));
123 signal(SIGALRM, SIG_IGN);
125 fdflags = fcntl(s, F_GETFL);
127 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
128 host, service, strerror(errno));
129 fdflags = fdflags | O_NONBLOCK;
130 if (fcntl(s, F_SETFD, fdflags) < 0)
131 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
132 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, &ErrStr);
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 const char *ErrStr = strerror(errno);
250 lprintf(1, "Server connection broken: %s\n",
252 close(WCC->serv_sock);
253 WCC->serv_sock = (-1);
258 bytes_written = bytes_written + retval;
264 * send line to server
265 * string the line to send to the citadel server
267 void serv_puts(const char *string)
271 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
273 FlushStrBuf(WCC->ReadBuf);
276 serv_write(string, strlen(string));
281 * send line to server
282 * string the line to send to the citadel server
284 void serv_putbuf(const StrBuf *string)
288 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
290 FlushStrBuf(WCC->ReadBuf);
293 serv_write(ChrPtr(string), StrLength(string));
299 * convenience function to send stuff to the server
300 * format the formatstring
301 * ... the entities to insert into format
303 void serv_printf(const char *format,...)
310 FlushStrBuf(WCC->ReadBuf);
313 va_start(arg_ptr, format);
314 vsnprintf(buf, sizeof buf, format, arg_ptr);
320 serv_write(buf, len);
322 lprintf(9, "<%s", buf);
329 * Read binary data from server into memory using a series of
330 * server READ commands.
331 * \return the read content as StrBuf
333 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
337 size_t thisblock = 0;
344 serv_printf("READ %d|%d", 0, total_len);
345 if (StrBuf_ServGetln(Buf) > 0)
352 if (GetServerStatus(Buf, NULL) == 6)
354 StrBufCutLeft(Buf, 4);
355 thisblock = StrTol(Buf);
356 if (WCC->serv_sock==-1) {
361 pch = ChrPtr(WCC->ReadBuf);
362 YetRead = WCC->ReadPos - pch;
367 StillThere = StrLength(WCC->ReadBuf) -
373 total_len -= StillThere;
375 FlushStrBuf(WCC->ReadBuf);
380 rc = StrBufReadBLOB(Ret,
387 lprintf(1, "Server connection broken: %s\n",
390 WCC->serv_sock = (-1);
396 return StrLength(Ret);
399 return StrLength(Ret);
405 else while ((WCC->serv_sock!=-1) &&
406 (bytes < total_len)) {
408 if ((total_len - bytes) < thisblock) {
409 thisblock = total_len - bytes;
410 if (thisblock == 0) {
415 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
416 if (StrBuf_ServGetln(Buf) > 0)
418 if (GetServerStatus(Buf, NULL) == 6)
420 StrBufCutLeft(Buf, 4);
421 thisblock = StrTol(Buf);
422 if (WCC->serv_sock==-1) {
426 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
430 lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
435 return StrLength(Ret);
439 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
441 const char *Error, *pch, *pchs;
442 int rlen, len, retval = 0;
447 if (StrLength(Hdr->ReadBuf) > 0) {
448 pchs = ChrPtr(Hdr->ReadBuf);
449 pch = strchr(pchs, '\n');
453 if (len > 0 && (*(pch - 1) == '\r') )
455 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
456 StrBufCutLeft(Hdr->ReadBuf, len + 1);
461 while (retval == 0) {
463 pchs = ChrPtr(Hdr->ReadBuf);
465 pch = strchr(pchs, '\n');
467 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
468 pchs = ChrPtr(Hdr->ReadBuf);
469 pch = strchr(pchs, '\n');
478 if ((retval > 0) && (pch != NULL)) {
481 if (len > 0 && (*(pch - 1) == '\r') )
483 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
484 StrBufCutLeft(Hdr->ReadBuf, len + 1);
493 return StrBufTCP_read_buffered_line_fast(Target,
503 * This is a generic function to set up a master socket for listening on
504 * a TCP port. The server shuts down if the bind fails.
506 * ip_addr IP address to bind
507 * port_number port number to bind
508 * queue_len number of incoming connections to allow in the queue
510 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
513 struct sockaddr_in sin;
516 memset(&sin, 0, sizeof(sin));
517 sin.sin_family = AF_INET;
518 if (ip_addr == NULL) {
519 sin.sin_addr.s_addr = INADDR_ANY;
521 sin.sin_addr.s_addr = inet_addr(ip_addr);
524 if (sin.sin_addr.s_addr == INADDR_NONE) {
525 sin.sin_addr.s_addr = INADDR_ANY;
528 if (port_number == 0) {
529 lprintf(1, "Cannot start: no port number specified.\n");
530 return (-WC_EXIT_BIND);
532 sin.sin_port = htons((u_short) port_number);
534 p = getprotobyname("tcp");
536 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
538 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
539 return (-WC_EXIT_BIND);
541 /* Set some socket options that make sense. */
543 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
546 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
547 there should be a preceding F_GETFL
548 and a bitwise OR with the previous
552 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
553 lprintf(1, "Can't bind: %s\n", strerror(errno));
554 return (-WC_EXIT_BIND);
556 if (listen(s, queue_len) < 0) {
557 lprintf(1, "Can't listen: %s\n", strerror(errno));
558 return (-WC_EXIT_BIND);
566 * Create a Unix domain socket and listen on it
567 * sockpath - file name of the unix domain socket
568 * queue_len - Number of incoming connections to allow in the queue
570 int ig_uds_server(char *sockpath, int queue_len)
572 struct sockaddr_un addr;
575 int actual_queue_len;
577 actual_queue_len = queue_len;
578 if (actual_queue_len < 5) actual_queue_len = 5;
580 i = unlink(sockpath);
581 if ((i != 0) && (errno != ENOENT)) {
582 lprintf(1, "webcit: can't unlink %s: %s\n",
583 sockpath, strerror(errno));
584 return (-WC_EXIT_BIND);
587 memset(&addr, 0, sizeof(addr));
588 addr.sun_family = AF_UNIX;
589 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
591 s = socket(AF_UNIX, SOCK_STREAM, 0);
593 lprintf(1, "webcit: Can't create a socket: %s\n",
595 return (-WC_EXIT_BIND);
598 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
599 lprintf(1, "webcit: Can't bind: %s\n",
601 return (-WC_EXIT_BIND);
604 if (listen(s, actual_queue_len) < 0) {
605 lprintf(1, "webcit: Can't listen: %s\n",
607 return (-WC_EXIT_BIND);
610 chmod(sockpath, 0777);
618 * Read data from the client socket.
620 * sock socket fd to read from
621 * buf buffer to read into
622 * bytes number of bytes to read
623 * timeout Number of seconds to wait before timing out
625 * Possible return values:
626 * 1 Requested number of bytes has been read.
627 * 0 Request timed out.
628 * -1 Connection is broken, or other error.
630 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
640 baselen = StrLength(Target);
642 if (Hdr->Pos == NULL)
643 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
644 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
646 if (bytes < bufremain)
648 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
649 StrBufCutLeft(Hdr->ReadBuf, bufremain);
651 if (bytes > bufremain)
653 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
655 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
657 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
659 write(2, "\033[32m", 5);
660 write(2, buf, bytes);
661 write(2, "\033[30m", 5);
666 lprintf(2, "client_read_ssl() failed\n");
675 retval = StrBufReadBLOBBuffered(Target,
684 lprintf(2, "client_read() failed: %s\n",
690 write(2, "\033[32m", 5);
691 write(2, buf, bytes);
692 write(2, "\033[30m", 5);
699 * Begin buffering HTTP output so we can transmit it all in one write operation later.
701 void begin_burst(void)
703 if (WC->WBuf == NULL) {
704 WC->WBuf = NewStrBufPlain(NULL, 32768);
710 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
715 const char *ptr, *eptr;
721 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
723 if (CompressBuffer(WCC->WBuf) > 0)
724 hprintf("Content-encoding: gzip\r\n");
726 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
731 if (WCC->Hdr->HR.prohibit_caching)
732 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
733 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
735 ptr = ChrPtr(WCC->HBuf);
736 count = StrLength(WCC->HBuf);
741 client_write_ssl(WCC->HBuf);
742 client_write_ssl(WCC->WBuf);
750 write(2, "\033[34m", 5);
751 write(2, ptr, StrLength(WCC->WBuf));
752 write(2, "\033[30m", 5);
754 if (WCC->Hdr->http_sock == -1)
756 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
758 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
759 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
761 FD_SET(WCC->Hdr->http_sock, &wset);
762 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
763 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
768 if ((WCC->Hdr->http_sock == -1) ||
769 (res = write(WCC->Hdr->http_sock,
772 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
780 ptr = ChrPtr(WCC->WBuf);
781 count = StrLength(WCC->WBuf);
786 write(2, "\033[34m", 5);
787 write(2, ptr, StrLength(WCC->WBuf));
788 write(2, "\033[30m", 5);
791 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
792 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
794 FD_SET(WCC->Hdr->http_sock, &wset);
795 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
796 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
801 if ((WCC->Hdr->http_sock == -1) ||
802 (res = write(WCC->Hdr->http_sock,
805 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
813 return StrLength(WCC->WBuf);
818 * lingering_close() a`la Apache. see
819 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
821 int lingering_close(int fd)
826 struct timeval tv, start;
828 gettimeofday(&start, NULL);
834 gettimeofday(&tv, NULL);
835 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
836 tv.tv_usec = start.tv_usec - tv.tv_usec;
837 if (tv.tv_usec < 0) {
839 tv.tv_usec += 1000000;
843 i = select(fd + 1, &set, NULL, NULL, &tv);
844 } while (i == -1 && errno == EINTR);
849 i = read(fd, buf, sizeof buf);
850 } while (i != 0 && (i != -1 || errno == EINTR));
856 HttpNewModule_TCPSOCKETS
857 (ParsedHttpHdrs *httpreq)
860 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
864 HttpDetachModule_TCPSOCKETS
865 (ParsedHttpHdrs *httpreq)
868 FlushStrBuf(httpreq->ReadBuf);
869 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
873 HttpDestroyModule_TCPSOCKETS
874 (ParsedHttpHdrs *httpreq)
877 FreeStrBuf(&httpreq->ReadBuf);
882 SessionNewModule_TCPSOCKETS
885 sess->CLineBuf = NewStrBuf();
886 sess->MigrateReadLineBuf = NewStrBuf();
890 SessionDestroyModule_TCPSOCKETS
893 FreeStrBuf(&sess->CLineBuf);
894 FreeStrBuf(&sess->ReadBuf);
895 sess->ReadPos = NULL;
896 FreeStrBuf(&sess->MigrateReadLineBuf);
897 if (sess->serv_sock > 0)
898 close(sess->serv_sock);