]> granicus.if.org Git - apache/blobdiff - modules/filters/mod_deflate.c
* Rather use a pool cleanup function than calling deflateEnd before every
[apache] / modules / filters / mod_deflate.c
index 00bbc014ad3a2fb8bfec59c640f2c2d6c9d07ad2..a1b456bb81c60c0af0a51ccf852a3c369a3f19b0 100644 (file)
@@ -1,8 +1,9 @@
-/* Copyright 2002-2004 Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  */
 
 /*
- * mod_deflate.c: Perform deflate transfer-encoding on the fly
- *
- * Written by Ian Holsman
+ * mod_deflate.c: Perform deflate content-encoding on the fly
  *
+ * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew
  */
 
 /*
- * Portions of this software are based upon public domain software
- * (zlib functions gz_open and gzwrite)
+ * Portions of this software are based upon zlib code by Jean-loup Gailly
+ * (zlib functions gz_open and gzwrite, check_header)
  */
 
+/* zlib flags */
+#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
+#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
+#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
+#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
+#define COMMENT      0x10 /* bit 4 set: file comment present */
+#define RESERVED     0xE0 /* bits 5..7: reserved */
+
+
 #include "httpd.h"
 #include "http_config.h"
 #include "http_log.h"
 
 #include "zlib.h"
 
-#ifdef HAVE_ZUTIL_H
-#include "zutil.h"
-#else
-/* As part of the encoding process, we must send what our OS_CODE is
- * (or so it seems based on what I can tell of how gzip encoding works).
- *
- * zutil.h is not always included with zlib distributions (it is a private
- * header), so this is straight from zlib 1.1.3's zutil.h.
- */
-#ifdef OS2
-#define OS_CODE  0x06
-#endif
-
-#ifdef WIN32 /* Window 95 & Windows NT */
-#define OS_CODE  0x0b
-#endif
-
-#if defined(VAXC) || defined(VMS)
-#define OS_CODE  0x02
-#endif
-
-#ifdef AMIGA
-#define OS_CODE  0x01
-#endif
-
-#if defined(ATARI) || defined(atarist)
-#define OS_CODE  0x05
-#endif
-
-#if defined(MACOS) || defined(TARGET_OS_MAC)
-#define OS_CODE  0x07
-#endif
-
-#ifdef __50SERIES /* Prime/PRIMOS */
-#define OS_CODE  0x0F
-#endif
-
-#ifdef TOPS20
-#define OS_CODE  0x0a
-#endif
-
-#ifndef OS_CODE
-#define OS_CODE  0x03  /* assume Unix */
-#endif
-#endif
-
 static const char deflateFilterName[] = "DEFLATE";
 module AP_MODULE_DECLARE_DATA deflate_module;
 
@@ -98,6 +61,21 @@ typedef struct deflate_filter_config_t
     char *note_output_name;
 } deflate_filter_config;
 
+/* RFC 1952 Section 2.3 defines the gzip header:
+ *
+ * +---+---+---+---+---+---+---+---+---+---+
+ * |ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
+ * +---+---+---+---+---+---+---+---+---+---+
+ */
+static const char gzip_header[10] =
+{ '\037', '\213', Z_DEFLATED, 0,
+  0, 0, 0, 0, /* mtime */
+  0, 0x03 /* Unix OS_CODE */
+};
+
+/* magic header */
+static const char deflate_magic[2] = { '\037', '\213' };
+
 /* windowsize is negative to suppress Zlib header */
 #define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
 #define DEFAULT_WINDOWSIZE -15
@@ -174,7 +152,7 @@ static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
 {
     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
                                                     &deflate_module);
-    
+
     if (arg2 == NULL) {
         c->note_ratio_name = apr_pstrdup(cmd->pool, arg1);
     }
@@ -228,17 +206,61 @@ static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
     return NULL;
 }
 
-/* magic header */
-static char deflate_magic[2] = { '\037', '\213' };
-
 typedef struct deflate_ctx_t
 {
     z_stream stream;
     unsigned char *buffer;
     unsigned long crc;
     apr_bucket_brigade *bb, *proc_bb;
+    int (*libz_end_func)(z_streamp);
 } deflate_ctx;
 
+static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
+                             struct apr_bucket_alloc_t *bucket_alloc,
+                             int (*libz_func)(z_streamp, int), int flush)
+{
+    int zRC = Z_OK;
+    int done = 0;
+    unsigned int deflate_len;
+    apr_bucket *b;
+
+    for (;;) {
+         deflate_len = c->bufferSize - ctx->stream.avail_out;
+
+         if (deflate_len != 0) {
+             b = apr_bucket_heap_create((char *)ctx->buffer,
+                                        deflate_len, NULL,
+                                        bucket_alloc);
+             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
+             ctx->stream.next_out = ctx->buffer;
+             ctx->stream.avail_out = c->bufferSize;
+         }
+
+         if (done)
+             break;
+
+         zRC = libz_func(&ctx->stream, flush);
+
+         if (deflate_len == 0 && zRC == Z_BUF_ERROR)
+             zRC = Z_OK;
+
+         done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
+
+         if (zRC != Z_OK && zRC != Z_STREAM_END)
+             break;
+    }
+    return zRC;
+}
+
+static apr_status_t deflate_ctx_cleanup(void *data)
+{
+    deflate_ctx *ctx = (deflate_ctx *)data;
+
+    if (ctx)
+        ctx->libz_end_func(&ctx->stream);
+    return APR_SUCCESS;
+}
+
 static apr_status_t deflate_out_filter(ap_filter_t *f,
                                        apr_bucket_brigade *bb)
 {
@@ -249,6 +271,11 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
     deflate_filter_config *c = ap_get_module_config(r->server->module_config,
                                                     &deflate_module);
 
+    /* Do nothing if asked to filter nothing. */
+    if (APR_BRIGADE_EMPTY(bb)) {
+        return ap_pass_brigade(f->next, bb);
+    }
+
     /* If we don't have a context, we need to ensure that it is okay to send
      * the deflated content.  If we have a context, that means we've done
      * this before and we liked it.
@@ -256,11 +283,11 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
      * we're in better shape.
      */
     if (!ctx) {
-        char *buf, *token;
-        const char *encoding, *accepts;
+        char *token;
+        const char *encoding;
 
         /* only work on main request/no subrequests */
-        if (r->main) {
+        if (r->main != NULL) {
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
@@ -284,7 +311,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             if ( env_value && (strcmp(env_value,"1") == 0) ) {
                 ap_remove_output_filter(f);
                 return ap_pass_brigade(f->next, bb);
-            }            
+            }
         }
 
         /* Let's see what our current Content-Encoding is.
@@ -320,7 +347,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
                     strcmp(token, "8bit") && strcmp(token, "binary")) {
 
                     ap_remove_output_filter(f);
-                    return ap_pass_brigade(f->next, bb);                       
+                    return ap_pass_brigade(f->next, bb);
                 }
 
                 /* Otherwise, skip token */
@@ -335,32 +362,45 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
          * the Accept-Encoding, we need to note that we were looking
          * for this header and downstream proxies should be aware of that.
          */
-        apr_table_setn(r->headers_out, "Vary", "Accept-Encoding");
+        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) {
-            ap_remove_output_filter(f);
-            return ap_pass_brigade(f->next, bb);
-        }
+        /* force-gzip will just force it out regardless if the browser
+         * can actually do anything with it.
+         */
+        if (!apr_table_get(r->subprocess_env, "force-gzip")) {
+            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) {
+                ap_remove_output_filter(f);
+                return ap_pass_brigade(f->next, bb);
+            }
+
+            token = ap_get_token(r->pool, &accepts, 0);
+            while (token && token[0] && strcasecmp(token, "gzip")) {
+                /* skip parameters, XXX: ;q=foo evaluation? */
+                while (*accepts == ';') {
+                    ++accepts;
+                    token = ap_get_token(r->pool, &accepts, 1);
+                }
 
-        token = ap_get_token(r->pool, &accepts, 0);
-        while (token && token[0] && strcasecmp(token, "gzip")) {
-            /* 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;
             }
 
-            /* retrieve next token */
-            if (*accepts == ',') {
-                ++accepts;
+            /* No acceptable token found. */
+            if (token == NULL || token[0] == '\0') {
+                ap_remove_output_filter(f);
+                return ap_pass_brigade(f->next, bb);
             }
-            token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
         }
 
-        /* No acceptable token found. */
-        if (token == NULL || token[0] == '\0') {
+        /* For a 304 or 204 response there is no entity included in
+         * the response and hence nothing to deflate. */
+        if (r->status == HTTP_NOT_MODIFIED || r->status == HTTP_NO_CONTENT) {
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
@@ -369,36 +409,35 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
         ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
         ctx->buffer = apr_palloc(r->pool, c->bufferSize);
+        ctx->libz_end_func = deflateEnd;
 
         zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
                            c->windowSize, c->memlevel,
                            Z_DEFAULT_STRATEGY);
 
         if (zRC != Z_OK) {
-            f->ctx = NULL;
             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                           "unable to init Zlib: "
                           "deflateInit2 returned %d: URL %s",
                           zRC, r->uri);
+            /*
+             * Remove ourselves as it does not make sense to return:
+             * We are not able to init libz and pass data down the chain
+             * uncompressed.
+             */
+            ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
-
-        /* RFC 1952 Section 2.3 dictates the gzip header:
-         *
-         * +---+---+---+---+---+---+---+---+---+---+
-         * |ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
-         * +---+---+---+---+---+---+---+---+---+---+
-         *
-         * If we wish to populate in MTIME (as hinted in RFC 1952), do:
-         * putLong(date_array, apr_time_now() / APR_USEC_PER_SEC);
-         * where date_array is a char[4] and then print date_array in the
-         * MTIME position.  WARNING: ENDIANNESS ISSUE HERE.
+        /*
+         * Register a cleanup function to ensure that we cleanup the internal
+         * libz resources.
          */
-        buf = apr_psprintf(r->pool, "%c%c%c%c%c%c%c%c%c%c", deflate_magic[0],
-                           deflate_magic[1], Z_DEFLATED, 0 /* flags */,
-                           0, 0, 0, 0 /* 4 chars for mtime */,
-                           0 /* xflags */, OS_CODE);
-        e = apr_bucket_pool_create(buf, 10, r->pool, f->c->bucket_alloc);
+        apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
+                                  apr_pool_cleanup_null);
+
+        /* add immortal gzip header */
+        e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
+                                       f->c->bucket_alloc);
         APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
 
         /* If the entire Content-Encoding is "identity", we can replace it. */
@@ -414,50 +453,21 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
         ctx->stream.next_out = ctx->buffer;
         ctx->stream.avail_out = c->bufferSize;
     }
-    
-    for (e = APR_BRIGADE_FIRST(bb);
-         e != APR_BRIGADE_SENTINEL(bb);
-         e = APR_BUCKET_NEXT(e))
+
+    while (!APR_BRIGADE_EMPTY(bb))
     {
         const char *data;
         apr_bucket *b;
         apr_size_t len;
 
-        int done = 0;
+        e = APR_BRIGADE_FIRST(bb);
 
         if (APR_BUCKET_IS_EOS(e)) {
             char *buf;
-            unsigned int deflate_len;
 
             ctx->stream.avail_in = 0; /* should be zero already anyway */
-            for (;;) {
-                deflate_len = c->bufferSize - ctx->stream.avail_out;
-
-                if (deflate_len != 0) {
-                    b = apr_bucket_heap_create((char *)ctx->buffer,
-                                               deflate_len, NULL,
-                                               f->c->bucket_alloc);
-                    APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
-                    ctx->stream.next_out = ctx->buffer;
-                    ctx->stream.avail_out = c->bufferSize;
-                }
-
-                if (done) {
-                    break;
-                }
-
-                zRC = deflate(&ctx->stream, Z_FINISH);
-
-                if (deflate_len == 0 && zRC == Z_BUF_ERROR) {
-                    zRC = Z_OK;
-                }
-
-                done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
-
-                if (zRC != Z_OK && zRC != Z_STREAM_END) {
-                    break;
-                }
-            }
+            /* flush the remaining data from the zlib buffers */
+            flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH);
 
             buf = apr_palloc(r->pool, 8);
             putLong((unsigned char *)&buf[0], ctx->crc);
@@ -497,6 +507,8 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             }
 
             deflateEnd(&ctx->stream);
+            /* No need for cleanup any longer */
+            apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
 
             /* Remove EOS from the old list, and insert into the new. */
             APR_BUCKET_REMOVE(e);
@@ -509,25 +521,18 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
         }
 
         if (APR_BUCKET_IS_FLUSH(e)) {
-            apr_bucket *bkt;
             apr_status_t rv;
-            if (ctx->stream.avail_in > 0) {
-                zRC = deflate(&(ctx->stream), Z_SYNC_FLUSH);
-                if (zRC != Z_OK) {
-                    return APR_EGENERAL;
-                }
-            }
-
-            ctx->stream.next_out = ctx->buffer;
-            len = c->bufferSize - ctx->stream.avail_out;
 
-            b = apr_bucket_heap_create((char *)ctx->buffer, len,
-                                       NULL, f->c->bucket_alloc);
-            APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
-            ctx->stream.avail_out = c->bufferSize;
+            /* flush the remaining data from the zlib buffers */
+            zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
+                                    Z_SYNC_FLUSH);
+            if (zRC != Z_OK) {
+                return APR_EGENERAL;
+            }
 
-            bkt = apr_bucket_flush_create(f->c->bucket_alloc);
-            APR_BRIGADE_INSERT_TAIL(ctx->bb, bkt);
+            /* Remove flush bucket from old brigade anf insert into the new. */
+            APR_BUCKET_REMOVE(e);
+            APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
             rv = ap_pass_brigade(f->next, ctx->bb);
             if (rv != APR_SUCCESS) {
                 return rv;
@@ -567,9 +572,12 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
 
             zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
 
-            if (zRC != Z_OK)
+            if (zRC != Z_OK) {
                 return APR_EGENERAL;
+            }
         }
+
+        apr_bucket_delete(e);
     }
 
     apr_brigade_cleanup(bb);
@@ -604,7 +612,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
         apr_size_t len;
 
         /* only work on main request/no subrequests */
-        if (r->main) {
+        if (!ap_is_initial_req(r)) {
             ap_remove_input_filter(f);
             return ap_get_brigade(f->next, bb, mode, block, readbytes);
         }
@@ -643,8 +651,8 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
             return rv;
         }
 
-        len = 10; 
-        rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len); 
+        len = 10;
+        rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
         if (rv != APR_SUCCESS) {
             return rv;
         }
@@ -800,7 +808,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
                 inflateEnd(&ctx->stream);
 
                 eos = apr_bucket_eos_create(f->c->bucket_alloc);
-                APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos); 
+                APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
                 break;
             }
 
@@ -840,10 +848,257 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
     return APR_SUCCESS;
 }
 
+
+/* Filter to inflate for a content-transforming proxy.  */
+static apr_status_t inflate_out_filter(ap_filter_t *f,
+                                      apr_bucket_brigade *bb)
+{
+    int zlib_method;
+    int zlib_flags;
+    int deflate_init = 1;
+    apr_bucket *bkt;
+    request_rec *r = f->r;
+    deflate_ctx *ctx = f->ctx;
+    int zRC;
+    apr_status_t rv;
+    deflate_filter_config *c;
+
+    /* Do nothing if asked to filter nothing. */
+    if (APR_BRIGADE_EMPTY(bb)) {
+        return ap_pass_brigade(f->next, bb);
+    }
+
+    c = ap_get_module_config(r->server->module_config, &deflate_module);
+
+    if (!ctx) {
+        int found = 0;
+        char *token;
+        const char *encoding;
+
+        /* only work on main request/no subrequests */
+        if (!ap_is_initial_req(r)) {
+            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 (encoding) {
+            const char *tmp = encoding;
+
+            token = ap_get_token(r->pool, &tmp, 0);
+            while (token && token[0]) {
+                if (!strcasecmp(token, "gzip")) {
+                    found = 1;
+                    break;
+                }
+                /* Otherwise, skip token */
+                tmp++;
+                token = ap_get_token(r->pool, &tmp, 0);
+            }
+        }
+
+        if (found == 0) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+        apr_table_unset(r->headers_out, "Content-Encoding");
+
+        /* No need to inflate HEAD or 204/304 */
+        if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+
+        f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
+        ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
+        ctx->buffer = apr_palloc(r->pool, c->bufferSize);
+
+
+        zRC = inflateInit2(&ctx->stream, c->windowSize);
+
+        if (zRC != Z_OK) {
+            f->ctx = NULL;
+            inflateEnd(&ctx->stream);
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                          "unable to init Zlib: "
+                          "inflateInit2 returned %d: URL %s",
+                          zRC, r->uri);
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        /* initialize deflate output buffer */
+        ctx->stream.next_out = ctx->buffer;
+        ctx->stream.avail_out = c->bufferSize;
+
+        deflate_init = 0;
+    }
+
+    for (bkt = APR_BRIGADE_FIRST(bb);
+         bkt != APR_BRIGADE_SENTINEL(bb);
+         bkt = APR_BUCKET_NEXT(bkt))
+    {
+        const char *data;
+        apr_size_t len;
+
+        /* If we actually see the EOS, that means we screwed up! */
+        /* no it doesn't - not in a HEAD or 204/304 */
+        if (APR_BUCKET_IS_EOS(bkt)) {
+            inflateEnd(&ctx->stream);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        if (APR_BUCKET_IS_FLUSH(bkt)) {
+            continue;
+        }
+
+        /* read */
+        apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
+
+        /* first bucket contains zlib header */
+        if (!deflate_init++) {
+            if (len < 10) {
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                              "Insufficient data for inflate");
+                return APR_EGENERAL;
+            }
+            else  {
+                zlib_method = data[2];
+                zlib_flags = data[3];
+                if (zlib_method != Z_DEFLATED) {
+                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                                  "inflate: data not deflated!");
+                    ap_remove_output_filter(f);
+                    return ap_pass_brigade(f->next, bb);
+                }
+                if (data[0] != deflate_magic[0] ||
+                    data[1] != deflate_magic[1] ||
+                    (zlib_flags & RESERVED) != 0) {
+                        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                                      "inflate: bad header");
+                    return APR_EGENERAL ;
+                }
+                data += 10 ;
+                len -= 10 ;
+           }
+           if (zlib_flags & EXTRA_FIELD) {
+               unsigned int bytes = (unsigned int)(data[0]);
+               bytes += ((unsigned int)(data[1])) << 8;
+               bytes += 2;
+               if (len < bytes) {
+                   ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                                 "inflate: extra field too big (not "
+                                 "supported)");
+                   return APR_EGENERAL;
+               }
+               data += bytes;
+               len -= bytes;
+           }
+           if (zlib_flags & ORIG_NAME) {
+               while (len-- && *data++);
+           }
+           if (zlib_flags & COMMENT) {
+               while (len-- && *data++);
+           }
+           if (zlib_flags & HEAD_CRC) {
+                len -= 2;
+                data += 2;
+           }
+        }
+
+        /* pass through zlib inflate. */
+        ctx->stream.next_in = (unsigned char *)data;
+        ctx->stream.avail_in = len;
+
+        zRC = Z_OK;
+
+        while (ctx->stream.avail_in != 0) {
+            if (ctx->stream.avail_out == 0) {
+                apr_bucket *tmp_heap;
+                ctx->stream.next_out = ctx->buffer;
+                len = c->bufferSize - ctx->stream.avail_out;
+
+                ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
+                tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
+                                                  NULL, f->c->bucket_alloc);
+                APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
+                ctx->stream.avail_out = c->bufferSize;
+            }
+
+            zRC = inflate(&ctx->stream, Z_NO_FLUSH);
+
+            if (zRC == Z_STREAM_END) {
+                break;
+            }
+
+            if (zRC != Z_OK) {
+                    inflateEnd(&ctx->stream);
+                    return APR_EGENERAL;
+            }
+        }
+        if (zRC == Z_STREAM_END) {
+            apr_bucket *tmp_heap, *eos;
+
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                          "Zlib: Inflated %ld to %ld : URL %s",
+                          ctx->stream.total_in, ctx->stream.total_out,
+                          r->uri);
+
+            len = c->bufferSize - ctx->stream.avail_out;
+
+            ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
+            tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
+                                              NULL, f->c->bucket_alloc);
+            APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
+            ctx->stream.avail_out = c->bufferSize;
+
+            /* Is the remaining 8 bytes already in the avail stream? */
+            if (ctx->stream.avail_in >= 8) {
+                unsigned long compCRC, compLen;
+                compCRC = getLong(ctx->stream.next_in);
+                if (ctx->crc != compCRC) {
+                    inflateEnd(&ctx->stream);
+                    return APR_EGENERAL;
+                }
+                ctx->stream.next_in += 4;
+                compLen = getLong(ctx->stream.next_in);
+                if (ctx->stream.total_out != compLen) {
+                    inflateEnd(&ctx->stream);
+                    return APR_EGENERAL;
+                }
+            }
+            else {
+                /* FIXME: We need to grab the 8 verification bytes
+                 * from the wire! */
+                inflateEnd(&ctx->stream);
+                return APR_EGENERAL;
+            }
+
+            inflateEnd(&ctx->stream);
+
+            eos = apr_bucket_eos_create(f->c->bucket_alloc);
+            APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
+            break;
+        }
+
+    }
+
+    rv = ap_pass_brigade(f->next, ctx->proc_bb);
+    apr_brigade_cleanup(ctx->proc_bb);
+    return rv ;
+}
+
+#define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
 static void register_hooks(apr_pool_t *p)
 {
     ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
                               AP_FTYPE_CONTENT_SET);
+    ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
+                              AP_FTYPE_RESOURCE-1);
     ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
                               AP_FTYPE_CONTENT_SET);
 }