--- mod_deflate.c.orig	2006-12-20 06:45:35.000000000 +0900
+++ mod_deflate.c	2007-04-22 20:59:44.000000000 +0900
@@ -919,17 +919,29 @@
             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.
          * Only inflate if gzip is present.
          */
         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;
                 }
@@ -943,7 +955,43 @@
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
+
+        /* Even if we don't accept this request based on it not having
+         * the Accept-Encoding, we need to note that we were looking
+         * for this header and downstream proxies should be aware of that.
+         */
+        apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
+
+        /* force-gunzip will just force it out regardless if the browser
+         * can actually do anything with it.
+         */
+        if (!apr_table_get(r->subprocess_env, "force-gunzip")) {
+            const char *accepts;
+            /* 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))) {
