HTTP: added basic URI rewrite.

This commit introduced the basic URI rewrite. It allows users to change request URI. Note the "rewrite" option ignores the contained query if any and the query from the request is preserverd.
An example:
"routes": [
    {
        "match": {
            "uri": "/v1/test"
        },
        "action": {
            "return": 200
        }
    },
    {
        "action": {
            "rewrite": "/v1$uri",
            "pass": "routes"
        }
    }
]

Reviewed-by: Alejandro Colomar <alx@nginx.com>
This commit is contained in:
Zhidao HONG
2023-04-20 23:20:41 +08:00
parent 8843e30e82
commit 14d6d97bac
11 changed files with 167 additions and 11 deletions

View File

@@ -578,6 +578,11 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
static nxt_conf_map_t nxt_http_route_action_conf[] = {
{
nxt_string("rewrite"),
NXT_CONF_MAP_PTR,
offsetof(nxt_http_action_conf_t, rewrite)
},
{
nxt_string("pass"),
NXT_CONF_MAP_PTR,
@@ -659,6 +664,13 @@ nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
rtcf = tmcf->router_conf;
mp = rtcf->mem_pool;
if (acf.rewrite != NULL) {
ret = nxt_http_rewrite_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);
}
@@ -1312,8 +1324,8 @@ nxt_http_pass_var(nxt_task_t *task, nxt_http_request_t *r,
goto fail;
}
action = nxt_mp_get(r->mem_pool,
sizeof(nxt_http_action_t) + sizeof(nxt_str_t));
action = nxt_mp_zget(r->mem_pool,
sizeof(nxt_http_action_t) + sizeof(nxt_str_t));
if (nxt_slow_path(action == NULL)) {
goto fail;
}
@@ -1496,7 +1508,7 @@ nxt_http_action_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
rtcf = tmcf->router_conf;
mp = rtcf->mem_pool;
action = nxt_mp_alloc(mp, sizeof(nxt_http_action_t));
action = nxt_mp_zalloc(mp, sizeof(nxt_http_action_t));
if (nxt_slow_path(action == NULL)) {
return NULL;
}
@@ -1525,7 +1537,7 @@ nxt_http_pass_application(nxt_task_t *task, nxt_router_conf_t *rtcf,
{
nxt_http_action_t *action;
action = nxt_mp_alloc(rtcf->mem_pool, sizeof(nxt_http_action_t));
action = nxt_mp_zalloc(rtcf->mem_pool, sizeof(nxt_http_action_t));
if (nxt_slow_path(action == NULL)) {
return NULL;
}