3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
11 #define NGX_HTTP_SSI_ERROR 1
13 #define NGX_HTTP_SSI_DATE_LEN 2048
15 #define NGX_HTTP_SSI_ADD_PREFIX 1
16 #define NGX_HTTP_SSI_ADD_ZERO 2
17 #define NGX_HTTP_SSI_EXPR_TEST 4
22 ngx_flag_t silent_errors;
23 ngx_flag_t ignore_recycled_buffers;
27 size_t min_file_chunk;
30 ngx_array_t *types_keys;
31 } ngx_http_ssi_loc_conf_t;
45 } ngx_http_ssi_block_t;
60 ssi_double_quoted_value_state,
61 ssi_quoted_value_state,
62 ssi_quoted_symbol_state,
64 ssi_comment_end0_state,
65 ssi_comment_end1_state,
69 } ngx_http_ssi_state_e;
72 static ngx_int_t ngx_http_ssi_output(ngx_http_request_t *r,
73 ngx_http_ssi_ctx_t *ctx);
74 static ngx_int_t ngx_http_ssi_parse(ngx_http_request_t *r,
75 ngx_http_ssi_ctx_t *ctx);
76 static ngx_str_t *ngx_http_ssi_get_variable(ngx_http_request_t *r,
77 ngx_str_t *name, ngx_uint_t key);
78 static ngx_int_t ngx_http_ssi_evaluate_string(ngx_http_request_t *r,
79 ngx_http_ssi_ctx_t *ctx, ngx_str_t *text, ngx_uint_t flags);
81 static ngx_int_t ngx_http_ssi_include(ngx_http_request_t *r,
82 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
83 static ngx_int_t ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data,
85 static ngx_int_t ngx_http_ssi_set_variable(ngx_http_request_t *r, void *data,
87 static ngx_int_t ngx_http_ssi_echo(ngx_http_request_t *r,
88 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
89 static ngx_int_t ngx_http_ssi_config(ngx_http_request_t *r,
90 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
91 static ngx_int_t ngx_http_ssi_set(ngx_http_request_t *r,
92 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
93 static ngx_int_t ngx_http_ssi_if(ngx_http_request_t *r,
94 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
95 static ngx_int_t ngx_http_ssi_else(ngx_http_request_t *r,
96 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
97 static ngx_int_t ngx_http_ssi_endif(ngx_http_request_t *r,
98 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
99 static ngx_int_t ngx_http_ssi_block(ngx_http_request_t *r,
100 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
101 static ngx_int_t ngx_http_ssi_endblock(ngx_http_request_t *r,
102 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
104 static ngx_int_t ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
105 ngx_http_variable_value_t *v, uintptr_t gmt);
107 static ngx_int_t ngx_http_ssi_preconfiguration(ngx_conf_t *cf);
108 static void *ngx_http_ssi_create_main_conf(ngx_conf_t *cf);
109 static char *ngx_http_ssi_init_main_conf(ngx_conf_t *cf, void *conf);
110 static void *ngx_http_ssi_create_loc_conf(ngx_conf_t *cf);
111 static char *ngx_http_ssi_merge_loc_conf(ngx_conf_t *cf,
112 void *parent, void *child);
113 static ngx_int_t ngx_http_ssi_filter_init(ngx_conf_t *cf);
116 static ngx_command_t ngx_http_ssi_filter_commands[] = {
119 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
121 ngx_conf_set_flag_slot,
122 NGX_HTTP_LOC_CONF_OFFSET,
123 offsetof(ngx_http_ssi_loc_conf_t, enable),
126 { ngx_string("ssi_silent_errors"),
127 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
128 ngx_conf_set_flag_slot,
129 NGX_HTTP_LOC_CONF_OFFSET,
130 offsetof(ngx_http_ssi_loc_conf_t, silent_errors),
133 { ngx_string("ssi_ignore_recycled_buffers"),
134 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
135 ngx_conf_set_flag_slot,
136 NGX_HTTP_LOC_CONF_OFFSET,
137 offsetof(ngx_http_ssi_loc_conf_t, ignore_recycled_buffers),
140 { ngx_string("ssi_min_file_chunk"),
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
142 ngx_conf_set_size_slot,
143 NGX_HTTP_LOC_CONF_OFFSET,
144 offsetof(ngx_http_ssi_loc_conf_t, min_file_chunk),
147 { ngx_string("ssi_value_length"),
148 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
149 ngx_conf_set_size_slot,
150 NGX_HTTP_LOC_CONF_OFFSET,
151 offsetof(ngx_http_ssi_loc_conf_t, value_len),
154 { ngx_string("ssi_types"),
155 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
157 NGX_HTTP_LOC_CONF_OFFSET,
158 offsetof(ngx_http_ssi_loc_conf_t, types_keys),
159 &ngx_http_html_default_types[0] },
166 static ngx_http_module_t ngx_http_ssi_filter_module_ctx = {
167 ngx_http_ssi_preconfiguration, /* preconfiguration */
168 ngx_http_ssi_filter_init, /* postconfiguration */
170 ngx_http_ssi_create_main_conf, /* create main configuration */
171 ngx_http_ssi_init_main_conf, /* init main configuration */
173 NULL, /* create server configuration */
174 NULL, /* merge server configuration */
176 ngx_http_ssi_create_loc_conf, /* create location configuration */
177 ngx_http_ssi_merge_loc_conf /* merge location configuration */
181 ngx_module_t ngx_http_ssi_filter_module = {
183 &ngx_http_ssi_filter_module_ctx, /* module context */
184 ngx_http_ssi_filter_commands, /* module directives */
185 NGX_HTTP_MODULE, /* module type */
186 NULL, /* init master */
187 NULL, /* init module */
188 NULL, /* init process */
189 NULL, /* init thread */
190 NULL, /* exit thread */
191 NULL, /* exit process */
192 NULL, /* exit master */
193 NGX_MODULE_V1_PADDING
197 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
198 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
201 static u_char ngx_http_ssi_string[] = "<!--";
203 static ngx_str_t ngx_http_ssi_none = ngx_string("(none)");
204 static ngx_str_t ngx_http_ssi_null_string = ngx_null_string;
207 #define NGX_HTTP_SSI_INCLUDE_VIRTUAL 0
208 #define NGX_HTTP_SSI_INCLUDE_FILE 1
209 #define NGX_HTTP_SSI_INCLUDE_WAIT 2
210 #define NGX_HTTP_SSI_INCLUDE_SET 3
211 #define NGX_HTTP_SSI_INCLUDE_STUB 4
213 #define NGX_HTTP_SSI_ECHO_VAR 0
214 #define NGX_HTTP_SSI_ECHO_DEFAULT 1
215 #define NGX_HTTP_SSI_ECHO_ENCODING 2
217 #define NGX_HTTP_SSI_CONFIG_ERRMSG 0
218 #define NGX_HTTP_SSI_CONFIG_TIMEFMT 1
220 #define NGX_HTTP_SSI_SET_VAR 0
221 #define NGX_HTTP_SSI_SET_VALUE 1
223 #define NGX_HTTP_SSI_IF_EXPR 0
225 #define NGX_HTTP_SSI_BLOCK_NAME 0
228 static ngx_http_ssi_param_t ngx_http_ssi_include_params[] = {
229 { ngx_string("virtual"), NGX_HTTP_SSI_INCLUDE_VIRTUAL, 0, 0 },
230 { ngx_string("file"), NGX_HTTP_SSI_INCLUDE_FILE, 0, 0 },
231 { ngx_string("wait"), NGX_HTTP_SSI_INCLUDE_WAIT, 0, 0 },
232 { ngx_string("set"), NGX_HTTP_SSI_INCLUDE_SET, 0, 0 },
233 { ngx_string("stub"), NGX_HTTP_SSI_INCLUDE_STUB, 0, 0 },
234 { ngx_null_string, 0, 0, 0 }
238 static ngx_http_ssi_param_t ngx_http_ssi_echo_params[] = {
239 { ngx_string("var"), NGX_HTTP_SSI_ECHO_VAR, 1, 0 },
240 { ngx_string("default"), NGX_HTTP_SSI_ECHO_DEFAULT, 0, 0 },
241 { ngx_string("encoding"), NGX_HTTP_SSI_ECHO_ENCODING, 0, 0 },
242 { ngx_null_string, 0, 0, 0 }
246 static ngx_http_ssi_param_t ngx_http_ssi_config_params[] = {
247 { ngx_string("errmsg"), NGX_HTTP_SSI_CONFIG_ERRMSG, 0, 0 },
248 { ngx_string("timefmt"), NGX_HTTP_SSI_CONFIG_TIMEFMT, 0, 0 },
249 { ngx_null_string, 0, 0, 0 }
253 static ngx_http_ssi_param_t ngx_http_ssi_set_params[] = {
254 { ngx_string("var"), NGX_HTTP_SSI_SET_VAR, 1, 0 },
255 { ngx_string("value"), NGX_HTTP_SSI_SET_VALUE, 1, 0 },
256 { ngx_null_string, 0, 0, 0 }
260 static ngx_http_ssi_param_t ngx_http_ssi_if_params[] = {
261 { ngx_string("expr"), NGX_HTTP_SSI_IF_EXPR, 1, 0 },
262 { ngx_null_string, 0, 0, 0 }
266 static ngx_http_ssi_param_t ngx_http_ssi_block_params[] = {
267 { ngx_string("name"), NGX_HTTP_SSI_BLOCK_NAME, 1, 0 },
268 { ngx_null_string, 0, 0, 0 }
272 static ngx_http_ssi_param_t ngx_http_ssi_no_params[] = {
273 { ngx_null_string, 0, 0, 0 }
277 static ngx_http_ssi_command_t ngx_http_ssi_commands[] = {
278 { ngx_string("include"), ngx_http_ssi_include,
279 ngx_http_ssi_include_params, 0, 0, 1 },
280 { ngx_string("echo"), ngx_http_ssi_echo,
281 ngx_http_ssi_echo_params, 0, 0, 0 },
282 { ngx_string("config"), ngx_http_ssi_config,
283 ngx_http_ssi_config_params, 0, 0, 0 },
284 { ngx_string("set"), ngx_http_ssi_set, ngx_http_ssi_set_params, 0, 0, 0 },
286 { ngx_string("if"), ngx_http_ssi_if, ngx_http_ssi_if_params, 0, 0, 0 },
287 { ngx_string("elif"), ngx_http_ssi_if, ngx_http_ssi_if_params,
288 NGX_HTTP_SSI_COND_IF, 0, 0 },
289 { ngx_string("else"), ngx_http_ssi_else, ngx_http_ssi_no_params,
290 NGX_HTTP_SSI_COND_IF, 0, 0 },
291 { ngx_string("endif"), ngx_http_ssi_endif, ngx_http_ssi_no_params,
292 NGX_HTTP_SSI_COND_ELSE, 0, 0 },
294 { ngx_string("block"), ngx_http_ssi_block,
295 ngx_http_ssi_block_params, 0, 0, 0 },
296 { ngx_string("endblock"), ngx_http_ssi_endblock,
297 ngx_http_ssi_no_params, 0, 1, 0 },
299 { ngx_null_string, NULL, NULL, 0, 0, 0 }
303 static ngx_http_variable_t ngx_http_ssi_vars[] = {
305 { ngx_string("date_local"), NULL, ngx_http_ssi_date_gmt_local_variable, 0,
306 NGX_HTTP_VAR_NOCACHEABLE, 0 },
308 { ngx_string("date_gmt"), NULL, ngx_http_ssi_date_gmt_local_variable, 1,
309 NGX_HTTP_VAR_NOCACHEABLE, 0 },
311 { ngx_null_string, NULL, NULL, 0, 0, 0 }
317 ngx_http_ssi_header_filter(ngx_http_request_t *r)
319 ngx_http_ssi_ctx_t *ctx;
320 ngx_http_ssi_loc_conf_t *slcf;
322 slcf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
325 || r->headers_out.content_length_n == 0
326 || ngx_http_test_content_type(r, &slcf->types) == NULL)
328 return ngx_http_next_header_filter(r);
331 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t));
336 ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module);
339 ctx->value_len = slcf->value_len;
340 ctx->last_out = &ctx->out;
342 ctx->encoding = NGX_HTTP_SSI_ENTITY_ENCODING;
345 ctx->params.elts = ctx->params_array;
346 ctx->params.size = sizeof(ngx_table_elt_t);
347 ctx->params.nalloc = NGX_HTTP_SSI_PARAMS_N;
348 ctx->params.pool = r->pool;
350 ctx->timefmt.len = sizeof("%A, %d-%b-%Y %H:%M:%S %Z") - 1;
351 ctx->timefmt.data = (u_char *) "%A, %d-%b-%Y %H:%M:%S %Z";
354 sizeof("[an error occurred while processing the directive]") - 1;
355 ctx->errmsg.data = (u_char *)
356 "[an error occurred while processing the directive]";
358 r->filter_need_in_memory = 1;
361 ngx_http_clear_content_length(r);
362 ngx_http_clear_last_modified(r);
365 return ngx_http_next_header_filter(r);
370 ngx_http_ssi_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
376 ngx_chain_t *cl, **ll;
377 ngx_table_elt_t *param;
378 ngx_http_ssi_ctx_t *ctx, *mctx;
379 ngx_http_ssi_block_t *bl;
380 ngx_http_ssi_param_t *prm;
381 ngx_http_ssi_command_t *cmd;
382 ngx_http_ssi_loc_conf_t *slcf;
383 ngx_http_ssi_main_conf_t *smcf;
384 ngx_str_t *params[NGX_HTTP_SSI_MAX_PARAMS + 1];
386 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
392 && ctx->busy == NULL))
394 return ngx_http_next_body_filter(r, in);
397 /* add the incoming chain to the chain ctx->in */
400 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
405 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
406 "http ssi filter \"%V?%V\"", &r->uri, &r->args);
410 if (r != r->connection->data) {
411 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
412 "http ssi filter wait \"%V?%V\" non-active",
413 &ctx->wait->uri, &ctx->wait->args);
418 if (ctx->wait->done) {
419 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
420 "http ssi filter wait \"%V?%V\" done",
421 &ctx->wait->uri, &ctx->wait->args);
426 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
427 "http ssi filter wait \"%V?%V\"",
428 &ctx->wait->uri, &ctx->wait->args);
430 return ngx_http_next_body_filter(r, NULL);
434 slcf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
436 while (ctx->in || ctx->buf) {
438 if (ctx->buf == NULL ){
439 ctx->buf = ctx->in->buf;
440 ctx->in = ctx->in->next;
441 ctx->pos = ctx->buf->pos;
444 if (ctx->state == ssi_start_state) {
445 ctx->copy_start = ctx->pos;
446 ctx->copy_end = ctx->pos;
451 while (ctx->pos < ctx->buf->last) {
453 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
454 "saved: %d state: %d", ctx->saved, ctx->state);
456 rc = ngx_http_ssi_parse(r, ctx);
458 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
459 "parse: %d, looked: %d %p-%p",
460 rc, ctx->looked, ctx->copy_start, ctx->copy_end);
462 if (rc == NGX_ERROR) {
466 if (ctx->copy_start != ctx->copy_end) {
470 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
471 "saved: %d", ctx->saved);
477 ctx->free = ctx->free->next;
479 ngx_memzero(b, sizeof(ngx_buf_t));
482 b = ngx_calloc_buf(r->pool);
487 cl = ngx_alloc_chain_link(r->pool);
496 b->pos = ngx_http_ssi_string;
497 b->last = ngx_http_ssi_string + ctx->saved;
500 ctx->last_out = &cl->next;
507 ctx->free = ctx->free->next;
511 b = ngx_alloc_buf(r->pool);
516 cl = ngx_alloc_chain_link(r->pool);
524 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
526 b->pos = ctx->copy_start;
527 b->last = ctx->copy_end;
533 if (slcf->min_file_chunk < (size_t) (b->last - b->pos))
535 b->file_last = b->file_pos
536 + (b->last - ctx->buf->pos);
537 b->file_pos += b->pos - ctx->buf->pos;
546 ctx->last_out = &cl->next;
550 && ctx->saved + (ctx->copy_end - ctx->copy_start))
552 b = ngx_create_temp_buf(r->pool,
553 ctx->saved + (ctx->copy_end - ctx->copy_start));
560 b->last = ngx_cpymem(b->pos, ngx_http_ssi_string,
564 b->last = ngx_cpymem(b->last, ctx->copy_start,
565 ctx->copy_end - ctx->copy_start);
567 cl = ngx_alloc_chain_link(r->pool);
577 mctx = ngx_http_get_module_ctx(r->main,
578 ngx_http_ssi_filter_module);
579 bl = mctx->blocks->elts;
580 for (ll = &bl[mctx->blocks->nelts - 1].bufs;
594 if (ctx->state == ssi_start_state) {
595 ctx->copy_start = ctx->pos;
596 ctx->copy_end = ctx->pos;
599 ctx->copy_start = NULL;
600 ctx->copy_end = NULL;
603 if (rc == NGX_AGAIN) {
612 smcf = ngx_http_get_module_main_conf(r,
613 ngx_http_ssi_filter_module);
615 cmd = ngx_hash_find(&smcf->hash, ctx->key, ctx->command.data,
620 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
621 "invalid SSI command: \"%V\"",
630 && (ctx->conditional == 0
631 || ctx->conditional > cmd->conditional))
633 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
634 "invalid context of SSI command: \"%V\"",
639 if (!ctx->output && !cmd->block) {
643 /* reconstruct the SSI command text */
645 len = 5 + ctx->command.len + 4;
647 param = ctx->params.elts;
648 for (i = 0; i < ctx->params.nelts; i++) {
649 len += 1 + param[i].key.len + 2
650 + param[i].value.len + 1;
653 b = ngx_create_temp_buf(r->pool, len);
659 cl = ngx_alloc_chain_link(r->pool);
673 b->last = ngx_cpymem(b->last, ctx->command.data,
676 for (i = 0; i < ctx->params.nelts; i++) {
678 b->last = ngx_cpymem(b->last, param[i].key.data,
682 b->last = ngx_cpymem(b->last, param[i].value.data,
692 mctx = ngx_http_get_module_ctx(r->main,
693 ngx_http_ssi_filter_module);
694 bl = mctx->blocks->elts;
695 for (ll = &bl[mctx->blocks->nelts - 1].bufs;
709 if (cmd->conditional == 0) {
714 if (ctx->params.nelts > NGX_HTTP_SSI_MAX_PARAMS) {
715 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
716 "too many SSI command paramters: \"%V\"",
722 (NGX_HTTP_SSI_MAX_PARAMS + 1) * sizeof(ngx_str_t *));
724 param = ctx->params.elts;
726 for (i = 0; i < ctx->params.nelts; i++) {
728 for (prm = cmd->params; prm->name.len; prm++) {
730 if (param[i].key.len != prm->name.len
731 || ngx_strncmp(param[i].key.data, prm->name.data,
737 if (!prm->multiple) {
738 if (params[prm->index]) {
739 ngx_log_error(NGX_LOG_ERR,
740 r->connection->log, 0,
741 "duplicate \"%V\" parameter "
742 "in \"%V\" SSI command",
743 ¶m[i].key, &ctx->command);
748 params[prm->index] = ¶m[i].value;
753 for (index = prm->index; params[index]; index++) {
757 params[index] = ¶m[i].value;
762 if (prm->name.len == 0) {
763 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
764 "invalid parameter name: \"%V\" "
765 "in \"%V\" SSI command",
766 ¶m[i].key, &ctx->command);
772 for (prm = cmd->params; prm->name.len; prm++) {
773 if (prm->mandatory && params[prm->index] == 0) {
774 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
775 "mandatory \"%V\" parameter is absent "
776 "in \"%V\" SSI command",
777 &prm->name, &ctx->command);
783 if (cmd->flush && ctx->out) {
785 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
788 if (ngx_http_ssi_output(r, ctx) == NGX_ERROR) {
793 rc = cmd->handler(r, ctx, params);
799 if (rc == NGX_DONE || rc == NGX_AGAIN || rc == NGX_ERROR) {
805 /* rc == NGX_HTTP_SSI_ERROR */
809 if (slcf->silent_errors) {
815 ctx->free = ctx->free->next;
817 ngx_memzero(b, sizeof(ngx_buf_t));
820 b = ngx_calloc_buf(r->pool);
825 cl = ngx_alloc_chain_link(r->pool);
834 b->pos = ctx->errmsg.data;
835 b->last = ctx->errmsg.data + ctx->errmsg.len;
839 ctx->last_out = &cl->next;
844 if (ctx->buf->last_buf || ngx_buf_in_memory(ctx->buf)) {
848 ctx->free = ctx->free->next;
850 ngx_memzero(b, sizeof(ngx_buf_t));
853 b = ngx_calloc_buf(r->pool);
858 cl = ngx_alloc_chain_link(r->pool);
870 ctx->last_out = &cl->next;
873 b->last_buf = ctx->buf->last_buf;
874 b->shadow = ctx->buf;
876 if (slcf->ignore_recycled_buffers == 0) {
877 b->recycled = ctx->buf->recycled;
883 ctx->saved = ctx->looked;
886 if (ctx->out == NULL && ctx->busy == NULL) {
890 return ngx_http_ssi_output(r, ctx);
895 ngx_http_ssi_output(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
903 for (cl = ctx->out; cl; cl = cl->next) {
904 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
905 "ssi out: %p %p", cl->buf, cl->buf->pos);
907 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
908 "the same buf was used in ssi");
916 rc = ngx_http_next_body_filter(r, ctx->out);
918 if (ctx->busy == NULL) {
919 ctx->busy = ctx->out;
922 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
927 ctx->last_out = &ctx->out;
934 if (ngx_buf_size(b) != 0) {
939 b->shadow->pos = b->shadow->last;
942 ctx->busy = cl->next;
944 if (ngx_buf_in_memory(b) || b->in_file) {
945 /* add data bufs only to the free buf chain */
947 cl->next = ctx->free;
952 if (ctx->in || ctx->buf) {
953 r->buffered |= NGX_HTTP_SSI_BUFFERED;
956 r->buffered &= ~NGX_HTTP_SSI_BUFFERED;
964 ngx_http_ssi_parse(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
966 u_char *p, *value, *last, *copy_end, ch;
968 ngx_http_ssi_state_e state;
971 looked = ctx->looked;
972 last = ctx->buf->last;
973 copy_end = ctx->copy_end;
975 for (p = ctx->pos; p < last; p++) {
979 if (state == ssi_start_state) {
987 state = ssi_tag_state;
1001 ctx->looked = looked;
1004 if (ctx->copy_start == NULL) {
1005 ctx->copy_start = ctx->buf->pos;
1017 case ssi_start_state:
1024 state = ssi_comment0_state;
1034 state = ssi_start_state;
1040 case ssi_comment0_state:
1044 state = ssi_comment1_state;
1050 state = ssi_tag_state;
1056 state = ssi_start_state;
1062 case ssi_comment1_state:
1066 state = ssi_sharp_state;
1072 state = ssi_tag_state;
1078 state = ssi_start_state;
1084 case ssi_sharp_state:
1087 if (p - ctx->pos < 4) {
1091 state = ssi_precommand_state;
1097 state = ssi_tag_state;
1103 state = ssi_start_state;
1109 case ssi_precommand_state:
1118 ctx->command.len = 1;
1119 ctx->command.data = ngx_pnalloc(r->pool,
1120 NGX_HTTP_SSI_COMMAND_LEN);
1121 if (ctx->command.data == NULL) {
1125 ctx->command.data[0] = ch;
1128 ctx->key = ngx_hash(ctx->key, ch);
1130 ctx->params.nelts = 0;
1132 state = ssi_command_state;
1138 case ssi_command_state:
1144 state = ssi_preparam_state;
1148 state = ssi_comment_end0_state;
1152 if (ctx->command.len == NGX_HTTP_SSI_COMMAND_LEN) {
1153 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1154 "the \"%V%c...\" SSI command is too long",
1157 state = ssi_error_state;
1161 ctx->command.data[ctx->command.len++] = ch;
1162 ctx->key = ngx_hash(ctx->key, ch);
1167 case ssi_preparam_state:
1176 state = ssi_comment_end0_state;
1180 ctx->param = ngx_array_push(&ctx->params);
1181 if (ctx->param == NULL) {
1185 ctx->param->key.len = 1;
1186 ctx->param->key.data = ngx_pnalloc(r->pool,
1187 NGX_HTTP_SSI_PARAM_LEN);
1188 if (ctx->param->key.data == NULL) {
1192 ctx->param->key.data[0] = ch;
1194 ctx->param->value.len = 0;
1196 if (ctx->value_buf == NULL) {
1197 ctx->param->value.data = ngx_pnalloc(r->pool,
1199 if (ctx->param->value.data == NULL) {
1204 ctx->param->value.data = ctx->value_buf;
1207 state = ssi_param_state;
1213 case ssi_param_state:
1219 state = ssi_preequal_state;
1223 state = ssi_prevalue_state;
1227 state = ssi_error_end0_state;
1229 ctx->param->key.data[ctx->param->key.len++] = ch;
1230 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1231 "invalid \"%V\" parameter in \"%V\" SSI command",
1232 &ctx->param->key, &ctx->command);
1236 if (ctx->param->key.len == NGX_HTTP_SSI_PARAM_LEN) {
1237 state = ssi_error_state;
1238 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1239 "too long \"%V%c...\" parameter in "
1240 "\"%V\" SSI command",
1241 &ctx->param->key, ch, &ctx->command);
1245 ctx->param->key.data[ctx->param->key.len++] = ch;
1250 case ssi_preequal_state:
1259 state = ssi_prevalue_state;
1264 state = ssi_error_end0_state;
1266 state = ssi_error_state;
1269 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1270 "unexpected \"%c\" symbol after \"%V\" "
1271 "parameter in \"%V\" SSI command",
1272 ch, &ctx->param->key, &ctx->command);
1278 case ssi_prevalue_state:
1287 state = ssi_double_quoted_value_state;
1291 state = ssi_quoted_value_state;
1296 state = ssi_error_end0_state;
1298 state = ssi_error_state;
1301 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1302 "unexpected \"%c\" symbol before value of "
1303 "\"%V\" parameter in \"%V\" SSI command",
1304 ch, &ctx->param->key, &ctx->command);
1310 case ssi_double_quoted_value_state:
1313 state = ssi_postparam_state;
1317 ctx->saved_state = ssi_double_quoted_value_state;
1318 state = ssi_quoted_symbol_state;
1323 if (ctx->param->value.len == ctx->value_len) {
1324 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1325 "too long \"%V%c...\" value of \"%V\" "
1326 "parameter in \"%V\" SSI command",
1327 &ctx->param->value, ch, &ctx->param->key,
1329 state = ssi_error_state;
1333 ctx->param->value.data[ctx->param->value.len++] = ch;
1338 case ssi_quoted_value_state:
1341 state = ssi_postparam_state;
1345 ctx->saved_state = ssi_quoted_value_state;
1346 state = ssi_quoted_symbol_state;
1351 if (ctx->param->value.len == ctx->value_len) {
1352 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1353 "too long \"%V%c...\" value of \"%V\" "
1354 "parameter in \"%V\" SSI command",
1355 &ctx->param->value, ch, &ctx->param->key,
1357 state = ssi_error_state;
1361 ctx->param->value.data[ctx->param->value.len++] = ch;
1366 case ssi_quoted_symbol_state:
1367 state = ctx->saved_state;
1369 ctx->param->value.data[ctx->param->value.len++] = ch;
1373 case ssi_postparam_state:
1375 if (ctx->param->value.len + 1 < ctx->value_len / 2) {
1376 value = ngx_pnalloc(r->pool, ctx->param->value.len + 1);
1377 if (value == NULL) {
1381 ngx_memcpy(value, ctx->param->value.data,
1382 ctx->param->value.len);
1384 ctx->value_buf = ctx->param->value.data;
1385 ctx->param->value.data = value;
1388 ctx->value_buf = NULL;
1396 state = ssi_preparam_state;
1400 state = ssi_comment_end0_state;
1404 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1405 "unexpected \"%c\" symbol after \"%V\" value "
1406 "of \"%V\" parameter in \"%V\" SSI command",
1407 ch, &ctx->param->value, &ctx->param->key,
1409 state = ssi_error_state;
1415 case ssi_comment_end0_state:
1418 state = ssi_comment_end1_state;
1422 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1423 "unexpected \"%c\" symbol in \"%V\" SSI command",
1425 state = ssi_error_state;
1431 case ssi_comment_end1_state:
1434 ctx->state = ssi_start_state;
1436 ctx->looked = looked;
1437 ctx->copy_end = copy_end;
1439 if (ctx->copy_start == NULL && copy_end) {
1440 ctx->copy_start = ctx->buf->pos;
1446 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1447 "unexpected \"%c\" symbol in \"%V\" SSI command",
1449 state = ssi_error_state;
1455 case ssi_error_state:
1458 state = ssi_error_end0_state;
1467 case ssi_error_end0_state:
1470 state = ssi_error_end1_state;
1474 state = ssi_error_state;
1480 case ssi_error_end1_state:
1483 ctx->state = ssi_start_state;
1485 ctx->looked = looked;
1486 ctx->copy_end = copy_end;
1488 if (ctx->copy_start == NULL && copy_end) {
1489 ctx->copy_start = ctx->buf->pos;
1492 return NGX_HTTP_SSI_ERROR;
1495 state = ssi_error_state;
1505 ctx->looked = looked;
1507 ctx->copy_end = (state == ssi_start_state) ? p : copy_end;
1509 if (ctx->copy_start == NULL && ctx->copy_end) {
1510 ctx->copy_start = ctx->buf->pos;
1518 ngx_http_ssi_get_variable(ngx_http_request_t *r, ngx_str_t *name,
1522 ngx_list_part_t *part;
1523 ngx_http_ssi_var_t *var;
1524 ngx_http_ssi_ctx_t *ctx;
1526 ctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
1528 if (ctx->variables == NULL) {
1532 part = &ctx->variables->part;
1535 for (i = 0; /* void */ ; i++) {
1537 if (i >= part->nelts) {
1538 if (part->next == NULL) {
1547 if (name->len != var[i].name.len) {
1551 if (key != var[i].key) {
1555 if (ngx_strncmp(name->data, var[i].name.data, name->len) == 0) {
1556 return &var[i].value;
1565 ngx_http_ssi_evaluate_string(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
1566 ngx_str_t *text, ngx_uint_t flags)
1568 u_char ch, *p, **value, *data, *part_data;
1569 size_t *size, len, prefix, part_len;
1570 ngx_str_t var, *val;
1572 ngx_uint_t i, n, bracket, quoted;
1573 ngx_array_t lengths, values;
1574 ngx_http_variable_value_t *vv;
1576 n = ngx_http_script_variables_count(text);
1583 if ((flags & NGX_HTTP_SSI_ADD_PREFIX) && text->data[0] != '/') {
1585 for (prefix = r->uri.len; prefix; prefix--) {
1586 if (r->uri.data[prefix - 1] == '/') {
1592 len = prefix + text->len;
1594 data = ngx_pnalloc(r->pool, len);
1599 p = ngx_copy(data, r->uri.data, prefix);
1605 for (i = 0; i < text->len; i++) {
1618 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
1626 text->len = p - data;
1632 if (ngx_array_init(&lengths, r->pool, 8, sizeof(size_t *)) != NGX_OK) {
1636 if (ngx_array_init(&values, r->pool, 8, sizeof(u_char *)) != NGX_OK) {
1643 while (i < text->len) {
1645 if (text->data[i] == '$') {
1649 if (++i == text->len) {
1650 goto invalid_variable;
1653 if (text->data[i] == '{') {
1656 if (++i == text->len) {
1657 goto invalid_variable;
1660 var.data = &text->data[i];
1664 var.data = &text->data[i];
1667 for ( /* void */ ; i < text->len; i++, var.len++) {
1670 if (ch == '}' && bracket) {
1676 if ((ch >= 'A' && ch <= 'Z')
1677 || (ch >= 'a' && ch <= 'z')
1678 || (ch >= '0' && ch <= '9')
1688 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1689 "the closing bracket in \"%V\" "
1690 "variable is missing", &var);
1691 return NGX_HTTP_SSI_ERROR;
1695 goto invalid_variable;
1698 key = ngx_hash_strlow(var.data, var.data, var.len);
1700 val = ngx_http_ssi_get_variable(r, &var, key);
1703 vv = ngx_http_get_variable(r, &var, key,
1704 flags & NGX_HTTP_SSI_EXPR_TEST);
1709 if (vv->not_found) {
1713 part_data = vv->data;
1717 part_data = val->data;
1718 part_len = val->len;
1722 part_data = &text->data[i];
1725 for (p = part_data; i < text->len; i++) {
1742 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
1750 part_len = p - part_data;
1755 size = ngx_array_push(&lengths);
1762 value = ngx_array_push(&values);
1763 if (value == NULL) {
1772 size = lengths.elts;
1773 value = values.elts;
1775 if (flags & NGX_HTTP_SSI_ADD_PREFIX) {
1776 for (i = 0; i < values.nelts; i++) {
1778 if (*value[i] != '/') {
1779 for (prefix = r->uri.len; prefix; prefix--) {
1780 if (r->uri.data[prefix - 1] == '/') {
1792 p = ngx_pnalloc(r->pool, len + ((flags & NGX_HTTP_SSI_ADD_ZERO) ? 1 : 0));
1800 p = ngx_copy(p, r->uri.data, prefix);
1802 for (i = 0; i < values.nelts; i++) {
1803 p = ngx_copy(p, value[i], size[i]);
1810 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1811 "invalid variable name in \"%V\"", text);
1813 return NGX_HTTP_SSI_ERROR;
1818 ngx_http_ssi_include(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
1824 ngx_str_t *uri, *file, *wait, *set, *stub, args;
1826 ngx_uint_t flags, i;
1827 ngx_chain_t *cl, *tl, **ll, *out;
1828 ngx_http_request_t *sr;
1829 ngx_http_ssi_var_t *var;
1830 ngx_http_ssi_ctx_t *mctx;
1831 ngx_http_ssi_block_t *bl;
1832 ngx_http_post_subrequest_t *psr;
1834 uri = params[NGX_HTTP_SSI_INCLUDE_VIRTUAL];
1835 file = params[NGX_HTTP_SSI_INCLUDE_FILE];
1836 wait = params[NGX_HTTP_SSI_INCLUDE_WAIT];
1837 set = params[NGX_HTTP_SSI_INCLUDE_SET];
1838 stub = params[NGX_HTTP_SSI_INCLUDE_STUB];
1841 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1842 "inlcusion may be either virtual=\"%V\" or file=\"%V\"",
1844 return NGX_HTTP_SSI_ERROR;
1847 if (uri == NULL && file == NULL) {
1848 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1849 "no parameter in \"include\" SSI command");
1850 return NGX_HTTP_SSI_ERROR;
1854 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1855 "\"set\" and \"stub\" may not be used together "
1856 "in \"include\" SSI command");
1857 return NGX_HTTP_SSI_ERROR;
1862 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1863 "\"wait\" may not be used with file=\"%V\"", file);
1864 return NGX_HTTP_SSI_ERROR;
1868 && ngx_strncasecmp(wait->data, (u_char *) "no", 2) == 0)
1872 } else if (wait->len != 3
1873 || ngx_strncasecmp(wait->data, (u_char *) "yes", 3) != 0)
1875 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1876 "invalid value \"%V\" in the \"wait\" parameter",
1878 return NGX_HTTP_SSI_ERROR;
1884 wait = (ngx_str_t *) -1;
1887 rc = ngx_http_ssi_evaluate_string(r, ctx, uri, NGX_HTTP_SSI_ADD_PREFIX);
1896 ngx_unescape_uri(&dst, &src, uri->len, NGX_UNESCAPE_URI);
1898 len = (uri->data + uri->len) - src;
1900 dst = ngx_copy(dst, src, len);
1903 uri->len = dst - uri->data;
1905 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1906 "ssi include: \"%V\"", uri);
1912 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
1913 return NGX_HTTP_SSI_ERROR;
1918 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
1922 bl = mctx->blocks->elts;
1923 for (i = 0; i < mctx->blocks->nelts; i++) {
1924 if (stub->len == bl[i].name.len
1925 && ngx_strncmp(stub->data, bl[i].name.data, stub->len) == 0)
1932 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1933 "\"stub\"=\"%V\" for \"include\" not found", stub);
1934 return NGX_HTTP_SSI_ERROR;
1938 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
1943 psr->handler = ngx_http_ssi_stub_output;
1945 if (bl[i].count++) {
1950 for (tl = bl[i].bufs; tl; tl = tl->next) {
1954 ctx->free = ctx->free->next;
1958 b = ngx_alloc_buf(r->pool);
1963 cl = ngx_alloc_chain_link(r->pool);
1971 ngx_memcpy(b, tl->buf, sizeof(ngx_buf_t));
1983 psr->data = bl[i].bufs;
1988 flags |= NGX_HTTP_SUBREQUEST_WAITED;
1992 key = ngx_hash_strlow(set->data, set->data, set->len);
1994 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
1999 psr->handler = ngx_http_ssi_set_variable;
2000 psr->data = ngx_http_ssi_get_variable(r, set, key);
2002 if (psr->data == NULL) {
2004 if (mctx->variables == NULL) {
2005 mctx->variables = ngx_list_create(r->pool, 4,
2006 sizeof(ngx_http_ssi_var_t));
2007 if (mctx->variables == NULL) {
2012 var = ngx_list_push(mctx->variables);
2019 var->value = ngx_http_ssi_null_string;
2020 psr->data = &var->value;
2023 flags |= NGX_HTTP_SUBREQUEST_IN_MEMORY|NGX_HTTP_SUBREQUEST_WAITED;
2026 if (ngx_http_subrequest(r, uri, &args, &sr, psr, flags) != NGX_OK) {
2027 return NGX_HTTP_SSI_ERROR;
2030 if (wait == NULL && set == NULL) {
2034 if (ctx->wait == NULL) {
2040 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2041 "only one subrequest may be waited at the same time");
2049 ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc)
2053 if (rc == NGX_ERROR || r->connection->error || r->request_output) {
2057 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2058 "ssi stub output: \"%V?%V\"", &r->uri, &r->args);
2062 if (!r->header_sent) {
2063 if (ngx_http_set_content_type(r) == NGX_ERROR) {
2067 if (ngx_http_send_header(r) == NGX_ERROR) {
2072 return ngx_http_output_filter(r, out);
2077 ngx_http_ssi_set_variable(ngx_http_request_t *r, void *data, ngx_int_t rc)
2079 ngx_str_t *value = data;
2082 value->len = r->upstream->buffer.last - r->upstream->buffer.pos;
2083 value->data = r->upstream->buffer.pos;
2091 ngx_http_ssi_echo(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2098 ngx_str_t *var, *value, *enc, text;
2100 ngx_http_variable_value_t *vv;
2102 var = params[NGX_HTTP_SSI_ECHO_VAR];
2104 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2105 "ssi echo \"%V\"", var);
2107 key = ngx_hash_strlow(var->data, var->data, var->len);
2109 value = ngx_http_ssi_get_variable(r, var, key);
2111 if (value == NULL) {
2112 vv = ngx_http_get_variable(r, var, key, 1);
2115 return NGX_HTTP_SSI_ERROR;
2118 if (!vv->not_found) {
2119 text.data = vv->data;
2125 if (value == NULL) {
2126 value = params[NGX_HTTP_SSI_ECHO_DEFAULT];
2128 if (value == NULL) {
2129 value = &ngx_http_ssi_none;
2131 } else if (value->len == 0) {
2136 if (value->len == 0) {
2141 enc = params[NGX_HTTP_SSI_ECHO_ENCODING];
2144 if (enc->len == 4 && ngx_strncmp(enc->data, "none", 4) == 0) {
2146 ctx->encoding = NGX_HTTP_SSI_NO_ENCODING;
2148 } else if (enc->len == 3 && ngx_strncmp(enc->data, "url", 3) == 0) {
2150 ctx->encoding = NGX_HTTP_SSI_URL_ENCODING;
2152 } else if (enc->len == 6 && ngx_strncmp(enc->data, "entity", 6) == 0) {
2154 ctx->encoding = NGX_HTTP_SSI_ENTITY_ENCODING;
2157 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2158 "unknown encoding \"%V\" in the \"echo\" command",
2163 switch (ctx->encoding) {
2165 case NGX_HTTP_SSI_NO_ENCODING:
2168 case NGX_HTTP_SSI_URL_ENCODING:
2169 len = 2 * ngx_escape_uri(NULL, value->data, value->len,
2173 p = ngx_pnalloc(r->pool, value->len + len);
2175 return NGX_HTTP_SSI_ERROR;
2178 (void) ngx_escape_uri(p, value->data, value->len, NGX_ESCAPE_HTML);
2186 case NGX_HTTP_SSI_ENTITY_ENCODING:
2187 len = ngx_escape_html(NULL, value->data, value->len);
2190 p = ngx_pnalloc(r->pool, value->len + len);
2192 return NGX_HTTP_SSI_ERROR;
2195 (void) ngx_escape_html(p, value->data, value->len);
2204 b = ngx_calloc_buf(r->pool);
2206 return NGX_HTTP_SSI_ERROR;
2209 cl = ngx_alloc_chain_link(r->pool);
2211 return NGX_HTTP_SSI_ERROR;
2215 b->pos = value->data;
2216 b->last = value->data + value->len;
2220 *ctx->last_out = cl;
2221 ctx->last_out = &cl->next;
2228 ngx_http_ssi_config(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2233 value = params[NGX_HTTP_SSI_CONFIG_TIMEFMT];
2236 ctx->timefmt.len = value->len;
2237 ctx->timefmt.data = ngx_pnalloc(r->pool, value->len + 1);
2238 if (ctx->timefmt.data == NULL) {
2239 return NGX_HTTP_SSI_ERROR;
2242 ngx_cpystrn(ctx->timefmt.data, value->data, value->len + 1);
2245 value = params[NGX_HTTP_SSI_CONFIG_ERRMSG];
2248 ctx->errmsg = *value;
2256 ngx_http_ssi_set(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2260 ngx_str_t *name, *value, *vv;
2261 ngx_http_ssi_var_t *var;
2262 ngx_http_ssi_ctx_t *mctx;
2264 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
2266 if (mctx->variables == NULL) {
2267 mctx->variables = ngx_list_create(r->pool, 4,
2268 sizeof(ngx_http_ssi_var_t));
2269 if (mctx->variables == NULL) {
2274 name = params[NGX_HTTP_SSI_SET_VAR];
2275 value = params[NGX_HTTP_SSI_SET_VALUE];
2277 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2278 "ssi set \"%V\" \"%V\"", name, value);
2280 rc = ngx_http_ssi_evaluate_string(r, ctx, value, 0);
2286 key = ngx_hash_strlow(name->data, name->data, name->len);
2288 vv = ngx_http_ssi_get_variable(r, name, key);
2295 var = ngx_list_push(mctx->variables);
2302 var->value = *value;
2304 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2305 "set: \"%V\"=\"%V\"", name, value);
2312 ngx_http_ssi_if(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2316 ngx_str_t *expr, left, right;
2318 ngx_uint_t negative, noregex, flags;
2320 if (ctx->command.len == 2) {
2321 if (ctx->conditional) {
2322 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2323 "the \"if\" command inside the \"if\" command");
2324 return NGX_HTTP_SSI_ERROR;
2328 if (ctx->output_chosen) {
2333 expr = params[NGX_HTTP_SSI_IF_EXPR];
2335 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2336 "ssi if expr=\"%V\"", expr);
2338 left.data = expr->data;
2339 last = expr->data + expr->len;
2341 for (p = left.data; p < last; p++) {
2342 if (*p >= 'A' && *p <= 'Z') {
2347 if ((*p >= 'a' && *p <= 'z')
2348 || (*p >= '0' && *p <= '9')
2349 || *p == '$' || *p == '{' || *p == '}' || *p == '_'
2350 || *p == '"' || *p == '\'')
2358 left.len = p - left.data;
2360 while (p < last && *p == ' ') {
2364 flags = (p == last) ? NGX_HTTP_SSI_EXPR_TEST : 0;
2366 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2367 "left: \"%V\"", &left);
2369 rc = ngx_http_ssi_evaluate_string(r, ctx, &left, flags);
2375 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2376 "evaluted left: \"%V\"", &left);
2381 ctx->output_chosen = 1;
2387 ctx->conditional = NGX_HTTP_SSI_COND_IF;
2392 if (p < last && *p == '=') {
2396 } else if (p + 1 < last && *p == '!' && *(p + 1) == '=') {
2401 goto invalid_expression;
2404 while (p < last && *p == ' ') {
2408 if (p < last - 1 && *p == '/') {
2409 if (*(last - 1) != '/') {
2410 goto invalid_expression;
2414 flags = NGX_HTTP_SSI_ADD_ZERO;
2422 if (p < last - 1 && p[0] == '\\' && p[1] == '/') {
2427 right.len = last - p;
2430 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2431 "right: \"%V\"", &right);
2433 rc = ngx_http_ssi_evaluate_string(r, ctx, &right, flags);
2439 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2440 "evaluted right: \"%V\"", &right);
2443 if (left.len != right.len) {
2447 rc = ngx_strncmp(left.data, right.data, right.len);
2454 u_char errstr[NGX_MAX_CONF_ERRSTR];
2456 err.len = NGX_MAX_CONF_ERRSTR;
2459 right.data[right.len] = '\0';
2461 regex = ngx_regex_compile(&right, 0, r->pool, &err);
2463 if (regex == NULL) {
2464 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", err.data);
2465 return NGX_HTTP_SSI_ERROR;
2468 rc = ngx_regex_exec(regex, &left, NULL, 0);
2470 if (rc != NGX_REGEX_NO_MATCHED && rc < 0) {
2471 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
2472 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
2474 return NGX_HTTP_SSI_ERROR;
2477 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
2478 "the using of the regex \"%V\" in SSI "
2479 "requires PCRE library", &right);
2481 return NGX_HTTP_SSI_ERROR;
2485 if ((rc == 0 && !negative) || (rc != 0 && negative)) {
2487 ctx->output_chosen = 1;
2493 ctx->conditional = NGX_HTTP_SSI_COND_IF;
2499 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2500 "invalid expression in \"%V\"", expr);
2502 return NGX_HTTP_SSI_ERROR;
2507 ngx_http_ssi_else(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2510 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2513 if (ctx->output_chosen) {
2519 ctx->conditional = NGX_HTTP_SSI_COND_ELSE;
2526 ngx_http_ssi_endif(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2529 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2533 ctx->output_chosen = 0;
2534 ctx->conditional = 0;
2541 ngx_http_ssi_block(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2544 ngx_http_ssi_ctx_t *mctx;
2545 ngx_http_ssi_block_t *bl;
2547 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2550 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
2552 if (mctx->blocks == NULL) {
2553 mctx->blocks = ngx_array_create(r->pool, 4,
2554 sizeof(ngx_http_ssi_block_t));
2555 if (mctx->blocks == NULL) {
2556 return NGX_HTTP_SSI_ERROR;
2560 bl = ngx_array_push(mctx->blocks);
2562 return NGX_HTTP_SSI_ERROR;
2565 bl->name = *params[NGX_HTTP_SSI_BLOCK_NAME];
2577 ngx_http_ssi_endblock(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2580 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2591 ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
2592 ngx_http_variable_value_t *v, uintptr_t gmt)
2594 ngx_http_ssi_ctx_t *ctx;
2597 char buf[NGX_HTTP_SSI_DATE_LEN];
2600 v->no_cacheable = 0;
2603 tp = ngx_timeofday();
2605 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
2608 || (ctx->timefmt.len == sizeof("%s") - 1
2609 && ctx->timefmt.data[0] == '%' && ctx->timefmt.data[1] == 's'))
2611 v->data = ngx_pnalloc(r->pool, NGX_TIME_T_LEN);
2612 if (v->data == NULL) {
2616 v->len = ngx_sprintf(v->data, "%T", tp->sec + (gmt ? 0 : tp->gmtoff))
2623 ngx_libc_gmtime(tp->sec, &tm);
2625 ngx_libc_localtime(tp->sec, &tm);
2628 v->len = strftime(buf, NGX_HTTP_SSI_DATE_LEN,
2629 (char *) ctx->timefmt.data, &tm);
2634 v->data = ngx_pnalloc(r->pool, v->len);
2635 if (v->data == NULL) {
2639 ngx_memcpy(v->data, buf, v->len);
2646 ngx_http_ssi_preconfiguration(ngx_conf_t *cf)
2649 ngx_http_variable_t *var, *v;
2650 ngx_http_ssi_command_t *cmd;
2651 ngx_http_ssi_main_conf_t *smcf;
2653 for (v = ngx_http_ssi_vars; v->name.len; v++) {
2654 var = ngx_http_add_variable(cf, &v->name, v->flags);
2659 var->get_handler = v->get_handler;
2660 var->data = v->data;
2663 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
2665 for (cmd = ngx_http_ssi_commands; cmd->name.len; cmd++) {
2666 rc = ngx_hash_add_key(&smcf->commands, &cmd->name, cmd,
2667 NGX_HASH_READONLY_KEY);
2673 if (rc == NGX_BUSY) {
2674 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2675 "conflicting SSI command \"%V\"", &cmd->name);
2686 ngx_http_ssi_create_main_conf(ngx_conf_t *cf)
2688 ngx_http_ssi_main_conf_t *smcf;
2690 smcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_main_conf_t));
2692 return NGX_CONF_ERROR;
2695 smcf->commands.pool = cf->pool;
2696 smcf->commands.temp_pool = cf->temp_pool;
2698 if (ngx_hash_keys_array_init(&smcf->commands, NGX_HASH_SMALL) != NGX_OK) {
2699 return NGX_CONF_ERROR;
2707 ngx_http_ssi_init_main_conf(ngx_conf_t *cf, void *conf)
2709 ngx_http_ssi_main_conf_t *smcf = conf;
2711 ngx_hash_init_t hash;
2713 hash.hash = &smcf->hash;
2714 hash.key = ngx_hash_key;
2715 hash.max_size = 1024;
2716 hash.bucket_size = ngx_cacheline_size;
2717 hash.name = "ssi_command_hash";
2718 hash.pool = cf->pool;
2719 hash.temp_pool = NULL;
2721 if (ngx_hash_init(&hash, smcf->commands.keys.elts,
2722 smcf->commands.keys.nelts)
2725 return NGX_CONF_ERROR;
2733 ngx_http_ssi_create_loc_conf(ngx_conf_t *cf)
2735 ngx_http_ssi_loc_conf_t *slcf;
2737 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_loc_conf_t));
2739 return NGX_CONF_ERROR;
2743 * set by ngx_pcalloc():
2745 * conf->types = { NULL };
2746 * conf->types_keys = NULL;
2749 slcf->enable = NGX_CONF_UNSET;
2750 slcf->silent_errors = NGX_CONF_UNSET;
2751 slcf->ignore_recycled_buffers = NGX_CONF_UNSET;
2753 slcf->min_file_chunk = NGX_CONF_UNSET_SIZE;
2754 slcf->value_len = NGX_CONF_UNSET_SIZE;
2761 ngx_http_ssi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
2763 ngx_http_ssi_loc_conf_t *prev = parent;
2764 ngx_http_ssi_loc_conf_t *conf = child;
2766 ngx_conf_merge_value(conf->enable, prev->enable, 0);
2767 ngx_conf_merge_value(conf->silent_errors, prev->silent_errors, 0);
2768 ngx_conf_merge_value(conf->ignore_recycled_buffers,
2769 prev->ignore_recycled_buffers, 0);
2771 ngx_conf_merge_size_value(conf->min_file_chunk, prev->min_file_chunk, 1024);
2772 ngx_conf_merge_size_value(conf->value_len, prev->value_len, 256);
2774 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types,
2775 prev->types_keys, &prev->types,
2776 ngx_http_html_default_types)
2779 return NGX_CONF_ERROR;
2787 ngx_http_ssi_filter_init(ngx_conf_t *cf)
2789 ngx_http_next_header_filter = ngx_http_top_header_filter;
2790 ngx_http_top_header_filter = ngx_http_ssi_header_filter;
2792 ngx_http_next_body_filter = ngx_http_top_body_filter;
2793 ngx_http_top_body_filter = ngx_http_ssi_body_filter;