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 if (WCC->ReadPos != NULL) {
398 pch = ChrPtr(WCC->ReadBuf);
400 YetRead = WCC->ReadPos - pch;
405 StillThere = StrLength(WCC->ReadBuf) -
411 total_len -= StillThere;
413 FlushStrBuf(WCC->ReadBuf);
418 rc = StrBufReadBLOB(Ret,
425 lprintf(1, "Server connection broken: %s\n",
428 WCC->serv_sock = (-1);
434 return StrLength(Ret);
437 return StrLength(Ret);
443 else while ((WCC->serv_sock!=-1) &&
444 (bytes < total_len)) {
446 if ((total_len - bytes) < thisblock) {
447 thisblock = total_len - bytes;
448 if (thisblock == 0) {
453 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
454 if (StrBuf_ServGetln(Buf) > 0)
456 if (GetServerStatus(Buf, NULL) == 6)
458 StrBufCutLeft(Buf, 4);
459 thisblock = StrTol(Buf);
460 if (WCC->serv_sock==-1) {
464 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
468 lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
473 return StrLength(Ret);
477 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
481 const char *pch, *pchs;
482 int rlen, len, retval = 0;
486 if (StrLength(Hdr->ReadBuf) > 0) {
487 pchs = ChrPtr(Hdr->ReadBuf);
488 pch = strchr(pchs, '\n');
492 if (len > 0 && (*(pch - 1) == '\r') )
494 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
495 StrBufCutLeft(Hdr->ReadBuf, len + 1);
500 while (retval == 0) {
502 pchs = ChrPtr(Hdr->ReadBuf);
504 pch = strchr(pchs, '\n');
506 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
507 pchs = ChrPtr(Hdr->ReadBuf);
508 pch = strchr(pchs, '\n');
517 if ((retval > 0) && (pch != NULL)) {
520 if (len > 0 && (*(pch - 1) == '\r') )
522 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
523 StrBufCutLeft(Hdr->ReadBuf, len + 1);
532 return StrBufTCP_read_buffered_line_fast(Target,
544 * This is a generic function to set up a master socket for listening on
545 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
547 * ip_addr IP address to bind
548 * port_number port number to bind
549 * queue_len number of incoming connections to allow in the queue
551 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
554 struct sockaddr_in6 sin;
557 memset(&sin, 0, sizeof(sin));
558 sin.sin6_family = AF_INET6;
560 if ((ip_addr == NULL) || (IsEmptyStr(ip_addr)) || (!strcmp(ip_addr, "0.0.0.0"))) {
561 sin.sin6_addr = in6addr_any;
564 if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) {
565 snprintf(bind_to, sizeof bind_to, "::ffff:%s", ip_addr);
568 safestrncpy(bind_to, ip_addr, sizeof bind_to);
570 if (inet_pton(AF_INET6, bind_to, &sin.sin6_addr) <= 0) {
571 lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
572 return (-WC_EXIT_BIND);
576 if (port_number == 0) {
577 lprintf(1, "Cannot start: no port number specified.\n");
578 return (-WC_EXIT_BIND);
580 sin.sin6_port = htons((u_short) port_number);
582 p = getprotobyname("tcp");
584 s = socket(PF_INET6, SOCK_STREAM, (p->p_proto));
586 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
587 return (-WC_EXIT_BIND);
589 /* Set some socket options that make sense. */
591 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
594 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
595 there should be a preceding F_GETFL
596 and a bitwise OR with the previous
600 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
601 lprintf(1, "Can't bind: %s\n", strerror(errno));
602 return (-WC_EXIT_BIND);
604 if (listen(s, queue_len) < 0) {
605 lprintf(1, "Can't listen: %s\n", strerror(errno));
606 return (-WC_EXIT_BIND);
611 #else /* CTDL_IPV6 */
614 * This is a generic function to set up a master socket for listening on
615 * a TCP port. The server shuts down if the bind fails.
617 * ip_addr IP address to bind
618 * port_number port number to bind
619 * queue_len number of incoming connections to allow in the queue
621 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
624 struct sockaddr_in sin;
627 memset(&sin, 0, sizeof(sin));
628 sin.sin_family = AF_INET;
629 if (ip_addr == NULL) {
630 sin.sin_addr.s_addr = INADDR_ANY;
632 sin.sin_addr.s_addr = inet_addr(ip_addr);
635 if (sin.sin_addr.s_addr == INADDR_NONE) {
636 sin.sin_addr.s_addr = INADDR_ANY;
639 if (port_number == 0) {
640 lprintf(1, "Cannot start: no port number specified.\n");
641 return (-WC_EXIT_BIND);
643 sin.sin_port = htons((u_short) port_number);
645 p = getprotobyname("tcp");
647 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
649 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
650 return (-WC_EXIT_BIND);
652 /* Set some socket options that make sense. */
654 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
657 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
658 there should be a preceding F_GETFL
659 and a bitwise OR with the previous
663 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
664 lprintf(1, "Can't bind: %s\n", strerror(errno));
665 return (-WC_EXIT_BIND);
667 if (listen(s, queue_len) < 0) {
668 lprintf(1, "Can't listen: %s\n", strerror(errno));
669 return (-WC_EXIT_BIND);
674 #endif /* CTDL_IPV6 */
678 * Create a Unix domain socket and listen on it
679 * sockpath - file name of the unix domain socket
680 * queue_len - Number of incoming connections to allow in the queue
682 int ig_uds_server(char *sockpath, int queue_len)
684 struct sockaddr_un addr;
687 int actual_queue_len;
689 actual_queue_len = queue_len;
690 if (actual_queue_len < 5) actual_queue_len = 5;
692 i = unlink(sockpath);
693 if ((i != 0) && (errno != ENOENT)) {
694 lprintf(1, "webcit: can't unlink %s: %s\n",
695 sockpath, strerror(errno));
696 return (-WC_EXIT_BIND);
699 memset(&addr, 0, sizeof(addr));
700 addr.sun_family = AF_UNIX;
701 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
703 s = socket(AF_UNIX, SOCK_STREAM, 0);
705 lprintf(1, "webcit: Can't create a socket: %s\n",
707 return (-WC_EXIT_BIND);
710 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
711 lprintf(1, "webcit: Can't bind: %s\n",
713 return (-WC_EXIT_BIND);
716 if (listen(s, actual_queue_len) < 0) {
717 lprintf(1, "webcit: Can't listen: %s\n",
719 return (-WC_EXIT_BIND);
722 chmod(sockpath, 0777);
730 * Read data from the client socket.
732 * sock socket fd to read from
733 * buf buffer to read into
734 * bytes number of bytes to read
735 * timeout Number of seconds to wait before timing out
737 * Possible return values:
738 * 1 Requested number of bytes has been read.
739 * 0 Request timed out.
740 * -1 Connection is broken, or other error.
742 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
752 baselen = StrLength(Target);
754 if (Hdr->Pos == NULL)
755 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
756 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
758 if (bytes < bufremain)
760 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
761 StrBufCutLeft(Hdr->ReadBuf, bufremain);
763 if (bytes > bufremain)
765 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
767 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
769 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
771 write(2, "\033[32m", 5);
772 write(2, buf, bytes);
773 write(2, "\033[30m", 5);
778 lprintf(2, "client_read_ssl() failed\n");
787 retval = StrBufReadBLOBBuffered(Target,
796 lprintf(2, "client_read() failed: %s\n",
803 write(2, "\033[32m", 5);
804 write(2, buf, bytes);
805 write(2, "\033[30m", 5);
812 * Begin buffering HTTP output so we can transmit it all in one write operation later.
814 void begin_burst(void)
816 if (WC->WBuf == NULL) {
817 WC->WBuf = NewStrBufPlain(NULL, 32768);
823 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
828 const char *ptr, *eptr;
834 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
836 if (CompressBuffer(WCC->WBuf) > 0)
837 hprintf("Content-encoding: gzip\r\n");
839 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
844 if (WCC->Hdr->HR.prohibit_caching)
845 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
846 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
848 ptr = ChrPtr(WCC->HBuf);
849 count = StrLength(WCC->HBuf);
854 client_write_ssl(WCC->HBuf);
855 client_write_ssl(WCC->WBuf);
863 write(2, "\033[34m", 5);
864 write(2, ptr, StrLength(WCC->WBuf));
865 write(2, "\033[30m", 5);
867 if (WCC->Hdr->http_sock == -1)
869 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
871 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
872 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
874 FD_SET(WCC->Hdr->http_sock, &wset);
875 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
876 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
881 if ((WCC->Hdr->http_sock == -1) ||
882 (res = write(WCC->Hdr->http_sock,
885 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
893 ptr = ChrPtr(WCC->WBuf);
894 count = StrLength(WCC->WBuf);
899 write(2, "\033[34m", 5);
900 write(2, ptr, StrLength(WCC->WBuf));
901 write(2, "\033[30m", 5);
904 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
905 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
907 FD_SET(WCC->Hdr->http_sock, &wset);
908 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
909 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
914 if ((WCC->Hdr->http_sock == -1) ||
915 (res = write(WCC->Hdr->http_sock,
918 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
926 return StrLength(WCC->WBuf);
931 * lingering_close() a`la Apache. see
932 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
934 int lingering_close(int fd)
939 struct timeval tv, start;
941 gettimeofday(&start, NULL);
947 gettimeofday(&tv, NULL);
948 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
949 tv.tv_usec = start.tv_usec - tv.tv_usec;
950 if (tv.tv_usec < 0) {
952 tv.tv_usec += 1000000;
956 i = select(fd + 1, &set, NULL, NULL, &tv);
957 } while (i == -1 && errno == EINTR);
962 i = read(fd, buf, sizeof buf);
963 } while (i != 0 && (i != -1 || errno == EINTR));
969 HttpNewModule_TCPSOCKETS
970 (ParsedHttpHdrs *httpreq)
973 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
977 HttpDetachModule_TCPSOCKETS
978 (ParsedHttpHdrs *httpreq)
981 FlushStrBuf(httpreq->ReadBuf);
982 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
986 HttpDestroyModule_TCPSOCKETS
987 (ParsedHttpHdrs *httpreq)
990 FreeStrBuf(&httpreq->ReadBuf);
995 SessionNewModule_TCPSOCKETS
998 sess->CLineBuf = NewStrBuf();
999 sess->MigrateReadLineBuf = NewStrBuf();
1003 SessionDestroyModule_TCPSOCKETS
1006 FreeStrBuf(&sess->CLineBuf);
1007 FreeStrBuf(&sess->ReadBuf);
1008 sess->ReadPos = NULL;
1009 FreeStrBuf(&sess->MigrateReadLineBuf);
1010 if (sess->serv_sock > 0)
1011 close(sess->serv_sock);