--- mod_deflate.c.ORIG	Wed Jul 12 12:38:44 2006
+++ mod_deflate.c	Thu Dec 28 00:56:36 2006
@@ -857,16 +857,28 @@
             return ap_pass_brigade(f->next, bb);
         }
 
+        if (apr_table_get(r->subprocess_env, "force-gzip")) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
         /* Let's see what our current Content-Encoding is.
          * If gzip is present, don't gzip again.  (We could, but let's not.)
          */
         encoding = apr_table_get(r->headers_out, "Content-Encoding");
+        if (r->content_encoding) {
+            encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
+                                              r->content_encoding, NULL)
+                                : r->content_encoding;
+        }
         if (encoding) {
             const char *tmp = encoding;
 
             token = ap_get_token(r->pool, &tmp, 0);
             while (token && token[0]) {
-                if (!strcasecmp(token, "gzip")) {
+                /* stolen from mod_negotiation: */
+                if (strcmp(token, "identity") && strcmp(token, "7bit") &&
+                    strcmp(token, "8bit") && strcmp(token, "binary")) {
                     found = 1;
                     break;
                 }
@@ -880,7 +892,34 @@
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
+        if (!apr_table_get(r->subprocess_env, "force-gunzip")) {
+            const char *accepts;
+            apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
+            /* if they don't have the line, then they can't play */
+            accepts = apr_table_get(r->headers_in, "Accept-Encoding");
+            if (accepts != NULL) {
+                token = ap_get_token(r->pool, &accepts, 0);
+                while (token && token[0]) {
+                    if (!strcasecmp(token, "gzip")) {
+                         ap_remove_output_filter(f);
+                         return ap_pass_brigade(f->next, bb);
+                    }
+                    /* skip parameters, XXX: ;q=foo evaluation? */
+                    while (*accepts == ';') {
+                        ++accepts;
+                        token = ap_get_token(r->pool, &accepts, 1);
+                    }
+
+                    /* retrieve next token */
+                    if (*accepts == ',') {
+                        ++accepts;
+                    }
+                    token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
+                }
+            }
+        }
         apr_table_unset(r->headers_out, "Content-Encoding");
+        r->content_encoding = NULL;
 
         /* No need to inflate HEAD or 204/304 */
         if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
