Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_script.c @ 6671:6b1b8c4b7a95
Realip: fixed uninitialized memory access.
Previously, the realip module could be left with uninitialized context after an
error in the ngx_http_realip_set_addr() function. That context could be later
accessed by $realip_remote_addr and $realip_remote_port variable handlers.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Thu, 01 Sep 2016 14:33:51 +0300 |
parents | af642539cd53 |
children | 0125b151c9a5 |
rev | line source |
---|---|
6607 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 static ngx_int_t ngx_stream_script_init_arrays( | |
14 ngx_stream_script_compile_t *sc); | |
15 static ngx_int_t ngx_stream_script_done(ngx_stream_script_compile_t *sc); | |
16 static ngx_int_t ngx_stream_script_add_copy_code( | |
17 ngx_stream_script_compile_t *sc, ngx_str_t *value, ngx_uint_t last); | |
18 static ngx_int_t ngx_stream_script_add_var_code( | |
19 ngx_stream_script_compile_t *sc, ngx_str_t *name); | |
20 #if (NGX_PCRE) | |
21 static ngx_int_t ngx_stream_script_add_capture_code( | |
22 ngx_stream_script_compile_t *sc, ngx_uint_t n); | |
23 #endif | |
24 static ngx_int_t ngx_stream_script_add_full_name_code( | |
25 ngx_stream_script_compile_t *sc); | |
26 static size_t ngx_stream_script_full_name_len_code( | |
27 ngx_stream_script_engine_t *e); | |
28 static void ngx_stream_script_full_name_code(ngx_stream_script_engine_t *e); | |
29 | |
30 | |
31 #define ngx_stream_script_exit (u_char *) &ngx_stream_script_exit_code | |
32 | |
33 static uintptr_t ngx_stream_script_exit_code = (uintptr_t) NULL; | |
34 | |
35 | |
36 void | |
37 ngx_stream_script_flush_complex_value(ngx_stream_session_t *s, | |
38 ngx_stream_complex_value_t *val) | |
39 { | |
40 ngx_uint_t *index; | |
41 | |
42 index = val->flushes; | |
43 | |
44 if (index) { | |
45 while (*index != (ngx_uint_t) -1) { | |
46 | |
47 if (s->variables[*index].no_cacheable) { | |
48 s->variables[*index].valid = 0; | |
49 s->variables[*index].not_found = 0; | |
50 } | |
51 | |
52 index++; | |
53 } | |
54 } | |
55 } | |
56 | |
57 | |
58 ngx_int_t | |
59 ngx_stream_complex_value(ngx_stream_session_t *s, | |
60 ngx_stream_complex_value_t *val, ngx_str_t *value) | |
61 { | |
62 size_t len; | |
63 ngx_stream_script_code_pt code; | |
64 ngx_stream_script_engine_t e; | |
65 ngx_stream_script_len_code_pt lcode; | |
66 | |
67 if (val->lengths == NULL) { | |
68 *value = val->value; | |
69 return NGX_OK; | |
70 } | |
71 | |
72 ngx_stream_script_flush_complex_value(s, val); | |
73 | |
74 ngx_memzero(&e, sizeof(ngx_stream_script_engine_t)); | |
75 | |
76 e.ip = val->lengths; | |
77 e.session = s; | |
78 e.flushed = 1; | |
79 | |
80 len = 0; | |
81 | |
82 while (*(uintptr_t *) e.ip) { | |
83 lcode = *(ngx_stream_script_len_code_pt *) e.ip; | |
84 len += lcode(&e); | |
85 } | |
86 | |
87 value->len = len; | |
88 value->data = ngx_pnalloc(s->connection->pool, len); | |
89 if (value->data == NULL) { | |
90 return NGX_ERROR; | |
91 } | |
92 | |
93 e.ip = val->values; | |
94 e.pos = value->data; | |
95 e.buf = *value; | |
96 | |
97 while (*(uintptr_t *) e.ip) { | |
98 code = *(ngx_stream_script_code_pt *) e.ip; | |
99 code((ngx_stream_script_engine_t *) &e); | |
100 } | |
101 | |
102 *value = e.buf; | |
103 | |
104 return NGX_OK; | |
105 } | |
106 | |
107 | |
108 ngx_int_t | |
109 ngx_stream_compile_complex_value(ngx_stream_compile_complex_value_t *ccv) | |
110 { | |
111 ngx_str_t *v; | |
112 ngx_uint_t i, n, nv, nc; | |
113 ngx_array_t flushes, lengths, values, *pf, *pl, *pv; | |
114 ngx_stream_script_compile_t sc; | |
115 | |
116 v = ccv->value; | |
117 | |
118 nv = 0; | |
119 nc = 0; | |
120 | |
121 for (i = 0; i < v->len; i++) { | |
122 if (v->data[i] == '$') { | |
123 if (v->data[i + 1] >= '1' && v->data[i + 1] <= '9') { | |
124 nc++; | |
125 | |
126 } else { | |
127 nv++; | |
128 } | |
129 } | |
130 } | |
131 | |
132 if ((v->len == 0 || v->data[0] != '$') | |
133 && (ccv->conf_prefix || ccv->root_prefix)) | |
134 { | |
135 if (ngx_conf_full_name(ccv->cf->cycle, v, ccv->conf_prefix) != NGX_OK) { | |
136 return NGX_ERROR; | |
137 } | |
138 | |
139 ccv->conf_prefix = 0; | |
140 ccv->root_prefix = 0; | |
141 } | |
142 | |
143 ccv->complex_value->value = *v; | |
144 ccv->complex_value->flushes = NULL; | |
145 ccv->complex_value->lengths = NULL; | |
146 ccv->complex_value->values = NULL; | |
147 | |
148 if (nv == 0 && nc == 0) { | |
149 return NGX_OK; | |
150 } | |
151 | |
152 n = nv + 1; | |
153 | |
154 if (ngx_array_init(&flushes, ccv->cf->pool, n, sizeof(ngx_uint_t)) | |
155 != NGX_OK) | |
156 { | |
157 return NGX_ERROR; | |
158 } | |
159 | |
160 n = nv * (2 * sizeof(ngx_stream_script_copy_code_t) | |
161 + sizeof(ngx_stream_script_var_code_t)) | |
162 + sizeof(uintptr_t); | |
163 | |
164 if (ngx_array_init(&lengths, ccv->cf->pool, n, 1) != NGX_OK) { | |
165 return NGX_ERROR; | |
166 } | |
167 | |
168 n = (nv * (2 * sizeof(ngx_stream_script_copy_code_t) | |
169 + sizeof(ngx_stream_script_var_code_t)) | |
170 + sizeof(uintptr_t) | |
171 + v->len | |
172 + sizeof(uintptr_t) - 1) | |
173 & ~(sizeof(uintptr_t) - 1); | |
174 | |
175 if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) { | |
176 return NGX_ERROR; | |
177 } | |
178 | |
179 pf = &flushes; | |
180 pl = &lengths; | |
181 pv = &values; | |
182 | |
183 ngx_memzero(&sc, sizeof(ngx_stream_script_compile_t)); | |
184 | |
185 sc.cf = ccv->cf; | |
186 sc.source = v; | |
187 sc.flushes = &pf; | |
188 sc.lengths = &pl; | |
189 sc.values = &pv; | |
190 sc.complete_lengths = 1; | |
191 sc.complete_values = 1; | |
192 sc.zero = ccv->zero; | |
193 sc.conf_prefix = ccv->conf_prefix; | |
194 sc.root_prefix = ccv->root_prefix; | |
195 | |
196 if (ngx_stream_script_compile(&sc) != NGX_OK) { | |
197 return NGX_ERROR; | |
198 } | |
199 | |
200 if (flushes.nelts) { | |
201 ccv->complex_value->flushes = flushes.elts; | |
202 ccv->complex_value->flushes[flushes.nelts] = (ngx_uint_t) -1; | |
203 } | |
204 | |
205 ccv->complex_value->lengths = lengths.elts; | |
206 ccv->complex_value->values = values.elts; | |
207 | |
208 return NGX_OK; | |
209 } | |
210 | |
211 | |
212 char * | |
213 ngx_stream_set_complex_value_slot(ngx_conf_t *cf, ngx_command_t *cmd, | |
214 void *conf) | |
215 { | |
216 char *p = conf; | |
217 | |
218 ngx_str_t *value; | |
219 ngx_stream_complex_value_t **cv; | |
220 ngx_stream_compile_complex_value_t ccv; | |
221 | |
222 cv = (ngx_stream_complex_value_t **) (p + cmd->offset); | |
223 | |
224 if (*cv != NULL) { | |
225 return "duplicate"; | |
226 } | |
227 | |
228 *cv = ngx_palloc(cf->pool, sizeof(ngx_stream_complex_value_t)); | |
229 if (*cv == NULL) { | |
230 return NGX_CONF_ERROR; | |
231 } | |
232 | |
233 value = cf->args->elts; | |
234 | |
235 ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t)); | |
236 | |
237 ccv.cf = cf; | |
238 ccv.value = &value[1]; | |
239 ccv.complex_value = *cv; | |
240 | |
241 if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) { | |
242 return NGX_CONF_ERROR; | |
243 } | |
244 | |
245 return NGX_CONF_OK; | |
246 } | |
247 | |
248 | |
249 ngx_uint_t | |
250 ngx_stream_script_variables_count(ngx_str_t *value) | |
251 { | |
252 ngx_uint_t i, n; | |
253 | |
254 for (n = 0, i = 0; i < value->len; i++) { | |
255 if (value->data[i] == '$') { | |
256 n++; | |
257 } | |
258 } | |
259 | |
260 return n; | |
261 } | |
262 | |
263 | |
264 ngx_int_t | |
265 ngx_stream_script_compile(ngx_stream_script_compile_t *sc) | |
266 { | |
267 u_char ch; | |
268 ngx_str_t name; | |
269 ngx_uint_t i, bracket; | |
270 | |
271 if (ngx_stream_script_init_arrays(sc) != NGX_OK) { | |
272 return NGX_ERROR; | |
273 } | |
274 | |
275 for (i = 0; i < sc->source->len; /* void */ ) { | |
276 | |
277 name.len = 0; | |
278 | |
279 if (sc->source->data[i] == '$') { | |
280 | |
281 if (++i == sc->source->len) { | |
282 goto invalid_variable; | |
283 } | |
284 | |
6644
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
285 if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') { |
6607 | 286 #if (NGX_PCRE) |
6644
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
287 ngx_uint_t n; |
6607 | 288 |
289 n = sc->source->data[i] - '0'; | |
290 | |
291 if (ngx_stream_script_add_capture_code(sc, n) != NGX_OK) { | |
292 return NGX_ERROR; | |
293 } | |
294 | |
295 i++; | |
296 | |
297 continue; | |
6644
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
298 #else |
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
299 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, |
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
300 "using variable \"$%c\" requires " |
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
301 "PCRE library", sc->source->data[i]); |
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
302 return NGX_ERROR; |
af642539cd53
Fixed regex captures handling without PCRE.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
303 #endif |
6607 | 304 } |
305 | |
306 if (sc->source->data[i] == '{') { | |
307 bracket = 1; | |
308 | |
309 if (++i == sc->source->len) { | |
310 goto invalid_variable; | |
311 } | |
312 | |
313 name.data = &sc->source->data[i]; | |
314 | |
315 } else { | |
316 bracket = 0; | |
317 name.data = &sc->source->data[i]; | |
318 } | |
319 | |
320 for ( /* void */ ; i < sc->source->len; i++, name.len++) { | |
321 ch = sc->source->data[i]; | |
322 | |
323 if (ch == '}' && bracket) { | |
324 i++; | |
325 bracket = 0; | |
326 break; | |
327 } | |
328 | |
329 if ((ch >= 'A' && ch <= 'Z') | |
330 || (ch >= 'a' && ch <= 'z') | |
331 || (ch >= '0' && ch <= '9') | |
332 || ch == '_') | |
333 { | |
334 continue; | |
335 } | |
336 | |
337 break; | |
338 } | |
339 | |
340 if (bracket) { | |
341 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, | |
342 "the closing bracket in \"%V\" " | |
343 "variable is missing", &name); | |
344 return NGX_ERROR; | |
345 } | |
346 | |
347 if (name.len == 0) { | |
348 goto invalid_variable; | |
349 } | |
350 | |
351 sc->variables++; | |
352 | |
353 if (ngx_stream_script_add_var_code(sc, &name) != NGX_OK) { | |
354 return NGX_ERROR; | |
355 } | |
356 | |
357 continue; | |
358 } | |
359 | |
360 name.data = &sc->source->data[i]; | |
361 | |
362 while (i < sc->source->len) { | |
363 | |
364 if (sc->source->data[i] == '$') { | |
365 break; | |
366 } | |
367 | |
368 i++; | |
369 name.len++; | |
370 } | |
371 | |
372 sc->size += name.len; | |
373 | |
374 if (ngx_stream_script_add_copy_code(sc, &name, (i == sc->source->len)) | |
375 != NGX_OK) | |
376 { | |
377 return NGX_ERROR; | |
378 } | |
379 } | |
380 | |
381 return ngx_stream_script_done(sc); | |
382 | |
383 invalid_variable: | |
384 | |
385 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name"); | |
386 | |
387 return NGX_ERROR; | |
388 } | |
389 | |
390 | |
391 static ngx_int_t | |
392 ngx_stream_script_init_arrays(ngx_stream_script_compile_t *sc) | |
393 { | |
394 ngx_uint_t n; | |
395 | |
396 if (sc->flushes && *sc->flushes == NULL) { | |
397 n = sc->variables ? sc->variables : 1; | |
398 *sc->flushes = ngx_array_create(sc->cf->pool, n, sizeof(ngx_uint_t)); | |
399 if (*sc->flushes == NULL) { | |
400 return NGX_ERROR; | |
401 } | |
402 } | |
403 | |
404 if (*sc->lengths == NULL) { | |
405 n = sc->variables * (2 * sizeof(ngx_stream_script_copy_code_t) | |
406 + sizeof(ngx_stream_script_var_code_t)) | |
407 + sizeof(uintptr_t); | |
408 | |
409 *sc->lengths = ngx_array_create(sc->cf->pool, n, 1); | |
410 if (*sc->lengths == NULL) { | |
411 return NGX_ERROR; | |
412 } | |
413 } | |
414 | |
415 if (*sc->values == NULL) { | |
416 n = (sc->variables * (2 * sizeof(ngx_stream_script_copy_code_t) | |
417 + sizeof(ngx_stream_script_var_code_t)) | |
418 + sizeof(uintptr_t) | |
419 + sc->source->len | |
420 + sizeof(uintptr_t) - 1) | |
421 & ~(sizeof(uintptr_t) - 1); | |
422 | |
423 *sc->values = ngx_array_create(sc->cf->pool, n, 1); | |
424 if (*sc->values == NULL) { | |
425 return NGX_ERROR; | |
426 } | |
427 } | |
428 | |
429 sc->variables = 0; | |
430 | |
431 return NGX_OK; | |
432 } | |
433 | |
434 | |
435 static ngx_int_t | |
436 ngx_stream_script_done(ngx_stream_script_compile_t *sc) | |
437 { | |
438 ngx_str_t zero; | |
439 uintptr_t *code; | |
440 | |
441 if (sc->zero) { | |
442 | |
443 zero.len = 1; | |
444 zero.data = (u_char *) "\0"; | |
445 | |
446 if (ngx_stream_script_add_copy_code(sc, &zero, 0) != NGX_OK) { | |
447 return NGX_ERROR; | |
448 } | |
449 } | |
450 | |
451 if (sc->conf_prefix || sc->root_prefix) { | |
452 if (ngx_stream_script_add_full_name_code(sc) != NGX_OK) { | |
453 return NGX_ERROR; | |
454 } | |
455 } | |
456 | |
457 if (sc->complete_lengths) { | |
458 code = ngx_stream_script_add_code(*sc->lengths, sizeof(uintptr_t), | |
459 NULL); | |
460 if (code == NULL) { | |
461 return NGX_ERROR; | |
462 } | |
463 | |
464 *code = (uintptr_t) NULL; | |
465 } | |
466 | |
467 if (sc->complete_values) { | |
468 code = ngx_stream_script_add_code(*sc->values, sizeof(uintptr_t), | |
469 &sc->main); | |
470 if (code == NULL) { | |
471 return NGX_ERROR; | |
472 } | |
473 | |
474 *code = (uintptr_t) NULL; | |
475 } | |
476 | |
477 return NGX_OK; | |
478 } | |
479 | |
480 | |
481 void * | |
482 ngx_stream_script_add_code(ngx_array_t *codes, size_t size, void *code) | |
483 { | |
484 u_char *elts, **p; | |
485 void *new; | |
486 | |
487 elts = codes->elts; | |
488 | |
489 new = ngx_array_push_n(codes, size); | |
490 if (new == NULL) { | |
491 return NULL; | |
492 } | |
493 | |
494 if (code) { | |
495 if (elts != codes->elts) { | |
496 p = code; | |
497 *p += (u_char *) codes->elts - elts; | |
498 } | |
499 } | |
500 | |
501 return new; | |
502 } | |
503 | |
504 | |
505 static ngx_int_t | |
506 ngx_stream_script_add_copy_code(ngx_stream_script_compile_t *sc, | |
507 ngx_str_t *value, ngx_uint_t last) | |
508 { | |
509 u_char *p; | |
510 size_t size, len, zero; | |
511 ngx_stream_script_copy_code_t *code; | |
512 | |
513 zero = (sc->zero && last); | |
514 len = value->len + zero; | |
515 | |
516 code = ngx_stream_script_add_code(*sc->lengths, | |
517 sizeof(ngx_stream_script_copy_code_t), | |
518 NULL); | |
519 if (code == NULL) { | |
520 return NGX_ERROR; | |
521 } | |
522 | |
523 code->code = (ngx_stream_script_code_pt) ngx_stream_script_copy_len_code; | |
524 code->len = len; | |
525 | |
526 size = (sizeof(ngx_stream_script_copy_code_t) + len + sizeof(uintptr_t) - 1) | |
527 & ~(sizeof(uintptr_t) - 1); | |
528 | |
529 code = ngx_stream_script_add_code(*sc->values, size, &sc->main); | |
530 if (code == NULL) { | |
531 return NGX_ERROR; | |
532 } | |
533 | |
534 code->code = ngx_stream_script_copy_code; | |
535 code->len = len; | |
536 | |
537 p = ngx_cpymem((u_char *) code + sizeof(ngx_stream_script_copy_code_t), | |
538 value->data, value->len); | |
539 | |
540 if (zero) { | |
541 *p = '\0'; | |
542 sc->zero = 0; | |
543 } | |
544 | |
545 return NGX_OK; | |
546 } | |
547 | |
548 | |
549 size_t | |
550 ngx_stream_script_copy_len_code(ngx_stream_script_engine_t *e) | |
551 { | |
552 ngx_stream_script_copy_code_t *code; | |
553 | |
554 code = (ngx_stream_script_copy_code_t *) e->ip; | |
555 | |
556 e->ip += sizeof(ngx_stream_script_copy_code_t); | |
557 | |
558 return code->len; | |
559 } | |
560 | |
561 | |
562 void | |
563 ngx_stream_script_copy_code(ngx_stream_script_engine_t *e) | |
564 { | |
565 u_char *p; | |
566 ngx_stream_script_copy_code_t *code; | |
567 | |
568 code = (ngx_stream_script_copy_code_t *) e->ip; | |
569 | |
570 p = e->pos; | |
571 | |
572 if (!e->skip) { | |
573 e->pos = ngx_copy(p, e->ip + sizeof(ngx_stream_script_copy_code_t), | |
574 code->len); | |
575 } | |
576 | |
577 e->ip += sizeof(ngx_stream_script_copy_code_t) | |
578 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1)); | |
579 | |
580 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, e->session->connection->log, 0, | |
581 "stream script copy: \"%*s\"", e->pos - p, p); | |
582 } | |
583 | |
584 | |
585 static ngx_int_t | |
586 ngx_stream_script_add_var_code(ngx_stream_script_compile_t *sc, ngx_str_t *name) | |
587 { | |
588 ngx_int_t index, *p; | |
589 ngx_stream_script_var_code_t *code; | |
590 | |
591 index = ngx_stream_get_variable_index(sc->cf, name); | |
592 | |
593 if (index == NGX_ERROR) { | |
594 return NGX_ERROR; | |
595 } | |
596 | |
597 if (sc->flushes) { | |
598 p = ngx_array_push(*sc->flushes); | |
599 if (p == NULL) { | |
600 return NGX_ERROR; | |
601 } | |
602 | |
603 *p = index; | |
604 } | |
605 | |
606 code = ngx_stream_script_add_code(*sc->lengths, | |
607 sizeof(ngx_stream_script_var_code_t), | |
608 NULL); | |
609 if (code == NULL) { | |
610 return NGX_ERROR; | |
611 } | |
612 | |
613 code->code = (ngx_stream_script_code_pt) | |
614 ngx_stream_script_copy_var_len_code; | |
615 code->index = (uintptr_t) index; | |
616 | |
617 code = ngx_stream_script_add_code(*sc->values, | |
618 sizeof(ngx_stream_script_var_code_t), | |
619 &sc->main); | |
620 if (code == NULL) { | |
621 return NGX_ERROR; | |
622 } | |
623 | |
624 code->code = ngx_stream_script_copy_var_code; | |
625 code->index = (uintptr_t) index; | |
626 | |
627 return NGX_OK; | |
628 } | |
629 | |
630 | |
631 size_t | |
632 ngx_stream_script_copy_var_len_code(ngx_stream_script_engine_t *e) | |
633 { | |
634 ngx_stream_variable_value_t *value; | |
635 ngx_stream_script_var_code_t *code; | |
636 | |
637 code = (ngx_stream_script_var_code_t *) e->ip; | |
638 | |
639 e->ip += sizeof(ngx_stream_script_var_code_t); | |
640 | |
641 if (e->flushed) { | |
642 value = ngx_stream_get_indexed_variable(e->session, code->index); | |
643 | |
644 } else { | |
645 value = ngx_stream_get_flushed_variable(e->session, code->index); | |
646 } | |
647 | |
648 if (value && !value->not_found) { | |
649 return value->len; | |
650 } | |
651 | |
652 return 0; | |
653 } | |
654 | |
655 | |
656 void | |
657 ngx_stream_script_copy_var_code(ngx_stream_script_engine_t *e) | |
658 { | |
659 u_char *p; | |
660 ngx_stream_variable_value_t *value; | |
661 ngx_stream_script_var_code_t *code; | |
662 | |
663 code = (ngx_stream_script_var_code_t *) e->ip; | |
664 | |
665 e->ip += sizeof(ngx_stream_script_var_code_t); | |
666 | |
667 if (!e->skip) { | |
668 | |
669 if (e->flushed) { | |
670 value = ngx_stream_get_indexed_variable(e->session, code->index); | |
671 | |
672 } else { | |
673 value = ngx_stream_get_flushed_variable(e->session, code->index); | |
674 } | |
675 | |
676 if (value && !value->not_found) { | |
677 p = e->pos; | |
678 e->pos = ngx_copy(p, value->data, value->len); | |
679 | |
680 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, | |
681 e->session->connection->log, 0, | |
682 "stream script var: \"%*s\"", e->pos - p, p); | |
683 } | |
684 } | |
685 } | |
686 | |
687 | |
688 #if (NGX_PCRE) | |
689 | |
690 static ngx_int_t | |
691 ngx_stream_script_add_capture_code(ngx_stream_script_compile_t *sc, | |
692 ngx_uint_t n) | |
693 { | |
694 ngx_stream_script_copy_capture_code_t *code; | |
695 | |
696 code = ngx_stream_script_add_code(*sc->lengths, | |
697 sizeof(ngx_stream_script_copy_capture_code_t), | |
698 NULL); | |
699 if (code == NULL) { | |
700 return NGX_ERROR; | |
701 } | |
702 | |
703 code->code = (ngx_stream_script_code_pt) | |
704 ngx_stream_script_copy_capture_len_code; | |
705 code->n = 2 * n; | |
706 | |
707 | |
708 code = ngx_stream_script_add_code(*sc->values, | |
709 sizeof(ngx_stream_script_copy_capture_code_t), | |
710 &sc->main); | |
711 if (code == NULL) { | |
712 return NGX_ERROR; | |
713 } | |
714 | |
715 code->code = ngx_stream_script_copy_capture_code; | |
716 code->n = 2 * n; | |
717 | |
718 if (sc->ncaptures < n) { | |
719 sc->ncaptures = n; | |
720 } | |
721 | |
722 return NGX_OK; | |
723 } | |
724 | |
725 | |
726 size_t | |
727 ngx_stream_script_copy_capture_len_code(ngx_stream_script_engine_t *e) | |
728 { | |
729 int *cap; | |
730 ngx_uint_t n; | |
731 ngx_stream_session_t *s; | |
732 ngx_stream_script_copy_capture_code_t *code; | |
733 | |
734 s = e->session; | |
735 | |
736 code = (ngx_stream_script_copy_capture_code_t *) e->ip; | |
737 | |
738 e->ip += sizeof(ngx_stream_script_copy_capture_code_t); | |
739 | |
740 n = code->n; | |
741 | |
742 if (n < s->ncaptures) { | |
743 cap = s->captures; | |
744 return cap[n + 1] - cap[n]; | |
745 } | |
746 | |
747 return 0; | |
748 } | |
749 | |
750 | |
751 void | |
752 ngx_stream_script_copy_capture_code(ngx_stream_script_engine_t *e) | |
753 { | |
754 int *cap; | |
755 u_char *p, *pos; | |
756 ngx_uint_t n; | |
757 ngx_stream_session_t *s; | |
758 ngx_stream_script_copy_capture_code_t *code; | |
759 | |
760 s = e->session; | |
761 | |
762 code = (ngx_stream_script_copy_capture_code_t *) e->ip; | |
763 | |
764 e->ip += sizeof(ngx_stream_script_copy_capture_code_t); | |
765 | |
766 n = code->n; | |
767 | |
768 pos = e->pos; | |
769 | |
770 if (n < s->ncaptures) { | |
771 cap = s->captures; | |
772 p = s->captures_data; | |
773 e->pos = ngx_copy(pos, &p[cap[n]], cap[n + 1] - cap[n]); | |
774 } | |
775 | |
776 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, e->session->connection->log, 0, | |
777 "stream script capture: \"%*s\"", e->pos - pos, pos); | |
778 } | |
779 | |
780 #endif | |
781 | |
782 | |
783 static ngx_int_t | |
784 ngx_stream_script_add_full_name_code(ngx_stream_script_compile_t *sc) | |
785 { | |
786 ngx_stream_script_full_name_code_t *code; | |
787 | |
788 code = ngx_stream_script_add_code(*sc->lengths, | |
789 sizeof(ngx_stream_script_full_name_code_t), | |
790 NULL); | |
791 if (code == NULL) { | |
792 return NGX_ERROR; | |
793 } | |
794 | |
795 code->code = (ngx_stream_script_code_pt) | |
796 ngx_stream_script_full_name_len_code; | |
797 code->conf_prefix = sc->conf_prefix; | |
798 | |
799 code = ngx_stream_script_add_code(*sc->values, | |
800 sizeof(ngx_stream_script_full_name_code_t), &sc->main); | |
801 if (code == NULL) { | |
802 return NGX_ERROR; | |
803 } | |
804 | |
805 code->code = ngx_stream_script_full_name_code; | |
806 code->conf_prefix = sc->conf_prefix; | |
807 | |
808 return NGX_OK; | |
809 } | |
810 | |
811 | |
812 static size_t | |
813 ngx_stream_script_full_name_len_code(ngx_stream_script_engine_t *e) | |
814 { | |
815 ngx_stream_script_full_name_code_t *code; | |
816 | |
817 code = (ngx_stream_script_full_name_code_t *) e->ip; | |
818 | |
819 e->ip += sizeof(ngx_stream_script_full_name_code_t); | |
820 | |
821 return code->conf_prefix ? ngx_cycle->conf_prefix.len: | |
822 ngx_cycle->prefix.len; | |
823 } | |
824 | |
825 | |
826 static void | |
827 ngx_stream_script_full_name_code(ngx_stream_script_engine_t *e) | |
828 { | |
829 ngx_stream_script_full_name_code_t *code; | |
830 | |
831 ngx_str_t value, *prefix; | |
832 | |
833 code = (ngx_stream_script_full_name_code_t *) e->ip; | |
834 | |
835 value.data = e->buf.data; | |
836 value.len = e->pos - e->buf.data; | |
837 | |
838 prefix = code->conf_prefix ? (ngx_str_t *) &ngx_cycle->conf_prefix: | |
839 (ngx_str_t *) &ngx_cycle->prefix; | |
840 | |
841 if (ngx_get_full_name(e->session->connection->pool, prefix, &value) | |
842 != NGX_OK) | |
843 { | |
844 e->ip = ngx_stream_script_exit; | |
845 return; | |
846 } | |
847 | |
848 e->buf = value; | |
849 | |
850 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, e->session->connection->log, 0, | |
851 "stream script fullname: \"%V\"", &value); | |
852 | |
853 e->ip += sizeof(ngx_stream_script_full_name_code_t); | |
854 } |