Apache HTTPD
mod_deflate.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/*
18 * mod_deflate.c: Perform deflate content-encoding on the fly
19 *
20 * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew
21 */
22
23/*
24 * Portions of this software are based upon zlib code by Jean-loup Gailly
25 * (zlib functions gz_open and gzwrite, check_header)
26 */
27
28/* zlib flags */
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 */
35
36
37#include "httpd.h"
38#include "http_config.h"
39#include "http_log.h"
40#include "http_core.h"
41#include "apr_lib.h"
42#include "apr_strings.h"
43#include "apr_general.h"
44#include "util_filter.h"
45#include "apr_buckets.h"
46#include "http_protocol.h"
47#include "http_request.h"
48#include "http_ssl.h"
49#define APR_WANT_STRFUNC
50#include "apr_want.h"
51
52#include "zlib.h"
53
54static const char deflateFilterName[] = "DEFLATE";
55module AP_MODULE_DECLARE_DATA deflate_module;
56
57#define AP_INFLATE_RATIO_LIMIT 200
58#define AP_INFLATE_RATIO_BURST 3
59
60#define AP_DEFLATE_ETAG_ADDSUFFIX 0
61#define AP_DEFLATE_ETAG_NOCHANGE 1
62#define AP_DEFLATE_ETAG_REMOVE 2
63
75
81
82/* RFC 1952 Section 2.3 defines the gzip header:
83 *
84 * +---+---+---+---+---+---+---+---+---+---+
85 * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
86 * +---+---+---+---+---+---+---+---+---+---+
87 */
88static const char gzip_header[10] =
89{ '\037', '\213', Z_DEFLATED, 0,
90 0, 0, 0, 0, /* mtime */
91 0, 0x03 /* Unix OS_CODE */
92};
93
94/* magic header */
95static const char deflate_magic[2] = { '\037', '\213' };
96
97/* windowsize is negative to suppress Zlib header */
98#define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
99#define DEFAULT_WINDOWSIZE -15
100#define DEFAULT_MEMLEVEL 9
101#define DEFAULT_BUFFERSIZE 8096
102
103/* Check whether a request is gzipped, so we can un-gzip it.
104 * If a request has multiple encodings, we need the gzip
105 * to be the outermost non-identity encoding.
106 */
108{
109 int found = 0;
110 apr_table_t *hdrs = hdrs1;
111 const char *encoding = apr_table_get(hdrs, "Content-Encoding");
112
113 if (!encoding && (hdrs2 != NULL)) {
114 /* the output filter has two tables and a content_encoding to check */
115 encoding = apr_table_get(hdrs2, "Content-Encoding");
116 hdrs = hdrs2;
117 if (!encoding) {
119 hdrs = NULL;
120 }
121 }
122 if (encoding && *encoding) {
123
124 /* check the usual/simple case first */
125 if (!ap_cstr_casecmp(encoding, "gzip")
126 || !ap_cstr_casecmp(encoding, "x-gzip")) {
127 found = 1;
128 if (hdrs) {
129 apr_table_unset(hdrs, "Content-Encoding");
130 }
131 else {
133 }
134 }
135 else if (ap_strchr_c(encoding, ',') != NULL) {
136 /* If the outermost encoding isn't gzip, there's nothing
137 * we can do. So only check the last non-identity token
138 */
140 char *ptr;
141 for(;;) {
142 char *token = ap_strrchr(new_encoding, ',');
143 if (!token) { /* gzip:identity or other:identity */
144 if (!ap_cstr_casecmp(new_encoding, "gzip")
145 || !ap_cstr_casecmp(new_encoding, "x-gzip")) {
146 found = 1;
147 if (hdrs) {
148 apr_table_unset(hdrs, "Content-Encoding");
149 }
150 else {
152 }
153 }
154 break; /* seen all tokens */
155 }
156 for (ptr=token+1; apr_isspace(*ptr); ++ptr);
157 if (!ap_cstr_casecmp(ptr, "gzip")
158 || !ap_cstr_casecmp(ptr, "x-gzip")) {
159 *token = '\0';
160 if (hdrs) {
161 apr_table_setn(hdrs, "Content-Encoding", new_encoding);
162 }
163 else {
165 }
166 found = 1;
167 }
168 else if (!ptr[0] || !ap_cstr_casecmp(ptr, "identity")) {
169 *token = '\0';
170 continue; /* strip the token and find the next one */
171 }
172 break; /* found a non-identity token */
173 }
174 }
175 }
176 /*
177 * If we have dealt with the headers above but content_encoding was set
178 * before sync it with the new value in the hdrs table as
179 * r->content_encoding takes precedence later on in the http_header_filter
180 * and hence would destroy what we have just set in the hdrs table.
181 */
182 if (hdrs && r->content_encoding) {
183 r->content_encoding = apr_table_get(hdrs, "Content-Encoding");
184 }
185 return found;
186}
187
188/* Outputs a long in LSB order to the given file
189 * only the bottom 4 bits are required for the deflate file format.
190 */
191static void putLong(unsigned char *string, unsigned long x)
192{
193 string[0] = (unsigned char)(x & 0xff);
194 string[1] = (unsigned char)((x & 0xff00) >> 8);
195 string[2] = (unsigned char)((x & 0xff0000) >> 16);
196 string[3] = (unsigned char)((x & 0xff000000) >> 24);
197}
198
199/* Inputs a string and returns a long.
200 */
201static unsigned long getLong(unsigned char *string)
202{
203 return ((unsigned long)string[0])
204 | (((unsigned long)string[1]) << 8)
205 | (((unsigned long)string[2]) << 16)
206 | (((unsigned long)string[3]) << 24);
207}
208
210{
212
213 c->memlevel = DEFAULT_MEMLEVEL;
214 c->windowSize = DEFAULT_WINDOWSIZE;
215 c->bufferSize = DEFAULT_BUFFERSIZE;
216 c->compressionlevel = DEFAULT_COMPRESSION;
217
218 return c;
219}
220
222{
223 deflate_dirconf_t *dc = apr_pcalloc(p, sizeof(*dc));
226 return dc;
227}
228
229static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
230 const char *arg)
231{
232 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
233 &deflate_module);
234 int i;
235
236 i = atoi(arg);
237
238 if (i < 1 || i > 15)
239 return "DeflateWindowSize must be between 1 and 15";
240
241 c->windowSize = i * -1;
242
243 return NULL;
244}
245
246static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
247 const char *arg)
248{
249 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
250 &deflate_module);
251 int n = atoi(arg);
252
253 if (n <= 0) {
254 return "DeflateBufferSize should be positive";
255 }
256
257 c->bufferSize = n;
258
259 return NULL;
260}
261static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
262 const char *arg1, const char *arg2)
263{
264 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
265 &deflate_module);
266
267 if (arg2 == NULL) {
268 c->note_ratio_name = arg1;
269 }
270 else if (!strcasecmp(arg1, "ratio")) {
271 c->note_ratio_name = arg2;
272 }
273 else if (!strcasecmp(arg1, "input")) {
274 c->note_input_name = arg2;
275 }
276 else if (!strcasecmp(arg1, "output")) {
277 c->note_output_name = arg2;
278 }
279 else {
280 return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
281 }
282
283 return NULL;
284}
285
286static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
287 const char *arg)
288{
289 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
290 &deflate_module);
291 int i;
292
293 i = atoi(arg);
294
295 if (i < 1 || i > 9)
296 return "DeflateMemLevel must be between 1 and 9";
297
298 c->memlevel = i;
299
300 return NULL;
301}
302
303static const char *deflate_set_etag(cmd_parms *cmd, void *dummy,
304 const char *arg)
305{
306 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
307 &deflate_module);
308
309 if (!strcasecmp(arg, "NoChange")) {
310 c->etag_opt = AP_DEFLATE_ETAG_NOCHANGE;
311 }
312 else if (!strcasecmp(arg, "AddSuffix")) {
313 c->etag_opt = AP_DEFLATE_ETAG_ADDSUFFIX;
314 }
315 else if (!strcasecmp(arg, "Remove")) {
316 c->etag_opt = AP_DEFLATE_ETAG_REMOVE;
317 }
318 else {
319 return "DeflateAlterETAG accepts only 'NoChange', 'AddSuffix', and 'Remove'";
320 }
321
322 return NULL;
323}
324
325
327 const char *arg)
328{
329 deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
330 &deflate_module);
331 int i;
332
333 i = atoi(arg);
334
335 if (i < 1 || i > 9)
336 return "Compression Level must be between 1 and 9";
337
338 c->compressionlevel = i;
339
340 return NULL;
341}
342
343
345 const char *arg)
346{
348 char *errp;
349
350 if (APR_SUCCESS != apr_strtoff(&dc->inflate_limit, arg, &errp, 10)) {
351 return "DeflateInflateLimitRequestBody is not parsable.";
352 }
353 if (*errp || dc->inflate_limit < 0) {
354 return "DeflateInflateLimitRequestBody requires a non-negative integer.";
355 }
356
357 return NULL;
358}
359
361 void *dirconf,
362 const char *arg)
363{
365 int i;
366
367 i = atoi(arg);
368 if (i <= 0)
369 return "DeflateInflateRatioLimit must be positive";
370
371 dc->ratio_limit = i;
372
373 return NULL;
374}
375
377 void *dirconf,
378 const char *arg)
379{
381 int i;
382
383 i = atoi(arg);
384 if (i <= 0)
385 return "DeflateInflateRatioBurst must be positive";
386
387 dc->ratio_burst = i;
388
389 return NULL;
390}
391
411
412/* Number of validation bytes (CRC and length) after the compressed data */
413#define VALIDATION_SIZE 8
414/* Do not update ctx->crc, see comment in flush_libz_buffer */
415#define NO_UPDATE_CRC 0
416/* Do update ctx->crc, see comment in flush_libz_buffer */
417#define UPDATE_CRC 1
418
420 int len, int crc, apr_bucket_brigade *bb)
421{
422 apr_bucket *b;
423
424 /*
425 * Do we need to update ctx->crc? Usually this is the case for
426 * inflate action where we need to do a crc on the output, whereas
427 * in the deflate case we need to do a crc on the input
428 */
429 if (crc) {
430 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
431 }
432
433 b = apr_bucket_heap_create((char *)ctx->buffer, len, NULL,
434 bb->bucket_alloc);
436
437 ctx->stream.next_out = ctx->buffer;
438 ctx->stream.avail_out = c->bufferSize;
439}
440
442 int (*libz_func)(z_streamp, int), int flush,
443 int crc)
444{
445 int zRC = Z_OK;
446 int done = 0;
447 int deflate_len;
448
449 for (;;) {
450 deflate_len = c->bufferSize - ctx->stream.avail_out;
451 if (deflate_len > 0) {
452 consume_buffer(ctx, c, deflate_len, crc, ctx->bb);
453 }
454
455 if (done)
456 break;
457
458 zRC = libz_func(&ctx->stream, flush);
459
460 /*
461 * We can ignore Z_BUF_ERROR because:
462 * When we call libz_func we can assume that
463 *
464 * - avail_in is zero (due to the surrounding code that calls
465 * flush_libz_buffer)
466 * - avail_out is non zero due to our actions some lines above
467 *
468 * So the only reason for Z_BUF_ERROR is that the internal libz
469 * buffers are now empty and thus we called libz_func one time
470 * too often. This does not hurt. It simply says that we are done.
471 */
472 if (zRC == Z_BUF_ERROR) {
473 zRC = Z_OK;
474 break;
475 }
476
477 done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
478
479 if (zRC != Z_OK && zRC != Z_STREAM_END)
480 break;
481 }
482 return zRC;
483}
484
486{
488
489 if (ctx)
490 ctx->libz_end_func(&ctx->stream);
491 return APR_SUCCESS;
492}
493
494/* ETag must be unique among the possible representations, so a change
495 * to content-encoding requires a corresponding change to the ETag.
496 * This routine appends -transform (e.g., -gzip) to the entity-tag
497 * value inside the double-quotes if an ETag has already been set
498 * and its value already contains double-quotes. PR 39727
499 */
500static void deflate_check_etag(request_rec *r, const char *transform, int etag_opt)
501{
502 const char *etag = apr_table_get(r->headers_out, "ETag");
504
505 if (etag_opt == AP_DEFLATE_ETAG_REMOVE) {
506 apr_table_unset(r->headers_out, "ETag");
507 return;
508 }
509
510 if ((etag && ((etaglen = strlen(etag)) > 2))) {
511 if (etag[etaglen - 1] == '"') {
513 char *newtag = apr_palloc(r->pool, etaglen + transformlen + 2);
514 char *d = newtag;
515 char *e = d + etaglen - 1;
516 const char *s = etag;
517
518 for (; d < e; ++d, ++s) {
519 *d = *s; /* copy etag to newtag up to last quote */
520 }
521 *d++ = '-'; /* append dash to newtag */
522 s = transform;
523 e = d + transformlen;
524 for (; d < e; ++d, ++s) {
525 *d = *s; /* copy transform to newtag */
526 }
527 *d++ = '"'; /* append quote to newtag */
528 *d = '\0'; /* null terminate newtag */
529
531 }
532 }
533}
534
535/* Check whether the (inflate) ratio exceeds the configured limit/burst. */
537 const deflate_dirconf_t *dc)
538{
539 if (ctx->stream.total_in) {
540 int ratio = ctx->stream.total_out / ctx->stream.total_in;
541 if (ratio < dc->ratio_limit) {
542 ctx->ratio_hits = 0;
543 }
544 else if (++ctx->ratio_hits > dc->ratio_burst) {
545 return 0;
546 }
547 }
548 return 1;
549}
550
552{
553 const char *comp;
555 "SSL_COMPRESS_METHOD");
556 if (comp == NULL || *comp == '\0' || strcmp(comp, "NULL") == 0)
557 return 0;
558 return 1;
559}
560
563{
564 apr_bucket *e;
565 request_rec *r = f->r;
566 deflate_ctx *ctx = f->ctx;
567 int zRC;
568 apr_status_t rv;
569 apr_size_t len = 0, blen;
570 const char *data;
572
573 /* Do nothing if asked to filter nothing. */
574 if (APR_BRIGADE_EMPTY(bb)) {
575 return APR_SUCCESS;
576 }
577
579 &deflate_module);
580
581 /* If we don't have a context, we need to ensure that it is okay to send
582 * the deflated content. If we have a context, that means we've done
583 * this before and we liked it.
584 * This could be not so nice if we always fail. But, if we succeed,
585 * we're in better shape.
586 */
587 if (!ctx) {
588 char *token;
589 const char *encoding;
590
591 if (have_ssl_compression(r)) {
593 "Compression enabled at SSL level; not compressing "
594 "at HTTP level.");
596 return ap_pass_brigade(f->next, bb);
597 }
598
599 /* We have checked above that bb is not empty */
600 e = APR_BRIGADE_LAST(bb);
601 if (APR_BUCKET_IS_EOS(e)) {
602 /*
603 * If we already know the size of the response, we can skip
604 * compression on responses smaller than the compression overhead.
605 * However, if we compress, we must initialize deflate_out before
606 * calling ap_pass_brigade() for the first time. Otherwise the
607 * headers will be sent to the client without
608 * "Content-Encoding: gzip".
609 */
610 e = APR_BRIGADE_FIRST(bb);
611 while (1) {
613 if (APR_BUCKET_IS_EOS(e)) {
615 "Not compressing very small response of %"
616 APR_SIZE_T_FMT " bytes", len);
618 return ap_pass_brigade(f->next, bb);
619 }
622 continue;
623 }
624
625 if (e->length == (apr_size_t)-1) {
627 if (rc != APR_SUCCESS)
628 return rc;
629 }
630 else {
631 blen = e->length;
632 }
633 len += blen;
634 /* 50 is for Content-Encoding and Vary headers and ETag suffix */
635 if (len > sizeof(gzip_header) + VALIDATION_SIZE + 50)
636 break;
637
639 }
640 }
641
642 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
643
644 /*
645 * Only work on main request, not subrequests,
646 * that are not a 204 response with no content
647 * and are not tagged with the no-gzip env variable
648 * and not a partial response to a Range request.
649 *
650 * Note that responding to 304 is handled separately to
651 * set the required headers (such as ETag) per RFC7232, 4.1.
652 */
653 if ((r->main != NULL) || (r->status == HTTP_NO_CONTENT) ||
654 apr_table_get(r->subprocess_env, "no-gzip") ||
655 apr_table_get(r->headers_out, "Content-Range")
656 ) {
658 const char *reason =
659 (r->main != NULL) ? "subrequest" :
660 (r->status == HTTP_NO_CONTENT) ? "no content" :
661 apr_table_get(r->subprocess_env, "no-gzip") ? "no-gzip" :
662 "content-range";
664 "Not compressing (%s)", reason);
665 }
667 return ap_pass_brigade(f->next, bb);
668 }
669
670 /* Some browsers might have problems with content types
671 * other than text/html, so set gzip-only-text/html
672 * (with browsermatch) for them
673 */
674 if (r->content_type == NULL
675 || strncmp(r->content_type, "text/html", 9)) {
677 "gzip-only-text/html");
678 if ( env_value && (strcmp(env_value,"1") == 0) ) {
680 "Not compressing, (gzip-only-text/html)");
682 return ap_pass_brigade(f->next, bb);
683 }
684 }
685
686 /* Let's see what our current Content-Encoding is.
687 * If it's already encoded, don't compress again.
688 * (We could, but let's not.)
689 */
690 encoding = apr_table_get(r->headers_out, "Content-Encoding");
691 if (encoding) {
692 const char *err_enc;
693
694 err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
695 if (err_enc) {
697 }
698 }
699 else {
700 encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
701 }
702
703 if (r->content_encoding) {
707 }
708
709 if (encoding) {
710 const char *tmp = encoding;
711
712 token = ap_get_token(r->pool, &tmp, 0);
713 while (token && *token) {
714 /* stolen from mod_negotiation: */
715 if (strcmp(token, "identity") && strcmp(token, "7bit") &&
716 strcmp(token, "8bit") && strcmp(token, "binary")) {
718 "Not compressing (content-encoding already "
719 " set: %s)", token);
721 return ap_pass_brigade(f->next, bb);
722 }
723
724 /* Otherwise, skip token */
725 if (*tmp) {
726 ++tmp;
727 }
728 token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
729 }
730 }
731
732 /* Even if we don't accept this request based on it not having
733 * the Accept-Encoding, we need to note that we were looking
734 * for this header and downstream proxies should be aware of that.
735 */
736 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
737
738 /* force-gzip will just force it out regardless if the browser
739 * can actually do anything with it.
740 */
741 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
742 const char *accepts;
743 const char *q = NULL;
744
745 /* if they don't have the line, then they can't play */
746 accepts = apr_table_get(r->headers_in, "Accept-Encoding");
747 if (accepts == NULL) {
749 return ap_pass_brigade(f->next, bb);
750 }
751
752 token = ap_get_token(r->pool, &accepts, 0);
753 while (token && token[0] && ap_cstr_casecmp(token, "gzip")) {
754 /* skip parameters, XXX: ;q=foo evaluation? */
755 while (*accepts == ';') {
756 ++accepts;
757 ap_get_token(r->pool, &accepts, 1);
758 }
759
760 /* retrieve next token */
761 if (*accepts == ',') {
762 ++accepts;
763 }
764 token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
765 }
766
767 /* Find the qvalue, if provided */
768 if (*accepts) {
769 while (*accepts == ';') {
770 ++accepts;
771 }
772 q = ap_get_token(r->pool, &accepts, 1);
774 "token: '%s' - q: '%s'", token ? token : "NULL", q);
775 }
776
777 /* No acceptable token found or q=0 */
778 if (!token || token[0] == '\0' ||
779 (q && strlen(q) >= 3 && strncmp("q=0.000", q, strlen(q)) == 0)) {
781 "Not compressing (no Accept-Encoding: gzip or q=0)");
783 return ap_pass_brigade(f->next, bb);
784 }
785 }
786 else {
788 "Forcing compression (force-gzip set)");
789 }
790
791 /* At this point we have decided to filter the content. Let's try to
792 * to initialize zlib (except for 304 responses, where we will only
793 * send out the headers).
794 */
795
796 if (r->status != HTTP_NOT_MODIFIED) {
797 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
798 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
799 ctx->libz_end_func = deflateEnd;
800
801 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
802 c->windowSize, c->memlevel,
804
805 if (zRC != Z_OK) {
806 deflateEnd(&ctx->stream);
808 "unable to init Zlib: "
809 "deflateInit2 returned %d: URL %s",
810 zRC, r->uri);
811 /*
812 * Remove ourselves as it does not make sense to return:
813 * We are not able to init libz and pass data down the chain
814 * uncompressed.
815 */
817 return ap_pass_brigade(f->next, bb);
818 }
819 /*
820 * Register a cleanup function to ensure that we cleanup the internal
821 * libz resources.
822 */
825
826 /* Set the filter init flag so subsequent invocations know we are
827 * active.
828 */
829 ctx->filter_init = 1;
830 }
831
832 /*
833 * Zlib initialization worked, so we can now change the important
834 * content metadata before sending the response out.
835 */
836
837 /* If the entire Content-Encoding is "identity", we can replace it. */
838 if (!encoding || !ap_cstr_casecmp(encoding, "identity")) {
839 apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
840 }
841 else {
842 apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
843 }
844 /* Fix r->content_encoding if it was set before */
845 if (r->content_encoding) {
847 "Content-Encoding");
848 }
849 apr_table_unset(r->headers_out, "Content-Length");
850 apr_table_unset(r->headers_out, "Content-MD5");
851 if (c->etag_opt != AP_DEFLATE_ETAG_NOCHANGE) {
852 deflate_check_etag(r, "gzip", c->etag_opt);
853 }
854
855 /* For a 304 response, only change the headers */
856 if (r->status == HTTP_NOT_MODIFIED) {
858 return ap_pass_brigade(f->next, bb);
859 }
860
861 /* add immortal gzip header */
863 f->c->bucket_alloc);
865
866 /* initialize deflate output buffer */
867 ctx->stream.next_out = ctx->buffer;
868 ctx->stream.avail_out = c->bufferSize;
869 } else if (!ctx->filter_init) {
870 /* Hmm. We've run through the filter init before as we have a ctx,
871 * but we never initialized. We probably have a dangling ref. Bail.
872 */
873 return ap_pass_brigade(f->next, bb);
874 }
875
876 while (!APR_BRIGADE_EMPTY(bb))
877 {
878 apr_bucket *b;
879
880 /*
881 * Optimization: If we are a HEAD request and bytes_sent is not zero
882 * it means that we have passed the content-length filter once and
883 * have more data to sent. This means that the content-length filter
884 * could not determine our content-length for the response to the
885 * HEAD request anyway (the associated GET request would deliver the
886 * body in chunked encoding) and we can stop compressing.
887 */
888 if (r->header_only && r->bytes_sent) {
890 return ap_pass_brigade(f->next, bb);
891 }
892
893 e = APR_BRIGADE_FIRST(bb);
894
895 if (APR_BUCKET_IS_EOS(e)) {
896 char *buf;
897
898 ctx->stream.avail_in = 0; /* should be zero already anyway */
899 /* flush the remaining data from the zlib buffers */
901
903 putLong((unsigned char *)&buf[0], ctx->crc);
904 putLong((unsigned char *)&buf[4], ctx->stream.total_in);
905
907 f->c->bucket_alloc);
910 "Zlib: Compressed %" APR_UINT64_T_FMT
911 " to %" APR_UINT64_T_FMT " : URL %s",
912 (apr_uint64_t)ctx->stream.total_in,
913 (apr_uint64_t)ctx->stream.total_out, r->uri);
914
915 /* leave notes for logging */
916 if (c->note_input_name) {
917 apr_table_setn(r->notes, c->note_input_name,
918 (ctx->stream.total_in > 0)
920 ctx->stream.total_in)
921 : "-");
922 }
923
924 if (c->note_output_name) {
925 apr_table_setn(r->notes, c->note_output_name,
926 (ctx->stream.total_out > 0)
928 ctx->stream.total_out)
929 : "-");
930 }
931
932 if (c->note_ratio_name) {
933 apr_table_setn(r->notes, c->note_ratio_name,
934 (ctx->stream.total_in > 0)
935 ? apr_itoa(r->pool,
936 (int)(ctx->stream.total_out
937 * 100
938 / ctx->stream.total_in))
939 : "-");
940 }
941
942 deflateEnd(&ctx->stream);
943
944 /* We've ended the libz stream, so remove ourselves. */
946
947 /* No need for cleanup any longer */
949
950 /* Remove EOS from the old list, and insert into the new. */
953
954 /* Okay, we've seen the EOS.
955 * Time to pass it along down the chain.
956 */
957 rv = ap_pass_brigade(f->next, ctx->bb);
959 return rv;
960 }
961
962 if (APR_BUCKET_IS_FLUSH(e)) {
963 /* flush the remaining data from the zlib buffers */
966 if (zRC != Z_OK) {
968 "Zlib error %d flushing zlib output buffer (%s)",
969 zRC, ctx->stream.msg);
970 return APR_EGENERAL;
971 }
972
973 /* Remove flush bucket from old brigade anf insert into the new. */
976 rv = ap_pass_brigade(f->next, ctx->bb);
978 if (rv != APR_SUCCESS) {
979 return rv;
980 }
981 continue;
982 }
983
985 /*
986 * Remove meta data bucket from old brigade and insert into the
987 * new.
988 */
991 continue;
992 }
993
994 /* read */
996 if (rv) {
998 "failed reading from %s bucket", e->type->name);
999 return rv;
1000 }
1001 if (!len) {
1003 continue;
1004 }
1005 if (len > APR_INT32_MAX) {
1008 }
1009
1010 /* This crc32 function is from zlib. */
1011 ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
1012
1013 /* write */
1014 ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
1015 * but we'll just have to
1016 * trust zlib */
1017 ctx->stream.avail_in = (int)len;
1018
1019 while (ctx->stream.avail_in != 0) {
1020 if (ctx->stream.avail_out == 0) {
1021 consume_buffer(ctx, c, c->bufferSize, NO_UPDATE_CRC, ctx->bb);
1022
1023 /* Send what we have right now to the next filter. */
1024 rv = ap_pass_brigade(f->next, ctx->bb);
1026 if (rv != APR_SUCCESS) {
1027 return rv;
1028 }
1029 }
1030
1031 zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
1032
1033 if (zRC != Z_OK) {
1035 "Zlib error %d deflating data (%s)", zRC,
1036 ctx->stream.msg);
1037 return APR_EGENERAL;
1038 }
1039 }
1040
1042 }
1043
1044 return APR_SUCCESS;
1045}
1046
1048 const char **data, apr_size_t *len)
1049{
1050 if ((ctx->zlib_flags & EXTRA_FIELD)) {
1051 /* Consume 2 bytes length prefixed data. */
1052 if (ctx->consume_pos == 0) {
1053 if (!*len) {
1054 return APR_INCOMPLETE;
1055 }
1056 ctx->consume_len = (unsigned int)**data;
1057 ctx->consume_pos++;
1058 ++*data;
1059 --*len;
1060 }
1061 if (ctx->consume_pos == 1) {
1062 if (!*len) {
1063 return APR_INCOMPLETE;
1064 }
1065 ctx->consume_len += ((unsigned int)**data) << 8;
1066 ctx->consume_pos++;
1067 ++*data;
1068 --*len;
1069 }
1070 if (*len < ctx->consume_len) {
1071 ctx->consume_len -= *len;
1072 *len = 0;
1073 return APR_INCOMPLETE;
1074 }
1075 *data += ctx->consume_len;
1076 *len -= ctx->consume_len;
1077
1078 ctx->consume_len = ctx->consume_pos = 0;
1079 ctx->zlib_flags &= ~EXTRA_FIELD;
1080 }
1081
1082 if ((ctx->zlib_flags & ORIG_NAME)) {
1083 /* Consume nul terminated string. */
1084 while (*len && **data) {
1085 ++*data;
1086 --*len;
1087 }
1088 if (!*len) {
1089 return APR_INCOMPLETE;
1090 }
1091 /* .. and nul. */
1092 ++*data;
1093 --*len;
1094
1095 ctx->zlib_flags &= ~ORIG_NAME;
1096 }
1097
1098 if ((ctx->zlib_flags & COMMENT)) {
1099 /* Consume nul terminated string. */
1100 while (*len && **data) {
1101 ++*data;
1102 --*len;
1103 }
1104 if (!*len) {
1105 return APR_INCOMPLETE;
1106 }
1107 /* .. and nul. */
1108 ++*data;
1109 --*len;
1110
1111 ctx->zlib_flags &= ~COMMENT;
1112 }
1113
1114 if ((ctx->zlib_flags & HEAD_CRC)) {
1115 /* Consume CRC16 (2 octets). */
1116 if (ctx->consume_pos == 0) {
1117 if (!*len) {
1118 return APR_INCOMPLETE;
1119 }
1120 ctx->consume_pos++;
1121 ++*data;
1122 --*len;
1123 }
1124 if (!*len) {
1125 return APR_INCOMPLETE;
1126 }
1127 ++*data;
1128 --*len;
1129
1130 ctx->consume_pos = 0;
1131 ctx->zlib_flags &= ~HEAD_CRC;
1132 }
1133
1134 return APR_SUCCESS;
1135}
1136
1137/* This is the deflate input filter (inflates). */
1143{
1144 apr_bucket *bkt;
1145 request_rec *r = f->r;
1146 deflate_ctx *ctx = f->ctx;
1147 int zRC;
1148 apr_status_t rv;
1151 apr_off_t inflate_limit;
1152
1153 /* just get out of the way of things we don't want. */
1154 if (mode != AP_MODE_READBYTES) {
1155 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1156 }
1157
1158 c = ap_get_module_config(r->server->module_config, &deflate_module);
1159 dc = ap_get_module_config(r->per_dir_config, &deflate_module);
1160
1161 if (!ctx || ctx->header_len < sizeof(ctx->header)) {
1163
1164 if (!ctx) {
1165 /* only work on main request/no subrequests */
1166 if (!ap_is_initial_req(r)) {
1168 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1169 }
1170
1171 /* We can't operate on Content-Ranges */
1172 if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
1174 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1175 }
1176
1177 /* Check whether request body is gzipped.
1178 *
1179 * If it is, we're transforming the contents, invalidating
1180 * some request headers including Content-Encoding.
1181 *
1182 * If not, we just remove ourself.
1183 */
1184 if (check_gzip(r, r->headers_in, NULL) == 0) {
1186 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1187 }
1188
1189 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1190 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1191 ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1192 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1193 }
1194
1195 do {
1197
1198 len = sizeof(ctx->header) - ctx->header_len;
1199 rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block,
1200 len);
1201
1202 /* ap_get_brigade may return success with an empty brigade for
1203 * a non-blocking read which would block (an empty brigade for
1204 * a blocking read is an issue which is simply forwarded here).
1205 */
1206 if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(ctx->bb)) {
1207 return rv;
1208 }
1209
1210 /* zero length body? step aside */
1211 bkt = APR_BRIGADE_FIRST(ctx->bb);
1212 if (APR_BUCKET_IS_EOS(bkt)) {
1213 if (ctx->header_len) {
1214 /* If the header was (partially) read it's an error, this
1215 * is not a gzip Content-Encoding, as claimed.
1216 */
1218 "Encountered premature end-of-stream while "
1219 "reading inflate header");
1220 return APR_EGENERAL;
1221 }
1225 return APR_SUCCESS;
1226 }
1227
1228 rv = apr_brigade_flatten(ctx->bb,
1229 ctx->header + ctx->header_len, &len);
1230 if (rv != APR_SUCCESS) {
1231 return rv;
1232 }
1233 if (len && !ctx->header_len) {
1234 apr_table_unset(r->headers_in, "Content-Length");
1235 apr_table_unset(r->headers_in, "Content-MD5");
1236 }
1237 ctx->header_len += len;
1238
1239 } while (ctx->header_len < sizeof(ctx->header));
1240
1241 /* We didn't get the magic bytes. */
1242 if (ctx->header[0] != deflate_magic[0] ||
1243 ctx->header[1] != deflate_magic[1]) {
1245 "Zlib: Invalid header");
1246 return APR_EGENERAL;
1247 }
1248
1249 ctx->zlib_flags = ctx->header[3];
1250 if ((ctx->zlib_flags & RESERVED)) {
1252 "Zlib: Invalid flags %02x", ctx->zlib_flags);
1253 return APR_EGENERAL;
1254 }
1255
1256 zRC = inflateInit2(&ctx->stream, c->windowSize);
1257
1258 if (zRC != Z_OK) {
1259 f->ctx = NULL;
1260 inflateEnd(&ctx->stream);
1262 "unable to init Zlib: "
1263 "inflateInit2 returned %d: URL %s",
1264 zRC, r->uri);
1266 return ap_get_brigade(f->next, bb, mode, block, readbytes);
1267 }
1268
1269 /* initialize deflate output buffer */
1270 ctx->stream.next_out = ctx->buffer;
1271 ctx->stream.avail_out = c->bufferSize;
1272
1274 }
1275
1276 inflate_limit = dc->inflate_limit;
1277 if (inflate_limit == 0) {
1278 /* The core is checking the deflated body, we'll check the inflated */
1279 inflate_limit = ap_get_limit_req_body(f->r);
1280 }
1281
1282 if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1283 rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
1284
1285 /* Don't terminate on EAGAIN (or success with an empty brigade in
1286 * non-blocking mode), just return focus.
1287 */
1289 && (APR_STATUS_IS_EAGAIN(rv)
1290 || (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(ctx->bb)))) {
1291 return rv;
1292 }
1293 if (rv != APR_SUCCESS) {
1294 inflateEnd(&ctx->stream);
1295 return rv;
1296 }
1297
1298 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
1301 {
1302 const char *data;
1304
1305 if (APR_BUCKET_IS_EOS(bkt)) {
1306 if (!ctx->done) {
1307 inflateEnd(&ctx->stream);
1309 "Encountered premature end-of-stream while inflating");
1310 return APR_EGENERAL;
1311 }
1312
1313 /* Move everything to the returning brigade. */
1315 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, bkt);
1316 break;
1317 }
1318
1319 if (APR_BUCKET_IS_FLUSH(bkt)) {
1321
1322 if (!ctx->done) {
1323 ctx->inflate_total += ctx->stream.avail_out;
1324 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
1325 ctx->inflate_total -= ctx->stream.avail_out;
1326 if (zRC != Z_OK) {
1327 inflateEnd(&ctx->stream);
1329 "Zlib error %d inflating data (%s)", zRC,
1330 ctx->stream.msg);
1331 return APR_EGENERAL;
1332 }
1333
1334 if (inflate_limit && ctx->inflate_total > inflate_limit) {
1335 inflateEnd(&ctx->stream);
1337 "Inflated content length of %" APR_OFF_T_FMT
1338 " is larger than the configured limit"
1339 " of %" APR_OFF_T_FMT,
1340 ctx->inflate_total, inflate_limit);
1341 return APR_ENOSPC;
1342 }
1343
1344 if (!check_ratio(r, ctx, dc)) {
1345 inflateEnd(&ctx->stream);
1347 "Inflated content ratio is larger than the "
1348 "configured limit %i by %i time(s)",
1349 dc->ratio_limit, dc->ratio_burst);
1350 return APR_EINVAL;
1351 }
1352
1353 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1354 UPDATE_CRC, ctx->proc_bb);
1355 }
1356
1357 /* Flush everything so far in the returning brigade, but continue
1358 * reading should EOS/more follow (don't lose them).
1359 */
1362 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, bkt);
1363 bkt = tmp_b;
1364 continue;
1365 }
1366
1367 /* sanity check - data after completed compressed body and before eos? */
1368 if (ctx->done) {
1370 "Encountered extra data after compressed data");
1371 return APR_EGENERAL;
1372 }
1373
1374 /* read */
1376 if (!len) {
1377 continue;
1378 }
1379 if (len > APR_INT32_MAX) {
1382 }
1383
1384 if (ctx->zlib_flags) {
1385 rv = consume_zlib_flags(ctx, &data, &len);
1386 if (rv == APR_SUCCESS) {
1387 ctx->zlib_flags = 0;
1388 }
1389 if (!len) {
1390 continue;
1391 }
1392 }
1393
1394 /* pass through zlib inflate. */
1395 ctx->stream.next_in = (unsigned char *)data;
1396 ctx->stream.avail_in = (int)len;
1397
1398 if (!ctx->validation_buffer) {
1399 while (ctx->stream.avail_in != 0) {
1400 if (ctx->stream.avail_out == 0) {
1401 consume_buffer(ctx, c, c->bufferSize, UPDATE_CRC,
1402 ctx->proc_bb);
1403 }
1404
1405 ctx->inflate_total += ctx->stream.avail_out;
1406 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1407 ctx->inflate_total -= ctx->stream.avail_out;
1408 if (zRC != Z_OK && zRC != Z_STREAM_END) {
1409 inflateEnd(&ctx->stream);
1411 "Zlib error %d inflating data (%s)", zRC,
1412 ctx->stream.msg);
1413 return APR_EGENERAL;
1414 }
1415
1416 if (inflate_limit && ctx->inflate_total > inflate_limit) {
1417 inflateEnd(&ctx->stream);
1419 "Inflated content length of %" APR_OFF_T_FMT
1420 " is larger than the configured limit"
1421 " of %" APR_OFF_T_FMT,
1422 ctx->inflate_total, inflate_limit);
1423 return APR_ENOSPC;
1424 }
1425
1426 if (!check_ratio(r, ctx, dc)) {
1427 inflateEnd(&ctx->stream);
1429 "Inflated content ratio is larger than the "
1430 "configured limit %i by %i time(s)",
1431 dc->ratio_limit, dc->ratio_burst);
1432 return APR_EINVAL;
1433 }
1434
1435 if (zRC == Z_STREAM_END) {
1436 ctx->validation_buffer = apr_pcalloc(r->pool,
1438 ctx->validation_buffer_length = 0;
1439 break;
1440 }
1441 }
1442 }
1443
1444 if (ctx->validation_buffer) {
1445 apr_size_t avail, valid;
1446 unsigned char *buf = ctx->validation_buffer;
1447
1448 avail = ctx->stream.avail_in;
1449 valid = (apr_size_t)VALIDATION_SIZE -
1450 ctx->validation_buffer_length;
1451
1452 /*
1453 * We have inflated all data. Now try to capture the
1454 * validation bytes. We may not have them all available
1455 * right now, but capture what is there.
1456 */
1457 if (avail < valid) {
1458 memcpy(buf + ctx->validation_buffer_length,
1459 ctx->stream.next_in, avail);
1460 ctx->validation_buffer_length += avail;
1461 continue;
1462 }
1463 memcpy(buf + ctx->validation_buffer_length,
1464 ctx->stream.next_in, valid);
1465 ctx->validation_buffer_length += valid;
1466
1468 "Zlib: Inflated %" APR_UINT64_T_FMT
1469 " to %" APR_UINT64_T_FMT " : URL %s",
1470 (apr_uint64_t)ctx->stream.total_in,
1471 (apr_uint64_t)ctx->stream.total_out, r->uri);
1472
1473 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1474 UPDATE_CRC, ctx->proc_bb);
1475
1476 {
1477 unsigned long compCRC, compLen;
1478 compCRC = getLong(buf);
1479 if (ctx->crc != compCRC) {
1480 inflateEnd(&ctx->stream);
1482 "Zlib: CRC error inflating data");
1483 return APR_EGENERAL;
1484 }
1486 /* gzip stores original size only as 4 byte value */
1487 if ((ctx->stream.total_out & 0xFFFFFFFF) != compLen) {
1488 inflateEnd(&ctx->stream);
1490 "Zlib: Length %" APR_UINT64_T_FMT
1491 " of inflated data does not match"
1492 " expected value %ld",
1493 (apr_uint64_t)ctx->stream.total_out, compLen);
1494 return APR_EGENERAL;
1495 }
1496 }
1497
1498 inflateEnd(&ctx->stream);
1499
1500 ctx->done = 1;
1501
1502 /* Did we have trailing data behind the closing 8 bytes? */
1503 if (avail > valid) {
1505 "Encountered extra data after compressed data");
1506 return APR_EGENERAL;
1507 }
1508 }
1509
1510 }
1512 }
1513
1514 /* If we are about to return nothing for a 'blocking' read and we have
1515 * some data in our zlib buffer, flush it out so we can return something.
1516 */
1517 if (block == APR_BLOCK_READ &&
1518 APR_BRIGADE_EMPTY(ctx->proc_bb) &&
1519 ctx->stream.avail_out < c->bufferSize) {
1520 consume_buffer(ctx, c, c->bufferSize - ctx->stream.avail_out,
1521 UPDATE_CRC, ctx->proc_bb);
1522 }
1523
1524 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1526 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1527 }
1528 else {
1529 APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1530 apr_brigade_split_ex(bb, bkt, ctx->proc_bb);
1531 }
1534 }
1535 }
1536
1537 return APR_SUCCESS;
1538}
1539
1540
1541/* Filter to inflate for a content-transforming proxy. */
1544{
1545 apr_bucket *e;
1546 request_rec *r = f->r;
1547 deflate_ctx *ctx = f->ctx;
1548 int zRC;
1549 apr_status_t rv;
1552
1553 /* Do nothing if asked to filter nothing. */
1554 if (APR_BRIGADE_EMPTY(bb)) {
1555 return APR_SUCCESS;
1556 }
1557
1558 c = ap_get_module_config(r->server->module_config, &deflate_module);
1559 dc = ap_get_module_config(r->per_dir_config, &deflate_module);
1560
1561 if (!ctx) {
1562
1563 /*
1564 * Only work on main request, not subrequests,
1565 * that are not a 204 response with no content
1566 * and not a partial response to a Range request,
1567 * and only when Content-Encoding ends in gzip.
1568 *
1569 * Note that responding to 304 is handled separately to
1570 * set the required headers (such as ETag) per RFC7232, 4.1.
1571 */
1572 if (!ap_is_initial_req(r) || (r->status == HTTP_NO_CONTENT) ||
1573 (apr_table_get(r->headers_out, "Content-Range") != NULL) ||
1575 ) {
1577 return ap_pass_brigade(f->next, bb);
1578 }
1579
1580 /*
1581 * At this point we have decided to filter the content, so change
1582 * important content metadata before sending any response out.
1583 * Content-Encoding was already reset by the check_gzip() call.
1584 */
1585 apr_table_unset(r->headers_out, "Content-Length");
1586 apr_table_unset(r->headers_out, "Content-MD5");
1587 if (c->etag_opt != AP_DEFLATE_ETAG_NOCHANGE) {
1588 deflate_check_etag(r, "gunzip", c->etag_opt);
1589 }
1590
1591 /* For a 304 response, only change the headers */
1592 if (r->status == HTTP_NOT_MODIFIED) {
1594 return ap_pass_brigade(f->next, bb);
1595 }
1596
1597 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1598 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1599 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1600 ctx->libz_end_func = inflateEnd;
1601 ctx->validation_buffer = NULL;
1602 ctx->validation_buffer_length = 0;
1603
1604 zRC = inflateInit2(&ctx->stream, c->windowSize);
1605
1606 if (zRC != Z_OK) {
1607 f->ctx = NULL;
1608 inflateEnd(&ctx->stream);
1610 "unable to init Zlib: "
1611 "inflateInit2 returned %d: URL %s",
1612 zRC, r->uri);
1613 /*
1614 * Remove ourselves as it does not make sense to return:
1615 * We are not able to init libz and pass data down the chain
1616 * compressed.
1617 */
1619 return ap_pass_brigade(f->next, bb);
1620 }
1621
1622 /*
1623 * Register a cleanup function to ensure that we cleanup the internal
1624 * libz resources.
1625 */
1628
1629 /* initialize inflate output buffer */
1630 ctx->stream.next_out = ctx->buffer;
1631 ctx->stream.avail_out = c->bufferSize;
1632 }
1633
1634 while (!APR_BRIGADE_EMPTY(bb))
1635 {
1636 const char *data;
1638
1639 e = APR_BRIGADE_FIRST(bb);
1640
1641 if (APR_BUCKET_IS_EOS(e)) {
1642 /*
1643 * We are really done now. Ensure that we never return here, even
1644 * if a second EOS bucket falls down the chain. Thus remove
1645 * ourselves.
1646 */
1648 /* should be zero already anyway */
1649 ctx->stream.avail_in = 0;
1650 /*
1651 * Flush the remaining data from the zlib buffers. It is correct
1652 * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1653 * deflate case. In the inflate case Z_FINISH requires to have a
1654 * large enough output buffer to put ALL data in otherwise it
1655 * fails, whereas in the deflate case you can empty a filled output
1656 * buffer and call it again until no more output can be created.
1657 */
1660 "Zlib: Inflated %" APR_UINT64_T_FMT
1661 " to %" APR_UINT64_T_FMT " : URL %s",
1662 (apr_uint64_t)ctx->stream.total_in,
1663 (apr_uint64_t)ctx->stream.total_out, r->uri);
1664
1665 if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1666 unsigned long compCRC, compLen;
1667 compCRC = getLong(ctx->validation_buffer);
1668 if (ctx->crc != compCRC) {
1670 "Zlib: Checksum of inflated stream invalid");
1671 return APR_EGENERAL;
1672 }
1673 ctx->validation_buffer += VALIDATION_SIZE / 2;
1674 compLen = getLong(ctx->validation_buffer);
1675 /* gzip stores original size only as 4 byte value */
1676 if ((ctx->stream.total_out & 0xFFFFFFFF) != compLen) {
1678 "Zlib: Length of inflated stream invalid");
1679 return APR_EGENERAL;
1680 }
1681 }
1682 else {
1684 "Zlib: Validation bytes not present");
1685 return APR_EGENERAL;
1686 }
1687
1688 inflateEnd(&ctx->stream);
1689 /* No need for cleanup any longer */
1691
1692 /* Remove EOS from the old list, and insert into the new. */
1695
1696 /*
1697 * Okay, we've seen the EOS.
1698 * Time to pass it along down the chain.
1699 */
1700 rv = ap_pass_brigade(f->next, ctx->bb);
1702 return rv;
1703 }
1704
1705 if (APR_BUCKET_IS_FLUSH(e)) {
1706 /* flush the remaining data from the zlib buffers */
1708 if (zRC == Z_STREAM_END) {
1709 if (ctx->validation_buffer == NULL) {
1710 ctx->validation_buffer = apr_pcalloc(f->r->pool,
1712 }
1713 }
1714 else if (zRC != Z_OK) {
1716 "Zlib error %d flushing inflate buffer (%s)",
1717 zRC, ctx->stream.msg);
1718 return APR_EGENERAL;
1719 }
1720
1721 /* Remove flush bucket from old brigade anf insert into the new. */
1724 rv = ap_pass_brigade(f->next, ctx->bb);
1726 if (rv != APR_SUCCESS) {
1727 return rv;
1728 }
1729 continue;
1730 }
1731
1733 /*
1734 * Remove meta data bucket from old brigade and insert into the
1735 * new.
1736 */
1739 continue;
1740 }
1741
1742 /* read */
1744 if (!len) {
1746 continue;
1747 }
1748 if (len > APR_INT32_MAX) {
1751 }
1752
1753 /* first bucket contains zlib header */
1754 if (ctx->header_len < sizeof(ctx->header)) {
1756
1757 rem = sizeof(ctx->header) - ctx->header_len;
1758 if (len < rem) {
1759 memcpy(ctx->header + ctx->header_len, data, len);
1760 ctx->header_len += len;
1762 continue;
1763 }
1764 memcpy(ctx->header + ctx->header_len, data, rem);
1765 ctx->header_len += rem;
1766 {
1767 int zlib_method;
1768 zlib_method = ctx->header[2];
1769 if (zlib_method != Z_DEFLATED) {
1771 "inflate: data not deflated!");
1773 return ap_pass_brigade(f->next, bb);
1774 }
1775 if (ctx->header[0] != deflate_magic[0] ||
1776 ctx->header[1] != deflate_magic[1]) {
1778 "inflate: bad header");
1779 return APR_EGENERAL ;
1780 }
1781 ctx->zlib_flags = ctx->header[3];
1782 if ((ctx->zlib_flags & RESERVED)) {
1784 "inflate: bad flags %02x",
1785 ctx->zlib_flags);
1786 return APR_EGENERAL;
1787 }
1788 }
1789 if (len == rem) {
1791 continue;
1792 }
1793 data += rem;
1794 len -= rem;
1795 }
1796
1797 if (ctx->zlib_flags) {
1798 rv = consume_zlib_flags(ctx, &data, &len);
1799 if (rv == APR_SUCCESS) {
1800 ctx->zlib_flags = 0;
1801 }
1802 if (!len) {
1804 continue;
1805 }
1806 }
1807
1808 /* pass through zlib inflate. */
1809 ctx->stream.next_in = (unsigned char *)data;
1810 ctx->stream.avail_in = len;
1811
1812 if (ctx->validation_buffer) {
1813 if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1815
1816 copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1817 if (copy_size > ctx->stream.avail_in)
1818 copy_size = ctx->stream.avail_in;
1819 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1820 ctx->stream.next_in, copy_size);
1821 /* Saved copy_size bytes */
1822 ctx->stream.avail_in -= copy_size;
1823 ctx->validation_buffer_length += copy_size;
1824 }
1825 if (ctx->stream.avail_in) {
1827 "Zlib: %d bytes of garbage at the end of "
1828 "compressed stream.", ctx->stream.avail_in);
1829 /*
1830 * There is nothing worth consuming for zlib left, because it is
1831 * either garbage data or the data has been copied to the
1832 * validation buffer (processing validation data is no business
1833 * for zlib). So set ctx->stream.avail_in to zero to indicate
1834 * this to the following while loop.
1835 */
1836 ctx->stream.avail_in = 0;
1837 }
1838 }
1839
1840 while (ctx->stream.avail_in != 0) {
1841 if (ctx->stream.avail_out == 0) {
1842 consume_buffer(ctx, c, c->bufferSize, UPDATE_CRC, ctx->bb);
1843
1844 /* Send what we have right now to the next filter. */
1845 rv = ap_pass_brigade(f->next, ctx->bb);
1847 if (rv != APR_SUCCESS) {
1848 return rv;
1849 }
1850 }
1851
1852 zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1853
1854 if (zRC != Z_OK && zRC != Z_STREAM_END) {
1856 "Zlib error %d inflating data (%s)", zRC,
1857 ctx->stream.msg);
1858 return APR_EGENERAL;
1859 }
1860
1861 /* Don't check length limits on inflate_out */
1862 if (!check_ratio(r, ctx, dc)) {
1864 "Inflated content ratio is larger than the "
1865 "configured limit %i by %i time(s)",
1866 dc->ratio_limit, dc->ratio_burst);
1867 return APR_EINVAL;
1868 }
1869
1870 if (zRC == Z_STREAM_END) {
1871 /*
1872 * We have inflated all data. Now try to capture the
1873 * validation bytes. We may not have them all available
1874 * right now, but capture what is there.
1875 */
1876 ctx->validation_buffer = apr_pcalloc(f->r->pool,
1878 if (ctx->stream.avail_in > VALIDATION_SIZE) {
1879 ctx->validation_buffer_length = VALIDATION_SIZE;
1881 "Zlib: %d bytes of garbage at the end of "
1882 "compressed stream.",
1883 ctx->stream.avail_in - VALIDATION_SIZE);
1884 }
1885 else if (ctx->stream.avail_in > 0) {
1886 ctx->validation_buffer_length = ctx->stream.avail_in;
1887 }
1888 if (ctx->validation_buffer_length)
1889 memcpy(ctx->validation_buffer, ctx->stream.next_in,
1890 ctx->validation_buffer_length);
1891 break;
1892 }
1893 }
1894
1896 }
1897
1898 return APR_SUCCESS;
1899}
1900
1902 apr_pool_t *ptemp, server_rec *s)
1903{
1904 return OK;
1905}
1906
1907
1908#define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1919
1921 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1922 "Set a note to report on compression ratio"),
1923 AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1924 RSRC_CONF, "Set the Deflate window size (1-15)"),
1925 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1926 "Set the Deflate Buffer Size"),
1927 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1928 "Set the Deflate Memory Level (1-9)"),
1929 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1930 "Set the Deflate Compression Level (1-9)"),
1931 AP_INIT_TAKE1("DeflateInflateLimitRequestBody", deflate_set_inflate_limit, NULL, OR_ALL,
1932 "Set a limit on size of inflated input"),
1933 AP_INIT_TAKE1("DeflateInflateRatioLimit", deflate_set_inflate_ratio_limit, NULL, OR_ALL,
1934 "Set the inflate ratio limit above which inflation is "
1935 "aborted (default: " APR_STRINGIFY(AP_INFLATE_RATIO_LIMIT) ")"),
1936 AP_INIT_TAKE1("DeflateInflateRatioBurst", deflate_set_inflate_ratio_burst, NULL, OR_ALL,
1937 "Set the maximum number of following inflate ratios above limit "
1938 "(default: " APR_STRINGIFY(AP_INFLATE_RATIO_BURST) ")"),
1939 AP_INIT_TAKE1("DeflateAlterEtag", deflate_set_etag, NULL, RSRC_CONF,
1940 "Set how mod_deflate should modify ETAG response headers: 'AddSuffix' (default), 'NoChange' (2.2.x behavior), 'Remove'"),
1941
1942 {NULL}
1943};
1944
1947 create_deflate_dirconf, /* dir config creater */
1948 NULL, /* dir merger --- default is to override */
1949 create_deflate_server_config, /* server config */
1950 NULL, /* merge server config */
1951 deflate_filter_cmds, /* command table */
1952 register_hooks /* register hooks */
1953};
int n
Definition ap_regex.h:278
const char apr_size_t len
Definition ap_regex.h:187
#define APR_STRINGIFY(n)
Definition ap_release.h:62
APR-UTIL Buckets/Bucket Brigades.
APR Miscellaneous library routines.
APR general purpose library routines.
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
APR Standard Headers Support.
return found
Definition core.c:2840
static apr_pool_t * pconf
Definition event.c:441
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
#define ap_get_module_config(v, m)
void ap_hook_post_config(ap_HOOK_post_config_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:105
#define AP_DECLARE_MODULE(foo)
#define AP_INIT_TAKE12(directive, func, mconfig, where, help)
request_rec * r
int flush
#define OK
Definition httpd.h:456
void ap_remove_input_filter(ap_filter_t *f)
apr_status_t ap_pass_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket)
ap_filter_rec_t * ap_register_output_filter(const char *name, ap_out_filter_func filter_func, ap_init_filter_func filter_init, ap_filter_type ftype)
apr_status_t ap_filter_rec_t * ap_register_input_filter(const char *name, ap_in_filter_func filter_func, ap_init_filter_func filter_init, ap_filter_type ftype)
apr_status_t ap_get_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
void ap_remove_output_filter(ap_filter_t *f)
@ AP_FTYPE_CONTENT_SET
@ AP_FTYPE_RESOURCE
apr_off_t ap_get_limit_req_body(const request_rec *r)
Definition core.c:1259
#define APLOGNO(n)
Definition http_log.h:117
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_WARNING
Definition http_log.h:68
#define APLOG_R_IS_LEVEL(r, level)
Definition http_log.h:229
#define APLOG_TRACE1
Definition http_log.h:72
#define APLOG_DEBUG
Definition http_log.h:71
const unsigned char * buf
Definition util_md5.h:50
const char * ap_ssl_var_lookup(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *name)
Definition ssl.c:186
int ap_is_initial_req(request_rec *r)
Definition request.c:2567
void * dummy
Definition http_vhost.h:62
void const char * arg
Definition http_vhost.h:63
#define APR_EGENERAL
Definition apr_errno.h:313
#define APR_ENOSPC
Definition apr_errno.h:676
#define APR_INCOMPLETE
Definition apr_errno.h:452
#define APR_EINVAL
Definition apr_errno.h:711
#define APR_STATUS_IS_EAGAIN(s)
Definition apr_errno.h:1272
apr_file_t * f
#define APR_BUCKET_IS_FLUSH(e)
#define APR_BUCKET_REMOVE(e)
#define APR_BRIGADE_LAST(b)
#define APR_BUCKET_IS_METADATA(e)
#define APR_BRIGADE_INSERT_TAIL(b, e)
#define apr_bucket_split(e, point)
#define APR_BUCKET_NEXT(e)
apr_read_type_e
Definition apr_buckets.h:57
apr_bucket * e
#define APR_BRIGADE_CONCAT(a, b)
#define APR_BRIGADE_EMPTY(b)
#define APR_BRIGADE_SENTINEL(b)
#define apr_bucket_delete(e)
#define APR_BUCKET_IS_EOS(e)
apr_brigade_flush void * ctx
#define APR_BRIGADE_FIRST(b)
#define apr_bucket_read(e, str, len, block)
#define APR_BUCKET_PREV(e)
@ APR_BLOCK_READ
Definition apr_buckets.h:58
@ APR_NONBLOCK_READ
Definition apr_buckets.h:59
apr_dbd_transaction_t int mode
Definition apr_dbd.h:261
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
apr_redis_t * rc
Definition apr_redis.h:173
#define OR_ALL
#define RSRC_CONF
#define HTTP_NOT_MODIFIED
Definition httpd.h:504
#define HTTP_NO_CONTENT
Definition httpd.h:494
#define STANDARD20_MODULE_STUFF
#define ap_strrchr(s, c)
Definition httpd.h:2355
int ap_cstr_casecmp(const char *s1, const char *s2)
Definition util.c:3542
#define ap_strchr_c(s, c)
Definition httpd.h:2353
char * ap_get_token(apr_pool_t *p, const char **accept_line, int accept_white)
Definition util.c:1687
apr_size_t size
#define apr_isspace(c)
Definition apr_lib.h:225
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
int strcasecmp(const char *a, const char *b)
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_pool_t * b
Definition apr_pools.h:529
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
void apr_skiplistnode apr_skiplist_compare comp
const char * s
Definition apr_strings.h:95
apr_cmdtype_e cmd
int reason
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
HTTP protocol handling.
Apache Request library.
SSL protocol handling.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
static const char * deflate_set_inflate_ratio_limit(cmd_parms *cmd, void *dirconf, const char *arg)
struct deflate_ctx_t deflate_ctx
static const char * deflate_set_compressionlevel(cmd_parms *cmd, void *dummy, const char *arg)
static void * create_deflate_server_config(apr_pool_t *p, server_rec *s)
static const char * deflate_set_etag(cmd_parms *cmd, void *dummy, const char *arg)
static void * create_deflate_dirconf(apr_pool_t *p, char *dummy)
static int have_ssl_compression(request_rec *r)
#define COMMENT
Definition mod_deflate.c:33
static const char * deflate_set_memlevel(cmd_parms *cmd, void *dummy, const char *arg)
#define AP_INFLATE_RATIO_BURST
Definition mod_deflate.c:58
#define DEFAULT_MEMLEVEL
static apr_status_t inflate_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
static const char * deflate_set_window_size(cmd_parms *cmd, void *dummy, const char *arg)
static const char * deflate_set_inflate_ratio_burst(cmd_parms *cmd, void *dirconf, const char *arg)
#define AP_INFLATE_RATIO_LIMIT
Definition mod_deflate.c:57
struct deflate_filter_config_t deflate_filter_config
static const command_rec deflate_filter_cmds[]
static void putLong(unsigned char *string, unsigned long x)
static const char * deflate_set_buffer_size(cmd_parms *cmd, void *dummy, const char *arg)
#define EXTRA_FIELD
Definition mod_deflate.c:31
#define AP_DEFLATE_ETAG_NOCHANGE
Definition mod_deflate.c:61
#define AP_DEFLATE_ETAG_ADDSUFFIX
Definition mod_deflate.c:60
#define HEAD_CRC
Definition mod_deflate.c:30
#define RESERVED
Definition mod_deflate.c:34
static const char deflateFilterName[]
Definition mod_deflate.c:54
static void deflate_check_etag(request_rec *r, const char *transform, int etag_opt)
#define DEFAULT_COMPRESSION
Definition mod_deflate.c:98
static unsigned long getLong(unsigned char *string)
static const char deflate_magic[2]
Definition mod_deflate.c:95
#define DEFAULT_BUFFERSIZE
static const char * deflate_set_note(cmd_parms *cmd, void *dummy, const char *arg1, const char *arg2)
static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
static void register_hooks(apr_pool_t *p)
#define DEFAULT_WINDOWSIZE
Definition mod_deflate.c:99
static apr_status_t consume_zlib_flags(deflate_ctx *ctx, const char **data, apr_size_t *len)
#define UPDATE_CRC
static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c, int(*libz_func)(z_streamp, int), int flush, int crc)
static apr_status_t deflate_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
#define ORIG_NAME
Definition mod_deflate.c:32
static apr_status_t deflate_in_filter(ap_filter_t *f, apr_bucket_brigade *bb, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
static int check_ratio(request_rec *r, deflate_ctx *ctx, const deflate_dirconf_t *dc)
static int mod_deflate_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
static const char gzip_header[10]
Definition mod_deflate.c:88
#define VALIDATION_SIZE
#define AP_DEFLATE_ETAG_REMOVE
Definition mod_deflate.c:62
static apr_status_t deflate_ctx_cleanup(void *data)
static const char * deflate_set_inflate_limit(cmd_parms *cmd, void *dirconf, const char *arg)
#define NO_UPDATE_CRC
#define consume_buffer(ctx, inbuf, bytes, flag)
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
The representation of a filter chain.
apr_bucket_alloc_t * bucket_alloc
const char * name
apr_size_t length
const apr_bucket_type_t * type
apr_pool_t * pool
unsigned int filter_init
apr_bucket_brigade * bb
apr_bucket_brigade * proc_bb
int(* libz_end_func)(z_streamp)
unsigned char * validation_buffer
unsigned int consume_pos
apr_size_t header_len
apr_size_t validation_buffer_length
char header[10]
unsigned int consume_len
z_stream stream
unsigned int done
apr_off_t inflate_total
unsigned long crc
unsigned char * buffer
apr_off_t inflate_limit
Definition mod_deflate.c:77
const char * note_input_name
Definition mod_deflate.c:71
const char * note_ratio_name
Definition mod_deflate.c:70
const char * note_output_name
Definition mod_deflate.c:72
A structure that represents the current request.
Definition httpd.h:845
int status
Definition httpd.h:891
apr_off_t bytes_sent
Definition httpd.h:931
char * uri
Definition httpd.h:1016
const char * content_type
Definition httpd.h:992
int header_only
Definition httpd.h:875
apr_table_t * notes
Definition httpd.h:985
apr_pool_t * pool
Definition httpd.h:847
conn_rec * connection
Definition httpd.h:849
apr_table_t * err_headers_out
Definition httpd.h:981
apr_table_t * headers_in
Definition httpd.h:976
request_rec * main
Definition httpd.h:860
apr_table_t * subprocess_env
Definition httpd.h:983
server_rec * server
Definition httpd.h:851
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
const char * content_encoding
Definition httpd.h:997
apr_table_t * headers_out
Definition httpd.h:978
A structure to store information for each virtual server.
Definition httpd.h:1322
struct ap_conf_vector_t * module_config
Definition httpd.h:1341
Apache filter library.
ap_input_mode_t
input filtering modes
Definition util_filter.h:41
@ AP_MODE_READBYTES
Definition util_filter.h:43
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray