Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_sub_filter_module.c @ 1555:76fe59c6fafb
fix empty string replacement in sub_filter
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 01 Oct 2007 16:24:15 +0000 |
parents | 383ae25c3171 |
children | 9d094e581587 |
rev | line source |
---|---|
1172 | 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 | |
1555
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
372 if (ctx->sub.len) { |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
373 b->memory = 1; |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
374 b->pos = ctx->sub.data; |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
375 b->last = ctx->sub.data + ctx->sub.len; |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
376 |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
377 } else { |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
378 b->sync = 1; |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
379 } |
1172 | 380 |
381 cl->buf = b; | |
382 cl->next = NULL; | |
383 *ctx->last_out = cl; | |
384 ctx->last_out = &cl->next; | |
385 | |
386 ctx->once = slcf->once; | |
387 | |
388 continue; | |
389 } | |
390 | |
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)); | |
398 | |
399 } else { | |
400 b = ngx_calloc_buf(r->pool); | |
401 if (b == NULL) { | |
402 return NGX_ERROR; | |
403 } | |
404 | |
405 cl = ngx_alloc_chain_link(r->pool); | |
406 if (cl == NULL) { | |
407 return NGX_ERROR; | |
408 } | |
409 | |
410 cl->buf = b; | |
411 } | |
412 | |
413 b->sync = 1; | |
414 | |
415 cl->next = NULL; | |
416 *ctx->last_out = cl; | |
417 ctx->last_out = &cl->next; | |
418 } | |
419 | |
420 b->last_buf = ctx->buf->last_buf; | |
421 b->shadow = ctx->buf; | |
422 | |
423 b->recycled = ctx->buf->recycled; | |
424 } | |
425 | |
426 ctx->buf = NULL; | |
427 | |
428 ctx->saved = ctx->looked; | |
429 } | |
430 | |
431 if (ctx->out == NULL && ctx->busy == NULL) { | |
432 return NGX_OK; | |
433 } | |
434 | |
435 return ngx_http_sub_output(r, ctx); | |
436 } | |
437 | |
438 | |
439 static ngx_int_t | |
440 ngx_http_sub_output(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx) | |
441 { | |
442 ngx_int_t rc; | |
443 ngx_buf_t *b; | |
444 ngx_chain_t *cl; | |
445 | |
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; | |
456 } | |
457 b = cl->buf; | |
458 } | |
459 #endif | |
460 | |
461 rc = ngx_http_next_body_filter(r, ctx->out); | |
462 | |
463 if (ctx->busy == NULL) { | |
464 ctx->busy = ctx->out; | |
465 | |
466 } else { | |
467 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ } | |
468 cl->next = ctx->out; | |
469 } | |
470 | |
471 ctx->out = NULL; | |
472 ctx->last_out = &ctx->out; | |
473 | |
474 while (ctx->busy) { | |
475 | |
476 cl = ctx->busy; | |
477 b = cl->buf; | |
478 | |
479 if (ngx_buf_size(b) != 0) { | |
480 break; | |
481 } | |
482 | |
483 #if (NGX_HAVE_WRITE_ZEROCOPY) | |
484 if (b->zerocopy_busy) { | |
485 break; | |
486 } | |
487 #endif | |
488 | |
489 if (b->shadow) { | |
490 b->shadow->pos = b->shadow->last; | |
491 } | |
492 | |
493 ctx->busy = cl->next; | |
494 | |
495 if (ngx_buf_in_memory(b) || b->in_file) { | |
496 /* add data bufs only to the free buf chain */ | |
497 | |
498 cl->next = ctx->free; | |
499 ctx->free = cl; | |
500 } | |
501 } | |
502 | |
503 if (ctx->in || ctx->buf) { | |
504 r->buffered |= NGX_HTTP_SUB_BUFFERED; | |
505 | |
506 } else { | |
507 r->buffered &= ~NGX_HTTP_SUB_BUFFERED; | |
508 } | |
509 | |
510 return rc; | |
511 } | |
512 | |
513 | |
514 static ngx_int_t | |
515 ngx_http_sub_parse(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx) | |
516 { | |
517 u_char *p, *last, *copy_end, ch, match; | |
518 size_t looked; | |
519 ngx_http_sub_state_e state; | |
520 | |
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; | |
526 | |
527 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "once"); | |
528 | |
529 return NGX_AGAIN; | |
530 } | |
531 | |
532 state = ctx->state; | |
533 looked = ctx->looked; | |
534 last = ctx->buf->last; | |
535 copy_end = ctx->copy_end; | |
536 | |
537 for (p = ctx->pos; p < last; p++) { | |
538 | |
539 ch = *p; | |
540 ch = ngx_tolower(ch); | |
541 | |
542 if (state == sub_start_state) { | |
543 | |
544 /* the tight loop */ | |
545 | |
546 match = ctx->match.data[0]; | |
547 | |
548 for ( ;; ) { | |
549 if (ch == match) { | |
550 copy_end = p; | |
551 looked = 1; | |
552 state = sub_match_state; | |
553 | |
554 goto match_started; | |
555 } | |
556 | |
557 if (++p == last) { | |
558 break; | |
559 } | |
560 | |
561 ch = *p; | |
562 ch = ngx_tolower(ch); | |
563 } | |
564 | |
565 ctx->pos = p; | |
566 ctx->looked = looked; | |
567 ctx->copy_end = p; | |
568 | |
569 if (ctx->copy_start == NULL) { | |
570 ctx->copy_start = ctx->buf->pos; | |
571 } | |
572 | |
573 return NGX_AGAIN; | |
574 | |
575 match_started: | |
576 | |
577 continue; | |
578 } | |
579 | |
580 /* state == sub_match_state */ | |
581 | |
582 if (ch == ctx->match.data[looked]) { | |
583 looked++; | |
584 | |
585 if (looked == ctx->match.len) { | |
586 ctx->state = sub_start_state; | |
587 ctx->pos = p + 1; | |
588 ctx->looked = looked; | |
589 ctx->copy_end = copy_end; | |
590 | |
591 if (ctx->copy_start == NULL && copy_end) { | |
592 ctx->copy_start = ctx->buf->pos; | |
593 } | |
594 | |
595 return NGX_OK; | |
596 } | |
597 | |
598 } else if (ch == ctx->match.data[0]) { | |
599 copy_end = p; | |
600 looked = 1; | |
601 | |
602 } else { | |
603 copy_end = p; | |
604 looked = 0; | |
605 state = sub_start_state; | |
606 } | |
607 } | |
608 | |
609 ctx->state = state; | |
610 ctx->pos = p; | |
611 ctx->looked = looked; | |
612 | |
613 ctx->copy_end = (state == sub_start_state) ? p : copy_end; | |
614 | |
615 if (ctx->copy_start == NULL && ctx->copy_end) { | |
616 ctx->copy_start = ctx->buf->pos; | |
617 } | |
618 | |
619 return NGX_AGAIN; | |
620 } | |
621 | |
622 | |
623 static char * | |
624 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
625 { | |
626 ngx_http_sub_loc_conf_t *slcf = conf; | |
627 | |
628 ngx_str_t *value; | |
629 ngx_int_t n; | |
630 ngx_uint_t i; | |
631 ngx_http_script_compile_t sc; | |
632 | |
633 if (slcf->match.len) { | |
634 return "is duplicate"; | |
635 } | |
636 | |
637 value = cf->args->elts; | |
638 | |
639 slcf->match = value[1]; | |
640 | |
641 for (i = 0; i < value[1].len; i++) { | |
642 value[1].data[i] = ngx_tolower(value[1].data[i]); | |
643 } | |
644 | |
645 n = ngx_http_script_variables_count(&value[2]); | |
646 | |
647 if (n == 0) { | |
648 slcf->sub = value[2]; | |
649 return NGX_CONF_OK; | |
650 } | |
651 | |
652 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
653 | |
654 sc.cf = cf; | |
655 sc.source = &value[2]; | |
656 sc.lengths = &slcf->sub_lengths; | |
657 sc.values = &slcf->sub_values; | |
658 sc.variables = n; | |
659 sc.complete_lengths = 1; | |
660 sc.complete_values = 1; | |
661 | |
662 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
663 return NGX_CONF_ERROR; | |
664 } | |
665 | |
666 return NGX_CONF_OK; | |
667 } | |
668 | |
669 | |
670 static char * | |
671 ngx_http_sub_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
672 { | |
673 ngx_http_sub_loc_conf_t *slcf = conf; | |
674 | |
675 ngx_str_t *value, *type; | |
676 ngx_uint_t i; | |
677 | |
678 if (slcf->types == NULL) { | |
679 slcf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t)); | |
680 if (slcf->types == NULL) { | |
681 return NGX_CONF_ERROR; | |
682 } | |
683 | |
684 type = ngx_array_push(slcf->types); | |
685 if (type == NULL) { | |
686 return NGX_CONF_ERROR; | |
687 } | |
688 | |
689 type->len = sizeof("text/html") - 1; | |
690 type->data = (u_char *) "text/html"; | |
691 } | |
692 | |
693 value = cf->args->elts; | |
694 | |
695 for (i = 1; i < cf->args->nelts; i++) { | |
696 | |
697 if (ngx_strcmp(value[i].data, "text/html") == 0) { | |
698 continue; | |
699 } | |
700 | |
701 type = ngx_array_push(slcf->types); | |
702 if (type == NULL) { | |
703 return NGX_CONF_ERROR; | |
704 } | |
705 | |
706 type->len = value[i].len; | |
707 | |
708 type->data = ngx_palloc(cf->pool, type->len + 1); | |
709 if (type->data == NULL) { | |
710 return NGX_CONF_ERROR; | |
711 } | |
712 | |
713 ngx_cpystrn(type->data, value[i].data, type->len + 1); | |
714 } | |
715 | |
716 return NGX_CONF_OK; | |
717 } | |
718 | |
719 | |
720 static void * | |
721 ngx_http_sub_create_conf(ngx_conf_t *cf) | |
722 { | |
723 ngx_http_sub_loc_conf_t *slcf; | |
724 | |
725 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t)); | |
726 if (slcf == NULL) { | |
727 return NGX_CONF_ERROR; | |
728 } | |
729 | |
730 /* | |
731 * set by ngx_pcalloc(): | |
732 * | |
733 * conf->match.len = 0; | |
734 * conf->match.data = NULL; | |
735 * conf->sub.len = 0; | |
736 * conf->sub.data = NULL; | |
737 * conf->sub_lengths = NULL; | |
738 * conf->sub_values = NULL; | |
739 * conf->types = NULL; | |
740 */ | |
741 | |
742 slcf->once = NGX_CONF_UNSET; | |
743 | |
744 return slcf; | |
745 } | |
746 | |
747 | |
748 static char * | |
749 ngx_http_sub_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
750 { | |
751 ngx_http_sub_loc_conf_t *prev = parent; | |
752 ngx_http_sub_loc_conf_t *conf = child; | |
753 | |
754 ngx_str_t *type; | |
755 | |
756 ngx_conf_merge_value(conf->once, prev->once, 1); | |
757 ngx_conf_merge_str_value(conf->match, prev->match, ""); | |
758 | |
759 if (conf->sub.data == NULL && conf->sub_lengths == NULL) { | |
760 conf->sub = prev->sub; | |
761 conf->sub_lengths = prev->sub_lengths; | |
762 conf->sub_values = prev->sub_values; | |
763 } | |
764 | |
765 if (conf->types == NULL) { | |
766 if (prev->types == NULL) { | |
767 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t)); | |
768 if (conf->types == NULL) { | |
769 return NGX_CONF_ERROR; | |
770 } | |
771 | |
772 type = ngx_array_push(conf->types); | |
773 if (type == NULL) { | |
774 return NGX_CONF_ERROR; | |
775 } | |
776 | |
777 type->len = sizeof("text/html") - 1; | |
778 type->data = (u_char *) "text/html"; | |
779 | |
780 } else { | |
781 conf->types = prev->types; | |
782 } | |
783 } | |
784 | |
785 return NGX_CONF_OK; | |
786 } | |
787 | |
788 | |
789 static ngx_int_t | |
790 ngx_http_sub_filter_init(ngx_conf_t *cf) | |
791 { | |
792 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
793 ngx_http_top_header_filter = ngx_http_sub_header_filter; | |
794 | |
795 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
796 ngx_http_top_body_filter = ngx_http_sub_body_filter; | |
797 | |
798 return NGX_OK; | |
799 } |