]> granicus.if.org Git - apache/blobdiff - modules/filters/mod_deflate.c
Stop compressing if we are doing a HEAD request and the content-length filter
[apache] / modules / filters / mod_deflate.c
index ae372e41e83b869988e0d36d9114c7e4c43ed111..c3be180bf7b03723726c609503d38da1651f4be5 100644 (file)
@@ -83,6 +83,92 @@ static const char deflate_magic[2] = { '\037', '\213' };
 #define DEFAULT_MEMLEVEL 9
 #define DEFAULT_BUFFERSIZE 8096
 
+
+/* Check whether a request is gzipped, so we can un-gzip it.
+ * If a request has multiple encodings, we need the gzip
+ * to be the outermost non-identity encoding.
+ */
+static int check_gzip(request_rec *r, apr_table_t *hdrs1, apr_table_t *hdrs2)
+{
+    int found = 0;
+    apr_table_t *hdrs = hdrs1;
+    const char *encoding = apr_table_get(hdrs, "Content-Encoding");
+
+    if (!encoding && (hdrs2 != NULL)) {
+        /* the output filter has two tables and a content_encoding to check */
+        encoding = apr_table_get(hdrs2, "Content-Encoding");
+        hdrs = hdrs2;
+        if (!encoding) {
+            encoding = r->content_encoding;
+            hdrs = NULL;
+        }
+    }
+    if (encoding && *encoding) {
+
+        /* check the usual/simple case first */
+        if (!strcasecmp(encoding, "gzip")
+            || !strcasecmp(encoding, "x-gzip")) {
+            found = 1;
+            if (hdrs) {
+                apr_table_unset(hdrs, "Content-Encoding");
+            }
+            else {
+                r->content_encoding = NULL;
+            }
+        }
+        else if (ap_strchr_c(encoding, ',') != NULL) {
+            /* If the outermost encoding isn't gzip, there's nowt
+             * we can do.  So only check the last non-identity token
+             */
+            char *new_encoding = apr_pstrdup(r->pool, encoding);
+            char *ptr;
+            for(;;) {
+                char *token = ap_strrchr(new_encoding, ',');
+                if (!token) {        /* gzip:identity or other:identity */
+                    if (!strcasecmp(new_encoding, "gzip")
+                        || !strcasecmp(new_encoding, "x-gzip")) {
+                        found = 1;
+                        if (hdrs) {
+                            apr_table_unset(hdrs, "Content-Encoding");
+                        }
+                        else {
+                            r->content_encoding = NULL;
+                        }
+                    }
+                    break; /* seen all tokens */
+                }
+                for (ptr=token+1; apr_isspace(*ptr); ++ptr);
+                if (!strcasecmp(ptr, "gzip")
+                    || !strcasecmp(ptr, "x-gzip")) {
+                    *token = '\0';
+                    if (hdrs) {
+                        apr_table_setn(hdrs, "Content-Encoding", new_encoding);
+                    }
+                    else {
+                        r->content_encoding = new_encoding;
+                    }
+                    found = 1;
+                }
+                else if (!ptr[0] || !strcasecmp(ptr, "identity")) {
+                    *token = '\0';
+                    continue; /* strip the token and find the next one */
+                }
+                break; /* found a non-identity token */
+            }
+        }
+    }
+    /*
+     * If we have dealt with the headers above but content_encoding was set
+     * before sync it with the new value in the hdrs table as
+     * r->content_encoding takes precedence later on in the http_header_filter
+     * and hence would destroy what we have just set in the hdrs table.
+     */
+    if (hdrs && r->content_encoding) {
+        r->content_encoding = apr_table_get(hdrs, "Content-Encoding");
+    }
+    return found;
+}
+
 /* Outputs a long in LSB order to the given file
  * only the bottom 4 bits are required for the deflate file format.
  */
@@ -216,6 +302,7 @@ typedef struct deflate_ctx_t
     int (*libz_end_func)(z_streamp);
     unsigned char *validation_buffer;
     apr_size_t validation_buffer_length;
+    int inflate_init;
 } deflate_ctx;
 
 /* Number of validation bytes (CRC and length) after the compressed data */
@@ -295,6 +382,42 @@ static apr_status_t deflate_ctx_cleanup(void *data)
     return APR_SUCCESS;
 }
 
+/* ETag must be unique among the possible representations, so a change
+ * to content-encoding requires a corresponding change to the ETag.
+ * This routine appends -transform (e.g., -gzip) to the entity-tag
+ * value inside the double-quotes if an ETag has already been set
+ * and its value already contains double-quotes. PR 39727
+ */
+static void deflate_check_etag(request_rec *r, const char *transform)
+{
+    const char *etag = apr_table_get(r->headers_out, "ETag");
+    apr_size_t etaglen;
+
+    if ((etag && ((etaglen = strlen(etag)) > 2))) {
+        if (etag[etaglen - 1] == '"') {
+            apr_size_t transformlen = strlen(transform);
+            char *newtag = apr_palloc(r->pool, etaglen + transformlen + 2);
+            char *d = newtag;
+            char *e = d + etaglen - 1;
+            const char *s = etag;
+
+            for (; d < e; ++d, ++s) {
+                *d = *s;          /* copy etag to newtag up to last quote */
+            }
+            *d++ = '-';           /* append dash to newtag */
+            s = transform;
+            e = d + transformlen;
+            for (; d < e; ++d, ++s) {
+                *d = *s;          /* copy transform to newtag */
+            }
+            *d++ = '"';           /* append quote to newtag */
+            *d   = '\0';          /* null terminate newtag */
+
+            apr_table_setn(r->headers_out, "ETag", newtag);
+        }
+    }   
+}
+
 static apr_status_t deflate_out_filter(ap_filter_t *f,
                                        apr_bucket_brigade *bb)
 {
@@ -306,7 +429,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
 
     /* Do nothing if asked to filter nothing. */
     if (APR_BRIGADE_EMPTY(bb)) {
-        return ap_pass_brigade(f->next, bb);
+        return APR_SUCCESS;
     }
 
     c = ap_get_module_config(r->server->module_config,
@@ -322,16 +445,16 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
         char *token;
         const char *encoding;
 
-        /* only work on main request/no subrequests */
-        if (r->main != NULL) {
-            ap_remove_output_filter(f);
-            return ap_pass_brigade(f->next, bb);
-        }
-
-        /* some browsers might have problems, so set no-gzip
-         * (with browsermatch) for them
+        /*
+         * Only work on main request, not subrequests,
+         * that are not a 204 response with no content
+         * and are not tagged with the no-gzip env variable
+         * and not a partial response to a Range request.
          */
-        if (apr_table_get(r->subprocess_env, "no-gzip")) {
+        if ((r->main != NULL) || (r->status == HTTP_NO_CONTENT) ||
+            apr_table_get(r->subprocess_env, "no-gzip") ||
+            apr_table_get(r->headers_out, "Content-Range")
+           ) {
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
@@ -434,48 +557,47 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             }
         }
 
-        /* For a 304 or 204 response there is no entity included in
-         * the response and hence nothing to deflate. */
-        if (r->status == HTTP_NOT_MODIFIED || r->status == HTTP_NO_CONTENT) {
-            ap_remove_output_filter(f);
-            return ap_pass_brigade(f->next, bb);
-        }
+        /* 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).
+         */
 
-        /* We're cool with filtering this. */
-        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;
+        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);
+            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);
+            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);
+            }
             /*
-             * Remove ourselves as it does not make sense to return:
-             * We are not able to init libz and pass data down the chain
-             * uncompressed.
+             * Register a cleanup function to ensure that we cleanup the internal
+             * libz resources.
              */
-            ap_remove_output_filter(f);
-            return ap_pass_brigade(f->next, bb);
+            apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
+                                      apr_pool_cleanup_null);
         }
+
         /*
-         * Register a cleanup function to ensure that we cleanup the internal
-         * libz resources.
+         * Zlib initialization worked, so we can now change the important
+         * content metadata before sending the response out.
          */
-        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);
-        APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
 
         /* If the entire Content-Encoding is "identity", we can replace it. */
         if (!encoding || !strcasecmp(encoding, "identity")) {
@@ -484,7 +606,25 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
         else {
             apr_table_mergen(r->headers_out, "Content-Encoding", "gzip");
         }
+        /* Fix r->content_encoding if it was set before */
+        if (r->content_encoding) {
+            r->content_encoding = apr_table_get(r->headers_out,
+                                                "Content-Encoding");
+        }
         apr_table_unset(r->headers_out, "Content-Length");
+        apr_table_unset(r->headers_out, "Content-MD5");
+        deflate_check_etag(r, "gzip");
+
+        /* For a 304 response, only change the headers */
+        if (r->status == HTTP_NOT_MODIFIED) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        /* add immortal gzip header */
+        e = apr_bucket_immortal_create(gzip_header, sizeof gzip_header,
+                                       f->c->bucket_alloc);
+        APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
 
         /* initialize deflate output buffer */
         ctx->stream.next_out = ctx->buffer;
@@ -497,6 +637,19 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
         apr_bucket *b;
         apr_size_t len;
 
+        /*
+         * Optimization: If we are a HEAD request and bytes_sent is not zero
+         * it means that we have passed the content-length filter once and
+         * have more data to sent. This means that the content-length filter
+         * could not determine our content-length for the response to the
+         * HEAD request anyway (the associated GET request would deliver the
+         * body in chunked encoding) and we can stop compressing.
+         */
+        if (r->header_only && r->bytes_sent) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
         e = APR_BRIGADE_FIRST(bb);
 
         if (APR_BUCKET_IS_EOS(e)) {
@@ -655,9 +808,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
     c = ap_get_module_config(r->server->module_config, &deflate_module);
 
     if (!ctx) {
-        int found = 0;
-        char *token, deflate_hdr[10];
-        const char *encoding;
+        char deflate_hdr[10];
         apr_size_t len;
 
         /* only work on main request/no subrequests */
@@ -666,6 +817,12 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
             return ap_get_brigade(f->next, bb, mode, block, readbytes);
         }
 
+        /* We can't operate on Content-Ranges */
+        if (apr_table_get(r->headers_in, "Content-Range") != NULL) {
+            ap_remove_input_filter(f);
+            return ap_get_brigade(f->next, bb, mode, block, readbytes);
+        }
+
         /* Check whether request body is gzipped.
          *
          * If it is, we're transforming the contents, invalidating
@@ -673,55 +830,10 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
          *
          * If not, we just remove ourself.
          */
-        encoding = apr_table_get(r->headers_in, "Content-Encoding");
-        if (encoding && *encoding) {
-
-            /* check the usual/simple case first */
-            if (!strcasecmp(encoding, "gzip")
-                || !strcasecmp(encoding, "x-gzip")) {
-                found = 1;
-                apr_table_unset(r->headers_in, "Content-Encoding");
-            }
-            else if (ap_strchr_c(encoding, ',') != NULL) {
-                /* If the outermost encoding isn't gzip, there's nowt
-                 * we can do.  So only check the last non-identity token
-                 */
-                char *new_encoding = apr_pstrdup(r->pool, encoding);
-                char *ptr;
-                for(;;) {
-                    token = ap_strrchr(new_encoding, ',');
-                    if (!token) {        /* gzip:identity or other:identity */
-                        if (!strcasecmp(new_encoding, "gzip")
-                            || !strcasecmp(new_encoding, "x-gzip")) {
-                            apr_table_unset(r->headers_in, "Content-Encoding");
-                            found = 1;
-                        }
-                        break; /* seen all tokens */
-                    }
-                    for (ptr=token+1; apr_isspace(*ptr); ++ptr);
-                    if (!strcasecmp(ptr, "gzip")
-                        || !strcasecmp(ptr, "x-gzip")) {
-                        *token = '\0';
-                        apr_table_setn(r->headers_in, "Content-Encoding",
-                                       new_encoding);
-                        found = 1;
-                    }
-                    else if (!strcasecmp(ptr, "identity")) {
-                        *token = '\0';
-                        continue; /* strip the token and find the next one */
-                    }
-                    break; /* found a non-identity token */
-                }
-            }
-        }
-
-        if (found == 0) {
+        if (check_gzip(r, r->headers_in, NULL) == 0) {
             ap_remove_input_filter(f);
             return ap_get_brigade(f->next, bb, mode, block, readbytes);
         }
-        apr_table_unset(r->headers_in, "Content-Length");
-        apr_table_unset(r->headers_in, "Content-MD5");
-        apr_table_unset(r->headers_in, "Content-Range");
 
         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
@@ -733,6 +845,9 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
             return rv;
         }
 
+        apr_table_unset(r->headers_in, "Content-Length");
+        apr_table_unset(r->headers_in, "Content-MD5");
+
         len = 10;
         rv = apr_brigade_flatten(ctx->bb, deflate_hdr, &len);
         if (rv != APR_SUCCESS) {
@@ -917,14 +1032,13 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
     }
 
     if (!APR_BRIGADE_EMPTY(ctx->proc_bb)) {
-        apr_bucket_brigade *newbb;
-
-        /* May return APR_INCOMPLETE which is fine by us. */
-        apr_brigade_partition(ctx->proc_bb, readbytes, &bkt);
-
-        newbb = apr_brigade_split(ctx->proc_bb, bkt);
-        APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
-        APR_BRIGADE_CONCAT(ctx->proc_bb, newbb);
+        if (apr_brigade_partition(ctx->proc_bb, readbytes, &bkt) == APR_INCOMPLETE) {
+            APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
+        }
+        else {
+            APR_BRIGADE_CONCAT(bb, ctx->proc_bb);
+            apr_brigade_split_ex(bb, bkt, ctx->proc_bb);
+        }
     }
 
     return APR_SUCCESS;
@@ -937,7 +1051,6 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
 {
     int zlib_method;
     int zlib_flags;
-    int inflate_init = 1;
     apr_bucket *e;
     request_rec *r = f->r;
     deflate_ctx *ctx = f->ctx;
@@ -947,55 +1060,42 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
 
     /* Do nothing if asked to filter nothing. */
     if (APR_BRIGADE_EMPTY(bb)) {
-        return ap_pass_brigade(f->next, bb);
+        return APR_SUCCESS;
     }
 
     c = ap_get_module_config(r->server->module_config, &deflate_module);
 
     if (!ctx) {
-        int found = 0;
-        char *token;
-        const char *encoding;
 
-        /* only work on main request/no subrequests */
-        if (!ap_is_initial_req(r)) {
+        /*
+         * Only work on main request, not subrequests,
+         * that are not a 204 response with no content
+         * and not a partial response to a Range request,
+         * and only when Content-Encoding ends in gzip.
+         */
+        if (!ap_is_initial_req(r) || (r->status == HTTP_NO_CONTENT) ||
+            (apr_table_get(r->headers_out, "Content-Range") != NULL) ||
+            (check_gzip(r, r->headers_out, r->err_headers_out) == 0)
+           ) {
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
 
         /*
-         * Let's see what our current Content-Encoding is.
-         * Only inflate if gzip is present.
+         * At this point we have decided to filter the content, so change
+         * important content metadata before sending any response out.
+         * Content-Encoding was already reset by the check_gzip() call.
          */
-        encoding = apr_table_get(r->headers_out, "Content-Encoding");
-        if (encoding) {
-            const char *tmp = encoding;
-
-            token = ap_get_token(r->pool, &tmp, 0);
-            while (token && token[0]) {
-                if (!strcasecmp(token, "gzip")) {
-                    found = 1;
-                    break;
-                }
-                /* Otherwise, skip token */
-                tmp++;
-                token = ap_get_token(r->pool, &tmp, 0);
-            }
-        }
-
-        if (found == 0) {
-            ap_remove_output_filter(f);
-            return ap_pass_brigade(f->next, bb);
-        }
-        apr_table_unset(r->headers_out, "Content-Encoding");
+        apr_table_unset(r->headers_out, "Content-Length");
+        apr_table_unset(r->headers_out, "Content-MD5");
+        deflate_check_etag(r, "gunzip");
 
-        /* No need to inflate HEAD or 204/304 */
-        if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(bb))) {
+        /* For a 304 response, only change the headers */
+        if (r->status == HTTP_NOT_MODIFIED) {
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
 
-
         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
         ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
         ctx->buffer = apr_palloc(r->pool, c->bufferSize);
@@ -1028,13 +1128,11 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
         apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
                                   apr_pool_cleanup_null);
 
-        apr_table_unset(r->headers_out, "Content-Length");
-
         /* initialize inflate output buffer */
         ctx->stream.next_out = ctx->buffer;
         ctx->stream.avail_out = c->bufferSize;
 
-        inflate_init = 0;
+        ctx->inflate_init = 0;
     }
 
     while (!APR_BRIGADE_EMPTY(bb))
@@ -1139,7 +1237,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
         apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
 
         /* first bucket contains zlib header */
-        if (!inflate_init++) {
+        if (!ctx->inflate_init++) {
             if (len < 10) {
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                               "Insufficient data for inflate");
@@ -1303,7 +1401,7 @@ static const command_rec deflate_filter_cmds[] = {
     {NULL}
 };
 
-module AP_MODULE_DECLARE_DATA deflate_module = {
+AP_DECLARE_MODULE(deflate) = {
     STANDARD20_MODULE_STUFF,
     NULL,                         /* dir config creater */
     NULL,                         /* dir merger --- default is to override */