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