diff options
-rw-r--r-- | src/nxt_conf_validation.c | 3 | ||||
-rw-r--r-- | src/nxt_http_compress.c | 34 | ||||
-rw-r--r-- | src/nxt_http_compress.h | 3 | ||||
-rw-r--r-- | src/nxt_http_compress_gzip.c | 9 |
4 files changed, 43 insertions, 6 deletions
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index 4943c603..7e1da709 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -1161,6 +1161,9 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_compress_members[] = { }, { .name = nxt_string("level"), .type = NXT_CONF_VLDT_INTEGER, + }, { + .name = nxt_string("min_length"), + .type = NXT_CONF_VLDT_INTEGER, }, NXT_CONF_VLDT_END diff --git a/src/nxt_http_compress.c b/src/nxt_http_compress.c index 55e20e3a..613bf5f1 100644 --- a/src/nxt_http_compress.c +++ b/src/nxt_http_compress.c @@ -6,6 +6,7 @@ #include "nxt_http_compress.h" +#include <errno.h> #include <stddef.h> #include <nxt_unit_cdefs.h> @@ -37,6 +38,11 @@ static nxt_conf_map_t nxt_http_compress_conf[] = { NXT_CONF_MAP_INT8, offsetof(nxt_http_compress_conf_t, level), }, + { + nxt_string("min_length"), + NXT_CONF_MAP_SIZE, + offsetof(nxt_http_compress_conf_t, min_len), + }, }; @@ -56,6 +62,7 @@ nxt_http_compress_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, } conf->level = NXT_DEFAULT_COMPRESSION; + conf->min_len = 20; ret = nxt_conf_map_object(mp, acf->compress, nxt_http_compress_conf, nxt_nitems(nxt_http_compress_conf), conf); @@ -98,3 +105,30 @@ nxt_http_compress_append_field(nxt_task_t *task, nxt_http_request_t *r, return NXT_OK; } + + +ssize_t +nxt_http_compress_resp_content_length(nxt_http_response_t *resp) +{ + size_t cl; + nxt_str_t str; + + if (resp->content_length_n != -1) { + return resp->content_length_n; + } + + if (resp->content_length == NULL) { + return -1; + } + + str.length = resp->content_length->value_length; + str.start = resp->content_length->value; + + errno = 0; + cl = nxt_atoul(&str); + if (errno != 0) { + return -1; + } + + return cl; +} diff --git a/src/nxt_http_compress.h b/src/nxt_http_compress.h index 19075025..6e8e45f9 100644 --- a/src/nxt_http_compress.h +++ b/src/nxt_http_compress.h @@ -9,6 +9,7 @@ #include "nxt_router.h" +#include <stddef.h> #include <stdint.h> #include "nxt_http.h" @@ -26,12 +27,14 @@ struct nxt_http_compress_conf_s { nxt_http_compress_conf_t *conf); int8_t level; + size_t min_len; }; nxt_int_t nxt_http_compress_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, nxt_http_action_conf_t *acf); +ssize_t nxt_http_compress_resp_content_length(nxt_http_response_t *resp); nxt_int_t nxt_http_compress_append_field(nxt_task_t *task, nxt_http_request_t *r, nxt_str_t *field, nxt_str_t *value); diff --git a/src/nxt_http_compress_gzip.c b/src/nxt_http_compress_gzip.c index db675ecf..2d0d65ae 100644 --- a/src/nxt_http_compress_gzip.c +++ b/src/nxt_http_compress_gzip.c @@ -54,18 +54,15 @@ nxt_int_t nxt_http_compress_gzip(nxt_task_t *task, nxt_http_request_t *r, nxt_http_compress_conf_t *conf) { + size_t clen; nxt_int_t ret; nxt_http_compress_gzip_ctx_t *ctx; static nxt_str_t ce = nxt_string("Content-Encoding"); static nxt_str_t gzip = nxt_string("gzip"); - if (r->body_handler == NULL - || r->resp.content_length_n == 0 - || (r->resp.content_length != NULL - && r->resp.content_length->value_length == 1 - && r->resp.content_length->value[0] == '0')) - { + clen = nxt_http_compress_resp_content_length(&r->resp); + if (clen < nxt_max(1u, conf->min_len) || r->body_handler == NULL) { return NXT_OK; } |