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