Mercurial > hg > nginx-quic
annotate src/http/ngx_http_script.c @ 2509:b4661481f198
the $ancient_browser variables did not treat as an ancient browser
listed in modern_browser, but has lower version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 18 Feb 2009 09:23:47 +0000 |
parents | d3357bf23a5e |
children | 912ce4113f21 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
5 |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_http.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
509 | 12 #define ngx_http_script_exit (u_char *) &ngx_http_script_exit_code |
13 | |
14 static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL; | |
15 | |
16 | |
17 ngx_uint_t | |
18 ngx_http_script_variables_count(ngx_str_t *value) | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
19 { |
509 | 20 ngx_uint_t i, n; |
507 | 21 |
509 | 22 for (n = 0, i = 0; i < value->len; i++) { |
23 if (value->data[i] == '$') { | |
24 n++; | |
25 } | |
507 | 26 } |
27 | |
509 | 28 return n; |
29 } | |
30 | |
31 | |
32 ngx_int_t | |
33 ngx_http_script_compile(ngx_http_script_compile_t *sc) | |
34 { | |
35 u_char ch; | |
36 size_t size; | |
573 | 37 ngx_int_t index, *p; |
509 | 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; | |
44 | |
573 | 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; | |
50 } | |
51 } | |
52 | |
53 | |
509 | 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); | |
58 | |
59 *sc->lengths = ngx_array_create(sc->cf->pool, n, 1); | |
60 if (*sc->lengths == NULL) { | |
61 return NGX_ERROR; | |
62 } | |
63 } | |
64 | |
65 | |
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); | |
73 | |
74 *sc->values = ngx_array_create(sc->cf->pool, n, 1); | |
75 if (*sc->values == NULL) { | |
507 | 76 return NGX_ERROR; |
77 } | |
78 } | |
79 | |
509 | 80 sc->variables = 0; |
81 | |
82 for (i = 0; i < sc->source->len; /* void */ ) { | |
83 | |
84 name.len = 0; | |
85 | |
86 if (sc->source->data[i] == '$') { | |
87 | |
88 if (++i == sc->source->len) { | |
89 goto invalid_variable; | |
90 } | |
91 | |
92 if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') { | |
93 | |
641 | 94 n = sc->source->data[i] - '0'; |
95 | |
96 if (sc->captures_mask & (1 << n)) { | |
97 sc->dup_capture = 1; | |
98 } | |
99 | |
100 sc->captures_mask |= 1 << n; | |
101 | |
509 | 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; | |
107 } | |
108 | |
109 copy_capture->code = (ngx_http_script_code_pt) | |
110 ngx_http_script_copy_capture_len_code; | |
641 | 111 copy_capture->n = 2 * n; |
112 | |
509 | 113 |
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; | |
119 } | |
120 | |
121 copy_capture->code = ngx_http_script_copy_capture_code; | |
641 | 122 copy_capture->n = 2 * n; |
509 | 123 |
641 | 124 if (sc->ncaptures < n) { |
125 sc->ncaptures = n; | |
509 | 126 } |
127 | |
128 i++; | |
129 | |
130 continue; | |
131 } | |
132 | |
133 if (sc->source->data[i] == '{') { | |
134 bracket = 1; | |
135 | |
136 if (++i == sc->source->len) { | |
137 goto invalid_variable; | |
138 } | |
139 | |
140 name.data = &sc->source->data[i]; | |
141 | |
142 } else { | |
143 bracket = 0; | |
144 name.data = &sc->source->data[i]; | |
145 } | |
146 | |
147 for ( /* void */ ; i < sc->source->len; i++, name.len++) { | |
148 ch = sc->source->data[i]; | |
507 | 149 |
509 | 150 if (ch == '}' && bracket) { |
151 i++; | |
152 bracket = 0; | |
153 break; | |
154 } | |
155 | |
156 if ((ch >= 'A' && ch <= 'Z') | |
157 || (ch >= 'a' && ch <= 'z') | |
158 || (ch >= '0' && ch <= '9') | |
159 || ch == '_') | |
160 { | |
161 continue; | |
162 } | |
163 | |
164 break; | |
165 } | |
166 | |
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); | |
507 | 171 return NGX_ERROR; |
172 } | |
173 | |
509 | 174 if (name.len == 0) { |
175 goto invalid_variable; | |
176 } | |
507 | 177 |
509 | 178 sc->variables++; |
507 | 179 |
509 | 180 index = ngx_http_get_variable_index(sc->cf, &name); |
181 | |
182 if (index == NGX_ERROR) { | |
507 | 183 return NGX_ERROR; |
184 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
185 |
573 | 186 if (sc->flushes) { |
187 p = ngx_array_push(*sc->flushes); | |
188 if (p == NULL) { | |
189 return NGX_ERROR; | |
190 } | |
191 | |
192 *p = index; | |
193 } | |
194 | |
509 | 195 var_code = ngx_http_script_add_code(*sc->lengths, |
196 sizeof(ngx_http_script_var_code_t), | |
197 NULL); | |
507 | 198 if (var_code == NULL) { |
199 return NGX_ERROR; | |
200 } | |
201 | |
202 var_code->code = (ngx_http_script_code_pt) | |
509 | 203 ngx_http_script_copy_var_len_code; |
204 var_code->index = (uintptr_t) index; | |
507 | 205 |
206 | |
509 | 207 var_code = ngx_http_script_add_code(*sc->values, |
208 sizeof(ngx_http_script_var_code_t), | |
209 &sc->main); | |
507 | 210 if (var_code == NULL) { |
211 return NGX_ERROR; | |
212 } | |
213 | |
509 | 214 var_code->code = ngx_http_script_copy_var_code; |
215 var_code->index = (uintptr_t) index; | |
216 | |
217 continue; | |
218 } | |
507 | 219 |
509 | 220 if (sc->source->data[i] == '?' && sc->compile_args) { |
221 sc->args = 1; | |
222 sc->compile_args = 0; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
223 |
1891
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
224 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
225 NULL); |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
226 if (code == NULL) { |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
227 return NGX_ERROR; |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
228 } |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
229 |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
230 *code = (uintptr_t) ngx_http_script_mark_args_code; |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
231 |
509 | 232 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), |
233 &sc->main); | |
234 if (code == NULL) { | |
507 | 235 return NGX_ERROR; |
236 } | |
237 | |
509 | 238 *code = (uintptr_t) ngx_http_script_start_args_code; |
239 | |
240 i++; | |
241 | |
507 | 242 continue; |
243 } | |
244 | |
509 | 245 name.data = &sc->source->data[i]; |
246 | |
2498
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
247 while (i < sc->source->len) { |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
248 |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
249 if (sc->source->data[i] == '$') { |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
250 break; |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
251 } |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
252 |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
253 if (sc->source->data[i] == '?') { |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
254 |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
255 sc->args = 1; |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
256 |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
257 if (sc->compile_args) { |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
258 break; |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
259 } |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
260 } |
d3357bf23a5e
fix /?new=arg?old=arg redirect case
Igor Sysoev <igor@sysoev.ru>
parents:
2397
diff
changeset
|
261 |
509 | 262 i++; |
263 name.len++; | |
264 } | |
265 | |
266 sc->size += name.len; | |
267 | |
268 copy = ngx_http_script_add_code(*sc->lengths, | |
269 sizeof(ngx_http_script_copy_code_t), | |
270 NULL); | |
271 if (copy == NULL) { | |
507 | 272 return NGX_ERROR; |
273 } | |
509 | 274 |
275 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code; | |
276 copy->len = name.len; | |
277 | |
278 size = (sizeof(ngx_http_script_copy_code_t) + name.len | |
279 + sizeof(uintptr_t) - 1) | |
280 & ~(sizeof(uintptr_t) - 1); | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
281 |
509 | 282 copy = ngx_http_script_add_code(*sc->values, size, &sc->main); |
283 if (copy == NULL) { | |
284 return NGX_ERROR; | |
285 } | |
286 | |
287 copy->code = ngx_http_script_copy_code; | |
288 copy->len = name.len; | |
289 | |
290 ngx_memcpy((u_char *) copy + sizeof(ngx_http_script_copy_code_t), | |
291 name.data, name.len); | |
507 | 292 } |
293 | |
509 | 294 if (sc->complete_lengths) { |
295 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL); | |
296 if (code == NULL) { | |
297 return NGX_ERROR; | |
298 } | |
507 | 299 |
509 | 300 *code = (uintptr_t) NULL; |
507 | 301 } |
302 | |
509 | 303 if (sc->complete_values) { |
304 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), | |
305 &sc->main); | |
306 if (code == NULL) { | |
307 return NGX_ERROR; | |
308 } | |
309 | |
310 *code = (uintptr_t) NULL; | |
311 } | |
507 | 312 |
313 return NGX_OK; | |
509 | 314 |
315 invalid_variable: | |
316 | |
317 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name"); | |
318 | |
319 return NGX_ERROR; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
320 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
321 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
322 |
623 | 323 u_char * |
324 ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value, | |
325 void *code_lengths, size_t len, void *code_values) | |
326 { | |
1152
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
327 ngx_uint_t i; |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
328 ngx_http_script_code_pt code; |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
329 ngx_http_script_len_code_pt lcode; |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
330 ngx_http_script_engine_t e; |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
331 ngx_http_core_main_conf_t *cmcf; |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
332 |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
333 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
334 |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
335 for (i = 0; i < cmcf->variables.nelts; i++) { |
1565 | 336 if (r->variables[i].no_cacheable) { |
1152
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
337 r->variables[i].valid = 0; |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
338 r->variables[i].not_found = 0; |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
339 } |
a9715f8f17a5
flush nocachable variables before ngx_http_script_run()
Igor Sysoev <igor@sysoev.ru>
parents:
845
diff
changeset
|
340 } |
623 | 341 |
342 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | |
343 | |
344 e.ip = code_lengths; | |
345 e.request = r; | |
346 e.flushed = 1; | |
347 | |
348 while (*(uintptr_t *) e.ip) { | |
349 lcode = *(ngx_http_script_len_code_pt *) e.ip; | |
350 len += lcode(&e); | |
351 } | |
352 | |
353 | |
354 value->len = len; | |
2049 | 355 value->data = ngx_pnalloc(r->pool, len); |
623 | 356 if (value->data == NULL) { |
357 return NULL; | |
358 } | |
359 | |
360 e.ip = code_values; | |
361 e.pos = value->data; | |
362 | |
363 while (*(uintptr_t *) e.ip) { | |
364 code = *(ngx_http_script_code_pt *) e.ip; | |
365 code((ngx_http_script_engine_t *) &e); | |
366 } | |
367 | |
368 return e.pos; | |
369 } | |
370 | |
371 | |
573 | 372 void |
1565 | 373 ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r, |
573 | 374 ngx_array_t *indices) |
375 { | |
376 ngx_uint_t n, *index; | |
377 | |
378 if (indices) { | |
379 index = indices->elts; | |
380 for (n = 0; n < indices->nelts; n++) { | |
1565 | 381 if (r->variables[index[n]].no_cacheable) { |
573 | 382 r->variables[index[n]].valid = 0; |
383 r->variables[index[n]].not_found = 0; | |
384 } | |
385 } | |
386 } | |
387 } | |
388 | |
389 | |
509 | 390 void * |
507 | 391 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size) |
479 | 392 { |
507 | 393 if (*codes == NULL) { |
394 *codes = ngx_array_create(pool, 256, 1); | |
395 if (*codes == NULL) { | |
396 return NULL; | |
397 } | |
398 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
399 |
507 | 400 return ngx_array_push_n(*codes, size); |
401 } | |
402 | |
509 | 403 |
404 void * | |
405 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code) | |
406 { | |
407 u_char *elts, **p; | |
408 void *new; | |
409 | |
410 elts = codes->elts; | |
411 | |
412 new = ngx_array_push_n(codes, size); | |
413 if (new == NULL) { | |
414 return NGX_CONF_ERROR; | |
415 } | |
416 | |
417 if (code) { | |
418 if (elts != codes->elts) { | |
419 p = code; | |
420 *p += (u_char *) codes->elts - elts; | |
421 } | |
422 } | |
423 | |
424 return new; | |
425 } | |
507 | 426 |
479 | 427 |
507 | 428 size_t |
509 | 429 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e) |
507 | 430 { |
431 ngx_http_script_copy_code_t *code; | |
479 | 432 |
509 | 433 code = (ngx_http_script_copy_code_t *) e->ip; |
507 | 434 |
509 | 435 e->ip += sizeof(ngx_http_script_copy_code_t); |
507 | 436 |
437 return code->len; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
438 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
439 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
440 |
507 | 441 void |
509 | 442 ngx_http_script_copy_code(ngx_http_script_engine_t *e) |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
443 { |
2393 | 444 u_char *p; |
507 | 445 ngx_http_script_copy_code_t *code; |
446 | |
509 | 447 code = (ngx_http_script_copy_code_t *) e->ip; |
448 | |
2393 | 449 p = e->pos; |
450 | |
509 | 451 if (!e->skip) { |
2393 | 452 e->pos = ngx_copy(p, e->ip + sizeof(ngx_http_script_copy_code_t), |
573 | 453 code->len); |
509 | 454 } |
507 | 455 |
509 | 456 e->ip += sizeof(ngx_http_script_copy_code_t) |
457 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1)); | |
507 | 458 |
2393 | 459 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
460 "http script copy: \"%*s\"", e->pos - p, p); | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
461 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
462 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
463 |
507 | 464 size_t |
509 | 465 ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e) |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
466 { |
507 | 467 ngx_http_variable_value_t *value; |
468 ngx_http_script_var_code_t *code; | |
469 | |
509 | 470 code = (ngx_http_script_var_code_t *) e->ip; |
507 | 471 |
509 | 472 e->ip += sizeof(ngx_http_script_var_code_t); |
507 | 473 |
573 | 474 if (e->flushed) { |
475 value = ngx_http_get_indexed_variable(e->request, code->index); | |
507 | 476 |
573 | 477 } else { |
478 value = ngx_http_get_flushed_variable(e->request, code->index); | |
479 } | |
480 | |
481 if (value && !value->not_found) { | |
482 return value->len; | |
507 | 483 } |
484 | |
553 | 485 return 0; |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
486 } |
479 | 487 |
488 | |
507 | 489 void |
509 | 490 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e) |
479 | 491 { |
2393 | 492 u_char *p; |
507 | 493 ngx_http_variable_value_t *value; |
494 ngx_http_script_var_code_t *code; | |
495 | |
509 | 496 code = (ngx_http_script_var_code_t *) e->ip; |
497 | |
498 e->ip += sizeof(ngx_http_script_var_code_t); | |
499 | |
500 if (!e->skip) { | |
573 | 501 |
502 if (e->flushed) { | |
503 value = ngx_http_get_indexed_variable(e->request, code->index); | |
509 | 504 |
573 | 505 } else { |
506 value = ngx_http_get_flushed_variable(e->request, code->index); | |
507 } | |
508 | |
509 if (value && !value->not_found) { | |
2393 | 510 p = e->pos; |
511 e->pos = ngx_copy(p, value->data, value->len); | |
509 | 512 |
2393 | 513 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, |
845
eb5cf273e5d2
debug log should not be under rewrite_log control
Igor Sysoev <igor@sysoev.ru>
parents:
730
diff
changeset
|
514 e->request->connection->log, 0, |
2393 | 515 "http script var: \"%*s\"", e->pos - p, p); |
509 | 516 } |
517 } | |
518 } | |
519 | |
520 | |
521 size_t | |
522 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e) | |
523 { | |
524 ngx_http_script_copy_capture_code_t *code; | |
525 | |
526 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
527 | |
528 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
529 | |
515 | 530 if (code->n < e->ncaptures) { |
1891
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
531 if ((e->is_args || e->quote) |
515 | 532 && (e->request->quoted_uri || e->request->plus_in_uri)) |
533 { | |
534 return e->captures[code->n + 1] - e->captures[code->n] | |
655 | 535 + 2 * ngx_escape_uri(NULL, |
515 | 536 &e->line.data[e->captures[code->n]], |
509 | 537 e->captures[code->n + 1] - e->captures[code->n], |
538 NGX_ESCAPE_ARGS); | |
515 | 539 } else { |
540 return e->captures[code->n + 1] - e->captures[code->n]; | |
541 } | |
509 | 542 } |
515 | 543 |
544 return 0; | |
509 | 545 } |
546 | |
547 | |
548 void | |
549 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e) | |
550 { | |
2393 | 551 u_char *p; |
509 | 552 ngx_http_script_copy_capture_code_t *code; |
553 | |
554 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
555 | |
556 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
507 | 557 |
2393 | 558 p = e->pos; |
559 | |
515 | 560 if (code->n < e->ncaptures) { |
1891
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
561 if ((e->is_args || e->quote) |
515 | 562 && (e->request->quoted_uri || e->request->plus_in_uri)) |
563 { | |
2393 | 564 e->pos = (u_char *) ngx_escape_uri(p, |
515 | 565 &e->line.data[e->captures[code->n]], |
509 | 566 e->captures[code->n + 1] - e->captures[code->n], |
567 NGX_ESCAPE_ARGS); | |
515 | 568 } else { |
2393 | 569 e->pos = ngx_copy(p, |
573 | 570 &e->line.data[e->captures[code->n]], |
571 e->captures[code->n + 1] - e->captures[code->n]); | |
515 | 572 } |
509 | 573 } |
574 | |
2393 | 575 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
576 "http script capture: \"%*s\"", e->pos - p, p); | |
509 | 577 } |
578 | |
579 | |
1891
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
580 size_t |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
581 ngx_http_script_mark_args_code(ngx_http_script_engine_t *e) |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
582 { |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
583 e->is_args = 1; |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
584 e->ip += sizeof(uintptr_t); |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
585 |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
586 return 1; |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
587 } |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
588 |
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
589 |
509 | 590 void |
591 ngx_http_script_start_args_code(ngx_http_script_engine_t *e) | |
592 { | |
593 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
594 "http script args"); | |
577 | 595 |
2225
207827f7bf71
escape a query string characters taken from URI while rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2133
diff
changeset
|
596 e->is_args = 1; |
509 | 597 e->args = e->pos; |
598 e->ip += sizeof(uintptr_t); | |
599 } | |
600 | |
601 | |
517 | 602 |
603 #if (NGX_PCRE) | |
604 | |
509 | 605 void |
606 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e) | |
607 { | |
608 size_t len; | |
609 ngx_int_t rc; | |
610 ngx_uint_t n; | |
611 ngx_http_request_t *r; | |
612 ngx_http_script_engine_t le; | |
613 ngx_http_script_len_code_pt lcode; | |
614 ngx_http_script_regex_code_t *code; | |
615 | |
616 code = (ngx_http_script_regex_code_t *) e->ip; | |
617 | |
618 r = e->request; | |
619 | |
620 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
621 "http script regex: \"%V\"", &code->name); | |
622 | |
623 if (code->uri) { | |
515 | 624 e->line = r->uri; |
509 | 625 } else { |
626 e->sp--; | |
573 | 627 e->line.len = e->sp->len; |
628 e->line.data = e->sp->data; | |
509 | 629 } |
507 | 630 |
515 | 631 rc = ngx_regex_exec(code->regex, &e->line, e->captures, code->ncaptures); |
509 | 632 |
633 if (rc == NGX_REGEX_NO_MATCHED) { | |
2397 | 634 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) { |
509 | 635 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
515 | 636 "\"%V\" does not match \"%V\"", |
637 &code->name, &e->line); | |
509 | 638 } |
639 | |
515 | 640 e->ncaptures = 0; |
641 | |
509 | 642 if (code->test) { |
631 | 643 if (code->negative_test) { |
644 e->sp->len = 1; | |
645 e->sp->data = (u_char *) "1"; | |
646 | |
647 } else { | |
648 e->sp->len = 0; | |
649 e->sp->data = (u_char *) ""; | |
650 } | |
651 | |
509 | 652 e->sp++; |
653 | |
654 e->ip += sizeof(ngx_http_script_regex_code_t); | |
655 return; | |
656 } | |
657 | |
658 e->ip += code->next; | |
659 return; | |
660 } | |
661 | |
662 if (rc < 0) { | |
663 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
664 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"", | |
515 | 665 rc, &e->line, &code->name); |
509 | 666 |
667 e->ip = ngx_http_script_exit; | |
668 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
669 return; | |
670 } | |
671 | |
2397 | 672 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) { |
509 | 673 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
515 | 674 "\"%V\" matches \"%V\"", &code->name, &e->line); |
509 | 675 } |
676 | |
515 | 677 e->ncaptures = code->ncaptures; |
678 | |
509 | 679 if (code->test) { |
631 | 680 if (code->negative_test) { |
681 e->sp->len = 0; | |
682 e->sp->data = (u_char *) ""; | |
683 | |
684 } else { | |
685 e->sp->len = 1; | |
686 e->sp->data = (u_char *) "1"; | |
687 } | |
688 | |
509 | 689 e->sp++; |
690 | |
691 e->ip += sizeof(ngx_http_script_regex_code_t); | |
692 return; | |
693 } | |
694 | |
695 if (code->status) { | |
696 e->status = code->status; | |
507 | 697 |
509 | 698 if (!code->redirect) { |
699 e->ip = ngx_http_script_exit; | |
700 return; | |
701 } | |
702 } | |
703 | |
704 if (code->uri) { | |
705 r->internal = 1; | |
706 r->valid_unparsed_uri = 0; | |
707 | |
708 if (code->break_cycle) { | |
709 r->valid_location = 0; | |
527 | 710 r->uri_changed = 0; |
509 | 711 |
712 } else { | |
713 r->uri_changed = 1; | |
714 } | |
715 } | |
716 | |
717 if (code->lengths == NULL) { | |
718 e->buf.len = code->size; | |
719 | |
720 if (code->uri) { | |
721 if (rc && (r->quoted_uri || r->plus_in_uri)) { | |
722 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, | |
723 NGX_ESCAPE_ARGS); | |
724 } | |
725 } | |
726 | |
727 for (n = 1; n < (ngx_uint_t) rc; n++) { | |
728 e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n]; | |
729 } | |
730 | |
731 } else { | |
732 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
733 | |
734 le.ip = code->lengths->elts; | |
679 | 735 le.line = e->line; |
509 | 736 le.request = r; |
737 le.captures = e->captures; | |
515 | 738 le.ncaptures = e->ncaptures; |
679 | 739 le.quote = code->redirect; |
509 | 740 |
1891
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
741 len = 0; |
509 | 742 |
743 while (*(uintptr_t *) le.ip) { | |
744 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
745 len += lcode(&le); | |
746 } | |
747 | |
748 e->buf.len = len; | |
1891
782af1038115
length calculation did not take into account escaped symbols in arguments
Igor Sysoev <igor@sysoev.ru>
parents:
1799
diff
changeset
|
749 e->is_args = le.is_args; |
509 | 750 } |
751 | |
515 | 752 if (code->add_args && r->args.len) { |
509 | 753 e->buf.len += r->args.len + 1; |
754 } | |
755 | |
2049 | 756 e->buf.data = ngx_pnalloc(r->pool, e->buf.len); |
509 | 757 if (e->buf.data == NULL) { |
758 e->ip = ngx_http_script_exit; | |
759 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
507 | 760 return; |
761 } | |
762 | |
509 | 763 e->quote = code->redirect; |
764 | |
765 e->pos = e->buf.data; | |
766 | |
767 e->ip += sizeof(ngx_http_script_regex_code_t); | |
768 } | |
769 | |
770 | |
771 void | |
772 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e) | |
773 { | |
577 | 774 u_char *dst, *src; |
509 | 775 ngx_http_request_t *r; |
776 ngx_http_script_regex_end_code_t *code; | |
777 | |
778 code = (ngx_http_script_regex_end_code_t *) e->ip; | |
779 | |
780 r = e->request; | |
781 | |
782 e->quote = 0; | |
783 | |
784 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
785 "http script regex end"); | |
786 | |
787 if (code->redirect) { | |
788 | |
577 | 789 dst = e->buf.data; |
790 src = e->buf.data; | |
791 | |
1593 | 792 ngx_unescape_uri(&dst, &src, e->pos - e->buf.data, |
793 NGX_UNESCAPE_REDIRECT); | |
577 | 794 |
795 if (src < e->pos) { | |
796 dst = ngx_copy(dst, src, e->pos - src); | |
797 } | |
798 | |
799 e->pos = dst; | |
800 | |
801 if (code->add_args && r->args.len) { | |
509 | 802 *e->pos++ = (u_char) (code->args ? '&' : '?'); |
573 | 803 e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
509 | 804 } |
805 | |
806 e->buf.len = e->pos - e->buf.data; | |
807 | |
2397 | 808 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) { |
509 | 809 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
810 "rewritten redirect: \"%V\"", &e->buf); | |
811 } | |
812 | |
813 r->headers_out.location = ngx_list_push(&r->headers_out.headers); | |
814 if (r->headers_out.location == NULL) { | |
815 e->ip = ngx_http_script_exit; | |
816 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
817 return; | |
818 } | |
819 | |
577 | 820 r->headers_out.location->hash = 1; |
509 | 821 r->headers_out.location->key.len = sizeof("Location") - 1; |
822 r->headers_out.location->key.data = (u_char *) "Location"; | |
823 r->headers_out.location->value = e->buf; | |
824 | |
825 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
826 return; | |
827 } | |
828 | |
577 | 829 if (e->args) { |
509 | 830 e->buf.len = e->args - e->buf.data; |
831 | |
832 if (code->add_args && r->args.len) { | |
833 *e->pos++ = '&'; | |
573 | 834 e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
509 | 835 } |
836 | |
837 r->args.len = e->pos - e->args; | |
838 r->args.data = e->args; | |
839 | |
840 e->args = NULL; | |
841 | |
842 } else { | |
843 e->buf.len = e->pos - e->buf.data; | |
673 | 844 |
845 if (!code->add_args) { | |
846 r->args.len = 0; | |
847 } | |
509 | 848 } |
849 | |
2397 | 850 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) { |
509 | 851 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
852 "rewritten data: \"%V\", args: \"%V\"", | |
853 &e->buf, &r->args); | |
854 } | |
855 | |
856 if (code->uri) { | |
857 r->uri = e->buf; | |
858 | |
535 | 859 if (r->uri.len == 0) { |
860 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
861 "the rewritten URI has a zero length"); | |
862 e->ip = ngx_http_script_exit; | |
863 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
864 return; | |
865 } | |
866 | |
509 | 867 if (ngx_http_set_exten(r) != NGX_OK) { |
868 e->ip = ngx_http_script_exit; | |
869 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
870 return; | |
871 } | |
872 } | |
873 | |
874 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
875 } | |
876 | |
517 | 877 #endif |
878 | |
509 | 879 |
880 void | |
881 ngx_http_script_return_code(ngx_http_script_engine_t *e) | |
577 | 882 { |
509 | 883 ngx_http_script_return_code_t *code; |
577 | 884 |
509 | 885 code = (ngx_http_script_return_code_t *) e->ip; |
886 | |
887 e->status = code->status; | |
888 | |
653 | 889 if (code->status == NGX_HTTP_NO_CONTENT) { |
890 e->request->header_only = 1; | |
730
1be87cdd010f
set "Content-Length: 0" for errors handled by "return 204"
Igor Sysoev <igor@sysoev.ru>
parents:
679
diff
changeset
|
891 e->request->zero_body = 1; |
653 | 892 } |
893 | |
509 | 894 e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t); |
479 | 895 } |
509 | 896 |
897 | |
898 void | |
527 | 899 ngx_http_script_break_code(ngx_http_script_engine_t *e) |
900 { | |
901 e->request->uri_changed = 0; | |
902 | |
903 e->ip = ngx_http_script_exit; | |
904 } | |
905 | |
906 | |
907 void | |
509 | 908 ngx_http_script_if_code(ngx_http_script_engine_t *e) |
909 { | |
910 ngx_http_script_if_code_t *code; | |
911 | |
912 code = (ngx_http_script_if_code_t *) e->ip; | |
913 | |
914 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
915 "http script if"); | |
916 | |
917 e->sp--; | |
918 | |
573 | 919 if (e->sp->len && e->sp->data[0] != '0') { |
509 | 920 if (code->loc_conf) { |
921 e->request->loc_conf = code->loc_conf; | |
535 | 922 ngx_http_update_location_config(e->request); |
509 | 923 } |
924 | |
925 e->ip += sizeof(ngx_http_script_if_code_t); | |
926 return; | |
927 } | |
928 | |
929 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
577 | 930 "http script if: false"); |
509 | 931 |
932 e->ip += code->next; | |
933 } | |
934 | |
935 | |
936 void | |
577 | 937 ngx_http_script_equal_code(ngx_http_script_engine_t *e) |
938 { | |
939 ngx_http_variable_value_t *val, *res; | |
940 | |
941 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
942 "http script equal"); | |
943 | |
944 e->sp--; | |
945 val = e->sp; | |
946 res = e->sp - 1; | |
947 | |
948 e->ip += sizeof(uintptr_t); | |
949 | |
2393 | 950 if (val->len == res->len |
951 && ngx_strncmp(val->data, res->data, res->len) == 0) | |
577 | 952 { |
953 *res = ngx_http_variable_true_value; | |
954 return; | |
955 } | |
956 | |
957 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
958 "http script equal: no"); | |
959 | |
960 *res = ngx_http_variable_null_value; | |
961 } | |
962 | |
963 | |
964 void | |
965 ngx_http_script_not_equal_code(ngx_http_script_engine_t *e) | |
966 { | |
967 ngx_http_variable_value_t *val, *res; | |
968 | |
969 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
970 "http script not equal"); | |
971 | |
972 e->sp--; | |
973 val = e->sp; | |
974 res = e->sp - 1; | |
975 | |
976 e->ip += sizeof(uintptr_t); | |
977 | |
2393 | 978 if (val->len == res->len |
979 && ngx_strncmp(val->data, res->data, res->len) == 0) | |
577 | 980 { |
981 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
982 "http script not equal: no"); | |
983 | |
984 *res = ngx_http_variable_null_value; | |
985 return; | |
986 } | |
987 | |
988 *res = ngx_http_variable_true_value; | |
989 } | |
990 | |
991 | |
992 void | |
631 | 993 ngx_http_script_file_code(ngx_http_script_engine_t *e) |
994 { | |
1454 | 995 ngx_str_t path; |
996 ngx_http_request_t *r; | |
997 ngx_open_file_info_t of; | |
998 ngx_http_core_loc_conf_t *clcf; | |
631 | 999 ngx_http_variable_value_t *value; |
1000 ngx_http_script_file_code_t *code; | |
1001 | |
1002 value = e->sp - 1; | |
1003 | |
1004 code = (ngx_http_script_file_code_t *) e->ip; | |
1005 e->ip += sizeof(ngx_http_script_file_code_t); | |
1006 | |
1454 | 1007 path.len = value->len - 1; |
1008 path.data = value->data; | |
1009 | |
1010 r = e->request; | |
1011 | |
1012 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1013 "http script file op %p \"%V\"", code->op, &path); | |
1014 | |
1015 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
631 | 1016 |
2068
75a8d34459c5
ngx_memzero() ngx_open_file_info_t
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
1017 ngx_memzero(&of, sizeof(ngx_open_file_info_t)); |
75a8d34459c5
ngx_memzero() ngx_open_file_info_t
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
1018 |
2129 | 1019 of.directio = clcf->directio; |
1767
c42431762903
open_file_cache_retest > open_file_cache_valid
Igor Sysoev <igor@sysoev.ru>
parents:
1593
diff
changeset
|
1020 of.valid = clcf->open_file_cache_valid; |
1772 | 1021 of.min_uses = clcf->open_file_cache_min_uses; |
1454 | 1022 of.errors = clcf->open_file_cache_errors; |
1457 | 1023 of.events = clcf->open_file_cache_events; |
631 | 1024 |
1454 | 1025 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) |
1799 | 1026 != NGX_OK) |
1454 | 1027 { |
1028 if (of.err != NGX_ENOENT && of.err != NGX_ENOTDIR) { | |
1029 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err, | |
631 | 1030 ngx_file_info_n " \"%s\" failed", value->data); |
1031 } | |
1032 | |
1033 switch (code->op) { | |
671 | 1034 |
631 | 1035 case ngx_http_script_file_plain: |
669 | 1036 case ngx_http_script_file_dir: |
1037 case ngx_http_script_file_exists: | |
1038 case ngx_http_script_file_exec: | |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1039 goto false_value; |
671 | 1040 |
631 | 1041 case ngx_http_script_file_not_plain: |
669 | 1042 case ngx_http_script_file_not_dir: |
671 | 1043 case ngx_http_script_file_not_exists: |
669 | 1044 case ngx_http_script_file_not_exec: |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1045 goto true_value; |
631 | 1046 } |
1047 | |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1048 goto false_value; |
631 | 1049 } |
1050 | |
1051 switch (code->op) { | |
1052 case ngx_http_script_file_plain: | |
1454 | 1053 if (of.is_file) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1054 goto true_value; |
631 | 1055 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1056 goto false_value; |
631 | 1057 |
1058 case ngx_http_script_file_not_plain: | |
1454 | 1059 if (of.is_file) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1060 goto false_value; |
631 | 1061 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1062 goto true_value; |
669 | 1063 |
1064 case ngx_http_script_file_dir: | |
1454 | 1065 if (of.is_dir) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1066 goto true_value; |
669 | 1067 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1068 goto false_value; |
669 | 1069 |
1070 case ngx_http_script_file_not_dir: | |
1454 | 1071 if (of.is_dir) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1072 goto false_value; |
669 | 1073 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1074 goto true_value; |
669 | 1075 |
1076 case ngx_http_script_file_exists: | |
1454 | 1077 if (of.is_file || of.is_dir || of.is_link) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1078 goto true_value; |
669 | 1079 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1080 goto false_value; |
669 | 1081 |
1082 case ngx_http_script_file_not_exists: | |
1454 | 1083 if (of.is_file || of.is_dir || of.is_link) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1084 goto false_value; |
669 | 1085 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1086 goto true_value; |
669 | 1087 |
1088 case ngx_http_script_file_exec: | |
1454 | 1089 if (of.is_exec) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1090 goto true_value; |
669 | 1091 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1092 goto false_value; |
669 | 1093 |
1094 case ngx_http_script_file_not_exec: | |
1454 | 1095 if (of.is_exec) { |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1096 goto false_value; |
669 | 1097 } |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1098 goto true_value; |
631 | 1099 } |
1100 | |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1101 false_value: |
631 | 1102 |
1454 | 1103 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
631 | 1104 "http script file op false"); |
1105 | |
1106 *value = ngx_http_variable_null_value; | |
1107 return; | |
1108 | |
2133
c505e2712e13
fix conflicting names "true" and "false"
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
1109 true_value: |
631 | 1110 |
1111 *value = ngx_http_variable_true_value; | |
1112 return; | |
1113 } | |
1114 | |
1115 | |
1116 void | |
515 | 1117 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e) |
1118 { | |
1119 size_t len; | |
1120 ngx_http_script_engine_t le; | |
1121 ngx_http_script_len_code_pt lcode; | |
1122 ngx_http_script_complex_value_code_t *code; | |
1123 | |
1124 code = (ngx_http_script_complex_value_code_t *) e->ip; | |
1125 | |
1126 e->ip += sizeof(ngx_http_script_complex_value_code_t); | |
1127 | |
1128 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1129 "http script complex value"); | |
1130 | |
1131 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
1132 | |
1133 le.ip = code->lengths->elts; | |
655 | 1134 le.line = e->line; |
515 | 1135 le.request = e->request; |
1136 le.captures = e->captures; | |
1137 le.ncaptures = e->ncaptures; | |
655 | 1138 le.quote = e->quote; |
515 | 1139 |
1140 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) { | |
1141 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
1142 } | |
1143 | |
1144 e->buf.len = len; | |
2049 | 1145 e->buf.data = ngx_pnalloc(e->request->pool, len); |
515 | 1146 if (e->buf.data == NULL) { |
1147 e->ip = ngx_http_script_exit; | |
1148 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1149 return; | |
1150 } | |
1151 | |
1152 e->pos = e->buf.data; | |
1153 | |
577 | 1154 e->sp->len = e->buf.len; |
573 | 1155 e->sp->data = e->buf.data; |
515 | 1156 e->sp++; |
1157 } | |
1158 | |
1159 | |
1160 void | |
509 | 1161 ngx_http_script_value_code(ngx_http_script_engine_t *e) |
1162 { | |
1163 ngx_http_script_value_code_t *code; | |
1164 | |
1165 code = (ngx_http_script_value_code_t *) e->ip; | |
1166 | |
1167 e->ip += sizeof(ngx_http_script_value_code_t); | |
1168 | |
573 | 1169 e->sp->len = code->text_len; |
1170 e->sp->data = (u_char *) code->text_data; | |
577 | 1171 |
1172 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1561 | 1173 "http script value: \"%v\"", e->sp); |
577 | 1174 |
509 | 1175 e->sp++; |
1176 } | |
1177 | |
1178 | |
1179 void | |
1180 ngx_http_script_set_var_code(ngx_http_script_engine_t *e) | |
1181 { | |
1182 ngx_http_request_t *r; | |
1183 ngx_http_script_var_code_t *code; | |
1184 | |
1185 code = (ngx_http_script_var_code_t *) e->ip; | |
1186 | |
1187 e->ip += sizeof(ngx_http_script_var_code_t); | |
1188 | |
1189 r = e->request; | |
1190 | |
1191 e->sp--; | |
1192 | |
573 | 1193 r->variables[code->index].len = e->sp->len; |
1194 r->variables[code->index].valid = 1; | |
1565 | 1195 r->variables[code->index].no_cacheable = 0; |
573 | 1196 r->variables[code->index].not_found = 0; |
1197 r->variables[code->index].data = e->sp->data; | |
2393 | 1198 |
1199 #if (NGX_DEBUG) | |
1200 { | |
1201 ngx_http_variable_t *v; | |
1202 ngx_http_core_main_conf_t *cmcf; | |
1203 | |
1204 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
1205 | |
1206 v = cmcf->variables.elts; | |
1207 | |
1208 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1209 "http script set $%V", &v[code->index].name); | |
1210 } | |
1211 #endif | |
509 | 1212 } |
1213 | |
1214 | |
1215 void | |
637 | 1216 ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e) |
1217 { | |
1218 ngx_http_script_var_handler_code_t *code; | |
1219 | |
1220 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1221 "http script set var handler"); | |
1222 | |
1223 code = (ngx_http_script_var_handler_code_t *) e->ip; | |
1224 | |
1225 e->ip += sizeof(ngx_http_script_var_handler_code_t); | |
1226 | |
1227 e->sp--; | |
1228 | |
1229 code->handler(e->request, e->sp, code->data); | |
1230 } | |
1231 | |
1232 | |
1233 void | |
509 | 1234 ngx_http_script_var_code(ngx_http_script_engine_t *e) |
1235 { | |
1236 ngx_http_variable_value_t *value; | |
1237 ngx_http_script_var_code_t *code; | |
1238 | |
1239 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1240 "http script var"); | |
1241 | |
1242 code = (ngx_http_script_var_code_t *) e->ip; | |
1243 | |
1244 e->ip += sizeof(ngx_http_script_var_code_t); | |
1245 | |
573 | 1246 value = ngx_http_get_flushed_variable(e->request, code->index); |
509 | 1247 |
573 | 1248 if (value && !value->not_found) { |
1249 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1406
03341711f9a2
use %v for ngx_variable_value_t in ngx_sprintf(),
Igor Sysoev <igor@sysoev.ru>
parents:
1152
diff
changeset
|
1250 "http script var: \"%v\"", value); |
573 | 1251 |
553 | 1252 *e->sp = *value; |
509 | 1253 e->sp++; |
1254 | |
1255 return; | |
1256 } | |
1257 | |
577 | 1258 *e->sp = ngx_http_variable_null_value; |
509 | 1259 e->sp++; |
1260 } | |
1261 | |
1262 | |
1263 void | |
1264 ngx_http_script_nop_code(ngx_http_script_engine_t *e) | |
1265 { | |
1266 e->ip += sizeof(uintptr_t); | |
1267 } |