diff options
author | Alejandro Colomar <alx@nginx.com> | 2023-08-28 17:34:20 +0200 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2023-12-19 15:02:41 +0100 |
commit | 9ec5c6013be752b8e069103a2ec7c9bff5b01679 (patch) | |
tree | e6caae1b7553ac281c58d20998f3f2ca936e6085 | |
parent | 04e9f235ddbb3ae1343897f755c9ccf80c99f021 (diff) | |
download | unit-9ec5c6013be752b8e069103a2ec7c9bff5b01679.tar.gz unit-9ec5c6013be752b8e069103a2ec7c9bff5b01679.tar.bz2 |
HTTP: compress: added "mime_types" rule.
Signed-off-by: Alejandro Colomar <alx@nginx.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | src/nxt_conf_validation.c | 4 | ||||
-rw-r--r-- | src/nxt_http.h | 1 | ||||
-rw-r--r-- | src/nxt_http_compress.c | 37 | ||||
-rw-r--r-- | src/nxt_http_compress.h | 18 | ||||
-rw-r--r-- | src/nxt_http_compress_gzip.c | 10 | ||||
-rw-r--r-- | src/nxt_http_route.c | 2 | ||||
-rw-r--r-- | src/nxt_http_static.c | 1 |
7 files changed, 63 insertions, 10 deletions
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index 7e1da709..bae3efbd 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -1164,6 +1164,10 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_compress_members[] = { }, { .name = nxt_string("min_length"), .type = NXT_CONF_VLDT_INTEGER, + }, { + .name = nxt_string("mime_types"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_patterns, }, NXT_CONF_VLDT_END diff --git a/src/nxt_http.h b/src/nxt_http.h index 94df6f2e..28803ba9 100644 --- a/src/nxt_http.h +++ b/src/nxt_http.h @@ -113,6 +113,7 @@ typedef struct { nxt_http_field_t *content_type; nxt_http_field_t *content_length; nxt_off_t content_length_n; + const nxt_str_t *mtype; } nxt_http_response_t; diff --git a/src/nxt_http_compress.c b/src/nxt_http_compress.c index 613bf5f1..b7c4c08d 100644 --- a/src/nxt_http_compress.c +++ b/src/nxt_http_compress.c @@ -43,12 +43,17 @@ static nxt_conf_map_t nxt_http_compress_conf[] = { NXT_CONF_MAP_SIZE, offsetof(nxt_http_compress_conf_t, min_len), }, + { + nxt_string("mime_types"), + NXT_CONF_MAP_PTR, + offsetof(nxt_http_compress_conf_t, mtypes), + }, }; nxt_int_t -nxt_http_compress_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, - nxt_http_action_conf_t *acf) +nxt_http_compress_init(nxt_task_t *task, nxt_router_conf_t *rtcf, + nxt_http_action_t *action, nxt_http_action_conf_t *acf) { nxt_mp_t *mp; nxt_int_t ret; @@ -70,6 +75,13 @@ nxt_http_compress_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, return NXT_ERROR; } + if (conf->mtypes != NULL) { + conf->mtrule = nxt_http_route_types_rule_create(task, mp, conf->mtypes); + if (nxt_slow_path(conf->mtrule == NULL)) { + return NXT_ERROR; + } + } + if (NXT_WITH_ZLIB && nxt_str_eq(&conf->encoding, "gzip", strlen("gzip"))) { conf->handler = nxt_http_compress_gzip; @@ -132,3 +144,24 @@ nxt_http_compress_resp_content_length(nxt_http_response_t *resp) return cl; } + + +nxt_int_t +nxt_http_compressible_mtype(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_route_rule_t *mtrule) +{ + const nxt_str_t *type; + + static nxt_str_t unknown_mtype = nxt_string(""); + + if (mtrule == NULL) { + return 1; + } + + type = r->resp.mtype; + if (type == NULL) { + type = &unknown_mtype; + } + + return nxt_http_route_test_rule(r, mtrule, type->start, type->length); +} diff --git a/src/nxt_http_compress.h b/src/nxt_http_compress.h index 6e8e45f9..1d671bf9 100644 --- a/src/nxt_http_compress.h +++ b/src/nxt_http_compress.h @@ -20,21 +20,25 @@ struct nxt_http_compress_conf_s { - nxt_str_t encoding; + nxt_str_t encoding; - nxt_int_t (*handler)(nxt_task_t *task, - nxt_http_request_t *r, - nxt_http_compress_conf_t *conf); + nxt_int_t (*handler)(nxt_task_t *task, + nxt_http_request_t *r, + nxt_http_compress_conf_t *conf); - int8_t level; - size_t min_len; + int8_t level; + size_t min_len; + nxt_conf_value_t *mtypes; + nxt_http_route_rule_t *mtrule; }; -nxt_int_t nxt_http_compress_init(nxt_router_conf_t *rtcf, +nxt_int_t nxt_http_compress_init(nxt_task_t *task, 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_compressible_mtype(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_route_rule_t *mtrule); 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 2d0d65ae..c1ff962b 100644 --- a/src/nxt_http_compress_gzip.c +++ b/src/nxt_http_compress_gzip.c @@ -66,6 +66,16 @@ nxt_http_compress_gzip(nxt_task_t *task, nxt_http_request_t *r, return NXT_OK; } + ret = nxt_http_compressible_mtype(task, r, conf->mtrule); + switch (ret) { + case NXT_ERROR: + return NXT_ERROR; + case 0: + return NXT_OK; + case 1: + break; + } + r->resp.content_length = NULL; r->resp.content_length_n = -1; diff --git a/src/nxt_http_route.c b/src/nxt_http_route.c index 523dda40..a23ebbcf 100644 --- a/src/nxt_http_route.c +++ b/src/nxt_http_route.c @@ -690,7 +690,7 @@ nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } if (acf.compress != NULL) { - ret = nxt_http_compress_init(rtcf, action, &acf); + ret = nxt_http_compress_init(task, rtcf, action, &acf); if (nxt_slow_path(ret != NXT_OK)) { return ret; } diff --git a/src/nxt_http_static.c b/src/nxt_http_static.c index fc26924d..490a41d4 100644 --- a/src/nxt_http_static.c +++ b/src/nxt_http_static.c @@ -648,6 +648,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data) r->body_handler = NULL; } + r->resp.mtype = mtype; nxt_http_request_header_send(task, r); r->state = &nxt_http_static_send_state; |