Mercurial > hg > nginx
comparison src/http/modules/ngx_http_ssi_filter.c @ 15:cbb38b60495c
nginx-0.0.1-2002-09-18-20:32:22 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 18 Sep 2002 16:32:22 +0000 |
parents | f8a0d0f31a24 |
children | 6ce4755737b4 |
comparison
equal
deleted
inserted
replaced
14:f8a0d0f31a24 | 15:cbb38b60495c |
---|---|
12 static void *ngx_http_ssi_filter_create_conf(ngx_pool_t *pool); | 12 static void *ngx_http_ssi_filter_create_conf(ngx_pool_t *pool); |
13 | 13 |
14 ngx_http_module_t ngx_http_ssi_filter_module = { | 14 ngx_http_module_t ngx_http_ssi_filter_module = { |
15 NGX_HTTP_MODULE, | 15 NGX_HTTP_MODULE, |
16 NULL, /* create server config */ | 16 NULL, /* create server config */ |
17 ngx_http_ssi_filter_create_conf, /* create location config */ | 17 ngx_http_ssi_filter_create_conf, /* create location config */ |
18 ngx_http_ssi_filter_commands, /* module directives */ | 18 ngx_http_ssi_filter_commands, /* module directives */ |
19 NULL, /* init module */ | 19 NULL, /* init module */ |
20 NULL /* init output body filter */ | 20 NULL /* init output body filter */ |
21 }; | 21 }; |
22 | 22 |
23 | 23 |
24 static ngx_command_t ngx_http_ssi_filter_commands[] = { | 24 static ngx_command_t ngx_http_ssi_filter_commands[] = { |
25 | 25 |
26 {"ssi", ngx_conf_set_size_slot, | 26 {"ssi", ngx_conf_set_flag_slot, |
27 offsetof(ngx_http_write_filter_conf_t, buffer_output), | 27 offsetof(ngx_http_ssi_filter_conf_t, on), |
28 NGX_HTTP_LOC_CONF, NGX_CONF_TAKE1, | 28 NGX_HTTP_LOC_CONF, NGX_CONF_FLAG, |
29 "set write filter size to buffer output"}, | 29 "enable ssi filter"}, |
30 | 30 |
31 {NULL} | 31 {NULL} |
32 | 32 |
33 }; | 33 }; |
34 | 34 |
35 | 35 |
36 int ngx_http_ssi_filter(ngx_http_request_t *r, ngx_chain_t *in) | 36 int ngx_http_ssi_filter(ngx_http_request_t *r, ngx_chain_t *in) |
37 { | 37 { |
38 int last; | |
39 off_t size, flush; | |
40 ngx_chain_t *ch, **prev, *chain; | 38 ngx_chain_t *ch, **prev, *chain; |
41 ngx_http_ssi_filter_ctx_t *ctx; | 39 ngx_http_ssi_filter_ctx_t *ctx; |
42 ngx_http_ssi_filter_conf_t *conf; | 40 ngx_http_ssi_filter_conf_t *conf; |
43 | 41 |
44 ctx = (ngx_http_ssi_filter_ctx_t *) | 42 ctx = (ngx_http_ssi_filter_ctx_t *) |
51 | 49 |
52 ctx->state = &ssi_start; | 50 ctx->state = &ssi_start; |
53 } | 51 } |
54 | 52 |
55 state = ctx->state; | 53 state = ctx->state; |
54 length = ctx->length; | |
56 | 55 |
57 ch = in; | 56 ch = in; |
58 p = ch->hunk->pos.mem; | 57 p = ch->hunk->pos.mem; |
58 | |
59 rc = ngx_http_ssi_parse(r, ctx, in); | |
60 if (rc == NGX_SSI_FOUND) { | |
61 } | |
62 | |
63 } | |
64 | |
65 | |
66 static int ngx_http_ssi_parse(ngx_http_request_t *r, | |
67 ngx_http_ssi_filter_ctx_t *ctx, ngx_chain_t *in) | |
68 { | |
69 state = ctx->state; | |
70 length = ctx->length; | |
59 | 71 |
60 for ( ;; ) { | 72 for ( ;; ) { |
61 | 73 |
62 if (state == ssi_start_state) { | 74 if (state == ssi_start_state) { |
63 for (/* void */ ; p < ch->hunk->last.mem; p++) { | 75 for (/* void */ ; p < ch->hunk->last.mem; p++) { |
64 if (*p == '<') { | 76 if (*p == '<') { |
65 state = ssi_exclam_state; | 77 state = ssi_exclam_state; |
66 saved_pos = p; | 78 length = 1; |
67 saved_chain = ch; | |
68 break; | 79 break; |
69 } | 80 } |
70 } | 81 } |
71 } | 82 } |
72 | 83 |
73 for (/* void */ ; | 84 for (/* void */ ; |
74 p < ch->hunk->last.mem && state > ssi_start_state; | 85 p < ch->hunk->last.mem |
86 && (state > ssi_start_state && state < ssi_command_state) | |
75 p++) | 87 p++) |
76 { | 88 { |
77 | 89 switch (state) { |
78 if (*p == '<') { | 90 |
79 state = ssi_exclam_state; | 91 case ssi_exclam_state: |
80 saved_pos = p; | 92 switch (*p) { |
81 saved_chain = ch; | 93 |
82 continue; | 94 case '!': |
95 state = ssi_dash1_state; | |
96 length = 2; | |
97 break; | |
98 | |
99 case '<': | |
100 state = ssi_exclam_state; | |
101 length = 1; | |
102 break; | |
103 | |
104 default: | |
105 state = ssi_start_state; | |
106 length = 0; | |
107 break; | |
108 } | |
109 | |
110 break; | |
111 | |
112 case ssi_dash1_state: | |
113 switch (*p) { | |
114 | |
115 case '-': | |
116 state = ssi_dash2_state; | |
117 length = 3; | |
118 break; | |
119 | |
120 case '<': | |
121 state = ssi_exclam_state; | |
122 length = 1; | |
123 break; | |
124 | |
125 default: | |
126 state = ssi_start_state; | |
127 length = 0; | |
128 break; | |
129 } | |
130 | |
131 break; | |
132 | |
133 case ssi_dash2_state: | |
134 switch (*p) { | |
135 | |
136 case '-': | |
137 state = ssi_sharp_state; | |
138 length = 4; | |
139 break; | |
140 | |
141 case '<': | |
142 state = ssi_exclam_state; | |
143 length = 1; | |
144 break; | |
145 | |
146 default: | |
147 state = ssi_start_state; | |
148 length = 0; | |
149 break; | |
150 } | |
151 | |
152 break; | |
153 | |
154 case ssi_sharp_state: | |
155 switch (*p) { | |
156 | |
157 case '#': | |
158 ctx->state = ssi_command_state; | |
159 ctx->length = 5; | |
160 return NGX_SSI_FOUND; | |
161 | |
162 case '<': | |
163 state = ssi_exclam_state; | |
164 length = 1; | |
165 break; | |
166 | |
167 default: | |
168 state = ssi_start_state; | |
169 length = 0; | |
170 break; | |
171 } | |
172 | |
173 break; | |
83 } | 174 } |
84 | 175 } |
85 switch (state) { | 176 |
86 | 177 if (state > ssi_start_state) { |
87 case ssi_exclam_state: | 178 ngx_add_hunk_to_chain(ch->hunk); |
88 if (*p == '!') { | |
89 state = ssi_dash1_state; | |
90 | |
91 else { | |
92 state = ssi_ssi_state; | |
93 saved_pos = NULL; | |
94 saved_chain = NULL; | |
95 } | |
96 | |
97 break; | |
98 | |
99 case ssi_dash1_state: | |
100 if (*p == '-') { | |
101 state = ssi_dash2_state; | |
102 | |
103 else { | |
104 state = ssi_ssi_state; | |
105 saved_pos = NULL; | |
106 saved_chain = NULL; | |
107 } | |
108 | |
109 break; | |
110 | |
111 case ssi_dash2_state: | |
112 if (*p == '-') { | |
113 state = ssi_sharp_state; | |
114 | |
115 else { | |
116 state = ssi_ssi_state; | |
117 saved_pos = NULL; | |
118 saved_chain = NULL; | |
119 } | |
120 | |
121 break; | |
122 | |
123 case ssi_sharp_state: | |
124 switch (*p) { | |
125 case '#': | |
126 state = ssi_command_state; | |
127 break; | |
128 | |
129 case ' ': | |
130 case '\t': | |
131 case CR: | |
132 case LF: | |
133 break; | |
134 | |
135 default: | |
136 state = ssi_ssi_state; | |
137 saved_pos = NULL; | |
138 saved_chain = NULL; | |
139 break; | |
140 } | |
141 | |
142 break; | |
143 } | |
144 } | 179 } |
145 | 180 |
146 ch = ch->next; | 181 ch = ch->next; |
147 if (ch == NULL) { | 182 if (ch == NULL) { |
148 ctx->state = state; | 183 ctx->state = state; |
150 } | 185 } |
151 | 186 |
152 p = ch->hunk->pos.mem; | 187 p = ch->hunk->pos.mem; |
153 } | 188 } |
154 | 189 |
155 for (p = saved_pos, ch = saved_chain; | 190 if (state > ssi_start_state) |
156 ch; | 191 if (ngx_http_ssi_dup_hunk(r, ch->hunk) == NGX_ERROR) |
157 ch = ch->next, p = ch->hunk->pos.mem) | 192 return NGX_ERROR; |
158 { | 193 |
159 ngx_memcpy(saved_line, p, ch->hunk->last.mem - p); | 194 } |
160 saved_size += ch->hunk->last.mem - p; | 195 |
161 if (ch->next == NULL) | 196 |
162 break; | 197 static ngx_http_ssi_dup_hunk(ngx_http_request_t *r, ngx_hunk_t *hunk); |
163 } | 198 { |
164 | 199 new dup_hunk |
165 | 200 set dup_hunk |
166 | 201 ngx_add_hunk_to_chain dup_hunk |
167 | 202 |
168 | 203 ngx_test_null(ssi_hunk, ngx_push_array); |
169 | 204 ssi_hunk->ssi_hunk = dup_hunk; |
170 | 205 ssi_hunk->hunk = hunk; |
171 | 206 ssi_hunk->pos = NULL; |
172 | 207 } |
173 | 208 |
174 | 209 |
175 | 210 |
176 | 211 |
177 | 212 |
178 for (/* void */; in; in = in->next) { | 213 |
179 | 214 |
180 for (p = in->hunk->pos.mem; | 215 |
181 in->hunk->pos.mem < in->hunk->last.mem; | 216 |
182 p++) | 217 |
183 { | 218 |
184 switch (state) { | 219 |
185 | 220 |
186 case ssi_start_state: | 221 |
187 if (*p == '<') { | 222 |
188 state = ssi_exclam_state; | 223 |
189 save_line = p; | 224 |
190 } | 225 |
191 break; | 226 |
192 | 227 |
193 case ssi_exclam_state: | 228 |
194 } | 229 |
195 } | 230 |
196 } | 231 |
197 | 232 |
198 | 233 |
199 | 234 |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 size = flush = 0; | |
217 last = 0; | |
218 prev = &ctx->out; | |
219 | |
220 /* find size, flush point and last link of saved chain */ | |
221 for (ch = ctx->out; ch; ch = ch->next) { | |
222 prev = &ch->next; | |
223 size += ch->hunk->last.file - ch->hunk->pos.file; | |
224 | |
225 ngx_log_debug(r->connection->log, "old chunk: %x " QX_FMT " " QD_FMT _ | |
226 ch->hunk->type _ ch->hunk->pos.file _ | |
227 ch->hunk->last.file - ch->hunk->pos.file); | |
228 | |
229 if (ch->hunk->type & NGX_HUNK_FLUSH|NGX_HUNK_RECYCLED) | |
230 flush = size; | |
231 | |
232 if (ch->hunk->type & NGX_HUNK_LAST) | |
233 last = 1; | |
234 } | |
235 | |
236 /* add new chain to existent one */ | |
237 for (/* void */; in; in = in->next) { | |
238 ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); | |
239 | |
240 ch->hunk = in->hunk; | |
241 ch->next = NULL; | |
242 *prev = ch; | |
243 prev = &ch->next; | |
244 size += ch->hunk->last.file - ch->hunk->pos.file; | |
245 | |
246 ngx_log_debug(r->connection->log, "new chunk: %x " QX_FMT " " QD_FMT _ | |
247 ch->hunk->type _ ch->hunk->pos.file _ | |
248 ch->hunk->last.file - ch->hunk->pos.file); | |
249 | |
250 if (ch->hunk->type & NGX_HUNK_FLUSH|NGX_HUNK_RECYCLED) | |
251 flush = size; | |
252 | |
253 if (ch->hunk->type & NGX_HUNK_LAST) | |
254 last = 1; | |
255 } | |
256 | |
257 conf = (ngx_http_write_filter_conf_t *) | |
258 ngx_get_module_loc_conf(r->main ? r->main : r, | |
259 ngx_http_write_filter_module); | |
260 | |
261 if (!last && flush == 0 && size < conf->buffer_output) | |
262 return NGX_OK; | |
263 | |
264 chain = ngx_event_write(r->connection, ctx->out, flush); | |
265 if (chain == (ngx_chain_t *) -1) | |
266 return NGX_ERROR; | |
267 | |
268 ctx->out = chain; | |
269 | |
270 ngx_log_debug(r->connection->log, "write filter %x" _ chain); | |
271 | |
272 return (chain ? NGX_AGAIN : NGX_OK); | |
273 } | |
274 | 235 |
275 | 236 |
276 static void *ngx_http_ssi_filter_create_conf(ngx_pool_t *pool) | 237 static void *ngx_http_ssi_filter_create_conf(ngx_pool_t *pool) |
277 { | 238 { |
278 ngx_http_ssi_filter_conf_t *conf; | 239 ngx_http_ssi_filter_conf_t *conf; |