]> granicus.if.org Git - apache/blob - modules/filters/mod_deflate.c
for the time being, rename as ProxyProtocolFilter to avoid
[apache] / modules / filters / mod_deflate.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * mod_deflate.c: Perform deflate content-encoding on the fly
19  *
20  * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew
21  */
22
23 /*
24  * Portions of this software are based upon zlib code by Jean-loup Gailly
25  * (zlib functions gz_open and gzwrite, check_header)
26  */
27
28 /* zlib flags */
29 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
30 #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
31 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
32 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
33 #define COMMENT      0x10 /* bit 4 set: file comment present */
34 #define RESERVED     0xE0 /* bits 5..7: reserved */
35
36
37 #include "httpd.h"
38 #include "http_config.h"
39 #include "http_log.h"
40 #include "http_core.h"
41 #include "apr_lib.h"
42 #include "apr_strings.h"
43 #include "apr_general.h"
44 #include "util_filter.h"
45 #include "apr_buckets.h"
46 #include "http_request.h"
47 #define APR_WANT_STRFUNC
48 #include "apr_want.h"
49 #include "mod_ssl.h"
50
51 #include "zlib.h"
52
53 static const char deflateFilterName[] = "DEFLATE";
54 module AP_MODULE_DECLARE_DATA deflate_module;
55
56 #define AP_DEFLATE_ETAG_ADDSUFFIX 0
57 #define AP_DEFLATE_ETAG_NOCHANGE  1
58 #define AP_DEFLATE_ETAG_REMOVE    2
59
60 #define AP_INFLATE_RATIO_LIMIT 200
61 #define AP_INFLATE_RATIO_BURST 3
62
63 typedef struct deflate_filter_config_t
64 {
65     int windowSize;
66     int memlevel;
67     int compressionlevel;
68     int bufferSize;
69     const char *note_ratio_name;
70     const char *note_input_name;
71     const char *note_output_name;
72     int etag_opt;
73 } deflate_filter_config;
74
75 typedef struct deflate_dirconf_t {
76     apr_off_t inflate_limit;
77     int ratio_limit,
78         ratio_burst;
79 } deflate_dirconf_t;
80
81 /* RFC 1952 Section 2.3 defines the gzip header:
82  *
83  * +---+---+---+---+---+---+---+---+---+---+
84  * |ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
85  * +---+---+---+---+---+---+---+---+---+---+
86  */
87 static const char gzip_header[10] =
88 { '\037', '\213', Z_DEFLATED, 0,
89   0, 0, 0, 0, /* mtime */
90   0, 0x03 /* Unix OS_CODE */
91 };
92
93 /* magic header */
94 static const char deflate_magic[2] = { '\037', '\213' };
95
96 /* windowsize is negative to suppress Zlib header */
97 #define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
98 #define DEFAULT_WINDOWSIZE -15
99 #define DEFAULT_MEMLEVEL 9
100 #define DEFAULT_BUFFERSIZE 8096
101
102 static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *mod_deflate_ssl_var = NULL;
103
104 /* Check whether a request is gzipped, so we can un-gzip it.
105  * If a request has multiple encodings, we need the gzip
106  * to be the outermost non-identity encoding.
107  */
108 static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
109 {
110     int found = 0;
111     apr_table_t *hdrs = hdrs1;
112     const char *encoding = apr_table_get(hdrs, "Content-Encoding");
113
114     if (!encoding && (hdrs2 != NULL)) {
115         /* the output filter has two tables and a content_encoding to check */
116         encoding = apr_table_get(hdrs2, "Content-Encoding");
117         hdrs = hdrs2;
118         if (!encoding) {
119             encoding = r->content_encoding;
120             hdrs = NULL;
121         }
122     }
123     if (encoding && *encoding) {
124
125         /* check the usual/simple case first */
126         if (!ap_cstr_casecmp(encoding, "gzip")
127             || !ap_cstr_casecmp(encoding, "x-gzip")) {
128             found = 1;
129             if (hdrs) {
130                 apr_table_unset(hdrs, "Content-Encoding");
131             }
132             else {
133                 r->content_encoding = NULL;
134             }
135         }
136         else if (ap_strchr_c(encoding, ',') != NULL) {
137             /* If the outermost encoding isn't gzip, there's nothing
138              * we can do.  So only check the last non-identity token
139              */
140             char *new_encoding = apr_pstrdup(r->pool, encoding);
141             char *ptr;
142             for(;;) {
143                 char *token = ap_strrchr(new_encoding, ',');
144                 if (!token) {        /* gzip:identity or other:identity */
145                     if (!ap_cstr_casecmp(new_encoding, "gzip")
146                         || !ap_cstr_casecmp(new_encoding, "x-gzip")) {
147                         found = 1;
148                         if (hdrs) {
149                             apr_table_unset(hdrs, "Content-Encoding");
150                         }
151                         else {
152                             r->content_encoding = NULL;
153                         }
154                     }
155                     break; /* seen all tokens */
156                 }
157                 for (ptr=token+1; apr_isspace(*ptr); ++ptr);
158                 if (!ap_cstr_casecmp(ptr, "gzip")
159                     || !ap_cstr_casecmp(ptr, "x-gzip")) {
160                     *token = '\0';
161                     if (hdrs) {
162                         apr_table_setn(hdrs, "Content-Encoding", new_encoding);
163                     }
164                     else {
165                         r->content_encoding = new_encoding;
166                     }
167                     found = 1;
168                 }
169                 else if (!ptr[0] || !ap_cstr_casecmp(ptr, "identity")) {
170                     *token = '\0';
171                     continue; /* strip the token and find the next one */
172                 }
173                 break; /* found a non-identity token */
174             }
175         }
176     }
177     /*
178      * If we have dealt with the headers above but content_encoding was set
179      * before sync it with the new value in the hdrs table as
180      * r->content_encoding takes precedence later on in the http_header_filter
181      * and hence would destroy what we have just set in the hdrs table.
182      */
183     if (hdrs && r->content_encoding) {
184         r->content_encoding = apr_table_get(hdrs, "Content-Encoding");
185     }
186     return found;
187 }
188
189 /* Outputs a long in LSB order to the given file
190  * only the bottom 4 bits are required for the deflate file format.
191  */
192 static void putLong(unsigned char *string, unsigned long x)
193 {
194     string[0] = (unsigned char)(x & 0xff);
195     string[1] = (unsigned char)((x & 0xff00) >> 8);
196     string[2] = (unsigned char)((x & 0xff0000) >> 16);
197     string[3] = (unsigned char)((x & 0xff000000) >> 24);
198 }
199
200 /* Inputs a string and returns a long.
201  */
202 static unsigned long getLong(unsigned char *string)
203 {
204     return ((unsigned long)string[0])
205           | (((unsigned long)string[1]) << 8)
206           | (((unsigned long)string[2]) << 16)
207           | (((unsigned long)string[3]) << 24);
208 }
209
210 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
211 {
212     deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
213
214     c->memlevel   = DEFAULT_MEMLEVEL;
215     c->windowSize = DEFAULT_WINDOWSIZE;
216     c->bufferSize = DEFAULT_BUFFERSIZE;
217     c->compressionlevel = DEFAULT_COMPRESSION;
218     c->etag_opt = AP_DEFLATE_ETAG_ADDSUFFIX;
219
220     return c;
221 }
222
223 static void *create_deflate_dirconf(apr_pool_t *p, char *dummy)
224 {
225     deflate_dirconf_t *dc = apr_pcalloc(p, sizeof(*dc));
226     dc->ratio_limit = AP_INFLATE_RATIO_LIMIT;
227     dc->ratio_burst = AP_INFLATE_RATIO_BURST;
228     return dc;
229 }
230
231 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
232                                            const char *arg)
233 {
234     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
235                                                     &deflate_module);
236     int i;
237
238     i = atoi(arg);
239
240     if (i < 1 || i > 15)
241         return "DeflateWindowSize must be between 1 and 15";
242
243     c->windowSize = i * -1;
244
245     return NULL;
246 }
247
248 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
249                                            const char *arg)
250 {
251     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
252                                                     &deflate_module);
253     int n = atoi(arg);
254
255     if (n <= 0) {
256         return "DeflateBufferSize should be positive";
257     }
258
259     c->bufferSize = n;
260
261     return NULL;
262 }
263 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
264                                     const char *arg1, const char *arg2)
265 {
266     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
267                                                     &deflate_module);
268
269     if (arg2 == NULL) {
270         c->note_ratio_name = arg1;
271     }
272     else if (!strcasecmp(arg1, "ratio")) {
273         c->note_ratio_name = arg2;
274     }
275     else if (!strcasecmp(arg1, "input")) {
276         c->note_input_name = arg2;
277     }
278     else if (!strcasecmp(arg1, "output")) {
279         c->note_output_name = arg2;
280     }
281     else {
282         return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
283     }
284
285     return NULL;
286 }
287
288 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
289                                         const char *arg)
290 {
291     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
292                                                     &deflate_module);
293     int i;
294
295     i = atoi(arg);
296
297     if (i < 1 || i > 9)
298         return "DeflateMemLevel must be between 1 and 9";
299
300     c->memlevel = i;
301
302     return NULL;
303 }
304
305 static const char *deflate_set_etag(cmd_parms *cmd, void *dummy,
306                                         const char *arg)
307 {
308     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
309                                                     &deflate_module);
310
311     if (!strcasecmp(arg, "NoChange")) { 
312       c->etag_opt = AP_DEFLATE_ETAG_NOCHANGE;
313     }
314     else if (!strcasecmp(arg, "AddSuffix")) { 
315       c->etag_opt = AP_DEFLATE_ETAG_ADDSUFFIX;
316     }
317     else if (!strcasecmp(arg, "Remove")) { 
318       c->etag_opt = AP_DEFLATE_ETAG_REMOVE;
319     }
320     else { 
321         return "DeflateAlterETAG accepts only 'NoChange', 'AddSuffix', and 'Remove'";
322     }
323
324     return NULL;
325 }
326
327
328 static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
329                                         const char *arg)
330 {
331     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
332                                                     &deflate_module);
333     int i;
334
335     i = atoi(arg);
336
337     if (i < 1 || i > 9)
338         return "Compression Level must be between 1 and 9";
339
340     c->compressionlevel = i;
341
342     return NULL;
343 }
344
345
346 static const char *deflate_set_inflate_limit(cmd_parms *cmd, void *dirconf,
347                                       const char *arg)
348 {
349     deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
350     char *errp;
351
352     if (APR_SUCCESS != apr_strtoff(&dc->inflate_limit, arg, &errp, 10)) {
353         return "DeflateInflateLimitRequestBody is not parsable.";
354     }
355     if (*errp || dc->inflate_limit < 0) {
356         return "DeflateInflateLimitRequestBody requires a non-negative integer.";
357     }
358
359     return NULL;
360 }
361
362 static const char *deflate_set_inflate_ratio_limit(cmd_parms *cmd,
363                                                    void *dirconf,
364                                                    const char *arg)
365 {
366     deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
367     int i;
368
369     i = atoi(arg);
370     if (i <= 0)
371         return "DeflateInflateRatioLimit must be positive";
372
373     dc->ratio_limit = i;
374
375     return NULL;
376 }
377
378 static const char *deflate_set_inflate_ratio_burst(cmd_parms *cmd,
379                                                    void *dirconf,
380                                                    const char *arg)
381 {
382     deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
383     int i;
384
385     i = atoi(arg);
386     if (i <= 0)
387         return "DeflateInflateRatioBurst must be positive";
388
389     dc->ratio_burst = i;
390
391     return NULL;
392 }
393
394 typedef struct deflate_ctx_t
395 {
396     z_stream stream;
397     unsigned char *buffer;
398     unsigned long crc;
399     apr_bucket_brigade *bb, *proc_bb;
400     int (*libz_end_func)(z_streamp);
401     unsigned char *validation_buffer;
402     apr_size_t validation_buffer_length;
403     char header[10]; /* sizeof(gzip_header) */
404     apr_size_t header_len;
405     int zlib_flags;
406     int ratio_hits;
407     apr_off_t inflate_total;
408     unsigned int consume_pos,
409                  consume_len;
410     unsigned int filter_init:1;
411     unsigned int done:1;
412 } deflate_ctx;
413
414 /* Number of validation bytes (CRC and length) after the compressed data */
415 #define VALIDATION_SIZE 8
416 /* Do not update ctx->crc, see comment in flush_libz_buffer */
417 #define NO_UPDATE_CRC 0
418 /* Do update ctx->crc, see comment in flush_libz_buffer */
419 #define UPDATE_CRC 1
420
421 static void consume_buffer(deflate_ctx *ctx, deflate_filter_config *c,
422                            int len, int crc, apr_bucket_brigade *bb)
423 {
424     apr_bucket *b;
425
426     /*
427      * Do we need to update ctx->crc? Usually this is the case for
428      * inflate action where we need to do a crc on the output, whereas
429      * in the deflate case we need to do a crc on the input
430      */
431     if (crc) {
432         ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
433     }
434
435     b = apr_bucket_heap_create((char *)ctx->buffer, len, NULL,
436                                bb->bucket_alloc);
437     APR_BRIGADE_INSERT_TAIL(bb, b);
438
439     ctx->stream.next_out = ctx->buffer;
440     ctx->stream.avail_out = c->bufferSize;
441 }
442
443 static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
444                              int (*libz_func)(z_streamp, int), int flush,
445                              int crc)
446 {
447     int zRC = Z_OK;
448     int done = 0;
449     int deflate_len;
450
451     for (;;) {
452          deflate_len = c->bufferSize - ctx->stream.avail_out;
453          if (deflate_len > 0) {
454              consume_buffer(ctx, c, deflate_len, crc, ctx->bb);
455          }
456
457          if (done)
458              break;
459
460          zRC = libz_func(&ctx->stream, flush);
461
462          /*
463           * We can ignore Z_BUF_ERROR because:
464           * When we call libz_func we can assume that
465           *
466           * - avail_in is zero (due to the surrounding code that calls
467           *   flush_libz_buffer)
468           * - avail_out is non zero due to our actions some lines above
469           *
470           * So the only reason for Z_BUF_ERROR is that the internal libz
471           * buffers are now empty and thus we called libz_func one time
472           * too often. This does not hurt. It simply says that we are done.
473           */
474          if (zRC == Z_BUF_ERROR) {
475              zRC = Z_OK;
476              break;
477          }
478
479          done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
480
481          if (zRC != Z_OK && zRC != Z_STREAM_END)
482              break;
483     }
484     return zRC;
485 }
486
487 static apr_status_t deflate_ctx_cleanup(void *data)
488 {
489     deflate_ctx *ctx = (deflate_ctx *)data;
490
491     if (ctx)
492         ctx->libz_end_func(&ctx->stream);
493     return APR_SUCCESS;
494 }
495
496 /* ETag must be unique among the possible representations, so a change
497  * to content-encoding requires a corresponding change to the ETag.
498  * This routine appends -transform (e.g., -gzip) to the entity-tag
499  * value inside the double-quotes if an ETag has already been set
500  * and its value already contains double-quotes. PR 39727
501  */
502 static void deflate_check_etag(request_rec *r, const char *transform, int etag_opt)
503 {
504     const char *etag = apr_table_get(r->headers_out, "ETag");
505     apr_size_t etaglen;
506
507     if (etag_opt == AP_DEFLATE_ETAG_REMOVE) { 
508         apr_table_unset(r->headers_out, "ETag");
509         return;
510     }
511
512     if ((etag && ((etaglen = strlen(etag)) > 2))) {
513         if (etag[etaglen - 1] == '"') {
514             apr_size_t transformlen = strlen(transform);
515             char *newtag = apr_palloc(r->pool, etaglen + transformlen + 2);
516             char *d = newtag;
517             char *e = d + etaglen - 1;
518             const char *s = etag;
519
520             for (; d < e; ++d, ++s) {
521                 *d = *s;          /* copy etag to newtag up to last quote */
522             }
523             *d++ = '-';           /* append dash to newtag */
524             s = transform;
525             e = d + transformlen;
526             for (; d < e; ++d, ++s) {
527                 *d = *s;          /* copy transform to newtag */
528             }
529             *d++ = '"';           /* append quote to newtag */
530             *d   = '\0';          /* null terminate newtag */
531
532             apr_table_setn(r->headers_out, "ETag", newtag);
533         }
534     }
535 }
536
537 /* Check whether the (inflate) ratio exceeds the configured limit/burst. */
538 static int check_ratio(request_rec *r, deflate_ctx *ctx,
539                        const deflate_dirconf_t *dc)
540 {
541     if (ctx->stream.total_in) {
542         int ratio = ctx->stream.total_out / ctx->stream.total_in;
543         if (ratio < dc->ratio_limit) {
544             ctx->ratio_hits = 0;
545         }
546         else if (++ctx->ratio_hits > dc->ratio_burst) {
547             return 0;
548         }
549     }
550     return 1;
551 }
552
553 static int have_ssl_compression(request_rec *r)
554 {
555     const char *comp;
556     if (mod_deflate_ssl_var == NULL)
557         return 0;
558     comp = mod_deflate_ssl_var(r->pool, r->server, r->connection, r,
559                                "SSL_COMPRESS_METHOD");
560     if (comp == NULL || *comp == '\0' || strcmp(comp, "NULL") == 0)
561         return 0;
562     return 1;
563 }
564
565 static apr_status_t deflate_out_filter(ap_filter_t *f,
566                                        apr_bucket_brigade *bb)
567 {
568     apr_bucket *e;
569     request_rec *r = f->r;
570     deflate_ctx *ctx = f->ctx;
571     int zRC;
572     apr_status_t rv;
573     apr_size_t len = 0, blen;
574     const char *data;
575     deflate_filter_config *c;
576
577     /* Do nothing if asked to filter nothing. */
578     if (APR_BRIGADE_EMPTY(bb)) {
579         return APR_SUCCESS;
580     }
581
582     c = ap_get_module_config(r->server->module_config,
583                              &deflate_module);
584
585     /* If we don't have a context, we need to ensure that it is okay to send
586      * the deflated content.  If we have a context, that means we've done
587      * this before and we liked it.
588      * This could be not so nice if we always fail.  But, if we succeed,
589      * we're in better shape.
590      */
591     if (!ctx) {
592         char *token;
593         const char *encoding;
594
595         if (have_ssl_compression(r)) {
596             ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
597                           "Compression enabled at SSL level; not compressing "
598                           "at HTTP level.");
599             ap_remove_output_filter(f);
600             return ap_pass_brigade(f->next, bb);
601         }
602
603         /* We have checked above that bb is not empty */
604         e = APR_BRIGADE_LAST(bb);
605         if (APR_BUCKET_IS_EOS(e)) {
606             /*
607              * If we already know the size of the response, we can skip
608              * compression on responses smaller than the compression overhead.
609              * However, if we compress, we must initialize deflate_out before
610              * calling ap_pass_brigade() for the first time.  Otherwise the
611              * headers will be sent to the client without
612              * "Content-Encoding: gzip".
613              */
614             e = APR_BRIGADE_FIRST(bb);
615             while (1) {
616                 apr_status_t rc;
617                 if (APR_BUCKET_IS_EOS(e)) {
618                     ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
619                                   "Not compressing very small response of %"
620                                   APR_SIZE_T_FMT " bytes", len);
621                     ap_remove_output_filter(f);
622                     return ap_pass_brigade(f->next, bb);
623                 }
624                 if (APR_BUCKET_IS_METADATA(e)) {
625                     e = APR_BUCKET_NEXT(e);
626                     continue;
627                 }
628
629                 rc = apr_bucket_read(e, &data, &blen, APR_BLOCK_READ);
630                 if (rc != APR_SUCCESS)
631                     return rc;
632                 len += blen;
633                 /* 50 is for Content-Encoding and Vary headers and ETag suffix */
634                 if (len > sizeof(gzip_header) + VALIDATION_SIZE + 50)
635                     break;
636
637                 e = APR_BUCKET_NEXT(e);
638             }
639         }
640
641         ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
642
643         /*
644          * Only work on main request, not subrequests,
645          * that are not a 204 response with no content
646          * and are not tagged with the no-gzip env variable
647          * and not a partial response to a Range request.
648          */
649         if ((r->main != NULL) || (r->status == HTTP_NO_CONTENT) ||
650             apr_table_get(r->subprocess_env, "no-gzip") ||
651             apr_table_get(r->headers_out, "Content-Range")
652            ) {
653             if (APLOG_R_IS_LEVEL(r, APLOG_TRACE1)) {
654                 const char *reason =
655                     (r->main != NULL)                           ? "subrequest" :
656                     (r->status == HTTP_NO_CONTENT)              ? "no content" :
657                     apr_table_get(r->subprocess_env, "no-gzip") ? "no-gzip" :
658                     "content-range";
659                 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
660                               "Not compressing (%s)", reason);
661             }
662             ap_remove_output_filter(f);
663             return ap_pass_brigade(f->next, bb);
664         }
665
666         /* Some browsers might have problems with content types
667          * other than text/html, so set gzip-only-text/html
668          * (with browsermatch) for them
669          */
670         if (r->content_type == NULL
671              || strncmp(r->content_type, "text/html", 9)) {
672             const char *env_value = apr_table_get(r->subprocess_env,
673                                                   "gzip-only-text/html");
674             if ( env_value && (strcmp(env_value,"1") == 0) ) {
675                 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
676                               "Not compressing, (gzip-only-text/html)");
677                 ap_remove_output_filter(f);
678                 return ap_pass_brigade(f->next, bb);
679             }
680         }
681
682         /* Let's see what our current Content-Encoding is.
683          * If it's already encoded, don't compress again.
684          * (We could, but let's not.)
685          */
686         encoding = apr_table_get(r->headers_out, "Content-Encoding");
687         if (encoding) {
688             const char *err_enc;
689
690             err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
691             if (err_enc) {
692                 encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
693             }
694         }
695         else {
696             encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
697         }
698
699         if (r->content_encoding) {
700             encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
701                                               r->content_encoding, NULL)
702                                 : r->content_encoding;
703         }
704
705         if (encoding) {
706             const char *tmp = encoding;
707
708             token = ap_get_token(r->pool, &tmp, 0);
709             while (token && *token) {
710                 /* stolen from mod_negotiation: */
711                 if (strcmp(token, "identity") && strcmp(token, "7bit") &&
712                     strcmp(token, "8bit") && strcmp(token, "binary")) {
713                     ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
714                                   "Not compressing (content-encoding already "
715                                   " set: %s)", token);
716                     ap_remove_output_filter(f);
717                     return ap_pass_brigade(f->next, bb);
718                 }
719
720                 /* Otherwise, skip token */
721                 if (*tmp) {
722                     ++tmp;
723                 }
724                 token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
725             }
726         }
727
728         /* Even if we don't accept this request based on it not having
729          * the Accept-Encoding, we need to note that we were looking
730          * for this header and downstream proxies should be aware of that.
731          */
732         apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
733
734         /* force-gzip will just force it out regardless if the browser
735          * can actually do anything with it.
736          */
737         if (!apr_table_get(r->subprocess_env, "force-gzip")) {
738             const char *accepts;
739             /* if they don't have the line, then they can't play */
740             accepts = apr_table_get(r->headers_in, "Accept-Encoding");
741             if (accepts == NULL) {
742                 ap_remove_output_filter(f);
743                 return ap_pass_brigade(f->next, bb);
744             }
745
746             token = ap_get_token(r->pool, &accepts, 0);
747             while (token && token[0] && ap_cstr_casecmp(token, "gzip")) {
748                 /* skip parameters, XXX: ;q=foo evaluation? */
749                 while (*accepts == ';') {
750                     ++accepts;
751                     ap_get_token(r->pool, &accepts, 1);
752                 }
753
754                 /* retrieve next token */
755                 if (*accepts == ',') {
756                     ++accepts;
757                 }
758                 token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
759             }
760
761             /* No acceptable token found. */
762             if (token == NULL || token[0] == '\0') {
763                 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
764                               "Not compressing (no Accept-Encoding: gzip)");
765                 ap_remove_output_filter(f);
766                 return ap_pass_brigade(f->next, bb);
767             }
768         }
769         else {
770             ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
771                           "Forcing compression (force-gzip set)");
772         }
773
774         /* At this point we have decided to filter the content. Let's try to
775          * to initialize zlib (except for 304 responses, where we will only
776          * send out the headers).
777          */
778
779         if (r->status != HTTP_NOT_MODIFIED) {
780             ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
781             ctx->buffer = apr_palloc(r->pool, c->bufferSize);
782             ctx->libz_end_func = deflateEnd;
783
784             zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
785                                c->windowSize, c->memlevel,
786                                Z_DEFAULT_STRATEGY);
787
788             if (zRC != Z_OK) {
789                 deflateEnd(&ctx->stream);
790                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01383)
791                               "unable to init Zlib: "
792                               "deflateInit2 returned %d: URL %s",
793                               zRC, r->uri);
794                 /*
795                  * Remove ourselves as it does not make sense to return:
796                  * We are not able to init libz and pass data down the chain
797                  * uncompressed.
798                  */
799                 ap_remove_output_filter(f);
800                 return ap_pass_brigade(f->next, bb);
801             }
802             /*
803              * Register a cleanup function to ensure that we cleanup the internal
804              * libz resources.
805              */
806             apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
807                                       apr_pool_cleanup_null);
808
809             /* Set the filter init flag so subsequent invocations know we are
810              * active.
811              */
812             ctx->filter_init = 1;
813         }
814
815         /*
816          * Zlib initialization worked, so we can now change the important
817          * content metadata before sending the response out.
818          */
819
820         /* If the entire Content-Encoding is "identity", we can replace it. */
821         if (!encoding || !ap_cstr_casecmp(encoding, "identity")) {
822             apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
823         }
824         else {
825             apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
826         }
827         /* Fix r->content_encoding if it was set before */
828         if (r->content_encoding) {
829             r->content_encoding = apr_table_get(r->headers_out,
830                                                 "Content-Encoding");
831         }
832         apr_table_unset(r->headers_out, "Content-Length");
833         apr_table_unset(r->headers_out, "Content-MD5");
834         if (c->etag_opt != AP_DEFLATE_ETAG_NOCHANGE) {  
835             deflate_check_etag(r, "gzip", c->etag_opt);
836         }
837
838         /* For a 304 response, only change the headers */
839         if (r->status == HTTP_NOT_MODIFIED) {
840             ap_remove_output_filter(f);
841             return ap_pass_brigade(f->next, bb);
842         }
843
844         /* add immortal gzip header */
845         e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
846                                        f->c->bucket_alloc);
847         APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
848
849         /* initialize deflate output buffer */
850         ctx->stream.next_out = ctx->buffer;
851         ctx->stream.avail_out = c->bufferSize;
852     } else if (!ctx->filter_init) {
853         /* Hmm.  We've run through the filter init before as we have a ctx,
854          * but we never initialized.  We probably have a dangling ref.  Bail.
855          */
856         return ap_pass_brigade(f->next, bb);
857     }
858
859     while (!APR_BRIGADE_EMPTY(bb))
860     {
861         apr_bucket *b;
862
863         /*
864          * Optimization: If we are a HEAD request and bytes_sent is not zero
865          * it means that we have passed the content-length filter once and
866          * have more data to sent. This means that the content-length filter
867          * could not determine our content-length for the response to the
868          * HEAD request anyway (the associated GET request would deliver the
869          * body in chunked encoding) and we can stop compressing.
870          */
871         if (r->header_only && r->bytes_sent) {
872             ap_remove_output_filter(f);
873             return ap_pass_brigade(f->next, bb);
874         }
875
876         e = APR_BRIGADE_FIRST(bb);
877
878         if (APR_BUCKET_IS_EOS(e)) {
879             char *buf;
880
881             ctx->stream.avail_in = 0; /* should be zero already anyway */
882             /* flush the remaining data from the zlib buffers */
883             flush_libz_buffer(ctx, c, deflate, Z_FINISH, NO_UPDATE_CRC);
884
885             buf = apr_palloc(r->pool, VALIDATION_SIZE);
886             putLong((unsigned char *)&buf[0], ctx->crc);
887             putLong((unsigned char *)&buf[4], ctx->stream.total_in);
888
889             b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
890                                        f->c->bucket_alloc);
891             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
892             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01384)
893                           "Zlib: Compressed %ld to %ld : URL %s",
894                           ctx->stream.total_in, ctx->stream.total_out, r->uri);
895
896             /* leave notes for logging */
897             if (c->note_input_name) {
898                 apr_table_setn(r->notes, c->note_input_name,
899                                (ctx->stream.total_in > 0)
900                                 ? apr_off_t_toa(r->pool,
901                                                 ctx->stream.total_in)
902                                 : "-");
903             }
904
905             if (c->note_output_name) {
906                 apr_table_setn(r->notes, c->note_output_name,
907                                (ctx->stream.total_in > 0)
908                                 ? apr_off_t_toa(r->pool,
909                                                 ctx->stream.total_out)
910                                 : "-");
911             }
912
913             if (c->note_ratio_name) {
914                 apr_table_setn(r->notes, c->note_ratio_name,
915                                (ctx->stream.total_in > 0)
916                                 ? apr_itoa(r->pool,
917                                            (int)(ctx->stream.total_out
918                                                  * 100
919                                                  / ctx->stream.total_in))
920                                 : "-");
921             }
922
923             deflateEnd(&ctx->stream);
924             /* No need for cleanup any longer */
925             apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
926
927             /* Remove EOS from the old list, and insert into the new. */
928             APR_BUCKET_REMOVE(e);
929             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
930
931             /* Okay, we've seen the EOS.
932              * Time to pass it along down the chain.
933              */
934             rv = ap_pass_brigade(f->next, ctx->bb);
935             apr_brigade_cleanup(ctx->bb);
936             return rv;
937         }
938
939         if (APR_BUCKET_IS_FLUSH(e)) {
940             /* flush the remaining data from the zlib buffers */
941             zRC = flush_libz_buffer(ctx, c, deflate, Z_SYNC_FLUSH,
942                                     NO_UPDATE_CRC);
943             if (zRC != Z_OK) {
944                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01385)
945                               "Zlib error %d flushing zlib output buffer (%s)",
946                               zRC, ctx->stream.msg);
947                 return APR_EGENERAL;
948             }
949
950             /* Remove flush bucket from old brigade anf insert into the new. */
951             APR_BUCKET_REMOVE(e);
952             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
953             rv = ap_pass_brigade(f->next, ctx->bb);
954             apr_brigade_cleanup(ctx->bb);
955             if (rv != APR_SUCCESS) {
956                 return rv;
957             }
958             continue;
959         }
960
961         if (APR_BUCKET_IS_METADATA(e)) {
962             /*
963              * Remove meta data bucket from old brigade and insert into the
964              * new.
965              */
966             APR_BUCKET_REMOVE(e);
967             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
968             continue;
969         }
970
971         /* read */
972         apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
973         if (!len) {
974             apr_bucket_delete(e);
975             continue;
976         }
977         if (len > APR_INT32_MAX) {
978             apr_bucket_split(e, APR_INT32_MAX);
979             apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
980         }
981
982         /* This crc32 function is from zlib. */
983         ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
984
985         /* write */
986         ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
987                                                       * but we'll just have to
988                                                       * trust zlib */
989         ctx->stream.avail_in = (int)len;
990
991         while (ctx->stream.avail_in != 0) {
992             if (ctx->stream.avail_out == 0) {
993                 consume_buffer(ctx, c, c->bufferSize, NO_UPDATE_CRC, ctx->bb);
994
995                 /* Send what we have right now to the next filter. */
996                 rv = ap_pass_brigade(f->next, ctx->bb);
997                 apr_brigade_cleanup(ctx->bb);
998                 if (rv != APR_SUCCESS) {
999                     return rv;
1000                 }
1001             }
1002
1003             zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
1004
1005             if (zRC != Z_OK) {
1006                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01386)
1007                               "Zlib error %d deflating data (%s)", zRC,
1008                               ctx->stream.msg);
1009                 return APR_EGENERAL;
1010             }
1011         }
1012
1013         apr_bucket_delete(e);
1014     }
1015
1016     return APR_SUCCESS;
1017 }
1018
1019 static apr_status_t consume_zlib_flags(deflate_ctx *ctx,
1020                                        const char **data, apr_size_t *len)
1021 {
1022     if ((ctx->zlib_flags & EXTRA_FIELD)) {
1023         /* Consume 2 bytes length prefixed data. */
1024         if (ctx->consume_pos == 0) {
1025             if (!*len) {
1026                 return APR_INCOMPLETE;
1027             }
1028             ctx->consume_len = (unsigned int)**data;
1029             ctx->consume_pos++;
1030             ++*data;
1031             --*len;
1032         }
1033         if (ctx->consume_pos == 1) {
1034             if (!*len) {
1035                 return APR_INCOMPLETE;
1036             }
1037             ctx->consume_len += ((unsigned int)**data) << 8;
1038             ctx->consume_pos++;
1039             ++*data;
1040             --*len;
1041         }
1042         if (*len < ctx->consume_len) {
1043             ctx->consume_len -= *len;
1044             *len = 0;
1045             return APR_INCOMPLETE;
1046         }
1047         *data += ctx->consume_len;
1048         *len -= ctx->consume_len;
1049
1050         ctx->consume_len = ctx->consume_pos = 0;
1051         ctx->zlib_flags &= ~EXTRA_FIELD;
1052     }
1053
1054     if ((ctx->zlib_flags & ORIG_NAME)) {
1055         /* Consume nul terminated string. */
1056         while (*len && **data) {
1057             ++*data;
1058             --*len;
1059         }
1060         if (!*len) {
1061             return APR_INCOMPLETE;
1062         }
1063         /* .. and nul. */
1064         ++*data;
1065         --*len;
1066
1067         ctx->zlib_flags &= ~ORIG_NAME;
1068     }
1069
1070     if ((ctx->zlib_flags & COMMENT)) {
1071         /* Consume nul terminated string. */
1072         while (*len && **data) {
1073             ++*data;
1074             --*len;
1075         }
1076         if (!*len) {
1077             return APR_INCOMPLETE;
1078         }
1079         /* .. and nul. */
1080         ++*data;
1081         --*len;
1082
1083         ctx->zlib_flags &= ~COMMENT;
1084     }
1085
1086     if ((ctx->zlib_flags & HEAD_CRC)) {
1087         /* Consume CRC16 (2 octets). */
1088         if (ctx->consume_pos == 0) {
1089             if (!*len) {
1090                 return APR_INCOMPLETE;
1091             }
1092             ctx->consume_pos++;
1093             ++*data;
1094             --*len;
1095         }
1096         if (!*len) {
1097             return APR_INCOMPLETE;
1098         }
1099         ++*data;
1100         --*len;
1101         
1102         ctx->consume_pos = 0;
1103         ctx->zlib_flags &= ~HEAD_CRC;
1104     }
1105
1106     return APR_SUCCESS;
1107 }
1108
1109 /* This is the deflate input filter (inflates).  */
1110 static apr_status_t deflate_in_filter(ap_filter_t *f,
1111                                       apr_bucket_brigade *bb,
1112                                       ap_input_mode_t mode,
1113                                       apr_read_type_e block,
1114                                       apr_off_t readbytes)
1115 {
1116     apr_bucket *bkt;
1117     request_rec *r = f->r;
1118     deflate_ctx *ctx = f->ctx;
1119     int zRC;
1120     apr_status_t rv;
1121     deflate_filter_config *c;
1122     deflate_dirconf_t *dc;
1123     apr_off_t inflate_limit;
1124
1125     /* just get out of the way of things we don't want. */
1126     if (mode != AP_MODE_READBYTES) {
1127         return ap_get_brigade(f->next, bb, mode, block, readbytes);
1128     }
1129
1130     c = ap_get_module_config(r->server->module_config, &deflate_module);
1131     dc = ap_get_module_config(r->per_dir_config, &deflate_module);
1132
1133     if (!ctx || ctx->header_len < sizeof(ctx->header)) {
1134         apr_size_t len;
1135
1136         if (!ctx) {
1137             /* only work on main request/no subrequests */
1138             if (!ap_is_initial_req(r)) {
1139                 ap_remove_input_filter(f);
1140                 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1141             }
1142
1143             /* We can't operate on Content-Ranges */
1144             if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
1145                 ap_remove_input_filter(f);
1146                 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1147             }
1148
1149             /* Check whether request body is gzipped.
1150              *
1151              * If it is, we're transforming the contents, invalidating
1152              * some request headers including Content-Encoding.
1153              *
1154              * If not, we just remove ourself.
1155              */
1156             if (check_gzip(r, r->headers_in, NULL) == 0) {
1157                 ap_remove_input_filter(f);
1158                 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1159             }
1160
1161             f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1162             ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1163             ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1164             ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1165         }
1166
1167         do {
1168             apr_brigade_cleanup(ctx->bb);
1169
1170             len = sizeof(ctx->header) - ctx->header_len;
1171             rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block,
1172                                 len);
1173
1174             /* ap_get_brigade may return success with an empty brigade for
1175              * a non-blocking read which would block (an empty brigade for
1176              * a blocking read is an issue which is simply forwarded here).
1177              */
1178             if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(ctx->bb)) {
1179                 return rv;
1180             }
1181
1182             /* zero length body? step aside */
1183             bkt = APR_BRIGADE_FIRST(ctx->bb);
1184             if (APR_BUCKET_IS_EOS(bkt)) {
1185                 if (ctx->header_len) {
1186                     /* If the header was (partially) read it's an error, this
1187                      * is not a gzip Content-Encoding, as claimed.
1188                      */
1189                     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02619)
1190                                   "Encountered premature end-of-stream while "
1191                                   "reading inflate header");
1192                     return APR_EGENERAL;
1193                 }
1194                 APR_BUCKET_REMOVE(bkt);
1195                 APR_BRIGADE_INSERT_TAIL(bb, bkt);
1196                 ap_remove_input_filter(f);
1197                 return APR_SUCCESS;
1198             }
1199
1200             rv = apr_brigade_flatten(ctx->bb,
1201                                      ctx->header + ctx->header_len, &len);
1202             if (rv != APR_SUCCESS) {
1203                 return rv;
1204             }
1205             if (len && !ctx->header_len) {
1206                 apr_table_unset(r->headers_in, "Content-Length");
1207                 apr_table_unset(r->headers_in, "Content-MD5");
1208             }
1209             ctx->header_len += len;
1210
1211         } while (ctx->header_len < sizeof(ctx->header));
1212
1213         /* We didn't get the magic bytes. */
1214         if (ctx->header[0] != deflate_magic[0] ||
1215             ctx->header[1] != deflate_magic[1]) {
1216             ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01387)
1217                           "Zlib: Invalid header");
1218             return APR_EGENERAL;
1219         }
1220
1221         ctx->zlib_flags = ctx->header[3];
1222         if ((ctx->zlib_flags & RESERVED)) {
1223             ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01388)
1224                           "Zlib: Invalid flags %02x", ctx->zlib_flags);
1225             return APR_EGENERAL;
1226         }
1227
1228         zRC = inflateInit2(&ctx->stream, c->windowSize);
1229
1230         if (zRC != Z_OK) {
1231             f->ctx = NULL;
1232             inflateEnd(&ctx->stream);
1233             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01389)
1234                           "unable to init Zlib: "
1235                           "inflateInit2 returned %d: URL %s",
1236                           zRC, r->uri);
1237             ap_remove_input_filter(f);
1238             return ap_get_brigade(f->next, bb, mode, block, readbytes);
1239         }
1240
1241         /* initialize deflate output buffer */
1242         ctx->stream.next_out = ctx->buffer;
1243         ctx->stream.avail_out = c->bufferSize;
1244
1245         apr_brigade_cleanup(ctx->bb);
1246     }
1247
1248     inflate_limit = dc->inflate_limit; 
1249     if (inflate_limit == 0) { 
1250         /* The core is checking the deflated body, we'll check the inflated */
1251         inflate_limit = ap_get_limit_req_body(f->r);
1252     }
1253
1254     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1255         rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
1256
1257         /* Don't terminate on EAGAIN (or success with an empty brigade in
1258          * non-blocking mode), just return focus.
1259          */
1260         if (block == APR_NONBLOCK_READ
1261                 && (APR_STATUS_IS_EAGAIN(rv)
1262                     || (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(ctx->bb)))) {
1263             return rv;
1264         }
1265         if (rv != APR_SUCCESS) {
1266             inflateEnd(&ctx->stream);
1267             return rv;
1268         }
1269
1270         for (bkt = APR_BRIGADE_FIRST(ctx->bb);
1271              bkt != APR_BRIGADE_SENTINEL(ctx->bb);
1272              bkt = APR_BUCKET_NEXT(bkt))
1273         {
1274             const char *data;
1275             apr_size_t len;
1276
1277             if (APR_BUCKET_IS_EOS(bkt)) {
1278                 if (!ctx->done) {
1279                     inflateEnd(&ctx->stream);
1280                     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02481)
1281                                   "Encountered premature end-of-stream while inflating");
1282                     return APR_EGENERAL;
1283                 }
1284
1285                 /* Move everything to the returning brigade. */
1286                 APR_BUCKET_REMOVE(bkt);
1287                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, bkt);
1288                 break;
1289             }
1290
1291             if (APR_BUCKET_IS_FLUSH(bkt)) {
1292                 apr_bucket *tmp_b;
1293
1294                 ctx->inflate_total += ctx->stream.avail_out;
1295                 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
1296                 ctx->inflate_total -= ctx->stream.avail_out;
1297                 if (zRC != Z_OK) {
1298                     inflateEnd(&ctx->stream);
1299                     ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01391)
1300                                   "Zlib error %d inflating data (%s)", zRC,
1301                                   ctx->stream.msg);
1302                     return APR_EGENERAL;
1303                 }
1304  
1305                 if (inflate_limit && ctx->inflate_total > inflate_limit) { 
1306                     inflateEnd(&ctx->stream);
1307                     ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02647)
1308                             "Inflated content length of %" APR_OFF_T_FMT
1309                             " is larger than the configured limit"
1310                             " of %" APR_OFF_T_FMT, 
1311                             ctx->inflate_total, inflate_limit);
1312                     return APR_ENOSPC;
1313                 }
1314
1315                 if (!check_ratio(r, ctx, dc)) {
1316                     inflateEnd(&ctx->stream);
1317                     ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02805)
1318                             "Inflated content ratio is larger than the "
1319                             "configured limit %i by %i time(s)",
1320                             dc->ratio_limit, dc->ratio_burst);
1321                     return APR_EINVAL;
1322                 }
1323
1324                 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1325                                UPDATE_CRC, ctx->proc_bb);
1326
1327                 /* Flush everything so far in the returning brigade, but continue
1328                  * reading should EOS/more follow (don't lose them).
1329                  */
1330                 tmp_b = APR_BUCKET_PREV(bkt);
1331                 APR_BUCKET_REMOVE(bkt);
1332                 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, bkt);
1333                 bkt = tmp_b;
1334                 continue;
1335             }
1336
1337             /* sanity check - data after completed compressed body and before eos? */
1338             if (ctx->done) {
1339                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02482)
1340                               "Encountered extra data after compressed data");
1341                 return APR_EGENERAL;
1342             }
1343
1344             /* read */
1345             apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
1346             if (!len) {
1347                 continue;
1348             }
1349             if (len > APR_INT32_MAX) {
1350                 apr_bucket_split(bkt, APR_INT32_MAX);
1351                 apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
1352             }
1353
1354             if (ctx->zlib_flags) {
1355                 rv = consume_zlib_flags(ctx, &data, &len);
1356                 if (rv == APR_SUCCESS) {
1357                     ctx->zlib_flags = 0;
1358                 }
1359                 if (!len) {
1360                     continue;
1361                 }
1362             }
1363
1364             /* pass through zlib inflate. */
1365             ctx->stream.next_in = (unsigned char *)data;
1366             ctx->stream.avail_in = (int)len;
1367
1368             zRC = Z_OK;
1369
1370             if (!ctx->validation_buffer) {
1371                 while (ctx->stream.avail_in != 0) {
1372                     if (ctx->stream.avail_out == 0) {
1373                         consume_buffer(ctx, c, c->bufferSize, UPDATE_CRC,
1374                                        ctx->proc_bb);
1375                     }
1376
1377                     ctx->inflate_total += ctx->stream.avail_out;
1378                     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1379                     ctx->inflate_total -= ctx->stream.avail_out;
1380                     if (zRC != Z_OK && zRC != Z_STREAM_END) {
1381                         inflateEnd(&ctx->stream);
1382                         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01392)
1383                                       "Zlib error %d inflating data (%s)", zRC,
1384                                       ctx->stream.msg);
1385                         return APR_EGENERAL;
1386                     }
1387
1388                     if (inflate_limit && ctx->inflate_total > inflate_limit) { 
1389                         inflateEnd(&ctx->stream);
1390                         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02648)
1391                                 "Inflated content length of %" APR_OFF_T_FMT
1392                                 " is larger than the configured limit"
1393                                 " of %" APR_OFF_T_FMT, 
1394                                 ctx->inflate_total, inflate_limit);
1395                         return APR_ENOSPC;
1396                     }
1397
1398                     if (!check_ratio(r, ctx, dc)) {
1399                         inflateEnd(&ctx->stream);
1400                         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02649)
1401                                 "Inflated content ratio is larger than the "
1402                                 "configured limit %i by %i time(s)",
1403                                 dc->ratio_limit, dc->ratio_burst);
1404                         return APR_EINVAL;
1405                     }
1406
1407                     if (zRC == Z_STREAM_END) {
1408                         ctx->validation_buffer = apr_pcalloc(r->pool,
1409                                                              VALIDATION_SIZE);
1410                         ctx->validation_buffer_length = 0;
1411                         break;
1412                     }
1413                 }
1414             }
1415
1416             if (ctx->validation_buffer) {
1417                 apr_size_t avail, valid;
1418                 unsigned char *buf = ctx->validation_buffer;
1419
1420                 avail = ctx->stream.avail_in;
1421                 valid = (apr_size_t)VALIDATION_SIZE -
1422                          ctx->validation_buffer_length;
1423
1424                 /*
1425                  * We have inflated all data. Now try to capture the
1426                  * validation bytes. We may not have them all available
1427                  * right now, but capture what is there.
1428                  */
1429                 if (avail < valid) {
1430                     memcpy(buf + ctx->validation_buffer_length,
1431                            ctx->stream.next_in, avail);
1432                     ctx->validation_buffer_length += avail;
1433                     continue;
1434                 }
1435                 memcpy(buf + ctx->validation_buffer_length,
1436                        ctx->stream.next_in, valid);
1437                 ctx->validation_buffer_length += valid;
1438
1439                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01393)
1440                               "Zlib: Inflated %ld to %ld : URL %s",
1441                               ctx->stream.total_in, ctx->stream.total_out,
1442                               r->uri);
1443
1444                 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1445                                UPDATE_CRC, ctx->proc_bb);
1446
1447                 {
1448                     unsigned long compCRC, compLen;
1449                     compCRC = getLong(buf);
1450                     if (ctx->crc != compCRC) {
1451                         inflateEnd(&ctx->stream);
1452                         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01394)
1453                                       "Zlib: CRC error inflating data");
1454                         return APR_EGENERAL;
1455                     }
1456                     compLen = getLong(buf + VALIDATION_SIZE / 2);
1457                     /* gzip stores original size only as 4 byte value */
1458                     if ((ctx->stream.total_out & 0xFFFFFFFF) != compLen) {
1459                         inflateEnd(&ctx->stream);
1460                         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01395)
1461                                       "Zlib: Length %ld of inflated data does "
1462                                       "not match expected value %ld",
1463                                       ctx->stream.total_out, compLen);
1464                         return APR_EGENERAL;
1465                     }
1466                 }
1467
1468                 inflateEnd(&ctx->stream);
1469
1470                 ctx->done = 1;
1471
1472                 /* Did we have trailing data behind the closing 8 bytes? */
1473                 if (avail > valid) {
1474                     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02485)
1475                                   "Encountered extra data after compressed data");
1476                     return APR_EGENERAL;
1477                 }
1478             }
1479
1480         }
1481         apr_brigade_cleanup(ctx->bb);
1482     }
1483
1484     /* If we are about to return nothing for a 'blocking' read and we have
1485      * some data in our zlib buffer, flush it out so we can return something.
1486      */
1487     if (block == APR_BLOCK_READ &&
1488             APR_BRIGADE_EMPTY(ctx->proc_bb) &&
1489             ctx->stream.avail_out < c->bufferSize) {
1490         consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1491                        UPDATE_CRC, ctx->proc_bb);
1492     }
1493
1494     if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1495         if (apr_brigade_partition(ctx->proc_bb, readbytes, &bkt) == APR_INCOMPLETE) {
1496             APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1497         }
1498         else {
1499             APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1500             apr_brigade_split_ex(bb, bkt, ctx->proc_bb);
1501         }
1502         if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
1503             ap_remove_input_filter(f);
1504         }
1505     }
1506
1507     return APR_SUCCESS;
1508 }
1509
1510
1511 /* Filter to inflate for a content-transforming proxy.  */
1512 static apr_status_t inflate_out_filter(ap_filter_t *f,
1513                                       apr_bucket_brigade *bb)
1514 {
1515     apr_bucket *e;
1516     request_rec *r = f->r;
1517     deflate_ctx *ctx = f->ctx;
1518     int zRC;
1519     apr_status_t rv;
1520     deflate_filter_config *c;
1521     deflate_dirconf_t *dc;
1522
1523     /* Do nothing if asked to filter nothing. */
1524     if (APR_BRIGADE_EMPTY(bb)) {
1525         return APR_SUCCESS;
1526     }
1527
1528     c = ap_get_module_config(r->server->module_config, &deflate_module);
1529     dc = ap_get_module_config(r->per_dir_config, &deflate_module);
1530
1531     if (!ctx) {
1532
1533         /*
1534          * Only work on main request, not subrequests,
1535          * that are not a 204 response with no content
1536          * and not a partial response to a Range request,
1537          * and only when Content-Encoding ends in gzip.
1538          */
1539         if (!ap_is_initial_req(r) || (r->status == HTTP_NO_CONTENT) ||
1540             (apr_table_get(r->headers_out, "Content-Range") != NULL) ||
1541             (check_gzip(r, r->headers_out, r->err_headers_out) == 0)
1542            ) {
1543             ap_remove_output_filter(f);
1544             return ap_pass_brigade(f->next, bb);
1545         }
1546
1547         /*
1548          * At this point we have decided to filter the content, so change
1549          * important content metadata before sending any response out.
1550          * Content-Encoding was already reset by the check_gzip() call.
1551          */
1552         apr_table_unset(r->headers_out, "Content-Length");
1553         apr_table_unset(r->headers_out, "Content-MD5");
1554         if (c->etag_opt != AP_DEFLATE_ETAG_NOCHANGE) {
1555             deflate_check_etag(r, "gunzip", c->etag_opt);
1556         }
1557
1558         /* For a 304 response, only change the headers */
1559         if (r->status == HTTP_NOT_MODIFIED) {
1560             ap_remove_output_filter(f);
1561             return ap_pass_brigade(f->next, bb);
1562         }
1563
1564         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1565         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1566         ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1567         ctx->libz_end_func = inflateEnd;
1568         ctx->validation_buffer = NULL;
1569         ctx->validation_buffer_length = 0;
1570
1571         zRC = inflateInit2(&ctx->stream, c->windowSize);
1572
1573         if (zRC != Z_OK) {
1574             f->ctx = NULL;
1575             inflateEnd(&ctx->stream);
1576             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01397)
1577                           "unable to init Zlib: "
1578                           "inflateInit2 returned %d: URL %s",
1579                           zRC, r->uri);
1580             /*
1581              * Remove ourselves as it does not make sense to return:
1582              * We are not able to init libz and pass data down the chain
1583              * compressed.
1584              */
1585             ap_remove_output_filter(f);
1586             return ap_pass_brigade(f->next, bb);
1587         }
1588
1589         /*
1590          * Register a cleanup function to ensure that we cleanup the internal
1591          * libz resources.
1592          */
1593         apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
1594                                   apr_pool_cleanup_null);
1595
1596         /* initialize inflate output buffer */
1597         ctx->stream.next_out = ctx->buffer;
1598         ctx->stream.avail_out = c->bufferSize;
1599     }
1600
1601     while (!APR_BRIGADE_EMPTY(bb))
1602     {
1603         const char *data;
1604         apr_size_t len;
1605
1606         e = APR_BRIGADE_FIRST(bb);
1607
1608         if (APR_BUCKET_IS_EOS(e)) {
1609             /*
1610              * We are really done now. Ensure that we never return here, even
1611              * if a second EOS bucket falls down the chain. Thus remove
1612              * ourselves.
1613              */
1614             ap_remove_output_filter(f);
1615             /* should be zero already anyway */
1616             ctx->stream.avail_in = 0;
1617             /*
1618              * Flush the remaining data from the zlib buffers. It is correct
1619              * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1620              * deflate case. In the inflate case Z_FINISH requires to have a
1621              * large enough output buffer to put ALL data in otherwise it
1622              * fails, whereas in the deflate case you can empty a filled output
1623              * buffer and call it again until no more output can be created.
1624              */
1625             flush_libz_buffer(ctx, c, inflate, Z_SYNC_FLUSH, UPDATE_CRC);
1626             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01398)
1627                           "Zlib: Inflated %ld to %ld : URL %s",
1628                           ctx->stream.total_in, ctx->stream.total_out, r->uri);
1629
1630             if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1631                 unsigned long compCRC, compLen;
1632                 compCRC = getLong(ctx->validation_buffer);
1633                 if (ctx->crc != compCRC) {
1634                     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01399)
1635                                   "Zlib: Checksum of inflated stream invalid");
1636                     return APR_EGENERAL;
1637                 }
1638                 ctx->validation_buffer += VALIDATION_SIZE / 2;
1639                 compLen = getLong(ctx->validation_buffer);
1640                 /* gzip stores original size only as 4 byte value */
1641                 if ((ctx->stream.total_out & 0xFFFFFFFF) != compLen) {
1642                     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01400)
1643                                   "Zlib: Length of inflated stream invalid");
1644                     return APR_EGENERAL;
1645                 }
1646             }
1647             else {
1648                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01401)
1649                               "Zlib: Validation bytes not present");
1650                 return APR_EGENERAL;
1651             }
1652
1653             inflateEnd(&ctx->stream);
1654             /* No need for cleanup any longer */
1655             apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1656
1657             /* Remove EOS from the old list, and insert into the new. */
1658             APR_BUCKET_REMOVE(e);
1659             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1660
1661             /*
1662              * Okay, we've seen the EOS.
1663              * Time to pass it along down the chain.
1664              */
1665             rv = ap_pass_brigade(f->next, ctx->bb);
1666             apr_brigade_cleanup(ctx->bb);
1667             return rv;
1668         }
1669
1670         if (APR_BUCKET_IS_FLUSH(e)) {
1671             /* flush the remaining data from the zlib buffers */
1672             zRC = flush_libz_buffer(ctx, c, inflate, Z_SYNC_FLUSH, UPDATE_CRC);
1673             if (zRC == Z_STREAM_END) {
1674                 if (ctx->validation_buffer == NULL) {
1675                     ctx->validation_buffer = apr_pcalloc(f->r->pool,
1676                                                          VALIDATION_SIZE);
1677                 }
1678             }
1679             else if (zRC != Z_OK) {
1680                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01402)
1681                               "Zlib error %d flushing inflate buffer (%s)",
1682                               zRC, ctx->stream.msg);
1683                 return APR_EGENERAL;
1684             }
1685
1686             /* Remove flush bucket from old brigade anf insert into the new. */
1687             APR_BUCKET_REMOVE(e);
1688             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1689             rv = ap_pass_brigade(f->next, ctx->bb);
1690             apr_brigade_cleanup(ctx->bb);
1691             if (rv != APR_SUCCESS) {
1692                 return rv;
1693             }
1694             continue;
1695         }
1696
1697         if (APR_BUCKET_IS_METADATA(e)) {
1698             /*
1699              * Remove meta data bucket from old brigade and insert into the
1700              * new.
1701              */
1702             APR_BUCKET_REMOVE(e);
1703             APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1704             continue;
1705         }
1706
1707         /* read */
1708         apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1709         if (!len) {
1710             apr_bucket_delete(e);
1711             continue;
1712         }
1713         if (len > APR_INT32_MAX) {
1714             apr_bucket_split(e, APR_INT32_MAX);
1715             apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1716         }
1717
1718         /* first bucket contains zlib header */
1719         if (ctx->header_len < sizeof(ctx->header)) {
1720             apr_size_t rem;
1721
1722             rem = sizeof(ctx->header) - ctx->header_len;
1723             if (len < rem) {
1724                 memcpy(ctx->header + ctx->header_len, data, len);
1725                 ctx->header_len += len;
1726                 apr_bucket_delete(e);
1727                 continue;
1728             }
1729             memcpy(ctx->header + ctx->header_len, data, rem);
1730             ctx->header_len += rem;
1731             {
1732                 int zlib_method;
1733                 zlib_method = ctx->header[2];
1734                 if (zlib_method != Z_DEFLATED) {
1735                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01404)
1736                                   "inflate: data not deflated!");
1737                     ap_remove_output_filter(f);
1738                     return ap_pass_brigade(f->next, bb);
1739                 }
1740                 if (ctx->header[0] != deflate_magic[0] ||
1741                     ctx->header[1] != deflate_magic[1]) {
1742                         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01405)
1743                                       "inflate: bad header");
1744                     return APR_EGENERAL ;
1745                 }
1746                 ctx->zlib_flags = ctx->header[3];
1747                 if ((ctx->zlib_flags & RESERVED)) {
1748                     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02620)
1749                                   "inflate: bad flags %02x",
1750                                   ctx->zlib_flags);
1751                     return APR_EGENERAL;
1752                 }
1753             }
1754             if (len == rem) {
1755                 apr_bucket_delete(e);
1756                 continue;
1757             }
1758             data += rem;
1759             len -= rem;
1760         }
1761
1762         if (ctx->zlib_flags) {
1763             rv = consume_zlib_flags(ctx, &data, &len);
1764             if (rv == APR_SUCCESS) {
1765                 ctx->zlib_flags = 0;
1766             }
1767             if (!len) {
1768                 apr_bucket_delete(e);
1769                 continue;
1770             }
1771         }
1772
1773         /* pass through zlib inflate. */
1774         ctx->stream.next_in = (unsigned char *)data;
1775         ctx->stream.avail_in = len;
1776
1777         if (ctx->validation_buffer) {
1778             if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1779                 apr_size_t copy_size;
1780
1781                 copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1782                 if (copy_size > ctx->stream.avail_in)
1783                     copy_size = ctx->stream.avail_in;
1784                 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1785                        ctx->stream.next_in, copy_size);
1786                 /* Saved copy_size bytes */
1787                 ctx->stream.avail_in -= copy_size;
1788                 ctx->validation_buffer_length += copy_size;
1789             }
1790             if (ctx->stream.avail_in) {
1791                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01407)
1792                               "Zlib: %d bytes of garbage at the end of "
1793                               "compressed stream.", ctx->stream.avail_in);
1794                 /*
1795                  * There is nothing worth consuming for zlib left, because it is
1796                  * either garbage data or the data has been copied to the
1797                  * validation buffer (processing validation data is no business
1798                  * for zlib). So set ctx->stream.avail_in to zero to indicate
1799                  * this to the following while loop.
1800                  */
1801                 ctx->stream.avail_in = 0;
1802             }
1803         }
1804
1805         while (ctx->stream.avail_in != 0) {
1806             if (ctx->stream.avail_out == 0) {
1807                 consume_buffer(ctx, c, c->bufferSize, UPDATE_CRC, ctx->bb);
1808
1809                 /* Send what we have right now to the next filter. */
1810                 rv = ap_pass_brigade(f->next, ctx->bb);
1811                 apr_brigade_cleanup(ctx->bb);
1812                 if (rv != APR_SUCCESS) {
1813                     return rv;
1814                 }
1815             }
1816
1817             zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1818
1819             if (zRC != Z_OK && zRC != Z_STREAM_END) {
1820                 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01409)
1821                               "Zlib error %d inflating data (%s)", zRC,
1822                               ctx->stream.msg);
1823                 return APR_EGENERAL;
1824             }
1825
1826             /* Don't check length limits on inflate_out */
1827             if (!check_ratio(r, ctx, dc)) {
1828                 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02650)
1829                               "Inflated content ratio is larger than the "
1830                               "configured limit %i by %i time(s)",
1831                               dc->ratio_limit, dc->ratio_burst);
1832                 return APR_EINVAL;
1833             }
1834
1835             if (zRC == Z_STREAM_END) {
1836                 /*
1837                  * We have inflated all data. Now try to capture the
1838                  * validation bytes. We may not have them all available
1839                  * right now, but capture what is there.
1840                  */
1841                 ctx->validation_buffer = apr_pcalloc(f->r->pool,
1842                                                      VALIDATION_SIZE);
1843                 if (ctx->stream.avail_in > VALIDATION_SIZE) {
1844                     ctx->validation_buffer_length = VALIDATION_SIZE;
1845                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01408)
1846                                   "Zlib: %d bytes of garbage at the end of "
1847                                   "compressed stream.",
1848                                   ctx->stream.avail_in - VALIDATION_SIZE);
1849                 }
1850                 else if (ctx->stream.avail_in > 0) {
1851                     ctx->validation_buffer_length = ctx->stream.avail_in;
1852                 }
1853                 if (ctx->validation_buffer_length)
1854                     memcpy(ctx->validation_buffer, ctx->stream.next_in,
1855                            ctx->validation_buffer_length);
1856                 break;
1857             }
1858         }
1859
1860         apr_bucket_delete(e);
1861     }
1862
1863     return APR_SUCCESS;
1864 }
1865
1866 static int mod_deflate_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1867                                    apr_pool_t *ptemp, server_rec *s)
1868 {
1869     mod_deflate_ssl_var = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
1870     return OK;
1871 }
1872
1873
1874 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1875 static void register_hooks(apr_pool_t *p)
1876 {
1877     ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1878                               AP_FTYPE_CONTENT_SET);
1879     ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1880                               AP_FTYPE_RESOURCE-1);
1881     ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1882                               AP_FTYPE_CONTENT_SET);
1883     ap_hook_post_config(mod_deflate_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1884 }
1885
1886 static const command_rec deflate_filter_cmds[] = {
1887     AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1888                   "Set a note to report on compression ratio"),
1889     AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1890                   RSRC_CONF, "Set the Deflate window size (1-15)"),
1891     AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1892                   "Set the Deflate Buffer Size"),
1893     AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1894                   "Set the Deflate Memory Level (1-9)"),
1895     AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1896                   "Set the Deflate Compression Level (1-9)"),
1897     AP_INIT_TAKE1("DeflateAlterEtag", deflate_set_etag, NULL, RSRC_CONF,
1898                   "Set how mod_deflate should modify ETAG response headers: 'AddSuffix' (default), 'NoChange' (2.2.x behavior), 'Remove'"),
1899     AP_INIT_TAKE1("DeflateInflateLimitRequestBody", deflate_set_inflate_limit, NULL, OR_ALL,
1900                   "Set a limit on size of inflated input"),
1901     AP_INIT_TAKE1("DeflateInflateRatioLimit", deflate_set_inflate_ratio_limit, NULL, OR_ALL,
1902                   "Set the inflate ratio limit above which inflation is "
1903                   "aborted (default: " APR_STRINGIFY(AP_INFLATE_RATIO_LIMIT) ")"),
1904     AP_INIT_TAKE1("DeflateInflateRatioBurst", deflate_set_inflate_ratio_burst, NULL, OR_ALL,
1905                   "Set the maximum number of following inflate ratios above limit "
1906                   "(default: " APR_STRINGIFY(AP_INFLATE_RATIO_BURST) ")"),
1907     {NULL}
1908 };
1909
1910 /* zlib can be built with #define deflate z_deflate */
1911 #ifdef deflate
1912 #undef deflate
1913 #endif
1914 AP_DECLARE_MODULE(deflate) = {
1915     STANDARD20_MODULE_STUFF,
1916     create_deflate_dirconf,       /* dir config creater */
1917     NULL,                         /* dir merger --- default is to override */
1918     create_deflate_server_config, /* server config */
1919     NULL,                         /* merge server config */
1920     deflate_filter_cmds,          /* command table */
1921     register_hooks                /* register hooks */
1922 };