]> granicus.if.org Git - apache/blob - modules/filters/mod_deflate.c
get mod_deflate to compile with compilers that care about the
[apache] / modules / filters / mod_deflate.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * (zlib functions gz_open and gzwrite)
56  */
57
58 /*
59  * mod_deflate.c: Perform deflate transfer-encoding on the fly
60  *
61  * Written by Ian Holsman (IanH@apache.org)
62  *
63  */
64
65 #include "httpd.h"
66 #include "http_config.h"
67 #include "http_log.h"
68 #include "apr_strings.h"
69 #include "apr_general.h"
70 #include "util_filter.h"
71 #include "apr_buckets.h"
72 #include "http_request.h"
73
74 #include "zlib.h"
75
76 #ifdef HAVE_ZUTIL_H
77 #include "zutil.h"
78 #else
79 /* As part of the encoding process, we must send what our OS_CODE is
80  * (or so it seems based on what I can tell of how gzip encoding works).
81  *
82  * zutil.h is not always included with zlib distributions (it is a private
83  * header), so this is straight from zlib 1.1.3's zutil.h.
84  */
85 #ifdef OS2
86 #define OS_CODE  0x06
87 #endif
88
89 #ifdef WIN32 /* Window 95 & Windows NT */
90 #define OS_CODE  0x0b
91 #endif
92
93 #if defined(VAXC) || defined(VMS)
94 #define OS_CODE  0x02
95 #endif
96
97 #ifdef AMIGA
98 #define OS_CODE  0x01
99 #endif
100
101 #if defined(ATARI) || defined(atarist)
102 #define OS_CODE  0x05
103 #endif
104
105 #if defined(MACOS) || defined(TARGET_OS_MAC)
106 #define OS_CODE  0x07
107 #endif
108
109 #ifdef __50SERIES /* Prime/PRIMOS */
110 #define OS_CODE  0x0F
111 #endif
112
113 #ifdef TOPS20
114 #define OS_CODE  0x0a
115 #endif
116
117 #ifndef OS_CODE
118 #define OS_CODE  0x03  /* assume Unix */
119 #endif
120 #endif
121
122 static const char deflateFilterName[] = "DEFLATE";
123 module AP_MODULE_DECLARE_DATA deflate_module;
124
125 typedef struct deflate_filter_config_t
126 {
127     int windowSize;
128     int memlevel;
129     int bufferSize;
130     char *noteName;
131 } deflate_filter_config;
132
133 /* windowsize is negative to suppress Zlib header */
134 #define DEFAULT_WINDOWSIZE -15
135 #define DEFAULT_MEMLEVEL 9
136 #define DEFAULT_BUFFERSIZE 8096
137
138 /* Outputs a long in LSB order to the given file
139  * only the bottom 4 bits are required for the deflate file format.
140  */
141 static void putLong(char *string, unsigned long x)
142 {
143     int n;
144     for (n = 0; n < 4; n++) {
145         string[n] = (int) (x & 0xff);
146         x >>= 8;
147     }
148 }
149
150 /* Inputs a string and returns a long.
151  */
152 static unsigned long getLong(unsigned char *string)
153 {
154     int n = 3;
155     unsigned long x = 0;
156
157     while (n) {
158         x |= (unsigned long)(string[n--]) & 0xff;
159         x <<= 8;
160     }
161
162     x |= (unsigned long)(string[0]) & 0xff;
163     return x;
164 }
165
166 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
167 {
168     deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
169
170     c->memlevel   = DEFAULT_MEMLEVEL;
171     c->windowSize = DEFAULT_WINDOWSIZE;
172     c->bufferSize = DEFAULT_BUFFERSIZE;
173
174     return c;
175 }
176
177 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
178                                            const char *arg)
179 {
180     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
181                                                     &deflate_module);
182     int i;
183
184     i = atoi(arg);
185
186     if (i < 1 || i > 15)
187         return "DeflateWindowSize must be between 1 and 15";
188
189     c->windowSize = i * -1;
190
191     return NULL;
192 }
193
194 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
195                                            const char *arg)
196 {
197     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
198                                                     &deflate_module);
199
200     c->bufferSize = atoi(arg);
201
202     if (c->bufferSize <= 0) {
203         return "DeflateBufferSize should be positive";
204     }
205
206     return NULL;
207 }
208 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
209                                     const char *arg)
210 {
211     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
212                                                     &deflate_module);
213     c->noteName = apr_pstrdup(cmd->pool, arg);
214
215     return NULL;
216 }
217
218 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
219                                         const char *arg)
220 {
221     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
222                                                     &deflate_module);
223     int i;
224
225     i = atoi(arg);
226
227     if (i < 1 || i > 9)
228         return "DeflateMemLevel must be between 1 and 9";
229
230     c->memlevel = i;
231
232     return NULL;
233 }
234
235 /* magic header */
236 static char deflate_magic[2] = { '\037', '\213' };
237
238 typedef struct deflate_ctx_t
239 {
240     z_stream stream;
241     unsigned char *buffer;
242     unsigned long crc;
243     apr_bucket_brigade *bb, *proc_bb;
244 } deflate_ctx;
245
246 static apr_status_t deflate_out_filter(ap_filter_t *f,
247                                        apr_bucket_brigade *bb)
248 {
249     apr_bucket *e;
250     request_rec *r = f->r;
251     deflate_ctx *ctx = f->ctx;
252     int zRC;
253     deflate_filter_config *c = ap_get_module_config(r->server->module_config,
254                                                     &deflate_module);
255
256     /* If we don't have a context, we need to ensure that it is okay to send
257      * the deflated content.  If we have a context, that means we've done
258      * this before and we liked it.
259      * This could be not so nice if we always fail.  But, if we succeed,
260      * we're in better shape.
261      */
262     if (!ctx) {
263         char *buf, *token;
264         const char *encoding, *accepts;
265
266         /* only work on main request/no subrequests */
267         if (r->main) {
268             ap_remove_output_filter(f);
269             return ap_pass_brigade(f->next, bb);
270         }
271
272         /* some browsers might have problems, so set no-gzip
273          * (with browsermatch) for them
274          */
275         if (apr_table_get(r->subprocess_env, "no-gzip")) {
276             ap_remove_output_filter(f);
277             return ap_pass_brigade(f->next, bb);
278         }
279
280         /* Some browsers might have problems with content types
281          * other than text/html, so set gzip-only-text/html
282          * (with browsermatch) for them
283          */
284         if ((r->content_type == NULL
285              || strncmp(r->content_type, "text/html", 9))
286             && apr_table_get(r->subprocess_env, "gzip-only-text/html")) {
287             ap_remove_output_filter(f);
288             return ap_pass_brigade(f->next, bb);
289         }
290
291         /* Let's see what our current Content-Encoding is.
292          * If gzip is present, don't gzip again.  (We could, but let's not.)
293          */
294         encoding = apr_table_get(r->headers_out, "Content-Encoding");
295         if (encoding) {
296             const char *tmp = encoding;
297
298             token = ap_get_token(r->pool, &tmp, 0);
299             while (token && token[0]) {
300                 if (!strcasecmp(token, "gzip")) {
301                     ap_remove_output_filter(f);
302                     return ap_pass_brigade(f->next, bb);                        
303                 }
304                 /* Otherwise, skip token */
305                 tmp++;
306                 token = ap_get_token(r->pool, &tmp, 0);
307             }
308         }
309
310         /* if they don't have the line, then they can't play */
311         accepts = apr_table_get(r->headers_in, "Accept-Encoding");
312         if (accepts == NULL) {
313             ap_remove_output_filter(f);
314             return ap_pass_brigade(f->next, bb);
315         }
316
317         token = ap_get_token(r->pool, &accepts, 0);
318         while (token && token[0] && strcasecmp(token, "gzip")) {
319             /* skip token */
320             accepts++;
321             token = ap_get_token(r->pool, &accepts, 0);
322         }
323
324         /* No acceptable token found. */
325         if (token == NULL || token[0] == '\0') {
326             ap_remove_output_filter(f);
327             return ap_pass_brigade(f->next, bb);
328         }
329
330         /* We're cool with filtering this. */
331         ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
332         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
333         ctx->buffer = apr_palloc(r->pool, c->bufferSize);
334
335         zRC = deflateInit2(&ctx->stream, Z_BEST_SPEED, Z_DEFLATED,
336                            c->windowSize, c->memlevel,
337                            Z_DEFAULT_STRATEGY);
338
339         if (zRC != Z_OK) {
340             f->ctx = NULL;
341             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
342                           "unable to init Zlib: "
343                           "deflateInit2 returned %d: URL %s",
344                           zRC, r->uri);
345             return ap_pass_brigade(f->next, bb);
346         }
347
348         /* RFC 1952 Section 2.3 dictates the gzip header:
349          *
350          * +---+---+---+---+---+---+---+---+---+---+
351          * |ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
352          * +---+---+---+---+---+---+---+---+---+---+
353          *
354          * If we wish to populate in MTIME (as hinted in RFC 1952), do:
355          * putLong(date_array, apr_time_now() / APR_USEC_PER_SEC);
356          * where date_array is a char[4] and then print date_array in the
357          * MTIME position.
358          */
359         buf = apr_psprintf(r->pool, "%c%c%c%c%c%c%c%c%c%c", deflate_magic[0],
360                            deflate_magic[1], Z_DEFLATED, 0 /* flags */,
361                            0, 0, 0, 0 /* 4 chars for mtime */,
362                            0 /* xflags */, OS_CODE);
363         e = apr_bucket_pool_create(buf, 10, r->pool, f->c->bucket_alloc);
364         APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
365
366         /* If the entire Content-Encoding is "identity", we can replace it. */
367         if (!encoding || !strcasecmp(encoding, "identity")) {
368             apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
369         }
370         else {
371             apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
372         }
373         apr_table_setn(r->headers_out, "Vary", "Accept-Encoding");
374         apr_table_unset(r->headers_out, "Content-Length");
375     }
376     
377     /* initialize deflate output buffer */
378     ctx->stream.next_out = ctx->buffer;
379     ctx->stream.avail_out = c->bufferSize;
380
381     APR_BRIGADE_FOREACH(e, bb) {
382         const char *data;
383         apr_bucket *b;
384         apr_size_t len;
385
386         int done = 0;
387
388         if (APR_BUCKET_IS_EOS(e)) {
389             char *buf, *p;
390             char crc_array[4], len_array[4];
391             unsigned int deflate_len;
392
393             ctx->stream.avail_in = 0; /* should be zero already anyway */
394             for (;;) {
395                 deflate_len = c->bufferSize - ctx->stream.avail_out;
396
397                 if (deflate_len != 0) {
398                     b = apr_bucket_heap_create((char *)ctx->buffer,
399                                                deflate_len, NULL,
400                                                f->c->bucket_alloc);
401                     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
402                     ctx->stream.next_out = ctx->buffer;
403                     ctx->stream.avail_out = c->bufferSize;
404                 }
405
406                 if (done) {
407                     break;
408                 }
409
410                 zRC = deflate(&ctx->stream, Z_FINISH);
411
412                 if (deflate_len == 0 && zRC == Z_BUF_ERROR) {
413                     zRC = Z_OK;
414                 }
415
416                 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
417
418                 if (zRC != Z_OK && zRC != Z_STREAM_END) {
419                     break;
420                 }
421             }
422
423             putLong(crc_array, ctx->crc);
424             putLong(len_array, ctx->stream.total_in);
425
426             p = buf = apr_palloc(r->pool, 8);
427             *p++ = crc_array[0];
428             *p++ = crc_array[1];
429             *p++ = crc_array[2];
430             *p++ = crc_array[3];
431             *p++ = len_array[0];
432             *p++ = len_array[1];
433             *p++ = len_array[2];
434             *p++ = len_array[3];
435
436             b = apr_bucket_pool_create(buf, 8, r->pool, f->c->bucket_alloc);
437             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
438             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
439                           "Zlib: Compressed %ld to %ld : URL %s",
440                           ctx->stream.total_in, ctx->stream.total_out, r->uri);
441
442             if (c->noteName) {
443                 if (ctx->stream.total_in > 0) {
444                     int total;
445
446                     total = ctx->stream.total_out * 100 / ctx->stream.total_in;
447
448                     apr_table_setn(r->notes, c->noteName,
449                                    apr_itoa(r->pool, total));
450                 }
451                 else {
452                     apr_table_setn(r->notes, c->noteName, "-");
453                 }
454             }
455
456             deflateEnd(&ctx->stream);
457
458             /* Remove EOS from the old list, and insert into the new. */
459             APR_BUCKET_REMOVE(e);
460             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
461
462             /* Okay, we've seen the EOS.
463              * Time to pass it along down the chain.
464              */
465             return ap_pass_brigade(f->next, ctx->bb);
466         }
467
468         if (APR_BUCKET_IS_FLUSH(e)) {
469             apr_bucket *bkt;
470             zRC = deflate(&(ctx->stream), Z_SYNC_FLUSH);
471             if (zRC != Z_OK) {
472                 return APR_EGENERAL;
473             }
474
475             ctx->stream.next_out = ctx->buffer;
476             len = c->bufferSize - ctx->stream.avail_out;
477
478             b = apr_bucket_heap_create((char *)ctx->buffer, len,
479                                        NULL, f->c->bucket_alloc);
480             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
481             ctx->stream.avail_out = c->bufferSize;
482
483             bkt = apr_bucket_flush_create(f->c->bucket_alloc);
484             APR_BRIGADE_INSERT_TAIL(ctx->bb, bkt);
485             ap_pass_brigade(f->next, ctx->bb);
486             continue;
487         }
488
489         /* read */
490         apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
491
492         /* This crc32 function is from zlib. */
493         ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
494
495         /* write */
496         ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
497                                                       * but we'll just have to
498                                                       * trust zlib */
499         ctx->stream.avail_in = len;
500
501         while (ctx->stream.avail_in != 0) {
502             if (ctx->stream.avail_out == 0) {
503                 ctx->stream.next_out = ctx->buffer;
504                 len = c->bufferSize - ctx->stream.avail_out;
505
506                 b = apr_bucket_heap_create((char *)ctx->buffer, len,
507                                            NULL, f->c->bucket_alloc);
508                 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
509                 ctx->stream.avail_out = c->bufferSize;
510             }
511
512             zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
513
514             if (zRC != Z_OK)
515                 return APR_EGENERAL;
516         }
517     }
518
519     apr_brigade_destroy(bb);
520     return APR_SUCCESS;
521 }
522
523 /* This is the deflate input filter (inflates).  */
524 static apr_status_t deflate_in_filter(ap_filter_t *f,
525                                       apr_bucket_brigade *bb,
526                                       ap_input_mode_t mode,
527                                       apr_read_type_e block,
528                                       apr_off_t readbytes)
529 {
530     apr_bucket *bkt;
531     request_rec *r = f->r;
532     deflate_ctx *ctx = f->ctx;
533     int zRC;
534     apr_status_t rv;
535     deflate_filter_config *c;
536
537     /* just get out of the way of things we don't want. */
538     if (mode != AP_MODE_READBYTES) {
539         return ap_get_brigade(f->next, bb, mode, block, readbytes);
540     }
541
542     c = ap_get_module_config(r->server->module_config, &deflate_module);
543
544     if (!ctx) {
545         int found = 0;
546         char *token, deflate_hdr[10];
547         const char *encoding;
548         apr_size_t len;
549
550         /* only work on main request/no subrequests */
551         if (r->main) {
552             ap_remove_input_filter(f);
553             return ap_get_brigade(f->next, bb, mode, block, readbytes);
554         }
555
556         /* Let's see what our current Content-Encoding is.
557          * If gzip is present, don't gzip again.  (We could, but let's not.)
558          */
559         encoding = apr_table_get(r->headers_in, "Content-Encoding");
560         if (encoding) {
561             const char *tmp = encoding;
562
563             token = ap_get_token(r->pool, &tmp, 0);
564             while (token && token[0]) {
565                 if (!strcasecmp(token, "gzip")) {
566                     found = 1;
567                     break;
568                 }
569                 /* Otherwise, skip token */
570                 tmp++;
571                 token = ap_get_token(r->pool, &tmp, 0);
572             }
573         }
574
575         if (found == 0) {
576             ap_remove_input_filter(f);
577             return ap_get_brigade(f->next, bb, mode, block, readbytes);
578         }
579
580         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
581         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
582         ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
583         ctx->buffer = apr_palloc(r->pool, c->bufferSize);
584
585         rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
586         if (rv != APR_SUCCESS) {
587             return rv;
588         }
589       
590         len = 10; 
591         rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len); 
592         if (rv != APR_SUCCESS) {
593             return rv;
594         }
595
596         /* We didn't get the magic bytes. */
597         if (len != 10 ||
598             deflate_hdr[0] != deflate_magic[0] ||
599             deflate_hdr[1] != deflate_magic[1]) {
600             return APR_EGENERAL;
601         }
602
603         /* We can't handle flags for now. */
604         if (deflate_hdr[3] != 0) {
605             return APR_EGENERAL;
606         }
607
608         zRC = inflateInit2(&ctx->stream, c->windowSize);
609
610         if (zRC != Z_OK) {
611             f->ctx = NULL;
612             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
613                           "unable to init Zlib: "
614                           "inflateInit2 returned %d: URL %s",
615                           zRC, r->uri);
616             ap_remove_input_filter(f);
617             return ap_get_brigade(f->next, bb, mode, block, readbytes);
618         }
619
620         /* initialize deflate output buffer */
621         ctx->stream.next_out = ctx->buffer;
622         ctx->stream.avail_out = c->bufferSize;
623
624         apr_brigade_cleanup(ctx->bb);
625     }
626
627     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
628         rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
629
630         if (rv != APR_SUCCESS) {
631             return rv;
632         }
633
634         APR_BRIGADE_FOREACH(bkt, ctx->bb) {
635             const char *data;
636             apr_size_t len;
637
638             /* If we actually see the EOS, that means we screwed up! */
639             if (APR_BUCKET_IS_EOS(bkt)) {
640                 return APR_EGENERAL;
641             }
642
643             if (APR_BUCKET_IS_FLUSH(bkt)) {
644                 apr_bucket *tmp_heap;
645                 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
646                 if (zRC != Z_OK) {
647                     return APR_EGENERAL;
648                 }
649
650                 ctx->stream.next_out = ctx->buffer;
651                 len = c->bufferSize - ctx->stream.avail_out;
652
653                 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
654                 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
655                                                  NULL, f->c->bucket_alloc);
656                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
657                 ctx->stream.avail_out = c->bufferSize;
658
659                 /* Move everything to the returning brigade. */
660                 APR_BUCKET_REMOVE(bkt);
661                 APR_BRIGADE_CONCAT(bb, ctx->bb);
662                 break;
663             }
664
665             /* read */
666             apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
667
668             /* pass through zlib inflate. */
669             ctx->stream.next_in = (unsigned char *)data;
670             ctx->stream.avail_in = len;
671
672             while (ctx->stream.avail_in != 0) {
673                 if (ctx->stream.avail_out == 0) {
674                     apr_bucket *tmp_heap;
675                     ctx->stream.next_out = ctx->buffer;
676                     len = c->bufferSize - ctx->stream.avail_out;
677
678                     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
679                     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
680                                                       NULL, f->c->bucket_alloc);
681                     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
682                     ctx->stream.avail_out = c->bufferSize;
683                 }
684
685                 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
686
687                 if (zRC == Z_STREAM_END) {
688                     break;
689                 }
690
691                 if (zRC != Z_OK) {
692                     return APR_EGENERAL;
693                 }
694             }
695             if (zRC == Z_STREAM_END) {
696                 apr_bucket *tmp_heap, *eos;
697
698                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
699                               "Zlib: Inflated %ld to %ld : URL %s",
700                               ctx->stream.total_in, ctx->stream.total_out,
701                               r->uri);
702
703                 len = c->bufferSize - ctx->stream.avail_out;
704
705                 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
706                 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
707                                                   NULL, f->c->bucket_alloc);
708                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
709                 ctx->stream.avail_out = c->bufferSize;
710
711                 /* Is the remaining 8 bytes already in the avail stream? */
712                 if (ctx->stream.avail_in >= 8) {
713                     unsigned long compCRC, compLen;
714                     compCRC = getLong(ctx->stream.next_in);
715                     if (ctx->crc != compCRC) {
716                         return APR_EGENERAL;
717                     }
718                     ctx->stream.next_in += 4;
719                     compLen = getLong(ctx->stream.next_in);
720                     if (ctx->stream.total_out != compLen) {
721                         return APR_EGENERAL;
722                     }
723                 }
724                 else {
725                     /* FIXME: We need to grab the 8 verification bytes
726                      * from the wire! */
727                     return APR_EGENERAL;
728                 }
729
730                 inflateEnd(&ctx->stream);
731                 apr_brigade_cleanup(ctx->bb);
732     
733                 eos = apr_bucket_eos_create(f->c->bucket_alloc);
734                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos); 
735                 break;
736             }
737         }
738     }
739
740     if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
741         apr_bucket_brigade *newbb;
742
743         /* May return APR_INCOMPLETE which is fine by us. */
744         apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
745
746         newbb = apr_brigade_split(ctx->proc_bb, bkt);
747         APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
748         APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
749     }
750
751     
752
753     return APR_SUCCESS;
754 }
755
756 static void register_hooks(apr_pool_t *p)
757 {
758     ap_register_output_filter(deflateFilterName, deflate_out_filter,
759                               AP_FTYPE_CONTENT_SET);
760     ap_register_input_filter(deflateFilterName, deflate_in_filter,
761                               AP_FTYPE_CONTENT_SET);
762 }
763
764 static const command_rec deflate_filter_cmds[] = {
765     AP_INIT_TAKE1("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
766                   "Set a note to report on compression ratio"),
767     AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
768                   RSRC_CONF, "Set the Deflate window size (1-15)"),
769     AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
770                   "Set the Deflate Buffer Size"),
771     AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
772                   "Set the Deflate Memory Level (1-9)"),
773     {NULL}
774 };
775
776 module AP_MODULE_DECLARE_DATA deflate_module = {
777     STANDARD20_MODULE_STUFF,
778     NULL,                         /* dir config creater */
779     NULL,                         /* dir merger --- default is to override */
780     create_deflate_server_config, /* server config */
781     NULL,                         /* merge server config */
782     deflate_filter_cmds,          /* command table */
783     register_hooks                /* register hooks */
784 };