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 ( (StrBuf_ServGetln(Buf) > 0) && (GetServerStatus(Buf, NULL) == 6) ) {
337 StrBufCutLeft(Buf, 4);
338 this_block = StrTol(Buf);
339 rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
341 lprintf(1, "Server connection broken during download\n");
343 WCC->serv_sock = (-1);
352 return StrLength(Ret);
356 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
360 const char *pch, *pchs;
361 int rlen, len, retval = 0;
365 if (StrLength(Hdr->ReadBuf) > 0) {
366 pchs = ChrPtr(Hdr->ReadBuf);
367 pch = strchr(pchs, '\n');
371 if (len > 0 && (*(pch - 1) == '\r') )
373 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
374 StrBufCutLeft(Hdr->ReadBuf, len + 1);
379 while (retval == 0) {
381 pchs = ChrPtr(Hdr->ReadBuf);
383 pch = strchr(pchs, '\n');
385 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
386 pchs = ChrPtr(Hdr->ReadBuf);
387 pch = strchr(pchs, '\n');
396 if ((retval > 0) && (pch != NULL)) {
399 if (len > 0 && (*(pch - 1) == '\r') )
401 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
402 StrBufCutLeft(Hdr->ReadBuf, len + 1);
411 return StrBufTCP_read_buffered_line_fast(Target,
423 * This is a generic function to set up a master socket for listening on
424 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
426 * ip_addr IP address to bind
427 * port_number port number to bind
428 * queue_len number of incoming connections to allow in the queue
430 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
433 struct sockaddr_in6 sin;
436 memset(&sin, 0, sizeof(sin));
437 sin.sin6_family = AF_INET6;
439 if ((ip_addr == NULL) || (IsEmptyStr(ip_addr)) || (!strcmp(ip_addr, "0.0.0.0"))) {
440 sin.sin6_addr = in6addr_any;
443 if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) {
444 snprintf(bind_to, sizeof bind_to, "::ffff:%s", ip_addr);
447 safestrncpy(bind_to, ip_addr, sizeof bind_to);
449 if (inet_pton(AF_INET6, bind_to, &sin.sin6_addr) <= 0) {
450 lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
451 return (-WC_EXIT_BIND);
455 if (port_number == 0) {
456 lprintf(1, "Cannot start: no port number specified.\n");
457 return (-WC_EXIT_BIND);
459 sin.sin6_port = htons((u_short) port_number);
461 p = getprotobyname("tcp");
463 s = socket(PF_INET6, SOCK_STREAM, (p->p_proto));
465 lprintf(1, "Can't create an IPv6 socket: %s\n", strerror(errno));
466 return (-WC_EXIT_BIND);
468 /* Set some socket options that make sense. */
470 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
473 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
474 there should be a preceding F_GETFL
475 and a bitwise OR with the previous
479 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
480 lprintf(1, "Can't bind: %s\n", strerror(errno));
481 return (-WC_EXIT_BIND);
483 if (listen(s, queue_len) < 0) {
484 lprintf(1, "Can't listen: %s\n", strerror(errno));
485 return (-WC_EXIT_BIND);
490 #else /* CTDL_IPV6 */
493 * This is a generic function to set up a master socket for listening on
494 * a TCP port. The server shuts down if the bind fails.
496 * ip_addr IP address to bind
497 * port_number port number to bind
498 * queue_len number of incoming connections to allow in the queue
500 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
503 struct sockaddr_in sin;
506 memset(&sin, 0, sizeof(sin));
507 sin.sin_family = AF_INET;
508 if (ip_addr == NULL) {
509 sin.sin_addr.s_addr = INADDR_ANY;
511 sin.sin_addr.s_addr = inet_addr(ip_addr);
514 if (sin.sin_addr.s_addr == INADDR_NONE) {
515 sin.sin_addr.s_addr = INADDR_ANY;
518 if (port_number == 0) {
519 lprintf(1, "Cannot start: no port number specified.\n");
520 return (-WC_EXIT_BIND);
522 sin.sin_port = htons((u_short) port_number);
524 p = getprotobyname("tcp");
526 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
528 lprintf(1, "Can't create an IPv4 socket: %s\n", strerror(errno));
529 return (-WC_EXIT_BIND);
531 /* Set some socket options that make sense. */
533 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
535 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
536 lprintf(1, "Can't bind: %s\n", strerror(errno));
537 return (-WC_EXIT_BIND);
539 if (listen(s, queue_len) < 0) {
540 lprintf(1, "Can't listen: %s\n", strerror(errno));
541 return (-WC_EXIT_BIND);
546 #endif /* CTDL_IPV6 */
550 * Create a Unix domain socket and listen on it
551 * sockpath - file name of the unix domain socket
552 * queue_len - Number of incoming connections to allow in the queue
554 int ig_uds_server(char *sockpath, int queue_len)
556 struct sockaddr_un addr;
559 int actual_queue_len;
561 actual_queue_len = queue_len;
562 if (actual_queue_len < 5) actual_queue_len = 5;
564 i = unlink(sockpath);
565 if ((i != 0) && (errno != ENOENT)) {
566 lprintf(1, "webcit: can't unlink %s: %s\n",
567 sockpath, strerror(errno));
568 return (-WC_EXIT_BIND);
571 memset(&addr, 0, sizeof(addr));
572 addr.sun_family = AF_UNIX;
573 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
575 s = socket(AF_UNIX, SOCK_STREAM, 0);
577 lprintf(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
578 return (-WC_EXIT_BIND);
581 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
582 lprintf(1, "webcit: Can't bind: %s\n",
584 return (-WC_EXIT_BIND);
587 if (listen(s, actual_queue_len) < 0) {
588 lprintf(1, "webcit: Can't listen: %s\n",
590 return (-WC_EXIT_BIND);
593 chmod(sockpath, 0777);
601 * Read data from the client socket.
603 * sock socket fd to read from
604 * buf buffer to read into
605 * bytes number of bytes to read
606 * timeout Number of seconds to wait before timing out
608 * Possible return values:
609 * 1 Requested number of bytes has been read.
610 * 0 Request timed out.
611 * -1 Connection is broken, or other error.
613 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
623 baselen = StrLength(Target);
625 if (Hdr->Pos == NULL)
626 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
627 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
629 if (bytes < bufremain)
631 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
632 StrBufCutLeft(Hdr->ReadBuf, bufremain);
634 if (bytes > bufremain)
636 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
638 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
640 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
642 write(2, "\033[32m", 5);
643 write(2, buf, bytes);
644 write(2, "\033[30m", 5);
649 lprintf(2, "client_read_ssl() failed\n");
658 retval = StrBufReadBLOBBuffered(Target,
667 lprintf(2, "client_read() failed: %s\n",
674 write(2, "\033[32m", 5);
675 write(2, buf, bytes);
676 write(2, "\033[30m", 5);
683 * Begin buffering HTTP output so we can transmit it all in one write operation later.
685 void begin_burst(void)
687 if (WC->WBuf == NULL) {
688 WC->WBuf = NewStrBufPlain(NULL, 32768);
694 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
699 const char *ptr, *eptr;
705 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
707 if (CompressBuffer(WCC->WBuf) > 0)
708 hprintf("Content-encoding: gzip\r\n");
710 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
715 if (WCC->WFBuf != NULL) {
716 WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
717 FreeStrBuf(&WCC->WFBuf);
720 if (WCC->Hdr->HR.prohibit_caching)
721 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
722 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
724 ptr = ChrPtr(WCC->HBuf);
725 count = StrLength(WCC->HBuf);
730 client_write_ssl(WCC->HBuf);
731 client_write_ssl(WCC->WBuf);
739 write(2, "\033[34m", 5);
740 write(2, ptr, StrLength(WCC->WBuf));
741 write(2, "\033[30m", 5);
743 if (WCC->Hdr->http_sock == -1)
745 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
747 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
748 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
750 FD_SET(WCC->Hdr->http_sock, &wset);
751 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
752 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
757 if ((WCC->Hdr->http_sock == -1) ||
758 (res = write(WCC->Hdr->http_sock,
761 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
769 ptr = ChrPtr(WCC->WBuf);
770 count = StrLength(WCC->WBuf);
775 write(2, "\033[34m", 5);
776 write(2, ptr, StrLength(WCC->WBuf));
777 write(2, "\033[30m", 5);
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 return StrLength(WCC->WBuf);
807 * lingering_close() a`la Apache. see
808 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
810 int lingering_close(int fd)
815 struct timeval tv, start;
817 gettimeofday(&start, NULL);
823 gettimeofday(&tv, NULL);
824 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
825 tv.tv_usec = start.tv_usec - tv.tv_usec;
826 if (tv.tv_usec < 0) {
828 tv.tv_usec += 1000000;
832 i = select(fd + 1, &set, NULL, NULL, &tv);
833 } while (i == -1 && errno == EINTR);
838 i = read(fd, buf, sizeof buf);
839 } while (i != 0 && (i != -1 || errno == EINTR));
845 HttpNewModule_TCPSOCKETS
846 (ParsedHttpHdrs *httpreq)
849 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
853 HttpDetachModule_TCPSOCKETS
854 (ParsedHttpHdrs *httpreq)
857 FlushStrBuf(httpreq->ReadBuf);
858 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
862 HttpDestroyModule_TCPSOCKETS
863 (ParsedHttpHdrs *httpreq)
866 FreeStrBuf(&httpreq->ReadBuf);
871 SessionNewModule_TCPSOCKETS
874 sess->CLineBuf = NewStrBuf();
875 sess->MigrateReadLineBuf = NewStrBuf();
879 SessionDestroyModule_TCPSOCKETS
882 FreeStrBuf(&sess->CLineBuf);
883 FreeStrBuf(&sess->ReadBuf);
884 sess->ReadPos = NULL;
885 FreeStrBuf(&sess->MigrateReadLineBuf);
886 if (sess->serv_sock > 0)
887 close(sess->serv_sock);