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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * mod_deflate.c: Perform deflate content-encoding on the fly
20 * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew
24 * Portions of this software are based upon zlib code by Jean-loup Gailly
25 * (zlib functions gz_open and gzwrite, check_header)
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 */
38 #include "http_config.h"
40 #include "apr_strings.h"
41 #include "apr_general.h"
42 #include "util_filter.h"
43 #include "apr_buckets.h"
44 #include "http_request.h"
45 #define APR_WANT_STRFUNC
50 static const char deflateFilterName[] = "DEFLATE";
51 module AP_MODULE_DECLARE_DATA deflate_module;
53 typedef struct deflate_filter_config_t
58 apr_size_t bufferSize;
59 char *note_ratio_name;
60 char *note_input_name;
61 char *note_output_name;
62 } deflate_filter_config;
64 /* RFC 1952 Section 2.3 defines the gzip header:
66 * +---+---+---+---+---+---+---+---+---+---+
67 * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
68 * +---+---+---+---+---+---+---+---+---+---+
70 static const char gzip_header[10] =
71 { '\037', '\213', Z_DEFLATED, 0,
72 0, 0, 0, 0, /* mtime */
73 0, 0x03 /* Unix OS_CODE */
77 static const char deflate_magic[2] = { '\037', '\213' };
79 /* windowsize is negative to suppress Zlib header */
80 #define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
81 #define DEFAULT_WINDOWSIZE -15
82 #define DEFAULT_MEMLEVEL 9
83 #define DEFAULT_BUFFERSIZE 8096
85 /* Outputs a long in LSB order to the given file
86 * only the bottom 4 bits are required for the deflate file format.
88 static void putLong(unsigned char *string, unsigned long x)
90 string[0] = (unsigned char)(x & 0xff);
91 string[1] = (unsigned char)((x & 0xff00) >> 8);
92 string[2] = (unsigned char)((x & 0xff0000) >> 16);
93 string[3] = (unsigned char)((x & 0xff000000) >> 24);
96 /* Inputs a string and returns a long.
98 static unsigned long getLong(unsigned char *string)
100 return ((unsigned long)string[0])
101 | (((unsigned long)string[1]) << 8)
102 | (((unsigned long)string[2]) << 16)
103 | (((unsigned long)string[3]) << 24);
106 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
108 deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
110 c->memlevel = DEFAULT_MEMLEVEL;
111 c->windowSize = DEFAULT_WINDOWSIZE;
112 c->bufferSize = DEFAULT_BUFFERSIZE;
113 c->compressionlevel = DEFAULT_COMPRESSION;
118 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
121 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
128 return "DeflateWindowSize must be between 1 and 15";
130 c->windowSize = i * -1;
135 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
138 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
143 return "DeflateBufferSize should be positive";
146 c->bufferSize = (apr_size_t)n;
150 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
151 const char *arg1, const char *arg2)
153 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
157 c->note_ratio_name = apr_pstrdup(cmd->pool, arg1);
159 else if (!strcasecmp(arg1, "ratio")) {
160 c->note_ratio_name = apr_pstrdup(cmd->pool, arg2);
162 else if (!strcasecmp(arg1, "input")) {
163 c->note_input_name = apr_pstrdup(cmd->pool, arg2);
165 else if (!strcasecmp(arg1, "output")) {
166 c->note_output_name = apr_pstrdup(cmd->pool, arg2);
169 return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
175 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
178 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
185 return "DeflateMemLevel must be between 1 and 9";
192 static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
195 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
202 return "Compression Level must be between 1 and 9";
204 c->compressionlevel = i;
209 typedef struct deflate_ctx_t
212 unsigned char *buffer;
214 apr_bucket_brigade *bb, *proc_bb;
215 int (*libz_end_func)(z_streamp);
216 unsigned char *validation_buffer;
217 apr_size_t validation_buffer_length;
220 /* Number of validation bytes (CRC and length) after the compressed data */
221 #define VALIDATION_SIZE 8
222 /* Do not update ctx->crc, see comment in flush_libz_buffer */
223 #define NO_UPDATE_CRC 0
224 /* Do update ctx->crc, see comment in flush_libz_buffer */
227 static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
228 struct apr_bucket_alloc_t *bucket_alloc,
229 int (*libz_func)(z_streamp, int), int flush,
234 unsigned int deflate_len;
238 deflate_len = c->bufferSize - ctx->stream.avail_out;
240 if (deflate_len != 0) {
242 * Do we need to update ctx->crc? Usually this is the case for
243 * inflate action where we need to do a crc on the output, whereas
244 * in the deflate case we need to do a crc on the input
247 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer,
250 b = apr_bucket_heap_create((char *)ctx->buffer,
253 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
254 ctx->stream.next_out = ctx->buffer;
255 ctx->stream.avail_out = c->bufferSize;
261 zRC = libz_func(&ctx->stream, flush);
264 * We can ignore Z_BUF_ERROR because:
265 * When we call libz_func we can assume that
267 * - avail_in is zero (due to the surrounding code that calls
269 * - avail_out is non zero due to our actions some lines above
271 * So the only reason for Z_BUF_ERROR is that the internal libz
272 * buffers are now empty and thus we called libz_func one time
273 * too often. This does not hurt. It simply says that we are done.
275 if (zRC == Z_BUF_ERROR) {
280 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
282 if (zRC != Z_OK && zRC != Z_STREAM_END)
288 static apr_status_t deflate_ctx_cleanup(void *data)
290 deflate_ctx *ctx = (deflate_ctx *)data;
293 ctx->libz_end_func(&ctx->stream);
297 static apr_status_t deflate_out_filter(ap_filter_t *f,
298 apr_bucket_brigade *bb)
301 request_rec *r = f->r;
302 deflate_ctx *ctx = f->ctx;
304 deflate_filter_config *c;
306 /* Do nothing if asked to filter nothing. */
307 if (APR_BRIGADE_EMPTY(bb)) {
308 return ap_pass_brigade(f->next, bb);
311 c = ap_get_module_config(r->server->module_config,
314 /* If we don't have a context, we need to ensure that it is okay to send
315 * the deflated content. If we have a context, that means we've done
316 * this before and we liked it.
317 * This could be not so nice if we always fail. But, if we succeed,
318 * we're in better shape.
322 const char *encoding;
324 /* only work on main request/no subrequests */
325 if (r->main != NULL) {
326 ap_remove_output_filter(f);
327 return ap_pass_brigade(f->next, bb);
330 /* some browsers might have problems, so set no-gzip
331 * (with browsermatch) for them
333 if (apr_table_get(r->subprocess_env, "no-gzip")) {
334 ap_remove_output_filter(f);
335 return ap_pass_brigade(f->next, bb);
338 /* Some browsers might have problems with content types
339 * other than text/html, so set gzip-only-text/html
340 * (with browsermatch) for them
342 if (r->content_type == NULL
343 || strncmp(r->content_type, "text/html", 9)) {
344 const char *env_value = apr_table_get(r->subprocess_env,
345 "gzip-only-text/html");
346 if ( env_value && (strcmp(env_value,"1") == 0) ) {
347 ap_remove_output_filter(f);
348 return ap_pass_brigade(f->next, bb);
352 /* Let's see what our current Content-Encoding is.
353 * If it's already encoded, don't compress again.
354 * (We could, but let's not.)
356 encoding = apr_table_get(r->headers_out, "Content-Encoding");
360 err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
362 encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
366 encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
369 if (r->content_encoding) {
370 encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
371 r->content_encoding, NULL)
372 : r->content_encoding;
376 const char *tmp = encoding;
378 token = ap_get_token(r->pool, &tmp, 0);
379 while (token && *token) {
380 /* stolen from mod_negotiation: */
381 if (strcmp(token, "identity") && strcmp(token, "7bit") &&
382 strcmp(token, "8bit") && strcmp(token, "binary")) {
384 ap_remove_output_filter(f);
385 return ap_pass_brigade(f->next, bb);
388 /* Otherwise, skip token */
392 token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
396 /* Even if we don't accept this request based on it not having
397 * the Accept-Encoding, we need to note that we were looking
398 * for this header and downstream proxies should be aware of that.
400 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
402 /* force-gzip will just force it out regardless if the browser
403 * can actually do anything with it.
405 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
407 /* if they don't have the line, then they can't play */
408 accepts = apr_table_get(r->headers_in, "Accept-Encoding");
409 if (accepts == NULL) {
410 ap_remove_output_filter(f);
411 return ap_pass_brigade(f->next, bb);
414 token = ap_get_token(r->pool, &accepts, 0);
415 while (token && token[0] && strcasecmp(token, "gzip")) {
416 /* skip parameters, XXX: ;q=foo evaluation? */
417 while (*accepts == ';') {
419 token = ap_get_token(r->pool, &accepts, 1);
422 /* retrieve next token */
423 if (*accepts == ',') {
426 token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
429 /* No acceptable token found. */
430 if (token == NULL || token[0] == '\0') {
431 ap_remove_output_filter(f);
432 return ap_pass_brigade(f->next, bb);
436 /* For a 304 or 204 response there is no entity included in
437 * the response and hence nothing to deflate. */
438 if (r->status == HTTP_NOT_MODIFIED || r->status == HTTP_NO_CONTENT) {
439 ap_remove_output_filter(f);
440 return ap_pass_brigade(f->next, bb);
443 /* We're cool with filtering this. */
444 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
445 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
446 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
447 ctx->libz_end_func = deflateEnd;
449 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
450 c->windowSize, c->memlevel,
454 deflateEnd(&ctx->stream);
455 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
456 "unable to init Zlib: "
457 "deflateInit2 returned %d: URL %s",
460 * Remove ourselves as it does not make sense to return:
461 * We are not able to init libz and pass data down the chain
464 ap_remove_output_filter(f);
465 return ap_pass_brigade(f->next, bb);
468 * Register a cleanup function to ensure that we cleanup the internal
471 apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
472 apr_pool_cleanup_null);
474 /* add immortal gzip header */
475 e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
477 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
479 /* If the entire Content-Encoding is "identity", we can replace it. */
480 if (!encoding || !strcasecmp(encoding, "identity")) {
481 apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
484 apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
486 apr_table_unset(r->headers_out, "Content-Length");
488 /* initialize deflate output buffer */
489 ctx->stream.next_out = ctx->buffer;
490 ctx->stream.avail_out = c->bufferSize;
493 while (!APR_BRIGADE_EMPTY(bb))
499 e = APR_BRIGADE_FIRST(bb);
501 if (APR_BUCKET_IS_EOS(e)) {
504 ctx->stream.avail_in = 0; /* should be zero already anyway */
505 /* flush the remaining data from the zlib buffers */
506 flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
509 buf = apr_palloc(r->pool, VALIDATION_SIZE);
510 putLong((unsigned char *)&buf[0], ctx->crc);
511 putLong((unsigned char *)&buf[4], ctx->stream.total_in);
513 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
515 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
516 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
517 "Zlib: Compressed %ld to %ld : URL %s",
518 ctx->stream.total_in, ctx->stream.total_out, r->uri);
520 /* leave notes for logging */
521 if (c->note_input_name) {
522 apr_table_setn(r->notes, c->note_input_name,
523 (ctx->stream.total_in > 0)
524 ? apr_off_t_toa(r->pool,
525 ctx->stream.total_in)
529 if (c->note_output_name) {
530 apr_table_setn(r->notes, c->note_output_name,
531 (ctx->stream.total_in > 0)
532 ? apr_off_t_toa(r->pool,
533 ctx->stream.total_out)
537 if (c->note_ratio_name) {
538 apr_table_setn(r->notes, c->note_ratio_name,
539 (ctx->stream.total_in > 0)
541 (int)(ctx->stream.total_out
543 / ctx->stream.total_in))
547 deflateEnd(&ctx->stream);
548 /* No need for cleanup any longer */
549 apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
551 /* Remove EOS from the old list, and insert into the new. */
552 APR_BUCKET_REMOVE(e);
553 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
555 /* Okay, we've seen the EOS.
556 * Time to pass it along down the chain.
558 return ap_pass_brigade(f->next, ctx->bb);
561 if (APR_BUCKET_IS_FLUSH(e)) {
564 /* flush the remaining data from the zlib buffers */
565 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
566 Z_SYNC_FLUSH, NO_UPDATE_CRC);
571 /* Remove flush bucket from old brigade anf insert into the new. */
572 APR_BUCKET_REMOVE(e);
573 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
574 rv = ap_pass_brigade(f->next, ctx->bb);
575 if (rv != APR_SUCCESS) {
582 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
584 /* This crc32 function is from zlib. */
585 ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
588 ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
589 * but we'll just have to
591 ctx->stream.avail_in = len;
593 while (ctx->stream.avail_in != 0) {
594 if (ctx->stream.avail_out == 0) {
597 ctx->stream.next_out = ctx->buffer;
598 len = c->bufferSize - ctx->stream.avail_out;
600 b = apr_bucket_heap_create((char *)ctx->buffer, len,
601 NULL, f->c->bucket_alloc);
602 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
603 ctx->stream.avail_out = c->bufferSize;
604 /* Send what we have right now to the next filter. */
605 rv = ap_pass_brigade(f->next, ctx->bb);
606 if (rv != APR_SUCCESS) {
611 zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
618 apr_bucket_delete(e);
621 apr_brigade_cleanup(bb);
625 /* This is the deflate input filter (inflates). */
626 static apr_status_t deflate_in_filter(ap_filter_t *f,
627 apr_bucket_brigade *bb,
628 ap_input_mode_t mode,
629 apr_read_type_e block,
633 request_rec *r = f->r;
634 deflate_ctx *ctx = f->ctx;
637 deflate_filter_config *c;
639 /* just get out of the way of things we don't want. */
640 if (mode != AP_MODE_READBYTES) {
641 return ap_get_brigade(f->next, bb, mode, block, readbytes);
644 c = ap_get_module_config(r->server->module_config, &deflate_module);
648 char *token, deflate_hdr[10];
649 const char *encoding;
652 /* only work on main request/no subrequests */
653 if (!ap_is_initial_req(r)) {
654 ap_remove_input_filter(f);
655 return ap_get_brigade(f->next, bb, mode, block, readbytes);
658 /* Let's see what our current Content-Encoding is.
659 * If gzip is present, don't gzip again. (We could, but let's not.)
661 encoding = apr_table_get(r->headers_in, "Content-Encoding");
663 const char *tmp = encoding;
665 token = ap_get_token(r->pool, &tmp, 0);
666 while (token && token[0]) {
667 if (!strcasecmp(token, "gzip")) {
671 /* Otherwise, skip token */
673 token = ap_get_token(r->pool, &tmp, 0);
678 ap_remove_input_filter(f);
679 return ap_get_brigade(f->next, bb, mode, block, readbytes);
682 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
683 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
684 ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
685 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
687 rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
688 if (rv != APR_SUCCESS) {
693 rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
694 if (rv != APR_SUCCESS) {
698 /* We didn't get the magic bytes. */
700 deflate_hdr[0] != deflate_magic[0] ||
701 deflate_hdr[1] != deflate_magic[1]) {
705 /* We can't handle flags for now. */
706 if (deflate_hdr[3] != 0) {
710 zRC = inflateInit2(&ctx->stream, c->windowSize);
714 inflateEnd(&ctx->stream);
715 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
716 "unable to init Zlib: "
717 "inflateInit2 returned %d: URL %s",
719 ap_remove_input_filter(f);
720 return ap_get_brigade(f->next, bb, mode, block, readbytes);
723 /* initialize deflate output buffer */
724 ctx->stream.next_out = ctx->buffer;
725 ctx->stream.avail_out = c->bufferSize;
727 apr_brigade_cleanup(ctx->bb);
730 if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
731 rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
733 if (rv != APR_SUCCESS) {
734 /* What about APR_EAGAIN errors? */
735 inflateEnd(&ctx->stream);
739 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
740 bkt != APR_BRIGADE_SENTINEL(ctx->bb);
741 bkt = APR_BUCKET_NEXT(bkt))
746 /* If we actually see the EOS, that means we screwed up! */
747 if (APR_BUCKET_IS_EOS(bkt)) {
748 inflateEnd(&ctx->stream);
752 if (APR_BUCKET_IS_FLUSH(bkt)) {
753 apr_bucket *tmp_heap;
754 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
756 inflateEnd(&ctx->stream);
760 ctx->stream.next_out = ctx->buffer;
761 len = c->bufferSize - ctx->stream.avail_out;
763 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
764 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
765 NULL, f->c->bucket_alloc);
766 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
767 ctx->stream.avail_out = c->bufferSize;
769 /* Move everything to the returning brigade. */
770 APR_BUCKET_REMOVE(bkt);
771 APR_BRIGADE_CONCAT(bb, ctx->bb);
776 apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
778 /* pass through zlib inflate. */
779 ctx->stream.next_in = (unsigned char *)data;
780 ctx->stream.avail_in = len;
784 while (ctx->stream.avail_in != 0) {
785 if (ctx->stream.avail_out == 0) {
786 apr_bucket *tmp_heap;
787 ctx->stream.next_out = ctx->buffer;
788 len = c->bufferSize - ctx->stream.avail_out;
790 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
791 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
792 NULL, f->c->bucket_alloc);
793 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
794 ctx->stream.avail_out = c->bufferSize;
797 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
799 if (zRC == Z_STREAM_END) {
804 inflateEnd(&ctx->stream);
808 if (zRC == Z_STREAM_END) {
809 apr_bucket *tmp_heap, *eos;
811 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
812 "Zlib: Inflated %ld to %ld : URL %s",
813 ctx->stream.total_in, ctx->stream.total_out,
816 len = c->bufferSize - ctx->stream.avail_out;
818 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
819 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
820 NULL, f->c->bucket_alloc);
821 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
822 ctx->stream.avail_out = c->bufferSize;
824 /* Is the remaining 8 bytes already in the avail stream? */
825 if (ctx->stream.avail_in >= 8) {
826 unsigned long compCRC, compLen;
827 compCRC = getLong(ctx->stream.next_in);
828 if (ctx->crc != compCRC) {
829 inflateEnd(&ctx->stream);
832 ctx->stream.next_in += 4;
833 compLen = getLong(ctx->stream.next_in);
834 if (ctx->stream.total_out != compLen) {
835 inflateEnd(&ctx->stream);
840 /* FIXME: We need to grab the 8 verification bytes
842 inflateEnd(&ctx->stream);
846 inflateEnd(&ctx->stream);
848 eos = apr_bucket_eos_create(f->c->bucket_alloc);
849 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
854 apr_brigade_cleanup(ctx->bb);
857 /* If we are about to return nothing for a 'blocking' read and we have
858 * some data in our zlib buffer, flush it out so we can return something.
860 if (block == APR_BLOCK_READ &&
861 APR_BRIGADE_EMPTY(ctx->proc_bb) &&
862 ctx->stream.avail_out < c->bufferSize) {
863 apr_bucket *tmp_heap;
865 ctx->stream.next_out = ctx->buffer;
866 len = c->bufferSize - ctx->stream.avail_out;
868 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
869 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
870 NULL, f->c->bucket_alloc);
871 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
872 ctx->stream.avail_out = c->bufferSize;
875 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
876 apr_bucket_brigade *newbb;
878 /* May return APR_INCOMPLETE which is fine by us. */
879 apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
881 newbb = apr_brigade_split(ctx->proc_bb, bkt);
882 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
883 APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
890 /* Filter to inflate for a content-transforming proxy. */
891 static apr_status_t inflate_out_filter(ap_filter_t *f,
892 apr_bucket_brigade *bb)
896 int inflate_init = 1;
898 request_rec *r = f->r;
899 deflate_ctx *ctx = f->ctx;
902 deflate_filter_config *c;
904 /* Do nothing if asked to filter nothing. */
905 if (APR_BRIGADE_EMPTY(bb)) {
906 return ap_pass_brigade(f->next, bb);
909 c = ap_get_module_config(r->server->module_config, &deflate_module);
914 const char *encoding;
916 /* only work on main request/no subrequests */
917 if (!ap_is_initial_req(r)) {
918 ap_remove_output_filter(f);
919 return ap_pass_brigade(f->next, bb);
922 /* Let's see what our current Content-Encoding is.
923 * If gzip is present, don't gzip again. (We could, but let's not.)
925 encoding = apr_table_get(r->headers_out, "Content-Encoding");
927 const char *tmp = encoding;
929 token = ap_get_token(r->pool, &tmp, 0);
930 while (token && token[0]) {
931 if (!strcasecmp(token, "gzip")) {
935 /* Otherwise, skip token */
937 token = ap_get_token(r->pool, &tmp, 0);
942 ap_remove_output_filter(f);
943 return ap_pass_brigade(f->next, bb);
945 apr_table_unset(r->headers_out, "Content-Encoding");
947 /* No need to inflate HEAD or 204/304 */
948 if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
949 ap_remove_output_filter(f);
950 return ap_pass_brigade(f->next, bb);
954 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
955 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
956 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
957 ctx->libz_end_func = inflateEnd;
958 ctx->validation_buffer = NULL;
959 ctx->validation_buffer_length = 0;
961 zRC = inflateInit2(&ctx->stream, c->windowSize);
965 inflateEnd(&ctx->stream);
966 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
967 "unable to init Zlib: "
968 "inflateInit2 returned %d: URL %s",
971 * Remove ourselves as it does not make sense to return:
972 * We are not able to init libz and pass data down the chain
975 ap_remove_output_filter(f);
976 return ap_pass_brigade(f->next, bb);
980 * Register a cleanup function to ensure that we cleanup the internal
983 apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
984 apr_pool_cleanup_null);
986 apr_table_unset(r->headers_out, "Content-Length");
988 /* initialize inflate output buffer */
989 ctx->stream.next_out = ctx->buffer;
990 ctx->stream.avail_out = c->bufferSize;
995 while (!APR_BRIGADE_EMPTY(bb))
1001 e = APR_BRIGADE_FIRST(bb);
1003 if (APR_BUCKET_IS_EOS(e)) {
1004 ctx->stream.avail_in = 0; /* should be zero already anyway */
1006 * Flush the remaining data from the zlib buffers. It is correct
1007 * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1008 * deflate case. In the inflate case Z_FINISH requires to have a
1009 * large enough output buffer to put ALL data in otherwise it
1010 * fails, whereas in the deflate case you can empty a filled output
1011 * buffer and call it again until no more output can be created.
1013 flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
1015 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1016 "Zlib: Inflated %ld to %ld : URL %s",
1017 ctx->stream.total_in, ctx->stream.total_out, r->uri);
1019 if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1020 unsigned long compCRC, compLen;
1021 compCRC = getLong(ctx->validation_buffer);
1022 if (ctx->crc != compCRC) {
1023 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1024 "Zlib: Checksum of inflated stream invalid");
1025 return APR_EGENERAL;
1027 ctx->validation_buffer += VALIDATION_SIZE / 2;
1028 compLen = getLong(ctx->validation_buffer);
1029 if (ctx->stream.total_out != compLen) {
1030 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1031 "Zlib: Length of inflated stream invalid");
1032 return APR_EGENERAL;
1036 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1037 "Zlib: Validation bytes not present");
1038 return APR_EGENERAL;
1041 inflateEnd(&ctx->stream);
1042 /* No need for cleanup any longer */
1043 apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1045 /* Remove EOS from the old list, and insert into the new. */
1046 APR_BUCKET_REMOVE(e);
1047 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1050 * Okay, we've seen the EOS.
1051 * Time to pass it along down the chain.
1053 return ap_pass_brigade(f->next, ctx->bb);
1056 if (APR_BUCKET_IS_FLUSH(e)) {
1059 /* flush the remaining data from the zlib buffers */
1060 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
1061 Z_SYNC_FLUSH, UPDATE_CRC);
1063 return APR_EGENERAL;
1066 /* Remove flush bucket from old brigade anf insert into the new. */
1067 APR_BUCKET_REMOVE(e);
1068 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1069 rv = ap_pass_brigade(f->next, ctx->bb);
1070 if (rv != APR_SUCCESS) {
1077 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1079 /* first bucket contains zlib header */
1080 if (!inflate_init++) {
1082 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1083 "Insufficient data for inflate");
1084 return APR_EGENERAL;
1087 zlib_method = data[2];
1088 zlib_flags = data[3];
1089 if (zlib_method != Z_DEFLATED) {
1090 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1091 "inflate: data not deflated!");
1092 ap_remove_output_filter(f);
1093 return ap_pass_brigade(f->next, bb);
1095 if (data[0] != deflate_magic[0] ||
1096 data[1] != deflate_magic[1] ||
1097 (zlib_flags & RESERVED) != 0) {
1098 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1099 "inflate: bad header");
1100 return APR_EGENERAL ;
1105 if (zlib_flags & EXTRA_FIELD) {
1106 unsigned int bytes = (unsigned int)(data[0]);
1107 bytes += ((unsigned int)(data[1])) << 8;
1110 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1111 "inflate: extra field too big (not "
1113 return APR_EGENERAL;
1118 if (zlib_flags & ORIG_NAME) {
1119 while (len-- && *data++);
1121 if (zlib_flags & COMMENT) {
1122 while (len-- && *data++);
1124 if (zlib_flags & HEAD_CRC) {
1130 /* pass through zlib inflate. */
1131 ctx->stream.next_in = (unsigned char *)data;
1132 ctx->stream.avail_in = len;
1134 if (ctx->validation_buffer) {
1135 if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1136 apr_size_t copy_size;
1138 copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1139 if (copy_size > ctx->stream.avail_in)
1140 copy_size = ctx->stream.avail_in;
1141 memcpy(ctx->validation_buffer, ctx->stream.next_in, copy_size);
1143 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1144 "Zlib: %d bytes of garbage at the end of "
1145 "compressed stream.", ctx->stream.avail_in);
1151 while (ctx->stream.avail_in != 0) {
1152 if (ctx->stream.avail_out == 0) {
1154 ctx->stream.next_out = ctx->buffer;
1155 len = c->bufferSize - ctx->stream.avail_out;
1157 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1158 b = apr_bucket_heap_create((char *)ctx->buffer, len,
1159 NULL, f->c->bucket_alloc);
1160 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
1161 ctx->stream.avail_out = c->bufferSize;
1162 /* Send what we have right now to the next filter. */
1163 rv = ap_pass_brigade(f->next, ctx->bb);
1164 if (rv != APR_SUCCESS) {
1169 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1171 if (zRC == Z_STREAM_END) {
1173 * We have inflated all data. Now try to capture the
1174 * validation bytes. We may not have them all available
1175 * right now, but capture what is there.
1177 ctx->validation_buffer = apr_pcalloc(f->r->pool,
1179 if (ctx->stream.avail_in > VALIDATION_SIZE) {
1180 ctx->validation_buffer_length = VALIDATION_SIZE;
1181 } else if (ctx->stream.avail_in > 0) {
1182 ctx->validation_buffer_length = ctx->stream.avail_in;
1184 if (ctx->validation_buffer_length)
1185 memcpy(ctx->validation_buffer, ctx->stream.next_in,
1186 ctx->validation_buffer_length);
1191 return APR_EGENERAL;
1195 apr_bucket_delete(e);
1198 apr_brigade_cleanup(bb);
1202 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1203 static void register_hooks(apr_pool_t *p)
1205 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1206 AP_FTYPE_CONTENT_SET);
1207 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1208 AP_FTYPE_RESOURCE-1);
1209 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1210 AP_FTYPE_CONTENT_SET);
1213 static const command_rec deflate_filter_cmds[] = {
1214 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1215 "Set a note to report on compression ratio"),
1216 AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1217 RSRC_CONF, "Set the Deflate window size (1-15)"),
1218 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1219 "Set the Deflate Buffer Size"),
1220 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1221 "Set the Deflate Memory Level (1-9)"),
1222 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1223 "Set the Deflate Compression Level (1-9)"),
1227 module AP_MODULE_DECLARE_DATA deflate_module = {
1228 STANDARD20_MODULE_STUFF,
1229 NULL, /* dir config creater */
1230 NULL, /* dir merger --- default is to override */
1231 create_deflate_server_config, /* server config */
1232 NULL, /* merge server config */
1233 deflate_filter_cmds, /* command table */
1234 register_hooks /* register hooks */