/[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 726791 - (hide annotations)
Mon Dec 15 20:22:07 2008 UTC (11 months, 1 week ago) by rpluem
File MIME type: text/plain
File size: 49270 byte(s)
*  Fix r->content_encoding in deflate_out_filter if it was set before.
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 rpluem 726791 /* Fix r->content_encoding if it was set before */
588     if (r->content_encoding) {
589     r->content_encoding = apr_table_get(r->headers_out,
590     "Content-Encoding");
591     }
592 jerenkrantz 94979 apr_table_unset(r->headers_out, "Content-Length");
593 rpluem 563230 apr_table_unset(r->headers_out, "Content-MD5");
594 niq 607219 deflate_check_etag(r, "gzip");
595 jerenkrantz 95521
596     /* initialize deflate output buffer */
597     ctx->stream.next_out = ctx->buffer;
598     ctx->stream.avail_out = c->bufferSize;
599 jerenkrantz 94979 }
600 jim 332306
601 jorton 103810 while (!APR_BRIGADE_EMPTY(bb))
602 jwoolley 101788 {
603 jerenkrantz 94979 const char *data;
604     apr_bucket *b;
605     apr_size_t len;
606    
607 jorton 103810 e = APR_BRIGADE_FIRST(bb);
608    
609 jerenkrantz 94979 if (APR_BUCKET_IS_EOS(e)) {
610 jwoolley 95413 char *buf;
611 jerenkrantz 94979
612     ctx->stream.avail_in = 0; /* should be zero already anyway */
613 rpluem 422731 /* flush the remaining data from the zlib buffers */
614 rpluem 426790 flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
615     NO_UPDATE_CRC);
616 jerenkrantz 94979
617 rpluem 426791 buf = apr_palloc(r->pool, VALIDATION_SIZE);
618 jwoolley 95413 putLong((unsigned char *)&buf[0], ctx->crc);
619     putLong((unsigned char *)&buf[4], ctx->stream.total_in);
620 jerenkrantz 94979
621 rpluem 426791 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
622     f->c->bucket_alloc);
623 jerenkrantz 94979 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
624 trawick 95151 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
625 jerenkrantz 94979 "Zlib: Compressed %ld to %ld : URL %s",
626     ctx->stream.total_in, ctx->stream.total_out, r->uri);
627    
628 nd 98154 /* leave notes for logging */
629     if (c->note_input_name) {
630     apr_table_setn(r->notes, c->note_input_name,
631     (ctx->stream.total_in > 0)
632     ? apr_off_t_toa(r->pool,
633     ctx->stream.total_in)
634     : "-");
635     }
636 jerenkrantz 94979
637 nd 98154 if (c->note_output_name) {
638     apr_table_setn(r->notes, c->note_output_name,
639     (ctx->stream.total_in > 0)
640     ? apr_off_t_toa(r->pool,
641     ctx->stream.total_out)
642     : "-");
643     }
644    
645     if (c->note_ratio_name) {
646     apr_table_setn(r->notes, c->note_ratio_name,
647     (ctx->stream.total_in > 0)
648     ? apr_itoa(r->pool,
649     (int)(ctx->stream.total_out
650     * 100
651     / ctx->stream.total_in))
652     : "-");
653     }
654    
655 jerenkrantz 94979 deflateEnd(&ctx->stream);
656 rpluem 425109 /* No need for cleanup any longer */
657     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
658 jerenkrantz 94979
659     /* Remove EOS from the old list, and insert into the new. */
660     APR_BUCKET_REMOVE(e);
661     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
662    
663     /* Okay, we've seen the EOS.
664     * Time to pass it along down the chain.
665     */
666     return ap_pass_brigade(f->next, ctx->bb);
667     }
668    
669     if (APR_BUCKET_IS_FLUSH(e)) {
670 jerenkrantz 100730 apr_status_t rv;
671 jorton 103810
672 rpluem 422731 /* flush the remaining data from the zlib buffers */
673 rpluem 423940 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
674 rpluem 426790 Z_SYNC_FLUSH, NO_UPDATE_CRC);
675 rpluem 422731 if (zRC != Z_OK) {
676     return APR_EGENERAL;
677 jerenkrantz 95341 }
678 jerenkrantz 94979
679 rpluem 422731 /* Remove flush bucket from old brigade anf insert into the new. */
680     APR_BUCKET_REMOVE(e);
681     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
682 jerenkrantz 100730 rv = ap_pass_brigade(f->next, ctx->bb);
683     if (rv != APR_SUCCESS) {
684     return rv;
685     }
686 jerenkrantz 94979 continue;
687     }
688    
689 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
690     /*
691     * Remove meta data bucket from old brigade and insert into the
692     * new.
693     */
694     APR_BUCKET_REMOVE(e);
695     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
696     continue;
697 gregames 554011 }
698    
699 jerenkrantz 94979 /* read */
700     apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
701    
702     /* This crc32 function is from zlib. */
703     ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
704    
705     /* write */
706     ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
707     * but we'll just have to
708     * trust zlib */
709     ctx->stream.avail_in = len;
710    
711     while (ctx->stream.avail_in != 0) {
712     if (ctx->stream.avail_out == 0) {
713 jerenkrantz 100730 apr_status_t rv;
714    
715 jerenkrantz 94979 ctx->stream.next_out = ctx->buffer;
716 jerenkrantz 94987 len = c->bufferSize - ctx->stream.avail_out;
717 jerenkrantz 94979
718     b = apr_bucket_heap_create((char *)ctx->buffer, len,
719     NULL, f->c->bucket_alloc);
720     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
721 jerenkrantz 94987 ctx->stream.avail_out = c->bufferSize;
722 jerenkrantz 100730 /* Send what we have right now to the next filter. */
723     rv = ap_pass_brigade(f->next, ctx->bb);
724     if (rv != APR_SUCCESS) {
725     return rv;
726     }
727 jerenkrantz 94979 }
728    
729     zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
730    
731 rpluem 422736 if (zRC != Z_OK) {
732 jerenkrantz 94979 return APR_EGENERAL;
733 rpluem 422736 }
734 jerenkrantz 94979 }
735 jorton 103810
736     apr_bucket_delete(e);
737 jerenkrantz 94979 }
738    
739 jwoolley 97526 apr_brigade_cleanup(bb);
740 jerenkrantz 94979 return APR_SUCCESS;
741     }
742    
743 jerenkrantz 95345 /* This is the deflate input filter (inflates). */
744     static apr_status_t deflate_in_filter(ap_filter_t *f,
745     apr_bucket_brigade *bb,
746     ap_input_mode_t mode,
747     apr_read_type_e block,
748     apr_off_t readbytes)
749     {
750     apr_bucket *bkt;
751     request_rec *r = f->r;
752     deflate_ctx *ctx = f->ctx;
753     int zRC;
754     apr_status_t rv;
755     deflate_filter_config *c;
756    
757     /* just get out of the way of things we don't want. */
758     if (mode != AP_MODE_READBYTES) {
759     return ap_get_brigade(f->next, bb, mode, block, readbytes);
760     }
761    
762     c = ap_get_module_config(r->server->module_config, &deflate_module);
763    
764     if (!ctx) {
765 niq 562507 char deflate_hdr[10];
766 jerenkrantz 95345 apr_size_t len;
767    
768     /* only work on main request/no subrequests */
769 pquerna 105403 if (!ap_is_initial_req(r)) {
770 jerenkrantz 95345 ap_remove_input_filter(f);
771     return ap_get_brigade(f->next, bb, mode, block, readbytes);
772     }
773    
774 niq 563154 /* We can't operate on Content-Ranges */
775     if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
776     ap_remove_input_filter(f);
777     return ap_get_brigade(f->next, bb, mode, block, readbytes);
778     }
779    
780 niq 560689 /* Check whether request body is gzipped.
781     *
782     * If it is, we're transforming the contents, invalidating
783     * some request headers including Content-Encoding.
784     *
785     * If not, we just remove ourself.
786 jerenkrantz 95345 */
787 niq 563803 if (check_gzip(r, r->headers_in, NULL) == 0) {
788 jerenkrantz 95345 ap_remove_input_filter(f);
789     return ap_get_brigade(f->next, bb, mode, block, readbytes);
790     }
791    
792     f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
793     ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
794     ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
795     ctx->buffer = apr_palloc(r->pool, c->bufferSize);
796    
797     rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
798     if (rv != APR_SUCCESS) {
799     return rv;
800     }
801 jerenkrantz 98689
802 niq 563133 apr_table_unset(r->headers_in, "Content-Length");
803     apr_table_unset(r->headers_in, "Content-MD5");
804    
805 jim 332306 len = 10;
806     rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
807 jerenkrantz 95345 if (rv != APR_SUCCESS) {
808     return rv;
809     }
810    
811     /* We didn't get the magic bytes. */
812     if (len != 10 ||
813     deflate_hdr[0] != deflate_magic[0] ||
814     deflate_hdr[1] != deflate_magic[1]) {
815     return APR_EGENERAL;
816     }
817    
818     /* We can't handle flags for now. */
819     if (deflate_hdr[3] != 0) {
820     return APR_EGENERAL;
821     }
822    
823     zRC = inflateInit2(&ctx->stream, c->windowSize);
824    
825     if (zRC != Z_OK) {
826     f->ctx = NULL;
827 jerenkrantz 98689 inflateEnd(&ctx->stream);
828 jerenkrantz 95345 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
829     "unable to init Zlib: "
830     "inflateInit2 returned %d: URL %s",
831     zRC, r->uri);
832     ap_remove_input_filter(f);
833     return ap_get_brigade(f->next, bb, mode, block, readbytes);
834     }
835    
836     /* initialize deflate output buffer */
837     ctx->stream.next_out = ctx->buffer;
838     ctx->stream.avail_out = c->bufferSize;
839    
840     apr_brigade_cleanup(ctx->bb);
841     }
842    
843     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
844     rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
845    
846     if (rv != APR_SUCCESS) {
847 jerenkrantz 98689 /* What about APR_EAGAIN errors? */
848     inflateEnd(&ctx->stream);
849 jerenkrantz 95345 return rv;
850     }
851    
852 jwoolley 101788 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
853     bkt != APR_BRIGADE_SENTINEL(ctx->bb);
854     bkt = APR_BUCKET_NEXT(bkt))
855     {
856 jerenkrantz 95345 const char *data;
857     apr_size_t len;
858    
859     /* If we actually see the EOS, that means we screwed up! */
860     if (APR_BUCKET_IS_EOS(bkt)) {
861 jerenkrantz 98689 inflateEnd(&ctx->stream);
862 jerenkrantz 95345 return APR_EGENERAL;
863     }
864    
865     if (APR_BUCKET_IS_FLUSH(bkt)) {
866     apr_bucket *tmp_heap;
867     zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
868     if (zRC != Z_OK) {
869 jerenkrantz 98689 inflateEnd(&ctx->stream);
870 jerenkrantz 95345 return APR_EGENERAL;
871     }
872    
873     ctx->stream.next_out = ctx->buffer;
874     len = c->bufferSize - ctx->stream.avail_out;
875    
876     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
877     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
878     NULL, f->c->bucket_alloc);
879     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
880     ctx->stream.avail_out = c->bufferSize;
881    
882     /* Move everything to the returning brigade. */
883     APR_BUCKET_REMOVE(bkt);
884     APR_BRIGADE_CONCAT(bb, ctx->bb);
885     break;
886     }
887    
888     /* read */
889     apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
890    
891     /* pass through zlib inflate. */
892     ctx->stream.next_in = (unsigned char *)data;
893     ctx->stream.avail_in = len;
894    
895 jerenkrantz 95359 zRC = Z_OK;
896    
897 jerenkrantz 95345 while (ctx->stream.avail_in != 0) {
898     if (ctx->stream.avail_out == 0) {
899     apr_bucket *tmp_heap;
900     ctx->stream.next_out = ctx->buffer;
901     len = c->bufferSize - ctx->stream.avail_out;
902    
903     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
904     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
905     NULL, f->c->bucket_alloc);
906     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
907     ctx->stream.avail_out = c->bufferSize;
908     }
909    
910     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
911    
912     if (zRC == Z_STREAM_END) {
913     break;
914     }
915    
916     if (zRC != Z_OK) {
917 jerenkrantz 98689 inflateEnd(&ctx->stream);
918 jerenkrantz 95345 return APR_EGENERAL;
919     }
920     }
921     if (zRC == Z_STREAM_END) {
922     apr_bucket *tmp_heap, *eos;
923    
924     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
925     "Zlib: Inflated %ld to %ld : URL %s",
926     ctx->stream.total_in, ctx->stream.total_out,
927     r->uri);
928    
929     len = c->bufferSize - ctx->stream.avail_out;
930    
931     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
932     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
933     NULL, f->c->bucket_alloc);
934     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
935     ctx->stream.avail_out = c->bufferSize;
936    
937     /* Is the remaining 8 bytes already in the avail stream? */
938     if (ctx->stream.avail_in >= 8) {
939     unsigned long compCRC, compLen;
940     compCRC = getLong(ctx->stream.next_in);
941     if (ctx->crc != compCRC) {
942 jerenkrantz 98689 inflateEnd(&ctx->stream);
943 jerenkrantz 95345 return APR_EGENERAL;
944     }
945     ctx->stream.next_in += 4;
946     compLen = getLong(ctx->stream.next_in);
947     if (ctx->stream.total_out != compLen) {
948 jerenkrantz 98689 inflateEnd(&ctx->stream);
949 jerenkrantz 95345 return APR_EGENERAL;
950     }
951     }
952     else {
953     /* FIXME: We need to grab the 8 verification bytes
954     * from the wire! */
955 jerenkrantz 98689 inflateEnd(&ctx->stream);
956 jerenkrantz 95345 return APR_EGENERAL;
957     }
958    
959     inflateEnd(&ctx->stream);
960 jerenkrantz 98689
961 jerenkrantz 95345 eos = apr_bucket_eos_create(f->c->bucket_alloc);
962 jim 332306 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
963 jerenkrantz 95345 break;
964     }
965 jerenkrantz 95666
966 jerenkrantz 95345 }
967 jerenkrantz 95521 apr_brigade_cleanup(ctx->bb);
968 jerenkrantz 95345 }
969    
970 jerenkrantz 95666 /* If we are about to return nothing for a 'blocking' read and we have
971     * some data in our zlib buffer, flush it out so we can return something.
972     */
973     if (block == APR_BLOCK_READ &&
974     APR_BRIGADE_EMPTY(ctx->proc_bb) &&
975     ctx->stream.avail_out < c->bufferSize) {
976     apr_bucket *tmp_heap;
977     apr_size_t len;
978     ctx->stream.next_out = ctx->buffer;
979     len = c->bufferSize - ctx->stream.avail_out;
980    
981     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
982     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
983     NULL, f->c->bucket_alloc);
984     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
985     ctx->stream.avail_out = c->bufferSize;
986     }
987    
988 jerenkrantz 95345 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
989     apr_bucket_brigade *newbb;
990    
991     /* May return APR_INCOMPLETE which is fine by us. */
992     apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
993    
994     newbb = apr_brigade_split(ctx->proc_bb, bkt);
995     APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
996     APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
997     }
998    
999     return APR_SUCCESS;
1000     }
1001    
1002 ianh 103405
1003     /* Filter to inflate for a content-transforming proxy. */
1004     static apr_status_t inflate_out_filter(ap_filter_t *f,
1005     apr_bucket_brigade *bb)
1006     {
1007 nd 104166 int zlib_method;
1008     int zlib_flags;
1009 rpluem 426799 apr_bucket *e;
1010 ianh 103405 request_rec *r = f->r;
1011     deflate_ctx *ctx = f->ctx;
1012     int zRC;
1013     apr_status_t rv;
1014     deflate_filter_config *c;
1015    
1016 niq 104352 /* Do nothing if asked to filter nothing. */
1017     if (APR_BRIGADE_EMPTY(bb)) {
1018 rpluem 424759 return ap_pass_brigade(f->next, bb);
1019 niq 104352 }
1020    
1021 ianh 103405 c = ap_get_module_config(r->server->module_config, &deflate_module);
1022    
1023     if (!ctx) {
1024    
1025     /* only work on main request/no subrequests */
1026 pquerna 105403 if (!ap_is_initial_req(r)) {
1027 ianh 103405 ap_remove_output_filter(f);
1028     return ap_pass_brigade(f->next, bb);
1029     }
1030    
1031 niq 563154 /* We can't operate on Content-Ranges */
1032     if (apr_table_get(r->headers_out, "Content-Range") != NULL) {
1033     ap_remove_output_filter(f);
1034     return ap_pass_brigade(f->next, bb);
1035     }
1036    
1037 rpluem 475922 /*
1038     * Let's see what our current Content-Encoding is.
1039 niq 562507 * Only inflate if gzipped.
1040 ianh 103405 */
1041 niq 563803 if (check_gzip(r, r->headers_out, r->err_headers_out) == 0) {
1042 ianh 103405 ap_remove_output_filter(f);
1043     return ap_pass_brigade(f->next, bb);
1044     }
1045    
1046 niq 104352 /* No need to inflate HEAD or 204/304 */
1047     if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
1048     ap_remove_output_filter(f);
1049     return ap_pass_brigade(f->next, bb);
1050     }
1051    
1052 ianh 103405 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1053 rpluem 426799 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1054 ianh 103405 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1055 rpluem 426799 ctx->libz_end_func = inflateEnd;
1056     ctx->validation_buffer = NULL;
1057     ctx->validation_buffer_length = 0;
1058 ianh 103405
1059     zRC = inflateInit2(&ctx->stream, c->windowSize);
1060    
1061     if (zRC != Z_OK) {
1062     f->ctx = NULL;
1063     inflateEnd(&ctx->stream);
1064     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1065     "unable to init Zlib: "
1066     "inflateInit2 returned %d: URL %s",
1067     zRC, r->uri);
1068 rpluem 426799 /*
1069     * Remove ourselves as it does not make sense to return:
1070     * We are not able to init libz and pass data down the chain
1071     * compressed.
1072     */
1073 ianh 103405 ap_remove_output_filter(f);
1074     return ap_pass_brigade(f->next, bb);
1075     }
1076    
1077 rpluem 426799 /*
1078     * Register a cleanup function to ensure that we cleanup the internal
1079     * libz resources.
1080     */
1081     apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
1082     apr_pool_cleanup_null);
1083    
1084 rpluem 563229 /* these are unlikely to be set anyway, but ... */
1085 rpluem 426799 apr_table_unset(r->headers_out, "Content-Length");
1086 rpluem 563229 apr_table_unset(r->headers_out, "Content-MD5");
1087 niq 607219 deflate_check_etag(r, "gunzip");
1088 rpluem 426799
1089     /* initialize inflate output buffer */
1090 ianh 103405 ctx->stream.next_out = ctx->buffer;
1091     ctx->stream.avail_out = c->bufferSize;
1092    
1093 niq 580598 ctx->inflate_init = 0;
1094 ianh 103405 }
1095    
1096 rpluem 426799 while (!APR_BRIGADE_EMPTY(bb))
1097 ianh 103405 {
1098     const char *data;
1099 rpluem 426799 apr_bucket *b;
1100 ianh 103405 apr_size_t len;
1101    
1102 rpluem 426799 e = APR_BRIGADE_FIRST(bb);
1103    
1104     if (APR_BUCKET_IS_EOS(e)) {
1105 rpluem 475406 /*
1106     * We are really done now. Ensure that we never return here, even
1107     * if a second EOS bucket falls down the chain. Thus remove
1108     * ourselves.
1109     */
1110     ap_remove_output_filter(f);
1111 rpluem 475922 /* should be zero already anyway */
1112     ctx->stream.avail_in = 0;
1113 rpluem 426799 /*
1114     * Flush the remaining data from the zlib buffers. It is correct
1115     * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1116     * deflate case. In the inflate case Z_FINISH requires to have a
1117     * large enough output buffer to put ALL data in otherwise it
1118     * fails, whereas in the deflate case you can empty a filled output
1119     * buffer and call it again until no more output can be created.
1120     */
1121     flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
1122     UPDATE_CRC);
1123     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1124     "Zlib: Inflated %ld to %ld : URL %s",
1125     ctx->stream.total_in, ctx->stream.total_out, r->uri);
1126    
1127     if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1128     unsigned long compCRC, compLen;
1129     compCRC = getLong(ctx->validation_buffer);
1130     if (ctx->crc != compCRC) {
1131     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1132     "Zlib: Checksum of inflated stream invalid");
1133     return APR_EGENERAL;
1134     }
1135     ctx->validation_buffer += VALIDATION_SIZE / 2;
1136     compLen = getLong(ctx->validation_buffer);
1137     if (ctx->stream.total_out != compLen) {
1138     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1139     "Zlib: Length of inflated stream invalid");
1140     return APR_EGENERAL;
1141     }
1142     }
1143     else {
1144     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1145     "Zlib: Validation bytes not present");
1146     return APR_EGENERAL;
1147     }
1148    
1149 ianh 103405 inflateEnd(&ctx->stream);
1150 rpluem 426799 /* No need for cleanup any longer */
1151     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1152    
1153     /* Remove EOS from the old list, and insert into the new. */
1154     APR_BUCKET_REMOVE(e);
1155     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1156    
1157     /*
1158     * Okay, we've seen the EOS.
1159     * Time to pass it along down the chain.
1160     */
1161     return ap_pass_brigade(f->next, ctx->bb);
1162 ianh 103405 }
1163    
1164 rpluem 426799 if (APR_BUCKET_IS_FLUSH(e)) {
1165     apr_status_t rv;
1166    
1167     /* flush the remaining data from the zlib buffers */
1168     zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
1169     Z_SYNC_FLUSH, UPDATE_CRC);
1170     if (zRC != Z_OK) {
1171     return APR_EGENERAL;
1172     }
1173    
1174     /* Remove flush bucket from old brigade anf insert into the new. */
1175     APR_BUCKET_REMOVE(e);
1176     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1177     rv = ap_pass_brigade(f->next, ctx->bb);
1178     if (rv != APR_SUCCESS) {
1179     return rv;
1180     }
1181 niq 416165 continue;
1182 ianh 103405 }
1183    
1184 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
1185     /*
1186     * Remove meta data bucket from old brigade and insert into the
1187     * new.
1188     */
1189     APR_BUCKET_REMOVE(e);
1190     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1191     continue;
1192     }
1193    
1194 ianh 103405 /* read */
1195 rpluem 426799 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1196 ianh 103405
1197     /* first bucket contains zlib header */
1198 niq 580598 if (!ctx->inflate_init++) {
1199 nd 104166 if (len < 10) {
1200 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1201     "Insufficient data for inflate");
1202 nd 104166 return APR_EGENERAL;
1203 jim 332306 }
1204 ianh 103405 else {
1205 nd 104166 zlib_method = data[2];
1206     zlib_flags = data[3];
1207     if (zlib_method != Z_DEFLATED) {
1208 nd 104155 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1209 nd 104166 "inflate: data not deflated!");
1210     ap_remove_output_filter(f);
1211     return ap_pass_brigade(f->next, bb);
1212 niq 104034 }
1213 ianh 103405 if (data[0] != deflate_magic[0] ||
1214 niq 104034 data[1] != deflate_magic[1] ||
1215 nd 104166 (zlib_flags & RESERVED) != 0) {
1216 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1217 nd 104166 "inflate: bad header");
1218 ianh 103405 return APR_EGENERAL ;
1219     }
1220     data += 10 ;
1221     len -= 10 ;
1222     }
1223 nd 104166 if (zlib_flags & EXTRA_FIELD) {
1224     unsigned int bytes = (unsigned int)(data[0]);
1225     bytes += ((unsigned int)(data[1])) << 8;
1226     bytes += 2;
1227     if (len < bytes) {
1228 niq 104034 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1229 nd 104166 "inflate: extra field too big (not "
1230     "supported)");
1231     return APR_EGENERAL;
1232 niq 104034 }
1233 nd 104166 data += bytes;
1234     len -= bytes;
1235 niq 104034 }
1236 nd 104166 if (zlib_flags & ORIG_NAME) {
1237     while (len-- && *data++);
1238 niq 104034 }
1239 nd 104166 if (zlib_flags & COMMENT) {
1240     while (len-- && *data++);
1241 niq 104034 }
1242 nd 104166 if (zlib_flags & HEAD_CRC) {
1243     len -= 2;
1244     data += 2;
1245 niq 104034 }
1246 ianh 103405 }
1247    
1248     /* pass through zlib inflate. */
1249     ctx->stream.next_in = (unsigned char *)data;
1250     ctx->stream.avail_in = len;
1251    
1252 rpluem 426799 if (ctx->validation_buffer) {
1253     if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1254     apr_size_t copy_size;
1255    
1256     copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1257     if (copy_size > ctx->stream.avail_in)
1258     copy_size = ctx->stream.avail_in;
1259 rpluem 475915 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1260     ctx->stream.next_in, copy_size);
1261     /* Saved copy_size bytes */
1262     ctx->stream.avail_in -= copy_size;
1263 niq 476600 ctx->validation_buffer_length += copy_size;
1264 rpluem 475915 }
1265     if (ctx->stream.avail_in) {
1266 rpluem 426799 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1267     "Zlib: %d bytes of garbage at the end of "
1268     "compressed stream.", ctx->stream.avail_in);
1269 rpluem 475915 /*
1270     * There is nothing worth consuming for zlib left, because it is
1271     * either garbage data or the data has been copied to the
1272     * validation buffer (processing validation data is no business
1273     * for zlib). So set ctx->stream.avail_in to zero to indicate
1274     * this to the following while loop.
1275     */
1276     ctx->stream.avail_in = 0;
1277 rpluem 426799 }
1278     }
1279    
1280 ianh 103405 zRC = Z_OK;
1281    
1282     while (ctx->stream.avail_in != 0) {
1283     if (ctx->stream.avail_out == 0) {
1284 rpluem 426799
1285 ianh 103405 ctx->stream.next_out = ctx->buffer;
1286     len = c->bufferSize - ctx->stream.avail_out;
1287    
1288     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1289 rpluem 426799 b = apr_bucket_heap_create((char *)ctx->buffer, len,
1290     NULL, f->c->bucket_alloc);
1291     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
1292 ianh 103405 ctx->stream.avail_out = c->bufferSize;
1293 rpluem 426799 /* Send what we have right now to the next filter. */
1294     rv = ap_pass_brigade(f->next, ctx->bb);
1295     if (rv != APR_SUCCESS) {
1296     return rv;
1297     }
1298 ianh 103405 }
1299    
1300     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1301    
1302     if (zRC == Z_STREAM_END) {
1303 rpluem 426799 /*
1304     * We have inflated all data. Now try to capture the
1305     * validation bytes. We may not have them all available
1306     * right now, but capture what is there.
1307     */
1308     ctx->validation_buffer = apr_pcalloc(f->r->pool,
1309     VALIDATION_SIZE);
1310     if (ctx->stream.avail_in > VALIDATION_SIZE) {
1311     ctx->validation_buffer_length = VALIDATION_SIZE;
1312 rpluem 475920 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1313     "Zlib: %d bytes of garbage at the end of "
1314     "compressed stream.",
1315     ctx->stream.avail_in - VALIDATION_SIZE);
1316 rpluem 426799 } else if (ctx->stream.avail_in > 0) {
1317     ctx->validation_buffer_length = ctx->stream.avail_in;
1318     }
1319     if (ctx->validation_buffer_length)
1320     memcpy(ctx->validation_buffer, ctx->stream.next_in,
1321     ctx->validation_buffer_length);
1322 ianh 103405 break;
1323     }
1324    
1325     if (zRC != Z_OK) {
1326     return APR_EGENERAL;
1327     }
1328     }
1329    
1330 rpluem 426799 apr_bucket_delete(e);
1331 ianh 103405 }
1332    
1333 rpluem 426799 apr_brigade_cleanup(bb);
1334     return APR_SUCCESS;
1335 ianh 103405 }
1336    
1337 niq 416165 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1338 jerenkrantz 94979 static void register_hooks(apr_pool_t *p)
1339     {
1340 jerenkrantz 95906 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1341 jerenkrantz 94979 AP_FTYPE_CONTENT_SET);
1342 ianh 103405 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1343     AP_FTYPE_RESOURCE-1);
1344 jerenkrantz 95906 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1345 jerenkrantz 95345 AP_FTYPE_CONTENT_SET);
1346 jerenkrantz 94979 }
1347    
1348     static const command_rec deflate_filter_cmds[] = {
1349 nd 98149 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1350 jerenkrantz 94979 "Set a note to report on compression ratio"),
1351     AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1352     RSRC_CONF, "Set the Deflate window size (1-15)"),
1353 jerenkrantz 94987 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1354     "Set the Deflate Buffer Size"),
1355 jerenkrantz 94979 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1356     "Set the Deflate Memory Level (1-9)"),
1357 ianh 98982 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1358 ianh 98895 "Set the Deflate Compression Level (1-9)"),
1359 jerenkrantz 94979 {NULL}
1360     };
1361    
1362     module AP_MODULE_DECLARE_DATA deflate_module = {
1363     STANDARD20_MODULE_STUFF,
1364     NULL, /* dir config creater */
1365     NULL, /* dir merger --- default is to override */
1366     create_deflate_server_config, /* server config */
1367     NULL, /* merge server config */
1368     deflate_filter_cmds, /* command table */
1369     register_hooks /* register hooks */
1370     };

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2