There can be only two.
[citadel.git] / webcit / tcp_sockets.c
index 963007f87a759a39414e58453a1e043fb5e391af..1ac55143eed9409980df370bbca9112a3d768a8c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1987-2012 by the citadel.org team
+ * Copyright (c) 1987-2017 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.
@@ -293,12 +293,12 @@ void FlushReadBuf (void)
                pche = pch + len;
                if (WCC->ReadPos != pche)
                {
-                       syslog(LOG_EMERG,
+                       syslog(LOG_ERR,
                                "ERROR: somebody didn't eat his soup! Remaing Chars: %ld [%s]\n", 
                                (long)(pche - WCC->ReadPos),
                                pche
                        );
-                       syslog(LOG_EMERG
+                       syslog(LOG_ERR
                                "--------------------------------------------------------------------------------\n"
                                "Whole buf: [%s]\n"
                                "--------------------------------------------------------------------------------\n", 
@@ -482,12 +482,12 @@ int client_write(StrBuf *ThisBuf)
                 }
 
                 if ((WCC->Hdr->http_sock == -1) || 
-                   (res = write(WCC->Hdr->http_sock, 
-                                ptr,
-                                count)) == -1) {
+                   ((res = write(WCC->Hdr->http_sock, ptr, count)),
+                    (res == -1)))
+               {
                         syslog(LOG_INFO, "client_write: Socket write failed (%s)\n", strerror(errno));
                        wc_backtrace(LOG_INFO);
-                        return res;
+                        return -1;
                 }
                 count -= res;
                ptr += res;
@@ -495,22 +495,77 @@ int client_write(StrBuf *ThisBuf)
        return 0;
 }
 
+
+int
+read_serv_chunk(
+
+       StrBuf *Buf,
+       size_t total_len,
+       size_t *bytes_read
+       )
+{
+       int rc;
+       int ServerRc;
+       wcsession *WCC = WC;
+
+       serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, *bytes_read, total_len-(*bytes_read));
+       if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
+            (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) ) 
+       {
+               size_t this_block = 0;
+
+               if (rc < 0)
+                       return rc;
+
+               StrBufCutLeft(Buf, 4);
+               this_block = StrTol(Buf);
+               rc = StrBuf_ServGetBLOBBuffered(WCC->WBuf, this_block);
+               if (rc < 0) {
+                       syslog(LOG_INFO, "Server connection broken during download\n");
+                       wc_backtrace(LOG_INFO);
+                       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 6;
+}
+
+static inline int send_http(StrBuf *Buf)
+{
+#ifdef HAVE_OPENSSL
+       if (is_https)
+               return client_write_ssl(Buf);
+       else
+#endif
+               return client_write(Buf);
+}
 /*
  * Read binary data from server into memory using a series of server READ commands.
  * returns the read content as StrBuf
  */
 void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static, int detect_mime)
 {
+       int ServerRc = 6;
        wcsession *WCC = WC;
        size_t bytes_read = 0;
-       size_t this_block = 0;
-       int rc = 6;
-       int ServerRc = 6;
        int first = 1;
+       int client_con_state = 0;
        int chunked = 0;
-       StrBuf *BufHeader;
+       int is_gzip = 0;
+       const char *Err = NULL;
+       StrBuf *BufHeader = NULL;
        StrBuf *Buf;
+       StrBuf *pBuf = NULL;
+       vStreamT *SC = NULL;
+       IOBuffer ReadBuffer;
+       IOBuffer WriteBuffer;
+       
 
+       Buf = NewStrBuf();
 
        if (WCC->Hdr->HaveRange)
        {
@@ -527,86 +582,169 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
 
        if (chunked)
        {
-               BufHeader=NewStrBuf();
+               BufHeader = NewStrBuf();
        }
-       Buf = NewStrBuf();
 
-       http_transmit_headers(ChrPtr(MimeType), is_static, chunked);
-#ifdef HAVE_OPENSSL
-       if (is_https)
-               client_write_ssl(WCC->HBuf);
+       if ((detect_mime != 0) && (bytes_read != 0))
+       {
+               /* need to read first chunk to detect mime, though the client doesn't care */
+               size_t bytes_read = 0;
+               const char *CT;
+
+               ServerRc = read_serv_chunk(
+                       Buf,
+                       total_len,
+                       &bytes_read);
+
+               if (ServerRc != 6)
+               {
+                       FreeStrBuf(&BufHeader);
+                       FreeStrBuf(&Buf);
+                       return;
+               }
+               CT = GuessMimeType(SKEY(WCC->WBuf));
+               FlushStrBuf(WCC->WBuf);
+               StrBufPlain(MimeType, CT, -1);
+               CheckGZipCompressionAllowed(SKEY(MimeType));
+               detect_mime = 0;
+               FreeStrBuf(&Buf);
+       }
+
+       memset(&WriteBuffer, 0, sizeof(IOBuffer));
+       if (chunked && !DisableGzip && WCC->Hdr->HR.gzip_ok)
+       {
+               is_gzip = 1;
+               SC = StrBufNewStreamContext (eZLibEncode, &Err);
+               if (SC == NULL) {
+                       syslog(LOG_ERR, "Error while initializing stream context: %s", Err);
+                       FreeStrBuf(&Buf);
+                       return;
+               }
+
+               memset(&ReadBuffer, 0, sizeof(IOBuffer));
+               ReadBuffer.Buf = WCC->WBuf;
+
+               WriteBuffer.Buf = NewStrBufPlain(NULL, SIZ*2);;
+               pBuf = WriteBuffer.Buf;
+       }
        else
-#endif
-               client_write(WCC->HBuf);
+       {
+               pBuf = WCC->WBuf;
+       }
 
-       while ((bytes_read < total_len) && (ServerRc == 6)) {
+       if (!detect_mime)
+       {
+               http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
+               
+               if (send_http(WCC->HBuf) < 0)
+               {
+                       FreeStrBuf(&Buf);
+                       FreeStrBuf(&WriteBuffer.Buf);
+                       FreeStrBuf(&BufHeader);
+                       if (StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
+                               syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
+                       }
+                       return;
+               }
+       }
+
+       while ((bytes_read < total_len) &&
+              (ServerRc == 6) &&
+              (client_con_state == 0))
+       {
 
                if (WCC->serv_sock==-1) {
                        FlushStrBuf(WCC->WBuf); 
+                       FreeStrBuf(&Buf);
+                       FreeStrBuf(&WriteBuffer.Buf);
+                       FreeStrBuf(&BufHeader);
+                       StrBufDestroyStreamContext(eZLibEncode, &SC, &Err);
+                       if (StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
+                               syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
+                       }
                        return;
                }
 
-               serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, bytes_read, total_len-bytes_read);
-               if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
-                    (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) ) 
+               ServerRc = read_serv_chunk(
+                       Buf,
+                       total_len,
+                       &bytes_read);
+               if (ServerRc != 6)
+                       break;
+
+               if (detect_mime)
                {
-                       if (rc < 0)
-                               return;
-                       StrBufCutLeft(Buf, 4);
-                       this_block = StrTol(Buf);
-                       rc = StrBuf_ServGetBLOBBuffered(WCC->WBuf, this_block);
-                       if (rc < 0) {
-                               syslog(LOG_INFO, "Server connection broken during download\n");
-                               wc_backtrace(LOG_INFO);
-                               if (WCC->serv_sock > 0) close(WCC->serv_sock);
-                               WCC->serv_sock = (-1);
-                               WCC->connected = 0;
-                               WCC->logged_in = 0;
-                               return;
+                       const char *CT;
+                       detect_mime = 0;
+                       
+                       CT = GuessMimeType(SKEY(WCC->WBuf));
+                       StrBufPlain(MimeType, CT, -1);
+                       if (is_gzip) {
+                               CheckGZipCompressionAllowed(SKEY(MimeType));
+                               is_gzip = WCC->Hdr->HR.gzip_ok;
                        }
-                       bytes_read += rc;
+                       http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
                        
+                       client_con_state = send_http(WCC->HBuf);
                }
 
-               if (chunked)
+               if (is_gzip)
                {
-                       StrBufPrintf(BufHeader, "%s%x\r\n", 
-                                    (first)?"":"\r\n",
-                                    StrLength (WCC->WBuf));
-#ifdef HAVE_OPENSSL
-                       if (is_https)
-                               rc = client_write_ssl(BufHeader);
-                       else
-#endif
-                               rc = client_write(BufHeader);
-                       if (rc < 0)
-                               break;
+                       int done = (bytes_read == total_len);
+                       while ((IOBufferStrLength(&ReadBuffer) > 0) && (client_con_state == 0)) {
+                               int rc;
+
+                               do {
+                                       rc = StrBufStreamTranscode(eZLibEncode, &WriteBuffer, &ReadBuffer, NULL, -1, SC, done, &Err);
+
+                                       if (StrLength (pBuf) > 0) {
+                                               StrBufPrintf(BufHeader, "%s%x\r\n", 
+                                                    (first)?"":"\r\n",
+                                                            StrLength (pBuf));
+                                               first = 0;
+                                               client_con_state = send_http(BufHeader);
+                                               if (client_con_state == 0) {
+                                                       client_con_state = send_http(pBuf);
+                                               }
+                                               FlushStrBuf(pBuf);
+                                       }
+                               } while ((rc == 1) && (StrLength(pBuf) > 0));
+                       }
+                       FlushStrBuf(WCC->WBuf);
                }
+               else {
+                       if ((chunked) && (client_con_state == 0))
+                       {
+                               StrBufPrintf(BufHeader, "%s%x\r\n", 
+                                            (first)?"":"\r\n",
+                                            StrLength (pBuf));
+                               first = 0;
+                               client_con_state = send_http(BufHeader);
+                       }
 
-#ifdef HAVE_OPENSSL
-               if (is_https)
-                       rc = client_write_ssl(WCC->WBuf);
-               else
-#endif
-                       rc = client_write(WCC->WBuf);
+                       if (client_con_state == 0)
+                               client_con_state = send_http(pBuf);
 
-               if (rc < 0)
-                       break;
-               first = 0;
-               FlushStrBuf(WCC->WBuf);
+                       FlushStrBuf(pBuf);
+               }
        }
 
-       if (chunked)
+       if (SC && StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
+               syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
+       }
+       FreeStrBuf(&WriteBuffer.Buf);
+       if ((chunked) && (client_con_state == 0))
        {
-               StrBufPrintf(BufHeader, "\r\n0\r\n\r\n");
-#ifdef HAVE_OPENSSL
-               if (is_https)
-                       rc = client_write_ssl(BufHeader);
-               else
-#endif
-                       rc = client_write(BufHeader);
+               StrBufPlain(BufHeader, HKEY("\r\n0\r\n\r\n"));
+               if (send_http(BufHeader) < 0)
+               {
+                       FreeStrBuf(&Buf);
+                       FreeStrBuf(&BufHeader);
+                       return;
+               }
        }
-
+       FreeStrBuf(&BufHeader);
+       FreeStrBuf(&Buf);
 }
 
 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
@@ -763,13 +901,13 @@ retry:
        }
 
        if (b < 0) {
-               syslog(LOG_EMERG, "Can't bind: %s\n", strerror(errno));
+               syslog(LOG_ERR, "Can't bind: %s\n", strerror(errno));
                close(s);
                return (-WC_EXIT_BIND);
        }
 
        if (listen(s, queue_len) < 0) {
-               syslog(LOG_EMERG, "Can't listen: %s\n", strerror(errno));
+               syslog(LOG_ERR, "Can't listen: %s\n", strerror(errno));
                close(s);
                return (-WC_EXIT_BIND);
        }
@@ -782,8 +920,7 @@ retry:
  * sockpath - file name of the unix domain socket
  * queue_len - Number of incoming connections to allow in the queue
  */
-int webcit_uds_server(char *sockpath, int queue_len)
-{
+int webcit_uds_server(char *sockpath, int queue_len) {
        struct sockaddr_un addr;
        int s;
        int i;
@@ -826,8 +963,6 @@ int webcit_uds_server(char *sockpath, int queue_len)
 }
 
 
-
-
 /*
  * Read data from the client socket.
  *
@@ -841,8 +976,7 @@ int webcit_uds_server(char *sockpath, int queue_len)
  *      0       Request timed out.
  *     -1      Connection is broken, or other error.
  */
-int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
-{
+int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout) {
        const char *Error;
        int retval = 0;
 
@@ -853,11 +987,11 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
 
                baselen = StrLength(Target);
 
-               if (Hdr->Pos == NULL)
+               if (Hdr->Pos == NULL) {
                        Hdr->Pos = ChrPtr(Hdr->ReadBuf);
+               }
 
-               if (StrLength(Hdr->ReadBuf) > 0)
-               {
+               if (StrLength(Hdr->ReadBuf) > 0) {
                        bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
                        
                        if (bytes < bufremain)
@@ -866,8 +1000,7 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
                        StrBufCutLeft(Hdr->ReadBuf, bufremain);
                }
 
-               if (bytes > bufremain) 
-               {
+               if (bytes > bufremain) {
                        while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
                               (retval >= 0))
                                retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
@@ -894,8 +1027,7 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
                                        O_TERM,
                                        &Error);
        if (retval < 0) {
-               syslog(LOG_INFO, "client_read() failed: %s\n",
-                       Error);
+               syslog(LOG_INFO, "client_read() failed: %s\n", Error);
                wc_backtrace(LOG_DEBUG);
                return retval;
        }
@@ -958,11 +1090,12 @@ long end_burst(void)
        }
 #endif
 
-       if (WCC->Hdr->http_sock == -1)
+       if (WCC->Hdr->http_sock == -1) {
                return -1;
+       }
        fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
 
-       while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
+       while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
                         FD_ZERO(&wset);
                         FD_SET(WCC->Hdr->http_sock, &wset);
@@ -1016,7 +1149,7 @@ long end_burst(void)
 
 /*
  * lingering_close() a`la Apache. see
- * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
+ * http://httpd.apache.org/docs/2.0/misc/fin_wait_2.html for rationale
  */
 int lingering_close(int fd)
 {