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 * Client side - connect to a unix domain socket
46 int uds_connectsock(char *sockpath)
48 struct sockaddr_un addr;
51 memset(&addr, 0, sizeof(addr));
52 addr.sun_family = AF_UNIX;
53 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
55 s = socket(AF_UNIX, SOCK_STREAM, 0);
57 lprintf(1, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
61 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
62 lprintf(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
72 * TCP client - connect to a host/port
74 int tcp_connectsock(char *host, int port)
76 struct sockaddr_in stSockAddr;
80 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
82 lprintf(1, "Can't create socket: %s\n", strerror(errno));
86 memset(&stSockAddr, 0, sizeof(struct sockaddr_in));
87 stSockAddr.sin_family = AF_INET;
88 stSockAddr.sin_port = htons(port);
89 rv = inet_pton(AF_INET, host, &stSockAddr.sin_addr);
92 lprintf(1, "Can't grok %s: %s\n", host, strerror(errno));
96 if (connect(sock, (const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)) != 0) {
97 lprintf(1, "Can't connect to %s.%d: %s\n", host, port, strerror(errno));
108 * input string from pipe
110 int serv_getln(char *strbuf, int bufsize)
116 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
117 len = StrLength(WCC->MigrateReadLineBuf);
120 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
121 FlushStrBuf(WCC->MigrateReadLineBuf);
124 lprintf(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
130 int StrBuf_ServGetln(StrBuf *buf)
133 const char *ErrStr = NULL;
137 rc = StrBufTCP_read_buffered_line_fast(buf,
145 lprintf(1, "Server connection broken: %s\n",
148 WCC->serv_sock = (-1);
156 if (WCC->ReadPos != NULL)
157 pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
158 lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
164 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
170 rc = StrBufReadBLOBBuffered(buf,
180 lprintf(1, "Server connection broken: %s\n",
183 WCC->serv_sock = (-1);
189 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
195 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
202 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
205 lprintf(1, "Server connection broken: %s\n",
208 WCC->serv_sock = (-1);
214 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
221 * send binary to server
222 * buf the buffer to write to citadel server
223 * nbytes how many bytes to send to citadel server
225 void serv_write(const char *buf, int nbytes)
228 int bytes_written = 0;
231 FlushStrBuf(WCC->ReadBuf);
233 while (bytes_written < nbytes) {
234 retval = write(WCC->serv_sock, &buf[bytes_written],
235 nbytes - bytes_written);
237 const char *ErrStr = strerror(errno);
238 lprintf(1, "Server connection broken: %s\n",
240 close(WCC->serv_sock);
241 WCC->serv_sock = (-1);
246 bytes_written = bytes_written + retval;
252 * send line to server
253 * string the line to send to the citadel server
255 void serv_puts(const char *string)
259 lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
261 FlushStrBuf(WCC->ReadBuf);
264 serv_write(string, strlen(string));
269 * send line to server
270 * string the line to send to the citadel server
272 void serv_putbuf(const StrBuf *string)
276 lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
278 FlushStrBuf(WCC->ReadBuf);
281 serv_write(ChrPtr(string), StrLength(string));
287 * convenience function to send stuff to the server
288 * format the formatstring
289 * ... the entities to insert into format
291 void serv_printf(const char *format,...)
298 FlushStrBuf(WCC->ReadBuf);
301 va_start(arg_ptr, format);
302 vsnprintf(buf, sizeof buf, format, arg_ptr);
308 serv_write(buf, len);
310 lprintf(9, ">>>%s", buf);
317 * Read binary data from server into memory using a series of
318 * server READ commands.
319 * \return the read content as StrBuf
321 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
325 size_t thisblock = 0;
332 serv_printf("READ %d|"SIZE_T_FMT, 0, total_len);
333 if (StrBuf_ServGetln(Buf) > 0)
340 if (GetServerStatus(Buf, NULL) == 6)
342 StrBufCutLeft(Buf, 4);
343 thisblock = StrTol(Buf);
344 if (WCC->serv_sock==-1) {
349 if (WCC->ReadPos != NULL) {
350 pch = ChrPtr(WCC->ReadBuf);
352 YetRead = WCC->ReadPos - pch;
357 StillThere = StrLength(WCC->ReadBuf) -
363 total_len -= StillThere;
365 FlushStrBuf(WCC->ReadBuf);
370 rc = StrBufReadBLOB(Ret,
377 lprintf(1, "Server connection broken: %s\n",
380 WCC->serv_sock = (-1);
386 return StrLength(Ret);
389 return StrLength(Ret);
395 else while ((WCC->serv_sock!=-1) &&
396 (bytes < total_len)) {
398 if ((total_len - bytes) < thisblock) {
399 thisblock = total_len - bytes;
400 if (thisblock == 0) {
405 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
406 if (StrBuf_ServGetln(Buf) > 0)
408 if (GetServerStatus(Buf, NULL) == 6)
410 StrBufCutLeft(Buf, 4);
411 thisblock = StrTol(Buf);
412 if (WCC->serv_sock==-1) {
416 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
420 lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
425 return StrLength(Ret);
429 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
433 const char *pch, *pchs;
434 int rlen, len, retval = 0;
438 if (StrLength(Hdr->ReadBuf) > 0) {
439 pchs = ChrPtr(Hdr->ReadBuf);
440 pch = strchr(pchs, '\n');
444 if (len > 0 && (*(pch - 1) == '\r') )
446 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
447 StrBufCutLeft(Hdr->ReadBuf, len + 1);
452 while (retval == 0) {
454 pchs = ChrPtr(Hdr->ReadBuf);
456 pch = strchr(pchs, '\n');
458 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
459 pchs = ChrPtr(Hdr->ReadBuf);
460 pch = strchr(pchs, '\n');
469 if ((retval > 0) && (pch != NULL)) {
472 if (len > 0 && (*(pch - 1) == '\r') )
474 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
475 StrBufCutLeft(Hdr->ReadBuf, len + 1);
484 return StrBufTCP_read_buffered_line_fast(Target,
496 * This is a generic function to set up a master socket for listening on
497 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
499 * ip_addr IP address to bind
500 * port_number port number to bind
501 * queue_len number of incoming connections to allow in the queue
503 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
506 struct sockaddr_in6 sin;
509 memset(&sin, 0, sizeof(sin));
510 sin.sin6_family = AF_INET6;
512 if ((ip_addr == NULL) || (IsEmptyStr(ip_addr)) || (!strcmp(ip_addr, "0.0.0.0"))) {
513 sin.sin6_addr = in6addr_any;
516 if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) {
517 snprintf(bind_to, sizeof bind_to, "::ffff:%s", ip_addr);
520 safestrncpy(bind_to, ip_addr, sizeof bind_to);
522 if (inet_pton(AF_INET6, bind_to, &sin.sin6_addr) <= 0) {
523 lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
524 return (-WC_EXIT_BIND);
528 if (port_number == 0) {
529 lprintf(1, "Cannot start: no port number specified.\n");
530 return (-WC_EXIT_BIND);
532 sin.sin6_port = htons((u_short) port_number);
534 p = getprotobyname("tcp");
536 s = socket(PF_INET6, SOCK_STREAM, (p->p_proto));
538 lprintf(1, "Can't create an IPv6 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);
563 #else /* CTDL_IPV6 */
566 * This is a generic function to set up a master socket for listening on
567 * a TCP port. The server shuts down if the bind fails.
569 * ip_addr IP address to bind
570 * port_number port number to bind
571 * queue_len number of incoming connections to allow in the queue
573 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
576 struct sockaddr_in sin;
579 memset(&sin, 0, sizeof(sin));
580 sin.sin_family = AF_INET;
581 if (ip_addr == NULL) {
582 sin.sin_addr.s_addr = INADDR_ANY;
584 sin.sin_addr.s_addr = inet_addr(ip_addr);
587 if (sin.sin_addr.s_addr == INADDR_NONE) {
588 sin.sin_addr.s_addr = INADDR_ANY;
591 if (port_number == 0) {
592 lprintf(1, "Cannot start: no port number specified.\n");
593 return (-WC_EXIT_BIND);
595 sin.sin_port = htons((u_short) port_number);
597 p = getprotobyname("tcp");
599 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
601 lprintf(1, "Can't create an IPv4 socket: %s\n", strerror(errno));
602 return (-WC_EXIT_BIND);
604 /* Set some socket options that make sense. */
606 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
608 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
609 lprintf(1, "Can't bind: %s\n", strerror(errno));
610 return (-WC_EXIT_BIND);
612 if (listen(s, queue_len) < 0) {
613 lprintf(1, "Can't listen: %s\n", strerror(errno));
614 return (-WC_EXIT_BIND);
619 #endif /* CTDL_IPV6 */
623 * Create a Unix domain socket and listen on it
624 * sockpath - file name of the unix domain socket
625 * queue_len - Number of incoming connections to allow in the queue
627 int ig_uds_server(char *sockpath, int queue_len)
629 struct sockaddr_un addr;
632 int actual_queue_len;
634 actual_queue_len = queue_len;
635 if (actual_queue_len < 5) actual_queue_len = 5;
637 i = unlink(sockpath);
638 if ((i != 0) && (errno != ENOENT)) {
639 lprintf(1, "webcit: can't unlink %s: %s\n",
640 sockpath, strerror(errno));
641 return (-WC_EXIT_BIND);
644 memset(&addr, 0, sizeof(addr));
645 addr.sun_family = AF_UNIX;
646 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
648 s = socket(AF_UNIX, SOCK_STREAM, 0);
650 lprintf(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
651 return (-WC_EXIT_BIND);
654 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
655 lprintf(1, "webcit: Can't bind: %s\n",
657 return (-WC_EXIT_BIND);
660 if (listen(s, actual_queue_len) < 0) {
661 lprintf(1, "webcit: Can't listen: %s\n",
663 return (-WC_EXIT_BIND);
666 chmod(sockpath, 0777);
674 * Read data from the client socket.
676 * sock socket fd to read from
677 * buf buffer to read into
678 * bytes number of bytes to read
679 * timeout Number of seconds to wait before timing out
681 * Possible return values:
682 * 1 Requested number of bytes has been read.
683 * 0 Request timed out.
684 * -1 Connection is broken, or other error.
686 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
696 baselen = StrLength(Target);
698 if (Hdr->Pos == NULL)
699 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
700 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
702 if (bytes < bufremain)
704 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
705 StrBufCutLeft(Hdr->ReadBuf, bufremain);
707 if (bytes > bufremain)
709 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
711 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
713 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
715 write(2, "\033[32m", 5);
716 write(2, buf, bytes);
717 write(2, "\033[30m", 5);
722 lprintf(2, "client_read_ssl() failed\n");
731 retval = StrBufReadBLOBBuffered(Target,
740 lprintf(2, "client_read() failed: %s\n",
747 write(2, "\033[32m", 5);
748 write(2, buf, bytes);
749 write(2, "\033[30m", 5);
756 * Begin buffering HTTP output so we can transmit it all in one write operation later.
758 void begin_burst(void)
760 if (WC->WBuf == NULL) {
761 WC->WBuf = NewStrBufPlain(NULL, 32768);
767 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
772 const char *ptr, *eptr;
778 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
780 if (CompressBuffer(WCC->WBuf) > 0)
781 hprintf("Content-encoding: gzip\r\n");
783 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
788 if (WCC->WFBuf != NULL) {
789 WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
790 FreeStrBuf(&WCC->WFBuf);
793 if (WCC->Hdr->HR.prohibit_caching)
794 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
795 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
797 ptr = ChrPtr(WCC->HBuf);
798 count = StrLength(WCC->HBuf);
803 client_write_ssl(WCC->HBuf);
804 client_write_ssl(WCC->WBuf);
812 write(2, "\033[34m", 5);
813 write(2, ptr, StrLength(WCC->WBuf));
814 write(2, "\033[30m", 5);
816 if (WCC->Hdr->http_sock == -1)
818 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
820 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
821 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
823 FD_SET(WCC->Hdr->http_sock, &wset);
824 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
825 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
830 if ((WCC->Hdr->http_sock == -1) ||
831 (res = write(WCC->Hdr->http_sock,
834 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
842 ptr = ChrPtr(WCC->WBuf);
843 count = StrLength(WCC->WBuf);
848 write(2, "\033[34m", 5);
849 write(2, ptr, StrLength(WCC->WBuf));
850 write(2, "\033[30m", 5);
853 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
854 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
856 FD_SET(WCC->Hdr->http_sock, &wset);
857 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
858 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
863 if ((WCC->Hdr->http_sock == -1) ||
864 (res = write(WCC->Hdr->http_sock,
867 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
875 return StrLength(WCC->WBuf);
880 * lingering_close() a`la Apache. see
881 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
883 int lingering_close(int fd)
888 struct timeval tv, start;
890 gettimeofday(&start, NULL);
896 gettimeofday(&tv, NULL);
897 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
898 tv.tv_usec = start.tv_usec - tv.tv_usec;
899 if (tv.tv_usec < 0) {
901 tv.tv_usec += 1000000;
905 i = select(fd + 1, &set, NULL, NULL, &tv);
906 } while (i == -1 && errno == EINTR);
911 i = read(fd, buf, sizeof buf);
912 } while (i != 0 && (i != -1 || errno == EINTR));
918 HttpNewModule_TCPSOCKETS
919 (ParsedHttpHdrs *httpreq)
922 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
926 HttpDetachModule_TCPSOCKETS
927 (ParsedHttpHdrs *httpreq)
930 FlushStrBuf(httpreq->ReadBuf);
931 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
935 HttpDestroyModule_TCPSOCKETS
936 (ParsedHttpHdrs *httpreq)
939 FreeStrBuf(&httpreq->ReadBuf);
944 SessionNewModule_TCPSOCKETS
947 sess->CLineBuf = NewStrBuf();
948 sess->MigrateReadLineBuf = NewStrBuf();
952 SessionDestroyModule_TCPSOCKETS
955 FreeStrBuf(&sess->CLineBuf);
956 FreeStrBuf(&sess->ReadBuf);
957 sess->ReadPos = NULL;
958 FreeStrBuf(&sess->MigrateReadLineBuf);
959 if (sess->serv_sock > 0)
960 close(sess->serv_sock);