* IPv6 experiment. -DCTDL_IPV6 to enable. Disabled otherwise.
[citadel.git] / webcit / tcp_sockets.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 1987-2010 by the citadel.org team
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /*
22  * Uncomment this to log all communications with the Citadel server
23 #define SERV_TRACE 1
24  */
25
26
27 #include "webcit.h"
28 #include "webserver.h"
29
30 extern int DisableGzip;
31 long MaxRead = -1; /* should we do READ scattered or all at once? */
32
33 /*
34  * register the timeout
35  */
36 RETSIGTYPE timeout(int signum)
37 {
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); */
40 }
41
42
43 /*
44  *  Connect a unix domain socket
45  *  sockpath where to open a unix domain socket
46  */
47 int uds_connectsock(char *sockpath)
48 {
49         struct sockaddr_un addr;
50         int s;
51
52         memset(&addr, 0, sizeof(addr));
53         addr.sun_family = AF_UNIX;
54         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
55
56         s = socket(AF_UNIX, SOCK_STREAM, 0);
57         if (s < 0) {
58                 lprintf(1, "Can't create socket[%s]: %s\n",
59                         sockpath,
60                         strerror(errno));
61                 return(-1);
62         }
63
64         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
65                 lprintf(1, "Can't connect [%s]: %s\n",
66                         sockpath,
67                         strerror(errno));
68                 close(s);
69                 return(-1);
70         }
71
72         return s;
73 }
74
75
76 /*
77  *  Connect a TCP/IP socket
78  *  host the host to connect to
79  *  service the service on the host to call
80  */
81 int tcp_connectsock(char *host, char *service)
82 {
83         int fdflags;
84         struct hostent *phe;
85         struct servent *pse;
86         struct protoent *ppe;
87         struct sockaddr_in sin;
88         int s;
89
90         memset(&sin, 0, sizeof(sin));
91         sin.sin_family = AF_INET;
92
93         pse = getservbyname(service, "tcp");
94         if (pse) {
95                 sin.sin_port = pse->s_port;
96         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
97                 lprintf(1, "Can't get %s service entry\n", service);
98                 return (-1);
99         }
100         phe = gethostbyname(host);
101         if (phe) {
102                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
103         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
104                 lprintf(1, "Can't get %s host entry: %s\n",
105                         host, strerror(errno));
106                 return (-1);
107         }
108         if ((ppe = getprotobyname("tcp")) == 0) {
109                 lprintf(1, "Can't get TCP protocol entry: %s\n",
110                         strerror(errno));
111                 return (-1);
112         }
113
114         s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
115         if (s < 0) {
116                 lprintf(1, "Can't create socket: %s\n", strerror(errno));
117                 return (-1);
118         }
119
120         fdflags = fcntl(s, F_GETFL);
121         if (fdflags < 0)
122                 lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
123                         host, service, strerror(errno));
124         fdflags = fdflags | O_NONBLOCK;
125         if (fcntl(s, F_SETFD, fdflags) < 0)
126                 lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
127                         host, service, strerror(errno));
128
129         signal(SIGALRM, timeout);
130         alarm(30);
131
132         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
133                 lprintf(1, "Can't connect to %s.%s: %s\n",
134                         host, service, strerror(errno));
135                 close(s);
136                 return (-1);
137         }
138         alarm(0);
139         signal(SIGALRM, SIG_IGN);
140         if (!is_https) {
141                 fdflags = fcntl(s, F_GETFL);
142                 if (fdflags < 0)
143                         lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
144                                 host, service, strerror(errno));
145                 fdflags = fdflags | O_NONBLOCK;
146                 if (fcntl(s, F_SETFD, fdflags) < 0)
147                         lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
148                                 host, service, strerror(errno));
149         }
150         return (s);
151 }
152
153
154
155 /*
156  *  input string from pipe
157  */
158 int serv_getln(char *strbuf, int bufsize)
159 {
160         wcsession *WCC = WC;
161         int len;
162
163         *strbuf = '\0';
164         StrBuf_ServGetln(WCC->MigrateReadLineBuf);
165         len = StrLength(WCC->MigrateReadLineBuf);
166         if (len > bufsize)
167                 len = bufsize - 1;
168         memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
169         FlushStrBuf(WCC->MigrateReadLineBuf);
170         strbuf[len] = '\0';
171 #ifdef SERV_TRACE
172         lprintf(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
173 #endif
174         return len;
175 }
176
177
178 int StrBuf_ServGetln(StrBuf *buf)
179 {
180         wcsession *WCC = WC;
181         const char *ErrStr = NULL;
182         int rc;
183         
184         FlushStrBuf(buf);
185         rc = StrBufTCP_read_buffered_line_fast(buf, 
186                                                WCC->ReadBuf, 
187                                                &WCC->ReadPos, 
188                                                &WCC->serv_sock, 
189                                                5, 1, 
190                                                &ErrStr);
191         if (rc < 0)
192         {
193                 lprintf(1, "Server connection broken: %s\n",
194                         (ErrStr)?ErrStr:"");
195                 wc_backtrace();
196                 WCC->serv_sock = (-1);
197                 WCC->connected = 0;
198                 WCC->logged_in = 0;
199         }
200 #ifdef SERV_TRACE
201         else 
202         {
203                 long pos=0;
204                 if (WCC->ReadPos != NULL)
205                         pos = WCC->ReadPos - ChrPtr(buf);
206                 lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
207         }
208 #endif
209         return rc;
210 }
211
212 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
213 {
214         wcsession *WCC = WC;
215         const char *ErrStr;
216         int rc;
217         
218         rc = StrBufReadBLOBBuffered(buf, 
219                                     WCC->ReadBuf, 
220                                     &WCC->ReadPos,
221                                     &WCC->serv_sock, 
222                                     1, 
223                                     BlobSize, 
224                                     NNN_TERM,
225                                     &ErrStr);
226         if (rc < 0)
227         {
228                 lprintf(1, "Server connection broken: %s\n",
229                         (ErrStr)?ErrStr:"");
230                 wc_backtrace();
231                 WCC->serv_sock = (-1);
232                 WCC->connected = 0;
233                 WCC->logged_in = 0;
234         }
235 #ifdef SERV_TRACE
236         else
237                 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
238 #endif
239
240         return rc;
241 }
242
243 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
244 {
245         wcsession *WCC = WC;
246         const char *ErrStr;
247         int rc;
248         
249         WCC->ReadPos = NULL;
250         rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
251         if (rc < 0)
252         {
253                 lprintf(1, "Server connection broken: %s\n",
254                         (ErrStr)?ErrStr:"");
255                 wc_backtrace();
256                 WCC->serv_sock = (-1);
257                 WCC->connected = 0;
258                 WCC->logged_in = 0;
259         }
260 #ifdef SERV_TRACE
261         else
262                 lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
263 #endif
264
265         return rc;
266 }
267
268 /*
269  *  send binary to server
270  *  buf the buffer to write to citadel server
271  *  nbytes how many bytes to send to citadel server
272  */
273 void serv_write(const char *buf, int nbytes)
274 {
275         wcsession *WCC = WC;
276         int bytes_written = 0;
277         int retval;
278
279         FlushStrBuf(WCC->ReadBuf);
280         WCC->ReadPos = NULL;
281         while (bytes_written < nbytes) {
282                 retval = write(WCC->serv_sock, &buf[bytes_written],
283                                nbytes - bytes_written);
284                 if (retval < 1) {
285                         const char *ErrStr = strerror(errno);
286                         lprintf(1, "Server connection broken: %s\n",
287                                 (ErrStr)?ErrStr:"");
288                         close(WCC->serv_sock);
289                         WCC->serv_sock = (-1);
290                         WCC->connected = 0;
291                         WCC->logged_in = 0;
292                         return;
293                 }
294                 bytes_written = bytes_written + retval;
295         }
296 }
297
298
299 /*
300  *  send line to server
301  *  string the line to send to the citadel server
302  */
303 void serv_puts(const char *string)
304 {
305         wcsession *WCC = WC;
306 #ifdef SERV_TRACE
307         lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
308 #endif
309         FlushStrBuf(WCC->ReadBuf);
310         WCC->ReadPos = NULL;
311
312         serv_write(string, strlen(string));
313         serv_write("\n", 1);
314 }
315
316 /*
317  *  send line to server
318  *  string the line to send to the citadel server
319  */
320 void serv_putbuf(const StrBuf *string)
321 {
322         wcsession *WCC = WC;
323 #ifdef SERV_TRACE
324         lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
325 #endif
326         FlushStrBuf(WCC->ReadBuf);
327         WCC->ReadPos = NULL;
328
329         serv_write(ChrPtr(string), StrLength(string));
330         serv_write("\n", 1);
331 }
332
333
334 /*
335  *  convenience function to send stuff to the server
336  *  format the formatstring
337  *  ... the entities to insert into format 
338  */
339 void serv_printf(const char *format,...)
340 {
341         wcsession *WCC = WC;
342         va_list arg_ptr;
343         char buf[SIZ];
344         size_t len;
345
346         FlushStrBuf(WCC->ReadBuf);
347         WCC->ReadPos = NULL;
348
349         va_start(arg_ptr, format);
350         vsnprintf(buf, sizeof buf, format, arg_ptr);
351         va_end(arg_ptr);
352
353         len = strlen(buf);
354         buf[len++] = '\n';
355         buf[len] = '\0';
356         serv_write(buf, len);
357 #ifdef SERV_TRACE
358         lprintf(9, ">>>%s", buf);
359 #endif
360 }
361
362
363
364 /**
365  * Read binary data from server into memory using a series of
366  * server READ commands.
367  * \return the read content as StrBuf
368  */
369 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
370 {
371         wcsession *WCC = WC;
372         size_t bytes = 0;
373         size_t thisblock = 0;
374         
375         if (Ret == NULL)
376             return -1;
377
378         if (MaxRead == -1)
379         {
380                 serv_printf("READ %d|%d", 0, total_len);
381                 if (StrBuf_ServGetln(Buf) > 0)
382                 {
383                         long YetRead;
384                         const char *ErrStr;
385                         const char *pch;
386                         int rc;
387
388                         if (GetServerStatus(Buf, NULL) == 6)
389                         {
390                             StrBufCutLeft(Buf, 4);
391                             thisblock = StrTol(Buf);
392                             if (WCC->serv_sock==-1) {
393                                     FlushStrBuf(Ret); 
394                                     return -1; 
395                             }
396
397                             pch = ChrPtr(WCC->ReadBuf);
398                             YetRead = WCC->ReadPos - pch;
399                             if (YetRead > 0)
400                             {
401                                     long StillThere;
402                                     
403                                     StillThere = StrLength(WCC->ReadBuf) - 
404                                             YetRead;
405
406                                     StrBufPlain(Ret, 
407                                                 WCC->ReadPos,
408                                                 StillThere);
409                                     total_len -= StillThere;
410                             }
411                             FlushStrBuf(WCC->ReadBuf);
412                             WCC->ReadPos = NULL;
413                             
414                             if (total_len > 0)
415                             {
416                                     rc = StrBufReadBLOB(Ret, 
417                                                         &WCC->serv_sock, 
418                                                         1, 
419                                                         total_len,
420                                                         &ErrStr);
421                                     if (rc < 0)
422                                     {
423                                             lprintf(1, "Server connection broken: %s\n",
424                                                     (ErrStr)?ErrStr:"");
425                                             wc_backtrace();
426                                             WCC->serv_sock = (-1);
427                                             WCC->connected = 0;
428                                             WCC->logged_in = 0;
429                                             return rc;
430                                     }
431                                     else
432                                             return StrLength(Ret);
433                             }
434                             else 
435                                     return StrLength(Ret);
436                         }
437                 }
438                 else
439                         return -1;
440         }
441         else while ((WCC->serv_sock!=-1) &&
442                (bytes < total_len)) {
443                 thisblock = MaxRead;
444                 if ((total_len - bytes) < thisblock) {
445                         thisblock = total_len - bytes;
446                         if (thisblock == 0) {
447                                 FlushStrBuf(Ret); 
448                                 return -1; 
449                         }
450                 }
451                 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
452                 if (StrBuf_ServGetln(Buf) > 0)
453                 {
454                         if (GetServerStatus(Buf, NULL) == 6)
455                         {
456                             StrBufCutLeft(Buf, 4);
457                             thisblock = StrTol(Buf);
458                             if (WCC->serv_sock==-1) {
459                                     FlushStrBuf(Ret); 
460                                     return -1; 
461                             }
462                             StrBuf_ServGetBLOBBuffered(Ret, thisblock);
463                             bytes += thisblock;
464                     }
465                     else {
466                             lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
467                             return -1;
468                     }
469                 }
470         }
471         return StrLength(Ret);
472 }
473
474
475 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
476 {
477         const char *Error;
478 #ifdef HAVE_OPENSSL
479         const char *pch, *pchs;
480         int rlen, len, retval = 0;
481
482         if (is_https) {
483                 int ntries = 0;
484                 if (StrLength(Hdr->ReadBuf) > 0) {
485                         pchs = ChrPtr(Hdr->ReadBuf);
486                         pch = strchr(pchs, '\n');
487                         if (pch != NULL) {
488                                 rlen = 0;
489                                 len = pch - pchs;
490                                 if (len > 0 && (*(pch - 1) == '\r') )
491                                         rlen ++;
492                                 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
493                                 StrBufCutLeft(Hdr->ReadBuf, len + 1);
494                                 return len - rlen;
495                         }
496                 }
497
498                 while (retval == 0) { 
499                                 pch = NULL;
500                                 pchs = ChrPtr(Hdr->ReadBuf);
501                                 if (*pchs != '\0')
502                                         pch = strchr(pchs, '\n');
503                                 if (pch == NULL) {
504                                         retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
505                                         pchs = ChrPtr(Hdr->ReadBuf);
506                                         pch = strchr(pchs, '\n');
507                                 }
508                                 if (retval == 0) {
509                                         sleeeeeeeeeep(1);
510                                         ntries ++;
511                                 }
512                                 if (ntries > 10)
513                                         return 0;
514                 }
515                 if ((retval > 0) && (pch != NULL)) {
516                         rlen = 0;
517                         len = pch - pchs;
518                         if (len > 0 && (*(pch - 1) == '\r') )
519                                 rlen ++;
520                         StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
521                         StrBufCutLeft(Hdr->ReadBuf, len + 1);
522                         return len - rlen;
523
524                 }
525                 else 
526                         return -1;
527         }
528         else 
529 #endif
530                 return StrBufTCP_read_buffered_line_fast(Target, 
531                                                          Hdr->ReadBuf,
532                                                          &Hdr->Pos,
533                                                          &Hdr->http_sock,
534                                                          5,
535                                                          1,
536                                                          &Error);
537 }
538
539 #ifdef CTDL_IPV6
540
541 /* 
542  * This is a generic function to set up a master socket for listening on
543  * a TCP port.  The server shuts down if the bind fails.  (IPv4/IPv6 version)
544  *
545  * ip_addr      IP address to bind
546  * port_number  port number to bind
547  * queue_len    number of incoming connections to allow in the queue
548  */
549 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
550 {
551         struct protoent *p;
552         struct sockaddr_in6 sin;
553         int s, i;
554
555         memset(&sin, 0, sizeof(sin));
556         sin.sin6_family = AF_INET6;
557
558         if ((ip_addr == NULL) || (IsEmptyStr(ip_addr)) || (!strcmp(ip_addr, "0.0.0.0"))) {
559                 sin.sin6_addr = in6addr_any;
560         } else {
561                 char bind_to[256];
562                 if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) {
563                         snprintf(bind_to, sizeof bind_to, "::ffff:%s", ip_addr);
564                 }
565                 else {
566                         safestrncpy(bind_to, ip_addr, sizeof bind_to);
567                 }
568                 if (inet_pton(AF_INET6, bind_to, &sin.sin6_addr) <= 0) {
569                         lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
570                         abort();
571                 }
572         }
573
574         if (port_number == 0) {
575                 lprintf(1, "Cannot start: no port number specified.\n");
576                 return (-WC_EXIT_BIND);
577         }
578         sin.sin6_port = htons((u_short) port_number);
579
580         p = getprotobyname("tcp");
581
582         s = socket(PF_INET6, SOCK_STREAM, (p->p_proto));
583         if (s < 0) {
584                 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
585                 return (-WC_EXIT_BIND);
586         }
587         /* Set some socket options that make sense. */
588         i = 1;
589         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
590
591         #ifndef __APPLE__
592         fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
593                                           there should be a preceding F_GETFL
594                                           and a bitwise OR with the previous
595                                           fd flags */
596         #endif
597         
598         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
599                 lprintf(1, "Can't bind: %s\n", strerror(errno));
600                 return (-WC_EXIT_BIND);
601         }
602         if (listen(s, queue_len) < 0) {
603                 lprintf(1, "Can't listen: %s\n", strerror(errno));
604                 return (-WC_EXIT_BIND);
605         }
606         return (s);
607 }
608
609 #else /* CTDL_IPV6 */
610
611 /* 
612  * This is a generic function to set up a master socket for listening on
613  * a TCP port.  The server shuts down if the bind fails.
614  *
615  * ip_addr      IP address to bind
616  * port_number  port number to bind
617  * queue_len    number of incoming connections to allow in the queue
618  */
619 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
620 {
621         struct protoent *p;
622         struct sockaddr_in sin;
623         int s, i;
624
625         memset(&sin, 0, sizeof(sin));
626         sin.sin_family = AF_INET;
627         if (ip_addr == NULL) {
628                 sin.sin_addr.s_addr = INADDR_ANY;
629         } else {
630                 sin.sin_addr.s_addr = inet_addr(ip_addr);
631         }
632
633         if (sin.sin_addr.s_addr == INADDR_NONE) {
634                 sin.sin_addr.s_addr = INADDR_ANY;
635         }
636
637         if (port_number == 0) {
638                 lprintf(1, "Cannot start: no port number specified.\n");
639                 return (-WC_EXIT_BIND);
640         }
641         sin.sin_port = htons((u_short) port_number);
642
643         p = getprotobyname("tcp");
644
645         s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
646         if (s < 0) {
647                 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
648                 return (-WC_EXIT_BIND);
649         }
650         /* Set some socket options that make sense. */
651         i = 1;
652         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
653
654         #ifndef __APPLE__
655         fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
656                                           there should be a preceding F_GETFL
657                                           and a bitwise OR with the previous
658                                           fd flags */
659         #endif
660         
661         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
662                 lprintf(1, "Can't bind: %s\n", strerror(errno));
663                 return (-WC_EXIT_BIND);
664         }
665         if (listen(s, queue_len) < 0) {
666                 lprintf(1, "Can't listen: %s\n", strerror(errno));
667                 return (-WC_EXIT_BIND);
668         }
669         return (s);
670 }
671
672 #endif /* CTDL_IPV6 */
673
674
675 /*
676  * Create a Unix domain socket and listen on it
677  * sockpath - file name of the unix domain socket
678  * queue_len - Number of incoming connections to allow in the queue
679  */
680 int ig_uds_server(char *sockpath, int queue_len)
681 {
682         struct sockaddr_un addr;
683         int s;
684         int i;
685         int actual_queue_len;
686
687         actual_queue_len = queue_len;
688         if (actual_queue_len < 5) actual_queue_len = 5;
689
690         i = unlink(sockpath);
691         if ((i != 0) && (errno != ENOENT)) {
692                 lprintf(1, "webcit: can't unlink %s: %s\n",
693                         sockpath, strerror(errno));
694                 return (-WC_EXIT_BIND);
695         }
696
697         memset(&addr, 0, sizeof(addr));
698         addr.sun_family = AF_UNIX;
699         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
700
701         s = socket(AF_UNIX, SOCK_STREAM, 0);
702         if (s < 0) {
703                 lprintf(1, "webcit: Can't create a socket: %s\n",
704                         strerror(errno));
705                 return (-WC_EXIT_BIND);
706         }
707
708         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
709                 lprintf(1, "webcit: Can't bind: %s\n",
710                         strerror(errno));
711                 return (-WC_EXIT_BIND);
712         }
713
714         if (listen(s, actual_queue_len) < 0) {
715                 lprintf(1, "webcit: Can't listen: %s\n",
716                         strerror(errno));
717                 return (-WC_EXIT_BIND);
718         }
719
720         chmod(sockpath, 0777);
721         return(s);
722 }
723
724
725
726
727 /*
728  * Read data from the client socket.
729  *
730  * sock         socket fd to read from
731  * buf          buffer to read into 
732  * bytes        number of bytes to read
733  * timeout      Number of seconds to wait before timing out
734  *
735  * Possible return values:
736  *      1       Requested number of bytes has been read.
737  *      0       Request timed out.
738  *      -1      Connection is broken, or other error.
739  */
740 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
741 {
742         const char *Error;
743         int retval = 0;
744
745 #ifdef HAVE_OPENSSL
746         if (is_https) {
747                 long bufremain;
748                 long baselen;
749
750                 baselen = StrLength(Target);
751
752                 if (Hdr->Pos == NULL)
753                         Hdr->Pos = ChrPtr(Hdr->ReadBuf);
754                 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
755
756                 if (bytes < bufremain)
757                         bufremain = bytes;
758                 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
759                 StrBufCutLeft(Hdr->ReadBuf, bufremain);
760
761                 if (bytes > bufremain) 
762                 {
763                         while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
764                                (retval >= 0))
765                                 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
766                         if (retval >= 0) {
767                                 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
768 #ifdef HTTP_TRACING
769                                 write(2, "\033[32m", 5);
770                                 write(2, buf, bytes);
771                                 write(2, "\033[30m", 5);
772 #endif
773                                 return 1;
774                         }
775                         else {
776                                 lprintf(2, "client_read_ssl() failed\n");
777                                 return -1;
778                         }
779                 }
780                 else 
781                         return 1;
782         }
783 #endif
784
785         retval = StrBufReadBLOBBuffered(Target, 
786                                         Hdr->ReadBuf, 
787                                         &Hdr->Pos, 
788                                         &Hdr->http_sock, 
789                                         1, 
790                                         bytes,
791                                         O_TERM,
792                                         &Error);
793         if (retval < 0) {
794                 lprintf(2, "client_read() failed: %s\n",
795                         Error);
796                 return retval;
797         }
798
799 #ifdef HTTP_TRACING
800         write(2, "\033[32m", 5);
801         write(2, buf, bytes);
802         write(2, "\033[30m", 5);
803 #endif
804         return 1;
805 }
806
807
808 /*
809  * Begin buffering HTTP output so we can transmit it all in one write operation later.
810  */
811 void begin_burst(void)
812 {
813         if (WC->WBuf == NULL) {
814                 WC->WBuf = NewStrBufPlain(NULL, 32768);
815         }
816 }
817
818
819 /*
820  * Finish buffering HTTP output.  [Compress using zlib and] output with a Content-Length: header.
821  */
822 long end_burst(void)
823 {
824         wcsession *WCC = WC;
825         const char *ptr, *eptr;
826         long count;
827         ssize_t res = 0;
828         fd_set wset;
829         int fdflags;
830
831         if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
832         {
833                 if (CompressBuffer(WCC->WBuf) > 0)
834                         hprintf("Content-encoding: gzip\r\n");
835                 else {
836                         lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
837                         wc_backtrace();
838                 }
839         }
840
841         if (WCC->Hdr->HR.prohibit_caching)
842                 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
843         hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
844
845         ptr = ChrPtr(WCC->HBuf);
846         count = StrLength(WCC->HBuf);
847         eptr = ptr + count;
848
849 #ifdef HAVE_OPENSSL
850         if (is_https) {
851                 client_write_ssl(WCC->HBuf);
852                 client_write_ssl(WCC->WBuf);
853                 return (count);
854         }
855 #endif
856
857         
858 #ifdef HTTP_TRACING
859         
860         write(2, "\033[34m", 5);
861         write(2, ptr, StrLength(WCC->WBuf));
862         write(2, "\033[30m", 5);
863 #endif
864         if (WCC->Hdr->http_sock == -1)
865                 return -1;
866         fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
867
868         while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
869                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
870                         FD_ZERO(&wset);
871                         FD_SET(WCC->Hdr->http_sock, &wset);
872                         if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
873                                 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
874                                 return -1;
875                         }
876                 }
877
878                 if ((WCC->Hdr->http_sock == -1) || 
879                     (res = write(WCC->Hdr->http_sock, 
880                                  ptr,
881                                  count)) == -1) {
882                         lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
883                         wc_backtrace();
884                         return res;
885                 }
886                 count -= res;
887                 ptr += res;
888         }
889
890         ptr = ChrPtr(WCC->WBuf);
891         count = StrLength(WCC->WBuf);
892         eptr = ptr + count;
893
894 #ifdef HTTP_TRACING
895         
896         write(2, "\033[34m", 5);
897         write(2, ptr, StrLength(WCC->WBuf));
898         write(2, "\033[30m", 5);
899 #endif
900
901         while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
902                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
903                         FD_ZERO(&wset);
904                         FD_SET(WCC->Hdr->http_sock, &wset);
905                         if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
906                                 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
907                                 return -1;
908                         }
909                 }
910
911                 if ((WCC->Hdr->http_sock == -1) || 
912                     (res = write(WCC->Hdr->http_sock, 
913                                  ptr,
914                                  count)) == -1) {
915                         lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
916                         wc_backtrace();
917                         return res;
918                 }
919                 count -= res;
920                 ptr += res;
921         }
922
923         return StrLength(WCC->WBuf);
924 }
925
926
927 /*
928  * lingering_close() a`la Apache. see
929  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
930  */
931 int lingering_close(int fd)
932 {
933         char buf[SIZ];
934         int i;
935         fd_set set;
936         struct timeval tv, start;
937
938         gettimeofday(&start, NULL);
939         if (fd == -1)
940                 return -1;
941         shutdown(fd, 1);
942         do {
943                 do {
944                         gettimeofday(&tv, NULL);
945                         tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
946                         tv.tv_usec = start.tv_usec - tv.tv_usec;
947                         if (tv.tv_usec < 0) {
948                                 tv.tv_sec--;
949                                 tv.tv_usec += 1000000;
950                         }
951                         FD_ZERO(&set);
952                         FD_SET(fd, &set);
953                         i = select(fd + 1, &set, NULL, NULL, &tv);
954                 } while (i == -1 && errno == EINTR);
955
956                 if (i <= 0)
957                         break;
958
959                 i = read(fd, buf, sizeof buf);
960         } while (i != 0 && (i != -1 || errno == EINTR));
961
962         return close(fd);
963 }
964
965 void
966 HttpNewModule_TCPSOCKETS
967 (ParsedHttpHdrs *httpreq)
968 {
969
970         httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
971 }
972
973 void
974 HttpDetachModule_TCPSOCKETS
975 (ParsedHttpHdrs *httpreq)
976 {
977
978         FlushStrBuf(httpreq->ReadBuf);
979         ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
980 }
981
982 void
983 HttpDestroyModule_TCPSOCKETS
984 (ParsedHttpHdrs *httpreq)
985 {
986
987         FreeStrBuf(&httpreq->ReadBuf);
988 }
989
990
991 void
992 SessionNewModule_TCPSOCKETS
993 (wcsession *sess)
994 {
995         sess->CLineBuf = NewStrBuf();
996         sess->MigrateReadLineBuf = NewStrBuf();
997 }
998
999 void 
1000 SessionDestroyModule_TCPSOCKETS
1001 (wcsession *sess)
1002 {
1003         FreeStrBuf(&sess->CLineBuf);
1004         FreeStrBuf(&sess->ReadBuf);
1005         sess->ReadPos = NULL;
1006         FreeStrBuf(&sess->MigrateReadLineBuf);
1007         if (sess->serv_sock > 0)
1008                 close(sess->serv_sock);
1009 }