removed a bunch of blank comment lines
[citadel.git] / webcit / tcp_sockets.c
index 6f32d66ef108ad36f24c4a843fff77a0f96280df..e45231f56a3dd21eba074192ca3facc0d4012ae8 100644 (file)
@@ -1,21 +1,13 @@
 /*
- * $Id$
- *
- * Copyright (c) 1987-2010 by the citadel.org team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
+ * Copyright (c) 1987-2012 by the citadel.org team
  *
+ * This program is open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 3.
+ * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 /*
 #define SERV_TRACE 1
  */
 
-
 #include "webcit.h"
 #include "webserver.h"
 
-extern int DisableGzip;
 long MaxRead = -1; /* should we do READ scattered or all at once? */
 
 /*
@@ -35,7 +25,7 @@ long MaxRead = -1; /* should we do READ scattered or all at once? */
  */
 RETSIGTYPE timeout(int signum)
 {
-       lprintf(1, "Connection timed out; unable to reach citserver\n");
+       syslog(1, "Connection timed out; unable to reach citserver\n");
        /* no exit here, since we need to server the connection unreachable thing. exit(3); */
 }
 
@@ -54,12 +44,12 @@ int uds_connectsock(char *sockpath)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               lprintf(1, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
+               syslog(1, "Can't create socket [%s]: %s\n", sockpath, strerror(errno));
                return(-1);
        }
 
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               lprintf(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
+               syslog(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
                close(s);
                return(-1);
        }
@@ -85,7 +75,7 @@ int tcp_connectsock(char *host, char *service)
        if ((service == NULL) || IsEmptyStr(service))
                return (-1);
 
-       lprintf(9, "tcp_connectsock(%s,%s)\n", host, service);
+       syslog(9, "tcp_connectsock(%s,%s)\n", host, service);
 
        memset(&hints, 0x00, sizeof(hints));
        hints.ai_flags = AI_NUMERICSERV;
@@ -111,7 +101,8 @@ int tcp_connectsock(char *host, char *service)
 
        rc = getaddrinfo(host, service, &hints, &res);
        if (rc != 0) {
-               lprintf(1, "%s: %s\n", host, gai_strerror(rc));
+               syslog(1, "%s: %s\n", host, gai_strerror(rc));
+               freeaddrinfo(res);
                return(-1);
        }
 
@@ -120,25 +111,27 @@ int tcp_connectsock(char *host, char *service)
         */
        for (ai = res; ai != NULL; ai = ai->ai_next) {
 
-               if (ai->ai_family == AF_INET) lprintf(9, "Trying IPv4\n");
-               else if (ai->ai_family == AF_INET6) lprintf(9, "Trying IPv6\n");
-               else lprintf(9, "This is going to fail.\n");
+               if (ai->ai_family == AF_INET) syslog(9, "Trying IPv4\n");
+               else if (ai->ai_family == AF_INET6) syslog(9, "Trying IPv6\n");
+               else syslog(9, "This is going to fail.\n");
 
                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
                if (s < 0) {
-                       lprintf(1, "socket() failed: %s\n", strerror(errno));
+                       syslog(1, "socket() failed: %s\n", strerror(errno));
+                       freeaddrinfo(res);
                        return(-1);
                }
                rc = connect(s, ai->ai_addr, ai->ai_addrlen);
                if (rc >= 0) {
+                       freeaddrinfo(res);
                        return(s);
                }
                else {
-                       lprintf(1, "connect() failed: %s\n", strerror(errno));
+                       syslog(1, "connect() failed: %s\n", strerror(errno));
                        close(s);
                }
        }
-
+        freeaddrinfo(res);
        return(-1);
 }
 
@@ -160,7 +153,7 @@ int serv_getln(char *strbuf, int bufsize)
        FlushStrBuf(WCC->MigrateReadLineBuf);
        strbuf[len] = '\0';
 #ifdef SERV_TRACE
-       lprintf(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
+       syslog(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
 #endif
        return len;
 }
@@ -172,6 +165,9 @@ int StrBuf_ServGetln(StrBuf *buf)
        const char *ErrStr = NULL;
        int rc;
        
+       if (!WCC->connected)
+               return -1;
+
        FlushStrBuf(buf);
        rc = StrBufTCP_read_buffered_line_fast(buf, 
                                               WCC->ReadBuf, 
@@ -181,9 +177,10 @@ int StrBuf_ServGetln(StrBuf *buf)
                                               &ErrStr);
        if (rc < 0)
        {
-               lprintf(1, "Server connection broken: %s\n",
+               syslog(1, "StrBuf_ServGetln(): Server connection broken: %s\n",
                        (ErrStr)?ErrStr:"");
                wc_backtrace();
+               if (WCC->serv_sock > 0) close(WCC->serv_sock);
                WCC->serv_sock = (-1);
                WCC->connected = 0;
                WCC->logged_in = 0;
@@ -194,7 +191,7 @@ int StrBuf_ServGetln(StrBuf *buf)
                long pos = 0;
                if (WCC->ReadPos != NULL)
                        pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
-               lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
+               syslog(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
        }
 #endif
        return rc;
@@ -216,16 +213,17 @@ int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
                                    &ErrStr);
        if (rc < 0)
        {
-               lprintf(1, "Server connection broken: %s\n",
+               syslog(1, "StrBuf_ServGetBLOBBuffered(): Server connection broken: %s\n",
                        (ErrStr)?ErrStr:"");
                wc_backtrace();
+               if (WCC->serv_sock > 0) close(WCC->serv_sock);
                WCC->serv_sock = (-1);
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
 #ifdef SERV_TRACE
         else
-                lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
+                syslog(9, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
 #endif
 
        return rc;
@@ -241,16 +239,17 @@ int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
        rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
        if (rc < 0)
        {
-               lprintf(1, "Server connection broken: %s\n",
+               syslog(1, "StrBuf_ServGetBLOB(): Server connection broken: %s\n",
                        (ErrStr)?ErrStr:"");
                wc_backtrace();
+               if (WCC->serv_sock > 0) close(WCC->serv_sock);
                WCC->serv_sock = (-1);
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
 #ifdef SERV_TRACE
         else
-                lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
+                syslog(9, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
 #endif
 
        return rc;
@@ -274,9 +273,12 @@ void FlushReadBuf (void)
                pche = pch + len;
                if (WCC->ReadPos != pche)
                {
-                       lprintf(1, "ERROR: somebody didn't eat his soup! Remaing Chars: %d [%s]\n", 
-                               pche - WCC->ReadPos, pche);
-                       lprintf(1, 
+                       syslog(1,
+                               "ERROR: somebody didn't eat his soup! Remaing Chars: %ld [%s]\n", 
+                               (long)(pche - WCC->ReadPos),
+                               pche
+                       );
+                       syslog(1, 
                                "--------------------------------------------------------------------------------\n"
                                "Whole buf: [%s]\n"
                                "--------------------------------------------------------------------------------\n", 
@@ -297,7 +299,7 @@ void FlushReadBuf (void)
  *  buf the buffer to write to citadel server
  *  nbytes how many bytes to send to citadel server
  */
-void serv_write(const char *buf, int nbytes)
+int serv_write(const char *buf, int nbytes)
 {
        wcsession *WCC = WC;
        int bytes_written = 0;
@@ -309,16 +311,17 @@ void serv_write(const char *buf, int nbytes)
                               nbytes - bytes_written);
                if (retval < 1) {
                        const char *ErrStr = strerror(errno);
-                       lprintf(1, "Server connection broken: %s\n",
+                       syslog(1, "serv_write(): Server connection broken: %s\n",
                                (ErrStr)?ErrStr:"");
-                       close(WCC->serv_sock);
+                       if (WCC->serv_sock > 0) close(WCC->serv_sock);
                        WCC->serv_sock = (-1);
                        WCC->connected = 0;
                        WCC->logged_in = 0;
-                       return;
+                       return 0;
                }
                bytes_written = bytes_written + retval;
        }
+       return 1;
 }
 
 
@@ -326,30 +329,32 @@ void serv_write(const char *buf, int nbytes)
  *  send line to server
  *  string the line to send to the citadel server
  */
-void serv_puts(const char *string)
+int serv_puts(const char *string)
 {
 #ifdef SERV_TRACE
-       lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
+       syslog(9, "%3d>>>%s\n", WC->serv_sock, string);
 #endif
        FlushReadBuf();
 
-       serv_write(string, strlen(string));
-       serv_write("\n", 1);
+       if (!serv_write(string, strlen(string)))
+               return 0;
+       return serv_write("\n", 1);
 }
 
 /*
  *  send line to server
  *  string the line to send to the citadel server
  */
-void serv_putbuf(const StrBuf *string)
+int serv_putbuf(const StrBuf *string)
 {
 #ifdef SERV_TRACE
-       lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
+       syslog(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
 #endif
        FlushReadBuf();
 
-       serv_write(ChrPtr(string), StrLength(string));
-       serv_write("\n", 1);
+       if (!serv_write(ChrPtr(string), StrLength(string)))
+               return 0;
+       return serv_write("\n", 1);
 }
 
 
@@ -358,11 +363,12 @@ void serv_putbuf(const StrBuf *string)
  *  format the formatstring
  *  ... the entities to insert into format 
  */
-void serv_printf(const char *format,...)
+int serv_printf(const char *format,...)
 {
        va_list arg_ptr;
        char buf[SIZ];
        size_t len;
+       int rc;
 
        FlushReadBuf();
 
@@ -373,123 +379,57 @@ void serv_printf(const char *format,...)
        len = strlen(buf);
        buf[len++] = '\n';
        buf[len] = '\0';
-       serv_write(buf, len);
+       rc = serv_write(buf, len);
 #ifdef SERV_TRACE
-       lprintf(9, ">>>%s", buf);
+       syslog(9, ">>>%s", buf);
 #endif
+       return rc;
 }
 
 
-
-/**
- * Read binary data from server into memory using a series of
- * server READ commands.
- * \return the read content as StrBuf
+/*
+ * Read binary data from server into memory using a series of server READ commands.
+ * returns the read content as StrBuf
  */
 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
 {
        wcsession *WCC = WC;
-       size_t bytes = 0;
-       size_t thisblock = 0;
-       
-       if (Ret == NULL)
-           return -1;
+       size_t bytes_read = 0;
+       size_t this_block = 0;
+       int rc;
 
-       if (MaxRead == -1)
-       {
-               serv_printf("READ %d|"SIZE_T_FMT, 0, total_len);
-               if (StrBuf_ServGetln(Buf) > 0)
-               {
-                       long YetRead;
-                       const char *ErrStr;
-                       const char *pch;
-                       int rc;
-
-                       if (GetServerStatus(Buf, NULL) == 6)
-                       {
-                           StrBufCutLeft(Buf, 4);
-                           thisblock = StrTol(Buf);
-                           if (WCC->serv_sock==-1) {
-                                   FlushStrBuf(Ret); 
-                                   return -1; 
-                           }
-
-                           if (WCC->ReadPos != NULL) {
-                                   pch = ChrPtr(WCC->ReadBuf);
-
-                                   YetRead = WCC->ReadPos - pch;
-                                   if (YetRead > 0)
-                                   {
-                                           long StillThere;
-                                           
-                                           StillThere = StrLength(WCC->ReadBuf) - 
-                                                   YetRead;
-                                           
-                                           StrBufPlain(Ret, 
-                                                       WCC->ReadPos,
-                                                       StillThere);
-                                           total_len -= StillThere;
-                                   }
-                                   FlushStrBuf(WCC->ReadBuf);
-                                   WCC->ReadPos = NULL;
-                           } 
-                           if (total_len > 0)
-                           {
-                                   rc = StrBufReadBLOB(Ret, 
-                                                       &WCC->serv_sock, 
-                                                       1, 
-                                                       total_len,
-                                                       &ErrStr);
-                                   if (rc < 0)
-                                   {
-                                           lprintf(1, "Server connection broken: %s\n",
-                                                   (ErrStr)?ErrStr:"");
-                                           wc_backtrace();
-                                           WCC->serv_sock = (-1);
-                                           WCC->connected = 0;
-                                           WCC->logged_in = 0;
-                                           return rc;
-                                   }
-                                   else
-                                           return StrLength(Ret);
-                           }
-                           else 
-                                   return StrLength(Ret);
-                       }
-               }
-               else
-                       return -1;
+       if (Ret == NULL) {
+               return -1;
        }
-       else while ((WCC->serv_sock!=-1) &&
-              (bytes < total_len)) {
-               thisblock = MaxRead;
-               if ((total_len - bytes) < thisblock) {
-                       thisblock = total_len - bytes;
-                       if (thisblock == 0) {
-                               FlushStrBuf(Ret); 
-                               return -1; 
-                       }
+
+       while (bytes_read < total_len) {
+
+               if (WCC->serv_sock==-1) {
+                       FlushStrBuf(Ret); 
+                       return -1; 
                }
-               serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
-               if (StrBuf_ServGetln(Buf) > 0)
+
+               serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, bytes_read, total_len-bytes_read);
+               if ( (rc = StrBuf_ServGetln(Buf) > 0) && (GetServerStatus(Buf, NULL) == 6) ) 
                {
-                       if (GetServerStatus(Buf, NULL) == 6)
-                       {
-                           StrBufCutLeft(Buf, 4);
-                           thisblock = StrTol(Buf);
-                           if (WCC->serv_sock==-1) {
-                                   FlushStrBuf(Ret); 
-                                   return -1; 
-                           }
-                           StrBuf_ServGetBLOBBuffered(Ret, thisblock);
-                           bytes += thisblock;
-                   }
-                   else {
-                           lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
-                           return -1;
-                   }
+                       if (rc < 0)
+                               return rc;
+                       StrBufCutLeft(Buf, 4);
+                       this_block = StrTol(Buf);
+                       rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
+                       if (rc < 0) {
+                               syslog(1, "Server connection broken during download\n");
+                               wc_backtrace();
+                               if (WCC->serv_sock > 0) close(WCC->serv_sock);
+                               WCC->serv_sock = (-1);
+                               WCC->connected = 0;
+                               WCC->logged_in = 0;
+                               return rc;
+                       }
+                       bytes_read += rc;
                }
        }
+
        return StrLength(Ret);
 }
 
@@ -503,7 +443,8 @@ int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
 
        if (is_https) {
                int ntries = 0;
-               if (StrLength(Hdr->ReadBuf) > 0) {
+               if (StrLength(Hdr->ReadBuf) > 0)
+               {
                        pchs = ChrPtr(Hdr->ReadBuf);
                        pch = strchr(pchs, '\n');
                        if (pch != NULL) {
@@ -526,6 +467,8 @@ int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
                                        retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
                                        pchs = ChrPtr(Hdr->ReadBuf);
                                        pch = strchr(pchs, '\n');
+                                       if (pch == NULL)
+                                               retval = 0;
                                }
                                if (retval == 0) {
                                        sleeeeeeeeeep(1);
@@ -596,7 +539,7 @@ int webcit_tcp_server(char *ip_addr, int port_number, int queue_len)
        {
                ip_version = 4;
                if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
-                       lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
+                       syslog(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
                        return (-WC_EXIT_BIND);
                }
        }
@@ -604,13 +547,13 @@ int webcit_tcp_server(char *ip_addr, int port_number, int queue_len)
        {
                ip_version = 6;
                if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
-                       lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
+                       syslog(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
                        return (-WC_EXIT_BIND);
                }
        }
 
        if (port_number == 0) {
-               lprintf(1, "Cannot start: no port number specified.\n");
+               syslog(1, "Cannot start: no port number specified.\n");
                return (-WC_EXIT_BIND);
        }
        sin6.sin6_port = htons((u_short) port_number);
@@ -620,7 +563,7 @@ int webcit_tcp_server(char *ip_addr, int port_number, int queue_len)
 
        s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
        if (s < 0) {
-               lprintf(1, "Can't create a listening socket: %s\n", strerror(errno));
+               syslog(1, "Can't create a listening socket: %s\n", strerror(errno));
                return (-WC_EXIT_BIND);
        }
        /* Set some socket options that make sense. */
@@ -635,12 +578,14 @@ int webcit_tcp_server(char *ip_addr, int port_number, int queue_len)
        }
 
        if (b < 0) {
-               lprintf(1, "Can't bind: %s\n", strerror(errno));
+               syslog(1, "Can't bind: %s\n", strerror(errno));
+               close(s);
                return (-WC_EXIT_BIND);
        }
 
        if (listen(s, queue_len) < 0) {
-               lprintf(1, "Can't listen: %s\n", strerror(errno));
+               syslog(1, "Can't listen: %s\n", strerror(errno));
+               close(s);
                return (-WC_EXIT_BIND);
        }
        return (s);
@@ -664,7 +609,7 @@ int webcit_uds_server(char *sockpath, int queue_len)
 
        i = unlink(sockpath);
        if ((i != 0) && (errno != ENOENT)) {
-               lprintf(1, "webcit: can't unlink %s: %s\n",
+               syslog(1, "webcit: can't unlink %s: %s\n",
                        sockpath, strerror(errno));
                return (-WC_EXIT_BIND);
        }
@@ -675,19 +620,19 @@ int webcit_uds_server(char *sockpath, int queue_len)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               lprintf(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
+               syslog(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
                return (-WC_EXIT_BIND);
        }
 
        if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-               lprintf(1, "webcit: Can't bind: %s\n",
-                       strerror(errno));
+               syslog(1, "webcit: Can't bind: %s\n", strerror(errno));
+               close(s);
                return (-WC_EXIT_BIND);
        }
 
        if (listen(s, actual_queue_len) < 0) {
-               lprintf(1, "webcit: Can't listen: %s\n",
-                       strerror(errno));
+               syslog(1, "webcit: Can't listen: %s\n", strerror(errno));
+               close(s);
                return (-WC_EXIT_BIND);
        }
 
@@ -718,19 +663,23 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
 
 #ifdef HAVE_OPENSSL
        if (is_https) {
-               long bufremain;
+               long bufremain = 0;
                long baselen;
 
                baselen = StrLength(Target);
 
                if (Hdr->Pos == NULL)
                        Hdr->Pos = ChrPtr(Hdr->ReadBuf);
-               bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
 
-               if (bytes < bufremain)
-                       bufremain = bytes;
-               StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
-               StrBufCutLeft(Hdr->ReadBuf, bufremain);
+               if (StrLength(Hdr->ReadBuf) > 0)
+               {
+                       bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
+                       
+                       if (bytes < bufremain)
+                               bufremain = bytes;
+                       StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
+                       StrBufCutLeft(Hdr->ReadBuf, bufremain);
+               }
 
                if (bytes > bufremain) 
                {
@@ -739,15 +688,10 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
                                retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
                        if (retval >= 0) {
                                StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
-#ifdef HTTP_TRACING
-                               write(2, "\033[32m", 5);
-                               write(2, buf, bytes);
-                               write(2, "\033[30m", 5);
-#endif
                                return 1;
                        }
                        else {
-                               lprintf(2, "client_read_ssl() failed\n");
+                               syslog(2, "client_read_ssl() failed\n");
                                return -1;
                        }
                }
@@ -765,17 +709,12 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
                                        O_TERM,
                                        &Error);
        if (retval < 0) {
-               lprintf(2, "client_read() failed: %s\n",
+               syslog(2, "client_read() failed: %s\n",
                        Error);
                wc_backtrace();
                return retval;
        }
 
-#ifdef HTTP_TRACING
-       write(2, "\033[32m", 5);
-       write(2, buf, bytes);
-       write(2, "\033[30m", 5);
-#endif
        return 1;
 }
 
@@ -808,7 +747,7 @@ long end_burst(void)
                if (CompressBuffer(WCC->WBuf) > 0)
                        hprintf("Content-encoding: gzip\r\n");
                else {
-                       lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
+                       syslog(LOG_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
                        wc_backtrace();
                }
        }
@@ -834,13 +773,6 @@ long end_burst(void)
        }
 #endif
 
-       
-#ifdef HTTP_TRACING
-       
-       write(2, "\033[34m", 5);
-       write(2, ptr, StrLength(WCC->WBuf));
-       write(2, "\033[30m", 5);
-#endif
        if (WCC->Hdr->http_sock == -1)
                return -1;
        fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
@@ -850,7 +782,7 @@ long end_burst(void)
                         FD_ZERO(&wset);
                         FD_SET(WCC->Hdr->http_sock, &wset);
                         if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
-                                lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
+                                syslog(2, "client_write: Socket select failed (%s)\n", strerror(errno));
                                 return -1;
                         }
                 }
@@ -859,7 +791,7 @@ long end_burst(void)
                    (res = write(WCC->Hdr->http_sock, 
                                 ptr,
                                 count)) == -1) {
-                        lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
+                        syslog(2, "client_write: Socket write failed (%s)\n", strerror(errno));
                        wc_backtrace();
                         return res;
                 }
@@ -871,19 +803,12 @@ long end_burst(void)
        count = StrLength(WCC->WBuf);
        eptr = ptr + count;
 
-#ifdef HTTP_TRACING
-       
-       write(2, "\033[34m", 5);
-       write(2, ptr, StrLength(WCC->WBuf));
-       write(2, "\033[30m", 5);
-#endif
-
         while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
                         FD_ZERO(&wset);
                         FD_SET(WCC->Hdr->http_sock, &wset);
                         if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
-                                lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
+                                syslog(2, "client_write: Socket select failed (%s)\n", strerror(errno));
                                 return -1;
                         }
                 }
@@ -892,7 +817,7 @@ long end_burst(void)
                    (res = write(WCC->Hdr->http_sock, 
                                 ptr,
                                 count)) == -1) {
-                        lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
+                        syslog(2, "client_write: Socket write failed (%s)\n", strerror(errno));
                        wc_backtrace();
                         return res;
                 }
@@ -982,8 +907,12 @@ SessionDestroyModule_TCPSOCKETS
 {
        FreeStrBuf(&sess->CLineBuf);
        FreeStrBuf(&sess->ReadBuf);
+       sess->connected = 0;
        sess->ReadPos = NULL;
        FreeStrBuf(&sess->MigrateReadLineBuf);
-       if (sess->serv_sock > 0)
+       if (sess->serv_sock > 0) {
+               syslog(LOG_DEBUG, "Closing socket %d", sess->serv_sock);
                close(sess->serv_sock);
+       }
+       sess->serv_sock = -1;
 }