/[Apache-SVN]/httpd/httpd/trunk/modules/filters/mod_deflate.c
ViewVC logotype

Contents of /httpd/httpd/trunk/modules/filters/mod_deflate.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 607219 - (hide annotations)
Fri Dec 28 12:03:20 2007 UTC (22 months, 4 weeks ago) by niq
File MIME type: text/plain
File size: 49033 byte(s)
PR 39727: Fixup ETag handling in mod deflate (updated following extensive
discussion on-list).
This is not a full-and-final fix, because we don't ourselves do anything
useful with these ETags.  But at least we're no longer screwing up clients.
1 fielding 420983 /* 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 jerenkrantz 94979 *
8 nd 102525 * http://www.apache.org/licenses/LICENSE-2.0
9 jerenkrantz 94979 *
10 nd 102525 * 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 jerenkrantz 94979 */
16    
17     /*
18 pquerna 161738 * mod_deflate.c: Perform deflate content-encoding on the fly
19 jerenkrantz 94979 *
20 ianh 103405 * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew
21 jerenkrantz 94979 */
22    
23 nd 102525 /*
24 niq 104034 * Portions of this software are based upon zlib code by Jean-loup Gailly
25     * (zlib functions gz_open and gzwrite, check_header)
26 nd 102525 */
27    
28 niq 104034 /* 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 jerenkrantz 94979 #include "httpd.h"
38     #include "http_config.h"
39     #include "http_log.h"
40 niq 560937 #include "apr_lib.h"
41 jerenkrantz 94979 #include "apr_strings.h"
42     #include "apr_general.h"
43     #include "util_filter.h"
44     #include "apr_buckets.h"
45     #include "http_request.h"
46 trawick 95548 #define APR_WANT_STRFUNC
47     #include "apr_want.h"
48 jerenkrantz 94979
49     #include "zlib.h"
50    
51     static const char deflateFilterName[] = "DEFLATE";
52     module AP_MODULE_DECLARE_DATA deflate_module;
53    
54     typedef struct deflate_filter_config_t
55     {
56     int windowSize;
57     int memlevel;
58 ianh 98895 int compressionlevel;
59 wrowe 95676 apr_size_t bufferSize;
60 nd 98154 char *note_ratio_name;
61     char *note_input_name;
62     char *note_output_name;
63 jerenkrantz 94979 } deflate_filter_config;
64    
65 jorton 105410 /* RFC 1952 Section 2.3 defines the gzip header:
66     *
67     * +---+---+---+---+---+---+---+---+---+---+
68     * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
69     * +---+---+---+---+---+---+---+---+---+---+
70     */
71 jim 332306 static const char gzip_header[10] =
72 jorton 105410 { '\037', '\213', Z_DEFLATED, 0,
73     0, 0, 0, 0, /* mtime */
74     0, 0x03 /* Unix OS_CODE */
75     };
76    
77     /* magic header */
78     static const char deflate_magic[2] = { '\037', '\213' };
79    
80 jerenkrantz 94979 /* windowsize is negative to suppress Zlib header */
81 ianh 98895 #define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
82 jerenkrantz 94979 #define DEFAULT_WINDOWSIZE -15
83     #define DEFAULT_MEMLEVEL 9
84 jerenkrantz 94987 #define DEFAULT_BUFFERSIZE 8096
85 jerenkrantz 94979
86 niq 562507
87     /* Check whether a request is gzipped, so we can un-gzip it.
88     * If a request has multiple encodings, we need the gzip
89     * to be the outermost non-identity encoding.
90     */
91 niq 563803 static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
92 niq 562507 {
93     int found = 0;
94 niq 563803 apr_table_t *hdrs = hdrs1;
95 niq 562507 const char *encoding = apr_table_get(hdrs, "Content-Encoding");
96 niq 563317
97     if (!encoding && (hdrs2 != NULL)) {
98 niq 563803 /* the output filter has two tables and a content_encoding to check */
99 niq 563317 encoding = apr_table_get(hdrs2, "Content-Encoding");
100 niq 563803 hdrs = hdrs2;
101     if (!encoding) {
102     encoding = r->content_encoding;
103     hdrs = NULL;
104     }
105 niq 563317 }
106 niq 562507 if (encoding && *encoding) {
107    
108     /* check the usual/simple case first */
109     if (!strcasecmp(encoding, "gzip")
110     || !strcasecmp(encoding, "x-gzip")) {
111     found = 1;
112 niq 563803 if (hdrs) {
113     apr_table_unset(hdrs, "Content-Encoding");
114     }
115     else {
116     r->content_encoding = NULL;
117     }
118 niq 562507 }
119     else if (ap_strchr_c(encoding, ',') != NULL) {
120     /* If the outermost encoding isn't gzip, there's nowt
121     * we can do. So only check the last non-identity token
122     */
123 niq 563803 char *new_encoding = apr_pstrdup(r->pool, encoding);
124 niq 562507 char *ptr;
125     for(;;) {
126     char *token = ap_strrchr(new_encoding, ',');
127     if (!token) { /* gzip:identity or other:identity */
128     if (!strcasecmp(new_encoding, "gzip")
129     || !strcasecmp(new_encoding, "x-gzip")) {
130     found = 1;
131 niq 563803 if (hdrs) {
132     apr_table_unset(hdrs, "Content-Encoding");
133     }
134     else {
135     r->content_encoding = NULL;
136     }
137 niq 562507 }
138     break; /* seen all tokens */
139     }
140     for (ptr=token+1; apr_isspace(*ptr); ++ptr);
141     if (!strcasecmp(ptr, "gzip")
142     || !strcasecmp(ptr, "x-gzip")) {
143     *token = '\0';
144 niq 563803 if (hdrs) {
145     apr_table_setn(hdrs, "Content-Encoding", new_encoding);
146     }
147     else {
148     r->content_encoding = new_encoding;
149     }
150 niq 562507 found = 1;
151     }
152     else if (!ptr[0] || !strcasecmp(ptr, "identity")) {
153     *token = '\0';
154     continue; /* strip the token and find the next one */
155     }
156     break; /* found a non-identity token */
157     }
158     }
159     }
160     return found;
161     }
162    
163 jerenkrantz 94979 /* Outputs a long in LSB order to the given file
164     * only the bottom 4 bits are required for the deflate file format.
165     */
166 jerenkrantz 95397 static void putLong(unsigned char *string, unsigned long x)
167 jerenkrantz 94979 {
168 jerenkrantz 95397 string[0] = (unsigned char)(x & 0xff);
169     string[1] = (unsigned char)((x & 0xff00) >> 8);
170     string[2] = (unsigned char)((x & 0xff0000) >> 16);
171     string[3] = (unsigned char)((x & 0xff000000) >> 24);
172 jerenkrantz 94979 }
173    
174 jerenkrantz 95345 /* Inputs a string and returns a long.
175     */
176 trawick 95350 static unsigned long getLong(unsigned char *string)
177 jerenkrantz 95345 {
178 jerenkrantz 95397 return ((unsigned long)string[0])
179     | (((unsigned long)string[1]) << 8)
180     | (((unsigned long)string[2]) << 16)
181     | (((unsigned long)string[3]) << 24);
182 jerenkrantz 95345 }
183    
184 jerenkrantz 94979 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
185     {
186     deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
187    
188     c->memlevel = DEFAULT_MEMLEVEL;
189     c->windowSize = DEFAULT_WINDOWSIZE;
190 jerenkrantz 94987 c->bufferSize = DEFAULT_BUFFERSIZE;
191 ianh 98895 c->compressionlevel = DEFAULT_COMPRESSION;
192 jerenkrantz 94979
193     return c;
194     }
195    
196     static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
197     const char *arg)
198     {
199     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
200     &deflate_module);
201     int i;
202    
203     i = atoi(arg);
204    
205     if (i < 1 || i > 15)
206     return "DeflateWindowSize must be between 1 and 15";
207    
208     c->windowSize = i * -1;
209    
210     return NULL;
211     }
212    
213 jerenkrantz 94987 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
214     const char *arg)
215     {
216     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
217     &deflate_module);
218 wrowe 95676 int n = atoi(arg);
219 jerenkrantz 94987
220 wrowe 95676 if (n <= 0) {
221 jerenkrantz 94987 return "DeflateBufferSize should be positive";
222     }
223    
224 wrowe 95676 c->bufferSize = (apr_size_t)n;
225    
226 jerenkrantz 94987 return NULL;
227     }
228 jerenkrantz 94979 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
229 nd 98149 const char *arg1, const char *arg2)
230 jerenkrantz 94979 {
231     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
232     &deflate_module);
233 jim 332306
234 nd 98149 if (arg2 == NULL) {
235 nd 98154 c->note_ratio_name = apr_pstrdup(cmd->pool, arg1);
236 nd 98149 }
237     else if (!strcasecmp(arg1, "ratio")) {
238 nd 98154 c->note_ratio_name = apr_pstrdup(cmd->pool, arg2);
239 nd 98149 }
240     else if (!strcasecmp(arg1, "input")) {
241 nd 98154 c->note_input_name = apr_pstrdup(cmd->pool, arg2);
242 nd 98149 }
243     else if (!strcasecmp(arg1, "output")) {
244 nd 98154 c->note_output_name = apr_pstrdup(cmd->pool, arg2);
245 nd 98149 }
246     else {
247     return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
248     }
249 jerenkrantz 94979
250     return NULL;
251     }
252    
253     static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
254     const char *arg)
255     {
256     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
257     &deflate_module);
258     int i;
259    
260     i = atoi(arg);
261    
262     if (i < 1 || i > 9)
263     return "DeflateMemLevel must be between 1 and 9";
264    
265     c->memlevel = i;
266    
267     return NULL;
268     }
269    
270 ianh 98895 static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
271     const char *arg)
272     {
273     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
274     &deflate_module);
275     int i;
276    
277     i = atoi(arg);
278    
279     if (i < 1 || i > 9)
280     return "Compression Level must be between 1 and 9";
281    
282     c->compressionlevel = i;
283    
284     return NULL;
285     }
286    
287 jerenkrantz 94979 typedef struct deflate_ctx_t
288     {
289     z_stream stream;
290 jerenkrantz 94987 unsigned char *buffer;
291 jerenkrantz 94979 unsigned long crc;
292 jerenkrantz 95345 apr_bucket_brigade *bb, *proc_bb;
293 rpluem 425109 int (*libz_end_func)(z_streamp);
294 rpluem 426799 unsigned char *validation_buffer;
295     apr_size_t validation_buffer_length;
296 niq 580598 int inflate_init;
297 jerenkrantz 94979 } deflate_ctx;
298    
299 rpluem 426791 /* Number of validation bytes (CRC and length) after the compressed data */
300     #define VALIDATION_SIZE 8
301 rpluem 426790 /* Do not update ctx->crc, see comment in flush_libz_buffer */
302     #define NO_UPDATE_CRC 0
303     /* Do update ctx->crc, see comment in flush_libz_buffer */
304     #define UPDATE_CRC 1
305    
306 rpluem 423940 static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
307 rpluem 422731 struct apr_bucket_alloc_t *bucket_alloc,
308 rpluem 426790 int (*libz_func)(z_streamp, int), int flush,
309     int crc)
310 rpluem 422731 {
311 rpluem 424950 int zRC = Z_OK;
312 rpluem 422731 int done = 0;
313     unsigned int deflate_len;
314     apr_bucket *b;
315    
316     for (;;) {
317     deflate_len = c->bufferSize - ctx->stream.avail_out;
318    
319     if (deflate_len != 0) {
320 rpluem 426790 /*
321     * Do we need to update ctx->crc? Usually this is the case for
322     * inflate action where we need to do a crc on the output, whereas
323     * in the deflate case we need to do a crc on the input
324     */
325     if (crc) {
326     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer,
327     deflate_len);
328     }
329 rpluem 422731 b = apr_bucket_heap_create((char *)ctx->buffer,
330     deflate_len, NULL,
331     bucket_alloc);
332     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
333     ctx->stream.next_out = ctx->buffer;
334     ctx->stream.avail_out = c->bufferSize;
335     }
336    
337     if (done)
338     break;
339    
340 rpluem 423940 zRC = libz_func(&ctx->stream, flush);
341 rpluem 422731
342 rpluem 426793 /*
343     * We can ignore Z_BUF_ERROR because:
344     * When we call libz_func we can assume that
345     *
346     * - avail_in is zero (due to the surrounding code that calls
347     * flush_libz_buffer)
348     * - avail_out is non zero due to our actions some lines above
349     *
350     * So the only reason for Z_BUF_ERROR is that the internal libz
351     * buffers are now empty and thus we called libz_func one time
352     * too often. This does not hurt. It simply says that we are done.
353     */
354     if (zRC == Z_BUF_ERROR) {
355 rpluem 422731 zRC = Z_OK;
356 rpluem 426793 break;
357     }
358 rpluem 422731
359     done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
360    
361     if (zRC != Z_OK && zRC != Z_STREAM_END)
362     break;
363     }
364     return zRC;
365     }
366    
367 rpluem 425109 static apr_status_t deflate_ctx_cleanup(void *data)
368     {
369     deflate_ctx *ctx = (deflate_ctx *)data;
370    
371     if (ctx)
372     ctx->libz_end_func(&ctx->stream);
373     return APR_SUCCESS;
374     }
375 niq 581198 /* PR 39727: we're screwing up our clients if we leave a strong ETag
376 niq 607219 * header while transforming content. Henrik Nordstrom suggests
377     * appending ";gzip".
378 niq 581198 *
379 niq 607219 * Pending a more thorough review of our Etag handling, let's just
380     * implement his suggestion. It fixes the bug, or at least turns it
381     * from a showstopper to an inefficiency. And it breaks nothing that
382     * wasn't already broken.
383 niq 581198 */
384 niq 607219 static void deflate_check_etag(request_rec *r, const char *transform)
385 niq 581198 {
386     const char *etag = apr_table_get(r->headers_out, "ETag");
387     if (etag && (((etag[0] != 'W') && (etag[0] !='w')) || (etag[1] != '/'))) {
388     apr_table_set(r->headers_out, "ETag",
389 niq 607219 apr_pstrcat(r->pool, etag, "-", transform, NULL));
390 niq 581198 }
391     }
392 jerenkrantz 94979 static apr_status_t deflate_out_filter(ap_filter_t *f,
393     apr_bucket_brigade *bb)
394     {
395     apr_bucket *e;
396     request_rec *r = f->r;
397     deflate_ctx *ctx = f->ctx;
398     int zRC;
399 rpluem 426795 deflate_filter_config *c;
400 jerenkrantz 94979
401 niq 104352 /* Do nothing if asked to filter nothing. */
402     if (APR_BRIGADE_EMPTY(bb)) {
403 rpluem 424759 return ap_pass_brigade(f->next, bb);
404 niq 104352 }
405    
406 rpluem 426795 c = ap_get_module_config(r->server->module_config,
407     &deflate_module);
408    
409 jerenkrantz 94979 /* If we don't have a context, we need to ensure that it is okay to send
410     * the deflated content. If we have a context, that means we've done
411     * this before and we liked it.
412     * This could be not so nice if we always fail. But, if we succeed,
413     * we're in better shape.
414     */
415     if (!ctx) {
416 jorton 105410 char *token;
417 niq 104352 const char *encoding;
418 jerenkrantz 94979
419     /* only work on main request/no subrequests */
420 colm 395079 if (r->main != NULL) {
421 jerenkrantz 94982 ap_remove_output_filter(f);
422 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
423     }
424    
425     /* some browsers might have problems, so set no-gzip
426     * (with browsermatch) for them
427     */
428     if (apr_table_get(r->subprocess_env, "no-gzip")) {
429 jerenkrantz 94982 ap_remove_output_filter(f);
430 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
431     }
432    
433 niq 563154 /* We can't operate on Content-Ranges */
434     if (apr_table_get(r->headers_out, "Content-Range") != NULL) {
435     ap_remove_output_filter(f);
436     return ap_pass_brigade(f->next, bb);
437     }
438    
439 jerenkrantz 94979 /* Some browsers might have problems with content types
440     * other than text/html, so set gzip-only-text/html
441     * (with browsermatch) for them
442     */
443 ianh 96318 if (r->content_type == NULL
444     || strncmp(r->content_type, "text/html", 9)) {
445     const char *env_value = apr_table_get(r->subprocess_env,
446     "gzip-only-text/html");
447 ianh 96588 if ( env_value && (strcmp(env_value,"1") == 0) ) {
448 ianh 96318 ap_remove_output_filter(f);
449 ianh 96590 return ap_pass_brigade(f->next, bb);
450 jim 332306 }
451 jerenkrantz 94979 }
452    
453 jerenkrantz 95339 /* Let's see what our current Content-Encoding is.
454 nd 101019 * If it's already encoded, don't compress again.
455     * (We could, but let's not.)
456 jerenkrantz 95339 */
457     encoding = apr_table_get(r->headers_out, "Content-Encoding");
458     if (encoding) {
459 nd 98948 const char *err_enc;
460    
461     err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
462     if (err_enc) {
463     encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
464     }
465     }
466     else {
467     encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
468     }
469    
470 nd 99880 if (r->content_encoding) {
471     encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
472     r->content_encoding, NULL)
473     : r->content_encoding;
474     }
475    
476 nd 98948 if (encoding) {
477 jerenkrantz 95339 const char *tmp = encoding;
478    
479     token = ap_get_token(r->pool, &tmp, 0);
480 nd 101019 while (token && *token) {
481     /* stolen from mod_negotiation: */
482     if (strcmp(token, "identity") && strcmp(token, "7bit") &&
483     strcmp(token, "8bit") && strcmp(token, "binary")) {
484    
485 jerenkrantz 95339 ap_remove_output_filter(f);
486 ianh 103405 return ap_pass_brigade(f->next, bb);
487 jerenkrantz 95339 }
488 nd 101019
489 jerenkrantz 95339 /* Otherwise, skip token */
490 nd 101019 if (*tmp) {
491     ++tmp;
492     }
493     token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
494 jerenkrantz 95339 }
495 ianh 95181 }
496    
497 jerenkrantz 97473 /* Even if we don't accept this request based on it not having
498     * the Accept-Encoding, we need to note that we were looking
499     * for this header and downstream proxies should be aware of that.
500     */
501 pquerna 161691 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
502 jerenkrantz 97473
503 ianh 103405 /* force-gzip will just force it out regardless if the browser
504     * can actually do anything with it.
505     */
506 jerenkrantz 103803 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
507 ianh 103405 const char *accepts;
508     /* if they don't have the line, then they can't play */
509     accepts = apr_table_get(r->headers_in, "Accept-Encoding");
510     if (accepts == NULL) {
511     ap_remove_output_filter(f);
512     return ap_pass_brigade(f->next, bb);
513 nd 101015 }
514    
515 ianh 103405 token = ap_get_token(r->pool, &accepts, 0);
516     while (token && token[0] && strcasecmp(token, "gzip")) {
517     /* skip parameters, XXX: ;q=foo evaluation? */
518 jim 332306 while (*accepts == ';') {
519 ianh 103405 ++accepts;
520     token = ap_get_token(r->pool, &accepts, 1);
521     }
522    
523     /* retrieve next token */
524     if (*accepts == ',') {
525     ++accepts;
526     }
527     token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
528 nd 101015 }
529 jerenkrantz 94979
530 ianh 103405 /* No acceptable token found. */
531     if (token == NULL || token[0] == '\0') {
532     ap_remove_output_filter(f);
533     return ap_pass_brigade(f->next, bb);
534     }
535 jerenkrantz 94979 }
536 ake 103910
537 jorton 109500 /* For a 304 or 204 response there is no entity included in
538     * the response and hence nothing to deflate. */
539     if (r->status == HTTP_NOT_MODIFIED || r->status == HTTP_NO_CONTENT) {
540 niq 104352 ap_remove_output_filter(f);
541 ake 103910 return ap_pass_brigade(f->next, bb);
542     }
543    
544 jerenkrantz 94979 /* We're cool with filtering this. */
545     ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
546     ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
547 jerenkrantz 94987 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
548 rpluem 425109 ctx->libz_end_func = deflateEnd;
549 jerenkrantz 95339
550 ianh 98895 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
551 jerenkrantz 94979 c->windowSize, c->memlevel,
552     Z_DEFAULT_STRATEGY);
553    
554     if (zRC != Z_OK) {
555 rpluem 426795 deflateEnd(&ctx->stream);
556 trawick 95151 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
557 jerenkrantz 94979 "unable to init Zlib: "
558     "deflateInit2 returned %d: URL %s",
559     zRC, r->uri);
560 rpluem 422739 /*
561     * Remove ourselves as it does not make sense to return:
562     * We are not able to init libz and pass data down the chain
563     * uncompressed.
564     */
565     ap_remove_output_filter(f);
566 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
567     }
568 rpluem 425109 /*
569     * Register a cleanup function to ensure that we cleanup the internal
570     * libz resources.
571     */
572     apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
573     apr_pool_cleanup_null);
574 jerenkrantz 94979
575 jorton 105410 /* add immortal gzip header */
576     e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
577     f->c->bucket_alloc);
578 jerenkrantz 94979 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
579    
580 jerenkrantz 95339 /* If the entire Content-Encoding is "identity", we can replace it. */
581     if (!encoding || !strcasecmp(encoding, "identity")) {
582     apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
583     }
584     else {
585     apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
586     }
587 jerenkrantz 94979 apr_table_unset(r->headers_out, "Content-Length");
588 rpluem 563230 apr_table_unset(r->headers_out, "Content-MD5");
589 niq 607219 deflate_check_etag(r, "gzip");
590 jerenkrantz 95521
591     /* initialize deflate output buffer */
592     ctx->stream.next_out = ctx->buffer;
593     ctx->stream.avail_out = c->bufferSize;
594 jerenkrantz 94979 }
595 jim 332306
596 jorton 103810 while (!APR_BRIGADE_EMPTY(bb))
597 jwoolley 101788 {
598 jerenkrantz 94979 const char *data;
599     apr_bucket *b;
600     apr_size_t len;
601    
602 jorton 103810 e = APR_BRIGADE_FIRST(bb);
603    
604 jerenkrantz 94979 if (APR_BUCKET_IS_EOS(e)) {
605 jwoolley 95413 char *buf;
606 jerenkrantz 94979
607     ctx->stream.avail_in = 0; /* should be zero already anyway */
608 rpluem 422731 /* flush the remaining data from the zlib buffers */
609 rpluem 426790 flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
610     NO_UPDATE_CRC);
611 jerenkrantz 94979
612 rpluem 426791 buf = apr_palloc(r->pool, VALIDATION_SIZE);
613 jwoolley 95413 putLong((unsigned char *)&buf[0], ctx->crc);
614     putLong((unsigned char *)&buf[4], ctx->stream.total_in);
615 jerenkrantz 94979
616 rpluem 426791 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
617     f->c->bucket_alloc);
618 jerenkrantz 94979 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
619 trawick 95151 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
620 jerenkrantz 94979 "Zlib: Compressed %ld to %ld : URL %s",
621     ctx->stream.total_in, ctx->stream.total_out, r->uri);
622    
623 nd 98154 /* leave notes for logging */
624     if (c->note_input_name) {
625     apr_table_setn(r->notes, c->note_input_name,
626     (ctx->stream.total_in > 0)
627     ? apr_off_t_toa(r->pool,
628     ctx->stream.total_in)
629     : "-");
630     }
631 jerenkrantz 94979
632 nd 98154 if (c->note_output_name) {
633     apr_table_setn(r->notes, c->note_output_name,
634     (ctx->stream.total_in > 0)
635     ? apr_off_t_toa(r->pool,
636     ctx->stream.total_out)
637     : "-");
638     }
639    
640     if (c->note_ratio_name) {
641     apr_table_setn(r->notes, c->note_ratio_name,
642     (ctx->stream.total_in > 0)
643     ? apr_itoa(r->pool,
644     (int)(ctx->stream.total_out
645     * 100
646     / ctx->stream.total_in))
647     : "-");
648     }
649    
650 jerenkrantz 94979 deflateEnd(&ctx->stream);
651 rpluem 425109 /* No need for cleanup any longer */
652     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
653 jerenkrantz 94979
654     /* Remove EOS from the old list, and insert into the new. */
655     APR_BUCKET_REMOVE(e);
656     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
657    
658     /* Okay, we've seen the EOS.
659     * Time to pass it along down the chain.
660     */
661     return ap_pass_brigade(f->next, ctx->bb);
662     }
663    
664     if (APR_BUCKET_IS_FLUSH(e)) {
665 jerenkrantz 100730 apr_status_t rv;
666 jorton 103810
667 rpluem 422731 /* flush the remaining data from the zlib buffers */
668 rpluem 423940 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
669 rpluem 426790 Z_SYNC_FLUSH, NO_UPDATE_CRC);
670 rpluem 422731 if (zRC != Z_OK) {
671     return APR_EGENERAL;
672 jerenkrantz 95341 }
673 jerenkrantz 94979
674 rpluem 422731 /* Remove flush bucket from old brigade anf insert into the new. */
675     APR_BUCKET_REMOVE(e);
676     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
677 jerenkrantz 100730 rv = ap_pass_brigade(f->next, ctx->bb);
678     if (rv != APR_SUCCESS) {
679     return rv;
680     }
681 jerenkrantz 94979 continue;
682     }
683    
684 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
685     /*
686     * Remove meta data bucket from old brigade and insert into the
687     * new.
688     */
689     APR_BUCKET_REMOVE(e);
690     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
691     continue;
692 gregames 554011 }
693    
694 jerenkrantz 94979 /* read */
695     apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
696    
697     /* This crc32 function is from zlib. */
698     ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
699    
700     /* write */
701     ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
702     * but we'll just have to
703     * trust zlib */
704     ctx->stream.avail_in = len;
705    
706     while (ctx->stream.avail_in != 0) {
707     if (ctx->stream.avail_out == 0) {
708 jerenkrantz 100730 apr_status_t rv;
709    
710 jerenkrantz 94979 ctx->stream.next_out = ctx->buffer;
711 jerenkrantz 94987 len = c->bufferSize - ctx->stream.avail_out;
712 jerenkrantz 94979
713     b = apr_bucket_heap_create((char *)ctx->buffer, len,
714     NULL, f->c->bucket_alloc);
715     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
716 jerenkrantz 94987 ctx->stream.avail_out = c->bufferSize;
717 jerenkrantz 100730 /* Send what we have right now to the next filter. */
718     rv = ap_pass_brigade(f->next, ctx->bb);
719     if (rv != APR_SUCCESS) {
720     return rv;
721     }
722 jerenkrantz 94979 }
723    
724     zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
725    
726 rpluem 422736 if (zRC != Z_OK) {
727 jerenkrantz 94979 return APR_EGENERAL;
728 rpluem 422736 }
729 jerenkrantz 94979 }
730 jorton 103810
731     apr_bucket_delete(e);
732 jerenkrantz 94979 }
733    
734 jwoolley 97526 apr_brigade_cleanup(bb);
735 jerenkrantz 94979 return APR_SUCCESS;
736     }
737    
738 jerenkrantz 95345 /* This is the deflate input filter (inflates). */
739     static apr_status_t deflate_in_filter(ap_filter_t *f,
740     apr_bucket_brigade *bb,
741     ap_input_mode_t mode,
742     apr_read_type_e block,
743     apr_off_t readbytes)
744     {
745     apr_bucket *bkt;
746     request_rec *r = f->r;
747     deflate_ctx *ctx = f->ctx;
748     int zRC;
749     apr_status_t rv;
750     deflate_filter_config *c;
751    
752     /* just get out of the way of things we don't want. */
753     if (mode != AP_MODE_READBYTES) {
754     return ap_get_brigade(f->next, bb, mode, block, readbytes);
755     }
756    
757     c = ap_get_module_config(r->server->module_config, &deflate_module);
758    
759     if (!ctx) {
760 niq 562507 char deflate_hdr[10];
761 jerenkrantz 95345 apr_size_t len;
762    
763     /* only work on main request/no subrequests */
764 pquerna 105403 if (!ap_is_initial_req(r)) {
765 jerenkrantz 95345 ap_remove_input_filter(f);
766     return ap_get_brigade(f->next, bb, mode, block, readbytes);
767     }
768    
769 niq 563154 /* We can't operate on Content-Ranges */
770     if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
771     ap_remove_input_filter(f);
772     return ap_get_brigade(f->next, bb, mode, block, readbytes);
773     }
774    
775 niq 560689 /* Check whether request body is gzipped.
776     *
777     * If it is, we're transforming the contents, invalidating
778     * some request headers including Content-Encoding.
779     *
780     * If not, we just remove ourself.
781 jerenkrantz 95345 */
782 niq 563803 if (check_gzip(r, r->headers_in, NULL) == 0) {
783 jerenkrantz 95345 ap_remove_input_filter(f);
784     return ap_get_brigade(f->next, bb, mode, block, readbytes);
785     }
786    
787     f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
788     ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
789     ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
790     ctx->buffer = apr_palloc(r->pool, c->bufferSize);
791    
792     rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
793     if (rv != APR_SUCCESS) {
794     return rv;
795     }
796 jerenkrantz 98689
797 niq 563133 apr_table_unset(r->headers_in, "Content-Length");
798     apr_table_unset(r->headers_in, "Content-MD5");
799    
800 jim 332306 len = 10;
801     rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
802 jerenkrantz 95345 if (rv != APR_SUCCESS) {
803     return rv;
804     }
805    
806     /* We didn't get the magic bytes. */
807     if (len != 10 ||
808     deflate_hdr[0] != deflate_magic[0] ||
809     deflate_hdr[1] != deflate_magic[1]) {
810     return APR_EGENERAL;
811     }
812    
813     /* We can't handle flags for now. */
814     if (deflate_hdr[3] != 0) {
815     return APR_EGENERAL;
816     }
817    
818     zRC = inflateInit2(&ctx->stream, c->windowSize);
819    
820     if (zRC != Z_OK) {
821     f->ctx = NULL;
822 jerenkrantz 98689 inflateEnd(&ctx->stream);
823 jerenkrantz 95345 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
824     "unable to init Zlib: "
825     "inflateInit2 returned %d: URL %s",
826     zRC, r->uri);
827     ap_remove_input_filter(f);
828     return ap_get_brigade(f->next, bb, mode, block, readbytes);
829     }
830    
831     /* initialize deflate output buffer */
832     ctx->stream.next_out = ctx->buffer;
833     ctx->stream.avail_out = c->bufferSize;
834    
835     apr_brigade_cleanup(ctx->bb);
836     }
837    
838     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
839     rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
840    
841     if (rv != APR_SUCCESS) {
842 jerenkrantz 98689 /* What about APR_EAGAIN errors? */
843     inflateEnd(&ctx->stream);
844 jerenkrantz 95345 return rv;
845     }
846    
847 jwoolley 101788 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
848     bkt != APR_BRIGADE_SENTINEL(ctx->bb);
849     bkt = APR_BUCKET_NEXT(bkt))
850     {
851 jerenkrantz 95345 const char *data;
852     apr_size_t len;
853    
854     /* If we actually see the EOS, that means we screwed up! */
855     if (APR_BUCKET_IS_EOS(bkt)) {
856 jerenkrantz 98689 inflateEnd(&ctx->stream);
857 jerenkrantz 95345 return APR_EGENERAL;
858     }
859    
860     if (APR_BUCKET_IS_FLUSH(bkt)) {
861     apr_bucket *tmp_heap;
862     zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
863     if (zRC != Z_OK) {
864 jerenkrantz 98689 inflateEnd(&ctx->stream);
865 jerenkrantz 95345 return APR_EGENERAL;
866     }
867    
868     ctx->stream.next_out = ctx->buffer;
869     len = c->bufferSize - ctx->stream.avail_out;
870    
871     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
872     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
873     NULL, f->c->bucket_alloc);
874     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
875     ctx->stream.avail_out = c->bufferSize;
876    
877     /* Move everything to the returning brigade. */
878     APR_BUCKET_REMOVE(bkt);
879     APR_BRIGADE_CONCAT(bb, ctx->bb);
880     break;
881     }
882    
883     /* read */
884     apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
885    
886     /* pass through zlib inflate. */
887     ctx->stream.next_in = (unsigned char *)data;
888     ctx->stream.avail_in = len;
889    
890 jerenkrantz 95359 zRC = Z_OK;
891    
892 jerenkrantz 95345 while (ctx->stream.avail_in != 0) {
893     if (ctx->stream.avail_out == 0) {
894     apr_bucket *tmp_heap;
895     ctx->stream.next_out = ctx->buffer;
896     len = c->bufferSize - ctx->stream.avail_out;
897    
898     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
899     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
900     NULL, f->c->bucket_alloc);
901     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
902     ctx->stream.avail_out = c->bufferSize;
903     }
904    
905     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
906    
907     if (zRC == Z_STREAM_END) {
908     break;
909     }
910    
911     if (zRC != Z_OK) {
912 jerenkrantz 98689 inflateEnd(&ctx->stream);
913 jerenkrantz 95345 return APR_EGENERAL;
914     }
915     }
916     if (zRC == Z_STREAM_END) {
917     apr_bucket *tmp_heap, *eos;
918    
919     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
920     "Zlib: Inflated %ld to %ld : URL %s",
921     ctx->stream.total_in, ctx->stream.total_out,
922     r->uri);
923    
924     len = c->bufferSize - ctx->stream.avail_out;
925    
926     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
927     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
928     NULL, f->c->bucket_alloc);
929     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
930     ctx->stream.avail_out = c->bufferSize;
931    
932     /* Is the remaining 8 bytes already in the avail stream? */
933     if (ctx->stream.avail_in >= 8) {
934     unsigned long compCRC, compLen;
935     compCRC = getLong(ctx->stream.next_in);
936     if (ctx->crc != compCRC) {
937 jerenkrantz 98689 inflateEnd(&ctx->stream);
938 jerenkrantz 95345 return APR_EGENERAL;
939     }
940     ctx->stream.next_in += 4;
941     compLen = getLong(ctx->stream.next_in);
942     if (ctx->stream.total_out != compLen) {
943 jerenkrantz 98689 inflateEnd(&ctx->stream);
944 jerenkrantz 95345 return APR_EGENERAL;
945     }
946     }
947     else {
948     /* FIXME: We need to grab the 8 verification bytes
949     * from the wire! */
950 jerenkrantz 98689 inflateEnd(&ctx->stream);
951 jerenkrantz 95345 return APR_EGENERAL;
952     }
953    
954     inflateEnd(&ctx->stream);
955 jerenkrantz 98689
956 jerenkrantz 95345 eos = apr_bucket_eos_create(f->c->bucket_alloc);
957 jim 332306 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
958 jerenkrantz 95345 break;
959     }
960 jerenkrantz 95666
961 jerenkrantz 95345 }
962 jerenkrantz 95521 apr_brigade_cleanup(ctx->bb);
963 jerenkrantz 95345 }
964    
965 jerenkrantz 95666 /* If we are about to return nothing for a 'blocking' read and we have
966     * some data in our zlib buffer, flush it out so we can return something.
967     */
968     if (block == APR_BLOCK_READ &&
969     APR_BRIGADE_EMPTY(ctx->proc_bb) &&
970     ctx->stream.avail_out < c->bufferSize) {
971     apr_bucket *tmp_heap;
972     apr_size_t len;
973     ctx->stream.next_out = ctx->buffer;
974     len = c->bufferSize - ctx->stream.avail_out;
975    
976     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
977     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
978     NULL, f->c->bucket_alloc);
979     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
980     ctx->stream.avail_out = c->bufferSize;
981     }
982    
983 jerenkrantz 95345 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
984     apr_bucket_brigade *newbb;
985    
986     /* May return APR_INCOMPLETE which is fine by us. */
987     apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
988    
989     newbb = apr_brigade_split(ctx->proc_bb, bkt);
990     APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
991     APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
992     }
993    
994     return APR_SUCCESS;
995     }
996    
997 ianh 103405
998     /* Filter to inflate for a content-transforming proxy. */
999     static apr_status_t inflate_out_filter(ap_filter_t *f,
1000     apr_bucket_brigade *bb)
1001     {
1002 nd 104166 int zlib_method;
1003     int zlib_flags;
1004 rpluem 426799 apr_bucket *e;
1005 ianh 103405 request_rec *r = f->r;
1006     deflate_ctx *ctx = f->ctx;
1007     int zRC;
1008     apr_status_t rv;
1009     deflate_filter_config *c;
1010    
1011 niq 104352 /* Do nothing if asked to filter nothing. */
1012     if (APR_BRIGADE_EMPTY(bb)) {
1013 rpluem 424759 return ap_pass_brigade(f->next, bb);
1014 niq 104352 }
1015    
1016 ianh 103405 c = ap_get_module_config(r->server->module_config, &deflate_module);
1017    
1018     if (!ctx) {
1019    
1020     /* only work on main request/no subrequests */
1021 pquerna 105403 if (!ap_is_initial_req(r)) {
1022 ianh 103405 ap_remove_output_filter(f);
1023     return ap_pass_brigade(f->next, bb);
1024     }
1025    
1026 niq 563154 /* We can't operate on Content-Ranges */
1027     if (apr_table_get(r->headers_out, "Content-Range") != NULL) {
1028     ap_remove_output_filter(f);
1029     return ap_pass_brigade(f->next, bb);
1030     }
1031    
1032 rpluem 475922 /*
1033     * Let's see what our current Content-Encoding is.
1034 niq 562507 * Only inflate if gzipped.
1035 ianh 103405 */
1036 niq 563803 if (check_gzip(r, r->headers_out, r->err_headers_out) == 0) {
1037 ianh 103405 ap_remove_output_filter(f);
1038     return ap_pass_brigade(f->next, bb);
1039     }
1040    
1041 niq 104352 /* No need to inflate HEAD or 204/304 */
1042     if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
1043     ap_remove_output_filter(f);
1044     return ap_pass_brigade(f->next, bb);
1045     }
1046    
1047 ianh 103405 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1048 rpluem 426799 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1049 ianh 103405 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1050 rpluem 426799 ctx->libz_end_func = inflateEnd;
1051     ctx->validation_buffer = NULL;
1052     ctx->validation_buffer_length = 0;
1053 ianh 103405
1054     zRC = inflateInit2(&ctx->stream, c->windowSize);
1055    
1056     if (zRC != Z_OK) {
1057     f->ctx = NULL;
1058     inflateEnd(&ctx->stream);
1059     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1060     "unable to init Zlib: "
1061     "inflateInit2 returned %d: URL %s",
1062     zRC, r->uri);
1063 rpluem 426799 /*
1064     * Remove ourselves as it does not make sense to return:
1065     * We are not able to init libz and pass data down the chain
1066     * compressed.
1067     */
1068 ianh 103405 ap_remove_output_filter(f);
1069     return ap_pass_brigade(f->next, bb);
1070     }
1071    
1072 rpluem 426799 /*
1073     * Register a cleanup function to ensure that we cleanup the internal
1074     * libz resources.
1075     */
1076     apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
1077     apr_pool_cleanup_null);
1078    
1079 rpluem 563229 /* these are unlikely to be set anyway, but ... */
1080 rpluem 426799 apr_table_unset(r->headers_out, "Content-Length");
1081 rpluem 563229 apr_table_unset(r->headers_out, "Content-MD5");
1082 niq 607219 deflate_check_etag(r, "gunzip");
1083 rpluem 426799
1084     /* initialize inflate output buffer */
1085 ianh 103405 ctx->stream.next_out = ctx->buffer;
1086     ctx->stream.avail_out = c->bufferSize;
1087    
1088 niq 580598 ctx->inflate_init = 0;
1089 ianh 103405 }
1090    
1091 rpluem 426799 while (!APR_BRIGADE_EMPTY(bb))
1092 ianh 103405 {
1093     const char *data;
1094 rpluem 426799 apr_bucket *b;
1095 ianh 103405 apr_size_t len;
1096    
1097 rpluem 426799 e = APR_BRIGADE_FIRST(bb);
1098    
1099     if (APR_BUCKET_IS_EOS(e)) {
1100 rpluem 475406 /*
1101     * We are really done now. Ensure that we never return here, even
1102     * if a second EOS bucket falls down the chain. Thus remove
1103     * ourselves.
1104     */
1105     ap_remove_output_filter(f);
1106 rpluem 475922 /* should be zero already anyway */
1107     ctx->stream.avail_in = 0;
1108 rpluem 426799 /*
1109     * Flush the remaining data from the zlib buffers. It is correct
1110     * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1111     * deflate case. In the inflate case Z_FINISH requires to have a
1112     * large enough output buffer to put ALL data in otherwise it
1113     * fails, whereas in the deflate case you can empty a filled output
1114     * buffer and call it again until no more output can be created.
1115     */
1116     flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
1117     UPDATE_CRC);
1118     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1119     "Zlib: Inflated %ld to %ld : URL %s",
1120     ctx->stream.total_in, ctx->stream.total_out, r->uri);
1121    
1122     if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1123     unsigned long compCRC, compLen;
1124     compCRC = getLong(ctx->validation_buffer);
1125     if (ctx->crc != compCRC) {
1126     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1127     "Zlib: Checksum of inflated stream invalid");
1128     return APR_EGENERAL;
1129     }
1130     ctx->validation_buffer += VALIDATION_SIZE / 2;
1131     compLen = getLong(ctx->validation_buffer);
1132     if (ctx->stream.total_out != compLen) {
1133     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1134     "Zlib: Length of inflated stream invalid");
1135     return APR_EGENERAL;
1136     }
1137     }
1138     else {
1139     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1140     "Zlib: Validation bytes not present");
1141     return APR_EGENERAL;
1142     }
1143    
1144 ianh 103405 inflateEnd(&ctx->stream);
1145 rpluem 426799 /* No need for cleanup any longer */
1146     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1147    
1148     /* Remove EOS from the old list, and insert into the new. */
1149     APR_BUCKET_REMOVE(e);
1150     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1151    
1152     /*
1153     * Okay, we've seen the EOS.
1154     * Time to pass it along down the chain.
1155     */
1156     return ap_pass_brigade(f->next, ctx->bb);
1157 ianh 103405 }
1158    
1159 rpluem 426799 if (APR_BUCKET_IS_FLUSH(e)) {
1160     apr_status_t rv;
1161    
1162     /* flush the remaining data from the zlib buffers */
1163     zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
1164     Z_SYNC_FLUSH, UPDATE_CRC);
1165     if (zRC != Z_OK) {
1166     return APR_EGENERAL;
1167     }
1168    
1169     /* Remove flush bucket from old brigade anf insert into the new. */
1170     APR_BUCKET_REMOVE(e);
1171     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1172     rv = ap_pass_brigade(f->next, ctx->bb);
1173     if (rv != APR_SUCCESS) {
1174     return rv;
1175     }
1176 niq 416165 continue;
1177 ianh 103405 }
1178    
1179 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
1180     /*
1181     * Remove meta data bucket from old brigade and insert into the
1182     * new.
1183     */
1184     APR_BUCKET_REMOVE(e);
1185     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1186     continue;
1187     }
1188    
1189 ianh 103405 /* read */
1190 rpluem 426799 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1191 ianh 103405
1192     /* first bucket contains zlib header */
1193 niq 580598 if (!ctx->inflate_init++) {
1194 nd 104166 if (len < 10) {
1195 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1196     "Insufficient data for inflate");
1197 nd 104166 return APR_EGENERAL;
1198 jim 332306 }
1199 ianh 103405 else {
1200 nd 104166 zlib_method = data[2];
1201     zlib_flags = data[3];
1202     if (zlib_method != Z_DEFLATED) {
1203 nd 104155 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1204 nd 104166 "inflate: data not deflated!");
1205     ap_remove_output_filter(f);
1206     return ap_pass_brigade(f->next, bb);
1207 niq 104034 }
1208 ianh 103405 if (data[0] != deflate_magic[0] ||
1209 niq 104034 data[1] != deflate_magic[1] ||
1210 nd 104166 (zlib_flags & RESERVED) != 0) {
1211 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1212 nd 104166 "inflate: bad header");
1213 ianh 103405 return APR_EGENERAL ;
1214     }
1215     data += 10 ;
1216     len -= 10 ;
1217     }
1218 nd 104166 if (zlib_flags & EXTRA_FIELD) {
1219     unsigned int bytes = (unsigned int)(data[0]);
1220     bytes += ((unsigned int)(data[1])) << 8;
1221     bytes += 2;
1222     if (len < bytes) {
1223 niq 104034 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1224 nd 104166 "inflate: extra field too big (not "
1225     "supported)");
1226     return APR_EGENERAL;
1227 niq 104034 }
1228 nd 104166 data += bytes;
1229     len -= bytes;
1230 niq 104034 }
1231 nd 104166 if (zlib_flags & ORIG_NAME) {
1232     while (len-- && *data++);
1233 niq 104034 }
1234 nd 104166 if (zlib_flags & COMMENT) {
1235     while (len-- && *data++);
1236 niq 104034 }
1237 nd 104166 if (zlib_flags & HEAD_CRC) {
1238     len -= 2;
1239     data += 2;
1240 niq 104034 }
1241 ianh 103405 }
1242    
1243     /* pass through zlib inflate. */
1244     ctx->stream.next_in = (unsigned char *)data;
1245     ctx->stream.avail_in = len;
1246    
1247 rpluem 426799 if (ctx->validation_buffer) {
1248     if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1249     apr_size_t copy_size;
1250    
1251     copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1252     if (copy_size > ctx->stream.avail_in)
1253     copy_size = ctx->stream.avail_in;
1254 rpluem 475915 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1255     ctx->stream.next_in, copy_size);
1256     /* Saved copy_size bytes */
1257     ctx->stream.avail_in -= copy_size;
1258 niq 476600 ctx->validation_buffer_length += copy_size;
1259 rpluem 475915 }
1260     if (ctx->stream.avail_in) {
1261 rpluem 426799 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1262     "Zlib: %d bytes of garbage at the end of "
1263     "compressed stream.", ctx->stream.avail_in);
1264 rpluem 475915 /*
1265     * There is nothing worth consuming for zlib left, because it is
1266     * either garbage data or the data has been copied to the
1267     * validation buffer (processing validation data is no business
1268     * for zlib). So set ctx->stream.avail_in to zero to indicate
1269     * this to the following while loop.
1270     */
1271     ctx->stream.avail_in = 0;
1272 rpluem 426799 }
1273     }
1274    
1275 ianh 103405 zRC = Z_OK;
1276    
1277     while (ctx->stream.avail_in != 0) {
1278     if (ctx->stream.avail_out == 0) {
1279 rpluem 426799
1280 ianh 103405 ctx->stream.next_out = ctx->buffer;
1281     len = c->bufferSize - ctx->stream.avail_out;
1282    
1283     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1284 rpluem 426799 b = apr_bucket_heap_create((char *)ctx->buffer, len,
1285     NULL, f->c->bucket_alloc);
1286     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
1287 ianh 103405 ctx->stream.avail_out = c->bufferSize;
1288 rpluem 426799 /* Send what we have right now to the next filter. */
1289     rv = ap_pass_brigade(f->next, ctx->bb);
1290     if (rv != APR_SUCCESS) {
1291     return rv;
1292     }
1293 ianh 103405 }
1294    
1295     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1296    
1297     if (zRC == Z_STREAM_END) {
1298 rpluem 426799 /*
1299     * We have inflated all data. Now try to capture the
1300     * validation bytes. We may not have them all available
1301     * right now, but capture what is there.
1302     */
1303     ctx->validation_buffer = apr_pcalloc(f->r->pool,
1304     VALIDATION_SIZE);
1305     if (ctx->stream.avail_in > VALIDATION_SIZE) {
1306     ctx->validation_buffer_length = VALIDATION_SIZE;
1307 rpluem 475920 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1308     "Zlib: %d bytes of garbage at the end of "
1309     "compressed stream.",
1310     ctx->stream.avail_in - VALIDATION_SIZE);
1311 rpluem 426799 } else if (ctx->stream.avail_in > 0) {
1312     ctx->validation_buffer_length = ctx->stream.avail_in;
1313     }
1314     if (ctx->validation_buffer_length)
1315     memcpy(ctx->validation_buffer, ctx->stream.next_in,
1316     ctx->validation_buffer_length);
1317 ianh 103405 break;
1318     }
1319    
1320     if (zRC != Z_OK) {
1321     return APR_EGENERAL;
1322     }
1323     }
1324    
1325 rpluem 426799 apr_bucket_delete(e);
1326 ianh 103405 }
1327    
1328 rpluem 426799 apr_brigade_cleanup(bb);
1329     return APR_SUCCESS;
1330 ianh 103405 }
1331    
1332 niq 416165 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1333 jerenkrantz 94979 static void register_hooks(apr_pool_t *p)
1334     {
1335 jerenkrantz 95906 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1336 jerenkrantz 94979 AP_FTYPE_CONTENT_SET);
1337 ianh 103405 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1338     AP_FTYPE_RESOURCE-1);
1339 jerenkrantz 95906 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1340 jerenkrantz 95345 AP_FTYPE_CONTENT_SET);
1341 jerenkrantz 94979 }
1342    
1343     static const command_rec deflate_filter_cmds[] = {
1344 nd 98149 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1345 jerenkrantz 94979 "Set a note to report on compression ratio"),
1346     AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1347     RSRC_CONF, "Set the Deflate window size (1-15)"),
1348 jerenkrantz 94987 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1349     "Set the Deflate Buffer Size"),
1350 jerenkrantz 94979 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1351     "Set the Deflate Memory Level (1-9)"),
1352 ianh 98982 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1353 ianh 98895 "Set the Deflate Compression Level (1-9)"),
1354 jerenkrantz 94979 {NULL}
1355     };
1356    
1357     module AP_MODULE_DECLARE_DATA deflate_module = {
1358     STANDARD20_MODULE_STUFF,
1359     NULL, /* dir config creater */
1360     NULL, /* dir merger --- default is to override */
1361     create_deflate_server_config, /* server config */
1362     NULL, /* merge server config */
1363     deflate_filter_cmds, /* command table */
1364     register_hooks /* register hooks */
1365     };

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2