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;
169 rc = StrBufTCP_read_buffered_line_fast(buf,
177 lprintf(1, "Server connection broken: %s\n",
180 WCC->serv_sock = (-1);
188 if (WCC->ReadPos != NULL)
189 pos = WCC->ReadPos - ChrPtr(buf);
190 lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
196 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
202 rc = StrBufReadBLOBBuffered(buf,
212 lprintf(1, "Server connection broken: %s\n",
215 WCC->serv_sock = (-1);
221 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
227 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
234 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
237 lprintf(1, "Server connection broken: %s\n",
240 WCC->serv_sock = (-1);
246 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
253 * send binary to server
254 * buf the buffer to write to citadel server
255 * nbytes how many bytes to send to citadel server
257 void serv_write(const char *buf, int nbytes)
260 int bytes_written = 0;
263 FlushStrBuf(WCC->ReadBuf);
265 while (bytes_written < nbytes) {
266 retval = write(WCC->serv_sock, &buf[bytes_written],
267 nbytes - bytes_written);
269 const char *ErrStr = strerror(errno);
270 lprintf(1, "Server connection broken: %s\n",
272 close(WCC->serv_sock);
273 WCC->serv_sock = (-1);
278 bytes_written = bytes_written + retval;
284 * send line to server
285 * string the line to send to the citadel server
287 void serv_puts(const char *string)
291 lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
293 FlushStrBuf(WCC->ReadBuf);
296 serv_write(string, strlen(string));
301 * send line to server
302 * string the line to send to the citadel server
304 void serv_putbuf(const StrBuf *string)
308 lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
310 FlushStrBuf(WCC->ReadBuf);
313 serv_write(ChrPtr(string), StrLength(string));
319 * convenience function to send stuff to the server
320 * format the formatstring
321 * ... the entities to insert into format
323 void serv_printf(const char *format,...)
330 FlushStrBuf(WCC->ReadBuf);
333 va_start(arg_ptr, format);
334 vsnprintf(buf, sizeof buf, format, arg_ptr);
340 serv_write(buf, len);
342 lprintf(9, ">>>%s", buf);
349 * Read binary data from server into memory using a series of
350 * server READ commands.
351 * \return the read content as StrBuf
353 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
357 size_t thisblock = 0;
364 serv_printf("READ %d|%d", 0, total_len);
365 if (StrBuf_ServGetln(Buf) > 0)
372 if (GetServerStatus(Buf, NULL) == 6)
374 StrBufCutLeft(Buf, 4);
375 thisblock = StrTol(Buf);
376 if (WCC->serv_sock==-1) {
381 pch = ChrPtr(WCC->ReadBuf);
382 YetRead = WCC->ReadPos - pch;
387 StillThere = StrLength(WCC->ReadBuf) -
393 total_len -= StillThere;
395 FlushStrBuf(WCC->ReadBuf);
400 rc = StrBufReadBLOB(Ret,
407 lprintf(1, "Server connection broken: %s\n",
410 WCC->serv_sock = (-1);
416 return StrLength(Ret);
419 return StrLength(Ret);
425 else while ((WCC->serv_sock!=-1) &&
426 (bytes < total_len)) {
428 if ((total_len - bytes) < thisblock) {
429 thisblock = total_len - bytes;
430 if (thisblock == 0) {
435 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
436 if (StrBuf_ServGetln(Buf) > 0)
438 if (GetServerStatus(Buf, NULL) == 6)
440 StrBufCutLeft(Buf, 4);
441 thisblock = StrTol(Buf);
442 if (WCC->serv_sock==-1) {
446 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
450 lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
455 return StrLength(Ret);
459 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
463 const char *pch, *pchs;
464 int rlen, len, retval = 0;
468 if (StrLength(Hdr->ReadBuf) > 0) {
469 pchs = ChrPtr(Hdr->ReadBuf);
470 pch = strchr(pchs, '\n');
474 if (len > 0 && (*(pch - 1) == '\r') )
476 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
477 StrBufCutLeft(Hdr->ReadBuf, len + 1);
482 while (retval == 0) {
484 pchs = ChrPtr(Hdr->ReadBuf);
486 pch = strchr(pchs, '\n');
488 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
489 pchs = ChrPtr(Hdr->ReadBuf);
490 pch = strchr(pchs, '\n');
499 if ((retval > 0) && (pch != NULL)) {
502 if (len > 0 && (*(pch - 1) == '\r') )
504 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
505 StrBufCutLeft(Hdr->ReadBuf, len + 1);
514 return StrBufTCP_read_buffered_line_fast(Target,
524 * This is a generic function to set up a master socket for listening on
525 * a TCP port. The server shuts down if the bind fails.
527 * ip_addr IP address to bind
528 * port_number port number to bind
529 * queue_len number of incoming connections to allow in the queue
531 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
534 struct sockaddr_in sin;
537 memset(&sin, 0, sizeof(sin));
538 sin.sin_family = AF_INET;
539 if (ip_addr == NULL) {
540 sin.sin_addr.s_addr = INADDR_ANY;
542 sin.sin_addr.s_addr = inet_addr(ip_addr);
545 if (sin.sin_addr.s_addr == INADDR_NONE) {
546 sin.sin_addr.s_addr = INADDR_ANY;
549 if (port_number == 0) {
550 lprintf(1, "Cannot start: no port number specified.\n");
551 return (-WC_EXIT_BIND);
553 sin.sin_port = htons((u_short) port_number);
555 p = getprotobyname("tcp");
557 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
559 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
560 return (-WC_EXIT_BIND);
562 /* Set some socket options that make sense. */
564 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
567 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
568 there should be a preceding F_GETFL
569 and a bitwise OR with the previous
573 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
574 lprintf(1, "Can't bind: %s\n", strerror(errno));
575 return (-WC_EXIT_BIND);
577 if (listen(s, queue_len) < 0) {
578 lprintf(1, "Can't listen: %s\n", strerror(errno));
579 return (-WC_EXIT_BIND);
587 * Create a Unix domain socket and listen on it
588 * sockpath - file name of the unix domain socket
589 * queue_len - Number of incoming connections to allow in the queue
591 int ig_uds_server(char *sockpath, int queue_len)
593 struct sockaddr_un addr;
596 int actual_queue_len;
598 actual_queue_len = queue_len;
599 if (actual_queue_len < 5) actual_queue_len = 5;
601 i = unlink(sockpath);
602 if ((i != 0) && (errno != ENOENT)) {
603 lprintf(1, "webcit: can't unlink %s: %s\n",
604 sockpath, strerror(errno));
605 return (-WC_EXIT_BIND);
608 memset(&addr, 0, sizeof(addr));
609 addr.sun_family = AF_UNIX;
610 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
612 s = socket(AF_UNIX, SOCK_STREAM, 0);
614 lprintf(1, "webcit: Can't create a socket: %s\n",
616 return (-WC_EXIT_BIND);
619 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
620 lprintf(1, "webcit: Can't bind: %s\n",
622 return (-WC_EXIT_BIND);
625 if (listen(s, actual_queue_len) < 0) {
626 lprintf(1, "webcit: Can't listen: %s\n",
628 return (-WC_EXIT_BIND);
631 chmod(sockpath, 0777);
639 * Read data from the client socket.
641 * sock socket fd to read from
642 * buf buffer to read into
643 * bytes number of bytes to read
644 * timeout Number of seconds to wait before timing out
646 * Possible return values:
647 * 1 Requested number of bytes has been read.
648 * 0 Request timed out.
649 * -1 Connection is broken, or other error.
651 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
661 baselen = StrLength(Target);
663 if (Hdr->Pos == NULL)
664 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
665 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
667 if (bytes < bufremain)
669 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
670 StrBufCutLeft(Hdr->ReadBuf, bufremain);
672 if (bytes > bufremain)
674 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
676 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
678 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
680 write(2, "\033[32m", 5);
681 write(2, buf, bytes);
682 write(2, "\033[30m", 5);
687 lprintf(2, "client_read_ssl() failed\n");
696 retval = StrBufReadBLOBBuffered(Target,
705 lprintf(2, "client_read() failed: %s\n",
711 write(2, "\033[32m", 5);
712 write(2, buf, bytes);
713 write(2, "\033[30m", 5);
720 * Begin buffering HTTP output so we can transmit it all in one write operation later.
722 void begin_burst(void)
724 if (WC->WBuf == NULL) {
725 WC->WBuf = NewStrBufPlain(NULL, 32768);
731 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
736 const char *ptr, *eptr;
742 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
744 if (CompressBuffer(WCC->WBuf) > 0)
745 hprintf("Content-encoding: gzip\r\n");
747 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
752 if (WCC->Hdr->HR.prohibit_caching)
753 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
754 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
756 ptr = ChrPtr(WCC->HBuf);
757 count = StrLength(WCC->HBuf);
762 client_write_ssl(WCC->HBuf);
763 client_write_ssl(WCC->WBuf);
771 write(2, "\033[34m", 5);
772 write(2, ptr, StrLength(WCC->WBuf));
773 write(2, "\033[30m", 5);
775 if (WCC->Hdr->http_sock == -1)
777 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
779 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
780 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
782 FD_SET(WCC->Hdr->http_sock, &wset);
783 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
784 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
789 if ((WCC->Hdr->http_sock == -1) ||
790 (res = write(WCC->Hdr->http_sock,
793 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
801 ptr = ChrPtr(WCC->WBuf);
802 count = StrLength(WCC->WBuf);
807 write(2, "\033[34m", 5);
808 write(2, ptr, StrLength(WCC->WBuf));
809 write(2, "\033[30m", 5);
812 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
813 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
815 FD_SET(WCC->Hdr->http_sock, &wset);
816 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
817 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
822 if ((WCC->Hdr->http_sock == -1) ||
823 (res = write(WCC->Hdr->http_sock,
826 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
834 return StrLength(WCC->WBuf);
839 * lingering_close() a`la Apache. see
840 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
842 int lingering_close(int fd)
847 struct timeval tv, start;
849 gettimeofday(&start, NULL);
855 gettimeofday(&tv, NULL);
856 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
857 tv.tv_usec = start.tv_usec - tv.tv_usec;
858 if (tv.tv_usec < 0) {
860 tv.tv_usec += 1000000;
864 i = select(fd + 1, &set, NULL, NULL, &tv);
865 } while (i == -1 && errno == EINTR);
870 i = read(fd, buf, sizeof buf);
871 } while (i != 0 && (i != -1 || errno == EINTR));
877 HttpNewModule_TCPSOCKETS
878 (ParsedHttpHdrs *httpreq)
881 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
885 HttpDetachModule_TCPSOCKETS
886 (ParsedHttpHdrs *httpreq)
889 FlushStrBuf(httpreq->ReadBuf);
890 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
894 HttpDestroyModule_TCPSOCKETS
895 (ParsedHttpHdrs *httpreq)
898 FreeStrBuf(&httpreq->ReadBuf);
903 SessionNewModule_TCPSOCKETS
906 sess->CLineBuf = NewStrBuf();
907 sess->MigrateReadLineBuf = NewStrBuf();
911 SessionDestroyModule_TCPSOCKETS
914 FreeStrBuf(&sess->CLineBuf);
915 FreeStrBuf(&sess->ReadBuf);
916 sess->ReadPos = NULL;
917 FreeStrBuf(&sess->MigrateReadLineBuf);
918 if (sess->serv_sock > 0)
919 close(sess->serv_sock);