1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * (zlib functions gz_open and gzwrite)
59 * mod_deflate.c: Perform deflate transfer-encoding on the fly
61 * Written by Ian Holsman (IanH@apache.org)
66 #include "http_config.h"
68 #include "apr_strings.h"
69 #include "apr_general.h"
70 #include "util_filter.h"
71 #include "apr_buckets.h"
72 #include "http_request.h"
79 /* As part of the encoding process, we must send what our OS_CODE is
80 * (or so it seems based on what I can tell of how gzip encoding works).
82 * zutil.h is not always included with zlib distributions (it is a private
83 * header), so this is straight from zlib 1.1.3's zutil.h.
89 #ifdef WIN32 /* Window 95 & Windows NT */
93 #if defined(VAXC) || defined(VMS)
101 #if defined(ATARI) || defined(atarist)
105 #if defined(MACOS) || defined(TARGET_OS_MAC)
109 #ifdef __50SERIES /* Prime/PRIMOS */
118 #define OS_CODE 0x03 /* assume Unix */
122 static const char deflateFilterName[] = "DEFLATE";
123 module AP_MODULE_DECLARE_DATA deflate_module;
125 typedef struct deflate_filter_config_t
131 } deflate_filter_config;
133 /* windowsize is negative to suppress Zlib header */
134 #define DEFAULT_WINDOWSIZE -15
135 #define DEFAULT_MEMLEVEL 9
136 #define DEFAULT_BUFFERSIZE 8096
138 /* Outputs a long in LSB order to the given file
139 * only the bottom 4 bits are required for the deflate file format.
141 static void putLong(unsigned char *string, unsigned long x)
143 string[0] = (unsigned char)(x & 0xff);
144 string[1] = (unsigned char)((x & 0xff00) >> 8);
145 string[2] = (unsigned char)((x & 0xff0000) >> 16);
146 string[3] = (unsigned char)((x & 0xff000000) >> 24);
149 /* Inputs a string and returns a long.
151 static unsigned long getLong(unsigned char *string)
153 return ((unsigned long)string[0])
154 | (((unsigned long)string[1]) << 8)
155 | (((unsigned long)string[2]) << 16)
156 | (((unsigned long)string[3]) << 24);
159 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
161 deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
163 c->memlevel = DEFAULT_MEMLEVEL;
164 c->windowSize = DEFAULT_WINDOWSIZE;
165 c->bufferSize = DEFAULT_BUFFERSIZE;
170 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
173 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
180 return "DeflateWindowSize must be between 1 and 15";
182 c->windowSize = i * -1;
187 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
190 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
193 c->bufferSize = atoi(arg);
195 if (c->bufferSize <= 0) {
196 return "DeflateBufferSize should be positive";
201 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
204 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
206 c->noteName = apr_pstrdup(cmd->pool, arg);
211 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
214 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
221 return "DeflateMemLevel must be between 1 and 9";
229 static char deflate_magic[2] = { '\037', '\213' };
231 typedef struct deflate_ctx_t
234 unsigned char *buffer;
236 apr_bucket_brigade *bb, *proc_bb;
239 static apr_status_t deflate_out_filter(ap_filter_t *f,
240 apr_bucket_brigade *bb)
243 request_rec *r = f->r;
244 deflate_ctx *ctx = f->ctx;
246 deflate_filter_config *c = ap_get_module_config(r->server->module_config,
249 /* If we don't have a context, we need to ensure that it is okay to send
250 * the deflated content. If we have a context, that means we've done
251 * this before and we liked it.
252 * This could be not so nice if we always fail. But, if we succeed,
253 * we're in better shape.
257 const char *encoding, *accepts;
259 /* only work on main request/no subrequests */
261 ap_remove_output_filter(f);
262 return ap_pass_brigade(f->next, bb);
265 /* some browsers might have problems, so set no-gzip
266 * (with browsermatch) for them
268 if (apr_table_get(r->subprocess_env, "no-gzip")) {
269 ap_remove_output_filter(f);
270 return ap_pass_brigade(f->next, bb);
273 /* Some browsers might have problems with content types
274 * other than text/html, so set gzip-only-text/html
275 * (with browsermatch) for them
277 if ((r->content_type == NULL
278 || strncmp(r->content_type, "text/html", 9))
279 && apr_table_get(r->subprocess_env, "gzip-only-text/html")) {
280 ap_remove_output_filter(f);
281 return ap_pass_brigade(f->next, bb);
284 /* Let's see what our current Content-Encoding is.
285 * If gzip is present, don't gzip again. (We could, but let's not.)
287 encoding = apr_table_get(r->headers_out, "Content-Encoding");
289 const char *tmp = encoding;
291 token = ap_get_token(r->pool, &tmp, 0);
292 while (token && token[0]) {
293 if (!strcasecmp(token, "gzip")) {
294 ap_remove_output_filter(f);
295 return ap_pass_brigade(f->next, bb);
297 /* Otherwise, skip token */
299 token = ap_get_token(r->pool, &tmp, 0);
303 /* if they don't have the line, then they can't play */
304 accepts = apr_table_get(r->headers_in, "Accept-Encoding");
305 if (accepts == NULL) {
306 ap_remove_output_filter(f);
307 return ap_pass_brigade(f->next, bb);
310 token = ap_get_token(r->pool, &accepts, 0);
311 while (token && token[0] && strcasecmp(token, "gzip")) {
314 token = ap_get_token(r->pool, &accepts, 0);
317 /* No acceptable token found. */
318 if (token == NULL || token[0] == '\0') {
319 ap_remove_output_filter(f);
320 return ap_pass_brigade(f->next, bb);
323 /* We're cool with filtering this. */
324 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
325 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
326 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
328 zRC = deflateInit2(&ctx->stream, Z_BEST_SPEED, Z_DEFLATED,
329 c->windowSize, c->memlevel,
334 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
335 "unable to init Zlib: "
336 "deflateInit2 returned %d: URL %s",
338 return ap_pass_brigade(f->next, bb);
341 /* RFC 1952 Section 2.3 dictates the gzip header:
343 * +---+---+---+---+---+---+---+---+---+---+
344 * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
345 * +---+---+---+---+---+---+---+---+---+---+
347 * If we wish to populate in MTIME (as hinted in RFC 1952), do:
348 * putLong(date_array, apr_time_now() / APR_USEC_PER_SEC);
349 * where date_array is a char[4] and then print date_array in the
352 buf = apr_psprintf(r->pool, "%c%c%c%c%c%c%c%c%c%c", deflate_magic[0],
353 deflate_magic[1], Z_DEFLATED, 0 /* flags */,
354 0, 0, 0, 0 /* 4 chars for mtime */,
355 0 /* xflags */, OS_CODE);
356 e = apr_bucket_pool_create(buf, 10, r->pool, f->c->bucket_alloc);
357 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
359 /* If the entire Content-Encoding is "identity", we can replace it. */
360 if (!encoding || !strcasecmp(encoding, "identity")) {
361 apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
364 apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
366 apr_table_setn(r->headers_out, "Vary", "Accept-Encoding");
367 apr_table_unset(r->headers_out, "Content-Length");
369 /* initialize deflate output buffer */
370 ctx->stream.next_out = ctx->buffer;
371 ctx->stream.avail_out = c->bufferSize;
374 APR_BRIGADE_FOREACH(e, bb) {
381 if (APR_BUCKET_IS_EOS(e)) {
383 unsigned int deflate_len;
385 ctx->stream.avail_in = 0; /* should be zero already anyway */
387 deflate_len = c->bufferSize - ctx->stream.avail_out;
389 if (deflate_len != 0) {
390 b = apr_bucket_heap_create((char *)ctx->buffer,
393 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
394 ctx->stream.next_out = ctx->buffer;
395 ctx->stream.avail_out = c->bufferSize;
402 zRC = deflate(&ctx->stream, Z_FINISH);
404 if (deflate_len == 0 && zRC == Z_BUF_ERROR) {
408 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
410 if (zRC != Z_OK && zRC != Z_STREAM_END) {
415 buf = apr_palloc(r->pool, 8);
416 putLong((unsigned char *)&buf[0], ctx->crc);
417 putLong((unsigned char *)&buf[4], ctx->stream.total_in);
419 b = apr_bucket_pool_create(buf, 8, r->pool, f->c->bucket_alloc);
420 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
421 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
422 "Zlib: Compressed %ld to %ld : URL %s",
423 ctx->stream.total_in, ctx->stream.total_out, r->uri);
426 if (ctx->stream.total_in > 0) {
429 total = ctx->stream.total_out * 100 / ctx->stream.total_in;
431 apr_table_setn(r->notes, c->noteName,
432 apr_itoa(r->pool, total));
435 apr_table_setn(r->notes, c->noteName, "-");
439 deflateEnd(&ctx->stream);
441 /* Remove EOS from the old list, and insert into the new. */
442 APR_BUCKET_REMOVE(e);
443 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
445 /* Okay, we've seen the EOS.
446 * Time to pass it along down the chain.
448 return ap_pass_brigade(f->next, ctx->bb);
451 if (APR_BUCKET_IS_FLUSH(e)) {
453 zRC = deflate(&(ctx->stream), Z_SYNC_FLUSH);
458 ctx->stream.next_out = ctx->buffer;
459 len = c->bufferSize - ctx->stream.avail_out;
461 b = apr_bucket_heap_create((char *)ctx->buffer, len,
462 NULL, f->c->bucket_alloc);
463 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
464 ctx->stream.avail_out = c->bufferSize;
466 bkt = apr_bucket_flush_create(f->c->bucket_alloc);
467 APR_BRIGADE_INSERT_TAIL(ctx->bb, bkt);
468 ap_pass_brigade(f->next, ctx->bb);
473 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
475 /* This crc32 function is from zlib. */
476 ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
479 ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
480 * but we'll just have to
482 ctx->stream.avail_in = len;
484 while (ctx->stream.avail_in != 0) {
485 if (ctx->stream.avail_out == 0) {
486 ctx->stream.next_out = ctx->buffer;
487 len = c->bufferSize - ctx->stream.avail_out;
489 b = apr_bucket_heap_create((char *)ctx->buffer, len,
490 NULL, f->c->bucket_alloc);
491 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
492 ctx->stream.avail_out = c->bufferSize;
495 zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
502 apr_brigade_destroy(bb);
506 /* This is the deflate input filter (inflates). */
507 static apr_status_t deflate_in_filter(ap_filter_t *f,
508 apr_bucket_brigade *bb,
509 ap_input_mode_t mode,
510 apr_read_type_e block,
514 request_rec *r = f->r;
515 deflate_ctx *ctx = f->ctx;
518 deflate_filter_config *c;
520 /* just get out of the way of things we don't want. */
521 if (mode != AP_MODE_READBYTES) {
522 return ap_get_brigade(f->next, bb, mode, block, readbytes);
525 c = ap_get_module_config(r->server->module_config, &deflate_module);
529 char *token, deflate_hdr[10];
530 const char *encoding;
533 /* only work on main request/no subrequests */
535 ap_remove_input_filter(f);
536 return ap_get_brigade(f->next, bb, mode, block, readbytes);
539 /* Let's see what our current Content-Encoding is.
540 * If gzip is present, don't gzip again. (We could, but let's not.)
542 encoding = apr_table_get(r->headers_in, "Content-Encoding");
544 const char *tmp = encoding;
546 token = ap_get_token(r->pool, &tmp, 0);
547 while (token && token[0]) {
548 if (!strcasecmp(token, "gzip")) {
552 /* Otherwise, skip token */
554 token = ap_get_token(r->pool, &tmp, 0);
559 ap_remove_input_filter(f);
560 return ap_get_brigade(f->next, bb, mode, block, readbytes);
563 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
564 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
565 ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
566 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
568 rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
569 if (rv != APR_SUCCESS) {
574 rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
575 if (rv != APR_SUCCESS) {
579 /* We didn't get the magic bytes. */
581 deflate_hdr[0] != deflate_magic[0] ||
582 deflate_hdr[1] != deflate_magic[1]) {
586 /* We can't handle flags for now. */
587 if (deflate_hdr[3] != 0) {
591 zRC = inflateInit2(&ctx->stream, c->windowSize);
595 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
596 "unable to init Zlib: "
597 "inflateInit2 returned %d: URL %s",
599 ap_remove_input_filter(f);
600 return ap_get_brigade(f->next, bb, mode, block, readbytes);
603 /* initialize deflate output buffer */
604 ctx->stream.next_out = ctx->buffer;
605 ctx->stream.avail_out = c->bufferSize;
607 apr_brigade_cleanup(ctx->bb);
610 if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
611 rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
613 if (rv != APR_SUCCESS) {
617 APR_BRIGADE_FOREACH(bkt, ctx->bb) {
621 /* If we actually see the EOS, that means we screwed up! */
622 if (APR_BUCKET_IS_EOS(bkt)) {
626 if (APR_BUCKET_IS_FLUSH(bkt)) {
627 apr_bucket *tmp_heap;
628 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
633 ctx->stream.next_out = ctx->buffer;
634 len = c->bufferSize - ctx->stream.avail_out;
636 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
637 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
638 NULL, f->c->bucket_alloc);
639 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
640 ctx->stream.avail_out = c->bufferSize;
642 /* Move everything to the returning brigade. */
643 APR_BUCKET_REMOVE(bkt);
644 APR_BRIGADE_CONCAT(bb, ctx->bb);
649 apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
651 /* pass through zlib inflate. */
652 ctx->stream.next_in = (unsigned char *)data;
653 ctx->stream.avail_in = len;
657 while (ctx->stream.avail_in != 0) {
658 if (ctx->stream.avail_out == 0) {
659 apr_bucket *tmp_heap;
660 ctx->stream.next_out = ctx->buffer;
661 len = c->bufferSize - ctx->stream.avail_out;
663 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
664 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
665 NULL, f->c->bucket_alloc);
666 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
667 ctx->stream.avail_out = c->bufferSize;
670 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
672 if (zRC == Z_STREAM_END) {
680 if (zRC == Z_STREAM_END) {
681 apr_bucket *tmp_heap, *eos;
683 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
684 "Zlib: Inflated %ld to %ld : URL %s",
685 ctx->stream.total_in, ctx->stream.total_out,
688 len = c->bufferSize - ctx->stream.avail_out;
690 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
691 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
692 NULL, f->c->bucket_alloc);
693 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
694 ctx->stream.avail_out = c->bufferSize;
696 /* Is the remaining 8 bytes already in the avail stream? */
697 if (ctx->stream.avail_in >= 8) {
698 unsigned long compCRC, compLen;
699 compCRC = getLong(ctx->stream.next_in);
700 if (ctx->crc != compCRC) {
703 ctx->stream.next_in += 4;
704 compLen = getLong(ctx->stream.next_in);
705 if (ctx->stream.total_out != compLen) {
710 /* FIXME: We need to grab the 8 verification bytes
715 inflateEnd(&ctx->stream);
717 eos = apr_bucket_eos_create(f->c->bucket_alloc);
718 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
722 apr_brigade_cleanup(ctx->bb);
725 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
726 apr_bucket_brigade *newbb;
728 /* May return APR_INCOMPLETE which is fine by us. */
729 apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
731 newbb = apr_brigade_split(ctx->proc_bb, bkt);
732 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
733 APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
739 static void register_hooks(apr_pool_t *p)
741 ap_register_output_filter(deflateFilterName, deflate_out_filter,
742 AP_FTYPE_CONTENT_SET);
743 ap_register_input_filter(deflateFilterName, deflate_in_filter,
744 AP_FTYPE_CONTENT_SET);
747 static const command_rec deflate_filter_cmds[] = {
748 AP_INIT_TAKE1("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
749 "Set a note to report on compression ratio"),
750 AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
751 RSRC_CONF, "Set the Deflate window size (1-15)"),
752 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
753 "Set the Deflate Buffer Size"),
754 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
755 "Set the Deflate Memory Level (1-9)"),
759 module AP_MODULE_DECLARE_DATA deflate_module = {
760 STANDARD20_MODULE_STUFF,
761 NULL, /* dir config creater */
762 NULL, /* dir merger --- default is to override */
763 create_deflate_server_config, /* server config */
764 NULL, /* merge server config */
765 deflate_filter_cmds, /* command table */
766 register_hooks /* register hooks */