/[Apache-SVN]/httpd/httpd/trunk/modules/filters/mod_deflate.c
ViewVC logotype

Contents of /httpd/httpd/trunk/modules/filters/mod_deflate.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 743814 - (hide annotations)
Thu Feb 12 17:43:39 2009 UTC (9 months, 2 weeks ago) by fielding
File MIME type: text/plain
File size: 50297 byte(s)
Adjust content metadata on deflate/inflate response before bailing out
on a 304 response so that the metadata does not differ from 200 response.
1 fielding 420983 /* 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 jerenkrantz 94979 *
8 nd 102525 * http://www.apache.org/licenses/LICENSE-2.0
9 jerenkrantz 94979 *
10 nd 102525 * 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 jerenkrantz 94979 */
16    
17     /*
18 pquerna 161738 * mod_deflate.c: Perform deflate content-encoding on the fly
19 jerenkrantz 94979 *
20 ianh 103405 * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew
21 jerenkrantz 94979 */
22    
23 nd 102525 /*
24 niq 104034 * Portions of this software are based upon zlib code by Jean-loup Gailly
25     * (zlib functions gz_open and gzwrite, check_header)
26 nd 102525 */
27    
28 niq 104034 /* 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 jerenkrantz 94979 #include "httpd.h"
38     #include "http_config.h"
39     #include "http_log.h"
40 niq 560937 #include "apr_lib.h"
41 jerenkrantz 94979 #include "apr_strings.h"
42     #include "apr_general.h"
43     #include "util_filter.h"
44     #include "apr_buckets.h"
45     #include "http_request.h"
46 trawick 95548 #define APR_WANT_STRFUNC
47     #include "apr_want.h"
48 jerenkrantz 94979
49     #include "zlib.h"
50    
51     static const char deflateFilterName[] = "DEFLATE";
52     module AP_MODULE_DECLARE_DATA deflate_module;
53    
54     typedef struct deflate_filter_config_t
55     {
56     int windowSize;
57     int memlevel;
58 ianh 98895 int compressionlevel;
59 wrowe 95676 apr_size_t bufferSize;
60 nd 98154 char *note_ratio_name;
61     char *note_input_name;
62     char *note_output_name;
63 jerenkrantz 94979 } deflate_filter_config;
64    
65 jorton 105410 /* RFC 1952 Section 2.3 defines the gzip header:
66     *
67     * +---+---+---+---+---+---+---+---+---+---+
68     * |ID1|ID2|CM |FLG| MTIME |XFL|OS |
69     * +---+---+---+---+---+---+---+---+---+---+
70     */
71 jim 332306 static const char gzip_header[10] =
72 jorton 105410 { '\037', '\213', Z_DEFLATED, 0,
73     0, 0, 0, 0, /* mtime */
74     0, 0x03 /* Unix OS_CODE */
75     };
76    
77     /* magic header */
78     static const char deflate_magic[2] = { '\037', '\213' };
79    
80 jerenkrantz 94979 /* windowsize is negative to suppress Zlib header */
81 ianh 98895 #define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
82 jerenkrantz 94979 #define DEFAULT_WINDOWSIZE -15
83     #define DEFAULT_MEMLEVEL 9
84 jerenkrantz 94987 #define DEFAULT_BUFFERSIZE 8096
85 jerenkrantz 94979
86 niq 562507
87     /* Check whether a request is gzipped, so we can un-gzip it.
88     * If a request has multiple encodings, we need the gzip
89     * to be the outermost non-identity encoding.
90     */
91 niq 563803 static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
92 niq 562507 {
93     int found = 0;
94 niq 563803 apr_table_t *hdrs = hdrs1;
95 niq 562507 const char *encoding = apr_table_get(hdrs, "Content-Encoding");
96 niq 563317
97     if (!encoding && (hdrs2 != NULL)) {
98 niq 563803 /* the output filter has two tables and a content_encoding to check */
99 niq 563317 encoding = apr_table_get(hdrs2, "Content-Encoding");
100 niq 563803 hdrs = hdrs2;
101     if (!encoding) {
102     encoding = r->content_encoding;
103     hdrs = NULL;
104     }
105 niq 563317 }
106 niq 562507 if (encoding && *encoding) {
107    
108     /* check the usual/simple case first */
109     if (!strcasecmp(encoding, "gzip")
110     || !strcasecmp(encoding, "x-gzip")) {
111     found = 1;
112 niq 563803 if (hdrs) {
113     apr_table_unset(hdrs, "Content-Encoding");
114     }
115     else {
116     r->content_encoding = NULL;
117     }
118 niq 562507 }
119     else if (ap_strchr_c(encoding, ',') != NULL) {
120     /* If the outermost encoding isn't gzip, there's nowt
121     * we can do. So only check the last non-identity token
122     */
123 niq 563803 char *new_encoding = apr_pstrdup(r->pool, encoding);
124 niq 562507 char *ptr;
125     for(;;) {
126     char *token = ap_strrchr(new_encoding, ',');
127     if (!token) { /* gzip:identity or other:identity */
128     if (!strcasecmp(new_encoding, "gzip")
129     || !strcasecmp(new_encoding, "x-gzip")) {
130     found = 1;
131 niq 563803 if (hdrs) {
132     apr_table_unset(hdrs, "Content-Encoding");
133     }
134     else {
135     r->content_encoding = NULL;
136     }
137 niq 562507 }
138     break; /* seen all tokens */
139     }
140     for (ptr=token+1; apr_isspace(*ptr); ++ptr);
141     if (!strcasecmp(ptr, "gzip")
142     || !strcasecmp(ptr, "x-gzip")) {
143     *token = '\0';
144 niq 563803 if (hdrs) {
145     apr_table_setn(hdrs, "Content-Encoding", new_encoding);
146     }
147     else {
148     r->content_encoding = new_encoding;
149     }
150 niq 562507 found = 1;
151     }
152     else if (!ptr[0] || !strcasecmp(ptr, "identity")) {
153     *token = '\0';
154     continue; /* strip the token and find the next one */
155     }
156     break; /* found a non-identity token */
157     }
158     }
159     }
160 rpluem 726794 /*
161     * If we have dealt with the headers above but content_encoding was set
162     * before sync it with the new value in the hdrs table as
163     * r->content_encoding takes precedence later on in the http_header_filter
164     * and hence would destroy what we have just set in the hdrs table.
165     */
166     if (hdrs && r->content_encoding) {
167     r->content_encoding = apr_table_get(hdrs, "Content-Encoding");
168     }
169 niq 562507 return found;
170     }
171    
172 jerenkrantz 94979 /* Outputs a long in LSB order to the given file
173     * only the bottom 4 bits are required for the deflate file format.
174     */
175 jerenkrantz 95397 static void putLong(unsigned char *string, unsigned long x)
176 jerenkrantz 94979 {
177 jerenkrantz 95397 string[0] = (unsigned char)(x & 0xff);
178     string[1] = (unsigned char)((x & 0xff00) >> 8);
179     string[2] = (unsigned char)((x & 0xff0000) >> 16);
180     string[3] = (unsigned char)((x & 0xff000000) >> 24);
181 jerenkrantz 94979 }
182    
183 jerenkrantz 95345 /* Inputs a string and returns a long.
184     */
185 trawick 95350 static unsigned long getLong(unsigned char *string)
186 jerenkrantz 95345 {
187 jerenkrantz 95397 return ((unsigned long)string[0])
188     | (((unsigned long)string[1]) << 8)
189     | (((unsigned long)string[2]) << 16)
190     | (((unsigned long)string[3]) << 24);
191 jerenkrantz 95345 }
192    
193 jerenkrantz 94979 static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
194     {
195     deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
196    
197     c->memlevel = DEFAULT_MEMLEVEL;
198     c->windowSize = DEFAULT_WINDOWSIZE;
199 jerenkrantz 94987 c->bufferSize = DEFAULT_BUFFERSIZE;
200 ianh 98895 c->compressionlevel = DEFAULT_COMPRESSION;
201 jerenkrantz 94979
202     return c;
203     }
204    
205     static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
206     const char *arg)
207     {
208     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
209     &deflate_module);
210     int i;
211    
212     i = atoi(arg);
213    
214     if (i < 1 || i > 15)
215     return "DeflateWindowSize must be between 1 and 15";
216    
217     c->windowSize = i * -1;
218    
219     return NULL;
220     }
221    
222 jerenkrantz 94987 static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
223     const char *arg)
224     {
225     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
226     &deflate_module);
227 wrowe 95676 int n = atoi(arg);
228 jerenkrantz 94987
229 wrowe 95676 if (n <= 0) {
230 jerenkrantz 94987 return "DeflateBufferSize should be positive";
231     }
232    
233 wrowe 95676 c->bufferSize = (apr_size_t)n;
234    
235 jerenkrantz 94987 return NULL;
236     }
237 jerenkrantz 94979 static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
238 nd 98149 const char *arg1, const char *arg2)
239 jerenkrantz 94979 {
240     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
241     &deflate_module);
242 jim 332306
243 nd 98149 if (arg2 == NULL) {
244 nd 98154 c->note_ratio_name = apr_pstrdup(cmd->pool, arg1);
245 nd 98149 }
246     else if (!strcasecmp(arg1, "ratio")) {
247 nd 98154 c->note_ratio_name = apr_pstrdup(cmd->pool, arg2);
248 nd 98149 }
249     else if (!strcasecmp(arg1, "input")) {
250 nd 98154 c->note_input_name = apr_pstrdup(cmd->pool, arg2);
251 nd 98149 }
252     else if (!strcasecmp(arg1, "output")) {
253 nd 98154 c->note_output_name = apr_pstrdup(cmd->pool, arg2);
254 nd 98149 }
255     else {
256     return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
257     }
258 jerenkrantz 94979
259     return NULL;
260     }
261    
262     static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy,
263     const char *arg)
264     {
265     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
266     &deflate_module);
267     int i;
268    
269     i = atoi(arg);
270    
271     if (i < 1 || i > 9)
272     return "DeflateMemLevel must be between 1 and 9";
273    
274     c->memlevel = i;
275    
276     return NULL;
277     }
278    
279 ianh 98895 static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
280     const char *arg)
281     {
282     deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
283     &deflate_module);
284     int i;
285    
286     i = atoi(arg);
287    
288     if (i < 1 || i > 9)
289     return "Compression Level must be between 1 and 9";
290    
291     c->compressionlevel = i;
292    
293     return NULL;
294     }
295    
296 jerenkrantz 94979 typedef struct deflate_ctx_t
297     {
298     z_stream stream;
299 jerenkrantz 94987 unsigned char *buffer;
300 jerenkrantz 94979 unsigned long crc;
301 jerenkrantz 95345 apr_bucket_brigade *bb, *proc_bb;
302 rpluem 425109 int (*libz_end_func)(z_streamp);
303 rpluem 426799 unsigned char *validation_buffer;
304     apr_size_t validation_buffer_length;
305 niq 580598 int inflate_init;
306 jerenkrantz 94979 } deflate_ctx;
307    
308 rpluem 426791 /* Number of validation bytes (CRC and length) after the compressed data */
309     #define VALIDATION_SIZE 8
310 rpluem 426790 /* Do not update ctx->crc, see comment in flush_libz_buffer */
311     #define NO_UPDATE_CRC 0
312     /* Do update ctx->crc, see comment in flush_libz_buffer */
313     #define UPDATE_CRC 1
314    
315 rpluem 423940 static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
316 rpluem 422731 struct apr_bucket_alloc_t *bucket_alloc,
317 rpluem 426790 int (*libz_func)(z_streamp, int), int flush,
318     int crc)
319 rpluem 422731 {
320 rpluem 424950 int zRC = Z_OK;
321 rpluem 422731 int done = 0;
322     unsigned int deflate_len;
323     apr_bucket *b;
324    
325     for (;;) {
326     deflate_len = c->bufferSize - ctx->stream.avail_out;
327    
328     if (deflate_len != 0) {
329 rpluem 426790 /*
330     * Do we need to update ctx->crc? Usually this is the case for
331     * inflate action where we need to do a crc on the output, whereas
332     * in the deflate case we need to do a crc on the input
333     */
334     if (crc) {
335     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer,
336     deflate_len);
337     }
338 rpluem 422731 b = apr_bucket_heap_create((char *)ctx->buffer,
339     deflate_len, NULL,
340     bucket_alloc);
341     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
342     ctx->stream.next_out = ctx->buffer;
343     ctx->stream.avail_out = c->bufferSize;
344     }
345    
346     if (done)
347     break;
348    
349 rpluem 423940 zRC = libz_func(&ctx->stream, flush);
350 rpluem 422731
351 rpluem 426793 /*
352     * We can ignore Z_BUF_ERROR because:
353     * When we call libz_func we can assume that
354     *
355     * - avail_in is zero (due to the surrounding code that calls
356     * flush_libz_buffer)
357     * - avail_out is non zero due to our actions some lines above
358     *
359     * So the only reason for Z_BUF_ERROR is that the internal libz
360     * buffers are now empty and thus we called libz_func one time
361     * too often. This does not hurt. It simply says that we are done.
362     */
363     if (zRC == Z_BUF_ERROR) {
364 rpluem 422731 zRC = Z_OK;
365 rpluem 426793 break;
366     }
367 rpluem 422731
368     done = (ctx->stream.avail_out != 0 || zRC == Z_STREAM_END);
369    
370     if (zRC != Z_OK && zRC != Z_STREAM_END)
371     break;
372     }
373     return zRC;
374     }
375    
376 rpluem 425109 static apr_status_t deflate_ctx_cleanup(void *data)
377     {
378     deflate_ctx *ctx = (deflate_ctx *)data;
379    
380     if (ctx)
381     ctx->libz_end_func(&ctx->stream);
382     return APR_SUCCESS;
383     }
384 fielding 743595
385     /* ETag must be unique among the possible representations, so a change
386     * to content-encoding requires a corresponding change to the ETag.
387     * This routine appends -transform (e.g., -gzip) to the entity-tag
388     * value inside the double-quotes if an ETag has already been set
389     * and its value already contains double-quotes. PR 39727
390 niq 581198 */
391 niq 607219 static void deflate_check_etag(request_rec *r, const char *transform)
392 niq 581198 {
393     const char *etag = apr_table_get(r->headers_out, "ETag");
394 fielding 743595 apr_size_t etaglen;
395    
396     if ((etag && ((etaglen = strlen(etag)) > 2))) {
397     if (etag[etaglen - 1] == '"') {
398     apr_size_t transformlen = strlen(transform);
399     char *newtag = apr_palloc(r->pool, etaglen + transformlen + 2);
400     char *d = newtag;
401     char *e = d + etaglen - 1;
402     const char *s = etag;
403    
404     for (; d < e; ++d, ++s) {
405     *d = *s; /* copy etag to newtag up to last quote */
406     }
407     *d++ = '-'; /* append dash to newtag */
408     s = transform;
409     e = d + transformlen;
410     for (; d < e; ++d, ++s) {
411     *d = *s; /* copy transform to newtag */
412     }
413     *d++ = '"'; /* append quote to newtag */
414     *d = '\0'; /* null terminate newtag */
415    
416     apr_table_setn(r->headers_out, "ETag", newtag);
417 lars 740149 }
418     }
419 niq 581198 }
420 fielding 743595
421 jerenkrantz 94979 static apr_status_t deflate_out_filter(ap_filter_t *f,
422     apr_bucket_brigade *bb)
423     {
424     apr_bucket *e;
425     request_rec *r = f->r;
426     deflate_ctx *ctx = f->ctx;
427     int zRC;
428 rpluem 426795 deflate_filter_config *c;
429 jerenkrantz 94979
430 niq 104352 /* Do nothing if asked to filter nothing. */
431     if (APR_BRIGADE_EMPTY(bb)) {
432 rpluem 424759 return ap_pass_brigade(f->next, bb);
433 niq 104352 }
434    
435 rpluem 426795 c = ap_get_module_config(r->server->module_config,
436     &deflate_module);
437    
438 jerenkrantz 94979 /* If we don't have a context, we need to ensure that it is okay to send
439     * the deflated content. If we have a context, that means we've done
440     * this before and we liked it.
441     * This could be not so nice if we always fail. But, if we succeed,
442     * we're in better shape.
443     */
444     if (!ctx) {
445 jorton 105410 char *token;
446 niq 104352 const char *encoding;
447 jerenkrantz 94979
448 fielding 743814 /*
449     * Only work on main request, not subrequests,
450     * that are not a 204 response with no content
451     * and are not tagged with the no-gzip env variable
452     * and not a partial response to a Range request.
453 jerenkrantz 94979 */
454 fielding 743814 if ((r->main != NULL) || (r->status == HTTP_NO_CONTENT) ||
455     apr_table_get(r->subprocess_env, "no-gzip") ||
456     apr_table_get(r->headers_out, "Content-Range")
457     ) {
458 jerenkrantz 94982 ap_remove_output_filter(f);
459 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
460     }
461    
462     /* Some browsers might have problems with content types
463     * other than text/html, so set gzip-only-text/html
464     * (with browsermatch) for them
465     */
466 ianh 96318 if (r->content_type == NULL
467     || strncmp(r->content_type, "text/html", 9)) {
468     const char *env_value = apr_table_get(r->subprocess_env,
469     "gzip-only-text/html");
470 ianh 96588 if ( env_value && (strcmp(env_value,"1") == 0) ) {
471 ianh 96318 ap_remove_output_filter(f);
472 ianh 96590 return ap_pass_brigade(f->next, bb);
473 jim 332306 }
474 jerenkrantz 94979 }
475    
476 jerenkrantz 95339 /* Let's see what our current Content-Encoding is.
477 nd 101019 * If it's already encoded, don't compress again.
478     * (We could, but let's not.)
479 jerenkrantz 95339 */
480     encoding = apr_table_get(r->headers_out, "Content-Encoding");
481     if (encoding) {
482 nd 98948 const char *err_enc;
483    
484     err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
485     if (err_enc) {
486     encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
487     }
488     }
489     else {
490     encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
491     }
492    
493 nd 99880 if (r->content_encoding) {
494     encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
495     r->content_encoding, NULL)
496     : r->content_encoding;
497     }
498    
499 nd 98948 if (encoding) {
500 jerenkrantz 95339 const char *tmp = encoding;
501    
502     token = ap_get_token(r->pool, &tmp, 0);
503 nd 101019 while (token && *token) {
504     /* stolen from mod_negotiation: */
505     if (strcmp(token, "identity") && strcmp(token, "7bit") &&
506     strcmp(token, "8bit") && strcmp(token, "binary")) {
507    
508 jerenkrantz 95339 ap_remove_output_filter(f);
509 ianh 103405 return ap_pass_brigade(f->next, bb);
510 jerenkrantz 95339 }
511 nd 101019
512 jerenkrantz 95339 /* Otherwise, skip token */
513 nd 101019 if (*tmp) {
514     ++tmp;
515     }
516     token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
517 jerenkrantz 95339 }
518 ianh 95181 }
519    
520 jerenkrantz 97473 /* Even if we don't accept this request based on it not having
521     * the Accept-Encoding, we need to note that we were looking
522     * for this header and downstream proxies should be aware of that.
523     */
524 pquerna 161691 apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
525 jerenkrantz 97473
526 ianh 103405 /* force-gzip will just force it out regardless if the browser
527     * can actually do anything with it.
528     */
529 jerenkrantz 103803 if (!apr_table_get(r->subprocess_env, "force-gzip")) {
530 ianh 103405 const char *accepts;
531     /* if they don't have the line, then they can't play */
532     accepts = apr_table_get(r->headers_in, "Accept-Encoding");
533     if (accepts == NULL) {
534     ap_remove_output_filter(f);
535     return ap_pass_brigade(f->next, bb);
536 nd 101015 }
537    
538 ianh 103405 token = ap_get_token(r->pool, &accepts, 0);
539     while (token && token[0] && strcasecmp(token, "gzip")) {
540     /* skip parameters, XXX: ;q=foo evaluation? */
541 jim 332306 while (*accepts == ';') {
542 ianh 103405 ++accepts;
543     token = ap_get_token(r->pool, &accepts, 1);
544     }
545    
546     /* retrieve next token */
547     if (*accepts == ',') {
548     ++accepts;
549     }
550     token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
551 nd 101015 }
552 jerenkrantz 94979
553 ianh 103405 /* No acceptable token found. */
554     if (token == NULL || token[0] == '\0') {
555     ap_remove_output_filter(f);
556     return ap_pass_brigade(f->next, bb);
557     }
558 jerenkrantz 94979 }
559 ake 103910
560 fielding 743814 /* At this point we have decided to filter the content, so change
561     * important content metadata before sending any response out.
562     */
563    
564     /* If the entire Content-Encoding is "identity", we can replace it. */
565     if (!encoding || !strcasecmp(encoding, "identity")) {
566     apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
567     }
568     else {
569     apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
570     }
571     /* Fix r->content_encoding if it was set before */
572     if (r->content_encoding) {
573     r->content_encoding = apr_table_get(r->headers_out,
574     "Content-Encoding");
575     }
576     apr_table_unset(r->headers_out, "Content-Length");
577     apr_table_unset(r->headers_out, "Content-MD5");
578     deflate_check_etag(r, "gzip");
579    
580     /* For a 304 response, only change the headers */
581     if (r->status == HTTP_NOT_MODIFIED) {
582 niq 104352 ap_remove_output_filter(f);
583 ake 103910 return ap_pass_brigade(f->next, bb);
584     }
585    
586 jerenkrantz 94979 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
587     ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
588 jerenkrantz 94987 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
589 rpluem 425109 ctx->libz_end_func = deflateEnd;
590 jerenkrantz 95339
591 ianh 98895 zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
592 jerenkrantz 94979 c->windowSize, c->memlevel,
593     Z_DEFAULT_STRATEGY);
594    
595     if (zRC != Z_OK) {
596 rpluem 426795 deflateEnd(&ctx->stream);
597 trawick 95151 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
598 jerenkrantz 94979 "unable to init Zlib: "
599     "deflateInit2 returned %d: URL %s",
600     zRC, r->uri);
601 rpluem 422739 /*
602     * Remove ourselves as it does not make sense to return:
603     * We are not able to init libz and pass data down the chain
604     * uncompressed.
605     */
606     ap_remove_output_filter(f);
607 jerenkrantz 94979 return ap_pass_brigade(f->next, bb);
608     }
609 rpluem 425109 /*
610     * Register a cleanup function to ensure that we cleanup the internal
611     * libz resources.
612     */
613     apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
614     apr_pool_cleanup_null);
615 jerenkrantz 94979
616 jorton 105410 /* add immortal gzip header */
617     e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
618     f->c->bucket_alloc);
619 jerenkrantz 94979 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
620    
621 jerenkrantz 95521 /* initialize deflate output buffer */
622     ctx->stream.next_out = ctx->buffer;
623     ctx->stream.avail_out = c->bufferSize;
624 jerenkrantz 94979 }
625 jim 332306
626 jorton 103810 while (!APR_BRIGADE_EMPTY(bb))
627 jwoolley 101788 {
628 jerenkrantz 94979 const char *data;
629     apr_bucket *b;
630     apr_size_t len;
631    
632 jorton 103810 e = APR_BRIGADE_FIRST(bb);
633    
634 jerenkrantz 94979 if (APR_BUCKET_IS_EOS(e)) {
635 jwoolley 95413 char *buf;
636 jerenkrantz 94979
637     ctx->stream.avail_in = 0; /* should be zero already anyway */
638 rpluem 422731 /* flush the remaining data from the zlib buffers */
639 rpluem 426790 flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
640     NO_UPDATE_CRC);
641 jerenkrantz 94979
642 rpluem 426791 buf = apr_palloc(r->pool, VALIDATION_SIZE);
643 jwoolley 95413 putLong((unsigned char *)&buf[0], ctx->crc);
644     putLong((unsigned char *)&buf[4], ctx->stream.total_in);
645 jerenkrantz 94979
646 rpluem 426791 b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
647     f->c->bucket_alloc);
648 jerenkrantz 94979 APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
649 trawick 95151 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
650 jerenkrantz 94979 "Zlib: Compressed %ld to %ld : URL %s",
651     ctx->stream.total_in, ctx->stream.total_out, r->uri);
652    
653 nd 98154 /* leave notes for logging */
654     if (c->note_input_name) {
655     apr_table_setn(r->notes, c->note_input_name,
656     (ctx->stream.total_in > 0)
657     ? apr_off_t_toa(r->pool,
658     ctx->stream.total_in)
659     : "-");
660     }
661 jerenkrantz 94979
662 nd 98154 if (c->note_output_name) {
663     apr_table_setn(r->notes, c->note_output_name,
664     (ctx->stream.total_in > 0)
665     ? apr_off_t_toa(r->pool,
666     ctx->stream.total_out)
667     : "-");
668     }
669    
670     if (c->note_ratio_name) {
671     apr_table_setn(r->notes, c->note_ratio_name,
672     (ctx->stream.total_in > 0)
673     ? apr_itoa(r->pool,
674     (int)(ctx->stream.total_out
675     * 100
676     / ctx->stream.total_in))
677     : "-");
678     }
679    
680 jerenkrantz 94979 deflateEnd(&ctx->stream);
681 rpluem 425109 /* No need for cleanup any longer */
682     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
683 jerenkrantz 94979
684     /* Remove EOS from the old list, and insert into the new. */
685     APR_BUCKET_REMOVE(e);
686     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
687    
688     /* Okay, we've seen the EOS.
689     * Time to pass it along down the chain.
690     */
691     return ap_pass_brigade(f->next, ctx->bb);
692     }
693    
694     if (APR_BUCKET_IS_FLUSH(e)) {
695 jerenkrantz 100730 apr_status_t rv;
696 jorton 103810
697 rpluem 422731 /* flush the remaining data from the zlib buffers */
698 rpluem 423940 zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
699 rpluem 426790 Z_SYNC_FLUSH, NO_UPDATE_CRC);
700 rpluem 422731 if (zRC != Z_OK) {
701     return APR_EGENERAL;
702 jerenkrantz 95341 }
703 jerenkrantz 94979
704 rpluem 422731 /* Remove flush bucket from old brigade anf insert into the new. */
705     APR_BUCKET_REMOVE(e);
706     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
707 jerenkrantz 100730 rv = ap_pass_brigade(f->next, ctx->bb);
708     if (rv != APR_SUCCESS) {
709     return rv;
710     }
711 jerenkrantz 94979 continue;
712     }
713    
714 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
715     /*
716     * Remove meta data bucket from old brigade and insert into the
717     * new.
718     */
719     APR_BUCKET_REMOVE(e);
720     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
721     continue;
722 gregames 554011 }
723    
724 jerenkrantz 94979 /* read */
725     apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
726    
727     /* This crc32 function is from zlib. */
728     ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
729    
730     /* write */
731     ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
732     * but we'll just have to
733     * trust zlib */
734     ctx->stream.avail_in = len;
735    
736     while (ctx->stream.avail_in != 0) {
737     if (ctx->stream.avail_out == 0) {
738 jerenkrantz 100730 apr_status_t rv;
739    
740 jerenkrantz 94979 ctx->stream.next_out = ctx->buffer;
741 jerenkrantz 94987 len = c->bufferSize - ctx->stream.avail_out;
742 jerenkrantz 94979
743     b = apr_bucket_heap_create((char *)ctx->buffer, len,
744     NULL, f->c->bucket_alloc);
745     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
746 jerenkrantz 94987 ctx->stream.avail_out = c->bufferSize;
747 jerenkrantz 100730 /* Send what we have right now to the next filter. */
748     rv = ap_pass_brigade(f->next, ctx->bb);
749     if (rv != APR_SUCCESS) {
750     return rv;
751     }
752 jerenkrantz 94979 }
753    
754     zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
755    
756 rpluem 422736 if (zRC != Z_OK) {
757 jerenkrantz 94979 return APR_EGENERAL;
758 rpluem 422736 }
759 jerenkrantz 94979 }
760 jorton 103810
761     apr_bucket_delete(e);
762 jerenkrantz 94979 }
763    
764 jwoolley 97526 apr_brigade_cleanup(bb);
765 jerenkrantz 94979 return APR_SUCCESS;
766     }
767    
768 jerenkrantz 95345 /* This is the deflate input filter (inflates). */
769     static apr_status_t deflate_in_filter(ap_filter_t *f,
770     apr_bucket_brigade *bb,
771     ap_input_mode_t mode,
772     apr_read_type_e block,
773     apr_off_t readbytes)
774     {
775     apr_bucket *bkt;
776     request_rec *r = f->r;
777     deflate_ctx *ctx = f->ctx;
778     int zRC;
779     apr_status_t rv;
780     deflate_filter_config *c;
781    
782     /* just get out of the way of things we don't want. */
783     if (mode != AP_MODE_READBYTES) {
784     return ap_get_brigade(f->next, bb, mode, block, readbytes);
785     }
786    
787     c = ap_get_module_config(r->server->module_config, &deflate_module);
788    
789     if (!ctx) {
790 niq 562507 char deflate_hdr[10];
791 jerenkrantz 95345 apr_size_t len;
792    
793     /* only work on main request/no subrequests */
794 pquerna 105403 if (!ap_is_initial_req(r)) {
795 jerenkrantz 95345 ap_remove_input_filter(f);
796     return ap_get_brigade(f->next, bb, mode, block, readbytes);
797     }
798    
799 niq 563154 /* We can't operate on Content-Ranges */
800     if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
801     ap_remove_input_filter(f);
802     return ap_get_brigade(f->next, bb, mode, block, readbytes);
803     }
804    
805 niq 560689 /* Check whether request body is gzipped.
806     *
807     * If it is, we're transforming the contents, invalidating
808     * some request headers including Content-Encoding.
809     *
810     * If not, we just remove ourself.
811 jerenkrantz 95345 */
812 niq 563803 if (check_gzip(r, r->headers_in, NULL) == 0) {
813 jerenkrantz 95345 ap_remove_input_filter(f);
814     return ap_get_brigade(f->next, bb, mode, block, readbytes);
815     }
816    
817     f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
818     ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
819     ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
820     ctx->buffer = apr_palloc(r->pool, c->bufferSize);
821    
822     rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
823     if (rv != APR_SUCCESS) {
824     return rv;
825     }
826 jerenkrantz 98689
827 niq 563133 apr_table_unset(r->headers_in, "Content-Length");
828     apr_table_unset(r->headers_in, "Content-MD5");
829    
830 jim 332306 len = 10;
831     rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
832 jerenkrantz 95345 if (rv != APR_SUCCESS) {
833     return rv;
834     }
835    
836     /* We didn't get the magic bytes. */
837     if (len != 10 ||
838     deflate_hdr[0] != deflate_magic[0] ||
839     deflate_hdr[1] != deflate_magic[1]) {
840     return APR_EGENERAL;
841     }
842    
843     /* We can't handle flags for now. */
844     if (deflate_hdr[3] != 0) {
845     return APR_EGENERAL;
846     }
847    
848     zRC = inflateInit2(&ctx->stream, c->windowSize);
849    
850     if (zRC != Z_OK) {
851     f->ctx = NULL;
852 jerenkrantz 98689 inflateEnd(&ctx->stream);
853 jerenkrantz 95345 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
854     "unable to init Zlib: "
855     "inflateInit2 returned %d: URL %s",
856     zRC, r->uri);
857     ap_remove_input_filter(f);
858     return ap_get_brigade(f->next, bb, mode, block, readbytes);
859     }
860    
861     /* initialize deflate output buffer */
862     ctx->stream.next_out = ctx->buffer;
863     ctx->stream.avail_out = c->bufferSize;
864    
865     apr_brigade_cleanup(ctx->bb);
866     }
867    
868     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
869     rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
870    
871     if (rv != APR_SUCCESS) {
872 jerenkrantz 98689 /* What about APR_EAGAIN errors? */
873     inflateEnd(&ctx->stream);
874 jerenkrantz 95345 return rv;
875     }
876    
877 jwoolley 101788 for (bkt = APR_BRIGADE_FIRST(ctx->bb);
878     bkt != APR_BRIGADE_SENTINEL(ctx->bb);
879     bkt = APR_BUCKET_NEXT(bkt))
880     {
881 jerenkrantz 95345 const char *data;
882     apr_size_t len;
883    
884     /* If we actually see the EOS, that means we screwed up! */
885     if (APR_BUCKET_IS_EOS(bkt)) {
886 jerenkrantz 98689 inflateEnd(&ctx->stream);
887 jerenkrantz 95345 return APR_EGENERAL;
888     }
889    
890     if (APR_BUCKET_IS_FLUSH(bkt)) {
891     apr_bucket *tmp_heap;
892     zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
893     if (zRC != Z_OK) {
894 jerenkrantz 98689 inflateEnd(&ctx->stream);
895 jerenkrantz 95345 return APR_EGENERAL;
896     }
897    
898     ctx->stream.next_out = ctx->buffer;
899     len = c->bufferSize - ctx->stream.avail_out;
900    
901     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
902     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
903     NULL, f->c->bucket_alloc);
904     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
905     ctx->stream.avail_out = c->bufferSize;
906    
907     /* Move everything to the returning brigade. */
908     APR_BUCKET_REMOVE(bkt);
909     APR_BRIGADE_CONCAT(bb, ctx->bb);
910     break;
911     }
912    
913     /* read */
914     apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
915    
916     /* pass through zlib inflate. */
917     ctx->stream.next_in = (unsigned char *)data;
918     ctx->stream.avail_in = len;
919    
920 jerenkrantz 95359 zRC = Z_OK;
921    
922 jerenkrantz 95345 while (ctx->stream.avail_in != 0) {
923     if (ctx->stream.avail_out == 0) {
924     apr_bucket *tmp_heap;
925     ctx->stream.next_out = ctx->buffer;
926     len = c->bufferSize - ctx->stream.avail_out;
927    
928     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
929     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
930     NULL, f->c->bucket_alloc);
931     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
932     ctx->stream.avail_out = c->bufferSize;
933     }
934    
935     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
936    
937     if (zRC == Z_STREAM_END) {
938     break;
939     }
940    
941     if (zRC != Z_OK) {
942 jerenkrantz 98689 inflateEnd(&ctx->stream);
943 jerenkrantz 95345 return APR_EGENERAL;
944     }
945     }
946     if (zRC == Z_STREAM_END) {
947     apr_bucket *tmp_heap, *eos;
948    
949     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
950     "Zlib: Inflated %ld to %ld : URL %s",
951     ctx->stream.total_in, ctx->stream.total_out,
952     r->uri);
953    
954     len = c->bufferSize - ctx->stream.avail_out;
955    
956     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
957     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
958     NULL, f->c->bucket_alloc);
959     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
960     ctx->stream.avail_out = c->bufferSize;
961    
962     /* Is the remaining 8 bytes already in the avail stream? */
963     if (ctx->stream.avail_in >= 8) {
964     unsigned long compCRC, compLen;
965     compCRC = getLong(ctx->stream.next_in);
966     if (ctx->crc != compCRC) {
967 jerenkrantz 98689 inflateEnd(&ctx->stream);
968 jerenkrantz 95345 return APR_EGENERAL;
969     }
970     ctx->stream.next_in += 4;
971     compLen = getLong(ctx->stream.next_in);
972     if (ctx->stream.total_out != compLen) {
973 jerenkrantz 98689 inflateEnd(&ctx->stream);
974 jerenkrantz 95345 return APR_EGENERAL;
975     }
976     }
977     else {
978     /* FIXME: We need to grab the 8 verification bytes
979     * from the wire! */
980 jerenkrantz 98689 inflateEnd(&ctx->stream);
981 jerenkrantz 95345 return APR_EGENERAL;
982     }
983    
984     inflateEnd(&ctx->stream);
985 jerenkrantz 98689
986 jerenkrantz 95345 eos = apr_bucket_eos_create(f->c->bucket_alloc);
987 jim 332306 APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
988 jerenkrantz 95345 break;
989     }
990 jerenkrantz 95666
991 jerenkrantz 95345 }
992 jerenkrantz 95521 apr_brigade_cleanup(ctx->bb);
993 jerenkrantz 95345 }
994    
995 jerenkrantz 95666 /* If we are about to return nothing for a 'blocking' read and we have
996     * some data in our zlib buffer, flush it out so we can return something.
997     */
998     if (block == APR_BLOCK_READ &&
999     APR_BRIGADE_EMPTY(ctx->proc_bb) &&
1000     ctx->stream.avail_out < c->bufferSize) {
1001     apr_bucket *tmp_heap;
1002     apr_size_t len;
1003     ctx->stream.next_out = ctx->buffer;
1004     len = c->bufferSize - ctx->stream.avail_out;
1005    
1006     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1007     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
1008     NULL, f->c->bucket_alloc);
1009     APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
1010     ctx->stream.avail_out = c->bufferSize;
1011     }
1012    
1013 jerenkrantz 95345 if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
1014     apr_bucket_brigade *newbb;
1015    
1016     /* May return APR_INCOMPLETE which is fine by us. */
1017     apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
1018    
1019     newbb = apr_brigade_split(ctx->proc_bb, bkt);
1020     APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
1021     APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
1022     }
1023    
1024     return APR_SUCCESS;
1025     }
1026    
1027 ianh 103405
1028     /* Filter to inflate for a content-transforming proxy. */
1029     static apr_status_t inflate_out_filter(ap_filter_t *f,
1030     apr_bucket_brigade *bb)
1031     {
1032 nd 104166 int zlib_method;
1033     int zlib_flags;
1034 rpluem 426799 apr_bucket *e;
1035 ianh 103405 request_rec *r = f->r;
1036     deflate_ctx *ctx = f->ctx;
1037     int zRC;
1038     apr_status_t rv;
1039     deflate_filter_config *c;
1040    
1041 niq 104352 /* Do nothing if asked to filter nothing. */
1042     if (APR_BRIGADE_EMPTY(bb)) {
1043 rpluem 424759 return ap_pass_brigade(f->next, bb);
1044 niq 104352 }
1045    
1046 ianh 103405 c = ap_get_module_config(r->server->module_config, &deflate_module);
1047    
1048     if (!ctx) {
1049    
1050 fielding 743814 /*
1051     * Only work on main request, not subrequests,
1052     * that are not a 204 response with no content
1053     * and not a partial response to a Range request,
1054     * and only when Content-Encoding ends in gzip.
1055     */
1056     if (!ap_is_initial_req(r) || (r->status == HTTP_NO_CONTENT) ||
1057     (apr_table_get(r->headers_out, "Content-Range") != NULL) ||
1058     (check_gzip(r, r->headers_out, r->err_headers_out) == 0)
1059     ) {
1060 ianh 103405 ap_remove_output_filter(f);
1061     return ap_pass_brigade(f->next, bb);
1062     }
1063    
1064 rpluem 475922 /*
1065 fielding 743814 * At this point we have decided to filter the content, so change
1066     * important content metadata before sending any response out.
1067     * Content-Encoding was already reset by the check_gzip() call.
1068 ianh 103405 */
1069 fielding 743814 apr_table_unset(r->headers_out, "Content-Length");
1070     apr_table_unset(r->headers_out, "Content-MD5");
1071     deflate_check_etag(r, "gunzip");
1072 ianh 103405
1073 fielding 743814 /* For a 304 response, only change the headers */
1074     if (r->status == HTTP_NOT_MODIFIED) {
1075 niq 104352 ap_remove_output_filter(f);
1076     return ap_pass_brigade(f->next, bb);
1077     }
1078    
1079 ianh 103405 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
1080 rpluem 426799 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
1081 ianh 103405 ctx->buffer = apr_palloc(r->pool, c->bufferSize);
1082 rpluem 426799 ctx->libz_end_func = inflateEnd;
1083     ctx->validation_buffer = NULL;
1084     ctx->validation_buffer_length = 0;
1085 ianh 103405
1086     zRC = inflateInit2(&ctx->stream, c->windowSize);
1087    
1088     if (zRC != Z_OK) {
1089     f->ctx = NULL;
1090     inflateEnd(&ctx->stream);
1091     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1092     "unable to init Zlib: "
1093     "inflateInit2 returned %d: URL %s",
1094     zRC, r->uri);
1095 rpluem 426799 /*
1096     * Remove ourselves as it does not make sense to return:
1097     * We are not able to init libz and pass data down the chain
1098     * compressed.
1099     */
1100 ianh 103405 ap_remove_output_filter(f);
1101     return ap_pass_brigade(f->next, bb);
1102     }
1103    
1104 rpluem 426799 /*
1105     * Register a cleanup function to ensure that we cleanup the internal
1106     * libz resources.
1107     */
1108     apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
1109     apr_pool_cleanup_null);
1110    
1111     /* initialize inflate output buffer */
1112 ianh 103405 ctx->stream.next_out = ctx->buffer;
1113     ctx->stream.avail_out = c->bufferSize;
1114    
1115 niq 580598 ctx->inflate_init = 0;
1116 ianh 103405 }
1117    
1118 rpluem 426799 while (!APR_BRIGADE_EMPTY(bb))
1119 ianh 103405 {
1120     const char *data;
1121 rpluem 426799 apr_bucket *b;
1122 ianh 103405 apr_size_t len;
1123    
1124 rpluem 426799 e = APR_BRIGADE_FIRST(bb);
1125    
1126     if (APR_BUCKET_IS_EOS(e)) {
1127 rpluem 475406 /*
1128     * We are really done now. Ensure that we never return here, even
1129     * if a second EOS bucket falls down the chain. Thus remove
1130     * ourselves.
1131     */
1132     ap_remove_output_filter(f);
1133 rpluem 475922 /* should be zero already anyway */
1134     ctx->stream.avail_in = 0;
1135 rpluem 426799 /*
1136     * Flush the remaining data from the zlib buffers. It is correct
1137     * to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
1138     * deflate case. In the inflate case Z_FINISH requires to have a
1139     * large enough output buffer to put ALL data in otherwise it
1140     * fails, whereas in the deflate case you can empty a filled output
1141     * buffer and call it again until no more output can be created.
1142     */
1143     flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
1144     UPDATE_CRC);
1145     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1146     "Zlib: Inflated %ld to %ld : URL %s",
1147     ctx->stream.total_in, ctx->stream.total_out, r->uri);
1148    
1149     if (ctx->validation_buffer_length == VALIDATION_SIZE) {
1150     unsigned long compCRC, compLen;
1151     compCRC = getLong(ctx->validation_buffer);
1152     if (ctx->crc != compCRC) {
1153     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1154     "Zlib: Checksum of inflated stream invalid");
1155     return APR_EGENERAL;
1156     }
1157     ctx->validation_buffer += VALIDATION_SIZE / 2;
1158     compLen = getLong(ctx->validation_buffer);
1159     if (ctx->stream.total_out != compLen) {
1160     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1161     "Zlib: Length of inflated stream invalid");
1162     return APR_EGENERAL;
1163     }
1164     }
1165     else {
1166     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1167     "Zlib: Validation bytes not present");
1168     return APR_EGENERAL;
1169     }
1170    
1171 ianh 103405 inflateEnd(&ctx->stream);
1172 rpluem 426799 /* No need for cleanup any longer */
1173     apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
1174    
1175     /* Remove EOS from the old list, and insert into the new. */
1176     APR_BUCKET_REMOVE(e);
1177     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1178    
1179     /*
1180     * Okay, we've seen the EOS.
1181     * Time to pass it along down the chain.
1182     */
1183     return ap_pass_brigade(f->next, ctx->bb);
1184 ianh 103405 }
1185    
1186 rpluem 426799 if (APR_BUCKET_IS_FLUSH(e)) {
1187     apr_status_t rv;
1188    
1189     /* flush the remaining data from the zlib buffers */
1190     zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
1191     Z_SYNC_FLUSH, UPDATE_CRC);
1192     if (zRC != Z_OK) {
1193     return APR_EGENERAL;
1194     }
1195    
1196     /* Remove flush bucket from old brigade anf insert into the new. */
1197     APR_BUCKET_REMOVE(e);
1198     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1199     rv = ap_pass_brigade(f->next, ctx->bb);
1200     if (rv != APR_SUCCESS) {
1201     return rv;
1202     }
1203 niq 416165 continue;
1204 ianh 103405 }
1205    
1206 gregames 556028 if (APR_BUCKET_IS_METADATA(e)) {
1207     /*
1208     * Remove meta data bucket from old brigade and insert into the
1209     * new.
1210     */
1211     APR_BUCKET_REMOVE(e);
1212     APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
1213     continue;
1214     }
1215    
1216 ianh 103405 /* read */
1217 rpluem 426799 apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
1218 ianh 103405
1219     /* first bucket contains zlib header */
1220 niq 580598 if (!ctx->inflate_init++) {
1221 nd 104166 if (len < 10) {
1222 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1223     "Insufficient data for inflate");
1224 nd 104166 return APR_EGENERAL;
1225 jim 332306 }
1226 ianh 103405 else {
1227 nd 104166 zlib_method = data[2];
1228     zlib_flags = data[3];
1229     if (zlib_method != Z_DEFLATED) {
1230 nd 104155 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1231 nd 104166 "inflate: data not deflated!");
1232     ap_remove_output_filter(f);
1233     return ap_pass_brigade(f->next, bb);
1234 niq 104034 }
1235 ianh 103405 if (data[0] != deflate_magic[0] ||
1236 niq 104034 data[1] != deflate_magic[1] ||
1237 nd 104166 (zlib_flags & RESERVED) != 0) {
1238 ianh 103405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1239 nd 104166 "inflate: bad header");
1240 ianh 103405 return APR_EGENERAL ;
1241     }
1242     data += 10 ;
1243     len -= 10 ;
1244     }
1245 nd 104166 if (zlib_flags & EXTRA_FIELD) {
1246     unsigned int bytes = (unsigned int)(data[0]);
1247     bytes += ((unsigned int)(data[1])) << 8;
1248     bytes += 2;
1249     if (len < bytes) {
1250 niq 104034 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1251 nd 104166 "inflate: extra field too big (not "
1252     "supported)");
1253     return APR_EGENERAL;
1254 niq 104034 }
1255 nd 104166 data += bytes;
1256     len -= bytes;
1257 niq 104034 }
1258 nd 104166 if (zlib_flags & ORIG_NAME) {
1259     while (len-- && *data++);
1260 niq 104034 }
1261 nd 104166 if (zlib_flags & COMMENT) {
1262     while (len-- && *data++);
1263 niq 104034 }
1264 nd 104166 if (zlib_flags & HEAD_CRC) {
1265     len -= 2;
1266     data += 2;
1267 niq 104034 }
1268 ianh 103405 }
1269    
1270     /* pass through zlib inflate. */
1271     ctx->stream.next_in = (unsigned char *)data;
1272     ctx->stream.avail_in = len;
1273    
1274 rpluem 426799 if (ctx->validation_buffer) {
1275     if (ctx->validation_buffer_length < VALIDATION_SIZE) {
1276     apr_size_t copy_size;
1277    
1278     copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
1279     if (copy_size > ctx->stream.avail_in)
1280     copy_size = ctx->stream.avail_in;
1281 rpluem 475915 memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
1282     ctx->stream.next_in, copy_size);
1283     /* Saved copy_size bytes */
1284     ctx->stream.avail_in -= copy_size;
1285 niq 476600 ctx->validation_buffer_length += copy_size;
1286 rpluem 475915 }
1287     if (ctx->stream.avail_in) {
1288 rpluem 426799 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1289     "Zlib: %d bytes of garbage at the end of "
1290     "compressed stream.", ctx->stream.avail_in);
1291 rpluem 475915 /*
1292     * There is nothing worth consuming for zlib left, because it is
1293     * either garbage data or the data has been copied to the
1294     * validation buffer (processing validation data is no business
1295     * for zlib). So set ctx->stream.avail_in to zero to indicate
1296     * this to the following while loop.
1297     */
1298     ctx->stream.avail_in = 0;
1299 rpluem 426799 }
1300     }
1301    
1302 ianh 103405 zRC = Z_OK;
1303    
1304     while (ctx->stream.avail_in != 0) {
1305     if (ctx->stream.avail_out == 0) {
1306 rpluem 426799
1307 ianh 103405 ctx->stream.next_out = ctx->buffer;
1308     len = c->bufferSize - ctx->stream.avail_out;
1309    
1310     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
1311 rpluem 426799 b = apr_bucket_heap_create((char *)ctx->buffer, len,
1312     NULL, f->c->bucket_alloc);
1313     APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
1314 ianh 103405 ctx->stream.avail_out = c->bufferSize;
1315 rpluem 426799 /* Send what we have right now to the next filter. */
1316     rv = ap_pass_brigade(f->next, ctx->bb);
1317     if (rv != APR_SUCCESS) {
1318     return rv;
1319     }
1320 ianh 103405 }
1321    
1322     zRC = inflate(&ctx->stream, Z_NO_FLUSH);
1323    
1324     if (zRC == Z_STREAM_END) {
1325 rpluem 426799 /*
1326     * We have inflated all data. Now try to capture the
1327     * validation bytes. We may not have them all available
1328     * right now, but capture what is there.
1329     */
1330     ctx->validation_buffer = apr_pcalloc(f->r->pool,
1331     VALIDATION_SIZE);
1332     if (ctx->stream.avail_in > VALIDATION_SIZE) {
1333     ctx->validation_buffer_length = VALIDATION_SIZE;
1334 rpluem 475920 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1335     "Zlib: %d bytes of garbage at the end of "
1336     "compressed stream.",
1337     ctx->stream.avail_in - VALIDATION_SIZE);
1338 rpluem 426799 } else if (ctx->stream.avail_in > 0) {
1339     ctx->validation_buffer_length = ctx->stream.avail_in;
1340     }
1341     if (ctx->validation_buffer_length)
1342     memcpy(ctx->validation_buffer, ctx->stream.next_in,
1343     ctx->validation_buffer_length);
1344 ianh 103405 break;
1345     }
1346    
1347     if (zRC != Z_OK) {
1348     return APR_EGENERAL;
1349     }
1350     }
1351    
1352 rpluem 426799 apr_bucket_delete(e);
1353 ianh 103405 }
1354    
1355 rpluem 426799 apr_brigade_cleanup(bb);
1356     return APR_SUCCESS;
1357 ianh 103405 }
1358    
1359 niq 416165 #define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
1360 jerenkrantz 94979 static void register_hooks(apr_pool_t *p)
1361     {
1362 jerenkrantz 95906 ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
1363 jerenkrantz 94979 AP_FTYPE_CONTENT_SET);
1364 ianh 103405 ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
1365     AP_FTYPE_RESOURCE-1);
1366 jerenkrantz 95906 ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
1367 jerenkrantz 95345 AP_FTYPE_CONTENT_SET);
1368 jerenkrantz 94979 }
1369    
1370     static const command_rec deflate_filter_cmds[] = {
1371 nd 98149 AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
1372 jerenkrantz 94979 "Set a note to report on compression ratio"),
1373     AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
1374     RSRC_CONF, "Set the Deflate window size (1-15)"),
1375 jerenkrantz 94987 AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
1376     "Set the Deflate Buffer Size"),
1377 jerenkrantz 94979 AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
1378     "Set the Deflate Memory Level (1-9)"),
1379 ianh 98982 AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
1380 ianh 98895 "Set the Deflate Compression Level (1-9)"),
1381 jerenkrantz 94979 {NULL}
1382     };
1383    
1384     module AP_MODULE_DECLARE_DATA deflate_module = {
1385     STANDARD20_MODULE_STUFF,
1386     NULL, /* dir config creater */
1387     NULL, /* dir merger --- default is to override */
1388     create_deflate_server_config, /* server config */
1389     NULL, /* merge server config */
1390     deflate_filter_cmds, /* command table */
1391     register_hooks /* register hooks */
1392     };

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2