]> granicus.if.org Git - apache/commitdiff
In case zlib initialization fails, make sure we do not modify the Content-*
authorStefan Fritsch <sf@apache.org>
Sun, 14 Feb 2010 20:36:58 +0000 (20:36 +0000)
committerStefan Fritsch <sf@apache.org>
Sun, 14 Feb 2010 20:36:58 +0000 (20:36 +0000)
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

index 2cbbf6bbf61c14e7151281af620652ec32684378..2322ef8e59711bbc205e824966e9753240f4150b 100644 (file)
@@ -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);