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"
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
51 static const char deflateFilterName[] = "DEFLATE";
52 module AP_MODULE_DECLARE_DATA deflate_module;
54 typedef struct deflate_filter_config_t
59 apr_size_t bufferSize;
60 char *note_ratio_name;
61 char *note_input_name;
62 char *note_output_name;
63 } deflate_filter_config;
65 /* RFC 1952 Section 2.3 defines the gzip header:
67 * +---+---+---+---+---+---+---+---+---+---+
68 * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
69 * +---+---+---+---+---+---+---+---+---+---+
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 */
78 static const char deflate_magic[2] = { '\037', '\213' };
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
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.
91 static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
94 apr_table_t *hdrs = hdrs1;
95 const char *encoding = apr_table_get(hdrs, "Content-Encoding");
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");
102 encoding = r->content_encoding;
106 if (encoding && *encoding) {
108 /* check the usual/simple case first */
109 if (!strcasecmp(encoding, "gzip")
110 || !strcasecmp(encoding, "x-gzip")) {
113 apr_table_unset(hdrs, "Content-Encoding");
116 r->content_encoding = NULL;
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
123 char *new_encoding = apr_pstrdup(r->pool, encoding);
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")) {
132 apr_table_unset(hdrs, "Content-Encoding");
135 r->content_encoding = NULL;
138 break; /* seen all tokens */
140 for (ptr=token+1; apr_isspace(*ptr); ++ptr);
141 if (!strcasecmp(ptr, "gzip")
142 || !strcasecmp(ptr, "x-gzip")) {
145 apr_table_setn(hdrs, "Content-Encoding", new_encoding);
148 r->content_encoding = new_encoding;
152 else if (!ptr[0] || !strcasecmp(ptr, "identity")) {
154 continue; /* strip the token and find the next one */
156 break; /* found a non-identity token */
161 * If we have dealt with the headers above but content_encoding was set
162 * before sync it with the new value in the hdrs table as
163 * r->content_encoding takes precedence later on in the http_header_filter
164 * and hence would destroy what we have just set in the hdrs table.
166 if (hdrs && r->content_encoding) {
167 r->content_encoding = apr_table_get(hdrs, "Content-Encoding");
172 /* Outputs a long in LSB order to the given file
173 * only the bottom 4 bits are required for the deflate file format.
175 static void putLong(unsigned char *string, unsigned long x)
177 string[0] = (unsigned char)(x & 0xff);
178 string[1] = (unsigned char)((x & 0xff00) >> 8);
179 string[2] = (unsigned char)((x & 0xff0000) >> 16);
180 string[3] = (unsigned char)((x & 0xff000000) >> 24);
183 /* Inputs a string and returns a long.
185 static unsigned long getLong(unsigned char *string)
187 return ((unsigned long)string[0])
188 | (((unsigned long)string[1]) << 8)
189 | (((unsigned long)string[2]) << 16)
190 | (((unsigned long)string[3]) << 24);
193 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
195 deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
197 c->memlevel = DEFAULT_MEMLEVEL;
198 c->windowSize = DEFAULT_WINDOWSIZE;
199 c->bufferSize = DEFAULT_BUFFERSIZE;
200 c->compressionlevel = DEFAULT_COMPRESSION;
205 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
208 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
215 return "DeflateWindowSize must be between 1 and 15";
217 c->windowSize = i * -1;
222 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
225 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
230 return "DeflateBufferSize should be positive";
233 c->bufferSize = (apr_size_t)n;
237 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
238 const char *arg1, const char *arg2)
240 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
244 c->note_ratio_name = apr_pstrdup(cmd->pool, arg1);
246 else if (!strcasecmp(arg1, "ratio")) {
247 c->note_ratio_name = apr_pstrdup(cmd->pool, arg2);
249 else if (!strcasecmp(arg1, "input")) {
250 c->note_input_name = apr_pstrdup(cmd->pool, arg2);
252 else if (!strcasecmp(arg1, "output")) {
253 c->note_output_name = apr_pstrdup(cmd->pool, arg2);
256 return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
262 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
265 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
272 return "DeflateMemLevel must be between 1 and 9";
279 static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
282 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
289 return "Compression Level must be between 1 and 9";
291 c->compressionlevel = i;
296 typedef struct deflate_ctx_t
299 unsigned char *buffer;
301 apr_bucket_brigade *bb, *proc_bb;
302 int (*libz_end_func)(z_streamp);
303 unsigned char *validation_buffer;
304 apr_size_t validation_buffer_length;
308 /* Number of validation bytes (CRC and length) after the compressed data */
309 #define VALIDATION_SIZE 8
310 /* Do not update ctx->crc, see comment in flush_libz_buffer */
311 #define NO_UPDATE_CRC 0
312 /* Do update ctx->crc, see comment in flush_libz_buffer */
315 static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
316 struct apr_bucket_alloc_t *bucket_alloc,
317 int (*libz_func)(z_streamp, int), int flush,
322 unsigned int deflate_len;
326 deflate_len = c->bufferSize - ctx->stream.avail_out;
328 if (deflate_len != 0) {
330 * Do we need to update ctx->crc? Usually this is the case for
331 * inflate action where we need to do a crc on the output, whereas
332 * in the deflate case we need to do a crc on the input
335 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer,
338 b = apr_bucket_heap_create((char *)ctx->buffer,
341 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
342 ctx->stream.next_out = ctx->buffer;
343 ctx->stream.avail_out = c->bufferSize;
349 zRC = libz_func(&ctx->stream, flush);
352 * We can ignore Z_BUF_ERROR because:
353 * When we call libz_func we can assume that
355 * - avail_in is zero (due to the surrounding code that calls
357 * - avail_out is non zero due to our actions some lines above
359 * So the only reason for Z_BUF_ERROR is that the internal libz
360 * buffers are now empty and thus we called libz_func one time
361 * too often. This does not hurt. It simply says that we are done.
363 if (zRC == Z_BUF_ERROR) {
368 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
370 if (zRC != Z_OK && zRC != Z_STREAM_END)
376 static apr_status_t deflate_ctx_cleanup(void *data)
378 deflate_ctx *ctx = (deflate_ctx *)data;
381 ctx->libz_end_func(&ctx->stream);
385 /* ETag must be unique among the possible representations, so a change
386 * to content-encoding requires a corresponding change to the ETag.
387 * This routine appends -transform (e.g., -gzip) to the entity-tag
388 * value inside the double-quotes if an ETag has already been set
389 * and its value already contains double-quotes. PR 39727
391 static void deflate_check_etag(request_rec *r, const char *transform)
393 const char *etag = apr_table_get(r->headers_out, "ETag");
396 if ((etag && ((etaglen = strlen(etag)) > 2))) {
397 if (etag[etaglen - 1] == '"') {
398 apr_size_t transformlen = strlen(transform);
399 char *newtag = apr_palloc(r->pool, etaglen + transformlen + 2);
401 char *e = d + etaglen - 1;
402 const char *s = etag;
404 for (; d < e; ++d, ++s) {
405 *d = *s; /* copy etag to newtag up to last quote */
407 *d++ = '-'; /* append dash to newtag */
409 e = d + transformlen;
410 for (; d < e; ++d, ++s) {
411 *d = *s; /* copy transform to newtag */
413 *d++ = '"'; /* append quote to newtag */
414 *d = '\0'; /* null terminate newtag */
416 apr_table_setn(r->headers_out, "ETag", newtag);
421 static apr_status_t deflate_out_filter(ap_filter_t *f,
422 apr_bucket_brigade *bb)
425 request_rec *r = f->r;
426 deflate_ctx *ctx = f->ctx;
428 deflate_filter_config *c;
430 /* Do nothing if asked to filter nothing. */
431 if (APR_BRIGADE_EMPTY(bb)) {
435 c = ap_get_module_config(r->server->module_config,
438 /* If we don't have a context, we need to ensure that it is okay to send
439 * the deflated content. If we have a context, that means we've done
440 * this before and we liked it.
441 * This could be not so nice if we always fail. But, if we succeed,
442 * we're in better shape.
446 const char *encoding;
449 * Only work on main request, not subrequests,
450 * that are not a 204 response with no content
451 * and are not tagged with the no-gzip env variable
452 * and not a partial response to a Range request.
454 if ((r->main != NULL) || (r->status == HTTP_NO_CONTENT) ||
455 apr_table_get(r->subprocess_env, "no-gzip") ||
456 apr_table_get(r->headers_out, "Content-Range")
458 ap_remove_output_filter(f);
459 return ap_pass_brigade(f->next, bb);
462 /* Some browsers might have problems with content types
463 * other than text/html, so set gzip-only-text/html
464 * (with browsermatch) for them
466 if (r->content_type == NULL
467 || strncmp(r->content_type, "text/html", 9)) {
468 const char *env_value = apr_table_get(r->subprocess_env,
469 "gzip-only-text/html");
470 if ( env_value && (strcmp(env_value,"1") == 0) ) {
471 ap_remove_output_filter(f);
472 return ap_pass_brigade(f->next, bb);
476 /* Let's see what our current Content-Encoding is.
477 * If it's already encoded, don't compress again.
478 * (We could, but let's not.)
480 encoding = apr_table_get(r->headers_out, "Content-Encoding");
484 err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
486 encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
490 encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
493 if (r->content_encoding) {
494 encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
495 r->content_encoding, NULL)
496 : r->content_encoding;
500 const char *tmp = encoding;
502 token = ap_get_token(r->pool, &tmp, 0);
503 while (token && *token) {
504 /* stolen from mod_negotiation: */
505 if (strcmp(token, "identity") && strcmp(token, "7bit") &&
506 strcmp(token, "8bit") && strcmp(token, "binary")) {
508 ap_remove_output_filter(f);
509 return ap_pass_brigade(f->next, bb);
512 /* Otherwise, skip token */
516 token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
520 /* Even if we don't accept this request based on it not having
521 * the Accept-Encoding, we need to note that we were looking
522 * for this header and downstream proxies should be aware of that.
524 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
526 /* force-gzip will just force it out regardless if the browser
527 * can actually do anything with it.
529 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
531 /* if they don't have the line, then they can't play */
532 accepts = apr_table_get(r->headers_in, "Accept-Encoding");
533 if (accepts == NULL) {
534 ap_remove_output_filter(f);
535 return ap_pass_brigade(f->next, bb);
538 token = ap_get_token(r->pool, &accepts, 0);
539 while (token && token[0] && strcasecmp(token, "gzip")) {
540 /* skip parameters, XXX: ;q=foo evaluation? */
541 while (*accepts == ';') {
543 token = ap_get_token(r->pool, &accepts, 1);
546 /* retrieve next token */
547 if (*accepts == ',') {
550 token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
553 /* No acceptable token found. */
554 if (token == NULL || token[0] == '\0') {
555 ap_remove_output_filter(f);
556 return ap_pass_brigade(f->next, bb);
560 /* At this point we have decided to filter the content. Let's try to
561 * to initialize zlib (except for 304 responses, where we will only
562 * send out the headers).
565 if (r->status != HTTP_NOT_MODIFIED) {
566 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
567 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
568 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
569 ctx->libz_end_func = deflateEnd;
571 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
572 c->windowSize, c->memlevel,
576 deflateEnd(&ctx->stream);
577 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
578 "unable to init Zlib: "
579 "deflateInit2 returned %d: URL %s",
582 * Remove ourselves as it does not make sense to return:
583 * We are not able to init libz and pass data down the chain
586 ap_remove_output_filter(f);
587 return ap_pass_brigade(f->next, bb);
590 * Register a cleanup function to ensure that we cleanup the internal
593 apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
594 apr_pool_cleanup_null);
598 * Zlib initialization worked, so we can now change the important
599 * content metadata before sending the response out.
602 /* If the entire Content-Encoding is "identity", we can replace it. */
603 if (!encoding || !strcasecmp(encoding, "identity")) {
604 apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
607 apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
609 /* Fix r->content_encoding if it was set before */
610 if (r->content_encoding) {
611 r->content_encoding = apr_table_get(r->headers_out,
614 apr_table_unset(r->headers_out, "Content-Length");
615 apr_table_unset(r->headers_out, "Content-MD5");
616 deflate_check_etag(r, "gzip");
618 /* For a 304 response, only change the headers */
619 if (r->status == HTTP_NOT_MODIFIED) {
620 ap_remove_output_filter(f);
621 return ap_pass_brigade(f->next, bb);
624 /* add immortal gzip header */
625 e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
627 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
629 /* initialize deflate output buffer */
630 ctx->stream.next_out = ctx->buffer;
631 ctx->stream.avail_out = c->bufferSize;
634 while (!APR_BRIGADE_EMPTY(bb))
641 * Optimization: If we are a HEAD request and bytes_sent is not zero
642 * it means that we have passed the content-length filter once and
643 * have more data to sent. This means that the content-length filter
644 * could not determine our content-length for the response to the
645 * HEAD request anyway (the associated GET request would deliver the
646 * body in chunked encoding) and we can stop compressing.
648 if (r->header_only && r->bytes_sent) {
649 ap_remove_output_filter(f);
650 return ap_pass_brigade(f->next, bb);
653 e = APR_BRIGADE_FIRST(bb);
655 if (APR_BUCKET_IS_EOS(e)) {
658 ctx->stream.avail_in = 0; /* should be zero already anyway */
659 /* flush the remaining data from the zlib buffers */
660 flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
663 buf = apr_palloc(r->pool, VALIDATION_SIZE);
664 putLong((unsigned char *)&buf[0], ctx->crc);
665 putLong((unsigned char *)&buf[4], ctx->stream.total_in);
667 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
669 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
670 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
671 "Zlib: Compressed %ld to %ld : URL %s",
672 ctx->stream.total_in, ctx->stream.total_out, r->uri);
674 /* leave notes for logging */
675 if (c->note_input_name) {
676 apr_table_setn(r->notes, c->note_input_name,
677 (ctx->stream.total_in > 0)
678 ? apr_off_t_toa(r->pool,
679 ctx->stream.total_in)
683 if (c->note_output_name) {
684 apr_table_setn(r->notes, c->note_output_name,
685 (ctx->stream.total_in > 0)
686 ? apr_off_t_toa(r->pool,
687 ctx->stream.total_out)
691 if (c->note_ratio_name) {
692 apr_table_setn(r->notes, c->note_ratio_name,
693 (ctx->stream.total_in > 0)
695 (int)(ctx->stream.total_out
697 / ctx->stream.total_in))
701 deflateEnd(&ctx->stream);
702 /* No need for cleanup any longer */
703 apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
705 /* Remove EOS from the old list, and insert into the new. */
706 APR_BUCKET_REMOVE(e);
707 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
709 /* Okay, we've seen the EOS.
710 * Time to pass it along down the chain.
712 return ap_pass_brigade(f->next, ctx->bb);
715 if (APR_BUCKET_IS_FLUSH(e)) {
718 /* flush the remaining data from the zlib buffers */
719 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
720 Z_SYNC_FLUSH, NO_UPDATE_CRC);
725 /* Remove flush bucket from old brigade anf insert into the new. */
726 APR_BUCKET_REMOVE(e);
727 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
728 rv = ap_pass_brigade(f->next, ctx->bb);
729 if (rv != APR_SUCCESS) {
735 if (APR_BUCKET_IS_METADATA(e)) {
737 * Remove meta data bucket from old brigade and insert into the
740 APR_BUCKET_REMOVE(e);
741 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
746 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
748 /* This crc32 function is from zlib. */
749 ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
752 ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
753 * but we'll just have to
755 ctx->stream.avail_in = len;
757 while (ctx->stream.avail_in != 0) {
758 if (ctx->stream.avail_out == 0) {
761 ctx->stream.next_out = ctx->buffer;
762 len = c->bufferSize - ctx->stream.avail_out;
764 b = apr_bucket_heap_create((char *)ctx->buffer, len,
765 NULL, f->c->bucket_alloc);
766 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
767 ctx->stream.avail_out = c->bufferSize;
768 /* Send what we have right now to the next filter. */
769 rv = ap_pass_brigade(f->next, ctx->bb);
770 if (rv != APR_SUCCESS) {
775 zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
782 apr_bucket_delete(e);
785 apr_brigade_cleanup(bb);
789 /* This is the deflate input filter (inflates). */
790 static apr_status_t deflate_in_filter(ap_filter_t *f,
791 apr_bucket_brigade *bb,
792 ap_input_mode_t mode,
793 apr_read_type_e block,
797 request_rec *r = f->r;
798 deflate_ctx *ctx = f->ctx;
801 deflate_filter_config *c;
803 /* just get out of the way of things we don't want. */
804 if (mode != AP_MODE_READBYTES) {
805 return ap_get_brigade(f->next, bb, mode, block, readbytes);
808 c = ap_get_module_config(r->server->module_config, &deflate_module);
811 char deflate_hdr[10];
814 /* only work on main request/no subrequests */
815 if (!ap_is_initial_req(r)) {
816 ap_remove_input_filter(f);
817 return ap_get_brigade(f->next, bb, mode, block, readbytes);
820 /* We can't operate on Content-Ranges */
821 if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
822 ap_remove_input_filter(f);
823 return ap_get_brigade(f->next, bb, mode, block, readbytes);
826 /* Check whether request body is gzipped.
828 * If it is, we're transforming the contents, invalidating
829 * some request headers including Content-Encoding.
831 * If not, we just remove ourself.
833 if (check_gzip(r, r->headers_in, NULL) == 0) {
834 ap_remove_input_filter(f);
835 return ap_get_brigade(f->next, bb, mode, block, readbytes);
838 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
839 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
840 ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
841 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
843 rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
844 if (rv != APR_SUCCESS) {
848 apr_table_unset(r->headers_in, "Content-Length");
849 apr_table_unset(r->headers_in, "Content-MD5");
852 rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
853 if (rv != APR_SUCCESS) {
857 /* We didn't get the magic bytes. */
859 deflate_hdr[0] != deflate_magic[0] ||
860 deflate_hdr[1] != deflate_magic[1]) {
864 /* We can't handle flags for now. */
865 if (deflate_hdr[3] != 0) {
869 zRC = inflateInit2(&ctx->stream, c->windowSize);
873 inflateEnd(&ctx->stream);
874 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
875 "unable to init Zlib: "
876 "inflateInit2 returned %d: URL %s",
878 ap_remove_input_filter(f);
879 return ap_get_brigade(f->next, bb, mode, block, readbytes);
882 /* initialize deflate output buffer */
883 ctx->stream.next_out = ctx->buffer;
884 ctx->stream.avail_out = c->bufferSize;
886 apr_brigade_cleanup(ctx->bb);
889 if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
890 rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
892 if (rv != APR_SUCCESS) {
893 /* What about APR_EAGAIN errors? */
894 inflateEnd(&ctx->stream);
898 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
899 bkt != APR_BRIGADE_SENTINEL(ctx->bb);
900 bkt = APR_BUCKET_NEXT(bkt))
905 /* If we actually see the EOS, that means we screwed up! */
906 if (APR_BUCKET_IS_EOS(bkt)) {
907 inflateEnd(&ctx->stream);
911 if (APR_BUCKET_IS_FLUSH(bkt)) {
912 apr_bucket *tmp_heap;
913 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
915 inflateEnd(&ctx->stream);
919 ctx->stream.next_out = ctx->buffer;
920 len = c->bufferSize - ctx->stream.avail_out;
922 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
923 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
924 NULL, f->c->bucket_alloc);
925 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
926 ctx->stream.avail_out = c->bufferSize;
928 /* Move everything to the returning brigade. */
929 APR_BUCKET_REMOVE(bkt);
930 APR_BRIGADE_CONCAT(bb, ctx->bb);
935 apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
937 /* pass through zlib inflate. */
938 ctx->stream.next_in = (unsigned char *)data;
939 ctx->stream.avail_in = len;
943 while (ctx->stream.avail_in != 0) {
944 if (ctx->stream.avail_out == 0) {
945 apr_bucket *tmp_heap;
946 ctx->stream.next_out = ctx->buffer;
947 len = c->bufferSize - ctx->stream.avail_out;
949 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
950 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
951 NULL, f->c->bucket_alloc);
952 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
953 ctx->stream.avail_out = c->bufferSize;
956 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
958 if (zRC == Z_STREAM_END) {
963 inflateEnd(&ctx->stream);
967 if (zRC == Z_STREAM_END) {
968 apr_bucket *tmp_heap, *eos;
970 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
971 "Zlib: Inflated %ld to %ld : URL %s",
972 ctx->stream.total_in, ctx->stream.total_out,
975 len = c->bufferSize - ctx->stream.avail_out;
977 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
978 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
979 NULL, f->c->bucket_alloc);
980 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
981 ctx->stream.avail_out = c->bufferSize;
983 /* Is the remaining 8 bytes already in the avail stream? */
984 if (ctx->stream.avail_in >= 8) {
985 unsigned long compCRC, compLen;
986 compCRC = getLong(ctx->stream.next_in);
987 if (ctx->crc != compCRC) {
988 inflateEnd(&ctx->stream);
991 ctx->stream.next_in += 4;
992 compLen = getLong(ctx->stream.next_in);
993 if (ctx->stream.total_out != compLen) {
994 inflateEnd(&ctx->stream);
999 /* FIXME: We need to grab the 8 verification bytes
1001 inflateEnd(&ctx->stream);
1002 return APR_EGENERAL;
1005 inflateEnd(&ctx->stream);
1007 eos = apr_bucket_eos_create(f->c->bucket_alloc);
1008 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
1013 apr_brigade_cleanup(ctx->bb);
1016 /* If we are about to return nothing for a 'blocking' read and we have
1017 * some data in our zlib buffer, flush it out so we can return something.
1019 if (block == APR_BLOCK_READ &&
1020 APR_BRIGADE_EMPTY(ctx->proc_bb) &&
1021 ctx->stream.avail_out < c->bufferSize) {
1022 apr_bucket *tmp_heap;
1024 ctx->stream.next_out = ctx->buffer;
1025 len = c->bufferSize - ctx->stream.avail_out;
1027 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1028 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
1029 NULL, f->c->bucket_alloc);
1030 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
1031 ctx->stream.avail_out = c->bufferSize;
1034 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1035 if (apr_brigade_partition(ctx->proc_bb, readbytes, &bkt) == APR_INCOMPLETE) {
1036 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1039 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1040 apr_brigade_split_ex(bb, bkt, ctx->proc_bb);
1048 /* Filter to inflate for a content-transforming proxy. */
1049 static apr_status_t inflate_out_filter(ap_filter_t *f,
1050 apr_bucket_brigade *bb)
1055 request_rec *r = f->r;
1056 deflate_ctx *ctx = f->ctx;
1059 deflate_filter_config *c;
1061 /* Do nothing if asked to filter nothing. */
1062 if (APR_BRIGADE_EMPTY(bb)) {
1066 c = ap_get_module_config(r->server->module_config, &deflate_module);
1071 * Only work on main request, not subrequests,
1072 * that are not a 204 response with no content
1073 * and not a partial response to a Range request,
1074 * and only when Content-Encoding ends in gzip.
1076 if (!ap_is_initial_req(r) || (r->status == HTTP_NO_CONTENT) ||
1077 (apr_table_get(r->headers_out, "Content-Range") != NULL) ||
1078 (check_gzip(r, r->headers_out, r->err_headers_out) == 0)
1080 ap_remove_output_filter(f);
1081 return ap_pass_brigade(f->next, bb);
1085 * At this point we have decided to filter the content, so change
1086 * important content metadata before sending any response out.
1087 * Content-Encoding was already reset by the check_gzip() call.
1089 apr_table_unset(r->headers_out, "Content-Length");
1090 apr_table_unset(r->headers_out, "Content-MD5");
1091 deflate_check_etag(r, "gunzip");
1093 /* For a 304 response, only change the headers */
1094 if (r->status == HTTP_NOT_MODIFIED) {
1095 ap_remove_output_filter(f);
1096 return ap_pass_brigade(f->next, bb);
1099 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1100 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1101 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1102 ctx->libz_end_func = inflateEnd;
1103 ctx->validation_buffer = NULL;
1104 ctx->validation_buffer_length = 0;
1106 zRC = inflateInit2(&ctx->stream, c->windowSize);
1110 inflateEnd(&ctx->stream);
1111 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1112 "unable to init Zlib: "
1113 "inflateInit2 returned %d: URL %s",
1116 * Remove ourselves as it does not make sense to return:
1117 * We are not able to init libz and pass data down the chain
1120 ap_remove_output_filter(f);
1121 return ap_pass_brigade(f->next, bb);
1125 * Register a cleanup function to ensure that we cleanup the internal
1128 apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
1129 apr_pool_cleanup_null);
1131 /* initialize inflate output buffer */
1132 ctx->stream.next_out = ctx->buffer;
1133 ctx->stream.avail_out = c->bufferSize;
1135 ctx->inflate_init = 0;
1138 while (!APR_BRIGADE_EMPTY(bb))
1144 e = APR_BRIGADE_FIRST(bb);
1146 if (APR_BUCKET_IS_EOS(e)) {
1148 * We are really done now. Ensure that we never return here, even
1149 * if a second EOS bucket falls down the chain. Thus remove
1152 ap_remove_output_filter(f);
1153 /* should be zero already anyway */
1154 ctx->stream.avail_in = 0;
1156 * Flush the remaining data from the zlib buffers. It is correct
1157 * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1158 * deflate case. In the inflate case Z_FINISH requires to have a
1159 * large enough output buffer to put ALL data in otherwise it
1160 * fails, whereas in the deflate case you can empty a filled output
1161 * buffer and call it again until no more output can be created.
1163 flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
1165 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1166 "Zlib: Inflated %ld to %ld : URL %s",
1167 ctx->stream.total_in, ctx->stream.total_out, r->uri);
1169 if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1170 unsigned long compCRC, compLen;
1171 compCRC = getLong(ctx->validation_buffer);
1172 if (ctx->crc != compCRC) {
1173 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1174 "Zlib: Checksum of inflated stream invalid");
1175 return APR_EGENERAL;
1177 ctx->validation_buffer += VALIDATION_SIZE / 2;
1178 compLen = getLong(ctx->validation_buffer);
1179 if (ctx->stream.total_out != compLen) {
1180 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1181 "Zlib: Length of inflated stream invalid");
1182 return APR_EGENERAL;
1186 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1187 "Zlib: Validation bytes not present");
1188 return APR_EGENERAL;
1191 inflateEnd(&ctx->stream);
1192 /* No need for cleanup any longer */
1193 apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1195 /* Remove EOS from the old list, and insert into the new. */
1196 APR_BUCKET_REMOVE(e);
1197 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1200 * Okay, we've seen the EOS.
1201 * Time to pass it along down the chain.
1203 return ap_pass_brigade(f->next, ctx->bb);
1206 if (APR_BUCKET_IS_FLUSH(e)) {
1209 /* flush the remaining data from the zlib buffers */
1210 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
1211 Z_SYNC_FLUSH, UPDATE_CRC);
1213 return APR_EGENERAL;
1216 /* Remove flush bucket from old brigade anf insert into the new. */
1217 APR_BUCKET_REMOVE(e);
1218 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1219 rv = ap_pass_brigade(f->next, ctx->bb);
1220 if (rv != APR_SUCCESS) {
1226 if (APR_BUCKET_IS_METADATA(e)) {
1228 * Remove meta data bucket from old brigade and insert into the
1231 APR_BUCKET_REMOVE(e);
1232 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1237 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1239 /* first bucket contains zlib header */
1240 if (!ctx->inflate_init++) {
1242 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1243 "Insufficient data for inflate");
1244 return APR_EGENERAL;
1247 zlib_method = data[2];
1248 zlib_flags = data[3];
1249 if (zlib_method != Z_DEFLATED) {
1250 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1251 "inflate: data not deflated!");
1252 ap_remove_output_filter(f);
1253 return ap_pass_brigade(f->next, bb);
1255 if (data[0] != deflate_magic[0] ||
1256 data[1] != deflate_magic[1] ||
1257 (zlib_flags & RESERVED) != 0) {
1258 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1259 "inflate: bad header");
1260 return APR_EGENERAL ;
1265 if (zlib_flags & EXTRA_FIELD) {
1266 unsigned int bytes = (unsigned int)(data[0]);
1267 bytes += ((unsigned int)(data[1])) << 8;
1270 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1271 "inflate: extra field too big (not "
1273 return APR_EGENERAL;
1278 if (zlib_flags & ORIG_NAME) {
1279 while (len-- && *data++);
1281 if (zlib_flags & COMMENT) {
1282 while (len-- && *data++);
1284 if (zlib_flags & HEAD_CRC) {
1290 /* pass through zlib inflate. */
1291 ctx->stream.next_in = (unsigned char *)data;
1292 ctx->stream.avail_in = len;
1294 if (ctx->validation_buffer) {
1295 if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1296 apr_size_t copy_size;
1298 copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1299 if (copy_size > ctx->stream.avail_in)
1300 copy_size = ctx->stream.avail_in;
1301 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1302 ctx->stream.next_in, copy_size);
1303 /* Saved copy_size bytes */
1304 ctx->stream.avail_in -= copy_size;
1305 ctx->validation_buffer_length += copy_size;
1307 if (ctx->stream.avail_in) {
1308 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1309 "Zlib: %d bytes of garbage at the end of "
1310 "compressed stream.", ctx->stream.avail_in);
1312 * There is nothing worth consuming for zlib left, because it is
1313 * either garbage data or the data has been copied to the
1314 * validation buffer (processing validation data is no business
1315 * for zlib). So set ctx->stream.avail_in to zero to indicate
1316 * this to the following while loop.
1318 ctx->stream.avail_in = 0;
1324 while (ctx->stream.avail_in != 0) {
1325 if (ctx->stream.avail_out == 0) {
1327 ctx->stream.next_out = ctx->buffer;
1328 len = c->bufferSize - ctx->stream.avail_out;
1330 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1331 b = apr_bucket_heap_create((char *)ctx->buffer, len,
1332 NULL, f->c->bucket_alloc);
1333 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
1334 ctx->stream.avail_out = c->bufferSize;
1335 /* Send what we have right now to the next filter. */
1336 rv = ap_pass_brigade(f->next, ctx->bb);
1337 if (rv != APR_SUCCESS) {
1342 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1344 if (zRC == Z_STREAM_END) {
1346 * We have inflated all data. Now try to capture the
1347 * validation bytes. We may not have them all available
1348 * right now, but capture what is there.
1350 ctx->validation_buffer = apr_pcalloc(f->r->pool,
1352 if (ctx->stream.avail_in > VALIDATION_SIZE) {
1353 ctx->validation_buffer_length = VALIDATION_SIZE;
1354 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1355 "Zlib: %d bytes of garbage at the end of "
1356 "compressed stream.",
1357 ctx->stream.avail_in - VALIDATION_SIZE);
1358 } else if (ctx->stream.avail_in > 0) {
1359 ctx->validation_buffer_length = ctx->stream.avail_in;
1361 if (ctx->validation_buffer_length)
1362 memcpy(ctx->validation_buffer, ctx->stream.next_in,
1363 ctx->validation_buffer_length);
1368 return APR_EGENERAL;
1372 apr_bucket_delete(e);
1375 apr_brigade_cleanup(bb);
1379 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1380 static void register_hooks(apr_pool_t *p)
1382 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1383 AP_FTYPE_CONTENT_SET);
1384 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1385 AP_FTYPE_RESOURCE-1);
1386 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1387 AP_FTYPE_CONTENT_SET);
1390 static const command_rec deflate_filter_cmds[] = {
1391 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1392 "Set a note to report on compression ratio"),
1393 AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1394 RSRC_CONF, "Set the Deflate window size (1-15)"),
1395 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1396 "Set the Deflate Buffer Size"),
1397 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1398 "Set the Deflate Memory Level (1-9)"),
1399 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1400 "Set the Deflate Compression Level (1-9)"),
1404 AP_DECLARE_MODULE(deflate) = {
1405 STANDARD20_MODULE_STUFF,
1406 NULL, /* dir config creater */
1407 NULL, /* dir merger --- default is to override */
1408 create_deflate_server_config, /* server config */
1409 NULL, /* merge server config */
1410 deflate_filter_cmds, /* command table */
1411 register_hooks /* register hooks */