* just define vars if we need them
[citadel.git] / webcit / tcp_sockets.c
index 23fd9cf371422d8c9696647ebe42c7cb3e7126ae..8d688d2564f467aa78fa4a736b5b378976e69ca2 100644 (file)
@@ -12,6 +12,7 @@
 #include "webserver.h"
 
 extern int DisableGzip;
+long MaxRead = -1; /* should we do READ scattered or all at once? */
 
 /*
  * register the timeout
@@ -120,15 +121,16 @@ int tcp_connectsock(char *host, char *service)
        }
        alarm(0);
        signal(SIGALRM, SIG_IGN);
-
-       fdflags = fcntl(s, F_GETFL);
-       if (fdflags < 0)
-               lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
-                       host, service, strerror(errno));
-       fdflags = fdflags | O_NONBLOCK;
-       if (fcntl(s, F_SETFD, fdflags) < 0)
-               lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
-                       host, service, strerror(errno));
+       if (!is_https) {
+               fdflags = fcntl(s, F_GETFL);
+               if (fdflags < 0)
+                       lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
+                               host, service, strerror(errno));
+               fdflags = fdflags | O_NONBLOCK;
+               if (fcntl(s, F_SETFD, fdflags) < 0)
+                       lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
+                               host, service, strerror(errno));
+       }
        return (s);
 }
 
@@ -151,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);
+       lprintf(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
 #endif
        return len;
 }
@@ -162,7 +164,8 @@ int StrBuf_ServGetln(StrBuf *buf)
        wcsession *WCC = WC;
        const char *ErrStr = NULL;
        int rc;
-
+       
+       FlushStrBuf(buf);
        rc = StrBufTCP_read_buffered_line_fast(buf, 
                                               WCC->ReadBuf, 
                                               &WCC->ReadPos, 
@@ -178,6 +181,15 @@ int StrBuf_ServGetln(StrBuf *buf)
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
+#ifdef SERV_TRACE
+       else 
+       {
+               long pos=0;
+               if (WCC->ReadPos != NULL)
+                       pos = WCC->ReadPos - ChrPtr(buf);
+               lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
+       }
+#endif
        return rc;
 }
 
@@ -204,6 +216,11 @@ int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
+#ifdef SERV_TRACE
+        else
+                lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
+#endif
+
        return rc;
 }
 
@@ -224,6 +241,11 @@ int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
+#ifdef SERV_TRACE
+        else
+                lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
+#endif
+
        return rc;
 }
 
@@ -266,7 +288,7 @@ void serv_puts(const char *string)
 {
        wcsession *WCC = WC;
 #ifdef SERV_TRACE
-       lprintf(9, "%3d<%s\n", WC->serv_sock, string);
+       lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
 #endif
        FlushStrBuf(WCC->ReadBuf);
        WCC->ReadPos = NULL;
@@ -283,7 +305,7 @@ void serv_putbuf(const StrBuf *string)
 {
        wcsession *WCC = WC;
 #ifdef SERV_TRACE
-       lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
+       lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
 #endif
        FlushStrBuf(WCC->ReadBuf);
        WCC->ReadPos = NULL;
@@ -317,19 +339,130 @@ void serv_printf(const char *format,...)
        buf[len] = '\0';
        serv_write(buf, len);
 #ifdef SERV_TRACE
-       lprintf(9, "<%s", buf);
+       lprintf(9, ">>>%s", buf);
 #endif
 }
 
 
 
+/**
+ * Read binary data from server into memory using a series of
+ * server READ commands.
+ * \return 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;
+
+       if (MaxRead == -1)
+       {
+               serv_printf("READ %d|%d", 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; 
+                           }
+
+                           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;
+       }
+       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; 
+                       }
+               }
+               serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
+               if (StrBuf_ServGetln(Buf) > 0)
+               {
+                       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;
+                   }
+               }
+       }
+       return StrLength(Ret);
+}
+
 
 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
 {
-       const char *Error, *pch, *pchs;
+       const char *Error;
+#ifdef HAVE_OPENSSL
+       const char *pch, *pchs;
        int rlen, len, retval = 0;
 
-#ifdef HAVE_OPENSSL
        if (is_https) {
                int ntries = 0;
                if (StrLength(Hdr->ReadBuf) > 0) {
@@ -415,7 +548,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);
 
@@ -424,7 +557,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;
@@ -439,11 +572,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);
 }
@@ -469,7 +602,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));
@@ -480,19 +613,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);
@@ -523,6 +656,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);
@@ -535,7 +671,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) {
@@ -777,6 +913,7 @@ SessionDestroyModule_TCPSOCKETS
 {
        FreeStrBuf(&sess->CLineBuf);
        FreeStrBuf(&sess->ReadBuf);
+       sess->ReadPos = NULL;
        FreeStrBuf(&sess->MigrateReadLineBuf);
        if (sess->serv_sock > 0)
                close(sess->serv_sock);