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
|
1620
|
372 if (ctx->sub.len) {
|
|
373 b->memory = 1;
|
|
374 b->pos = ctx->sub.data;
|
|
375 b->last = ctx->sub.data + ctx->sub.len;
|
|
376
|
|
377 } else {
|
|
378 b->sync = 1;
|
|
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
|
1620
|
565 ctx->state = state;
|
1172
|
566 ctx->pos = p;
|
|
567 ctx->looked = looked;
|
|
568 ctx->copy_end = p;
|
|
569
|
|
570 if (ctx->copy_start == NULL) {
|
|
571 ctx->copy_start = ctx->buf->pos;
|
|
572 }
|
|
573
|
|
574 return NGX_AGAIN;
|
|
575
|
|
576 match_started:
|
|
577
|
|
578 continue;
|
|
579 }
|
|
580
|
|
581 /* state == sub_match_state */
|
|
582
|
|
583 if (ch == ctx->match.data[looked]) {
|
|
584 looked++;
|
|
585
|
|
586 if (looked == ctx->match.len) {
|
1620
|
587 if ((size_t) (p - ctx->pos) < looked) {
|
|
588 ctx->saved = 0;
|
|
589 }
|
|
590
|
1172
|
591 ctx->state = sub_start_state;
|
|
592 ctx->pos = p + 1;
|
|
593 ctx->looked = looked;
|
|
594 ctx->copy_end = copy_end;
|
|
595
|
|
596 if (ctx->copy_start == NULL && copy_end) {
|
|
597 ctx->copy_start = ctx->buf->pos;
|
|
598 }
|
|
599
|
|
600 return NGX_OK;
|
|
601 }
|
|
602
|
|
603 } else if (ch == ctx->match.data[0]) {
|
|
604 copy_end = p;
|
|
605 looked = 1;
|
|
606
|
|
607 } else {
|
|
608 copy_end = p;
|
|
609 looked = 0;
|
|
610 state = sub_start_state;
|
|
611 }
|
|
612 }
|
|
613
|
|
614 ctx->state = state;
|
|
615 ctx->pos = p;
|
|
616 ctx->looked = looked;
|
|
617
|
|
618 ctx->copy_end = (state == sub_start_state) ? p : copy_end;
|
|
619
|
|
620 if (ctx->copy_start == NULL && ctx->copy_end) {
|
|
621 ctx->copy_start = ctx->buf->pos;
|
|
622 }
|
|
623
|
|
624 return NGX_AGAIN;
|
|
625 }
|
|
626
|
|
627
|
|
628 static char *
|
|
629 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
630 {
|
|
631 ngx_http_sub_loc_conf_t *slcf = conf;
|
|
632
|
|
633 ngx_str_t *value;
|
|
634 ngx_int_t n;
|
|
635 ngx_uint_t i;
|
|
636 ngx_http_script_compile_t sc;
|
|
637
|
|
638 if (slcf->match.len) {
|
|
639 return "is duplicate";
|
|
640 }
|
|
641
|
|
642 value = cf->args->elts;
|
|
643
|
|
644 slcf->match = value[1];
|
|
645
|
|
646 for (i = 0; i < value[1].len; i++) {
|
|
647 value[1].data[i] = ngx_tolower(value[1].data[i]);
|
|
648 }
|
|
649
|
|
650 n = ngx_http_script_variables_count(&value[2]);
|
|
651
|
|
652 if (n == 0) {
|
|
653 slcf->sub = value[2];
|
|
654 return NGX_CONF_OK;
|
|
655 }
|
|
656
|
|
657 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
658
|
|
659 sc.cf = cf;
|
|
660 sc.source = &value[2];
|
|
661 sc.lengths = &slcf->sub_lengths;
|
|
662 sc.values = &slcf->sub_values;
|
|
663 sc.variables = n;
|
|
664 sc.complete_lengths = 1;
|
|
665 sc.complete_values = 1;
|
|
666
|
|
667 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
668 return NGX_CONF_ERROR;
|
|
669 }
|
|
670
|
|
671 return NGX_CONF_OK;
|
|
672 }
|
|
673
|
|
674
|
|
675 static char *
|
|
676 ngx_http_sub_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
677 {
|
|
678 ngx_http_sub_loc_conf_t *slcf = conf;
|
|
679
|
|
680 ngx_str_t *value, *type;
|
|
681 ngx_uint_t i;
|
|
682
|
|
683 if (slcf->types == NULL) {
|
|
684 slcf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
|
|
685 if (slcf->types == NULL) {
|
|
686 return NGX_CONF_ERROR;
|
|
687 }
|
|
688
|
|
689 type = ngx_array_push(slcf->types);
|
|
690 if (type == NULL) {
|
|
691 return NGX_CONF_ERROR;
|
|
692 }
|
|
693
|
|
694 type->len = sizeof("text/html") - 1;
|
|
695 type->data = (u_char *) "text/html";
|
|
696 }
|
|
697
|
|
698 value = cf->args->elts;
|
|
699
|
|
700 for (i = 1; i < cf->args->nelts; i++) {
|
|
701
|
|
702 if (ngx_strcmp(value[i].data, "text/html") == 0) {
|
|
703 continue;
|
|
704 }
|
|
705
|
|
706 type = ngx_array_push(slcf->types);
|
|
707 if (type == NULL) {
|
|
708 return NGX_CONF_ERROR;
|
|
709 }
|
|
710
|
|
711 type->len = value[i].len;
|
|
712
|
|
713 type->data = ngx_palloc(cf->pool, type->len + 1);
|
|
714 if (type->data == NULL) {
|
|
715 return NGX_CONF_ERROR;
|
|
716 }
|
|
717
|
|
718 ngx_cpystrn(type->data, value[i].data, type->len + 1);
|
|
719 }
|
|
720
|
|
721 return NGX_CONF_OK;
|
|
722 }
|
|
723
|
|
724
|
|
725 static void *
|
|
726 ngx_http_sub_create_conf(ngx_conf_t *cf)
|
|
727 {
|
|
728 ngx_http_sub_loc_conf_t *slcf;
|
|
729
|
|
730 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t));
|
|
731 if (slcf == NULL) {
|
|
732 return NGX_CONF_ERROR;
|
|
733 }
|
|
734
|
|
735 /*
|
|
736 * set by ngx_pcalloc():
|
|
737 *
|
|
738 * conf->match.len = 0;
|
|
739 * conf->match.data = NULL;
|
|
740 * conf->sub.len = 0;
|
|
741 * conf->sub.data = NULL;
|
|
742 * conf->sub_lengths = NULL;
|
|
743 * conf->sub_values = NULL;
|
|
744 * conf->types = NULL;
|
|
745 */
|
|
746
|
|
747 slcf->once = NGX_CONF_UNSET;
|
|
748
|
|
749 return slcf;
|
|
750 }
|
|
751
|
|
752
|
|
753 static char *
|
|
754 ngx_http_sub_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
755 {
|
|
756 ngx_http_sub_loc_conf_t *prev = parent;
|
|
757 ngx_http_sub_loc_conf_t *conf = child;
|
|
758
|
|
759 ngx_str_t *type;
|
|
760
|
|
761 ngx_conf_merge_value(conf->once, prev->once, 1);
|
|
762 ngx_conf_merge_str_value(conf->match, prev->match, "");
|
|
763
|
|
764 if (conf->sub.data == NULL && conf->sub_lengths == NULL) {
|
|
765 conf->sub = prev->sub;
|
|
766 conf->sub_lengths = prev->sub_lengths;
|
|
767 conf->sub_values = prev->sub_values;
|
|
768 }
|
|
769
|
|
770 if (conf->types == NULL) {
|
|
771 if (prev->types == NULL) {
|
|
772 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
|
|
773 if (conf->types == NULL) {
|
|
774 return NGX_CONF_ERROR;
|
|
775 }
|
|
776
|
|
777 type = ngx_array_push(conf->types);
|
|
778 if (type == NULL) {
|
|
779 return NGX_CONF_ERROR;
|
|
780 }
|
|
781
|
|
782 type->len = sizeof("text/html") - 1;
|
|
783 type->data = (u_char *) "text/html";
|
|
784
|
|
785 } else {
|
|
786 conf->types = prev->types;
|
|
787 }
|
|
788 }
|
|
789
|
|
790 return NGX_CONF_OK;
|
|
791 }
|
|
792
|
|
793
|
|
794 static ngx_int_t
|
|
795 ngx_http_sub_filter_init(ngx_conf_t *cf)
|
|
796 {
|
|
797 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
798 ngx_http_top_header_filter = ngx_http_sub_header_filter;
|
|
799
|
|
800 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
801 ngx_http_top_body_filter = ngx_http_sub_body_filter;
|
|
802
|
|
803 return NGX_OK;
|
|
804 }
|