Gzip stream chunked encoding is working now.
[citadel.git] / webcit / tcp_sockets.c
index 4a60fed494853a913bd325b4c7ce11ae107977b6..f744090b4b20ba3bb6c9c519b39ff968487818a3 100644 (file)
@@ -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;
@@ -553,9 +553,16 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
        wcsession *WCC = WC;
        size_t bytes_read = 0;
        int first = 1;
+       int client_con_state = 0;
        int chunked = 0;
+       int is_gzip = 0;
        StrBuf *BufHeader = NULL;
        StrBuf *Buf;
+       StrBuf *pBuf = NULL;
+       void *SC = NULL;
+       IOBuffer ReadBuffer;
+       IOBuffer WriteBuffer;
+       
 
        Buf = NewStrBuf();
 
@@ -600,9 +607,25 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
                FreeStrBuf(&Buf);
        }
 
+       if (chunked && !DisableGzip && WCC->Hdr->HR.gzip_ok)
+       {
+               is_gzip = 1;
+               SC = StrBufNewStreamContext (eZLibEncode);
+
+               memset(&ReadBuffer, 0, sizeof(IOBuffer));
+               ReadBuffer.Buf = WCC->WBuf;
+
+               memset(&WriteBuffer, 0, sizeof(IOBuffer));
+               WriteBuffer.Buf = NewStrBufPlain(NULL, SIZ*2);;
+       }
+       else
+       {
+               pBuf = WCC->WBuf;
+       }
+
        if (!detect_mime)
        {
-               http_transmit_headers(ChrPtr(MimeType), is_static, chunked);
+               http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
                
                if (send_http(WCC->HBuf) < 0)
                {
@@ -611,7 +634,10 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
                }
        }
 
-       while ((bytes_read < total_len) && (ServerRc == 6)) {
+       while ((bytes_read < total_len) &&
+              (ServerRc == 6) &&
+              (client_con_state == 0))
+       {
 
                if (WCC->serv_sock==-1) {
                        FlushStrBuf(WCC->WBuf); 
@@ -633,28 +659,46 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
                        
                        CT = GuessMimeType(SKEY(WCC->WBuf));
                        StrBufPlain(MimeType, CT, -1);
-                       http_transmit_headers(ChrPtr(MimeType), is_static, chunked);
+                       http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
                        
-                       if (send_http(WCC->HBuf) < 0)
-                               break;
+                       client_con_state = send_http(WCC->HBuf);
                }
 
-               if (chunked)
+               if (is_gzip)
                {
-                       StrBufPrintf(BufHeader, "%s%x\r\n", 
-                                    (first)?"":"\r\n",
-                                    StrLength (WCC->WBuf));
-                       if (send_http(BufHeader) < 0)
-                               break;
+                       int done = (bytes_read == total_len);
+                       while ((IOBufferStrLength(&ReadBuffer) > 0) && (client_con_state == 0)) {
+                               StrBufStreamTranscode(eZLibEncode, &WriteBuffer, &ReadBuffer, NULL, -1, SC, done);
+
+                               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(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);
+                       }
 
-               if (send_http(WCC->WBuf) < 0)
-                       break;
+                       if (client_con_state == 0)
+                               client_con_state = send_http(pBuf);
 
-               FlushStrBuf(WCC->WBuf);
+                       FlushStrBuf(pBuf);
+               }
        }
 
-       if (chunked)
+       if ((chunked) && (client_con_state == 0))
        {
                StrBufPlain(BufHeader, HKEY("\r\n0\r\n\r\n"));
                if (send_http(BufHeader) < 0)
@@ -1073,7 +1117,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)
 {