6 * Uncomment this to log all communications with the Citadel server
12 #include "webserver.h"
14 extern int DisableGzip;
17 * register the timeout
19 RETSIGTYPE timeout(int signum)
21 lprintf(1, "Connection timed out; unable to reach citserver\n");
22 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
27 * Connect a unix domain socket
28 * sockpath where to open a unix domain socket
30 int uds_connectsock(char *sockpath)
32 struct sockaddr_un addr;
35 memset(&addr, 0, sizeof(addr));
36 addr.sun_family = AF_UNIX;
37 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
39 s = socket(AF_UNIX, SOCK_STREAM, 0);
41 lprintf(1, "Can't create socket[%s]: %s\n",
47 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
48 lprintf(1, "Can't connect [%s]: %s\n",
60 * Connect a TCP/IP socket
61 * host the host to connect to
62 * service the service on the host to call
64 int tcp_connectsock(char *host, char *service)
70 struct sockaddr_in sin;
73 memset(&sin, 0, sizeof(sin));
74 sin.sin_family = AF_INET;
76 pse = getservbyname(service, "tcp");
78 sin.sin_port = pse->s_port;
79 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
80 lprintf(1, "Can't get %s service entry\n", service);
83 phe = gethostbyname(host);
85 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
86 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
87 lprintf(1, "Can't get %s host entry: %s\n",
88 host, strerror(errno));
91 if ((ppe = getprotobyname("tcp")) == 0) {
92 lprintf(1, "Can't get TCP protocol entry: %s\n",
97 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
99 lprintf(1, "Can't create socket: %s\n", strerror(errno));
103 fdflags = fcntl(s, F_GETFL);
105 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
106 host, service, strerror(errno));
107 fdflags = fdflags | O_NONBLOCK;
108 if (fcntl(s, F_SETFD, fdflags) < 0)
109 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
110 host, service, strerror(errno));
112 signal(SIGALRM, timeout);
115 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
116 lprintf(1, "Can't connect to %s.%s: %s\n",
117 host, service, strerror(errno));
122 signal(SIGALRM, SIG_IGN);
124 fdflags = fcntl(s, F_GETFL);
126 lprintf(1, "unable to get socket flags! %s.%s: %s \n",
127 host, service, strerror(errno));
128 fdflags = fdflags | O_NONBLOCK;
129 if (fcntl(s, F_SETFD, fdflags) < 0)
130 lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
131 host, service, strerror(errno));
138 * input string from pipe
140 int serv_getln(char *strbuf, int bufsize)
146 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
147 len = StrLength(WCC->MigrateReadLineBuf);
150 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
151 FlushStrBuf(WCC->MigrateReadLineBuf);
154 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
160 int StrBuf_ServGetln(StrBuf *buf)
163 const char *ErrStr = NULL;
166 rc = StrBufTCP_read_buffered_line_fast(buf,
174 lprintf(1, "Server connection broken: %s\n",
177 WCC->serv_sock = (-1);
184 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
190 rc = StrBufReadBLOBBuffered(buf,
200 lprintf(1, "Server connection broken: %s\n",
203 WCC->serv_sock = (-1);
210 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
217 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
220 lprintf(1, "Server connection broken: %s\n",
223 WCC->serv_sock = (-1);
231 * send binary to server
232 * buf the buffer to write to citadel server
233 * nbytes how many bytes to send to citadel server
235 void serv_write(const char *buf, int nbytes)
238 int bytes_written = 0;
241 FlushStrBuf(WCC->ReadBuf);
243 while (bytes_written < nbytes) {
244 retval = write(WCC->serv_sock, &buf[bytes_written],
245 nbytes - bytes_written);
247 const char *ErrStr = strerror(errno);
248 lprintf(1, "Server connection broken: %s\n",
250 close(WCC->serv_sock);
251 WCC->serv_sock = (-1);
256 bytes_written = bytes_written + retval;
262 * send line to server
263 * string the line to send to the citadel server
265 void serv_puts(const char *string)
269 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
271 FlushStrBuf(WCC->ReadBuf);
274 serv_write(string, strlen(string));
279 * send line to server
280 * string the line to send to the citadel server
282 void serv_putbuf(const StrBuf *string)
286 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
288 FlushStrBuf(WCC->ReadBuf);
291 serv_write(ChrPtr(string), StrLength(string));
297 * convenience function to send stuff to the server
298 * format the formatstring
299 * ... the entities to insert into format
301 void serv_printf(const char *format,...)
308 FlushStrBuf(WCC->ReadBuf);
311 va_start(arg_ptr, format);
312 vsnprintf(buf, sizeof buf, format, arg_ptr);
318 serv_write(buf, len);
320 lprintf(9, "<%s", buf);
327 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
329 const char *Error, *pch, *pchs;
330 int rlen, len, retval = 0;
335 if (StrLength(Hdr->ReadBuf) > 0) {
336 pchs = ChrPtr(Hdr->ReadBuf);
337 pch = strchr(pchs, '\n');
341 if (len > 0 && (*(pch - 1) == '\r') )
343 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
344 StrBufCutLeft(Hdr->ReadBuf, len + 1);
349 while (retval == 0) {
351 pchs = ChrPtr(Hdr->ReadBuf);
353 pch = strchr(pchs, '\n');
355 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
356 pchs = ChrPtr(Hdr->ReadBuf);
357 pch = strchr(pchs, '\n');
366 if ((retval > 0) && (pch != NULL)) {
369 if (len > 0 && (*(pch - 1) == '\r') )
371 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
372 StrBufCutLeft(Hdr->ReadBuf, len + 1);
381 return StrBufTCP_read_buffered_line_fast(Target,
391 * This is a generic function to set up a master socket for listening on
392 * a TCP port. The server shuts down if the bind fails.
394 * ip_addr IP address to bind
395 * port_number port number to bind
396 * queue_len number of incoming connections to allow in the queue
398 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
401 struct sockaddr_in sin;
404 memset(&sin, 0, sizeof(sin));
405 sin.sin_family = AF_INET;
406 if (ip_addr == NULL) {
407 sin.sin_addr.s_addr = INADDR_ANY;
409 sin.sin_addr.s_addr = inet_addr(ip_addr);
412 if (sin.sin_addr.s_addr == INADDR_NONE) {
413 sin.sin_addr.s_addr = INADDR_ANY;
416 if (port_number == 0) {
417 lprintf(1, "Cannot start: no port number specified.\n");
420 sin.sin_port = htons((u_short) port_number);
422 p = getprotobyname("tcp");
424 s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
426 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
429 /* Set some socket options that make sense. */
431 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
434 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
435 there should be a preceding F_GETFL
436 and a bitwise OR with the previous
440 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
441 lprintf(1, "Can't bind: %s\n", strerror(errno));
444 if (listen(s, queue_len) < 0) {
445 lprintf(1, "Can't listen: %s\n", strerror(errno));
454 * Create a Unix domain socket and listen on it
455 * sockpath - file name of the unix domain socket
456 * queue_len - Number of incoming connections to allow in the queue
458 int ig_uds_server(char *sockpath, int queue_len)
460 struct sockaddr_un addr;
463 int actual_queue_len;
465 actual_queue_len = queue_len;
466 if (actual_queue_len < 5) actual_queue_len = 5;
468 i = unlink(sockpath);
469 if ((i != 0) && (errno != ENOENT)) {
470 lprintf(1, "webcit: can't unlink %s: %s\n",
471 sockpath, strerror(errno));
475 memset(&addr, 0, sizeof(addr));
476 addr.sun_family = AF_UNIX;
477 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
479 s = socket(AF_UNIX, SOCK_STREAM, 0);
481 lprintf(1, "webcit: Can't create a socket: %s\n",
486 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
487 lprintf(1, "webcit: Can't bind: %s\n",
492 if (listen(s, actual_queue_len) < 0) {
493 lprintf(1, "webcit: Can't listen: %s\n",
498 chmod(sockpath, 0777);
506 * Read data from the client socket.
508 * sock socket fd to read from
509 * buf buffer to read into
510 * bytes number of bytes to read
511 * timeout Number of seconds to wait before timing out
513 * Possible return values:
514 * 1 Requested number of bytes has been read.
515 * 0 Request timed out.
516 * -1 Connection is broken, or other error.
518 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
527 if (Hdr->Pos == NULL)
528 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
529 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
531 if (bytes < bufremain)
533 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
534 StrBufCutLeft(Hdr->ReadBuf, bufremain);
536 if (bytes > bufremain)
538 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes) &&
540 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
542 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
544 write(2, "\033[32m", 5);
545 write(2, buf, bytes);
546 write(2, "\033[30m", 5);
551 lprintf(2, "client_read_ssl() failed\n");
560 retval = StrBufReadBLOBBuffered(Target,
569 lprintf(2, "client_read() failed: %s\n",
575 write(2, "\033[32m", 5);
576 write(2, buf, bytes);
577 write(2, "\033[30m", 5);
584 * Begin buffering HTTP output so we can transmit it all in one write operation later.
586 void begin_burst(void)
588 if (WC->WBuf == NULL) {
589 WC->WBuf = NewStrBufPlain(NULL, 32768);
595 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
600 const char *ptr, *eptr;
606 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
608 if (CompressBuffer(WCC->WBuf) > 0)
609 hprintf("Content-encoding: gzip\r\n");
611 lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
616 if (WCC->Hdr->HR.prohibit_caching)
617 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
618 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
620 ptr = ChrPtr(WCC->HBuf);
621 count = StrLength(WCC->HBuf);
626 client_write_ssl(WCC->HBuf);
627 client_write_ssl(WCC->WBuf);
635 write(2, "\033[34m", 5);
636 write(2, ptr, StrLength(WCC->WBuf));
637 write(2, "\033[30m", 5);
639 if (WCC->Hdr->http_sock == -1)
641 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
643 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
644 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
646 FD_SET(WCC->Hdr->http_sock, &wset);
647 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
648 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
653 if ((WCC->Hdr->http_sock == -1) ||
654 (res = write(WCC->Hdr->http_sock,
657 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
665 ptr = ChrPtr(WCC->WBuf);
666 count = StrLength(WCC->WBuf);
671 write(2, "\033[34m", 5);
672 write(2, ptr, StrLength(WCC->WBuf));
673 write(2, "\033[30m", 5);
676 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
677 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
679 FD_SET(WCC->Hdr->http_sock, &wset);
680 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
681 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
686 if ((WCC->Hdr->http_sock == -1) ||
687 (res = write(WCC->Hdr->http_sock,
690 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
698 return StrLength(WCC->WBuf);
703 * lingering_close() a`la Apache. see
704 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
706 int lingering_close(int fd)
711 struct timeval tv, start;
713 gettimeofday(&start, NULL);
719 gettimeofday(&tv, NULL);
720 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
721 tv.tv_usec = start.tv_usec - tv.tv_usec;
722 if (tv.tv_usec < 0) {
724 tv.tv_usec += 1000000;
728 i = select(fd + 1, &set, NULL, NULL, &tv);
729 } while (i == -1 && errno == EINTR);
734 i = read(fd, buf, sizeof buf);
735 } while (i != 0 && (i != -1 || errno == EINTR));
741 HttpNewModule_TCPSOCKETS
742 (ParsedHttpHdrs *httpreq)
745 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
749 HttpDetachModule_TCPSOCKETS
750 (ParsedHttpHdrs *httpreq)
753 FlushStrBuf(httpreq->ReadBuf);
754 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
758 HttpDestroyModule_TCPSOCKETS
759 (ParsedHttpHdrs *httpreq)
762 FreeStrBuf(&httpreq->ReadBuf);
767 SessionNewModule_TCPSOCKETS
770 sess->CLineBuf = NewStrBuf();
771 sess->MigrateReadLineBuf = NewStrBuf();
775 SessionDestroyModule_TCPSOCKETS
778 FreeStrBuf(&sess->CLineBuf);
779 FreeStrBuf(&sess->ReadBuf);
780 FreeStrBuf(&sess->MigrateReadLineBuf);
781 if (sess->serv_sock > 0)
782 close(sess->serv_sock);