nginx 0.7.8
[nginx-catap.git] / src / http / ngx_http_script.c
blob95c1cc1051c27249dc9b0a096c0350a83e44e123
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
12 #define ngx_http_script_exit (u_char *) &ngx_http_script_exit_code
14 static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL;
17 ngx_uint_t
18 ngx_http_script_variables_count(ngx_str_t *value)
20 ngx_uint_t i, n;
22 for (n = 0, i = 0; i < value->len; i++) {
23 if (value->data[i] == '$') {
24 n++;
28 return n;
32 ngx_int_t
33 ngx_http_script_compile(ngx_http_script_compile_t *sc)
35 u_char ch;
36 size_t size;
37 ngx_int_t index, *p;
38 ngx_str_t name;
39 uintptr_t *code;
40 ngx_uint_t i, n, bracket;
41 ngx_http_script_var_code_t *var_code;
42 ngx_http_script_copy_code_t *copy;
43 ngx_http_script_copy_capture_code_t *copy_capture;
45 if (sc->flushes && *sc->flushes == NULL) {
46 n = sc->variables ? sc->variables : 1;
47 *sc->flushes = ngx_array_create(sc->cf->pool, n, sizeof(ngx_uint_t));
48 if (*sc->flushes == NULL) {
49 return NGX_ERROR;
54 if (*sc->lengths == NULL) {
55 n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
56 + sizeof(ngx_http_script_var_code_t))
57 + sizeof(uintptr_t);
59 *sc->lengths = ngx_array_create(sc->cf->pool, n, 1);
60 if (*sc->lengths == NULL) {
61 return NGX_ERROR;
66 if (*sc->values == NULL) {
67 n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
68 + sizeof(ngx_http_script_var_code_t))
69 + sizeof(uintptr_t)
70 + sc->source->len
71 + sizeof(uintptr_t) - 1)
72 & ~(sizeof(uintptr_t) - 1);
74 *sc->values = ngx_array_create(sc->cf->pool, n, 1);
75 if (*sc->values == NULL) {
76 return NGX_ERROR;
80 sc->variables = 0;
82 for (i = 0; i < sc->source->len; /* void */ ) {
84 name.len = 0;
86 if (sc->source->data[i] == '$') {
88 if (++i == sc->source->len) {
89 goto invalid_variable;
92 if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') {
94 n = sc->source->data[i] - '0';
96 if (sc->captures_mask & (1 << n)) {
97 sc->dup_capture = 1;
100 sc->captures_mask |= 1 << n;
102 copy_capture = ngx_http_script_add_code(*sc->lengths,
103 sizeof(ngx_http_script_copy_capture_code_t),
104 NULL);
105 if (copy_capture == NULL) {
106 return NGX_ERROR;
109 copy_capture->code = (ngx_http_script_code_pt)
110 ngx_http_script_copy_capture_len_code;
111 copy_capture->n = 2 * n;
114 copy_capture = ngx_http_script_add_code(*sc->values,
115 sizeof(ngx_http_script_copy_capture_code_t),
116 &sc->main);
117 if (copy_capture == NULL) {
118 return NGX_ERROR;
121 copy_capture->code = ngx_http_script_copy_capture_code;
122 copy_capture->n = 2 * n;
124 if (sc->ncaptures < n) {
125 sc->ncaptures = n;
128 i++;
130 continue;
133 if (sc->source->data[i] == '{') {
134 bracket = 1;
136 if (++i == sc->source->len) {
137 goto invalid_variable;
140 name.data = &sc->source->data[i];
142 } else {
143 bracket = 0;
144 name.data = &sc->source->data[i];
147 for ( /* void */ ; i < sc->source->len; i++, name.len++) {
148 ch = sc->source->data[i];
150 if (ch == '}' && bracket) {
151 i++;
152 bracket = 0;
153 break;
156 if ((ch >= 'A' && ch <= 'Z')
157 || (ch >= 'a' && ch <= 'z')
158 || (ch >= '0' && ch <= '9')
159 || ch == '_')
161 continue;
164 break;
167 if (bracket) {
168 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0,
169 "the closing bracket in \"%V\" "
170 "variable is missing", &name);
171 return NGX_ERROR;
174 if (name.len == 0) {
175 goto invalid_variable;
178 sc->variables++;
180 index = ngx_http_get_variable_index(sc->cf, &name);
182 if (index == NGX_ERROR) {
183 return NGX_ERROR;
186 if (sc->flushes) {
187 p = ngx_array_push(*sc->flushes);
188 if (p == NULL) {
189 return NGX_ERROR;
192 *p = index;
195 var_code = ngx_http_script_add_code(*sc->lengths,
196 sizeof(ngx_http_script_var_code_t),
197 NULL);
198 if (var_code == NULL) {
199 return NGX_ERROR;
202 var_code->code = (ngx_http_script_code_pt)
203 ngx_http_script_copy_var_len_code;
204 var_code->index = (uintptr_t) index;
207 var_code = ngx_http_script_add_code(*sc->values,
208 sizeof(ngx_http_script_var_code_t),
209 &sc->main);
210 if (var_code == NULL) {
211 return NGX_ERROR;
214 var_code->code = ngx_http_script_copy_var_code;
215 var_code->index = (uintptr_t) index;
217 continue;
220 if (sc->source->data[i] == '?' && sc->compile_args) {
221 sc->args = 1;
222 sc->compile_args = 0;
224 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t),
225 NULL);
226 if (code == NULL) {
227 return NGX_ERROR;
230 *code = (uintptr_t) ngx_http_script_mark_args_code;
232 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
233 &sc->main);
234 if (code == NULL) {
235 return NGX_ERROR;
238 *code = (uintptr_t) ngx_http_script_start_args_code;
240 i++;
242 continue;
245 name.data = &sc->source->data[i];
247 while (i < sc->source->len
248 && sc->source->data[i] != '$'
249 && !(sc->source->data[i] == '?' && sc->compile_args))
251 i++;
252 name.len++;
255 sc->size += name.len;
257 copy = ngx_http_script_add_code(*sc->lengths,
258 sizeof(ngx_http_script_copy_code_t),
259 NULL);
260 if (copy == NULL) {
261 return NGX_ERROR;
264 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
265 copy->len = name.len;
267 size = (sizeof(ngx_http_script_copy_code_t) + name.len
268 + sizeof(uintptr_t) - 1)
269 & ~(sizeof(uintptr_t) - 1);
271 copy = ngx_http_script_add_code(*sc->values, size, &sc->main);
272 if (copy == NULL) {
273 return NGX_ERROR;
276 copy->code = ngx_http_script_copy_code;
277 copy->len = name.len;
279 ngx_memcpy((u_char *) copy + sizeof(ngx_http_script_copy_code_t),
280 name.data, name.len);
283 if (sc->complete_lengths) {
284 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
285 if (code == NULL) {
286 return NGX_ERROR;
289 *code = (uintptr_t) NULL;
292 if (sc->complete_values) {
293 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
294 &sc->main);
295 if (code == NULL) {
296 return NGX_ERROR;
299 *code = (uintptr_t) NULL;
302 return NGX_OK;
304 invalid_variable:
306 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name");
308 return NGX_ERROR;
312 u_char *
313 ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value,
314 void *code_lengths, size_t len, void *code_values)
316 ngx_uint_t i;
317 ngx_http_script_code_pt code;
318 ngx_http_script_len_code_pt lcode;
319 ngx_http_script_engine_t e;
320 ngx_http_core_main_conf_t *cmcf;
322 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
324 for (i = 0; i < cmcf->variables.nelts; i++) {
325 if (r->variables[i].no_cacheable) {
326 r->variables[i].valid = 0;
327 r->variables[i].not_found = 0;
331 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
333 e.ip = code_lengths;
334 e.request = r;
335 e.flushed = 1;
337 while (*(uintptr_t *) e.ip) {
338 lcode = *(ngx_http_script_len_code_pt *) e.ip;
339 len += lcode(&e);
343 value->len = len;
344 value->data = ngx_pnalloc(r->pool, len);
345 if (value->data == NULL) {
346 return NULL;
349 e.ip = code_values;
350 e.pos = value->data;
352 while (*(uintptr_t *) e.ip) {
353 code = *(ngx_http_script_code_pt *) e.ip;
354 code((ngx_http_script_engine_t *) &e);
357 return e.pos;
361 void
362 ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r,
363 ngx_array_t *indices)
365 ngx_uint_t n, *index;
367 if (indices) {
368 index = indices->elts;
369 for (n = 0; n < indices->nelts; n++) {
370 if (r->variables[index[n]].no_cacheable) {
371 r->variables[index[n]].valid = 0;
372 r->variables[index[n]].not_found = 0;
379 void *
380 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
382 if (*codes == NULL) {
383 *codes = ngx_array_create(pool, 256, 1);
384 if (*codes == NULL) {
385 return NULL;
389 return ngx_array_push_n(*codes, size);
393 void *
394 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code)
396 u_char *elts, **p;
397 void *new;
399 elts = codes->elts;
401 new = ngx_array_push_n(codes, size);
402 if (new == NULL) {
403 return NGX_CONF_ERROR;
406 if (code) {
407 if (elts != codes->elts) {
408 p = code;
409 *p += (u_char *) codes->elts - elts;
413 return new;
417 size_t
418 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e)
420 ngx_http_script_copy_code_t *code;
422 code = (ngx_http_script_copy_code_t *) e->ip;
424 e->ip += sizeof(ngx_http_script_copy_code_t);
426 return code->len;
430 void
431 ngx_http_script_copy_code(ngx_http_script_engine_t *e)
433 ngx_http_script_copy_code_t *code;
435 code = (ngx_http_script_copy_code_t *) e->ip;
437 if (!e->skip) {
438 e->pos = ngx_copy(e->pos, e->ip + sizeof(ngx_http_script_copy_code_t),
439 code->len);
442 e->ip += sizeof(ngx_http_script_copy_code_t)
443 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
445 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
446 "http script copy: \"%V\"", &e->buf);
450 size_t
451 ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e)
453 ngx_http_variable_value_t *value;
454 ngx_http_script_var_code_t *code;
456 code = (ngx_http_script_var_code_t *) e->ip;
458 e->ip += sizeof(ngx_http_script_var_code_t);
460 if (e->flushed) {
461 value = ngx_http_get_indexed_variable(e->request, code->index);
463 } else {
464 value = ngx_http_get_flushed_variable(e->request, code->index);
467 if (value && !value->not_found) {
468 return value->len;
471 return 0;
475 void
476 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e)
478 ngx_http_variable_value_t *value;
479 ngx_http_script_var_code_t *code;
481 code = (ngx_http_script_var_code_t *) e->ip;
483 e->ip += sizeof(ngx_http_script_var_code_t);
485 if (!e->skip) {
487 if (e->flushed) {
488 value = ngx_http_get_indexed_variable(e->request, code->index);
490 } else {
491 value = ngx_http_get_flushed_variable(e->request, code->index);
494 if (value && !value->not_found) {
495 e->pos = ngx_copy(e->pos, value->data, value->len);
497 ngx_log_debug1(NGX_LOG_DEBUG_HTTP,
498 e->request->connection->log, 0,
499 "http script var: \"%V\"", &e->buf);
505 size_t
506 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e)
508 ngx_http_script_copy_capture_code_t *code;
510 code = (ngx_http_script_copy_capture_code_t *) e->ip;
512 e->ip += sizeof(ngx_http_script_copy_capture_code_t);
514 if (code->n < e->ncaptures) {
515 if ((e->is_args || e->quote)
516 && (e->request->quoted_uri || e->request->plus_in_uri))
518 return e->captures[code->n + 1] - e->captures[code->n]
519 + 2 * ngx_escape_uri(NULL,
520 &e->line.data[e->captures[code->n]],
521 e->captures[code->n + 1] - e->captures[code->n],
522 NGX_ESCAPE_ARGS);
523 } else {
524 return e->captures[code->n + 1] - e->captures[code->n];
528 return 0;
532 void
533 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e)
535 ngx_http_script_copy_capture_code_t *code;
537 code = (ngx_http_script_copy_capture_code_t *) e->ip;
539 e->ip += sizeof(ngx_http_script_copy_capture_code_t);
541 if (code->n < e->ncaptures) {
542 if ((e->is_args || e->quote)
543 && (e->request->quoted_uri || e->request->plus_in_uri))
545 e->pos = (u_char *) ngx_escape_uri(e->pos,
546 &e->line.data[e->captures[code->n]],
547 e->captures[code->n + 1] - e->captures[code->n],
548 NGX_ESCAPE_ARGS);
549 } else {
550 e->pos = ngx_copy(e->pos,
551 &e->line.data[e->captures[code->n]],
552 e->captures[code->n + 1] - e->captures[code->n]);
556 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
557 "http script capture: \"%V\"", &e->buf);
561 size_t
562 ngx_http_script_mark_args_code(ngx_http_script_engine_t *e)
564 e->is_args = 1;
565 e->ip += sizeof(uintptr_t);
567 return 1;
571 void
572 ngx_http_script_start_args_code(ngx_http_script_engine_t *e)
574 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
575 "http script args");
577 e->args = e->pos;
578 e->ip += sizeof(uintptr_t);
583 #if (NGX_PCRE)
585 void
586 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e)
588 size_t len;
589 ngx_int_t rc;
590 ngx_uint_t n;
591 ngx_http_request_t *r;
592 ngx_http_script_engine_t le;
593 ngx_http_script_len_code_pt lcode;
594 ngx_http_script_regex_code_t *code;
596 code = (ngx_http_script_regex_code_t *) e->ip;
598 r = e->request;
600 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
601 "http script regex: \"%V\"", &code->name);
603 if (code->uri) {
604 e->line = r->uri;
605 } else {
606 e->sp--;
607 e->line.len = e->sp->len;
608 e->line.data = e->sp->data;
611 rc = ngx_regex_exec(code->regex, &e->line, e->captures, code->ncaptures);
613 if (rc == NGX_REGEX_NO_MATCHED) {
614 if (e->log) {
615 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
616 "\"%V\" does not match \"%V\"",
617 &code->name, &e->line);
620 e->ncaptures = 0;
622 if (code->test) {
623 if (code->negative_test) {
624 e->sp->len = 1;
625 e->sp->data = (u_char *) "1";
627 } else {
628 e->sp->len = 0;
629 e->sp->data = (u_char *) "";
632 e->sp++;
634 e->ip += sizeof(ngx_http_script_regex_code_t);
635 return;
638 e->ip += code->next;
639 return;
642 if (rc < 0) {
643 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
644 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
645 rc, &e->line, &code->name);
647 e->ip = ngx_http_script_exit;
648 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
649 return;
652 if (e->log) {
653 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
654 "\"%V\" matches \"%V\"", &code->name, &e->line);
657 e->ncaptures = code->ncaptures;
659 if (code->test) {
660 if (code->negative_test) {
661 e->sp->len = 0;
662 e->sp->data = (u_char *) "";
664 } else {
665 e->sp->len = 1;
666 e->sp->data = (u_char *) "1";
669 e->sp++;
671 e->ip += sizeof(ngx_http_script_regex_code_t);
672 return;
675 if (code->status) {
676 e->status = code->status;
678 if (!code->redirect) {
679 e->ip = ngx_http_script_exit;
680 return;
684 if (code->uri) {
685 r->internal = 1;
686 r->valid_unparsed_uri = 0;
688 if (code->break_cycle) {
689 r->valid_location = 0;
690 r->uri_changed = 0;
692 } else {
693 r->uri_changed = 1;
697 if (code->lengths == NULL) {
698 e->buf.len = code->size;
700 if (code->uri) {
701 if (rc && (r->quoted_uri || r->plus_in_uri)) {
702 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len,
703 NGX_ESCAPE_ARGS);
707 for (n = 1; n < (ngx_uint_t) rc; n++) {
708 e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n];
711 } else {
712 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
714 le.ip = code->lengths->elts;
715 le.line = e->line;
716 le.request = r;
717 le.captures = e->captures;
718 le.ncaptures = e->ncaptures;
719 le.quote = code->redirect;
721 len = 0;
723 while (*(uintptr_t *) le.ip) {
724 lcode = *(ngx_http_script_len_code_pt *) le.ip;
725 len += lcode(&le);
728 e->buf.len = len;
729 e->is_args = le.is_args;
732 if (code->add_args && r->args.len) {
733 e->buf.len += r->args.len + 1;
736 e->buf.data = ngx_pnalloc(r->pool, e->buf.len);
737 if (e->buf.data == NULL) {
738 e->ip = ngx_http_script_exit;
739 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
740 return;
743 e->quote = code->redirect;
745 e->pos = e->buf.data;
747 e->ip += sizeof(ngx_http_script_regex_code_t);
751 void
752 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e)
754 u_char *dst, *src;
755 ngx_http_request_t *r;
756 ngx_http_script_regex_end_code_t *code;
758 code = (ngx_http_script_regex_end_code_t *) e->ip;
760 r = e->request;
762 e->quote = 0;
764 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
765 "http script regex end");
767 if (code->redirect) {
769 dst = e->buf.data;
770 src = e->buf.data;
772 ngx_unescape_uri(&dst, &src, e->pos - e->buf.data,
773 NGX_UNESCAPE_REDIRECT);
775 if (src < e->pos) {
776 dst = ngx_copy(dst, src, e->pos - src);
779 e->pos = dst;
781 if (code->add_args && r->args.len) {
782 *e->pos++ = (u_char) (code->args ? '&' : '?');
783 e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
786 e->buf.len = e->pos - e->buf.data;
788 if (e->log) {
789 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
790 "rewritten redirect: \"%V\"", &e->buf);
793 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
794 if (r->headers_out.location == NULL) {
795 e->ip = ngx_http_script_exit;
796 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
797 return;
800 r->headers_out.location->hash = 1;
801 r->headers_out.location->key.len = sizeof("Location") - 1;
802 r->headers_out.location->key.data = (u_char *) "Location";
803 r->headers_out.location->value = e->buf;
805 e->ip += sizeof(ngx_http_script_regex_end_code_t);
806 return;
809 if (e->args) {
810 e->buf.len = e->args - e->buf.data;
812 if (code->add_args && r->args.len) {
813 *e->pos++ = '&';
814 e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
817 r->args.len = e->pos - e->args;
818 r->args.data = e->args;
820 e->args = NULL;
822 } else {
823 e->buf.len = e->pos - e->buf.data;
825 if (!code->add_args) {
826 r->args.len = 0;
830 if (e->log) {
831 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
832 "rewritten data: \"%V\", args: \"%V\"",
833 &e->buf, &r->args);
836 if (code->uri) {
837 r->uri = e->buf;
839 if (r->uri.len == 0) {
840 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
841 "the rewritten URI has a zero length");
842 e->ip = ngx_http_script_exit;
843 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
844 return;
847 if (ngx_http_set_exten(r) != NGX_OK) {
848 e->ip = ngx_http_script_exit;
849 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
850 return;
854 e->ip += sizeof(ngx_http_script_regex_end_code_t);
857 #endif
860 void
861 ngx_http_script_return_code(ngx_http_script_engine_t *e)
863 ngx_http_script_return_code_t *code;
865 code = (ngx_http_script_return_code_t *) e->ip;
867 e->status = code->status;
869 if (code->status == NGX_HTTP_NO_CONTENT) {
870 e->request->header_only = 1;
871 e->request->zero_body = 1;
874 e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t);
878 void
879 ngx_http_script_break_code(ngx_http_script_engine_t *e)
881 e->request->uri_changed = 0;
883 e->ip = ngx_http_script_exit;
887 void
888 ngx_http_script_if_code(ngx_http_script_engine_t *e)
890 ngx_http_script_if_code_t *code;
892 code = (ngx_http_script_if_code_t *) e->ip;
894 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
895 "http script if");
897 e->sp--;
899 if (e->sp->len && e->sp->data[0] != '0') {
900 if (code->loc_conf) {
901 e->request->loc_conf = code->loc_conf;
902 ngx_http_update_location_config(e->request);
905 e->ip += sizeof(ngx_http_script_if_code_t);
906 return;
909 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
910 "http script if: false");
912 e->ip += code->next;
916 void
917 ngx_http_script_equal_code(ngx_http_script_engine_t *e)
919 ngx_http_variable_value_t *val, *res;
921 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
922 "http script equal");
924 e->sp--;
925 val = e->sp;
926 res = e->sp - 1;
928 e->ip += sizeof(uintptr_t);
930 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len)
931 == 0)
933 *res = ngx_http_variable_true_value;
934 return;
937 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
938 "http script equal: no");
940 *res = ngx_http_variable_null_value;
944 void
945 ngx_http_script_not_equal_code(ngx_http_script_engine_t *e)
947 ngx_http_variable_value_t *val, *res;
949 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
950 "http script not equal");
952 e->sp--;
953 val = e->sp;
954 res = e->sp - 1;
956 e->ip += sizeof(uintptr_t);
958 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len)
959 == 0)
961 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
962 "http script not equal: no");
964 *res = ngx_http_variable_null_value;
965 return;
968 *res = ngx_http_variable_true_value;
972 void
973 ngx_http_script_file_code(ngx_http_script_engine_t *e)
975 ngx_str_t path;
976 ngx_http_request_t *r;
977 ngx_open_file_info_t of;
978 ngx_http_core_loc_conf_t *clcf;
979 ngx_http_variable_value_t *value;
980 ngx_http_script_file_code_t *code;
982 value = e->sp - 1;
984 code = (ngx_http_script_file_code_t *) e->ip;
985 e->ip += sizeof(ngx_http_script_file_code_t);
987 path.len = value->len - 1;
988 path.data = value->data;
990 r = e->request;
992 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
993 "http script file op %p \"%V\"", code->op, &path);
995 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
997 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
999 of.directio = clcf->directio;
1000 of.valid = clcf->open_file_cache_valid;
1001 of.min_uses = clcf->open_file_cache_min_uses;
1002 of.errors = clcf->open_file_cache_errors;
1003 of.events = clcf->open_file_cache_events;
1005 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
1006 != NGX_OK)
1008 if (of.err != NGX_ENOENT && of.err != NGX_ENOTDIR) {
1009 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
1010 ngx_file_info_n " \"%s\" failed", value->data);
1013 switch (code->op) {
1015 case ngx_http_script_file_plain:
1016 case ngx_http_script_file_dir:
1017 case ngx_http_script_file_exists:
1018 case ngx_http_script_file_exec:
1019 goto false_value;
1021 case ngx_http_script_file_not_plain:
1022 case ngx_http_script_file_not_dir:
1023 case ngx_http_script_file_not_exists:
1024 case ngx_http_script_file_not_exec:
1025 goto true_value;
1028 goto false_value;
1031 switch (code->op) {
1032 case ngx_http_script_file_plain:
1033 if (of.is_file) {
1034 goto true_value;
1036 goto false_value;
1038 case ngx_http_script_file_not_plain:
1039 if (of.is_file) {
1040 goto false_value;
1042 goto true_value;
1044 case ngx_http_script_file_dir:
1045 if (of.is_dir) {
1046 goto true_value;
1048 goto false_value;
1050 case ngx_http_script_file_not_dir:
1051 if (of.is_dir) {
1052 goto false_value;
1054 goto true_value;
1056 case ngx_http_script_file_exists:
1057 if (of.is_file || of.is_dir || of.is_link) {
1058 goto true_value;
1060 goto false_value;
1062 case ngx_http_script_file_not_exists:
1063 if (of.is_file || of.is_dir || of.is_link) {
1064 goto false_value;
1066 goto true_value;
1068 case ngx_http_script_file_exec:
1069 if (of.is_exec) {
1070 goto true_value;
1072 goto false_value;
1074 case ngx_http_script_file_not_exec:
1075 if (of.is_exec) {
1076 goto false_value;
1078 goto true_value;
1081 false_value:
1083 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1084 "http script file op false");
1086 *value = ngx_http_variable_null_value;
1087 return;
1089 true_value:
1091 *value = ngx_http_variable_true_value;
1092 return;
1096 void
1097 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e)
1099 size_t len;
1100 ngx_http_script_engine_t le;
1101 ngx_http_script_len_code_pt lcode;
1102 ngx_http_script_complex_value_code_t *code;
1104 code = (ngx_http_script_complex_value_code_t *) e->ip;
1106 e->ip += sizeof(ngx_http_script_complex_value_code_t);
1108 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1109 "http script complex value");
1111 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
1113 le.ip = code->lengths->elts;
1114 le.line = e->line;
1115 le.request = e->request;
1116 le.captures = e->captures;
1117 le.ncaptures = e->ncaptures;
1118 le.quote = e->quote;
1120 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
1121 lcode = *(ngx_http_script_len_code_pt *) le.ip;
1124 e->buf.len = len;
1125 e->buf.data = ngx_pnalloc(e->request->pool, len);
1126 if (e->buf.data == NULL) {
1127 e->ip = ngx_http_script_exit;
1128 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1129 return;
1132 e->pos = e->buf.data;
1134 e->sp->len = e->buf.len;
1135 e->sp->data = e->buf.data;
1136 e->sp++;
1140 void
1141 ngx_http_script_value_code(ngx_http_script_engine_t *e)
1143 ngx_http_script_value_code_t *code;
1145 code = (ngx_http_script_value_code_t *) e->ip;
1147 e->ip += sizeof(ngx_http_script_value_code_t);
1149 e->sp->len = code->text_len;
1150 e->sp->data = (u_char *) code->text_data;
1152 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1153 "http script value: \"%v\"", e->sp);
1155 e->sp++;
1159 void
1160 ngx_http_script_set_var_code(ngx_http_script_engine_t *e)
1162 ngx_http_request_t *r;
1163 ngx_http_script_var_code_t *code;
1165 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1166 "http script set var");
1168 code = (ngx_http_script_var_code_t *) e->ip;
1170 e->ip += sizeof(ngx_http_script_var_code_t);
1172 r = e->request;
1174 e->sp--;
1176 r->variables[code->index].len = e->sp->len;
1177 r->variables[code->index].valid = 1;
1178 r->variables[code->index].no_cacheable = 0;
1179 r->variables[code->index].not_found = 0;
1180 r->variables[code->index].data = e->sp->data;
1184 void
1185 ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e)
1187 ngx_http_script_var_handler_code_t *code;
1189 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1190 "http script set var handler");
1192 code = (ngx_http_script_var_handler_code_t *) e->ip;
1194 e->ip += sizeof(ngx_http_script_var_handler_code_t);
1196 e->sp--;
1198 code->handler(e->request, e->sp, code->data);
1202 void
1203 ngx_http_script_var_code(ngx_http_script_engine_t *e)
1205 ngx_http_variable_value_t *value;
1206 ngx_http_script_var_code_t *code;
1208 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1209 "http script var");
1211 code = (ngx_http_script_var_code_t *) e->ip;
1213 e->ip += sizeof(ngx_http_script_var_code_t);
1215 value = ngx_http_get_flushed_variable(e->request, code->index);
1217 if (value && !value->not_found) {
1218 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1219 "http script var: \"%v\"", value);
1221 *e->sp = *value;
1222 e->sp++;
1224 return;
1227 *e->sp = ngx_http_variable_null_value;
1228 e->sp++;
1232 void
1233 ngx_http_script_nop_code(ngx_http_script_engine_t *e)
1235 e->ip += sizeof(uintptr_t);