+ if (chunked)
+ {
+ BufHeader = NewStrBuf();
+ }
+
+ 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
+ {
+ pBuf = WCC->WBuf;
+ }
+
+ 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;
+ }
+
+ ServerRc = read_serv_chunk(
+ Buf,
+ total_len,
+ &bytes_read);
+ if (ServerRc != 6)
+ break;
+
+ if (detect_mime)
+ {
+ 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;
+ }
+ http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
+
+ client_con_state = send_http(WCC->HBuf);
+ }
+
+ if (is_gzip)
+ {
+ 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);
+ }
+
+ if (client_con_state == 0)
+ client_con_state = send_http(pBuf);
+
+ FlushStrBuf(pBuf);
+ }
+ }
+
+ 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))
+ {
+ StrBufPlain(BufHeader, HKEY("\r\n0\r\n\r\n"));
+ if (send_http(BufHeader) < 0)
+ {
+ FreeStrBuf(&Buf);
+ FreeStrBuf(&BufHeader);
+ return;
+ }
+ }
+ FreeStrBuf(&BufHeader);
+ FreeStrBuf(&Buf);
+}