]> granicus.if.org Git - apache/blob - modules/filters/mod_deflate.c
Oops.
[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(unsigned char *string, unsigned long x)
142 {
143     string[0] = (unsigned char)(x & 0xff);
144     string[1] = (unsigned char)((x & 0xff00) >> 8);
145     string[2] = (unsigned char)((x & 0xff0000) >> 16);
146     string[3] = (unsigned char)((x & 0xff000000) >> 24);
147 }
148
149 /* Inputs a string and returns a long.
150  */
151 static unsigned long getLong(unsigned char *string)
152 {
153     return ((unsigned long)string[0])
154           | (((unsigned long)string[1]) << 8)
155           | (((unsigned long)string[2]) << 16)
156           | (((unsigned long)string[3]) << 24);
157 }
158
159 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
160 {
161     deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
162
163     c->memlevel   = DEFAULT_MEMLEVEL;
164     c->windowSize = DEFAULT_WINDOWSIZE;
165     c->bufferSize = DEFAULT_BUFFERSIZE;
166
167     return c;
168 }
169
170 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
171                                            const char *arg)
172 {
173     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
174                                                     &deflate_module);
175     int i;
176
177     i = atoi(arg);
178
179     if (i < 1 || i > 15)
180         return "DeflateWindowSize must be between 1 and 15";
181
182     c->windowSize = i * -1;
183
184     return NULL;
185 }
186
187 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
188                                            const char *arg)
189 {
190     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
191                                                     &deflate_module);
192
193     c->bufferSize = atoi(arg);
194
195     if (c->bufferSize <= 0) {
196         return "DeflateBufferSize should be positive";
197     }
198
199     return NULL;
200 }
201 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
202                                     const char *arg)
203 {
204     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
205                                                     &deflate_module);
206     c->noteName = apr_pstrdup(cmd->pool, arg);
207
208     return NULL;
209 }
210
211 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
212                                         const char *arg)
213 {
214     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
215                                                     &deflate_module);
216     int i;
217
218     i = atoi(arg);
219
220     if (i < 1 || i > 9)
221         return "DeflateMemLevel must be between 1 and 9";
222
223     c->memlevel = i;
224
225     return NULL;
226 }
227
228 /* magic header */
229 static char deflate_magic[2] = { '\037', '\213' };
230
231 typedef struct deflate_ctx_t
232 {
233     z_stream stream;
234     unsigned char *buffer;
235     unsigned long crc;
236     apr_bucket_brigade *bb, *proc_bb;
237 } deflate_ctx;
238
239 static apr_status_t deflate_out_filter(ap_filter_t *f,
240                                        apr_bucket_brigade *bb)
241 {
242     apr_bucket *e;
243     request_rec *r = f->r;
244     deflate_ctx *ctx = f->ctx;
245     int zRC;
246     deflate_filter_config *c = ap_get_module_config(r->server->module_config,
247                                                     &deflate_module);
248
249     /* If we don't have a context, we need to ensure that it is okay to send
250      * the deflated content.  If we have a context, that means we've done
251      * this before and we liked it.
252      * This could be not so nice if we always fail.  But, if we succeed,
253      * we're in better shape.
254      */
255     if (!ctx) {
256         char *buf, *token;
257         const char *encoding, *accepts;
258
259         /* only work on main request/no subrequests */
260         if (r->main) {
261             ap_remove_output_filter(f);
262             return ap_pass_brigade(f->next, bb);
263         }
264
265         /* some browsers might have problems, so set no-gzip
266          * (with browsermatch) for them
267          */
268         if (apr_table_get(r->subprocess_env, "no-gzip")) {
269             ap_remove_output_filter(f);
270             return ap_pass_brigade(f->next, bb);
271         }
272
273         /* Some browsers might have problems with content types
274          * other than text/html, so set gzip-only-text/html
275          * (with browsermatch) for them
276          */
277         if ((r->content_type == NULL
278              || strncmp(r->content_type, "text/html", 9))
279             && apr_table_get(r->subprocess_env, "gzip-only-text/html")) {
280             ap_remove_output_filter(f);
281             return ap_pass_brigade(f->next, bb);
282         }
283
284         /* Let's see what our current Content-Encoding is.
285          * If gzip is present, don't gzip again.  (We could, but let's not.)
286          */
287         encoding = apr_table_get(r->headers_out, "Content-Encoding");
288         if (encoding) {
289             const char *tmp = encoding;
290
291             token = ap_get_token(r->pool, &tmp, 0);
292             while (token && token[0]) {
293                 if (!strcasecmp(token, "gzip")) {
294                     ap_remove_output_filter(f);
295                     return ap_pass_brigade(f->next, bb);                        
296                 }
297                 /* Otherwise, skip token */
298                 tmp++;
299                 token = ap_get_token(r->pool, &tmp, 0);
300             }
301         }
302
303         /* if they don't have the line, then they can't play */
304         accepts = apr_table_get(r->headers_in, "Accept-Encoding");
305         if (accepts == NULL) {
306             ap_remove_output_filter(f);
307             return ap_pass_brigade(f->next, bb);
308         }
309
310         token = ap_get_token(r->pool, &accepts, 0);
311         while (token && token[0] && strcasecmp(token, "gzip")) {
312             /* skip token */
313             accepts++;
314             token = ap_get_token(r->pool, &accepts, 0);
315         }
316
317         /* No acceptable token found. */
318         if (token == NULL || token[0] == '\0') {
319             ap_remove_output_filter(f);
320             return ap_pass_brigade(f->next, bb);
321         }
322
323         /* We're cool with filtering this. */
324         ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
325         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
326         ctx->buffer = apr_palloc(r->pool, c->bufferSize);
327
328         zRC = deflateInit2(&ctx->stream, Z_BEST_SPEED, Z_DEFLATED,
329                            c->windowSize, c->memlevel,
330                            Z_DEFAULT_STRATEGY);
331
332         if (zRC != Z_OK) {
333             f->ctx = NULL;
334             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
335                           "unable to init Zlib: "
336                           "deflateInit2 returned %d: URL %s",
337                           zRC, r->uri);
338             return ap_pass_brigade(f->next, bb);
339         }
340
341         /* RFC 1952 Section 2.3 dictates the gzip header:
342          *
343          * +---+---+---+---+---+---+---+---+---+---+
344          * |ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
345          * +---+---+---+---+---+---+---+---+---+---+
346          *
347          * If we wish to populate in MTIME (as hinted in RFC 1952), do:
348          * putLong(date_array, apr_time_now() / APR_USEC_PER_SEC);
349          * where date_array is a char[4] and then print date_array in the
350          * MTIME position.
351          */
352         buf = apr_psprintf(r->pool, "%c%c%c%c%c%c%c%c%c%c", deflate_magic[0],
353                            deflate_magic[1], Z_DEFLATED, 0 /* flags */,
354                            0, 0, 0, 0 /* 4 chars for mtime */,
355                            0 /* xflags */, OS_CODE);
356         e = apr_bucket_pool_create(buf, 10, r->pool, f->c->bucket_alloc);
357         APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
358
359         /* If the entire Content-Encoding is "identity", we can replace it. */
360         if (!encoding || !strcasecmp(encoding, "identity")) {
361             apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
362         }
363         else {
364             apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
365         }
366         apr_table_setn(r->headers_out, "Vary", "Accept-Encoding");
367         apr_table_unset(r->headers_out, "Content-Length");
368
369         /* initialize deflate output buffer */
370         ctx->stream.next_out = ctx->buffer;
371         ctx->stream.avail_out = c->bufferSize;
372     }
373     
374     APR_BRIGADE_FOREACH(e, bb) {
375         const char *data;
376         apr_bucket *b;
377         apr_size_t len;
378
379         int done = 0;
380
381         if (APR_BUCKET_IS_EOS(e)) {
382             char *buf;
383             unsigned int deflate_len;
384
385             ctx->stream.avail_in = 0; /* should be zero already anyway */
386             for (;;) {
387                 deflate_len = c->bufferSize - ctx->stream.avail_out;
388
389                 if (deflate_len != 0) {
390                     b = apr_bucket_heap_create((char *)ctx->buffer,
391                                                deflate_len, NULL,
392                                                f->c->bucket_alloc);
393                     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
394                     ctx->stream.next_out = ctx->buffer;
395                     ctx->stream.avail_out = c->bufferSize;
396                 }
397
398                 if (done) {
399                     break;
400                 }
401
402                 zRC = deflate(&ctx->stream, Z_FINISH);
403
404                 if (deflate_len == 0 && zRC == Z_BUF_ERROR) {
405                     zRC = Z_OK;
406                 }
407
408                 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
409
410                 if (zRC != Z_OK && zRC != Z_STREAM_END) {
411                     break;
412                 }
413             }
414
415             buf = apr_palloc(r->pool, 8);
416             putLong((unsigned char *)&buf[0], ctx->crc);
417             putLong((unsigned char *)&buf[4], ctx->stream.total_in);
418
419             b = apr_bucket_pool_create(buf, 8, r->pool, f->c->bucket_alloc);
420             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
421             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
422                           "Zlib: Compressed %ld to %ld : URL %s",
423                           ctx->stream.total_in, ctx->stream.total_out, r->uri);
424
425             if (c->noteName) {
426                 if (ctx->stream.total_in > 0) {
427                     int total;
428
429                     total = ctx->stream.total_out * 100 / ctx->stream.total_in;
430
431                     apr_table_setn(r->notes, c->noteName,
432                                    apr_itoa(r->pool, total));
433                 }
434                 else {
435                     apr_table_setn(r->notes, c->noteName, "-");
436                 }
437             }
438
439             deflateEnd(&ctx->stream);
440
441             /* Remove EOS from the old list, and insert into the new. */
442             APR_BUCKET_REMOVE(e);
443             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
444
445             /* Okay, we've seen the EOS.
446              * Time to pass it along down the chain.
447              */
448             return ap_pass_brigade(f->next, ctx->bb);
449         }
450
451         if (APR_BUCKET_IS_FLUSH(e)) {
452             apr_bucket *bkt;
453             zRC = deflate(&(ctx->stream), Z_SYNC_FLUSH);
454             if (zRC != Z_OK) {
455                 return APR_EGENERAL;
456             }
457
458             ctx->stream.next_out = ctx->buffer;
459             len = c->bufferSize - ctx->stream.avail_out;
460
461             b = apr_bucket_heap_create((char *)ctx->buffer, len,
462                                        NULL, f->c->bucket_alloc);
463             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
464             ctx->stream.avail_out = c->bufferSize;
465
466             bkt = apr_bucket_flush_create(f->c->bucket_alloc);
467             APR_BRIGADE_INSERT_TAIL(ctx->bb, bkt);
468             ap_pass_brigade(f->next, ctx->bb);
469             continue;
470         }
471
472         /* read */
473         apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
474
475         /* This crc32 function is from zlib. */
476         ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
477
478         /* write */
479         ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
480                                                       * but we'll just have to
481                                                       * trust zlib */
482         ctx->stream.avail_in = len;
483
484         while (ctx->stream.avail_in != 0) {
485             if (ctx->stream.avail_out == 0) {
486                 ctx->stream.next_out = ctx->buffer;
487                 len = c->bufferSize - ctx->stream.avail_out;
488
489                 b = apr_bucket_heap_create((char *)ctx->buffer, len,
490                                            NULL, f->c->bucket_alloc);
491                 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
492                 ctx->stream.avail_out = c->bufferSize;
493             }
494
495             zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
496
497             if (zRC != Z_OK)
498                 return APR_EGENERAL;
499         }
500     }
501
502     apr_brigade_destroy(bb);
503     return APR_SUCCESS;
504 }
505
506 /* This is the deflate input filter (inflates).  */
507 static apr_status_t deflate_in_filter(ap_filter_t *f,
508                                       apr_bucket_brigade *bb,
509                                       ap_input_mode_t mode,
510                                       apr_read_type_e block,
511                                       apr_off_t readbytes)
512 {
513     apr_bucket *bkt;
514     request_rec *r = f->r;
515     deflate_ctx *ctx = f->ctx;
516     int zRC;
517     apr_status_t rv;
518     deflate_filter_config *c;
519
520     /* just get out of the way of things we don't want. */
521     if (mode != AP_MODE_READBYTES) {
522         return ap_get_brigade(f->next, bb, mode, block, readbytes);
523     }
524
525     c = ap_get_module_config(r->server->module_config, &deflate_module);
526
527     if (!ctx) {
528         int found = 0;
529         char *token, deflate_hdr[10];
530         const char *encoding;
531         apr_size_t len;
532
533         /* only work on main request/no subrequests */
534         if (r->main) {
535             ap_remove_input_filter(f);
536             return ap_get_brigade(f->next, bb, mode, block, readbytes);
537         }
538
539         /* Let's see what our current Content-Encoding is.
540          * If gzip is present, don't gzip again.  (We could, but let's not.)
541          */
542         encoding = apr_table_get(r->headers_in, "Content-Encoding");
543         if (encoding) {
544             const char *tmp = encoding;
545
546             token = ap_get_token(r->pool, &tmp, 0);
547             while (token && token[0]) {
548                 if (!strcasecmp(token, "gzip")) {
549                     found = 1;
550                     break;
551                 }
552                 /* Otherwise, skip token */
553                 tmp++;
554                 token = ap_get_token(r->pool, &tmp, 0);
555             }
556         }
557
558         if (found == 0) {
559             ap_remove_input_filter(f);
560             return ap_get_brigade(f->next, bb, mode, block, readbytes);
561         }
562
563         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
564         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
565         ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
566         ctx->buffer = apr_palloc(r->pool, c->bufferSize);
567
568         rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
569         if (rv != APR_SUCCESS) {
570             return rv;
571         }
572       
573         len = 10; 
574         rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len); 
575         if (rv != APR_SUCCESS) {
576             return rv;
577         }
578
579         /* We didn't get the magic bytes. */
580         if (len != 10 ||
581             deflate_hdr[0] != deflate_magic[0] ||
582             deflate_hdr[1] != deflate_magic[1]) {
583             return APR_EGENERAL;
584         }
585
586         /* We can't handle flags for now. */
587         if (deflate_hdr[3] != 0) {
588             return APR_EGENERAL;
589         }
590
591         zRC = inflateInit2(&ctx->stream, c->windowSize);
592
593         if (zRC != Z_OK) {
594             f->ctx = NULL;
595             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
596                           "unable to init Zlib: "
597                           "inflateInit2 returned %d: URL %s",
598                           zRC, r->uri);
599             ap_remove_input_filter(f);
600             return ap_get_brigade(f->next, bb, mode, block, readbytes);
601         }
602
603         /* initialize deflate output buffer */
604         ctx->stream.next_out = ctx->buffer;
605         ctx->stream.avail_out = c->bufferSize;
606
607         apr_brigade_cleanup(ctx->bb);
608     }
609
610     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
611         rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
612
613         if (rv != APR_SUCCESS) {
614             return rv;
615         }
616
617         APR_BRIGADE_FOREACH(bkt, ctx->bb) {
618             const char *data;
619             apr_size_t len;
620
621             /* If we actually see the EOS, that means we screwed up! */
622             if (APR_BUCKET_IS_EOS(bkt)) {
623                 return APR_EGENERAL;
624             }
625
626             if (APR_BUCKET_IS_FLUSH(bkt)) {
627                 apr_bucket *tmp_heap;
628                 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
629                 if (zRC != Z_OK) {
630                     return APR_EGENERAL;
631                 }
632
633                 ctx->stream.next_out = ctx->buffer;
634                 len = c->bufferSize - ctx->stream.avail_out;
635
636                 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
637                 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
638                                                  NULL, f->c->bucket_alloc);
639                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
640                 ctx->stream.avail_out = c->bufferSize;
641
642                 /* Move everything to the returning brigade. */
643                 APR_BUCKET_REMOVE(bkt);
644                 APR_BRIGADE_CONCAT(bb, ctx->bb);
645                 break;
646             }
647
648             /* read */
649             apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
650
651             /* pass through zlib inflate. */
652             ctx->stream.next_in = (unsigned char *)data;
653             ctx->stream.avail_in = len;
654
655             zRC = Z_OK;
656
657             while (ctx->stream.avail_in != 0) {
658                 if (ctx->stream.avail_out == 0) {
659                     apr_bucket *tmp_heap;
660                     ctx->stream.next_out = ctx->buffer;
661                     len = c->bufferSize - ctx->stream.avail_out;
662
663                     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
664                     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
665                                                       NULL, f->c->bucket_alloc);
666                     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
667                     ctx->stream.avail_out = c->bufferSize;
668                 }
669
670                 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
671
672                 if (zRC == Z_STREAM_END) {
673                     break;
674                 }
675
676                 if (zRC != Z_OK) {
677                     return APR_EGENERAL;
678                 }
679             }
680             if (zRC == Z_STREAM_END) {
681                 apr_bucket *tmp_heap, *eos;
682
683                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
684                               "Zlib: Inflated %ld to %ld : URL %s",
685                               ctx->stream.total_in, ctx->stream.total_out,
686                               r->uri);
687
688                 len = c->bufferSize - ctx->stream.avail_out;
689
690                 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
691                 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
692                                                   NULL, f->c->bucket_alloc);
693                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
694                 ctx->stream.avail_out = c->bufferSize;
695
696                 /* Is the remaining 8 bytes already in the avail stream? */
697                 if (ctx->stream.avail_in >= 8) {
698                     unsigned long compCRC, compLen;
699                     compCRC = getLong(ctx->stream.next_in);
700                     if (ctx->crc != compCRC) {
701                         return APR_EGENERAL;
702                     }
703                     ctx->stream.next_in += 4;
704                     compLen = getLong(ctx->stream.next_in);
705                     if (ctx->stream.total_out != compLen) {
706                         return APR_EGENERAL;
707                     }
708                 }
709                 else {
710                     /* FIXME: We need to grab the 8 verification bytes
711                      * from the wire! */
712                     return APR_EGENERAL;
713                 }
714
715                 inflateEnd(&ctx->stream);
716     
717                 eos = apr_bucket_eos_create(f->c->bucket_alloc);
718                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos); 
719                 break;
720             }
721         }
722         apr_brigade_cleanup(ctx->bb);
723     }
724
725     if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
726         apr_bucket_brigade *newbb;
727
728         /* May return APR_INCOMPLETE which is fine by us. */
729         apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
730
731         newbb = apr_brigade_split(ctx->proc_bb, bkt);
732         APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
733         APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
734     }
735
736     return APR_SUCCESS;
737 }
738
739 static void register_hooks(apr_pool_t *p)
740 {
741     ap_register_output_filter(deflateFilterName, deflate_out_filter,
742                               AP_FTYPE_CONTENT_SET);
743     ap_register_input_filter(deflateFilterName, deflate_in_filter,
744                               AP_FTYPE_CONTENT_SET);
745 }
746
747 static const command_rec deflate_filter_cmds[] = {
748     AP_INIT_TAKE1("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
749                   "Set a note to report on compression ratio"),
750     AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
751                   RSRC_CONF, "Set the Deflate window size (1-15)"),
752     AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
753                   "Set the Deflate Buffer Size"),
754     AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
755                   "Set the Deflate Memory Level (1-9)"),
756     {NULL}
757 };
758
759 module AP_MODULE_DECLARE_DATA deflate_module = {
760     STANDARD20_MODULE_STUFF,
761     NULL,                         /* dir config creater */
762     NULL,                         /* dir merger --- default is to override */
763     create_deflate_server_config, /* server config */
764     NULL,                         /* merge server config */
765     deflate_filter_cmds,          /* command table */
766     register_hooks                /* register hooks */
767 };