2 * Copyright (c) 1987-2011 by the citadel.org team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Uncomment this to log all communications with the Citadel server
26 #include "webserver.h"
28 extern int DisableGzip;
31 * register the timeout
33 RETSIGTYPE timeout(int signum)
35 lprintf(1, "Connection timed out; unable to reach citserver\n");
36 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
41 * Client side - connect to a unix domain socket
43 int uds_connectsock(char *sockpath)
45 struct sockaddr_un addr;
48 memset(&addr, 0, sizeof(addr));
49 addr.sun_family = AF_UNIX;
50 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
52 s = socket(AF_UNIX, SOCK_STREAM, 0);
54 lprintf(1, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
58 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
59 lprintf(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
69 * TCP client - connect to a host/port
71 int tcp_connectsock(char *host, int port)
73 struct sockaddr_in stSockAddr;
77 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
79 lprintf(1, "Can't create socket: %s\n", strerror(errno));
83 memset(&stSockAddr, 0, sizeof(struct sockaddr_in));
84 stSockAddr.sin_family = AF_INET;
85 stSockAddr.sin_port = htons(port);
86 rv = inet_pton(AF_INET, host, &stSockAddr.sin_addr);
89 lprintf(1, "Can't grok %s: %s\n", host, strerror(errno));
93 if (connect(sock, (const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)) != 0) {
94 lprintf(1, "Can't connect to %s.%d: %s\n", host, port, strerror(errno));
105 * input string from pipe
107 int serv_getln(char *strbuf, int bufsize)
113 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
114 len = StrLength(WCC->MigrateReadLineBuf);
117 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
118 FlushStrBuf(WCC->MigrateReadLineBuf);
121 lprintf(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
127 int StrBuf_ServGetln(StrBuf *buf)
130 const char *ErrStr = NULL;
134 rc = StrBufTCP_read_buffered_line_fast(buf,
142 lprintf(1, "Server connection broken: %s\n",
145 WCC->serv_sock = (-1);
153 if (WCC->ReadPos != NULL)
154 pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
155 lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
161 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
167 rc = StrBufReadBLOBBuffered(buf,
177 lprintf(1, "Server connection broken: %s\n",
180 WCC->serv_sock = (-1);
186 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
192 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
199 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
202 lprintf(1, "Server connection broken: %s\n",
205 WCC->serv_sock = (-1);
211 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
218 * send binary to server
219 * buf the buffer to write to citadel server
220 * nbytes how many bytes to send to citadel server
222 void serv_write(const char *buf, int nbytes)
225 int bytes_written = 0;
228 FlushStrBuf(WCC->ReadBuf);
230 while (bytes_written < nbytes) {
231 retval = write(WCC->serv_sock, &buf[bytes_written],
232 nbytes - bytes_written);
234 const char *ErrStr = strerror(errno);
235 lprintf(1, "Server connection broken: %s\n",
237 close(WCC->serv_sock);
238 WCC->serv_sock = (-1);
243 bytes_written = bytes_written + retval;
249 * send line to server
250 * string the line to send to the citadel server
252 void serv_puts(const char *string)
256 lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
258 FlushStrBuf(WCC->ReadBuf);
261 serv_write(string, strlen(string));
266 * send line to server
267 * string the line to send to the citadel server
269 void serv_putbuf(const StrBuf *string)
273 lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
275 FlushStrBuf(WCC->ReadBuf);
278 serv_write(ChrPtr(string), StrLength(string));
284 * convenience function to send stuff to the server
285 * format the formatstring
286 * ... the entities to insert into format
288 void serv_printf(const char *format,...)
295 FlushStrBuf(WCC->ReadBuf);
298 va_start(arg_ptr, format);
299 vsnprintf(buf, sizeof buf, format, arg_ptr);
305 serv_write(buf, len);
307 lprintf(9, ">>>%s", buf);
314 * Read binary data from server into memory using a series of server READ commands.
315 * returns the read content as StrBuf
317 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
320 size_t bytes_read = 0;
321 size_t this_block = 0;
328 while (bytes_read < total_len) {
330 if (WCC->serv_sock==-1) {
335 serv_printf("READ %d|%d", bytes_read, total_len-bytes_read);
336 if ( (rc = StrBuf_ServGetln(Buf) > 0) && (GetServerStatus(Buf, NULL) == 6) )
340 StrBufCutLeft(Buf, 4);
341 this_block = StrTol(Buf);
342 rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
344 lprintf(1, "Server connection broken during download\n");
346 WCC->serv_sock = (-1);
355 return StrLength(Ret);
359 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
363 const char *pch, *pchs;
364 int rlen, len, retval = 0;
368 if (StrLength(Hdr->ReadBuf) > 0) {
369 pchs = ChrPtr(Hdr->ReadBuf);
370 pch = strchr(pchs, '\n');
374 if (len > 0 && (*(pch - 1) == '\r') )
376 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
377 StrBufCutLeft(Hdr->ReadBuf, len + 1);
382 while (retval == 0) {
384 pchs = ChrPtr(Hdr->ReadBuf);
386 pch = strchr(pchs, '\n');
388 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
389 pchs = ChrPtr(Hdr->ReadBuf);
390 pch = strchr(pchs, '\n');
399 if ((retval > 0) && (pch != NULL)) {
402 if (len > 0 && (*(pch - 1) == '\r') )
404 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
405 StrBufCutLeft(Hdr->ReadBuf, len + 1);
414 return StrBufTCP_read_buffered_line_fast(Target,
426 * This is a generic function to set up a master socket for listening on
427 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
429 * ip_addr IP address to bind
430 * port_number port number to bind
431 * queue_len number of incoming connections to allow in the queue
433 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
436 struct sockaddr_in6 sin;
439 memset(&sin, 0, sizeof(sin));
440 sin.sin6_family = AF_INET6;
442 if ((ip_addr == NULL) || (IsEmptyStr(ip_addr)) || (!strcmp(ip_addr, "0.0.0.0"))) {
443 sin.sin6_addr = in6addr_any;
446 if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) {
447 snprintf(bind_to, sizeof bind_to, "::ffff:%s", ip_addr);
450 safestrncpy(bind_to, ip_addr, sizeof bind_to);
452 if (inet_pton(AF_INET6, bind_to, &sin.sin6_addr) <= 0) {
453 lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
454 return (-WC_EXIT_BIND);
458 if (port_number == 0) {
459 lprintf(1, "Cannot start: no port number specified.\n");
460 return (-WC_EXIT_BIND);
462 sin.sin6_port = htons((u_short) port_number);
464 p = getprotobyname("tcp");
466 s = socket(PF_INET6, SOCK_STREAM, (p->p_proto));
468 lprintf(1, "Can't create an IPv6 socket: %s\n", strerror(errno));
469 return (-WC_EXIT_BIND);
471 /* Set some socket options that make sense. */
473 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
476 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
477 there should be a preceding F_GETFL
478 and a bitwise OR with the previous
482 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
483 lprintf(1, "Can't bind: %s\n", strerror(errno));
484 return (-WC_EXIT_BIND);
486 if (listen(s, queue_len) < 0) {
487 lprintf(1, "Can't listen: %s\n", strerror(errno));
488 return (-WC_EXIT_BIND);
493 #else /* CTDL_IPV6 */
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.
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_in sin;
509 memset(&sin, 0, sizeof(sin));
510 sin.sin_family = AF_INET;
511 if (ip_addr == NULL) {
512 sin.sin_addr.s_addr = INADDR_ANY;
514 sin.sin_addr.s_addr = inet_addr(ip_addr);
517 if (sin.sin_addr.s_addr == INADDR_NONE) {
518 sin.sin_addr.s_addr = INADDR_ANY;
521 if (port_number == 0) {
522 lprintf(1, "Cannot start: no port number specified.\n");
523 return (-WC_EXIT_BIND);
525 sin.sin_port = htons((u_short) port_number);
527 p = getprotobyname("tcp");
529 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
531 lprintf(1, "Can't create an IPv4 socket: %s\n", strerror(errno));
532 return (-WC_EXIT_BIND);
534 /* Set some socket options that make sense. */
536 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
538 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
539 lprintf(1, "Can't bind: %s\n", strerror(errno));
540 return (-WC_EXIT_BIND);
542 if (listen(s, queue_len) < 0) {
543 lprintf(1, "Can't listen: %s\n", strerror(errno));
544 return (-WC_EXIT_BIND);
549 #endif /* CTDL_IPV6 */
553 * Create a Unix domain socket and listen on it
554 * sockpath - file name of the unix domain socket
555 * queue_len - Number of incoming connections to allow in the queue
557 int ig_uds_server(char *sockpath, int queue_len)
559 struct sockaddr_un addr;
562 int actual_queue_len;
564 actual_queue_len = queue_len;
565 if (actual_queue_len < 5) actual_queue_len = 5;
567 i = unlink(sockpath);
568 if ((i != 0) && (errno != ENOENT)) {
569 lprintf(1, "webcit: can't unlink %s: %s\n",
570 sockpath, strerror(errno));
571 return (-WC_EXIT_BIND);
574 memset(&addr, 0, sizeof(addr));
575 addr.sun_family = AF_UNIX;
576 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
578 s = socket(AF_UNIX, SOCK_STREAM, 0);
580 lprintf(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
581 return (-WC_EXIT_BIND);
584 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
585 lprintf(1, "webcit: Can't bind: %s\n",
587 return (-WC_EXIT_BIND);
590 if (listen(s, actual_queue_len) < 0) {
591 lprintf(1, "webcit: Can't listen: %s\n",
593 return (-WC_EXIT_BIND);
596 chmod(sockpath, 0777);
604 * Read data from the client socket.
606 * sock socket fd to read from
607 * buf buffer to read into
608 * bytes number of bytes to read
609 * timeout Number of seconds to wait before timing out
611 * Possible return values:
612 * 1 Requested number of bytes has been read.
613 * 0 Request timed out.
614 * -1 Connection is broken, or other error.
616 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
626 baselen = StrLength(Target);
628 if (Hdr->Pos == NULL)
629 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
630 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
632 if (bytes < bufremain)
634 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
635 StrBufCutLeft(Hdr->ReadBuf, bufremain);
637 if (bytes > bufremain)
639 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
641 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
643 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
645 write(2, "\033[32m", 5);
646 write(2, buf, bytes);
647 write(2, "\033[30m", 5);
652 lprintf(2, "client_read_ssl() failed\n");
661 retval = StrBufReadBLOBBuffered(Target,
670 lprintf(2, "client_read() failed: %s\n",
677 write(2, "\033[32m", 5);
678 write(2, buf, bytes);
679 write(2, "\033[30m", 5);
686 * Begin buffering HTTP output so we can transmit it all in one write operation later.
688 void begin_burst(void)
690 if (WC->WBuf == NULL) {
691 WC->WBuf = NewStrBufPlain(NULL, 32768);
697 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
702 const char *ptr, *eptr;
708 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
710 if (CompressBuffer(WCC->WBuf) > 0)
711 hprintf("Content-encoding: gzip\r\n");
713 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
718 if (WCC->WFBuf != NULL) {
719 WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
720 FreeStrBuf(&WCC->WFBuf);
723 if (WCC->Hdr->HR.prohibit_caching)
724 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
725 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
727 ptr = ChrPtr(WCC->HBuf);
728 count = StrLength(WCC->HBuf);
733 client_write_ssl(WCC->HBuf);
734 client_write_ssl(WCC->WBuf);
742 write(2, "\033[34m", 5);
743 write(2, ptr, StrLength(WCC->WBuf));
744 write(2, "\033[30m", 5);
746 if (WCC->Hdr->http_sock == -1)
748 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
750 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
751 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
753 FD_SET(WCC->Hdr->http_sock, &wset);
754 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
755 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
760 if ((WCC->Hdr->http_sock == -1) ||
761 (res = write(WCC->Hdr->http_sock,
764 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
772 ptr = ChrPtr(WCC->WBuf);
773 count = StrLength(WCC->WBuf);
778 write(2, "\033[34m", 5);
779 write(2, ptr, StrLength(WCC->WBuf));
780 write(2, "\033[30m", 5);
783 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
784 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
786 FD_SET(WCC->Hdr->http_sock, &wset);
787 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
788 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
793 if ((WCC->Hdr->http_sock == -1) ||
794 (res = write(WCC->Hdr->http_sock,
797 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
805 return StrLength(WCC->WBuf);
810 * lingering_close() a`la Apache. see
811 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
813 int lingering_close(int fd)
818 struct timeval tv, start;
820 gettimeofday(&start, NULL);
826 gettimeofday(&tv, NULL);
827 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
828 tv.tv_usec = start.tv_usec - tv.tv_usec;
829 if (tv.tv_usec < 0) {
831 tv.tv_usec += 1000000;
835 i = select(fd + 1, &set, NULL, NULL, &tv);
836 } while (i == -1 && errno == EINTR);
841 i = read(fd, buf, sizeof buf);
842 } while (i != 0 && (i != -1 || errno == EINTR));
848 HttpNewModule_TCPSOCKETS
849 (ParsedHttpHdrs *httpreq)
852 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
856 HttpDetachModule_TCPSOCKETS
857 (ParsedHttpHdrs *httpreq)
860 FlushStrBuf(httpreq->ReadBuf);
861 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
865 HttpDestroyModule_TCPSOCKETS
866 (ParsedHttpHdrs *httpreq)
869 FreeStrBuf(&httpreq->ReadBuf);
874 SessionNewModule_TCPSOCKETS
877 sess->CLineBuf = NewStrBuf();
878 sess->MigrateReadLineBuf = NewStrBuf();
882 SessionDestroyModule_TCPSOCKETS
885 FreeStrBuf(&sess->CLineBuf);
886 FreeStrBuf(&sess->ReadBuf);
887 sess->ReadPos = NULL;
888 FreeStrBuf(&sess->MigrateReadLineBuf);
889 if (sess->serv_sock > 0)
890 close(sess->serv_sock);