~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/http/ngx_http_script.c

Version: ~ [ nginx-0.8.20 ] ~ [ nginx-0.7.62 ] ~ [ nginx-0.6.39 ] ~

  1 
  2 /*
  3  * Copyright (C) Igor Sysoev
  4  */
  5 
  6 
  7 #include <ngx_config.h>
  8 #include <ngx_core.h>
  9 #include <ngx_http.h>
 10 
 11 
 12 static ngx_int_t ngx_http_script_init_arrays(ngx_http_script_compile_t *sc);
 13 static ngx_int_t ngx_http_script_done(ngx_http_script_compile_t *sc);
 14 static ngx_int_t ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc,
 15     ngx_str_t *value, ngx_uint_t last);
 16 static ngx_int_t ngx_http_script_add_var_code(ngx_http_script_compile_t *sc,
 17     ngx_str_t *name);
 18 static ngx_int_t ngx_http_script_add_args_code(ngx_http_script_compile_t *sc);
 19 #if (NGX_PCRE)
 20 static ngx_int_t ngx_http_script_add_capture_code(ngx_http_script_compile_t *sc,
 21      ngx_uint_t n);
 22 #endif
 23 static ngx_int_t
 24      ngx_http_script_add_full_name_code(ngx_http_script_compile_t *sc);
 25 static size_t ngx_http_script_full_name_len_code(ngx_http_script_engine_t *e);
 26 static void ngx_http_script_full_name_code(ngx_http_script_engine_t *e);
 27 
 28 
 29 #define ngx_http_script_exit  (u_char *) &ngx_http_script_exit_code
 30 
 31 static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL;
 32 
 33 
 34 void
 35 ngx_http_script_flush_complex_value(ngx_http_request_t *r,
 36     ngx_http_complex_value_t *val)
 37 {
 38     ngx_uint_t *index;
 39 
 40     index = val->flushes;
 41 
 42     if (index) {
 43         while (*index != (ngx_uint_t) -1) {
 44 
 45             if (r->variables[*index].no_cacheable) {
 46                 r->variables[*index].valid = 0;
 47                 r->variables[*index].not_found = 0;
 48             }
 49 
 50             index++;
 51         }
 52     }
 53 }
 54 
 55 
 56 ngx_int_t
 57 ngx_http_complex_value(ngx_http_request_t *r, ngx_http_complex_value_t *val,
 58     ngx_str_t *value)
 59 {
 60     size_t                        len;
 61     ngx_http_script_code_pt       code;
 62     ngx_http_script_len_code_pt   lcode;
 63     ngx_http_script_engine_t      e;
 64 
 65     if (val->lengths == NULL) {
 66         *value = val->value;
 67         return NGX_OK;
 68     }
 69 
 70     ngx_http_script_flush_complex_value(r, val);
 71 
 72     ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
 73 
 74     e.ip = val->lengths;
 75     e.request = r;
 76     e.flushed = 1;
 77 
 78     len = 0;
 79 
 80     while (*(uintptr_t *) e.ip) {
 81         lcode = *(ngx_http_script_len_code_pt *) e.ip;
 82         len += lcode(&e);
 83     }
 84 
 85     value->len = len;
 86     value->data = ngx_pnalloc(r->pool, len);
 87     if (value->data == NULL) {
 88         return NGX_ERROR;
 89     }
 90 
 91     e.ip = val->values;
 92     e.pos = value->data;
 93     e.buf = *value;
 94 
 95     while (*(uintptr_t *) e.ip) {
 96         code = *(ngx_http_script_code_pt *) e.ip;
 97         code((ngx_http_script_engine_t *) &e);
 98     }
 99 
100     *value = e.buf;
101 
102     return NGX_OK;
103 }
104 
105 
106 ngx_int_t
107 ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)
108 {
109     ngx_str_t                  *v;
110     ngx_uint_t                  i, n, nv, nc;
111     ngx_array_t                 flushes, lengths, values, *pf, *pl, *pv;
112     ngx_http_script_compile_t   sc;
113 
114     v = ccv->value;
115 
116     if (v->len == 0) {
117         ngx_conf_log_error(NGX_LOG_EMERG, ccv->cf, 0, "empty parameter");
118         return NGX_ERROR;
119     }
120 
121     nv = 0;
122     nc = 0;
123 
124     for (i = 0; i < v->len; i++) {
125         if (v->data[i] == '$') {
126             if (v->data[i + 1] >= '1' && v->data[i + 1] <= '9') {
127                 nc++;
128 
129             } else {
130                 nv++;
131             }
132         }
133     }
134 
135     if (v->data[0] != '$' && (ccv->conf_prefix || ccv->root_prefix)) {
136 
137         if (ngx_conf_full_name(ccv->cf->cycle, v, ccv->conf_prefix) != NGX_OK) {
138             return NGX_ERROR;
139         }
140 
141         ccv->conf_prefix = 0;
142         ccv->root_prefix = 0;
143     }
144 
145     ccv->complex_value->value = *v;
146     ccv->complex_value->flushes = NULL;
147     ccv->complex_value->lengths = NULL;
148     ccv->complex_value->values = NULL;
149 
150     if (nv == 0 && nc == 0) {
151         return NGX_OK;
152     }
153 
154     n = nv + 1;
155 
156     if (ngx_array_init(&flushes, ccv->cf->pool, n, sizeof(ngx_uint_t))
157         != NGX_OK)
158     {
159         return NGX_ERROR;
160     }
161 
162     n = nv * (2 * sizeof(ngx_http_script_copy_code_t)
163                   + sizeof(ngx_http_script_var_code_t))
164         + sizeof(uintptr_t);
165 
166     if (ngx_array_init(&lengths, ccv->cf->pool, n, 1) != NGX_OK) {
167         return NGX_ERROR;
168     }
169 
170     n = (nv * (2 * sizeof(ngx_http_script_copy_code_t)
171                    + sizeof(ngx_http_script_var_code_t))
172                 + sizeof(uintptr_t)
173                 + v->len
174                 + sizeof(uintptr_t) - 1)
175             & ~(sizeof(uintptr_t) - 1);
176 
177     if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) {
178         return NGX_ERROR;
179     }
180 
181     pf = &flushes;
182     pl = &lengths;
183     pv = &values;
184 
185     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
186 
187     sc.cf = ccv->cf;
188     sc.source = v;
189     sc.flushes = &pf;
190     sc.lengths = &pl;
191     sc.values = &pv;
192     sc.complete_lengths = 1;
193     sc.complete_values = 1;
194     sc.zero = ccv->zero;
195     sc.conf_prefix = ccv->conf_prefix;
196     sc.root_prefix = ccv->root_prefix;
197 
198     if (ngx_http_script_compile(&sc) != NGX_OK) {
199         return NGX_ERROR;
200     }
201 
202     if (flushes.nelts) {
203         ccv->complex_value->flushes = flushes.elts;
204         ccv->complex_value->flushes[flushes.nelts] = (ngx_uint_t) -1;
205     }
206 
207     ccv->complex_value->lengths = lengths.elts;
208     ccv->complex_value->values = values.elts;
209 
210     return NGX_OK;
211 }
212 
213 
214 ngx_uint_t
215 ngx_http_script_variables_count(ngx_str_t *value)
216 {
217     ngx_uint_t  i, n;
218 
219     for (n = 0, i = 0; i < value->len; i++) {
220         if (value->data[i] == '$') {
221             n++;
222         }
223     }
224 
225     return n;
226 }
227 
228 
229 ngx_int_t
230 ngx_http_script_compile(ngx_http_script_compile_t *sc)
231 {
232     u_char       ch;
233     ngx_str_t    name;
234     ngx_uint_t   i, bracket;
235 
236     if (ngx_http_script_init_arrays(sc) != NGX_OK) {
237         return NGX_ERROR;
238     }
239 
240     for (i = 0; i < sc->source->len; /* void */ ) {
241 
242         name.len = 0;
243 
244         if (sc->source->data[i] == '$') {
245 
246             if (++i == sc->source->len) {
247                 goto invalid_variable;
248             }
249 
250 #if (NGX_PCRE)
251             {
252             ngx_uint_t  n;
253 
254             /* NGX_HTTP_MAX_CAPTURES is 9 */
255 
256             if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') {
257 
258                 n = sc->source->data[i] - '';
259 
260                 if (sc->captures_mask & (1 << n)) {
261                     sc->dup_capture = 1;
262                 }
263 
264                 sc->captures_mask |= 1 << n;
265 
266                 if (ngx_http_script_add_capture_code(sc, n) != NGX_OK) {
267                     return NGX_ERROR;
268                 }
269 
270                 i++;
271 
272                 continue;
273             }
274             }
275 #endif
276 
277             if (sc->source->data[i] == '{') {
278                 bracket = 1;
279 
280                 if (++i == sc->source->len) {
281                     goto invalid_variable;
282                 }
283 
284                 name.data = &sc->source->data[i];
285 
286             } else {
287                 bracket = 0;
288                 name.data = &sc->source->data[i];
289             }
290 
291             for ( /* void */ ; i < sc->source->len; i++, name.len++) {
292                 ch = sc->source->data[i];
293 
294                 if (ch == '}' && bracket) {
295                     i++;
296                     bracket = 0;
297                     break;
298                 }
299 
300                 if ((ch >= 'A' && ch <= 'Z')
301                     || (ch >= 'a' && ch <= 'z')
302                     || (ch >= '' && ch <= '9')
303                     || ch == '_')
304                 {
305                     continue;
306                 }
307 
308                 break;
309             }
310 
311             if (bracket) {
312                 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0,
313                                    "the closing bracket in \"%V\" "
314                                    "variable is missing", &name);
315                 return NGX_ERROR;
316             }
317 
318             if (name.len == 0) {
319                 goto invalid_variable;
320             }
321 
322             sc->variables++;
323 
324             if (ngx_http_script_add_var_code(sc, &name) != NGX_OK) {
325                 return NGX_ERROR;
326             }
327 
328             continue;
329         }
330 
331         if (sc->source->data[i] == '?' && sc->compile_args) {
332             sc->args = 1;
333             sc->compile_args = 0;
334 
335             if (ngx_http_script_add_args_code(sc) != NGX_OK) {
336                 return NGX_ERROR;
337             }
338 
339             i++;
340 
341             continue;
342         }
343 
344         name.data = &sc->source->data[i];
345 
346         while (i < sc->source->len) {
347 
348             if (sc->source->data[i] == '$') {
349                 break;
350             }
351 
352             if (sc->source->data[i] == '?') {
353 
354                 sc->args = 1;
355 
356                 if (sc->compile_args) {
357                     break;
358                 }
359             }
360 
361             i++;
362             name.len++;
363         }
364 
365         sc->size += name.len;
366 
367         if (ngx_http_script_add_copy_code(sc, &name, (i == sc->source->len))
368             != NGX_OK)
369         {
370             return NGX_ERROR;
371         }
372     }
373 
374     return ngx_http_script_done(sc);
375 
376 invalid_variable:
377 
378     ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name");
379 
380     return NGX_ERROR;
381 }
382 
383 
384 u_char *
385 ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value,
386     void *code_lengths, size_t len, void *code_values)
387 {
388     ngx_uint_t                    i;
389     ngx_http_script_code_pt       code;
390     ngx_http_script_len_code_pt   lcode;
391     ngx_http_script_engine_t      e;
392     ngx_http_core_main_conf_t    *cmcf;
393 
394     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
395 
396     for (i = 0; i < cmcf->variables.nelts; i++) {
397         if (r->variables[i].no_cacheable) {
398             r->variables[i].valid = 0;
399             r->variables[i].not_found = 0;
400         }
401     }
402 
403     ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
404 
405     e.ip = code_lengths;
406     e.request = r;
407     e.flushed = 1;
408 
409     while (*(uintptr_t *) e.ip) {
410         lcode = *(ngx_http_script_len_code_pt *) e.ip;
411         len += lcode(&e);
412     }
413 
414 
415     value->len = len;
416     value->data = ngx_pnalloc(r->pool, len);
417     if (value->data == NULL) {
418         return NULL;
419     }
420 
421     e.ip = code_values;
422     e.pos = value->data;
423 
424     while (*(uintptr_t *) e.ip) {
425         code = *(ngx_http_script_code_pt *) e.ip;
426         code((ngx_http_script_engine_t *) &e);
427     }
428 
429     return e.pos;
430 }
431 
432 
433 void
434 ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r,
435     ngx_array_t *indices)
436 {
437     ngx_uint_t  n, *index;
438 
439     if (indices) {
440         index = indices->elts;
441         for (n = 0; n < indices->nelts; n++) {
442             if (r->variables[index[n]].no_cacheable) {
443                 r->variables[index[n]].valid = 0;
444                 r->variables[index[n]].not_found = 0;
445             }
446         }
447     }
448 }
449 
450 
451 static ngx_int_t
452 ngx_http_script_init_arrays(ngx_http_script_compile_t *sc)
453 {
454     ngx_uint_t   n;
455 
456     if (sc->flushes && *sc->flushes == NULL) {
457         n = sc->variables ? sc->variables : 1;
458         *sc->flushes = ngx_array_create(sc->cf->pool, n, sizeof(ngx_uint_t));
459         if (*sc->flushes == NULL) {
460             return NGX_ERROR;
461         }
462     }
463 
464     if (*sc->lengths == NULL) {
465         n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
466                              + sizeof(ngx_http_script_var_code_t))
467             + sizeof(uintptr_t);
468 
469         *sc->lengths = ngx_array_create(sc->cf->pool, n, 1);
470         if (*sc->lengths == NULL) {
471             return NGX_ERROR;
472         }
473     }
474 
475     if (*sc->values == NULL) {
476         n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
477                               + sizeof(ngx_http_script_var_code_t))
478                 + sizeof(uintptr_t)
479                 + sc->source->len
480                 + sizeof(uintptr_t) - 1)
481             & ~(sizeof(uintptr_t) - 1);
482 
483         *sc->values = ngx_array_create(sc->cf->pool, n, 1);
484         if (*sc->values == NULL) {
485             return NGX_ERROR;
486         }
487     }
488 
489     sc->variables = 0;
490 
491     return NGX_OK;
492 }
493 
494 
495 static ngx_int_t
496 ngx_http_script_done(ngx_http_script_compile_t *sc)
497 {
498     ngx_str_t    zero;
499     uintptr_t   *code;
500 
501     if (sc->zero) {
502 
503         zero.len = 1;
504         zero.data = (u_char *) "\0";
505 
506         if (ngx_http_script_add_copy_code(sc, &zero, 0) != NGX_OK) {
507             return NGX_ERROR;
508         }
509     }
510 
511     if (sc->conf_prefix || sc->root_prefix) {
512         if (ngx_http_script_add_full_name_code(sc) != NGX_OK) {
513             return NGX_ERROR;
514         }
515     }
516 
517     if (sc->complete_lengths) {
518         code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
519         if (code == NULL) {
520             return NGX_ERROR;
521         }
522 
523         *code = (uintptr_t) NULL;
524     }
525 
526     if (sc->complete_values) {
527         code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
528                                         &sc->main);
529         if (code == NULL) {
530             return NGX_ERROR;
531         }
532 
533         *code = (uintptr_t) NULL;
534     }
535 
536     return NGX_OK;
537 }
538 
539 
540 void *
541 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
542 {
543     if (*codes == NULL) {
544         *codes = ngx_array_create(pool, 256, 1);
545         if (*codes == NULL) {
546             return NULL;
547         }
548     }
549 
550     return ngx_array_push_n(*codes, size);
551 }
552 
553 
554 void *
555 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code)
556 {
557     u_char  *elts, **p;
558     void    *new;
559 
560     elts = codes->elts;
561 
562     new = ngx_array_push_n(codes, size);
563     if (new == NULL) {
564         return NULL;
565     }
566 
567     if (code) {
568         if (elts != codes->elts) {
569             p = code;
570             *p += (u_char *) codes->elts - elts;
571         }
572     }
573 
574     return new;
575 }
576 
577 
578 static ngx_int_t
579 ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc, ngx_str_t *value,
580     ngx_uint_t last)
581 {
582     u_char                       *p;
583     size_t                        size, len, zero;
584     ngx_http_script_copy_code_t  *code;
585 
586     zero = (sc->zero && last);
587     len = value->len + zero;
588 
589     code = ngx_http_script_add_code(*sc->lengths,
590                                     sizeof(ngx_http_script_copy_code_t), NULL);
591     if (code == NULL) {
592         return NGX_ERROR;
593     }
594 
595     code->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
596     code->len = len;
597 
598     size = (sizeof(ngx_http_script_copy_code_t) + len + sizeof(uintptr_t) - 1)
599             & ~(sizeof(uintptr_t) - 1);
600 
601     code = ngx_http_script_add_code(*sc->values, size, &sc->main);
602     if (code == NULL) {
603         return NGX_ERROR;
604     }
605 
606     code->code = ngx_http_script_copy_code;
607     code->len = len;
608 
609     p = ngx_cpymem((u_char *) code + sizeof(ngx_http_script_copy_code_t),
610                    value->data, value->len);
611 
612     if (zero) {
613         *p = '\0';
614         sc->zero = 0;
615     }
616 
617     return NGX_OK;
618 }
619 
620 
621 size_t
622 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e)
623 {
624     ngx_http_script_copy_code_t  *code;
625 
626     code = (ngx_http_script_copy_code_t *) e->ip;
627 
628     e->ip += sizeof(ngx_http_script_copy_code_t);
629 
630     return code->len;
631 }
632 
633 
634 void
635 ngx_http_script_copy_code(ngx_http_script_engine_t *e)
636 {
637     u_char                       *p;
638     ngx_http_script_copy_code_t  *code;
639 
640     code = (ngx_http_script_copy_code_t *) e->ip;
641 
642     p = e->pos;
643 
644     if (!e->skip) {
645         e->pos = ngx_copy(p, e->ip + sizeof(ngx_http_script_copy_code_t),
646                           code->len);
647     }
648 
649     e->ip += sizeof(ngx_http_script_copy_code_t)
650           + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
651 
652     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
653                    "http script copy: \"%*s\"", e->pos - p, p);
654 }
655 
656 
657 static ngx_int_t
658 ngx_http_script_add_var_code(ngx_http_script_compile_t *sc, ngx_str_t *name)
659 {
660     ngx_int_t                    index, *p;
661     ngx_http_script_var_code_t  *code;
662 
663     index = ngx_http_get_variable_index(sc->cf, name);
664 
665     if (index == NGX_ERROR) {
666         return NGX_ERROR;
667     }
668 
669     if (sc->flushes) {
670         p = ngx_array_push(*sc->flushes);
671         if (p == NULL) {
672             return NGX_ERROR;
673         }
674 
675         *p = index;
676     }
677 
678     code = ngx_http_script_add_code(*sc->lengths,
679                                     sizeof(ngx_http_script_var_code_t), NULL);
680     if (code == NULL) {
681         return NGX_ERROR;
682     }
683 
684     code->code = (ngx_http_script_code_pt) ngx_http_script_copy_var_len_code;
685     code->index = (uintptr_t) index;
686 
687     code = ngx_http_script_add_code(*sc->values,
688                                     sizeof(ngx_http_script_var_code_t),
689                                     &sc->main);
690     if (code == NULL) {
691         return NGX_ERROR;
692     }
693 
694     code->code = ngx_http_script_copy_var_code;
695     code->index = (uintptr_t) index;
696 
697     return NGX_OK;
698 }
699 
700 
701 size_t
702 ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e)
703 {
704     ngx_http_variable_value_t   *value;
705     ngx_http_script_var_code_t  *code;
706 
707     code = (ngx_http_script_var_code_t *) e->ip;
708 
709     e->ip += sizeof(ngx_http_script_var_code_t);
710 
711     if (e->flushed) {
712         value = ngx_http_get_indexed_variable(e->request, code->index);
713 
714     } else {
715         value = ngx_http_get_flushed_variable(e->request, code->index);
716     }
717 
718     if (value && !value->not_found) {
719         return value->len;
720     }
721 
722     return 0;
723 }
724 
725 
726 void
727 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e)
728 {
729     u_char                      *p;
730     ngx_http_variable_value_t   *value;
731     ngx_http_script_var_code_t  *code;
732 
733     code = (ngx_http_script_var_code_t *) e->ip;
734 
735     e->ip += sizeof(ngx_http_script_var_code_t);
736 
737     if (!e->skip) {
738 
739         if (e->flushed) {
740             value = ngx_http_get_indexed_variable(e->request, code->index);
741 
742         } else {
743             value = ngx_http_get_flushed_variable(e->request, code->index);
744         }
745 
746         if (value && !value->not_found) {
747             p = e->pos;
748             e->pos = ngx_copy(p, value->data, value->len);
749 
750             ngx_log_debug2(NGX_LOG_DEBUG_HTTP,
751                            e->request->connection->log, 0,
752                            "http script var: \"%*s\"", e->pos - p, p);
753         }
754     }
755 }
756 
757 
758 static ngx_int_t
759 ngx_http_script_add_args_code(ngx_http_script_compile_t *sc)
760 {
761     uintptr_t   *code;
762 
763     code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
764     if (code == NULL) {
765         return NGX_ERROR;
766     }
767 
768     *code = (uintptr_t) ngx_http_script_mark_args_code;
769 
770     code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), &sc->main);
771     if (code == NULL) {
772         return NGX_ERROR;
773     }
774 
775     *code = (uintptr_t) ngx_http_script_start_args_code;
776 
777     return NGX_OK;
778 }
779 
780 
781 size_t
782 ngx_http_script_mark_args_code(ngx_http_script_engine_t *e)
783 {
784     e->is_args = 1;
785     e->ip += sizeof(uintptr_t);
786 
787     return 1;
788 }
789 
790 
791 void
792 ngx_http_script_start_args_code(ngx_http_script_engine_t *e)
793 {
794     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
795                    "http script args");
796 
797     e->is_args = 1;
798     e->args = e->pos;
799     e->ip += sizeof(uintptr_t);
800 }
801 
802 
803 #if (NGX_PCRE)
804 
805 void
806 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e)
807 {
808     size_t                         len;
809     ngx_int_t                      rc;
810     ngx_uint_t                     n;
811     ngx_http_request_t            *r;
812     ngx_http_script_engine_t       le;
813     ngx_http_script_len_code_pt    lcode;
814     ngx_http_script_regex_code_t  *code;
815 
816     code = (ngx_http_script_regex_code_t *) e->ip;
817 
818     r = e->request;
819 
820     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
821                    "http script regex: \"%V\"", &code->name);
822 
823     if (code->uri) {
824         e->line = r->uri;
825     } else {
826         e->sp--;
827         e->line.len = e->sp->len;
828         e->line.data = e->sp->data;
829     }
830 
831     if (code->ncaptures && r->captures == NULL) {
832 
833         r->captures = ngx_palloc(r->pool,
834                                  (NGX_HTTP_MAX_CAPTURES + 1) * 3 * sizeof(int));
835         if (r->captures == NULL) {
836             e->ip = ngx_http_script_exit;
837             e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
838             return;
839         }
840     }
841 
842     rc = ngx_regex_exec(code->regex, &e->line, r->captures, code->ncaptures);
843 
844     if (rc == NGX_REGEX_NO_MATCHED) {
845         if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
846             ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
847                           "\"%V\" does not match \"%V\"",
848                           &code->name, &e->line);
849         }
850 
851         r->ncaptures = 0;
852 
853         if (code->test) {
854             if (code->negative_test) {
855                 e->sp->len = 1;
856                 e->sp->data = (u_char *) "1";
857 
858             } else {
859                 e->sp->len = 0;
860                 e->sp->data = (u_char *) "";
861             }
862 
863             e->sp++;
864 
865             e->ip += sizeof(ngx_http_script_regex_code_t);
866             return;
867         }
868 
869         e->ip += code->next;
870         return;
871     }
872 
873     if (rc < 0) {
874         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
875                       ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
876                       rc, &e->line, &code->name);
877 
878         e->ip = ngx_http_script_exit;
879         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
880         return;
881     }
882 
883     if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
884         ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
885                       "\"%V\" matches \"%V\"", &code->name, &e->line);
886     }
887 
888     r->ncaptures = code->ncaptures;
889     r->captures_data = e->line.data;
890 
891     if (code->test) {
892         if (code->negative_test) {
893             e->sp->len = 0;
894             e->sp->data = (u_char *) "";
895 
896         } else {
897             e->sp->len = 1;
898             e->sp->data = (u_char *) "1";
899         }
900 
901         e->sp++;
902 
903         e->ip += sizeof(ngx_http_script_regex_code_t);
904         return;
905     }
906 
907     if (code->status) {
908         e->status = code->status;
909 
910         if (!code->redirect) {
911             e->ip = ngx_http_script_exit;
912             return;
913         }
914     }
915 
916     if (code->uri) {
917         r->internal = 1;
918         r->valid_unparsed_uri = 0;
919 
920         if (code->break_cycle) {
921             r->valid_location = 0;
922             r->uri_changed = 0;
923 
924         } else {
925             r->uri_changed = 1;
926         }
927     }
928 
929     if (code->lengths == NULL) {
930         e->buf.len = code->size;
931 
932         if (code->uri) {
933             if (rc && (r->quoted_uri || r->plus_in_uri)) {
934                 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len,
935                                                  NGX_ESCAPE_ARGS);
936             }
937         }
938 
939         for (n = 1; n < (ngx_uint_t) rc; n++) {
940             e->buf.len += r->captures[2 * n + 1] - r->captures[2 * n];
941         }
942 
943     } else {
944         ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
945 
946         le.ip = code->lengths->elts;
947         le.line = e->line;
948         le.request = r;
949         le.quote = code->redirect;
950 
951         len = 0;
952 
953         while (*(uintptr_t *) le.ip) {
954             lcode = *(ngx_http_script_len_code_pt *) le.ip;
955             len += lcode(&le);
956         }
957 
958         e->buf.len = len;
959         e->is_args = le.is_args;
960     }
961 
962     if (code->add_args && r->args.len) {
963         e->buf.len += r->args.len + 1;
964     }
965 
966     e->buf.data = ngx_pnalloc(r->pool, e->buf.len);
967     if (e->buf.data == NULL) {
968         e->ip = ngx_http_script_exit;
969         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
970         return;
971     }
972 
973     e->quote = code->redirect;
974 
975     e->pos = e->buf.data;
976 
977     e->ip += sizeof(ngx_http_script_regex_code_t);
978 }
979 
980 
981 void
982 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e)
983 {
984     u_char                            *dst, *src;
985     ngx_http_request_t                *r;
986     ngx_http_script_regex_end_code_t  *code;
987 
988     code = (ngx_http_script_regex_end_code_t *) e->ip;
989 
990     r = e->request;
991 
992     e->quote = 0;
993 
994     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
995                    "http script regex end");
996 
997     if (code->redirect) {
998 
999         dst = e->buf.data;
1000         src = e->buf.data;
1001 
1002         ngx_unescape_uri(&dst, &src, e->pos - e->buf.data,
1003                          NGX_UNESCAPE_REDIRECT);
1004 
1005         if (src < e->pos) {
1006             dst = ngx_copy(dst, src, e->pos - src);
1007         }
1008 
1009         e->pos = dst;
1010 
1011         if (code->add_args && r->args.len) {
1012             *e->pos++ = (u_char) (code->args ? '&' : '?');
1013             e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
1014         }
1015 
1016         e->buf.len = e->pos - e->buf.data;
1017 
1018         if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
1019             ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
1020                           "rewritten redirect: \"%V\"", &e->buf);
1021         }
1022 
1023         r->headers_out.location = ngx_list_push(&r->headers_out.headers);
1024         if (r->headers_out.location == NULL) {
1025             e->ip = ngx_http_script_exit;
1026             e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1027             return;
1028         }
1029 
1030         r->headers_out.location->hash = 1;
1031         r->headers_out.location->key.len = sizeof("Location") - 1;
1032         r->headers_out.location->key.data = (u_char *) "Location";
1033         r->headers_out.location->value = e->buf;
1034 
1035         e->ip += sizeof(ngx_http_script_regex_end_code_t);
1036         return;
1037     }
1038 
1039     if (e->args) {
1040         e->buf.len = e->args - e->buf.data;
1041 
1042         if (code->add_args && r->args.len) {
1043             *e->pos++ = '&';
1044             e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
1045         }
1046 
1047         r->args.len = e->pos - e->args;
1048         r->args.data = e->args;
1049 
1050         e->args = NULL;
1051 
1052     } else {
1053         e->buf.len = e->pos - e->buf.data;
1054 
1055         if (!code->add_args) {
1056             r->args.len = 0;
1057         }
1058     }
1059 
1060     if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
1061         ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
1062                       "rewritten data: \"%V\", args: \"%V\"",
1063                       &e->buf, &r->args);
1064     }
1065 
1066     if (code->uri) {
1067         r->uri = e->buf;
1068 
1069         if (r->uri.len == 0) {
1070             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1071                           "the rewritten URI has a zero length");
1072             e->ip = ngx_http_script_exit;
1073             e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1074             return;
1075         }
1076 
1077         ngx_http_set_exten(r);
1078     }
1079 
1080     e->ip += sizeof(ngx_http_script_regex_end_code_t);
1081 }
1082 
1083 
1084 static ngx_int_t
1085 ngx_http_script_add_capture_code(ngx_http_script_compile_t *sc, ngx_uint_t n)
1086 {
1087     ngx_http_script_copy_capture_code_t  *code;
1088 
1089     code = ngx_http_script_add_code(*sc->lengths,
1090                                     sizeof(ngx_http_script_copy_capture_code_t),
1091                                     NULL);
1092     if (code == NULL) {
1093         return NGX_ERROR;
1094     }
1095 
1096     code->code = (ngx_http_script_code_pt)
1097                       ngx_http_script_copy_capture_len_code;
1098     code->n = 2 * n;
1099 
1100 
1101     code = ngx_http_script_add_code(*sc->values,
1102                                     sizeof(ngx_http_script_copy_capture_code_t),
1103                                     &sc->main);
1104     if (code == NULL) {
1105         return NGX_ERROR;
1106     }
1107 
1108     code->code = ngx_http_script_copy_capture_code;
1109     code->n = 2 * n;
1110 
1111     if (sc->ncaptures < n) {
1112         sc->ncaptures = n;
1113     }
1114 
1115     return NGX_OK;
1116 }
1117 
1118 
1119 size_t
1120 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e)
1121 {
1122     int                                  *cap;
1123     u_char                               *p;
1124     ngx_uint_t                            n;
1125     ngx_http_request_t                   *r;
1126     ngx_http_script_copy_capture_code_t  *code;
1127 
1128     r = e->request;
1129 
1130     code = (ngx_http_script_copy_capture_code_t *) e->ip;
1131 
1132     e->ip += sizeof(ngx_http_script_copy_capture_code_t);
1133 
1134     n = code->n;
1135 
1136     if (n < r->ncaptures) {
1137 
1138         cap = r->captures;
1139 
1140         if ((e->is_args || e->quote)
1141             && (e->request->quoted_uri || e->request->plus_in_uri))
1142         {
1143             p = r->captures_data;
1144 
1145             return cap[n + 1] - cap[n]
1146                    + 2 * ngx_escape_uri(NULL, &p[cap[n]], cap[n + 1] - cap[n],
1147                                         NGX_ESCAPE_ARGS);
1148         } else {
1149             return cap[n + 1] - cap[n];
1150         }
1151     }
1152 
1153     return 0;
1154 }
1155 
1156 
1157 void
1158 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e)
1159 {
1160     int                                  *cap;
1161     u_char                               *p, *pos;
1162     ngx_uint_t                            n;
1163     ngx_http_request_t                   *r;
1164     ngx_http_script_copy_capture_code_t  *code;
1165 
1166     r = e->request;
1167 
1168     code = (ngx_http_script_copy_capture_code_t *) e->ip;
1169 
1170     e->ip += sizeof(ngx_http_script_copy_capture_code_t);
1171 
1172     n = code->n;
1173 
1174     pos = e->pos;
1175 
1176     if (n < r->ncaptures) {
1177 
1178         cap = r->captures;
1179         p = r->captures_data;
1180 
1181         if ((e->is_args || e->quote)
1182             && (e->request->quoted_uri || e->request->plus_in_uri))
1183         {
1184             e->pos = (u_char *) ngx_escape_uri(pos, &p[cap[n]],
1185                                                cap[n + 1] - cap[n],
1186                                                NGX_ESCAPE_ARGS);
1187         } else {
1188             e->pos = ngx_copy(pos, &p[cap[n]], cap[n + 1] - cap[n]);
1189         }
1190     }
1191 
1192     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1193                    "http script capture: \"%*s\"", e->pos - pos, pos);
1194 }
1195 
1196 #endif
1197 
1198 
1199 static ngx_int_t
1200 ngx_http_script_add_full_name_code(ngx_http_script_compile_t *sc)
1201 {
1202     ngx_http_script_full_name_code_t  *code;
1203 
1204     code = ngx_http_script_add_code(*sc->lengths,
1205                                     sizeof(ngx_http_script_full_name_code_t),
1206                                     NULL);
1207     if (code == NULL) {
1208         return NGX_ERROR;
1209     }
1210 
1211     code->code = (ngx_http_script_code_pt) ngx_http_script_full_name_len_code;
1212     code->conf_prefix = sc->conf_prefix;
1213 
1214     code = ngx_http_script_add_code(*sc->values,
1215                                     sizeof(ngx_http_script_full_name_code_t),
1216                                     &sc->main);
1217     if (code == NULL) {
1218         return NGX_ERROR;
1219     }
1220 
1221     code->code = ngx_http_script_full_name_code;
1222     code->conf_prefix = sc->conf_prefix;
1223 
1224     return NGX_OK;
1225 }
1226 
1227 
1228 static size_t
1229 ngx_http_script_full_name_len_code(ngx_http_script_engine_t *e)
1230 {
1231     ngx_http_script_full_name_code_t  *code;
1232 
1233     code = (ngx_http_script_full_name_code_t *) e->ip;
1234 
1235     e->ip += sizeof(ngx_http_script_full_name_code_t);
1236 
1237     return code->conf_prefix ? ngx_cycle->conf_prefix.len:
1238                                ngx_cycle->prefix.len;
1239 }
1240 
1241 
1242 static void
1243 ngx_http_script_full_name_code(ngx_http_script_engine_t *e)
1244 {
1245     ngx_http_script_full_name_code_t  *code;
1246 
1247     ngx_str_t  value;
1248 
1249     code = (ngx_http_script_full_name_code_t *) e->ip;
1250 
1251     value.data = e->buf.data;
1252     value.len = e->pos - e->buf.data;
1253 
1254     if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &value, code->conf_prefix)
1255         != NGX_OK)
1256     {
1257         e->ip = ngx_http_script_exit;
1258         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1259         return;
1260     }
1261 
1262     e->buf = value;
1263 
1264     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1265                    "http script fullname: \"%V\"", &value);
1266 
1267     e->ip += sizeof(ngx_http_script_full_name_code_t);
1268 }
1269 
1270 
1271 void
1272 ngx_http_script_return_code(ngx_http_script_engine_t *e)
1273 {
1274     ngx_http_script_return_code_t  *code;
1275 
1276     code = (ngx_http_script_return_code_t *) e->ip;
1277 
1278     e->status = code->status;
1279 
1280     if (code->status == NGX_HTTP_NO_CONTENT) {
1281         e->request->header_only = 1;
1282         e->request->zero_body = 1;
1283     }
1284 
1285     e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t);
1286 }
1287 
1288 
1289 void
1290 ngx_http_script_break_code(ngx_http_script_engine_t *e)
1291 {
1292     e->request->uri_changed = 0;
1293 
1294     e->ip = ngx_http_script_exit;
1295 }
1296 
1297 
1298 void
1299 ngx_http_script_if_code(ngx_http_script_engine_t *e)
1300 {
1301     ngx_http_script_if_code_t  *code;
1302 
1303     code = (ngx_http_script_if_code_t *) e->ip;
1304 
1305     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1306                    "http script if");
1307 
1308     e->sp--;
1309 
1310     if (e->sp->len && e->sp->data[0] != '') {
1311         if (code->loc_conf) {
1312             e->request->loc_conf = code->loc_conf;
1313             ngx_http_update_location_config(e->request);
1314         }
1315 
1316         e->ip += sizeof(ngx_http_script_if_code_t);
1317         return;
1318     }
1319 
1320     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1321                    "http script if: false");
1322 
1323     e->ip += code->next;
1324 }
1325 
1326 
1327 void
1328 ngx_http_script_equal_code(ngx_http_script_engine_t *e)
1329 {
1330     ngx_http_variable_value_t  *val, *res;
1331 
1332     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1333                    "http script equal");
1334 
1335     e->sp--;
1336     val = e->sp;
1337     res = e->sp - 1;
1338 
1339     e->ip += sizeof(uintptr_t);
1340 
1341     if (val->len == res->len
1342         && ngx_strncmp(val->data, res->data, res->len) == 0)
1343     {
1344         *res = ngx_http_variable_true_value;
1345         return;
1346     }
1347 
1348     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1349                    "http script equal: no");
1350 
1351     *res = ngx_http_variable_null_value;
1352 }
1353 
1354 
1355 void
1356 ngx_http_script_not_equal_code(ngx_http_script_engine_t *e)
1357 {
1358     ngx_http_variable_value_t  *val, *res;
1359 
1360     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1361                    "http script not equal");
1362 
1363     e->sp--;
1364     val = e->sp;
1365     res = e->sp - 1;
1366 
1367     e->ip += sizeof(uintptr_t);
1368 
1369     if (val->len == res->len
1370         && ngx_strncmp(val->data, res->data, res->len) == 0)
1371     {
1372         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1373                        "http script not equal: no");
1374 
1375         *res = ngx_http_variable_null_value;
1376         return;
1377     }
1378 
1379     *res = ngx_http_variable_true_value;
1380 }
1381 
1382 
1383 void
1384 ngx_http_script_file_code(ngx_http_script_engine_t *e)
1385 {
1386     ngx_str_t                     path;
1387     ngx_http_request_t           *r;
1388     ngx_open_file_info_t          of;
1389     ngx_http_core_loc_conf_t     *clcf;
1390     ngx_http_variable_value_t    *value;
1391     ngx_http_script_file_code_t  *code;
1392 
1393     value = e->sp - 1;
1394 
1395     code = (ngx_http_script_file_code_t *) e->ip;
1396     e->ip += sizeof(ngx_http_script_file_code_t);
1397 
1398     path.len = value->len - 1;
1399     path.data = value->data;
1400 
1401     r = e->request;
1402 
1403     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1404                    "http script file op %p \"%V\"", code->op, &path);
1405 
1406     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1407 
1408     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
1409 
1410     of.read_ahead = clcf->read_ahead;
1411     of.directio = clcf->directio;
1412     of.valid = clcf->open_file_cache_valid;
1413     of.min_uses = clcf->open_file_cache_min_uses;
1414     of.test_only = 1;
1415     of.errors = clcf->open_file_cache_errors;
1416     of.events = clcf->open_file_cache_events;
1417 
1418     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
1419         != NGX_OK)
1420     {
1421         if (of.err != NGX_ENOENT
1422             && of.err != NGX_ENOTDIR
1423             && of.err != NGX_ENAMETOOLONG)
1424         {
1425             ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
1426                           "%s \"%s\" failed", of.failed, value->data);
1427         }
1428 
1429         switch (code->op) {
1430 
1431         case ngx_http_script_file_plain:
1432         case ngx_http_script_file_dir:
1433         case ngx_http_script_file_exists:
1434         case ngx_http_script_file_exec:
1435              goto false_value;
1436 
1437         case ngx_http_script_file_not_plain:
1438         case ngx_http_script_file_not_dir:
1439         case ngx_http_script_file_not_exists:
1440         case ngx_http_script_file_not_exec:
1441              goto true_value;
1442         }
1443 
1444         goto false_value;
1445     }
1446 
1447     switch (code->op) {
1448     case ngx_http_script_file_plain:
1449         if (of.is_file) {
1450              goto true_value;
1451         }
1452         goto false_value;
1453 
1454     case ngx_http_script_file_not_plain:
1455         if (of.is_file) {
1456             goto false_value;
1457         }
1458         goto true_value;
1459 
1460     case ngx_http_script_file_dir:
1461         if (of.is_dir) {
1462              goto true_value;
1463         }
1464         goto false_value;
1465 
1466     case ngx_http_script_file_not_dir:
1467         if (of.is_dir) {
1468             goto false_value;
1469         }
1470         goto true_value;
1471 
1472     case ngx_http_script_file_exists:
1473         if (of.is_file || of.is_dir || of.is_link) {
1474              goto true_value;
1475         }
1476         goto false_value;
1477 
1478     case ngx_http_script_file_not_exists:
1479         if (of.is_file || of.is_dir || of.is_link) {
1480             goto false_value;
1481         }
1482         goto true_value;
1483 
1484     case ngx_http_script_file_exec:
1485         if (of.is_exec) {
1486              goto true_value;
1487         }
1488         goto false_value;
1489 
1490     case ngx_http_script_file_not_exec:
1491         if (of.is_exec) {
1492             goto false_value;
1493         }
1494         goto true_value;
1495     }
1496 
1497 false_value:
1498 
1499     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1500                    "http script file op false");
1501 
1502     *value = ngx_http_variable_null_value;
1503     return;
1504 
1505 true_value:
1506 
1507     *value = ngx_http_variable_true_value;
1508     return;
1509 }
1510 
1511 
1512 void
1513 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e)
1514 {
1515     size_t                                 len;
1516     ngx_http_script_engine_t               le;
1517     ngx_http_script_len_code_pt            lcode;
1518     ngx_http_script_complex_value_code_t  *code;
1519 
1520     code = (ngx_http_script_complex_value_code_t *) e->ip;
1521 
1522     e->ip += sizeof(ngx_http_script_complex_value_code_t);
1523 
1524     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1525                    "http script complex value");
1526 
1527     ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
1528 
1529     le.ip = code->lengths->elts;
1530     le.line = e->line;
1531     le.request = e->request;
1532     le.quote = e->quote;
1533 
1534     for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
1535         lcode = *(ngx_http_script_len_code_pt *) le.ip;
1536     }
1537 
1538     e->buf.len = len;
1539     e->buf.data = ngx_pnalloc(e->request->pool, len);
1540     if (e->buf.data == NULL) {
1541         e->ip = ngx_http_script_exit;
1542         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1543         return;
1544     }
1545 
1546     e->pos = e->buf.data;
1547 
1548     e->sp->len = e->buf.len;
1549     e->sp->data = e->buf.data;
1550     e->sp++;
1551 }
1552 
1553 
1554 void
1555 ngx_http_script_value_code(ngx_http_script_engine_t *e)
1556 {
1557     ngx_http_script_value_code_t  *code;
1558 
1559     code = (ngx_http_script_value_code_t *) e->ip;
1560 
1561     e->ip += sizeof(ngx_http_script_value_code_t);
1562 
1563     e->sp->len = code->text_len;
1564     e->sp->data = (u_char *) code->text_data;
1565 
1566     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1567                    "http script value: \"%v\"", e->sp);
1568 
1569     e->sp++;
1570 }
1571 
1572 
1573 void
1574 ngx_http_script_set_var_code(ngx_http_script_engine_t *e)
1575 {
1576     ngx_http_request_t          *r;
1577     ngx_http_script_var_code_t  *code;
1578 
1579     code = (ngx_http_script_var_code_t *) e->ip;
1580 
1581     e->ip += sizeof(ngx_http_script_var_code_t);
1582 
1583     r = e->request;
1584 
1585     e->sp--;
1586 
1587     r->variables[code->index].len = e->sp->len;
1588     r->variables[code->index].valid = 1;
1589     r->variables[code->index].no_cacheable = 0;
1590     r->variables[code->index].not_found = 0;
1591     r->variables[code->index].data = e->sp->data;
1592 
1593 #if (NGX_DEBUG)
1594     {
1595     ngx_http_variable_t        *v;
1596     ngx_http_core_main_conf_t  *cmcf;
1597 
1598     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
1599 
1600     v = cmcf->variables.elts;
1601 
1602     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1603                    "http script set $%V", &v[code->index].name);
1604     }
1605 #endif
1606 }
1607 
1608 
1609 void
1610 ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e)
1611 {
1612     ngx_http_script_var_handler_code_t  *code;
1613 
1614     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1615                    "http script set var handler");
1616 
1617     code = (ngx_http_script_var_handler_code_t *) e->ip;
1618 
1619     e->ip += sizeof(ngx_http_script_var_handler_code_t);
1620 
1621     e->sp--;
1622 
1623     code->handler(e->request, e->sp, code->data);
1624 }
1625 
1626 
1627 void
1628 ngx_http_script_var_code(ngx_http_script_engine_t *e)
1629 {
1630     ngx_http_variable_value_t   *value;
1631     ngx_http_script_var_code_t  *code;
1632 
1633     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1634                    "http script var");
1635 
1636     code = (ngx_http_script_var_code_t *) e->ip;
1637 
1638     e->ip += sizeof(ngx_http_script_var_code_t);
1639 
1640     value = ngx_http_get_flushed_variable(e->request, code->index);
1641 
1642     if (value && !value->not_found) {
1643         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1644                        "http script var: \"%v\"", value);
1645 
1646         *e->sp = *value;
1647         e->sp++;
1648 
1649         return;
1650     }
1651 
1652     *e->sp = ngx_http_variable_null_value;
1653     e->sp++;
1654 }
1655 
1656 
1657 void
1658 ngx_http_script_nop_code(ngx_http_script_engine_t *e)
1659 {
1660     e->ip += sizeof(uintptr_t);
1661 }
1662 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.