50
|
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 typedef struct {
|
126
|
13 ngx_array_t *codes; /* uintptr_t */
|
50
|
14
|
126
|
15 ngx_uint_t stack_size;
|
50
|
16
|
126
|
17 ngx_flag_t log;
|
184
|
18 ngx_flag_t uninitialized_variable_warn;
|
50
|
19 } ngx_http_rewrite_loc_conf_t;
|
|
20
|
|
21
|
|
22 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf);
|
|
23 static char *ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf,
|
|
24 void *parent, void *child);
|
230
|
25 static ngx_int_t ngx_http_rewrite_init(ngx_conf_t *cf);
|
50
|
26 static char *ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
27 static char *ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
28 void *conf);
|
76
|
29 static char *ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
30 void *conf);
|
50
|
31 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
32 void *conf);
|
|
33 static char * ngx_http_rewrite_if_condition(ngx_conf_t *cf,
|
|
34 ngx_http_rewrite_loc_conf_t *lcf);
|
|
35 static char *ngx_http_rewrite_variable(ngx_conf_t *cf,
|
|
36 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
|
58
|
37 static char *ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd,
|
50
|
38 void *conf);
|
126
|
39 static char * ngx_http_rewrite_value(ngx_conf_t *cf,
|
|
40 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
|
50
|
41
|
|
42
|
|
43 static ngx_command_t ngx_http_rewrite_commands[] = {
|
|
44
|
|
45 { ngx_string("rewrite"),
|
|
46 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
47 |NGX_CONF_TAKE23,
|
|
48 ngx_http_rewrite,
|
|
49 NGX_HTTP_LOC_CONF_OFFSET,
|
|
50 0,
|
|
51 NULL },
|
|
52
|
|
53 { ngx_string("return"),
|
|
54 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
55 |NGX_CONF_TAKE1,
|
|
56 ngx_http_rewrite_return,
|
|
57 NGX_HTTP_LOC_CONF_OFFSET,
|
|
58 0,
|
|
59 NULL },
|
|
60
|
76
|
61 { ngx_string("break"),
|
|
62 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
63 |NGX_CONF_NOARGS,
|
|
64 ngx_http_rewrite_break,
|
|
65 NGX_HTTP_LOC_CONF_OFFSET,
|
|
66 0,
|
|
67 NULL },
|
|
68
|
50
|
69 { ngx_string("if"),
|
|
70 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
|
|
71 ngx_http_rewrite_if,
|
|
72 NGX_HTTP_LOC_CONF_OFFSET,
|
|
73 0,
|
|
74 NULL },
|
|
75
|
|
76 { ngx_string("set"),
|
|
77 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
78 |NGX_CONF_TAKE2,
|
|
79 ngx_http_rewrite_set,
|
|
80 NGX_HTTP_LOC_CONF_OFFSET,
|
|
81 0,
|
|
82 NULL },
|
|
83
|
|
84 { ngx_string("rewrite_log"),
|
184
|
85 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
|
|
86 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
|
50
|
87 ngx_conf_set_flag_slot,
|
|
88 NGX_HTTP_LOC_CONF_OFFSET,
|
|
89 offsetof(ngx_http_rewrite_loc_conf_t, log),
|
|
90 NULL },
|
|
91
|
184
|
92 { ngx_string("uninitialized_variable_warn"),
|
|
93 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
|
|
94 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
|
|
95 ngx_conf_set_flag_slot,
|
|
96 NGX_HTTP_LOC_CONF_OFFSET,
|
|
97 offsetof(ngx_http_rewrite_loc_conf_t, uninitialized_variable_warn),
|
|
98 NULL },
|
|
99
|
50
|
100 ngx_null_command
|
|
101 };
|
|
102
|
|
103
|
216
|
104 static ngx_http_module_t ngx_http_rewrite_module_ctx = {
|
58
|
105 NULL, /* preconfiguration */
|
230
|
106 ngx_http_rewrite_init, /* postconfiguration */
|
50
|
107
|
|
108 NULL, /* create main configuration */
|
|
109 NULL, /* init main configuration */
|
|
110
|
|
111 NULL, /* create server configuration */
|
|
112 NULL, /* merge server configuration */
|
|
113
|
|
114 ngx_http_rewrite_create_loc_conf, /* create location configration */
|
|
115 ngx_http_rewrite_merge_loc_conf /* merge location configration */
|
|
116 };
|
|
117
|
|
118
|
|
119 ngx_module_t ngx_http_rewrite_module = {
|
58
|
120 NGX_MODULE_V1,
|
126
|
121 &ngx_http_rewrite_module_ctx, /* module context */
|
50
|
122 ngx_http_rewrite_commands, /* module directives */
|
|
123 NGX_HTTP_MODULE, /* module type */
|
90
|
124 NULL, /* init master */
|
230
|
125 NULL, /* init module */
|
90
|
126 NULL, /* init process */
|
|
127 NULL, /* init thread */
|
|
128 NULL, /* exit thread */
|
|
129 NULL, /* exit process */
|
|
130 NULL, /* exit master */
|
|
131 NGX_MODULE_V1_PADDING
|
50
|
132 };
|
|
133
|
|
134
|
|
135 static ngx_int_t
|
|
136 ngx_http_rewrite_handler(ngx_http_request_t *r)
|
|
137 {
|
58
|
138 ngx_http_script_code_pt code;
|
|
139 ngx_http_script_engine_t *e;
|
184
|
140 ngx_http_rewrite_loc_conf_t *rlcf;
|
50
|
141
|
184
|
142 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
|
50
|
143
|
184
|
144 if (rlcf->codes == NULL) {
|
50
|
145 return NGX_DECLINED;
|
|
146 }
|
|
147
|
58
|
148 e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t));
|
50
|
149 if (e == NULL) {
|
|
150 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
151 }
|
|
152
|
58
|
153 e->sp = ngx_pcalloc(r->pool,
|
184
|
154 rlcf->stack_size * sizeof(ngx_http_variable_value_t));
|
50
|
155 if (e->sp == NULL) {
|
|
156 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
157 }
|
|
158
|
184
|
159 e->ip = rlcf->codes->elts;
|
58
|
160 e->request = r;
|
50
|
161 e->quote = 1;
|
184
|
162 e->log = rlcf->log;
|
50
|
163 e->status = NGX_DECLINED;
|
|
164
|
|
165 while (*(uintptr_t *) e->ip) {
|
58
|
166 code = *(ngx_http_script_code_pt *) e->ip;
|
50
|
167 code(e);
|
|
168 }
|
|
169
|
242
|
170 if (e->status == NGX_DECLINED) {
|
|
171 return NGX_DECLINED;
|
|
172 }
|
|
173
|
238
|
174 if (r->err_status == 0) {
|
|
175 return e->status;
|
|
176 }
|
|
177
|
|
178 return r->err_status;
|
50
|
179 }
|
|
180
|
|
181
|
122
|
182 static ngx_int_t
|
|
183 ngx_http_rewrite_var(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
184 uintptr_t data)
|
50
|
185 {
|
184
|
186 ngx_http_variable_t *var;
|
|
187 ngx_http_core_main_conf_t *cmcf;
|
|
188 ngx_http_rewrite_loc_conf_t *rlcf;
|
|
189
|
|
190 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
|
|
191
|
|
192 if (rlcf->uninitialized_variable_warn == 0) {
|
|
193 *v = ngx_http_variable_null_value;
|
|
194 return NGX_OK;
|
|
195 }
|
50
|
196
|
|
197 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
198
|
|
199 var = cmcf->variables.elts;
|
|
200
|
|
201 /*
|
|
202 * the ngx_http_rewrite_module sets variables directly in r->variables,
|
68
|
203 * and they should be handled by ngx_http_get_indexed_variable(),
|
50
|
204 * so the handler is called only if the variable is not initialized
|
|
205 */
|
|
206
|
184
|
207 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
|
50
|
208 "using uninitialized \"%V\" variable", &var[data].name);
|
|
209
|
126
|
210 *v = ngx_http_variable_null_value;
|
122
|
211
|
|
212 return NGX_OK;
|
50
|
213 }
|
|
214
|
|
215
|
|
216 static void *
|
|
217 ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
|
|
218 {
|
|
219 ngx_http_rewrite_loc_conf_t *conf;
|
|
220
|
|
221 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t));
|
|
222 if (conf == NULL) {
|
536
|
223 return NULL;
|
50
|
224 }
|
|
225
|
|
226 conf->stack_size = NGX_CONF_UNSET_UINT;
|
|
227 conf->log = NGX_CONF_UNSET;
|
184
|
228 conf->uninitialized_variable_warn = NGX_CONF_UNSET;
|
50
|
229
|
|
230 return conf;
|
|
231 }
|
|
232
|
|
233
|
|
234 static char *
|
|
235 ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
236 {
|
|
237 ngx_http_rewrite_loc_conf_t *prev = parent;
|
|
238 ngx_http_rewrite_loc_conf_t *conf = child;
|
|
239
|
58
|
240 uintptr_t *code;
|
50
|
241
|
|
242 ngx_conf_merge_value(conf->log, prev->log, 0);
|
184
|
243 ngx_conf_merge_value(conf->uninitialized_variable_warn,
|
|
244 prev->uninitialized_variable_warn, 1);
|
212
|
245 ngx_conf_merge_uint_value(conf->stack_size, prev->stack_size, 10);
|
50
|
246
|
|
247 if (conf->codes == NULL) {
|
|
248 return NGX_CONF_OK;
|
|
249 }
|
|
250
|
|
251 if (conf->codes == prev->codes) {
|
|
252 return NGX_CONF_OK;
|
|
253 }
|
|
254
|
|
255 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t));
|
|
256 if (code == NULL) {
|
|
257 return NGX_CONF_ERROR;
|
|
258 }
|
|
259
|
|
260 *code = (uintptr_t) NULL;
|
|
261
|
|
262 return NGX_CONF_OK;
|
|
263 }
|
|
264
|
|
265
|
58
|
266 static ngx_int_t
|
230
|
267 ngx_http_rewrite_init(ngx_conf_t *cf)
|
126
|
268 {
|
58
|
269 ngx_http_handler_pt *h;
|
|
270 ngx_http_core_main_conf_t *cmcf;
|
|
271
|
230
|
272 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
58
|
273
|
122
|
274 h = ngx_array_push(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers);
|
|
275 if (h == NULL) {
|
|
276 return NGX_ERROR;
|
|
277 }
|
|
278
|
|
279 *h = ngx_http_rewrite_handler;
|
|
280
|
58
|
281 h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
|
|
282 if (h == NULL) {
|
|
283 return NGX_ERROR;
|
|
284 }
|
|
285
|
|
286 *h = ngx_http_rewrite_handler;
|
|
287
|
|
288 return NGX_OK;
|
126
|
289 }
|
58
|
290
|
|
291
|
50
|
292 static char *
|
|
293 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
294 {
|
126
|
295 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
|
296
|
589
|
297 ngx_str_t *value;
|
58
|
298 ngx_uint_t last;
|
589
|
299 ngx_regex_compile_t rc;
|
58
|
300 ngx_http_script_code_pt *code;
|
|
301 ngx_http_script_compile_t sc;
|
|
302 ngx_http_script_regex_code_t *regex;
|
|
303 ngx_http_script_regex_end_code_t *regex_end;
|
|
304 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
50
|
305
|
58
|
306 regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
307 sizeof(ngx_http_script_regex_code_t));
|
50
|
308 if (regex == NULL) {
|
|
309 return NGX_CONF_ERROR;
|
|
310 }
|
|
311
|
58
|
312 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
|
|
313
|
50
|
314 value = cf->args->elts;
|
|
315
|
589
|
316 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
|
317
|
|
318 rc.pattern = value[1];
|
|
319 rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
320 rc.err.data = errstr;
|
50
|
321
|
|
322 /* TODO: NGX_REGEX_CASELESS */
|
|
323
|
589
|
324 regex->regex = ngx_http_regex_compile(cf, &rc);
|
50
|
325 if (regex->regex == NULL) {
|
|
326 return NGX_CONF_ERROR;
|
|
327 }
|
|
328
|
58
|
329 regex->code = ngx_http_script_regex_start_code;
|
50
|
330 regex->uri = 1;
|
|
331 regex->name = value[1];
|
|
332
|
58
|
333 if (value[2].data[value[2].len - 1] == '?') {
|
|
334
|
|
335 /* the last "?" drops the original arguments */
|
|
336 value[2].len--;
|
|
337
|
|
338 } else {
|
|
339 regex->add_args = 1;
|
|
340 }
|
|
341
|
50
|
342 last = 0;
|
|
343
|
615
|
344 if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0
|
|
345 || ngx_strncmp(value[2].data, "https://", sizeof("https://") - 1) == 0
|
|
346 || ngx_strncmp(value[2].data, "$scheme", sizeof("$scheme") - 1) == 0)
|
|
347 {
|
382
|
348 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
|
|
349 regex->redirect = 1;
|
|
350 last = 1;
|
|
351 }
|
|
352
|
50
|
353 if (cf->args->nelts == 4) {
|
|
354 if (ngx_strcmp(value[3].data, "last") == 0) {
|
|
355 last = 1;
|
|
356
|
|
357 } else if (ngx_strcmp(value[3].data, "break") == 0) {
|
|
358 regex->break_cycle = 1;
|
|
359 last = 1;
|
|
360
|
|
361 } else if (ngx_strcmp(value[3].data, "redirect") == 0) {
|
|
362 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
|
|
363 regex->redirect = 1;
|
|
364 last = 1;
|
|
365
|
|
366 } else if (ngx_strcmp(value[3].data, "permanent") == 0) {
|
|
367 regex->status = NGX_HTTP_MOVED_PERMANENTLY;
|
|
368 regex->redirect = 1;
|
|
369 last = 1;
|
|
370
|
|
371 } else {
|
|
372 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
373 "invalid parameter \"%V\"", &value[3]);
|
|
374 return NGX_CONF_ERROR;
|
|
375 }
|
|
376 }
|
|
377
|
58
|
378 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
50
|
379
|
58
|
380 sc.cf = cf;
|
|
381 sc.source = &value[2];
|
|
382 sc.lengths = ®ex->lengths;
|
|
383 sc.values = &lcf->codes;
|
|
384 sc.variables = ngx_http_script_variables_count(&value[2]);
|
|
385 sc.main = regex;
|
|
386 sc.complete_lengths = 1;
|
|
387 sc.compile_args = !regex->redirect;
|
50
|
388
|
58
|
389 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
390 return NGX_CONF_ERROR;
|
|
391 }
|
50
|
392
|
58
|
393 regex = sc.main;
|
50
|
394
|
58
|
395 regex->size = sc.size;
|
|
396 regex->args = sc.args;
|
50
|
397
|
190
|
398 if (sc.variables == 0 && !sc.dup_capture) {
|
58
|
399 regex->lengths = NULL;
|
50
|
400 }
|
|
401
|
58
|
402 regex_end = ngx_http_script_add_code(lcf->codes,
|
|
403 sizeof(ngx_http_script_regex_end_code_t),
|
|
404 ®ex);
|
50
|
405 if (regex_end == NULL) {
|
|
406 return NGX_CONF_ERROR;
|
|
407 }
|
|
408
|
58
|
409 regex_end->code = ngx_http_script_regex_end_code;
|
50
|
410 regex_end->uri = regex->uri;
|
|
411 regex_end->args = regex->args;
|
58
|
412 regex_end->add_args = regex->add_args;
|
50
|
413 regex_end->redirect = regex->redirect;
|
|
414
|
|
415 if (last) {
|
66
|
416 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex);
|
50
|
417 if (code == NULL) {
|
|
418 return NGX_CONF_ERROR;
|
|
419 }
|
|
420
|
575
|
421 *code = NULL;
|
50
|
422 }
|
|
423
|
|
424 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
|
|
425 - (u_char *) regex;
|
|
426
|
|
427 return NGX_CONF_OK;
|
|
428 }
|
|
429
|
|
430
|
|
431 static char *
|
|
432 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
433 {
|
126
|
434 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
50
|
435
|
58
|
436 ngx_str_t *value;
|
|
437 ngx_http_script_return_code_t *ret;
|
50
|
438
|
58
|
439 ret = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
440 sizeof(ngx_http_script_return_code_t));
|
50
|
441 if (ret == NULL) {
|
|
442 return NGX_CONF_ERROR;
|
|
443 }
|
|
444
|
|
445 value = cf->args->elts;
|
|
446
|
58
|
447 ret->code = ngx_http_script_return_code;
|
50
|
448 ret->null = (uintptr_t) NULL;
|
|
449
|
|
450 ret->status = ngx_atoi(value[1].data, value[1].len);
|
|
451
|
|
452 if (ret->status == (uintptr_t) NGX_ERROR) {
|
|
453 return NGX_CONF_ERROR;
|
|
454 }
|
|
455
|
|
456 return NGX_CONF_OK;
|
|
457 }
|
|
458
|
|
459
|
|
460 static char *
|
76
|
461 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
462 {
|
|
463 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
|
464
|
|
465 ngx_http_script_code_pt *code;
|
|
466
|
|
467 code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t));
|
|
468 if (code == NULL) {
|
|
469 return NGX_CONF_ERROR;
|
|
470 }
|
|
471
|
|
472 *code = ngx_http_script_break_code;
|
|
473
|
|
474 return NGX_CONF_OK;
|
|
475 }
|
|
476
|
|
477
|
|
478 static char *
|
50
|
479 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
480 {
|
126
|
481 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
50
|
482
|
|
483 void *mconf;
|
|
484 char *rv;
|
|
485 u_char *elts;
|
|
486 ngx_uint_t i;
|
|
487 ngx_conf_t save;
|
|
488 ngx_http_module_t *module;
|
|
489 ngx_http_conf_ctx_t *ctx, *pctx;
|
378
|
490 ngx_http_core_loc_conf_t *clcf, *pclcf;
|
58
|
491 ngx_http_script_if_code_t *if_code;
|
50
|
492 ngx_http_rewrite_loc_conf_t *nlcf;
|
|
493
|
|
494 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
495 if (ctx == NULL) {
|
|
496 return NGX_CONF_ERROR;
|
|
497 }
|
|
498
|
|
499 pctx = cf->ctx;
|
|
500 ctx->main_conf = pctx->main_conf;
|
126
|
501 ctx->srv_conf = pctx->srv_conf;
|
50
|
502
|
|
503 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
504 if (ctx->loc_conf == NULL) {
|
|
505 return NGX_CONF_ERROR;
|
|
506 }
|
|
507
|
|
508 for (i = 0; ngx_modules[i]; i++) {
|
|
509 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
|
|
510 continue;
|
|
511 }
|
|
512
|
|
513 module = ngx_modules[i]->ctx;
|
126
|
514
|
50
|
515 if (module->create_loc_conf) {
|
|
516
|
|
517 mconf = module->create_loc_conf(cf);
|
|
518 if (mconf == NULL) {
|
|
519 return NGX_CONF_ERROR;
|
|
520 }
|
|
521
|
|
522 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
|
|
523 }
|
|
524 }
|
|
525
|
|
526 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
527
|
|
528 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
529 clcf->loc_conf = ctx->loc_conf;
|
|
530 clcf->name = pclcf->name;
|
|
531 clcf->noname = 1;
|
|
532
|
378
|
533 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
|
50
|
534 return NGX_CONF_ERROR;
|
|
535 }
|
|
536
|
|
537 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) {
|
|
538 return NGX_CONF_ERROR;
|
|
539 }
|
|
540
|
58
|
541 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t));
|
50
|
542 if (if_code == NULL) {
|
550
|
543 return NGX_CONF_ERROR;
|
50
|
544 }
|
|
545
|
58
|
546 if_code->code = ngx_http_script_if_code;
|
50
|
547
|
|
548 elts = lcf->codes->elts;
|
|
549
|
|
550
|
180
|
551 /* the inner directives must be compiled to the same code array */
|
50
|
552
|
|
553 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index];
|
|
554 nlcf->codes = lcf->codes;
|
|
555
|
|
556
|
|
557 save = *cf;
|
|
558 cf->ctx = ctx;
|
|
559
|
|
560 if (pclcf->name.len == 0) {
|
|
561 if_code->loc_conf = NULL;
|
|
562 cf->cmd_type = NGX_HTTP_SIF_CONF;
|
|
563
|
|
564 } else {
|
|
565 if_code->loc_conf = ctx->loc_conf;
|
|
566 cf->cmd_type = NGX_HTTP_LIF_CONF;
|
|
567 }
|
|
568
|
|
569 rv = ngx_conf_parse(cf, NULL);
|
|
570
|
|
571 *cf = save;
|
|
572
|
|
573 if (rv != NGX_CONF_OK) {
|
|
574 return rv;
|
|
575 }
|
|
576
|
|
577
|
|
578 if (elts != lcf->codes->elts) {
|
58
|
579 if_code = (ngx_http_script_if_code_t *)
|
50
|
580 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts));
|
|
581 }
|
|
582
|
|
583 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
|
|
584 - (u_char *) if_code;
|
|
585
|
104
|
586 /* the code array belong to parent block */
|
|
587
|
|
588 nlcf->codes = NULL;
|
|
589
|
50
|
590 return NGX_CONF_OK;
|
|
591 }
|
|
592
|
|
593
|
|
594 static char *
|
|
595 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf)
|
|
596 {
|
180
|
597 u_char *p;
|
|
598 size_t len;
|
589
|
599 ngx_str_t *value;
|
|
600 ngx_uint_t cur, last;
|
|
601 ngx_regex_compile_t rc;
|
126
|
602 ngx_http_script_code_pt *code;
|
180
|
603 ngx_http_script_file_code_t *fop;
|
58
|
604 ngx_http_script_regex_code_t *regex;
|
|
605 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
50
|
606
|
|
607 value = cf->args->elts;
|
|
608 last = cf->args->nelts - 1;
|
|
609
|
|
610 if (value[1].len < 1 || value[1].data[0] != '(') {
|
|
611 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
612 "invalid condition \"%V\"", &value[1]);
|
|
613 return NGX_CONF_ERROR;
|
|
614 }
|
|
615
|
|
616 if (value[1].len == 1) {
|
|
617 cur = 2;
|
|
618
|
|
619 } else {
|
|
620 cur = 1;
|
|
621 value[1].len--;
|
|
622 value[1].data++;
|
|
623 }
|
|
624
|
|
625 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') {
|
|
626 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
627 "invalid condition \"%V\"", &value[last]);
|
|
628 return NGX_CONF_ERROR;
|
|
629 }
|
|
630
|
|
631 if (value[last].len == 1) {
|
|
632 last--;
|
|
633
|
|
634 } else {
|
|
635 value[last].len--;
|
|
636 value[last].data[value[last].len] = '\0';
|
|
637 }
|
|
638
|
180
|
639 len = value[cur].len;
|
|
640 p = value[cur].data;
|
|
641
|
|
642 if (len > 1 && p[0] == '$') {
|
50
|
643
|
|
644 if (cur != last && cur + 2 != last) {
|
|
645 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
180
|
646 "invalid condition \"%V\"", &value[cur]);
|
50
|
647 return NGX_CONF_ERROR;
|
|
648 }
|
|
649
|
126
|
650 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) {
|
50
|
651 return NGX_CONF_ERROR;
|
|
652 }
|
|
653
|
|
654 if (cur == last) {
|
|
655 return NGX_CONF_OK;
|
|
656 }
|
|
657
|
|
658 cur++;
|
|
659
|
180
|
660 len = value[cur].len;
|
|
661 p = value[cur].data;
|
|
662
|
|
663 if (len == 1 && p[0] == '=') {
|
126
|
664
|
|
665 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
|
|
666 return NGX_CONF_ERROR;
|
|
667 }
|
|
668
|
|
669 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
670 sizeof(uintptr_t));
|
|
671 if (code == NULL) {
|
|
672 return NGX_CONF_ERROR;
|
|
673 }
|
|
674
|
|
675 *code = ngx_http_script_equal_code;
|
|
676
|
|
677 return NGX_CONF_OK;
|
50
|
678 }
|
|
679
|
180
|
680 if (len == 2 && p[0] == '!' && p[1] == '=') {
|
|
681
|
126
|
682 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
|
|
683 return NGX_CONF_ERROR;
|
|
684 }
|
|
685
|
|
686 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
687 sizeof(uintptr_t));
|
|
688 if (code == NULL) {
|
|
689 return NGX_CONF_ERROR;
|
|
690 }
|
|
691
|
|
692 *code = ngx_http_script_not_equal_code;
|
|
693 return NGX_CONF_OK;
|
50
|
694 }
|
|
695
|
180
|
696 if ((len == 1 && p[0] == '~')
|
|
697 || (len == 2 && p[0] == '~' && p[1] == '*')
|
|
698 || (len == 2 && p[0] == '!' && p[1] == '~')
|
|
699 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*'))
|
126
|
700 {
|
|
701 regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
702 sizeof(ngx_http_script_regex_code_t));
|
|
703 if (regex == NULL) {
|
|
704 return NGX_CONF_ERROR;
|
|
705 }
|
58
|
706
|
126
|
707 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
|
|
708
|
589
|
709 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
126
|
710
|
589
|
711 rc.pattern = value[last];
|
|
712 rc.options = (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0;
|
|
713 rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
714 rc.err.data = errstr;
|
50
|
715
|
589
|
716 regex->regex = ngx_http_regex_compile(cf, &rc);
|
126
|
717 if (regex->regex == NULL) {
|
|
718 return NGX_CONF_ERROR;
|
|
719 }
|
|
720
|
|
721 regex->code = ngx_http_script_regex_start_code;
|
|
722 regex->next = sizeof(ngx_http_script_regex_code_t);
|
|
723 regex->test = 1;
|
180
|
724 if (p[0] == '!') {
|
|
725 regex->negative_test = 1;
|
|
726 }
|
126
|
727 regex->name = value[last];
|
50
|
728
|
126
|
729 return NGX_CONF_OK;
|
50
|
730 }
|
|
731
|
126
|
732 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
733 "unexpected \"%V\" in condition", &value[cur]);
|
|
734 return NGX_CONF_ERROR;
|
180
|
735
|
|
736 } else if ((len == 2 && p[0] == '-')
|
|
737 || (len == 3 && p[0] == '!' && p[1] == '-'))
|
|
738 {
|
|
739 if (cur + 1 != last) {
|
|
740 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
741 "invalid condition \"%V\"", &value[cur]);
|
|
742 return NGX_CONF_ERROR;
|
|
743 }
|
|
744
|
|
745 value[last].data[value[last].len] = '\0';
|
|
746 value[last].len++;
|
|
747
|
|
748 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
|
|
749 return NGX_CONF_ERROR;
|
|
750 }
|
|
751
|
|
752 fop = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
753 sizeof(ngx_http_script_file_code_t));
|
|
754 if (fop == NULL) {
|
|
755 return NGX_CONF_ERROR;
|
|
756 }
|
|
757
|
|
758 fop->code = ngx_http_script_file_code;
|
|
759
|
|
760 if (p[1] == 'f') {
|
|
761 fop->op = ngx_http_script_file_plain;
|
|
762 return NGX_CONF_OK;
|
|
763 }
|
|
764
|
218
|
765 if (p[1] == 'd') {
|
|
766 fop->op = ngx_http_script_file_dir;
|
|
767 return NGX_CONF_OK;
|
|
768 }
|
|
769
|
|
770 if (p[1] == 'e') {
|
|
771 fop->op = ngx_http_script_file_exists;
|
|
772 return NGX_CONF_OK;
|
|
773 }
|
|
774
|
|
775 if (p[1] == 'x') {
|
|
776 fop->op = ngx_http_script_file_exec;
|
|
777 return NGX_CONF_OK;
|
|
778 }
|
|
779
|
180
|
780 if (p[0] == '!') {
|
|
781 if (p[2] == 'f') {
|
|
782 fop->op = ngx_http_script_file_not_plain;
|
|
783 return NGX_CONF_OK;
|
|
784 }
|
218
|
785
|
|
786 if (p[2] == 'd') {
|
|
787 fop->op = ngx_http_script_file_not_dir;
|
|
788 return NGX_CONF_OK;
|
|
789 }
|
|
790
|
|
791 if (p[2] == 'e') {
|
|
792 fop->op = ngx_http_script_file_not_exists;
|
|
793 return NGX_CONF_OK;
|
|
794 }
|
|
795
|
|
796 if (p[2] == 'x') {
|
|
797 fop->op = ngx_http_script_file_not_exec;
|
|
798 return NGX_CONF_OK;
|
|
799 }
|
180
|
800 }
|
|
801
|
|
802 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
803 "invalid condition \"%V\"", &value[cur]);
|
|
804 return NGX_CONF_ERROR;
|
50
|
805 }
|
|
806
|
|
807 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
808 "invalid condition \"%V\"", &value[cur]);
|
|
809
|
|
810 return NGX_CONF_ERROR;
|
|
811 }
|
|
812
|
|
813
|
|
814 static char *
|
|
815 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
|
|
816 ngx_str_t *value)
|
|
817 {
|
58
|
818 ngx_int_t index;
|
|
819 ngx_http_script_var_code_t *var_code;
|
50
|
820
|
|
821 value->len--;
|
|
822 value->data++;
|
|
823
|
126
|
824 index = ngx_http_get_variable_index(cf, value);
|
50
|
825
|
126
|
826 if (index == NGX_ERROR) {
|
|
827 return NGX_CONF_ERROR;
|
50
|
828 }
|
|
829
|
126
|
830 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
831 sizeof(ngx_http_script_var_code_t));
|
|
832 if (var_code == NULL) {
|
|
833 return NGX_CONF_ERROR;
|
50
|
834 }
|
|
835
|
126
|
836 var_code->code = ngx_http_script_var_code;
|
|
837 var_code->index = index;
|
50
|
838
|
|
839 return NGX_CONF_OK;
|
|
840 }
|
|
841
|
|
842
|
|
843 static char *
|
|
844 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
845 {
|
126
|
846 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
50
|
847
|
186
|
848 ngx_int_t index;
|
|
849 ngx_str_t *value;
|
|
850 ngx_http_variable_t *v;
|
|
851 ngx_http_script_var_code_t *vcode;
|
|
852 ngx_http_script_var_handler_code_t *vhcode;
|
50
|
853
|
|
854 value = cf->args->elts;
|
|
855
|
|
856 if (value[1].data[0] != '$') {
|
|
857 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
858 "invalid variable name \"%V\"", &value[1]);
|
|
859 return NGX_CONF_ERROR;
|
|
860 }
|
|
861
|
|
862 value[1].len--;
|
|
863 value[1].data++;
|
|
864
|
340
|
865 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
|
50
|
866 if (v == NULL) {
|
|
867 return NGX_CONF_ERROR;
|
|
868 }
|
|
869
|
58
|
870 index = ngx_http_get_variable_index(cf, &value[1]);
|
|
871 if (index == NGX_ERROR) {
|
|
872 return NGX_CONF_ERROR;
|
|
873 }
|
50
|
874
|
300
|
875 if (v->get_handler == NULL
|
|
876 && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0
|
|
877 && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0
|
|
878 && ngx_strncasecmp(value[1].data, (u_char *) "upstream_http_", 14) != 0)
|
|
879 {
|
186
|
880 v->get_handler = ngx_http_rewrite_var;
|
104
|
881 v->data = index;
|
|
882 }
|
58
|
883
|
126
|
884 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) {
|
|
885 return NGX_CONF_ERROR;
|
64
|
886 }
|
50
|
887
|
186
|
888 if (v->set_handler) {
|
|
889 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
890 sizeof(ngx_http_script_var_handler_code_t));
|
|
891 if (vhcode == NULL) {
|
|
892 return NGX_CONF_ERROR;
|
|
893 }
|
|
894
|
|
895 vhcode->code = ngx_http_script_var_set_handler_code;
|
|
896 vhcode->handler = v->set_handler;
|
|
897 vhcode->data = v->data;
|
|
898
|
|
899 return NGX_CONF_OK;
|
|
900 }
|
|
901
|
|
902 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
903 sizeof(ngx_http_script_var_code_t));
|
|
904 if (vcode == NULL) {
|
50
|
905 return NGX_CONF_ERROR;
|
|
906 }
|
|
907
|
186
|
908 vcode->code = ngx_http_script_set_var_code;
|
|
909 vcode->index = (uintptr_t) index;
|
50
|
910
|
|
911 return NGX_CONF_OK;
|
|
912 }
|
126
|
913
|
|
914
|
|
915 static char *
|
|
916 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
|
|
917 ngx_str_t *value)
|
|
918 {
|
|
919 ngx_int_t n;
|
|
920 ngx_http_script_compile_t sc;
|
|
921 ngx_http_script_value_code_t *val;
|
|
922 ngx_http_script_complex_value_code_t *complex;
|
|
923
|
|
924 n = ngx_http_script_variables_count(value);
|
|
925
|
|
926 if (n == 0) {
|
|
927 val = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
928 sizeof(ngx_http_script_value_code_t));
|
|
929 if (val == NULL) {
|
|
930 return NGX_CONF_ERROR;
|
|
931 }
|
|
932
|
|
933 n = ngx_atoi(value->data, value->len);
|
|
934
|
|
935 if (n == NGX_ERROR) {
|
|
936 n = 0;
|
|
937 }
|
|
938
|
|
939 val->code = ngx_http_script_value_code;
|
|
940 val->value = (uintptr_t) n;
|
|
941 val->text_len = (uintptr_t) value->len;
|
|
942 val->text_data = (uintptr_t) value->data;
|
|
943
|
|
944 return NGX_CONF_OK;
|
|
945 }
|
|
946
|
|
947 complex = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
948 sizeof(ngx_http_script_complex_value_code_t));
|
|
949 if (complex == NULL) {
|
|
950 return NGX_CONF_ERROR;
|
|
951 }
|
|
952
|
|
953 complex->code = ngx_http_script_complex_value_code;
|
|
954 complex->lengths = NULL;
|
|
955
|
|
956 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
957
|
|
958 sc.cf = cf;
|
|
959 sc.source = value;
|
|
960 sc.lengths = &complex->lengths;
|
|
961 sc.values = &lcf->codes;
|
|
962 sc.variables = n;
|
|
963 sc.complete_lengths = 1;
|
|
964
|
|
965 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
966 return NGX_CONF_ERROR;
|
|
967 }
|
|
968
|
|
969 return NGX_CONF_OK;
|
|
970 }
|