46
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
102
|
11 #define NGX_HTTP_SSI_ERROR 1
|
|
12
|
|
13 #define NGX_HTTP_SSI_DATE_LEN 2048
|
|
14
|
|
15 #define NGX_HTTP_SSI_ADD_PREFIX 1
|
178
|
16 #define NGX_HTTP_SSI_ADD_ZERO 2
|
184
|
17 #define NGX_HTTP_SSI_EXPR_TEST 4
|
58
|
18
|
46
|
19
|
|
20 typedef struct {
|
146
|
21 ngx_flag_t enable;
|
|
22 ngx_flag_t silent_errors;
|
|
23 ngx_flag_t ignore_recycled_buffers;
|
|
24
|
|
25 ngx_array_t *types; /* array of ngx_str_t */
|
|
26
|
|
27 size_t min_file_chunk;
|
|
28 size_t value_len;
|
|
29 } ngx_http_ssi_loc_conf_t;
|
102
|
30
|
|
31
|
|
32 typedef struct {
|
146
|
33 ngx_str_t name;
|
160
|
34 ngx_uint_t key;
|
146
|
35 ngx_str_t value;
|
|
36 } ngx_http_ssi_var_t;
|
46
|
37
|
|
38
|
216
|
39 typedef struct {
|
|
40 ngx_str_t name;
|
|
41 ngx_chain_t *bufs;
|
|
42 ngx_uint_t count;
|
|
43 } ngx_http_ssi_block_t;
|
|
44
|
|
45
|
46
|
46 typedef enum {
|
|
47 ssi_start_state = 0,
|
|
48 ssi_tag_state,
|
|
49 ssi_comment0_state,
|
|
50 ssi_comment1_state,
|
|
51 ssi_sharp_state,
|
|
52 ssi_precommand_state,
|
|
53 ssi_command_state,
|
|
54 ssi_preparam_state,
|
|
55 ssi_param_state,
|
|
56 ssi_preequal_state,
|
|
57 ssi_prevalue_state,
|
|
58 ssi_double_quoted_value_state,
|
|
59 ssi_quoted_value_state,
|
|
60 ssi_quoted_symbol_state,
|
|
61 ssi_postparam_state,
|
|
62 ssi_comment_end0_state,
|
|
63 ssi_comment_end1_state,
|
|
64 ssi_error_state,
|
|
65 ssi_error_end0_state,
|
|
66 ssi_error_end1_state
|
|
67 } ngx_http_ssi_state_e;
|
|
68
|
|
69
|
58
|
70 static ngx_int_t ngx_http_ssi_output(ngx_http_request_t *r,
|
|
71 ngx_http_ssi_ctx_t *ctx);
|
46
|
72 static ngx_int_t ngx_http_ssi_parse(ngx_http_request_t *r,
|
|
73 ngx_http_ssi_ctx_t *ctx);
|
102
|
74 static ngx_str_t *ngx_http_ssi_get_variable(ngx_http_request_t *r,
|
160
|
75 ngx_str_t *name, ngx_uint_t key);
|
102
|
76 static ngx_int_t ngx_http_ssi_evaluate_string(ngx_http_request_t *r,
|
|
77 ngx_http_ssi_ctx_t *ctx, ngx_str_t *text, ngx_uint_t flags);
|
|
78
|
|
79 static ngx_int_t ngx_http_ssi_include(ngx_http_request_t *r,
|
|
80 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
248
|
81 static ngx_int_t ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data,
|
|
82 ngx_int_t rc);
|
|
83 static ngx_int_t ngx_http_ssi_set_variable(ngx_http_request_t *r, void *data,
|
|
84 ngx_int_t rc);
|
46
|
85 static ngx_int_t ngx_http_ssi_echo(ngx_http_request_t *r,
|
|
86 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
58
|
87 static ngx_int_t ngx_http_ssi_config(ngx_http_request_t *r,
|
|
88 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
102
|
89 static ngx_int_t ngx_http_ssi_set(ngx_http_request_t *r,
|
58
|
90 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
|
91 static ngx_int_t ngx_http_ssi_if(ngx_http_request_t *r,
|
|
92 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
|
93 static ngx_int_t ngx_http_ssi_else(ngx_http_request_t *r,
|
|
94 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
|
95 static ngx_int_t ngx_http_ssi_endif(ngx_http_request_t *r,
|
|
96 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
216
|
97 static ngx_int_t ngx_http_ssi_block(ngx_http_request_t *r,
|
|
98 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
|
99 static ngx_int_t ngx_http_ssi_endblock(ngx_http_request_t *r,
|
|
100 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
46
|
101
|
122
|
102 static ngx_int_t ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
|
|
103 ngx_http_variable_value_t *v, uintptr_t gmt);
|
58
|
104
|
76
|
105 static char *ngx_http_ssi_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
106
|
146
|
107 static ngx_int_t ngx_http_ssi_preconfiguration(ngx_conf_t *cf);
|
|
108 static void *ngx_http_ssi_create_main_conf(ngx_conf_t *cf);
|
|
109 static char *ngx_http_ssi_init_main_conf(ngx_conf_t *cf, void *conf);
|
46
|
110 static void *ngx_http_ssi_create_conf(ngx_conf_t *cf);
|
|
111 static char *ngx_http_ssi_merge_conf(ngx_conf_t *cf,
|
|
112 void *parent, void *child);
|
230
|
113 static ngx_int_t ngx_http_ssi_filter_init(ngx_conf_t *cf);
|
46
|
114
|
|
115
|
|
116 static ngx_command_t ngx_http_ssi_filter_commands[] = {
|
|
117
|
|
118 { ngx_string("ssi"),
|
230
|
119 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
120 |NGX_CONF_FLAG,
|
46
|
121 ngx_conf_set_flag_slot,
|
|
122 NGX_HTTP_LOC_CONF_OFFSET,
|
146
|
123 offsetof(ngx_http_ssi_loc_conf_t, enable),
|
46
|
124 NULL },
|
|
125
|
|
126 { ngx_string("ssi_silent_errors"),
|
|
127 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
128 ngx_conf_set_flag_slot,
|
|
129 NGX_HTTP_LOC_CONF_OFFSET,
|
146
|
130 offsetof(ngx_http_ssi_loc_conf_t, silent_errors),
|
46
|
131 NULL },
|
|
132
|
58
|
133 { ngx_string("ssi_ignore_recycled_buffers"),
|
|
134 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
135 ngx_conf_set_flag_slot,
|
|
136 NGX_HTTP_LOC_CONF_OFFSET,
|
146
|
137 offsetof(ngx_http_ssi_loc_conf_t, ignore_recycled_buffers),
|
58
|
138 NULL },
|
|
139
|
46
|
140 { ngx_string("ssi_min_file_chunk"),
|
|
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
142 ngx_conf_set_size_slot,
|
|
143 NGX_HTTP_LOC_CONF_OFFSET,
|
146
|
144 offsetof(ngx_http_ssi_loc_conf_t, min_file_chunk),
|
46
|
145 NULL },
|
|
146
|
138
|
147 { ngx_string("ssi_value_length"),
|
|
148 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
149 ngx_conf_set_size_slot,
|
|
150 NGX_HTTP_LOC_CONF_OFFSET,
|
146
|
151 offsetof(ngx_http_ssi_loc_conf_t, value_len),
|
138
|
152 NULL },
|
|
153
|
76
|
154 { ngx_string("ssi_types"),
|
|
155 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
156 ngx_http_ssi_types,
|
|
157 NGX_HTTP_LOC_CONF_OFFSET,
|
|
158 0,
|
|
159 NULL },
|
|
160
|
46
|
161 ngx_null_command
|
|
162 };
|
|
163
|
|
164
|
126
|
165
|
46
|
166 static ngx_http_module_t ngx_http_ssi_filter_module_ctx = {
|
146
|
167 ngx_http_ssi_preconfiguration, /* preconfiguration */
|
230
|
168 ngx_http_ssi_filter_init, /* postconfiguration */
|
46
|
169
|
146
|
170 ngx_http_ssi_create_main_conf, /* create main configuration */
|
|
171 ngx_http_ssi_init_main_conf, /* init main configuration */
|
46
|
172
|
|
173 NULL, /* create server configuration */
|
|
174 NULL, /* merge server configuration */
|
|
175
|
|
176 ngx_http_ssi_create_conf, /* create location configuration */
|
|
177 ngx_http_ssi_merge_conf /* merge location configuration */
|
126
|
178 };
|
46
|
179
|
|
180
|
|
181 ngx_module_t ngx_http_ssi_filter_module = {
|
58
|
182 NGX_MODULE_V1,
|
46
|
183 &ngx_http_ssi_filter_module_ctx, /* module context */
|
|
184 ngx_http_ssi_filter_commands, /* module directives */
|
|
185 NGX_HTTP_MODULE, /* module type */
|
90
|
186 NULL, /* init master */
|
230
|
187 NULL, /* init module */
|
90
|
188 NULL, /* init process */
|
|
189 NULL, /* init thread */
|
|
190 NULL, /* exit thread */
|
|
191 NULL, /* exit process */
|
|
192 NULL, /* exit master */
|
|
193 NGX_MODULE_V1_PADDING
|
46
|
194 };
|
|
195
|
|
196
|
118
|
197 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
198 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
46
|
199
|
|
200
|
|
201 static u_char ngx_http_ssi_string[] = "<!--";
|
|
202
|
|
203 static ngx_str_t ngx_http_ssi_none = ngx_string("(none)");
|
248
|
204 static ngx_str_t ngx_http_ssi_null_string = ngx_null_string;
|
|
205
|
46
|
206
|
102
|
207 #define NGX_HTTP_SSI_INCLUDE_VIRTUAL 0
|
|
208 #define NGX_HTTP_SSI_INCLUDE_FILE 1
|
194
|
209 #define NGX_HTTP_SSI_INCLUDE_WAIT 2
|
248
|
210 #define NGX_HTTP_SSI_INCLUDE_SET 3
|
|
211 #define NGX_HTTP_SSI_INCLUDE_STUB 4
|
46
|
212
|
58
|
213 #define NGX_HTTP_SSI_ECHO_VAR 0
|
|
214 #define NGX_HTTP_SSI_ECHO_DEFAULT 1
|
|
215
|
96
|
216 #define NGX_HTTP_SSI_CONFIG_ERRMSG 0
|
|
217 #define NGX_HTTP_SSI_CONFIG_TIMEFMT 1
|
58
|
218
|
102
|
219 #define NGX_HTTP_SSI_SET_VAR 0
|
|
220 #define NGX_HTTP_SSI_SET_VALUE 1
|
58
|
221
|
|
222 #define NGX_HTTP_SSI_IF_EXPR 0
|
|
223
|
216
|
224 #define NGX_HTTP_SSI_BLOCK_NAME 0
|
|
225
|
46
|
226
|
102
|
227 static ngx_http_ssi_param_t ngx_http_ssi_include_params[] = {
|
146
|
228 { ngx_string("virtual"), NGX_HTTP_SSI_INCLUDE_VIRTUAL, 0, 0 },
|
|
229 { ngx_string("file"), NGX_HTTP_SSI_INCLUDE_FILE, 0, 0 },
|
194
|
230 { ngx_string("wait"), NGX_HTTP_SSI_INCLUDE_WAIT, 0, 0 },
|
248
|
231 { ngx_string("set"), NGX_HTTP_SSI_INCLUDE_SET, 0, 0 },
|
216
|
232 { ngx_string("stub"), NGX_HTTP_SSI_INCLUDE_STUB, 0, 0 },
|
146
|
233 { ngx_null_string, 0, 0, 0 }
|
102
|
234 };
|
|
235
|
|
236
|
46
|
237 static ngx_http_ssi_param_t ngx_http_ssi_echo_params[] = {
|
146
|
238 { ngx_string("var"), NGX_HTTP_SSI_ECHO_VAR, 1, 0 },
|
|
239 { ngx_string("default"), NGX_HTTP_SSI_ECHO_DEFAULT, 0, 0 },
|
|
240 { ngx_null_string, 0, 0, 0 }
|
46
|
241 };
|
|
242
|
58
|
243
|
|
244 static ngx_http_ssi_param_t ngx_http_ssi_config_params[] = {
|
146
|
245 { ngx_string("errmsg"), NGX_HTTP_SSI_CONFIG_ERRMSG, 0, 0 },
|
|
246 { ngx_string("timefmt"), NGX_HTTP_SSI_CONFIG_TIMEFMT, 0, 0 },
|
|
247 { ngx_null_string, 0, 0, 0 }
|
58
|
248 };
|
|
249
|
|
250
|
102
|
251 static ngx_http_ssi_param_t ngx_http_ssi_set_params[] = {
|
146
|
252 { ngx_string("var"), NGX_HTTP_SSI_SET_VAR, 1, 0 },
|
|
253 { ngx_string("value"), NGX_HTTP_SSI_SET_VALUE, 1, 0 },
|
|
254 { ngx_null_string, 0, 0, 0 }
|
102
|
255 };
|
|
256
|
|
257
|
58
|
258 static ngx_http_ssi_param_t ngx_http_ssi_if_params[] = {
|
146
|
259 { ngx_string("expr"), NGX_HTTP_SSI_IF_EXPR, 1, 0 },
|
|
260 { ngx_null_string, 0, 0, 0 }
|
58
|
261 };
|
|
262
|
|
263
|
216
|
264 static ngx_http_ssi_param_t ngx_http_ssi_block_params[] = {
|
|
265 { ngx_string("name"), NGX_HTTP_SSI_BLOCK_NAME, 1, 0 },
|
|
266 { ngx_null_string, 0, 0, 0 }
|
|
267 };
|
|
268
|
|
269
|
58
|
270 static ngx_http_ssi_param_t ngx_http_ssi_no_params[] = {
|
146
|
271 { ngx_null_string, 0, 0, 0 }
|
58
|
272 };
|
|
273
|
46
|
274
|
|
275 static ngx_http_ssi_command_t ngx_http_ssi_commands[] = {
|
102
|
276 { ngx_string("include"), ngx_http_ssi_include,
|
216
|
277 ngx_http_ssi_include_params, 0, 0, 1 },
|
|
278 { ngx_string("echo"), ngx_http_ssi_echo,
|
|
279 ngx_http_ssi_echo_params, 0, 0, 0 },
|
58
|
280 { ngx_string("config"), ngx_http_ssi_config,
|
216
|
281 ngx_http_ssi_config_params, 0, 0, 0 },
|
|
282 { ngx_string("set"), ngx_http_ssi_set, ngx_http_ssi_set_params, 0, 0, 0 },
|
|
283
|
|
284 { ngx_string("if"), ngx_http_ssi_if, ngx_http_ssi_if_params, 0, 0, 0 },
|
178
|
285 { ngx_string("elif"), ngx_http_ssi_if, ngx_http_ssi_if_params,
|
216
|
286 NGX_HTTP_SSI_COND_IF, 0, 0 },
|
178
|
287 { ngx_string("else"), ngx_http_ssi_else, ngx_http_ssi_no_params,
|
216
|
288 NGX_HTTP_SSI_COND_IF, 0, 0 },
|
178
|
289 { ngx_string("endif"), ngx_http_ssi_endif, ngx_http_ssi_no_params,
|
216
|
290 NGX_HTTP_SSI_COND_ELSE, 0, 0 },
|
|
291
|
|
292 { ngx_string("block"), ngx_http_ssi_block,
|
|
293 ngx_http_ssi_block_params, 0, 0, 0 },
|
|
294 { ngx_string("endblock"), ngx_http_ssi_endblock,
|
|
295 ngx_http_ssi_no_params, 0, 1, 0 },
|
|
296
|
|
297 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
46
|
298 };
|
|
299
|
|
300
|
58
|
301 static ngx_http_variable_t ngx_http_ssi_vars[] = {
|
|
302
|
186
|
303 { ngx_string("date_local"), NULL, ngx_http_ssi_date_gmt_local_variable, 0,
|
82
|
304 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
58
|
305
|
186
|
306 { ngx_string("date_gmt"), NULL, ngx_http_ssi_date_gmt_local_variable, 1,
|
82
|
307 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
58
|
308
|
186
|
309 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
58
|
310 };
|
|
311
|
|
312
|
|
313
|
46
|
314 static ngx_int_t
|
|
315 ngx_http_ssi_header_filter(ngx_http_request_t *r)
|
|
316 {
|
146
|
317 ngx_uint_t i;
|
|
318 ngx_str_t *type;
|
|
319 ngx_http_ssi_ctx_t *ctx;
|
|
320 ngx_http_ssi_loc_conf_t *slcf;
|
|
321
|
|
322 slcf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
|
|
323
|
|
324 if (!slcf->enable
|
126
|
325 || r->headers_out.content_type.len == 0
|
|
326 || r->headers_out.content_length_n == 0)
|
46
|
327 {
|
|
328 return ngx_http_next_header_filter(r);
|
|
329 }
|
|
330
|
|
331
|
146
|
332 type = slcf->types->elts;
|
|
333 for (i = 0; i < slcf->types->nelts; i++) {
|
76
|
334 if (r->headers_out.content_type.len >= type[i].len
|
|
335 && ngx_strncasecmp(r->headers_out.content_type.data,
|
|
336 type[i].data, type[i].len) == 0)
|
|
337 {
|
|
338 goto found;
|
|
339 }
|
|
340 }
|
|
341
|
|
342 return ngx_http_next_header_filter(r);
|
|
343
|
|
344
|
|
345 found:
|
|
346
|
48
|
347 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t));
|
|
348 if (ctx == NULL) {
|
46
|
349 return NGX_ERROR;
|
|
350 }
|
|
351
|
|
352 ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module);
|
|
353
|
|
354
|
146
|
355 ctx->value_len = slcf->value_len;
|
46
|
356 ctx->last_out = &ctx->out;
|
|
357
|
58
|
358 ctx->output = 1;
|
|
359
|
46
|
360 ctx->params.elts = ctx->params_array;
|
|
361 ctx->params.size = sizeof(ngx_table_elt_t);
|
|
362 ctx->params.nalloc = NGX_HTTP_SSI_PARAMS_N;
|
|
363 ctx->params.pool = r->pool;
|
|
364
|
58
|
365 ctx->timefmt.len = sizeof("%A, %d-%b-%Y %H:%M:%S %Z") - 1;
|
|
366 ctx->timefmt.data = (u_char *) "%A, %d-%b-%Y %H:%M:%S %Z";
|
46
|
367
|
96
|
368 ctx->errmsg.len =
|
|
369 sizeof("[an error occurred while processing the directive]") - 1;
|
|
370 ctx->errmsg.data = (u_char *)
|
|
371 "[an error occurred while processing the directive]";
|
|
372
|
46
|
373 r->filter_need_in_memory = 1;
|
|
374
|
146
|
375 if (r == r->main) {
|
126
|
376 ngx_http_clear_content_length(r);
|
|
377 ngx_http_clear_last_modified(r);
|
58
|
378 }
|
|
379
|
46
|
380 return ngx_http_next_header_filter(r);
|
|
381 }
|
|
382
|
|
383
|
|
384 static ngx_int_t
|
|
385 ngx_http_ssi_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
386 {
|
216
|
387 size_t len;
|
146
|
388 ngx_int_t rc;
|
|
389 ngx_buf_t *b;
|
|
390 ngx_uint_t i, index;
|
216
|
391 ngx_chain_t *cl, **ll;
|
146
|
392 ngx_table_elt_t *param;
|
246
|
393 ngx_http_request_t *pr;
|
216
|
394 ngx_http_ssi_ctx_t *ctx, *mctx;
|
|
395 ngx_http_ssi_block_t *bl;
|
146
|
396 ngx_http_ssi_param_t *prm;
|
|
397 ngx_http_ssi_command_t *cmd;
|
|
398 ngx_http_ssi_loc_conf_t *slcf;
|
|
399 ngx_http_ssi_main_conf_t *smcf;
|
148
|
400 ngx_str_t *params[NGX_HTTP_SSI_MAX_PARAMS + 1];
|
46
|
401
|
|
402 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
|
|
403
|
194
|
404 if (ctx == NULL
|
|
405 || (in == NULL
|
|
406 && ctx->buf == NULL
|
|
407 && ctx->in == NULL
|
|
408 && ctx->busy == NULL))
|
|
409 {
|
46
|
410 return ngx_http_next_body_filter(r, in);
|
|
411 }
|
|
412
|
|
413 /* add the incoming chain to the chain ctx->in */
|
|
414
|
|
415 if (in) {
|
|
416 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
|
|
417 return NGX_ERROR;
|
|
418 }
|
|
419 }
|
|
420
|
194
|
421 if (ctx->wait) {
|
|
422 if (r->connection->data != r) {
|
|
423 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
424 "http ssi filter \"%V\" wait", &r->uri);
|
|
425 return NGX_AGAIN;
|
|
426 }
|
|
427
|
246
|
428 for (pr = ctx->wait->parent; pr; pr = pr->parent) {
|
|
429 if (pr == r) {
|
|
430 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
431 "http ssi filter \"%V\" flush", &r->uri);
|
|
432
|
|
433 rc = ngx_http_next_body_filter(r, NULL);
|
|
434
|
|
435 if (ctx->wait->done) {
|
|
436 ctx->wait = NULL;
|
|
437 }
|
|
438
|
|
439 if (rc == NGX_ERROR || rc == NGX_AGAIN) {
|
|
440 return rc;
|
|
441 }
|
|
442 }
|
|
443 }
|
|
444
|
244
|
445 if (ctx->wait == r) {
|
|
446 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
447 "http ssi filter \"%V\" continue", &r->uri);
|
|
448 ctx->wait = NULL;
|
|
449 }
|
194
|
450 }
|
|
451
|
146
|
452 slcf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
|
46
|
453
|
58
|
454 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
455 "http ssi filter \"%V\"", &r->uri);
|
46
|
456
|
48
|
457 while (ctx->in || ctx->buf) {
|
46
|
458
|
48
|
459 if (ctx->buf == NULL ){
|
|
460 ctx->buf = ctx->in->buf;
|
|
461 ctx->in = ctx->in->next;
|
|
462 ctx->pos = ctx->buf->pos;
|
|
463 }
|
46
|
464
|
|
465 if (ctx->state == ssi_start_state) {
|
|
466 ctx->copy_start = ctx->pos;
|
|
467 ctx->copy_end = ctx->pos;
|
|
468 }
|
|
469
|
48
|
470 b = NULL;
|
|
471
|
46
|
472 while (ctx->pos < ctx->buf->last) {
|
|
473
|
|
474 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
475 "saved: %d state: %d", ctx->saved, ctx->state);
|
|
476
|
|
477 rc = ngx_http_ssi_parse(r, ctx);
|
|
478
|
|
479 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
480 "parse: %d, looked: %d %p-%p",
|
|
481 rc, ctx->looked, ctx->copy_start, ctx->copy_end);
|
|
482
|
|
483 if (rc == NGX_ERROR) {
|
|
484 return rc;
|
|
485 }
|
|
486
|
|
487 if (ctx->copy_start != ctx->copy_end) {
|
|
488
|
58
|
489 if (ctx->output) {
|
|
490
|
|
491 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
492 "saved: %d", ctx->saved);
|
|
493
|
|
494 if (ctx->saved) {
|
|
495
|
|
496 if (ctx->free) {
|
|
497 cl = ctx->free;
|
|
498 ctx->free = ctx->free->next;
|
|
499 b = cl->buf;
|
|
500 ngx_memzero(b, sizeof(ngx_buf_t));
|
46
|
501
|
58
|
502 } else {
|
|
503 b = ngx_calloc_buf(r->pool);
|
|
504 if (b == NULL) {
|
|
505 return NGX_ERROR;
|
|
506 }
|
|
507
|
|
508 cl = ngx_alloc_chain_link(r->pool);
|
|
509 if (cl == NULL) {
|
|
510 return NGX_ERROR;
|
|
511 }
|
|
512
|
|
513 cl->buf = b;
|
|
514 }
|
|
515
|
|
516 b->memory = 1;
|
|
517 b->pos = ngx_http_ssi_string;
|
|
518 b->last = ngx_http_ssi_string + ctx->saved;
|
|
519
|
|
520 *ctx->last_out = cl;
|
|
521 ctx->last_out = &cl->next;
|
|
522
|
|
523 ctx->saved = 0;
|
|
524 }
|
48
|
525
|
|
526 if (ctx->free) {
|
|
527 cl = ctx->free;
|
|
528 ctx->free = ctx->free->next;
|
|
529 b = cl->buf;
|
|
530
|
|
531 } else {
|
58
|
532 b = ngx_alloc_buf(r->pool);
|
48
|
533 if (b == NULL) {
|
|
534 return NGX_ERROR;
|
|
535 }
|
|
536
|
|
537 cl = ngx_alloc_chain_link(r->pool);
|
|
538 if (cl == NULL) {
|
|
539 return NGX_ERROR;
|
|
540 }
|
|
541
|
|
542 cl->buf = b;
|
46
|
543 }
|
|
544
|
58
|
545 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
|
|
546
|
62
|
547 b->pos = ctx->copy_start;
|
|
548 b->last = ctx->copy_end;
|
|
549 b->shadow = NULL;
|
58
|
550 b->last_buf = 0;
|
|
551 b->recycled = 0;
|
46
|
552
|
58
|
553 if (b->in_file) {
|
146
|
554 if (slcf->min_file_chunk < (size_t) (b->last - b->pos))
|
58
|
555 {
|
|
556 b->file_last = b->file_pos + (b->last - b->start);
|
|
557 b->file_pos += b->pos - b->start;
|
|
558
|
|
559 } else {
|
|
560 b->in_file = 0;
|
|
561 }
|
|
562 }
|
|
563
|
|
564 cl->next = NULL;
|
46
|
565 *ctx->last_out = cl;
|
|
566 ctx->last_out = &cl->next;
|
|
567
|
58
|
568 } else {
|
216
|
569 if (ctx->block
|
|
570 && ctx->saved + (ctx->copy_end - ctx->copy_start))
|
|
571 {
|
|
572 b = ngx_create_temp_buf(r->pool,
|
|
573 ctx->saved + (ctx->copy_end - ctx->copy_start));
|
|
574
|
|
575 if (b == NULL) {
|
|
576 return NGX_ERROR;
|
|
577 }
|
|
578
|
|
579 if (ctx->saved) {
|
|
580 b->last = ngx_cpymem(b->pos, ngx_http_ssi_string,
|
|
581 ctx->saved);
|
|
582 }
|
|
583
|
|
584 b->last = ngx_cpymem(b->last, ctx->copy_start,
|
|
585 ctx->copy_end - ctx->copy_start);
|
|
586
|
|
587 cl = ngx_alloc_chain_link(r->pool);
|
|
588 if (cl == NULL) {
|
|
589 return NGX_ERROR;
|
|
590 }
|
|
591
|
|
592 cl->buf = b;
|
|
593 cl->next = NULL;
|
|
594
|
|
595 b = NULL;
|
|
596
|
|
597 mctx = ngx_http_get_module_ctx(r->main,
|
|
598 ngx_http_ssi_filter_module);
|
|
599 bl = mctx->blocks->elts;
|
|
600 for (ll = &bl[mctx->blocks->nelts - 1].bufs;
|
|
601 *ll;
|
|
602 ll = &(*ll)->next)
|
|
603 {
|
|
604 /* void */
|
|
605 }
|
|
606
|
|
607 *ll = cl;
|
|
608 }
|
|
609
|
46
|
610 ctx->saved = 0;
|
|
611 }
|
|
612 }
|
|
613
|
|
614 if (ctx->state == ssi_start_state) {
|
|
615 ctx->copy_start = ctx->pos;
|
|
616 ctx->copy_end = ctx->pos;
|
|
617
|
|
618 } else {
|
|
619 ctx->copy_start = NULL;
|
|
620 ctx->copy_end = NULL;
|
|
621 }
|
|
622
|
|
623 if (rc == NGX_AGAIN) {
|
|
624 continue;
|
|
625 }
|
|
626
|
|
627
|
122
|
628 b = NULL;
|
|
629
|
46
|
630 if (rc == NGX_OK) {
|
|
631
|
146
|
632 smcf = ngx_http_get_module_main_conf(r,
|
|
633 ngx_http_ssi_filter_module);
|
|
634
|
|
635 cmd = ngx_hash_find(&smcf->hash, ctx->key, ctx->command.data,
|
|
636 ctx->command.len);
|
|
637
|
|
638 if (cmd == NULL) {
|
|
639 if (ctx->output) {
|
|
640 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
641 "invalid SSI command: \"%V\"",
|
|
642 &ctx->command);
|
|
643 goto ssi_error;
|
46
|
644 }
|
|
645
|
146
|
646 continue;
|
46
|
647 }
|
|
648
|
178
|
649 if (cmd->conditional
|
|
650 && (ctx->conditional == 0
|
|
651 || ctx->conditional > cmd->conditional))
|
|
652 {
|
|
653 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
654 "invalid context of SSI command: \"%V\"",
|
|
655 &ctx->command);
|
|
656 goto ssi_error;
|
|
657 }
|
|
658
|
216
|
659 if (!ctx->output && !cmd->block) {
|
|
660
|
|
661 if (ctx->block) {
|
|
662
|
|
663 /* reconstruct the SSI command text */
|
|
664
|
|
665 len = 5 + ctx->command.len + 4;
|
|
666
|
|
667 param = ctx->params.elts;
|
|
668 for (i = 0; i < ctx->params.nelts; i++) {
|
|
669 len += 1 + param[i].key.len + 2
|
|
670 + param[i].value.len + 1;
|
|
671 }
|
|
672
|
|
673 b = ngx_create_temp_buf(r->pool, len);
|
|
674
|
|
675 if (b == NULL) {
|
|
676 return NGX_ERROR;
|
|
677 }
|
|
678
|
|
679 cl = ngx_alloc_chain_link(r->pool);
|
|
680 if (cl == NULL) {
|
|
681 return NGX_ERROR;
|
|
682 }
|
|
683
|
|
684 cl->buf = b;
|
|
685 cl->next = NULL;
|
|
686
|
|
687 *b->last++ = '<';
|
|
688 *b->last++ = '!';
|
|
689 *b->last++ = '-';
|
|
690 *b->last++ = '-';
|
|
691 *b->last++ = '#';
|
|
692
|
|
693 b->last = ngx_cpymem(b->last, ctx->command.data,
|
|
694 ctx->command.len);
|
|
695
|
|
696 for (i = 0; i < ctx->params.nelts; i++) {
|
|
697 *b->last++ = ' ';
|
|
698 b->last = ngx_cpymem(b->last, param[i].key.data,
|
|
699 param[i].key.len);
|
|
700 *b->last++ = '=';
|
|
701 *b->last++ = '"';
|
|
702 b->last = ngx_cpymem(b->last, param[i].value.data,
|
|
703 param[i].value.len);
|
|
704 *b->last++ = '"';
|
|
705 }
|
|
706
|
|
707 *b->last++ = ' ';
|
|
708 *b->last++ = '-';
|
|
709 *b->last++ = '-';
|
|
710 *b->last++ = '>';
|
|
711
|
|
712 mctx = ngx_http_get_module_ctx(r->main,
|
|
713 ngx_http_ssi_filter_module);
|
|
714 bl = mctx->blocks->elts;
|
|
715 for (ll = &bl[mctx->blocks->nelts - 1].bufs;
|
|
716 *ll;
|
|
717 ll = &(*ll)->next)
|
|
718 {
|
|
719 /* void */
|
|
720 }
|
|
721
|
|
722 *ll = cl;
|
|
723
|
|
724 b = NULL;
|
|
725
|
|
726 continue;
|
|
727 }
|
|
728
|
|
729 if (cmd->conditional == 0) {
|
|
730 continue;
|
|
731 }
|
58
|
732 }
|
|
733
|
146
|
734 if (ctx->params.nelts > NGX_HTTP_SSI_MAX_PARAMS) {
|
|
735 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
736 "too many SSI command paramters: \"%V\"",
|
|
737 &ctx->command);
|
|
738 goto ssi_error;
|
|
739 }
|
|
740
|
46
|
741 ngx_memzero(params,
|
148
|
742 (NGX_HTTP_SSI_MAX_PARAMS + 1) * sizeof(ngx_str_t *));
|
46
|
743
|
|
744 param = ctx->params.elts;
|
|
745
|
|
746 for (i = 0; i < ctx->params.nelts; i++) {
|
|
747
|
|
748 for (prm = cmd->params; prm->name.len; prm++) {
|
|
749
|
|
750 if (param[i].key.len != prm->name.len
|
|
751 || ngx_strncmp(param[i].key.data, prm->name.data,
|
|
752 prm->name.len) != 0)
|
|
753 {
|
|
754 continue;
|
|
755 }
|
|
756
|
146
|
757 if (!prm->multiple) {
|
|
758 if (params[prm->index]) {
|
|
759 ngx_log_error(NGX_LOG_ERR,
|
|
760 r->connection->log, 0,
|
|
761 "duplicate \"%V\" parameter "
|
|
762 "in \"%V\" SSI command",
|
|
763 ¶m[i].key, &ctx->command);
|
|
764
|
|
765 goto ssi_error;
|
|
766 }
|
|
767
|
|
768 params[prm->index] = ¶m[i].value;
|
|
769
|
|
770 break;
|
46
|
771 }
|
|
772
|
146
|
773 for (index = prm->index; params[index]; index++) {
|
|
774 /* void */
|
|
775 }
|
|
776
|
|
777 params[index] = ¶m[i].value;
|
46
|
778
|
|
779 break;
|
|
780 }
|
|
781
|
|
782 if (prm->name.len == 0) {
|
|
783 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
784 "invalid parameter name: \"%V\" "
|
|
785 "in \"%V\" SSI command",
|
|
786 ¶m[i].key, &ctx->command);
|
|
787
|
|
788 goto ssi_error;
|
|
789 }
|
|
790 }
|
|
791
|
|
792 for (prm = cmd->params; prm->name.len; prm++) {
|
|
793 if (prm->mandatory && params[prm->index] == 0) {
|
|
794 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
795 "mandatory \"%V\" parameter is absent "
|
|
796 "in \"%V\" SSI command",
|
|
797 &prm->name, &ctx->command);
|
|
798
|
|
799 goto ssi_error;
|
|
800 }
|
|
801 }
|
|
802
|
58
|
803 if (cmd->flush && ctx->out) {
|
|
804 rc = ngx_http_ssi_output(r, ctx);
|
|
805
|
|
806 if (rc == NGX_ERROR) {
|
|
807 return NGX_ERROR;
|
|
808 }
|
|
809 }
|
|
810
|
118
|
811 rc = cmd->handler(r, ctx, params);
|
|
812
|
|
813 if (rc == NGX_OK) {
|
46
|
814 continue;
|
|
815 }
|
132
|
816
|
248
|
817 if (rc == NGX_DONE || rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
818 return rc;
|
132
|
819 }
|
46
|
820 }
|
|
821
|
|
822
|
|
823 /* rc == NGX_HTTP_SSI_ERROR */
|
|
824
|
48
|
825 ssi_error:
|
46
|
826
|
146
|
827 if (slcf->silent_errors) {
|
46
|
828 continue;
|
|
829 }
|
|
830
|
48
|
831 if (ctx->free) {
|
|
832 cl = ctx->free;
|
|
833 ctx->free = ctx->free->next;
|
|
834 b = cl->buf;
|
|
835 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
836
|
|
837 } else {
|
|
838 b = ngx_calloc_buf(r->pool);
|
|
839 if (b == NULL) {
|
|
840 return NGX_ERROR;
|
|
841 }
|
|
842
|
|
843 cl = ngx_alloc_chain_link(r->pool);
|
|
844 if (cl == NULL) {
|
|
845 return NGX_ERROR;
|
|
846 }
|
|
847
|
|
848 cl->buf = b;
|
46
|
849 }
|
|
850
|
|
851 b->memory = 1;
|
96
|
852 b->pos = ctx->errmsg.data;
|
|
853 b->last = ctx->errmsg.data + ctx->errmsg.len;
|
46
|
854
|
|
855 cl->next = NULL;
|
|
856 *ctx->last_out = cl;
|
|
857 ctx->last_out = &cl->next;
|
|
858
|
|
859 continue;
|
|
860 }
|
|
861
|
236
|
862 if (ctx->buf->last_buf || ngx_buf_in_memory(ctx->buf)) {
|
48
|
863 if (b == NULL) {
|
|
864 if (ctx->free) {
|
|
865 cl = ctx->free;
|
|
866 ctx->free = ctx->free->next;
|
|
867 b = cl->buf;
|
|
868 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
869
|
|
870 } else {
|
|
871 b = ngx_calloc_buf(r->pool);
|
|
872 if (b == NULL) {
|
|
873 return NGX_ERROR;
|
|
874 }
|
46
|
875
|
48
|
876 cl = ngx_alloc_chain_link(r->pool);
|
|
877 if (cl == NULL) {
|
|
878 return NGX_ERROR;
|
|
879 }
|
|
880
|
|
881 cl->buf = b;
|
|
882 }
|
|
883
|
58
|
884 b->sync = 1;
|
|
885
|
48
|
886 cl->next = NULL;
|
|
887 *ctx->last_out = cl;
|
|
888 ctx->last_out = &cl->next;
|
|
889 }
|
|
890
|
|
891 b->last_buf = ctx->buf->last_buf;
|
|
892 b->shadow = ctx->buf;
|
58
|
893
|
146
|
894 if (slcf->ignore_recycled_buffers == 0) {
|
58
|
895 b->recycled = ctx->buf->recycled;
|
|
896 }
|
46
|
897 }
|
|
898
|
48
|
899 ctx->buf = NULL;
|
|
900
|
46
|
901 ctx->saved = ctx->looked;
|
|
902 }
|
|
903
|
48
|
904 if (ctx->out == NULL && ctx->busy == NULL) {
|
46
|
905 return NGX_OK;
|
|
906 }
|
|
907
|
58
|
908 return ngx_http_ssi_output(r, ctx);
|
|
909 }
|
|
910
|
|
911
|
|
912 static ngx_int_t
|
|
913 ngx_http_ssi_output(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
914 {
|
|
915 ngx_int_t rc;
|
|
916 ngx_buf_t *b;
|
|
917 ngx_chain_t *cl;
|
|
918
|
190
|
919 #if 1
|
|
920 b = NULL;
|
|
921 for (cl = ctx->out; cl; cl = cl->next) {
|
192
|
922 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
923 "ssi out: %p %p", cl->buf, cl->buf->pos);
|
190
|
924 if (cl->buf == b) {
|
|
925 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
926 "the same buf was used in ssi");
|
|
927 ngx_debug_point();
|
|
928 return NGX_ERROR;
|
|
929 }
|
|
930 b = cl->buf;
|
|
931 }
|
|
932 #endif
|
|
933
|
46
|
934 rc = ngx_http_next_body_filter(r, ctx->out);
|
|
935
|
48
|
936 if (ctx->busy == NULL) {
|
|
937 ctx->busy = ctx->out;
|
|
938
|
|
939 } else {
|
|
940 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
|
|
941 cl->next = ctx->out;
|
|
942 }
|
|
943
|
46
|
944 ctx->out = NULL;
|
|
945 ctx->last_out = &ctx->out;
|
|
946
|
48
|
947 while (ctx->busy) {
|
|
948
|
206
|
949 cl = ctx->busy;
|
|
950 b = cl->buf;
|
48
|
951
|
|
952 if (ngx_buf_size(b) != 0) {
|
|
953 break;
|
|
954 }
|
|
955
|
|
956 #if (NGX_HAVE_WRITE_ZEROCOPY)
|
|
957 if (b->zerocopy_busy) {
|
|
958 break;
|
|
959 }
|
|
960 #endif
|
|
961
|
|
962 if (b->shadow) {
|
|
963 b->shadow->pos = b->shadow->last;
|
|
964 }
|
|
965
|
|
966 ctx->busy = cl->next;
|
194
|
967
|
|
968 if (ngx_buf_in_memory(b) || b->in_file) {
|
|
969 /* add data bufs only to the free buf chain */
|
|
970
|
|
971 cl->next = ctx->free;
|
|
972 ctx->free = cl;
|
|
973 }
|
48
|
974 }
|
|
975
|
46
|
976 return rc;
|
|
977 }
|
|
978
|
|
979
|
|
980 static ngx_int_t
|
|
981 ngx_http_ssi_parse(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
982 {
|
158
|
983 u_char *p, *value, *last, *copy_end, ch;
|
46
|
984 size_t looked;
|
|
985 ngx_http_ssi_state_e state;
|
|
986
|
|
987 state = ctx->state;
|
|
988 looked = ctx->looked;
|
|
989 last = ctx->buf->last;
|
|
990 copy_end = ctx->copy_end;
|
|
991
|
|
992 for (p = ctx->pos; p < last; p++) {
|
|
993
|
|
994 ch = *p;
|
|
995
|
|
996 if (state == ssi_start_state) {
|
|
997
|
|
998 /* the tight loop */
|
|
999
|
48
|
1000 for ( ;; ) {
|
|
1001 if (ch == '<') {
|
|
1002 copy_end = p;
|
|
1003 looked = 1;
|
|
1004 state = ssi_tag_state;
|
|
1005
|
|
1006 goto tag_started;
|
46
|
1007 }
|
|
1008
|
48
|
1009 if (++p == last) {
|
|
1010 break;
|
|
1011 }
|
46
|
1012
|
48
|
1013 ch = *p;
|
46
|
1014 }
|
|
1015
|
|
1016 ctx->pos = p;
|
|
1017 ctx->looked = looked;
|
|
1018 ctx->copy_end = p;
|
|
1019
|
|
1020 if (ctx->copy_start == NULL) {
|
|
1021 ctx->copy_start = ctx->buf->pos;
|
|
1022 }
|
|
1023
|
|
1024 return NGX_AGAIN;
|
|
1025
|
48
|
1026 tag_started:
|
|
1027
|
46
|
1028 continue;
|
|
1029 }
|
|
1030
|
|
1031 switch (state) {
|
|
1032
|
|
1033 case ssi_start_state:
|
|
1034 break;
|
|
1035
|
|
1036 case ssi_tag_state:
|
|
1037 switch (ch) {
|
|
1038 case '!':
|
|
1039 looked = 2;
|
|
1040 state = ssi_comment0_state;
|
|
1041 break;
|
|
1042
|
|
1043 case '<':
|
|
1044 copy_end = p;
|
|
1045 break;
|
|
1046
|
|
1047 default:
|
|
1048 copy_end = p;
|
|
1049 looked = 0;
|
|
1050 state = ssi_start_state;
|
|
1051 break;
|
|
1052 }
|
|
1053
|
|
1054 break;
|
|
1055
|
|
1056 case ssi_comment0_state:
|
|
1057 switch (ch) {
|
|
1058 case '-':
|
|
1059 looked = 3;
|
|
1060 state = ssi_comment1_state;
|
|
1061 break;
|
|
1062
|
|
1063 case '<':
|
|
1064 copy_end = p;
|
|
1065 looked = 1;
|
|
1066 state = ssi_tag_state;
|
|
1067 break;
|
|
1068
|
|
1069 default:
|
|
1070 copy_end = p;
|
|
1071 looked = 0;
|
|
1072 state = ssi_start_state;
|
|
1073 break;
|
|
1074 }
|
|
1075
|
|
1076 break;
|
|
1077
|
|
1078 case ssi_comment1_state:
|
|
1079 switch (ch) {
|
|
1080 case '-':
|
|
1081 looked = 4;
|
|
1082 state = ssi_sharp_state;
|
|
1083 break;
|
|
1084
|
|
1085 case '<':
|
|
1086 copy_end = p;
|
|
1087 looked = 1;
|
|
1088 state = ssi_tag_state;
|
|
1089 break;
|
|
1090
|
|
1091 default:
|
|
1092 copy_end = p;
|
|
1093 looked = 0;
|
|
1094 state = ssi_start_state;
|
|
1095 break;
|
|
1096 }
|
|
1097
|
|
1098 break;
|
|
1099
|
|
1100 case ssi_sharp_state:
|
|
1101 switch (ch) {
|
|
1102 case '#':
|
208
|
1103 if (p - ctx->pos < 4) {
|
|
1104 ctx->saved = 0;
|
|
1105 }
|
46
|
1106 looked = 0;
|
|
1107 state = ssi_precommand_state;
|
|
1108 break;
|
|
1109
|
|
1110 case '<':
|
|
1111 copy_end = p;
|
|
1112 looked = 1;
|
|
1113 state = ssi_tag_state;
|
|
1114 break;
|
|
1115
|
|
1116 default:
|
|
1117 copy_end = p;
|
|
1118 looked = 0;
|
|
1119 state = ssi_start_state;
|
|
1120 break;
|
|
1121 }
|
|
1122
|
|
1123 break;
|
|
1124
|
|
1125 case ssi_precommand_state:
|
|
1126 switch (ch) {
|
|
1127 case ' ':
|
|
1128 case CR:
|
|
1129 case LF:
|
|
1130 case '\t':
|
|
1131 break;
|
|
1132
|
|
1133 default:
|
|
1134 ctx->command.len = 1;
|
|
1135 ctx->command.data = ngx_palloc(r->pool,
|
158
|
1136 NGX_HTTP_SSI_COMMAND_LEN);
|
46
|
1137 if (ctx->command.data == NULL) {
|
|
1138 return NGX_ERROR;
|
|
1139 }
|
|
1140
|
|
1141 ctx->command.data[0] = ch;
|
158
|
1142
|
146
|
1143 ctx->key = 0;
|
|
1144 ctx->key = ngx_hash(ctx->key, ch);
|
|
1145
|
46
|
1146 ctx->params.nelts = 0;
|
178
|
1147
|
46
|
1148 state = ssi_command_state;
|
|
1149 break;
|
|
1150 }
|
|
1151
|
|
1152 break;
|
|
1153
|
|
1154 case ssi_command_state:
|
|
1155 switch (ch) {
|
|
1156 case ' ':
|
|
1157 case CR:
|
|
1158 case LF:
|
|
1159 case '\t':
|
|
1160 state = ssi_preparam_state;
|
|
1161 break;
|
|
1162
|
|
1163 case '-':
|
|
1164 state = ssi_comment_end0_state;
|
|
1165 break;
|
|
1166
|
|
1167 default:
|
|
1168 if (ctx->command.len == NGX_HTTP_SSI_COMMAND_LEN) {
|
|
1169 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1170 "the \"%V%c...\" SSI command is too long",
|
|
1171 &ctx->command, ch);
|
46
|
1172
|
|
1173 state = ssi_error_state;
|
|
1174 break;
|
|
1175 }
|
158
|
1176
|
|
1177 ctx->command.data[ctx->command.len++] = ch;
|
|
1178 ctx->key = ngx_hash(ctx->key, ch);
|
46
|
1179 }
|
|
1180
|
|
1181 break;
|
|
1182
|
|
1183 case ssi_preparam_state:
|
|
1184 switch (ch) {
|
|
1185 case ' ':
|
|
1186 case CR:
|
|
1187 case LF:
|
|
1188 case '\t':
|
|
1189 break;
|
|
1190
|
|
1191 case '-':
|
|
1192 state = ssi_comment_end0_state;
|
|
1193 break;
|
|
1194
|
|
1195 default:
|
48
|
1196 ctx->param = ngx_array_push(&ctx->params);
|
|
1197 if (ctx->param == NULL) {
|
46
|
1198 return NGX_ERROR;
|
|
1199 }
|
|
1200
|
|
1201 ctx->param->key.len = 1;
|
|
1202 ctx->param->key.data = ngx_palloc(r->pool,
|
158
|
1203 NGX_HTTP_SSI_PARAM_LEN);
|
46
|
1204 if (ctx->param->key.data == NULL) {
|
|
1205 return NGX_ERROR;
|
|
1206 }
|
|
1207
|
|
1208 ctx->param->key.data[0] = ch;
|
|
1209
|
|
1210 ctx->param->value.len = 0;
|
158
|
1211
|
|
1212 if (ctx->value_buf == NULL) {
|
|
1213 ctx->param->value.data = ngx_palloc(r->pool,
|
|
1214 ctx->value_len);
|
|
1215 if (ctx->param->value.data == NULL) {
|
|
1216 return NGX_ERROR;
|
|
1217 }
|
|
1218
|
|
1219 } else {
|
|
1220 ctx->param->value.data = ctx->value_buf;
|
46
|
1221 }
|
|
1222
|
|
1223 state = ssi_param_state;
|
|
1224 break;
|
|
1225 }
|
|
1226
|
|
1227 break;
|
|
1228
|
|
1229 case ssi_param_state:
|
|
1230 switch (ch) {
|
|
1231 case ' ':
|
|
1232 case CR:
|
|
1233 case LF:
|
|
1234 case '\t':
|
|
1235 state = ssi_preequal_state;
|
|
1236 break;
|
|
1237
|
|
1238 case '=':
|
|
1239 state = ssi_prevalue_state;
|
|
1240 break;
|
|
1241
|
|
1242 case '-':
|
|
1243 state = ssi_error_end0_state;
|
|
1244
|
|
1245 ctx->param->key.data[ctx->param->key.len++] = ch;
|
|
1246 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1247 "invalid \"%V\" parameter in \"%V\" SSI command",
|
|
1248 &ctx->param->key, &ctx->command);
|
|
1249 break;
|
|
1250
|
|
1251 default:
|
|
1252 if (ctx->param->key.len == NGX_HTTP_SSI_PARAM_LEN) {
|
|
1253 state = ssi_error_state;
|
|
1254 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1255 "too long \"%V%c...\" parameter in "
|
46
|
1256 "\"%V\" SSI command",
|
158
|
1257 &ctx->param->key, ch, &ctx->command);
|
46
|
1258 break;
|
|
1259 }
|
158
|
1260
|
|
1261 ctx->param->key.data[ctx->param->key.len++] = ch;
|
46
|
1262 }
|
|
1263
|
|
1264 break;
|
|
1265
|
|
1266 case ssi_preequal_state:
|
|
1267 switch (ch) {
|
|
1268 case ' ':
|
|
1269 case CR:
|
|
1270 case LF:
|
|
1271 case '\t':
|
|
1272 break;
|
|
1273
|
|
1274 case '=':
|
|
1275 state = ssi_prevalue_state;
|
|
1276 break;
|
|
1277
|
|
1278 default:
|
|
1279 if (ch == '-') {
|
|
1280 state = ssi_error_end0_state;
|
|
1281 } else {
|
|
1282 state = ssi_error_state;
|
|
1283 }
|
|
1284
|
|
1285 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1286 "unexpected \"%c\" symbol after \"%V\" "
|
|
1287 "parameter in \"%V\" SSI command",
|
|
1288 ch, &ctx->param->key, &ctx->command);
|
|
1289 break;
|
|
1290 }
|
|
1291
|
|
1292 break;
|
|
1293
|
|
1294 case ssi_prevalue_state:
|
|
1295 switch (ch) {
|
|
1296 case ' ':
|
|
1297 case CR:
|
|
1298 case LF:
|
|
1299 case '\t':
|
|
1300 break;
|
|
1301
|
|
1302 case '"':
|
|
1303 state = ssi_double_quoted_value_state;
|
|
1304 break;
|
|
1305
|
|
1306 case '\'':
|
|
1307 state = ssi_quoted_value_state;
|
|
1308 break;
|
|
1309
|
|
1310 default:
|
|
1311 if (ch == '-') {
|
|
1312 state = ssi_error_end0_state;
|
|
1313 } else {
|
|
1314 state = ssi_error_state;
|
|
1315 }
|
|
1316
|
|
1317 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1318 "unexpected \"%c\" symbol before value of "
|
|
1319 "\"%V\" parameter in \"%V\" SSI command",
|
|
1320 ch, &ctx->param->key, &ctx->command);
|
|
1321 break;
|
|
1322 }
|
|
1323
|
|
1324 break;
|
|
1325
|
|
1326 case ssi_double_quoted_value_state:
|
|
1327 switch (ch) {
|
108
|
1328 case '"':
|
|
1329 state = ssi_postparam_state;
|
|
1330 break;
|
|
1331
|
46
|
1332 case '\\':
|
|
1333 ctx->saved_state = ssi_double_quoted_value_state;
|
|
1334 state = ssi_quoted_symbol_state;
|
108
|
1335
|
|
1336 /* fall through */
|
46
|
1337
|
|
1338 default:
|
|
1339 if (ctx->param->value.len == ctx->value_len) {
|
|
1340 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1341 "too long \"%V%c...\" value of \"%V\" "
|
|
1342 "parameter in \"%V\" SSI command",
|
|
1343 &ctx->param->value, ch, &ctx->param->key,
|
46
|
1344 &ctx->command);
|
|
1345 state = ssi_error_state;
|
|
1346 break;
|
|
1347 }
|
158
|
1348
|
|
1349 ctx->param->value.data[ctx->param->value.len++] = ch;
|
46
|
1350 }
|
|
1351
|
|
1352 break;
|
|
1353
|
|
1354 case ssi_quoted_value_state:
|
|
1355 switch (ch) {
|
108
|
1356 case '\'':
|
|
1357 state = ssi_postparam_state;
|
|
1358 break;
|
|
1359
|
46
|
1360 case '\\':
|
|
1361 ctx->saved_state = ssi_quoted_value_state;
|
|
1362 state = ssi_quoted_symbol_state;
|
108
|
1363
|
|
1364 /* fall through */
|
46
|
1365
|
|
1366 default:
|
|
1367 if (ctx->param->value.len == ctx->value_len) {
|
|
1368 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1369 "too long \"%V%c...\" value of \"%V\" "
|
|
1370 "parameter in \"%V\" SSI command",
|
|
1371 &ctx->param->value, ch, &ctx->param->key,
|
46
|
1372 &ctx->command);
|
|
1373 state = ssi_error_state;
|
|
1374 break;
|
|
1375 }
|
158
|
1376
|
|
1377 ctx->param->value.data[ctx->param->value.len++] = ch;
|
46
|
1378 }
|
|
1379
|
|
1380 break;
|
|
1381
|
|
1382 case ssi_quoted_symbol_state:
|
108
|
1383 state = ctx->saved_state;
|
|
1384
|
46
|
1385 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1386
|
|
1387 break;
|
|
1388
|
|
1389 case ssi_postparam_state:
|
158
|
1390
|
200
|
1391 if (ctx->param->value.len + 1 < ctx->value_len / 2) {
|
|
1392 value = ngx_palloc(r->pool, ctx->param->value.len + 1);
|
158
|
1393 if (value == NULL) {
|
|
1394 return NGX_ERROR;
|
|
1395 }
|
|
1396
|
|
1397 ngx_memcpy(value, ctx->param->value.data,
|
|
1398 ctx->param->value.len);
|
|
1399
|
|
1400 ctx->value_buf = ctx->param->value.data;
|
|
1401 ctx->param->value.data = value;
|
|
1402
|
|
1403 } else {
|
|
1404 ctx->value_buf = NULL;
|
|
1405 }
|
|
1406
|
46
|
1407 switch (ch) {
|
|
1408 case ' ':
|
|
1409 case CR:
|
|
1410 case LF:
|
|
1411 case '\t':
|
|
1412 state = ssi_preparam_state;
|
|
1413 break;
|
|
1414
|
|
1415 case '-':
|
|
1416 state = ssi_comment_end0_state;
|
|
1417 break;
|
|
1418
|
|
1419 default:
|
|
1420 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1421 "unexpected \"%c\" symbol after \"%V\" value "
|
|
1422 "of \"%V\" parameter in \"%V\" SSI command",
|
|
1423 ch, &ctx->param->value, &ctx->param->key,
|
|
1424 &ctx->command);
|
|
1425 state = ssi_error_state;
|
|
1426 break;
|
|
1427 }
|
|
1428
|
|
1429 break;
|
|
1430
|
|
1431 case ssi_comment_end0_state:
|
|
1432 switch (ch) {
|
|
1433 case '-':
|
|
1434 state = ssi_comment_end1_state;
|
|
1435 break;
|
|
1436
|
|
1437 default:
|
|
1438 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1439 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1440 ch, &ctx->command);
|
|
1441 state = ssi_error_state;
|
|
1442 break;
|
|
1443 }
|
|
1444
|
|
1445 break;
|
|
1446
|
|
1447 case ssi_comment_end1_state:
|
|
1448 switch (ch) {
|
|
1449 case '>':
|
|
1450 ctx->state = ssi_start_state;
|
|
1451 ctx->pos = p + 1;
|
|
1452 ctx->looked = looked;
|
|
1453 ctx->copy_end = copy_end;
|
|
1454
|
|
1455 if (ctx->copy_start == NULL && copy_end) {
|
|
1456 ctx->copy_start = ctx->buf->pos;
|
|
1457 }
|
|
1458
|
|
1459 return NGX_OK;
|
|
1460
|
|
1461 default:
|
|
1462 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1463 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1464 ch, &ctx->command);
|
|
1465 state = ssi_error_state;
|
|
1466 break;
|
|
1467 }
|
|
1468
|
|
1469 break;
|
|
1470
|
|
1471 case ssi_error_state:
|
|
1472 switch (ch) {
|
|
1473 case '-':
|
|
1474 state = ssi_error_end0_state;
|
|
1475 break;
|
|
1476
|
|
1477 default:
|
|
1478 break;
|
|
1479 }
|
|
1480
|
|
1481 break;
|
|
1482
|
|
1483 case ssi_error_end0_state:
|
|
1484 switch (ch) {
|
|
1485 case '-':
|
|
1486 state = ssi_error_end1_state;
|
|
1487 break;
|
|
1488
|
|
1489 default:
|
|
1490 state = ssi_error_state;
|
|
1491 break;
|
|
1492 }
|
|
1493
|
|
1494 break;
|
|
1495
|
|
1496 case ssi_error_end1_state:
|
|
1497 switch (ch) {
|
|
1498 case '>':
|
|
1499 ctx->state = ssi_start_state;
|
|
1500 ctx->pos = p + 1;
|
|
1501 ctx->looked = looked;
|
|
1502 ctx->copy_end = copy_end;
|
|
1503
|
|
1504 if (ctx->copy_start == NULL && copy_end) {
|
|
1505 ctx->copy_start = ctx->buf->pos;
|
|
1506 }
|
|
1507
|
|
1508 return NGX_HTTP_SSI_ERROR;
|
|
1509
|
|
1510 default:
|
|
1511 state = ssi_error_state;
|
|
1512 break;
|
|
1513 }
|
|
1514
|
|
1515 break;
|
|
1516 }
|
|
1517 }
|
|
1518
|
|
1519 ctx->state = state;
|
|
1520 ctx->pos = p;
|
|
1521 ctx->looked = looked;
|
|
1522
|
|
1523 ctx->copy_end = (state == ssi_start_state) ? p : copy_end;
|
|
1524
|
|
1525 if (ctx->copy_start == NULL && ctx->copy_end) {
|
|
1526 ctx->copy_start = ctx->buf->pos;
|
|
1527 }
|
|
1528
|
|
1529 return NGX_AGAIN;
|
|
1530 }
|
|
1531
|
|
1532
|
102
|
1533 static ngx_str_t *
|
160
|
1534 ngx_http_ssi_get_variable(ngx_http_request_t *r, ngx_str_t *name,
|
|
1535 ngx_uint_t key)
|
102
|
1536 {
|
|
1537 ngx_uint_t i;
|
248
|
1538 ngx_list_part_t *part;
|
102
|
1539 ngx_http_ssi_var_t *var;
|
|
1540 ngx_http_ssi_ctx_t *ctx;
|
|
1541
|
|
1542 ctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
1543
|
216
|
1544 if (ctx->variables == NULL) {
|
|
1545 return NULL;
|
|
1546 }
|
|
1547
|
248
|
1548 part = &ctx->variables->part;
|
|
1549 var = part->elts;
|
|
1550
|
|
1551 for (i = 0; /* void */ ; i++) {
|
|
1552
|
|
1553 if (i >= part->nelts) {
|
|
1554 if (part->next == NULL) {
|
|
1555 break;
|
|
1556 }
|
|
1557
|
|
1558 part = part->next;
|
|
1559 var = part->elts;
|
|
1560 i = 0;
|
|
1561 }
|
|
1562
|
102
|
1563 if (name->len != var[i].name.len) {
|
|
1564 continue;
|
|
1565 }
|
|
1566
|
160
|
1567 if (key != var[i].key) {
|
|
1568 continue;
|
|
1569 }
|
|
1570
|
|
1571 if (ngx_strncmp(name->data, var[i].name.data, name->len) == 0) {
|
102
|
1572 return &var[i].value;
|
|
1573 }
|
|
1574 }
|
|
1575
|
|
1576 return NULL;
|
|
1577 }
|
|
1578
|
|
1579
|
|
1580 static ngx_int_t
|
|
1581 ngx_http_ssi_evaluate_string(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1582 ngx_str_t *text, ngx_uint_t flags)
|
|
1583 {
|
122
|
1584 u_char ch, *p, **value, *data, *part_data;
|
|
1585 size_t *size, len, prefix, part_len;
|
|
1586 ngx_str_t var, *val;
|
160
|
1587 ngx_int_t key;
|
200
|
1588 ngx_uint_t i, j, n, bracket, quoted;
|
102
|
1589 ngx_array_t lengths, values;
|
|
1590 ngx_http_variable_value_t *vv;
|
|
1591
|
|
1592 n = ngx_http_script_variables_count(text);
|
|
1593
|
|
1594 if (n == 0) {
|
|
1595
|
206
|
1596 data = text->data;
|
|
1597 p = data;
|
|
1598
|
|
1599 if ((flags & NGX_HTTP_SSI_ADD_PREFIX) && text->data[0] != '/') {
|
|
1600
|
102
|
1601 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1602 if (r->uri.data[prefix - 1] == '/') {
|
|
1603 break;
|
|
1604 }
|
|
1605 }
|
|
1606
|
|
1607 if (prefix) {
|
|
1608 len = prefix + text->len;
|
|
1609
|
|
1610 data = ngx_palloc(r->pool, len);
|
|
1611 if (data == NULL) {
|
|
1612 return NGX_ERROR;
|
|
1613 }
|
|
1614
|
122
|
1615 p = ngx_copy(data, r->uri.data, prefix);
|
102
|
1616 }
|
|
1617 }
|
|
1618
|
206
|
1619 quoted = 0;
|
|
1620
|
|
1621 for (i = 0 ; i < text->len; i++) {
|
|
1622 ch = text->data[i];
|
|
1623
|
|
1624 if (!quoted) {
|
|
1625
|
|
1626 if (ch == '\\') {
|
|
1627 quoted = 1;
|
|
1628 continue;
|
|
1629 }
|
|
1630
|
|
1631 } else {
|
|
1632 quoted = 0;
|
|
1633
|
|
1634 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
|
|
1635 *p++ = '\\';
|
|
1636 }
|
|
1637 }
|
|
1638
|
|
1639 *p++ = ch;
|
|
1640 }
|
|
1641
|
|
1642 text->len = p - data;
|
|
1643 text->data = data;
|
|
1644
|
102
|
1645 return NGX_OK;
|
|
1646 }
|
|
1647
|
|
1648 if (ngx_array_init(&lengths, r->pool, 8, sizeof(size_t *)) != NGX_OK) {
|
|
1649 return NGX_ERROR;
|
|
1650 }
|
|
1651
|
|
1652 if (ngx_array_init(&values, r->pool, 8, sizeof(u_char *)) != NGX_OK) {
|
|
1653 return NGX_ERROR;
|
|
1654 }
|
|
1655
|
|
1656 len = 0;
|
|
1657 i = 0;
|
|
1658
|
|
1659 while (i < text->len) {
|
|
1660
|
|
1661 if (text->data[i] == '$') {
|
|
1662
|
|
1663 var.len = 0;
|
|
1664
|
|
1665 if (++i == text->len) {
|
|
1666 goto invalid_variable;
|
|
1667 }
|
|
1668
|
|
1669 if (text->data[i] == '{') {
|
|
1670 bracket = 1;
|
|
1671
|
|
1672 if (++i == text->len) {
|
|
1673 goto invalid_variable;
|
|
1674 }
|
|
1675
|
|
1676 var.data = &text->data[i];
|
|
1677
|
|
1678 } else {
|
|
1679 bracket = 0;
|
|
1680 var.data = &text->data[i];
|
|
1681 }
|
|
1682
|
|
1683 for ( /* void */ ; i < text->len; i++, var.len++) {
|
|
1684 ch = text->data[i];
|
|
1685
|
|
1686 if (ch == '}' && bracket) {
|
|
1687 i++;
|
|
1688 bracket = 0;
|
|
1689 break;
|
|
1690 }
|
|
1691
|
|
1692 if ((ch >= 'A' && ch <= 'Z')
|
|
1693 || (ch >= 'a' && ch <= 'z')
|
|
1694 || (ch >= '0' && ch <= '9')
|
|
1695 || ch == '_')
|
|
1696 {
|
|
1697 continue;
|
|
1698 }
|
|
1699
|
|
1700 break;
|
|
1701 }
|
|
1702
|
|
1703 if (bracket) {
|
|
1704 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1705 "the closing bracket in \"%V\" "
|
|
1706 "variable is missing", &var);
|
248
|
1707 return NGX_HTTP_SSI_ERROR;
|
102
|
1708 }
|
|
1709
|
126
|
1710 if (var.len == 0) {
|
102
|
1711 goto invalid_variable;
|
|
1712 }
|
|
1713
|
160
|
1714 key = 0;
|
|
1715
|
102
|
1716 for (j = 0; j < var.len; j++) {
|
|
1717 var.data[j] = ngx_tolower(var.data[j]);
|
160
|
1718 key = ngx_hash(key, var.data[j]);
|
102
|
1719 }
|
|
1720
|
160
|
1721 val = ngx_http_ssi_get_variable(r, &var, key);
|
102
|
1722
|
|
1723 if (val == NULL) {
|
184
|
1724 vv = ngx_http_get_variable(r, &var, key,
|
|
1725 flags & NGX_HTTP_SSI_EXPR_TEST);
|
102
|
1726 if (vv == NULL) {
|
|
1727 return NGX_ERROR;
|
|
1728 }
|
|
1729
|
122
|
1730 if (vv->not_found) {
|
102
|
1731 continue;
|
|
1732 }
|
|
1733
|
122
|
1734 part_data = vv->data;
|
|
1735 part_len = vv->len;
|
102
|
1736
|
|
1737 } else {
|
122
|
1738 part_data = val->data;
|
|
1739 part_len = val->len;
|
102
|
1740 }
|
|
1741
|
|
1742 } else {
|
122
|
1743 part_data = &text->data[i];
|
200
|
1744 quoted = 0;
|
122
|
1745
|
|
1746 for (p = part_data; i < text->len; i++) {
|
108
|
1747 ch = text->data[i];
|
|
1748
|
200
|
1749 if (!quoted) {
|
|
1750
|
|
1751 if (ch == '\\') {
|
|
1752 quoted = 1;
|
|
1753 continue;
|
|
1754 }
|
|
1755
|
|
1756 if (ch == '$') {
|
108
|
1757 break;
|
|
1758 }
|
|
1759
|
200
|
1760 } else {
|
|
1761 quoted = 0;
|
|
1762
|
|
1763 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
|
|
1764 *p++ = '\\';
|
|
1765 }
|
108
|
1766 }
|
|
1767
|
|
1768 *p++ = ch;
|
102
|
1769 }
|
108
|
1770
|
122
|
1771 part_len = p - part_data;
|
102
|
1772 }
|
|
1773
|
122
|
1774 len += part_len;
|
102
|
1775
|
|
1776 size = ngx_array_push(&lengths);
|
|
1777 if (size == NULL) {
|
|
1778 return NGX_ERROR;
|
|
1779 }
|
|
1780
|
122
|
1781 *size = part_len;
|
102
|
1782
|
|
1783 value = ngx_array_push(&values);
|
|
1784 if (value == NULL) {
|
|
1785 return NGX_ERROR;
|
|
1786 }
|
|
1787
|
122
|
1788 *value = part_data;
|
102
|
1789 }
|
|
1790
|
|
1791 prefix = 0;
|
|
1792
|
|
1793 size = lengths.elts;
|
|
1794 value = values.elts;
|
|
1795
|
|
1796 if (flags & NGX_HTTP_SSI_ADD_PREFIX) {
|
|
1797 for (i = 0; i < values.nelts; i++) {
|
|
1798 if (size[i] != 0) {
|
|
1799 if (*value[i] != '/') {
|
|
1800 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1801 if (r->uri.data[prefix - 1] == '/') {
|
|
1802 len += prefix;
|
|
1803 break;
|
|
1804 }
|
|
1805 }
|
|
1806 }
|
|
1807
|
|
1808 break;
|
|
1809 }
|
|
1810 }
|
|
1811 }
|
|
1812
|
178
|
1813 p = ngx_palloc(r->pool, len + ((flags & NGX_HTTP_SSI_ADD_ZERO) ? 1 : 0));
|
102
|
1814 if (p == NULL) {
|
|
1815 return NGX_ERROR;
|
|
1816 }
|
|
1817
|
|
1818 text->len = len;
|
|
1819 text->data = p;
|
|
1820
|
122
|
1821 p = ngx_copy(p, r->uri.data, prefix);
|
102
|
1822
|
|
1823 for (i = 0; i < values.nelts; i++) {
|
122
|
1824 p = ngx_copy(p, value[i], size[i]);
|
102
|
1825 }
|
|
1826
|
|
1827 return NGX_OK;
|
|
1828
|
|
1829 invalid_variable:
|
|
1830
|
|
1831 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1832 "invalid variable name in \"%V\"", text);
|
|
1833
|
248
|
1834 return NGX_HTTP_SSI_ERROR;
|
102
|
1835 }
|
|
1836
|
|
1837
|
|
1838 static ngx_int_t
|
|
1839 ngx_http_ssi_include(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1840 ngx_str_t **params)
|
|
1841 {
|
248
|
1842 ngx_int_t rc, key;
|
|
1843 ngx_str_t *uri, *file, *wait, *set, *stub, args;
|
|
1844 ngx_buf_t *b;
|
|
1845 ngx_uint_t flags, i;
|
|
1846 ngx_chain_t *cl, *tl, **ll;
|
|
1847 ngx_http_request_t *sr;
|
|
1848 ngx_http_ssi_var_t *var;
|
|
1849 ngx_http_ssi_ctx_t *mctx;
|
|
1850 ngx_http_ssi_block_t *bl;
|
|
1851 ngx_http_post_subrequest_t *psr;
|
102
|
1852
|
|
1853 uri = params[NGX_HTTP_SSI_INCLUDE_VIRTUAL];
|
|
1854 file = params[NGX_HTTP_SSI_INCLUDE_FILE];
|
194
|
1855 wait = params[NGX_HTTP_SSI_INCLUDE_WAIT];
|
248
|
1856 set = params[NGX_HTTP_SSI_INCLUDE_SET];
|
216
|
1857 stub = params[NGX_HTTP_SSI_INCLUDE_STUB];
|
102
|
1858
|
|
1859 if (uri && file) {
|
|
1860 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1861 "inlcusion may be either virtual=\"%V\" or file=\"%V\"",
|
|
1862 uri, file);
|
|
1863 return NGX_HTTP_SSI_ERROR;
|
|
1864 }
|
|
1865
|
|
1866 if (uri == NULL && file == NULL) {
|
|
1867 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1868 "no parameter in \"include\" SSI command");
|
|
1869 return NGX_HTTP_SSI_ERROR;
|
|
1870 }
|
|
1871
|
248
|
1872 if (set && stub) {
|
|
1873 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1874 "\"set\" and \"stub\" may not be used together "
|
|
1875 "in \"include\" SSI command");
|
|
1876 return NGX_HTTP_SSI_ERROR;
|
|
1877 }
|
|
1878
|
194
|
1879 if (wait) {
|
|
1880 if (uri == NULL) {
|
|
1881 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
216
|
1882 "\"wait\" may not be used with file=\"%V\"", file);
|
194
|
1883 return NGX_HTTP_SSI_ERROR;
|
|
1884 }
|
|
1885
|
|
1886 if (wait->len == 2 && ngx_strncasecmp(wait->data, "no", 2) == 0) {
|
|
1887 wait = NULL;
|
|
1888
|
|
1889 } else if (wait->len != 3 || ngx_strncasecmp(wait->data, "yes", 3) != 0)
|
|
1890 {
|
|
1891 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1892 "invalid value \"%V\" in the \"wait\" parameter",
|
216
|
1893 wait);
|
194
|
1894 return NGX_HTTP_SSI_ERROR;
|
|
1895 }
|
|
1896 }
|
|
1897
|
102
|
1898 if (uri == NULL) {
|
|
1899 uri = file;
|
|
1900 }
|
|
1901
|
248
|
1902 rc = ngx_http_ssi_evaluate_string(r, ctx, uri, NGX_HTTP_SSI_ADD_PREFIX);
|
|
1903
|
|
1904 if (rc != NGX_OK) {
|
|
1905 return rc;
|
102
|
1906 }
|
|
1907
|
|
1908 args.len = 0;
|
|
1909 args.data = NULL;
|
122
|
1910 flags = 0;
|
|
1911
|
|
1912 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1913 "ssi include: \"%V\"", uri);
|
|
1914
|
|
1915 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
|
|
1916 return NGX_HTTP_SSI_ERROR;
|
102
|
1917 }
|
|
1918
|
248
|
1919 psr = NULL;
|
|
1920
|
|
1921 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
216
|
1922
|
|
1923 if (stub) {
|
|
1924 if (mctx->blocks) {
|
|
1925 bl = mctx->blocks->elts;
|
|
1926 for (i = 0; i < mctx->blocks->nelts; i++) {
|
|
1927 if (stub->len == bl[i].name.len
|
|
1928 && ngx_strncmp(stub->data, bl[i].name.data, stub->len) == 0)
|
|
1929 {
|
|
1930 goto found;
|
|
1931 }
|
|
1932 }
|
|
1933 }
|
|
1934
|
|
1935 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1936 "\"stub\"=\"%V\" for \"include\" not found", stub);
|
|
1937 return NGX_HTTP_SSI_ERROR;
|
|
1938
|
|
1939 found:
|
|
1940
|
248
|
1941 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
|
|
1942 if (psr == NULL) {
|
|
1943 return NGX_ERROR;
|
|
1944 }
|
|
1945
|
|
1946 psr->handler = ngx_http_ssi_stub_output;
|
|
1947
|
216
|
1948 if (bl[i].count++) {
|
|
1949
|
248
|
1950 ll = (ngx_chain_t **) &psr->data;
|
216
|
1951
|
|
1952 for (tl = bl[i].bufs; tl; tl = tl->next) {
|
|
1953
|
|
1954 if (ctx->free) {
|
|
1955 cl = ctx->free;
|
|
1956 ctx->free = ctx->free->next;
|
|
1957 b = cl->buf;
|
|
1958
|
|
1959 } else {
|
|
1960 b = ngx_alloc_buf(r->pool);
|
|
1961 if (b == NULL) {
|
|
1962 return NGX_ERROR;
|
|
1963 }
|
|
1964
|
|
1965 cl = ngx_alloc_chain_link(r->pool);
|
|
1966 if (cl == NULL) {
|
|
1967 return NGX_ERROR;
|
|
1968 }
|
|
1969
|
|
1970 cl->buf = b;
|
|
1971 }
|
|
1972
|
|
1973 ngx_memcpy(b, tl->buf, sizeof(ngx_buf_t));
|
|
1974
|
|
1975 b->pos = b->start;
|
|
1976
|
|
1977 *ll = cl;
|
|
1978 cl->next = NULL;
|
|
1979 ll = &cl->next;
|
|
1980 }
|
|
1981
|
|
1982 } else {
|
248
|
1983 psr->data = bl[i].bufs;
|
216
|
1984 }
|
|
1985 }
|
|
1986
|
248
|
1987 if (set) {
|
|
1988 key = 0;
|
|
1989
|
|
1990 for (i = 0; i < set->len; i++) {
|
|
1991 set->data[i] = ngx_tolower(set->data[i]);
|
|
1992 key = ngx_hash(key, set->data[i]);
|
|
1993 }
|
|
1994
|
|
1995 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
|
|
1996 if (psr == NULL) {
|
|
1997 return NGX_ERROR;
|
|
1998 }
|
|
1999
|
|
2000 psr->handler = ngx_http_ssi_set_variable;
|
|
2001 psr->data = ngx_http_ssi_get_variable(r, set, key);
|
|
2002
|
|
2003 if (psr->data == NULL) {
|
|
2004
|
|
2005 if (mctx->variables == NULL) {
|
|
2006 mctx->variables = ngx_list_create(r->pool, 4,
|
|
2007 sizeof(ngx_http_ssi_var_t));
|
|
2008 if (mctx->variables == NULL) {
|
|
2009 return NGX_ERROR;
|
|
2010 }
|
|
2011 }
|
|
2012
|
|
2013 var = ngx_list_push(mctx->variables);
|
|
2014 if (var == NULL) {
|
|
2015 return NGX_ERROR;
|
|
2016 }
|
|
2017
|
|
2018 var->name = *set;
|
|
2019 var->key = key;
|
|
2020 var->value = ngx_http_ssi_null_string;
|
|
2021 psr->data = &var->value;
|
|
2022 }
|
|
2023
|
|
2024 flags |= NGX_HTTP_SUBREQUEST_IN_MEMORY;
|
|
2025 }
|
|
2026
|
|
2027 rc = ngx_http_subrequest(r, uri, &args, &sr, psr, flags);
|
|
2028
|
|
2029 if (rc == NGX_DONE) {
|
|
2030 return NGX_DONE;
|
|
2031 }
|
194
|
2032
|
|
2033 if (rc == NGX_ERROR) {
|
102
|
2034 return NGX_HTTP_SSI_ERROR;
|
|
2035 }
|
|
2036
|
248
|
2037 if (wait == NULL && set == NULL) {
|
194
|
2038 return NGX_OK;
|
|
2039 }
|
|
2040
|
|
2041 if (rc == NGX_AGAIN) {
|
244
|
2042 if (ctx->wait == NULL) {
|
|
2043 ctx->wait = sr;
|
|
2044
|
|
2045 } else {
|
|
2046 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
2047 "only one subrequest may be waited at the same time");
|
|
2048 }
|
194
|
2049 }
|
|
2050
|
|
2051 return rc;
|
102
|
2052 }
|
|
2053
|
|
2054
|
46
|
2055 static ngx_int_t
|
248
|
2056 ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc)
|
|
2057 {
|
|
2058 ngx_chain_t *out;
|
|
2059
|
|
2060 if (rc == NGX_ERROR || r->connection->error || r->request_output) {
|
|
2061 return rc;
|
|
2062 }
|
|
2063
|
|
2064 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2065 "ssi stub output: \"%V?%V\"", &r->uri, &r->args);
|
|
2066
|
|
2067 out = data;
|
|
2068
|
|
2069 if (!r->header_sent) {
|
|
2070 if (ngx_http_set_content_type(r) == NGX_ERROR) {
|
|
2071 return NGX_ERROR;
|
|
2072 }
|
|
2073
|
|
2074 if (ngx_http_send_header(r) == NGX_ERROR) {
|
|
2075 return NGX_ERROR;
|
|
2076 }
|
|
2077 }
|
|
2078
|
|
2079 return ngx_http_output_filter(r, out);
|
|
2080 }
|
|
2081
|
|
2082
|
|
2083 static ngx_int_t
|
|
2084 ngx_http_ssi_set_variable(ngx_http_request_t *r, void *data, ngx_int_t rc)
|
|
2085 {
|
|
2086 ngx_str_t *value = data;
|
|
2087
|
|
2088 if (r->upstream) {
|
|
2089 value->len = r->upstream->buffer.last - r->upstream->buffer.pos;
|
|
2090 value->data = r->upstream->buffer.pos;
|
|
2091 }
|
|
2092
|
|
2093 return rc;
|
|
2094 }
|
|
2095
|
|
2096
|
|
2097 static ngx_int_t
|
46
|
2098 ngx_http_ssi_echo(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2099 ngx_str_t **params)
|
|
2100 {
|
160
|
2101 ngx_int_t key;
|
50
|
2102 ngx_uint_t i;
|
48
|
2103 ngx_buf_t *b;
|
122
|
2104 ngx_str_t *var, *value, text;
|
48
|
2105 ngx_chain_t *cl;
|
50
|
2106 ngx_http_variable_value_t *vv;
|
46
|
2107
|
|
2108 var = params[NGX_HTTP_SSI_ECHO_VAR];
|
|
2109
|
192
|
2110 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2111 "ssi echo \"%V\"", var);
|
|
2112
|
160
|
2113 key = 0;
|
|
2114
|
|
2115 for (i = 0; i < var->len; i++) {
|
|
2116 var->data[i] = ngx_tolower(var->data[i]);
|
|
2117 key = ngx_hash(key, var->data[i]);
|
|
2118 }
|
|
2119
|
|
2120 value = ngx_http_ssi_get_variable(r, var, key);
|
102
|
2121
|
|
2122 if (value == NULL) {
|
184
|
2123 vv = ngx_http_get_variable(r, var, key, 1);
|
102
|
2124
|
|
2125 if (vv == NULL) {
|
|
2126 return NGX_HTTP_SSI_ERROR;
|
|
2127 }
|
|
2128
|
122
|
2129 if (!vv->not_found) {
|
|
2130 text.data = vv->data;
|
|
2131 text.len = vv->len;
|
|
2132 value = &text;
|
102
|
2133 }
|
50
|
2134 }
|
46
|
2135
|
102
|
2136 if (value == NULL) {
|
48
|
2137 value = params[NGX_HTTP_SSI_ECHO_DEFAULT];
|
|
2138
|
|
2139 if (value == NULL) {
|
|
2140 value = &ngx_http_ssi_none;
|
|
2141
|
|
2142 } else if (value->len == 0) {
|
|
2143 return NGX_OK;
|
|
2144 }
|
|
2145
|
|
2146 } else {
|
|
2147 if (value->len == 0) {
|
|
2148 return NGX_OK;
|
|
2149 }
|
|
2150 }
|
|
2151
|
|
2152 b = ngx_calloc_buf(r->pool);
|
|
2153 if (b == NULL) {
|
|
2154 return NGX_HTTP_SSI_ERROR;
|
|
2155 }
|
|
2156
|
|
2157 cl = ngx_alloc_chain_link(r->pool);
|
|
2158 if (cl == NULL) {
|
46
|
2159 return NGX_HTTP_SSI_ERROR;
|
|
2160 }
|
|
2161
|
|
2162 b->memory = 1;
|
|
2163 b->pos = value->data;
|
|
2164 b->last = value->data + value->len;
|
|
2165
|
|
2166 cl->buf = b;
|
|
2167 cl->next = NULL;
|
|
2168 *ctx->last_out = cl;
|
|
2169 ctx->last_out = &cl->next;
|
|
2170
|
|
2171 return NGX_OK;
|
|
2172 }
|
|
2173
|
|
2174
|
58
|
2175 static ngx_int_t
|
|
2176 ngx_http_ssi_config(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2177 ngx_str_t **params)
|
|
2178 {
|
|
2179 ngx_str_t *value;
|
|
2180
|
|
2181 value = params[NGX_HTTP_SSI_CONFIG_TIMEFMT];
|
|
2182
|
|
2183 if (value) {
|
138
|
2184 ctx->timefmt.len = value->len;
|
|
2185 ctx->timefmt.data = ngx_palloc(r->pool, value->len + 1);
|
|
2186 if (ctx->timefmt.data == NULL) {
|
|
2187 return NGX_HTTP_SSI_ERROR;
|
|
2188 }
|
|
2189
|
|
2190 ngx_cpystrn(ctx->timefmt.data, value->data, value->len + 1);
|
58
|
2191 }
|
|
2192
|
96
|
2193 value = params[NGX_HTTP_SSI_CONFIG_ERRMSG];
|
|
2194
|
|
2195 if (value) {
|
|
2196 ctx->errmsg = *value;
|
|
2197 }
|
|
2198
|
58
|
2199 return NGX_OK;
|
|
2200 }
|
|
2201
|
|
2202
|
|
2203 static ngx_int_t
|
102
|
2204 ngx_http_ssi_set(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
58
|
2205 ngx_str_t **params)
|
|
2206 {
|
248
|
2207 ngx_int_t key, rc;
|
160
|
2208 ngx_uint_t i;
|
102
|
2209 ngx_str_t *name, *value, *vv;
|
|
2210 ngx_http_ssi_var_t *var;
|
|
2211 ngx_http_ssi_ctx_t *mctx;
|
|
2212
|
|
2213 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
2214
|
216
|
2215 if (mctx->variables == NULL) {
|
248
|
2216 mctx->variables = ngx_list_create(r->pool, 4,
|
|
2217 sizeof(ngx_http_ssi_var_t));
|
216
|
2218 if (mctx->variables == NULL) {
|
248
|
2219 return NGX_ERROR;
|
58
|
2220 }
|
|
2221 }
|
|
2222
|
102
|
2223 name = params[NGX_HTTP_SSI_SET_VAR];
|
|
2224 value = params[NGX_HTTP_SSI_SET_VALUE];
|
|
2225
|
192
|
2226 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2227 "ssi set \"%V\" \"%V\"", name, value);
|
|
2228
|
248
|
2229 rc = ngx_http_ssi_evaluate_string(r, ctx, value, 0);
|
|
2230
|
|
2231 if (rc != NGX_OK) {
|
|
2232 return rc;
|
58
|
2233 }
|
|
2234
|
160
|
2235 key = 0;
|
|
2236
|
|
2237 for (i = 0; i < name->len; i++) {
|
|
2238 name->data[i] = ngx_tolower(name->data[i]);
|
|
2239 key = ngx_hash(key, name->data[i]);
|
|
2240 }
|
|
2241
|
|
2242 vv = ngx_http_ssi_get_variable(r, name, key);
|
102
|
2243
|
|
2244 if (vv) {
|
|
2245 *vv = *value;
|
|
2246 return NGX_OK;
|
|
2247 }
|
|
2248
|
248
|
2249 var = ngx_list_push(mctx->variables);
|
102
|
2250 if (var == NULL) {
|
248
|
2251 return NGX_ERROR;
|
102
|
2252 }
|
|
2253
|
|
2254 var->name = *name;
|
160
|
2255 var->key = key;
|
102
|
2256 var->value = *value;
|
|
2257
|
|
2258 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2259 "set: \"%V\"=\"%V\"", name, value);
|
|
2260
|
58
|
2261 return NGX_OK;
|
|
2262 }
|
|
2263
|
|
2264
|
|
2265 static ngx_int_t
|
|
2266 ngx_http_ssi_if(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2267 ngx_str_t **params)
|
|
2268 {
|
102
|
2269 u_char *p, *last;
|
|
2270 ngx_str_t *expr, left, right;
|
|
2271 ngx_int_t rc;
|
178
|
2272 ngx_uint_t negative, noregex, flags;
|
100
|
2273 #if (NGX_PCRE)
|
102
|
2274 ngx_str_t err;
|
|
2275 ngx_regex_t *regex;
|
|
2276 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
100
|
2277 #endif
|
58
|
2278
|
178
|
2279 if (ctx->command.len == 2) {
|
|
2280 if (ctx->conditional) {
|
|
2281 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
2282 "the \"if\" command inside the \"if\" command");
|
|
2283 return NGX_HTTP_SSI_ERROR;
|
|
2284 }
|
|
2285 }
|
|
2286
|
|
2287 if (ctx->output_chosen) {
|
|
2288 ctx->output = 0;
|
|
2289 return NGX_OK;
|
|
2290 }
|
|
2291
|
58
|
2292 expr = params[NGX_HTTP_SSI_IF_EXPR];
|
|
2293
|
192
|
2294 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2295 "ssi if expr=\"%V\"", expr);
|
|
2296
|
102
|
2297 left.data = expr->data;
|
100
|
2298 last = expr->data + expr->len;
|
|
2299
|
102
|
2300 for (p = left.data; p < last; p++) {
|
100
|
2301 if (*p >= 'A' && *p <= 'Z') {
|
|
2302 *p |= 0x20;
|
|
2303 continue;
|
|
2304 }
|
58
|
2305
|
102
|
2306 if ((*p >= 'a' && *p <= 'z')
|
|
2307 || (*p >= '0' && *p <= '9')
|
122
|
2308 || *p == '$' || *p == '{' || *p == '}' || *p == '_'
|
|
2309 || *p == '"' || *p == '\'')
|
102
|
2310 {
|
100
|
2311 continue;
|
|
2312 }
|
|
2313
|
|
2314 break;
|
|
2315 }
|
|
2316
|
102
|
2317 left.len = p - left.data;
|
100
|
2318
|
|
2319 while (p < last && *p == ' ') {
|
|
2320 p++;
|
58
|
2321 }
|
|
2322
|
184
|
2323 flags = (p == last) ? NGX_HTTP_SSI_EXPR_TEST : 0;
|
|
2324
|
102
|
2325 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2326 "left: \"%V\"", &left);
|
|
2327
|
248
|
2328 rc = ngx_http_ssi_evaluate_string(r, ctx, &left, flags);
|
|
2329
|
|
2330 if (rc != NGX_OK) {
|
|
2331 return rc;
|
58
|
2332 }
|
|
2333
|
102
|
2334 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2335 "evaluted left: \"%V\"", &left);
|
|
2336
|
100
|
2337 if (p == last) {
|
102
|
2338 if (left.len) {
|
100
|
2339 ctx->output = 1;
|
178
|
2340 ctx->output_chosen = 1;
|
100
|
2341
|
|
2342 } else {
|
|
2343 ctx->output = 0;
|
|
2344 }
|
|
2345
|
178
|
2346 ctx->conditional = NGX_HTTP_SSI_COND_IF;
|
|
2347
|
100
|
2348 return NGX_OK;
|
|
2349 }
|
|
2350
|
|
2351 if (p < last && *p == '=') {
|
|
2352 negative = 0;
|
|
2353 p++;
|
|
2354
|
|
2355 } else if (p + 1 < last && *p == '!' && *(p + 1) == '=') {
|
|
2356 negative = 1;
|
|
2357 p += 2;
|
|
2358
|
|
2359 } else {
|
|
2360 goto invalid_expression;
|
|
2361 }
|
|
2362
|
|
2363 while (p < last && *p == ' ') {
|
|
2364 p++;
|
|
2365 }
|
|
2366
|
|
2367 if (p < last && *p == '/') {
|
|
2368 if (*(last - 1) != '/') {
|
|
2369 goto invalid_expression;
|
|
2370 }
|
|
2371
|
|
2372 noregex = 0;
|
178
|
2373 flags = NGX_HTTP_SSI_ADD_ZERO;
|
100
|
2374 last--;
|
|
2375 p++;
|
|
2376
|
|
2377 } else {
|
|
2378 noregex = 1;
|
178
|
2379 flags = 0;
|
|
2380
|
|
2381 if (p < last - 1 && p[0] == '\\' && p[1] == '/') {
|
200
|
2382 p++;
|
178
|
2383 }
|
100
|
2384 }
|
|
2385
|
|
2386 right.len = last - p;
|
|
2387 right.data = p;
|
|
2388
|
102
|
2389 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2390 "right: \"%V\"", &right);
|
|
2391
|
248
|
2392 rc = ngx_http_ssi_evaluate_string(r, ctx, &right, flags);
|
|
2393
|
|
2394 if (rc != NGX_OK) {
|
|
2395 return rc;
|
100
|
2396 }
|
|
2397
|
102
|
2398 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2399 "evaluted right: \"%V\"", &right);
|
100
|
2400
|
|
2401 if (noregex) {
|
|
2402 if (left.len != right.len) {
|
|
2403 rc = -1;
|
|
2404
|
|
2405 } else {
|
|
2406 rc = ngx_strncmp(left.data, right.data, right.len);
|
|
2407 }
|
|
2408
|
|
2409 } else {
|
|
2410 #if (NGX_PCRE)
|
|
2411 err.len = NGX_MAX_CONF_ERRSTR;
|
|
2412 err.data = errstr;
|
|
2413
|
|
2414 right.data[right.len] = '\0';
|
|
2415
|
|
2416 regex = ngx_regex_compile(&right, 0, r->pool, &err);
|
|
2417
|
|
2418 if (regex == NULL) {
|
|
2419 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", err.data);
|
|
2420 return NGX_HTTP_SSI_ERROR;
|
|
2421 }
|
|
2422
|
|
2423 rc = ngx_regex_exec(regex, &left, NULL, 0);
|
|
2424
|
|
2425 if (rc != NGX_REGEX_NO_MATCHED && rc < 0) {
|
|
2426 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
2427 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
|
|
2428 rc, &left, &right);
|
|
2429 return NGX_HTTP_SSI_ERROR;
|
|
2430 }
|
|
2431 #else
|
|
2432 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
2433 "the using of the regex \"%V\" in SSI "
|
|
2434 "requires PCRE library", &right);
|
|
2435
|
|
2436 return NGX_HTTP_SSI_ERROR;
|
|
2437 #endif
|
|
2438 }
|
|
2439
|
|
2440 if ((rc == 0 && !negative) || (rc != 0 && negative)) {
|
58
|
2441 ctx->output = 1;
|
178
|
2442 ctx->output_chosen = 1;
|
58
|
2443
|
|
2444 } else {
|
|
2445 ctx->output = 0;
|
|
2446 }
|
|
2447
|
178
|
2448 ctx->conditional = NGX_HTTP_SSI_COND_IF;
|
|
2449
|
58
|
2450 return NGX_OK;
|
100
|
2451
|
|
2452 invalid_expression:
|
|
2453
|
|
2454 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
2455 "invalid expression in \"%V\"", expr);
|
|
2456
|
|
2457 return NGX_HTTP_SSI_ERROR;
|
58
|
2458 }
|
|
2459
|
|
2460
|
|
2461 static ngx_int_t
|
|
2462 ngx_http_ssi_else(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2463 ngx_str_t **params)
|
|
2464 {
|
192
|
2465 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2466 "ssi else");
|
|
2467
|
178
|
2468 if (ctx->output_chosen) {
|
|
2469 ctx->output = 0;
|
|
2470 } else {
|
|
2471 ctx->output = 1;
|
|
2472 }
|
|
2473
|
|
2474 ctx->conditional = NGX_HTTP_SSI_COND_ELSE;
|
58
|
2475
|
|
2476 return NGX_OK;
|
|
2477 }
|
|
2478
|
|
2479
|
|
2480 static ngx_int_t
|
|
2481 ngx_http_ssi_endif(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2482 ngx_str_t **params)
|
|
2483 {
|
192
|
2484 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2485 "ssi endif");
|
|
2486
|
58
|
2487 ctx->output = 1;
|
178
|
2488 ctx->output_chosen = 0;
|
|
2489 ctx->conditional = 0;
|
58
|
2490
|
|
2491 return NGX_OK;
|
|
2492 }
|
|
2493
|
|
2494
|
122
|
2495 static ngx_int_t
|
216
|
2496 ngx_http_ssi_block(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2497 ngx_str_t **params)
|
|
2498 {
|
|
2499 ngx_http_ssi_ctx_t *mctx;
|
|
2500 ngx_http_ssi_block_t *bl;
|
|
2501
|
|
2502 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2503 "ssi block");
|
|
2504
|
|
2505 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
2506
|
|
2507 if (mctx->blocks == NULL) {
|
|
2508 mctx->blocks = ngx_array_create(r->pool, 4,
|
|
2509 sizeof(ngx_http_ssi_block_t));
|
|
2510 if (mctx->blocks == NULL) {
|
|
2511 return NGX_HTTP_SSI_ERROR;
|
|
2512 }
|
|
2513 }
|
|
2514
|
|
2515 bl = ngx_array_push(mctx->blocks);
|
|
2516 if (bl == NULL) {
|
|
2517 return NGX_HTTP_SSI_ERROR;
|
|
2518 }
|
|
2519
|
|
2520 bl->name = *params[NGX_HTTP_SSI_BLOCK_NAME];
|
|
2521 bl->bufs = NULL;
|
|
2522 bl->count = 0;
|
|
2523
|
|
2524 ctx->output = 0;
|
|
2525 ctx->block = 1;
|
|
2526
|
|
2527 return NGX_OK;
|
|
2528 }
|
|
2529
|
|
2530
|
|
2531 static ngx_int_t
|
|
2532 ngx_http_ssi_endblock(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2533 ngx_str_t **params)
|
|
2534 {
|
|
2535 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2536 "ssi endblock");
|
|
2537
|
|
2538 ctx->output = 1;
|
|
2539 ctx->block = 0;
|
|
2540
|
|
2541 return NGX_OK;
|
|
2542 }
|
|
2543
|
|
2544
|
|
2545 static ngx_int_t
|
122
|
2546 ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
|
|
2547 ngx_http_variable_value_t *v, uintptr_t gmt)
|
58
|
2548 {
|
122
|
2549 ngx_http_ssi_ctx_t *ctx;
|
|
2550 ngx_time_t *tp;
|
|
2551 struct tm tm;
|
|
2552 char buf[NGX_HTTP_SSI_DATE_LEN];
|
|
2553
|
126
|
2554 v->valid = 1;
|
122
|
2555 v->no_cachable = 0;
|
|
2556 v->not_found = 0;
|
58
|
2557
|
112
|
2558 tp = ngx_timeofday();
|
|
2559
|
58
|
2560 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
|
|
2561
|
|
2562 if (ctx->timefmt.len == sizeof("%s") - 1
|
|
2563 && ctx->timefmt.data[0] == '%' && ctx->timefmt.data[1] == 's')
|
|
2564 {
|
122
|
2565 v->data = ngx_palloc(r->pool, NGX_TIME_T_LEN);
|
|
2566 if (v->data == NULL) {
|
|
2567 return NGX_ERROR;
|
58
|
2568 }
|
|
2569
|
122
|
2570 v->len = ngx_sprintf(v->data, "%T", tp->sec + (gmt ? 0 : tp->gmtoff))
|
|
2571 - v->data;
|
206
|
2572
|
122
|
2573 return NGX_OK;
|
58
|
2574 }
|
|
2575
|
|
2576 if (gmt) {
|
112
|
2577 ngx_libc_gmtime(tp->sec, &tm);
|
58
|
2578 } else {
|
112
|
2579 ngx_libc_localtime(tp->sec, &tm);
|
58
|
2580 }
|
|
2581
|
122
|
2582 v->len = strftime(buf, NGX_HTTP_SSI_DATE_LEN,
|
|
2583 (char *) ctx->timefmt.data, &tm);
|
|
2584 if (v->len == 0) {
|
|
2585 return NGX_ERROR;
|
58
|
2586 }
|
|
2587
|
122
|
2588 v->data = ngx_palloc(r->pool, v->len);
|
|
2589 if (v->data == NULL) {
|
|
2590 return NGX_ERROR;
|
58
|
2591 }
|
|
2592
|
122
|
2593 ngx_memcpy(v->data, buf, v->len);
|
|
2594
|
|
2595 return NGX_OK;
|
58
|
2596 }
|
|
2597
|
|
2598
|
76
|
2599 static char *
|
|
2600 ngx_http_ssi_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2601 {
|
146
|
2602 ngx_http_ssi_loc_conf_t *slcf = conf;
|
76
|
2603
|
|
2604 ngx_str_t *value, *type;
|
|
2605 ngx_uint_t i;
|
|
2606
|
146
|
2607 if (slcf->types == NULL) {
|
|
2608 slcf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
|
|
2609 if (slcf->types == NULL) {
|
76
|
2610 return NGX_CONF_ERROR;
|
|
2611 }
|
|
2612
|
146
|
2613 type = ngx_array_push(slcf->types);
|
76
|
2614 if (type == NULL) {
|
|
2615 return NGX_CONF_ERROR;
|
|
2616 }
|
|
2617
|
|
2618 type->len = sizeof("text/html") - 1;
|
|
2619 type->data = (u_char *) "text/html";
|
|
2620 }
|
|
2621
|
|
2622 value = cf->args->elts;
|
|
2623
|
|
2624 for (i = 1; i < cf->args->nelts; i++) {
|
|
2625
|
|
2626 if (ngx_strcmp(value[i].data, "text/html") == 0) {
|
|
2627 continue;
|
|
2628 }
|
|
2629
|
146
|
2630 type = ngx_array_push(slcf->types);
|
76
|
2631 if (type == NULL) {
|
|
2632 return NGX_CONF_ERROR;
|
|
2633 }
|
|
2634
|
|
2635 type->len = value[i].len;
|
|
2636
|
|
2637 type->data = ngx_palloc(cf->pool, type->len + 1);
|
|
2638 if (type->data == NULL) {
|
|
2639 return NGX_CONF_ERROR;
|
|
2640 }
|
|
2641
|
|
2642 ngx_cpystrn(type->data, value[i].data, type->len + 1);
|
|
2643 }
|
|
2644
|
|
2645 return NGX_CONF_OK;
|
|
2646 }
|
|
2647
|
|
2648
|
58
|
2649 static ngx_int_t
|
146
|
2650 ngx_http_ssi_preconfiguration(ngx_conf_t *cf)
|
58
|
2651 {
|
146
|
2652 ngx_int_t rc;
|
|
2653 ngx_http_variable_t *var, *v;
|
|
2654 ngx_http_ssi_command_t *cmd;
|
|
2655 ngx_http_ssi_main_conf_t *smcf;
|
58
|
2656
|
|
2657 for (v = ngx_http_ssi_vars; v->name.len; v++) {
|
|
2658 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2659 if (var == NULL) {
|
|
2660 return NGX_ERROR;
|
|
2661 }
|
|
2662
|
186
|
2663 var->get_handler = v->get_handler;
|
58
|
2664 var->data = v->data;
|
|
2665 }
|
|
2666
|
146
|
2667 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
|
|
2668
|
|
2669 for (cmd = ngx_http_ssi_commands; cmd->name.len; cmd++) {
|
|
2670 rc = ngx_hash_add_key(&smcf->commands, &cmd->name, cmd,
|
|
2671 NGX_HASH_READONLY_KEY);
|
|
2672
|
|
2673 if (rc == NGX_OK) {
|
|
2674 continue;
|
|
2675 }
|
|
2676
|
|
2677 if (rc == NGX_BUSY) {
|
|
2678 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2679 "conflicting SSI command \"%V\"", &cmd->name);
|
|
2680 }
|
|
2681
|
|
2682 return NGX_ERROR;
|
|
2683 }
|
|
2684
|
126
|
2685 return NGX_OK;
|
58
|
2686 }
|
|
2687
|
|
2688
|
46
|
2689 static void *
|
146
|
2690 ngx_http_ssi_create_main_conf(ngx_conf_t *cf)
|
|
2691 {
|
|
2692 ngx_http_ssi_main_conf_t *smcf;
|
|
2693
|
|
2694 smcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_main_conf_t));
|
|
2695 if (smcf == NULL) {
|
|
2696 return NGX_CONF_ERROR;
|
|
2697 }
|
|
2698
|
|
2699 smcf->commands.pool = cf->pool;
|
|
2700 smcf->commands.temp_pool = cf->temp_pool;
|
|
2701
|
|
2702 if (ngx_hash_keys_array_init(&smcf->commands, NGX_HASH_SMALL) != NGX_OK) {
|
|
2703 return NGX_CONF_ERROR;
|
|
2704 }
|
|
2705
|
|
2706 return smcf;
|
|
2707 }
|
|
2708
|
|
2709
|
|
2710 static char *
|
|
2711 ngx_http_ssi_init_main_conf(ngx_conf_t *cf, void *conf)
|
|
2712 {
|
|
2713 ngx_http_ssi_main_conf_t *smcf = conf;
|
|
2714
|
|
2715 ngx_hash_init_t hash;
|
|
2716
|
|
2717 hash.hash = &smcf->hash;
|
|
2718 hash.key = ngx_hash_key;
|
|
2719 hash.max_size = 1024;
|
|
2720 hash.bucket_size = ngx_cacheline_size;
|
|
2721 hash.name = "ssi_command_hash";
|
|
2722 hash.pool = cf->pool;
|
|
2723 hash.temp_pool = NULL;
|
|
2724
|
|
2725 if (ngx_hash_init(&hash, smcf->commands.keys.elts,
|
|
2726 smcf->commands.keys.nelts)
|
|
2727 != NGX_OK)
|
|
2728 {
|
|
2729 return NGX_CONF_ERROR;
|
|
2730 }
|
|
2731
|
|
2732 return NGX_CONF_OK;
|
|
2733 }
|
|
2734
|
|
2735
|
|
2736 static void *
|
46
|
2737 ngx_http_ssi_create_conf(ngx_conf_t *cf)
|
|
2738 {
|
146
|
2739 ngx_http_ssi_loc_conf_t *slcf;
|
|
2740
|
|
2741 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_loc_conf_t));
|
|
2742 if (slcf == NULL) {
|
46
|
2743 return NGX_CONF_ERROR;
|
|
2744 }
|
|
2745
|
76
|
2746 /*
|
|
2747 * set by ngx_pcalloc():
|
|
2748 *
|
|
2749 * conf->types = NULL;
|
|
2750 */
|
|
2751
|
146
|
2752 slcf->enable = NGX_CONF_UNSET;
|
|
2753 slcf->silent_errors = NGX_CONF_UNSET;
|
|
2754 slcf->ignore_recycled_buffers = NGX_CONF_UNSET;
|
|
2755
|
|
2756 slcf->min_file_chunk = NGX_CONF_UNSET_SIZE;
|
|
2757 slcf->value_len = NGX_CONF_UNSET_SIZE;
|
|
2758
|
|
2759 return slcf;
|
46
|
2760 }
|
|
2761
|
|
2762
|
|
2763 static char *
|
|
2764 ngx_http_ssi_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2765 {
|
146
|
2766 ngx_http_ssi_loc_conf_t *prev = parent;
|
|
2767 ngx_http_ssi_loc_conf_t *conf = child;
|
46
|
2768
|
76
|
2769 ngx_str_t *type;
|
|
2770
|
46
|
2771 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
2772 ngx_conf_merge_value(conf->silent_errors, prev->silent_errors, 0);
|
58
|
2773 ngx_conf_merge_value(conf->ignore_recycled_buffers,
|
|
2774 prev->ignore_recycled_buffers, 0);
|
46
|
2775
|
|
2776 ngx_conf_merge_size_value(conf->min_file_chunk, prev->min_file_chunk, 1024);
|
|
2777 ngx_conf_merge_size_value(conf->value_len, prev->value_len, 256);
|
|
2778
|
76
|
2779 if (conf->types == NULL) {
|
|
2780 if (prev->types == NULL) {
|
|
2781 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
|
|
2782 if (conf->types == NULL) {
|
|
2783 return NGX_CONF_ERROR;
|
|
2784 }
|
|
2785
|
|
2786 type = ngx_array_push(conf->types);
|
|
2787 if (type == NULL) {
|
|
2788 return NGX_CONF_ERROR;
|
|
2789 }
|
|
2790
|
|
2791 type->len = sizeof("text/html") - 1;
|
|
2792 type->data = (u_char *) "text/html";
|
|
2793
|
|
2794 } else {
|
|
2795 conf->types = prev->types;
|
|
2796 }
|
|
2797 }
|
|
2798
|
46
|
2799 return NGX_CONF_OK;
|
|
2800 }
|
|
2801
|
|
2802
|
|
2803 static ngx_int_t
|
230
|
2804 ngx_http_ssi_filter_init(ngx_conf_t *cf)
|
46
|
2805 {
|
|
2806 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
2807 ngx_http_top_header_filter = ngx_http_ssi_header_filter;
|
|
2808
|
|
2809 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
2810 ngx_http_top_body_filter = ngx_http_ssi_body_filter;
|
|
2811
|
|
2812 return NGX_OK;
|
|
2813 }
|