/[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 580598 - (hide annotations)
Sat Sep 29 15:43:08 2007 UTC (2 years, 1 month ago) by niq
File MIME type: text/plain
File size: 48219 byte(s)
mod_deflate: initialise inflate-out filter correctly when the
first brigade contains no data buckets.
PR 43512
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    
376 jerenkrantz 94979 static apr_status_t deflate_out_filter(ap_filter_t *f,
377     apr_bucket_brigade *bb)
378     {
379     apr_bucket *e;
380     request_rec *r = f->r;
381     deflate_ctx *ctx = f->ctx;
382     int zRC;
383 rpluem 426795 deflate_filter_config *c;
384 jerenkrantz 94979
385 niq 104352 /* Do nothing if asked to filter nothing. */
386     if (APR_BRIGADE_EMPTY(bb)) {
387 rpluem 424759 return ap_pass_brigade(f->next, bb);
388 niq 104352 }
389    
390 rpluem 426795 c = ap_get_module_config(r->server->module_config,
391     &deflate_module);
392    
393 jerenkrantz 94979 /* If we don't have a context, we need to ensure that it is okay to send
394     * the deflated content. If we have a context, that means we've done
395     * this before and we liked it.
396     * This could be not so nice if we always fail. But, if we succeed,
397     * we're in better shape.
398     */
399     if (!ctx) {
400 jorton 105410 char *token;
401 niq 104352 const char *encoding;
402 jerenkrantz 94979
403     /* only work on main request/no subrequests */
404 colm 395079 if (r->main != NULL) {
405 jerenkrantz 94982 ap_remove_output_filter(f);
406 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
407     }
408    
409     /* some browsers might have problems, so set no-gzip
410     * (with browsermatch) for them
411     */
412     if (apr_table_get(r->subprocess_env, "no-gzip")) {
413 jerenkrantz 94982 ap_remove_output_filter(f);
414 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
415     }
416    
417 niq 563154 /* We can't operate on Content-Ranges */
418     if (apr_table_get(r->headers_out, "Content-Range") != NULL) {
419     ap_remove_output_filter(f);
420     return ap_pass_brigade(f->next, bb);
421     }
422    
423 jerenkrantz 94979 /* Some browsers might have problems with content types
424     * other than text/html, so set gzip-only-text/html
425     * (with browsermatch) for them
426     */
427 ianh 96318 if (r->content_type == NULL
428     || strncmp(r->content_type, "text/html", 9)) {
429     const char *env_value = apr_table_get(r->subprocess_env,
430     "gzip-only-text/html");
431 ianh 96588 if ( env_value && (strcmp(env_value,"1") == 0) ) {
432 ianh 96318 ap_remove_output_filter(f);
433 ianh 96590 return ap_pass_brigade(f->next, bb);
434 jim 332306 }
435 jerenkrantz 94979 }
436    
437 jerenkrantz 95339 /* Let's see what our current Content-Encoding is.
438 nd 101019 * If it's already encoded, don't compress again.
439     * (We could, but let's not.)
440 jerenkrantz 95339 */
441     encoding = apr_table_get(r->headers_out, "Content-Encoding");
442     if (encoding) {
443 nd 98948 const char *err_enc;
444    
445     err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
446     if (err_enc) {
447     encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
448     }
449     }
450     else {
451     encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
452     }
453    
454 nd 99880 if (r->content_encoding) {
455     encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
456     r->content_encoding, NULL)
457     : r->content_encoding;
458     }
459    
460 nd 98948 if (encoding) {
461 jerenkrantz 95339 const char *tmp = encoding;
462    
463     token = ap_get_token(r->pool, &tmp, 0);
464 nd 101019 while (token && *token) {
465     /* stolen from mod_negotiation: */
466     if (strcmp(token, "identity") && strcmp(token, "7bit") &&
467     strcmp(token, "8bit") && strcmp(token, "binary")) {
468    
469 jerenkrantz 95339 ap_remove_output_filter(f);
470 ianh 103405 return ap_pass_brigade(f->next, bb);
471 jerenkrantz 95339 }
472 nd 101019
473 jerenkrantz 95339 /* Otherwise, skip token */
474 nd 101019 if (*tmp) {
475     ++tmp;
476     }
477     token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
478 jerenkrantz 95339 }
479 ianh 95181 }
480    
481 jerenkrantz 97473 /* Even if we don't accept this request based on it not having
482     * the Accept-Encoding, we need to note that we were looking
483     * for this header and downstream proxies should be aware of that.
484     */
485 pquerna 161691 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
486 jerenkrantz 97473
487 ianh 103405 /* force-gzip will just force it out regardless if the browser
488     * can actually do anything with it.
489     */
490 jerenkrantz 103803 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
491 ianh 103405 const char *accepts;
492     /* if they don't have the line, then they can't play */
493     accepts = apr_table_get(r->headers_in, "Accept-Encoding");
494     if (accepts == NULL) {
495     ap_remove_output_filter(f);
496     return ap_pass_brigade(f->next, bb);
497 nd 101015 }
498    
499 ianh 103405 token = ap_get_token(r->pool, &accepts, 0);
500     while (token && token[0] && strcasecmp(token, "gzip")) {
501     /* skip parameters, XXX: ;q=foo evaluation? */
502 jim 332306 while (*accepts == ';') {
503 ianh 103405 ++accepts;
504     token = ap_get_token(r->pool, &accepts, 1);
505     }
506    
507     /* retrieve next token */
508     if (*accepts == ',') {
509     ++accepts;
510     }
511     token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
512 nd 101015 }
513 jerenkrantz 94979
514 ianh 103405 /* No acceptable token found. */
515     if (token == NULL || token[0] == '\0') {
516     ap_remove_output_filter(f);
517     return ap_pass_brigade(f->next, bb);
518     }
519 jerenkrantz 94979 }
520 ake 103910
521 jorton 109500 /* For a 304 or 204 response there is no entity included in
522     * the response and hence nothing to deflate. */
523     if (r->status == HTTP_NOT_MODIFIED || r->status == HTTP_NO_CONTENT) {
524 niq 104352 ap_remove_output_filter(f);
525 ake 103910 return ap_pass_brigade(f->next, bb);
526     }
527    
528 jerenkrantz 94979 /* We're cool with filtering this. */
529     ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
530     ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
531 jerenkrantz 94987 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
532 rpluem 425109 ctx->libz_end_func = deflateEnd;
533 jerenkrantz 95339
534 ianh 98895 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
535 jerenkrantz 94979 c->windowSize, c->memlevel,
536     Z_DEFAULT_STRATEGY);
537    
538     if (zRC != Z_OK) {
539 rpluem 426795 deflateEnd(&ctx->stream);
540 trawick 95151 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
541 jerenkrantz 94979 "unable to init Zlib: "
542     "deflateInit2 returned %d: URL %s",
543     zRC, r->uri);
544 rpluem 422739 /*
545     * Remove ourselves as it does not make sense to return:
546     * We are not able to init libz and pass data down the chain
547     * uncompressed.
548     */
549     ap_remove_output_filter(f);
550 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
551     }
552 rpluem 425109 /*
553     * Register a cleanup function to ensure that we cleanup the internal
554     * libz resources.
555     */
556     apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
557     apr_pool_cleanup_null);
558 jerenkrantz 94979
559 jorton 105410 /* add immortal gzip header */
560     e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
561     f->c->bucket_alloc);
562 jerenkrantz 94979 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
563    
564 jerenkrantz 95339 /* If the entire Content-Encoding is "identity", we can replace it. */
565     if (!encoding || !strcasecmp(encoding, "identity")) {
566     apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
567     }
568     else {
569     apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
570     }
571 jerenkrantz 94979 apr_table_unset(r->headers_out, "Content-Length");
572 rpluem 563230 apr_table_unset(r->headers_out, "Content-MD5");
573 jerenkrantz 95521
574     /* initialize deflate output buffer */
575     ctx->stream.next_out = ctx->buffer;
576     ctx->stream.avail_out = c->bufferSize;
577 jerenkrantz 94979 }
578 jim 332306
579 jorton 103810 while (!APR_BRIGADE_EMPTY(bb))
580 jwoolley 101788 {
581 jerenkrantz 94979 const char *data;
582     apr_bucket *b;
583     apr_size_t len;
584    
585 jorton 103810 e = APR_BRIGADE_FIRST(bb);
586    
587 jerenkrantz 94979 if (APR_BUCKET_IS_EOS(e)) {
588 jwoolley 95413 char *buf;
589 jerenkrantz 94979
590     ctx->stream.avail_in = 0; /* should be zero already anyway */
591 rpluem 422731 /* flush the remaining data from the zlib buffers */
592 rpluem 426790 flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
593     NO_UPDATE_CRC);
594 jerenkrantz 94979
595 rpluem 426791 buf = apr_palloc(r->pool, VALIDATION_SIZE);
596 jwoolley 95413 putLong((unsigned char *)&buf[0], ctx->crc);
597     putLong((unsigned char *)&buf[4], ctx->stream.total_in);
598 jerenkrantz 94979
599 rpluem 426791 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
600     f->c->bucket_alloc);
601 jerenkrantz 94979 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
602 trawick 95151 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
603 jerenkrantz 94979 "Zlib: Compressed %ld to %ld : URL %s",
604     ctx->stream.total_in, ctx->stream.total_out, r->uri);
605    
606 nd 98154 /* leave notes for logging */
607     if (c->note_input_name) {
608     apr_table_setn(r->notes, c->note_input_name,
609     (ctx->stream.total_in > 0)
610     ? apr_off_t_toa(r->pool,
611     ctx->stream.total_in)
612     : "-");
613     }
614 jerenkrantz 94979
615 nd 98154 if (c->note_output_name) {
616     apr_table_setn(r->notes, c->note_output_name,
617     (ctx->stream.total_in > 0)
618     ? apr_off_t_toa(r->pool,
619     ctx->stream.total_out)
620     : "-");
621     }
622    
623     if (c->note_ratio_name) {
624     apr_table_setn(r->notes, c->note_ratio_name,
625     (ctx->stream.total_in > 0)
626     ? apr_itoa(r->pool,
627     (int)(ctx->stream.total_out
628     * 100
629     / ctx->stream.total_in))
630     : "-");
631     }
632    
633 jerenkrantz 94979 deflateEnd(&ctx->stream);
634 rpluem 425109 /* No need for cleanup any longer */
635     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
636 jerenkrantz 94979
637     /* Remove EOS from the old list, and insert into the new. */
638     APR_BUCKET_REMOVE(e);
639     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
640    
641     /* Okay, we've seen the EOS.
642     * Time to pass it along down the chain.
643     */
644     return ap_pass_brigade(f->next, ctx->bb);
645     }
646    
647     if (APR_BUCKET_IS_FLUSH(e)) {
648 jerenkrantz 100730 apr_status_t rv;
649 jorton 103810
650 rpluem 422731 /* flush the remaining data from the zlib buffers */
651 rpluem 423940 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
652 rpluem 426790 Z_SYNC_FLUSH, NO_UPDATE_CRC);
653 rpluem 422731 if (zRC != Z_OK) {
654     return APR_EGENERAL;
655 jerenkrantz 95341 }
656 jerenkrantz 94979
657 rpluem 422731 /* Remove flush bucket from old brigade anf insert into the new. */
658     APR_BUCKET_REMOVE(e);
659     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
660 jerenkrantz 100730 rv = ap_pass_brigade(f->next, ctx->bb);
661     if (rv != APR_SUCCESS) {
662     return rv;
663     }
664 jerenkrantz 94979 continue;
665     }
666    
667 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
668     /*
669     * Remove meta data bucket from old brigade and insert into the
670     * new.
671     */
672     APR_BUCKET_REMOVE(e);
673     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
674     continue;
675 gregames 554011 }
676    
677 jerenkrantz 94979 /* read */
678     apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
679    
680     /* This crc32 function is from zlib. */
681     ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
682    
683     /* write */
684     ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
685     * but we'll just have to
686     * trust zlib */
687     ctx->stream.avail_in = len;
688    
689     while (ctx->stream.avail_in != 0) {
690     if (ctx->stream.avail_out == 0) {
691 jerenkrantz 100730 apr_status_t rv;
692    
693 jerenkrantz 94979 ctx->stream.next_out = ctx->buffer;
694 jerenkrantz 94987 len = c->bufferSize - ctx->stream.avail_out;
695 jerenkrantz 94979
696     b = apr_bucket_heap_create((char *)ctx->buffer, len,
697     NULL, f->c->bucket_alloc);
698     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
699 jerenkrantz 94987 ctx->stream.avail_out = c->bufferSize;
700 jerenkrantz 100730 /* Send what we have right now to the next filter. */
701     rv = ap_pass_brigade(f->next, ctx->bb);
702     if (rv != APR_SUCCESS) {
703     return rv;
704     }
705 jerenkrantz 94979 }
706    
707     zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
708    
709 rpluem 422736 if (zRC != Z_OK) {
710 jerenkrantz 94979 return APR_EGENERAL;
711 rpluem 422736 }
712 jerenkrantz 94979 }
713 jorton 103810
714     apr_bucket_delete(e);
715 jerenkrantz 94979 }
716    
717 jwoolley 97526 apr_brigade_cleanup(bb);
718 jerenkrantz 94979 return APR_SUCCESS;
719     }
720    
721 jerenkrantz 95345 /* This is the deflate input filter (inflates). */
722     static apr_status_t deflate_in_filter(ap_filter_t *f,
723     apr_bucket_brigade *bb,
724     ap_input_mode_t mode,
725     apr_read_type_e block,
726     apr_off_t readbytes)
727     {
728     apr_bucket *bkt;
729     request_rec *r = f->r;
730     deflate_ctx *ctx = f->ctx;
731     int zRC;
732     apr_status_t rv;
733     deflate_filter_config *c;
734    
735     /* just get out of the way of things we don't want. */
736     if (mode != AP_MODE_READBYTES) {
737     return ap_get_brigade(f->next, bb, mode, block, readbytes);
738     }
739    
740     c = ap_get_module_config(r->server->module_config, &deflate_module);
741    
742     if (!ctx) {
743 niq 562507 char deflate_hdr[10];
744 jerenkrantz 95345 apr_size_t len;
745    
746     /* only work on main request/no subrequests */
747 pquerna 105403 if (!ap_is_initial_req(r)) {
748 jerenkrantz 95345 ap_remove_input_filter(f);
749     return ap_get_brigade(f->next, bb, mode, block, readbytes);
750     }
751    
752 niq 563154 /* We can't operate on Content-Ranges */
753     if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
754     ap_remove_input_filter(f);
755     return ap_get_brigade(f->next, bb, mode, block, readbytes);
756     }
757    
758 niq 560689 /* Check whether request body is gzipped.
759     *
760     * If it is, we're transforming the contents, invalidating
761     * some request headers including Content-Encoding.
762     *
763     * If not, we just remove ourself.
764 jerenkrantz 95345 */
765 niq 563803 if (check_gzip(r, r->headers_in, NULL) == 0) {
766 jerenkrantz 95345 ap_remove_input_filter(f);
767     return ap_get_brigade(f->next, bb, mode, block, readbytes);
768     }
769    
770     f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
771     ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
772     ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
773     ctx->buffer = apr_palloc(r->pool, c->bufferSize);
774    
775     rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
776     if (rv != APR_SUCCESS) {
777     return rv;
778     }
779 jerenkrantz 98689
780 niq 563133 apr_table_unset(r->headers_in, "Content-Length");
781     apr_table_unset(r->headers_in, "Content-MD5");
782    
783 jim 332306 len = 10;
784     rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
785 jerenkrantz 95345 if (rv != APR_SUCCESS) {
786     return rv;
787     }
788    
789     /* We didn't get the magic bytes. */
790     if (len != 10 ||
791     deflate_hdr[0] != deflate_magic[0] ||
792     deflate_hdr[1] != deflate_magic[1]) {
793     return APR_EGENERAL;
794     }
795    
796     /* We can't handle flags for now. */
797     if (deflate_hdr[3] != 0) {
798     return APR_EGENERAL;
799     }
800    
801     zRC = inflateInit2(&ctx->stream, c->windowSize);
802    
803     if (zRC != Z_OK) {
804     f->ctx = NULL;
805 jerenkrantz 98689 inflateEnd(&ctx->stream);
806 jerenkrantz 95345 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
807     "unable to init Zlib: "
808     "inflateInit2 returned %d: URL %s",
809     zRC, r->uri);
810     ap_remove_input_filter(f);
811     return ap_get_brigade(f->next, bb, mode, block, readbytes);
812     }
813    
814     /* initialize deflate output buffer */
815     ctx->stream.next_out = ctx->buffer;
816     ctx->stream.avail_out = c->bufferSize;
817    
818     apr_brigade_cleanup(ctx->bb);
819     }
820    
821     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
822     rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
823    
824     if (rv != APR_SUCCESS) {
825 jerenkrantz 98689 /* What about APR_EAGAIN errors? */
826     inflateEnd(&ctx->stream);
827 jerenkrantz 95345 return rv;
828     }
829    
830 jwoolley 101788 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
831     bkt != APR_BRIGADE_SENTINEL(ctx->bb);
832     bkt = APR_BUCKET_NEXT(bkt))
833     {
834 jerenkrantz 95345 const char *data;
835     apr_size_t len;
836    
837     /* If we actually see the EOS, that means we screwed up! */
838     if (APR_BUCKET_IS_EOS(bkt)) {
839 jerenkrantz 98689 inflateEnd(&ctx->stream);
840 jerenkrantz 95345 return APR_EGENERAL;
841     }
842    
843     if (APR_BUCKET_IS_FLUSH(bkt)) {
844     apr_bucket *tmp_heap;
845     zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
846     if (zRC != Z_OK) {
847 jerenkrantz 98689 inflateEnd(&ctx->stream);
848 jerenkrantz 95345 return APR_EGENERAL;
849     }
850    
851     ctx->stream.next_out = ctx->buffer;
852     len = c->bufferSize - ctx->stream.avail_out;
853    
854     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
855     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
856     NULL, f->c->bucket_alloc);
857     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
858     ctx->stream.avail_out = c->bufferSize;
859    
860     /* Move everything to the returning brigade. */
861     APR_BUCKET_REMOVE(bkt);
862     APR_BRIGADE_CONCAT(bb, ctx->bb);
863     break;
864     }
865    
866     /* read */
867     apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
868    
869     /* pass through zlib inflate. */
870     ctx->stream.next_in = (unsigned char *)data;
871     ctx->stream.avail_in = len;
872    
873 jerenkrantz 95359 zRC = Z_OK;
874    
875 jerenkrantz 95345 while (ctx->stream.avail_in != 0) {
876     if (ctx->stream.avail_out == 0) {
877     apr_bucket *tmp_heap;
878     ctx->stream.next_out = ctx->buffer;
879     len = c->bufferSize - ctx->stream.avail_out;
880    
881     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
882     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
883     NULL, f->c->bucket_alloc);
884     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
885     ctx->stream.avail_out = c->bufferSize;
886     }
887    
888     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
889    
890     if (zRC == Z_STREAM_END) {
891     break;
892     }
893    
894     if (zRC != Z_OK) {
895 jerenkrantz 98689 inflateEnd(&ctx->stream);
896 jerenkrantz 95345 return APR_EGENERAL;
897     }
898     }
899     if (zRC == Z_STREAM_END) {
900     apr_bucket *tmp_heap, *eos;
901    
902     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
903     "Zlib: Inflated %ld to %ld : URL %s",
904     ctx->stream.total_in, ctx->stream.total_out,
905     r->uri);
906    
907     len = c->bufferSize - ctx->stream.avail_out;
908    
909     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
910     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
911     NULL, f->c->bucket_alloc);
912     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
913     ctx->stream.avail_out = c->bufferSize;
914    
915     /* Is the remaining 8 bytes already in the avail stream? */
916     if (ctx->stream.avail_in >= 8) {
917     unsigned long compCRC, compLen;
918     compCRC = getLong(ctx->stream.next_in);
919     if (ctx->crc != compCRC) {
920 jerenkrantz 98689 inflateEnd(&ctx->stream);
921 jerenkrantz 95345 return APR_EGENERAL;
922     }
923     ctx->stream.next_in += 4;
924     compLen = getLong(ctx->stream.next_in);
925     if (ctx->stream.total_out != compLen) {
926 jerenkrantz 98689 inflateEnd(&ctx->stream);
927 jerenkrantz 95345 return APR_EGENERAL;
928     }
929     }
930     else {
931     /* FIXME: We need to grab the 8 verification bytes
932     * from the wire! */
933 jerenkrantz 98689 inflateEnd(&ctx->stream);
934 jerenkrantz 95345 return APR_EGENERAL;
935     }
936    
937     inflateEnd(&ctx->stream);
938 jerenkrantz 98689
939 jerenkrantz 95345 eos = apr_bucket_eos_create(f->c->bucket_alloc);
940 jim 332306 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
941 jerenkrantz 95345 break;
942     }
943 jerenkrantz 95666
944 jerenkrantz 95345 }
945 jerenkrantz 95521 apr_brigade_cleanup(ctx->bb);
946 jerenkrantz 95345 }
947    
948 jerenkrantz 95666 /* If we are about to return nothing for a 'blocking' read and we have
949     * some data in our zlib buffer, flush it out so we can return something.
950     */
951     if (block == APR_BLOCK_READ &&
952     APR_BRIGADE_EMPTY(ctx->proc_bb) &&
953     ctx->stream.avail_out < c->bufferSize) {
954     apr_bucket *tmp_heap;
955     apr_size_t len;
956     ctx->stream.next_out = ctx->buffer;
957     len = c->bufferSize - ctx->stream.avail_out;
958    
959     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
960     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
961     NULL, f->c->bucket_alloc);
962     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
963     ctx->stream.avail_out = c->bufferSize;
964     }
965    
966 jerenkrantz 95345 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
967     apr_bucket_brigade *newbb;
968    
969     /* May return APR_INCOMPLETE which is fine by us. */
970     apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
971    
972     newbb = apr_brigade_split(ctx->proc_bb, bkt);
973     APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
974     APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
975     }
976    
977     return APR_SUCCESS;
978     }
979    
980 ianh 103405
981     /* Filter to inflate for a content-transforming proxy. */
982     static apr_status_t inflate_out_filter(ap_filter_t *f,
983     apr_bucket_brigade *bb)
984     {
985 nd 104166 int zlib_method;
986     int zlib_flags;
987 rpluem 426799 apr_bucket *e;
988 ianh 103405 request_rec *r = f->r;
989     deflate_ctx *ctx = f->ctx;
990     int zRC;
991     apr_status_t rv;
992     deflate_filter_config *c;
993    
994 niq 104352 /* Do nothing if asked to filter nothing. */
995     if (APR_BRIGADE_EMPTY(bb)) {
996 rpluem 424759 return ap_pass_brigade(f->next, bb);
997 niq 104352 }
998    
999 ianh 103405 c = ap_get_module_config(r->server->module_config, &deflate_module);
1000    
1001     if (!ctx) {
1002    
1003     /* only work on main request/no subrequests */
1004 pquerna 105403 if (!ap_is_initial_req(r)) {
1005 ianh 103405 ap_remove_output_filter(f);
1006     return ap_pass_brigade(f->next, bb);
1007     }
1008    
1009 niq 563154 /* We can't operate on Content-Ranges */
1010     if (apr_table_get(r->headers_out, "Content-Range") != NULL) {
1011     ap_remove_output_filter(f);
1012     return ap_pass_brigade(f->next, bb);
1013     }
1014    
1015 rpluem 475922 /*
1016     * Let's see what our current Content-Encoding is.
1017 niq 562507 * Only inflate if gzipped.
1018 ianh 103405 */
1019 niq 563803 if (check_gzip(r, r->headers_out, r->err_headers_out) == 0) {
1020 ianh 103405 ap_remove_output_filter(f);
1021     return ap_pass_brigade(f->next, bb);
1022     }
1023    
1024 niq 104352 /* No need to inflate HEAD or 204/304 */
1025     if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
1026     ap_remove_output_filter(f);
1027     return ap_pass_brigade(f->next, bb);
1028     }
1029    
1030 ianh 103405 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1031 rpluem 426799 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1032 ianh 103405 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1033 rpluem 426799 ctx->libz_end_func = inflateEnd;
1034     ctx->validation_buffer = NULL;
1035     ctx->validation_buffer_length = 0;
1036 ianh 103405
1037     zRC = inflateInit2(&ctx->stream, c->windowSize);
1038    
1039     if (zRC != Z_OK) {
1040     f->ctx = NULL;
1041     inflateEnd(&ctx->stream);
1042     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1043     "unable to init Zlib: "
1044     "inflateInit2 returned %d: URL %s",
1045     zRC, r->uri);
1046 rpluem 426799 /*
1047     * Remove ourselves as it does not make sense to return:
1048     * We are not able to init libz and pass data down the chain
1049     * compressed.
1050     */
1051 ianh 103405 ap_remove_output_filter(f);
1052     return ap_pass_brigade(f->next, bb);
1053     }
1054    
1055 rpluem 426799 /*
1056     * Register a cleanup function to ensure that we cleanup the internal
1057     * libz resources.
1058     */
1059     apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
1060     apr_pool_cleanup_null);
1061    
1062 rpluem 563229 /* these are unlikely to be set anyway, but ... */
1063 rpluem 426799 apr_table_unset(r->headers_out, "Content-Length");
1064 rpluem 563229 apr_table_unset(r->headers_out, "Content-MD5");
1065 rpluem 426799
1066     /* initialize inflate output buffer */
1067 ianh 103405 ctx->stream.next_out = ctx->buffer;
1068     ctx->stream.avail_out = c->bufferSize;
1069    
1070 niq 580598 ctx->inflate_init = 0;
1071 ianh 103405 }
1072    
1073 rpluem 426799 while (!APR_BRIGADE_EMPTY(bb))
1074 ianh 103405 {
1075     const char *data;
1076 rpluem 426799 apr_bucket *b;
1077 ianh 103405 apr_size_t len;
1078    
1079 rpluem 426799 e = APR_BRIGADE_FIRST(bb);
1080    
1081     if (APR_BUCKET_IS_EOS(e)) {
1082 rpluem 475406 /*
1083     * We are really done now. Ensure that we never return here, even
1084     * if a second EOS bucket falls down the chain. Thus remove
1085     * ourselves.
1086     */
1087     ap_remove_output_filter(f);
1088 rpluem 475922 /* should be zero already anyway */
1089     ctx->stream.avail_in = 0;
1090 rpluem 426799 /*
1091     * Flush the remaining data from the zlib buffers. It is correct
1092     * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1093     * deflate case. In the inflate case Z_FINISH requires to have a
1094     * large enough output buffer to put ALL data in otherwise it
1095     * fails, whereas in the deflate case you can empty a filled output
1096     * buffer and call it again until no more output can be created.
1097     */
1098     flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
1099     UPDATE_CRC);
1100     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1101     "Zlib: Inflated %ld to %ld : URL %s",
1102     ctx->stream.total_in, ctx->stream.total_out, r->uri);
1103    
1104     if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1105     unsigned long compCRC, compLen;
1106     compCRC = getLong(ctx->validation_buffer);
1107     if (ctx->crc != compCRC) {
1108     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1109     "Zlib: Checksum of inflated stream invalid");
1110     return APR_EGENERAL;
1111     }
1112     ctx->validation_buffer += VALIDATION_SIZE / 2;
1113     compLen = getLong(ctx->validation_buffer);
1114     if (ctx->stream.total_out != compLen) {
1115     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1116     "Zlib: Length of inflated stream invalid");
1117     return APR_EGENERAL;
1118     }
1119     }
1120     else {
1121     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1122     "Zlib: Validation bytes not present");
1123     return APR_EGENERAL;
1124     }
1125    
1126 ianh 103405 inflateEnd(&ctx->stream);
1127 rpluem 426799 /* No need for cleanup any longer */
1128     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1129    
1130     /* Remove EOS from the old list, and insert into the new. */
1131     APR_BUCKET_REMOVE(e);
1132     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1133    
1134     /*
1135     * Okay, we've seen the EOS.
1136     * Time to pass it along down the chain.
1137     */
1138     return ap_pass_brigade(f->next, ctx->bb);
1139 ianh 103405 }
1140    
1141 rpluem 426799 if (APR_BUCKET_IS_FLUSH(e)) {
1142     apr_status_t rv;
1143    
1144     /* flush the remaining data from the zlib buffers */
1145     zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
1146     Z_SYNC_FLUSH, UPDATE_CRC);
1147     if (zRC != Z_OK) {
1148     return APR_EGENERAL;
1149     }
1150    
1151     /* Remove flush bucket from old brigade anf insert into the new. */
1152     APR_BUCKET_REMOVE(e);
1153     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1154     rv = ap_pass_brigade(f->next, ctx->bb);
1155     if (rv != APR_SUCCESS) {
1156     return rv;
1157     }
1158 niq 416165 continue;
1159 ianh 103405 }
1160    
1161 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
1162     /*
1163     * Remove meta data bucket from old brigade and insert into the
1164     * new.
1165     */
1166     APR_BUCKET_REMOVE(e);
1167     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1168     continue;
1169     }
1170    
1171 ianh 103405 /* read */
1172 rpluem 426799 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1173 ianh 103405
1174     /* first bucket contains zlib header */
1175 niq 580598 if (!ctx->inflate_init++) {
1176 nd 104166 if (len < 10) {
1177 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1178     "Insufficient data for inflate");
1179 nd 104166 return APR_EGENERAL;
1180 jim 332306 }
1181 ianh 103405 else {
1182 nd 104166 zlib_method = data[2];
1183     zlib_flags = data[3];
1184     if (zlib_method != Z_DEFLATED) {
1185 nd 104155 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1186 nd 104166 "inflate: data not deflated!");
1187     ap_remove_output_filter(f);
1188     return ap_pass_brigade(f->next, bb);
1189 niq 104034 }
1190 ianh 103405 if (data[0] != deflate_magic[0] ||
1191 niq 104034 data[1] != deflate_magic[1] ||
1192 nd 104166 (zlib_flags & RESERVED) != 0) {
1193 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1194 nd 104166 "inflate: bad header");
1195 ianh 103405 return APR_EGENERAL ;
1196     }
1197     data += 10 ;
1198     len -= 10 ;
1199     }
1200 nd 104166 if (zlib_flags & EXTRA_FIELD) {
1201     unsigned int bytes = (unsigned int)(data[0]);
1202     bytes += ((unsigned int)(data[1])) << 8;
1203     bytes += 2;
1204     if (len < bytes) {
1205 niq 104034 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1206 nd 104166 "inflate: extra field too big (not "
1207     "supported)");
1208     return APR_EGENERAL;
1209 niq 104034 }
1210 nd 104166 data += bytes;
1211     len -= bytes;
1212 niq 104034 }
1213 nd 104166 if (zlib_flags & ORIG_NAME) {
1214     while (len-- && *data++);
1215 niq 104034 }
1216 nd 104166 if (zlib_flags & COMMENT) {
1217     while (len-- && *data++);
1218 niq 104034 }
1219 nd 104166 if (zlib_flags & HEAD_CRC) {
1220     len -= 2;
1221     data += 2;
1222 niq 104034 }
1223 ianh 103405 }
1224    
1225     /* pass through zlib inflate. */
1226     ctx->stream.next_in = (unsigned char *)data;
1227     ctx->stream.avail_in = len;
1228    
1229 rpluem 426799 if (ctx->validation_buffer) {
1230     if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1231     apr_size_t copy_size;
1232    
1233     copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1234     if (copy_size > ctx->stream.avail_in)
1235     copy_size = ctx->stream.avail_in;
1236 rpluem 475915 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1237     ctx->stream.next_in, copy_size);
1238     /* Saved copy_size bytes */
1239     ctx->stream.avail_in -= copy_size;
1240 niq 476600 ctx->validation_buffer_length += copy_size;
1241 rpluem 475915 }
1242     if (ctx->stream.avail_in) {
1243 rpluem 426799 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1244     "Zlib: %d bytes of garbage at the end of "
1245     "compressed stream.", ctx->stream.avail_in);
1246 rpluem 475915 /*
1247     * There is nothing worth consuming for zlib left, because it is
1248     * either garbage data or the data has been copied to the
1249     * validation buffer (processing validation data is no business
1250     * for zlib). So set ctx->stream.avail_in to zero to indicate
1251     * this to the following while loop.
1252     */
1253     ctx->stream.avail_in = 0;
1254 rpluem 426799 }
1255     }
1256    
1257 ianh 103405 zRC = Z_OK;
1258    
1259     while (ctx->stream.avail_in != 0) {
1260     if (ctx->stream.avail_out == 0) {
1261 rpluem 426799
1262 ianh 103405 ctx->stream.next_out = ctx->buffer;
1263     len = c->bufferSize - ctx->stream.avail_out;
1264    
1265     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1266 rpluem 426799 b = apr_bucket_heap_create((char *)ctx->buffer, len,
1267     NULL, f->c->bucket_alloc);
1268     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
1269 ianh 103405 ctx->stream.avail_out = c->bufferSize;
1270 rpluem 426799 /* Send what we have right now to the next filter. */
1271     rv = ap_pass_brigade(f->next, ctx->bb);
1272     if (rv != APR_SUCCESS) {
1273     return rv;
1274     }
1275 ianh 103405 }
1276    
1277     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1278    
1279     if (zRC == Z_STREAM_END) {
1280 rpluem 426799 /*
1281     * We have inflated all data. Now try to capture the
1282     * validation bytes. We may not have them all available
1283     * right now, but capture what is there.
1284     */
1285     ctx->validation_buffer = apr_pcalloc(f->r->pool,
1286     VALIDATION_SIZE);
1287     if (ctx->stream.avail_in > VALIDATION_SIZE) {
1288     ctx->validation_buffer_length = VALIDATION_SIZE;
1289 rpluem 475920 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1290     "Zlib: %d bytes of garbage at the end of "
1291     "compressed stream.",
1292     ctx->stream.avail_in - VALIDATION_SIZE);
1293 rpluem 426799 } else if (ctx->stream.avail_in > 0) {
1294     ctx->validation_buffer_length = ctx->stream.avail_in;
1295     }
1296     if (ctx->validation_buffer_length)
1297     memcpy(ctx->validation_buffer, ctx->stream.next_in,
1298     ctx->validation_buffer_length);
1299 ianh 103405 break;
1300     }
1301    
1302     if (zRC != Z_OK) {
1303     return APR_EGENERAL;
1304     }
1305     }
1306    
1307 rpluem 426799 apr_bucket_delete(e);
1308 ianh 103405 }
1309    
1310 rpluem 426799 apr_brigade_cleanup(bb);
1311     return APR_SUCCESS;
1312 ianh 103405 }
1313    
1314 niq 416165 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1315 jerenkrantz 94979 static void register_hooks(apr_pool_t *p)
1316     {
1317 jerenkrantz 95906 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1318 jerenkrantz 94979 AP_FTYPE_CONTENT_SET);
1319 ianh 103405 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1320     AP_FTYPE_RESOURCE-1);
1321 jerenkrantz 95906 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1322 jerenkrantz 95345 AP_FTYPE_CONTENT_SET);
1323 jerenkrantz 94979 }
1324    
1325     static const command_rec deflate_filter_cmds[] = {
1326 nd 98149 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1327 jerenkrantz 94979 "Set a note to report on compression ratio"),
1328     AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1329     RSRC_CONF, "Set the Deflate window size (1-15)"),
1330 jerenkrantz 94987 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1331     "Set the Deflate Buffer Size"),
1332 jerenkrantz 94979 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1333     "Set the Deflate Memory Level (1-9)"),
1334 ianh 98982 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1335 ianh 98895 "Set the Deflate Compression Level (1-9)"),
1336 jerenkrantz 94979 {NULL}
1337     };
1338    
1339     module AP_MODULE_DECLARE_DATA deflate_module = {
1340     STANDARD20_MODULE_STUFF,
1341     NULL, /* dir config creater */
1342     NULL, /* dir merger --- default is to override */
1343     create_deflate_server_config, /* server config */
1344     NULL, /* merge server config */
1345     deflate_filter_cmds, /* command table */
1346     register_hooks /* register hooks */
1347     };

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2