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 (FIXME this needs to be IPv6 enabled)
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(buf);
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,
495 * This is a generic function to set up a master socket for listening on
496 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
498 * ip_addr IP address to bind
499 * port_number port number to bind
500 * queue_len number of incoming connections to allow in the queue
502 int webcit_tcp_server(char *ip_addr, int port_number, int queue_len)
505 struct sockaddr_in6 sin6;
506 struct sockaddr_in sin4;
510 memset(&sin6, 0, sizeof(sin6));
511 memset(&sin4, 0, sizeof(sin4));
512 sin6.sin6_family = AF_INET6;
513 sin4.sin_family = AF_INET;
515 if ( (ip_addr == NULL) /* any IPv6 */
516 || (IsEmptyStr(ip_addr))
517 || (!strcmp(ip_addr, "*"))
520 sin6.sin6_addr = in6addr_any;
522 else if (!strcmp(ip_addr, "0.0.0.0")) /* any IPv4 */
525 sin4.sin_addr.s_addr = INADDR_ANY;
527 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) /* specific IPv4 */
530 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
531 lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
532 return (-WC_EXIT_BIND);
535 else /* specific IPv6 */
538 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
539 lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
540 return (-WC_EXIT_BIND);
544 if (port_number == 0) {
545 lprintf(1, "Cannot start: no port number specified.\n");
546 return (-WC_EXIT_BIND);
548 sin6.sin6_port = htons((u_short) port_number);
549 sin4.sin_port = htons((u_short) port_number);
551 p = getprotobyname("tcp");
553 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
555 lprintf(1, "Can't create a listening socket: %s\n", strerror(errno));
556 return (-WC_EXIT_BIND);
558 /* Set some socket options that make sense. */
560 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
562 if (ip_version == 6) {
563 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
566 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
570 lprintf(1, "Can't bind: %s\n", strerror(errno));
571 return (-WC_EXIT_BIND);
574 if (listen(s, queue_len) < 0) {
575 lprintf(1, "Can't listen: %s\n", strerror(errno));
576 return (-WC_EXIT_BIND);
583 * Create a Unix domain socket and listen on it
584 * sockpath - file name of the unix domain socket
585 * queue_len - Number of incoming connections to allow in the queue
587 int webcit_uds_server(char *sockpath, int queue_len)
589 struct sockaddr_un addr;
592 int actual_queue_len;
594 actual_queue_len = queue_len;
595 if (actual_queue_len < 5) actual_queue_len = 5;
597 i = unlink(sockpath);
598 if ((i != 0) && (errno != ENOENT)) {
599 lprintf(1, "webcit: can't unlink %s: %s\n",
600 sockpath, strerror(errno));
601 return (-WC_EXIT_BIND);
604 memset(&addr, 0, sizeof(addr));
605 addr.sun_family = AF_UNIX;
606 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
608 s = socket(AF_UNIX, SOCK_STREAM, 0);
610 lprintf(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
611 return (-WC_EXIT_BIND);
614 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
615 lprintf(1, "webcit: Can't bind: %s\n",
617 return (-WC_EXIT_BIND);
620 if (listen(s, actual_queue_len) < 0) {
621 lprintf(1, "webcit: Can't listen: %s\n",
623 return (-WC_EXIT_BIND);
626 chmod(sockpath, 0777);
634 * Read data from the client socket.
636 * sock socket fd to read from
637 * buf buffer to read into
638 * bytes number of bytes to read
639 * timeout Number of seconds to wait before timing out
641 * Possible return values:
642 * 1 Requested number of bytes has been read.
643 * 0 Request timed out.
644 * -1 Connection is broken, or other error.
646 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
656 baselen = StrLength(Target);
658 if (Hdr->Pos == NULL)
659 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
660 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
662 if (bytes < bufremain)
664 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
665 StrBufCutLeft(Hdr->ReadBuf, bufremain);
667 if (bytes > bufremain)
669 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
671 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
673 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
675 write(2, "\033[32m", 5);
676 write(2, buf, bytes);
677 write(2, "\033[30m", 5);
682 lprintf(2, "client_read_ssl() failed\n");
691 retval = StrBufReadBLOBBuffered(Target,
700 lprintf(2, "client_read() failed: %s\n",
707 write(2, "\033[32m", 5);
708 write(2, buf, bytes);
709 write(2, "\033[30m", 5);
716 * Begin buffering HTTP output so we can transmit it all in one write operation later.
718 void begin_burst(void)
720 if (WC->WBuf == NULL) {
721 WC->WBuf = NewStrBufPlain(NULL, 32768);
727 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
732 const char *ptr, *eptr;
738 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
740 if (CompressBuffer(WCC->WBuf) > 0)
741 hprintf("Content-encoding: gzip\r\n");
743 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
748 if (WCC->WFBuf != NULL) {
749 WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
750 FreeStrBuf(&WCC->WFBuf);
753 if (WCC->Hdr->HR.prohibit_caching)
754 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
755 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
757 ptr = ChrPtr(WCC->HBuf);
758 count = StrLength(WCC->HBuf);
763 client_write_ssl(WCC->HBuf);
764 client_write_ssl(WCC->WBuf);
772 write(2, "\033[34m", 5);
773 write(2, ptr, StrLength(WCC->WBuf));
774 write(2, "\033[30m", 5);
776 if (WCC->Hdr->http_sock == -1)
778 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
780 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
781 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
783 FD_SET(WCC->Hdr->http_sock, &wset);
784 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
785 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
790 if ((WCC->Hdr->http_sock == -1) ||
791 (res = write(WCC->Hdr->http_sock,
794 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
802 ptr = ChrPtr(WCC->WBuf);
803 count = StrLength(WCC->WBuf);
808 write(2, "\033[34m", 5);
809 write(2, ptr, StrLength(WCC->WBuf));
810 write(2, "\033[30m", 5);
813 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
814 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
816 FD_SET(WCC->Hdr->http_sock, &wset);
817 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
818 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
823 if ((WCC->Hdr->http_sock == -1) ||
824 (res = write(WCC->Hdr->http_sock,
827 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
835 return StrLength(WCC->WBuf);
840 * lingering_close() a`la Apache. see
841 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
843 int lingering_close(int fd)
848 struct timeval tv, start;
850 gettimeofday(&start, NULL);
856 gettimeofday(&tv, NULL);
857 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
858 tv.tv_usec = start.tv_usec - tv.tv_usec;
859 if (tv.tv_usec < 0) {
861 tv.tv_usec += 1000000;
865 i = select(fd + 1, &set, NULL, NULL, &tv);
866 } while (i == -1 && errno == EINTR);
871 i = read(fd, buf, sizeof buf);
872 } while (i != 0 && (i != -1 || errno == EINTR));
878 HttpNewModule_TCPSOCKETS
879 (ParsedHttpHdrs *httpreq)
882 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
886 HttpDetachModule_TCPSOCKETS
887 (ParsedHttpHdrs *httpreq)
890 FlushStrBuf(httpreq->ReadBuf);
891 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
895 HttpDestroyModule_TCPSOCKETS
896 (ParsedHttpHdrs *httpreq)
899 FreeStrBuf(&httpreq->ReadBuf);
904 SessionNewModule_TCPSOCKETS
907 sess->CLineBuf = NewStrBuf();
908 sess->MigrateReadLineBuf = NewStrBuf();
912 SessionDestroyModule_TCPSOCKETS
915 FreeStrBuf(&sess->CLineBuf);
916 FreeStrBuf(&sess->ReadBuf);
917 sess->ReadPos = NULL;
918 FreeStrBuf(&sess->MigrateReadLineBuf);
919 if (sess->serv_sock > 0)
920 close(sess->serv_sock);