]> granicus.if.org Git - apache/blobdiff - modules/filters/mod_deflate.c
Add lots of unique tags to error log messages
[apache] / modules / filters / mod_deflate.c
index 2f186433b755f9ad5b7e9f22483298057ab3b771..0876cb4e3137876acb249a2dfe43170db10def9a 100644 (file)
@@ -416,7 +416,7 @@ static void deflate_check_etag(request_rec *r, const char *transform)
 
             apr_table_setn(r->headers_out, "ETag", newtag);
         }
-    }   
+    }
 }
 
 static apr_status_t deflate_out_filter(ap_filter_t *f,
@@ -426,7 +426,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
     request_rec *r = f->r;
     deflate_ctx *ctx = f->ctx;
     int zRC;
-    apr_size_t len;
+    apr_size_t len = 0, blen;
     const char *data;
     deflate_filter_config *c;
 
@@ -448,26 +448,42 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
         char *token;
         const char *encoding;
 
-        /* Delay initialization until we have seen some data */
-        e = APR_BRIGADE_FIRST(bb);
-        while (1) {
-            apr_status_t rc;
-            if (e == APR_BRIGADE_SENTINEL(bb))
-                return ap_pass_brigade(f->next, bb);
-            if (APR_BUCKET_IS_EOS(e)) {
-                ap_remove_output_filter(f);
-                return ap_pass_brigade(f->next, bb);
-            }
-            if (APR_BUCKET_IS_METADATA(e))
-                continue;
+        /* We have checked above that bb is not empty */
+        e = APR_BRIGADE_LAST(bb);
+        if (APR_BUCKET_IS_EOS(e)) {
+            /*
+             * If we already know the size of the response, we can skip
+             * compression on responses smaller than the compression overhead.
+             * However, if we compress, we must initialize deflate_out before
+             * calling ap_pass_brigade() for the first time.  Otherwise the
+             * headers will be sent to the client without
+             * "Content-Encoding: gzip".
+             */
+            e = APR_BRIGADE_FIRST(bb);
+            while (1) {
+                apr_status_t rc;
+                if (APR_BUCKET_IS_EOS(e)) {
+                    ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
+                                  "Not compressing very small response of %"
+                                  APR_SIZE_T_FMT " bytes", len);
+                    ap_remove_output_filter(f);
+                    return ap_pass_brigade(f->next, bb);
+                }
+                if (APR_BUCKET_IS_METADATA(e)) {
+                    e = APR_BUCKET_NEXT(e);
+                    continue;
+                }
 
-            rc = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
-            if (rc != APR_SUCCESS)
-                return rc;
-            if (len > 0)
-                break;
+                rc = apr_bucket_read(e, &data, &blen, APR_BLOCK_READ);
+                if (rc != APR_SUCCESS)
+                    return rc;
+                len += blen;
+                /* 50 is for Content-Encoding and Vary headers and ETag suffix */
+                if (len > sizeof(gzip_header) + VALIDATION_SIZE + 50)
+                    break;
 
-            e = APR_BUCKET_NEXT(e);
+                e = APR_BUCKET_NEXT(e);
+            }
         }
 
         ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
@@ -482,6 +498,15 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             apr_table_get(r->subprocess_env, "no-gzip") ||
             apr_table_get(r->headers_out, "Content-Range")
            ) {
+            if (APLOG_R_IS_LEVEL(r, APLOG_TRACE1)) {
+                const char *reason =
+                    (r->main != NULL)                           ? "subrequest" :
+                    (r->status == HTTP_NO_CONTENT)              ? "no content" :
+                    apr_table_get(r->subprocess_env, "no-gzip") ? "no-gzip" :
+                    "content-range";
+                ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
+                              "Not compressing (%s)", reason);
+            }
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next, bb);
         }
@@ -495,6 +520,8 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             const char *env_value = apr_table_get(r->subprocess_env,
                                                   "gzip-only-text/html");
             if ( env_value && (strcmp(env_value,"1") == 0) ) {
+                ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
+                              "Not compressing, (gzip-only-text/html)");
                 ap_remove_output_filter(f);
                 return ap_pass_brigade(f->next, bb);
             }
@@ -531,7 +558,9 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
                 /* stolen from mod_negotiation: */
                 if (strcmp(token, "identity") && strcmp(token, "7bit") &&
                     strcmp(token, "8bit") && strcmp(token, "binary")) {
-
+                    ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
+                                  "Not compressing (content-encoding already "
+                                  " set: %s)", token);
                     ap_remove_output_filter(f);
                     return ap_pass_brigade(f->next, bb);
                 }
@@ -579,10 +608,16 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
 
             /* No acceptable token found. */
             if (token == NULL || token[0] == '\0') {
+                ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
+                              "Not compressing (no Accept-Encoding: gzip)");
                 ap_remove_output_filter(f);
                 return ap_pass_brigade(f->next, bb);
             }
         }
+        else {
+            ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
+                          "Forcing compression (force-gzip set)");
+        }
 
         /* 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
@@ -600,7 +635,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
 
             if (zRC != Z_OK) {
                 deflateEnd(&ctx->stream);
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01383)
                               "unable to init Zlib: "
                               "deflateInit2 returned %d: URL %s",
                               zRC, r->uri);
@@ -701,7 +736,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             b = apr_bucket_pool_create(buf, VALIDATION_SIZE, r->pool,
                                        f->c->bucket_alloc);
             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
-            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01384)
                           "Zlib: Compressed %ld to %ld : URL %s",
                           ctx->stream.total_in, ctx->stream.total_out, r->uri);
 
@@ -753,7 +788,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, deflate,
                                     Z_SYNC_FLUSH, NO_UPDATE_CRC);
             if (zRC != Z_OK) {
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01385)
                               "Zlib error %d flushing zlib output buffer (%s)",
                               zRC, ctx->stream.msg);
                 return APR_EGENERAL;
@@ -812,7 +847,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
             zRC = deflate(&(ctx->stream), Z_NO_FLUSH);
 
             if (zRC != Z_OK) {
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01386)
                               "Zlib error %d deflating data (%s)", zRC,
                               ctx->stream.msg);
                 return APR_EGENERAL;
@@ -898,13 +933,13 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
         if (len != 10 ||
             deflate_hdr[0] != deflate_magic[0] ||
             deflate_hdr[1] != deflate_magic[1]) {
-            ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "Zlib: Invalid header");
+            ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01387) "Zlib: Invalid header");
             return APR_EGENERAL;
         }
 
         /* We can't handle flags for now. */
         if (deflate_hdr[3] != 0) {
-            ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
+            ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01388)
                           "Zlib: Unsupported flags %02x", (int)deflate_hdr[3]);
             return APR_EGENERAL;
         }
@@ -914,7 +949,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
         if (zRC != Z_OK) {
             f->ctx = NULL;
             inflateEnd(&ctx->stream);
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01389)
                           "unable to init Zlib: "
                           "inflateInit2 returned %d: URL %s",
                           zRC, r->uri);
@@ -948,7 +983,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
             /* If we actually see the EOS, that means we screwed up! */
             if (APR_BUCKET_IS_EOS(bkt)) {
                 inflateEnd(&ctx->stream);
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01390)
                               "Encountered EOS bucket in inflate filter (bug?)");
                 return APR_EGENERAL;
             }
@@ -958,7 +993,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
                 zRC = inflate(&(ctx->stream), Z_SYNC_FLUSH);
                 if (zRC != Z_OK) {
                     inflateEnd(&ctx->stream);
-                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
+                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01391)
                                   "Zlib error %d inflating data (%s)", zRC,
                                   ctx->stream.msg);
                     return APR_EGENERAL;
@@ -1009,7 +1044,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
 
                 if (zRC != Z_OK) {
                     inflateEnd(&ctx->stream);
-                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
+                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01392)
                                   "Zlib error %d inflating data (%s)", zRC,
                                   ctx->stream.msg);
                     return APR_EGENERAL;
@@ -1018,7 +1053,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
             if (zRC == Z_STREAM_END) {
                 apr_bucket *tmp_heap, *eos;
 
-                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01393)
                               "Zlib: Inflated %ld to %ld : URL %s",
                               ctx->stream.total_in, ctx->stream.total_out,
                               r->uri);
@@ -1037,7 +1072,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
                     compCRC = getLong(ctx->stream.next_in);
                     if (ctx->crc != compCRC) {
                         inflateEnd(&ctx->stream);
-                        ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
+                        ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01394)
                                       "Zlib: CRC error inflating data");
                         return APR_EGENERAL;
                     }
@@ -1045,7 +1080,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
                     compLen = getLong(ctx->stream.next_in);
                     if (ctx->stream.total_out != compLen) {
                         inflateEnd(&ctx->stream);
-                        ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
+                        ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01395)
                                       "Zlib: Length %ld of inflated data does "
                                       "not match expected value %ld",
                                       ctx->stream.total_out, compLen);
@@ -1056,7 +1091,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
                     /* FIXME: We need to grab the 8 verification bytes
                      * from the wire! */
                     inflateEnd(&ctx->stream);
-                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
+                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01396)
                                   "Verification data not available (bug?)");
                     return APR_EGENERAL;
                 }
@@ -1167,7 +1202,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
         if (zRC != Z_OK) {
             f->ctx = NULL;
             inflateEnd(&ctx->stream);
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01397)
                           "unable to init Zlib: "
                           "inflateInit2 returned %d: URL %s",
                           zRC, r->uri);
@@ -1221,7 +1256,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
              */
             flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate, Z_SYNC_FLUSH,
                               UPDATE_CRC);
-            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01398)
                           "Zlib: Inflated %ld to %ld : URL %s",
                           ctx->stream.total_in, ctx->stream.total_out, r->uri);
 
@@ -1229,20 +1264,20 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
                 unsigned long compCRC, compLen;
                 compCRC = getLong(ctx->validation_buffer);
                 if (ctx->crc != compCRC) {
-                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01399)
                                   "Zlib: Checksum of inflated stream invalid");
                     return APR_EGENERAL;
                 }
                 ctx->validation_buffer += VALIDATION_SIZE / 2;
                 compLen = getLong(ctx->validation_buffer);
                 if (ctx->stream.total_out != compLen) {
-                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01400)
                                   "Zlib: Length of inflated stream invalid");
                     return APR_EGENERAL;
                 }
             }
             else {
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01401)
                               "Zlib: Validation bytes not present");
                 return APR_EGENERAL;
             }
@@ -1269,7 +1304,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
             zRC = flush_libz_buffer(ctx, c, f->c->bucket_alloc, inflate,
                                     Z_SYNC_FLUSH, UPDATE_CRC);
             if (zRC != Z_OK) {
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01402)
                               "Zlib error %d flushing inflate buffer (%s)",
                               zRC, ctx->stream.msg);
                 return APR_EGENERAL;
@@ -1301,7 +1336,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
         /* first bucket contains zlib header */
         if (!ctx->inflate_init++) {
             if (len < 10) {
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01403)
                               "Insufficient data for inflate");
                 return APR_EGENERAL;
             }
@@ -1309,7 +1344,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
                 zlib_method = data[2];
                 zlib_flags = data[3];
                 if (zlib_method != Z_DEFLATED) {
-                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01404)
                                   "inflate: data not deflated!");
                     ap_remove_output_filter(f);
                     return ap_pass_brigade(f->next, bb);
@@ -1317,7 +1352,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
                 if (data[0] != deflate_magic[0] ||
                     data[1] != deflate_magic[1] ||
                     (zlib_flags & RESERVED) != 0) {
-                        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01405)
                                       "inflate: bad header");
                     return APR_EGENERAL ;
                 }
@@ -1329,7 +1364,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
                bytes += ((unsigned int)(data[1])) << 8;
                bytes += 2;
                if (len < bytes) {
-                   ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                   ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01406)
                                  "inflate: extra field too big (not "
                                  "supported)");
                    return APR_EGENERAL;
@@ -1367,7 +1402,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
                 ctx->validation_buffer_length += copy_size;
             }
             if (ctx->stream.avail_in) {
-                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01407)
                               "Zlib: %d bytes of garbage at the end of "
                               "compressed stream.", ctx->stream.avail_in);
                 /*
@@ -1411,7 +1446,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
                                                      VALIDATION_SIZE);
                 if (ctx->stream.avail_in > VALIDATION_SIZE) {
                     ctx->validation_buffer_length = VALIDATION_SIZE;
-                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01408)
                                   "Zlib: %d bytes of garbage at the end of "
                                   "compressed stream.",
                                   ctx->stream.avail_in - VALIDATION_SIZE);
@@ -1425,7 +1460,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
             }
 
             if (zRC != Z_OK) {
-                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
+                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01409)
                               "Zlib error %d inflating data (%s)", zRC,
                               ctx->stream.msg);
                 return APR_EGENERAL;