comparison src/http/modules/ngx_http_rewrite_module.c @ 50:72eb30262aac NGINX_0_1_25

nginx 0.1.25 *) Bugfix: nginx did run on Linux parisc. *) Feature: nginx now does not start under FreeBSD if the sysctl kern.ipc.somaxconn value is too big. *) Bugfix: if a request was internally redirected by the ngx_http_index_module module to the ngx_http_proxy_module or ngx_http_fastcgi_module modules, then the index file was not closed after request completion. *) Feature: the "proxy_pass" can be used in location with regular expression. *) Feature: the ngx_http_rewrite_filter_module module supports the condition like "if ($HTTP_USER_AGENT ~ MSIE)". *) Bugfix: nginx started too slow if the large number of addresses and text values were used in the "geo" directive. *) Change: a variable name must be declared as "$name" in the "geo" directive. The previous variant without "$" is still supported, but will be removed soon. *) Feature: the "%{VARIABLE}v" logging parameter. *) Feature: the "set $name value" directive. *) Bugfix: gcc 4.0 compatibility. *) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author Igor Sysoev <http://sysoev.ru>
date Sat, 19 Mar 2005 00:00:00 +0300
parents
children bcb5fce0b038
comparison
equal deleted inserted replaced
49:93dabbc9efb9 50:72eb30262aac
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 ngx_http_rewrite_engine_s ngx_http_rewrite_engine_t;
13
14 typedef void (*ngx_http_rewrite_code_pt) (ngx_http_rewrite_engine_t *e);
15
16
17 typedef struct {
18 ngx_str_t name;
19 ngx_uint_t wildcard;
20 } ngx_http_rewrite_referer_t;
21
22
23 typedef struct {
24 ngx_str_t *name;
25 ngx_http_variable_value_t *value;
26 } ngx_http_rewrite_variable_t;
27
28
29 typedef struct {
30 ngx_array_t *codes; /* uintptr_t */
31 ngx_array_t *referers; /* ngx_http_rewrite_referer_t */
32
33 ngx_uint_t max_captures;
34 ngx_uint_t stack_size;
35
36 ngx_flag_t log;
37
38 ngx_flag_t no_referer;
39 } ngx_http_rewrite_loc_conf_t;
40
41
42 typedef struct {
43 ngx_http_rewrite_code_pt code;
44 ngx_regex_t *regex;
45 uintptr_t size;
46 uintptr_t ncaptures;
47 uintptr_t status;
48 uintptr_t next;
49
50 uintptr_t test:1;
51 uintptr_t uri:1;
52
53 /* add the r->args to the new arguments */
54 uintptr_t args:1;
55
56 uintptr_t redirect:1;
57 uintptr_t break_cycle:1;
58
59 ngx_str_t name;
60 } ngx_http_rewrite_regex_code_t;
61
62
63 typedef struct {
64 ngx_http_rewrite_code_pt code;
65
66 uintptr_t uri:1;
67
68 /* add the r->args to the new arguments */
69 uintptr_t args:1;
70
71 uintptr_t redirect:1;
72 } ngx_http_rewrite_regex_end_code_t;
73
74 typedef struct {
75 ngx_http_rewrite_code_pt code;
76 uintptr_t n;
77 } ngx_http_rewrite_copy_capture_code_t;
78
79
80 typedef struct {
81 ngx_http_rewrite_code_pt code;
82 uintptr_t len;
83 } ngx_http_rewrite_copy_code_t;
84
85
86 typedef struct {
87 ngx_http_rewrite_code_pt code;
88 uintptr_t status;
89 uintptr_t null;
90 } ngx_http_rewrite_return_code_t;
91
92
93 typedef struct {
94 ngx_http_rewrite_code_pt code;
95 uintptr_t next;
96 void **loc_conf;
97 } ngx_http_rewrite_if_code_t;
98
99
100 typedef struct {
101 ngx_http_rewrite_code_pt code;
102 uintptr_t value;
103 uintptr_t text_len;
104 uintptr_t text_data;
105 } ngx_http_rewrite_value_code_t;
106
107
108 typedef struct {
109 ngx_http_rewrite_code_pt code;
110 uintptr_t index;
111 } ngx_http_rewrite_var_code_t;
112
113
114 struct ngx_http_rewrite_engine_s {
115 u_char *ip;
116 ngx_http_variable_value_t *sp;
117
118 ngx_str_t buf;
119 ngx_str_t *line;
120
121 u_char *pos;
122
123 /* the start of the rewritten arguments */
124 u_char *args;
125
126 unsigned quote:1;
127
128 ngx_int_t status;
129
130 int *captures;
131
132 ngx_http_request_t *request;
133 ngx_http_rewrite_loc_conf_t *conf;
134 };
135
136
137 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle);
138 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf);
139 static char *ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf,
140 void *parent, void *child);
141 static char *ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
142 static char *ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd,
143 void *conf);
144 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd,
145 void *conf);
146 static char * ngx_http_rewrite_if_condition(ngx_conf_t *cf,
147 ngx_http_rewrite_loc_conf_t *lcf);
148 static char *ngx_http_rewrite_variable(ngx_conf_t *cf,
149 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
150 static char *ngx_http_rewrite_valid_referers(ngx_conf_t *cf,
151 ngx_command_t *cmd, void *conf);
152 static char * ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd,
153 void *conf);
154 static void *ngx_http_rewrite_start_code(ngx_pool_t *pool,
155 ngx_array_t **codes, size_t size);
156 static void *ngx_http_rewrite_add_code(ngx_array_t *codes, size_t size,
157 void *code);
158
159
160 static ngx_command_t ngx_http_rewrite_commands[] = {
161
162 { ngx_string("rewrite"),
163 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
164 |NGX_CONF_TAKE23,
165 ngx_http_rewrite,
166 NGX_HTTP_LOC_CONF_OFFSET,
167 0,
168 NULL },
169
170 { ngx_string("return"),
171 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
172 |NGX_CONF_TAKE1,
173 ngx_http_rewrite_return,
174 NGX_HTTP_LOC_CONF_OFFSET,
175 0,
176 NULL },
177
178 { ngx_string("if"),
179 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
180 ngx_http_rewrite_if,
181 NGX_HTTP_LOC_CONF_OFFSET,
182 0,
183 NULL },
184
185 { ngx_string("valid_referers"),
186 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
187 ngx_http_rewrite_valid_referers,
188 NGX_HTTP_LOC_CONF_OFFSET,
189 0,
190 NULL },
191
192 { ngx_string("set"),
193 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
194 |NGX_CONF_TAKE2,
195 ngx_http_rewrite_set,
196 NGX_HTTP_LOC_CONF_OFFSET,
197 0,
198 NULL },
199
200 { ngx_string("rewrite_log"),
201 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
202 |NGX_CONF_TAKE1,
203 ngx_conf_set_flag_slot,
204 NGX_HTTP_LOC_CONF_OFFSET,
205 offsetof(ngx_http_rewrite_loc_conf_t, log),
206 NULL },
207
208 ngx_null_command
209 };
210
211
212 ngx_http_module_t ngx_http_rewrite_module_ctx = {
213 NULL, /* pre conf */
214
215 NULL, /* create main configuration */
216 NULL, /* init main configuration */
217
218 NULL, /* create server configuration */
219 NULL, /* merge server configuration */
220
221 ngx_http_rewrite_create_loc_conf, /* create location configration */
222 ngx_http_rewrite_merge_loc_conf /* merge location configration */
223 };
224
225
226 ngx_module_t ngx_http_rewrite_module = {
227 NGX_MODULE,
228 &ngx_http_rewrite_module_ctx, /* module context */
229 ngx_http_rewrite_commands, /* module directives */
230 NGX_HTTP_MODULE, /* module type */
231 ngx_http_rewrite_init, /* init module */
232 NULL /* init process */
233 };
234
235
236 #define ngx_http_rewrite_exit (u_char *) &ngx_http_rewrite_exit_code
237
238 uintptr_t ngx_http_rewrite_exit_code = (uintptr_t) NULL;
239
240 static ngx_http_variable_value_t ngx_http_rewrite_null_value =
241 { 0, ngx_string("") };
242
243
244 static ngx_int_t
245 ngx_http_rewrite_handler(ngx_http_request_t *r)
246 {
247 ngx_http_rewrite_code_pt code;
248 ngx_http_rewrite_engine_t *e;
249 ngx_http_rewrite_loc_conf_t *cf;
250
251 cf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
252
253 if (cf->codes == NULL) {
254 return NGX_DECLINED;
255 }
256
257 e = ngx_palloc(r->pool, sizeof(ngx_http_rewrite_engine_t));
258 if (e == NULL) {
259 return NGX_HTTP_INTERNAL_SERVER_ERROR;
260 }
261
262 e->sp = ngx_palloc(r->pool,
263 cf->stack_size * sizeof(ngx_http_variable_value_t));
264 if (e->sp == NULL) {
265 return NGX_HTTP_INTERNAL_SERVER_ERROR;
266 }
267
268 if (cf->max_captures) {
269 e->captures = ngx_palloc(r->pool, cf->max_captures * sizeof(int));
270 if (e->captures == NULL) {
271 return NGX_HTTP_INTERNAL_SERVER_ERROR;
272 }
273
274 } else {
275 e->captures = NULL;
276 }
277
278 e->ip = cf->codes->elts;
279 e->buf.len = 0;
280 e->buf.data = NULL;
281 e->line = NULL;
282 e->pos = NULL;
283 e->args = NULL;
284 e->quote = 1;
285 e->status = NGX_DECLINED;
286 e->request = r;
287 e->conf = cf;
288
289 while (*(uintptr_t *) e->ip) {
290 code = *(ngx_http_rewrite_code_pt *) e->ip;
291 code(e);
292 }
293
294 return e->status;
295 }
296
297
298 static void
299 ngx_http_rewrite_regex_start_code(ngx_http_rewrite_engine_t *e)
300 {
301 ngx_int_t rc;
302 ngx_uint_t n;
303 ngx_http_request_t *r;
304 ngx_http_rewrite_regex_code_t *code;
305
306 code = (ngx_http_rewrite_regex_code_t *) e->ip;
307
308 r = e->request;
309
310 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
311 "http rewrite regex: \"%V\"", &code->name);
312
313 if (code->uri) {
314 e->line = &r->uri;
315 } else {
316 e->sp--;
317 e->line = &e->sp->text;
318 }
319
320 rc = ngx_regex_exec(code->regex, e->line, e->captures, code->ncaptures);
321
322 if (rc == NGX_REGEX_NO_MATCHED) {
323 if (e->conf->log) {
324 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
325 "\"%V\" does not match \"%V\"", &code->name, e->line);
326 }
327
328 if (code->test) {
329 e->sp->value = 0;
330 e->sp->text.len = 0;
331 e->sp->text.data = (u_char *) "";
332 e->sp++;
333
334 e->ip += sizeof(ngx_http_rewrite_regex_code_t);
335 return;
336 }
337
338 e->ip += code->next;
339 return;
340 }
341
342 if (rc < 0) {
343 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
344 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
345 rc, e->line, &code->name);
346
347 e->ip = ngx_http_rewrite_exit;
348 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
349 return;
350 }
351
352 if (e->conf->log) {
353 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
354 "\"%V\" matches \"%V\"", &code->name, e->line);
355 }
356
357 if (code->test) {
358 e->sp->value = 1;
359 e->sp->text.len = 1;
360 e->sp->text.data = (u_char *) "1";
361 e->sp++;
362
363 e->ip += sizeof(ngx_http_rewrite_regex_code_t);
364 return;
365 }
366
367 if (code->status) {
368 e->status = code->status;
369
370 if (!code->redirect) {
371 e->ip = ngx_http_rewrite_exit;
372 return;
373 }
374 }
375
376 e->buf.len = code->size;
377
378 if (code->uri) {
379 if (!code->break_cycle) {
380 r->uri_changed = 1;
381 }
382
383 if (rc && (r->quoted_uri || r->plus_in_uri)) {
384 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len,
385 NGX_ESCAPE_ARGS);
386 }
387 }
388
389 for (n = 1; n < (ngx_uint_t) rc; n++) {
390 e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n];
391 }
392
393 if (code->args && r->args.len) {
394 e->buf.len += r->args.len + 1;
395 }
396
397 e->buf.data = ngx_palloc(r->pool, e->buf.len);
398 if (e->buf.data == NULL) {
399 e->ip = ngx_http_rewrite_exit;
400 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
401 return;
402 }
403
404 e->quote = code->redirect;
405
406 e->pos = e->buf.data;
407
408 e->ip += sizeof(ngx_http_rewrite_regex_code_t);
409 }
410
411
412 static void
413 ngx_http_rewrite_regex_end_code(ngx_http_rewrite_engine_t *e)
414 {
415 ngx_http_request_t *r;
416 ngx_http_rewrite_regex_end_code_t *code;
417
418 code = (ngx_http_rewrite_regex_end_code_t *) e->ip;
419
420 r = e->request;
421
422 e->quote = 0;
423
424 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
425 "http rewrite regex end");
426
427 if (e->args) {
428 e->buf.len = e->args - e->buf.data;
429
430 if (code->args && r->args.len) {
431 *e->pos++ = '&';
432 e->pos = ngx_cpymem(e->pos, r->args.data, r->args.len);
433 }
434
435 r->args.len = e->pos - e->args;
436 r->args.data = e->args;
437
438 e->args = NULL;
439
440 } else {
441 if (code->args && r->args.len) {
442 *e->pos++ = '?';
443 e->pos = ngx_cpymem(e->pos, r->args.data, r->args.len);
444 }
445
446 e->buf.len = e->pos - e->buf.data;
447 }
448
449 if (!code->redirect) {
450 if (e->conf->log) {
451 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
452 "rewritten data: \"%V\", args: \"%V\"",
453 &e->buf, &r->args);
454 }
455
456 if (code->uri) {
457 r->uri = e->buf;
458
459 if (ngx_http_set_exten(r) != NGX_OK) {
460 e->ip = ngx_http_rewrite_exit;
461 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
462 return;
463 }
464 }
465
466 e->ip += sizeof(ngx_http_rewrite_regex_end_code_t);
467 return;
468 }
469
470 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
471 "rewritten redirect: \"%V\"", &e->buf);
472
473 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
474 if (r->headers_out.location == NULL) {
475 e->ip = ngx_http_rewrite_exit;
476 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
477 return;
478 }
479
480 if (e->buf.data[0] != '/') {
481 r->headers_out.location->key.len = sizeof("Location") - 1;
482 r->headers_out.location->key.data = (u_char *) "Location";
483 }
484
485 r->headers_out.location->value = e->buf;
486
487 e->ip += sizeof(ngx_http_rewrite_regex_end_code_t);
488 }
489
490
491 static void
492 ngx_http_rewrite_copy_capture_code(ngx_http_rewrite_engine_t *e)
493 {
494 ngx_http_rewrite_copy_capture_code_t *code;
495
496 code = (ngx_http_rewrite_copy_capture_code_t *) e->ip;
497
498 e->ip += sizeof(ngx_http_rewrite_copy_capture_code_t);
499
500 if ((e->args || e->quote)
501 && (e->request->quoted_uri || e->request->plus_in_uri))
502 {
503 e->pos = (u_char *) ngx_escape_uri(e->pos,
504 &e->line->data[e->captures[code->n]],
505 e->captures[code->n + 1] - e->captures[code->n],
506 NGX_ESCAPE_ARGS);
507 } else {
508 e->pos = ngx_cpymem(e->pos, &e->line->data[e->captures[code->n]],
509 e->captures[code->n + 1] - e->captures[code->n]);
510 }
511
512 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
513 "http rewrite capture: \"%V\"", &e->buf);
514 }
515
516
517 static void
518 ngx_http_rewrite_copy_code(ngx_http_rewrite_engine_t *e)
519 {
520 ngx_http_rewrite_copy_code_t *code;
521
522 code = (ngx_http_rewrite_copy_code_t *) e->ip;
523
524 e->pos = ngx_cpymem(e->pos, e->ip + sizeof(ngx_http_rewrite_copy_code_t),
525 code->len);
526
527 e->ip += sizeof(ngx_http_rewrite_copy_code_t)
528 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
529
530 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
531 "http rewrite copy: \"%V\"", &e->buf);
532 }
533
534
535 static void
536 ngx_http_rewrite_start_args_code(ngx_http_rewrite_engine_t *e)
537 {
538 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
539 "http rewrite args");
540
541 e->args = e->pos;
542 e->ip += sizeof(uintptr_t);
543 }
544
545
546 static void
547 ngx_http_rewrite_return_code(ngx_http_rewrite_engine_t *e)
548 {
549 ngx_http_rewrite_return_code_t *code;
550
551 code = (ngx_http_rewrite_return_code_t *) e->ip;
552
553 e->status = code->status;
554
555 e->ip += sizeof(ngx_http_rewrite_return_code_t) - sizeof(uintptr_t);
556 }
557
558
559 static void
560 ngx_http_rewrite_if_code(ngx_http_rewrite_engine_t *e)
561 {
562 ngx_http_rewrite_if_code_t *code;
563
564 code = (ngx_http_rewrite_if_code_t *) e->ip;
565
566 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
567 "http rewrite if");
568
569 e->sp--;
570
571 if (e->sp->value) {
572 if (code->loc_conf) {
573 e->request->loc_conf = code->loc_conf;
574 }
575
576 e->ip += sizeof(ngx_http_rewrite_if_code_t);
577 return;
578 }
579
580 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
581 "http rewrite if false");
582
583 e->ip += code->next;
584 }
585
586
587 static void
588 ngx_http_rewrite_value_code(ngx_http_rewrite_engine_t *e)
589 {
590 ngx_http_rewrite_value_code_t *code;
591
592 code = (ngx_http_rewrite_value_code_t *) e->ip;
593
594 e->ip += sizeof(ngx_http_rewrite_value_code_t);
595
596 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
597 "http rewrite value");
598
599 e->sp->value = (ngx_uint_t) code->value;
600 e->sp->text.len = (size_t) code->text_len;
601 e->sp->text.data = (u_char *) code->text_data;
602 e->sp++;
603 }
604
605
606 static void
607 ngx_http_rewrite_set_var_code(ngx_http_rewrite_engine_t *e)
608 {
609 ngx_http_request_t *r;
610 ngx_http_variable_value_t *value;
611 ngx_http_core_main_conf_t *cmcf;
612 ngx_http_rewrite_var_code_t *code;
613
614 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
615 "http rewrite set var");
616
617 code = (ngx_http_rewrite_var_code_t *) e->ip;
618
619 e->ip += sizeof(ngx_http_rewrite_var_code_t);
620
621 r = e->request;
622
623 if (r->variables == NULL) {
624 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
625
626 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
627 * sizeof(ngx_http_variable_value_t *));
628 if (r->variables == NULL) {
629 e->ip = ngx_http_rewrite_exit;
630 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
631 return;
632 }
633 }
634
635 value = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
636 if (value == NULL) {
637 e->ip = ngx_http_rewrite_exit;
638 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
639 return;
640 }
641
642 e->sp--;
643
644 *value = *e->sp;
645
646 r->variables[code->index] = value;
647 }
648
649
650 static void
651 ngx_http_rewrite_var_code(ngx_http_rewrite_engine_t *e)
652 {
653 ngx_http_variable_value_t *value;
654 ngx_http_rewrite_var_code_t *code;
655
656 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
657 "http rewrite var");
658
659 code = (ngx_http_rewrite_var_code_t *) e->ip;
660
661 e->ip += sizeof(ngx_http_rewrite_var_code_t);
662
663 value = ngx_http_get_indexed_variable(e->request, code->index);
664
665 if (value == NULL || value == NGX_HTTP_VARIABLE_NOT_FOUND) {
666 e->sp->value = 0;
667 e->sp->text.len = 0;
668 e->sp->text.data = (u_char *) "";
669 e->sp++;
670
671 return;
672 }
673
674 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
675 "http rewrite var: %ui, \"%V\"", value->value, &value->text);
676
677 *e->sp = *value;
678 e->sp++;
679 }
680
681
682 static void
683 ngx_http_rewrite_invalid_referer_code(ngx_http_rewrite_engine_t *e)
684 {
685 u_char *ref;
686 size_t len;
687 ngx_uint_t i, n;
688 ngx_http_request_t *r;
689 ngx_http_rewrite_referer_t *refs;
690 ngx_http_rewrite_loc_conf_t *cf;
691
692 r = e->request;
693
694 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
695 "http rewrite invalid referer");
696
697 cf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
698
699 e->ip += sizeof(uintptr_t);
700
701 if (cf->referers == NULL) {
702 e->sp->value = 0;
703 e->sp->text.len = 0;
704 e->sp->text.data = (u_char *) "";
705 e->sp++;
706
707 return;
708 }
709
710 if (r->headers_in.referer == NULL) {
711 if (cf->no_referer) {
712 e->sp->value = 0;
713 e->sp->text.len = 0;
714 e->sp->text.data = (u_char *) "";
715 e->sp++;
716
717 return;
718 } else {
719 e->sp->value = 1;
720 e->sp->text.len = 1;
721 e->sp->text.data = (u_char *) "1";
722 e->sp++;
723
724 return;
725 }
726 }
727
728 len = r->headers_in.referer->value.len;
729 ref = r->headers_in.referer->value.data;
730
731 if (len < sizeof("http://i.ru") - 1
732 || (ngx_strncasecmp(ref, "http://", 7) != 0))
733 {
734 e->sp->value = 1;
735 e->sp->text.len = 1;
736 e->sp->text.data = (u_char *) "1";
737 e->sp++;
738
739 return;
740 }
741
742 len -= 7;
743 ref += 7;
744
745 refs = cf->referers->elts;
746 for (i = 0; i < cf->referers->nelts; i++ ){
747
748 if (refs[i].name.len > len) {
749 continue;
750 }
751
752 if (refs[i].wildcard) {
753 for (n = 0; n < len; n++) {
754 if (ref[n] == '/' || ref[n] == ':') {
755 break;
756 }
757
758 if (ref[n] != '.') {
759 continue;
760 }
761
762 if (ngx_strncmp(&ref[n], refs[i].name.data,
763 refs[i].name.len) == 0)
764 {
765 e->sp->value = 0;
766 e->sp->text.len = 0;
767 e->sp->text.data = (u_char *) "";
768 e->sp++;
769
770 return;
771 }
772 }
773
774 } else {
775 if (ngx_strncasecmp(refs[i].name.data, ref, refs[i].name.len) == 0)
776 {
777 e->sp->value = 0;
778 e->sp->text.len = 0;
779 e->sp->text.data = (u_char *) "";
780 e->sp++;
781
782 return;
783 }
784 }
785 }
786
787 e->sp->value = 1;
788 e->sp->text.len = 1;
789 e->sp->text.data = (u_char *) "1";
790 e->sp++;
791 }
792
793
794 static void
795 ngx_http_rewrite_nop_code(ngx_http_rewrite_engine_t *e)
796 {
797 e->ip += sizeof(uintptr_t);
798 }
799
800
801 static ngx_http_variable_value_t *
802 ngx_http_rewrite_var(ngx_http_request_t *r, uintptr_t data)
803 {
804 ngx_http_variable_t *var;
805 ngx_http_core_main_conf_t *cmcf;
806
807 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
808
809 var = cmcf->variables.elts;
810
811 /*
812 * the ngx_http_rewrite_module sets variables directly in r->variables,
813 * and they should be handle by ngx_http_get_indexed_variable(),
814 * so the handler is called only if the variable is not initialized
815 */
816
817 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
818 "using uninitialized \"%V\" variable", &var[data].name);
819
820 return &ngx_http_rewrite_null_value;
821 }
822
823
824 static ngx_int_t
825 ngx_http_rewrite_init(ngx_cycle_t *cycle)
826 {
827 ngx_http_handler_pt *h;
828 ngx_http_core_main_conf_t *cmcf;
829
830 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
831
832 h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
833 if (h == NULL) {
834 return NGX_ERROR;
835 }
836
837 *h = ngx_http_rewrite_handler;
838
839 return NGX_OK;
840 }
841
842
843 static void *
844 ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
845 {
846 ngx_http_rewrite_loc_conf_t *conf;
847
848 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t));
849 if (conf == NULL) {
850 return NGX_CONF_ERROR;
851 }
852
853 conf->stack_size = NGX_CONF_UNSET_UINT;
854 conf->log = NGX_CONF_UNSET;
855 conf->no_referer = NGX_CONF_UNSET;
856
857 return conf;
858 }
859
860
861 static char *
862 ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
863 {
864 ngx_http_rewrite_loc_conf_t *prev = parent;
865 ngx_http_rewrite_loc_conf_t *conf = child;
866
867 uintptr_t *code, *last;
868 ngx_http_rewrite_regex_code_t *regex;
869
870 ngx_conf_merge_value(conf->log, prev->log, 0);
871 ngx_conf_merge_unsigned_value(conf->stack_size, prev->stack_size, 10);
872
873 if (conf->referers == NULL) {
874 conf->referers = prev->referers;
875 ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0);
876 }
877
878 if (conf->no_referer == NGX_CONF_UNSET) {
879 conf->no_referer = 0;
880 }
881
882 if (conf->codes == NULL) {
883 return NGX_CONF_OK;
884 }
885
886 if (conf->codes == prev->codes) {
887 return NGX_CONF_OK;
888 }
889
890 code = conf->codes->elts;
891 last = (uintptr_t *) ((u_char *) code + conf->codes->nelts);
892
893 while (code < last) {
894 if (*code == (uintptr_t) NULL) {
895 return NGX_CONF_OK;
896 }
897
898 if (*code == (uintptr_t) &ngx_http_rewrite_regex_start_code) {
899 regex = (ngx_http_rewrite_regex_code_t *) code;
900 if (conf->max_captures < regex->ncaptures) {
901 conf->max_captures = regex->ncaptures;
902 }
903 code = (uintptr_t *) ((u_char *) code + regex->next);
904 continue;
905 }
906
907 if (*code == (uintptr_t) &ngx_http_rewrite_if_code) {
908 code += sizeof(ngx_http_rewrite_if_code_t) / sizeof(uintptr_t);
909 continue;
910 }
911
912 if (*code == (uintptr_t) &ngx_http_rewrite_return_code) {
913 code += sizeof(ngx_http_rewrite_return_code_t) / sizeof(uintptr_t);
914 continue;
915 }
916
917 if (*code == (uintptr_t) &ngx_http_rewrite_set_var_code) {
918 code += sizeof(ngx_http_rewrite_var_code_t) / sizeof(uintptr_t);
919 continue;
920 }
921
922 if (*code == (uintptr_t) &ngx_http_rewrite_var_code) {
923 code += sizeof(ngx_http_rewrite_var_code_t) / sizeof(uintptr_t);
924 continue;
925 }
926
927 if (*code == (uintptr_t) &ngx_http_rewrite_value_code) {
928 code += sizeof(ngx_http_rewrite_value_code_t) / sizeof(uintptr_t);
929 continue;
930 }
931
932 if (*code == (uintptr_t) &ngx_http_rewrite_invalid_referer_code) {
933 code++;
934 continue;
935 }
936
937 if (*code == (uintptr_t) &ngx_http_rewrite_nop_code) {
938 code++;
939 continue;
940 }
941
942 #if (NGX_DEBUG)
943 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
944 "unknown rewrite code: %p", *code);
945 return NGX_CONF_ERROR;
946 #endif
947 }
948
949 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t));
950 if (code == NULL) {
951 return NGX_CONF_ERROR;
952 }
953
954 *code = (uintptr_t) NULL;
955
956 return NGX_CONF_OK;
957 }
958
959
960 static char *
961 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
962 {
963 ngx_http_rewrite_loc_conf_t *lcf = conf;
964
965 u_char *data;
966 size_t len, size;
967 ngx_str_t *value, err;
968 ngx_int_t n;
969 ngx_uint_t i, last;
970 ngx_http_rewrite_code_pt *code;
971 ngx_http_rewrite_copy_code_t *copy;
972 ngx_http_rewrite_regex_code_t *regex;
973 ngx_http_rewrite_regex_end_code_t *regex_end;
974 ngx_http_rewrite_copy_capture_code_t *copy_capture;
975 u_char errstr[NGX_MAX_CONF_ERRSTR];
976
977 regex = ngx_http_rewrite_start_code(cf->pool, &lcf->codes,
978 sizeof(ngx_http_rewrite_regex_code_t));
979 if (regex == NULL) {
980 return NGX_CONF_ERROR;
981 }
982
983 value = cf->args->elts;
984
985 err.len = NGX_MAX_CONF_ERRSTR;
986 err.data = errstr;
987
988 /* TODO: NGX_REGEX_CASELESS */
989
990 regex->regex = ngx_regex_compile(&value[1], 0, cf->pool, &err);
991
992 if (regex->regex == NULL) {
993 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
994 return NGX_CONF_ERROR;
995 }
996
997 regex->code = ngx_http_rewrite_regex_start_code;
998 regex->size = 0;
999 regex->ncaptures = 0;
1000 regex->status = 0;
1001 regex->test = 0;
1002 regex->uri = 1;
1003 regex->args = 1;
1004 regex->redirect = 0;
1005 regex->name = value[1];
1006
1007 last = 0;
1008
1009 if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0) {
1010 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
1011 regex->redirect = 1;
1012 last = 1;
1013 }
1014
1015 if (cf->args->nelts == 4) {
1016 if (ngx_strcmp(value[3].data, "last") == 0) {
1017 last = 1;
1018
1019 } else if (ngx_strcmp(value[3].data, "break") == 0) {
1020 regex->break_cycle = 1;
1021 last = 1;
1022
1023 } else if (ngx_strcmp(value[3].data, "redirect") == 0) {
1024 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
1025 regex->redirect = 1;
1026 last = 1;
1027
1028 } else if (ngx_strcmp(value[3].data, "permanent") == 0) {
1029 regex->status = NGX_HTTP_MOVED_PERMANENTLY;
1030 regex->redirect = 1;
1031 last = 1;
1032
1033 } else {
1034 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1035 "invalid parameter \"%V\"", &value[3]);
1036 return NGX_CONF_ERROR;
1037 }
1038 }
1039
1040 i = 0;
1041
1042 while (i < value[2].len) {
1043
1044 data = &value[2].data[i];
1045
1046 if (value[2].data[i] == '$' && i < value[2].len
1047 && value[2].data[i + 1] >= '1' && value[2].data[i + 1] <= '9')
1048 {
1049
1050 /* the "$1" - "$9" captures */
1051
1052 copy_capture = ngx_http_rewrite_add_code(lcf->codes,
1053 sizeof(ngx_http_rewrite_copy_capture_code_t),
1054 &regex);
1055 if (copy_capture == NULL) {
1056 return NGX_CONF_ERROR;
1057 }
1058
1059 i++;
1060
1061 copy_capture->code = ngx_http_rewrite_copy_capture_code;
1062 copy_capture->n = value[2].data[i] - '0';
1063
1064 if (regex->ncaptures < copy_capture->n) {
1065 regex->ncaptures = copy_capture->n;
1066 }
1067
1068 copy_capture->n *= 2;
1069
1070 i++;
1071
1072 continue;
1073 }
1074
1075 if (value[2].data[i] == '?') {
1076
1077 /* the arguments */
1078
1079 if (i == value[2].len - 1) {
1080 /* the last "?" drops the original arguments */
1081 regex->args = 0;
1082 break;
1083 }
1084
1085 if (!regex->redirect) {
1086 code = ngx_http_rewrite_add_code(lcf->codes, sizeof(uintptr_t),
1087 &regex);
1088 if (code == NULL) {
1089 return NGX_CONF_ERROR;
1090 }
1091
1092 *code = ngx_http_rewrite_start_args_code;
1093
1094 i++;
1095
1096 continue;
1097 }
1098 }
1099
1100 i++;
1101
1102 /* the substituion strings */
1103
1104 while (i < value[2].len && value[2].data[i] != '$') {
1105
1106 if (value[2].data[i] == '?') {
1107
1108 if (i == value[2].len - 1) {
1109 /*
1110 * the last "?" drops the original arguments,
1111 * and it should not be copied to a substituion
1112 */
1113 regex->args = 0;
1114 break;
1115 }
1116
1117 if (!regex->redirect) {
1118 break;
1119 }
1120 }
1121
1122 i++;
1123 }
1124
1125 len = &value[2].data[i] - data;
1126
1127 if (len == 0) {
1128 continue;
1129 }
1130
1131 regex->size += len;
1132
1133 size = (len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
1134
1135 copy = ngx_http_rewrite_add_code(lcf->codes,
1136 sizeof(ngx_http_rewrite_copy_code_t) + size,
1137 &regex);
1138 if (copy == NULL) {
1139 return NGX_CONF_ERROR;
1140 }
1141
1142 copy->code = ngx_http_rewrite_copy_code;
1143 copy->len = len;
1144
1145 ngx_memcpy((u_char *) copy + sizeof(ngx_http_rewrite_copy_code_t),
1146 data, len);
1147 }
1148
1149 n = ngx_regex_capture_count(regex->regex);
1150
1151 if (n < 0) {
1152 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1153 ngx_regex_capture_count_n " failed for "
1154 "pattern \"%V\"", &value[1]);
1155 return NGX_CONF_ERROR;
1156 }
1157
1158 if (regex->ncaptures > (ngx_uint_t) n) {
1159 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1160 "pattern \"%V\" has less captures "
1161 "than referrenced in substitution \"%V\"",
1162 &value[1], &value[2]);
1163 return NGX_CONF_ERROR;
1164 }
1165
1166 if (regex->ncaptures < (ngx_uint_t) n) {
1167 regex->ncaptures = (ngx_uint_t) n;
1168 }
1169
1170 if (regex->ncaptures) {
1171 regex->ncaptures = (regex->ncaptures + 1) * 3;
1172 }
1173
1174 regex_end = ngx_http_rewrite_add_code(lcf->codes,
1175 sizeof(ngx_http_rewrite_regex_end_code_t),
1176 &regex);
1177 if (regex_end == NULL) {
1178 return NGX_CONF_ERROR;
1179 }
1180
1181 regex_end->code = ngx_http_rewrite_regex_end_code;
1182 regex_end->uri = regex->uri;
1183 regex_end->args = regex->args;
1184 regex_end->redirect = regex->redirect;
1185
1186 if (last) {
1187 code = ngx_http_rewrite_add_code(lcf->codes, sizeof(uintptr_t),
1188 &regex);
1189 if (code == NULL) {
1190 return NGX_CONF_ERROR;
1191 }
1192
1193 *code = (uintptr_t) NULL;
1194 }
1195
1196 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
1197 - (u_char *) regex;
1198
1199 return NGX_CONF_OK;
1200 }
1201
1202
1203 static char *
1204 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1205 {
1206 ngx_http_rewrite_loc_conf_t *lcf = conf;
1207
1208 ngx_str_t *value;
1209 ngx_http_rewrite_return_code_t *ret;
1210
1211 ret = ngx_http_rewrite_start_code(cf->pool, &lcf->codes,
1212 sizeof(ngx_http_rewrite_return_code_t));
1213 if (ret == NULL) {
1214 return NGX_CONF_ERROR;
1215 }
1216
1217 value = cf->args->elts;
1218
1219 ret->code = ngx_http_rewrite_return_code;
1220 ret->null = (uintptr_t) NULL;
1221
1222 ret->status = ngx_atoi(value[1].data, value[1].len);
1223
1224 if (ret->status == (uintptr_t) NGX_ERROR) {
1225 return NGX_CONF_ERROR;
1226 }
1227
1228 return NGX_CONF_OK;
1229 }
1230
1231
1232 static char *
1233 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1234 {
1235 ngx_http_rewrite_loc_conf_t *lcf = conf;
1236
1237 void *mconf;
1238 char *rv;
1239 u_char *elts;
1240 ngx_uint_t i;
1241 ngx_conf_t save;
1242 ngx_http_module_t *module;
1243 ngx_http_conf_ctx_t *ctx, *pctx;
1244 ngx_http_core_loc_conf_t *clcf, *pclcf, **clcfp;
1245 ngx_http_rewrite_if_code_t *if_code;
1246 ngx_http_rewrite_loc_conf_t *nlcf;
1247
1248 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
1249 if (ctx == NULL) {
1250 return NGX_CONF_ERROR;
1251 }
1252
1253 pctx = cf->ctx;
1254 ctx->main_conf = pctx->main_conf;
1255 ctx->srv_conf = pctx->srv_conf;
1256
1257 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
1258 if (ctx->loc_conf == NULL) {
1259 return NGX_CONF_ERROR;
1260 }
1261
1262 for (i = 0; ngx_modules[i]; i++) {
1263 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
1264 continue;
1265 }
1266
1267 module = ngx_modules[i]->ctx;
1268
1269 if (module->create_loc_conf) {
1270
1271 mconf = module->create_loc_conf(cf);
1272 if (mconf == NULL) {
1273 return NGX_CONF_ERROR;
1274 }
1275
1276 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
1277 }
1278 }
1279
1280 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
1281
1282 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
1283 clcf->loc_conf = ctx->loc_conf;
1284 clcf->name = pclcf->name;
1285 clcf->noname = 1;
1286
1287 if (pclcf->locations.elts == NULL) {
1288 if (ngx_array_init(&pclcf->locations, cf->pool, 4, sizeof(void *))
1289 == NGX_ERROR)
1290 {
1291 return NGX_CONF_ERROR;
1292 }
1293 }
1294
1295 clcfp = ngx_array_push(&pclcf->locations);
1296 if (clcfp == NULL) {
1297 return NGX_CONF_ERROR;
1298 }
1299
1300 *clcfp = clcf;
1301
1302
1303 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) {
1304 return NGX_CONF_ERROR;
1305 }
1306
1307 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_rewrite_if_code_t));
1308 if (if_code == NULL) {
1309 return NULL;
1310 }
1311
1312 if_code->code = ngx_http_rewrite_if_code;
1313
1314 elts = lcf->codes->elts;
1315
1316
1317 /* the inside directives must compile to the same code array */
1318
1319 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index];
1320 nlcf->codes = lcf->codes;
1321
1322
1323 save = *cf;
1324 cf->ctx = ctx;
1325
1326 if (pclcf->name.len == 0) {
1327 if_code->loc_conf = NULL;
1328 cf->cmd_type = NGX_HTTP_SIF_CONF;
1329
1330 } else {
1331 if_code->loc_conf = ctx->loc_conf;
1332 cf->cmd_type = NGX_HTTP_LIF_CONF;
1333 }
1334
1335 rv = ngx_conf_parse(cf, NULL);
1336
1337 *cf = save;
1338
1339 if (rv != NGX_CONF_OK) {
1340 return rv;
1341 }
1342
1343
1344 if (elts != lcf->codes->elts) {
1345 if_code = (ngx_http_rewrite_if_code_t *)
1346 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts));
1347 }
1348
1349 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
1350 - (u_char *) if_code;
1351
1352 return NGX_CONF_OK;
1353 }
1354
1355
1356 static char *
1357 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf)
1358 {
1359 ngx_str_t *value, err;
1360 ngx_uint_t cur, last;
1361 ngx_http_rewrite_regex_code_t *regex;
1362 u_char errstr[NGX_MAX_CONF_ERRSTR];
1363
1364 value = cf->args->elts;
1365 last = cf->args->nelts - 1;
1366
1367 if (value[1].len < 1 || value[1].data[0] != '(') {
1368 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1369 "invalid condition \"%V\"", &value[1]);
1370 return NGX_CONF_ERROR;
1371 }
1372
1373 if (value[1].len == 1) {
1374 cur = 2;
1375
1376 } else {
1377 cur = 1;
1378 value[1].len--;
1379 value[1].data++;
1380 }
1381
1382 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') {
1383 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1384 "invalid condition \"%V\"", &value[last]);
1385 return NGX_CONF_ERROR;
1386 }
1387
1388 if (value[last].len == 1) {
1389 last--;
1390
1391 } else {
1392 value[last].len--;
1393 value[last].data[value[last].len] = '\0';
1394 }
1395
1396 if (value[cur].len > 1 && value[cur].data[0] == '$') {
1397
1398 if (cur != last && cur + 2 != last) {
1399 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1400 "invalid condition \"%V\"", &value[cur]);
1401 return NGX_CONF_ERROR;
1402 }
1403
1404 if (ngx_http_rewrite_variable(cf, lcf, &value[cur])!= NGX_CONF_OK) {
1405 return NGX_CONF_ERROR;
1406 }
1407
1408 if (cur == last) {
1409 return NGX_CONF_OK;
1410 }
1411
1412 cur++;
1413
1414 if ((value[cur].len == 1 && value[cur].data[0] != '~')
1415 || (value[cur].len == 2
1416 && value[cur].data[0] != '~' && value[cur].data[1] != '*'))
1417 {
1418 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1419 "unexpected \"%V\" in condition", &value[cur]);
1420 return NGX_CONF_ERROR;
1421 }
1422
1423 regex = ngx_http_rewrite_start_code(cf->pool, &lcf->codes,
1424 sizeof(ngx_http_rewrite_regex_code_t));
1425 if (regex == NULL) {
1426 return NGX_CONF_ERROR;
1427 }
1428
1429 err.len = NGX_MAX_CONF_ERRSTR;
1430 err.data = errstr;
1431
1432 regex->regex = ngx_regex_compile(&value[last],
1433 (value[cur].len == 2) ? NGX_REGEX_CASELESS : 0,
1434 cf->pool, &err);
1435
1436 if (regex->regex == NULL) {
1437 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
1438 return NGX_CONF_ERROR;
1439 }
1440
1441 regex->code = ngx_http_rewrite_regex_start_code;
1442 regex->size = 0;
1443 regex->ncaptures = 0;
1444 regex->status = 0;
1445 regex->next = sizeof(ngx_http_rewrite_regex_code_t);
1446 regex->test = 1;
1447 regex->uri = 0;
1448 regex->args = 0;
1449 regex->redirect = 0;
1450 regex->name = value[last];
1451
1452 return NGX_CONF_OK;
1453 }
1454
1455 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1456 "invalid condition \"%V\"", &value[cur]);
1457
1458 return NGX_CONF_ERROR;
1459 }
1460
1461
1462 static char *
1463 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
1464 ngx_str_t *value)
1465 {
1466 ngx_http_variable_t *var;
1467 ngx_http_rewrite_code_pt *code;
1468 ngx_http_rewrite_var_code_t *var_code;
1469
1470 value->len--;
1471 value->data++;
1472
1473 if (value->len == sizeof("invalid_referer") - 1
1474 && ngx_strncmp(value->data, "invalid_referer",
1475 sizeof("invalid_referer") - 1) == 0)
1476 {
1477 code = ngx_http_rewrite_start_code(cf->pool, &lcf->codes,
1478 sizeof(ngx_http_rewrite_code_pt));
1479 if (code == NULL) {
1480 return NGX_CONF_ERROR;
1481 }
1482
1483 *code = ngx_http_rewrite_invalid_referer_code;
1484
1485 } else {
1486 var = ngx_http_add_variable(cf, value, 0);
1487
1488 if (var == NULL) {
1489 return NGX_CONF_ERROR;
1490 }
1491
1492 var_code = ngx_http_rewrite_start_code(cf->pool, &lcf->codes,
1493 sizeof(ngx_http_rewrite_var_code_t));
1494 if (var_code == NULL) {
1495 return NGX_CONF_ERROR;
1496 }
1497
1498 var_code->code = ngx_http_rewrite_var_code;
1499 var_code->index = var->index;
1500 }
1501
1502 return NGX_CONF_OK;
1503 }
1504
1505
1506 static char *
1507 ngx_http_rewrite_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1508 {
1509 ngx_http_rewrite_loc_conf_t *lcf = conf;
1510
1511 ngx_uint_t i, server_names;
1512 ngx_str_t *value;
1513 ngx_http_server_name_t *sn;
1514 ngx_http_core_srv_conf_t *cscf;
1515 ngx_http_rewrite_referer_t *ref;
1516
1517 cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
1518
1519 if (lcf->referers == NULL) {
1520 lcf->referers = ngx_array_create(cf->pool,
1521 cf->args->nelts + cscf->server_names.nelts,
1522 sizeof(ngx_http_rewrite_referer_t));
1523 if (lcf->referers == NULL) {
1524 return NGX_CONF_ERROR;
1525 }
1526 }
1527
1528 value = cf->args->elts;
1529 server_names = 0;
1530
1531 for (i = 1; i < cf->args->nelts; i++) {
1532 if (value[i].len == 0) {
1533 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1534 "invalid referer \"%V\"", &value[i]);
1535 return NGX_CONF_ERROR;
1536 }
1537
1538 if (ngx_strcmp(value[i].data, "none") == 0) {
1539 lcf->no_referer = 1;
1540 continue;
1541 }
1542
1543 if (ngx_strcmp(value[i].data, "server_names") == 0) {
1544 server_names = 1;
1545 continue;
1546 }
1547
1548 ref = ngx_array_push(lcf->referers);
1549 if (ref == NULL) {
1550 return NGX_CONF_ERROR;
1551 }
1552
1553 if (value[i].data[0] != '*') {
1554 ref->name = value[i];
1555 ref->wildcard = 0;
1556 continue;
1557 }
1558
1559 if (value[i].data[1] != '.') {
1560 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1561 "invalid wildcard referer \"%V\"", &value[i]);
1562 return NGX_CONF_ERROR;
1563 }
1564
1565 ref->name.len = value[i].len - 1;
1566 ref->name.data = value[i].data + 1;
1567 ref->wildcard = 1;
1568 }
1569
1570 if (!server_names) {
1571 return NGX_CONF_OK;
1572 }
1573
1574 sn = cscf->server_names.elts;
1575 for (i = 0; i < cscf->server_names.nelts; i++) {
1576 ref = ngx_array_push(lcf->referers);
1577 if (ref == NULL) {
1578 return NGX_CONF_ERROR;
1579 }
1580
1581 ref->name.len = sn[i].name.len + 1;
1582 ref->name.data = ngx_palloc(cf->pool, ref->name.len);
1583 if (ref->name.data == NULL) {
1584 return NGX_CONF_ERROR;
1585 }
1586
1587 ngx_memcpy(ref->name.data, sn[i].name.data, sn[i].name.len);
1588 ref->name.data[sn[i].name.len] = '/';
1589 ref->wildcard = sn[i].wildcard;
1590 }
1591
1592 return NGX_CONF_OK;
1593 }
1594
1595
1596 static char *
1597 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1598 {
1599 ngx_http_rewrite_loc_conf_t *lcf = conf;
1600
1601 ngx_int_t n;
1602 ngx_str_t *value;
1603 ngx_http_variable_t *v;
1604 ngx_http_rewrite_var_code_t *var;
1605 ngx_http_rewrite_value_code_t *val;
1606
1607 value = cf->args->elts;
1608
1609 if (value[1].data[0] != '$') {
1610 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1611 "invalid variable name \"%V\"", &value[1]);
1612 return NGX_CONF_ERROR;
1613 }
1614
1615 value[1].len--;
1616 value[1].data++;
1617
1618 v = ngx_http_add_variable(cf, &value[1], 1);
1619 if (v == NULL) {
1620 return NGX_CONF_ERROR;
1621 }
1622
1623 v->handler = ngx_http_rewrite_var;
1624 v->data = v->index;
1625
1626 val = ngx_http_rewrite_start_code(cf->pool, &lcf->codes,
1627 sizeof(ngx_http_rewrite_value_code_t));
1628 if (val == NULL) {
1629 return NGX_CONF_ERROR;
1630 }
1631
1632 n = ngx_atoi(value[2].data, value[2].len);
1633
1634 if (n == NGX_ERROR) {
1635 n = 0;
1636 }
1637
1638 val->code = ngx_http_rewrite_value_code;
1639 val->value = (uintptr_t) n;
1640 val->text_len = (uintptr_t) value[2].len;
1641 val->text_data = (uintptr_t) value[2].data;
1642
1643 var = ngx_http_rewrite_start_code(cf->pool, &lcf->codes,
1644 sizeof(ngx_http_rewrite_var_code_t));
1645 if (var == NULL) {
1646 return NGX_CONF_ERROR;
1647 }
1648
1649 var->code = ngx_http_rewrite_set_var_code;
1650 var->index = (uintptr_t) v->index;
1651
1652 return NGX_CONF_OK;
1653 }
1654
1655
1656 static void *
1657 ngx_http_rewrite_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
1658 {
1659 if (*codes == NULL) {
1660 *codes = ngx_array_create(pool, 256, 1);
1661 if (*codes == NULL) {
1662 return NULL;
1663 }
1664 }
1665
1666 return ngx_array_push_n(*codes, size);
1667 }
1668
1669
1670 static void *
1671 ngx_http_rewrite_add_code(ngx_array_t *codes, size_t size, void *code)
1672 {
1673 u_char *elts, **p;
1674 void *new;
1675
1676 elts = codes->elts;
1677
1678 new = ngx_array_push_n(codes, size);
1679 if (new == NULL) {
1680 return NGX_CONF_ERROR;
1681 }
1682
1683 if (elts != codes->elts) {
1684 p = code;
1685 *p += (u_char *) codes->elts - elts;
1686 }
1687
1688 return new;
1689 }