diff options
author | Alejandro Colomar <alx@nginx.com> | 2023-07-19 14:22:21 +0200 |
---|---|---|
committer | Alejandro Colomar <alx@nginx.com> | 2023-09-03 18:38:05 +0200 |
commit | 6e164a368b4aea80233b17486fe549ab70816e86 (patch) | |
tree | 266cc6f1d60162771c0c60b9055f1241199acf3e /src | |
parent | 4977d280cacc7dd2bbbd61a00d133574f18cb118 (diff) | |
download | unit-6e164a368b4aea80233b17486fe549ab70816e86.tar.gz unit-6e164a368b4aea80233b17486fe549ab70816e86.tar.bz2 |
HTTP: compress: added "compress" action.
There are still no supported encodings. This is just infrastructure for
the next commits, which will add gzip compression.
Signed-off-by: Alejandro Colomar <alx@nginx.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/nxt_conf_validation.c | 16 | ||||
-rw-r--r-- | src/nxt_http.h | 5 | ||||
-rw-r--r-- | src/nxt_http_compress.c | 88 | ||||
-rw-r--r-- | src/nxt_http_compress.h | 35 | ||||
-rw-r--r-- | src/nxt_http_request.c | 18 | ||||
-rw-r--r-- | src/nxt_http_route.c | 13 |
6 files changed, 171 insertions, 4 deletions
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index f00b28b8..0e1f7340 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -254,6 +254,7 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_php_options_members[]; static nxt_conf_vldt_object_t nxt_conf_vldt_php_target_members[]; static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_access_members[]; static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_compress_members[]; static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[]; static nxt_conf_vldt_object_t nxt_conf_vldt_app_processes_members[]; static nxt_conf_vldt_object_t nxt_conf_vldt_app_isolation_members[]; @@ -697,6 +698,11 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_action_common_members[] = { .validator = nxt_conf_vldt_object_iterator, .u.object = nxt_conf_vldt_response_header, }, + { + .name = nxt_string("compress"), + .type = NXT_CONF_VLDT_OBJECT, + .u.members = nxt_conf_vldt_compress_members, + }, NXT_CONF_VLDT_END }; @@ -1148,6 +1154,16 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[] = { }; +static nxt_conf_vldt_object_t nxt_conf_vldt_compress_members[] = { + { + .name = nxt_string("encoding"), + .type = NXT_CONF_VLDT_STRING, + }, + + NXT_CONF_VLDT_END +}; + + static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[] = { { .name = nxt_string("timeout"), diff --git a/src/nxt_http.h b/src/nxt_http.h index 300c197d..94df6f2e 100644 --- a/src/nxt_http.h +++ b/src/nxt_http.h @@ -10,6 +10,9 @@ #include <nxt_regex.h> +typedef struct nxt_http_compress_conf_s nxt_http_compress_conf_t; + + typedef enum { NXT_HTTP_UNSET = -1, NXT_HTTP_INVALID = 0, @@ -233,6 +236,7 @@ typedef struct nxt_http_route_addr_rule_s nxt_http_route_addr_rule_t; typedef struct { nxt_conf_value_t *rewrite; nxt_conf_value_t *set_headers; + nxt_conf_value_t *compress; nxt_conf_value_t *pass; nxt_conf_value_t *ret; nxt_conf_value_t *location; @@ -262,6 +266,7 @@ struct nxt_http_action_s { nxt_tstr_t *rewrite; nxt_array_t *set_headers; /* of nxt_http_field_t */ + nxt_http_compress_conf_t *compress; nxt_http_action_t *fallback; }; diff --git a/src/nxt_http_compress.c b/src/nxt_http_compress.c new file mode 100644 index 00000000..0f3d8a89 --- /dev/null +++ b/src/nxt_http_compress.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) Alejandro Colomar + * Copyright (C) NGINX, Inc. + */ + + +#include "nxt_http_compress.h" + +#include <stddef.h> + +#include <nxt_unit_cdefs.h> + +#include "nxt_clang.h" +#include "nxt_conf.h" +#include "nxt_errno.h" +#include "nxt_http.h" +#include "nxt_list.h" +#include "nxt_main.h" +#include "nxt_mp.h" +#include "nxt_router.h" +#include "nxt_string.h" +#include "nxt_types.h" + + +static nxt_conf_map_t nxt_http_compress_conf[] = { + { + nxt_string("encoding"), + NXT_CONF_MAP_STR, + offsetof(nxt_http_compress_conf_t, encoding), + }, +}; + + +nxt_int_t +nxt_http_compress_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, + nxt_http_action_conf_t *acf) +{ + nxt_mp_t *mp; + nxt_int_t ret; + nxt_http_compress_conf_t *conf; + + mp = rtcf->mem_pool; + + conf = nxt_mp_zget(mp, sizeof(nxt_http_compress_conf_t)); + if (nxt_slow_path(conf == NULL)) { + return NXT_ERROR; + } + + ret = nxt_conf_map_object(mp, acf->compress, nxt_http_compress_conf, + nxt_nitems(nxt_http_compress_conf), conf); + if (nxt_slow_path(ret == NXT_ERROR)) { + return NXT_ERROR; + } + + if (0) { + + } else { + return NXT_ERROR; + } + + action->compress = conf; + + return NXT_OK; +} + + +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) +{ + nxt_http_field_t *f; + + f = nxt_list_add(r->resp.fields); + if (nxt_slow_path(f == NULL)) { + return NXT_ERROR; + } + + f->hash = 0; + f->skip = 0; + f->hopbyhop = 0; + + f->name_length = field->length; + f->value_length = value->length; + f->name = field->start; + f->value = value->start; + + return NXT_OK; +} diff --git a/src/nxt_http_compress.h b/src/nxt_http_compress.h new file mode 100644 index 00000000..57bebd1c --- /dev/null +++ b/src/nxt_http_compress.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) Alejandro Colomar + * Copyright (C) NGINX, Inc. + */ + +#ifndef NXT_HTTP_COMPRESS_H_INCLUDED_ +#define NXT_HTTP_COMPRESS_H_INCLUDED_ + + +#include "nxt_router.h" + +#include "nxt_http.h" +#include "nxt_main.h" +#include "nxt_router.h" +#include "nxt_string.h" +#include "nxt_types.h" + + +struct nxt_http_compress_conf_s { + 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 nxt_http_compress_init(nxt_router_conf_t *rtcf, + nxt_http_action_t *action, nxt_http_action_conf_t *acf); + +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); + + +#endif /* NXT_HTTP_COMPRESS_H_INCLUDED_ */ diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c index e72a7492..ecbc5a45 100644 --- a/src/nxt_http_request.c +++ b/src/nxt_http_request.c @@ -7,6 +7,7 @@ #include <nxt_router.h> #include <nxt_http.h> +#include "nxt_http_compress.h" #include "nxt_http_filter.h" @@ -635,10 +636,19 @@ nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r) void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r) { - u_char *p, *end, *server_string; - nxt_int_t ret; - nxt_http_field_t *server, *date, *content_length; - nxt_socket_conf_t *skcf; + u_char *p, *end, *server_string; + nxt_int_t ret; + nxt_http_field_t *server, *date, *content_length; + nxt_socket_conf_t *skcf; + nxt_http_compress_conf_t *compress; + + compress = r->action->compress; + if (compress != NULL) { + ret = compress->handler(task, r, compress); + if (nxt_slow_path(ret == NXT_ERROR)) { + goto fail; + } + } ret = nxt_http_set_headers(r); if (nxt_slow_path(ret != NXT_OK)) { diff --git a/src/nxt_http_route.c b/src/nxt_http_route.c index 4a64d5c1..523dda40 100644 --- a/src/nxt_http_route.c +++ b/src/nxt_http_route.c @@ -9,6 +9,7 @@ #include <nxt_sockaddr.h> #include <nxt_http_route_addr.h> #include <nxt_regex.h> +#include "nxt_http_compress.h" typedef enum { @@ -589,6 +590,11 @@ static nxt_conf_map_t nxt_http_route_action_conf[] = { offsetof(nxt_http_action_conf_t, set_headers) }, { + nxt_string("compress"), + NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, compress) + }, + { nxt_string("pass"), NXT_CONF_MAP_PTR, offsetof(nxt_http_action_conf_t, pass) @@ -683,6 +689,13 @@ 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); + if (nxt_slow_path(ret != NXT_OK)) { + return ret; + } + } + if (acf.ret != NULL) { return nxt_http_return_init(rtcf, action, &acf); } |