Index: lib/content_encoding.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/content_encoding.c,v
retrieving revision 1.21
diff -u -r1.21 content_encoding.c
--- lib/content_encoding.c	31 Mar 2005 07:02:03 -0000	1.21
+++ lib/content_encoding.c	23 Apr 2006 23:16:10 -0000
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -18,7 +18,7 @@
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
- * $Id: content_encoding.c,v 1.21 2005-03-31 07:02:03 bagder Exp $
+ * $Id: content_encoding.c,v 1.21 2005/03/31 07:02:03 bagder Exp $
  ***************************************************************************/
 
 #include "setup.h"
@@ -91,6 +91,7 @@
   int status;                   /* zlib status */
   CURLcode result = CURLE_OK;   /* Curl_client_write status */
   char *decomp;                 /* Put the decompressed data here. */
+  bool fakeheadertested=FALSE;  /* tested a fake header yet? */
 
   /* Dynamically allocate a buffer for decompression because it's uncommonly
      large to hold on the stack */
@@ -103,10 +104,53 @@
      the client via client_write. */
   for (;;) {
     /* (re)set buffer for decompressed output for every iteration */
+    Bytef *nextin;
+    size_t availin;
+
     z->next_out = (Bytef *)decomp;
     z->avail_out = DSIZ;
 
+    nextin = z->next_in;
+    availin = z->avail_in;
+
     status = inflate(z, Z_SYNC_FLUSH);
+
+    if(!fakeheadertested && (status == Z_DATA_ERROR)) {
+      /* some servers seem to not generate zlib headers, so we insert a dummy
+       * header and try again.
+       *
+       * This work-around was inspired by the one used by Firefox:
+       *   http://bonsai.mozilla.org/cvsblame.cgi?file=mozilla/netwerk/
+       *   streamconv/converters/nsHTTPCompressConv.cpp&mark=245-280#245
+       * (merge to one single line URL)
+       *
+       * I have not been able to verify it a lot / Daniel
+       */
+
+      static char header[2] = {
+        0x8 + 0x7 * 0x10,
+        (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
+      };
+      inflateReset(z);
+
+      z->next_in = (Bytef*)header;
+      z->avail_in = sizeof(header);
+
+      z->next_out = (Bytef *)decomp;
+      z->avail_out = DSIZ;
+
+      status = inflate(z, Z_SYNC_FLUSH);
+      if (status != Z_OK)
+        return CURLE_BAD_CONTENT_ENCODING;
+
+      /* now retry the inflate on the previous chunk */
+      z->next_in = nextin;
+      z->avail_in = (uInt)availin;
+
+      fakeheadertested = TRUE;
+      continue;
+    }
+
     if (status == Z_OK || status == Z_STREAM_END) {
       if(DSIZ - z->avail_out) {
         result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
