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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
struct nxt_var_s {
size_t length;
nxt_uint_t vars;
nxt_var_flags_t flags;
u_char data[];
/*
nxt_var_sub_t subs[vars];
u_char raw[length];
*/
};
typedef struct {
uint32_t index;
uint32_t length;
uint32_t position;
} nxt_var_sub_t;
struct nxt_var_query_s {
nxt_mp_t *pool;
nxt_lvlhsh_t cache;
nxt_str_t *spare;
nxt_uint_t waiting;
nxt_uint_t failed; /* 1 bit */
void *ctx;
void *data;
nxt_work_handler_t ready;
nxt_work_handler_t error;
};
#define nxt_var_subs(var) ((nxt_var_sub_t *) (var)->data)
#define nxt_var_raw_start(var) \
((var)->data + (var)->vars * sizeof(nxt_var_sub_t))
static nxt_int_t nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
static nxt_var_decl_t *nxt_var_hash_find(nxt_str_t *name);
static nxt_var_decl_t *nxt_var_decl_get(nxt_str_t *name, nxt_array_t *fields,
uint32_t *index);
static nxt_var_field_t *nxt_var_field_add(nxt_array_t *fields, nxt_str_t *name,
uint32_t hash);
static nxt_int_t nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data);
static nxt_str_t *nxt_var_cache_value(nxt_task_t *task, nxt_var_query_t *query,
uint32_t index);
static u_char *nxt_var_next_part(u_char *start, size_t length, nxt_str_t *part,
nxt_bool_t *is_var);
static const nxt_lvlhsh_proto_t nxt_var_hash_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_var_hash_test,
nxt_lvlhsh_alloc,
nxt_lvlhsh_free,
};
static const nxt_lvlhsh_proto_t nxt_var_cache_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_var_cache_test,
nxt_mp_lvlhsh_alloc,
nxt_mp_lvlhsh_free,
};
static nxt_lvlhsh_t nxt_var_hash;
static uint32_t nxt_var_count;
static nxt_var_handler_t *nxt_var_index;
static nxt_int_t
nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
{
nxt_var_decl_t *decl;
decl = data;
return nxt_strstr_eq(&lhq->key, &decl->name) ? NXT_OK : NXT_DECLINED;
}
static nxt_var_decl_t *
nxt_var_hash_find(nxt_str_t *name)
{
nxt_lvlhsh_query_t lhq;
lhq.key_hash = nxt_djb_hash(name->start, name->length);
lhq.key = *name;
lhq.proto = &nxt_var_hash_proto;
if (nxt_lvlhsh_find(&nxt_var_hash, &lhq) != NXT_OK) {
return NULL;
}
return lhq.value;
}
static nxt_var_decl_t *
nxt_var_decl_get(nxt_str_t *name, nxt_array_t *fields, uint32_t *index)
{
u_char *p, *end;
int64_t hash;
uint16_t field;
nxt_str_t str;
nxt_var_decl_t *decl;
nxt_var_field_t *f;
f = NULL;
field = 0;
decl = nxt_var_hash_find(name);
if (decl == NULL) {
p = name->start;
end = p + name->length;
while (p < end) {
if (*p++ == '_') {
break;
}
}
if (p == end) {
return NULL;
}
str.start = name->start;
str.length = p - 1 - name->start;
decl = nxt_var_hash_find(&str);
if (decl != NULL) {
str.start = p;
str.length = end - p;
hash = decl->field_hash(fields->mem_pool, &str);
if (nxt_slow_path(hash == -1)) {
return NULL;
}
f = nxt_var_field_add(fields, &str, (uint32_t) hash);
if (nxt_slow_path(f == NULL)) {
return NULL;
}
field = f->index;
}
}
if (decl != NULL) {
if (decl->field_hash != NULL && f == NULL) {
return NULL;
}
if (index != NULL) {
*index = (decl->index << 16) | field;
}
}
return decl;
}
static nxt_var_field_t *
nxt_var_field_add(nxt_array_t *fields, nxt_str_t *name, uint32_t hash)
{
nxt_uint_t i;
nxt_var_field_t *field;
field = fields->elts;
for (i = 0; i < fields->nelts; i++) {
if (field[i].hash == hash
&& nxt_strstr_eq(&field[i].name, name))
{
return field;
}
}
field = nxt_array_add(fields);
if (nxt_slow_path(field == NULL)) {
return NULL;
}
field->name = *name;
field->hash = hash;
field->index = fields->nelts - 1;
return field;
}
nxt_var_field_t *
nxt_var_field_get(nxt_array_t *fields, uint16_t index)
{
nxt_uint_t nfields;
nxt_var_field_t *field;
field = fields->elts;
nfields = fields->nelts;
if (nfields > 0 && index <= nfields) {
return &field[index];
}
return NULL;
}
static nxt_int_t
nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data)
{
return NXT_OK;
}
static nxt_str_t *
nxt_var_cache_value(nxt_task_t *task, nxt_var_query_t *query, uint32_t index)
{
nxt_int_t ret;
nxt_str_t *value;
nxt_lvlhsh_query_t lhq;
value = query->spare;
if (value == NULL) {
value = nxt_mp_zget(query->pool, sizeof(nxt_str_t));
if (nxt_slow_path(value == NULL)) {
return NULL;
}
query->spare = value;
}
lhq.key_hash = nxt_murmur_hash2_uint32(&index);
lhq.replace = 0;
lhq.key.length = sizeof(uint32_t);
lhq.key.start = (u_char *) &index;
lhq.value = value;
lhq.proto = &nxt_var_cache_proto;
lhq.pool = query->pool;
ret = nxt_lvlhsh_insert(&query->cache, &lhq);
if (nxt_slow_path(ret == NXT_ERROR)) {
return NULL;
}
if (ret == NXT_OK) {
ret = nxt_var_index[index >> 16](task, value, query->ctx,
index & 0xffff);
if (nxt_slow_path(ret != NXT_OK)) {
return NULL;
}
query->spare = NULL;
}
return lhq.value;
}
nxt_int_t
nxt_var_register(nxt_var_decl_t *decl, size_t n)
{
nxt_uint_t i;
nxt_lvlhsh_query_t lhq;
lhq.replace = 0;
lhq.proto = &nxt_var_hash_proto;
for (i = 0; i < n; i++) {
lhq.key = decl[i].name;
lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
lhq.value = &decl[i];
if (nxt_slow_path(nxt_lvlhsh_insert(&nxt_var_hash, &lhq) != NXT_OK)) {
return NXT_ERROR;
}
}
nxt_var_count += n;
return NXT_OK;
}
nxt_int_t
nxt_var_index_init(void)
{
nxt_uint_t i;
nxt_var_decl_t *decl;
nxt_var_handler_t *index;
nxt_lvlhsh_each_t lhe;
index = nxt_memalign(64, nxt_var_count * sizeof(nxt_var_handler_t));
if (index == NULL) {
return NXT_ERROR;
}
nxt_lvlhsh_each_init(&lhe, &nxt_var_hash_proto);
for (i = 0; i < nxt_var_count; i++) {
decl = nxt_lvlhsh_each(&nxt_var_hash, &lhe);
decl->index = i;
index[i] = decl->handler;
}
nxt_var_index = index;
return NXT_OK;
}
nxt_var_t *
nxt_var_compile(nxt_str_t *str, nxt_mp_t *mp, nxt_array_t *fields,
nxt_var_flags_t flags)
{
u_char *p, *end, *next, *src;
size_t size;
uint32_t index;
nxt_bool_t strz;
nxt_var_t *var;
nxt_str_t part;
nxt_uint_t n;
nxt_bool_t is_var;
nxt_var_sub_t *subs;
nxt_var_decl_t *decl;
strz = (flags & NXT_VAR_STRZ) != 0;
n = 0;
p = str->start;
end = p + str->length;
while (p < end) {
p = nxt_var_next_part(p, end - p, &part, &is_var);
if (nxt_slow_path(p == NULL)) {
return NULL;
}
if (is_var) {
n++;
}
}
size = sizeof(nxt_var_t) + n * sizeof(nxt_var_sub_t) + str->length;
var = nxt_mp_get(mp, size + strz);
if (nxt_slow_path(var == NULL)) {
return NULL;
}
var->length = str->length;
var->vars = n;
var->flags = flags;
subs = nxt_var_subs(var);
src = nxt_var_raw_start(var);
nxt_memcpy(src, str->start, str->length);
if (strz) {
src[str->length] = '\0';
}
n = 0;
p = str->start;
while (p < end) {
next = nxt_var_next_part(p, end - p, &part, &is_var);
if (is_var) {
decl = nxt_var_decl_get(&part, fields, &index);
if (nxt_slow_path(decl == NULL)) {
return NULL;
}
subs[n].index = index;
subs[n].length = next - p;
subs[n].position = p - str->start;
n++;
}
p = next;
}
return var;
}
nxt_int_t
nxt_var_test(nxt_str_t *str, nxt_array_t *fields, u_char *error)
{
u_char *p, *end, *next;
nxt_str_t part;
nxt_bool_t is_var;
nxt_var_decl_t *decl;
p = str->start;
end = p + str->length;
while (p < end) {
next = nxt_var_next_part(p, end - p, &part, &is_var);
if (next == NULL) {
nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
"Invalid variable at position %uz%Z", p - str->start);
return NXT_ERROR;
}
if (is_var) {
decl = nxt_var_decl_get(&part, fields, NULL);
if (decl == NULL) {
nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
"Unknown variable \"%V\"%Z", &part);
return NXT_ERROR;
}
}
p = next;
}
return NXT_OK;
}
static u_char *
nxt_var_next_part(u_char *start, size_t length, nxt_str_t *part,
nxt_bool_t *is_var)
{
u_char *p, *end, ch, c;
nxt_bool_t bracket;
end = start + length;
p = nxt_memchr(start, '$', length);
if (p == start) {
*is_var = 1;
p++;
if (p == end) {
return NULL;
}
if (*p == '{') {
bracket = 1;
if (end - p < 2) {
return NULL;
}
p++;
} else {
bracket = 0;
}
start = p;
for ( ;; ) {
ch = *p;
c = (u_char) (ch | 0x20);
if ((c < 'a' || c > 'z') && ch != '_') {
if (bracket && ch != '}') {
return NULL;
}
break;
}
p++;
if (p == end) {
if (bracket) {
return NULL;
}
break;
}
}
length = p - start;
end = p + bracket;
} else {
*is_var = 0;
if (p != NULL) {
length = p - start;
end = p;
}
}
part->length = length;
part->start = start;
return end;
}
inline void
nxt_var_raw(nxt_var_t *var, nxt_str_t *str)
{
str->length = var->length;
str->start = nxt_var_raw_start(var);
}
inline nxt_bool_t
nxt_var_is_const(nxt_var_t *var)
{
return (var->vars == 0);
}
nxt_int_t
nxt_var_query_init(nxt_var_query_t **query_p, void *ctx, nxt_mp_t *mp)
{
nxt_var_query_t *query;
query = *query_p;
if (*query_p == NULL) {
query = nxt_mp_zget(mp, sizeof(nxt_var_query_t));
if (nxt_slow_path(query == NULL)) {
return NXT_ERROR;
}
}
query->pool = mp;
query->ctx = ctx;
*query_p = query;
return NXT_OK;
}
void
nxt_var_query(nxt_task_t *task, nxt_var_query_t *query, nxt_var_t *var,
nxt_str_t *str)
{
u_char *p, *src;
size_t length, last, next;
nxt_str_t *value, **part;
nxt_uint_t i;
nxt_bool_t strz, logging;
nxt_array_t parts;
nxt_var_sub_t *subs;
if (nxt_var_is_const(var)) {
nxt_var_raw(var, str);
return;
}
if (nxt_slow_path(query->failed)) {
return;
}
nxt_memzero(&parts, sizeof(nxt_array_t));
nxt_array_init(&parts, query->pool, sizeof(nxt_str_t *));
strz = (var->flags & NXT_VAR_STRZ) != 0;
logging = (var->flags & NXT_VAR_LOGGING) != 0;
subs = nxt_var_subs(var);
length = var->length;
for (i = 0; i < var->vars; i++) {
value = nxt_var_cache_value(task, query, subs[i].index);
if (nxt_slow_path(value == NULL)) {
goto fail;
}
part = nxt_array_add(&parts);
if (nxt_slow_path(part == NULL)) {
goto fail;
}
*part = value;
length += value->length - subs[i].length;
if (logging && value->start == NULL) {
length += 1;
}
}
p = nxt_mp_nget(query->pool, length + strz);
if (nxt_slow_path(p == NULL)) {
goto fail;
}
str->length = length;
str->start = p;
part = parts.elts;
src = nxt_var_raw_start(var);
last = 0;
for (i = 0; i < var->vars; i++) {
next = subs[i].position;
if (next != last) {
p = nxt_cpymem(p, &src[last], next - last);
}
p = nxt_cpymem(p, part[i]->start, part[i]->length);
if (logging && part[i]->start == NULL) {
*p++ = '-';
}
last = next + subs[i].length;
}
if (last != var->length) {
p = nxt_cpymem(p, &src[last], var->length - last);
}
if (strz) {
*p = '\0';
}
nxt_debug(task, "var: \"%*s\" -> \"%V\"", length, src, str);
return;
fail:
query->failed = 1;
}
void
nxt_var_query_resolve(nxt_task_t *task, nxt_var_query_t *query, void *data,
nxt_work_handler_t ready, nxt_work_handler_t error)
{
query->data = data;
query->ready = ready;
query->error = error;
if (query->waiting == 0) {
nxt_work_queue_add(&task->thread->engine->fast_work_queue,
query->failed ? query->error : query->ready,
task, query->ctx, query->data);
}
}
void
nxt_var_query_handle(nxt_task_t *task, nxt_var_query_t *query,
nxt_bool_t failed)
{
query->failed |= failed;
if (--query->waiting == 0) {
nxt_work_queue_add(&task->thread->engine->fast_work_queue,
query->failed ? query->error : query->ready,
task, query->ctx, query->data);
}
}
|