/[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 - (show 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 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * mod_deflate.c: Perform deflate content-encoding on the fly
19 *
20 * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew
21 */
22
23 /*
24 * Portions of this software are based upon zlib code by Jean-loup Gailly
25 * (zlib functions gz_open and gzwrite, check_header)
26 */
27
28 /* zlib flags */
29 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
30 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
31 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
32 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
33 #define COMMENT 0x10 /* bit 4 set: file comment present */
34 #define RESERVED 0xE0 /* bits 5..7: reserved */
35
36
37 #include "httpd.h"
38 #include "http_config.h"
39 #include "http_log.h"
40 #include "apr_lib.h"
41 #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 #define APR_WANT_STRFUNC
47 #include "apr_want.h"
48
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 int compressionlevel;
59 apr_size_t bufferSize;
60 char *note_ratio_name;
61 char *note_input_name;
62 char *note_output_name;
63 } deflate_filter_config;
64
65 /* RFC 1952 Section 2.3 defines the gzip header:
66 *
67 * +---+---+---+---+---+---+---+---+---+---+
68 * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
69 * +---+---+---+---+---+---+---+---+---+---+
70 */
71 static const char gzip_header[10] =
72 { '\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 /* windowsize is negative to suppress Zlib header */
81 #define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
82 #define DEFAULT_WINDOWSIZE -15
83 #define DEFAULT_MEMLEVEL 9
84 #define DEFAULT_BUFFERSIZE 8096
85
86
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 static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
92 {
93 int found = 0;
94 apr_table_t *hdrs = hdrs1;
95 const char *encoding = apr_table_get(hdrs, "Content-Encoding");
96
97 if (!encoding && (hdrs2 != NULL)) {
98 /* the output filter has two tables and a content_encoding to check */
99 encoding = apr_table_get(hdrs2, "Content-Encoding");
100 hdrs = hdrs2;
101 if (!encoding) {
102 encoding = r->content_encoding;
103 hdrs = NULL;
104 }
105 }
106 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 if (hdrs) {
113 apr_table_unset(hdrs, "Content-Encoding");
114 }
115 else {
116 r->content_encoding = NULL;
117 }
118 }
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 char *new_encoding = apr_pstrdup(r->pool, encoding);
124 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 if (hdrs) {
132 apr_table_unset(hdrs, "Content-Encoding");
133 }
134 else {
135 r->content_encoding = NULL;
136 }
137 }
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 if (hdrs) {
145 apr_table_setn(hdrs, "Content-Encoding", new_encoding);
146 }
147 else {
148 r->content_encoding = new_encoding;
149 }
150 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 /* 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 static void putLong(unsigned char *string, unsigned long x)
167 {
168 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 }
173
174 /* Inputs a string and returns a long.
175 */
176 static unsigned long getLong(unsigned char *string)
177 {
178 return ((unsigned long)string[0])
179 | (((unsigned long)string[1]) << 8)
180 | (((unsigned long)string[2]) << 16)
181 | (((unsigned long)string[3]) << 24);
182 }
183
184 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 c->bufferSize = DEFAULT_BUFFERSIZE;
191 c->compressionlevel = DEFAULT_COMPRESSION;
192
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 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 int n = atoi(arg);
219
220 if (n <= 0) {
221 return "DeflateBufferSize should be positive";
222 }
223
224 c->bufferSize = (apr_size_t)n;
225
226 return NULL;
227 }
228 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
229 const char *arg1, const char *arg2)
230 {
231 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
232 &deflate_module);
233
234 if (arg2 == NULL) {
235 c->note_ratio_name = apr_pstrdup(cmd->pool, arg1);
236 }
237 else if (!strcasecmp(arg1, "ratio")) {
238 c->note_ratio_name = apr_pstrdup(cmd->pool, arg2);
239 }
240 else if (!strcasecmp(arg1, "input")) {
241 c->note_input_name = apr_pstrdup(cmd->pool, arg2);
242 }
243 else if (!strcasecmp(arg1, "output")) {
244 c->note_output_name = apr_pstrdup(cmd->pool, arg2);
245 }
246 else {
247 return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
248 }
249
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 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 typedef struct deflate_ctx_t
288 {
289 z_stream stream;
290 unsigned char *buffer;
291 unsigned long crc;
292 apr_bucket_brigade *bb, *proc_bb;
293 int (*libz_end_func)(z_streamp);
294 unsigned char *validation_buffer;
295 apr_size_t validation_buffer_length;
296 int inflate_init;
297 } deflate_ctx;
298
299 /* Number of validation bytes (CRC and length) after the compressed data */
300 #define VALIDATION_SIZE 8
301 /* 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 static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
307 struct apr_bucket_alloc_t *bucket_alloc,
308 int (*libz_func)(z_streamp, int), int flush,
309 int crc)
310 {
311 int zRC = Z_OK;
312 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 /*
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 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 zRC = libz_func(&ctx->stream, flush);
341
342 /*
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 zRC = Z_OK;
356 break;
357 }
358
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 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 /* PR 39727: we're screwing up our clients if we leave a strong ETag
376 * header while transforming content. Henrik Nordstrom suggests
377 * appending ";gzip".
378 *
379 * 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 */
384 static void deflate_check_etag(request_rec *r, const char *transform)
385 {
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 apr_pstrcat(r->pool, etag, "-", transform, NULL));
390 }
391 }
392 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 deflate_filter_config *c;
400
401 /* Do nothing if asked to filter nothing. */
402 if (APR_BRIGADE_EMPTY(bb)) {
403 return ap_pass_brigade(f->next, bb);
404 }
405
406 c = ap_get_module_config(r->server->module_config,
407 &deflate_module);
408
409 /* 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 char *token;
417 const char *encoding;
418
419 /* only work on main request/no subrequests */
420 if (r->main != NULL) {
421 ap_remove_output_filter(f);
422 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 ap_remove_output_filter(f);
430 return ap_pass_brigade(f->next, bb);
431 }
432
433 /* 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 /* 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 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 if ( env_value && (strcmp(env_value,"1") == 0) ) {
448 ap_remove_output_filter(f);
449 return ap_pass_brigade(f->next, bb);
450 }
451 }
452
453 /* Let's see what our current Content-Encoding is.
454 * If it's already encoded, don't compress again.
455 * (We could, but let's not.)
456 */
457 encoding = apr_table_get(r->headers_out, "Content-Encoding");
458 if (encoding) {
459 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 if (r->content_encoding) {
471 encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
472 r->content_encoding, NULL)
473 : r->content_encoding;
474 }
475
476 if (encoding) {
477 const char *tmp = encoding;
478
479 token = ap_get_token(r->pool, &tmp, 0);
480 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 ap_remove_output_filter(f);
486 return ap_pass_brigade(f->next, bb);
487 }
488
489 /* Otherwise, skip token */
490 if (*tmp) {
491 ++tmp;
492 }
493 token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
494 }
495 }
496
497 /* 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 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
502
503 /* force-gzip will just force it out regardless if the browser
504 * can actually do anything with it.
505 */
506 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
507 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 }
514
515 token = ap_get_token(r->pool, &accepts, 0);
516 while (token && token[0] && strcasecmp(token, "gzip")) {
517 /* skip parameters, XXX: ;q=foo evaluation? */
518 while (*accepts == ';') {
519 ++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 }
529
530 /* 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 }
536
537 /* 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 ap_remove_output_filter(f);
541 return ap_pass_brigade(f->next, bb);
542 }
543
544 /* 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 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
548 ctx->libz_end_func = deflateEnd;
549
550 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
551 c->windowSize, c->memlevel,
552 Z_DEFAULT_STRATEGY);
553
554 if (zRC != Z_OK) {
555 deflateEnd(&ctx->stream);
556 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
557 "unable to init Zlib: "
558 "deflateInit2 returned %d: URL %s",
559 zRC, r->uri);
560 /*
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 return ap_pass_brigade(f->next, bb);
567 }
568 /*
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
575 /* add immortal gzip header */
576 e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
577 f->c->bucket_alloc);
578 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
579
580 /* 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 /* 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 apr_table_unset(r->headers_out, "Content-Length");
593 apr_table_unset(r->headers_out, "Content-MD5");
594 deflate_check_etag(r, "gzip");
595
596 /* initialize deflate output buffer */
597 ctx->stream.next_out = ctx->buffer;
598 ctx->stream.avail_out = c->bufferSize;
599 }
600
601 while (!APR_BRIGADE_EMPTY(bb))
602 {
603 const char *data;
604 apr_bucket *b;
605 apr_size_t len;
606
607 e = APR_BRIGADE_FIRST(bb);
608
609 if (APR_BUCKET_IS_EOS(e)) {
610 char *buf;
611
612 ctx->stream.avail_in = 0; /* should be zero already anyway */
613 /* flush the remaining data from the zlib buffers */
614 flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
615 NO_UPDATE_CRC);
616
617 buf = apr_palloc(r->pool, VALIDATION_SIZE);
618 putLong((unsigned char *)&buf[0], ctx->crc);
619 putLong((unsigned char *)&buf[4], ctx->stream.total_in);
620
621 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
622 f->c->bucket_alloc);
623 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
624 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
625 "Zlib: Compressed %ld to %ld : URL %s",
626 ctx->stream.total_in, ctx->stream.total_out, r->uri);
627
628 /* 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
637 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 deflateEnd(&ctx->stream);
656 /* No need for cleanup any longer */
657 apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
658
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 apr_status_t rv;
671
672 /* flush the remaining data from the zlib buffers */
673 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
674 Z_SYNC_FLUSH, NO_UPDATE_CRC);
675 if (zRC != Z_OK) {
676 return APR_EGENERAL;
677 }
678
679 /* 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 rv = ap_pass_brigade(f->next, ctx->bb);
683 if (rv != APR_SUCCESS) {
684 return rv;
685 }
686 continue;
687 }
688
689 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 }
698
699 /* 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 apr_status_t rv;
714
715 ctx->stream.next_out = ctx->buffer;
716 len = c->bufferSize - ctx->stream.avail_out;
717
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 ctx->stream.avail_out = c->bufferSize;
722 /* 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 }
728
729 zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
730
731 if (zRC != Z_OK) {
732 return APR_EGENERAL;
733 }
734 }
735
736 apr_bucket_delete(e);
737 }
738
739 apr_brigade_cleanup(bb);
740 return APR_SUCCESS;
741 }
742
743 /* 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 char deflate_hdr[10];
766 apr_size_t len;
767
768 /* only work on main request/no subrequests */
769 if (!ap_is_initial_req(r)) {
770 ap_remove_input_filter(f);
771 return ap_get_brigade(f->next, bb, mode, block, readbytes);
772 }
773
774 /* 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 /* 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 */
787 if (check_gzip(r, r->headers_in, NULL) == 0) {
788 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
802 apr_table_unset(r->headers_in, "Content-Length");
803 apr_table_unset(r->headers_in, "Content-MD5");
804
805 len = 10;
806 rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
807 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 inflateEnd(&ctx->stream);
828 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 /* What about APR_EAGAIN errors? */
848 inflateEnd(&ctx->stream);
849 return rv;
850 }
851
852 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
853 bkt != APR_BRIGADE_SENTINEL(ctx->bb);
854 bkt = APR_BUCKET_NEXT(bkt))
855 {
856 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 inflateEnd(&ctx->stream);
862 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 inflateEnd(&ctx->stream);
870 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 zRC = Z_OK;
896
897 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 inflateEnd(&ctx->stream);
918 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 inflateEnd(&ctx->stream);
943 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 inflateEnd(&ctx->stream);
949 return APR_EGENERAL;
950 }
951 }
952 else {
953 /* FIXME: We need to grab the 8 verification bytes
954 * from the wire! */
955 inflateEnd(&ctx->stream);
956 return APR_EGENERAL;
957 }
958
959 inflateEnd(&ctx->stream);
960
961 eos = apr_bucket_eos_create(f->c->bucket_alloc);
962 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
963 break;
964 }
965
966 }
967 apr_brigade_cleanup(ctx->bb);
968 }
969
970 /* 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 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
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 int zlib_method;
1008 int zlib_flags;
1009 apr_bucket *e;
1010 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 /* Do nothing if asked to filter nothing. */
1017 if (APR_BRIGADE_EMPTY(bb)) {
1018 return ap_pass_brigade(f->next, bb);
1019 }
1020
1021 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 if (!ap_is_initial_req(r)) {
1027 ap_remove_output_filter(f);
1028 return ap_pass_brigade(f->next, bb);
1029 }
1030
1031 /* 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 /*
1038 * Let's see what our current Content-Encoding is.
1039 * Only inflate if gzipped.
1040 */
1041 if (check_gzip(r, r->headers_out, r->err_headers_out) == 0) {
1042 ap_remove_output_filter(f);
1043 return ap_pass_brigade(f->next, bb);
1044 }
1045
1046 /* 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 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1053 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1054 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1055 ctx->libz_end_func = inflateEnd;
1056 ctx->validation_buffer = NULL;
1057 ctx->validation_buffer_length = 0;
1058
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 /*
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 ap_remove_output_filter(f);
1074 return ap_pass_brigade(f->next, bb);
1075 }
1076
1077 /*
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 /* these are unlikely to be set anyway, but ... */
1085 apr_table_unset(r->headers_out, "Content-Length");
1086 apr_table_unset(r->headers_out, "Content-MD5");
1087 deflate_check_etag(r, "gunzip");
1088
1089 /* initialize inflate output buffer */
1090 ctx->stream.next_out = ctx->buffer;
1091 ctx->stream.avail_out = c->bufferSize;
1092
1093 ctx->inflate_init = 0;
1094 }
1095
1096 while (!APR_BRIGADE_EMPTY(bb))
1097 {
1098 const char *data;
1099 apr_bucket *b;
1100 apr_size_t len;
1101
1102 e = APR_BRIGADE_FIRST(bb);
1103
1104 if (APR_BUCKET_IS_EOS(e)) {
1105 /*
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 /* should be zero already anyway */
1112 ctx->stream.avail_in = 0;
1113 /*
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 inflateEnd(&ctx->stream);
1150 /* 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 }
1163
1164 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 continue;
1182 }
1183
1184 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 /* read */
1195 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1196
1197 /* first bucket contains zlib header */
1198 if (!ctx->inflate_init++) {
1199 if (len < 10) {
1200 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1201 "Insufficient data for inflate");
1202 return APR_EGENERAL;
1203 }
1204 else {
1205 zlib_method = data[2];
1206 zlib_flags = data[3];
1207 if (zlib_method != Z_DEFLATED) {
1208 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1209 "inflate: data not deflated!");
1210 ap_remove_output_filter(f);
1211 return ap_pass_brigade(f->next, bb);
1212 }
1213 if (data[0] != deflate_magic[0] ||
1214 data[1] != deflate_magic[1] ||
1215 (zlib_flags & RESERVED) != 0) {
1216 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1217 "inflate: bad header");
1218 return APR_EGENERAL ;
1219 }
1220 data += 10 ;
1221 len -= 10 ;
1222 }
1223 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 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1229 "inflate: extra field too big (not "
1230 "supported)");
1231 return APR_EGENERAL;
1232 }
1233 data += bytes;
1234 len -= bytes;
1235 }
1236 if (zlib_flags & ORIG_NAME) {
1237 while (len-- && *data++);
1238 }
1239 if (zlib_flags & COMMENT) {
1240 while (len-- && *data++);
1241 }
1242 if (zlib_flags & HEAD_CRC) {
1243 len -= 2;
1244 data += 2;
1245 }
1246 }
1247
1248 /* pass through zlib inflate. */
1249 ctx->stream.next_in = (unsigned char *)data;
1250 ctx->stream.avail_in = len;
1251
1252 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 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 ctx->validation_buffer_length += copy_size;
1264 }
1265 if (ctx->stream.avail_in) {
1266 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 /*
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 }
1278 }
1279
1280 zRC = Z_OK;
1281
1282 while (ctx->stream.avail_in != 0) {
1283 if (ctx->stream.avail_out == 0) {
1284
1285 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 b = apr_bucket_heap_create((char *)ctx->buffer, len,
1290 NULL, f->c->bucket_alloc);
1291 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
1292 ctx->stream.avail_out = c->bufferSize;
1293 /* 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 }
1299
1300 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1301
1302 if (zRC == Z_STREAM_END) {
1303 /*
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 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 } 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 break;
1323 }
1324
1325 if (zRC != Z_OK) {
1326 return APR_EGENERAL;
1327 }
1328 }
1329
1330 apr_bucket_delete(e);
1331 }
1332
1333 apr_brigade_cleanup(bb);
1334 return APR_SUCCESS;
1335 }
1336
1337 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1338 static void register_hooks(apr_pool_t *p)
1339 {
1340 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1341 AP_FTYPE_CONTENT_SET);
1342 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1343 AP_FTYPE_RESOURCE-1);
1344 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1345 AP_FTYPE_CONTENT_SET);
1346 }
1347
1348 static const command_rec deflate_filter_cmds[] = {
1349 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1350 "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 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1354 "Set the Deflate Buffer Size"),
1355 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1356 "Set the Deflate Memory Level (1-9)"),
1357 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1358 "Set the Deflate Compression Level (1-9)"),
1359 {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