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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
typedef enum {
NXT_TSTR_CONST = 0,
NXT_TSTR_VAR,
#if (NXT_HAVE_NJS)
NXT_TSTR_JS,
#endif
} nxt_tstr_type_t;
struct nxt_tstr_s {
nxt_str_t str;
union {
nxt_var_t *var;
#if (NXT_HAVE_NJS)
nxt_js_t *js;
#endif
} u;
nxt_tstr_flags_t flags;
nxt_tstr_type_t type;
};
struct nxt_tstr_query_s {
nxt_mp_t *pool;
nxt_tstr_state_t *state;
nxt_tstr_cache_t *cache;
void *ctx;
void *data;
};
#define nxt_tstr_is_js(str) \
nxt_strchr_start(str, '`')
nxt_tstr_state_t *
nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test)
{
nxt_tstr_state_t *state;
state = nxt_mp_get(mp, sizeof(nxt_tstr_state_t));
if (nxt_slow_path(state == NULL)) {
return NULL;
}
state->pool = mp;
state->test = test;
state->var_refs = nxt_array_create(mp, 4, sizeof(nxt_var_ref_t));
if (nxt_slow_path(state->var_refs == NULL)) {
return NULL;
}
#if (NXT_HAVE_NJS)
state->jcf = nxt_js_conf_new(mp, test);
if (nxt_slow_path(state->jcf == NULL)) {
return NULL;
}
#endif
return state;
}
nxt_tstr_t *
nxt_tstr_compile(nxt_tstr_state_t *state, const nxt_str_t *str,
nxt_tstr_flags_t flags)
{
u_char *p;
nxt_tstr_t *tstr;
nxt_bool_t strz;
strz = (flags & NXT_TSTR_STRZ) != 0;
tstr = nxt_mp_get(state->pool, sizeof(nxt_tstr_t));
if (nxt_slow_path(tstr == NULL)) {
return NULL;
}
tstr->str.length = str->length + strz;
tstr->str.start = nxt_mp_nget(state->pool, tstr->str.length);
if (nxt_slow_path(tstr->str.start == NULL)) {
return NULL;
}
p = nxt_cpymem(tstr->str.start, str->start, str->length);
if (strz) {
*p = '\0';
}
tstr->flags = flags;
if (nxt_tstr_is_js(str)) {
#if (NXT_HAVE_NJS)
nxt_str_t tpl;
tstr->type = NXT_TSTR_JS;
nxt_tstr_str(tstr, &tpl);
tstr->u.js = nxt_js_add_tpl(state->jcf, &tpl, strz);
if (nxt_slow_path(tstr->u.js == NULL)) {
return NULL;
}
#endif
} else {
p = memchr(str->start, '$', str->length);
if (p != NULL) {
tstr->type = NXT_TSTR_VAR;
tstr->u.var = nxt_var_compile(state, &tstr->str);
if (nxt_slow_path(tstr->u.var == NULL)) {
return NULL;
}
} else {
tstr->type = NXT_TSTR_CONST;
}
}
return tstr;
}
nxt_int_t
nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error)
{
u_char *p;
if (nxt_tstr_is_js(str)) {
#if (NXT_HAVE_NJS)
return nxt_js_test(state->jcf, str, error);
#else
nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
"Unit is built without support of njs: "
"\"--njs\" ./configure option is missing.%Z");
return NXT_ERROR;
#endif
} else {
p = memchr(str->start, '$', str->length);
if (p != NULL) {
return nxt_var_test(state, str, error);
}
}
return NXT_OK;
}
nxt_int_t
nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error)
{
#if (NXT_HAVE_NJS)
if (!state->test) {
nxt_int_t ret;
ret = nxt_js_compile(state->jcf);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ERROR;
}
}
#endif
return NXT_OK;
}
void
nxt_tstr_state_release(nxt_tstr_state_t *state)
{
#if (NXT_HAVE_NJS)
nxt_js_conf_release(state->jcf);
#endif
}
nxt_int_t
nxt_tstr_cond_compile(nxt_tstr_state_t *state, nxt_str_t *str,
nxt_tstr_cond_t *cond)
{
if (str->length > 0 && str->start[0] == '!') {
cond->negate = 1;
str->start++;
str->length--;
}
cond->expr = nxt_tstr_compile(state, str, 0);
if (nxt_slow_path(cond->expr == NULL)) {
return NXT_ERROR;
}
return NXT_OK;
}
nxt_bool_t
nxt_tstr_is_const(nxt_tstr_t *tstr)
{
return (tstr->type == NXT_TSTR_CONST);
}
void
nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str)
{
*str = tstr->str;
if (tstr->flags & NXT_TSTR_STRZ) {
str->length--;
}
}
nxt_int_t
nxt_tstr_query_init(nxt_tstr_query_t **query_p, nxt_tstr_state_t *state,
nxt_tstr_cache_t *cache, void *ctx, nxt_mp_t *mp)
{
nxt_tstr_query_t *query;
query = *query_p;
if (*query_p == NULL) {
query = nxt_mp_zget(mp, sizeof(nxt_tstr_query_t));
if (nxt_slow_path(query == NULL)) {
return NXT_ERROR;
}
}
query->pool = mp;
query->state = state;
query->cache = cache;
query->ctx = ctx;
*query_p = query;
return NXT_OK;
}
nxt_int_t
nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr,
nxt_str_t *val)
{
nxt_int_t ret;
if (nxt_tstr_is_const(tstr)) {
nxt_tstr_str(tstr, val);
return NXT_OK;
}
if (tstr->type == NXT_TSTR_VAR) {
ret = nxt_var_interpreter(task, query->state, &query->cache->var,
tstr->u.var, val, query->ctx,
tstr->flags & NXT_TSTR_LOGGING);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ERROR;
}
} else {
#if (NXT_HAVE_NJS)
ret = nxt_js_call(task, query->state->jcf, &query->cache->js,
tstr->u.js, val, query->ctx);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ERROR;
}
#endif
}
if (tstr->flags & NXT_TSTR_STRZ) {
val->length--;
}
#if (NXT_DEBUG)
nxt_str_t str;
nxt_tstr_str(tstr, &str);
nxt_debug(task, "tstr query: \"%V\", result: \"%V\"", &str, val);
#endif
return NXT_OK;
}
void
nxt_tstr_query_release(nxt_tstr_query_t *query)
{
#if (NXT_HAVE_NJS)
nxt_js_release(&query->cache->js);
#endif
}
|