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