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)
461 const char *Error, *pch, *pchs;
462 int rlen, len, retval = 0;
467 if (StrLength(Hdr->ReadBuf) > 0) {
468 pchs = ChrPtr(Hdr->ReadBuf);
469 pch = strchr(pchs, '\n');
473 if (len > 0 && (*(pch - 1) == '\r') )
475 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
476 StrBufCutLeft(Hdr->ReadBuf, len + 1);
481 while (retval == 0) {
483 pchs = ChrPtr(Hdr->ReadBuf);
485 pch = strchr(pchs, '\n');
487 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
488 pchs = ChrPtr(Hdr->ReadBuf);
489 pch = strchr(pchs, '\n');
498 if ((retval > 0) && (pch != NULL)) {
501 if (len > 0 && (*(pch - 1) == '\r') )
503 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
504 StrBufCutLeft(Hdr->ReadBuf, len + 1);
513 return StrBufTCP_read_buffered_line_fast(Target,
523 * This is a generic function to set up a master socket for listening on
524 * a TCP port. The server shuts down if the bind fails.
526 * ip_addr IP address to bind
527 * port_number port number to bind
528 * queue_len number of incoming connections to allow in the queue
530 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
533 struct sockaddr_in sin;
536 memset(&sin, 0, sizeof(sin));
537 sin.sin_family = AF_INET;
538 if (ip_addr == NULL) {
539 sin.sin_addr.s_addr = INADDR_ANY;
541 sin.sin_addr.s_addr = inet_addr(ip_addr);
544 if (sin.sin_addr.s_addr == INADDR_NONE) {
545 sin.sin_addr.s_addr = INADDR_ANY;
548 if (port_number == 0) {
549 lprintf(1, "Cannot start: no port number specified.\n");
550 return (-WC_EXIT_BIND);
552 sin.sin_port = htons((u_short) port_number);
554 p = getprotobyname("tcp");
556 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
558 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
559 return (-WC_EXIT_BIND);
561 /* Set some socket options that make sense. */
563 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
566 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
567 there should be a preceding F_GETFL
568 and a bitwise OR with the previous
572 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
573 lprintf(1, "Can't bind: %s\n", strerror(errno));
574 return (-WC_EXIT_BIND);
576 if (listen(s, queue_len) < 0) {
577 lprintf(1, "Can't listen: %s\n", strerror(errno));
578 return (-WC_EXIT_BIND);
586 * Create a Unix domain socket and listen on it
587 * sockpath - file name of the unix domain socket
588 * queue_len - Number of incoming connections to allow in the queue
590 int ig_uds_server(char *sockpath, int queue_len)
592 struct sockaddr_un addr;
595 int actual_queue_len;
597 actual_queue_len = queue_len;
598 if (actual_queue_len < 5) actual_queue_len = 5;
600 i = unlink(sockpath);
601 if ((i != 0) && (errno != ENOENT)) {
602 lprintf(1, "webcit: can't unlink %s: %s\n",
603 sockpath, strerror(errno));
604 return (-WC_EXIT_BIND);
607 memset(&addr, 0, sizeof(addr));
608 addr.sun_family = AF_UNIX;
609 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
611 s = socket(AF_UNIX, SOCK_STREAM, 0);
613 lprintf(1, "webcit: Can't create a socket: %s\n",
615 return (-WC_EXIT_BIND);
618 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
619 lprintf(1, "webcit: Can't bind: %s\n",
621 return (-WC_EXIT_BIND);
624 if (listen(s, actual_queue_len) < 0) {
625 lprintf(1, "webcit: Can't listen: %s\n",
627 return (-WC_EXIT_BIND);
630 chmod(sockpath, 0777);
638 * Read data from the client socket.
640 * sock socket fd to read from
641 * buf buffer to read into
642 * bytes number of bytes to read
643 * timeout Number of seconds to wait before timing out
645 * Possible return values:
646 * 1 Requested number of bytes has been read.
647 * 0 Request timed out.
648 * -1 Connection is broken, or other error.
650 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
660 baselen = StrLength(Target);
662 if (Hdr->Pos == NULL)
663 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
664 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
666 if (bytes < bufremain)
668 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
669 StrBufCutLeft(Hdr->ReadBuf, bufremain);
671 if (bytes > bufremain)
673 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
675 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
677 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
679 write(2, "\033[32m", 5);
680 write(2, buf, bytes);
681 write(2, "\033[30m", 5);
686 lprintf(2, "client_read_ssl() failed\n");
695 retval = StrBufReadBLOBBuffered(Target,
704 lprintf(2, "client_read() failed: %s\n",
710 write(2, "\033[32m", 5);
711 write(2, buf, bytes);
712 write(2, "\033[30m", 5);
719 * Begin buffering HTTP output so we can transmit it all in one write operation later.
721 void begin_burst(void)
723 if (WC->WBuf == NULL) {
724 WC->WBuf = NewStrBufPlain(NULL, 32768);
730 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
735 const char *ptr, *eptr;
741 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
743 if (CompressBuffer(WCC->WBuf) > 0)
744 hprintf("Content-encoding: gzip\r\n");
746 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
751 if (WCC->Hdr->HR.prohibit_caching)
752 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
753 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
755 ptr = ChrPtr(WCC->HBuf);
756 count = StrLength(WCC->HBuf);
761 client_write_ssl(WCC->HBuf);
762 client_write_ssl(WCC->WBuf);
770 write(2, "\033[34m", 5);
771 write(2, ptr, StrLength(WCC->WBuf));
772 write(2, "\033[30m", 5);
774 if (WCC->Hdr->http_sock == -1)
776 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
778 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
779 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
781 FD_SET(WCC->Hdr->http_sock, &wset);
782 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
783 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
788 if ((WCC->Hdr->http_sock == -1) ||
789 (res = write(WCC->Hdr->http_sock,
792 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
800 ptr = ChrPtr(WCC->WBuf);
801 count = StrLength(WCC->WBuf);
806 write(2, "\033[34m", 5);
807 write(2, ptr, StrLength(WCC->WBuf));
808 write(2, "\033[30m", 5);
811 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
812 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
814 FD_SET(WCC->Hdr->http_sock, &wset);
815 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
816 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
821 if ((WCC->Hdr->http_sock == -1) ||
822 (res = write(WCC->Hdr->http_sock,
825 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
833 return StrLength(WCC->WBuf);
838 * lingering_close() a`la Apache. see
839 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
841 int lingering_close(int fd)
846 struct timeval tv, start;
848 gettimeofday(&start, NULL);
854 gettimeofday(&tv, NULL);
855 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
856 tv.tv_usec = start.tv_usec - tv.tv_usec;
857 if (tv.tv_usec < 0) {
859 tv.tv_usec += 1000000;
863 i = select(fd + 1, &set, NULL, NULL, &tv);
864 } while (i == -1 && errno == EINTR);
869 i = read(fd, buf, sizeof buf);
870 } while (i != 0 && (i != -1 || errno == EINTR));
876 HttpNewModule_TCPSOCKETS
877 (ParsedHttpHdrs *httpreq)
880 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
884 HttpDetachModule_TCPSOCKETS
885 (ParsedHttpHdrs *httpreq)
888 FlushStrBuf(httpreq->ReadBuf);
889 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
893 HttpDestroyModule_TCPSOCKETS
894 (ParsedHttpHdrs *httpreq)
897 FreeStrBuf(&httpreq->ReadBuf);
902 SessionNewModule_TCPSOCKETS
905 sess->CLineBuf = NewStrBuf();
906 sess->MigrateReadLineBuf = NewStrBuf();
910 SessionDestroyModule_TCPSOCKETS
913 FreeStrBuf(&sess->CLineBuf);
914 FreeStrBuf(&sess->ReadBuf);
915 sess->ReadPos = NULL;
916 FreeStrBuf(&sess->MigrateReadLineBuf);
917 if (sess->serv_sock > 0)
918 close(sess->serv_sock);