| 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 "apr_lib.h"
|
| 41 |
#include "apr_strings.h"
|
| 42 |
#include "apr_general.h"
|
| 43 |
#include "util_filter.h"
|
| 44 |
#include "apr_buckets.h"
|
| 45 |
#include "http_request.h"
|
| 46 |
#define APR_WANT_STRFUNC
|
| 47 |
#include "apr_want.h"
|
| 48 |
|
| 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 |
int compressionlevel;
|
| 59 |
apr_size_t bufferSize;
|
| 60 |
char *note_ratio_name;
|
| 61 |
char *note_input_name;
|
| 62 |
char *note_output_name;
|
| 63 |
} deflate_filter_config;
|
| 64 |
|
| 65 |
/* RFC 1952 Section 2.3 defines the gzip header:
|
| 66 |
*
|
| 67 |
* +---+---+---+---+---+---+---+---+---+---+
|
| 68 |
* |ID1|ID2|CM |FLG| MTIME |XFL|OS |
|
| 69 |
* +---+---+---+---+---+---+---+---+---+---+
|
| 70 |
*/
|
| 71 |
static const char gzip_header[10] =
|
| 72 |
{ '\037', '\213', Z_DEFLATED, 0,
|
| 73 |
0, 0, 0, 0, /* mtime */
|
| 74 |
0, 0x03 /* Unix OS_CODE */
|
| 75 |
};
|
| 76 |
|
| 77 |
/* magic header */
|
| 78 |
static const char deflate_magic[2] = { '\037', '\213' };
|
| 79 |
|
| 80 |
/* windowsize is negative to suppress Zlib header */
|
| 81 |
#define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
|
| 82 |
#define DEFAULT_WINDOWSIZE -15
|
| 83 |
#define DEFAULT_MEMLEVEL 9
|
| 84 |
#define DEFAULT_BUFFERSIZE 8096
|
| 85 |
|
| 86 |
|
| 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 |
static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
|
| 92 |
{
|
| 93 |
int found = 0;
|
| 94 |
apr_table_t *hdrs = hdrs1;
|
| 95 |
const char *encoding = apr_table_get(hdrs, "Content-Encoding");
|
| 96 |
|
| 97 |
if (!encoding && (hdrs2 != NULL)) {
|
| 98 |
/* the output filter has two tables and a content_encoding to check */
|
| 99 |
encoding = apr_table_get(hdrs2, "Content-Encoding");
|
| 100 |
hdrs = hdrs2;
|
| 101 |
if (!encoding) {
|
| 102 |
encoding = r->content_encoding;
|
| 103 |
hdrs = NULL;
|
| 104 |
}
|
| 105 |
}
|
| 106 |
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 |
if (hdrs) {
|
| 113 |
apr_table_unset(hdrs, "Content-Encoding");
|
| 114 |
}
|
| 115 |
else {
|
| 116 |
r->content_encoding = NULL;
|
| 117 |
}
|
| 118 |
}
|
| 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 |
char *new_encoding = apr_pstrdup(r->pool, encoding);
|
| 124 |
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 |
if (hdrs) {
|
| 132 |
apr_table_unset(hdrs, "Content-Encoding");
|
| 133 |
}
|
| 134 |
else {
|
| 135 |
r->content_encoding = NULL;
|
| 136 |
}
|
| 137 |
}
|
| 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 |
if (hdrs) {
|
| 145 |
apr_table_setn(hdrs, "Content-Encoding", new_encoding);
|
| 146 |
}
|
| 147 |
else {
|
| 148 |
r->content_encoding = new_encoding;
|
| 149 |
}
|
| 150 |
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 |
/*
|
| 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 |
return found;
|
| 170 |
}
|
| 171 |
|
| 172 |
/* 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 |
static void putLong(unsigned char *string, unsigned long x)
|
| 176 |
{
|
| 177 |
string[0] = (unsigned char)(x & 0xff);
|
| 178 |
string[1] = (unsigned char)((x & 0xff00) >> 8);
|
| 179 |
string[2] = (unsigned char)((x & 0xff0000) >> 16);
|
| 180 |
string[3] = (unsigned char)((x & 0xff000000) >> 24);
|
| 181 |
}
|
| 182 |
|
| 183 |
/* Inputs a string and returns a long.
|
| 184 |
*/
|
| 185 |
static unsigned long getLong(unsigned char *string)
|
| 186 |
{
|
| 187 |
return ((unsigned long)string[0])
|
| 188 |
| (((unsigned long)string[1]) << 8)
|
| 189 |
| (((unsigned long)string[2]) << 16)
|
| 190 |
| (((unsigned long)string[3]) << 24);
|
| 191 |
}
|
| 192 |
|
| 193 |
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 |
c->bufferSize = DEFAULT_BUFFERSIZE;
|
| 200 |
c->compressionlevel = DEFAULT_COMPRESSION;
|
| 201 |
|
| 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 |
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 |
int n = atoi(arg);
|
| 228 |
|
| 229 |
if (n <= 0) {
|
| 230 |
return "DeflateBufferSize should be positive";
|
| 231 |
}
|
| 232 |
|
| 233 |
c->bufferSize = (apr_size_t)n;
|
| 234 |
|
| 235 |
return NULL;
|
| 236 |
}
|
| 237 |
static const char *deflate_set_note(cmd_parms *cmd, void *dummy,
|
| 238 |
const char *arg1, const char *arg2)
|
| 239 |
{
|
| 240 |
deflate_filter_config *c = ap_get_module_config(cmd->server->module_config,
|
| 241 |
&deflate_module);
|
| 242 |
|
| 243 |
if (arg2 == NULL) {
|
| 244 |
c->note_ratio_name = apr_pstrdup(cmd->pool, arg1);
|
| 245 |
}
|
| 246 |
else if (!strcasecmp(arg1, "ratio")) {
|
| 247 |
c->note_ratio_name = apr_pstrdup(cmd->pool, arg2);
|
| 248 |
}
|
| 249 |
else if (!strcasecmp(arg1, "input")) {
|
| 250 |
c->note_input_name = apr_pstrdup(cmd->pool, arg2);
|
| 251 |
}
|
| 252 |
else if (!strcasecmp(arg1, "output")) {
|
| 253 |
c->note_output_name = apr_pstrdup(cmd->pool, arg2);
|
| 254 |
}
|
| 255 |
else {
|
| 256 |
return apr_psprintf(cmd->pool, "Unknown note type %s", arg1);
|
| 257 |
}
|
| 258 |
|
| 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 |
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 |
typedef struct deflate_ctx_t
|
| 297 |
{
|
| 298 |
z_stream stream;
|
| 299 |
unsigned char *buffer;
|
| 300 |
unsigned long crc;
|
| 301 |
apr_bucket_brigade *bb, *proc_bb;
|
| 302 |
int (*libz_end_func)(z_streamp);
|
| 303 |
unsigned char *validation_buffer;
|
| 304 |
apr_size_t validation_buffer_length;
|
| 305 |
int inflate_init;
|
| 306 |
} deflate_ctx;
|
| 307 |
|
| 308 |
/* Number of validation bytes (CRC and length) after the compressed data */
|
| 309 |
#define VALIDATION_SIZE 8
|
| 310 |
/* Do not update ctx->crc, see comment in flush_libz_buffer */
|
| 311 |
#define NO_UPDATE_CRC 0
|
| 312 |
/* Do update ctx->crc, see comment in flush_libz_buffer */
|
| 313 |
#define UPDATE_CRC 1
|
| 314 |
|
| 315 |
static int flush_libz_buffer(deflate_ctx *ctx, deflate_filter_config *c,
|
| 316 |
struct apr_bucket_alloc_t *bucket_alloc,
|
| 317 |
int (*libz_func)(z_streamp, int), int flush,
|
| 318 |
int crc)
|
| 319 |
{
|
| 320 |
int zRC = Z_OK;
|
| 321 |
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 |
/*
|
| 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 |
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 |
zRC = libz_func(&ctx->stream, flush);
|
| 350 |
|
| 351 |
/*
|
| 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 |
zRC = Z_OK;
|
| 365 |
break;
|
| 366 |
}
|
| 367 |
|
| 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 |
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 |
/* PR 39727: we're screwing up our clients if we leave a strong ETag
|
| 385 |
* header while transforming content. Henrik Nordstrom suggests
|
| 386 |
* appending ";gzip".
|
| 387 |
*
|
| 388 |
* Pending a more thorough review of our Etag handling, let's just
|
| 389 |
* implement his suggestion. It fixes the bug, or at least turns it
|
| 390 |
* from a showstopper to an inefficiency. And it breaks nothing that
|
| 391 |
* wasn't already broken.
|
| 392 |
*/
|
| 393 |
static void deflate_check_etag(request_rec *r, const char *transform)
|
| 394 |
{
|
| 395 |
const char *etag = apr_table_get(r->headers_out, "ETag");
|
| 396 |
if (etag && (((etag[0] != 'W') && (etag[0] !='w')) || (etag[1] != '/'))) {
|
| 397 |
apr_table_set(r->headers_out, "ETag",
|
| 398 |
apr_pstrcat(r->pool, etag, "-", transform, NULL));
|
| 399 |
}
|
| 400 |
}
|
| 401 |
static apr_status_t deflate_out_filter(ap_filter_t *f,
|
| 402 |
apr_bucket_brigade *bb)
|
| 403 |
{
|
| 404 |
apr_bucket *e;
|
| 405 |
request_rec *r = f->r;
|
| 406 |
deflate_ctx *ctx = f->ctx;
|
| 407 |
int zRC;
|
| 408 |
deflate_filter_config *c;
|
| 409 |
|
| 410 |
/* Do nothing if asked to filter nothing. */
|
| 411 |
if (APR_BRIGADE_EMPTY(bb)) {
|
| 412 |
return ap_pass_brigade(f->next, bb);
|
| 413 |
}
|
| 414 |
|
| 415 |
c = ap_get_module_config(r->server->module_config,
|
| 416 |
&deflate_module);
|
| 417 |
|
| 418 |
/* If we don't have a context, we need to ensure that it is okay to send
|
| 419 |
* the deflated content. If we have a context, that means we've done
|
| 420 |
* this before and we liked it.
|
| 421 |
* This could be not so nice if we always fail. But, if we succeed,
|
| 422 |
* we're in better shape.
|
| 423 |
*/
|
| 424 |
if (!ctx) {
|
| 425 |
char *token;
|
| 426 |
const char *encoding;
|
| 427 |
|
| 428 |
/* only work on main request/no subrequests */
|
| 429 |
if (r->main != NULL) {
|
| 430 |
ap_remove_output_filter(f);
|
| 431 |
return ap_pass_brigade(f->next, bb);
|
| 432 |
}
|
| 433 |
|
| 434 |
/* some browsers might have problems, so set no-gzip
|
| 435 |
* (with browsermatch) for them
|
| 436 |
*/
|
| 437 |
if (apr_table_get(r->subprocess_env, "no-gzip")) {
|
| 438 |
ap_remove_output_filter(f);
|
| 439 |
return ap_pass_brigade(f->next, bb);
|
| 440 |
}
|
| 441 |
|
| 442 |
/* We can't operate on Content-Ranges */
|
| 443 |
if (apr_table_get(r->headers_out, "Content-Range") != NULL) {
|
| 444 |
ap_remove_output_filter(f);
|
| 445 |
return ap_pass_brigade(f->next, bb);
|
| 446 |
}
|
| 447 |
|
| 448 |
/* Some browsers might have problems with content types
|
| 449 |
* other than text/html, so set gzip-only-text/html
|
| 450 |
* (with browsermatch) for them
|
| 451 |
*/
|
| 452 |
if (r->content_type == NULL
|
| 453 |
|| strncmp(r->content_type, "text/html", 9)) {
|
| 454 |
const char *env_value = apr_table_get(r->subprocess_env,
|
| 455 |
"gzip-only-text/html");
|
| 456 |
if ( env_value && (strcmp(env_value,"1") == 0) ) {
|
| 457 |
ap_remove_output_filter(f);
|
| 458 |
return ap_pass_brigade(f->next, bb);
|
| 459 |
}
|
| 460 |
}
|
| 461 |
|
| 462 |
/* Let's see what our current Content-Encoding is.
|
| 463 |
* If it's already encoded, don't compress again.
|
| 464 |
* (We could, but let's not.)
|
| 465 |
*/
|
| 466 |
encoding = apr_table_get(r->headers_out, "Content-Encoding");
|
| 467 |
if (encoding) {
|
| 468 |
const char *err_enc;
|
| 469 |
|
| 470 |
err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
|
| 471 |
if (err_enc) {
|
| 472 |
encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
|
| 473 |
}
|
| 474 |
}
|
| 475 |
else {
|
| 476 |
encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
|
| 477 |
}
|
| 478 |
|
| 479 |
if (r->content_encoding) {
|
| 480 |
encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
|
| 481 |
r->content_encoding, NULL)
|
| 482 |
: r->content_encoding;
|
| 483 |
}
|
| 484 |
|
| 485 |
if (encoding) {
|
| 486 |
const char *tmp = encoding;
|
| 487 |
|
| 488 |
token = ap_get_token(r->pool, &tmp, 0);
|
| 489 |
while (token && *token) {
|
| 490 |
/* stolen from mod_negotiation: */
|
| 491 |
if (strcmp(token, "identity") && strcmp(token, "7bit") &&
|
| 492 |
strcmp(token, "8bit") && strcmp(token, "binary")) {
|
| 493 |
|
| 494 |
ap_remove_output_filter(f);
|
| 495 |
return ap_pass_brigade(f->next, bb);
|
| 496 |
}
|
| 497 |
|
| 498 |
/* Otherwise, skip token */
|
| 499 |
if (*tmp) {
|
| 500 |
++tmp;
|
| 501 |
}
|
| 502 |
token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
|
| 503 |
}
|
| 504 |
}
|
| 505 |
|
| 506 |
/* Even if we don't accept this request based on it not having
|
| 507 |
* the Accept-Encoding, we need to note that we were looking
|
| 508 |
* for this header and downstream proxies should be aware of that.
|
| 509 |
*/
|
| 510 |
apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
|
| 511 |
|
| 512 |
/* force-gzip will just force it out regardless if the browser
|
| 513 |
* can actually do anything with it.
|
| 514 |
*/
|
| 515 |
if (!apr_table_get(r->subprocess_env, "force-gzip")) {
|
| 516 |
const char *accepts;
|
| 517 |
/* if they don't have the line, then they can't play */
|
| 518 |
accepts = apr_table_get(r->headers_in, "Accept-Encoding");
|
| 519 |
if (accepts == NULL) {
|
| 520 |
ap_remove_output_filter(f);
|
| 521 |
return ap_pass_brigade(f->next, bb);
|
| 522 |
}
|
| 523 |
|
| 524 |
token = ap_get_token(r->pool, &accepts, 0);
|
| 525 |
while (token && token[0] && strcasecmp(token, "gzip")) {
|
| 526 |
/* skip parameters, XXX: ;q=foo evaluation? */
|
| 527 |
while (*accepts == ';') {
|
| 528 |
++accepts;
|
| 529 |
token = ap_get_token(r->pool, &accepts, 1);
|
| 530 |
}
|
| 531 |
|
| 532 |
/* retrieve next token */
|
| 533 |
if (*accepts == ',') {
|
| 534 |
++accepts;
|
| 535 |
}
|
| 536 |
token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
|
| 537 |
}
|
| 538 |
|
| 539 |
/* No acceptable token found. */
|
| 540 |
if (token == NULL || token[0] == '\0') {
|
| 541 |
ap_remove_output_filter(f);
|
| 542 |
return ap_pass_brigade(f->next, bb);
|
| 543 |
}
|
| 544 |
}
|
| 545 |
|
| 546 |
/* For a 304 or 204 response there is no entity included in
|
| 547 |
* the response and hence nothing to deflate. */
|
| 548 |
if (r->status == HTTP_NOT_MODIFIED || r->status == HTTP_NO_CONTENT) {
|
| 549 |
ap_remove_output_filter(f);
|
| 550 |
return ap_pass_brigade(f->next, bb);
|
| 551 |
}
|
| 552 |
|
| 553 |
/* We're cool with filtering this. */
|
| 554 |
ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
|
| 555 |
ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
|
| 556 |
ctx->buffer = apr_palloc(r->pool, c->bufferSize);
|
| 557 |
ctx->libz_end_func = deflateEnd;
|
| 558 |
|
| 559 |
zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED,
|
| 560 |
c->windowSize, c->memlevel,
|
| 561 |
Z_DEFAULT_STRATEGY);
|
| 562 |
|
| 563 |
if (zRC != Z_OK) {
|
| 564 |
deflateEnd(&ctx->stream);
|
| 565 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 566 |
"unable to init Zlib: "
|
| 567 |
"deflateInit2 returned %d: URL %s",
|
| 568 |
zRC, r->uri);
|
| 569 |
/*
|
| 570 |
* Remove ourselves as it does not make sense to return:
|
| 571 |
* We are not able to init libz and pass data down the chain
|
| 572 |
* uncompressed.
|
| 573 |
*/
|
| 574 |
ap_remove_output_filter(f);
|
| 575 |
return ap_pass_brigade(f->next, bb);
|
| 576 |
}
|
| 577 |
/*
|
| 578 |
* Register a cleanup function to ensure that we cleanup the internal
|
| 579 |
* libz resources.
|
| 580 |
*/
|
| 581 |
apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
|
| 582 |
apr_pool_cleanup_null);
|
| 583 |
|
| 584 |
/* add immortal gzip header */
|
| 585 |
e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
|
| 586 |
f->c->bucket_alloc);
|
| 587 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
|
| 588 |
|
| 589 |
/* If the entire Content-Encoding is "identity", we can replace it. */
|
| 590 |
if (!encoding || !strcasecmp(encoding, "identity")) {
|
| 591 |
apr_table_setn(r->headers_out, "Content-Encoding", "gzip");
|
| 592 |
}
|
| 593 |
else {
|
| 594 |
apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
|
| 595 |
}
|
| 596 |
/* Fix r->content_encoding if it was set before */
|
| 597 |
if (r->content_encoding) {
|
| 598 |
r->content_encoding = apr_table_get(r->headers_out,
|
| 599 |
"Content-Encoding");
|
| 600 |
}
|
| 601 |
apr_table_unset(r->headers_out, "Content-Length");
|
| 602 |
apr_table_unset(r->headers_out, "Content-MD5");
|
| 603 |
deflate_check_etag(r, "gzip");
|
| 604 |
|
| 605 |
/* initialize deflate output buffer */
|
| 606 |
ctx->stream.next_out = ctx->buffer;
|
| 607 |
ctx->stream.avail_out = c->bufferSize;
|
| 608 |
}
|
| 609 |
|
| 610 |
while (!APR_BRIGADE_EMPTY(bb))
|
| 611 |
{
|
| 612 |
const char *data;
|
| 613 |
apr_bucket *b;
|
| 614 |
apr_size_t len;
|
| 615 |
|
| 616 |
e = APR_BRIGADE_FIRST(bb);
|
| 617 |
|
| 618 |
if (APR_BUCKET_IS_EOS(e)) {
|
| 619 |
char *buf;
|
| 620 |
|
| 621 |
ctx->stream.avail_in = 0; /* should be zero already anyway */
|
| 622 |
/* flush the remaining data from the zlib buffers */
|
| 623 |
flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate, Z_FINISH,
|
| 624 |
NO_UPDATE_CRC);
|
| 625 |
|
| 626 |
buf = apr_palloc(r->pool, VALIDATION_SIZE);
|
| 627 |
putLong((unsigned char *)&buf[0], ctx->crc);
|
| 628 |
putLong((unsigned char *)&buf[4], ctx->stream.total_in);
|
| 629 |
|
| 630 |
b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
|
| 631 |
f->c->bucket_alloc);
|
| 632 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
|
| 633 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
|
| 634 |
"Zlib: Compressed %ld to %ld : URL %s",
|
| 635 |
ctx->stream.total_in, ctx->stream.total_out, r->uri);
|
| 636 |
|
| 637 |
/* leave notes for logging */
|
| 638 |
if (c->note_input_name) {
|
| 639 |
apr_table_setn(r->notes, c->note_input_name,
|
| 640 |
(ctx->stream.total_in > 0)
|
| 641 |
? apr_off_t_toa(r->pool,
|
| 642 |
ctx->stream.total_in)
|
| 643 |
: "-");
|
| 644 |
}
|
| 645 |
|
| 646 |
if (c->note_output_name) {
|
| 647 |
apr_table_setn(r->notes, c->note_output_name,
|
| 648 |
(ctx->stream.total_in > 0)
|
| 649 |
? apr_off_t_toa(r->pool,
|
| 650 |
ctx->stream.total_out)
|
| 651 |
: "-");
|
| 652 |
}
|
| 653 |
|
| 654 |
if (c->note_ratio_name) {
|
| 655 |
apr_table_setn(r->notes, c->note_ratio_name,
|
| 656 |
(ctx->stream.total_in > 0)
|
| 657 |
? apr_itoa(r->pool,
|
| 658 |
(int)(ctx->stream.total_out
|
| 659 |
* 100
|
| 660 |
/ ctx->stream.total_in))
|
| 661 |
: "-");
|
| 662 |
}
|
| 663 |
|
| 664 |
deflateEnd(&ctx->stream);
|
| 665 |
/* No need for cleanup any longer */
|
| 666 |
apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
|
| 667 |
|
| 668 |
/* Remove EOS from the old list, and insert into the new. */
|
| 669 |
APR_BUCKET_REMOVE(e);
|
| 670 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
|
| 671 |
|
| 672 |
/* Okay, we've seen the EOS.
|
| 673 |
* Time to pass it along down the chain.
|
| 674 |
*/
|
| 675 |
return ap_pass_brigade(f->next, ctx->bb);
|
| 676 |
}
|
| 677 |
|
| 678 |
if (APR_BUCKET_IS_FLUSH(e)) {
|
| 679 |
apr_status_t rv;
|
| 680 |
|
| 681 |
/* flush the remaining data from the zlib buffers */
|
| 682 |
zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
|
| 683 |
Z_SYNC_FLUSH, NO_UPDATE_CRC);
|
| 684 |
if (zRC != Z_OK) {
|
| 685 |
return APR_EGENERAL;
|
| 686 |
}
|
| 687 |
|
| 688 |
/* Remove flush bucket from old brigade anf insert into the new. */
|
| 689 |
APR_BUCKET_REMOVE(e);
|
| 690 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
|
| 691 |
rv = ap_pass_brigade(f->next, ctx->bb);
|
| 692 |
if (rv != APR_SUCCESS) {
|
| 693 |
return rv;
|
| 694 |
}
|
| 695 |
continue;
|
| 696 |
}
|
| 697 |
|
| 698 |
if (APR_BUCKET_IS_METADATA(e)) {
|
| 699 |
/*
|
| 700 |
* Remove meta data bucket from old brigade and insert into the
|
| 701 |
* new.
|
| 702 |
*/
|
| 703 |
APR_BUCKET_REMOVE(e);
|
| 704 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
|
| 705 |
continue;
|
| 706 |
}
|
| 707 |
|
| 708 |
/* read */
|
| 709 |
apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
|
| 710 |
|
| 711 |
/* This crc32 function is from zlib. */
|
| 712 |
ctx->crc = crc32(ctx->crc, (const Bytef *)data, len);
|
| 713 |
|
| 714 |
/* write */
|
| 715 |
ctx->stream.next_in = (unsigned char *)data; /* We just lost const-ness,
|
| 716 |
* but we'll just have to
|
| 717 |
* trust zlib */
|
| 718 |
ctx->stream.avail_in = len;
|
| 719 |
|
| 720 |
while (ctx->stream.avail_in != 0) {
|
| 721 |
if (ctx->stream.avail_out == 0) {
|
| 722 |
apr_status_t rv;
|
| 723 |
|
| 724 |
ctx->stream.next_out = ctx->buffer;
|
| 725 |
len = c->bufferSize - ctx->stream.avail_out;
|
| 726 |
|
| 727 |
b = apr_bucket_heap_create((char *)ctx->buffer, len,
|
| 728 |
NULL, f->c->bucket_alloc);
|
| 729 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
|
| 730 |
ctx->stream.avail_out = c->bufferSize;
|
| 731 |
/* Send what we have right now to the next filter. */
|
| 732 |
rv = ap_pass_brigade(f->next, ctx->bb);
|
| 733 |
if (rv != APR_SUCCESS) {
|
| 734 |
return rv;
|
| 735 |
}
|
| 736 |
}
|
| 737 |
|
| 738 |
zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
|
| 739 |
|
| 740 |
if (zRC != Z_OK) {
|
| 741 |
return APR_EGENERAL;
|
| 742 |
}
|
| 743 |
}
|
| 744 |
|
| 745 |
apr_bucket_delete(e);
|
| 746 |
}
|
| 747 |
|
| 748 |
apr_brigade_cleanup(bb);
|
| 749 |
return APR_SUCCESS;
|
| 750 |
}
|
| 751 |
|
| 752 |
/* This is the deflate input filter (inflates). */
|
| 753 |
static apr_status_t deflate_in_filter(ap_filter_t *f,
|
| 754 |
apr_bucket_brigade *bb,
|
| 755 |
ap_input_mode_t mode,
|
| 756 |
apr_read_type_e block,
|
| 757 |
apr_off_t readbytes)
|
| 758 |
{
|
| 759 |
apr_bucket *bkt;
|
| 760 |
request_rec *r = f->r;
|
| 761 |
deflate_ctx *ctx = f->ctx;
|
| 762 |
int zRC;
|
| 763 |
apr_status_t rv;
|
| 764 |
deflate_filter_config *c;
|
| 765 |
|
| 766 |
/* just get out of the way of things we don't want. */
|
| 767 |
if (mode != AP_MODE_READBYTES) {
|
| 768 |
return ap_get_brigade(f->next, bb, mode, block, readbytes);
|
| 769 |
}
|
| 770 |
|
| 771 |
c = ap_get_module_config(r->server->module_config, &deflate_module);
|
| 772 |
|
| 773 |
if (!ctx) {
|
| 774 |
char deflate_hdr[10];
|
| 775 |
apr_size_t len;
|
| 776 |
|
| 777 |
/* only work on main request/no subrequests */
|
| 778 |
if (!ap_is_initial_req(r)) {
|
| 779 |
ap_remove_input_filter(f);
|
| 780 |
return ap_get_brigade(f->next, bb, mode, block, readbytes);
|
| 781 |
}
|
| 782 |
|
| 783 |
/* We can't operate on Content-Ranges */
|
| 784 |
if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
|
| 785 |
ap_remove_input_filter(f);
|
| 786 |
return ap_get_brigade(f->next, bb, mode, block, readbytes);
|
| 787 |
}
|
| 788 |
|
| 789 |
/* Check whether request body is gzipped.
|
| 790 |
*
|
| 791 |
* If it is, we're transforming the contents, invalidating
|
| 792 |
* some request headers including Content-Encoding.
|
| 793 |
*
|
| 794 |
* If not, we just remove ourself.
|
| 795 |
*/
|
| 796 |
if (check_gzip(r, r->headers_in, NULL) == 0) {
|
| 797 |
ap_remove_input_filter(f);
|
| 798 |
return ap_get_brigade(f->next, bb, mode, block, readbytes);
|
| 799 |
}
|
| 800 |
|
| 801 |
f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
|
| 802 |
ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
|
| 803 |
ctx->proc_bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
|
| 804 |
ctx->buffer = apr_palloc(r->pool, c->bufferSize);
|
| 805 |
|
| 806 |
rv = ap_get_brigade(f->next, ctx->bb, AP_MODE_READBYTES, block, 10);
|
| 807 |
if (rv != APR_SUCCESS) {
|
| 808 |
return rv;
|
| 809 |
}
|
| 810 |
|
| 811 |
apr_table_unset(r->headers_in, "Content-Length");
|
| 812 |
apr_table_unset(r->headers_in, "Content-MD5");
|
| 813 |
|
| 814 |
len = 10;
|
| 815 |
rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
|
| 816 |
if (rv != APR_SUCCESS) {
|
| 817 |
return rv;
|
| 818 |
}
|
| 819 |
|
| 820 |
/* We didn't get the magic bytes. */
|
| 821 |
if (len != 10 ||
|
| 822 |
deflate_hdr[0] != deflate_magic[0] ||
|
| 823 |
deflate_hdr[1] != deflate_magic[1]) {
|
| 824 |
return APR_EGENERAL;
|
| 825 |
}
|
| 826 |
|
| 827 |
/* We can't handle flags for now. */
|
| 828 |
if (deflate_hdr[3] != 0) {
|
| 829 |
return APR_EGENERAL;
|
| 830 |
}
|
| 831 |
|
| 832 |
zRC = inflateInit2(&ctx->stream, c->windowSize);
|
| 833 |
|
| 834 |
if (zRC != Z_OK) {
|
| 835 |
f->ctx = NULL;
|
| 836 |
inflateEnd(&ctx->stream);
|
| 837 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 838 |
"unable to init Zlib: "
|
| 839 |
"inflateInit2 returned %d: URL %s",
|
| 840 |
zRC, r->uri);
|
| 841 |
ap_remove_input_filter(f);
|
| 842 |
return ap_get_brigade(f->next, bb, mode, block, readbytes);
|
| 843 |
}
|
| 844 |
|
| 845 |
/* initialize deflate output buffer */
|
| 846 |
ctx->stream.next_out = ctx->buffer;
|
| 847 |
ctx->stream.avail_out = c->bufferSize;
|
| 848 |
|
| 849 |
apr_brigade_cleanup(ctx->bb);
|
| 850 |
}
|
| 851 |
|
| 852 |
if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
|
| 853 |
rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
|
| 854 |
|
| 855 |
if (rv != APR_SUCCESS) {
|
| 856 |
/* What about APR_EAGAIN errors? */
|
| 857 |
inflateEnd(&ctx->stream);
|
| 858 |
return rv;
|
| 859 |
}
|
| 860 |
|
| 861 |
for (bkt = APR_BRIGADE_FIRST(ctx->bb);
|
| 862 |
bkt != APR_BRIGADE_SENTINEL(ctx->bb);
|
| 863 |
bkt = APR_BUCKET_NEXT(bkt))
|
| 864 |
{
|
| 865 |
const char *data;
|
| 866 |
apr_size_t len;
|
| 867 |
|
| 868 |
/* If we actually see the EOS, that means we screwed up! */
|
| 869 |
if (APR_BUCKET_IS_EOS(bkt)) {
|
| 870 |
inflateEnd(&ctx->stream);
|
| 871 |
return APR_EGENERAL;
|
| 872 |
}
|
| 873 |
|
| 874 |
if (APR_BUCKET_IS_FLUSH(bkt)) {
|
| 875 |
apr_bucket *tmp_heap;
|
| 876 |
zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
|
| 877 |
if (zRC != Z_OK) {
|
| 878 |
inflateEnd(&ctx->stream);
|
| 879 |
return APR_EGENERAL;
|
| 880 |
}
|
| 881 |
|
| 882 |
ctx->stream.next_out = ctx->buffer;
|
| 883 |
len = c->bufferSize - ctx->stream.avail_out;
|
| 884 |
|
| 885 |
ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
|
| 886 |
tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
|
| 887 |
NULL, f->c->bucket_alloc);
|
| 888 |
APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
|
| 889 |
ctx->stream.avail_out = c->bufferSize;
|
| 890 |
|
| 891 |
/* Move everything to the returning brigade. */
|
| 892 |
APR_BUCKET_REMOVE(bkt);
|
| 893 |
APR_BRIGADE_CONCAT(bb, ctx->bb);
|
| 894 |
break;
|
| 895 |
}
|
| 896 |
|
| 897 |
/* read */
|
| 898 |
apr_bucket_read(bkt, &data, &len, APR_BLOCK_READ);
|
| 899 |
|
| 900 |
/* pass through zlib inflate. */
|
| 901 |
ctx->stream.next_in = (unsigned char *)data;
|
| 902 |
ctx->stream.avail_in = len;
|
| 903 |
|
| 904 |
zRC = Z_OK;
|
| 905 |
|
| 906 |
while (ctx->stream.avail_in != 0) {
|
| 907 |
if (ctx->stream.avail_out == 0) {
|
| 908 |
apr_bucket *tmp_heap;
|
| 909 |
ctx->stream.next_out = ctx->buffer;
|
| 910 |
len = c->bufferSize - ctx->stream.avail_out;
|
| 911 |
|
| 912 |
ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
|
| 913 |
tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
|
| 914 |
NULL, f->c->bucket_alloc);
|
| 915 |
APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
|
| 916 |
ctx->stream.avail_out = c->bufferSize;
|
| 917 |
}
|
| 918 |
|
| 919 |
zRC = inflate(&ctx->stream, Z_NO_FLUSH);
|
| 920 |
|
| 921 |
if (zRC == Z_STREAM_END) {
|
| 922 |
break;
|
| 923 |
}
|
| 924 |
|
| 925 |
if (zRC != Z_OK) {
|
| 926 |
inflateEnd(&ctx->stream);
|
| 927 |
return APR_EGENERAL;
|
| 928 |
}
|
| 929 |
}
|
| 930 |
if (zRC == Z_STREAM_END) {
|
| 931 |
apr_bucket *tmp_heap, *eos;
|
| 932 |
|
| 933 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
|
| 934 |
"Zlib: Inflated %ld to %ld : URL %s",
|
| 935 |
ctx->stream.total_in, ctx->stream.total_out,
|
| 936 |
r->uri);
|
| 937 |
|
| 938 |
len = c->bufferSize - ctx->stream.avail_out;
|
| 939 |
|
| 940 |
ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
|
| 941 |
tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
|
| 942 |
NULL, f->c->bucket_alloc);
|
| 943 |
APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
|
| 944 |
ctx->stream.avail_out = c->bufferSize;
|
| 945 |
|
| 946 |
/* Is the remaining 8 bytes already in the avail stream? */
|
| 947 |
if (ctx->stream.avail_in >= 8) {
|
| 948 |
unsigned long compCRC, compLen;
|
| 949 |
compCRC = getLong(ctx->stream.next_in);
|
| 950 |
if (ctx->crc != compCRC) {
|
| 951 |
inflateEnd(&ctx->stream);
|
| 952 |
return APR_EGENERAL;
|
| 953 |
}
|
| 954 |
ctx->stream.next_in += 4;
|
| 955 |
compLen = getLong(ctx->stream.next_in);
|
| 956 |
if (ctx->stream.total_out != compLen) {
|
| 957 |
inflateEnd(&ctx->stream);
|
| 958 |
return APR_EGENERAL;
|
| 959 |
}
|
| 960 |
}
|
| 961 |
else {
|
| 962 |
/* FIXME: We need to grab the 8 verification bytes
|
| 963 |
* from the wire! */
|
| 964 |
inflateEnd(&ctx->stream);
|
| 965 |
return APR_EGENERAL;
|
| 966 |
}
|
| 967 |
|
| 968 |
inflateEnd(&ctx->stream);
|
| 969 |
|
| 970 |
eos = apr_bucket_eos_create(f->c->bucket_alloc);
|
| 971 |
APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, eos);
|
| 972 |
break;
|
| 973 |
}
|
| 974 |
|
| 975 |
}
|
| 976 |
apr_brigade_cleanup(ctx->bb);
|
| 977 |
}
|
| 978 |
|
| 979 |
/* If we are about to return nothing for a 'blocking' read and we have
|
| 980 |
* some data in our zlib buffer, flush it out so we can return something.
|
| 981 |
*/
|
| 982 |
if (block == APR_BLOCK_READ &&
|
| 983 |
APR_BRIGADE_EMPTY(ctx->proc_bb) &&
|
| 984 |
ctx->stream.avail_out < c->bufferSize) {
|
| 985 |
apr_bucket *tmp_heap;
|
| 986 |
apr_size_t len;
|
| 987 |
ctx->stream.next_out = ctx->buffer;
|
| 988 |
len = c->bufferSize - ctx->stream.avail_out;
|
| 989 |
|
| 990 |
ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
|
| 991 |
tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
|
| 992 |
NULL, f->c->bucket_alloc);
|
| 993 |
APR_BRIGADE_INSERT_TAIL(ctx->proc_bb, tmp_heap);
|
| 994 |
ctx->stream.avail_out = c->bufferSize;
|
| 995 |
}
|
| 996 |
|
| 997 |
if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
|
| 998 |
apr_bucket_brigade *newbb;
|
| 999 |
|
| 1000 |
/* May return APR_INCOMPLETE which is fine by us. */
|
| 1001 |
apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
|
| 1002 |
|
| 1003 |
newbb = apr_brigade_split(ctx->proc_bb, bkt);
|
| 1004 |
APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
|
| 1005 |
APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
|
| 1006 |
}
|
| 1007 |
|
| 1008 |
return APR_SUCCESS;
|
| 1009 |
}
|
| 1010 |
|
| 1011 |
|
| 1012 |
/* Filter to inflate for a content-transforming proxy. */
|
| 1013 |
static apr_status_t inflate_out_filter(ap_filter_t *f,
|
| 1014 |
apr_bucket_brigade *bb)
|
| 1015 |
{
|
| 1016 |
int zlib_method;
|
| 1017 |
int zlib_flags;
|
| 1018 |
apr_bucket *e;
|
| 1019 |
request_rec *r = f->r;
|
| 1020 |
deflate_ctx *ctx = f->ctx;
|
| 1021 |
int zRC;
|
| 1022 |
apr_status_t rv;
|
| 1023 |
deflate_filter_config *c;
|
| 1024 |
|
| 1025 |
/* Do nothing if asked to filter nothing. */
|
| 1026 |
if (APR_BRIGADE_EMPTY(bb)) {
|
| 1027 |
return ap_pass_brigade(f->next, bb);
|
| 1028 |
}
|
| 1029 |
|
| 1030 |
c = ap_get_module_config(r->server->module_config, &deflate_module);
|
| 1031 |
|
| 1032 |
if (!ctx) {
|
| 1033 |
|
| 1034 |
/* only work on main request/no subrequests */
|
| 1035 |
if (!ap_is_initial_req(r)) {
|
| 1036 |
ap_remove_output_filter(f);
|
| 1037 |
return ap_pass_brigade(f->next, bb);
|
| 1038 |
}
|
| 1039 |
|
| 1040 |
/* We can't operate on Content-Ranges */
|
| 1041 |
if (apr_table_get(r->headers_out, "Content-Range") != NULL) {
|
| 1042 |
ap_remove_output_filter(f);
|
| 1043 |
return ap_pass_brigade(f->next, bb);
|
| 1044 |
}
|
| 1045 |
|
| 1046 |
/*
|
| 1047 |
* Let's see what our current Content-Encoding is.
|
| 1048 |
* Only inflate if gzipped.
|
| 1049 |
*/
|
| 1050 |
if (check_gzip(r, r->headers_out, r->err_headers_out) == 0) {
|
| 1051 |
ap_remove_output_filter(f);
|
| 1052 |
return ap_pass_brigade(f->next, bb);
|
| 1053 |
}
|
| 1054 |
|
| 1055 |
/* No need to inflate HEAD or 204/304 */
|
| 1056 |
if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
|
| 1057 |
ap_remove_output_filter(f);
|
| 1058 |
return ap_pass_brigade(f->next, bb);
|
| 1059 |
}
|
| 1060 |
|
| 1061 |
f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
|
| 1062 |
ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
|
| 1063 |
ctx->buffer = apr_palloc(r->pool, c->bufferSize);
|
| 1064 |
ctx->libz_end_func = inflateEnd;
|
| 1065 |
ctx->validation_buffer = NULL;
|
| 1066 |
ctx->validation_buffer_length = 0;
|
| 1067 |
|
| 1068 |
zRC = inflateInit2(&ctx->stream, c->windowSize);
|
| 1069 |
|
| 1070 |
if (zRC != Z_OK) {
|
| 1071 |
f->ctx = NULL;
|
| 1072 |
inflateEnd(&ctx->stream);
|
| 1073 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 1074 |
"unable to init Zlib: "
|
| 1075 |
"inflateInit2 returned %d: URL %s",
|
| 1076 |
zRC, r->uri);
|
| 1077 |
/*
|
| 1078 |
* Remove ourselves as it does not make sense to return:
|
| 1079 |
* We are not able to init libz and pass data down the chain
|
| 1080 |
* compressed.
|
| 1081 |
*/
|
| 1082 |
ap_remove_output_filter(f);
|
| 1083 |
return ap_pass_brigade(f->next, bb);
|
| 1084 |
}
|
| 1085 |
|
| 1086 |
/*
|
| 1087 |
* Register a cleanup function to ensure that we cleanup the internal
|
| 1088 |
* libz resources.
|
| 1089 |
*/
|
| 1090 |
apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
|
| 1091 |
apr_pool_cleanup_null);
|
| 1092 |
|
| 1093 |
/* these are unlikely to be set anyway, but ... */
|
| 1094 |
apr_table_unset(r->headers_out, "Content-Length");
|
| 1095 |
apr_table_unset(r->headers_out, "Content-MD5");
|
| 1096 |
deflate_check_etag(r, "gunzip");
|
| 1097 |
|
| 1098 |
/* initialize inflate output buffer */
|
| 1099 |
ctx->stream.next_out = ctx->buffer;
|
| 1100 |
ctx->stream.avail_out = c->bufferSize;
|
| 1101 |
|
| 1102 |
ctx->inflate_init = 0;
|
| 1103 |
}
|
| 1104 |
|
| 1105 |
while (!APR_BRIGADE_EMPTY(bb))
|
| 1106 |
{
|
| 1107 |
const char *data;
|
| 1108 |
apr_bucket *b;
|
| 1109 |
apr_size_t len;
|
| 1110 |
|
| 1111 |
e = APR_BRIGADE_FIRST(bb);
|
| 1112 |
|
| 1113 |
if (APR_BUCKET_IS_EOS(e)) {
|
| 1114 |
/*
|
| 1115 |
* We are really done now. Ensure that we never return here, even
|
| 1116 |
* if a second EOS bucket falls down the chain. Thus remove
|
| 1117 |
* ourselves.
|
| 1118 |
*/
|
| 1119 |
ap_remove_output_filter(f);
|
| 1120 |
/* should be zero already anyway */
|
| 1121 |
ctx->stream.avail_in = 0;
|
| 1122 |
/*
|
| 1123 |
* Flush the remaining data from the zlib buffers. It is correct
|
| 1124 |
* to use Z_SYNC_FLUSH in this case and not Z_FINISH as in the
|
| 1125 |
* deflate case. In the inflate case Z_FINISH requires to have a
|
| 1126 |
* large enough output buffer to put ALL data in otherwise it
|
| 1127 |
* fails, whereas in the deflate case you can empty a filled output
|
| 1128 |
* buffer and call it again until no more output can be created.
|
| 1129 |
*/
|
| 1130 |
flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
|
| 1131 |
UPDATE_CRC);
|
| 1132 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
|
| 1133 |
"Zlib: Inflated %ld to %ld : URL %s",
|
| 1134 |
ctx->stream.total_in, ctx->stream.total_out, r->uri);
|
| 1135 |
|
| 1136 |
if (ctx->validation_buffer_length == VALIDATION_SIZE) {
|
| 1137 |
unsigned long compCRC, compLen;
|
| 1138 |
compCRC = getLong(ctx->validation_buffer);
|
| 1139 |
if (ctx->crc != compCRC) {
|
| 1140 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 1141 |
"Zlib: Checksum of inflated stream invalid");
|
| 1142 |
return APR_EGENERAL;
|
| 1143 |
}
|
| 1144 |
ctx->validation_buffer += VALIDATION_SIZE / 2;
|
| 1145 |
compLen = getLong(ctx->validation_buffer);
|
| 1146 |
if (ctx->stream.total_out != compLen) {
|
| 1147 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 1148 |
"Zlib: Length of inflated stream invalid");
|
| 1149 |
return APR_EGENERAL;
|
| 1150 |
}
|
| 1151 |
}
|
| 1152 |
else {
|
| 1153 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 1154 |
"Zlib: Validation bytes not present");
|
| 1155 |
return APR_EGENERAL;
|
| 1156 |
}
|
| 1157 |
|
| 1158 |
inflateEnd(&ctx->stream);
|
| 1159 |
/* No need for cleanup any longer */
|
| 1160 |
apr_pool_cleanup_kill(r->pool, ctx, deflate_ctx_cleanup);
|
| 1161 |
|
| 1162 |
/* Remove EOS from the old list, and insert into the new. */
|
| 1163 |
APR_BUCKET_REMOVE(e);
|
| 1164 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
|
| 1165 |
|
| 1166 |
/*
|
| 1167 |
* Okay, we've seen the EOS.
|
| 1168 |
* Time to pass it along down the chain.
|
| 1169 |
*/
|
| 1170 |
return ap_pass_brigade(f->next, ctx->bb);
|
| 1171 |
}
|
| 1172 |
|
| 1173 |
if (APR_BUCKET_IS_FLUSH(e)) {
|
| 1174 |
apr_status_t rv;
|
| 1175 |
|
| 1176 |
/* flush the remaining data from the zlib buffers */
|
| 1177 |
zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
|
| 1178 |
Z_SYNC_FLUSH, UPDATE_CRC);
|
| 1179 |
if (zRC != Z_OK) {
|
| 1180 |
return APR_EGENERAL;
|
| 1181 |
}
|
| 1182 |
|
| 1183 |
/* Remove flush bucket from old brigade anf insert into the new. */
|
| 1184 |
APR_BUCKET_REMOVE(e);
|
| 1185 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
|
| 1186 |
rv = ap_pass_brigade(f->next, ctx->bb);
|
| 1187 |
if (rv != APR_SUCCESS) {
|
| 1188 |
return rv;
|
| 1189 |
}
|
| 1190 |
continue;
|
| 1191 |
}
|
| 1192 |
|
| 1193 |
if (APR_BUCKET_IS_METADATA(e)) {
|
| 1194 |
/*
|
| 1195 |
* Remove meta data bucket from old brigade and insert into the
|
| 1196 |
* new.
|
| 1197 |
*/
|
| 1198 |
APR_BUCKET_REMOVE(e);
|
| 1199 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
|
| 1200 |
continue;
|
| 1201 |
}
|
| 1202 |
|
| 1203 |
/* read */
|
| 1204 |
apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
|
| 1205 |
|
| 1206 |
/* first bucket contains zlib header */
|
| 1207 |
if (!ctx->inflate_init++) {
|
| 1208 |
if (len < 10) {
|
| 1209 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 1210 |
"Insufficient data for inflate");
|
| 1211 |
return APR_EGENERAL;
|
| 1212 |
}
|
| 1213 |
else {
|
| 1214 |
zlib_method = data[2];
|
| 1215 |
zlib_flags = data[3];
|
| 1216 |
if (zlib_method != Z_DEFLATED) {
|
| 1217 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
|
| 1218 |
"inflate: data not deflated!");
|
| 1219 |
ap_remove_output_filter(f);
|
| 1220 |
return ap_pass_brigade(f->next, bb);
|
| 1221 |
}
|
| 1222 |
if (data[0] != deflate_magic[0] ||
|
| 1223 |
data[1] != deflate_magic[1] ||
|
| 1224 |
(zlib_flags & RESERVED) != 0) {
|
| 1225 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 1226 |
"inflate: bad header");
|
| 1227 |
return APR_EGENERAL ;
|
| 1228 |
}
|
| 1229 |
data += 10 ;
|
| 1230 |
len -= 10 ;
|
| 1231 |
}
|
| 1232 |
if (zlib_flags & EXTRA_FIELD) {
|
| 1233 |
unsigned int bytes = (unsigned int)(data[0]);
|
| 1234 |
bytes += ((unsigned int)(data[1])) << 8;
|
| 1235 |
bytes += 2;
|
| 1236 |
if (len < bytes) {
|
| 1237 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
|
| 1238 |
"inflate: extra field too big (not "
|
| 1239 |
"supported)");
|
| 1240 |
return APR_EGENERAL;
|
| 1241 |
}
|
| 1242 |
data += bytes;
|
| 1243 |
len -= bytes;
|
| 1244 |
}
|
| 1245 |
if (zlib_flags & ORIG_NAME) {
|
| 1246 |
while (len-- && *data++);
|
| 1247 |
}
|
| 1248 |
if (zlib_flags & COMMENT) {
|
| 1249 |
while (len-- && *data++);
|
| 1250 |
}
|
| 1251 |
if (zlib_flags & HEAD_CRC) {
|
| 1252 |
len -= 2;
|
| 1253 |
data += 2;
|
| 1254 |
}
|
| 1255 |
}
|
| 1256 |
|
| 1257 |
/* pass through zlib inflate. */
|
| 1258 |
ctx->stream.next_in = (unsigned char *)data;
|
| 1259 |
ctx->stream.avail_in = len;
|
| 1260 |
|
| 1261 |
if (ctx->validation_buffer) {
|
| 1262 |
if (ctx->validation_buffer_length < VALIDATION_SIZE) {
|
| 1263 |
apr_size_t copy_size;
|
| 1264 |
|
| 1265 |
copy_size = VALIDATION_SIZE - ctx->validation_buffer_length;
|
| 1266 |
if (copy_size > ctx->stream.avail_in)
|
| 1267 |
copy_size = ctx->stream.avail_in;
|
| 1268 |
memcpy(ctx->validation_buffer + ctx->validation_buffer_length,
|
| 1269 |
ctx->stream.next_in, copy_size);
|
| 1270 |
/* Saved copy_size bytes */
|
| 1271 |
ctx->stream.avail_in -= copy_size;
|
| 1272 |
ctx->validation_buffer_length += copy_size;
|
| 1273 |
}
|
| 1274 |
if (ctx->stream.avail_in) {
|
| 1275 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
|
| 1276 |
"Zlib: %d bytes of garbage at the end of "
|
| 1277 |
"compressed stream.", ctx->stream.avail_in);
|
| 1278 |
/*
|
| 1279 |
* There is nothing worth consuming for zlib left, because it is
|
| 1280 |
* either garbage data or the data has been copied to the
|
| 1281 |
* validation buffer (processing validation data is no business
|
| 1282 |
* for zlib). So set ctx->stream.avail_in to zero to indicate
|
| 1283 |
* this to the following while loop.
|
| 1284 |
*/
|
| 1285 |
ctx->stream.avail_in = 0;
|
| 1286 |
}
|
| 1287 |
}
|
| 1288 |
|
| 1289 |
zRC = Z_OK;
|
| 1290 |
|
| 1291 |
while (ctx->stream.avail_in != 0) {
|
| 1292 |
if (ctx->stream.avail_out == 0) {
|
| 1293 |
|
| 1294 |
ctx->stream.next_out = ctx->buffer;
|
| 1295 |
len = c->bufferSize - ctx->stream.avail_out;
|
| 1296 |
|
| 1297 |
ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
|
| 1298 |
b = apr_bucket_heap_create((char *)ctx->buffer, len,
|
| 1299 |
NULL, f->c->bucket_alloc);
|
| 1300 |
APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
|
| 1301 |
ctx->stream.avail_out = c->bufferSize;
|
| 1302 |
/* Send what we have right now to the next filter. */
|
| 1303 |
rv = ap_pass_brigade(f->next, ctx->bb);
|
| 1304 |
if (rv != APR_SUCCESS) {
|
| 1305 |
return rv;
|
| 1306 |
}
|
| 1307 |
}
|
| 1308 |
|
| 1309 |
zRC = inflate(&ctx->stream, Z_NO_FLUSH);
|
| 1310 |
|
| 1311 |
if (zRC == Z_STREAM_END) {
|
| 1312 |
/*
|
| 1313 |
* We have inflated all data. Now try to capture the
|
| 1314 |
* validation bytes. We may not have them all available
|
| 1315 |
* right now, but capture what is there.
|
| 1316 |
*/
|
| 1317 |
ctx->validation_buffer = apr_pcalloc(f->r->pool,
|
| 1318 |
VALIDATION_SIZE);
|
| 1319 |
if (ctx->stream.avail_in > VALIDATION_SIZE) {
|
| 1320 |
ctx->validation_buffer_length = VALIDATION_SIZE;
|
| 1321 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
|
| 1322 |
"Zlib: %d bytes of garbage at the end of "
|
| 1323 |
"compressed stream.",
|
| 1324 |
ctx->stream.avail_in - VALIDATION_SIZE);
|
| 1325 |
} else if (ctx->stream.avail_in > 0) {
|
| 1326 |
ctx->validation_buffer_length = ctx->stream.avail_in;
|
| 1327 |
}
|
| 1328 |
if (ctx->validation_buffer_length)
|
| 1329 |
memcpy(ctx->validation_buffer, ctx->stream.next_in,
|
| 1330 |
ctx->validation_buffer_length);
|
| 1331 |
break;
|
| 1332 |
}
|
| 1333 |
|
| 1334 |
if (zRC != Z_OK) {
|
| 1335 |
return APR_EGENERAL;
|
| 1336 |
}
|
| 1337 |
}
|
| 1338 |
|
| 1339 |
apr_bucket_delete(e);
|
| 1340 |
}
|
| 1341 |
|
| 1342 |
apr_brigade_cleanup(bb);
|
| 1343 |
return APR_SUCCESS;
|
| 1344 |
}
|
| 1345 |
|
| 1346 |
#define PROTO_FLAGS AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH
|
| 1347 |
static void register_hooks(apr_pool_t *p)
|
| 1348 |
{
|
| 1349 |
ap_register_output_filter(deflateFilterName, deflate_out_filter, NULL,
|
| 1350 |
AP_FTYPE_CONTENT_SET);
|
| 1351 |
ap_register_output_filter("INFLATE", inflate_out_filter, NULL,
|
| 1352 |
AP_FTYPE_RESOURCE-1);
|
| 1353 |
ap_register_input_filter(deflateFilterName, deflate_in_filter, NULL,
|
| 1354 |
AP_FTYPE_CONTENT_SET);
|
| 1355 |
}
|
| 1356 |
|
| 1357 |
static const command_rec deflate_filter_cmds[] = {
|
| 1358 |
AP_INIT_TAKE12("DeflateFilterNote", deflate_set_note, NULL, RSRC_CONF,
|
| 1359 |
"Set a note to report on compression ratio"),
|
| 1360 |
AP_INIT_TAKE1("DeflateWindowSize", deflate_set_window_size, NULL,
|
| 1361 |
RSRC_CONF, "Set the Deflate window size (1-15)"),
|
| 1362 |
AP_INIT_TAKE1("DeflateBufferSize", deflate_set_buffer_size, NULL, RSRC_CONF,
|
| 1363 |
"Set the Deflate Buffer Size"),
|
| 1364 |
AP_INIT_TAKE1("DeflateMemLevel", deflate_set_memlevel, NULL, RSRC_CONF,
|
| 1365 |
"Set the Deflate Memory Level (1-9)"),
|
| 1366 |
AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
|
| 1367 |
"Set the Deflate Compression Level (1-9)"),
|
| 1368 |
{NULL}
|
| 1369 |
};
|
| 1370 |
|
| 1371 |
module AP_MODULE_DECLARE_DATA deflate_module = {
|
| 1372 |
STANDARD20_MODULE_STUFF,
|
| 1373 |
NULL, /* dir config creater */
|
| 1374 |
NULL, /* dir merger --- default is to override */
|
| 1375 |
create_deflate_server_config, /* server config */
|
| 1376 |
NULL, /* merge server config */
|
| 1377 |
deflate_filter_cmds, /* command table */
|
| 1378 |
register_hooks /* register hooks */
|
| 1379 |
};
|