nginx 0.7.8
[nginx-catap.git] / src / http / modules / ngx_http_sub_filter_module.c
blob62a8f081405fac16862daf97dfe979574b15ffbc
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
12 typedef struct {
13 ngx_str_t match;
14 ngx_str_t sub;
16 ngx_array_t *types; /* array of ngx_str_t */
18 ngx_array_t *sub_lengths;
19 ngx_array_t *sub_values;
21 ngx_flag_t once;
22 } ngx_http_sub_loc_conf_t;
25 typedef enum {
26 sub_start_state = 0,
27 sub_match_state,
28 } ngx_http_sub_state_e;
31 typedef struct {
32 ngx_str_t match;
34 ngx_uint_t once; /* unsigned once:1 */
36 ngx_buf_t *buf;
38 u_char *pos;
39 u_char *copy_start;
40 u_char *copy_end;
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;
48 ngx_str_t sub;
50 ngx_uint_t state;
51 size_t saved;
52 size_t looked;
53 } ngx_http_sub_ctx_t;
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);
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);
70 static ngx_command_t ngx_http_sub_filter_commands[] = {
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,
77 NULL },
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,
84 NULL },
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 },
93 ngx_null_command
97 static ngx_http_module_t ngx_http_sub_filter_module_ctx = {
98 NULL, /* preconfiguration */
99 ngx_http_sub_filter_init, /* postconfiguration */
101 NULL, /* create main configuration */
102 NULL, /* init main configuration */
104 NULL, /* create server configuration */
105 NULL, /* merge server configuration */
107 ngx_http_sub_create_conf, /* create location configuration */
108 ngx_http_sub_merge_conf /* merge location configuration */
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
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;
132 static ngx_int_t
133 ngx_http_sub_header_filter(ngx_http_request_t *r)
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;
140 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
142 if (slcf->match.len == 0
143 || r->headers_out.content_type.len == 0
144 || r->headers_out.content_length_n == 0)
146 return ngx_http_next_header_filter(r);
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)
155 goto found;
159 return ngx_http_next_header_filter(r);
161 found:
163 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_sub_ctx_t));
164 if (ctx == NULL) {
165 return NGX_ERROR;
168 ngx_http_set_ctx(r, ctx, ngx_http_sub_filter_module);
170 ctx->match = slcf->match;
171 ctx->last_out = &ctx->out;
172 ctx->sub = slcf->sub;
174 r->filter_need_in_memory = 1;
176 if (r == r->main) {
177 ngx_http_clear_content_length(r);
178 ngx_http_clear_last_modified(r);
181 return ngx_http_next_header_filter(r);
185 static ngx_int_t
186 ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
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;
194 ctx = ngx_http_get_module_ctx(r, ngx_http_sub_filter_module);
196 if (ctx == NULL) {
197 return ngx_http_next_body_filter(r, in);
200 if ((in == NULL
201 && ctx->buf == NULL
202 && ctx->in == NULL
203 && ctx->busy == NULL))
205 return ngx_http_next_body_filter(r, in);
208 if (ctx->once && (ctx->buf == NULL || ctx->in == NULL)) {
210 if (ctx->busy) {
211 if (ngx_http_sub_output(r, ctx) == NGX_ERROR) {
212 return NGX_ERROR;
216 return ngx_http_next_body_filter(r, in);
219 /* add the incoming chain to the chain ctx->in */
221 if (in) {
222 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
223 return NGX_ERROR;
227 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
228 "http sub filter \"%V\"", &r->uri);
230 while (ctx->in || ctx->buf) {
232 if (ctx->buf == NULL ){
233 ctx->buf = ctx->in->buf;
234 ctx->in = ctx->in->next;
235 ctx->pos = ctx->buf->pos;
238 if (ctx->state == sub_start_state) {
239 ctx->copy_start = ctx->pos;
240 ctx->copy_end = ctx->pos;
243 b = NULL;
245 while (ctx->pos < ctx->buf->last) {
247 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
248 "saved: %d state: %d", ctx->saved, ctx->state);
250 rc = ngx_http_sub_parse(r, ctx);
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);
256 if (rc == NGX_ERROR) {
257 return rc;
260 if (ctx->copy_start != ctx->copy_end) {
262 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
263 "saved: %d", ctx->saved);
265 if (ctx->saved) {
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));
273 } else {
274 b = ngx_calloc_buf(r->pool);
275 if (b == NULL) {
276 return NGX_ERROR;
279 cl = ngx_alloc_chain_link(r->pool);
280 if (cl == NULL) {
281 return NGX_ERROR;
284 cl->buf = b;
287 b->memory = 1;
288 b->pos = ctx->match.data;
289 b->last = ctx->match.data + ctx->saved;
291 *ctx->last_out = cl;
292 ctx->last_out = &cl->next;
294 ctx->saved = 0;
297 if (ctx->free) {
298 cl = ctx->free;
299 ctx->free = ctx->free->next;
300 b = cl->buf;
302 } else {
303 b = ngx_alloc_buf(r->pool);
304 if (b == NULL) {
305 return NGX_ERROR;
308 cl = ngx_alloc_chain_link(r->pool);
309 if (cl == NULL) {
310 return NGX_ERROR;
313 cl->buf = b;
316 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
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;
324 if (b->in_file) {
325 b->file_last = b->file_pos + (b->last - ctx->buf->pos);
326 b->file_pos += b->pos - ctx->buf->pos;
329 cl->next = NULL;
330 *ctx->last_out = cl;
331 ctx->last_out = &cl->next;
334 if (ctx->state == sub_start_state) {
335 ctx->copy_start = ctx->pos;
336 ctx->copy_end = ctx->pos;
338 } else {
339 ctx->copy_start = NULL;
340 ctx->copy_end = NULL;
343 if (rc == NGX_AGAIN) {
344 continue;
348 /* rc == NGX_OK */
350 b = ngx_calloc_buf(r->pool);
351 if (b == NULL) {
352 return NGX_ERROR;
355 cl = ngx_alloc_chain_link(r->pool);
356 if (cl == NULL) {
357 return NGX_ERROR;
360 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
362 if (ctx->sub.data == NULL) {
364 if (ngx_http_script_run(r, &ctx->sub, slcf->sub_lengths->elts,
365 0, slcf->sub_values->elts)
366 == NULL)
368 return NGX_ERROR;
372 if (ctx->sub.len) {
373 b->memory = 1;
374 b->pos = ctx->sub.data;
375 b->last = ctx->sub.data + ctx->sub.len;
377 } else {
378 b->sync = 1;
381 cl->buf = b;
382 cl->next = NULL;
383 *ctx->last_out = cl;
384 ctx->last_out = &cl->next;
386 ctx->once = slcf->once;
388 continue;
391 if (ctx->buf->last_buf || ngx_buf_in_memory(ctx->buf)) {
392 if (b == NULL) {
393 if (ctx->free) {
394 cl = ctx->free;
395 ctx->free = ctx->free->next;
396 b = cl->buf;
397 ngx_memzero(b, sizeof(ngx_buf_t));
399 } else {
400 b = ngx_calloc_buf(r->pool);
401 if (b == NULL) {
402 return NGX_ERROR;
405 cl = ngx_alloc_chain_link(r->pool);
406 if (cl == NULL) {
407 return NGX_ERROR;
410 cl->buf = b;
413 b->sync = 1;
415 cl->next = NULL;
416 *ctx->last_out = cl;
417 ctx->last_out = &cl->next;
420 b->last_buf = ctx->buf->last_buf;
421 b->shadow = ctx->buf;
423 b->recycled = ctx->buf->recycled;
426 ctx->buf = NULL;
428 ctx->saved = ctx->looked;
431 if (ctx->out == NULL && ctx->busy == NULL) {
432 return NGX_OK;
435 return ngx_http_sub_output(r, ctx);
439 static ngx_int_t
440 ngx_http_sub_output(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx)
442 ngx_int_t rc;
443 ngx_buf_t *b;
444 ngx_chain_t *cl;
446 #if 1
447 b = NULL;
448 for (cl = ctx->out; cl; cl = cl->next) {
449 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
450 "sub out: %p %p", cl->buf, cl->buf->pos);
451 if (cl->buf == b) {
452 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
453 "the same buf was used in sub");
454 ngx_debug_point();
455 return NGX_ERROR;
457 b = cl->buf;
459 #endif
461 rc = ngx_http_next_body_filter(r, ctx->out);
463 if (ctx->busy == NULL) {
464 ctx->busy = ctx->out;
466 } else {
467 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
468 cl->next = ctx->out;
471 ctx->out = NULL;
472 ctx->last_out = &ctx->out;
474 while (ctx->busy) {
476 cl = ctx->busy;
477 b = cl->buf;
479 if (ngx_buf_size(b) != 0) {
480 break;
483 #if (NGX_HAVE_WRITE_ZEROCOPY)
484 if (b->zerocopy_busy) {
485 break;
487 #endif
489 if (b->shadow) {
490 b->shadow->pos = b->shadow->last;
493 ctx->busy = cl->next;
495 if (ngx_buf_in_memory(b) || b->in_file) {
496 /* add data bufs only to the free buf chain */
498 cl->next = ctx->free;
499 ctx->free = cl;
503 if (ctx->in || ctx->buf) {
504 r->buffered |= NGX_HTTP_SUB_BUFFERED;
506 } else {
507 r->buffered &= ~NGX_HTTP_SUB_BUFFERED;
510 return rc;
514 static ngx_int_t
515 ngx_http_sub_parse(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx)
517 u_char *p, *last, *copy_end, ch, match;
518 size_t looked;
519 ngx_http_sub_state_e state;
521 if (ctx->once) {
522 ctx->copy_start = ctx->pos;
523 ctx->copy_end = ctx->buf->last;
524 ctx->pos = ctx->buf->last;
525 ctx->looked = 0;
527 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "once");
529 return NGX_AGAIN;
532 state = ctx->state;
533 looked = ctx->looked;
534 last = ctx->buf->last;
535 copy_end = ctx->copy_end;
537 for (p = ctx->pos; p < last; p++) {
539 ch = *p;
540 ch = ngx_tolower(ch);
542 if (state == sub_start_state) {
544 /* the tight loop */
546 match = ctx->match.data[0];
548 for ( ;; ) {
549 if (ch == match) {
550 copy_end = p;
551 looked = 1;
552 state = sub_match_state;
554 goto match_started;
557 if (++p == last) {
558 break;
561 ch = *p;
562 ch = ngx_tolower(ch);
565 ctx->state = state;
566 ctx->pos = p;
567 ctx->looked = looked;
568 ctx->copy_end = p;
570 if (ctx->copy_start == NULL) {
571 ctx->copy_start = ctx->buf->pos;
574 return NGX_AGAIN;
576 match_started:
578 continue;
581 /* state == sub_match_state */
583 if (ch == ctx->match.data[looked]) {
584 looked++;
586 if (looked == ctx->match.len) {
587 if ((size_t) (p - ctx->pos) < looked) {
588 ctx->saved = 0;
591 ctx->state = sub_start_state;
592 ctx->pos = p + 1;
593 ctx->looked = 0;
594 ctx->copy_end = copy_end;
596 if (ctx->copy_start == NULL && copy_end) {
597 ctx->copy_start = ctx->buf->pos;
600 return NGX_OK;
603 } else if (ch == ctx->match.data[0]) {
604 copy_end = p;
605 looked = 1;
607 } else {
608 copy_end = p;
609 looked = 0;
610 state = sub_start_state;
614 ctx->state = state;
615 ctx->pos = p;
616 ctx->looked = looked;
618 ctx->copy_end = (state == sub_start_state) ? p : copy_end;
620 if (ctx->copy_start == NULL && ctx->copy_end) {
621 ctx->copy_start = ctx->buf->pos;
624 return NGX_AGAIN;
628 static char *
629 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
631 ngx_http_sub_loc_conf_t *slcf = conf;
633 ngx_str_t *value;
634 ngx_int_t n;
635 ngx_http_script_compile_t sc;
637 if (slcf->match.len) {
638 return "is duplicate";
641 value = cf->args->elts;
643 ngx_strlow(value[1].data, value[1].data, value[1].len);
645 slcf->match = value[1];
647 n = ngx_http_script_variables_count(&value[2]);
649 if (n == 0) {
650 slcf->sub = value[2];
651 return NGX_CONF_OK;
654 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
656 sc.cf = cf;
657 sc.source = &value[2];
658 sc.lengths = &slcf->sub_lengths;
659 sc.values = &slcf->sub_values;
660 sc.variables = n;
661 sc.complete_lengths = 1;
662 sc.complete_values = 1;
664 if (ngx_http_script_compile(&sc) != NGX_OK) {
665 return NGX_CONF_ERROR;
668 return NGX_CONF_OK;
672 static char *
673 ngx_http_sub_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
675 ngx_http_sub_loc_conf_t *slcf = conf;
677 ngx_str_t *value, *type;
678 ngx_uint_t i;
680 if (slcf->types == NULL) {
681 slcf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
682 if (slcf->types == NULL) {
683 return NGX_CONF_ERROR;
686 type = ngx_array_push(slcf->types);
687 if (type == NULL) {
688 return NGX_CONF_ERROR;
691 type->len = sizeof("text/html") - 1;
692 type->data = (u_char *) "text/html";
695 value = cf->args->elts;
697 for (i = 1; i < cf->args->nelts; i++) {
699 if (ngx_strcmp(value[i].data, "text/html") == 0) {
700 continue;
703 type = ngx_array_push(slcf->types);
704 if (type == NULL) {
705 return NGX_CONF_ERROR;
708 type->len = value[i].len;
710 type->data = ngx_pnalloc(cf->pool, type->len + 1);
711 if (type->data == NULL) {
712 return NGX_CONF_ERROR;
715 ngx_cpystrn(type->data, value[i].data, type->len + 1);
718 return NGX_CONF_OK;
722 static void *
723 ngx_http_sub_create_conf(ngx_conf_t *cf)
725 ngx_http_sub_loc_conf_t *slcf;
727 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t));
728 if (slcf == NULL) {
729 return NGX_CONF_ERROR;
733 * set by ngx_pcalloc():
735 * conf->match.len = 0;
736 * conf->match.data = NULL;
737 * conf->sub.len = 0;
738 * conf->sub.data = NULL;
739 * conf->sub_lengths = NULL;
740 * conf->sub_values = NULL;
741 * conf->types = NULL;
744 slcf->once = NGX_CONF_UNSET;
746 return slcf;
750 static char *
751 ngx_http_sub_merge_conf(ngx_conf_t *cf, void *parent, void *child)
753 ngx_http_sub_loc_conf_t *prev = parent;
754 ngx_http_sub_loc_conf_t *conf = child;
756 ngx_str_t *type;
758 ngx_conf_merge_value(conf->once, prev->once, 1);
759 ngx_conf_merge_str_value(conf->match, prev->match, "");
761 if (conf->sub.data == NULL && conf->sub_lengths == NULL) {
762 conf->sub = prev->sub;
763 conf->sub_lengths = prev->sub_lengths;
764 conf->sub_values = prev->sub_values;
767 if (conf->types == NULL) {
768 if (prev->types == NULL) {
769 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
770 if (conf->types == NULL) {
771 return NGX_CONF_ERROR;
774 type = ngx_array_push(conf->types);
775 if (type == NULL) {
776 return NGX_CONF_ERROR;
779 type->len = sizeof("text/html") - 1;
780 type->data = (u_char *) "text/html";
782 } else {
783 conf->types = prev->types;
787 return NGX_CONF_OK;
791 static ngx_int_t
792 ngx_http_sub_filter_init(ngx_conf_t *cf)
794 ngx_http_next_header_filter = ngx_http_top_header_filter;
795 ngx_http_top_header_filter = ngx_http_sub_header_filter;
797 ngx_http_next_body_filter = ngx_http_top_body_filter;
798 ngx_http_top_body_filter = ngx_http_sub_body_filter;
800 return NGX_OK;