annotate src/http/modules/ngx_http_sub_filter_module.c @ 6757:cc327c3caf4a stable-1.10

Sub filter: eliminate unnecessary buffering. Previously, when a buffer was processed by the sub filter, its final bytes could be buffered by the filter even if they don't match any pattern. This happened because the Boyer-Moore algorithm, employed by the sub filter since b9447fc457b4 (1.9.4), matches the last characters of patterns prior to checking other characters. If the last character is out of scope, initial bytes of a potential match are buffered until the last character is available. Now, after receiving a flush or recycled buffer, the filter performs additional checks to reduce the number of buffered bytes. The potential match is checked against the initial parts of all patterns. Non-matching bytes are not buffered. This improves processing of a chunked response from upstream by sending the entire chunks without buffering unless a partial match is found at the end of a chunk.
author Roman Arutyunyan <arut@nginx.com>
date Sat, 02 Jul 2016 15:59:53 +0300
parents 521f5aa6e8fb
children f18c285c2e59
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
2 /*
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
4412
d620f497c50f Copyright updated.
Maxim Konovalov <maxim@nginx.com>
parents: 3675
diff changeset
4 * Copyright (C) Nginx, Inc.
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
5 */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
6
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
7
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
8 #include <ngx_config.h>
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
9 #include <ngx_core.h>
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
10 #include <ngx_http.h>
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
11
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
12
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
13 typedef struct {
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
14 ngx_http_complex_value_t match;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
15 ngx_http_complex_value_t value;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
16 } ngx_http_sub_pair_t;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
17
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
18
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
19 typedef struct {
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
20 ngx_str_t match;
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
21 ngx_http_complex_value_t *value;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
22 } ngx_http_sub_match_t;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
23
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
24
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
25 typedef struct {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
26 ngx_uint_t min_match_len;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
27 ngx_uint_t max_match_len;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
28
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
29 u_char index[257];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
30 u_char shift[256];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
31 } ngx_http_sub_tables_t;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
32
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
33
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
34 typedef struct {
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
35 ngx_uint_t dynamic; /* unsigned dynamic:1; */
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
36
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
37 ngx_array_t *pairs;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
38
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
39 ngx_http_sub_tables_t *tables;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
40
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
41 ngx_hash_t types;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
42
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
43 ngx_flag_t once;
5229
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
44 ngx_flag_t last_modified;
2166
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
45
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
46 ngx_array_t *types_keys;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
47 ngx_array_t *matches;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
48 } ngx_http_sub_loc_conf_t;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
49
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
50
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
51 typedef struct {
3675
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
52 ngx_str_t saved;
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
53 ngx_str_t looked;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
54
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
55 ngx_uint_t once; /* unsigned once:1 */
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
56
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
57 ngx_buf_t *buf;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
58
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
59 u_char *pos;
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
60 u_char *copy_start;
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
61 u_char *copy_end;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
62
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
63 ngx_chain_t *in;
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
64 ngx_chain_t *out;
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
65 ngx_chain_t **last_out;
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
66 ngx_chain_t *busy;
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
67 ngx_chain_t *free;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
68
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
69 ngx_str_t *sub;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
70 ngx_uint_t applied;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
71
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
72 ngx_int_t offset;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
73 ngx_uint_t index;
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
74
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
75 ngx_http_sub_tables_t *tables;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
76 ngx_array_t *matches;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
77 } ngx_http_sub_ctx_t;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
78
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
79
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
80 static ngx_uint_t ngx_http_sub_cmp_index;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
81
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
82
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
83 static ngx_int_t ngx_http_sub_output(ngx_http_request_t *r,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
84 ngx_http_sub_ctx_t *ctx);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
85 static ngx_int_t ngx_http_sub_parse(ngx_http_request_t *r,
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
86 ngx_http_sub_ctx_t *ctx, ngx_uint_t flush);
6756
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
87 static ngx_int_t ngx_http_sub_match(ngx_http_sub_ctx_t *ctx, ngx_int_t start,
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
88 ngx_str_t *m);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
89
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
90 static char * ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
91 void *conf);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
92 static void *ngx_http_sub_create_conf(ngx_conf_t *cf);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
93 static char *ngx_http_sub_merge_conf(ngx_conf_t *cf,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
94 void *parent, void *child);
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
95 static void ngx_http_sub_init_tables(ngx_http_sub_tables_t *tables,
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
96 ngx_http_sub_match_t *match, ngx_uint_t n);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
97 static ngx_int_t ngx_http_sub_cmp_matches(const void *one, const void *two);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
98 static ngx_int_t ngx_http_sub_filter_init(ngx_conf_t *cf);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
99
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
100
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
101 static ngx_command_t ngx_http_sub_filter_commands[] = {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
102
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
103 { ngx_string("sub_filter"),
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
104 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
105 ngx_http_sub_filter,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
106 NGX_HTTP_LOC_CONF_OFFSET,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
107 0,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
108 NULL },
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
109
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
110 { ngx_string("sub_filter_types"),
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
111 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
2166
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
112 ngx_http_types_slot,
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
113 NGX_HTTP_LOC_CONF_OFFSET,
2166
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
114 offsetof(ngx_http_sub_loc_conf_t, types_keys),
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
115 &ngx_http_html_default_types[0] },
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
116
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
117 { ngx_string("sub_filter_once"),
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
118 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
119 ngx_conf_set_flag_slot,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
120 NGX_HTTP_LOC_CONF_OFFSET,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
121 offsetof(ngx_http_sub_loc_conf_t, once),
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
122 NULL },
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
123
5229
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
124 { ngx_string("sub_filter_last_modified"),
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
125 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
126 ngx_conf_set_flag_slot,
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
127 NGX_HTTP_LOC_CONF_OFFSET,
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
128 offsetof(ngx_http_sub_loc_conf_t, last_modified),
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
129 NULL },
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
130
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
131 ngx_null_command
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
132 };
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
133
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
134
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
135 static ngx_http_module_t ngx_http_sub_filter_module_ctx = {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
136 NULL, /* preconfiguration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
137 ngx_http_sub_filter_init, /* postconfiguration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
138
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
139 NULL, /* create main configuration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
140 NULL, /* init main configuration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
141
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
142 NULL, /* create server configuration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
143 NULL, /* merge server configuration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
144
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
145 ngx_http_sub_create_conf, /* create location configuration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
146 ngx_http_sub_merge_conf /* merge location configuration */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
147 };
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
148
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
149
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
150 ngx_module_t ngx_http_sub_filter_module = {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
151 NGX_MODULE_V1,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
152 &ngx_http_sub_filter_module_ctx, /* module context */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
153 ngx_http_sub_filter_commands, /* module directives */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
154 NGX_HTTP_MODULE, /* module type */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
155 NULL, /* init master */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
156 NULL, /* init module */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
157 NULL, /* init process */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
158 NULL, /* init thread */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
159 NULL, /* exit thread */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
160 NULL, /* exit process */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
161 NULL, /* exit master */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
162 NGX_MODULE_V1_PADDING
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
163 };
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
164
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
165
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
166 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
167 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
168
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
169
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
170 static ngx_int_t
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
171 ngx_http_sub_header_filter(ngx_http_request_t *r)
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
172 {
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
173 ngx_str_t *m;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
174 ngx_uint_t i, j, n;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
175 ngx_http_sub_ctx_t *ctx;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
176 ngx_http_sub_pair_t *pairs;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
177 ngx_http_sub_match_t *matches;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
178 ngx_http_sub_loc_conf_t *slcf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
179
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
180 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
181
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
182 if (slcf->pairs == NULL
2166
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
183 || r->headers_out.content_length_n == 0
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
184 || ngx_http_test_content_type(r, &slcf->types) == NULL)
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
185 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
186 return ngx_http_next_header_filter(r);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
187 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
188
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
189 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_sub_ctx_t));
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
190 if (ctx == NULL) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
191 return NGX_ERROR;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
192 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
193
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
194 if (slcf->dynamic == 0) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
195 ctx->tables = slcf->tables;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
196 ctx->matches = slcf->matches;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
197
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
198 } else {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
199 pairs = slcf->pairs->elts;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
200 n = slcf->pairs->nelts;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
201
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
202 matches = ngx_pcalloc(r->pool, sizeof(ngx_http_sub_match_t) * n);
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
203 if (matches == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
204 return NGX_ERROR;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
205 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
206
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
207 j = 0;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
208 for (i = 0; i < n; i++) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
209 matches[j].value = &pairs[i].value;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
210
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
211 if (pairs[i].match.lengths == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
212 matches[j].match = pairs[i].match.value;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
213 j++;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
214 continue;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
215 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
216
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
217 m = &matches[j].match;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
218 if (ngx_http_complex_value(r, &pairs[i].match, m) != NGX_OK) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
219 return NGX_ERROR;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
220 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
221
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
222 if (m->len == 0) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
223 continue;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
224 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
225
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
226 ngx_strlow(m->data, m->data, m->len);
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
227 j++;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
228 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
230 if (j == 0) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
231 return ngx_http_next_header_filter(r);
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
232 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
233
6464
088ef087a011 Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents: 6247
diff changeset
234 ctx->matches = ngx_palloc(r->pool, sizeof(ngx_array_t));
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
235 if (ctx->matches == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
236 return NGX_ERROR;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
237 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
238
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
239 ctx->matches->elts = matches;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
240 ctx->matches->nelts = j;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
241
6464
088ef087a011 Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents: 6247
diff changeset
242 ctx->tables = ngx_palloc(r->pool, sizeof(ngx_http_sub_tables_t));
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
243 if (ctx->tables == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
244 return NGX_ERROR;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
245 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
246
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
247 ngx_http_sub_init_tables(ctx->tables, ctx->matches->elts,
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
248 ctx->matches->nelts);
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
249 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
250
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
251 ngx_http_set_ctx(r, ctx, ngx_http_sub_filter_module);
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
252
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
253 ctx->saved.data = ngx_pnalloc(r->pool, ctx->tables->max_match_len - 1);
3675
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
254 if (ctx->saved.data == NULL) {
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
255 return NGX_ERROR;
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
256 }
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
257
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
258 ctx->looked.data = ngx_pnalloc(r->pool, ctx->tables->max_match_len - 1);
3675
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
259 if (ctx->looked.data == NULL) {
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
260 return NGX_ERROR;
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
261 }
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
262
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
263 ctx->offset = ctx->tables->min_match_len - 1;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
264 ctx->last_out = &ctx->out;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
265
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
266 r->filter_need_in_memory = 1;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
267
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
268 if (r == r->main) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
269 ngx_http_clear_content_length(r);
5229
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
270
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
271 if (!slcf->last_modified) {
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
272 ngx_http_clear_last_modified(r);
5733
e491b26fa5a1 Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5710
diff changeset
273 ngx_http_clear_etag(r);
e491b26fa5a1 Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5710
diff changeset
274
e491b26fa5a1 Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5710
diff changeset
275 } else {
e491b26fa5a1 Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5710
diff changeset
276 ngx_http_weak_etag(r);
5229
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
277 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
278 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
279
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
280 return ngx_http_next_header_filter(r);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
281 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
282
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
283
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
284 static ngx_int_t
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
285 ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
286 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
287 ngx_int_t rc;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
288 ngx_buf_t *b;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
289 ngx_str_t *sub;
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
290 ngx_uint_t flush, last;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
291 ngx_chain_t *cl;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
292 ngx_http_sub_ctx_t *ctx;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
293 ngx_http_sub_match_t *match;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
294 ngx_http_sub_loc_conf_t *slcf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
295
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
296 ctx = ngx_http_get_module_ctx(r, ngx_http_sub_filter_module);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
297
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
298 if (ctx == NULL) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
299 return ngx_http_next_body_filter(r, in);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
300 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
301
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
302 if ((in == NULL
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
303 && ctx->buf == NULL
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
304 && ctx->in == NULL
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
305 && ctx->busy == NULL))
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
306 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
307 return ngx_http_next_body_filter(r, in);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
308 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
309
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
310 if (ctx->once && (ctx->buf == NULL || ctx->in == NULL)) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
311
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
312 if (ctx->busy) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
313 if (ngx_http_sub_output(r, ctx) == NGX_ERROR) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
314 return NGX_ERROR;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
315 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
316 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
317
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
318 return ngx_http_next_body_filter(r, in);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
319 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
320
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
321 /* add the incoming chain to the chain ctx->in */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
322
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
323 if (in) {
2536
a6d6d762c554 small optimization: " == NGX_ERROR" > " != NGX_OK"
Igor Sysoev <igor@sysoev.ru>
parents: 2414
diff changeset
324 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
325 return NGX_ERROR;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
326 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
327 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
328
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
329 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
330 "http sub filter \"%V\"", &r->uri);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
331
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
332 flush = 0;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
333 last = 0;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
334
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
335 while (ctx->in || ctx->buf) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
336
3642
ac33852faaac style fix
Igor Sysoev <igor@sysoev.ru>
parents: 3372
diff changeset
337 if (ctx->buf == NULL) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
338 ctx->buf = ctx->in->buf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
339 ctx->in = ctx->in->next;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
340 ctx->pos = ctx->buf->pos;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
341 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
342
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
343 if (ctx->buf->flush || ctx->buf->recycled) {
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
344 flush = 1;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
345 }
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
346
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
347 if (ctx->in == NULL) {
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
348 last = flush;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
349 }
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
350
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
351 b = NULL;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
352
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
353 while (ctx->pos < ctx->buf->last) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
354
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
355 rc = ngx_http_sub_parse(r, ctx, last);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
356
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
357 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
6480
f01ab2dbcfdc Fixed logging.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6464
diff changeset
358 "parse: %i, looked: \"%V\" %p-%p",
3675
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
359 rc, &ctx->looked, ctx->copy_start, ctx->copy_end);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
360
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
361 if (rc == NGX_ERROR) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
362 return rc;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
363 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
364
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
365 if (ctx->saved.len) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
366
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
367 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
3675
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
368 "saved: \"%V\"", &ctx->saved);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
369
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
370 cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
371 if (cl == NULL) {
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
372 return NGX_ERROR;
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
373 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
374
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
375 b = cl->buf;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
376
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
377 ngx_memzero(b, sizeof(ngx_buf_t));
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
378
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
379 b->pos = ngx_pnalloc(r->pool, ctx->saved.len);
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
380 if (b->pos == NULL) {
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
381 return NGX_ERROR;
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
382 }
3675
08d7165b6be1 fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
383
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
384 ngx_memcpy(b->pos, ctx->saved.data, ctx->saved.len);
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
385 b->last = b->pos + ctx->saved.len;
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
386 b->memory = 1;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
387
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
388 *ctx->last_out = cl;
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
389 ctx->last_out = &cl->next;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
390
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
391 ctx->saved.len = 0;
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
392 }
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
393
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
394 if (ctx->copy_start != ctx->copy_end) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
395
5285
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
396 cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
397 if (cl == NULL) {
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
398 return NGX_ERROR;
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
399 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
400
5285
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
401 b = cl->buf;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
402
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
403 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
404
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
405 b->pos = ctx->copy_start;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
406 b->last = ctx->copy_end;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
407 b->shadow = NULL;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
408 b->last_buf = 0;
5710
9e9c1310d17c Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5288
diff changeset
409 b->last_in_chain = 0;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
410 b->recycled = 0;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
411
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
412 if (b->in_file) {
2079
333ef9e18a59 fix error when response parsed by sub filter, then by SSI filter
Igor Sysoev <igor@sysoev.ru>
parents: 2049
diff changeset
413 b->file_last = b->file_pos + (b->last - ctx->buf->pos);
333ef9e18a59 fix error when response parsed by sub filter, then by SSI filter
Igor Sysoev <igor@sysoev.ru>
parents: 2049
diff changeset
414 b->file_pos += b->pos - ctx->buf->pos;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
415 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
416
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
417 *ctx->last_out = cl;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
418 ctx->last_out = &cl->next;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
419 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
420
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
421 if (rc == NGX_AGAIN) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
422 continue;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
423 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
424
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
425
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
426 /* rc == NGX_OK */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
427
5285
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
428 cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
429 if (cl == NULL) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
430 return NGX_ERROR;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
431 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
432
5285
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
433 b = cl->buf;
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
434
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
435 ngx_memzero(b, sizeof(ngx_buf_t));
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
436
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
437 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
438
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
439 if (ctx->sub == NULL) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
440 ctx->sub = ngx_pcalloc(r->pool, sizeof(ngx_str_t)
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
441 * ctx->matches->nelts);
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
442 if (ctx->sub == NULL) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
443 return NGX_ERROR;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
444 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
445 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
446
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
447 sub = &ctx->sub[ctx->index];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
448
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
449 if (sub->data == NULL) {
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
450 match = ctx->matches->elts;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
451
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
452 if (ngx_http_complex_value(r, match[ctx->index].value, sub)
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
453 != NGX_OK)
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
454 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
455 return NGX_ERROR;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
456 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
457 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
458
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
459 if (sub->len) {
1555
76fe59c6fafb fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 1172
diff changeset
460 b->memory = 1;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
461 b->pos = sub->data;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
462 b->last = sub->data + sub->len;
1555
76fe59c6fafb fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 1172
diff changeset
463
76fe59c6fafb fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 1172
diff changeset
464 } else {
76fe59c6fafb fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 1172
diff changeset
465 b->sync = 1;
76fe59c6fafb fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents: 1172
diff changeset
466 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
467
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
468 *ctx->last_out = cl;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
469 ctx->last_out = &cl->next;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
470
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
471 ctx->index = 0;
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
472 ctx->once = slcf->once && (++ctx->applied == ctx->matches->nelts);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
473
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
474 continue;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
475 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
476
5710
9e9c1310d17c Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5288
diff changeset
477 if (ctx->looked.len
9e9c1310d17c Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5288
diff changeset
478 && (ctx->buf->last_buf || ctx->buf->last_in_chain))
9e9c1310d17c Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5288
diff changeset
479 {
5287
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
480 cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
481 if (cl == NULL) {
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
482 return NGX_ERROR;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
483 }
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
484
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
485 b = cl->buf;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
486
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
487 ngx_memzero(b, sizeof(ngx_buf_t));
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
488
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
489 b->pos = ctx->looked.data;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
490 b->last = b->pos + ctx->looked.len;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
491 b->memory = 1;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
492
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
493 *ctx->last_out = cl;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
494 ctx->last_out = &cl->next;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
495
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
496 ctx->looked.len = 0;
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
497 }
2dbc5e38b65d Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5286
diff changeset
498
5710
9e9c1310d17c Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5288
diff changeset
499 if (ctx->buf->last_buf || ctx->buf->flush || ctx->buf->sync
5286
819c5b53d8b5 Sub filter: flush buffers handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5285
diff changeset
500 || ngx_buf_in_memory(ctx->buf))
819c5b53d8b5 Sub filter: flush buffers handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5285
diff changeset
501 {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
502 if (b == NULL) {
5285
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
503 cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
504 if (cl == NULL) {
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
505 return NGX_ERROR;
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
506 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
507
5285
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
508 b = cl->buf;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
509
5285
d47ef93134e5 Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents: 5284
diff changeset
510 ngx_memzero(b, sizeof(ngx_buf_t));
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
511
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
512 b->sync = 1;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
513
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
514 *ctx->last_out = cl;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
515 ctx->last_out = &cl->next;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
516 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
517
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
518 b->last_buf = ctx->buf->last_buf;
5710
9e9c1310d17c Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5288
diff changeset
519 b->last_in_chain = ctx->buf->last_in_chain;
5286
819c5b53d8b5 Sub filter: flush buffers handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5285
diff changeset
520 b->flush = ctx->buf->flush;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
521 b->shadow = ctx->buf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
522
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
523 b->recycled = ctx->buf->recycled;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
524 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
525
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
526 ctx->buf = NULL;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
527 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
528
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
529 if (ctx->out == NULL && ctx->busy == NULL) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
530 return NGX_OK;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
531 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
532
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
533 return ngx_http_sub_output(r, ctx);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
534 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
535
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
536
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
537 static ngx_int_t
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
538 ngx_http_sub_output(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx)
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
539 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
540 ngx_int_t rc;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
541 ngx_buf_t *b;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
542 ngx_chain_t *cl;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
543
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
544 #if 1
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
545 b = NULL;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
546 for (cl = ctx->out; cl; cl = cl->next) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
547 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
548 "sub out: %p %p", cl->buf, cl->buf->pos);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
549 if (cl->buf == b) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
550 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
551 "the same buf was used in sub");
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
552 ngx_debug_point();
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
553 return NGX_ERROR;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
554 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
555 b = cl->buf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
556 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
557 #endif
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
558
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
559 rc = ngx_http_next_body_filter(r, ctx->out);
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
560
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
561 if (ctx->busy == NULL) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
562 ctx->busy = ctx->out;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
563
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
564 } else {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
565 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
566 cl->next = ctx->out;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
567 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
568
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
569 ctx->out = NULL;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
570 ctx->last_out = &ctx->out;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
571
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
572 while (ctx->busy) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
573
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
574 cl = ctx->busy;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
575 b = cl->buf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
576
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
577 if (ngx_buf_size(b) != 0) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
578 break;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
579 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
580
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
581 if (b->shadow) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
582 b->shadow->pos = b->shadow->last;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
583 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
584
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
585 ctx->busy = cl->next;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
586
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
587 if (ngx_buf_in_memory(b) || b->in_file) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
588 /* add data bufs only to the free buf chain */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
589
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
590 cl->next = ctx->free;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
591 ctx->free = cl;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
592 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
593 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
594
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
595 if (ctx->in || ctx->buf) {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
596 r->buffered |= NGX_HTTP_SUB_BUFFERED;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
597
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
598 } else {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
599 r->buffered &= ~NGX_HTTP_SUB_BUFFERED;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
600 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
601
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
602 return rc;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
603 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
604
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
605
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
606 static ngx_int_t
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
607 ngx_http_sub_parse(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx,
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
608 ngx_uint_t flush)
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
609 {
6756
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
610 u_char *p, c;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
611 ngx_str_t *m;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
612 ngx_int_t offset, start, next, end, len, rc;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
613 ngx_uint_t shift, i, j;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
614 ngx_http_sub_match_t *match;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
615 ngx_http_sub_tables_t *tables;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
616 ngx_http_sub_loc_conf_t *slcf;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
617
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
618 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
619 tables = ctx->tables;
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
620 match = ctx->matches->elts;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
621
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
622 offset = ctx->offset;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
623 end = ctx->buf->last - ctx->pos;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
624
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
625 if (ctx->once) {
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
626 /* sets start and next to end */
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
627 offset = end + (ngx_int_t) tables->min_match_len - 1;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
628 goto again;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
629 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
630
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
631 while (offset < end) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
632
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
633 c = offset < 0 ? ctx->looked.data[ctx->looked.len + offset]
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
634 : ctx->pos[offset];
5810
5322be87fc02 Sub filter: fixed matching for a single character.
Valentin Bartenev <vbart@nginx.com>
parents: 5733
diff changeset
635
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
636 c = ngx_tolower(c);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
637
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
638 shift = tables->shift[c];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
639 if (shift > 0) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
640 offset += shift;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
641 continue;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
642 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
643
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
644 /* a potential match */
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
645
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
646 start = offset - (ngx_int_t) tables->min_match_len + 1;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
647
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
648 i = ngx_max(tables->index[c], ctx->index);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
649 j = tables->index[c + 1];
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
650
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
651 while (i != j) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
652
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
653 if (slcf->once && ctx->sub && ctx->sub[i].data) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
654 goto next;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
655 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
656
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
657 m = &match[i].match;
1557
9d094e581587 *) add sub_filter parser fix similar to r1261 in SSI parser
Igor Sysoev <igor@sysoev.ru>
parents: 1555
diff changeset
658
6756
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
659 rc = ngx_http_sub_match(ctx, start, m);
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
660
6756
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
661 if (rc == NGX_DECLINED) {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
662 goto next;
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
663 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
664
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
665 ctx->index = i;
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
666
6756
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
667 if (rc == NGX_AGAIN) {
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
668 goto again;
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
669 }
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
670
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
671 ctx->offset = offset + (ngx_int_t) m->len;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
672 next = start + (ngx_int_t) m->len;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
673 end = ngx_max(next, 0);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
674 rc = NGX_OK;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
675
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
676 goto done;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
677
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
678 next:
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
679
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
680 i++;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
681 }
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
682
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
683 offset++;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
684 ctx->index = 0;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
685 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
686
6757
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
687 if (flush) {
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
688 for ( ;; ) {
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
689 start = offset - (ngx_int_t) tables->min_match_len + 1;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
690
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
691 if (start >= end) {
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
692 break;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
693 }
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
694
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
695 for (i = 0; i < ctx->matches->nelts; i++) {
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
696 m = &match[i].match;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
697
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
698 if (ngx_http_sub_match(ctx, start, m) == NGX_AGAIN) {
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
699 goto again;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
700 }
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
701 }
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
702
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
703 offset++;
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
704 }
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
705 }
cc327c3caf4a Sub filter: eliminate unnecessary buffering.
Roman Arutyunyan <arut@nginx.com>
parents: 6756
diff changeset
706
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
707 again:
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
708
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
709 ctx->offset = offset;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
710 start = offset - (ngx_int_t) tables->min_match_len + 1;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
711 next = start;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
712 rc = NGX_AGAIN;
5288
102d7117ffb8 Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5287
diff changeset
713
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
714 done:
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
715
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
716 /* send [ - looked.len, start ] to client */
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
717
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
718 ctx->saved.len = ctx->looked.len + ngx_min(start, 0);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
719 ngx_memcpy(ctx->saved.data, ctx->looked.data, ctx->saved.len);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
720
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
721 ctx->copy_start = ctx->pos;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
722 ctx->copy_end = ctx->pos + ngx_max(start, 0);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
723
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
724 /* save [ next, end ] in looked */
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
725
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
726 len = ngx_min(next, 0);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
727 p = ctx->looked.data;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
728 p = ngx_movemem(p, p + ctx->looked.len + len, - len);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
729
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
730 len = ngx_max(next, 0);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
731 p = ngx_cpymem(p, ctx->pos + len, end - len);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
732 ctx->looked.len = p - ctx->looked.data;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
733
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
734 /* update position */
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
735
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
736 ctx->pos += end;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
737 ctx->offset -= end;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
738
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
739 return rc;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
740 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
741
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
742
6756
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
743 static ngx_int_t
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
744 ngx_http_sub_match(ngx_http_sub_ctx_t *ctx, ngx_int_t start, ngx_str_t *m)
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
745 {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
746 u_char *p, *last, *pat, *pat_end;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
747
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
748 pat = m->data;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
749 pat_end = m->data + m->len;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
750
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
751 if (start >= 0) {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
752 p = ctx->pos + start;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
753
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
754 } else {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
755 last = ctx->looked.data + ctx->looked.len;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
756 p = last + start;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
757
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
758 while (p < last && pat < pat_end) {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
759 if (ngx_tolower(*p) != *pat) {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
760 return NGX_DECLINED;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
761 }
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
762
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
763 p++;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
764 pat++;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
765 }
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
766
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
767 p = ctx->pos;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
768 }
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
769
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
770 while (p < ctx->buf->last && pat < pat_end) {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
771 if (ngx_tolower(*p) != *pat) {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
772 return NGX_DECLINED;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
773 }
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
774
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
775 p++;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
776 pat++;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
777 }
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
778
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
779 if (pat != pat_end) {
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
780 /* partial match */
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
781 return NGX_AGAIN;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
782 }
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
783
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
784 return NGX_OK;
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
785 }
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
786
521f5aa6e8fb Sub filter: introduced the ngx_http_sub_match() function.
Roman Arutyunyan <arut@nginx.com>
parents: 6480
diff changeset
787
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
788 static char *
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
789 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
790 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
791 ngx_http_sub_loc_conf_t *slcf = conf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
792
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
793 ngx_str_t *value;
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
794 ngx_http_sub_pair_t *pair;
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
795 ngx_http_compile_complex_value_t ccv;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
796
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
797 value = cf->args->elts;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
798
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
799 if (value[1].len == 0) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
800 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty search pattern");
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
801 return NGX_CONF_ERROR;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
802 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
803
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
804 if (slcf->pairs == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
805 slcf->pairs = ngx_array_create(cf->pool, 1,
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
806 sizeof(ngx_http_sub_pair_t));
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
807 if (slcf->pairs == NULL) {
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
808 return NGX_CONF_ERROR;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
809 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
810 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
811
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
812 if (slcf->pairs->nelts == 255) {
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
813 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
814 "number of search patterns exceeds 255");
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
815 return NGX_CONF_ERROR;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
816 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
817
2135
8c6521eedf84 ngx_strlow()
Igor Sysoev <igor@sysoev.ru>
parents: 2079
diff changeset
818 ngx_strlow(value[1].data, value[1].data, value[1].len);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
819
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
820 pair = ngx_array_push(slcf->pairs);
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
821 if (pair == NULL) {
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
822 return NGX_CONF_ERROR;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
823 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
824
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
825 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
826
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
827 ccv.cf = cf;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
828 ccv.value = &value[1];
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
829 ccv.complex_value = &pair->match;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
830
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
831 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
832 return NGX_CONF_ERROR;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
833 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
834
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
835 if (ccv.complex_value->lengths != NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
836 slcf->dynamic = 1;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
837
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
838 } else {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
839 ngx_strlow(pair->match.value.data, pair->match.value.data,
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
840 pair->match.value.len);
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
841 }
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
842
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
843 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
844
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
845 ccv.cf = cf;
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
846 ccv.value = &value[2];
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
847 ccv.complex_value = &pair->value;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
848
2588
a6954ce88b80 use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents: 2536
diff changeset
849 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
850 return NGX_CONF_ERROR;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
851 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
852
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
853 return NGX_CONF_OK;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
854 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
855
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
856
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
857 static void *
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
858 ngx_http_sub_create_conf(ngx_conf_t *cf)
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
859 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
860 ngx_http_sub_loc_conf_t *slcf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
861
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
862 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t));
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
863 if (slcf == NULL) {
2912
c7d57b539248 return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents: 2588
diff changeset
864 return NULL;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
865 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
866
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
867 /*
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
868 * set by ngx_pcalloc():
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
869 *
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
870 * conf->dynamic = 0;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
871 * conf->pairs = NULL;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
872 * conf->tables = NULL;
2166
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
873 * conf->types = { NULL };
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
874 * conf->types_keys = NULL;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
875 * conf->matches = NULL;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
876 */
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
877
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
878 slcf->once = NGX_CONF_UNSET;
5229
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
879 slcf->last_modified = NGX_CONF_UNSET;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
880
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
881 return slcf;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
882 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
883
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
884
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
885 static char *
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
886 ngx_http_sub_merge_conf(ngx_conf_t *cf, void *parent, void *child)
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
887 {
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
888 ngx_uint_t i, n;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
889 ngx_http_sub_pair_t *pairs;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
890 ngx_http_sub_match_t *matches;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
891 ngx_http_sub_loc_conf_t *prev = parent;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
892 ngx_http_sub_loc_conf_t *conf = child;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
893
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
894 ngx_conf_merge_value(conf->once, prev->once, 1);
5229
4c1a604b0285 Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4947
diff changeset
895 ngx_conf_merge_value(conf->last_modified, prev->last_modified, 0);
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
896
3372
6b8e5c882e47 support "*" in gzip_types, ssi_types, etc
Igor Sysoev <igor@sysoev.ru>
parents: 2912
diff changeset
897 if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
6b8e5c882e47 support "*" in gzip_types, ssi_types, etc
Igor Sysoev <igor@sysoev.ru>
parents: 2912
diff changeset
898 &prev->types_keys, &prev->types,
2166
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
899 ngx_http_html_default_types)
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
900 != NGX_OK)
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
901 {
723df5089c05 use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents: 2135
diff changeset
902 return NGX_CONF_ERROR;
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
903 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
904
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
905 if (conf->pairs == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
906 conf->dynamic = prev->dynamic;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
907 conf->pairs = prev->pairs;
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
908 conf->matches = prev->matches;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
909 conf->tables = prev->tables;
6247
fbbb1c1ce1eb Sub filter: fixed initialization in http{} level (ticket #791).
Roman Arutyunyan <arut@nginx.com>
parents: 6229
diff changeset
910 }
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
911
6247
fbbb1c1ce1eb Sub filter: fixed initialization in http{} level (ticket #791).
Roman Arutyunyan <arut@nginx.com>
parents: 6229
diff changeset
912 if (conf->pairs && conf->dynamic == 0 && conf->tables == NULL) {
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
913 pairs = conf->pairs->elts;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
914 n = conf->pairs->nelts;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
915
6464
088ef087a011 Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents: 6247
diff changeset
916 matches = ngx_palloc(cf->pool, sizeof(ngx_http_sub_match_t) * n);
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
917 if (matches == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
918 return NGX_CONF_ERROR;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
919 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
920
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
921 for (i = 0; i < n; i++) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
922 matches[i].match = pairs[i].match.value;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
923 matches[i].value = &pairs[i].value;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
924 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
925
6464
088ef087a011 Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents: 6247
diff changeset
926 conf->matches = ngx_palloc(cf->pool, sizeof(ngx_array_t));
6229
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
927 if (conf->matches == NULL) {
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
928 return NGX_CONF_ERROR;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
929 }
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
930
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
931 conf->matches->elts = matches;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
932 conf->matches->nelts = n;
2c045e5b8291 Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6228
diff changeset
933
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
934 conf->tables = ngx_palloc(cf->pool, sizeof(ngx_http_sub_tables_t));
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
935 if (conf->tables == NULL) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
936 return NGX_CONF_ERROR;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
937 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
938
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
939 ngx_http_sub_init_tables(conf->tables, conf->matches->elts,
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
940 conf->matches->nelts);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
941 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
942
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
943 return NGX_CONF_OK;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
944 }
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
945
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
946
6228
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
947 static void
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
948 ngx_http_sub_init_tables(ngx_http_sub_tables_t *tables,
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
949 ngx_http_sub_match_t *match, ngx_uint_t n)
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
950 {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
951 u_char c;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
952 ngx_uint_t i, j, min, max, ch;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
953
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
954 min = match[0].match.len;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
955 max = match[0].match.len;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
956
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
957 for (i = 1; i < n; i++) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
958 min = ngx_min(min, match[i].match.len);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
959 max = ngx_max(max, match[i].match.len);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
960 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
961
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
962 tables->min_match_len = min;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
963 tables->max_match_len = max;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
964
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
965 ngx_http_sub_cmp_index = tables->min_match_len - 1;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
966 ngx_sort(match, n, sizeof(ngx_http_sub_match_t), ngx_http_sub_cmp_matches);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
967
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
968 min = ngx_min(min, 255);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
969 ngx_memset(tables->shift, min, 256);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
970
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
971 ch = 0;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
972
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
973 for (i = 0; i < n; i++) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
974
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
975 for (j = 0; j < min; j++) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
976 c = match[i].match.data[tables->min_match_len - 1 - j];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
977 tables->shift[c] = ngx_min(tables->shift[c], (u_char) j);
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
978 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
979
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
980 c = match[i].match.data[tables->min_match_len - 1];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
981 while (ch <= c) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
982 tables->index[ch++] = (u_char) i;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
983 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
984 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
985
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
986 while (ch < 257) {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
987 tables->index[ch++] = (u_char) n;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
988 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
989 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
990
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
991
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
992 static ngx_int_t
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
993 ngx_http_sub_cmp_matches(const void *one, const void *two)
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
994 {
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
995 ngx_int_t c1, c2;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
996 ngx_http_sub_match_t *first, *second;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
997
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
998 first = (ngx_http_sub_match_t *) one;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
999 second = (ngx_http_sub_match_t *) two;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1000
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1001 c1 = first->match.data[ngx_http_sub_cmp_index];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1002 c2 = second->match.data[ngx_http_sub_cmp_index];
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1003
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1004 return c1 - c2;
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1005 }
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1006
b9447fc457b4 Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 5810
diff changeset
1007
1172
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1008 static ngx_int_t
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1009 ngx_http_sub_filter_init(ngx_conf_t *cf)
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1010 {
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1011 ngx_http_next_header_filter = ngx_http_top_header_filter;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1012 ngx_http_top_header_filter = ngx_http_sub_header_filter;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1013
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1014 ngx_http_next_body_filter = ngx_http_top_body_filter;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1015 ngx_http_top_body_filter = ngx_http_sub_body_filter;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1016
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1017 return NGX_OK;
383ae25c3171 ngx_http_sub_filter_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1018 }