* shut down clean in case of not being able to bind.
[citadel.git] / webcit / tcp_sockets.c
index 15a1c1c6998200697a708adcc23c62fb3e68abfc..3e3194b341859ff8a729d0eb66a838c490f7dd75 100644 (file)
@@ -172,7 +172,7 @@ int StrBuf_ServGetln(StrBuf *buf)
        if (rc < 0)
        {
                lprintf(1, "Server connection broken: %s\n",
-                       ErrStr);
+                       (ErrStr)?ErrStr:"");
                wc_backtrace();
                WCC->serv_sock = (-1);
                WCC->connected = 0;
@@ -184,7 +184,7 @@ int StrBuf_ServGetln(StrBuf *buf)
 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
 {
        wcsession *WCC = WC;
-       const char *Err;
+       const char *ErrStr;
        int rc;
        
        rc = StrBufReadBLOBBuffered(buf, 
@@ -194,11 +194,11 @@ int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
                                    1, 
                                    BlobSize, 
                                    NNN_TERM,
-                                   &Err);
+                                   &ErrStr);
        if (rc < 0)
        {
                lprintf(1, "Server connection broken: %s\n",
-                       Err);
+                       (ErrStr)?ErrStr:"");
                wc_backtrace();
                WCC->serv_sock = (-1);
                WCC->connected = 0;
@@ -210,15 +210,15 @@ int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
 {
        wcsession *WCC = WC;
-       const char *Err;
+       const char *ErrStr;
        int rc;
        
        WCC->ReadPos = NULL;
-       rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &Err);
+       rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
        if (rc < 0)
        {
                lprintf(1, "Server connection broken: %s\n",
-                       Err);
+                       (ErrStr)?ErrStr:"");
                wc_backtrace();
                WCC->serv_sock = (-1);
                WCC->connected = 0;
@@ -244,8 +244,9 @@ void serv_write(const char *buf, int nbytes)
                retval = write(WCC->serv_sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
+                       const char *ErrStr = strerror(errno);
                        lprintf(1, "Server connection broken: %s\n",
-                               strerror(errno));
+                               (ErrStr)?ErrStr:"");
                        close(WCC->serv_sock);
                        WCC->serv_sock = (-1);
                        WCC->connected = 0;
@@ -414,7 +415,7 @@ int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
 
        if (port_number == 0) {
                lprintf(1, "Cannot start: no port number specified.\n");
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
        sin.sin_port = htons((u_short) port_number);
 
@@ -423,7 +424,7 @@ int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
        s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
        if (s < 0) {
                lprintf(1, "Can't create a socket: %s\n", strerror(errno));
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
        /* Set some socket options that make sense. */
        i = 1;
@@ -438,11 +439,11 @@ int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
        
        if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
                lprintf(1, "Can't bind: %s\n", strerror(errno));
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
        if (listen(s, queue_len) < 0) {
                lprintf(1, "Can't listen: %s\n", strerror(errno));
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
        return (s);
 }
@@ -468,7 +469,7 @@ int ig_uds_server(char *sockpath, int queue_len)
        if ((i != 0) && (errno != ENOENT)) {
                lprintf(1, "webcit: can't unlink %s: %s\n",
                        sockpath, strerror(errno));
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
 
        memset(&addr, 0, sizeof(addr));
@@ -479,19 +480,19 @@ int ig_uds_server(char *sockpath, int queue_len)
        if (s < 0) {
                lprintf(1, "webcit: Can't create a socket: %s\n",
                        strerror(errno));
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
 
        if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
                lprintf(1, "webcit: Can't bind: %s\n",
                        strerror(errno));
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
 
        if (listen(s, actual_queue_len) < 0) {
                lprintf(1, "webcit: Can't listen: %s\n",
                        strerror(errno));
-               exit(WC_EXIT_BIND);
+               return (-WC_EXIT_BIND);
        }
 
        chmod(sockpath, 0777);
@@ -522,6 +523,9 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
 #ifdef HAVE_OPENSSL
        if (is_https) {
                long bufremain;
+               long baselen;
+
+               baselen = StrLength(Target);
 
                if (Hdr->Pos == NULL)
                        Hdr->Pos = ChrPtr(Hdr->ReadBuf);
@@ -534,7 +538,7 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
 
                if (bytes > bufremain) 
                {
-                       while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes) &&
+                       while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
                               (retval >= 0))
                                retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
                        if (retval >= 0) {
@@ -602,9 +606,14 @@ long end_burst(void)
         fd_set wset;
         int fdflags;
 
-       if (!DisableGzip && (WCC->Hdr->HR.gzip_ok) && CompressBuffer(WCC->WBuf))
+       if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
        {
-               hprintf("Content-encoding: gzip\r\n");
+               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));
+                       wc_backtrace();
+               }
        }
 
        if (WCC->Hdr->HR.prohibit_caching)