comparison src/http/modules/ngx_http_sub_filter_module.c @ 296:2ceaee987f37 NGINX_0_5_18

nginx 0.5.18 *) Feature: the ngx_http_sub_filter_module. *) Feature: the "$upstream_http_..." variables. *) Feature: now the $upstream_status and $upstream_response_time variables keep data about all upstreams before X-Accel-Redirect. *) Bugfix: a segmentation fault occurred in master process after first reconfiguration and receiving any signal if nginx was built with ngx_http_perl_module and perl did not support multiplicity; bug appeared in 0.5.9. *) Bugfix: if perl did not support multiplicity, then after reconfiguration perl code did not work; bug appeared in 0.3.38.
author Igor Sysoev <http://sysoev.ru>
date Thu, 19 Apr 2007 00:00:00 +0400
parents
children 10cc350ed8a1
comparison
equal deleted inserted replaced
295:65b7ac8795e3 296:2ceaee987f37
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 {
13 ngx_str_t match;
14 ngx_str_t sub;
15
16 ngx_array_t *types; /* array of ngx_str_t */
17
18 ngx_array_t *sub_lengths;
19 ngx_array_t *sub_values;
20
21 ngx_flag_t once;
22 } ngx_http_sub_loc_conf_t;
23
24
25 typedef enum {
26 sub_start_state = 0,
27 sub_match_state,
28 } ngx_http_sub_state_e;
29
30
31 typedef struct {
32 ngx_str_t match;
33
34 ngx_uint_t once; /* unsigned once:1 */
35
36 ngx_buf_t *buf;
37
38 u_char *pos;
39 u_char *copy_start;
40 u_char *copy_end;
41
42 ngx_chain_t *in;
43 ngx_chain_t *out;
44 ngx_chain_t **last_out;
45 ngx_chain_t *busy;
46 ngx_chain_t *free;
47
48 ngx_str_t sub;
49
50 ngx_uint_t state;
51 size_t saved;
52 size_t looked;
53 } ngx_http_sub_ctx_t;
54
55
56 static ngx_int_t ngx_http_sub_output(ngx_http_request_t *r,
57 ngx_http_sub_ctx_t *ctx);
58 static ngx_int_t ngx_http_sub_parse(ngx_http_request_t *r,
59 ngx_http_sub_ctx_t *ctx);
60
61 static char * ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd,
62 void *conf);
63 static char *ngx_http_sub_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
64 static void *ngx_http_sub_create_conf(ngx_conf_t *cf);
65 static char *ngx_http_sub_merge_conf(ngx_conf_t *cf,
66 void *parent, void *child);
67 static ngx_int_t ngx_http_sub_filter_init(ngx_conf_t *cf);
68
69
70 static ngx_command_t ngx_http_sub_filter_commands[] = {
71
72 { ngx_string("sub_filter"),
73 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
74 ngx_http_sub_filter,
75 NGX_HTTP_LOC_CONF_OFFSET,
76 0,
77 NULL },
78
79 { ngx_string("sub_filter_types"),
80 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
81 ngx_http_sub_types,
82 NGX_HTTP_LOC_CONF_OFFSET,
83 0,
84 NULL },
85
86 { ngx_string("sub_filter_once"),
87 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
88 ngx_conf_set_flag_slot,
89 NGX_HTTP_LOC_CONF_OFFSET,
90 offsetof(ngx_http_sub_loc_conf_t, once),
91 NULL },
92
93 ngx_null_command
94 };
95
96
97 static ngx_http_module_t ngx_http_sub_filter_module_ctx = {
98 NULL, /* preconfiguration */
99 ngx_http_sub_filter_init, /* postconfiguration */
100
101 NULL, /* create main configuration */
102 NULL, /* init main configuration */
103
104 NULL, /* create server configuration */
105 NULL, /* merge server configuration */
106
107 ngx_http_sub_create_conf, /* create location configuration */
108 ngx_http_sub_merge_conf /* merge location configuration */
109 };
110
111
112 ngx_module_t ngx_http_sub_filter_module = {
113 NGX_MODULE_V1,
114 &ngx_http_sub_filter_module_ctx, /* module context */
115 ngx_http_sub_filter_commands, /* module directives */
116 NGX_HTTP_MODULE, /* module type */
117 NULL, /* init master */
118 NULL, /* init module */
119 NULL, /* init process */
120 NULL, /* init thread */
121 NULL, /* exit thread */
122 NULL, /* exit process */
123 NULL, /* exit master */
124 NGX_MODULE_V1_PADDING
125 };
126
127
128 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
129 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
130
131
132 static ngx_int_t
133 ngx_http_sub_header_filter(ngx_http_request_t *r)
134 {
135 ngx_str_t *type;
136 ngx_uint_t i;
137 ngx_http_sub_ctx_t *ctx;
138 ngx_http_sub_loc_conf_t *slcf;
139
140 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
141
142 if (slcf->match.len == 0
143 || r->headers_out.content_type.len == 0
144 || r->headers_out.content_length_n == 0)
145 {
146 return ngx_http_next_header_filter(r);
147 }
148
149 type = slcf->types->elts;
150 for (i = 0; i < slcf->types->nelts; i++) {
151 if (r->headers_out.content_type.len >= type[i].len
152 && ngx_strncasecmp(r->headers_out.content_type.data,
153 type[i].data, type[i].len) == 0)
154 {
155 goto found;
156 }
157 }
158
159 return ngx_http_next_header_filter(r);
160
161 found:
162
163 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_sub_ctx_t));
164 if (ctx == NULL) {
165 return NGX_ERROR;
166 }
167
168 ngx_http_set_ctx(r, ctx, ngx_http_sub_filter_module);
169
170 ctx->match = slcf->match;
171 ctx->last_out = &ctx->out;
172 ctx->sub = slcf->sub;
173
174 r->filter_need_in_memory = 1;
175
176 if (r == r->main) {
177 ngx_http_clear_content_length(r);
178 ngx_http_clear_last_modified(r);
179 }
180
181 return ngx_http_next_header_filter(r);
182 }
183
184
185 static ngx_int_t
186 ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
187 {
188 ngx_int_t rc;
189 ngx_buf_t *b;
190 ngx_chain_t *cl;
191 ngx_http_sub_ctx_t *ctx;
192 ngx_http_sub_loc_conf_t *slcf;
193
194 ctx = ngx_http_get_module_ctx(r, ngx_http_sub_filter_module);
195
196 if (ctx == NULL) {
197 return ngx_http_next_body_filter(r, in);
198 }
199
200 if ((in == NULL
201 && ctx->buf == NULL
202 && ctx->in == NULL
203 && ctx->busy == NULL))
204 {
205 return ngx_http_next_body_filter(r, in);
206 }
207
208 if (ctx->once && (ctx->buf == NULL || ctx->in == NULL)) {
209
210 if (ctx->busy) {
211 if (ngx_http_sub_output(r, ctx) == NGX_ERROR) {
212 return NGX_ERROR;
213 }
214 }
215
216 return ngx_http_next_body_filter(r, in);
217 }
218
219 /* add the incoming chain to the chain ctx->in */
220
221 if (in) {
222 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
223 return NGX_ERROR;
224 }
225 }
226
227 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
228 "http sub filter \"%V\"", &r->uri);
229
230 while (ctx->in || ctx->buf) {
231
232 if (ctx->buf == NULL ){
233 ctx->buf = ctx->in->buf;
234 ctx->in = ctx->in->next;
235 ctx->pos = ctx->buf->pos;
236 }
237
238 if (ctx->state == sub_start_state) {
239 ctx->copy_start = ctx->pos;
240 ctx->copy_end = ctx->pos;
241 }
242
243 b = NULL;
244
245 while (ctx->pos < ctx->buf->last) {
246
247 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
248 "saved: %d state: %d", ctx->saved, ctx->state);
249
250 rc = ngx_http_sub_parse(r, ctx);
251
252 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
253 "parse: %d, looked: %d %p-%p",
254 rc, ctx->looked, ctx->copy_start, ctx->copy_end);
255
256 if (rc == NGX_ERROR) {
257 return rc;
258 }
259
260 if (ctx->copy_start != ctx->copy_end) {
261
262 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
263 "saved: %d", ctx->saved);
264
265 if (ctx->saved) {
266
267 if (ctx->free) {
268 cl = ctx->free;
269 ctx->free = ctx->free->next;
270 b = cl->buf;
271 ngx_memzero(b, sizeof(ngx_buf_t));
272
273 } else {
274 b = ngx_calloc_buf(r->pool);
275 if (b == NULL) {
276 return NGX_ERROR;
277 }
278
279 cl = ngx_alloc_chain_link(r->pool);
280 if (cl == NULL) {
281 return NGX_ERROR;
282 }
283
284 cl->buf = b;
285 }
286
287 b->memory = 1;
288 b->pos = ctx->match.data;
289 b->last = ctx->match.data + ctx->saved;
290
291 *ctx->last_out = cl;
292 ctx->last_out = &cl->next;
293
294 ctx->saved = 0;
295 }
296
297 if (ctx->free) {
298 cl = ctx->free;
299 ctx->free = ctx->free->next;
300 b = cl->buf;
301
302 } else {
303 b = ngx_alloc_buf(r->pool);
304 if (b == NULL) {
305 return NGX_ERROR;
306 }
307
308 cl = ngx_alloc_chain_link(r->pool);
309 if (cl == NULL) {
310 return NGX_ERROR;
311 }
312
313 cl->buf = b;
314 }
315
316 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
317
318 b->pos = ctx->copy_start;
319 b->last = ctx->copy_end;
320 b->shadow = NULL;
321 b->last_buf = 0;
322 b->recycled = 0;
323
324 if (b->in_file) {
325 b->file_last = b->file_pos + (b->last - b->start);
326 b->file_pos += b->pos - b->start;
327 }
328
329 cl->next = NULL;
330 *ctx->last_out = cl;
331 ctx->last_out = &cl->next;
332 }
333
334 if (ctx->state == sub_start_state) {
335 ctx->copy_start = ctx->pos;
336 ctx->copy_end = ctx->pos;
337
338 } else {
339 ctx->copy_start = NULL;
340 ctx->copy_end = NULL;
341 }
342
343 if (rc == NGX_AGAIN) {
344 continue;
345 }
346
347
348 /* rc == NGX_OK */
349
350 b = ngx_calloc_buf(r->pool);
351 if (b == NULL) {
352 return NGX_ERROR;
353 }
354
355 cl = ngx_alloc_chain_link(r->pool);
356 if (cl == NULL) {
357 return NGX_ERROR;
358 }
359
360 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
361
362 if (ctx->sub.data == NULL) {
363
364 if (ngx_http_script_run(r, &ctx->sub, slcf->sub_lengths->elts,
365 0, slcf->sub_values->elts)
366 == NULL)
367 {
368 return NGX_ERROR;
369 }
370 }
371
372 b->memory = 1;
373 b->pos = ctx->sub.data;
374 b->last = ctx->sub.data + ctx->sub.len;
375
376 cl->buf = b;
377 cl->next = NULL;
378 *ctx->last_out = cl;
379 ctx->last_out = &cl->next;
380
381 ctx->once = slcf->once;
382
383 continue;
384 }
385
386 if (ctx->buf->last_buf || ngx_buf_in_memory(ctx->buf)) {
387 if (b == NULL) {
388 if (ctx->free) {
389 cl = ctx->free;
390 ctx->free = ctx->free->next;
391 b = cl->buf;
392 ngx_memzero(b, sizeof(ngx_buf_t));
393
394 } else {
395 b = ngx_calloc_buf(r->pool);
396 if (b == NULL) {
397 return NGX_ERROR;
398 }
399
400 cl = ngx_alloc_chain_link(r->pool);
401 if (cl == NULL) {
402 return NGX_ERROR;
403 }
404
405 cl->buf = b;
406 }
407
408 b->sync = 1;
409
410 cl->next = NULL;
411 *ctx->last_out = cl;
412 ctx->last_out = &cl->next;
413 }
414
415 b->last_buf = ctx->buf->last_buf;
416 b->shadow = ctx->buf;
417
418 b->recycled = ctx->buf->recycled;
419 }
420
421 ctx->buf = NULL;
422
423 ctx->saved = ctx->looked;
424 }
425
426 if (ctx->out == NULL && ctx->busy == NULL) {
427 return NGX_OK;
428 }
429
430 return ngx_http_sub_output(r, ctx);
431 }
432
433
434 static ngx_int_t
435 ngx_http_sub_output(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx)
436 {
437 ngx_int_t rc;
438 ngx_buf_t *b;
439 ngx_chain_t *cl;
440
441 #if 1
442 b = NULL;
443 for (cl = ctx->out; cl; cl = cl->next) {
444 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
445 "sub out: %p %p", cl->buf, cl->buf->pos);
446 if (cl->buf == b) {
447 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
448 "the same buf was used in sub");
449 ngx_debug_point();
450 return NGX_ERROR;
451 }
452 b = cl->buf;
453 }
454 #endif
455
456 rc = ngx_http_next_body_filter(r, ctx->out);
457
458 if (ctx->busy == NULL) {
459 ctx->busy = ctx->out;
460
461 } else {
462 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
463 cl->next = ctx->out;
464 }
465
466 ctx->out = NULL;
467 ctx->last_out = &ctx->out;
468
469 while (ctx->busy) {
470
471 cl = ctx->busy;
472 b = cl->buf;
473
474 if (ngx_buf_size(b) != 0) {
475 break;
476 }
477
478 #if (NGX_HAVE_WRITE_ZEROCOPY)
479 if (b->zerocopy_busy) {
480 break;
481 }
482 #endif
483
484 if (b->shadow) {
485 b->shadow->pos = b->shadow->last;
486 }
487
488 ctx->busy = cl->next;
489
490 if (ngx_buf_in_memory(b) || b->in_file) {
491 /* add data bufs only to the free buf chain */
492
493 cl->next = ctx->free;
494 ctx->free = cl;
495 }
496 }
497
498 if (ctx->in || ctx->buf) {
499 r->buffered |= NGX_HTTP_SUB_BUFFERED;
500
501 } else {
502 r->buffered &= ~NGX_HTTP_SUB_BUFFERED;
503 }
504
505 return rc;
506 }
507
508
509 static ngx_int_t
510 ngx_http_sub_parse(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx)
511 {
512 u_char *p, *last, *copy_end, ch, match;
513 size_t looked;
514 ngx_http_sub_state_e state;
515
516 if (ctx->once) {
517 ctx->copy_start = ctx->pos;
518 ctx->copy_end = ctx->buf->last;
519 ctx->pos = ctx->buf->last;
520 ctx->looked = 0;
521
522 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "once");
523
524 return NGX_AGAIN;
525 }
526
527 state = ctx->state;
528 looked = ctx->looked;
529 last = ctx->buf->last;
530 copy_end = ctx->copy_end;
531
532 for (p = ctx->pos; p < last; p++) {
533
534 ch = *p;
535 ch = ngx_tolower(ch);
536
537 if (state == sub_start_state) {
538
539 /* the tight loop */
540
541 match = ctx->match.data[0];
542
543 for ( ;; ) {
544 if (ch == match) {
545 copy_end = p;
546 looked = 1;
547 state = sub_match_state;
548
549 goto match_started;
550 }
551
552 if (++p == last) {
553 break;
554 }
555
556 ch = *p;
557 ch = ngx_tolower(ch);
558 }
559
560 ctx->pos = p;
561 ctx->looked = looked;
562 ctx->copy_end = p;
563
564 if (ctx->copy_start == NULL) {
565 ctx->copy_start = ctx->buf->pos;
566 }
567
568 return NGX_AGAIN;
569
570 match_started:
571
572 continue;
573 }
574
575 /* state == sub_match_state */
576
577 if (ch == ctx->match.data[looked]) {
578 looked++;
579
580 if (looked == ctx->match.len) {
581 ctx->state = sub_start_state;
582 ctx->pos = p + 1;
583 ctx->looked = looked;
584 ctx->copy_end = copy_end;
585
586 if (ctx->copy_start == NULL && copy_end) {
587 ctx->copy_start = ctx->buf->pos;
588 }
589
590 return NGX_OK;
591 }
592
593 } else if (ch == ctx->match.data[0]) {
594 copy_end = p;
595 looked = 1;
596
597 } else {
598 copy_end = p;
599 looked = 0;
600 state = sub_start_state;
601 }
602 }
603
604 ctx->state = state;
605 ctx->pos = p;
606 ctx->looked = looked;
607
608 ctx->copy_end = (state == sub_start_state) ? p : copy_end;
609
610 if (ctx->copy_start == NULL && ctx->copy_end) {
611 ctx->copy_start = ctx->buf->pos;
612 }
613
614 return NGX_AGAIN;
615 }
616
617
618 static char *
619 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
620 {
621 ngx_http_sub_loc_conf_t *slcf = conf;
622
623 ngx_str_t *value;
624 ngx_int_t n;
625 ngx_uint_t i;
626 ngx_http_script_compile_t sc;
627
628 if (slcf->match.len) {
629 return "is duplicate";
630 }
631
632 value = cf->args->elts;
633
634 slcf->match = value[1];
635
636 for (i = 0; i < value[1].len; i++) {
637 value[1].data[i] = ngx_tolower(value[1].data[i]);
638 }
639
640 n = ngx_http_script_variables_count(&value[2]);
641
642 if (n == 0) {
643 slcf->sub = value[2];
644 return NGX_CONF_OK;
645 }
646
647 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
648
649 sc.cf = cf;
650 sc.source = &value[2];
651 sc.lengths = &slcf->sub_lengths;
652 sc.values = &slcf->sub_values;
653 sc.variables = n;
654 sc.complete_lengths = 1;
655 sc.complete_values = 1;
656
657 if (ngx_http_script_compile(&sc) != NGX_OK) {
658 return NGX_CONF_ERROR;
659 }
660
661 return NGX_CONF_OK;
662 }
663
664
665 static char *
666 ngx_http_sub_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
667 {
668 ngx_http_sub_loc_conf_t *slcf = conf;
669
670 ngx_str_t *value, *type;
671 ngx_uint_t i;
672
673 if (slcf->types == NULL) {
674 slcf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
675 if (slcf->types == NULL) {
676 return NGX_CONF_ERROR;
677 }
678
679 type = ngx_array_push(slcf->types);
680 if (type == NULL) {
681 return NGX_CONF_ERROR;
682 }
683
684 type->len = sizeof("text/html") - 1;
685 type->data = (u_char *) "text/html";
686 }
687
688 value = cf->args->elts;
689
690 for (i = 1; i < cf->args->nelts; i++) {
691
692 if (ngx_strcmp(value[i].data, "text/html") == 0) {
693 continue;
694 }
695
696 type = ngx_array_push(slcf->types);
697 if (type == NULL) {
698 return NGX_CONF_ERROR;
699 }
700
701 type->len = value[i].len;
702
703 type->data = ngx_palloc(cf->pool, type->len + 1);
704 if (type->data == NULL) {
705 return NGX_CONF_ERROR;
706 }
707
708 ngx_cpystrn(type->data, value[i].data, type->len + 1);
709 }
710
711 return NGX_CONF_OK;
712 }
713
714
715 static void *
716 ngx_http_sub_create_conf(ngx_conf_t *cf)
717 {
718 ngx_http_sub_loc_conf_t *slcf;
719
720 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t));
721 if (slcf == NULL) {
722 return NGX_CONF_ERROR;
723 }
724
725 /*
726 * set by ngx_pcalloc():
727 *
728 * conf->match.len = 0;
729 * conf->match.data = NULL;
730 * conf->sub.len = 0;
731 * conf->sub.data = NULL;
732 * conf->sub_lengths = NULL;
733 * conf->sub_values = NULL;
734 * conf->types = NULL;
735 */
736
737 slcf->once = NGX_CONF_UNSET;
738
739 return slcf;
740 }
741
742
743 static char *
744 ngx_http_sub_merge_conf(ngx_conf_t *cf, void *parent, void *child)
745 {
746 ngx_http_sub_loc_conf_t *prev = parent;
747 ngx_http_sub_loc_conf_t *conf = child;
748
749 ngx_str_t *type;
750
751 ngx_conf_merge_value(conf->once, prev->once, 1);
752 ngx_conf_merge_str_value(conf->match, prev->match, "");
753
754 if (conf->sub.data == NULL && conf->sub_lengths == NULL) {
755 conf->sub = prev->sub;
756 conf->sub_lengths = prev->sub_lengths;
757 conf->sub_values = prev->sub_values;
758 }
759
760 if (conf->types == NULL) {
761 if (prev->types == NULL) {
762 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
763 if (conf->types == NULL) {
764 return NGX_CONF_ERROR;
765 }
766
767 type = ngx_array_push(conf->types);
768 if (type == NULL) {
769 return NGX_CONF_ERROR;
770 }
771
772 type->len = sizeof("text/html") - 1;
773 type->data = (u_char *) "text/html";
774
775 } else {
776 conf->types = prev->types;
777 }
778 }
779
780 return NGX_CONF_OK;
781 }
782
783
784 static ngx_int_t
785 ngx_http_sub_filter_init(ngx_conf_t *cf)
786 {
787 ngx_http_next_header_filter = ngx_http_top_header_filter;
788 ngx_http_top_header_filter = ngx_http_sub_header_filter;
789
790 ngx_http_next_body_filter = ngx_http_top_body_filter;
791 ngx_http_top_body_filter = ngx_http_sub_body_filter;
792
793 return NGX_OK;
794 }