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(char *string, unsigned long x)
144 for (n = 0; n < 4; n++) {
145 string[n] = (int) (x & 0xff);
150 /* Inputs a string and returns a long.
152 static unsigned long getLong(unsigned char *string)
158 x |= (unsigned long)(string[n--]) & 0xff;
162 x |= (unsigned long)(string[0]) & 0xff;
166 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
168 deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
170 c->memlevel = DEFAULT_MEMLEVEL;
171 c->windowSize = DEFAULT_WINDOWSIZE;
172 c->bufferSize = DEFAULT_BUFFERSIZE;
177 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
180 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
187 return "DeflateWindowSize must be between 1 and 15";
189 c->windowSize = i * -1;
194 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
197 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
200 c->bufferSize = atoi(arg);
202 if (c->bufferSize <= 0) {
203 return "DeflateBufferSize should be positive";
208 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
211 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
213 c->noteName = apr_pstrdup(cmd->pool, arg);
218 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
221 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
228 return "DeflateMemLevel must be between 1 and 9";
236 static char deflate_magic[2] = { '\037', '\213' };
238 typedef struct deflate_ctx_t
241 unsigned char *buffer;
243 apr_bucket_brigade *bb, *proc_bb;
246 static apr_status_t deflate_out_filter(ap_filter_t *f,
247 apr_bucket_brigade *bb)
250 request_rec *r = f->r;
251 deflate_ctx *ctx = f->ctx;
253 deflate_filter_config *c = ap_get_module_config(r->server->module_config,
256 /* If we don't have a context, we need to ensure that it is okay to send
257 * the deflated content. If we have a context, that means we've done
258 * this before and we liked it.
259 * This could be not so nice if we always fail. But, if we succeed,
260 * we're in better shape.
264 const char *encoding, *accepts;
266 /* only work on main request/no subrequests */
268 ap_remove_output_filter(f);
269 return ap_pass_brigade(f->next, bb);
272 /* some browsers might have problems, so set no-gzip
273 * (with browsermatch) for them
275 if (apr_table_get(r->subprocess_env, "no-gzip")) {
276 ap_remove_output_filter(f);
277 return ap_pass_brigade(f->next, bb);
280 /* Some browsers might have problems with content types
281 * other than text/html, so set gzip-only-text/html
282 * (with browsermatch) for them
284 if ((r->content_type == NULL
285 || strncmp(r->content_type, "text/html", 9))
286 && apr_table_get(r->subprocess_env, "gzip-only-text/html")) {
287 ap_remove_output_filter(f);
288 return ap_pass_brigade(f->next, bb);
291 /* Let's see what our current Content-Encoding is.
292 * If gzip is present, don't gzip again. (We could, but let's not.)
294 encoding = apr_table_get(r->headers_out, "Content-Encoding");
296 const char *tmp = encoding;
298 token = ap_get_token(r->pool, &tmp, 0);
299 while (token && token[0]) {
300 if (!strcasecmp(token, "gzip")) {
301 ap_remove_output_filter(f);
302 return ap_pass_brigade(f->next, bb);
304 /* Otherwise, skip token */
306 token = ap_get_token(r->pool, &tmp, 0);
310 /* if they don't have the line, then they can't play */
311 accepts = apr_table_get(r->headers_in, "Accept-Encoding");
312 if (accepts == NULL) {
313 ap_remove_output_filter(f);
314 return ap_pass_brigade(f->next, bb);
317 token = ap_get_token(r->pool, &accepts, 0);
318 while (token && token[0] && strcasecmp(token, "gzip")) {
321 token = ap_get_token(r->pool, &accepts, 0);
324 /* No acceptable token found. */
325 if (token == NULL || token[0] == '\0') {
326 ap_remove_output_filter(f);
327 return ap_pass_brigade(f->next, bb);
330 /* We're cool with filtering this. */
331 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
332 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
333 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
335 zRC = deflateInit2(&ctx->stream, Z_BEST_SPEED, Z_DEFLATED,
336 c->windowSize, c->memlevel,
341 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
342 "unable to init Zlib: "
343 "deflateInit2 returned %d: URL %s",
345 return ap_pass_brigade(f->next, bb);
348 /* RFC 1952 Section 2.3 dictates the gzip header:
350 * +---+---+---+---+---+---+---+---+---+---+
351 * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
352 * +---+---+---+---+---+---+---+---+---+---+
354 * If we wish to populate in MTIME (as hinted in RFC 1952), do:
355 * putLong(date_array, apr_time_now() / APR_USEC_PER_SEC);
356 * where date_array is a char[4] and then print date_array in the
359 buf = apr_psprintf(r->pool, "%c%c%c%c%c%c%c%c%c%c", deflate_magic[0],
360 deflate_magic[1], Z_DEFLATED, 0 /* flags */,
361 0, 0, 0, 0 /* 4 chars for mtime */,
362 0 /* xflags */, OS_CODE);
363 e = apr_bucket_pool_create(buf, 10, r->pool, f->c->bucket_alloc);
364 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
366 /* If the entire Content-Encoding is "identity", we can replace it. */
367 if (!encoding || !strcasecmp(encoding, "identity")) {
368 apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
371 apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
373 apr_table_setn(r->headers_out, "Vary", "Accept-Encoding");
374 apr_table_unset(r->headers_out, "Content-Length");
377 /* initialize deflate output buffer */
378 ctx->stream.next_out = ctx->buffer;
379 ctx->stream.avail_out = c->bufferSize;
381 APR_BRIGADE_FOREACH(e, bb) {
388 if (APR_BUCKET_IS_EOS(e)) {
390 char crc_array[4], len_array[4];
391 unsigned int deflate_len;
393 ctx->stream.avail_in = 0; /* should be zero already anyway */
395 deflate_len = c->bufferSize - ctx->stream.avail_out;
397 if (deflate_len != 0) {
398 b = apr_bucket_heap_create((char *)ctx->buffer,
401 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
402 ctx->stream.next_out = ctx->buffer;
403 ctx->stream.avail_out = c->bufferSize;
410 zRC = deflate(&ctx->stream, Z_FINISH);
412 if (deflate_len == 0 && zRC == Z_BUF_ERROR) {
416 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
418 if (zRC != Z_OK && zRC != Z_STREAM_END) {
423 putLong(crc_array, ctx->crc);
424 putLong(len_array, ctx->stream.total_in);
426 p = buf = apr_palloc(r->pool, 8);
436 b = apr_bucket_pool_create(buf, 8, r->pool, f->c->bucket_alloc);
437 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
438 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
439 "Zlib: Compressed %ld to %ld : URL %s",
440 ctx->stream.total_in, ctx->stream.total_out, r->uri);
443 if (ctx->stream.total_in > 0) {
446 total = ctx->stream.total_out * 100 / ctx->stream.total_in;
448 apr_table_setn(r->notes, c->noteName,
449 apr_itoa(r->pool, total));
452 apr_table_setn(r->notes, c->noteName, "-");
456 deflateEnd(&ctx->stream);
458 /* Remove EOS from the old list, and insert into the new. */
459 APR_BUCKET_REMOVE(e);
460 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
462 /* Okay, we've seen the EOS.
463 * Time to pass it along down the chain.
465 return ap_pass_brigade(f->next, ctx->bb);
468 if (APR_BUCKET_IS_FLUSH(e)) {
470 zRC = deflate(&(ctx->stream), Z_SYNC_FLUSH);
475 ctx->stream.next_out = ctx->buffer;
476 len = c->bufferSize - ctx->stream.avail_out;
478 b = apr_bucket_heap_create((char *)ctx->buffer, len,
479 NULL, f->c->bucket_alloc);
480 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
481 ctx->stream.avail_out = c->bufferSize;
483 bkt = apr_bucket_flush_create(f->c->bucket_alloc);
484 APR_BRIGADE_INSERT_TAIL(ctx->bb, bkt);
485 ap_pass_brigade(f->next, ctx->bb);
490 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
492 /* This crc32 function is from zlib. */
493 ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
496 ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
497 * but we'll just have to
499 ctx->stream.avail_in = len;
501 while (ctx->stream.avail_in != 0) {
502 if (ctx->stream.avail_out == 0) {
503 ctx->stream.next_out = ctx->buffer;
504 len = c->bufferSize - ctx->stream.avail_out;
506 b = apr_bucket_heap_create((char *)ctx->buffer, len,
507 NULL, f->c->bucket_alloc);
508 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
509 ctx->stream.avail_out = c->bufferSize;
512 zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
519 apr_brigade_destroy(bb);
523 /* This is the deflate input filter (inflates). */
524 static apr_status_t deflate_in_filter(ap_filter_t *f,
525 apr_bucket_brigade *bb,
526 ap_input_mode_t mode,
527 apr_read_type_e block,
531 request_rec *r = f->r;
532 deflate_ctx *ctx = f->ctx;
535 deflate_filter_config *c;
537 /* just get out of the way of things we don't want. */
538 if (mode != AP_MODE_READBYTES) {
539 return ap_get_brigade(f->next, bb, mode, block, readbytes);
542 c = ap_get_module_config(r->server->module_config, &deflate_module);
546 char *token, deflate_hdr[10];
547 const char *encoding;
550 /* only work on main request/no subrequests */
552 ap_remove_input_filter(f);
553 return ap_get_brigade(f->next, bb, mode, block, readbytes);
556 /* Let's see what our current Content-Encoding is.
557 * If gzip is present, don't gzip again. (We could, but let's not.)
559 encoding = apr_table_get(r->headers_in, "Content-Encoding");
561 const char *tmp = encoding;
563 token = ap_get_token(r->pool, &tmp, 0);
564 while (token && token[0]) {
565 if (!strcasecmp(token, "gzip")) {
569 /* Otherwise, skip token */
571 token = ap_get_token(r->pool, &tmp, 0);
576 ap_remove_input_filter(f);
577 return ap_get_brigade(f->next, bb, mode, block, readbytes);
580 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
581 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
582 ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
583 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
585 rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
586 if (rv != APR_SUCCESS) {
591 rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
592 if (rv != APR_SUCCESS) {
596 /* We didn't get the magic bytes. */
598 deflate_hdr[0] != deflate_magic[0] ||
599 deflate_hdr[1] != deflate_magic[1]) {
603 /* We can't handle flags for now. */
604 if (deflate_hdr[3] != 0) {
608 zRC = inflateInit2(&ctx->stream, c->windowSize);
612 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
613 "unable to init Zlib: "
614 "inflateInit2 returned %d: URL %s",
616 ap_remove_input_filter(f);
617 return ap_get_brigade(f->next, bb, mode, block, readbytes);
620 /* initialize deflate output buffer */
621 ctx->stream.next_out = ctx->buffer;
622 ctx->stream.avail_out = c->bufferSize;
624 apr_brigade_cleanup(ctx->bb);
627 if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
628 rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
630 if (rv != APR_SUCCESS) {
634 APR_BRIGADE_FOREACH(bkt, ctx->bb) {
638 /* If we actually see the EOS, that means we screwed up! */
639 if (APR_BUCKET_IS_EOS(bkt)) {
643 if (APR_BUCKET_IS_FLUSH(bkt)) {
644 apr_bucket *tmp_heap;
645 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
650 ctx->stream.next_out = ctx->buffer;
651 len = c->bufferSize - ctx->stream.avail_out;
653 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
654 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
655 NULL, f->c->bucket_alloc);
656 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
657 ctx->stream.avail_out = c->bufferSize;
659 /* Move everything to the returning brigade. */
660 APR_BUCKET_REMOVE(bkt);
661 APR_BRIGADE_CONCAT(bb, ctx->bb);
666 apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
668 /* pass through zlib inflate. */
669 ctx->stream.next_in = (unsigned char *)data;
670 ctx->stream.avail_in = len;
672 while (ctx->stream.avail_in != 0) {
673 if (ctx->stream.avail_out == 0) {
674 apr_bucket *tmp_heap;
675 ctx->stream.next_out = ctx->buffer;
676 len = c->bufferSize - ctx->stream.avail_out;
678 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
679 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
680 NULL, f->c->bucket_alloc);
681 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
682 ctx->stream.avail_out = c->bufferSize;
685 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
687 if (zRC == Z_STREAM_END) {
695 if (zRC == Z_STREAM_END) {
696 apr_bucket *tmp_heap, *eos;
698 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
699 "Zlib: Inflated %ld to %ld : URL %s",
700 ctx->stream.total_in, ctx->stream.total_out,
703 len = c->bufferSize - ctx->stream.avail_out;
705 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
706 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
707 NULL, f->c->bucket_alloc);
708 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
709 ctx->stream.avail_out = c->bufferSize;
711 /* Is the remaining 8 bytes already in the avail stream? */
712 if (ctx->stream.avail_in >= 8) {
713 unsigned long compCRC, compLen;
714 compCRC = getLong(ctx->stream.next_in);
715 if (ctx->crc != compCRC) {
718 ctx->stream.next_in += 4;
719 compLen = getLong(ctx->stream.next_in);
720 if (ctx->stream.total_out != compLen) {
725 /* FIXME: We need to grab the 8 verification bytes
730 inflateEnd(&ctx->stream);
731 apr_brigade_cleanup(ctx->bb);
733 eos = apr_bucket_eos_create(f->c->bucket_alloc);
734 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
740 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
741 apr_bucket_brigade *newbb;
743 /* May return APR_INCOMPLETE which is fine by us. */
744 apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
746 newbb = apr_brigade_split(ctx->proc_bb, bkt);
747 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
748 APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
756 static void register_hooks(apr_pool_t *p)
758 ap_register_output_filter(deflateFilterName, deflate_out_filter,
759 AP_FTYPE_CONTENT_SET);
760 ap_register_input_filter(deflateFilterName, deflate_in_filter,
761 AP_FTYPE_CONTENT_SET);
764 static const command_rec deflate_filter_cmds[] = {
765 AP_INIT_TAKE1("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
766 "Set a note to report on compression ratio"),
767 AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
768 RSRC_CONF, "Set the Deflate window size (1-15)"),
769 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
770 "Set the Deflate Buffer Size"),
771 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
772 "Set the Deflate Memory Level (1-9)"),
776 module AP_MODULE_DECLARE_DATA deflate_module = {
777 STANDARD20_MODULE_STUFF,
778 NULL, /* dir config creater */
779 NULL, /* dir merger --- default is to override */
780 create_deflate_server_config, /* server config */
781 NULL, /* merge server config */
782 deflate_filter_cmds, /* command table */
783 register_hooks /* register hooks */