From e8a6cb798f248a98a1de30519dbdec1de7d1f149 Mon Sep 17 00:00:00 2001 From: Stefan Fritsch Date: Sun, 14 Feb 2010 20:36:58 +0000 Subject: [PATCH] In case zlib initialization fails, make sure we do not modify the Content-* headers before sending the uncompressed content down the filter chain. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@910069 13f79535-47bb-0310-9956-ffa450edef68 --- modules/filters/mod_deflate.c | 72 +++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/modules/filters/mod_deflate.c b/modules/filters/mod_deflate.c index 2cbbf6bbf6..2322ef8e59 100644 --- a/modules/filters/mod_deflate.c +++ b/modules/filters/mod_deflate.c @@ -557,8 +557,46 @@ static apr_status_t deflate_out_filter(ap_filter_t *f, } } - /* At this point we have decided to filter the content, so change - * important content metadata before sending any response out. + /* At this point we have decided to filter the content. Let's try to + * to initialize zlib (except for 304 responses, where we will only + * send out the headers). + */ + + if (r->status != HTTP_NOT_MODIFIED) { + ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx)); + ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc); + ctx->buffer = apr_palloc(r->pool, c->bufferSize); + ctx->libz_end_func = deflateEnd; + + zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED, + c->windowSize, c->memlevel, + Z_DEFAULT_STRATEGY); + + if (zRC != Z_OK) { + deflateEnd(&ctx->stream); + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, + "unable to init Zlib: " + "deflateInit2 returned %d: URL %s", + zRC, r->uri); + /* + * Remove ourselves as it does not make sense to return: + * We are not able to init libz and pass data down the chain + * uncompressed. + */ + ap_remove_output_filter(f); + return ap_pass_brigade(f->next, bb); + } + /* + * Register a cleanup function to ensure that we cleanup the internal + * libz resources. + */ + apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup, + apr_pool_cleanup_null); + } + + /* + * Zlib initialization worked, so we can now change the important + * content metadata before sending the response out. */ /* If the entire Content-Encoding is "identity", we can replace it. */ @@ -583,36 +621,6 @@ static apr_status_t deflate_out_filter(ap_filter_t *f, return ap_pass_brigade(f->next, bb); } - ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx)); - ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc); - ctx->buffer = apr_palloc(r->pool, c->bufferSize); - ctx->libz_end_func = deflateEnd; - - zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED, - c->windowSize, c->memlevel, - Z_DEFAULT_STRATEGY); - - if (zRC != Z_OK) { - deflateEnd(&ctx->stream); - ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, - "unable to init Zlib: " - "deflateInit2 returned %d: URL %s", - zRC, r->uri); - /* - * Remove ourselves as it does not make sense to return: - * We are not able to init libz and pass data down the chain - * uncompressed. - */ - ap_remove_output_filter(f); - return ap_pass_brigade(f->next, bb); - } - /* - * Register a cleanup function to ensure that we cleanup the internal - * libz resources. - */ - apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup, - apr_pool_cleanup_null); - /* add immortal gzip header */ e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header, f->c->bucket_alloc); -- 2.50.1