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) {
|
505
|
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
|
558
|
297 ngx_str_t *value;
|
58
|
298 ngx_uint_t last;
|
558
|
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
|
558
|
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
|
558
|
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
|
|
344 if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0) {
|
|
345 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
|
|
346 regex->redirect = 1;
|
|
347 last = 1;
|
|
348 }
|
|
349
|
382
|
350 if (ngx_strncmp(value[2].data, "https://", sizeof("https://") - 1) == 0) {
|
|
351 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
|
|
352 regex->redirect = 1;
|
|
353 last = 1;
|
|
354 }
|
|
355
|
50
|
356 if (cf->args->nelts == 4) {
|
|
357 if (ngx_strcmp(value[3].data, "last") == 0) {
|
|
358 last = 1;
|
|
359
|
|
360 } else if (ngx_strcmp(value[3].data, "break") == 0) {
|
|
361 regex->break_cycle = 1;
|
|
362 last = 1;
|
|
363
|
|
364 } else if (ngx_strcmp(value[3].data, "redirect") == 0) {
|
|
365 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
|
|
366 regex->redirect = 1;
|
|
367 last = 1;
|
|
368
|
|
369 } else if (ngx_strcmp(value[3].data, "permanent") == 0) {
|
|
370 regex->status = NGX_HTTP_MOVED_PERMANENTLY;
|
|
371 regex->redirect = 1;
|
|
372 last = 1;
|
|
373
|
|
374 } else {
|
|
375 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
376 "invalid parameter \"%V\"", &value[3]);
|
|
377 return NGX_CONF_ERROR;
|
|
378 }
|
|
379 }
|
|
380
|
58
|
381 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
50
|
382
|
58
|
383 sc.cf = cf;
|
|
384 sc.source = &value[2];
|
|
385 sc.lengths = ®ex->lengths;
|
|
386 sc.values = &lcf->codes;
|
|
387 sc.variables = ngx_http_script_variables_count(&value[2]);
|
|
388 sc.main = regex;
|
|
389 sc.complete_lengths = 1;
|
|
390 sc.compile_args = !regex->redirect;
|
50
|
391
|
58
|
392 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
393 return NGX_CONF_ERROR;
|
|
394 }
|
50
|
395
|
58
|
396 regex = sc.main;
|
50
|
397
|
58
|
398 regex->size = sc.size;
|
|
399 regex->args = sc.args;
|
50
|
400
|
190
|
401 if (sc.variables == 0 && !sc.dup_capture) {
|
58
|
402 regex->lengths = NULL;
|
50
|
403 }
|
|
404
|
58
|
405 regex_end = ngx_http_script_add_code(lcf->codes,
|
|
406 sizeof(ngx_http_script_regex_end_code_t),
|
|
407 ®ex);
|
50
|
408 if (regex_end == NULL) {
|
|
409 return NGX_CONF_ERROR;
|
|
410 }
|
|
411
|
58
|
412 regex_end->code = ngx_http_script_regex_end_code;
|
50
|
413 regex_end->uri = regex->uri;
|
|
414 regex_end->args = regex->args;
|
58
|
415 regex_end->add_args = regex->add_args;
|
50
|
416 regex_end->redirect = regex->redirect;
|
|
417
|
|
418 if (last) {
|
66
|
419 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex);
|
50
|
420 if (code == NULL) {
|
|
421 return NGX_CONF_ERROR;
|
|
422 }
|
|
423
|
544
|
424 *code = NULL;
|
50
|
425 }
|
|
426
|
|
427 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
|
|
428 - (u_char *) regex;
|
|
429
|
|
430 return NGX_CONF_OK;
|
|
431 }
|
|
432
|
|
433
|
|
434 static char *
|
|
435 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
436 {
|
126
|
437 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
50
|
438
|
58
|
439 ngx_str_t *value;
|
|
440 ngx_http_script_return_code_t *ret;
|
50
|
441
|
58
|
442 ret = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
443 sizeof(ngx_http_script_return_code_t));
|
50
|
444 if (ret == NULL) {
|
|
445 return NGX_CONF_ERROR;
|
|
446 }
|
|
447
|
|
448 value = cf->args->elts;
|
|
449
|
58
|
450 ret->code = ngx_http_script_return_code;
|
50
|
451 ret->null = (uintptr_t) NULL;
|
|
452
|
|
453 ret->status = ngx_atoi(value[1].data, value[1].len);
|
|
454
|
|
455 if (ret->status == (uintptr_t) NGX_ERROR) {
|
|
456 return NGX_CONF_ERROR;
|
|
457 }
|
|
458
|
|
459 return NGX_CONF_OK;
|
|
460 }
|
|
461
|
|
462
|
|
463 static char *
|
76
|
464 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
465 {
|
|
466 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
|
467
|
|
468 ngx_http_script_code_pt *code;
|
|
469
|
|
470 code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t));
|
|
471 if (code == NULL) {
|
|
472 return NGX_CONF_ERROR;
|
|
473 }
|
|
474
|
|
475 *code = ngx_http_script_break_code;
|
|
476
|
|
477 return NGX_CONF_OK;
|
|
478 }
|
|
479
|
|
480
|
|
481 static char *
|
50
|
482 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
483 {
|
126
|
484 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
50
|
485
|
|
486 void *mconf;
|
|
487 char *rv;
|
|
488 u_char *elts;
|
|
489 ngx_uint_t i;
|
|
490 ngx_conf_t save;
|
|
491 ngx_http_module_t *module;
|
|
492 ngx_http_conf_ctx_t *ctx, *pctx;
|
378
|
493 ngx_http_core_loc_conf_t *clcf, *pclcf;
|
58
|
494 ngx_http_script_if_code_t *if_code;
|
50
|
495 ngx_http_rewrite_loc_conf_t *nlcf;
|
|
496
|
|
497 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
498 if (ctx == NULL) {
|
|
499 return NGX_CONF_ERROR;
|
|
500 }
|
|
501
|
|
502 pctx = cf->ctx;
|
|
503 ctx->main_conf = pctx->main_conf;
|
126
|
504 ctx->srv_conf = pctx->srv_conf;
|
50
|
505
|
|
506 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
507 if (ctx->loc_conf == NULL) {
|
|
508 return NGX_CONF_ERROR;
|
|
509 }
|
|
510
|
|
511 for (i = 0; ngx_modules[i]; i++) {
|
|
512 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
|
|
513 continue;
|
|
514 }
|
|
515
|
|
516 module = ngx_modules[i]->ctx;
|
126
|
517
|
50
|
518 if (module->create_loc_conf) {
|
|
519
|
|
520 mconf = module->create_loc_conf(cf);
|
|
521 if (mconf == NULL) {
|
|
522 return NGX_CONF_ERROR;
|
|
523 }
|
|
524
|
|
525 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
|
|
526 }
|
|
527 }
|
|
528
|
|
529 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
530
|
|
531 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
532 clcf->loc_conf = ctx->loc_conf;
|
|
533 clcf->name = pclcf->name;
|
|
534 clcf->noname = 1;
|
|
535
|
378
|
536 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
|
50
|
537 return NGX_CONF_ERROR;
|
|
538 }
|
|
539
|
|
540 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) {
|
|
541 return NGX_CONF_ERROR;
|
|
542 }
|
|
543
|
58
|
544 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t));
|
50
|
545 if (if_code == NULL) {
|
520
|
546 return NGX_CONF_ERROR;
|
50
|
547 }
|
|
548
|
58
|
549 if_code->code = ngx_http_script_if_code;
|
50
|
550
|
|
551 elts = lcf->codes->elts;
|
|
552
|
|
553
|
180
|
554 /* the inner directives must be compiled to the same code array */
|
50
|
555
|
|
556 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index];
|
|
557 nlcf->codes = lcf->codes;
|
|
558
|
|
559
|
|
560 save = *cf;
|
|
561 cf->ctx = ctx;
|
|
562
|
|
563 if (pclcf->name.len == 0) {
|
|
564 if_code->loc_conf = NULL;
|
|
565 cf->cmd_type = NGX_HTTP_SIF_CONF;
|
|
566
|
|
567 } else {
|
|
568 if_code->loc_conf = ctx->loc_conf;
|
|
569 cf->cmd_type = NGX_HTTP_LIF_CONF;
|
|
570 }
|
|
571
|
|
572 rv = ngx_conf_parse(cf, NULL);
|
|
573
|
|
574 *cf = save;
|
|
575
|
|
576 if (rv != NGX_CONF_OK) {
|
|
577 return rv;
|
|
578 }
|
|
579
|
|
580
|
|
581 if (elts != lcf->codes->elts) {
|
58
|
582 if_code = (ngx_http_script_if_code_t *)
|
50
|
583 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts));
|
|
584 }
|
|
585
|
|
586 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
|
|
587 - (u_char *) if_code;
|
|
588
|
104
|
589 /* the code array belong to parent block */
|
|
590
|
|
591 nlcf->codes = NULL;
|
|
592
|
50
|
593 return NGX_CONF_OK;
|
|
594 }
|
|
595
|
|
596
|
|
597 static char *
|
|
598 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf)
|
|
599 {
|
180
|
600 u_char *p;
|
|
601 size_t len;
|
558
|
602 ngx_str_t *value;
|
|
603 ngx_uint_t cur, last;
|
|
604 ngx_regex_compile_t rc;
|
126
|
605 ngx_http_script_code_pt *code;
|
180
|
606 ngx_http_script_file_code_t *fop;
|
58
|
607 ngx_http_script_regex_code_t *regex;
|
|
608 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
50
|
609
|
|
610 value = cf->args->elts;
|
|
611 last = cf->args->nelts - 1;
|
|
612
|
|
613 if (value[1].len < 1 || value[1].data[0] != '(') {
|
|
614 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
615 "invalid condition \"%V\"", &value[1]);
|
|
616 return NGX_CONF_ERROR;
|
|
617 }
|
|
618
|
|
619 if (value[1].len == 1) {
|
|
620 cur = 2;
|
|
621
|
|
622 } else {
|
|
623 cur = 1;
|
|
624 value[1].len--;
|
|
625 value[1].data++;
|
|
626 }
|
|
627
|
|
628 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') {
|
|
629 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
630 "invalid condition \"%V\"", &value[last]);
|
|
631 return NGX_CONF_ERROR;
|
|
632 }
|
|
633
|
|
634 if (value[last].len == 1) {
|
|
635 last--;
|
|
636
|
|
637 } else {
|
|
638 value[last].len--;
|
|
639 value[last].data[value[last].len] = '\0';
|
|
640 }
|
|
641
|
180
|
642 len = value[cur].len;
|
|
643 p = value[cur].data;
|
|
644
|
|
645 if (len > 1 && p[0] == '$') {
|
50
|
646
|
|
647 if (cur != last && cur + 2 != last) {
|
|
648 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
180
|
649 "invalid condition \"%V\"", &value[cur]);
|
50
|
650 return NGX_CONF_ERROR;
|
|
651 }
|
|
652
|
126
|
653 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) {
|
50
|
654 return NGX_CONF_ERROR;
|
|
655 }
|
|
656
|
|
657 if (cur == last) {
|
|
658 return NGX_CONF_OK;
|
|
659 }
|
|
660
|
|
661 cur++;
|
|
662
|
180
|
663 len = value[cur].len;
|
|
664 p = value[cur].data;
|
|
665
|
|
666 if (len == 1 && p[0] == '=') {
|
126
|
667
|
|
668 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
|
|
669 return NGX_CONF_ERROR;
|
|
670 }
|
|
671
|
|
672 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
673 sizeof(uintptr_t));
|
|
674 if (code == NULL) {
|
|
675 return NGX_CONF_ERROR;
|
|
676 }
|
|
677
|
|
678 *code = ngx_http_script_equal_code;
|
|
679
|
|
680 return NGX_CONF_OK;
|
50
|
681 }
|
|
682
|
180
|
683 if (len == 2 && p[0] == '!' && p[1] == '=') {
|
|
684
|
126
|
685 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
|
|
686 return NGX_CONF_ERROR;
|
|
687 }
|
|
688
|
|
689 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
690 sizeof(uintptr_t));
|
|
691 if (code == NULL) {
|
|
692 return NGX_CONF_ERROR;
|
|
693 }
|
|
694
|
|
695 *code = ngx_http_script_not_equal_code;
|
|
696 return NGX_CONF_OK;
|
50
|
697 }
|
|
698
|
180
|
699 if ((len == 1 && p[0] == '~')
|
|
700 || (len == 2 && p[0] == '~' && p[1] == '*')
|
|
701 || (len == 2 && p[0] == '!' && p[1] == '~')
|
|
702 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*'))
|
126
|
703 {
|
|
704 regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
705 sizeof(ngx_http_script_regex_code_t));
|
|
706 if (regex == NULL) {
|
|
707 return NGX_CONF_ERROR;
|
|
708 }
|
58
|
709
|
126
|
710 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
|
|
711
|
558
|
712 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
126
|
713
|
558
|
714 rc.pattern = value[last];
|
|
715 rc.options = (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0;
|
|
716 rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
717 rc.err.data = errstr;
|
50
|
718
|
558
|
719 regex->regex = ngx_http_regex_compile(cf, &rc);
|
126
|
720 if (regex->regex == NULL) {
|
|
721 return NGX_CONF_ERROR;
|
|
722 }
|
|
723
|
|
724 regex->code = ngx_http_script_regex_start_code;
|
|
725 regex->next = sizeof(ngx_http_script_regex_code_t);
|
|
726 regex->test = 1;
|
180
|
727 if (p[0] == '!') {
|
|
728 regex->negative_test = 1;
|
|
729 }
|
126
|
730 regex->name = value[last];
|
50
|
731
|
126
|
732 return NGX_CONF_OK;
|
50
|
733 }
|
|
734
|
126
|
735 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
736 "unexpected \"%V\" in condition", &value[cur]);
|
|
737 return NGX_CONF_ERROR;
|
180
|
738
|
|
739 } else if ((len == 2 && p[0] == '-')
|
|
740 || (len == 3 && p[0] == '!' && p[1] == '-'))
|
|
741 {
|
|
742 if (cur + 1 != last) {
|
|
743 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
744 "invalid condition \"%V\"", &value[cur]);
|
|
745 return NGX_CONF_ERROR;
|
|
746 }
|
|
747
|
|
748 value[last].data[value[last].len] = '\0';
|
|
749 value[last].len++;
|
|
750
|
|
751 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
|
|
752 return NGX_CONF_ERROR;
|
|
753 }
|
|
754
|
|
755 fop = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
756 sizeof(ngx_http_script_file_code_t));
|
|
757 if (fop == NULL) {
|
|
758 return NGX_CONF_ERROR;
|
|
759 }
|
|
760
|
|
761 fop->code = ngx_http_script_file_code;
|
|
762
|
|
763 if (p[1] == 'f') {
|
|
764 fop->op = ngx_http_script_file_plain;
|
|
765 return NGX_CONF_OK;
|
|
766 }
|
|
767
|
218
|
768 if (p[1] == 'd') {
|
|
769 fop->op = ngx_http_script_file_dir;
|
|
770 return NGX_CONF_OK;
|
|
771 }
|
|
772
|
|
773 if (p[1] == 'e') {
|
|
774 fop->op = ngx_http_script_file_exists;
|
|
775 return NGX_CONF_OK;
|
|
776 }
|
|
777
|
|
778 if (p[1] == 'x') {
|
|
779 fop->op = ngx_http_script_file_exec;
|
|
780 return NGX_CONF_OK;
|
|
781 }
|
|
782
|
180
|
783 if (p[0] == '!') {
|
|
784 if (p[2] == 'f') {
|
|
785 fop->op = ngx_http_script_file_not_plain;
|
|
786 return NGX_CONF_OK;
|
|
787 }
|
218
|
788
|
|
789 if (p[2] == 'd') {
|
|
790 fop->op = ngx_http_script_file_not_dir;
|
|
791 return NGX_CONF_OK;
|
|
792 }
|
|
793
|
|
794 if (p[2] == 'e') {
|
|
795 fop->op = ngx_http_script_file_not_exists;
|
|
796 return NGX_CONF_OK;
|
|
797 }
|
|
798
|
|
799 if (p[2] == 'x') {
|
|
800 fop->op = ngx_http_script_file_not_exec;
|
|
801 return NGX_CONF_OK;
|
|
802 }
|
180
|
803 }
|
|
804
|
|
805 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
806 "invalid condition \"%V\"", &value[cur]);
|
|
807 return NGX_CONF_ERROR;
|
50
|
808 }
|
|
809
|
|
810 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
811 "invalid condition \"%V\"", &value[cur]);
|
|
812
|
|
813 return NGX_CONF_ERROR;
|
|
814 }
|
|
815
|
|
816
|
|
817 static char *
|
|
818 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
|
|
819 ngx_str_t *value)
|
|
820 {
|
58
|
821 ngx_int_t index;
|
|
822 ngx_http_script_var_code_t *var_code;
|
50
|
823
|
|
824 value->len--;
|
|
825 value->data++;
|
|
826
|
126
|
827 index = ngx_http_get_variable_index(cf, value);
|
50
|
828
|
126
|
829 if (index == NGX_ERROR) {
|
|
830 return NGX_CONF_ERROR;
|
50
|
831 }
|
|
832
|
126
|
833 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
834 sizeof(ngx_http_script_var_code_t));
|
|
835 if (var_code == NULL) {
|
|
836 return NGX_CONF_ERROR;
|
50
|
837 }
|
|
838
|
126
|
839 var_code->code = ngx_http_script_var_code;
|
|
840 var_code->index = index;
|
50
|
841
|
|
842 return NGX_CONF_OK;
|
|
843 }
|
|
844
|
|
845
|
|
846 static char *
|
|
847 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
848 {
|
126
|
849 ngx_http_rewrite_loc_conf_t *lcf = conf;
|
50
|
850
|
186
|
851 ngx_int_t index;
|
|
852 ngx_str_t *value;
|
|
853 ngx_http_variable_t *v;
|
|
854 ngx_http_script_var_code_t *vcode;
|
|
855 ngx_http_script_var_handler_code_t *vhcode;
|
50
|
856
|
|
857 value = cf->args->elts;
|
|
858
|
|
859 if (value[1].data[0] != '$') {
|
|
860 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
861 "invalid variable name \"%V\"", &value[1]);
|
|
862 return NGX_CONF_ERROR;
|
|
863 }
|
|
864
|
|
865 value[1].len--;
|
|
866 value[1].data++;
|
|
867
|
340
|
868 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
|
50
|
869 if (v == NULL) {
|
|
870 return NGX_CONF_ERROR;
|
|
871 }
|
|
872
|
58
|
873 index = ngx_http_get_variable_index(cf, &value[1]);
|
|
874 if (index == NGX_ERROR) {
|
|
875 return NGX_CONF_ERROR;
|
|
876 }
|
50
|
877
|
300
|
878 if (v->get_handler == NULL
|
|
879 && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0
|
|
880 && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0
|
|
881 && ngx_strncasecmp(value[1].data, (u_char *) "upstream_http_", 14) != 0)
|
|
882 {
|
186
|
883 v->get_handler = ngx_http_rewrite_var;
|
104
|
884 v->data = index;
|
|
885 }
|
58
|
886
|
126
|
887 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) {
|
|
888 return NGX_CONF_ERROR;
|
64
|
889 }
|
50
|
890
|
186
|
891 if (v->set_handler) {
|
|
892 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
893 sizeof(ngx_http_script_var_handler_code_t));
|
|
894 if (vhcode == NULL) {
|
|
895 return NGX_CONF_ERROR;
|
|
896 }
|
|
897
|
|
898 vhcode->code = ngx_http_script_var_set_handler_code;
|
|
899 vhcode->handler = v->set_handler;
|
|
900 vhcode->data = v->data;
|
|
901
|
|
902 return NGX_CONF_OK;
|
|
903 }
|
|
904
|
|
905 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
906 sizeof(ngx_http_script_var_code_t));
|
|
907 if (vcode == NULL) {
|
50
|
908 return NGX_CONF_ERROR;
|
|
909 }
|
|
910
|
186
|
911 vcode->code = ngx_http_script_set_var_code;
|
|
912 vcode->index = (uintptr_t) index;
|
50
|
913
|
|
914 return NGX_CONF_OK;
|
|
915 }
|
126
|
916
|
|
917
|
|
918 static char *
|
|
919 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
|
|
920 ngx_str_t *value)
|
|
921 {
|
|
922 ngx_int_t n;
|
|
923 ngx_http_script_compile_t sc;
|
|
924 ngx_http_script_value_code_t *val;
|
|
925 ngx_http_script_complex_value_code_t *complex;
|
|
926
|
|
927 n = ngx_http_script_variables_count(value);
|
|
928
|
|
929 if (n == 0) {
|
|
930 val = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
931 sizeof(ngx_http_script_value_code_t));
|
|
932 if (val == NULL) {
|
|
933 return NGX_CONF_ERROR;
|
|
934 }
|
|
935
|
|
936 n = ngx_atoi(value->data, value->len);
|
|
937
|
|
938 if (n == NGX_ERROR) {
|
|
939 n = 0;
|
|
940 }
|
|
941
|
|
942 val->code = ngx_http_script_value_code;
|
|
943 val->value = (uintptr_t) n;
|
|
944 val->text_len = (uintptr_t) value->len;
|
|
945 val->text_data = (uintptr_t) value->data;
|
|
946
|
|
947 return NGX_CONF_OK;
|
|
948 }
|
|
949
|
|
950 complex = ngx_http_script_start_code(cf->pool, &lcf->codes,
|
|
951 sizeof(ngx_http_script_complex_value_code_t));
|
|
952 if (complex == NULL) {
|
|
953 return NGX_CONF_ERROR;
|
|
954 }
|
|
955
|
|
956 complex->code = ngx_http_script_complex_value_code;
|
|
957 complex->lengths = NULL;
|
|
958
|
|
959 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
960
|
|
961 sc.cf = cf;
|
|
962 sc.source = value;
|
|
963 sc.lengths = &complex->lengths;
|
|
964 sc.values = &lcf->codes;
|
|
965 sc.variables = n;
|
|
966 sc.complete_lengths = 1;
|
|
967
|
|
968 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
969 return NGX_CONF_ERROR;
|
|
970 }
|
|
971
|
|
972 return NGX_CONF_OK;
|
|
973 }
|