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