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_MAX_PARAMS 16
|
|
12
|
|
13 #define NGX_HTTP_SSI_COMMAND_LEN 31
|
|
14 #define NGX_HTTP_SSI_PARAM_LEN 31
|
|
15 #define NGX_HTTP_SSI_PARAMS_N 4
|
|
16
|
|
17 #define NGX_HTTP_SSI_ERROR 1
|
|
18
|
|
19 #define NGX_HTTP_SSI_DATE_LEN 2048
|
|
20
|
|
21
|
|
22 #define NGX_HTTP_SSI_ADD_PREFIX 1
|
58
|
23
|
46
|
24
|
|
25 typedef struct {
|
|
26 ngx_flag_t enable;
|
|
27 ngx_flag_t silent_errors;
|
58
|
28 ngx_flag_t ignore_recycled_buffers;
|
46
|
29
|
76
|
30 ngx_array_t *types; /* array of ngx_str_t */
|
|
31
|
46
|
32 size_t min_file_chunk;
|
|
33 size_t value_len;
|
|
34 } ngx_http_ssi_conf_t;
|
|
35
|
|
36
|
|
37 typedef struct {
|
102
|
38 ngx_str_t name;
|
|
39 ngx_str_t value;
|
|
40 } ngx_http_ssi_var_t;
|
|
41
|
|
42
|
|
43 typedef struct {
|
46
|
44 ngx_buf_t *buf;
|
|
45
|
|
46 u_char *pos;
|
|
47 u_char *copy_start;
|
|
48 u_char *copy_end;
|
|
49
|
|
50 ngx_str_t command;
|
|
51 ngx_array_t params;
|
|
52 ngx_table_elt_t *param;
|
|
53 ngx_table_elt_t params_array[NGX_HTTP_SSI_PARAMS_N];
|
|
54
|
|
55 ngx_chain_t *in;
|
|
56 ngx_chain_t *out;
|
|
57 ngx_chain_t **last_out;
|
48
|
58 ngx_chain_t *busy;
|
|
59 ngx_chain_t *free;
|
46
|
60
|
|
61 ngx_uint_t state;
|
|
62 ngx_uint_t saved_state;
|
|
63 size_t saved;
|
|
64 size_t looked;
|
|
65
|
|
66 size_t value_len;
|
58
|
67
|
102
|
68 ngx_array_t variables;
|
|
69
|
58
|
70 ngx_uint_t output; /* unsigned output:1; */
|
|
71
|
|
72 ngx_str_t timefmt;
|
96
|
73 ngx_str_t errmsg;
|
46
|
74 } ngx_http_ssi_ctx_t;
|
|
75
|
|
76
|
|
77 typedef ngx_int_t (*ngx_http_ssi_command_pt) (ngx_http_request_t *r,
|
|
78 ngx_http_ssi_ctx_t *ctx, ngx_str_t **);
|
|
79
|
|
80
|
|
81 typedef struct {
|
|
82 ngx_str_t name;
|
|
83 ngx_uint_t index;
|
|
84
|
|
85 ngx_uint_t mandatory;
|
|
86 } ngx_http_ssi_param_t;
|
|
87
|
|
88
|
|
89 typedef struct {
|
|
90 ngx_str_t name;
|
|
91 ngx_http_ssi_command_pt handler;
|
|
92 ngx_http_ssi_param_t *params;
|
|
93
|
58
|
94 unsigned conditional:1;
|
|
95 unsigned flush:1;
|
46
|
96 } ngx_http_ssi_command_t;
|
|
97
|
|
98
|
|
99 typedef enum {
|
|
100 ssi_start_state = 0,
|
|
101 ssi_tag_state,
|
|
102 ssi_comment0_state,
|
|
103 ssi_comment1_state,
|
|
104 ssi_sharp_state,
|
|
105 ssi_precommand_state,
|
|
106 ssi_command_state,
|
|
107 ssi_preparam_state,
|
|
108 ssi_param_state,
|
|
109 ssi_preequal_state,
|
|
110 ssi_prevalue_state,
|
|
111 ssi_double_quoted_value_state,
|
|
112 ssi_quoted_value_state,
|
|
113 ssi_quoted_symbol_state,
|
|
114 ssi_postparam_state,
|
|
115 ssi_comment_end0_state,
|
|
116 ssi_comment_end1_state,
|
|
117 ssi_error_state,
|
|
118 ssi_error_end0_state,
|
|
119 ssi_error_end1_state
|
|
120 } ngx_http_ssi_state_e;
|
|
121
|
|
122
|
58
|
123 static ngx_int_t ngx_http_ssi_output(ngx_http_request_t *r,
|
|
124 ngx_http_ssi_ctx_t *ctx);
|
46
|
125 static ngx_int_t ngx_http_ssi_parse(ngx_http_request_t *r,
|
|
126 ngx_http_ssi_ctx_t *ctx);
|
102
|
127 static ngx_str_t *ngx_http_ssi_get_variable(ngx_http_request_t *r,
|
|
128 ngx_str_t *name);
|
|
129 static ngx_int_t ngx_http_ssi_evaluate_string(ngx_http_request_t *r,
|
|
130 ngx_http_ssi_ctx_t *ctx, ngx_str_t *text, ngx_uint_t flags);
|
|
131
|
|
132 static ngx_int_t ngx_http_ssi_include(ngx_http_request_t *r,
|
|
133 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
46
|
134 static ngx_int_t ngx_http_ssi_echo(ngx_http_request_t *r,
|
|
135 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
58
|
136 static ngx_int_t ngx_http_ssi_config(ngx_http_request_t *r,
|
|
137 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
102
|
138 static ngx_int_t ngx_http_ssi_set(ngx_http_request_t *r,
|
58
|
139 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
|
140 static ngx_int_t ngx_http_ssi_if(ngx_http_request_t *r,
|
|
141 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
|
142 static ngx_int_t ngx_http_ssi_else(ngx_http_request_t *r,
|
|
143 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
|
144 static ngx_int_t ngx_http_ssi_endif(ngx_http_request_t *r,
|
|
145 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
|
46
|
146
|
58
|
147 static ngx_http_variable_value_t *
|
|
148 ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r, uintptr_t gmt);
|
|
149
|
76
|
150 static char *ngx_http_ssi_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
151
|
58
|
152 static ngx_int_t ngx_http_ssi_add_variables(ngx_conf_t *cf);
|
46
|
153 static void *ngx_http_ssi_create_conf(ngx_conf_t *cf);
|
|
154 static char *ngx_http_ssi_merge_conf(ngx_conf_t *cf,
|
|
155 void *parent, void *child);
|
|
156 static ngx_int_t ngx_http_ssi_filter_init(ngx_cycle_t *cycle);
|
|
157
|
|
158
|
|
159 static ngx_command_t ngx_http_ssi_filter_commands[] = {
|
|
160
|
|
161 { ngx_string("ssi"),
|
|
162 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
163 ngx_conf_set_flag_slot,
|
|
164 NGX_HTTP_LOC_CONF_OFFSET,
|
|
165 offsetof(ngx_http_ssi_conf_t, enable),
|
|
166 NULL },
|
|
167
|
|
168 { ngx_string("ssi_silent_errors"),
|
|
169 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
170 ngx_conf_set_flag_slot,
|
|
171 NGX_HTTP_LOC_CONF_OFFSET,
|
|
172 offsetof(ngx_http_ssi_conf_t, silent_errors),
|
|
173 NULL },
|
|
174
|
58
|
175 { ngx_string("ssi_ignore_recycled_buffers"),
|
|
176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
177 ngx_conf_set_flag_slot,
|
|
178 NGX_HTTP_LOC_CONF_OFFSET,
|
|
179 offsetof(ngx_http_ssi_conf_t, ignore_recycled_buffers),
|
|
180 NULL },
|
|
181
|
46
|
182 { ngx_string("ssi_min_file_chunk"),
|
|
183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
184 ngx_conf_set_size_slot,
|
|
185 NGX_HTTP_LOC_CONF_OFFSET,
|
|
186 offsetof(ngx_http_ssi_conf_t, min_file_chunk),
|
|
187 NULL },
|
|
188
|
76
|
189 { ngx_string("ssi_types"),
|
|
190 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
191 ngx_http_ssi_types,
|
|
192 NGX_HTTP_LOC_CONF_OFFSET,
|
|
193 0,
|
|
194 NULL },
|
|
195
|
46
|
196 ngx_null_command
|
|
197 };
|
|
198
|
|
199
|
|
200
|
|
201 static ngx_http_module_t ngx_http_ssi_filter_module_ctx = {
|
58
|
202 ngx_http_ssi_add_variables, /* preconfiguration */
|
|
203 NULL, /* postconfiguration */
|
46
|
204
|
|
205 NULL, /* create main configuration */
|
|
206 NULL, /* init main configuration */
|
|
207
|
|
208 NULL, /* create server configuration */
|
|
209 NULL, /* merge server configuration */
|
|
210
|
|
211 ngx_http_ssi_create_conf, /* create location configuration */
|
|
212 ngx_http_ssi_merge_conf /* merge location configuration */
|
|
213 };
|
|
214
|
|
215
|
|
216 ngx_module_t ngx_http_ssi_filter_module = {
|
58
|
217 NGX_MODULE_V1,
|
46
|
218 &ngx_http_ssi_filter_module_ctx, /* module context */
|
|
219 ngx_http_ssi_filter_commands, /* module directives */
|
|
220 NGX_HTTP_MODULE, /* module type */
|
90
|
221 NULL, /* init master */
|
46
|
222 ngx_http_ssi_filter_init, /* init module */
|
90
|
223 NULL, /* init process */
|
|
224 NULL, /* init thread */
|
|
225 NULL, /* exit thread */
|
|
226 NULL, /* exit process */
|
|
227 NULL, /* exit master */
|
|
228 NGX_MODULE_V1_PADDING
|
46
|
229 };
|
|
230
|
|
231
|
118
|
232 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
233 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
46
|
234
|
|
235
|
|
236 static u_char ngx_http_ssi_string[] = "<!--";
|
|
237
|
|
238 static ngx_str_t ngx_http_ssi_none = ngx_string("(none)");
|
|
239
|
102
|
240 #define NGX_HTTP_SSI_INCLUDE_VIRTUAL 0
|
|
241 #define NGX_HTTP_SSI_INCLUDE_FILE 1
|
46
|
242
|
58
|
243 #define NGX_HTTP_SSI_ECHO_VAR 0
|
|
244 #define NGX_HTTP_SSI_ECHO_DEFAULT 1
|
|
245
|
96
|
246 #define NGX_HTTP_SSI_CONFIG_ERRMSG 0
|
|
247 #define NGX_HTTP_SSI_CONFIG_TIMEFMT 1
|
58
|
248
|
102
|
249 #define NGX_HTTP_SSI_SET_VAR 0
|
|
250 #define NGX_HTTP_SSI_SET_VALUE 1
|
58
|
251
|
|
252 #define NGX_HTTP_SSI_IF_EXPR 0
|
|
253
|
46
|
254
|
102
|
255 static ngx_http_ssi_param_t ngx_http_ssi_include_params[] = {
|
|
256 { ngx_string("virtual"), NGX_HTTP_SSI_INCLUDE_VIRTUAL, 0 },
|
|
257 { ngx_string("file"), NGX_HTTP_SSI_INCLUDE_FILE, 0 },
|
|
258 { ngx_null_string, 0, 0 }
|
|
259 };
|
|
260
|
|
261
|
46
|
262 static ngx_http_ssi_param_t ngx_http_ssi_echo_params[] = {
|
|
263 { ngx_string("var"), NGX_HTTP_SSI_ECHO_VAR, 1 },
|
|
264 { ngx_string("default"), NGX_HTTP_SSI_ECHO_DEFAULT, 0 },
|
|
265 { ngx_null_string, 0, 0 }
|
|
266 };
|
|
267
|
58
|
268
|
|
269 static ngx_http_ssi_param_t ngx_http_ssi_config_params[] = {
|
96
|
270 { ngx_string("errmsg"), NGX_HTTP_SSI_CONFIG_ERRMSG, 0 },
|
58
|
271 { ngx_string("timefmt"), NGX_HTTP_SSI_CONFIG_TIMEFMT, 0 },
|
|
272 { ngx_null_string, 0, 0 }
|
|
273 };
|
|
274
|
|
275
|
102
|
276 static ngx_http_ssi_param_t ngx_http_ssi_set_params[] = {
|
|
277 { ngx_string("var"), NGX_HTTP_SSI_SET_VAR, 1 },
|
|
278 { ngx_string("value"), NGX_HTTP_SSI_SET_VALUE, 1 },
|
|
279 { ngx_null_string, 0, 0 }
|
|
280 };
|
|
281
|
|
282
|
58
|
283 static ngx_http_ssi_param_t ngx_http_ssi_if_params[] = {
|
102
|
284 { ngx_string("expr"), NGX_HTTP_SSI_IF_EXPR, 1 },
|
58
|
285 { ngx_null_string, 0, 0 }
|
|
286 };
|
|
287
|
|
288
|
|
289 static ngx_http_ssi_param_t ngx_http_ssi_no_params[] = {
|
|
290 { ngx_null_string, 0, 0 }
|
|
291 };
|
|
292
|
46
|
293
|
|
294 static ngx_http_ssi_command_t ngx_http_ssi_commands[] = {
|
102
|
295 { ngx_string("include"), ngx_http_ssi_include,
|
|
296 ngx_http_ssi_include_params, 0, 1 },
|
58
|
297 { ngx_string("echo"), ngx_http_ssi_echo, ngx_http_ssi_echo_params, 0, 0 },
|
|
298 { ngx_string("config"), ngx_http_ssi_config,
|
|
299 ngx_http_ssi_config_params, 0, 0 },
|
102
|
300 { ngx_string("set"), ngx_http_ssi_set, ngx_http_ssi_set_params, 0, 0 },
|
58
|
301
|
|
302 { ngx_string("if"), ngx_http_ssi_if, ngx_http_ssi_if_params, 0, 0 },
|
|
303 { ngx_string("else"), ngx_http_ssi_else, ngx_http_ssi_no_params, 1, 0 },
|
|
304 { ngx_string("endif"), ngx_http_ssi_endif, ngx_http_ssi_no_params, 1, 0 },
|
|
305
|
|
306 { ngx_null_string, NULL, NULL, 0, 0 }
|
46
|
307 };
|
|
308
|
|
309
|
58
|
310 static ngx_http_variable_t ngx_http_ssi_vars[] = {
|
|
311
|
|
312 { ngx_string("date_local"), ngx_http_ssi_date_gmt_local_variable, 0,
|
82
|
313 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
58
|
314
|
|
315 { ngx_string("date_gmt"), ngx_http_ssi_date_gmt_local_variable, 1,
|
82
|
316 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
58
|
317
|
82
|
318 { ngx_null_string, NULL, 0, 0, 0 }
|
58
|
319 };
|
|
320
|
|
321
|
|
322
|
46
|
323 static ngx_int_t
|
|
324 ngx_http_ssi_header_filter(ngx_http_request_t *r)
|
|
325 {
|
76
|
326 ngx_uint_t i;
|
|
327 ngx_str_t *type;
|
46
|
328 ngx_http_ssi_ctx_t *ctx;
|
|
329 ngx_http_ssi_conf_t *conf;
|
|
330
|
|
331 conf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
|
|
332
|
76
|
333 if (!conf->enable
|
|
334 || r->headers_out.content_type.len == 0)
|
46
|
335 {
|
|
336 return ngx_http_next_header_filter(r);
|
|
337 }
|
|
338
|
|
339
|
76
|
340 type = conf->types->elts;
|
|
341 for (i = 0; i < conf->types->nelts; i++) {
|
|
342 if (r->headers_out.content_type.len >= type[i].len
|
|
343 && ngx_strncasecmp(r->headers_out.content_type.data,
|
|
344 type[i].data, type[i].len) == 0)
|
|
345 {
|
|
346 goto found;
|
|
347 }
|
|
348 }
|
|
349
|
|
350 return ngx_http_next_header_filter(r);
|
|
351
|
|
352
|
|
353 found:
|
|
354
|
48
|
355 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t));
|
|
356 if (ctx == NULL) {
|
46
|
357 return NGX_ERROR;
|
|
358 }
|
|
359
|
|
360 ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module);
|
|
361
|
|
362
|
|
363 ctx->value_len = conf->value_len;
|
|
364 ctx->last_out = &ctx->out;
|
|
365
|
58
|
366 ctx->output = 1;
|
|
367
|
46
|
368 ctx->params.elts = ctx->params_array;
|
|
369 ctx->params.size = sizeof(ngx_table_elt_t);
|
|
370 ctx->params.nalloc = NGX_HTTP_SSI_PARAMS_N;
|
|
371 ctx->params.pool = r->pool;
|
|
372
|
58
|
373 ctx->timefmt.len = sizeof("%A, %d-%b-%Y %H:%M:%S %Z") - 1;
|
|
374 ctx->timefmt.data = (u_char *) "%A, %d-%b-%Y %H:%M:%S %Z";
|
46
|
375
|
96
|
376 ctx->errmsg.len =
|
|
377 sizeof("[an error occurred while processing the directive]") - 1;
|
|
378 ctx->errmsg.data = (u_char *)
|
|
379 "[an error occurred while processing the directive]";
|
|
380
|
46
|
381 r->filter_need_in_memory = 1;
|
|
382
|
102
|
383 if (r->main == r) {
|
58
|
384 r->headers_out.content_length_n = -1;
|
|
385 if (r->headers_out.content_length) {
|
|
386 r->headers_out.content_length->hash = 0;
|
|
387 r->headers_out.content_length = NULL;
|
|
388 }
|
|
389
|
|
390 r->headers_out.last_modified_time = -1;
|
|
391 if (r->headers_out.last_modified) {
|
|
392 r->headers_out.last_modified->hash = 0;
|
|
393 r->headers_out.last_modified = NULL;
|
|
394 }
|
|
395 }
|
|
396
|
46
|
397 return ngx_http_next_header_filter(r);
|
|
398 }
|
|
399
|
|
400
|
|
401 static ngx_int_t
|
|
402 ngx_http_ssi_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
403 {
|
|
404 ngx_int_t rc;
|
|
405 ngx_uint_t i;
|
|
406 ngx_buf_t *b;
|
|
407 ngx_chain_t *cl;
|
|
408 ngx_table_elt_t *param;
|
118
|
409 ngx_connection_t *c;
|
46
|
410 ngx_http_ssi_ctx_t *ctx;
|
|
411 ngx_http_ssi_conf_t *conf;
|
|
412 ngx_http_ssi_param_t *prm;
|
|
413 ngx_http_ssi_command_t *cmd;
|
|
414 ngx_str_t *params[NGX_HTTP_SSI_MAX_PARAMS];
|
|
415
|
|
416 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
|
|
417
|
48
|
418 if (ctx == NULL || (in == NULL && ctx->in == NULL && ctx->busy == NULL)) {
|
46
|
419 return ngx_http_next_body_filter(r, in);
|
|
420 }
|
|
421
|
|
422 /* add the incoming chain to the chain ctx->in */
|
|
423
|
|
424 if (in) {
|
|
425 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
|
|
426 return NGX_ERROR;
|
|
427 }
|
|
428 }
|
|
429
|
|
430 conf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
|
|
431
|
58
|
432 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
433 "http ssi filter \"%V\"", &r->uri);
|
46
|
434
|
48
|
435 while (ctx->in || ctx->buf) {
|
46
|
436
|
48
|
437 if (ctx->buf == NULL ){
|
|
438 ctx->buf = ctx->in->buf;
|
|
439 ctx->in = ctx->in->next;
|
|
440 ctx->pos = ctx->buf->pos;
|
|
441 }
|
46
|
442
|
|
443 if (ctx->state == ssi_start_state) {
|
|
444 ctx->copy_start = ctx->pos;
|
|
445 ctx->copy_end = ctx->pos;
|
|
446 }
|
|
447
|
48
|
448 b = NULL;
|
|
449
|
46
|
450 while (ctx->pos < ctx->buf->last) {
|
|
451
|
|
452 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
453 "saved: %d state: %d", ctx->saved, ctx->state);
|
|
454
|
|
455 rc = ngx_http_ssi_parse(r, ctx);
|
|
456
|
|
457 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
458 "parse: %d, looked: %d %p-%p",
|
|
459 rc, ctx->looked, ctx->copy_start, ctx->copy_end);
|
|
460
|
|
461 if (rc == NGX_ERROR) {
|
|
462 return rc;
|
|
463 }
|
|
464
|
|
465 if (ctx->copy_start != ctx->copy_end) {
|
|
466
|
58
|
467 if (ctx->output) {
|
|
468
|
|
469 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
470 "saved: %d", ctx->saved);
|
|
471
|
|
472 if (ctx->saved) {
|
|
473
|
|
474 if (ctx->free) {
|
|
475 cl = ctx->free;
|
|
476 ctx->free = ctx->free->next;
|
|
477 b = cl->buf;
|
|
478 ngx_memzero(b, sizeof(ngx_buf_t));
|
46
|
479
|
58
|
480 } else {
|
|
481 b = ngx_calloc_buf(r->pool);
|
|
482 if (b == NULL) {
|
|
483 return NGX_ERROR;
|
|
484 }
|
|
485
|
|
486 cl = ngx_alloc_chain_link(r->pool);
|
|
487 if (cl == NULL) {
|
|
488 return NGX_ERROR;
|
|
489 }
|
|
490
|
|
491 cl->buf = b;
|
|
492 }
|
|
493
|
|
494 b->memory = 1;
|
|
495 b->pos = ngx_http_ssi_string;
|
|
496 b->last = ngx_http_ssi_string + ctx->saved;
|
|
497
|
|
498 *ctx->last_out = cl;
|
|
499 ctx->last_out = &cl->next;
|
|
500
|
|
501 ctx->saved = 0;
|
|
502 }
|
48
|
503
|
|
504 if (ctx->free) {
|
|
505 cl = ctx->free;
|
|
506 ctx->free = ctx->free->next;
|
|
507 b = cl->buf;
|
|
508
|
|
509 } else {
|
58
|
510 b = ngx_alloc_buf(r->pool);
|
48
|
511 if (b == NULL) {
|
|
512 return NGX_ERROR;
|
|
513 }
|
|
514
|
|
515 cl = ngx_alloc_chain_link(r->pool);
|
|
516 if (cl == NULL) {
|
|
517 return NGX_ERROR;
|
|
518 }
|
|
519
|
|
520 cl->buf = b;
|
46
|
521 }
|
|
522
|
58
|
523 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
|
|
524
|
62
|
525 b->pos = ctx->copy_start;
|
|
526 b->last = ctx->copy_end;
|
|
527 b->shadow = NULL;
|
58
|
528 b->last_buf = 0;
|
|
529 b->recycled = 0;
|
46
|
530
|
58
|
531 if (b->in_file) {
|
|
532 if (conf->min_file_chunk < (size_t) (b->last - b->pos))
|
|
533 {
|
|
534 b->file_last = b->file_pos + (b->last - b->start);
|
|
535 b->file_pos += b->pos - b->start;
|
|
536
|
|
537 } else {
|
|
538 b->in_file = 0;
|
|
539 }
|
|
540 }
|
|
541
|
|
542 cl->next = NULL;
|
46
|
543 *ctx->last_out = cl;
|
|
544 ctx->last_out = &cl->next;
|
|
545
|
58
|
546 } else {
|
46
|
547 ctx->saved = 0;
|
|
548 }
|
|
549 }
|
|
550
|
|
551 if (ctx->state == ssi_start_state) {
|
|
552 ctx->copy_start = ctx->pos;
|
|
553 ctx->copy_end = ctx->pos;
|
|
554
|
|
555 } else {
|
|
556 ctx->copy_start = NULL;
|
|
557 ctx->copy_end = NULL;
|
|
558 }
|
|
559
|
|
560 if (rc == NGX_AGAIN) {
|
|
561 continue;
|
|
562 }
|
|
563
|
|
564
|
|
565 if (rc == NGX_OK) {
|
|
566
|
|
567 for (cmd = ngx_http_ssi_commands; cmd->handler; cmd++) {
|
|
568 if (cmd->name.len == 0) {
|
|
569 cmd = (ngx_http_ssi_command_t *) cmd->handler;
|
|
570 }
|
|
571
|
|
572 if (cmd->name.len != ctx->command.len
|
|
573 || ngx_strncmp(cmd->name.data, ctx->command.data,
|
|
574 ctx->command.len) != 0)
|
|
575 {
|
|
576 continue;
|
|
577 }
|
|
578
|
|
579 break;
|
|
580 }
|
|
581
|
58
|
582 if (cmd->name.len == 0 && ctx->output) {
|
46
|
583 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
584 "invalid SSI command: \"%V\"", &ctx->command);
|
|
585 goto ssi_error;
|
|
586 }
|
|
587
|
58
|
588 if (!ctx->output && !cmd->conditional) {
|
|
589 continue;
|
|
590 }
|
|
591
|
46
|
592 ngx_memzero(params,
|
|
593 NGX_HTTP_SSI_MAX_PARAMS * sizeof(ngx_str_t *));
|
|
594
|
|
595 param = ctx->params.elts;
|
|
596
|
|
597
|
|
598 for (i = 0; i < ctx->params.nelts; i++) {
|
|
599
|
|
600 for (prm = cmd->params; prm->name.len; prm++) {
|
|
601
|
|
602 if (param[i].key.len != prm->name.len
|
|
603 || ngx_strncmp(param[i].key.data, prm->name.data,
|
|
604 prm->name.len) != 0)
|
|
605 {
|
|
606 continue;
|
|
607 }
|
|
608
|
|
609 if (params[prm->index]) {
|
|
610 ngx_log_error(NGX_LOG_ERR,
|
|
611 r->connection->log, 0,
|
|
612 "duplicate \"%V\" parameter "
|
|
613 "in \"%V\" SSI command",
|
|
614 ¶m[i].key, &ctx->command);
|
|
615
|
|
616 goto ssi_error;
|
|
617 }
|
|
618
|
|
619 params[prm->index] = ¶m[i].value;
|
|
620
|
|
621 break;
|
|
622 }
|
|
623
|
|
624 if (prm->name.len == 0) {
|
|
625 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
626 "invalid parameter name: \"%V\" "
|
|
627 "in \"%V\" SSI command",
|
|
628 ¶m[i].key, &ctx->command);
|
|
629
|
|
630 goto ssi_error;
|
|
631 }
|
|
632 }
|
|
633
|
|
634 for (prm = cmd->params; prm->name.len; prm++) {
|
|
635 if (prm->mandatory && params[prm->index] == 0) {
|
|
636 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
637 "mandatory \"%V\" parameter is absent "
|
|
638 "in \"%V\" SSI command",
|
|
639 &prm->name, &ctx->command);
|
|
640
|
|
641 goto ssi_error;
|
|
642 }
|
|
643 }
|
|
644
|
58
|
645 if (cmd->flush && ctx->out) {
|
|
646 rc = ngx_http_ssi_output(r, ctx);
|
|
647
|
|
648 if (rc == NGX_ERROR) {
|
|
649 return NGX_ERROR;
|
|
650 }
|
|
651 }
|
|
652
|
118
|
653 c = r->connection;
|
|
654
|
|
655 rc = cmd->handler(r, ctx, params);
|
|
656
|
|
657 if (c->closed) {
|
|
658 return NGX_DONE;
|
|
659 }
|
|
660
|
|
661 if (rc == NGX_OK) {
|
46
|
662 continue;
|
|
663 }
|
|
664 }
|
|
665
|
|
666
|
|
667 /* rc == NGX_HTTP_SSI_ERROR */
|
|
668
|
48
|
669 ssi_error:
|
46
|
670
|
|
671 if (conf->silent_errors) {
|
|
672 continue;
|
|
673 }
|
|
674
|
48
|
675 if (ctx->free) {
|
|
676 cl = ctx->free;
|
|
677 ctx->free = ctx->free->next;
|
|
678 b = cl->buf;
|
|
679 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
680
|
|
681 } else {
|
|
682 b = ngx_calloc_buf(r->pool);
|
|
683 if (b == NULL) {
|
|
684 return NGX_ERROR;
|
|
685 }
|
|
686
|
|
687 cl = ngx_alloc_chain_link(r->pool);
|
|
688 if (cl == NULL) {
|
|
689 return NGX_ERROR;
|
|
690 }
|
|
691
|
|
692 cl->buf = b;
|
46
|
693 }
|
|
694
|
|
695 b->memory = 1;
|
96
|
696 b->pos = ctx->errmsg.data;
|
|
697 b->last = ctx->errmsg.data + ctx->errmsg.len;
|
46
|
698
|
|
699 cl->next = NULL;
|
|
700 *ctx->last_out = cl;
|
|
701 ctx->last_out = &cl->next;
|
|
702
|
|
703 continue;
|
|
704 }
|
|
705
|
58
|
706 if (ctx->buf->last_buf || ctx->buf->recycled) {
|
|
707
|
48
|
708 if (b == NULL) {
|
|
709 if (ctx->free) {
|
|
710 cl = ctx->free;
|
|
711 ctx->free = ctx->free->next;
|
|
712 b = cl->buf;
|
|
713 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
714
|
|
715 } else {
|
|
716 b = ngx_calloc_buf(r->pool);
|
|
717 if (b == NULL) {
|
|
718 return NGX_ERROR;
|
|
719 }
|
46
|
720
|
48
|
721 cl = ngx_alloc_chain_link(r->pool);
|
|
722 if (cl == NULL) {
|
|
723 return NGX_ERROR;
|
|
724 }
|
|
725
|
|
726 cl->buf = b;
|
|
727 }
|
|
728
|
58
|
729 b->sync = 1;
|
|
730
|
48
|
731 cl->next = NULL;
|
|
732 *ctx->last_out = cl;
|
|
733 ctx->last_out = &cl->next;
|
|
734 }
|
|
735
|
|
736 b->last_buf = ctx->buf->last_buf;
|
|
737 b->shadow = ctx->buf;
|
58
|
738
|
|
739 if (conf->ignore_recycled_buffers == 0) {
|
|
740 b->recycled = ctx->buf->recycled;
|
|
741 }
|
46
|
742 }
|
|
743
|
48
|
744 ctx->buf = NULL;
|
|
745
|
46
|
746 ctx->saved = ctx->looked;
|
|
747 }
|
|
748
|
48
|
749 if (ctx->out == NULL && ctx->busy == NULL) {
|
46
|
750 return NGX_OK;
|
|
751 }
|
|
752
|
58
|
753 return ngx_http_ssi_output(r, ctx);
|
|
754 }
|
|
755
|
|
756
|
|
757 static ngx_int_t
|
|
758 ngx_http_ssi_output(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
759 {
|
|
760 ngx_int_t rc;
|
|
761 ngx_buf_t *b;
|
|
762 ngx_chain_t *cl;
|
|
763
|
46
|
764 rc = ngx_http_next_body_filter(r, ctx->out);
|
|
765
|
48
|
766 if (ctx->busy == NULL) {
|
|
767 ctx->busy = ctx->out;
|
|
768
|
|
769 } else {
|
|
770 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
|
|
771 cl->next = ctx->out;
|
|
772 }
|
|
773
|
46
|
774 ctx->out = NULL;
|
|
775 ctx->last_out = &ctx->out;
|
|
776
|
48
|
777 while (ctx->busy) {
|
|
778
|
|
779 b = ctx->busy->buf;
|
|
780
|
|
781 if (ngx_buf_size(b) != 0) {
|
|
782 break;
|
|
783 }
|
|
784
|
|
785 #if (NGX_HAVE_WRITE_ZEROCOPY)
|
|
786 if (b->zerocopy_busy) {
|
|
787 break;
|
|
788 }
|
|
789 #endif
|
|
790
|
|
791 if (b->shadow) {
|
|
792 b->shadow->pos = b->shadow->last;
|
|
793 }
|
|
794
|
|
795 cl = ctx->busy;
|
|
796 ctx->busy = cl->next;
|
|
797 cl->next = ctx->free;
|
|
798 ctx->free = cl;
|
|
799 }
|
|
800
|
46
|
801 return rc;
|
|
802 }
|
|
803
|
|
804
|
|
805 static ngx_int_t
|
|
806 ngx_http_ssi_parse(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
807 {
|
|
808 u_char *p, *last, *copy_end, ch;
|
|
809 size_t looked;
|
|
810 ngx_http_ssi_state_e state;
|
|
811
|
|
812 state = ctx->state;
|
|
813 looked = ctx->looked;
|
|
814 last = ctx->buf->last;
|
|
815 copy_end = ctx->copy_end;
|
|
816
|
|
817 for (p = ctx->pos; p < last; p++) {
|
|
818
|
|
819 ch = *p;
|
|
820
|
|
821 if (state == ssi_start_state) {
|
|
822
|
|
823 /* the tight loop */
|
|
824
|
48
|
825 for ( ;; ) {
|
|
826 if (ch == '<') {
|
|
827 copy_end = p;
|
|
828 looked = 1;
|
|
829 state = ssi_tag_state;
|
|
830
|
|
831 goto tag_started;
|
46
|
832 }
|
|
833
|
48
|
834 if (++p == last) {
|
|
835 break;
|
|
836 }
|
46
|
837
|
48
|
838 ch = *p;
|
46
|
839 }
|
|
840
|
|
841 ctx->pos = p;
|
|
842 ctx->looked = looked;
|
|
843 ctx->copy_end = p;
|
|
844
|
|
845 if (ctx->copy_start == NULL) {
|
|
846 ctx->copy_start = ctx->buf->pos;
|
|
847 }
|
|
848
|
|
849 return NGX_AGAIN;
|
|
850
|
48
|
851 tag_started:
|
|
852
|
46
|
853 continue;
|
|
854 }
|
|
855
|
|
856 switch (state) {
|
|
857
|
|
858 case ssi_start_state:
|
|
859 break;
|
|
860
|
|
861 case ssi_tag_state:
|
|
862 switch (ch) {
|
|
863 case '!':
|
|
864 looked = 2;
|
|
865 state = ssi_comment0_state;
|
|
866 break;
|
|
867
|
|
868 case '<':
|
|
869 copy_end = p;
|
|
870 break;
|
|
871
|
|
872 default:
|
|
873 copy_end = p;
|
|
874 looked = 0;
|
|
875 state = ssi_start_state;
|
|
876 break;
|
|
877 }
|
|
878
|
|
879 break;
|
|
880
|
|
881 case ssi_comment0_state:
|
|
882 switch (ch) {
|
|
883 case '-':
|
|
884 looked = 3;
|
|
885 state = ssi_comment1_state;
|
|
886 break;
|
|
887
|
|
888 case '<':
|
|
889 copy_end = p;
|
|
890 looked = 1;
|
|
891 state = ssi_tag_state;
|
|
892 break;
|
|
893
|
|
894 default:
|
|
895 copy_end = p;
|
|
896 looked = 0;
|
|
897 state = ssi_start_state;
|
|
898 break;
|
|
899 }
|
|
900
|
|
901 break;
|
|
902
|
|
903 case ssi_comment1_state:
|
|
904 switch (ch) {
|
|
905 case '-':
|
|
906 looked = 4;
|
|
907 state = ssi_sharp_state;
|
|
908 break;
|
|
909
|
|
910 case '<':
|
|
911 copy_end = p;
|
|
912 looked = 1;
|
|
913 state = ssi_tag_state;
|
|
914 break;
|
|
915
|
|
916 default:
|
|
917 copy_end = p;
|
|
918 looked = 0;
|
|
919 state = ssi_start_state;
|
|
920 break;
|
|
921 }
|
|
922
|
|
923 break;
|
|
924
|
|
925 case ssi_sharp_state:
|
|
926 switch (ch) {
|
|
927 case '#':
|
|
928 if (ctx->copy_start) {
|
|
929 ctx->saved = 0;
|
|
930 }
|
|
931 looked = 0;
|
|
932 state = ssi_precommand_state;
|
|
933 break;
|
|
934
|
|
935 case '<':
|
|
936 copy_end = p;
|
|
937 looked = 1;
|
|
938 state = ssi_tag_state;
|
|
939 break;
|
|
940
|
|
941 default:
|
|
942 copy_end = p;
|
|
943 looked = 0;
|
|
944 state = ssi_start_state;
|
|
945 break;
|
|
946 }
|
|
947
|
|
948 break;
|
|
949
|
|
950 case ssi_precommand_state:
|
|
951 switch (ch) {
|
|
952 case ' ':
|
|
953 case CR:
|
|
954 case LF:
|
|
955 case '\t':
|
|
956 break;
|
|
957
|
|
958 default:
|
|
959 ctx->command.len = 1;
|
|
960 ctx->command.data = ngx_palloc(r->pool,
|
|
961 NGX_HTTP_SSI_COMMAND_LEN + 1);
|
|
962 if (ctx->command.data == NULL) {
|
|
963 return NGX_ERROR;
|
|
964 }
|
|
965
|
|
966 ctx->command.data[0] = ch;
|
|
967 ctx->params.nelts = 0;
|
|
968 state = ssi_command_state;
|
|
969 break;
|
|
970 }
|
|
971
|
|
972 break;
|
|
973
|
|
974 case ssi_command_state:
|
|
975 switch (ch) {
|
|
976 case ' ':
|
|
977 case CR:
|
|
978 case LF:
|
|
979 case '\t':
|
|
980 state = ssi_preparam_state;
|
|
981 break;
|
|
982
|
|
983 case '-':
|
|
984 state = ssi_comment_end0_state;
|
|
985 break;
|
|
986
|
|
987 default:
|
|
988 ctx->command.data[ctx->command.len++] = ch;
|
|
989
|
|
990 if (ctx->command.len == NGX_HTTP_SSI_COMMAND_LEN) {
|
|
991 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
992 "the \"%V\" SSI command is too long",
|
|
993 &ctx->command);
|
|
994
|
|
995 state = ssi_error_state;
|
|
996 break;
|
|
997 }
|
|
998 }
|
|
999
|
|
1000 break;
|
|
1001
|
|
1002 case ssi_preparam_state:
|
|
1003 switch (ch) {
|
|
1004 case ' ':
|
|
1005 case CR:
|
|
1006 case LF:
|
|
1007 case '\t':
|
|
1008 break;
|
|
1009
|
|
1010 case '-':
|
|
1011 state = ssi_comment_end0_state;
|
|
1012 break;
|
|
1013
|
|
1014 default:
|
48
|
1015 ctx->param = ngx_array_push(&ctx->params);
|
|
1016 if (ctx->param == NULL) {
|
46
|
1017 return NGX_ERROR;
|
|
1018 }
|
|
1019
|
|
1020 ctx->param->key.len = 1;
|
|
1021 ctx->param->key.data = ngx_palloc(r->pool,
|
|
1022 NGX_HTTP_SSI_PARAM_LEN + 1);
|
|
1023 if (ctx->param->key.data == NULL) {
|
|
1024 return NGX_ERROR;
|
|
1025 }
|
|
1026
|
|
1027 ctx->param->key.data[0] = ch;
|
|
1028
|
|
1029 ctx->param->value.len = 0;
|
|
1030 ctx->param->value.data = ngx_palloc(r->pool,
|
|
1031 ctx->value_len + 1);
|
|
1032 if (ctx->param->value.data == NULL) {
|
|
1033 return NGX_ERROR;
|
|
1034 }
|
|
1035
|
|
1036 state = ssi_param_state;
|
|
1037 break;
|
|
1038 }
|
|
1039
|
|
1040 break;
|
|
1041
|
|
1042 case ssi_param_state:
|
|
1043 switch (ch) {
|
|
1044 case ' ':
|
|
1045 case CR:
|
|
1046 case LF:
|
|
1047 case '\t':
|
|
1048 state = ssi_preequal_state;
|
|
1049 break;
|
|
1050
|
|
1051 case '=':
|
|
1052 state = ssi_prevalue_state;
|
|
1053 break;
|
|
1054
|
|
1055 case '-':
|
|
1056 state = ssi_error_end0_state;
|
|
1057
|
|
1058 ctx->param->key.data[ctx->param->key.len++] = ch;
|
|
1059 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1060 "invalid \"%V\" parameter in \"%V\" SSI command",
|
|
1061 &ctx->param->key, &ctx->command);
|
|
1062 break;
|
|
1063
|
|
1064 default:
|
|
1065 ctx->param->key.data[ctx->param->key.len++] = ch;
|
|
1066
|
|
1067 if (ctx->param->key.len == NGX_HTTP_SSI_PARAM_LEN) {
|
|
1068 state = ssi_error_state;
|
|
1069 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1070 "too long \"%V\" parameter in "
|
|
1071 "\"%V\" SSI command",
|
|
1072 &ctx->param->key, &ctx->command);
|
|
1073 break;
|
|
1074 }
|
|
1075 }
|
|
1076
|
|
1077 break;
|
|
1078
|
|
1079 case ssi_preequal_state:
|
|
1080 switch (ch) {
|
|
1081 case ' ':
|
|
1082 case CR:
|
|
1083 case LF:
|
|
1084 case '\t':
|
|
1085 break;
|
|
1086
|
|
1087 case '=':
|
|
1088 state = ssi_prevalue_state;
|
|
1089 break;
|
|
1090
|
|
1091 default:
|
|
1092 if (ch == '-') {
|
|
1093 state = ssi_error_end0_state;
|
|
1094 } else {
|
|
1095 state = ssi_error_state;
|
|
1096 }
|
|
1097
|
|
1098 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1099 "unexpected \"%c\" symbol after \"%V\" "
|
|
1100 "parameter in \"%V\" SSI command",
|
|
1101 ch, &ctx->param->key, &ctx->command);
|
|
1102 break;
|
|
1103 }
|
|
1104
|
|
1105 break;
|
|
1106
|
|
1107 case ssi_prevalue_state:
|
|
1108 switch (ch) {
|
|
1109 case ' ':
|
|
1110 case CR:
|
|
1111 case LF:
|
|
1112 case '\t':
|
|
1113 break;
|
|
1114
|
|
1115 case '"':
|
|
1116 state = ssi_double_quoted_value_state;
|
|
1117 break;
|
|
1118
|
|
1119 case '\'':
|
|
1120 state = ssi_quoted_value_state;
|
|
1121 break;
|
|
1122
|
|
1123 default:
|
|
1124 if (ch == '-') {
|
|
1125 state = ssi_error_end0_state;
|
|
1126 } else {
|
|
1127 state = ssi_error_state;
|
|
1128 }
|
|
1129
|
|
1130 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1131 "unexpected \"%c\" symbol before value of "
|
|
1132 "\"%V\" parameter in \"%V\" SSI command",
|
|
1133 ch, &ctx->param->key, &ctx->command);
|
|
1134 break;
|
|
1135 }
|
|
1136
|
|
1137 break;
|
|
1138
|
|
1139 case ssi_double_quoted_value_state:
|
|
1140 switch (ch) {
|
108
|
1141 case '"':
|
|
1142 state = ssi_postparam_state;
|
|
1143 break;
|
|
1144
|
46
|
1145 case '\\':
|
|
1146 ctx->saved_state = ssi_double_quoted_value_state;
|
|
1147 state = ssi_quoted_symbol_state;
|
108
|
1148
|
|
1149 /* fall through */
|
46
|
1150
|
|
1151 default:
|
|
1152 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1153
|
|
1154 if (ctx->param->value.len == ctx->value_len) {
|
|
1155 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1156 "too long \"%V\" value of \"%V\" parameter "
|
|
1157 "in \"%V\" SSI command",
|
|
1158 &ctx->param->value, &ctx->param->key,
|
|
1159 &ctx->command);
|
|
1160 state = ssi_error_state;
|
|
1161 break;
|
|
1162 }
|
|
1163 }
|
|
1164
|
|
1165 break;
|
|
1166
|
|
1167 case ssi_quoted_value_state:
|
|
1168 switch (ch) {
|
108
|
1169 case '\'':
|
|
1170 state = ssi_postparam_state;
|
|
1171 break;
|
|
1172
|
46
|
1173 case '\\':
|
|
1174 ctx->saved_state = ssi_quoted_value_state;
|
|
1175 state = ssi_quoted_symbol_state;
|
108
|
1176
|
|
1177 /* fall through */
|
46
|
1178
|
|
1179 default:
|
|
1180 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1181
|
|
1182 if (ctx->param->value.len == ctx->value_len) {
|
|
1183 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1184 "too long \"%V\" value of \"%V\" parameter "
|
|
1185 "in \"%V\" SSI command",
|
|
1186 &ctx->param->value, &ctx->param->key,
|
|
1187 &ctx->command);
|
|
1188 state = ssi_error_state;
|
|
1189 break;
|
|
1190 }
|
|
1191 }
|
|
1192
|
|
1193 break;
|
|
1194
|
|
1195 case ssi_quoted_symbol_state:
|
108
|
1196 state = ctx->saved_state;
|
|
1197
|
|
1198 if (ch == '\\') {
|
|
1199 break;
|
|
1200 }
|
|
1201
|
|
1202 if (ch == '"' && state == ssi_double_quoted_value_state) {
|
|
1203 break;
|
|
1204 }
|
|
1205
|
|
1206 if (ch == '\'' && state == ssi_quoted_value_state) {
|
|
1207 break;
|
|
1208 }
|
|
1209
|
46
|
1210 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1211
|
|
1212 if (ctx->param->value.len == ctx->value_len) {
|
|
1213 if (ctx->param->value.len == ctx->value_len) {
|
|
1214 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1215 "too long \"%V\" value of \"%V\" parameter "
|
|
1216 "in \"%V\" SSI command",
|
|
1217 &ctx->param->value, &ctx->param->key,
|
|
1218 &ctx->command);
|
|
1219 state = ssi_error_state;
|
|
1220 break;
|
|
1221 }
|
|
1222 }
|
|
1223
|
|
1224 break;
|
|
1225
|
|
1226 case ssi_postparam_state:
|
|
1227 switch (ch) {
|
|
1228 case ' ':
|
|
1229 case CR:
|
|
1230 case LF:
|
|
1231 case '\t':
|
|
1232 state = ssi_preparam_state;
|
|
1233 break;
|
|
1234
|
|
1235 case '-':
|
|
1236 state = ssi_comment_end0_state;
|
|
1237 break;
|
|
1238
|
|
1239 default:
|
|
1240 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1241 "unexpected \"%c\" symbol after \"%V\" value "
|
|
1242 "of \"%V\" parameter in \"%V\" SSI command",
|
|
1243 ch, &ctx->param->value, &ctx->param->key,
|
|
1244 &ctx->command);
|
|
1245 state = ssi_error_state;
|
|
1246 break;
|
|
1247 }
|
|
1248
|
|
1249 break;
|
|
1250
|
|
1251 case ssi_comment_end0_state:
|
|
1252 switch (ch) {
|
|
1253 case '-':
|
|
1254 state = ssi_comment_end1_state;
|
|
1255 break;
|
|
1256
|
|
1257 default:
|
|
1258 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1259 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1260 ch, &ctx->command);
|
|
1261 state = ssi_error_state;
|
|
1262 break;
|
|
1263 }
|
|
1264
|
|
1265 break;
|
|
1266
|
|
1267 case ssi_comment_end1_state:
|
|
1268 switch (ch) {
|
|
1269 case '>':
|
|
1270 ctx->state = ssi_start_state;
|
|
1271 ctx->pos = p + 1;
|
|
1272 ctx->looked = looked;
|
|
1273 ctx->copy_end = copy_end;
|
|
1274
|
|
1275 if (ctx->copy_start == NULL && copy_end) {
|
|
1276 ctx->copy_start = ctx->buf->pos;
|
|
1277 }
|
|
1278
|
|
1279 return NGX_OK;
|
|
1280
|
|
1281 default:
|
|
1282 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1283 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1284 ch, &ctx->command);
|
|
1285 state = ssi_error_state;
|
|
1286 break;
|
|
1287 }
|
|
1288
|
|
1289 break;
|
|
1290
|
|
1291 case ssi_error_state:
|
|
1292 switch (ch) {
|
|
1293 case '-':
|
|
1294 state = ssi_error_end0_state;
|
|
1295 break;
|
|
1296
|
|
1297 default:
|
|
1298 break;
|
|
1299 }
|
|
1300
|
|
1301 break;
|
|
1302
|
|
1303 case ssi_error_end0_state:
|
|
1304 switch (ch) {
|
|
1305 case '-':
|
|
1306 state = ssi_error_end1_state;
|
|
1307 break;
|
|
1308
|
|
1309 default:
|
|
1310 state = ssi_error_state;
|
|
1311 break;
|
|
1312 }
|
|
1313
|
|
1314 break;
|
|
1315
|
|
1316 case ssi_error_end1_state:
|
|
1317 switch (ch) {
|
|
1318 case '>':
|
|
1319 ctx->state = ssi_start_state;
|
|
1320 ctx->pos = p + 1;
|
|
1321 ctx->looked = looked;
|
|
1322 ctx->copy_end = copy_end;
|
|
1323
|
|
1324 if (ctx->copy_start == NULL && copy_end) {
|
|
1325 ctx->copy_start = ctx->buf->pos;
|
|
1326 }
|
|
1327
|
|
1328 return NGX_HTTP_SSI_ERROR;
|
|
1329
|
|
1330 default:
|
|
1331 state = ssi_error_state;
|
|
1332 break;
|
|
1333 }
|
|
1334
|
|
1335 break;
|
|
1336 }
|
|
1337 }
|
|
1338
|
|
1339 ctx->state = state;
|
|
1340 ctx->pos = p;
|
|
1341 ctx->looked = looked;
|
|
1342
|
|
1343 ctx->copy_end = (state == ssi_start_state) ? p : copy_end;
|
|
1344
|
|
1345 if (ctx->copy_start == NULL && ctx->copy_end) {
|
|
1346 ctx->copy_start = ctx->buf->pos;
|
|
1347 }
|
|
1348
|
|
1349 return NGX_AGAIN;
|
|
1350 }
|
|
1351
|
|
1352
|
102
|
1353 static ngx_str_t *
|
|
1354 ngx_http_ssi_get_variable(ngx_http_request_t *r, ngx_str_t *name)
|
|
1355 {
|
|
1356 ngx_uint_t i;
|
|
1357 ngx_http_ssi_var_t *var;
|
|
1358 ngx_http_ssi_ctx_t *ctx;
|
|
1359
|
|
1360 ctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
1361
|
|
1362 var = ctx->variables.elts;
|
|
1363 for (i = 0; i < ctx->variables.nelts; i++) {
|
|
1364 if (name->len != var[i].name.len) {
|
|
1365 continue;
|
|
1366 }
|
|
1367
|
|
1368 if (ngx_strncasecmp(name->data, var[i].name.data, name->len) == 0) {
|
|
1369 return &var[i].value;
|
|
1370 }
|
|
1371 }
|
|
1372
|
|
1373 return NULL;
|
|
1374 }
|
|
1375
|
|
1376
|
|
1377 static ngx_int_t
|
|
1378 ngx_http_ssi_evaluate_string(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1379 ngx_str_t *text, ngx_uint_t flags)
|
|
1380 {
|
|
1381 u_char ch, *p, **value, *data;
|
|
1382 size_t *size, len, prefix;
|
|
1383 ngx_str_t var, part, *val;
|
|
1384 ngx_uint_t i, j, n, bracket;
|
|
1385 ngx_array_t lengths, values;
|
|
1386 ngx_http_variable_value_t *vv;
|
|
1387
|
|
1388 n = ngx_http_script_variables_count(text);
|
|
1389
|
|
1390 if (n == 0) {
|
|
1391
|
|
1392 if (!(flags & NGX_HTTP_SSI_ADD_PREFIX)) {
|
|
1393 return NGX_OK;
|
|
1394 }
|
|
1395
|
|
1396 if (text->data[0] != '/') {
|
|
1397 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1398 if (r->uri.data[prefix - 1] == '/') {
|
|
1399 break;
|
|
1400 }
|
|
1401 }
|
|
1402
|
|
1403 if (prefix) {
|
|
1404 len = prefix + text->len;
|
|
1405
|
|
1406 data = ngx_palloc(r->pool, len);
|
|
1407 if (data == NULL) {
|
|
1408 return NGX_ERROR;
|
|
1409 }
|
|
1410
|
|
1411 p = ngx_cpymem(data, r->uri.data, prefix);
|
|
1412 ngx_memcpy(p, text->data, text->len);
|
|
1413
|
|
1414 text->len = len;
|
|
1415 text->data = data;
|
|
1416 }
|
|
1417 }
|
|
1418
|
|
1419 return NGX_OK;
|
|
1420 }
|
|
1421
|
|
1422 if (ngx_array_init(&lengths, r->pool, 8, sizeof(size_t *)) != NGX_OK) {
|
|
1423 return NGX_ERROR;
|
|
1424 }
|
|
1425
|
|
1426 if (ngx_array_init(&values, r->pool, 8, sizeof(u_char *)) != NGX_OK) {
|
|
1427 return NGX_ERROR;
|
|
1428 }
|
|
1429
|
|
1430 len = 0;
|
|
1431 i = 0;
|
|
1432
|
|
1433 while (i < text->len) {
|
|
1434
|
|
1435 if (text->data[i] == '$') {
|
|
1436
|
|
1437 var.len = 0;
|
|
1438
|
|
1439 if (++i == text->len) {
|
|
1440 goto invalid_variable;
|
|
1441 }
|
|
1442
|
|
1443 if (text->data[i] == '{') {
|
|
1444 bracket = 1;
|
|
1445
|
|
1446 if (++i == text->len) {
|
|
1447 goto invalid_variable;
|
|
1448 }
|
|
1449
|
|
1450 var.data = &text->data[i];
|
|
1451
|
|
1452 } else {
|
|
1453 bracket = 0;
|
|
1454 var.data = &text->data[i];
|
|
1455 }
|
|
1456
|
|
1457 for ( /* void */ ; i < text->len; i++, var.len++) {
|
|
1458 ch = text->data[i];
|
|
1459
|
|
1460 if (ch == '}' && bracket) {
|
|
1461 i++;
|
|
1462 bracket = 0;
|
|
1463 break;
|
|
1464 }
|
|
1465
|
|
1466 if ((ch >= 'A' && ch <= 'Z')
|
|
1467 || (ch >= 'a' && ch <= 'z')
|
|
1468 || (ch >= '0' && ch <= '9')
|
|
1469 || ch == '_')
|
|
1470 {
|
|
1471 continue;
|
|
1472 }
|
|
1473
|
|
1474 break;
|
|
1475 }
|
|
1476
|
|
1477 if (bracket) {
|
|
1478 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1479 "the closing bracket in \"%V\" "
|
|
1480 "variable is missing", &var);
|
|
1481 return NGX_ERROR;
|
|
1482 }
|
|
1483
|
|
1484 if (var.len == 0) {
|
|
1485 goto invalid_variable;
|
|
1486 }
|
|
1487
|
|
1488 for (j = 0; j < var.len; j++) {
|
|
1489 var.data[j] = ngx_tolower(var.data[j]);
|
|
1490 }
|
|
1491
|
|
1492 val = ngx_http_ssi_get_variable(r, &var);
|
|
1493
|
|
1494 if (val == NULL) {
|
|
1495 vv = ngx_http_get_variable(r, &var);
|
|
1496
|
|
1497 if (vv == NULL) {
|
|
1498 return NGX_ERROR;
|
|
1499 }
|
|
1500
|
|
1501 if (vv == NGX_HTTP_VAR_NOT_FOUND) {
|
|
1502 continue;
|
|
1503 }
|
|
1504
|
|
1505 part = vv->text;
|
|
1506
|
|
1507 } else {
|
|
1508 part = *val;
|
|
1509 }
|
|
1510
|
|
1511 } else {
|
|
1512 part.data = &text->data[i];
|
|
1513
|
108
|
1514 for (p = part.data; i < text->len; i++) {
|
|
1515 ch = text->data[i];
|
|
1516
|
|
1517 if (ch == '$') {
|
|
1518 if (text->data[i - 1] != '\\') {
|
|
1519 break;
|
|
1520 }
|
|
1521
|
|
1522 *(p - 1) = ch;
|
|
1523
|
|
1524 continue;
|
|
1525 }
|
|
1526
|
|
1527 *p++ = ch;
|
102
|
1528 }
|
108
|
1529
|
|
1530 part.len = p - part.data;
|
102
|
1531 }
|
|
1532
|
|
1533 len += part.len;
|
|
1534
|
|
1535 size = ngx_array_push(&lengths);
|
|
1536 if (size == NULL) {
|
|
1537 return NGX_ERROR;
|
|
1538 }
|
|
1539
|
|
1540 *size = part.len;
|
|
1541
|
|
1542 value = ngx_array_push(&values);
|
|
1543 if (value == NULL) {
|
|
1544 return NGX_ERROR;
|
|
1545 }
|
|
1546
|
|
1547 *value = part.data;
|
|
1548 }
|
|
1549
|
|
1550 prefix = 0;
|
|
1551
|
|
1552 size = lengths.elts;
|
|
1553 value = values.elts;
|
|
1554
|
|
1555 if (flags & NGX_HTTP_SSI_ADD_PREFIX) {
|
|
1556 for (i = 0; i < values.nelts; i++) {
|
|
1557 if (size[i] != 0) {
|
|
1558 if (*value[i] != '/') {
|
|
1559 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1560 if (r->uri.data[prefix - 1] == '/') {
|
|
1561 len += prefix;
|
|
1562 break;
|
|
1563 }
|
|
1564 }
|
|
1565 }
|
|
1566
|
|
1567 break;
|
|
1568 }
|
|
1569 }
|
|
1570 }
|
|
1571
|
|
1572 p = ngx_palloc(r->pool, len);
|
|
1573 if (p == NULL) {
|
|
1574 return NGX_ERROR;
|
|
1575 }
|
|
1576
|
|
1577 text->len = len;
|
|
1578 text->data = p;
|
|
1579
|
|
1580 if (prefix) {
|
|
1581 p = ngx_cpymem(p, r->uri.data, prefix);
|
|
1582 }
|
|
1583
|
|
1584 for (i = 0; i < values.nelts; i++) {
|
|
1585 p = ngx_cpymem(p, value[i], size[i]);
|
|
1586 }
|
|
1587
|
|
1588 return NGX_OK;
|
|
1589
|
|
1590 invalid_variable:
|
|
1591
|
|
1592 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1593 "invalid variable name in \"%V\"", text);
|
|
1594
|
|
1595 return NGX_ERROR;
|
|
1596 }
|
|
1597
|
|
1598
|
|
1599 static ngx_int_t
|
|
1600 ngx_http_ssi_include(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1601 ngx_str_t **params)
|
|
1602 {
|
|
1603 ngx_str_t *uri, *file, args;
|
|
1604 ngx_uint_t i;
|
|
1605
|
|
1606 uri = params[NGX_HTTP_SSI_INCLUDE_VIRTUAL];
|
|
1607 file = params[NGX_HTTP_SSI_INCLUDE_FILE];
|
|
1608
|
|
1609 if (uri && file) {
|
|
1610 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1611 "inlcusion may be either virtual=\"%V\" or file=\"%V\"",
|
|
1612 uri, file);
|
|
1613 return NGX_HTTP_SSI_ERROR;
|
|
1614 }
|
|
1615
|
|
1616 if (uri == NULL && file == NULL) {
|
|
1617 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1618 "no parameter in \"include\" SSI command");
|
|
1619 return NGX_HTTP_SSI_ERROR;
|
|
1620 }
|
|
1621
|
|
1622 if (uri == NULL) {
|
|
1623 uri = file;
|
|
1624 }
|
|
1625
|
|
1626 if (ngx_http_ssi_evaluate_string(r, ctx, uri, NGX_HTTP_SSI_ADD_PREFIX)
|
|
1627 != NGX_OK)
|
|
1628 {
|
|
1629 return NGX_HTTP_SSI_ERROR;
|
|
1630 }
|
|
1631
|
|
1632 args.len = 0;
|
|
1633 args.data = NULL;
|
|
1634
|
|
1635 if (params[NGX_HTTP_SSI_INCLUDE_VIRTUAL]) {
|
|
1636 for (i = 0; i < uri->len; i++) {
|
|
1637 if (uri->data[i] == '?') {
|
|
1638 args.len = uri->len - i - 1;
|
|
1639 args.data = &uri->data[i + 1];
|
|
1640 uri->len -= args.len + 1;
|
|
1641
|
|
1642 break;
|
|
1643 }
|
|
1644 }
|
|
1645 }
|
|
1646
|
|
1647 if (ngx_http_subrequest(r, uri, &args) != NGX_OK) {
|
|
1648 return NGX_HTTP_SSI_ERROR;
|
|
1649 }
|
|
1650
|
|
1651 return NGX_OK;
|
|
1652 }
|
|
1653
|
|
1654
|
46
|
1655 static ngx_int_t
|
|
1656 ngx_http_ssi_echo(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1657 ngx_str_t **params)
|
|
1658 {
|
50
|
1659 ngx_uint_t i;
|
48
|
1660 ngx_buf_t *b;
|
|
1661 ngx_str_t *var, *value;
|
|
1662 ngx_chain_t *cl;
|
50
|
1663 ngx_http_variable_value_t *vv;
|
46
|
1664
|
|
1665 var = params[NGX_HTTP_SSI_ECHO_VAR];
|
|
1666
|
102
|
1667 value = ngx_http_ssi_get_variable(r, var);
|
|
1668
|
|
1669 if (value == NULL) {
|
|
1670 for (i = 0; i < var->len; i++) {
|
|
1671 var->data[i] = ngx_tolower(var->data[i]);
|
|
1672 }
|
|
1673
|
|
1674 vv = ngx_http_get_variable(r, var);
|
|
1675
|
|
1676 if (vv == NULL) {
|
|
1677 return NGX_HTTP_SSI_ERROR;
|
|
1678 }
|
|
1679
|
|
1680 if (vv != NGX_HTTP_VAR_NOT_FOUND) {
|
|
1681 value = &vv->text;
|
|
1682 }
|
50
|
1683 }
|
46
|
1684
|
102
|
1685 if (value == NULL) {
|
48
|
1686 value = params[NGX_HTTP_SSI_ECHO_DEFAULT];
|
|
1687
|
|
1688 if (value == NULL) {
|
|
1689 value = &ngx_http_ssi_none;
|
|
1690
|
|
1691 } else if (value->len == 0) {
|
|
1692 return NGX_OK;
|
|
1693 }
|
|
1694
|
|
1695 } else {
|
|
1696 if (value->len == 0) {
|
|
1697 return NGX_OK;
|
|
1698 }
|
|
1699 }
|
|
1700
|
|
1701 b = ngx_calloc_buf(r->pool);
|
|
1702 if (b == NULL) {
|
|
1703 return NGX_HTTP_SSI_ERROR;
|
|
1704 }
|
|
1705
|
|
1706 cl = ngx_alloc_chain_link(r->pool);
|
|
1707 if (cl == NULL) {
|
46
|
1708 return NGX_HTTP_SSI_ERROR;
|
|
1709 }
|
|
1710
|
|
1711 b->memory = 1;
|
|
1712 b->pos = value->data;
|
|
1713 b->last = value->data + value->len;
|
|
1714
|
|
1715 cl->buf = b;
|
|
1716 cl->next = NULL;
|
|
1717 *ctx->last_out = cl;
|
|
1718 ctx->last_out = &cl->next;
|
|
1719
|
|
1720 return NGX_OK;
|
|
1721 }
|
|
1722
|
|
1723
|
58
|
1724 static ngx_int_t
|
|
1725 ngx_http_ssi_config(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1726 ngx_str_t **params)
|
|
1727 {
|
|
1728 ngx_str_t *value;
|
|
1729
|
|
1730 value = params[NGX_HTTP_SSI_CONFIG_TIMEFMT];
|
|
1731
|
|
1732 if (value) {
|
|
1733 ctx->timefmt = *value;
|
|
1734 }
|
|
1735
|
96
|
1736 value = params[NGX_HTTP_SSI_CONFIG_ERRMSG];
|
|
1737
|
|
1738 if (value) {
|
|
1739 ctx->errmsg = *value;
|
|
1740 }
|
|
1741
|
58
|
1742 return NGX_OK;
|
|
1743 }
|
|
1744
|
|
1745
|
|
1746 static ngx_int_t
|
102
|
1747 ngx_http_ssi_set(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
58
|
1748 ngx_str_t **params)
|
|
1749 {
|
102
|
1750 ngx_str_t *name, *value, *vv;
|
|
1751 ngx_http_ssi_var_t *var;
|
|
1752 ngx_http_ssi_ctx_t *mctx;
|
|
1753
|
|
1754 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
1755
|
|
1756 if (mctx->variables.elts == NULL) {
|
|
1757 if (ngx_array_init(&mctx->variables, r->pool, 4,
|
|
1758 sizeof(ngx_http_ssi_var_t)) != NGX_OK)
|
|
1759 {
|
58
|
1760 return NGX_HTTP_SSI_ERROR;
|
|
1761 }
|
|
1762 }
|
|
1763
|
102
|
1764 name = params[NGX_HTTP_SSI_SET_VAR];
|
|
1765 value = params[NGX_HTTP_SSI_SET_VALUE];
|
|
1766
|
|
1767 if (ngx_http_ssi_evaluate_string(r, ctx, value, 0) != NGX_OK) {
|
58
|
1768 return NGX_HTTP_SSI_ERROR;
|
|
1769 }
|
|
1770
|
102
|
1771 vv = ngx_http_ssi_get_variable(r, name);
|
|
1772
|
|
1773 if (vv) {
|
|
1774 *vv = *value;
|
|
1775 return NGX_OK;
|
|
1776 }
|
|
1777
|
|
1778 var = ngx_array_push(&mctx->variables);
|
|
1779 if (var == NULL) {
|
|
1780 return NGX_HTTP_SSI_ERROR;
|
|
1781 }
|
|
1782
|
|
1783 var->name = *name;
|
|
1784 var->value = *value;
|
|
1785
|
|
1786 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1787 "set: \"%V\"=\"%V\"", name, value);
|
|
1788
|
58
|
1789 return NGX_OK;
|
|
1790 }
|
|
1791
|
|
1792
|
|
1793 static ngx_int_t
|
|
1794 ngx_http_ssi_if(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1795 ngx_str_t **params)
|
|
1796 {
|
102
|
1797 u_char *p, *last;
|
|
1798 ngx_str_t *expr, left, right;
|
|
1799 ngx_int_t rc;
|
|
1800 ngx_uint_t negative, noregex;
|
100
|
1801 #if (NGX_PCRE)
|
102
|
1802 ngx_str_t err;
|
|
1803 ngx_regex_t *regex;
|
|
1804 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
100
|
1805 #endif
|
58
|
1806
|
|
1807 expr = params[NGX_HTTP_SSI_IF_EXPR];
|
|
1808
|
102
|
1809 left.data = expr->data;
|
100
|
1810 last = expr->data + expr->len;
|
|
1811
|
102
|
1812 for (p = left.data; p < last; p++) {
|
100
|
1813 if (*p >= 'A' && *p <= 'Z') {
|
|
1814 *p |= 0x20;
|
|
1815 continue;
|
|
1816 }
|
58
|
1817
|
102
|
1818 if ((*p >= 'a' && *p <= 'z')
|
|
1819 || (*p >= '0' && *p <= '9')
|
|
1820 || *p == '$' || *p == '{' || *p == '}' || *p == '_')
|
|
1821 {
|
100
|
1822 continue;
|
|
1823 }
|
|
1824
|
|
1825 break;
|
|
1826 }
|
|
1827
|
102
|
1828 left.len = p - left.data;
|
100
|
1829
|
|
1830 while (p < last && *p == ' ') {
|
|
1831 p++;
|
58
|
1832 }
|
|
1833
|
102
|
1834 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1835 "left: \"%V\"", &left);
|
|
1836
|
|
1837 if (ngx_http_ssi_evaluate_string(r, ctx, &left, 0) != NGX_OK) {
|
58
|
1838 return NGX_HTTP_SSI_ERROR;
|
|
1839 }
|
|
1840
|
102
|
1841 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1842 "evaluted left: \"%V\"", &left);
|
|
1843
|
100
|
1844 if (p == last) {
|
102
|
1845 if (left.len) {
|
100
|
1846 ctx->output = 1;
|
|
1847
|
|
1848 } else {
|
|
1849 ctx->output = 0;
|
|
1850 }
|
|
1851
|
|
1852 return NGX_OK;
|
|
1853 }
|
|
1854
|
|
1855 if (p < last && *p == '=') {
|
|
1856 negative = 0;
|
|
1857 p++;
|
|
1858
|
|
1859 } else if (p + 1 < last && *p == '!' && *(p + 1) == '=') {
|
|
1860 negative = 1;
|
|
1861 p += 2;
|
|
1862
|
|
1863 } else {
|
|
1864 goto invalid_expression;
|
|
1865 }
|
|
1866
|
|
1867 while (p < last && *p == ' ') {
|
|
1868 p++;
|
|
1869 }
|
|
1870
|
|
1871 if (p < last && *p == '/') {
|
|
1872 if (*(last - 1) != '/') {
|
|
1873 goto invalid_expression;
|
|
1874 }
|
|
1875
|
|
1876 noregex = 0;
|
|
1877 last--;
|
|
1878 p++;
|
|
1879
|
|
1880 } else {
|
|
1881 noregex = 1;
|
|
1882 }
|
|
1883
|
|
1884 right.len = last - p;
|
|
1885 right.data = p;
|
|
1886
|
102
|
1887 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1888 "right: \"%V\"", &right);
|
|
1889
|
|
1890 if (ngx_http_ssi_evaluate_string(r, ctx, &right, 0) != NGX_OK) {
|
|
1891 return NGX_HTTP_SSI_ERROR;
|
100
|
1892 }
|
|
1893
|
102
|
1894 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1895 "evaluted right: \"%V\"", &right);
|
100
|
1896
|
|
1897 if (noregex) {
|
|
1898 if (left.len != right.len) {
|
|
1899 rc = -1;
|
|
1900
|
|
1901 } else {
|
|
1902 rc = ngx_strncmp(left.data, right.data, right.len);
|
|
1903 }
|
|
1904
|
|
1905 } else {
|
|
1906 #if (NGX_PCRE)
|
|
1907 err.len = NGX_MAX_CONF_ERRSTR;
|
|
1908 err.data = errstr;
|
|
1909
|
|
1910 right.data[right.len] = '\0';
|
|
1911
|
|
1912 regex = ngx_regex_compile(&right, 0, r->pool, &err);
|
|
1913
|
|
1914 if (regex == NULL) {
|
|
1915 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", err.data);
|
|
1916 return NGX_HTTP_SSI_ERROR;
|
|
1917 }
|
|
1918
|
|
1919 rc = ngx_regex_exec(regex, &left, NULL, 0);
|
|
1920
|
|
1921 if (rc != NGX_REGEX_NO_MATCHED && rc < 0) {
|
|
1922 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
1923 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
|
|
1924 rc, &left, &right);
|
|
1925 return NGX_HTTP_SSI_ERROR;
|
|
1926 }
|
|
1927 #else
|
|
1928 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
1929 "the using of the regex \"%V\" in SSI "
|
|
1930 "requires PCRE library", &right);
|
|
1931
|
|
1932 return NGX_HTTP_SSI_ERROR;
|
|
1933 #endif
|
|
1934 }
|
|
1935
|
|
1936 if ((rc == 0 && !negative) || (rc != 0 && negative)) {
|
58
|
1937 ctx->output = 1;
|
|
1938
|
|
1939 } else {
|
|
1940 ctx->output = 0;
|
|
1941 }
|
|
1942
|
|
1943 return NGX_OK;
|
100
|
1944
|
|
1945 invalid_expression:
|
|
1946
|
|
1947 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1948 "invalid expression in \"%V\"", expr);
|
|
1949
|
|
1950 return NGX_HTTP_SSI_ERROR;
|
58
|
1951 }
|
|
1952
|
|
1953
|
|
1954 static ngx_int_t
|
|
1955 ngx_http_ssi_else(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1956 ngx_str_t **params)
|
|
1957 {
|
|
1958 ctx->output = !ctx->output;
|
|
1959
|
|
1960 return NGX_OK;
|
|
1961 }
|
|
1962
|
|
1963
|
|
1964 static ngx_int_t
|
|
1965 ngx_http_ssi_endif(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1966 ngx_str_t **params)
|
|
1967 {
|
|
1968 ctx->output = 1;
|
|
1969
|
|
1970 return NGX_OK;
|
|
1971 }
|
|
1972
|
|
1973
|
|
1974 static ngx_http_variable_value_t *
|
|
1975 ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r, uintptr_t gmt)
|
|
1976 {
|
|
1977 ngx_http_ssi_ctx_t *ctx;
|
|
1978 ngx_http_variable_value_t *vv;
|
112
|
1979 ngx_time_t *tp;
|
58
|
1980 struct tm tm;
|
|
1981 char buf[NGX_HTTP_SSI_DATE_LEN];
|
|
1982
|
|
1983 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
|
|
1984 if (vv == NULL) {
|
|
1985 return NULL;
|
|
1986 }
|
|
1987
|
112
|
1988 tp = ngx_timeofday();
|
|
1989
|
58
|
1990 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
|
|
1991
|
|
1992 if (ctx->timefmt.len == sizeof("%s") - 1
|
|
1993 && ctx->timefmt.data[0] == '%' && ctx->timefmt.data[1] == 's')
|
|
1994 {
|
112
|
1995 vv->value = tp->sec + (gmt ? 0 : tp->gmtoff);
|
58
|
1996
|
|
1997 vv->text.data = ngx_palloc(r->pool, NGX_TIME_T_LEN);
|
|
1998 if (vv->text.data == NULL) {
|
|
1999 return NULL;
|
|
2000 }
|
|
2001
|
|
2002 vv->text.len = ngx_sprintf(vv->text.data, "%T", vv->value)
|
|
2003 - vv->text.data;
|
|
2004 return vv;
|
|
2005 }
|
|
2006
|
|
2007 if (gmt) {
|
112
|
2008 ngx_libc_gmtime(tp->sec, &tm);
|
58
|
2009 } else {
|
112
|
2010 ngx_libc_localtime(tp->sec, &tm);
|
58
|
2011 }
|
|
2012
|
112
|
2013 vv->value = tp->sec + (gmt ? 0 : tp->gmtoff);
|
58
|
2014
|
|
2015 vv->text.len = strftime(buf, NGX_HTTP_SSI_DATE_LEN,
|
|
2016 (char *) ctx->timefmt.data, &tm);
|
|
2017 if (vv->text.len == 0) {
|
|
2018 return NULL;
|
|
2019 }
|
|
2020
|
|
2021 vv->text.data = ngx_palloc(r->pool, vv->text.len);
|
|
2022 if (vv->text.data == NULL) {
|
|
2023 return NULL;
|
|
2024 }
|
|
2025
|
|
2026 ngx_memcpy(vv->text.data, buf, vv->text.len);
|
|
2027
|
|
2028 return vv;
|
|
2029 }
|
|
2030
|
|
2031
|
76
|
2032 static char *
|
|
2033 ngx_http_ssi_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2034 {
|
|
2035 ngx_http_ssi_conf_t *scf = conf;
|
|
2036
|
|
2037 ngx_str_t *value, *type;
|
|
2038 ngx_uint_t i;
|
|
2039
|
|
2040 if (scf->types == NULL) {
|
|
2041 scf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
|
|
2042 if (scf->types == NULL) {
|
|
2043 return NGX_CONF_ERROR;
|
|
2044 }
|
|
2045
|
|
2046 type = ngx_array_push(scf->types);
|
|
2047 if (type == NULL) {
|
|
2048 return NGX_CONF_ERROR;
|
|
2049 }
|
|
2050
|
|
2051 type->len = sizeof("text/html") - 1;
|
|
2052 type->data = (u_char *) "text/html";
|
|
2053 }
|
|
2054
|
|
2055 value = cf->args->elts;
|
|
2056
|
|
2057 for (i = 1; i < cf->args->nelts; i++) {
|
|
2058
|
|
2059 if (ngx_strcmp(value[i].data, "text/html") == 0) {
|
|
2060 continue;
|
|
2061 }
|
|
2062
|
|
2063 type = ngx_array_push(scf->types);
|
|
2064 if (type == NULL) {
|
|
2065 return NGX_CONF_ERROR;
|
|
2066 }
|
|
2067
|
|
2068 type->len = value[i].len;
|
|
2069
|
|
2070 type->data = ngx_palloc(cf->pool, type->len + 1);
|
|
2071 if (type->data == NULL) {
|
|
2072 return NGX_CONF_ERROR;
|
|
2073 }
|
|
2074
|
|
2075 ngx_cpystrn(type->data, value[i].data, type->len + 1);
|
|
2076 }
|
|
2077
|
|
2078 return NGX_CONF_OK;
|
|
2079 }
|
|
2080
|
|
2081
|
58
|
2082 static ngx_int_t
|
|
2083 ngx_http_ssi_add_variables(ngx_conf_t *cf)
|
|
2084 {
|
|
2085 ngx_http_variable_t *var, *v;
|
|
2086
|
|
2087 for (v = ngx_http_ssi_vars; v->name.len; v++) {
|
|
2088 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2089 if (var == NULL) {
|
|
2090 return NGX_ERROR;
|
|
2091 }
|
|
2092
|
|
2093 var->handler = v->handler;
|
|
2094 var->data = v->data;
|
|
2095 }
|
|
2096
|
|
2097 return NGX_OK;
|
|
2098 }
|
|
2099
|
|
2100
|
46
|
2101 static void *
|
|
2102 ngx_http_ssi_create_conf(ngx_conf_t *cf)
|
|
2103 {
|
|
2104 ngx_http_ssi_conf_t *conf;
|
|
2105
|
48
|
2106 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_conf_t));
|
|
2107 if (conf == NULL) {
|
46
|
2108 return NGX_CONF_ERROR;
|
|
2109 }
|
|
2110
|
76
|
2111 /*
|
|
2112 * set by ngx_pcalloc():
|
|
2113 *
|
|
2114 * conf->types = NULL;
|
|
2115 */
|
|
2116
|
46
|
2117 conf->enable = NGX_CONF_UNSET;
|
|
2118 conf->silent_errors = NGX_CONF_UNSET;
|
58
|
2119 conf->ignore_recycled_buffers = NGX_CONF_UNSET;
|
46
|
2120
|
|
2121 conf->min_file_chunk = NGX_CONF_UNSET_SIZE;
|
|
2122 conf->value_len = NGX_CONF_UNSET_SIZE;
|
|
2123
|
|
2124 return conf;
|
|
2125 }
|
|
2126
|
|
2127
|
|
2128 static char *
|
|
2129 ngx_http_ssi_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2130 {
|
|
2131 ngx_http_ssi_conf_t *prev = parent;
|
|
2132 ngx_http_ssi_conf_t *conf = child;
|
|
2133
|
76
|
2134 ngx_str_t *type;
|
|
2135
|
46
|
2136 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
2137 ngx_conf_merge_value(conf->silent_errors, prev->silent_errors, 0);
|
58
|
2138 ngx_conf_merge_value(conf->ignore_recycled_buffers,
|
|
2139 prev->ignore_recycled_buffers, 0);
|
46
|
2140
|
|
2141 ngx_conf_merge_size_value(conf->min_file_chunk, prev->min_file_chunk, 1024);
|
|
2142 ngx_conf_merge_size_value(conf->value_len, prev->value_len, 256);
|
|
2143
|
76
|
2144 if (conf->types == NULL) {
|
|
2145 if (prev->types == NULL) {
|
|
2146 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
|
|
2147 if (conf->types == NULL) {
|
|
2148 return NGX_CONF_ERROR;
|
|
2149 }
|
|
2150
|
|
2151 type = ngx_array_push(conf->types);
|
|
2152 if (type == NULL) {
|
|
2153 return NGX_CONF_ERROR;
|
|
2154 }
|
|
2155
|
|
2156 type->len = sizeof("text/html") - 1;
|
|
2157 type->data = (u_char *) "text/html";
|
|
2158
|
|
2159 } else {
|
|
2160 conf->types = prev->types;
|
|
2161 }
|
|
2162 }
|
|
2163
|
46
|
2164 return NGX_CONF_OK;
|
|
2165 }
|
|
2166
|
|
2167
|
|
2168 static ngx_int_t
|
|
2169 ngx_http_ssi_filter_init(ngx_cycle_t *cycle)
|
|
2170 {
|
|
2171 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
2172 ngx_http_top_header_filter = ngx_http_ssi_header_filter;
|
|
2173
|
|
2174 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
2175 ngx_http_top_body_filter = ngx_http_ssi_body_filter;
|
|
2176
|
|
2177 return NGX_OK;
|
|
2178 }
|