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.
This commit is contained in:
Zhidao HONG
2022-06-20 13:16:25 +08:00
parent fd38e69c3d
commit 14dfa439ee
4 changed files with 40 additions and 38 deletions

View File

@@ -273,10 +273,15 @@ typedef struct {
} nxt_http_proto_table_t; } nxt_http_proto_table_t;
struct nxt_http_client_ip_s { typedef struct {
nxt_http_route_addr_rule_t *source;
nxt_str_t *header; nxt_str_t *header;
uint32_t header_hash; 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 */ uint8_t recursive; /* 1 bit */
}; };

View File

@@ -314,25 +314,27 @@ nxt_http_request_start(nxt_task_t *task, void *obj, void *data)
static nxt_int_t static nxt_int_t
nxt_http_request_client_ip(nxt_task_t *task, nxt_http_request_t *r) nxt_http_request_client_ip(nxt_task_t *task, nxt_http_request_t *r)
{ {
u_char *start, *p; u_char *start, *p;
nxt_int_t ret, i, len; nxt_int_t ret, i, len;
nxt_str_t *header; nxt_str_t *header;
nxt_array_t *fields_arr; /* of nxt_http_field_t * */ nxt_array_t *fields_arr; /* of nxt_http_field_t * */
nxt_sockaddr_t *sa, *prev_sa; nxt_sockaddr_t *sa, *prev_sa;
nxt_http_field_t *f, **fields; nxt_http_field_t *f, **fields;
nxt_http_client_ip_t *client_ip; nxt_http_forward_t *forward;
nxt_http_forward_header_t *client_ip;
client_ip = r->conf->socket_conf->client_ip; forward = r->conf->socket_conf->client_ip;
if (client_ip == NULL) { if (forward == NULL) {
return NXT_OK; 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) { if (ret <= 0) {
return NXT_OK; return NXT_OK;
} }
client_ip = &forward->client_ip;
header = client_ip->header; header = client_ip->header;
fields_arr = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_field_t *)); 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; return NXT_OK;
} }
if (!client_ip->recursive) { if (!forward->recursive) {
r->remote = sa; r->remote = sa;
return NXT_OK; 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)) { if (ret <= 0 || (i == 0 && p == start)) {
r->remote = sa; r->remote = sa;

View File

@@ -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, static nxt_int_t nxt_router_conf_process_static(nxt_task_t *task,
nxt_router_conf_t *rtcf, nxt_conf_value_t *conf); nxt_router_conf_t *rtcf, nxt_conf_value_t *conf);
static nxt_int_t nxt_router_conf_process_client_ip(nxt_task_t *task, 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_mp_t *mp, nxt_socket_conf_t *skcf, nxt_conf_value_t *conf);
nxt_conf_value_t *conf);
static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name); 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); 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); conf = nxt_conf_get_path(listener, &client_ip_path);
ret = nxt_router_conf_process_client_ip(task, tmcf, skcf,
conf); if (conf != NULL) {
if (nxt_slow_path(ret != NXT_OK)) { ret = nxt_router_conf_process_client_ip(task, mp, skcf, conf);
return NXT_ERROR; if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ERROR;
}
} }
#if (NXT_TLS) #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 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) nxt_socket_conf_t *skcf, nxt_conf_value_t *conf)
{ {
char c; char c;
size_t i; size_t i;
nxt_mp_t *mp;
uint32_t hash; uint32_t hash;
nxt_str_t header; nxt_str_t header;
nxt_conf_value_t *source_conf, *header_conf, *recursive_conf; 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; nxt_http_route_addr_rule_t *source;
static nxt_str_t header_path = nxt_string("/header"); static nxt_str_t header_path = nxt_string("/header");
static nxt_str_t source_path = nxt_string("/source"); static nxt_str_t source_path = nxt_string("/source");
static nxt_str_t recursive_path = nxt_string("/recursive"); 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); source_conf = nxt_conf_get_path(conf, &source_path);
header_conf = nxt_conf_get_path(conf, &header_path); header_conf = nxt_conf_get_path(conf, &header_path);
recursive_conf = nxt_conf_get_path(conf, &recursive_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; return NXT_ERROR;
} }
client_ip = nxt_mp_zget(mp, sizeof(nxt_http_client_ip_t)); forward = nxt_mp_zget(mp, sizeof(nxt_http_forward_t));
if (nxt_slow_path(client_ip == NULL)) { if (nxt_slow_path(forward == NULL)) {
return NXT_ERROR; 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; return NXT_ERROR;
} }
client_ip->source = source; forward->source = source;
client_ip = &forward->client_ip;
nxt_conf_get_string(header_conf, &header); nxt_conf_get_string(header_conf, &header);
if (recursive_conf != NULL) { 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); 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; client_ip->header_hash = hash;
skcf->client_ip = client_ip; skcf->client_ip = forward;
return NXT_OK; return NXT_OK;
} }

View File

@@ -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_action_s nxt_http_action_t;
typedef struct nxt_http_routes_s nxt_http_routes_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_upstream_s nxt_upstream_t;
typedef struct nxt_upstreams_s nxt_upstreams_t; typedef struct nxt_upstreams_s nxt_upstreams_t;
typedef struct nxt_router_access_log_s nxt_router_access_log_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 */ uint8_t discard_unsafe_fields; /* 1 bit */
nxt_http_client_ip_t *client_ip; nxt_http_forward_t *client_ip;
#if (NXT_TLS) #if (NXT_TLS)
nxt_tls_conf_t *tls; nxt_tls_conf_t *tls;