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