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