nxt_var.c (2146:362258b173b3) nxt_var.c (2147:7bf58b1b18c4)
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6#include <nxt_main.h>
7
8

--- 38 unchanged lines hidden (view full) ---

47
48#define nxt_var_raw_start(var) \
49 ((var)->data + (var)->vars * sizeof(nxt_var_sub_t))
50
51
52static nxt_int_t nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
53static nxt_var_decl_t *nxt_var_hash_find(nxt_str_t *name);
54
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6#include <nxt_main.h>
7
8

--- 38 unchanged lines hidden (view full) ---

47
48#define nxt_var_raw_start(var) \
49 ((var)->data + (var)->vars * sizeof(nxt_var_sub_t))
50
51
52static nxt_int_t nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
53static nxt_var_decl_t *nxt_var_hash_find(nxt_str_t *name);
54
55static nxt_var_decl_t *nxt_var_decl_get(nxt_str_t *name, nxt_array_t *fields,
56 uint32_t *index);
57static nxt_var_field_t *nxt_var_field_add(nxt_array_t *fields, nxt_str_t *name,
58 uint32_t hash);
59
55static nxt_int_t nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data);
56static nxt_str_t *nxt_var_cache_value(nxt_task_t *task, nxt_var_query_t *query,
57 uint32_t index);
58
59static u_char *nxt_var_next_part(u_char *start, size_t length, nxt_str_t *part,
60 nxt_bool_t *is_var);
61
62

--- 41 unchanged lines hidden (view full) ---

104 if (nxt_lvlhsh_find(&nxt_var_hash, &lhq) != NXT_OK) {
105 return NULL;
106 }
107
108 return lhq.value;
109}
110
111
60static nxt_int_t nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data);
61static nxt_str_t *nxt_var_cache_value(nxt_task_t *task, nxt_var_query_t *query,
62 uint32_t index);
63
64static u_char *nxt_var_next_part(u_char *start, size_t length, nxt_str_t *part,
65 nxt_bool_t *is_var);
66
67

--- 41 unchanged lines hidden (view full) ---

109 if (nxt_lvlhsh_find(&nxt_var_hash, &lhq) != NXT_OK) {
110 return NULL;
111 }
112
113 return lhq.value;
114}
115
116
117static nxt_var_decl_t *
118nxt_var_decl_get(nxt_str_t *name, nxt_array_t *fields, uint32_t *index)
119{
120 u_char *p, *end;
121 int64_t hash;
122 uint16_t field;
123 nxt_str_t str;
124 nxt_var_decl_t *decl;
125 nxt_var_field_t *f;
126
127 f = NULL;
128 field = 0;
129 decl = nxt_var_hash_find(name);
130
131 if (decl == NULL) {
132 p = name->start;
133 end = p + name->length;
134
135 while (p < end) {
136 if (*p++ == '_') {
137 break;
138 }
139 }
140
141 if (p == end) {
142 return NULL;
143 }
144
145 str.start = name->start;
146 str.length = p - 1 - name->start;
147
148 decl = nxt_var_hash_find(&str);
149
150 if (decl != NULL) {
151 str.start = p;
152 str.length = end - p;
153
154 hash = decl->field_hash(fields->mem_pool, &str);
155 if (nxt_slow_path(hash == -1)) {
156 return NULL;
157 }
158
159 f = nxt_var_field_add(fields, &str, (uint32_t) hash);
160 if (nxt_slow_path(f == NULL)) {
161 return NULL;
162 }
163
164 field = f->index;
165 }
166 }
167
168 if (decl != NULL) {
169 if (decl->field_hash != NULL && f == NULL) {
170 return NULL;
171 }
172
173 if (index != NULL) {
174 *index = (decl->index << 16) | field;
175 }
176 }
177
178 return decl;
179}
180
181
182static nxt_var_field_t *
183nxt_var_field_add(nxt_array_t *fields, nxt_str_t *name, uint32_t hash)
184{
185 nxt_uint_t i;
186 nxt_var_field_t *field;
187
188 field = fields->elts;
189
190 for (i = 0; i < fields->nelts; i++) {
191 if (field[i].hash == hash
192 && nxt_strstr_eq(&field[i].name, name))
193 {
194 return field;
195 }
196 }
197
198 field = nxt_array_add(fields);
199 if (nxt_slow_path(field == NULL)) {
200 return NULL;
201 }
202
203 field->name = *name;
204 field->hash = hash;
205 field->index = fields->nelts - 1;
206
207 return field;
208}
209
210
211nxt_var_field_t *
212nxt_var_field_get(nxt_array_t *fields, uint16_t index)
213{
214 nxt_uint_t nfields;
215 nxt_var_field_t *field;
216
217 field = fields->elts;
218 nfields = fields->nelts;
219
220 if (nfields > 0 && index <= nfields) {
221 return &field[index];
222 }
223
224 return NULL;
225}
226
227
112static nxt_int_t
113nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data)
114{
115 return NXT_OK;
116}
117
118
119static nxt_str_t *

--- 23 unchanged lines hidden (view full) ---

143 lhq.pool = query->pool;
144
145 ret = nxt_lvlhsh_insert(&query->cache, &lhq);
146 if (nxt_slow_path(ret == NXT_ERROR)) {
147 return NULL;
148 }
149
150 if (ret == NXT_OK) {
228static nxt_int_t
229nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data)
230{
231 return NXT_OK;
232}
233
234
235static nxt_str_t *

--- 23 unchanged lines hidden (view full) ---

259 lhq.pool = query->pool;
260
261 ret = nxt_lvlhsh_insert(&query->cache, &lhq);
262 if (nxt_slow_path(ret == NXT_ERROR)) {
263 return NULL;
264 }
265
266 if (ret == NXT_OK) {
151 ret = nxt_var_index[index](task, value, query->ctx);
267 ret = nxt_var_index[index >> 16](task, value, query->ctx,
268 index & 0xffff);
152 if (nxt_slow_path(ret != NXT_OK)) {
153 return NULL;
154 }
155
156 query->spare = NULL;
157 }
158
159 return lhq.value;

--- 48 unchanged lines hidden (view full) ---

208
209 nxt_var_index = index;
210
211 return NXT_OK;
212}
213
214
215nxt_var_t *
269 if (nxt_slow_path(ret != NXT_OK)) {
270 return NULL;
271 }
272
273 query->spare = NULL;
274 }
275
276 return lhq.value;

--- 48 unchanged lines hidden (view full) ---

325
326 nxt_var_index = index;
327
328 return NXT_OK;
329}
330
331
332nxt_var_t *
216nxt_var_compile(nxt_str_t *str, nxt_mp_t *mp, nxt_bool_t strz)
333nxt_var_compile(nxt_str_t *str, nxt_mp_t *mp, nxt_array_t *fields,
334 nxt_bool_t strz)
217{
218 u_char *p, *end, *next, *src;
219 size_t size;
335{
336 u_char *p, *end, *next, *src;
337 size_t size;
338 uint32_t index;
220 nxt_var_t *var;
221 nxt_str_t part;
222 nxt_uint_t n;
223 nxt_bool_t is_var;
224 nxt_var_sub_t *subs;
225 nxt_var_decl_t *decl;
226
227 n = 0;

--- 34 unchanged lines hidden (view full) ---

262
263 n = 0;
264 p = str->start;
265
266 while (p < end) {
267 next = nxt_var_next_part(p, end - p, &part, &is_var);
268
269 if (is_var) {
339 nxt_var_t *var;
340 nxt_str_t part;
341 nxt_uint_t n;
342 nxt_bool_t is_var;
343 nxt_var_sub_t *subs;
344 nxt_var_decl_t *decl;
345
346 n = 0;

--- 34 unchanged lines hidden (view full) ---

381
382 n = 0;
383 p = str->start;
384
385 while (p < end) {
386 next = nxt_var_next_part(p, end - p, &part, &is_var);
387
388 if (is_var) {
270 decl = nxt_var_hash_find(&part);
389 decl = nxt_var_decl_get(&part, fields, &index);
271 if (nxt_slow_path(decl == NULL)) {
272 return NULL;
273 }
274
390 if (nxt_slow_path(decl == NULL)) {
391 return NULL;
392 }
393
275 subs[n].index = decl->index;
394 subs[n].index = index;
276 subs[n].length = next - p;
277 subs[n].position = p - str->start;
278
279 n++;
280 }
281
282 p = next;
283 }
284
285 return var;
286}
287
288
289nxt_int_t
395 subs[n].length = next - p;
396 subs[n].position = p - str->start;
397
398 n++;
399 }
400
401 p = next;
402 }
403
404 return var;
405}
406
407
408nxt_int_t
290nxt_var_test(nxt_str_t *str, u_char *error)
409nxt_var_test(nxt_str_t *str, nxt_array_t *fields, u_char *error)
291{
292 u_char *p, *end, *next;
293 nxt_str_t part;
294 nxt_bool_t is_var;
295 nxt_var_decl_t *decl;
296
297 p = str->start;
298 end = p + str->length;

--- 4 unchanged lines hidden (view full) ---

303 if (next == NULL) {
304 nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
305 "Invalid variable at position %uz%Z", p - str->start);
306
307 return NXT_ERROR;
308 }
309
310 if (is_var) {
410{
411 u_char *p, *end, *next;
412 nxt_str_t part;
413 nxt_bool_t is_var;
414 nxt_var_decl_t *decl;
415
416 p = str->start;
417 end = p + str->length;

--- 4 unchanged lines hidden (view full) ---

422 if (next == NULL) {
423 nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
424 "Invalid variable at position %uz%Z", p - str->start);
425
426 return NXT_ERROR;
427 }
428
429 if (is_var) {
311 decl = nxt_var_hash_find(&part);
430 decl = nxt_var_decl_get(&part, fields, NULL);
312
313 if (decl == NULL) {
314 nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
315 "Unknown variable \"%V\"%Z", &part);
316
317 return NXT_ERROR;
318 }
319 }

--- 238 unchanged lines hidden ---
431
432 if (decl == NULL) {
433 nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
434 "Unknown variable \"%V\"%Z", &part);
435
436 return NXT_ERROR;
437 }
438 }

--- 238 unchanged lines hidden ---