Coverage Report

Created: 2025-07-11 06:49

/src/unit/src/nxt_http_rewrite.c
Line
Count
Source (jump to first uncovered line)
1
2
/*
3
 * Copyright (C) Zhidao HONG
4
 * Copyright (C) NGINX, Inc.
5
 */
6
7
#include <nxt_router.h>
8
#include <nxt_http.h>
9
10
11
nxt_int_t
12
nxt_http_rewrite_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action,
13
    nxt_http_action_conf_t *acf)
14
0
{
15
0
    nxt_str_t  str;
16
17
0
    nxt_conf_get_string(acf->rewrite, &str);
18
19
0
    action->rewrite = nxt_tstr_compile(rtcf->tstr_state, &str, 0);
20
0
    if (nxt_slow_path(action->rewrite == NULL)) {
21
0
        return NXT_ERROR;
22
0
    }
23
24
0
    return NXT_OK;
25
0
}
26
27
28
nxt_int_t
29
nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r)
30
0
{
31
0
    nxt_int_t                 ret;
32
0
    nxt_str_t                 str;
33
0
    nxt_router_conf_t         *rtcf;
34
0
    nxt_http_action_t         *action;
35
0
    nxt_http_request_parse_t  rp;
36
37
0
    action = r->action;
38
39
0
    if (action == NULL || action->rewrite == NULL) {
40
0
        return NXT_OK;
41
0
    }
42
43
0
    if (nxt_tstr_is_const(action->rewrite)) {
44
0
        nxt_tstr_str(action->rewrite, &str);
45
46
0
    } else {
47
0
        rtcf = r->conf->socket_conf->router_conf;
48
49
0
        ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state,
50
0
                                  &r->tstr_cache, r, r->mem_pool);
51
0
        if (nxt_slow_path(ret != NXT_OK)) {
52
0
            return NXT_ERROR;
53
0
        }
54
55
0
        ret = nxt_tstr_query(task, r->tstr_query, action->rewrite, &str);
56
0
        if (nxt_slow_path(ret != NXT_OK)) {
57
0
            return NXT_ERROR;
58
0
        }
59
0
    }
60
61
0
    nxt_memzero(&rp, sizeof(nxt_http_request_parse_t));
62
63
0
    rp.mem_pool = r->mem_pool;
64
65
0
    rp.target_start = str.start;
66
0
    rp.target_end = str.start + str.length;
67
68
0
    ret = nxt_http_parse_complex_target(&rp);
69
0
    if (nxt_slow_path(ret != NXT_OK)) {
70
0
        return NXT_ERROR;
71
0
    }
72
73
0
    r->path = nxt_mp_alloc(r->mem_pool, sizeof(nxt_str_t));
74
0
    if (nxt_slow_path(r->path == NULL)) {
75
0
        return NXT_ERROR;
76
0
    }
77
78
0
    *r->path = rp.path;
79
80
0
    r->uri_changed = 1;
81
0
    r->quoted_target = rp.quoted_target;
82
83
0
    if (nxt_slow_path(r->log_route)) {
84
0
        nxt_log(task, NXT_LOG_NOTICE, "URI rewritten to \"%V\"", r->path);
85
0
    }
86
87
0
    return NXT_OK;
88
0
}