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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2