diff options
author | Zhidao HONG <z.hong@f5.com> | 2022-06-20 13:16:25 +0800 |
---|---|---|
committer | Zhidao HONG <z.hong@f5.com> | 2022-06-20 13:16:25 +0800 |
commit | 14dfa439eed1983693d7d128cc51c5e738a487d7 (patch) | |
tree | 771e4648406614b53c9195c4e66c78bf84520d27 /src | |
parent | fd38e69c3db9cda55fb24401c9eedbe18a05e962 (diff) | |
download | unit-14dfa439eed1983693d7d128cc51c5e738a487d7.tar.gz unit-14dfa439eed1983693d7d128cc51c5e738a487d7.tar.bz2 |
Router: introduced nxt_http_forward_t.
This makes the replacement of forwarded request header
like client_ip and protocol more generic.
It's a prerequirement for protocol replacement.
No functional changes.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_http.h | 9 | ||||
-rw-r--r-- | src/nxt_http_request.c | 30 | ||||
-rw-r--r-- | src/nxt_router.c | 39 | ||||
-rw-r--r-- | src/nxt_router.h | 4 |
4 files changed, 42 insertions, 40 deletions
diff --git a/src/nxt_http.h b/src/nxt_http.h index 37c27325..665ccdbf 100644 --- a/src/nxt_http.h +++ b/src/nxt_http.h @@ -273,10 +273,15 @@ typedef struct { } nxt_http_proto_table_t; -struct nxt_http_client_ip_s { - nxt_http_route_addr_rule_t *source; +typedef struct { nxt_str_t *header; uint32_t header_hash; +} nxt_http_forward_header_t; + + +struct nxt_http_forward_s { + nxt_http_forward_header_t client_ip; + nxt_http_route_addr_rule_t *source; uint8_t recursive; /* 1 bit */ }; diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c index 0eacf627..3f926796 100644 --- a/src/nxt_http_request.c +++ b/src/nxt_http_request.c @@ -314,25 +314,27 @@ nxt_http_request_start(nxt_task_t *task, void *obj, void *data) static nxt_int_t nxt_http_request_client_ip(nxt_task_t *task, nxt_http_request_t *r) { - u_char *start, *p; - nxt_int_t ret, i, len; - nxt_str_t *header; - nxt_array_t *fields_arr; /* of nxt_http_field_t * */ - nxt_sockaddr_t *sa, *prev_sa; - nxt_http_field_t *f, **fields; - nxt_http_client_ip_t *client_ip; - - client_ip = r->conf->socket_conf->client_ip; - - if (client_ip == NULL) { + u_char *start, *p; + nxt_int_t ret, i, len; + nxt_str_t *header; + nxt_array_t *fields_arr; /* of nxt_http_field_t * */ + nxt_sockaddr_t *sa, *prev_sa; + nxt_http_field_t *f, **fields; + nxt_http_forward_t *forward; + nxt_http_forward_header_t *client_ip; + + forward = r->conf->socket_conf->client_ip; + + if (forward == NULL) { return NXT_OK; } - ret = nxt_http_route_addr_rule(r, client_ip->source, r->remote); + ret = nxt_http_route_addr_rule(r, forward->source, r->remote); if (ret <= 0) { return NXT_OK; } + client_ip = &forward->client_ip; header = client_ip->header; fields_arr = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_field_t *)); @@ -388,13 +390,13 @@ nxt_http_request_client_ip(nxt_task_t *task, nxt_http_request_t *r) return NXT_OK; } - if (!client_ip->recursive) { + if (!forward->recursive) { r->remote = sa; return NXT_OK; } - ret = nxt_http_route_addr_rule(r, client_ip->source, sa); + ret = nxt_http_route_addr_rule(r, forward->source, sa); if (ret <= 0 || (i == 0 && p == start)) { r->remote = sa; diff --git a/src/nxt_router.c b/src/nxt_router.c index 00558d94..8487d239 100644 --- a/src/nxt_router.c +++ b/src/nxt_router.c @@ -109,8 +109,7 @@ static nxt_int_t nxt_router_conf_create(nxt_task_t *task, static nxt_int_t nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, nxt_conf_value_t *conf); static nxt_int_t nxt_router_conf_process_client_ip(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf, - nxt_conf_value_t *conf); + nxt_mp_t *mp, nxt_socket_conf_t *skcf, nxt_conf_value_t *conf); static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name); static nxt_int_t nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data); @@ -1885,10 +1884,12 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } conf = nxt_conf_get_path(listener, &client_ip_path); - ret = nxt_router_conf_process_client_ip(task, tmcf, skcf, - conf); - if (nxt_slow_path(ret != NXT_OK)) { - return NXT_ERROR; + + if (conf != NULL) { + ret = nxt_router_conf_process_client_ip(task, mp, skcf, conf); + if (nxt_slow_path(ret != NXT_OK)) { + return NXT_ERROR; + } } #if (NXT_TLS) @@ -2124,30 +2125,22 @@ nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, static nxt_int_t -nxt_router_conf_process_client_ip(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, +nxt_router_conf_process_client_ip(nxt_task_t *task, nxt_mp_t *mp, nxt_socket_conf_t *skcf, nxt_conf_value_t *conf) { char c; size_t i; - nxt_mp_t *mp; uint32_t hash; nxt_str_t header; nxt_conf_value_t *source_conf, *header_conf, *recursive_conf; - nxt_http_client_ip_t *client_ip; + nxt_http_forward_t *forward; + nxt_http_forward_header_t *client_ip; nxt_http_route_addr_rule_t *source; static nxt_str_t header_path = nxt_string("/header"); static nxt_str_t source_path = nxt_string("/source"); static nxt_str_t recursive_path = nxt_string("/recursive"); - if (conf == NULL) { - skcf->client_ip = NULL; - - return NXT_OK; - } - - mp = tmcf->router_conf->mem_pool; - source_conf = nxt_conf_get_path(conf, &source_path); header_conf = nxt_conf_get_path(conf, &header_path); recursive_conf = nxt_conf_get_path(conf, &recursive_path); @@ -2156,8 +2149,8 @@ nxt_router_conf_process_client_ip(nxt_task_t *task, nxt_router_temp_conf_t *tmcf return NXT_ERROR; } - client_ip = nxt_mp_zget(mp, sizeof(nxt_http_client_ip_t)); - if (nxt_slow_path(client_ip == NULL)) { + forward = nxt_mp_zget(mp, sizeof(nxt_http_forward_t)); + if (nxt_slow_path(forward == NULL)) { return NXT_ERROR; } @@ -2166,12 +2159,14 @@ nxt_router_conf_process_client_ip(nxt_task_t *task, nxt_router_temp_conf_t *tmcf return NXT_ERROR; } - client_ip->source = source; + forward->source = source; + + client_ip = &forward->client_ip; nxt_conf_get_string(header_conf, &header); if (recursive_conf != NULL) { - client_ip->recursive = nxt_conf_get_boolean(recursive_conf); + forward->recursive = nxt_conf_get_boolean(recursive_conf); } client_ip->header = nxt_str_dup(mp, NULL, &header); @@ -2190,7 +2185,7 @@ nxt_router_conf_process_client_ip(nxt_task_t *task, nxt_router_temp_conf_t *tmcf client_ip->header_hash = hash; - skcf->client_ip = client_ip; + skcf->client_ip = forward; return NXT_OK; } diff --git a/src/nxt_router.h b/src/nxt_router.h index 7e337d27..b97d5c20 100644 --- a/src/nxt_router.h +++ b/src/nxt_router.h @@ -18,7 +18,7 @@ typedef struct nxt_http_request_s nxt_http_request_t; typedef struct nxt_http_action_s nxt_http_action_t; typedef struct nxt_http_routes_s nxt_http_routes_t; -typedef struct nxt_http_client_ip_s nxt_http_client_ip_t; +typedef struct nxt_http_forward_s nxt_http_forward_t; typedef struct nxt_upstream_s nxt_upstream_t; typedef struct nxt_upstreams_s nxt_upstreams_t; typedef struct nxt_router_access_log_s nxt_router_access_log_t; @@ -197,7 +197,7 @@ typedef struct { uint8_t discard_unsafe_fields; /* 1 bit */ - nxt_http_client_ip_t *client_ip; + nxt_http_forward_t *client_ip; #if (NXT_TLS) nxt_tls_conf_t *tls; |