4 * Copyright (c) 1987-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Uncomment this to log all communications with the Citadel server
28 #include "webserver.h"
30 extern int DisableGzip;
31 long MaxRead = -1; /* should we do READ scattered or all at once? */
34 * register the timeout
36 RETSIGTYPE timeout(int signum)
38 lprintf(1, "Connection timed out; unable to reach citserver\n");
39 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
44 * Connect a unix domain socket
45 * sockpath where to open a unix domain socket
47 int uds_connectsock(char *sockpath)
49 struct sockaddr_un addr;
52 memset(&addr, 0, sizeof(addr));
53 addr.sun_family = AF_UNIX;
54 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
56 s = socket(AF_UNIX, SOCK_STREAM, 0);
58 lprintf(1, "Can't create socket[%s]: %s\n",
64 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
65 lprintf(1, "Can't connect [%s]: %s\n",
77 * Connect a TCP/IP socket
78 * host the host to connect to
79 * service the service on the host to call
81 int tcp_connectsock(char *host, char *service)
87 struct sockaddr_in sin;
90 memset(&sin, 0, sizeof(sin));
91 sin.sin_family = AF_INET;
93 pse = getservbyname(service, "tcp");
95 sin.sin_port = pse->s_port;
96 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
97 lprintf(1, "Can't get %s service entry\n", service);
100 phe = gethostbyname(host);
102 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
103 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
104 lprintf(1, "Can't get %s host entry: %s\n",
105 host, strerror(errno));
108 if ((ppe = getprotobyname("tcp")) == 0) {
109 lprintf(1, "Can't get TCP protocol entry: %s\n",
114 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
116 lprintf(1, "Can't create socket: %s\n", strerror(errno));
120 fdflags = fcntl(s, F_GETFL);
122 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
123 host, service, strerror(errno));
124 fdflags = fdflags | O_NONBLOCK;
125 if (fcntl(s, F_SETFD, fdflags) < 0)
126 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
127 host, service, strerror(errno));
129 signal(SIGALRM, timeout);
132 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
133 lprintf(1, "Can't connect to %s.%s: %s\n",
134 host, service, strerror(errno));
139 signal(SIGALRM, SIG_IGN);
141 fdflags = fcntl(s, F_GETFL);
143 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
144 host, service, strerror(errno));
145 fdflags = fdflags | O_NONBLOCK;
146 if (fcntl(s, F_SETFD, fdflags) < 0)
147 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
148 host, service, strerror(errno));
156 * input string from pipe
158 int serv_getln(char *strbuf, int bufsize)
164 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
165 len = StrLength(WCC->MigrateReadLineBuf);
168 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
169 FlushStrBuf(WCC->MigrateReadLineBuf);
172 lprintf(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
178 int StrBuf_ServGetln(StrBuf *buf)
181 const char *ErrStr = NULL;
185 rc = StrBufTCP_read_buffered_line_fast(buf,
193 lprintf(1, "Server connection broken: %s\n",
196 WCC->serv_sock = (-1);
204 if (WCC->ReadPos != NULL)
205 pos = WCC->ReadPos - ChrPtr(buf);
206 lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
212 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
218 rc = StrBufReadBLOBBuffered(buf,
228 lprintf(1, "Server connection broken: %s\n",
231 WCC->serv_sock = (-1);
237 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
243 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
250 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
253 lprintf(1, "Server connection broken: %s\n",
256 WCC->serv_sock = (-1);
262 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
269 * send binary to server
270 * buf the buffer to write to citadel server
271 * nbytes how many bytes to send to citadel server
273 void serv_write(const char *buf, int nbytes)
276 int bytes_written = 0;
279 FlushStrBuf(WCC->ReadBuf);
281 while (bytes_written < nbytes) {
282 retval = write(WCC->serv_sock, &buf[bytes_written],
283 nbytes - bytes_written);
285 const char *ErrStr = strerror(errno);
286 lprintf(1, "Server connection broken: %s\n",
288 close(WCC->serv_sock);
289 WCC->serv_sock = (-1);
294 bytes_written = bytes_written + retval;
300 * send line to server
301 * string the line to send to the citadel server
303 void serv_puts(const char *string)
307 lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
309 FlushStrBuf(WCC->ReadBuf);
312 serv_write(string, strlen(string));
317 * send line to server
318 * string the line to send to the citadel server
320 void serv_putbuf(const StrBuf *string)
324 lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
326 FlushStrBuf(WCC->ReadBuf);
329 serv_write(ChrPtr(string), StrLength(string));
335 * convenience function to send stuff to the server
336 * format the formatstring
337 * ... the entities to insert into format
339 void serv_printf(const char *format,...)
346 FlushStrBuf(WCC->ReadBuf);
349 va_start(arg_ptr, format);
350 vsnprintf(buf, sizeof buf, format, arg_ptr);
356 serv_write(buf, len);
358 lprintf(9, ">>>%s", buf);
365 * Read binary data from server into memory using a series of
366 * server READ commands.
367 * \return the read content as StrBuf
369 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
373 size_t thisblock = 0;
380 serv_printf("READ %d|%d", 0, total_len);
381 if (StrBuf_ServGetln(Buf) > 0)
388 if (GetServerStatus(Buf, NULL) == 6)
390 StrBufCutLeft(Buf, 4);
391 thisblock = StrTol(Buf);
392 if (WCC->serv_sock==-1) {
397 pch = ChrPtr(WCC->ReadBuf);
398 YetRead = WCC->ReadPos - pch;
403 StillThere = StrLength(WCC->ReadBuf) -
409 total_len -= StillThere;
411 FlushStrBuf(WCC->ReadBuf);
416 rc = StrBufReadBLOB(Ret,
423 lprintf(1, "Server connection broken: %s\n",
426 WCC->serv_sock = (-1);
432 return StrLength(Ret);
435 return StrLength(Ret);
441 else while ((WCC->serv_sock!=-1) &&
442 (bytes < total_len)) {
444 if ((total_len - bytes) < thisblock) {
445 thisblock = total_len - bytes;
446 if (thisblock == 0) {
451 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
452 if (StrBuf_ServGetln(Buf) > 0)
454 if (GetServerStatus(Buf, NULL) == 6)
456 StrBufCutLeft(Buf, 4);
457 thisblock = StrTol(Buf);
458 if (WCC->serv_sock==-1) {
462 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
466 lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
471 return StrLength(Ret);
475 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
479 const char *pch, *pchs;
480 int rlen, len, retval = 0;
484 if (StrLength(Hdr->ReadBuf) > 0) {
485 pchs = ChrPtr(Hdr->ReadBuf);
486 pch = strchr(pchs, '\n');
490 if (len > 0 && (*(pch - 1) == '\r') )
492 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
493 StrBufCutLeft(Hdr->ReadBuf, len + 1);
498 while (retval == 0) {
500 pchs = ChrPtr(Hdr->ReadBuf);
502 pch = strchr(pchs, '\n');
504 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
505 pchs = ChrPtr(Hdr->ReadBuf);
506 pch = strchr(pchs, '\n');
515 if ((retval > 0) && (pch != NULL)) {
518 if (len > 0 && (*(pch - 1) == '\r') )
520 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
521 StrBufCutLeft(Hdr->ReadBuf, len + 1);
530 return StrBufTCP_read_buffered_line_fast(Target,
542 * This is a generic function to set up a master socket for listening on
543 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
545 * ip_addr IP address to bind
546 * port_number port number to bind
547 * queue_len number of incoming connections to allow in the queue
549 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
552 struct sockaddr_in6 sin;
555 memset(&sin, 0, sizeof(sin));
556 sin.sin6_family = AF_INET6;
558 if ((ip_addr == NULL) || (IsEmptyStr(ip_addr)) || (!strcmp(ip_addr, "0.0.0.0"))) {
559 sin.sin6_addr = in6addr_any;
562 if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) {
563 snprintf(bind_to, sizeof bind_to, "::ffff:%s", ip_addr);
566 safestrncpy(bind_to, ip_addr, sizeof bind_to);
568 if (inet_pton(AF_INET6, bind_to, &sin.sin6_addr) <= 0) {
569 lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
570 return (-WC_EXIT_BIND);
574 if (port_number == 0) {
575 lprintf(1, "Cannot start: no port number specified.\n");
576 return (-WC_EXIT_BIND);
578 sin.sin6_port = htons((u_short) port_number);
580 p = getprotobyname("tcp");
582 s = socket(PF_INET6, SOCK_STREAM, (p->p_proto));
584 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
585 return (-WC_EXIT_BIND);
587 /* Set some socket options that make sense. */
589 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
592 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
593 there should be a preceding F_GETFL
594 and a bitwise OR with the previous
598 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
599 lprintf(1, "Can't bind: %s\n", strerror(errno));
600 return (-WC_EXIT_BIND);
602 if (listen(s, queue_len) < 0) {
603 lprintf(1, "Can't listen: %s\n", strerror(errno));
604 return (-WC_EXIT_BIND);
609 #else /* CTDL_IPV6 */
612 * This is a generic function to set up a master socket for listening on
613 * a TCP port. The server shuts down if the bind fails.
615 * ip_addr IP address to bind
616 * port_number port number to bind
617 * queue_len number of incoming connections to allow in the queue
619 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
622 struct sockaddr_in sin;
625 memset(&sin, 0, sizeof(sin));
626 sin.sin_family = AF_INET;
627 if (ip_addr == NULL) {
628 sin.sin_addr.s_addr = INADDR_ANY;
630 sin.sin_addr.s_addr = inet_addr(ip_addr);
633 if (sin.sin_addr.s_addr == INADDR_NONE) {
634 sin.sin_addr.s_addr = INADDR_ANY;
637 if (port_number == 0) {
638 lprintf(1, "Cannot start: no port number specified.\n");
639 return (-WC_EXIT_BIND);
641 sin.sin_port = htons((u_short) port_number);
643 p = getprotobyname("tcp");
645 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
647 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
648 return (-WC_EXIT_BIND);
650 /* Set some socket options that make sense. */
652 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
655 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
656 there should be a preceding F_GETFL
657 and a bitwise OR with the previous
661 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
662 lprintf(1, "Can't bind: %s\n", strerror(errno));
663 return (-WC_EXIT_BIND);
665 if (listen(s, queue_len) < 0) {
666 lprintf(1, "Can't listen: %s\n", strerror(errno));
667 return (-WC_EXIT_BIND);
672 #endif /* CTDL_IPV6 */
676 * Create a Unix domain socket and listen on it
677 * sockpath - file name of the unix domain socket
678 * queue_len - Number of incoming connections to allow in the queue
680 int ig_uds_server(char *sockpath, int queue_len)
682 struct sockaddr_un addr;
685 int actual_queue_len;
687 actual_queue_len = queue_len;
688 if (actual_queue_len < 5) actual_queue_len = 5;
690 i = unlink(sockpath);
691 if ((i != 0) && (errno != ENOENT)) {
692 lprintf(1, "webcit: can't unlink %s: %s\n",
693 sockpath, strerror(errno));
694 return (-WC_EXIT_BIND);
697 memset(&addr, 0, sizeof(addr));
698 addr.sun_family = AF_UNIX;
699 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
701 s = socket(AF_UNIX, SOCK_STREAM, 0);
703 lprintf(1, "webcit: Can't create a socket: %s\n",
705 return (-WC_EXIT_BIND);
708 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
709 lprintf(1, "webcit: Can't bind: %s\n",
711 return (-WC_EXIT_BIND);
714 if (listen(s, actual_queue_len) < 0) {
715 lprintf(1, "webcit: Can't listen: %s\n",
717 return (-WC_EXIT_BIND);
720 chmod(sockpath, 0777);
728 * Read data from the client socket.
730 * sock socket fd to read from
731 * buf buffer to read into
732 * bytes number of bytes to read
733 * timeout Number of seconds to wait before timing out
735 * Possible return values:
736 * 1 Requested number of bytes has been read.
737 * 0 Request timed out.
738 * -1 Connection is broken, or other error.
740 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
750 baselen = StrLength(Target);
752 if (Hdr->Pos == NULL)
753 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
754 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
756 if (bytes < bufremain)
758 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
759 StrBufCutLeft(Hdr->ReadBuf, bufremain);
761 if (bytes > bufremain)
763 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
765 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
767 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
769 write(2, "\033[32m", 5);
770 write(2, buf, bytes);
771 write(2, "\033[30m", 5);
776 lprintf(2, "client_read_ssl() failed\n");
785 retval = StrBufReadBLOBBuffered(Target,
794 lprintf(2, "client_read() failed: %s\n",
800 write(2, "\033[32m", 5);
801 write(2, buf, bytes);
802 write(2, "\033[30m", 5);
809 * Begin buffering HTTP output so we can transmit it all in one write operation later.
811 void begin_burst(void)
813 if (WC->WBuf == NULL) {
814 WC->WBuf = NewStrBufPlain(NULL, 32768);
820 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
825 const char *ptr, *eptr;
831 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
833 if (CompressBuffer(WCC->WBuf) > 0)
834 hprintf("Content-encoding: gzip\r\n");
836 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
841 if (WCC->Hdr->HR.prohibit_caching)
842 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
843 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
845 ptr = ChrPtr(WCC->HBuf);
846 count = StrLength(WCC->HBuf);
851 client_write_ssl(WCC->HBuf);
852 client_write_ssl(WCC->WBuf);
860 write(2, "\033[34m", 5);
861 write(2, ptr, StrLength(WCC->WBuf));
862 write(2, "\033[30m", 5);
864 if (WCC->Hdr->http_sock == -1)
866 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
868 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
869 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
871 FD_SET(WCC->Hdr->http_sock, &wset);
872 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
873 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
878 if ((WCC->Hdr->http_sock == -1) ||
879 (res = write(WCC->Hdr->http_sock,
882 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
890 ptr = ChrPtr(WCC->WBuf);
891 count = StrLength(WCC->WBuf);
896 write(2, "\033[34m", 5);
897 write(2, ptr, StrLength(WCC->WBuf));
898 write(2, "\033[30m", 5);
901 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
902 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
904 FD_SET(WCC->Hdr->http_sock, &wset);
905 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
906 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
911 if ((WCC->Hdr->http_sock == -1) ||
912 (res = write(WCC->Hdr->http_sock,
915 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
923 return StrLength(WCC->WBuf);
928 * lingering_close() a`la Apache. see
929 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
931 int lingering_close(int fd)
936 struct timeval tv, start;
938 gettimeofday(&start, NULL);
944 gettimeofday(&tv, NULL);
945 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
946 tv.tv_usec = start.tv_usec - tv.tv_usec;
947 if (tv.tv_usec < 0) {
949 tv.tv_usec += 1000000;
953 i = select(fd + 1, &set, NULL, NULL, &tv);
954 } while (i == -1 && errno == EINTR);
959 i = read(fd, buf, sizeof buf);
960 } while (i != 0 && (i != -1 || errno == EINTR));
966 HttpNewModule_TCPSOCKETS
967 (ParsedHttpHdrs *httpreq)
970 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
974 HttpDetachModule_TCPSOCKETS
975 (ParsedHttpHdrs *httpreq)
978 FlushStrBuf(httpreq->ReadBuf);
979 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
983 HttpDestroyModule_TCPSOCKETS
984 (ParsedHttpHdrs *httpreq)
987 FreeStrBuf(&httpreq->ReadBuf);
992 SessionNewModule_TCPSOCKETS
995 sess->CLineBuf = NewStrBuf();
996 sess->MigrateReadLineBuf = NewStrBuf();
1000 SessionDestroyModule_TCPSOCKETS
1003 FreeStrBuf(&sess->CLineBuf);
1004 FreeStrBuf(&sess->ReadBuf);
1005 sess->ReadPos = NULL;
1006 FreeStrBuf(&sess->MigrateReadLineBuf);
1007 if (sess->serv_sock > 0)
1008 close(sess->serv_sock);