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);
|
296
|
110 static void *ngx_http_ssi_create_loc_conf(ngx_conf_t *cf);
|
|
111 static char *ngx_http_ssi_merge_loc_conf(ngx_conf_t *cf,
|
46
|
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
|
296
|
176 ngx_http_ssi_create_loc_conf, /* create location configuration */
|
|
177 ngx_http_ssi_merge_loc_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
|
314
|
803 if (cmd->flush) {
|
|
804
|
|
805 if (ctx->out) {
|
|
806 rc = ngx_http_ssi_output(r, ctx);
|
|
807
|
|
808 } else {
|
|
809 rc = ngx_http_next_body_filter(r, NULL);
|
|
810 }
|
58
|
811
|
|
812 if (rc == NGX_ERROR) {
|
|
813 return NGX_ERROR;
|
|
814 }
|
|
815 }
|
|
816
|
118
|
817 rc = cmd->handler(r, ctx, params);
|
|
818
|
|
819 if (rc == NGX_OK) {
|
46
|
820 continue;
|
|
821 }
|
132
|
822
|
248
|
823 if (rc == NGX_DONE || rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
824 return rc;
|
132
|
825 }
|
46
|
826 }
|
|
827
|
|
828
|
|
829 /* rc == NGX_HTTP_SSI_ERROR */
|
|
830
|
48
|
831 ssi_error:
|
46
|
832
|
146
|
833 if (slcf->silent_errors) {
|
46
|
834 continue;
|
|
835 }
|
|
836
|
48
|
837 if (ctx->free) {
|
|
838 cl = ctx->free;
|
|
839 ctx->free = ctx->free->next;
|
|
840 b = cl->buf;
|
|
841 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
842
|
|
843 } else {
|
|
844 b = ngx_calloc_buf(r->pool);
|
|
845 if (b == NULL) {
|
|
846 return NGX_ERROR;
|
|
847 }
|
|
848
|
|
849 cl = ngx_alloc_chain_link(r->pool);
|
|
850 if (cl == NULL) {
|
|
851 return NGX_ERROR;
|
|
852 }
|
|
853
|
|
854 cl->buf = b;
|
46
|
855 }
|
|
856
|
|
857 b->memory = 1;
|
96
|
858 b->pos = ctx->errmsg.data;
|
|
859 b->last = ctx->errmsg.data + ctx->errmsg.len;
|
46
|
860
|
|
861 cl->next = NULL;
|
|
862 *ctx->last_out = cl;
|
|
863 ctx->last_out = &cl->next;
|
|
864
|
|
865 continue;
|
|
866 }
|
|
867
|
236
|
868 if (ctx->buf->last_buf || ngx_buf_in_memory(ctx->buf)) {
|
48
|
869 if (b == NULL) {
|
|
870 if (ctx->free) {
|
|
871 cl = ctx->free;
|
|
872 ctx->free = ctx->free->next;
|
|
873 b = cl->buf;
|
|
874 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
875
|
|
876 } else {
|
|
877 b = ngx_calloc_buf(r->pool);
|
|
878 if (b == NULL) {
|
|
879 return NGX_ERROR;
|
|
880 }
|
46
|
881
|
48
|
882 cl = ngx_alloc_chain_link(r->pool);
|
|
883 if (cl == NULL) {
|
|
884 return NGX_ERROR;
|
|
885 }
|
|
886
|
|
887 cl->buf = b;
|
|
888 }
|
|
889
|
58
|
890 b->sync = 1;
|
|
891
|
48
|
892 cl->next = NULL;
|
|
893 *ctx->last_out = cl;
|
|
894 ctx->last_out = &cl->next;
|
|
895 }
|
|
896
|
|
897 b->last_buf = ctx->buf->last_buf;
|
|
898 b->shadow = ctx->buf;
|
58
|
899
|
146
|
900 if (slcf->ignore_recycled_buffers == 0) {
|
58
|
901 b->recycled = ctx->buf->recycled;
|
|
902 }
|
46
|
903 }
|
|
904
|
48
|
905 ctx->buf = NULL;
|
|
906
|
46
|
907 ctx->saved = ctx->looked;
|
|
908 }
|
|
909
|
48
|
910 if (ctx->out == NULL && ctx->busy == NULL) {
|
46
|
911 return NGX_OK;
|
|
912 }
|
|
913
|
58
|
914 return ngx_http_ssi_output(r, ctx);
|
|
915 }
|
|
916
|
|
917
|
|
918 static ngx_int_t
|
|
919 ngx_http_ssi_output(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
920 {
|
|
921 ngx_int_t rc;
|
|
922 ngx_buf_t *b;
|
|
923 ngx_chain_t *cl;
|
|
924
|
190
|
925 #if 1
|
|
926 b = NULL;
|
|
927 for (cl = ctx->out; cl; cl = cl->next) {
|
192
|
928 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
929 "ssi out: %p %p", cl->buf, cl->buf->pos);
|
190
|
930 if (cl->buf == b) {
|
|
931 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
932 "the same buf was used in ssi");
|
|
933 ngx_debug_point();
|
|
934 return NGX_ERROR;
|
|
935 }
|
|
936 b = cl->buf;
|
|
937 }
|
|
938 #endif
|
|
939
|
46
|
940 rc = ngx_http_next_body_filter(r, ctx->out);
|
|
941
|
48
|
942 if (ctx->busy == NULL) {
|
|
943 ctx->busy = ctx->out;
|
|
944
|
|
945 } else {
|
|
946 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
|
|
947 cl->next = ctx->out;
|
|
948 }
|
|
949
|
46
|
950 ctx->out = NULL;
|
|
951 ctx->last_out = &ctx->out;
|
|
952
|
48
|
953 while (ctx->busy) {
|
|
954
|
206
|
955 cl = ctx->busy;
|
|
956 b = cl->buf;
|
48
|
957
|
|
958 if (ngx_buf_size(b) != 0) {
|
|
959 break;
|
|
960 }
|
|
961
|
|
962 #if (NGX_HAVE_WRITE_ZEROCOPY)
|
|
963 if (b->zerocopy_busy) {
|
|
964 break;
|
|
965 }
|
|
966 #endif
|
|
967
|
|
968 if (b->shadow) {
|
|
969 b->shadow->pos = b->shadow->last;
|
|
970 }
|
|
971
|
|
972 ctx->busy = cl->next;
|
194
|
973
|
|
974 if (ngx_buf_in_memory(b) || b->in_file) {
|
|
975 /* add data bufs only to the free buf chain */
|
|
976
|
|
977 cl->next = ctx->free;
|
|
978 ctx->free = cl;
|
|
979 }
|
48
|
980 }
|
|
981
|
254
|
982 if (ctx->in || ctx->buf) {
|
|
983 r->buffered |= NGX_HTTP_SSI_BUFFERED;
|
|
984
|
|
985 } else {
|
|
986 r->buffered &= ~NGX_HTTP_SSI_BUFFERED;
|
|
987 }
|
|
988
|
46
|
989 return rc;
|
|
990 }
|
|
991
|
|
992
|
|
993 static ngx_int_t
|
|
994 ngx_http_ssi_parse(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
995 {
|
158
|
996 u_char *p, *value, *last, *copy_end, ch;
|
46
|
997 size_t looked;
|
|
998 ngx_http_ssi_state_e state;
|
|
999
|
|
1000 state = ctx->state;
|
|
1001 looked = ctx->looked;
|
|
1002 last = ctx->buf->last;
|
|
1003 copy_end = ctx->copy_end;
|
|
1004
|
|
1005 for (p = ctx->pos; p < last; p++) {
|
|
1006
|
|
1007 ch = *p;
|
|
1008
|
|
1009 if (state == ssi_start_state) {
|
|
1010
|
|
1011 /* the tight loop */
|
|
1012
|
48
|
1013 for ( ;; ) {
|
|
1014 if (ch == '<') {
|
|
1015 copy_end = p;
|
|
1016 looked = 1;
|
|
1017 state = ssi_tag_state;
|
|
1018
|
|
1019 goto tag_started;
|
46
|
1020 }
|
|
1021
|
48
|
1022 if (++p == last) {
|
|
1023 break;
|
|
1024 }
|
46
|
1025
|
48
|
1026 ch = *p;
|
46
|
1027 }
|
|
1028
|
314
|
1029 ctx->state = state;
|
46
|
1030 ctx->pos = p;
|
|
1031 ctx->looked = looked;
|
|
1032 ctx->copy_end = p;
|
|
1033
|
|
1034 if (ctx->copy_start == NULL) {
|
|
1035 ctx->copy_start = ctx->buf->pos;
|
|
1036 }
|
|
1037
|
|
1038 return NGX_AGAIN;
|
|
1039
|
48
|
1040 tag_started:
|
|
1041
|
46
|
1042 continue;
|
|
1043 }
|
|
1044
|
|
1045 switch (state) {
|
|
1046
|
|
1047 case ssi_start_state:
|
|
1048 break;
|
|
1049
|
|
1050 case ssi_tag_state:
|
|
1051 switch (ch) {
|
|
1052 case '!':
|
|
1053 looked = 2;
|
|
1054 state = ssi_comment0_state;
|
|
1055 break;
|
|
1056
|
|
1057 case '<':
|
|
1058 copy_end = p;
|
|
1059 break;
|
|
1060
|
|
1061 default:
|
|
1062 copy_end = p;
|
|
1063 looked = 0;
|
|
1064 state = ssi_start_state;
|
|
1065 break;
|
|
1066 }
|
|
1067
|
|
1068 break;
|
|
1069
|
|
1070 case ssi_comment0_state:
|
|
1071 switch (ch) {
|
|
1072 case '-':
|
|
1073 looked = 3;
|
|
1074 state = ssi_comment1_state;
|
|
1075 break;
|
|
1076
|
|
1077 case '<':
|
|
1078 copy_end = p;
|
|
1079 looked = 1;
|
|
1080 state = ssi_tag_state;
|
|
1081 break;
|
|
1082
|
|
1083 default:
|
|
1084 copy_end = p;
|
|
1085 looked = 0;
|
|
1086 state = ssi_start_state;
|
|
1087 break;
|
|
1088 }
|
|
1089
|
|
1090 break;
|
|
1091
|
|
1092 case ssi_comment1_state:
|
|
1093 switch (ch) {
|
|
1094 case '-':
|
|
1095 looked = 4;
|
|
1096 state = ssi_sharp_state;
|
|
1097 break;
|
|
1098
|
|
1099 case '<':
|
|
1100 copy_end = p;
|
|
1101 looked = 1;
|
|
1102 state = ssi_tag_state;
|
|
1103 break;
|
|
1104
|
|
1105 default:
|
|
1106 copy_end = p;
|
|
1107 looked = 0;
|
|
1108 state = ssi_start_state;
|
|
1109 break;
|
|
1110 }
|
|
1111
|
|
1112 break;
|
|
1113
|
|
1114 case ssi_sharp_state:
|
|
1115 switch (ch) {
|
|
1116 case '#':
|
208
|
1117 if (p - ctx->pos < 4) {
|
|
1118 ctx->saved = 0;
|
|
1119 }
|
46
|
1120 looked = 0;
|
|
1121 state = ssi_precommand_state;
|
|
1122 break;
|
|
1123
|
|
1124 case '<':
|
|
1125 copy_end = p;
|
|
1126 looked = 1;
|
|
1127 state = ssi_tag_state;
|
|
1128 break;
|
|
1129
|
|
1130 default:
|
|
1131 copy_end = p;
|
|
1132 looked = 0;
|
|
1133 state = ssi_start_state;
|
|
1134 break;
|
|
1135 }
|
|
1136
|
|
1137 break;
|
|
1138
|
|
1139 case ssi_precommand_state:
|
|
1140 switch (ch) {
|
|
1141 case ' ':
|
|
1142 case CR:
|
|
1143 case LF:
|
|
1144 case '\t':
|
|
1145 break;
|
|
1146
|
|
1147 default:
|
|
1148 ctx->command.len = 1;
|
|
1149 ctx->command.data = ngx_palloc(r->pool,
|
158
|
1150 NGX_HTTP_SSI_COMMAND_LEN);
|
46
|
1151 if (ctx->command.data == NULL) {
|
|
1152 return NGX_ERROR;
|
|
1153 }
|
|
1154
|
|
1155 ctx->command.data[0] = ch;
|
158
|
1156
|
146
|
1157 ctx->key = 0;
|
|
1158 ctx->key = ngx_hash(ctx->key, ch);
|
|
1159
|
46
|
1160 ctx->params.nelts = 0;
|
178
|
1161
|
46
|
1162 state = ssi_command_state;
|
|
1163 break;
|
|
1164 }
|
|
1165
|
|
1166 break;
|
|
1167
|
|
1168 case ssi_command_state:
|
|
1169 switch (ch) {
|
|
1170 case ' ':
|
|
1171 case CR:
|
|
1172 case LF:
|
|
1173 case '\t':
|
|
1174 state = ssi_preparam_state;
|
|
1175 break;
|
|
1176
|
|
1177 case '-':
|
|
1178 state = ssi_comment_end0_state;
|
|
1179 break;
|
|
1180
|
|
1181 default:
|
|
1182 if (ctx->command.len == NGX_HTTP_SSI_COMMAND_LEN) {
|
|
1183 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1184 "the \"%V%c...\" SSI command is too long",
|
|
1185 &ctx->command, ch);
|
46
|
1186
|
|
1187 state = ssi_error_state;
|
|
1188 break;
|
|
1189 }
|
158
|
1190
|
|
1191 ctx->command.data[ctx->command.len++] = ch;
|
|
1192 ctx->key = ngx_hash(ctx->key, ch);
|
46
|
1193 }
|
|
1194
|
|
1195 break;
|
|
1196
|
|
1197 case ssi_preparam_state:
|
|
1198 switch (ch) {
|
|
1199 case ' ':
|
|
1200 case CR:
|
|
1201 case LF:
|
|
1202 case '\t':
|
|
1203 break;
|
|
1204
|
|
1205 case '-':
|
|
1206 state = ssi_comment_end0_state;
|
|
1207 break;
|
|
1208
|
|
1209 default:
|
48
|
1210 ctx->param = ngx_array_push(&ctx->params);
|
|
1211 if (ctx->param == NULL) {
|
46
|
1212 return NGX_ERROR;
|
|
1213 }
|
|
1214
|
|
1215 ctx->param->key.len = 1;
|
|
1216 ctx->param->key.data = ngx_palloc(r->pool,
|
158
|
1217 NGX_HTTP_SSI_PARAM_LEN);
|
46
|
1218 if (ctx->param->key.data == NULL) {
|
|
1219 return NGX_ERROR;
|
|
1220 }
|
|
1221
|
|
1222 ctx->param->key.data[0] = ch;
|
|
1223
|
|
1224 ctx->param->value.len = 0;
|
158
|
1225
|
|
1226 if (ctx->value_buf == NULL) {
|
|
1227 ctx->param->value.data = ngx_palloc(r->pool,
|
|
1228 ctx->value_len);
|
|
1229 if (ctx->param->value.data == NULL) {
|
|
1230 return NGX_ERROR;
|
|
1231 }
|
|
1232
|
|
1233 } else {
|
|
1234 ctx->param->value.data = ctx->value_buf;
|
46
|
1235 }
|
|
1236
|
|
1237 state = ssi_param_state;
|
|
1238 break;
|
|
1239 }
|
|
1240
|
|
1241 break;
|
|
1242
|
|
1243 case ssi_param_state:
|
|
1244 switch (ch) {
|
|
1245 case ' ':
|
|
1246 case CR:
|
|
1247 case LF:
|
|
1248 case '\t':
|
|
1249 state = ssi_preequal_state;
|
|
1250 break;
|
|
1251
|
|
1252 case '=':
|
|
1253 state = ssi_prevalue_state;
|
|
1254 break;
|
|
1255
|
|
1256 case '-':
|
|
1257 state = ssi_error_end0_state;
|
|
1258
|
|
1259 ctx->param->key.data[ctx->param->key.len++] = ch;
|
|
1260 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1261 "invalid \"%V\" parameter in \"%V\" SSI command",
|
|
1262 &ctx->param->key, &ctx->command);
|
|
1263 break;
|
|
1264
|
|
1265 default:
|
|
1266 if (ctx->param->key.len == NGX_HTTP_SSI_PARAM_LEN) {
|
|
1267 state = ssi_error_state;
|
|
1268 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1269 "too long \"%V%c...\" parameter in "
|
46
|
1270 "\"%V\" SSI command",
|
158
|
1271 &ctx->param->key, ch, &ctx->command);
|
46
|
1272 break;
|
|
1273 }
|
158
|
1274
|
|
1275 ctx->param->key.data[ctx->param->key.len++] = ch;
|
46
|
1276 }
|
|
1277
|
|
1278 break;
|
|
1279
|
|
1280 case ssi_preequal_state:
|
|
1281 switch (ch) {
|
|
1282 case ' ':
|
|
1283 case CR:
|
|
1284 case LF:
|
|
1285 case '\t':
|
|
1286 break;
|
|
1287
|
|
1288 case '=':
|
|
1289 state = ssi_prevalue_state;
|
|
1290 break;
|
|
1291
|
|
1292 default:
|
|
1293 if (ch == '-') {
|
|
1294 state = ssi_error_end0_state;
|
|
1295 } else {
|
|
1296 state = ssi_error_state;
|
|
1297 }
|
|
1298
|
|
1299 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1300 "unexpected \"%c\" symbol after \"%V\" "
|
|
1301 "parameter in \"%V\" SSI command",
|
|
1302 ch, &ctx->param->key, &ctx->command);
|
|
1303 break;
|
|
1304 }
|
|
1305
|
|
1306 break;
|
|
1307
|
|
1308 case ssi_prevalue_state:
|
|
1309 switch (ch) {
|
|
1310 case ' ':
|
|
1311 case CR:
|
|
1312 case LF:
|
|
1313 case '\t':
|
|
1314 break;
|
|
1315
|
|
1316 case '"':
|
|
1317 state = ssi_double_quoted_value_state;
|
|
1318 break;
|
|
1319
|
|
1320 case '\'':
|
|
1321 state = ssi_quoted_value_state;
|
|
1322 break;
|
|
1323
|
|
1324 default:
|
|
1325 if (ch == '-') {
|
|
1326 state = ssi_error_end0_state;
|
|
1327 } else {
|
|
1328 state = ssi_error_state;
|
|
1329 }
|
|
1330
|
|
1331 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1332 "unexpected \"%c\" symbol before value of "
|
|
1333 "\"%V\" parameter in \"%V\" SSI command",
|
|
1334 ch, &ctx->param->key, &ctx->command);
|
|
1335 break;
|
|
1336 }
|
|
1337
|
|
1338 break;
|
|
1339
|
|
1340 case ssi_double_quoted_value_state:
|
|
1341 switch (ch) {
|
108
|
1342 case '"':
|
|
1343 state = ssi_postparam_state;
|
|
1344 break;
|
|
1345
|
46
|
1346 case '\\':
|
|
1347 ctx->saved_state = ssi_double_quoted_value_state;
|
|
1348 state = ssi_quoted_symbol_state;
|
108
|
1349
|
|
1350 /* fall through */
|
46
|
1351
|
|
1352 default:
|
|
1353 if (ctx->param->value.len == ctx->value_len) {
|
|
1354 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1355 "too long \"%V%c...\" value of \"%V\" "
|
|
1356 "parameter in \"%V\" SSI command",
|
|
1357 &ctx->param->value, ch, &ctx->param->key,
|
46
|
1358 &ctx->command);
|
|
1359 state = ssi_error_state;
|
|
1360 break;
|
|
1361 }
|
158
|
1362
|
|
1363 ctx->param->value.data[ctx->param->value.len++] = ch;
|
46
|
1364 }
|
|
1365
|
|
1366 break;
|
|
1367
|
|
1368 case ssi_quoted_value_state:
|
|
1369 switch (ch) {
|
108
|
1370 case '\'':
|
|
1371 state = ssi_postparam_state;
|
|
1372 break;
|
|
1373
|
46
|
1374 case '\\':
|
|
1375 ctx->saved_state = ssi_quoted_value_state;
|
|
1376 state = ssi_quoted_symbol_state;
|
108
|
1377
|
|
1378 /* fall through */
|
46
|
1379
|
|
1380 default:
|
|
1381 if (ctx->param->value.len == ctx->value_len) {
|
|
1382 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
158
|
1383 "too long \"%V%c...\" value of \"%V\" "
|
|
1384 "parameter in \"%V\" SSI command",
|
|
1385 &ctx->param->value, ch, &ctx->param->key,
|
46
|
1386 &ctx->command);
|
|
1387 state = ssi_error_state;
|
|
1388 break;
|
|
1389 }
|
158
|
1390
|
|
1391 ctx->param->value.data[ctx->param->value.len++] = ch;
|
46
|
1392 }
|
|
1393
|
|
1394 break;
|
|
1395
|
|
1396 case ssi_quoted_symbol_state:
|
108
|
1397 state = ctx->saved_state;
|
|
1398
|
46
|
1399 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1400
|
|
1401 break;
|
|
1402
|
|
1403 case ssi_postparam_state:
|
158
|
1404
|
200
|
1405 if (ctx->param->value.len + 1 < ctx->value_len / 2) {
|
|
1406 value = ngx_palloc(r->pool, ctx->param->value.len + 1);
|
158
|
1407 if (value == NULL) {
|
|
1408 return NGX_ERROR;
|
|
1409 }
|
|
1410
|
|
1411 ngx_memcpy(value, ctx->param->value.data,
|
|
1412 ctx->param->value.len);
|
|
1413
|
|
1414 ctx->value_buf = ctx->param->value.data;
|
|
1415 ctx->param->value.data = value;
|
|
1416
|
|
1417 } else {
|
|
1418 ctx->value_buf = NULL;
|
|
1419 }
|
|
1420
|
46
|
1421 switch (ch) {
|
|
1422 case ' ':
|
|
1423 case CR:
|
|
1424 case LF:
|
|
1425 case '\t':
|
|
1426 state = ssi_preparam_state;
|
|
1427 break;
|
|
1428
|
|
1429 case '-':
|
|
1430 state = ssi_comment_end0_state;
|
|
1431 break;
|
|
1432
|
|
1433 default:
|
|
1434 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1435 "unexpected \"%c\" symbol after \"%V\" value "
|
|
1436 "of \"%V\" parameter in \"%V\" SSI command",
|
|
1437 ch, &ctx->param->value, &ctx->param->key,
|
|
1438 &ctx->command);
|
|
1439 state = ssi_error_state;
|
|
1440 break;
|
|
1441 }
|
|
1442
|
|
1443 break;
|
|
1444
|
|
1445 case ssi_comment_end0_state:
|
|
1446 switch (ch) {
|
|
1447 case '-':
|
|
1448 state = ssi_comment_end1_state;
|
|
1449 break;
|
|
1450
|
|
1451 default:
|
|
1452 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1453 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1454 ch, &ctx->command);
|
|
1455 state = ssi_error_state;
|
|
1456 break;
|
|
1457 }
|
|
1458
|
|
1459 break;
|
|
1460
|
|
1461 case ssi_comment_end1_state:
|
|
1462 switch (ch) {
|
|
1463 case '>':
|
|
1464 ctx->state = ssi_start_state;
|
|
1465 ctx->pos = p + 1;
|
|
1466 ctx->looked = looked;
|
|
1467 ctx->copy_end = copy_end;
|
|
1468
|
|
1469 if (ctx->copy_start == NULL && copy_end) {
|
|
1470 ctx->copy_start = ctx->buf->pos;
|
|
1471 }
|
|
1472
|
|
1473 return NGX_OK;
|
|
1474
|
|
1475 default:
|
|
1476 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1477 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1478 ch, &ctx->command);
|
|
1479 state = ssi_error_state;
|
|
1480 break;
|
|
1481 }
|
|
1482
|
|
1483 break;
|
|
1484
|
|
1485 case ssi_error_state:
|
|
1486 switch (ch) {
|
|
1487 case '-':
|
|
1488 state = ssi_error_end0_state;
|
|
1489 break;
|
|
1490
|
|
1491 default:
|
|
1492 break;
|
|
1493 }
|
|
1494
|
|
1495 break;
|
|
1496
|
|
1497 case ssi_error_end0_state:
|
|
1498 switch (ch) {
|
|
1499 case '-':
|
|
1500 state = ssi_error_end1_state;
|
|
1501 break;
|
|
1502
|
|
1503 default:
|
|
1504 state = ssi_error_state;
|
|
1505 break;
|
|
1506 }
|
|
1507
|
|
1508 break;
|
|
1509
|
|
1510 case ssi_error_end1_state:
|
|
1511 switch (ch) {
|
|
1512 case '>':
|
|
1513 ctx->state = ssi_start_state;
|
|
1514 ctx->pos = p + 1;
|
|
1515 ctx->looked = looked;
|
|
1516 ctx->copy_end = copy_end;
|
|
1517
|
|
1518 if (ctx->copy_start == NULL && copy_end) {
|
|
1519 ctx->copy_start = ctx->buf->pos;
|
|
1520 }
|
|
1521
|
|
1522 return NGX_HTTP_SSI_ERROR;
|
|
1523
|
|
1524 default:
|
|
1525 state = ssi_error_state;
|
|
1526 break;
|
|
1527 }
|
|
1528
|
|
1529 break;
|
|
1530 }
|
|
1531 }
|
|
1532
|
|
1533 ctx->state = state;
|
|
1534 ctx->pos = p;
|
|
1535 ctx->looked = looked;
|
|
1536
|
|
1537 ctx->copy_end = (state == ssi_start_state) ? p : copy_end;
|
|
1538
|
|
1539 if (ctx->copy_start == NULL && ctx->copy_end) {
|
|
1540 ctx->copy_start = ctx->buf->pos;
|
|
1541 }
|
|
1542
|
|
1543 return NGX_AGAIN;
|
|
1544 }
|
|
1545
|
|
1546
|
102
|
1547 static ngx_str_t *
|
160
|
1548 ngx_http_ssi_get_variable(ngx_http_request_t *r, ngx_str_t *name,
|
|
1549 ngx_uint_t key)
|
102
|
1550 {
|
|
1551 ngx_uint_t i;
|
248
|
1552 ngx_list_part_t *part;
|
102
|
1553 ngx_http_ssi_var_t *var;
|
|
1554 ngx_http_ssi_ctx_t *ctx;
|
|
1555
|
|
1556 ctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
1557
|
216
|
1558 if (ctx->variables == NULL) {
|
|
1559 return NULL;
|
|
1560 }
|
|
1561
|
248
|
1562 part = &ctx->variables->part;
|
|
1563 var = part->elts;
|
|
1564
|
|
1565 for (i = 0; /* void */ ; i++) {
|
|
1566
|
|
1567 if (i >= part->nelts) {
|
|
1568 if (part->next == NULL) {
|
|
1569 break;
|
|
1570 }
|
250
|
1571
|
248
|
1572 part = part->next;
|
|
1573 var = part->elts;
|
|
1574 i = 0;
|
|
1575 }
|
|
1576
|
102
|
1577 if (name->len != var[i].name.len) {
|
|
1578 continue;
|
|
1579 }
|
|
1580
|
160
|
1581 if (key != var[i].key) {
|
|
1582 continue;
|
|
1583 }
|
|
1584
|
|
1585 if (ngx_strncmp(name->data, var[i].name.data, name->len) == 0) {
|
102
|
1586 return &var[i].value;
|
|
1587 }
|
|
1588 }
|
|
1589
|
|
1590 return NULL;
|
|
1591 }
|
|
1592
|
|
1593
|
|
1594 static ngx_int_t
|
|
1595 ngx_http_ssi_evaluate_string(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1596 ngx_str_t *text, ngx_uint_t flags)
|
|
1597 {
|
122
|
1598 u_char ch, *p, **value, *data, *part_data;
|
|
1599 size_t *size, len, prefix, part_len;
|
|
1600 ngx_str_t var, *val;
|
160
|
1601 ngx_int_t key;
|
200
|
1602 ngx_uint_t i, j, n, bracket, quoted;
|
102
|
1603 ngx_array_t lengths, values;
|
|
1604 ngx_http_variable_value_t *vv;
|
|
1605
|
|
1606 n = ngx_http_script_variables_count(text);
|
|
1607
|
|
1608 if (n == 0) {
|
|
1609
|
206
|
1610 data = text->data;
|
|
1611 p = data;
|
|
1612
|
|
1613 if ((flags & NGX_HTTP_SSI_ADD_PREFIX) && text->data[0] != '/') {
|
|
1614
|
102
|
1615 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1616 if (r->uri.data[prefix - 1] == '/') {
|
|
1617 break;
|
|
1618 }
|
|
1619 }
|
|
1620
|
|
1621 if (prefix) {
|
|
1622 len = prefix + text->len;
|
|
1623
|
|
1624 data = ngx_palloc(r->pool, len);
|
|
1625 if (data == NULL) {
|
|
1626 return NGX_ERROR;
|
|
1627 }
|
|
1628
|
122
|
1629 p = ngx_copy(data, r->uri.data, prefix);
|
102
|
1630 }
|
|
1631 }
|
|
1632
|
206
|
1633 quoted = 0;
|
|
1634
|
|
1635 for (i = 0 ; i < text->len; i++) {
|
|
1636 ch = text->data[i];
|
|
1637
|
|
1638 if (!quoted) {
|
|
1639
|
|
1640 if (ch == '\\') {
|
|
1641 quoted = 1;
|
|
1642 continue;
|
|
1643 }
|
|
1644
|
|
1645 } else {
|
|
1646 quoted = 0;
|
|
1647
|
|
1648 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
|
|
1649 *p++ = '\\';
|
|
1650 }
|
|
1651 }
|
|
1652
|
|
1653 *p++ = ch;
|
|
1654 }
|
|
1655
|
|
1656 text->len = p - data;
|
|
1657 text->data = data;
|
|
1658
|
102
|
1659 return NGX_OK;
|
|
1660 }
|
|
1661
|
|
1662 if (ngx_array_init(&lengths, r->pool, 8, sizeof(size_t *)) != NGX_OK) {
|
|
1663 return NGX_ERROR;
|
|
1664 }
|
|
1665
|
|
1666 if (ngx_array_init(&values, r->pool, 8, sizeof(u_char *)) != NGX_OK) {
|
|
1667 return NGX_ERROR;
|
|
1668 }
|
|
1669
|
|
1670 len = 0;
|
|
1671 i = 0;
|
|
1672
|
|
1673 while (i < text->len) {
|
|
1674
|
|
1675 if (text->data[i] == '$') {
|
|
1676
|
|
1677 var.len = 0;
|
|
1678
|
|
1679 if (++i == text->len) {
|
|
1680 goto invalid_variable;
|
|
1681 }
|
|
1682
|
|
1683 if (text->data[i] == '{') {
|
|
1684 bracket = 1;
|
|
1685
|
|
1686 if (++i == text->len) {
|
|
1687 goto invalid_variable;
|
|
1688 }
|
|
1689
|
|
1690 var.data = &text->data[i];
|
|
1691
|
|
1692 } else {
|
|
1693 bracket = 0;
|
|
1694 var.data = &text->data[i];
|
|
1695 }
|
|
1696
|
|
1697 for ( /* void */ ; i < text->len; i++, var.len++) {
|
|
1698 ch = text->data[i];
|
|
1699
|
|
1700 if (ch == '}' && bracket) {
|
|
1701 i++;
|
|
1702 bracket = 0;
|
|
1703 break;
|
|
1704 }
|
|
1705
|
|
1706 if ((ch >= 'A' && ch <= 'Z')
|
|
1707 || (ch >= 'a' && ch <= 'z')
|
|
1708 || (ch >= '0' && ch <= '9')
|
|
1709 || ch == '_')
|
|
1710 {
|
|
1711 continue;
|
|
1712 }
|
|
1713
|
|
1714 break;
|
|
1715 }
|
|
1716
|
|
1717 if (bracket) {
|
|
1718 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1719 "the closing bracket in \"%V\" "
|
|
1720 "variable is missing", &var);
|
248
|
1721 return NGX_HTTP_SSI_ERROR;
|
102
|
1722 }
|
|
1723
|
126
|
1724 if (var.len == 0) {
|
102
|
1725 goto invalid_variable;
|
|
1726 }
|
|
1727
|
160
|
1728 key = 0;
|
|
1729
|
102
|
1730 for (j = 0; j < var.len; j++) {
|
|
1731 var.data[j] = ngx_tolower(var.data[j]);
|
160
|
1732 key = ngx_hash(key, var.data[j]);
|
102
|
1733 }
|
|
1734
|
160
|
1735 val = ngx_http_ssi_get_variable(r, &var, key);
|
102
|
1736
|
|
1737 if (val == NULL) {
|
184
|
1738 vv = ngx_http_get_variable(r, &var, key,
|
|
1739 flags & NGX_HTTP_SSI_EXPR_TEST);
|
102
|
1740 if (vv == NULL) {
|
|
1741 return NGX_ERROR;
|
|
1742 }
|
|
1743
|
122
|
1744 if (vv->not_found) {
|
102
|
1745 continue;
|
|
1746 }
|
|
1747
|
122
|
1748 part_data = vv->data;
|
|
1749 part_len = vv->len;
|
102
|
1750
|
|
1751 } else {
|
122
|
1752 part_data = val->data;
|
|
1753 part_len = val->len;
|
102
|
1754 }
|
|
1755
|
|
1756 } else {
|
122
|
1757 part_data = &text->data[i];
|
200
|
1758 quoted = 0;
|
122
|
1759
|
|
1760 for (p = part_data; i < text->len; i++) {
|
108
|
1761 ch = text->data[i];
|
|
1762
|
200
|
1763 if (!quoted) {
|
|
1764
|
|
1765 if (ch == '\\') {
|
|
1766 quoted = 1;
|
|
1767 continue;
|
|
1768 }
|
|
1769
|
|
1770 if (ch == '$') {
|
108
|
1771 break;
|
|
1772 }
|
|
1773
|
200
|
1774 } else {
|
|
1775 quoted = 0;
|
|
1776
|
|
1777 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
|
|
1778 *p++ = '\\';
|
|
1779 }
|
108
|
1780 }
|
|
1781
|
|
1782 *p++ = ch;
|
102
|
1783 }
|
108
|
1784
|
122
|
1785 part_len = p - part_data;
|
102
|
1786 }
|
|
1787
|
122
|
1788 len += part_len;
|
102
|
1789
|
|
1790 size = ngx_array_push(&lengths);
|
|
1791 if (size == NULL) {
|
|
1792 return NGX_ERROR;
|
|
1793 }
|
|
1794
|
122
|
1795 *size = part_len;
|
102
|
1796
|
|
1797 value = ngx_array_push(&values);
|
|
1798 if (value == NULL) {
|
|
1799 return NGX_ERROR;
|
|
1800 }
|
|
1801
|
122
|
1802 *value = part_data;
|
102
|
1803 }
|
|
1804
|
|
1805 prefix = 0;
|
|
1806
|
|
1807 size = lengths.elts;
|
|
1808 value = values.elts;
|
|
1809
|
|
1810 if (flags & NGX_HTTP_SSI_ADD_PREFIX) {
|
|
1811 for (i = 0; i < values.nelts; i++) {
|
|
1812 if (size[i] != 0) {
|
|
1813 if (*value[i] != '/') {
|
|
1814 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1815 if (r->uri.data[prefix - 1] == '/') {
|
|
1816 len += prefix;
|
|
1817 break;
|
|
1818 }
|
|
1819 }
|
|
1820 }
|
|
1821
|
|
1822 break;
|
|
1823 }
|
|
1824 }
|
|
1825 }
|
|
1826
|
178
|
1827 p = ngx_palloc(r->pool, len + ((flags & NGX_HTTP_SSI_ADD_ZERO) ? 1 : 0));
|
102
|
1828 if (p == NULL) {
|
|
1829 return NGX_ERROR;
|
|
1830 }
|
|
1831
|
|
1832 text->len = len;
|
|
1833 text->data = p;
|
|
1834
|
122
|
1835 p = ngx_copy(p, r->uri.data, prefix);
|
102
|
1836
|
|
1837 for (i = 0; i < values.nelts; i++) {
|
122
|
1838 p = ngx_copy(p, value[i], size[i]);
|
102
|
1839 }
|
|
1840
|
|
1841 return NGX_OK;
|
|
1842
|
|
1843 invalid_variable:
|
|
1844
|
|
1845 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1846 "invalid variable name in \"%V\"", text);
|
|
1847
|
248
|
1848 return NGX_HTTP_SSI_ERROR;
|
102
|
1849 }
|
|
1850
|
|
1851
|
|
1852 static ngx_int_t
|
|
1853 ngx_http_ssi_include(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1854 ngx_str_t **params)
|
|
1855 {
|
248
|
1856 ngx_int_t rc, key;
|
|
1857 ngx_str_t *uri, *file, *wait, *set, *stub, args;
|
|
1858 ngx_buf_t *b;
|
|
1859 ngx_uint_t flags, i;
|
250
|
1860 ngx_chain_t *cl, *tl, **ll, *out;
|
248
|
1861 ngx_http_request_t *sr;
|
|
1862 ngx_http_ssi_var_t *var;
|
|
1863 ngx_http_ssi_ctx_t *mctx;
|
|
1864 ngx_http_ssi_block_t *bl;
|
|
1865 ngx_http_post_subrequest_t *psr;
|
102
|
1866
|
|
1867 uri = params[NGX_HTTP_SSI_INCLUDE_VIRTUAL];
|
|
1868 file = params[NGX_HTTP_SSI_INCLUDE_FILE];
|
194
|
1869 wait = params[NGX_HTTP_SSI_INCLUDE_WAIT];
|
248
|
1870 set = params[NGX_HTTP_SSI_INCLUDE_SET];
|
216
|
1871 stub = params[NGX_HTTP_SSI_INCLUDE_STUB];
|
102
|
1872
|
|
1873 if (uri && file) {
|
|
1874 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1875 "inlcusion may be either virtual=\"%V\" or file=\"%V\"",
|
|
1876 uri, file);
|
|
1877 return NGX_HTTP_SSI_ERROR;
|
|
1878 }
|
|
1879
|
|
1880 if (uri == NULL && file == NULL) {
|
|
1881 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1882 "no parameter in \"include\" SSI command");
|
|
1883 return NGX_HTTP_SSI_ERROR;
|
|
1884 }
|
|
1885
|
248
|
1886 if (set && stub) {
|
|
1887 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1888 "\"set\" and \"stub\" may not be used together "
|
|
1889 "in \"include\" SSI command");
|
|
1890 return NGX_HTTP_SSI_ERROR;
|
|
1891 }
|
|
1892
|
194
|
1893 if (wait) {
|
|
1894 if (uri == NULL) {
|
|
1895 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
216
|
1896 "\"wait\" may not be used with file=\"%V\"", file);
|
194
|
1897 return NGX_HTTP_SSI_ERROR;
|
|
1898 }
|
|
1899
|
286
|
1900 if (wait->len == 2
|
|
1901 && ngx_strncasecmp(wait->data, (u_char *) "no", 2) == 0)
|
|
1902 {
|
194
|
1903 wait = NULL;
|
|
1904
|
286
|
1905 } else if (wait->len != 3
|
|
1906 || ngx_strncasecmp(wait->data, (u_char *) "yes", 3) != 0)
|
194
|
1907 {
|
|
1908 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1909 "invalid value \"%V\" in the \"wait\" parameter",
|
216
|
1910 wait);
|
194
|
1911 return NGX_HTTP_SSI_ERROR;
|
|
1912 }
|
|
1913 }
|
|
1914
|
102
|
1915 if (uri == NULL) {
|
|
1916 uri = file;
|
|
1917 }
|
|
1918
|
248
|
1919 rc = ngx_http_ssi_evaluate_string(r, ctx, uri, NGX_HTTP_SSI_ADD_PREFIX);
|
|
1920
|
|
1921 if (rc != NGX_OK) {
|
|
1922 return rc;
|
102
|
1923 }
|
|
1924
|
|
1925 args.len = 0;
|
|
1926 args.data = NULL;
|
122
|
1927 flags = 0;
|
|
1928
|
|
1929 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1930 "ssi include: \"%V\"", uri);
|
|
1931
|
|
1932 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
|
|
1933 return NGX_HTTP_SSI_ERROR;
|
102
|
1934 }
|
|
1935
|
248
|
1936 psr = NULL;
|
|
1937
|
|
1938 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
216
|
1939
|
|
1940 if (stub) {
|
|
1941 if (mctx->blocks) {
|
|
1942 bl = mctx->blocks->elts;
|
|
1943 for (i = 0; i < mctx->blocks->nelts; i++) {
|
|
1944 if (stub->len == bl[i].name.len
|
|
1945 && ngx_strncmp(stub->data, bl[i].name.data, stub->len) == 0)
|
|
1946 {
|
|
1947 goto found;
|
|
1948 }
|
|
1949 }
|
|
1950 }
|
|
1951
|
|
1952 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1953 "\"stub\"=\"%V\" for \"include\" not found", stub);
|
|
1954 return NGX_HTTP_SSI_ERROR;
|
|
1955
|
|
1956 found:
|
|
1957
|
248
|
1958 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
|
|
1959 if (psr == NULL) {
|
|
1960 return NGX_ERROR;
|
|
1961 }
|
|
1962
|
|
1963 psr->handler = ngx_http_ssi_stub_output;
|
|
1964
|
216
|
1965 if (bl[i].count++) {
|
|
1966
|
250
|
1967 ll = &out;
|
216
|
1968
|
|
1969 for (tl = bl[i].bufs; tl; tl = tl->next) {
|
|
1970
|
|
1971 if (ctx->free) {
|
|
1972 cl = ctx->free;
|
|
1973 ctx->free = ctx->free->next;
|
|
1974 b = cl->buf;
|
|
1975
|
|
1976 } else {
|
|
1977 b = ngx_alloc_buf(r->pool);
|
|
1978 if (b == NULL) {
|
|
1979 return NGX_ERROR;
|
|
1980 }
|
|
1981
|
|
1982 cl = ngx_alloc_chain_link(r->pool);
|
|
1983 if (cl == NULL) {
|
|
1984 return NGX_ERROR;
|
|
1985 }
|
|
1986
|
|
1987 cl->buf = b;
|
|
1988 }
|
|
1989
|
|
1990 ngx_memcpy(b, tl->buf, sizeof(ngx_buf_t));
|
|
1991
|
|
1992 b->pos = b->start;
|
|
1993
|
|
1994 *ll = cl;
|
|
1995 cl->next = NULL;
|
|
1996 ll = &cl->next;
|
|
1997 }
|
|
1998
|
250
|
1999 psr->data = out;
|
|
2000
|
216
|
2001 } else {
|
248
|
2002 psr->data = bl[i].bufs;
|
216
|
2003 }
|
|
2004 }
|
|
2005
|
248
|
2006 if (set) {
|
|
2007 key = 0;
|
|
2008
|
|
2009 for (i = 0; i < set->len; i++) {
|
|
2010 set->data[i] = ngx_tolower(set->data[i]);
|
|
2011 key = ngx_hash(key, set->data[i]);
|
|
2012 }
|
|
2013
|
|
2014 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
|
|
2015 if (psr == NULL) {
|
|
2016 return NGX_ERROR;
|
|
2017 }
|
|
2018
|
|
2019 psr->handler = ngx_http_ssi_set_variable;
|
|
2020 psr->data = ngx_http_ssi_get_variable(r, set, key);
|
|
2021
|
|
2022 if (psr->data == NULL) {
|
|
2023
|
|
2024 if (mctx->variables == NULL) {
|
|
2025 mctx->variables = ngx_list_create(r->pool, 4,
|
|
2026 sizeof(ngx_http_ssi_var_t));
|
|
2027 if (mctx->variables == NULL) {
|
|
2028 return NGX_ERROR;
|
|
2029 }
|
|
2030 }
|
|
2031
|
|
2032 var = ngx_list_push(mctx->variables);
|
|
2033 if (var == NULL) {
|
|
2034 return NGX_ERROR;
|
|
2035 }
|
|
2036
|
|
2037 var->name = *set;
|
|
2038 var->key = key;
|
|
2039 var->value = ngx_http_ssi_null_string;
|
|
2040 psr->data = &var->value;
|
|
2041 }
|
|
2042
|
|
2043 flags |= NGX_HTTP_SUBREQUEST_IN_MEMORY;
|
|
2044 }
|
|
2045
|
|
2046 rc = ngx_http_subrequest(r, uri, &args, &sr, psr, flags);
|
|
2047
|
|
2048 if (rc == NGX_DONE) {
|
|
2049 return NGX_DONE;
|
|
2050 }
|
194
|
2051
|
|
2052 if (rc == NGX_ERROR) {
|
102
|
2053 return NGX_HTTP_SSI_ERROR;
|
|
2054 }
|
|
2055
|
248
|
2056 if (wait == NULL && set == NULL) {
|
194
|
2057 return NGX_OK;
|
|
2058 }
|
|
2059
|
|
2060 if (rc == NGX_AGAIN) {
|
244
|
2061 if (ctx->wait == NULL) {
|
|
2062 ctx->wait = sr;
|
|
2063
|
|
2064 } else {
|
|
2065 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
2066 "only one subrequest may be waited at the same time");
|
|
2067 }
|
194
|
2068 }
|
|
2069
|
|
2070 return rc;
|
102
|
2071 }
|
|
2072
|
|
2073
|
46
|
2074 static ngx_int_t
|
248
|
2075 ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc)
|
|
2076 {
|
|
2077 ngx_chain_t *out;
|
|
2078
|
|
2079 if (rc == NGX_ERROR || r->connection->error || r->request_output) {
|
|
2080 return rc;
|
|
2081 }
|
|
2082
|
|
2083 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2084 "ssi stub output: \"%V?%V\"", &r->uri, &r->args);
|
|
2085
|
|
2086 out = data;
|
|
2087
|
|
2088 if (!r->header_sent) {
|
|
2089 if (ngx_http_set_content_type(r) == NGX_ERROR) {
|
|
2090 return NGX_ERROR;
|
|
2091 }
|
|
2092
|
|
2093 if (ngx_http_send_header(r) == NGX_ERROR) {
|
|
2094 return NGX_ERROR;
|
|
2095 }
|
|
2096 }
|
|
2097
|
|
2098 return ngx_http_output_filter(r, out);
|
|
2099 }
|
|
2100
|
|
2101
|
|
2102 static ngx_int_t
|
|
2103 ngx_http_ssi_set_variable(ngx_http_request_t *r, void *data, ngx_int_t rc)
|
|
2104 {
|
|
2105 ngx_str_t *value = data;
|
|
2106
|
|
2107 if (r->upstream) {
|
|
2108 value->len = r->upstream->buffer.last - r->upstream->buffer.pos;
|
|
2109 value->data = r->upstream->buffer.pos;
|
|
2110 }
|
|
2111
|
|
2112 return rc;
|
|
2113 }
|
|
2114
|
|
2115
|
|
2116 static ngx_int_t
|
46
|
2117 ngx_http_ssi_echo(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2118 ngx_str_t **params)
|
|
2119 {
|
160
|
2120 ngx_int_t key;
|
50
|
2121 ngx_uint_t i;
|
48
|
2122 ngx_buf_t *b;
|
122
|
2123 ngx_str_t *var, *value, text;
|
48
|
2124 ngx_chain_t *cl;
|
50
|
2125 ngx_http_variable_value_t *vv;
|
46
|
2126
|
|
2127 var = params[NGX_HTTP_SSI_ECHO_VAR];
|
|
2128
|
192
|
2129 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2130 "ssi echo \"%V\"", var);
|
|
2131
|
160
|
2132 key = 0;
|
|
2133
|
|
2134 for (i = 0; i < var->len; i++) {
|
|
2135 var->data[i] = ngx_tolower(var->data[i]);
|
|
2136 key = ngx_hash(key, var->data[i]);
|
|
2137 }
|
|
2138
|
|
2139 value = ngx_http_ssi_get_variable(r, var, key);
|
102
|
2140
|
|
2141 if (value == NULL) {
|
184
|
2142 vv = ngx_http_get_variable(r, var, key, 1);
|
102
|
2143
|
|
2144 if (vv == NULL) {
|
|
2145 return NGX_HTTP_SSI_ERROR;
|
|
2146 }
|
|
2147
|
122
|
2148 if (!vv->not_found) {
|
|
2149 text.data = vv->data;
|
|
2150 text.len = vv->len;
|
|
2151 value = &text;
|
102
|
2152 }
|
50
|
2153 }
|
46
|
2154
|
102
|
2155 if (value == NULL) {
|
48
|
2156 value = params[NGX_HTTP_SSI_ECHO_DEFAULT];
|
|
2157
|
|
2158 if (value == NULL) {
|
|
2159 value = &ngx_http_ssi_none;
|
|
2160
|
|
2161 } else if (value->len == 0) {
|
|
2162 return NGX_OK;
|
|
2163 }
|
|
2164
|
|
2165 } else {
|
|
2166 if (value->len == 0) {
|
|
2167 return NGX_OK;
|
|
2168 }
|
|
2169 }
|
|
2170
|
|
2171 b = ngx_calloc_buf(r->pool);
|
|
2172 if (b == NULL) {
|
|
2173 return NGX_HTTP_SSI_ERROR;
|
|
2174 }
|
|
2175
|
|
2176 cl = ngx_alloc_chain_link(r->pool);
|
|
2177 if (cl == NULL) {
|
46
|
2178 return NGX_HTTP_SSI_ERROR;
|
|
2179 }
|
|
2180
|
|
2181 b->memory = 1;
|
|
2182 b->pos = value->data;
|
|
2183 b->last = value->data + value->len;
|
|
2184
|
|
2185 cl->buf = b;
|
|
2186 cl->next = NULL;
|
|
2187 *ctx->last_out = cl;
|
|
2188 ctx->last_out = &cl->next;
|
|
2189
|
|
2190 return NGX_OK;
|
|
2191 }
|
|
2192
|
|
2193
|
58
|
2194 static ngx_int_t
|
|
2195 ngx_http_ssi_config(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2196 ngx_str_t **params)
|
|
2197 {
|
|
2198 ngx_str_t *value;
|
|
2199
|
|
2200 value = params[NGX_HTTP_SSI_CONFIG_TIMEFMT];
|
|
2201
|
|
2202 if (value) {
|
138
|
2203 ctx->timefmt.len = value->len;
|
|
2204 ctx->timefmt.data = ngx_palloc(r->pool, value->len + 1);
|
|
2205 if (ctx->timefmt.data == NULL) {
|
|
2206 return NGX_HTTP_SSI_ERROR;
|
|
2207 }
|
|
2208
|
|
2209 ngx_cpystrn(ctx->timefmt.data, value->data, value->len + 1);
|
58
|
2210 }
|
|
2211
|
96
|
2212 value = params[NGX_HTTP_SSI_CONFIG_ERRMSG];
|
|
2213
|
|
2214 if (value) {
|
|
2215 ctx->errmsg = *value;
|
|
2216 }
|
|
2217
|
58
|
2218 return NGX_OK;
|
|
2219 }
|
|
2220
|
|
2221
|
|
2222 static ngx_int_t
|
102
|
2223 ngx_http_ssi_set(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
58
|
2224 ngx_str_t **params)
|
|
2225 {
|
248
|
2226 ngx_int_t key, rc;
|
160
|
2227 ngx_uint_t i;
|
102
|
2228 ngx_str_t *name, *value, *vv;
|
|
2229 ngx_http_ssi_var_t *var;
|
|
2230 ngx_http_ssi_ctx_t *mctx;
|
|
2231
|
|
2232 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
2233
|
216
|
2234 if (mctx->variables == NULL) {
|
248
|
2235 mctx->variables = ngx_list_create(r->pool, 4,
|
|
2236 sizeof(ngx_http_ssi_var_t));
|
216
|
2237 if (mctx->variables == NULL) {
|
248
|
2238 return NGX_ERROR;
|
58
|
2239 }
|
|
2240 }
|
|
2241
|
102
|
2242 name = params[NGX_HTTP_SSI_SET_VAR];
|
|
2243 value = params[NGX_HTTP_SSI_SET_VALUE];
|
|
2244
|
192
|
2245 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2246 "ssi set \"%V\" \"%V\"", name, value);
|
|
2247
|
248
|
2248 rc = ngx_http_ssi_evaluate_string(r, ctx, value, 0);
|
|
2249
|
|
2250 if (rc != NGX_OK) {
|
|
2251 return rc;
|
58
|
2252 }
|
|
2253
|
160
|
2254 key = 0;
|
|
2255
|
|
2256 for (i = 0; i < name->len; i++) {
|
|
2257 name->data[i] = ngx_tolower(name->data[i]);
|
|
2258 key = ngx_hash(key, name->data[i]);
|
|
2259 }
|
|
2260
|
|
2261 vv = ngx_http_ssi_get_variable(r, name, key);
|
102
|
2262
|
|
2263 if (vv) {
|
|
2264 *vv = *value;
|
|
2265 return NGX_OK;
|
|
2266 }
|
|
2267
|
248
|
2268 var = ngx_list_push(mctx->variables);
|
102
|
2269 if (var == NULL) {
|
248
|
2270 return NGX_ERROR;
|
102
|
2271 }
|
|
2272
|
|
2273 var->name = *name;
|
160
|
2274 var->key = key;
|
102
|
2275 var->value = *value;
|
|
2276
|
|
2277 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2278 "set: \"%V\"=\"%V\"", name, value);
|
|
2279
|
58
|
2280 return NGX_OK;
|
|
2281 }
|
|
2282
|
|
2283
|
|
2284 static ngx_int_t
|
|
2285 ngx_http_ssi_if(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2286 ngx_str_t **params)
|
|
2287 {
|
102
|
2288 u_char *p, *last;
|
|
2289 ngx_str_t *expr, left, right;
|
|
2290 ngx_int_t rc;
|
178
|
2291 ngx_uint_t negative, noregex, flags;
|
100
|
2292 #if (NGX_PCRE)
|
102
|
2293 ngx_str_t err;
|
|
2294 ngx_regex_t *regex;
|
|
2295 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
100
|
2296 #endif
|
58
|
2297
|
178
|
2298 if (ctx->command.len == 2) {
|
|
2299 if (ctx->conditional) {
|
|
2300 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
2301 "the \"if\" command inside the \"if\" command");
|
|
2302 return NGX_HTTP_SSI_ERROR;
|
|
2303 }
|
|
2304 }
|
|
2305
|
|
2306 if (ctx->output_chosen) {
|
|
2307 ctx->output = 0;
|
|
2308 return NGX_OK;
|
|
2309 }
|
|
2310
|
58
|
2311 expr = params[NGX_HTTP_SSI_IF_EXPR];
|
|
2312
|
192
|
2313 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2314 "ssi if expr=\"%V\"", expr);
|
|
2315
|
102
|
2316 left.data = expr->data;
|
100
|
2317 last = expr->data + expr->len;
|
|
2318
|
102
|
2319 for (p = left.data; p < last; p++) {
|
100
|
2320 if (*p >= 'A' && *p <= 'Z') {
|
|
2321 *p |= 0x20;
|
|
2322 continue;
|
|
2323 }
|
58
|
2324
|
102
|
2325 if ((*p >= 'a' && *p <= 'z')
|
|
2326 || (*p >= '0' && *p <= '9')
|
122
|
2327 || *p == '$' || *p == '{' || *p == '}' || *p == '_'
|
|
2328 || *p == '"' || *p == '\'')
|
102
|
2329 {
|
100
|
2330 continue;
|
|
2331 }
|
|
2332
|
|
2333 break;
|
|
2334 }
|
|
2335
|
102
|
2336 left.len = p - left.data;
|
100
|
2337
|
|
2338 while (p < last && *p == ' ') {
|
|
2339 p++;
|
58
|
2340 }
|
|
2341
|
184
|
2342 flags = (p == last) ? NGX_HTTP_SSI_EXPR_TEST : 0;
|
|
2343
|
102
|
2344 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2345 "left: \"%V\"", &left);
|
|
2346
|
248
|
2347 rc = ngx_http_ssi_evaluate_string(r, ctx, &left, flags);
|
|
2348
|
|
2349 if (rc != NGX_OK) {
|
|
2350 return rc;
|
58
|
2351 }
|
|
2352
|
102
|
2353 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2354 "evaluted left: \"%V\"", &left);
|
|
2355
|
100
|
2356 if (p == last) {
|
102
|
2357 if (left.len) {
|
100
|
2358 ctx->output = 1;
|
178
|
2359 ctx->output_chosen = 1;
|
100
|
2360
|
|
2361 } else {
|
|
2362 ctx->output = 0;
|
|
2363 }
|
|
2364
|
178
|
2365 ctx->conditional = NGX_HTTP_SSI_COND_IF;
|
|
2366
|
100
|
2367 return NGX_OK;
|
|
2368 }
|
|
2369
|
|
2370 if (p < last && *p == '=') {
|
|
2371 negative = 0;
|
|
2372 p++;
|
|
2373
|
|
2374 } else if (p + 1 < last && *p == '!' && *(p + 1) == '=') {
|
|
2375 negative = 1;
|
|
2376 p += 2;
|
|
2377
|
|
2378 } else {
|
|
2379 goto invalid_expression;
|
|
2380 }
|
|
2381
|
|
2382 while (p < last && *p == ' ') {
|
|
2383 p++;
|
|
2384 }
|
|
2385
|
300
|
2386 if (p < last - 1 && *p == '/') {
|
100
|
2387 if (*(last - 1) != '/') {
|
|
2388 goto invalid_expression;
|
|
2389 }
|
|
2390
|
|
2391 noregex = 0;
|
178
|
2392 flags = NGX_HTTP_SSI_ADD_ZERO;
|
100
|
2393 last--;
|
274
|
2394 p++;
|
100
|
2395
|
|
2396 } else {
|
|
2397 noregex = 1;
|
178
|
2398 flags = 0;
|
|
2399
|
|
2400 if (p < last - 1 && p[0] == '\\' && p[1] == '/') {
|
200
|
2401 p++;
|
178
|
2402 }
|
100
|
2403 }
|
|
2404
|
|
2405 right.len = last - p;
|
|
2406 right.data = p;
|
|
2407
|
102
|
2408 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2409 "right: \"%V\"", &right);
|
|
2410
|
248
|
2411 rc = ngx_http_ssi_evaluate_string(r, ctx, &right, flags);
|
|
2412
|
|
2413 if (rc != NGX_OK) {
|
|
2414 return rc;
|
100
|
2415 }
|
|
2416
|
102
|
2417 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2418 "evaluted right: \"%V\"", &right);
|
100
|
2419
|
|
2420 if (noregex) {
|
|
2421 if (left.len != right.len) {
|
|
2422 rc = -1;
|
|
2423
|
|
2424 } else {
|
|
2425 rc = ngx_strncmp(left.data, right.data, right.len);
|
|
2426 }
|
|
2427
|
|
2428 } else {
|
|
2429 #if (NGX_PCRE)
|
|
2430 err.len = NGX_MAX_CONF_ERRSTR;
|
|
2431 err.data = errstr;
|
|
2432
|
|
2433 right.data[right.len] = '\0';
|
|
2434
|
|
2435 regex = ngx_regex_compile(&right, 0, r->pool, &err);
|
|
2436
|
|
2437 if (regex == NULL) {
|
|
2438 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", err.data);
|
|
2439 return NGX_HTTP_SSI_ERROR;
|
|
2440 }
|
|
2441
|
|
2442 rc = ngx_regex_exec(regex, &left, NULL, 0);
|
|
2443
|
|
2444 if (rc != NGX_REGEX_NO_MATCHED && rc < 0) {
|
|
2445 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
2446 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
|
|
2447 rc, &left, &right);
|
|
2448 return NGX_HTTP_SSI_ERROR;
|
|
2449 }
|
|
2450 #else
|
|
2451 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
2452 "the using of the regex \"%V\" in SSI "
|
|
2453 "requires PCRE library", &right);
|
|
2454
|
|
2455 return NGX_HTTP_SSI_ERROR;
|
|
2456 #endif
|
|
2457 }
|
|
2458
|
|
2459 if ((rc == 0 && !negative) || (rc != 0 && negative)) {
|
58
|
2460 ctx->output = 1;
|
178
|
2461 ctx->output_chosen = 1;
|
58
|
2462
|
|
2463 } else {
|
|
2464 ctx->output = 0;
|
|
2465 }
|
|
2466
|
178
|
2467 ctx->conditional = NGX_HTTP_SSI_COND_IF;
|
|
2468
|
58
|
2469 return NGX_OK;
|
100
|
2470
|
|
2471 invalid_expression:
|
|
2472
|
|
2473 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
2474 "invalid expression in \"%V\"", expr);
|
|
2475
|
|
2476 return NGX_HTTP_SSI_ERROR;
|
58
|
2477 }
|
|
2478
|
|
2479
|
|
2480 static ngx_int_t
|
|
2481 ngx_http_ssi_else(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 else");
|
|
2486
|
178
|
2487 if (ctx->output_chosen) {
|
|
2488 ctx->output = 0;
|
|
2489 } else {
|
|
2490 ctx->output = 1;
|
|
2491 }
|
|
2492
|
|
2493 ctx->conditional = NGX_HTTP_SSI_COND_ELSE;
|
58
|
2494
|
|
2495 return NGX_OK;
|
|
2496 }
|
|
2497
|
|
2498
|
|
2499 static ngx_int_t
|
|
2500 ngx_http_ssi_endif(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2501 ngx_str_t **params)
|
|
2502 {
|
192
|
2503 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2504 "ssi endif");
|
|
2505
|
58
|
2506 ctx->output = 1;
|
178
|
2507 ctx->output_chosen = 0;
|
|
2508 ctx->conditional = 0;
|
58
|
2509
|
|
2510 return NGX_OK;
|
|
2511 }
|
|
2512
|
|
2513
|
122
|
2514 static ngx_int_t
|
216
|
2515 ngx_http_ssi_block(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2516 ngx_str_t **params)
|
|
2517 {
|
|
2518 ngx_http_ssi_ctx_t *mctx;
|
|
2519 ngx_http_ssi_block_t *bl;
|
|
2520
|
|
2521 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2522 "ssi block");
|
|
2523
|
|
2524 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
2525
|
|
2526 if (mctx->blocks == NULL) {
|
|
2527 mctx->blocks = ngx_array_create(r->pool, 4,
|
|
2528 sizeof(ngx_http_ssi_block_t));
|
|
2529 if (mctx->blocks == NULL) {
|
|
2530 return NGX_HTTP_SSI_ERROR;
|
|
2531 }
|
|
2532 }
|
|
2533
|
|
2534 bl = ngx_array_push(mctx->blocks);
|
|
2535 if (bl == NULL) {
|
|
2536 return NGX_HTTP_SSI_ERROR;
|
|
2537 }
|
|
2538
|
|
2539 bl->name = *params[NGX_HTTP_SSI_BLOCK_NAME];
|
|
2540 bl->bufs = NULL;
|
|
2541 bl->count = 0;
|
|
2542
|
|
2543 ctx->output = 0;
|
|
2544 ctx->block = 1;
|
|
2545
|
|
2546 return NGX_OK;
|
|
2547 }
|
|
2548
|
|
2549
|
|
2550 static ngx_int_t
|
|
2551 ngx_http_ssi_endblock(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
2552 ngx_str_t **params)
|
|
2553 {
|
|
2554 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2555 "ssi endblock");
|
|
2556
|
|
2557 ctx->output = 1;
|
|
2558 ctx->block = 0;
|
|
2559
|
|
2560 return NGX_OK;
|
|
2561 }
|
|
2562
|
|
2563
|
|
2564 static ngx_int_t
|
122
|
2565 ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
|
|
2566 ngx_http_variable_value_t *v, uintptr_t gmt)
|
58
|
2567 {
|
122
|
2568 ngx_http_ssi_ctx_t *ctx;
|
|
2569 ngx_time_t *tp;
|
|
2570 struct tm tm;
|
|
2571 char buf[NGX_HTTP_SSI_DATE_LEN];
|
|
2572
|
126
|
2573 v->valid = 1;
|
122
|
2574 v->no_cachable = 0;
|
|
2575 v->not_found = 0;
|
58
|
2576
|
112
|
2577 tp = ngx_timeofday();
|
|
2578
|
58
|
2579 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
|
|
2580
|
|
2581 if (ctx->timefmt.len == sizeof("%s") - 1
|
|
2582 && ctx->timefmt.data[0] == '%' && ctx->timefmt.data[1] == 's')
|
|
2583 {
|
122
|
2584 v->data = ngx_palloc(r->pool, NGX_TIME_T_LEN);
|
|
2585 if (v->data == NULL) {
|
|
2586 return NGX_ERROR;
|
58
|
2587 }
|
|
2588
|
122
|
2589 v->len = ngx_sprintf(v->data, "%T", tp->sec + (gmt ? 0 : tp->gmtoff))
|
|
2590 - v->data;
|
206
|
2591
|
122
|
2592 return NGX_OK;
|
58
|
2593 }
|
|
2594
|
|
2595 if (gmt) {
|
112
|
2596 ngx_libc_gmtime(tp->sec, &tm);
|
58
|
2597 } else {
|
112
|
2598 ngx_libc_localtime(tp->sec, &tm);
|
58
|
2599 }
|
|
2600
|
122
|
2601 v->len = strftime(buf, NGX_HTTP_SSI_DATE_LEN,
|
|
2602 (char *) ctx->timefmt.data, &tm);
|
|
2603 if (v->len == 0) {
|
|
2604 return NGX_ERROR;
|
58
|
2605 }
|
|
2606
|
122
|
2607 v->data = ngx_palloc(r->pool, v->len);
|
|
2608 if (v->data == NULL) {
|
|
2609 return NGX_ERROR;
|
58
|
2610 }
|
|
2611
|
122
|
2612 ngx_memcpy(v->data, buf, v->len);
|
|
2613
|
|
2614 return NGX_OK;
|
58
|
2615 }
|
|
2616
|
|
2617
|
76
|
2618 static char *
|
|
2619 ngx_http_ssi_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2620 {
|
146
|
2621 ngx_http_ssi_loc_conf_t *slcf = conf;
|
76
|
2622
|
|
2623 ngx_str_t *value, *type;
|
|
2624 ngx_uint_t i;
|
|
2625
|
146
|
2626 if (slcf->types == NULL) {
|
|
2627 slcf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
|
|
2628 if (slcf->types == NULL) {
|
76
|
2629 return NGX_CONF_ERROR;
|
|
2630 }
|
|
2631
|
146
|
2632 type = ngx_array_push(slcf->types);
|
76
|
2633 if (type == NULL) {
|
|
2634 return NGX_CONF_ERROR;
|
|
2635 }
|
|
2636
|
|
2637 type->len = sizeof("text/html") - 1;
|
|
2638 type->data = (u_char *) "text/html";
|
|
2639 }
|
|
2640
|
|
2641 value = cf->args->elts;
|
|
2642
|
|
2643 for (i = 1; i < cf->args->nelts; i++) {
|
|
2644
|
|
2645 if (ngx_strcmp(value[i].data, "text/html") == 0) {
|
|
2646 continue;
|
|
2647 }
|
|
2648
|
146
|
2649 type = ngx_array_push(slcf->types);
|
76
|
2650 if (type == NULL) {
|
|
2651 return NGX_CONF_ERROR;
|
|
2652 }
|
|
2653
|
|
2654 type->len = value[i].len;
|
|
2655
|
|
2656 type->data = ngx_palloc(cf->pool, type->len + 1);
|
|
2657 if (type->data == NULL) {
|
|
2658 return NGX_CONF_ERROR;
|
|
2659 }
|
|
2660
|
|
2661 ngx_cpystrn(type->data, value[i].data, type->len + 1);
|
|
2662 }
|
|
2663
|
|
2664 return NGX_CONF_OK;
|
|
2665 }
|
|
2666
|
|
2667
|
58
|
2668 static ngx_int_t
|
146
|
2669 ngx_http_ssi_preconfiguration(ngx_conf_t *cf)
|
58
|
2670 {
|
146
|
2671 ngx_int_t rc;
|
|
2672 ngx_http_variable_t *var, *v;
|
|
2673 ngx_http_ssi_command_t *cmd;
|
|
2674 ngx_http_ssi_main_conf_t *smcf;
|
58
|
2675
|
|
2676 for (v = ngx_http_ssi_vars; v->name.len; v++) {
|
|
2677 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2678 if (var == NULL) {
|
|
2679 return NGX_ERROR;
|
|
2680 }
|
|
2681
|
186
|
2682 var->get_handler = v->get_handler;
|
58
|
2683 var->data = v->data;
|
|
2684 }
|
|
2685
|
146
|
2686 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
|
|
2687
|
|
2688 for (cmd = ngx_http_ssi_commands; cmd->name.len; cmd++) {
|
|
2689 rc = ngx_hash_add_key(&smcf->commands, &cmd->name, cmd,
|
|
2690 NGX_HASH_READONLY_KEY);
|
|
2691
|
|
2692 if (rc == NGX_OK) {
|
|
2693 continue;
|
|
2694 }
|
|
2695
|
|
2696 if (rc == NGX_BUSY) {
|
|
2697 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2698 "conflicting SSI command \"%V\"", &cmd->name);
|
|
2699 }
|
|
2700
|
|
2701 return NGX_ERROR;
|
|
2702 }
|
|
2703
|
126
|
2704 return NGX_OK;
|
58
|
2705 }
|
|
2706
|
|
2707
|
46
|
2708 static void *
|
146
|
2709 ngx_http_ssi_create_main_conf(ngx_conf_t *cf)
|
|
2710 {
|
|
2711 ngx_http_ssi_main_conf_t *smcf;
|
|
2712
|
|
2713 smcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_main_conf_t));
|
|
2714 if (smcf == NULL) {
|
|
2715 return NGX_CONF_ERROR;
|
|
2716 }
|
|
2717
|
|
2718 smcf->commands.pool = cf->pool;
|
|
2719 smcf->commands.temp_pool = cf->temp_pool;
|
|
2720
|
|
2721 if (ngx_hash_keys_array_init(&smcf->commands, NGX_HASH_SMALL) != NGX_OK) {
|
|
2722 return NGX_CONF_ERROR;
|
|
2723 }
|
|
2724
|
|
2725 return smcf;
|
|
2726 }
|
|
2727
|
|
2728
|
|
2729 static char *
|
|
2730 ngx_http_ssi_init_main_conf(ngx_conf_t *cf, void *conf)
|
|
2731 {
|
|
2732 ngx_http_ssi_main_conf_t *smcf = conf;
|
|
2733
|
|
2734 ngx_hash_init_t hash;
|
|
2735
|
|
2736 hash.hash = &smcf->hash;
|
|
2737 hash.key = ngx_hash_key;
|
|
2738 hash.max_size = 1024;
|
|
2739 hash.bucket_size = ngx_cacheline_size;
|
|
2740 hash.name = "ssi_command_hash";
|
|
2741 hash.pool = cf->pool;
|
|
2742 hash.temp_pool = NULL;
|
|
2743
|
|
2744 if (ngx_hash_init(&hash, smcf->commands.keys.elts,
|
|
2745 smcf->commands.keys.nelts)
|
|
2746 != NGX_OK)
|
|
2747 {
|
|
2748 return NGX_CONF_ERROR;
|
|
2749 }
|
|
2750
|
|
2751 return NGX_CONF_OK;
|
|
2752 }
|
|
2753
|
|
2754
|
|
2755 static void *
|
296
|
2756 ngx_http_ssi_create_loc_conf(ngx_conf_t *cf)
|
46
|
2757 {
|
146
|
2758 ngx_http_ssi_loc_conf_t *slcf;
|
|
2759
|
|
2760 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_loc_conf_t));
|
|
2761 if (slcf == NULL) {
|
46
|
2762 return NGX_CONF_ERROR;
|
|
2763 }
|
|
2764
|
76
|
2765 /*
|
|
2766 * set by ngx_pcalloc():
|
|
2767 *
|
|
2768 * conf->types = NULL;
|
|
2769 */
|
|
2770
|
146
|
2771 slcf->enable = NGX_CONF_UNSET;
|
|
2772 slcf->silent_errors = NGX_CONF_UNSET;
|
|
2773 slcf->ignore_recycled_buffers = NGX_CONF_UNSET;
|
|
2774
|
|
2775 slcf->min_file_chunk = NGX_CONF_UNSET_SIZE;
|
|
2776 slcf->value_len = NGX_CONF_UNSET_SIZE;
|
|
2777
|
|
2778 return slcf;
|
46
|
2779 }
|
|
2780
|
|
2781
|
|
2782 static char *
|
296
|
2783 ngx_http_ssi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
46
|
2784 {
|
146
|
2785 ngx_http_ssi_loc_conf_t *prev = parent;
|
|
2786 ngx_http_ssi_loc_conf_t *conf = child;
|
46
|
2787
|
76
|
2788 ngx_str_t *type;
|
|
2789
|
46
|
2790 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
2791 ngx_conf_merge_value(conf->silent_errors, prev->silent_errors, 0);
|
58
|
2792 ngx_conf_merge_value(conf->ignore_recycled_buffers,
|
|
2793 prev->ignore_recycled_buffers, 0);
|
46
|
2794
|
|
2795 ngx_conf_merge_size_value(conf->min_file_chunk, prev->min_file_chunk, 1024);
|
|
2796 ngx_conf_merge_size_value(conf->value_len, prev->value_len, 256);
|
|
2797
|
76
|
2798 if (conf->types == NULL) {
|
|
2799 if (prev->types == NULL) {
|
|
2800 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
|
|
2801 if (conf->types == NULL) {
|
|
2802 return NGX_CONF_ERROR;
|
|
2803 }
|
|
2804
|
|
2805 type = ngx_array_push(conf->types);
|
|
2806 if (type == NULL) {
|
|
2807 return NGX_CONF_ERROR;
|
|
2808 }
|
|
2809
|
|
2810 type->len = sizeof("text/html") - 1;
|
|
2811 type->data = (u_char *) "text/html";
|
|
2812
|
|
2813 } else {
|
|
2814 conf->types = prev->types;
|
|
2815 }
|
|
2816 }
|
|
2817
|
46
|
2818 return NGX_CONF_OK;
|
|
2819 }
|
|
2820
|
|
2821
|
|
2822 static ngx_int_t
|
230
|
2823 ngx_http_ssi_filter_init(ngx_conf_t *cf)
|
46
|
2824 {
|
|
2825 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
2826 ngx_http_top_header_filter = ngx_http_ssi_header_filter;
|
|
2827
|
|
2828 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
2829 ngx_http_top_body_filter = ngx_http_ssi_body_filter;
|
|
2830
|
|
2831 return NGX_OK;
|
|
2832 }
|