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 "http_core.h"
42 #include "apr_strings.h"
43 #include "apr_general.h"
44 #include "util_filter.h"
45 #include "apr_buckets.h"
46 #include "http_request.h"
47 #define APR_WANT_STRFUNC
53 static const char deflateFilterName[] = "DEFLATE";
54 module AP_MODULE_DECLARE_DATA deflate_module;
56 #define AP_DEFLATE_ETAG_ADDSUFFIX 0
57 #define AP_DEFLATE_ETAG_NOCHANGE 1
58 #define AP_DEFLATE_ETAG_REMOVE 2
60 #define AP_INFLATE_RATIO_LIMIT 200
61 #define AP_INFLATE_RATIO_BURST 3
63 typedef struct deflate_filter_config_t
69 const char *note_ratio_name;
70 const char *note_input_name;
71 const char *note_output_name;
73 } deflate_filter_config;
75 typedef struct deflate_dirconf_t {
76 apr_off_t inflate_limit;
81 /* RFC 1952 Section 2.3 defines the gzip header:
83 * +---+---+---+---+---+---+---+---+---+---+
84 * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
85 * +---+---+---+---+---+---+---+---+---+---+
87 static const char gzip_header[10] =
88 { '\037', '\213', Z_DEFLATED, 0,
89 0, 0, 0, 0, /* mtime */
90 0, 0x03 /* Unix OS_CODE */
94 static const char deflate_magic[2] = { '\037', '\213' };
96 /* windowsize is negative to suppress Zlib header */
97 #define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
98 #define DEFAULT_WINDOWSIZE -15
99 #define DEFAULT_MEMLEVEL 9
100 #define DEFAULT_BUFFERSIZE 8096
102 static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *mod_deflate_ssl_var = NULL;
104 /* Check whether a request is gzipped, so we can un-gzip it.
105 * If a request has multiple encodings, we need the gzip
106 * to be the outermost non-identity encoding.
108 static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
111 apr_table_t *hdrs = hdrs1;
112 const char *encoding = apr_table_get(hdrs, "Content-Encoding");
114 if (!encoding && (hdrs2 != NULL)) {
115 /* the output filter has two tables and a content_encoding to check */
116 encoding = apr_table_get(hdrs2, "Content-Encoding");
119 encoding = r->content_encoding;
123 if (encoding && *encoding) {
125 /* check the usual/simple case first */
126 if (!ap_cstr_casecmp(encoding, "gzip")
127 || !ap_cstr_casecmp(encoding, "x-gzip")) {
130 apr_table_unset(hdrs, "Content-Encoding");
133 r->content_encoding = NULL;
136 else if (ap_strchr_c(encoding, ',') != NULL) {
137 /* If the outermost encoding isn't gzip, there's nothing
138 * we can do. So only check the last non-identity token
140 char *new_encoding = apr_pstrdup(r->pool, encoding);
143 char *token = ap_strrchr(new_encoding, ',');
144 if (!token) { /* gzip:identity or other:identity */
145 if (!ap_cstr_casecmp(new_encoding, "gzip")
146 || !ap_cstr_casecmp(new_encoding, "x-gzip")) {
149 apr_table_unset(hdrs, "Content-Encoding");
152 r->content_encoding = NULL;
155 break; /* seen all tokens */
157 for (ptr=token+1; apr_isspace(*ptr); ++ptr);
158 if (!ap_cstr_casecmp(ptr, "gzip")
159 || !ap_cstr_casecmp(ptr, "x-gzip")) {
162 apr_table_setn(hdrs, "Content-Encoding", new_encoding);
165 r->content_encoding = new_encoding;
169 else if (!ptr[0] || !ap_cstr_casecmp(ptr, "identity")) {
171 continue; /* strip the token and find the next one */
173 break; /* found a non-identity token */
178 * If we have dealt with the headers above but content_encoding was set
179 * before sync it with the new value in the hdrs table as
180 * r->content_encoding takes precedence later on in the http_header_filter
181 * and hence would destroy what we have just set in the hdrs table.
183 if (hdrs && r->content_encoding) {
184 r->content_encoding = apr_table_get(hdrs, "Content-Encoding");
189 /* Outputs a long in LSB order to the given file
190 * only the bottom 4 bits are required for the deflate file format.
192 static void putLong(unsigned char *string, unsigned long x)
194 string[0] = (unsigned char)(x & 0xff);
195 string[1] = (unsigned char)((x & 0xff00) >> 8);
196 string[2] = (unsigned char)((x & 0xff0000) >> 16);
197 string[3] = (unsigned char)((x & 0xff000000) >> 24);
200 /* Inputs a string and returns a long.
202 static unsigned long getLong(unsigned char *string)
204 return ((unsigned long)string[0])
205 | (((unsigned long)string[1]) << 8)
206 | (((unsigned long)string[2]) << 16)
207 | (((unsigned long)string[3]) << 24);
210 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
212 deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
214 c->memlevel = DEFAULT_MEMLEVEL;
215 c->windowSize = DEFAULT_WINDOWSIZE;
216 c->bufferSize = DEFAULT_BUFFERSIZE;
217 c->compressionlevel = DEFAULT_COMPRESSION;
218 c->etag_opt = AP_DEFLATE_ETAG_ADDSUFFIX;
223 static void *create_deflate_dirconf(apr_pool_t *p, char *dummy)
225 deflate_dirconf_t *dc = apr_pcalloc(p, sizeof(*dc));
226 dc->ratio_limit = AP_INFLATE_RATIO_LIMIT;
227 dc->ratio_burst = AP_INFLATE_RATIO_BURST;
231 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
234 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
241 return "DeflateWindowSize must be between 1 and 15";
243 c->windowSize = i * -1;
248 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
251 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
256 return "DeflateBufferSize should be positive";
263 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
264 const char *arg1, const char *arg2)
266 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
270 c->note_ratio_name = arg1;
272 else if (!strcasecmp(arg1, "ratio")) {
273 c->note_ratio_name = arg2;
275 else if (!strcasecmp(arg1, "input")) {
276 c->note_input_name = arg2;
278 else if (!strcasecmp(arg1, "output")) {
279 c->note_output_name = arg2;
282 return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
288 static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
291 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
298 return "DeflateMemLevel must be between 1 and 9";
305 static const char *deflate_set_etag(cmd_parms *cmd, void *dummy,
308 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
311 if (!strcasecmp(arg, "NoChange")) {
312 c->etag_opt = AP_DEFLATE_ETAG_NOCHANGE;
314 else if (!strcasecmp(arg, "AddSuffix")) {
315 c->etag_opt = AP_DEFLATE_ETAG_ADDSUFFIX;
317 else if (!strcasecmp(arg, "Remove")) {
318 c->etag_opt = AP_DEFLATE_ETAG_REMOVE;
321 return "DeflateAlterETAG accepts only 'NoChange', 'AddSuffix', and 'Remove'";
328 static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
331 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
338 return "Compression Level must be between 1 and 9";
340 c->compressionlevel = i;
346 static const char *deflate_set_inflate_limit(cmd_parms *cmd, void *dirconf,
349 deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
352 if (APR_SUCCESS != apr_strtoff(&dc->inflate_limit, arg, &errp, 10)) {
353 return "DeflateInflateLimitRequestBody is not parsable.";
355 if (*errp || dc->inflate_limit < 0) {
356 return "DeflateInflateLimitRequestBody requires a non-negative integer.";
362 static const char *deflate_set_inflate_ratio_limit(cmd_parms *cmd,
366 deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
371 return "DeflateInflateRatioLimit must be positive";
378 static const char *deflate_set_inflate_ratio_burst(cmd_parms *cmd,
382 deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
387 return "DeflateInflateRatioBurst must be positive";
394 typedef struct deflate_ctx_t
397 unsigned char *buffer;
399 apr_bucket_brigade *bb, *proc_bb;
400 int (*libz_end_func)(z_streamp);
401 unsigned char *validation_buffer;
402 apr_size_t validation_buffer_length;
403 char header[10]; /* sizeof(gzip_header) */
404 apr_size_t header_len;
407 apr_off_t inflate_total;
408 unsigned int consume_pos,
410 unsigned int filter_init:1;
414 /* Number of validation bytes (CRC and length) after the compressed data */
415 #define VALIDATION_SIZE 8
416 /* Do not update ctx->crc, see comment in flush_libz_buffer */
417 #define NO_UPDATE_CRC 0
418 /* Do update ctx->crc, see comment in flush_libz_buffer */
421 static void consume_buffer(deflate_ctx *ctx, deflate_filter_config *c,
422 int len, int crc, apr_bucket_brigade *bb)
427 * Do we need to update ctx->crc? Usually this is the case for
428 * inflate action where we need to do a crc on the output, whereas
429 * in the deflate case we need to do a crc on the input
432 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
435 b = apr_bucket_heap_create((char *)ctx->buffer, len, NULL,
437 APR_BRIGADE_INSERT_TAIL(bb, b);
439 ctx->stream.next_out = ctx->buffer;
440 ctx->stream.avail_out = c->bufferSize;
443 static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
444 int (*libz_func)(z_streamp, int), int flush,
452 deflate_len = c->bufferSize - ctx->stream.avail_out;
453 if (deflate_len > 0) {
454 consume_buffer(ctx, c, deflate_len, crc, ctx->bb);
460 zRC = libz_func(&ctx->stream, flush);
463 * We can ignore Z_BUF_ERROR because:
464 * When we call libz_func we can assume that
466 * - avail_in is zero (due to the surrounding code that calls
468 * - avail_out is non zero due to our actions some lines above
470 * So the only reason for Z_BUF_ERROR is that the internal libz
471 * buffers are now empty and thus we called libz_func one time
472 * too often. This does not hurt. It simply says that we are done.
474 if (zRC == Z_BUF_ERROR) {
479 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
481 if (zRC != Z_OK && zRC != Z_STREAM_END)
487 static apr_status_t deflate_ctx_cleanup(void *data)
489 deflate_ctx *ctx = (deflate_ctx *)data;
492 ctx->libz_end_func(&ctx->stream);
496 /* ETag must be unique among the possible representations, so a change
497 * to content-encoding requires a corresponding change to the ETag.
498 * This routine appends -transform (e.g., -gzip) to the entity-tag
499 * value inside the double-quotes if an ETag has already been set
500 * and its value already contains double-quotes. PR 39727
502 static void deflate_check_etag(request_rec *r, const char *transform, int etag_opt)
504 const char *etag = apr_table_get(r->headers_out, "ETag");
507 if (etag_opt == AP_DEFLATE_ETAG_REMOVE) {
508 apr_table_unset(r->headers_out, "ETag");
512 if ((etag && ((etaglen = strlen(etag)) > 2))) {
513 if (etag[etaglen - 1] == '"') {
514 apr_size_t transformlen = strlen(transform);
515 char *newtag = apr_palloc(r->pool, etaglen + transformlen + 2);
517 char *e = d + etaglen - 1;
518 const char *s = etag;
520 for (; d < e; ++d, ++s) {
521 *d = *s; /* copy etag to newtag up to last quote */
523 *d++ = '-'; /* append dash to newtag */
525 e = d + transformlen;
526 for (; d < e; ++d, ++s) {
527 *d = *s; /* copy transform to newtag */
529 *d++ = '"'; /* append quote to newtag */
530 *d = '\0'; /* null terminate newtag */
532 apr_table_setn(r->headers_out, "ETag", newtag);
537 /* Check whether the (inflate) ratio exceeds the configured limit/burst. */
538 static int check_ratio(request_rec *r, deflate_ctx *ctx,
539 const deflate_dirconf_t *dc)
541 if (ctx->stream.total_in) {
542 int ratio = ctx->stream.total_out / ctx->stream.total_in;
543 if (ratio < dc->ratio_limit) {
546 else if (++ctx->ratio_hits > dc->ratio_burst) {
553 static int have_ssl_compression(request_rec *r)
556 if (mod_deflate_ssl_var == NULL)
558 comp = mod_deflate_ssl_var(r->pool, r->server, r->connection, r,
559 "SSL_COMPRESS_METHOD");
560 if (comp == NULL || *comp == '\0' || strcmp(comp, "NULL") == 0)
565 static apr_status_t deflate_out_filter(ap_filter_t *f,
566 apr_bucket_brigade *bb)
569 request_rec *r = f->r;
570 deflate_ctx *ctx = f->ctx;
573 apr_size_t len = 0, blen;
575 deflate_filter_config *c;
577 /* Do nothing if asked to filter nothing. */
578 if (APR_BRIGADE_EMPTY(bb)) {
582 c = ap_get_module_config(r->server->module_config,
585 /* If we don't have a context, we need to ensure that it is okay to send
586 * the deflated content. If we have a context, that means we've done
587 * this before and we liked it.
588 * This could be not so nice if we always fail. But, if we succeed,
589 * we're in better shape.
593 const char *encoding;
595 if (have_ssl_compression(r)) {
596 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
597 "Compression enabled at SSL level; not compressing "
599 ap_remove_output_filter(f);
600 return ap_pass_brigade(f->next, bb);
603 /* We have checked above that bb is not empty */
604 e = APR_BRIGADE_LAST(bb);
605 if (APR_BUCKET_IS_EOS(e)) {
607 * If we already know the size of the response, we can skip
608 * compression on responses smaller than the compression overhead.
609 * However, if we compress, we must initialize deflate_out before
610 * calling ap_pass_brigade() for the first time. Otherwise the
611 * headers will be sent to the client without
612 * "Content-Encoding: gzip".
614 e = APR_BRIGADE_FIRST(bb);
617 if (APR_BUCKET_IS_EOS(e)) {
618 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
619 "Not compressing very small response of %"
620 APR_SIZE_T_FMT " bytes", len);
621 ap_remove_output_filter(f);
622 return ap_pass_brigade(f->next, bb);
624 if (APR_BUCKET_IS_METADATA(e)) {
625 e = APR_BUCKET_NEXT(e);
629 rc = apr_bucket_read(e, &data, &blen, APR_BLOCK_READ);
630 if (rc != APR_SUCCESS)
633 /* 50 is for Content-Encoding and Vary headers and ETag suffix */
634 if (len > sizeof(gzip_header) + VALIDATION_SIZE + 50)
637 e = APR_BUCKET_NEXT(e);
641 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
644 * Only work on main request, not subrequests,
645 * that are not a 204 response with no content
646 * and are not tagged with the no-gzip env variable
647 * and not a partial response to a Range request.
649 if ((r->main != NULL) || (r->status == HTTP_NO_CONTENT) ||
650 apr_table_get(r->subprocess_env, "no-gzip") ||
651 apr_table_get(r->headers_out, "Content-Range")
653 if (APLOG_R_IS_LEVEL(r, APLOG_TRACE1)) {
655 (r->main != NULL) ? "subrequest" :
656 (r->status == HTTP_NO_CONTENT) ? "no content" :
657 apr_table_get(r->subprocess_env, "no-gzip") ? "no-gzip" :
659 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
660 "Not compressing (%s)", reason);
662 ap_remove_output_filter(f);
663 return ap_pass_brigade(f->next, bb);
666 /* Some browsers might have problems with content types
667 * other than text/html, so set gzip-only-text/html
668 * (with browsermatch) for them
670 if (r->content_type == NULL
671 || strncmp(r->content_type, "text/html", 9)) {
672 const char *env_value = apr_table_get(r->subprocess_env,
673 "gzip-only-text/html");
674 if ( env_value && (strcmp(env_value,"1") == 0) ) {
675 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
676 "Not compressing, (gzip-only-text/html)");
677 ap_remove_output_filter(f);
678 return ap_pass_brigade(f->next, bb);
682 /* Let's see what our current Content-Encoding is.
683 * If it's already encoded, don't compress again.
684 * (We could, but let's not.)
686 encoding = apr_table_get(r->headers_out, "Content-Encoding");
690 err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
692 encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
696 encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
699 if (r->content_encoding) {
700 encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
701 r->content_encoding, NULL)
702 : r->content_encoding;
706 const char *tmp = encoding;
708 token = ap_get_token(r->pool, &tmp, 0);
709 while (token && *token) {
710 /* stolen from mod_negotiation: */
711 if (strcmp(token, "identity") && strcmp(token, "7bit") &&
712 strcmp(token, "8bit") && strcmp(token, "binary")) {
713 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
714 "Not compressing (content-encoding already "
716 ap_remove_output_filter(f);
717 return ap_pass_brigade(f->next, bb);
720 /* Otherwise, skip token */
724 token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
728 /* Even if we don't accept this request based on it not having
729 * the Accept-Encoding, we need to note that we were looking
730 * for this header and downstream proxies should be aware of that.
732 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
734 /* force-gzip will just force it out regardless if the browser
735 * can actually do anything with it.
737 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
739 /* if they don't have the line, then they can't play */
740 accepts = apr_table_get(r->headers_in, "Accept-Encoding");
741 if (accepts == NULL) {
742 ap_remove_output_filter(f);
743 return ap_pass_brigade(f->next, bb);
746 token = ap_get_token(r->pool, &accepts, 0);
747 while (token && token[0] && ap_cstr_casecmp(token, "gzip")) {
748 /* skip parameters, XXX: ;q=foo evaluation? */
749 while (*accepts == ';') {
751 ap_get_token(r->pool, &accepts, 1);
754 /* retrieve next token */
755 if (*accepts == ',') {
758 token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
761 /* No acceptable token found. */
762 if (token == NULL || token[0] == '\0') {
763 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
764 "Not compressing (no Accept-Encoding: gzip)");
765 ap_remove_output_filter(f);
766 return ap_pass_brigade(f->next, bb);
770 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
771 "Forcing compression (force-gzip set)");
774 /* At this point we have decided to filter the content. Let's try to
775 * to initialize zlib (except for 304 responses, where we will only
776 * send out the headers).
779 if (r->status != HTTP_NOT_MODIFIED) {
780 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
781 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
782 ctx->libz_end_func = deflateEnd;
784 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
785 c->windowSize, c->memlevel,
789 deflateEnd(&ctx->stream);
790 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01383)
791 "unable to init Zlib: "
792 "deflateInit2 returned %d: URL %s",
795 * Remove ourselves as it does not make sense to return:
796 * We are not able to init libz and pass data down the chain
799 ap_remove_output_filter(f);
800 return ap_pass_brigade(f->next, bb);
803 * Register a cleanup function to ensure that we cleanup the internal
806 apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
807 apr_pool_cleanup_null);
809 /* Set the filter init flag so subsequent invocations know we are
812 ctx->filter_init = 1;
816 * Zlib initialization worked, so we can now change the important
817 * content metadata before sending the response out.
820 /* If the entire Content-Encoding is "identity", we can replace it. */
821 if (!encoding || !ap_cstr_casecmp(encoding, "identity")) {
822 apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
825 apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
827 /* Fix r->content_encoding if it was set before */
828 if (r->content_encoding) {
829 r->content_encoding = apr_table_get(r->headers_out,
832 apr_table_unset(r->headers_out, "Content-Length");
833 apr_table_unset(r->headers_out, "Content-MD5");
834 if (c->etag_opt != AP_DEFLATE_ETAG_NOCHANGE) {
835 deflate_check_etag(r, "gzip", c->etag_opt);
838 /* For a 304 response, only change the headers */
839 if (r->status == HTTP_NOT_MODIFIED) {
840 ap_remove_output_filter(f);
841 return ap_pass_brigade(f->next, bb);
844 /* add immortal gzip header */
845 e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
847 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
849 /* initialize deflate output buffer */
850 ctx->stream.next_out = ctx->buffer;
851 ctx->stream.avail_out = c->bufferSize;
852 } else if (!ctx->filter_init) {
853 /* Hmm. We've run through the filter init before as we have a ctx,
854 * but we never initialized. We probably have a dangling ref. Bail.
856 return ap_pass_brigade(f->next, bb);
859 while (!APR_BRIGADE_EMPTY(bb))
864 * Optimization: If we are a HEAD request and bytes_sent is not zero
865 * it means that we have passed the content-length filter once and
866 * have more data to sent. This means that the content-length filter
867 * could not determine our content-length for the response to the
868 * HEAD request anyway (the associated GET request would deliver the
869 * body in chunked encoding) and we can stop compressing.
871 if (r->header_only && r->bytes_sent) {
872 ap_remove_output_filter(f);
873 return ap_pass_brigade(f->next, bb);
876 e = APR_BRIGADE_FIRST(bb);
878 if (APR_BUCKET_IS_EOS(e)) {
881 ctx->stream.avail_in = 0; /* should be zero already anyway */
882 /* flush the remaining data from the zlib buffers */
883 flush_libz_buffer(ctx, c, deflate, Z_FINISH, NO_UPDATE_CRC);
885 buf = apr_palloc(r->pool, VALIDATION_SIZE);
886 putLong((unsigned char *)&buf[0], ctx->crc);
887 putLong((unsigned char *)&buf[4], ctx->stream.total_in);
889 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
891 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
892 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01384)
893 "Zlib: Compressed %ld to %ld : URL %s",
894 ctx->stream.total_in, ctx->stream.total_out, r->uri);
896 /* leave notes for logging */
897 if (c->note_input_name) {
898 apr_table_setn(r->notes, c->note_input_name,
899 (ctx->stream.total_in > 0)
900 ? apr_off_t_toa(r->pool,
901 ctx->stream.total_in)
905 if (c->note_output_name) {
906 apr_table_setn(r->notes, c->note_output_name,
907 (ctx->stream.total_in > 0)
908 ? apr_off_t_toa(r->pool,
909 ctx->stream.total_out)
913 if (c->note_ratio_name) {
914 apr_table_setn(r->notes, c->note_ratio_name,
915 (ctx->stream.total_in > 0)
917 (int)(ctx->stream.total_out
919 / ctx->stream.total_in))
923 deflateEnd(&ctx->stream);
924 /* No need for cleanup any longer */
925 apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
927 /* Remove EOS from the old list, and insert into the new. */
928 APR_BUCKET_REMOVE(e);
929 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
931 /* Okay, we've seen the EOS.
932 * Time to pass it along down the chain.
934 rv = ap_pass_brigade(f->next, ctx->bb);
935 apr_brigade_cleanup(ctx->bb);
939 if (APR_BUCKET_IS_FLUSH(e)) {
940 /* flush the remaining data from the zlib buffers */
941 zRC = flush_libz_buffer(ctx, c, deflate, Z_SYNC_FLUSH,
944 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01385)
945 "Zlib error %d flushing zlib output buffer (%s)",
946 zRC, ctx->stream.msg);
950 /* Remove flush bucket from old brigade anf insert into the new. */
951 APR_BUCKET_REMOVE(e);
952 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
953 rv = ap_pass_brigade(f->next, ctx->bb);
954 apr_brigade_cleanup(ctx->bb);
955 if (rv != APR_SUCCESS) {
961 if (APR_BUCKET_IS_METADATA(e)) {
963 * Remove meta data bucket from old brigade and insert into the
966 APR_BUCKET_REMOVE(e);
967 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
972 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
974 apr_bucket_delete(e);
977 if (len > APR_INT32_MAX) {
978 apr_bucket_split(e, APR_INT32_MAX);
979 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
982 /* This crc32 function is from zlib. */
983 ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
986 ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
987 * but we'll just have to
989 ctx->stream.avail_in = (int)len;
991 while (ctx->stream.avail_in != 0) {
992 if (ctx->stream.avail_out == 0) {
993 consume_buffer(ctx, c, c->bufferSize, NO_UPDATE_CRC, ctx->bb);
995 /* Send what we have right now to the next filter. */
996 rv = ap_pass_brigade(f->next, ctx->bb);
997 apr_brigade_cleanup(ctx->bb);
998 if (rv != APR_SUCCESS) {
1003 zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
1006 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01386)
1007 "Zlib error %d deflating data (%s)", zRC,
1009 return APR_EGENERAL;
1013 apr_bucket_delete(e);
1019 static apr_status_t consume_zlib_flags(deflate_ctx *ctx,
1020 const char **data, apr_size_t *len)
1022 if ((ctx->zlib_flags & EXTRA_FIELD)) {
1023 /* Consume 2 bytes length prefixed data. */
1024 if (ctx->consume_pos == 0) {
1026 return APR_INCOMPLETE;
1028 ctx->consume_len = (unsigned int)**data;
1033 if (ctx->consume_pos == 1) {
1035 return APR_INCOMPLETE;
1037 ctx->consume_len += ((unsigned int)**data) << 8;
1042 if (*len < ctx->consume_len) {
1043 ctx->consume_len -= *len;
1045 return APR_INCOMPLETE;
1047 *data += ctx->consume_len;
1048 *len -= ctx->consume_len;
1050 ctx->consume_len = ctx->consume_pos = 0;
1051 ctx->zlib_flags &= ~EXTRA_FIELD;
1054 if ((ctx->zlib_flags & ORIG_NAME)) {
1055 /* Consume nul terminated string. */
1056 while (*len && **data) {
1061 return APR_INCOMPLETE;
1067 ctx->zlib_flags &= ~ORIG_NAME;
1070 if ((ctx->zlib_flags & COMMENT)) {
1071 /* Consume nul terminated string. */
1072 while (*len && **data) {
1077 return APR_INCOMPLETE;
1083 ctx->zlib_flags &= ~COMMENT;
1086 if ((ctx->zlib_flags & HEAD_CRC)) {
1087 /* Consume CRC16 (2 octets). */
1088 if (ctx->consume_pos == 0) {
1090 return APR_INCOMPLETE;
1097 return APR_INCOMPLETE;
1102 ctx->consume_pos = 0;
1103 ctx->zlib_flags &= ~HEAD_CRC;
1109 /* This is the deflate input filter (inflates). */
1110 static apr_status_t deflate_in_filter(ap_filter_t *f,
1111 apr_bucket_brigade *bb,
1112 ap_input_mode_t mode,
1113 apr_read_type_e block,
1114 apr_off_t readbytes)
1117 request_rec *r = f->r;
1118 deflate_ctx *ctx = f->ctx;
1121 deflate_filter_config *c;
1122 deflate_dirconf_t *dc;
1123 apr_off_t inflate_limit;
1125 /* just get out of the way of things we don't want. */
1126 if (mode != AP_MODE_READBYTES) {
1127 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1130 c = ap_get_module_config(r->server->module_config, &deflate_module);
1131 dc = ap_get_module_config(r->per_dir_config, &deflate_module);
1133 if (!ctx || ctx->header_len < sizeof(ctx->header)) {
1137 /* only work on main request/no subrequests */
1138 if (!ap_is_initial_req(r)) {
1139 ap_remove_input_filter(f);
1140 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1143 /* We can't operate on Content-Ranges */
1144 if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
1145 ap_remove_input_filter(f);
1146 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1149 /* Check whether request body is gzipped.
1151 * If it is, we're transforming the contents, invalidating
1152 * some request headers including Content-Encoding.
1154 * If not, we just remove ourself.
1156 if (check_gzip(r, r->headers_in, NULL) == 0) {
1157 ap_remove_input_filter(f);
1158 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1161 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1162 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1163 ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1164 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1168 apr_brigade_cleanup(ctx->bb);
1170 len = sizeof(ctx->header) - ctx->header_len;
1171 rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block,
1174 /* ap_get_brigade may return success with an empty brigade for
1175 * a non-blocking read which would block (an empty brigade for
1176 * a blocking read is an issue which is simply forwarded here).
1178 if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(ctx->bb)) {
1182 /* zero length body? step aside */
1183 bkt = APR_BRIGADE_FIRST(ctx->bb);
1184 if (APR_BUCKET_IS_EOS(bkt)) {
1185 if (ctx->header_len) {
1186 /* If the header was (partially) read it's an error, this
1187 * is not a gzip Content-Encoding, as claimed.
1189 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02619)
1190 "Encountered premature end-of-stream while "
1191 "reading inflate header");
1192 return APR_EGENERAL;
1194 APR_BUCKET_REMOVE(bkt);
1195 APR_BRIGADE_INSERT_TAIL(bb, bkt);
1196 ap_remove_input_filter(f);
1200 rv = apr_brigade_flatten(ctx->bb,
1201 ctx->header + ctx->header_len, &len);
1202 if (rv != APR_SUCCESS) {
1205 if (len && !ctx->header_len) {
1206 apr_table_unset(r->headers_in, "Content-Length");
1207 apr_table_unset(r->headers_in, "Content-MD5");
1209 ctx->header_len += len;
1211 } while (ctx->header_len < sizeof(ctx->header));
1213 /* We didn't get the magic bytes. */
1214 if (ctx->header[0] != deflate_magic[0] ||
1215 ctx->header[1] != deflate_magic[1]) {
1216 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01387)
1217 "Zlib: Invalid header");
1218 return APR_EGENERAL;
1221 ctx->zlib_flags = ctx->header[3];
1222 if ((ctx->zlib_flags & RESERVED)) {
1223 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01388)
1224 "Zlib: Invalid flags %02x", ctx->zlib_flags);
1225 return APR_EGENERAL;
1228 zRC = inflateInit2(&ctx->stream, c->windowSize);
1232 inflateEnd(&ctx->stream);
1233 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01389)
1234 "unable to init Zlib: "
1235 "inflateInit2 returned %d: URL %s",
1237 ap_remove_input_filter(f);
1238 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1241 /* initialize deflate output buffer */
1242 ctx->stream.next_out = ctx->buffer;
1243 ctx->stream.avail_out = c->bufferSize;
1245 apr_brigade_cleanup(ctx->bb);
1248 inflate_limit = dc->inflate_limit;
1249 if (inflate_limit == 0) {
1250 /* The core is checking the deflated body, we'll check the inflated */
1251 inflate_limit = ap_get_limit_req_body(f->r);
1254 if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1255 rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
1257 /* Don't terminate on EAGAIN (or success with an empty brigade in
1258 * non-blocking mode), just return focus.
1260 if (block == APR_NONBLOCK_READ
1261 && (APR_STATUS_IS_EAGAIN(rv)
1262 || (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(ctx->bb)))) {
1265 if (rv != APR_SUCCESS) {
1266 inflateEnd(&ctx->stream);
1270 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
1271 bkt != APR_BRIGADE_SENTINEL(ctx->bb);
1272 bkt = APR_BUCKET_NEXT(bkt))
1277 if (APR_BUCKET_IS_EOS(bkt)) {
1279 inflateEnd(&ctx->stream);
1280 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02481)
1281 "Encountered premature end-of-stream while inflating");
1282 return APR_EGENERAL;
1285 /* Move everything to the returning brigade. */
1286 APR_BUCKET_REMOVE(bkt);
1287 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, bkt);
1291 if (APR_BUCKET_IS_FLUSH(bkt)) {
1294 ctx->inflate_total += ctx->stream.avail_out;
1295 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
1296 ctx->inflate_total -= ctx->stream.avail_out;
1298 inflateEnd(&ctx->stream);
1299 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01391)
1300 "Zlib error %d inflating data (%s)", zRC,
1302 return APR_EGENERAL;
1305 if (inflate_limit && ctx->inflate_total > inflate_limit) {
1306 inflateEnd(&ctx->stream);
1307 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02647)
1308 "Inflated content length of %" APR_OFF_T_FMT
1309 " is larger than the configured limit"
1310 " of %" APR_OFF_T_FMT,
1311 ctx->inflate_total, inflate_limit);
1315 if (!check_ratio(r, ctx, dc)) {
1316 inflateEnd(&ctx->stream);
1317 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02805)
1318 "Inflated content ratio is larger than the "
1319 "configured limit %i by %i time(s)",
1320 dc->ratio_limit, dc->ratio_burst);
1324 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1325 UPDATE_CRC, ctx->proc_bb);
1327 /* Flush everything so far in the returning brigade, but continue
1328 * reading should EOS/more follow (don't lose them).
1330 tmp_b = APR_BUCKET_PREV(bkt);
1331 APR_BUCKET_REMOVE(bkt);
1332 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, bkt);
1337 /* sanity check - data after completed compressed body and before eos? */
1339 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02482)
1340 "Encountered extra data after compressed data");
1341 return APR_EGENERAL;
1345 apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
1349 if (len > APR_INT32_MAX) {
1350 apr_bucket_split(bkt, APR_INT32_MAX);
1351 apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
1354 if (ctx->zlib_flags) {
1355 rv = consume_zlib_flags(ctx, &data, &len);
1356 if (rv == APR_SUCCESS) {
1357 ctx->zlib_flags = 0;
1364 /* pass through zlib inflate. */
1365 ctx->stream.next_in = (unsigned char *)data;
1366 ctx->stream.avail_in = (int)len;
1370 if (!ctx->validation_buffer) {
1371 while (ctx->stream.avail_in != 0) {
1372 if (ctx->stream.avail_out == 0) {
1373 consume_buffer(ctx, c, c->bufferSize, UPDATE_CRC,
1377 ctx->inflate_total += ctx->stream.avail_out;
1378 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1379 ctx->inflate_total -= ctx->stream.avail_out;
1380 if (zRC != Z_OK && zRC != Z_STREAM_END) {
1381 inflateEnd(&ctx->stream);
1382 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01392)
1383 "Zlib error %d inflating data (%s)", zRC,
1385 return APR_EGENERAL;
1388 if (inflate_limit && ctx->inflate_total > inflate_limit) {
1389 inflateEnd(&ctx->stream);
1390 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02648)
1391 "Inflated content length of %" APR_OFF_T_FMT
1392 " is larger than the configured limit"
1393 " of %" APR_OFF_T_FMT,
1394 ctx->inflate_total, inflate_limit);
1398 if (!check_ratio(r, ctx, dc)) {
1399 inflateEnd(&ctx->stream);
1400 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02649)
1401 "Inflated content ratio is larger than the "
1402 "configured limit %i by %i time(s)",
1403 dc->ratio_limit, dc->ratio_burst);
1407 if (zRC == Z_STREAM_END) {
1408 ctx->validation_buffer = apr_pcalloc(r->pool,
1410 ctx->validation_buffer_length = 0;
1416 if (ctx->validation_buffer) {
1417 apr_size_t avail, valid;
1418 unsigned char *buf = ctx->validation_buffer;
1420 avail = ctx->stream.avail_in;
1421 valid = (apr_size_t)VALIDATION_SIZE -
1422 ctx->validation_buffer_length;
1425 * We have inflated all data. Now try to capture the
1426 * validation bytes. We may not have them all available
1427 * right now, but capture what is there.
1429 if (avail < valid) {
1430 memcpy(buf + ctx->validation_buffer_length,
1431 ctx->stream.next_in, avail);
1432 ctx->validation_buffer_length += avail;
1435 memcpy(buf + ctx->validation_buffer_length,
1436 ctx->stream.next_in, valid);
1437 ctx->validation_buffer_length += valid;
1439 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01393)
1440 "Zlib: Inflated %ld to %ld : URL %s",
1441 ctx->stream.total_in, ctx->stream.total_out,
1444 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1445 UPDATE_CRC, ctx->proc_bb);
1448 unsigned long compCRC, compLen;
1449 compCRC = getLong(buf);
1450 if (ctx->crc != compCRC) {
1451 inflateEnd(&ctx->stream);
1452 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01394)
1453 "Zlib: CRC error inflating data");
1454 return APR_EGENERAL;
1456 compLen = getLong(buf + VALIDATION_SIZE / 2);
1457 /* gzip stores original size only as 4 byte value */
1458 if ((ctx->stream.total_out & 0xFFFFFFFF) != compLen) {
1459 inflateEnd(&ctx->stream);
1460 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01395)
1461 "Zlib: Length %ld of inflated data does "
1462 "not match expected value %ld",
1463 ctx->stream.total_out, compLen);
1464 return APR_EGENERAL;
1468 inflateEnd(&ctx->stream);
1472 /* Did we have trailing data behind the closing 8 bytes? */
1473 if (avail > valid) {
1474 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02485)
1475 "Encountered extra data after compressed data");
1476 return APR_EGENERAL;
1481 apr_brigade_cleanup(ctx->bb);
1484 /* If we are about to return nothing for a 'blocking' read and we have
1485 * some data in our zlib buffer, flush it out so we can return something.
1487 if (block == APR_BLOCK_READ &&
1488 APR_BRIGADE_EMPTY(ctx->proc_bb) &&
1489 ctx->stream.avail_out < c->bufferSize) {
1490 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1491 UPDATE_CRC, ctx->proc_bb);
1494 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1495 if (apr_brigade_partition(ctx->proc_bb, readbytes, &bkt) == APR_INCOMPLETE) {
1496 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1499 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1500 apr_brigade_split_ex(bb, bkt, ctx->proc_bb);
1502 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
1503 ap_remove_input_filter(f);
1511 /* Filter to inflate for a content-transforming proxy. */
1512 static apr_status_t inflate_out_filter(ap_filter_t *f,
1513 apr_bucket_brigade *bb)
1516 request_rec *r = f->r;
1517 deflate_ctx *ctx = f->ctx;
1520 deflate_filter_config *c;
1521 deflate_dirconf_t *dc;
1523 /* Do nothing if asked to filter nothing. */
1524 if (APR_BRIGADE_EMPTY(bb)) {
1528 c = ap_get_module_config(r->server->module_config, &deflate_module);
1529 dc = ap_get_module_config(r->per_dir_config, &deflate_module);
1534 * Only work on main request, not subrequests,
1535 * that are not a 204 response with no content
1536 * and not a partial response to a Range request,
1537 * and only when Content-Encoding ends in gzip.
1539 if (!ap_is_initial_req(r) || (r->status == HTTP_NO_CONTENT) ||
1540 (apr_table_get(r->headers_out, "Content-Range") != NULL) ||
1541 (check_gzip(r, r->headers_out, r->err_headers_out) == 0)
1543 ap_remove_output_filter(f);
1544 return ap_pass_brigade(f->next, bb);
1548 * At this point we have decided to filter the content, so change
1549 * important content metadata before sending any response out.
1550 * Content-Encoding was already reset by the check_gzip() call.
1552 apr_table_unset(r->headers_out, "Content-Length");
1553 apr_table_unset(r->headers_out, "Content-MD5");
1554 if (c->etag_opt != AP_DEFLATE_ETAG_NOCHANGE) {
1555 deflate_check_etag(r, "gunzip", c->etag_opt);
1558 /* For a 304 response, only change the headers */
1559 if (r->status == HTTP_NOT_MODIFIED) {
1560 ap_remove_output_filter(f);
1561 return ap_pass_brigade(f->next, bb);
1564 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1565 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1566 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1567 ctx->libz_end_func = inflateEnd;
1568 ctx->validation_buffer = NULL;
1569 ctx->validation_buffer_length = 0;
1571 zRC = inflateInit2(&ctx->stream, c->windowSize);
1575 inflateEnd(&ctx->stream);
1576 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01397)
1577 "unable to init Zlib: "
1578 "inflateInit2 returned %d: URL %s",
1581 * Remove ourselves as it does not make sense to return:
1582 * We are not able to init libz and pass data down the chain
1585 ap_remove_output_filter(f);
1586 return ap_pass_brigade(f->next, bb);
1590 * Register a cleanup function to ensure that we cleanup the internal
1593 apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
1594 apr_pool_cleanup_null);
1596 /* initialize inflate output buffer */
1597 ctx->stream.next_out = ctx->buffer;
1598 ctx->stream.avail_out = c->bufferSize;
1601 while (!APR_BRIGADE_EMPTY(bb))
1606 e = APR_BRIGADE_FIRST(bb);
1608 if (APR_BUCKET_IS_EOS(e)) {
1610 * We are really done now. Ensure that we never return here, even
1611 * if a second EOS bucket falls down the chain. Thus remove
1614 ap_remove_output_filter(f);
1615 /* should be zero already anyway */
1616 ctx->stream.avail_in = 0;
1618 * Flush the remaining data from the zlib buffers. It is correct
1619 * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1620 * deflate case. In the inflate case Z_FINISH requires to have a
1621 * large enough output buffer to put ALL data in otherwise it
1622 * fails, whereas in the deflate case you can empty a filled output
1623 * buffer and call it again until no more output can be created.
1625 flush_libz_buffer(ctx, c, inflate, Z_SYNC_FLUSH, UPDATE_CRC);
1626 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01398)
1627 "Zlib: Inflated %ld to %ld : URL %s",
1628 ctx->stream.total_in, ctx->stream.total_out, r->uri);
1630 if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1631 unsigned long compCRC, compLen;
1632 compCRC = getLong(ctx->validation_buffer);
1633 if (ctx->crc != compCRC) {
1634 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01399)
1635 "Zlib: Checksum of inflated stream invalid");
1636 return APR_EGENERAL;
1638 ctx->validation_buffer += VALIDATION_SIZE / 2;
1639 compLen = getLong(ctx->validation_buffer);
1640 /* gzip stores original size only as 4 byte value */
1641 if ((ctx->stream.total_out & 0xFFFFFFFF) != compLen) {
1642 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01400)
1643 "Zlib: Length of inflated stream invalid");
1644 return APR_EGENERAL;
1648 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01401)
1649 "Zlib: Validation bytes not present");
1650 return APR_EGENERAL;
1653 inflateEnd(&ctx->stream);
1654 /* No need for cleanup any longer */
1655 apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1657 /* Remove EOS from the old list, and insert into the new. */
1658 APR_BUCKET_REMOVE(e);
1659 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1662 * Okay, we've seen the EOS.
1663 * Time to pass it along down the chain.
1665 rv = ap_pass_brigade(f->next, ctx->bb);
1666 apr_brigade_cleanup(ctx->bb);
1670 if (APR_BUCKET_IS_FLUSH(e)) {
1671 /* flush the remaining data from the zlib buffers */
1672 zRC = flush_libz_buffer(ctx, c, inflate, Z_SYNC_FLUSH, UPDATE_CRC);
1673 if (zRC == Z_STREAM_END) {
1674 if (ctx->validation_buffer == NULL) {
1675 ctx->validation_buffer = apr_pcalloc(f->r->pool,
1679 else if (zRC != Z_OK) {
1680 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01402)
1681 "Zlib error %d flushing inflate buffer (%s)",
1682 zRC, ctx->stream.msg);
1683 return APR_EGENERAL;
1686 /* Remove flush bucket from old brigade anf insert into the new. */
1687 APR_BUCKET_REMOVE(e);
1688 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1689 rv = ap_pass_brigade(f->next, ctx->bb);
1690 apr_brigade_cleanup(ctx->bb);
1691 if (rv != APR_SUCCESS) {
1697 if (APR_BUCKET_IS_METADATA(e)) {
1699 * Remove meta data bucket from old brigade and insert into the
1702 APR_BUCKET_REMOVE(e);
1703 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1708 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1710 apr_bucket_delete(e);
1713 if (len > APR_INT32_MAX) {
1714 apr_bucket_split(e, APR_INT32_MAX);
1715 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1718 /* first bucket contains zlib header */
1719 if (ctx->header_len < sizeof(ctx->header)) {
1722 rem = sizeof(ctx->header) - ctx->header_len;
1724 memcpy(ctx->header + ctx->header_len, data, len);
1725 ctx->header_len += len;
1726 apr_bucket_delete(e);
1729 memcpy(ctx->header + ctx->header_len, data, rem);
1730 ctx->header_len += rem;
1733 zlib_method = ctx->header[2];
1734 if (zlib_method != Z_DEFLATED) {
1735 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01404)
1736 "inflate: data not deflated!");
1737 ap_remove_output_filter(f);
1738 return ap_pass_brigade(f->next, bb);
1740 if (ctx->header[0] != deflate_magic[0] ||
1741 ctx->header[1] != deflate_magic[1]) {
1742 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01405)
1743 "inflate: bad header");
1744 return APR_EGENERAL ;
1746 ctx->zlib_flags = ctx->header[3];
1747 if ((ctx->zlib_flags & RESERVED)) {
1748 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02620)
1749 "inflate: bad flags %02x",
1751 return APR_EGENERAL;
1755 apr_bucket_delete(e);
1762 if (ctx->zlib_flags) {
1763 rv = consume_zlib_flags(ctx, &data, &len);
1764 if (rv == APR_SUCCESS) {
1765 ctx->zlib_flags = 0;
1768 apr_bucket_delete(e);
1773 /* pass through zlib inflate. */
1774 ctx->stream.next_in = (unsigned char *)data;
1775 ctx->stream.avail_in = len;
1777 if (ctx->validation_buffer) {
1778 if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1779 apr_size_t copy_size;
1781 copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1782 if (copy_size > ctx->stream.avail_in)
1783 copy_size = ctx->stream.avail_in;
1784 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1785 ctx->stream.next_in, copy_size);
1786 /* Saved copy_size bytes */
1787 ctx->stream.avail_in -= copy_size;
1788 ctx->validation_buffer_length += copy_size;
1790 if (ctx->stream.avail_in) {
1791 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01407)
1792 "Zlib: %d bytes of garbage at the end of "
1793 "compressed stream.", ctx->stream.avail_in);
1795 * There is nothing worth consuming for zlib left, because it is
1796 * either garbage data or the data has been copied to the
1797 * validation buffer (processing validation data is no business
1798 * for zlib). So set ctx->stream.avail_in to zero to indicate
1799 * this to the following while loop.
1801 ctx->stream.avail_in = 0;
1805 while (ctx->stream.avail_in != 0) {
1806 if (ctx->stream.avail_out == 0) {
1807 consume_buffer(ctx, c, c->bufferSize, UPDATE_CRC, ctx->bb);
1809 /* Send what we have right now to the next filter. */
1810 rv = ap_pass_brigade(f->next, ctx->bb);
1811 apr_brigade_cleanup(ctx->bb);
1812 if (rv != APR_SUCCESS) {
1817 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1819 if (zRC != Z_OK && zRC != Z_STREAM_END) {
1820 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01409)
1821 "Zlib error %d inflating data (%s)", zRC,
1823 return APR_EGENERAL;
1826 /* Don't check length limits on inflate_out */
1827 if (!check_ratio(r, ctx, dc)) {
1828 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02650)
1829 "Inflated content ratio is larger than the "
1830 "configured limit %i by %i time(s)",
1831 dc->ratio_limit, dc->ratio_burst);
1835 if (zRC == Z_STREAM_END) {
1837 * We have inflated all data. Now try to capture the
1838 * validation bytes. We may not have them all available
1839 * right now, but capture what is there.
1841 ctx->validation_buffer = apr_pcalloc(f->r->pool,
1843 if (ctx->stream.avail_in > VALIDATION_SIZE) {
1844 ctx->validation_buffer_length = VALIDATION_SIZE;
1845 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01408)
1846 "Zlib: %d bytes of garbage at the end of "
1847 "compressed stream.",
1848 ctx->stream.avail_in - VALIDATION_SIZE);
1850 else if (ctx->stream.avail_in > 0) {
1851 ctx->validation_buffer_length = ctx->stream.avail_in;
1853 if (ctx->validation_buffer_length)
1854 memcpy(ctx->validation_buffer, ctx->stream.next_in,
1855 ctx->validation_buffer_length);
1860 apr_bucket_delete(e);
1866 static int mod_deflate_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1867 apr_pool_t *ptemp, server_rec *s)
1869 mod_deflate_ssl_var = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
1874 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1875 static void register_hooks(apr_pool_t *p)
1877 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1878 AP_FTYPE_CONTENT_SET);
1879 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1880 AP_FTYPE_RESOURCE-1);
1881 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1882 AP_FTYPE_CONTENT_SET);
1883 ap_hook_post_config(mod_deflate_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1886 static const command_rec deflate_filter_cmds[] = {
1887 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1888 "Set a note to report on compression ratio"),
1889 AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1890 RSRC_CONF, "Set the Deflate window size (1-15)"),
1891 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1892 "Set the Deflate Buffer Size"),
1893 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1894 "Set the Deflate Memory Level (1-9)"),
1895 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1896 "Set the Deflate Compression Level (1-9)"),
1897 AP_INIT_TAKE1("DeflateAlterEtag", deflate_set_etag, NULL, RSRC_CONF,
1898 "Set how mod_deflate should modify ETAG response headers: 'AddSuffix' (default), 'NoChange' (2.2.x behavior), 'Remove'"),
1899 AP_INIT_TAKE1("DeflateInflateLimitRequestBody", deflate_set_inflate_limit, NULL, OR_ALL,
1900 "Set a limit on size of inflated input"),
1901 AP_INIT_TAKE1("DeflateInflateRatioLimit", deflate_set_inflate_ratio_limit, NULL, OR_ALL,
1902 "Set the inflate ratio limit above which inflation is "
1903 "aborted (default: " APR_STRINGIFY(AP_INFLATE_RATIO_LIMIT) ")"),
1904 AP_INIT_TAKE1("DeflateInflateRatioBurst", deflate_set_inflate_ratio_burst, NULL, OR_ALL,
1905 "Set the maximum number of following inflate ratios above limit "
1906 "(default: " APR_STRINGIFY(AP_INFLATE_RATIO_BURST) ")"),
1910 /* zlib can be built with #define deflate z_deflate */
1914 AP_DECLARE_MODULE(deflate) = {
1915 STANDARD20_MODULE_STUFF,
1916 create_deflate_dirconf, /* dir config creater */
1917 NULL, /* dir merger --- default is to override */
1918 create_deflate_server_config, /* server config */
1919 NULL, /* merge server config */
1920 deflate_filter_cmds, /* command table */
1921 register_hooks /* register hooks */