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