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
|
122
|
147 static ngx_int_t ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
|
|
148 ngx_http_variable_value_t *v, uintptr_t gmt);
|
58
|
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
|
122
|
565 b = NULL;
|
|
566
|
46
|
567 if (rc == NGX_OK) {
|
|
568
|
|
569 for (cmd = ngx_http_ssi_commands; cmd->handler; cmd++) {
|
|
570 if (cmd->name.len == 0) {
|
|
571 cmd = (ngx_http_ssi_command_t *) cmd->handler;
|
|
572 }
|
|
573
|
|
574 if (cmd->name.len != ctx->command.len
|
|
575 || ngx_strncmp(cmd->name.data, ctx->command.data,
|
|
576 ctx->command.len) != 0)
|
|
577 {
|
|
578 continue;
|
|
579 }
|
|
580
|
|
581 break;
|
|
582 }
|
|
583
|
58
|
584 if (cmd->name.len == 0 && ctx->output) {
|
46
|
585 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
586 "invalid SSI command: \"%V\"", &ctx->command);
|
|
587 goto ssi_error;
|
|
588 }
|
|
589
|
58
|
590 if (!ctx->output && !cmd->conditional) {
|
|
591 continue;
|
|
592 }
|
|
593
|
46
|
594 ngx_memzero(params,
|
|
595 NGX_HTTP_SSI_MAX_PARAMS * sizeof(ngx_str_t *));
|
|
596
|
|
597 param = ctx->params.elts;
|
|
598
|
|
599
|
|
600 for (i = 0; i < ctx->params.nelts; i++) {
|
|
601
|
|
602 for (prm = cmd->params; prm->name.len; prm++) {
|
|
603
|
|
604 if (param[i].key.len != prm->name.len
|
|
605 || ngx_strncmp(param[i].key.data, prm->name.data,
|
|
606 prm->name.len) != 0)
|
|
607 {
|
|
608 continue;
|
|
609 }
|
|
610
|
|
611 if (params[prm->index]) {
|
|
612 ngx_log_error(NGX_LOG_ERR,
|
|
613 r->connection->log, 0,
|
|
614 "duplicate \"%V\" parameter "
|
|
615 "in \"%V\" SSI command",
|
|
616 ¶m[i].key, &ctx->command);
|
|
617
|
|
618 goto ssi_error;
|
|
619 }
|
|
620
|
|
621 params[prm->index] = ¶m[i].value;
|
|
622
|
|
623 break;
|
|
624 }
|
|
625
|
|
626 if (prm->name.len == 0) {
|
|
627 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
628 "invalid parameter name: \"%V\" "
|
|
629 "in \"%V\" SSI command",
|
|
630 ¶m[i].key, &ctx->command);
|
|
631
|
|
632 goto ssi_error;
|
|
633 }
|
|
634 }
|
|
635
|
|
636 for (prm = cmd->params; prm->name.len; prm++) {
|
|
637 if (prm->mandatory && params[prm->index] == 0) {
|
|
638 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
639 "mandatory \"%V\" parameter is absent "
|
|
640 "in \"%V\" SSI command",
|
|
641 &prm->name, &ctx->command);
|
|
642
|
|
643 goto ssi_error;
|
|
644 }
|
|
645 }
|
|
646
|
58
|
647 if (cmd->flush && ctx->out) {
|
|
648 rc = ngx_http_ssi_output(r, ctx);
|
|
649
|
|
650 if (rc == NGX_ERROR) {
|
|
651 return NGX_ERROR;
|
|
652 }
|
|
653 }
|
|
654
|
118
|
655 c = r->connection;
|
|
656
|
|
657 rc = cmd->handler(r, ctx, params);
|
|
658
|
|
659 if (c->closed) {
|
|
660 return NGX_DONE;
|
|
661 }
|
|
662
|
|
663 if (rc == NGX_OK) {
|
46
|
664 continue;
|
|
665 }
|
|
666 }
|
|
667
|
|
668
|
|
669 /* rc == NGX_HTTP_SSI_ERROR */
|
|
670
|
48
|
671 ssi_error:
|
46
|
672
|
|
673 if (conf->silent_errors) {
|
|
674 continue;
|
|
675 }
|
|
676
|
48
|
677 if (ctx->free) {
|
|
678 cl = ctx->free;
|
|
679 ctx->free = ctx->free->next;
|
|
680 b = cl->buf;
|
|
681 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
682
|
|
683 } else {
|
|
684 b = ngx_calloc_buf(r->pool);
|
|
685 if (b == NULL) {
|
|
686 return NGX_ERROR;
|
|
687 }
|
|
688
|
|
689 cl = ngx_alloc_chain_link(r->pool);
|
|
690 if (cl == NULL) {
|
|
691 return NGX_ERROR;
|
|
692 }
|
|
693
|
|
694 cl->buf = b;
|
46
|
695 }
|
|
696
|
|
697 b->memory = 1;
|
96
|
698 b->pos = ctx->errmsg.data;
|
|
699 b->last = ctx->errmsg.data + ctx->errmsg.len;
|
46
|
700
|
|
701 cl->next = NULL;
|
|
702 *ctx->last_out = cl;
|
|
703 ctx->last_out = &cl->next;
|
|
704
|
|
705 continue;
|
|
706 }
|
|
707
|
58
|
708 if (ctx->buf->last_buf || ctx->buf->recycled) {
|
|
709
|
48
|
710 if (b == NULL) {
|
|
711 if (ctx->free) {
|
|
712 cl = ctx->free;
|
|
713 ctx->free = ctx->free->next;
|
|
714 b = cl->buf;
|
|
715 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
716
|
|
717 } else {
|
|
718 b = ngx_calloc_buf(r->pool);
|
|
719 if (b == NULL) {
|
|
720 return NGX_ERROR;
|
|
721 }
|
46
|
722
|
48
|
723 cl = ngx_alloc_chain_link(r->pool);
|
|
724 if (cl == NULL) {
|
|
725 return NGX_ERROR;
|
|
726 }
|
|
727
|
|
728 cl->buf = b;
|
|
729 }
|
|
730
|
58
|
731 b->sync = 1;
|
|
732
|
48
|
733 cl->next = NULL;
|
|
734 *ctx->last_out = cl;
|
|
735 ctx->last_out = &cl->next;
|
|
736 }
|
|
737
|
|
738 b->last_buf = ctx->buf->last_buf;
|
|
739 b->shadow = ctx->buf;
|
58
|
740
|
|
741 if (conf->ignore_recycled_buffers == 0) {
|
|
742 b->recycled = ctx->buf->recycled;
|
|
743 }
|
46
|
744 }
|
|
745
|
48
|
746 ctx->buf = NULL;
|
|
747
|
46
|
748 ctx->saved = ctx->looked;
|
|
749 }
|
|
750
|
48
|
751 if (ctx->out == NULL && ctx->busy == NULL) {
|
46
|
752 return NGX_OK;
|
|
753 }
|
|
754
|
58
|
755 return ngx_http_ssi_output(r, ctx);
|
|
756 }
|
|
757
|
|
758
|
|
759 static ngx_int_t
|
|
760 ngx_http_ssi_output(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
761 {
|
|
762 ngx_int_t rc;
|
|
763 ngx_buf_t *b;
|
|
764 ngx_chain_t *cl;
|
|
765
|
46
|
766 rc = ngx_http_next_body_filter(r, ctx->out);
|
|
767
|
48
|
768 if (ctx->busy == NULL) {
|
|
769 ctx->busy = ctx->out;
|
|
770
|
|
771 } else {
|
|
772 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
|
|
773 cl->next = ctx->out;
|
|
774 }
|
|
775
|
46
|
776 ctx->out = NULL;
|
|
777 ctx->last_out = &ctx->out;
|
|
778
|
48
|
779 while (ctx->busy) {
|
|
780
|
|
781 b = ctx->busy->buf;
|
|
782
|
|
783 if (ngx_buf_size(b) != 0) {
|
|
784 break;
|
|
785 }
|
|
786
|
|
787 #if (NGX_HAVE_WRITE_ZEROCOPY)
|
|
788 if (b->zerocopy_busy) {
|
|
789 break;
|
|
790 }
|
|
791 #endif
|
|
792
|
|
793 if (b->shadow) {
|
|
794 b->shadow->pos = b->shadow->last;
|
|
795 }
|
|
796
|
|
797 cl = ctx->busy;
|
|
798 ctx->busy = cl->next;
|
|
799 cl->next = ctx->free;
|
|
800 ctx->free = cl;
|
|
801 }
|
|
802
|
46
|
803 return rc;
|
|
804 }
|
|
805
|
|
806
|
|
807 static ngx_int_t
|
|
808 ngx_http_ssi_parse(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
|
|
809 {
|
|
810 u_char *p, *last, *copy_end, ch;
|
|
811 size_t looked;
|
|
812 ngx_http_ssi_state_e state;
|
|
813
|
|
814 state = ctx->state;
|
|
815 looked = ctx->looked;
|
|
816 last = ctx->buf->last;
|
|
817 copy_end = ctx->copy_end;
|
|
818
|
|
819 for (p = ctx->pos; p < last; p++) {
|
|
820
|
|
821 ch = *p;
|
|
822
|
|
823 if (state == ssi_start_state) {
|
|
824
|
|
825 /* the tight loop */
|
|
826
|
48
|
827 for ( ;; ) {
|
|
828 if (ch == '<') {
|
|
829 copy_end = p;
|
|
830 looked = 1;
|
|
831 state = ssi_tag_state;
|
|
832
|
|
833 goto tag_started;
|
46
|
834 }
|
|
835
|
48
|
836 if (++p == last) {
|
|
837 break;
|
|
838 }
|
46
|
839
|
48
|
840 ch = *p;
|
46
|
841 }
|
|
842
|
|
843 ctx->pos = p;
|
|
844 ctx->looked = looked;
|
|
845 ctx->copy_end = p;
|
|
846
|
|
847 if (ctx->copy_start == NULL) {
|
|
848 ctx->copy_start = ctx->buf->pos;
|
|
849 }
|
|
850
|
|
851 return NGX_AGAIN;
|
|
852
|
48
|
853 tag_started:
|
|
854
|
46
|
855 continue;
|
|
856 }
|
|
857
|
|
858 switch (state) {
|
|
859
|
|
860 case ssi_start_state:
|
|
861 break;
|
|
862
|
|
863 case ssi_tag_state:
|
|
864 switch (ch) {
|
|
865 case '!':
|
|
866 looked = 2;
|
|
867 state = ssi_comment0_state;
|
|
868 break;
|
|
869
|
|
870 case '<':
|
|
871 copy_end = p;
|
|
872 break;
|
|
873
|
|
874 default:
|
|
875 copy_end = p;
|
|
876 looked = 0;
|
|
877 state = ssi_start_state;
|
|
878 break;
|
|
879 }
|
|
880
|
|
881 break;
|
|
882
|
|
883 case ssi_comment0_state:
|
|
884 switch (ch) {
|
|
885 case '-':
|
|
886 looked = 3;
|
|
887 state = ssi_comment1_state;
|
|
888 break;
|
|
889
|
|
890 case '<':
|
|
891 copy_end = p;
|
|
892 looked = 1;
|
|
893 state = ssi_tag_state;
|
|
894 break;
|
|
895
|
|
896 default:
|
|
897 copy_end = p;
|
|
898 looked = 0;
|
|
899 state = ssi_start_state;
|
|
900 break;
|
|
901 }
|
|
902
|
|
903 break;
|
|
904
|
|
905 case ssi_comment1_state:
|
|
906 switch (ch) {
|
|
907 case '-':
|
|
908 looked = 4;
|
|
909 state = ssi_sharp_state;
|
|
910 break;
|
|
911
|
|
912 case '<':
|
|
913 copy_end = p;
|
|
914 looked = 1;
|
|
915 state = ssi_tag_state;
|
|
916 break;
|
|
917
|
|
918 default:
|
|
919 copy_end = p;
|
|
920 looked = 0;
|
|
921 state = ssi_start_state;
|
|
922 break;
|
|
923 }
|
|
924
|
|
925 break;
|
|
926
|
|
927 case ssi_sharp_state:
|
|
928 switch (ch) {
|
|
929 case '#':
|
|
930 if (ctx->copy_start) {
|
|
931 ctx->saved = 0;
|
|
932 }
|
|
933 looked = 0;
|
|
934 state = ssi_precommand_state;
|
|
935 break;
|
|
936
|
|
937 case '<':
|
|
938 copy_end = p;
|
|
939 looked = 1;
|
|
940 state = ssi_tag_state;
|
|
941 break;
|
|
942
|
|
943 default:
|
|
944 copy_end = p;
|
|
945 looked = 0;
|
|
946 state = ssi_start_state;
|
|
947 break;
|
|
948 }
|
|
949
|
|
950 break;
|
|
951
|
|
952 case ssi_precommand_state:
|
|
953 switch (ch) {
|
|
954 case ' ':
|
|
955 case CR:
|
|
956 case LF:
|
|
957 case '\t':
|
|
958 break;
|
|
959
|
|
960 default:
|
|
961 ctx->command.len = 1;
|
|
962 ctx->command.data = ngx_palloc(r->pool,
|
|
963 NGX_HTTP_SSI_COMMAND_LEN + 1);
|
|
964 if (ctx->command.data == NULL) {
|
|
965 return NGX_ERROR;
|
|
966 }
|
|
967
|
|
968 ctx->command.data[0] = ch;
|
|
969 ctx->params.nelts = 0;
|
|
970 state = ssi_command_state;
|
|
971 break;
|
|
972 }
|
|
973
|
|
974 break;
|
|
975
|
|
976 case ssi_command_state:
|
|
977 switch (ch) {
|
|
978 case ' ':
|
|
979 case CR:
|
|
980 case LF:
|
|
981 case '\t':
|
|
982 state = ssi_preparam_state;
|
|
983 break;
|
|
984
|
|
985 case '-':
|
|
986 state = ssi_comment_end0_state;
|
|
987 break;
|
|
988
|
|
989 default:
|
|
990 ctx->command.data[ctx->command.len++] = ch;
|
|
991
|
|
992 if (ctx->command.len == NGX_HTTP_SSI_COMMAND_LEN) {
|
|
993 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
994 "the \"%V\" SSI command is too long",
|
|
995 &ctx->command);
|
|
996
|
|
997 state = ssi_error_state;
|
|
998 break;
|
|
999 }
|
|
1000 }
|
|
1001
|
|
1002 break;
|
|
1003
|
|
1004 case ssi_preparam_state:
|
|
1005 switch (ch) {
|
|
1006 case ' ':
|
|
1007 case CR:
|
|
1008 case LF:
|
|
1009 case '\t':
|
|
1010 break;
|
|
1011
|
|
1012 case '-':
|
|
1013 state = ssi_comment_end0_state;
|
|
1014 break;
|
|
1015
|
|
1016 default:
|
48
|
1017 ctx->param = ngx_array_push(&ctx->params);
|
|
1018 if (ctx->param == NULL) {
|
46
|
1019 return NGX_ERROR;
|
|
1020 }
|
|
1021
|
|
1022 ctx->param->key.len = 1;
|
|
1023 ctx->param->key.data = ngx_palloc(r->pool,
|
|
1024 NGX_HTTP_SSI_PARAM_LEN + 1);
|
|
1025 if (ctx->param->key.data == NULL) {
|
|
1026 return NGX_ERROR;
|
|
1027 }
|
|
1028
|
|
1029 ctx->param->key.data[0] = ch;
|
|
1030
|
|
1031 ctx->param->value.len = 0;
|
|
1032 ctx->param->value.data = ngx_palloc(r->pool,
|
|
1033 ctx->value_len + 1);
|
|
1034 if (ctx->param->value.data == NULL) {
|
|
1035 return NGX_ERROR;
|
|
1036 }
|
|
1037
|
|
1038 state = ssi_param_state;
|
|
1039 break;
|
|
1040 }
|
|
1041
|
|
1042 break;
|
|
1043
|
|
1044 case ssi_param_state:
|
|
1045 switch (ch) {
|
|
1046 case ' ':
|
|
1047 case CR:
|
|
1048 case LF:
|
|
1049 case '\t':
|
|
1050 state = ssi_preequal_state;
|
|
1051 break;
|
|
1052
|
|
1053 case '=':
|
|
1054 state = ssi_prevalue_state;
|
|
1055 break;
|
|
1056
|
|
1057 case '-':
|
|
1058 state = ssi_error_end0_state;
|
|
1059
|
|
1060 ctx->param->key.data[ctx->param->key.len++] = ch;
|
|
1061 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1062 "invalid \"%V\" parameter in \"%V\" SSI command",
|
|
1063 &ctx->param->key, &ctx->command);
|
|
1064 break;
|
|
1065
|
|
1066 default:
|
|
1067 ctx->param->key.data[ctx->param->key.len++] = ch;
|
|
1068
|
|
1069 if (ctx->param->key.len == NGX_HTTP_SSI_PARAM_LEN) {
|
|
1070 state = ssi_error_state;
|
|
1071 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1072 "too long \"%V\" parameter in "
|
|
1073 "\"%V\" SSI command",
|
|
1074 &ctx->param->key, &ctx->command);
|
|
1075 break;
|
|
1076 }
|
|
1077 }
|
|
1078
|
|
1079 break;
|
|
1080
|
|
1081 case ssi_preequal_state:
|
|
1082 switch (ch) {
|
|
1083 case ' ':
|
|
1084 case CR:
|
|
1085 case LF:
|
|
1086 case '\t':
|
|
1087 break;
|
|
1088
|
|
1089 case '=':
|
|
1090 state = ssi_prevalue_state;
|
|
1091 break;
|
|
1092
|
|
1093 default:
|
|
1094 if (ch == '-') {
|
|
1095 state = ssi_error_end0_state;
|
|
1096 } else {
|
|
1097 state = ssi_error_state;
|
|
1098 }
|
|
1099
|
|
1100 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1101 "unexpected \"%c\" symbol after \"%V\" "
|
|
1102 "parameter in \"%V\" SSI command",
|
|
1103 ch, &ctx->param->key, &ctx->command);
|
|
1104 break;
|
|
1105 }
|
|
1106
|
|
1107 break;
|
|
1108
|
|
1109 case ssi_prevalue_state:
|
|
1110 switch (ch) {
|
|
1111 case ' ':
|
|
1112 case CR:
|
|
1113 case LF:
|
|
1114 case '\t':
|
|
1115 break;
|
|
1116
|
|
1117 case '"':
|
|
1118 state = ssi_double_quoted_value_state;
|
|
1119 break;
|
|
1120
|
|
1121 case '\'':
|
|
1122 state = ssi_quoted_value_state;
|
|
1123 break;
|
|
1124
|
|
1125 default:
|
|
1126 if (ch == '-') {
|
|
1127 state = ssi_error_end0_state;
|
|
1128 } else {
|
|
1129 state = ssi_error_state;
|
|
1130 }
|
|
1131
|
|
1132 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1133 "unexpected \"%c\" symbol before value of "
|
|
1134 "\"%V\" parameter in \"%V\" SSI command",
|
|
1135 ch, &ctx->param->key, &ctx->command);
|
|
1136 break;
|
|
1137 }
|
|
1138
|
|
1139 break;
|
|
1140
|
|
1141 case ssi_double_quoted_value_state:
|
|
1142 switch (ch) {
|
108
|
1143 case '"':
|
|
1144 state = ssi_postparam_state;
|
|
1145 break;
|
|
1146
|
46
|
1147 case '\\':
|
|
1148 ctx->saved_state = ssi_double_quoted_value_state;
|
|
1149 state = ssi_quoted_symbol_state;
|
108
|
1150
|
|
1151 /* fall through */
|
46
|
1152
|
|
1153 default:
|
|
1154 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1155
|
|
1156 if (ctx->param->value.len == ctx->value_len) {
|
|
1157 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1158 "too long \"%V\" value of \"%V\" parameter "
|
|
1159 "in \"%V\" SSI command",
|
|
1160 &ctx->param->value, &ctx->param->key,
|
|
1161 &ctx->command);
|
|
1162 state = ssi_error_state;
|
|
1163 break;
|
|
1164 }
|
|
1165 }
|
|
1166
|
|
1167 break;
|
|
1168
|
|
1169 case ssi_quoted_value_state:
|
|
1170 switch (ch) {
|
108
|
1171 case '\'':
|
|
1172 state = ssi_postparam_state;
|
|
1173 break;
|
|
1174
|
46
|
1175 case '\\':
|
|
1176 ctx->saved_state = ssi_quoted_value_state;
|
|
1177 state = ssi_quoted_symbol_state;
|
108
|
1178
|
|
1179 /* fall through */
|
46
|
1180
|
|
1181 default:
|
|
1182 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1183
|
|
1184 if (ctx->param->value.len == ctx->value_len) {
|
|
1185 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1186 "too long \"%V\" value of \"%V\" parameter "
|
|
1187 "in \"%V\" SSI command",
|
|
1188 &ctx->param->value, &ctx->param->key,
|
|
1189 &ctx->command);
|
|
1190 state = ssi_error_state;
|
|
1191 break;
|
|
1192 }
|
|
1193 }
|
|
1194
|
|
1195 break;
|
|
1196
|
|
1197 case ssi_quoted_symbol_state:
|
108
|
1198 state = ctx->saved_state;
|
|
1199
|
|
1200 if (ch == '\\') {
|
|
1201 break;
|
|
1202 }
|
|
1203
|
|
1204 if (ch == '"' && state == ssi_double_quoted_value_state) {
|
122
|
1205 ctx->param->value.data[ctx->param->value.len - 1] = ch;
|
108
|
1206 break;
|
|
1207 }
|
|
1208
|
|
1209 if (ch == '\'' && state == ssi_quoted_value_state) {
|
122
|
1210 ctx->param->value.data[ctx->param->value.len - 1] = ch;
|
108
|
1211 break;
|
|
1212 }
|
|
1213
|
46
|
1214 ctx->param->value.data[ctx->param->value.len++] = ch;
|
|
1215
|
|
1216 if (ctx->param->value.len == ctx->value_len) {
|
|
1217 if (ctx->param->value.len == ctx->value_len) {
|
|
1218 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1219 "too long \"%V\" value of \"%V\" parameter "
|
|
1220 "in \"%V\" SSI command",
|
|
1221 &ctx->param->value, &ctx->param->key,
|
|
1222 &ctx->command);
|
|
1223 state = ssi_error_state;
|
|
1224 break;
|
|
1225 }
|
|
1226 }
|
|
1227
|
|
1228 break;
|
|
1229
|
|
1230 case ssi_postparam_state:
|
|
1231 switch (ch) {
|
|
1232 case ' ':
|
|
1233 case CR:
|
|
1234 case LF:
|
|
1235 case '\t':
|
|
1236 state = ssi_preparam_state;
|
|
1237 break;
|
|
1238
|
|
1239 case '-':
|
|
1240 state = ssi_comment_end0_state;
|
|
1241 break;
|
|
1242
|
|
1243 default:
|
|
1244 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1245 "unexpected \"%c\" symbol after \"%V\" value "
|
|
1246 "of \"%V\" parameter in \"%V\" SSI command",
|
|
1247 ch, &ctx->param->value, &ctx->param->key,
|
|
1248 &ctx->command);
|
|
1249 state = ssi_error_state;
|
|
1250 break;
|
|
1251 }
|
|
1252
|
|
1253 break;
|
|
1254
|
|
1255 case ssi_comment_end0_state:
|
|
1256 switch (ch) {
|
|
1257 case '-':
|
|
1258 state = ssi_comment_end1_state;
|
|
1259 break;
|
|
1260
|
|
1261 default:
|
|
1262 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1263 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1264 ch, &ctx->command);
|
|
1265 state = ssi_error_state;
|
|
1266 break;
|
|
1267 }
|
|
1268
|
|
1269 break;
|
|
1270
|
|
1271 case ssi_comment_end1_state:
|
|
1272 switch (ch) {
|
|
1273 case '>':
|
|
1274 ctx->state = ssi_start_state;
|
|
1275 ctx->pos = p + 1;
|
|
1276 ctx->looked = looked;
|
|
1277 ctx->copy_end = copy_end;
|
|
1278
|
|
1279 if (ctx->copy_start == NULL && copy_end) {
|
|
1280 ctx->copy_start = ctx->buf->pos;
|
|
1281 }
|
|
1282
|
|
1283 return NGX_OK;
|
|
1284
|
|
1285 default:
|
|
1286 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1287 "unexpected \"%c\" symbol in \"%V\" SSI command",
|
|
1288 ch, &ctx->command);
|
|
1289 state = ssi_error_state;
|
|
1290 break;
|
|
1291 }
|
|
1292
|
|
1293 break;
|
|
1294
|
|
1295 case ssi_error_state:
|
|
1296 switch (ch) {
|
|
1297 case '-':
|
|
1298 state = ssi_error_end0_state;
|
|
1299 break;
|
|
1300
|
|
1301 default:
|
|
1302 break;
|
|
1303 }
|
|
1304
|
|
1305 break;
|
|
1306
|
|
1307 case ssi_error_end0_state:
|
|
1308 switch (ch) {
|
|
1309 case '-':
|
|
1310 state = ssi_error_end1_state;
|
|
1311 break;
|
|
1312
|
|
1313 default:
|
|
1314 state = ssi_error_state;
|
|
1315 break;
|
|
1316 }
|
|
1317
|
|
1318 break;
|
|
1319
|
|
1320 case ssi_error_end1_state:
|
|
1321 switch (ch) {
|
|
1322 case '>':
|
|
1323 ctx->state = ssi_start_state;
|
|
1324 ctx->pos = p + 1;
|
|
1325 ctx->looked = looked;
|
|
1326 ctx->copy_end = copy_end;
|
|
1327
|
|
1328 if (ctx->copy_start == NULL && copy_end) {
|
|
1329 ctx->copy_start = ctx->buf->pos;
|
|
1330 }
|
|
1331
|
|
1332 return NGX_HTTP_SSI_ERROR;
|
|
1333
|
|
1334 default:
|
|
1335 state = ssi_error_state;
|
|
1336 break;
|
|
1337 }
|
|
1338
|
|
1339 break;
|
|
1340 }
|
|
1341 }
|
|
1342
|
|
1343 ctx->state = state;
|
|
1344 ctx->pos = p;
|
|
1345 ctx->looked = looked;
|
|
1346
|
|
1347 ctx->copy_end = (state == ssi_start_state) ? p : copy_end;
|
|
1348
|
|
1349 if (ctx->copy_start == NULL && ctx->copy_end) {
|
|
1350 ctx->copy_start = ctx->buf->pos;
|
|
1351 }
|
|
1352
|
|
1353 return NGX_AGAIN;
|
|
1354 }
|
|
1355
|
|
1356
|
102
|
1357 static ngx_str_t *
|
|
1358 ngx_http_ssi_get_variable(ngx_http_request_t *r, ngx_str_t *name)
|
|
1359 {
|
|
1360 ngx_uint_t i;
|
|
1361 ngx_http_ssi_var_t *var;
|
|
1362 ngx_http_ssi_ctx_t *ctx;
|
|
1363
|
|
1364 ctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
1365
|
|
1366 var = ctx->variables.elts;
|
|
1367 for (i = 0; i < ctx->variables.nelts; i++) {
|
|
1368 if (name->len != var[i].name.len) {
|
|
1369 continue;
|
|
1370 }
|
|
1371
|
|
1372 if (ngx_strncasecmp(name->data, var[i].name.data, name->len) == 0) {
|
|
1373 return &var[i].value;
|
|
1374 }
|
|
1375 }
|
|
1376
|
|
1377 return NULL;
|
|
1378 }
|
|
1379
|
|
1380
|
|
1381 static ngx_int_t
|
|
1382 ngx_http_ssi_evaluate_string(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1383 ngx_str_t *text, ngx_uint_t flags)
|
|
1384 {
|
122
|
1385 u_char ch, *p, **value, *data, *part_data;
|
|
1386 size_t *size, len, prefix, part_len;
|
|
1387 ngx_str_t var, *val;
|
102
|
1388 ngx_uint_t i, j, n, bracket;
|
|
1389 ngx_array_t lengths, values;
|
|
1390 ngx_http_variable_value_t *vv;
|
|
1391
|
|
1392 n = ngx_http_script_variables_count(text);
|
|
1393
|
|
1394 if (n == 0) {
|
|
1395
|
|
1396 if (!(flags & NGX_HTTP_SSI_ADD_PREFIX)) {
|
|
1397 return NGX_OK;
|
|
1398 }
|
|
1399
|
|
1400 if (text->data[0] != '/') {
|
|
1401 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1402 if (r->uri.data[prefix - 1] == '/') {
|
|
1403 break;
|
|
1404 }
|
|
1405 }
|
|
1406
|
|
1407 if (prefix) {
|
|
1408 len = prefix + text->len;
|
|
1409
|
|
1410 data = ngx_palloc(r->pool, len);
|
|
1411 if (data == NULL) {
|
|
1412 return NGX_ERROR;
|
|
1413 }
|
|
1414
|
122
|
1415 p = ngx_copy(data, r->uri.data, prefix);
|
102
|
1416 ngx_memcpy(p, text->data, text->len);
|
|
1417
|
|
1418 text->len = len;
|
|
1419 text->data = data;
|
|
1420 }
|
|
1421 }
|
|
1422
|
|
1423 return NGX_OK;
|
|
1424 }
|
|
1425
|
|
1426 if (ngx_array_init(&lengths, r->pool, 8, sizeof(size_t *)) != NGX_OK) {
|
|
1427 return NGX_ERROR;
|
|
1428 }
|
|
1429
|
|
1430 if (ngx_array_init(&values, r->pool, 8, sizeof(u_char *)) != NGX_OK) {
|
|
1431 return NGX_ERROR;
|
|
1432 }
|
|
1433
|
|
1434 len = 0;
|
|
1435 i = 0;
|
|
1436
|
|
1437 while (i < text->len) {
|
|
1438
|
|
1439 if (text->data[i] == '$') {
|
|
1440
|
|
1441 var.len = 0;
|
|
1442
|
|
1443 if (++i == text->len) {
|
|
1444 goto invalid_variable;
|
|
1445 }
|
|
1446
|
|
1447 if (text->data[i] == '{') {
|
|
1448 bracket = 1;
|
|
1449
|
|
1450 if (++i == text->len) {
|
|
1451 goto invalid_variable;
|
|
1452 }
|
|
1453
|
|
1454 var.data = &text->data[i];
|
|
1455
|
|
1456 } else {
|
|
1457 bracket = 0;
|
|
1458 var.data = &text->data[i];
|
|
1459 }
|
|
1460
|
|
1461 for ( /* void */ ; i < text->len; i++, var.len++) {
|
|
1462 ch = text->data[i];
|
|
1463
|
|
1464 if (ch == '}' && bracket) {
|
|
1465 i++;
|
|
1466 bracket = 0;
|
|
1467 break;
|
|
1468 }
|
|
1469
|
|
1470 if ((ch >= 'A' && ch <= 'Z')
|
|
1471 || (ch >= 'a' && ch <= 'z')
|
|
1472 || (ch >= '0' && ch <= '9')
|
|
1473 || ch == '_')
|
|
1474 {
|
|
1475 continue;
|
|
1476 }
|
|
1477
|
|
1478 break;
|
|
1479 }
|
|
1480
|
|
1481 if (bracket) {
|
|
1482 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1483 "the closing bracket in \"%V\" "
|
|
1484 "variable is missing", &var);
|
|
1485 return NGX_ERROR;
|
|
1486 }
|
|
1487
|
|
1488 if (var.len == 0) {
|
|
1489 goto invalid_variable;
|
|
1490 }
|
|
1491
|
|
1492 for (j = 0; j < var.len; j++) {
|
|
1493 var.data[j] = ngx_tolower(var.data[j]);
|
|
1494 }
|
|
1495
|
|
1496 val = ngx_http_ssi_get_variable(r, &var);
|
|
1497
|
|
1498 if (val == NULL) {
|
|
1499 vv = ngx_http_get_variable(r, &var);
|
|
1500
|
|
1501 if (vv == NULL) {
|
|
1502 return NGX_ERROR;
|
|
1503 }
|
|
1504
|
122
|
1505 if (vv->not_found) {
|
102
|
1506 continue;
|
|
1507 }
|
|
1508
|
122
|
1509 part_data = vv->data;
|
|
1510 part_len = vv->len;
|
102
|
1511
|
|
1512 } else {
|
122
|
1513 part_data = val->data;
|
|
1514 part_len = val->len;
|
102
|
1515 }
|
|
1516
|
|
1517 } else {
|
122
|
1518 part_data = &text->data[i];
|
|
1519
|
|
1520 for (p = part_data; i < text->len; i++) {
|
108
|
1521 ch = text->data[i];
|
|
1522
|
|
1523 if (ch == '$') {
|
|
1524 if (text->data[i - 1] != '\\') {
|
|
1525 break;
|
|
1526 }
|
|
1527
|
|
1528 *(p - 1) = ch;
|
|
1529
|
|
1530 continue;
|
|
1531 }
|
|
1532
|
|
1533 *p++ = ch;
|
102
|
1534 }
|
108
|
1535
|
122
|
1536 part_len = p - part_data;
|
102
|
1537 }
|
|
1538
|
122
|
1539 len += part_len;
|
102
|
1540
|
|
1541 size = ngx_array_push(&lengths);
|
|
1542 if (size == NULL) {
|
|
1543 return NGX_ERROR;
|
|
1544 }
|
|
1545
|
122
|
1546 *size = part_len;
|
102
|
1547
|
|
1548 value = ngx_array_push(&values);
|
|
1549 if (value == NULL) {
|
|
1550 return NGX_ERROR;
|
|
1551 }
|
|
1552
|
122
|
1553 *value = part_data;
|
102
|
1554 }
|
|
1555
|
|
1556 prefix = 0;
|
|
1557
|
|
1558 size = lengths.elts;
|
|
1559 value = values.elts;
|
|
1560
|
|
1561 if (flags & NGX_HTTP_SSI_ADD_PREFIX) {
|
|
1562 for (i = 0; i < values.nelts; i++) {
|
|
1563 if (size[i] != 0) {
|
|
1564 if (*value[i] != '/') {
|
|
1565 for (prefix = r->uri.len; prefix; prefix--) {
|
|
1566 if (r->uri.data[prefix - 1] == '/') {
|
|
1567 len += prefix;
|
|
1568 break;
|
|
1569 }
|
|
1570 }
|
|
1571 }
|
|
1572
|
|
1573 break;
|
|
1574 }
|
|
1575 }
|
|
1576 }
|
|
1577
|
|
1578 p = ngx_palloc(r->pool, len);
|
|
1579 if (p == NULL) {
|
|
1580 return NGX_ERROR;
|
|
1581 }
|
|
1582
|
|
1583 text->len = len;
|
|
1584 text->data = p;
|
|
1585
|
122
|
1586 p = ngx_copy(p, r->uri.data, prefix);
|
102
|
1587
|
|
1588 for (i = 0; i < values.nelts; i++) {
|
122
|
1589 p = ngx_copy(p, value[i], size[i]);
|
102
|
1590 }
|
|
1591
|
|
1592 return NGX_OK;
|
|
1593
|
|
1594 invalid_variable:
|
|
1595
|
|
1596 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1597 "invalid variable name in \"%V\"", text);
|
|
1598
|
|
1599 return NGX_ERROR;
|
|
1600 }
|
|
1601
|
|
1602
|
|
1603 static ngx_int_t
|
|
1604 ngx_http_ssi_include(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1605 ngx_str_t **params)
|
|
1606 {
|
|
1607 ngx_str_t *uri, *file, args;
|
122
|
1608 ngx_uint_t flags;
|
102
|
1609
|
|
1610 uri = params[NGX_HTTP_SSI_INCLUDE_VIRTUAL];
|
|
1611 file = params[NGX_HTTP_SSI_INCLUDE_FILE];
|
|
1612
|
|
1613 if (uri && file) {
|
|
1614 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1615 "inlcusion may be either virtual=\"%V\" or file=\"%V\"",
|
|
1616 uri, file);
|
|
1617 return NGX_HTTP_SSI_ERROR;
|
|
1618 }
|
|
1619
|
|
1620 if (uri == NULL && file == NULL) {
|
|
1621 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1622 "no parameter in \"include\" SSI command");
|
|
1623 return NGX_HTTP_SSI_ERROR;
|
|
1624 }
|
|
1625
|
|
1626 if (uri == NULL) {
|
|
1627 uri = file;
|
|
1628 }
|
|
1629
|
|
1630 if (ngx_http_ssi_evaluate_string(r, ctx, uri, NGX_HTTP_SSI_ADD_PREFIX)
|
|
1631 != NGX_OK)
|
|
1632 {
|
|
1633 return NGX_HTTP_SSI_ERROR;
|
|
1634 }
|
|
1635
|
|
1636 args.len = 0;
|
|
1637 args.data = NULL;
|
122
|
1638 flags = 0;
|
|
1639
|
|
1640 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1641 "ssi include: \"%V\"", uri);
|
|
1642
|
|
1643 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
|
|
1644 return NGX_HTTP_SSI_ERROR;
|
102
|
1645 }
|
|
1646
|
122
|
1647 if (ngx_http_subrequest(r, uri, &args, flags) != NGX_OK) {
|
102
|
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;
|
122
|
1661 ngx_str_t *var, *value, text;
|
48
|
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
|
122
|
1680 if (!vv->not_found) {
|
|
1681 text.data = vv->data;
|
|
1682 text.len = vv->len;
|
|
1683 value = &text;
|
102
|
1684 }
|
50
|
1685 }
|
46
|
1686
|
102
|
1687 if (value == NULL) {
|
48
|
1688 value = params[NGX_HTTP_SSI_ECHO_DEFAULT];
|
|
1689
|
|
1690 if (value == NULL) {
|
|
1691 value = &ngx_http_ssi_none;
|
|
1692
|
|
1693 } else if (value->len == 0) {
|
|
1694 return NGX_OK;
|
|
1695 }
|
|
1696
|
|
1697 } else {
|
|
1698 if (value->len == 0) {
|
|
1699 return NGX_OK;
|
|
1700 }
|
|
1701 }
|
|
1702
|
|
1703 b = ngx_calloc_buf(r->pool);
|
|
1704 if (b == NULL) {
|
|
1705 return NGX_HTTP_SSI_ERROR;
|
|
1706 }
|
|
1707
|
|
1708 cl = ngx_alloc_chain_link(r->pool);
|
|
1709 if (cl == NULL) {
|
46
|
1710 return NGX_HTTP_SSI_ERROR;
|
|
1711 }
|
|
1712
|
|
1713 b->memory = 1;
|
|
1714 b->pos = value->data;
|
|
1715 b->last = value->data + value->len;
|
|
1716
|
|
1717 cl->buf = b;
|
|
1718 cl->next = NULL;
|
|
1719 *ctx->last_out = cl;
|
|
1720 ctx->last_out = &cl->next;
|
|
1721
|
|
1722 return NGX_OK;
|
|
1723 }
|
|
1724
|
|
1725
|
58
|
1726 static ngx_int_t
|
|
1727 ngx_http_ssi_config(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1728 ngx_str_t **params)
|
|
1729 {
|
|
1730 ngx_str_t *value;
|
|
1731
|
|
1732 value = params[NGX_HTTP_SSI_CONFIG_TIMEFMT];
|
|
1733
|
|
1734 if (value) {
|
|
1735 ctx->timefmt = *value;
|
|
1736 }
|
|
1737
|
96
|
1738 value = params[NGX_HTTP_SSI_CONFIG_ERRMSG];
|
|
1739
|
|
1740 if (value) {
|
|
1741 ctx->errmsg = *value;
|
|
1742 }
|
|
1743
|
58
|
1744 return NGX_OK;
|
|
1745 }
|
|
1746
|
|
1747
|
|
1748 static ngx_int_t
|
102
|
1749 ngx_http_ssi_set(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
58
|
1750 ngx_str_t **params)
|
|
1751 {
|
102
|
1752 ngx_str_t *name, *value, *vv;
|
|
1753 ngx_http_ssi_var_t *var;
|
|
1754 ngx_http_ssi_ctx_t *mctx;
|
|
1755
|
|
1756 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
|
|
1757
|
|
1758 if (mctx->variables.elts == NULL) {
|
|
1759 if (ngx_array_init(&mctx->variables, r->pool, 4,
|
|
1760 sizeof(ngx_http_ssi_var_t)) != NGX_OK)
|
|
1761 {
|
58
|
1762 return NGX_HTTP_SSI_ERROR;
|
|
1763 }
|
|
1764 }
|
|
1765
|
102
|
1766 name = params[NGX_HTTP_SSI_SET_VAR];
|
|
1767 value = params[NGX_HTTP_SSI_SET_VALUE];
|
|
1768
|
|
1769 if (ngx_http_ssi_evaluate_string(r, ctx, value, 0) != NGX_OK) {
|
58
|
1770 return NGX_HTTP_SSI_ERROR;
|
|
1771 }
|
|
1772
|
102
|
1773 vv = ngx_http_ssi_get_variable(r, name);
|
|
1774
|
|
1775 if (vv) {
|
|
1776 *vv = *value;
|
|
1777 return NGX_OK;
|
|
1778 }
|
|
1779
|
|
1780 var = ngx_array_push(&mctx->variables);
|
|
1781 if (var == NULL) {
|
|
1782 return NGX_HTTP_SSI_ERROR;
|
|
1783 }
|
|
1784
|
|
1785 var->name = *name;
|
|
1786 var->value = *value;
|
|
1787
|
|
1788 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1789 "set: \"%V\"=\"%V\"", name, value);
|
|
1790
|
58
|
1791 return NGX_OK;
|
|
1792 }
|
|
1793
|
|
1794
|
|
1795 static ngx_int_t
|
|
1796 ngx_http_ssi_if(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1797 ngx_str_t **params)
|
|
1798 {
|
102
|
1799 u_char *p, *last;
|
|
1800 ngx_str_t *expr, left, right;
|
|
1801 ngx_int_t rc;
|
|
1802 ngx_uint_t negative, noregex;
|
100
|
1803 #if (NGX_PCRE)
|
102
|
1804 ngx_str_t err;
|
|
1805 ngx_regex_t *regex;
|
|
1806 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
100
|
1807 #endif
|
58
|
1808
|
|
1809 expr = params[NGX_HTTP_SSI_IF_EXPR];
|
|
1810
|
102
|
1811 left.data = expr->data;
|
100
|
1812 last = expr->data + expr->len;
|
|
1813
|
102
|
1814 for (p = left.data; p < last; p++) {
|
100
|
1815 if (*p >= 'A' && *p <= 'Z') {
|
|
1816 *p |= 0x20;
|
|
1817 continue;
|
|
1818 }
|
58
|
1819
|
102
|
1820 if ((*p >= 'a' && *p <= 'z')
|
|
1821 || (*p >= '0' && *p <= '9')
|
122
|
1822 || *p == '$' || *p == '{' || *p == '}' || *p == '_'
|
|
1823 || *p == '"' || *p == '\'')
|
102
|
1824 {
|
100
|
1825 continue;
|
|
1826 }
|
|
1827
|
|
1828 break;
|
|
1829 }
|
|
1830
|
102
|
1831 left.len = p - left.data;
|
100
|
1832
|
|
1833 while (p < last && *p == ' ') {
|
|
1834 p++;
|
58
|
1835 }
|
|
1836
|
102
|
1837 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1838 "left: \"%V\"", &left);
|
|
1839
|
|
1840 if (ngx_http_ssi_evaluate_string(r, ctx, &left, 0) != NGX_OK) {
|
58
|
1841 return NGX_HTTP_SSI_ERROR;
|
|
1842 }
|
|
1843
|
102
|
1844 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1845 "evaluted left: \"%V\"", &left);
|
|
1846
|
100
|
1847 if (p == last) {
|
102
|
1848 if (left.len) {
|
100
|
1849 ctx->output = 1;
|
|
1850
|
|
1851 } else {
|
|
1852 ctx->output = 0;
|
|
1853 }
|
|
1854
|
|
1855 return NGX_OK;
|
|
1856 }
|
|
1857
|
|
1858 if (p < last && *p == '=') {
|
|
1859 negative = 0;
|
|
1860 p++;
|
|
1861
|
|
1862 } else if (p + 1 < last && *p == '!' && *(p + 1) == '=') {
|
|
1863 negative = 1;
|
|
1864 p += 2;
|
|
1865
|
|
1866 } else {
|
|
1867 goto invalid_expression;
|
|
1868 }
|
|
1869
|
|
1870 while (p < last && *p == ' ') {
|
|
1871 p++;
|
|
1872 }
|
|
1873
|
|
1874 if (p < last && *p == '/') {
|
|
1875 if (*(last - 1) != '/') {
|
|
1876 goto invalid_expression;
|
|
1877 }
|
|
1878
|
|
1879 noregex = 0;
|
|
1880 last--;
|
|
1881 p++;
|
|
1882
|
|
1883 } else {
|
|
1884 noregex = 1;
|
|
1885 }
|
|
1886
|
|
1887 right.len = last - p;
|
|
1888 right.data = p;
|
|
1889
|
102
|
1890 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1891 "right: \"%V\"", &right);
|
|
1892
|
|
1893 if (ngx_http_ssi_evaluate_string(r, ctx, &right, 0) != NGX_OK) {
|
|
1894 return NGX_HTTP_SSI_ERROR;
|
100
|
1895 }
|
|
1896
|
102
|
1897 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1898 "evaluted right: \"%V\"", &right);
|
100
|
1899
|
|
1900 if (noregex) {
|
|
1901 if (left.len != right.len) {
|
|
1902 rc = -1;
|
|
1903
|
|
1904 } else {
|
|
1905 rc = ngx_strncmp(left.data, right.data, right.len);
|
|
1906 }
|
|
1907
|
|
1908 } else {
|
|
1909 #if (NGX_PCRE)
|
|
1910 err.len = NGX_MAX_CONF_ERRSTR;
|
|
1911 err.data = errstr;
|
|
1912
|
|
1913 right.data[right.len] = '\0';
|
|
1914
|
|
1915 regex = ngx_regex_compile(&right, 0, r->pool, &err);
|
|
1916
|
|
1917 if (regex == NULL) {
|
|
1918 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", err.data);
|
|
1919 return NGX_HTTP_SSI_ERROR;
|
|
1920 }
|
|
1921
|
|
1922 rc = ngx_regex_exec(regex, &left, NULL, 0);
|
|
1923
|
|
1924 if (rc != NGX_REGEX_NO_MATCHED && rc < 0) {
|
|
1925 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
1926 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
|
|
1927 rc, &left, &right);
|
|
1928 return NGX_HTTP_SSI_ERROR;
|
|
1929 }
|
|
1930 #else
|
|
1931 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
1932 "the using of the regex \"%V\" in SSI "
|
|
1933 "requires PCRE library", &right);
|
|
1934
|
|
1935 return NGX_HTTP_SSI_ERROR;
|
|
1936 #endif
|
|
1937 }
|
|
1938
|
|
1939 if ((rc == 0 && !negative) || (rc != 0 && negative)) {
|
58
|
1940 ctx->output = 1;
|
|
1941
|
|
1942 } else {
|
|
1943 ctx->output = 0;
|
|
1944 }
|
|
1945
|
|
1946 return NGX_OK;
|
100
|
1947
|
|
1948 invalid_expression:
|
|
1949
|
|
1950 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1951 "invalid expression in \"%V\"", expr);
|
|
1952
|
|
1953 return NGX_HTTP_SSI_ERROR;
|
58
|
1954 }
|
|
1955
|
|
1956
|
|
1957 static ngx_int_t
|
|
1958 ngx_http_ssi_else(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1959 ngx_str_t **params)
|
|
1960 {
|
|
1961 ctx->output = !ctx->output;
|
|
1962
|
|
1963 return NGX_OK;
|
|
1964 }
|
|
1965
|
|
1966
|
|
1967 static ngx_int_t
|
|
1968 ngx_http_ssi_endif(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
|
|
1969 ngx_str_t **params)
|
|
1970 {
|
|
1971 ctx->output = 1;
|
|
1972
|
|
1973 return NGX_OK;
|
|
1974 }
|
|
1975
|
|
1976
|
122
|
1977 static ngx_int_t
|
|
1978 ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
|
|
1979 ngx_http_variable_value_t *v, uintptr_t gmt)
|
58
|
1980 {
|
122
|
1981 ngx_http_ssi_ctx_t *ctx;
|
|
1982 ngx_time_t *tp;
|
|
1983 struct tm tm;
|
|
1984 char buf[NGX_HTTP_SSI_DATE_LEN];
|
|
1985
|
|
1986 v->valid = 1;
|
|
1987 v->no_cachable = 0;
|
|
1988 v->not_found = 0;
|
58
|
1989
|
112
|
1990 tp = ngx_timeofday();
|
|
1991
|
58
|
1992 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
|
|
1993
|
|
1994 if (ctx->timefmt.len == sizeof("%s") - 1
|
|
1995 && ctx->timefmt.data[0] == '%' && ctx->timefmt.data[1] == 's')
|
|
1996 {
|
122
|
1997 v->data = ngx_palloc(r->pool, NGX_TIME_T_LEN);
|
|
1998 if (v->data == NULL) {
|
|
1999 return NGX_ERROR;
|
58
|
2000 }
|
|
2001
|
122
|
2002 v->len = ngx_sprintf(v->data, "%T", tp->sec + (gmt ? 0 : tp->gmtoff))
|
|
2003 - v->data;
|
|
2004 return NGX_OK;
|
58
|
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
|
122
|
2013 v->len = strftime(buf, NGX_HTTP_SSI_DATE_LEN,
|
|
2014 (char *) ctx->timefmt.data, &tm);
|
|
2015 if (v->len == 0) {
|
|
2016 return NGX_ERROR;
|
58
|
2017 }
|
|
2018
|
122
|
2019 v->data = ngx_palloc(r->pool, v->len);
|
|
2020 if (v->data == NULL) {
|
|
2021 return NGX_ERROR;
|
58
|
2022 }
|
|
2023
|
122
|
2024 ngx_memcpy(v->data, buf, v->len);
|
|
2025
|
|
2026 return NGX_OK;
|
58
|
2027 }
|
|
2028
|
|
2029
|
76
|
2030 static char *
|
|
2031 ngx_http_ssi_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2032 {
|
|
2033 ngx_http_ssi_conf_t *scf = conf;
|
|
2034
|
|
2035 ngx_str_t *value, *type;
|
|
2036 ngx_uint_t i;
|
|
2037
|
|
2038 if (scf->types == NULL) {
|
|
2039 scf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
|
|
2040 if (scf->types == NULL) {
|
|
2041 return NGX_CONF_ERROR;
|
|
2042 }
|
|
2043
|
|
2044 type = ngx_array_push(scf->types);
|
|
2045 if (type == NULL) {
|
|
2046 return NGX_CONF_ERROR;
|
|
2047 }
|
|
2048
|
|
2049 type->len = sizeof("text/html") - 1;
|
|
2050 type->data = (u_char *) "text/html";
|
|
2051 }
|
|
2052
|
|
2053 value = cf->args->elts;
|
|
2054
|
|
2055 for (i = 1; i < cf->args->nelts; i++) {
|
|
2056
|
|
2057 if (ngx_strcmp(value[i].data, "text/html") == 0) {
|
|
2058 continue;
|
|
2059 }
|
|
2060
|
|
2061 type = ngx_array_push(scf->types);
|
|
2062 if (type == NULL) {
|
|
2063 return NGX_CONF_ERROR;
|
|
2064 }
|
|
2065
|
|
2066 type->len = value[i].len;
|
|
2067
|
|
2068 type->data = ngx_palloc(cf->pool, type->len + 1);
|
|
2069 if (type->data == NULL) {
|
|
2070 return NGX_CONF_ERROR;
|
|
2071 }
|
|
2072
|
|
2073 ngx_cpystrn(type->data, value[i].data, type->len + 1);
|
|
2074 }
|
|
2075
|
|
2076 return NGX_CONF_OK;
|
|
2077 }
|
|
2078
|
|
2079
|
58
|
2080 static ngx_int_t
|
|
2081 ngx_http_ssi_add_variables(ngx_conf_t *cf)
|
|
2082 {
|
|
2083 ngx_http_variable_t *var, *v;
|
|
2084
|
|
2085 for (v = ngx_http_ssi_vars; v->name.len; v++) {
|
|
2086 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2087 if (var == NULL) {
|
|
2088 return NGX_ERROR;
|
|
2089 }
|
|
2090
|
|
2091 var->handler = v->handler;
|
|
2092 var->data = v->data;
|
|
2093 }
|
|
2094
|
|
2095 return NGX_OK;
|
|
2096 }
|
|
2097
|
|
2098
|
46
|
2099 static void *
|
|
2100 ngx_http_ssi_create_conf(ngx_conf_t *cf)
|
|
2101 {
|
|
2102 ngx_http_ssi_conf_t *conf;
|
|
2103
|
48
|
2104 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_conf_t));
|
|
2105 if (conf == NULL) {
|
46
|
2106 return NGX_CONF_ERROR;
|
|
2107 }
|
|
2108
|
76
|
2109 /*
|
|
2110 * set by ngx_pcalloc():
|
|
2111 *
|
|
2112 * conf->types = NULL;
|
|
2113 */
|
|
2114
|
46
|
2115 conf->enable = NGX_CONF_UNSET;
|
|
2116 conf->silent_errors = NGX_CONF_UNSET;
|
58
|
2117 conf->ignore_recycled_buffers = NGX_CONF_UNSET;
|
46
|
2118
|
|
2119 conf->min_file_chunk = NGX_CONF_UNSET_SIZE;
|
|
2120 conf->value_len = NGX_CONF_UNSET_SIZE;
|
|
2121
|
|
2122 return conf;
|
|
2123 }
|
|
2124
|
|
2125
|
|
2126 static char *
|
|
2127 ngx_http_ssi_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2128 {
|
|
2129 ngx_http_ssi_conf_t *prev = parent;
|
|
2130 ngx_http_ssi_conf_t *conf = child;
|
|
2131
|
76
|
2132 ngx_str_t *type;
|
|
2133
|
46
|
2134 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
2135 ngx_conf_merge_value(conf->silent_errors, prev->silent_errors, 0);
|
58
|
2136 ngx_conf_merge_value(conf->ignore_recycled_buffers,
|
|
2137 prev->ignore_recycled_buffers, 0);
|
46
|
2138
|
|
2139 ngx_conf_merge_size_value(conf->min_file_chunk, prev->min_file_chunk, 1024);
|
|
2140 ngx_conf_merge_size_value(conf->value_len, prev->value_len, 256);
|
|
2141
|
76
|
2142 if (conf->types == NULL) {
|
|
2143 if (prev->types == NULL) {
|
|
2144 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
|
|
2145 if (conf->types == NULL) {
|
|
2146 return NGX_CONF_ERROR;
|
|
2147 }
|
|
2148
|
|
2149 type = ngx_array_push(conf->types);
|
|
2150 if (type == NULL) {
|
|
2151 return NGX_CONF_ERROR;
|
|
2152 }
|
|
2153
|
|
2154 type->len = sizeof("text/html") - 1;
|
|
2155 type->data = (u_char *) "text/html";
|
|
2156
|
|
2157 } else {
|
|
2158 conf->types = prev->types;
|
|
2159 }
|
|
2160 }
|
|
2161
|
46
|
2162 return NGX_CONF_OK;
|
|
2163 }
|
|
2164
|
|
2165
|
|
2166 static ngx_int_t
|
|
2167 ngx_http_ssi_filter_init(ngx_cycle_t *cycle)
|
|
2168 {
|
|
2169 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
2170 ngx_http_top_header_filter = ngx_http_ssi_header_filter;
|
|
2171
|
|
2172 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
2173 ngx_http_top_body_filter = ngx_http_ssi_body_filter;
|
|
2174
|
|
2175 return NGX_OK;
|
|
2176 }
|