diff options
author | Alejandro Colomar <alx@nginx.com> | 2023-09-01 01:41:48 +0200 |
---|---|---|
committer | Alejandro Colomar <alx@nginx.com> | 2023-09-05 02:12:28 +0200 |
commit | 861bc4948b84e738c09cbb57618969f0a7227f79 (patch) | |
tree | a65f937f804f4811df2def962de867741f2e38f1 | |
parent | 77bda1133c2f614ba13c8d208be96646c2c08626 (diff) | |
download | unit-861bc4948b84e738c09cbb57618969f0a7227f79.tar.gz unit-861bc4948b84e738c09cbb57618969f0a7227f79.tar.bz2 |
HTTP: compress: checking $header_accept_encoding.
Signed-off-by: Alejandro Colomar <alx@nginx.com>
-rw-r--r-- | src/nxt_http_compress.c | 33 | ||||
-rw-r--r-- | src/nxt_http_compress.h | 4 | ||||
-rw-r--r-- | src/nxt_http_compress_gzip.c | 11 |
3 files changed, 48 insertions, 0 deletions
diff --git a/src/nxt_http_compress.c b/src/nxt_http_compress.c index 5216c5d7..ff2f898b 100644 --- a/src/nxt_http_compress.c +++ b/src/nxt_http_compress.c @@ -20,6 +20,7 @@ #include "nxt_mp.h" #include "nxt_router.h" #include "nxt_string.h" +#include "nxt_tstr.h" #include "nxt_types.h" @@ -58,6 +59,8 @@ nxt_http_compress_init(nxt_task_t *task, nxt_router_conf_t *rtcf, nxt_int_t ret; nxt_http_compress_conf_t *conf; + static nxt_str_t hdr_a_e = nxt_string("$header_accept_encoding"); + mp = rtcf->mem_pool; conf = nxt_mp_zget(mp, sizeof(nxt_http_compress_conf_t)); @@ -81,6 +84,11 @@ nxt_http_compress_init(nxt_task_t *task, nxt_router_conf_t *rtcf, } } + conf->accept_encoding = nxt_tstr_compile(rtcf->tstr_state, &hdr_a_e, 0); + if (nxt_slow_path(conf->accept_encoding == NULL)) { + return NXT_ERROR; + } + if (NXT_WITH_ZLIB && nxt_str_eq(&conf->encoding, "gzip", strlen("gzip"))) { conf->handler = nxt_http_compress_gzip; @@ -146,6 +154,31 @@ nxt_http_compress_resp_content_length(nxt_http_response_t *resp) nxt_int_t +nxt_http_compress_accept_encoding(nxt_task_t *task, nxt_http_request_t *r, + nxt_tstr_t *accept_encoding, nxt_str_t *encoding) +{ + nxt_int_t ret; + nxt_str_t str; + nxt_router_conf_t *rtcf; + + rtcf = r->conf->socket_conf->router_conf; + + ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, &r->tstr_cache, + r, r->mem_pool); + if (nxt_slow_path(ret == NXT_ERROR)) { + return NXT_ERROR; + } + + nxt_tstr_query(task, r->tstr_query, accept_encoding, &str); + if (nxt_slow_path(nxt_tstr_query_failed(r->tstr_query))) { + return NXT_ERROR; + } + + return nxt_strstr_eq(&str, encoding); +} + + +nxt_int_t nxt_http_compressible_mtype(nxt_task_t *task, nxt_http_request_t *r, nxt_http_route_rule_t *mtrule) { diff --git a/src/nxt_http_compress.h b/src/nxt_http_compress.h index 1d671bf9..27b8b1c3 100644 --- a/src/nxt_http_compress.h +++ b/src/nxt_http_compress.h @@ -16,6 +16,7 @@ #include "nxt_main.h" #include "nxt_router.h" #include "nxt_string.h" +#include "nxt_tstr.h" #include "nxt_types.h" @@ -28,6 +29,7 @@ struct nxt_http_compress_conf_s { int8_t level; size_t min_len; + nxt_tstr_t *accept_encoding; nxt_conf_value_t *mtypes; nxt_http_route_rule_t *mtrule; }; @@ -37,6 +39,8 @@ 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_compress_accept_encoding(nxt_task_t *task, + nxt_http_request_t *r, nxt_tstr_t *accept_encoding, nxt_str_t *encoding); 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, diff --git a/src/nxt_http_compress_gzip.c b/src/nxt_http_compress_gzip.c index c1ff962b..f8d88bf8 100644 --- a/src/nxt_http_compress_gzip.c +++ b/src/nxt_http_compress_gzip.c @@ -66,6 +66,17 @@ nxt_http_compress_gzip(nxt_task_t *task, nxt_http_request_t *r, return NXT_OK; } + ret = nxt_http_compress_accept_encoding(task, r, conf->accept_encoding, + &gzip); + switch (ret) { + case NXT_ERROR: + return NXT_ERROR; + case 0: + return NXT_OK; + case 1: + break; + } + ret = nxt_http_compressible_mtype(task, r, conf->mtrule); switch (ret) { case NXT_ERROR: |