3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #include <ngx_config.h>
12 #define NGX_HTTP_SSI_ERROR 1
14 #define NGX_HTTP_SSI_DATE_LEN 2048
16 #define NGX_HTTP_SSI_ADD_PREFIX 1
17 #define NGX_HTTP_SSI_ADD_ZERO 2
22 ngx_flag_t silent_errors;
23 ngx_flag_t ignore_recycled_buffers;
24 ngx_flag_t last_modified;
28 size_t min_file_chunk;
31 ngx_array_t *types_keys;
32 } ngx_http_ssi_loc_conf_t;
46 } ngx_http_ssi_block_t;
61 ssi_double_quoted_value_state,
62 ssi_quoted_value_state,
63 ssi_quoted_symbol_state,
65 ssi_comment_end0_state,
66 ssi_comment_end1_state,
70 } ngx_http_ssi_state_e;
73 static ngx_int_t ngx_http_ssi_output(ngx_http_request_t *r,
74 ngx_http_ssi_ctx_t *ctx);
75 static void ngx_http_ssi_buffered(ngx_http_request_t *r,
76 ngx_http_ssi_ctx_t *ctx);
77 static ngx_int_t ngx_http_ssi_parse(ngx_http_request_t *r,
78 ngx_http_ssi_ctx_t *ctx);
79 static ngx_str_t *ngx_http_ssi_get_variable(ngx_http_request_t *r,
80 ngx_str_t *name, ngx_uint_t key);
81 static ngx_int_t ngx_http_ssi_evaluate_string(ngx_http_request_t *r,
82 ngx_http_ssi_ctx_t *ctx, ngx_str_t *text, ngx_uint_t flags);
83 static ngx_int_t ngx_http_ssi_regex_match(ngx_http_request_t *r,
84 ngx_str_t *pattern, ngx_str_t *str);
86 static ngx_int_t ngx_http_ssi_include(ngx_http_request_t *r,
87 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
88 static ngx_int_t ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data,
90 static ngx_int_t ngx_http_ssi_set_variable(ngx_http_request_t *r, void *data,
92 static ngx_int_t ngx_http_ssi_echo(ngx_http_request_t *r,
93 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
94 static ngx_int_t ngx_http_ssi_config(ngx_http_request_t *r,
95 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
96 static ngx_int_t ngx_http_ssi_set(ngx_http_request_t *r,
97 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
98 static ngx_int_t ngx_http_ssi_if(ngx_http_request_t *r,
99 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
100 static ngx_int_t ngx_http_ssi_else(ngx_http_request_t *r,
101 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
102 static ngx_int_t ngx_http_ssi_endif(ngx_http_request_t *r,
103 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
104 static ngx_int_t ngx_http_ssi_block(ngx_http_request_t *r,
105 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
106 static ngx_int_t ngx_http_ssi_endblock(ngx_http_request_t *r,
107 ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
109 static ngx_int_t ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
110 ngx_http_variable_value_t *v, uintptr_t gmt);
112 static ngx_int_t ngx_http_ssi_preconfiguration(ngx_conf_t *cf);
113 static void *ngx_http_ssi_create_main_conf(ngx_conf_t *cf);
114 static char *ngx_http_ssi_init_main_conf(ngx_conf_t *cf, void *conf);
115 static void *ngx_http_ssi_create_loc_conf(ngx_conf_t *cf);
116 static char *ngx_http_ssi_merge_loc_conf(ngx_conf_t *cf,
117 void *parent, void *child);
118 static ngx_int_t ngx_http_ssi_filter_init(ngx_conf_t *cf);
121 static ngx_command_t ngx_http_ssi_filter_commands[] = {
124 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
126 ngx_conf_set_flag_slot,
127 NGX_HTTP_LOC_CONF_OFFSET,
128 offsetof(ngx_http_ssi_loc_conf_t, enable),
131 { ngx_string("ssi_silent_errors"),
132 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
133 ngx_conf_set_flag_slot,
134 NGX_HTTP_LOC_CONF_OFFSET,
135 offsetof(ngx_http_ssi_loc_conf_t, silent_errors),
138 { ngx_string("ssi_ignore_recycled_buffers"),
139 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
140 ngx_conf_set_flag_slot,
141 NGX_HTTP_LOC_CONF_OFFSET,
142 offsetof(ngx_http_ssi_loc_conf_t, ignore_recycled_buffers),
145 { ngx_string("ssi_min_file_chunk"),
146 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
147 ngx_conf_set_size_slot,
148 NGX_HTTP_LOC_CONF_OFFSET,
149 offsetof(ngx_http_ssi_loc_conf_t, min_file_chunk),
152 { ngx_string("ssi_value_length"),
153 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
154 ngx_conf_set_size_slot,
155 NGX_HTTP_LOC_CONF_OFFSET,
156 offsetof(ngx_http_ssi_loc_conf_t, value_len),
159 { ngx_string("ssi_types"),
160 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
162 NGX_HTTP_LOC_CONF_OFFSET,
163 offsetof(ngx_http_ssi_loc_conf_t, types_keys),
164 &ngx_http_html_default_types[0] },
166 { ngx_string("ssi_last_modified"),
167 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
168 ngx_conf_set_flag_slot,
169 NGX_HTTP_LOC_CONF_OFFSET,
170 offsetof(ngx_http_ssi_loc_conf_t, last_modified),
178 static ngx_http_module_t ngx_http_ssi_filter_module_ctx = {
179 ngx_http_ssi_preconfiguration, /* preconfiguration */
180 ngx_http_ssi_filter_init, /* postconfiguration */
182 ngx_http_ssi_create_main_conf, /* create main configuration */
183 ngx_http_ssi_init_main_conf, /* init main configuration */
185 NULL, /* create server configuration */
186 NULL, /* merge server configuration */
188 ngx_http_ssi_create_loc_conf, /* create location configuration */
189 ngx_http_ssi_merge_loc_conf /* merge location configuration */
193 ngx_module_t ngx_http_ssi_filter_module = {
195 &ngx_http_ssi_filter_module_ctx, /* module context */
196 ngx_http_ssi_filter_commands, /* module directives */
197 NGX_HTTP_MODULE, /* module type */
198 NULL, /* init master */
199 NULL, /* init module */
200 NULL, /* init process */
201 NULL, /* init thread */
202 NULL, /* exit thread */
203 NULL, /* exit process */
204 NULL, /* exit master */
205 NGX_MODULE_V1_PADDING
209 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
210 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
213 static u_char ngx_http_ssi_string[] = "<!--";
215 static ngx_str_t ngx_http_ssi_none = ngx_string("(none)");
216 static ngx_str_t ngx_http_ssi_timefmt = ngx_string("%A, %d-%b-%Y %H:%M:%S %Z");
217 static ngx_str_t ngx_http_ssi_null_string = ngx_null_string;
220 #define NGX_HTTP_SSI_INCLUDE_VIRTUAL 0
221 #define NGX_HTTP_SSI_INCLUDE_FILE 1
222 #define NGX_HTTP_SSI_INCLUDE_WAIT 2
223 #define NGX_HTTP_SSI_INCLUDE_SET 3
224 #define NGX_HTTP_SSI_INCLUDE_STUB 4
226 #define NGX_HTTP_SSI_ECHO_VAR 0
227 #define NGX_HTTP_SSI_ECHO_DEFAULT 1
228 #define NGX_HTTP_SSI_ECHO_ENCODING 2
230 #define NGX_HTTP_SSI_CONFIG_ERRMSG 0
231 #define NGX_HTTP_SSI_CONFIG_TIMEFMT 1
233 #define NGX_HTTP_SSI_SET_VAR 0
234 #define NGX_HTTP_SSI_SET_VALUE 1
236 #define NGX_HTTP_SSI_IF_EXPR 0
238 #define NGX_HTTP_SSI_BLOCK_NAME 0
241 static ngx_http_ssi_param_t ngx_http_ssi_include_params[] = {
242 { ngx_string("virtual"), NGX_HTTP_SSI_INCLUDE_VIRTUAL, 0, 0 },
243 { ngx_string("file"), NGX_HTTP_SSI_INCLUDE_FILE, 0, 0 },
244 { ngx_string("wait"), NGX_HTTP_SSI_INCLUDE_WAIT, 0, 0 },
245 { ngx_string("set"), NGX_HTTP_SSI_INCLUDE_SET, 0, 0 },
246 { ngx_string("stub"), NGX_HTTP_SSI_INCLUDE_STUB, 0, 0 },
247 { ngx_null_string, 0, 0, 0 }
251 static ngx_http_ssi_param_t ngx_http_ssi_echo_params[] = {
252 { ngx_string("var"), NGX_HTTP_SSI_ECHO_VAR, 1, 0 },
253 { ngx_string("default"), NGX_HTTP_SSI_ECHO_DEFAULT, 0, 0 },
254 { ngx_string("encoding"), NGX_HTTP_SSI_ECHO_ENCODING, 0, 0 },
255 { ngx_null_string, 0, 0, 0 }
259 static ngx_http_ssi_param_t ngx_http_ssi_config_params[] = {
260 { ngx_string("errmsg"), NGX_HTTP_SSI_CONFIG_ERRMSG, 0, 0 },
261 { ngx_string("timefmt"), NGX_HTTP_SSI_CONFIG_TIMEFMT, 0, 0 },
262 { ngx_null_string, 0, 0, 0 }
266 static ngx_http_ssi_param_t ngx_http_ssi_set_params[] = {
267 { ngx_string("var"), NGX_HTTP_SSI_SET_VAR, 1, 0 },
268 { ngx_string("value"), NGX_HTTP_SSI_SET_VALUE, 1, 0 },
269 { ngx_null_string, 0, 0, 0 }
273 static ngx_http_ssi_param_t ngx_http_ssi_if_params[] = {
274 { ngx_string("expr"), NGX_HTTP_SSI_IF_EXPR, 1, 0 },
275 { ngx_null_string, 0, 0, 0 }
279 static ngx_http_ssi_param_t ngx_http_ssi_block_params[] = {
280 { ngx_string("name"), NGX_HTTP_SSI_BLOCK_NAME, 1, 0 },
281 { ngx_null_string, 0, 0, 0 }
285 static ngx_http_ssi_param_t ngx_http_ssi_no_params[] = {
286 { ngx_null_string, 0, 0, 0 }
290 static ngx_http_ssi_command_t ngx_http_ssi_commands[] = {
291 { ngx_string("include"), ngx_http_ssi_include,
292 ngx_http_ssi_include_params, 0, 0, 1 },
293 { ngx_string("echo"), ngx_http_ssi_echo,
294 ngx_http_ssi_echo_params, 0, 0, 0 },
295 { ngx_string("config"), ngx_http_ssi_config,
296 ngx_http_ssi_config_params, 0, 0, 0 },
297 { ngx_string("set"), ngx_http_ssi_set, ngx_http_ssi_set_params, 0, 0, 0 },
299 { ngx_string("if"), ngx_http_ssi_if, ngx_http_ssi_if_params, 0, 0, 0 },
300 { ngx_string("elif"), ngx_http_ssi_if, ngx_http_ssi_if_params,
301 NGX_HTTP_SSI_COND_IF, 0, 0 },
302 { ngx_string("else"), ngx_http_ssi_else, ngx_http_ssi_no_params,
303 NGX_HTTP_SSI_COND_IF, 0, 0 },
304 { ngx_string("endif"), ngx_http_ssi_endif, ngx_http_ssi_no_params,
305 NGX_HTTP_SSI_COND_ELSE, 0, 0 },
307 { ngx_string("block"), ngx_http_ssi_block,
308 ngx_http_ssi_block_params, 0, 0, 0 },
309 { ngx_string("endblock"), ngx_http_ssi_endblock,
310 ngx_http_ssi_no_params, 0, 1, 0 },
312 { ngx_null_string, NULL, NULL, 0, 0, 0 }
316 static ngx_http_variable_t ngx_http_ssi_vars[] = {
318 { ngx_string("date_local"), NULL, ngx_http_ssi_date_gmt_local_variable, 0,
319 NGX_HTTP_VAR_NOCACHEABLE, 0 },
321 { ngx_string("date_gmt"), NULL, ngx_http_ssi_date_gmt_local_variable, 1,
322 NGX_HTTP_VAR_NOCACHEABLE, 0 },
324 { ngx_null_string, NULL, NULL, 0, 0, 0 }
330 ngx_http_ssi_header_filter(ngx_http_request_t *r)
332 ngx_http_ssi_ctx_t *ctx;
333 ngx_http_ssi_loc_conf_t *slcf;
335 slcf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
338 || r->headers_out.content_length_n == 0
339 || ngx_http_test_content_type(r, &slcf->types) == NULL)
341 return ngx_http_next_header_filter(r);
344 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t));
349 ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module);
352 ctx->value_len = slcf->value_len;
353 ctx->last_out = &ctx->out;
355 ctx->encoding = NGX_HTTP_SSI_ENTITY_ENCODING;
358 ctx->params.elts = ctx->params_array;
359 ctx->params.size = sizeof(ngx_table_elt_t);
360 ctx->params.nalloc = NGX_HTTP_SSI_PARAMS_N;
361 ctx->params.pool = r->pool;
363 ctx->timefmt = ngx_http_ssi_timefmt;
364 ngx_str_set(&ctx->errmsg,
365 "[an error occurred while processing the directive]");
367 r->filter_need_in_memory = 1;
370 ngx_http_clear_content_length(r);
371 ngx_http_clear_accept_ranges(r);
373 if (!slcf->last_modified) {
374 ngx_http_clear_last_modified(r);
375 ngx_http_clear_etag(r);
378 ngx_http_weak_etag(r);
382 return ngx_http_next_header_filter(r);
387 ngx_http_ssi_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
393 ngx_chain_t *cl, **ll;
394 ngx_table_elt_t *param;
395 ngx_http_ssi_ctx_t *ctx, *mctx;
396 ngx_http_ssi_block_t *bl;
397 ngx_http_ssi_param_t *prm;
398 ngx_http_ssi_command_t *cmd;
399 ngx_http_ssi_loc_conf_t *slcf;
400 ngx_http_ssi_main_conf_t *smcf;
401 ngx_str_t *params[NGX_HTTP_SSI_MAX_PARAMS + 1];
403 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
409 && ctx->busy == NULL))
411 return ngx_http_next_body_filter(r, in);
414 /* add the incoming chain to the chain ctx->in */
417 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) {
422 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
423 "http ssi filter \"%V?%V\"", &r->uri, &r->args);
427 if (r != r->connection->data) {
428 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
429 "http ssi filter wait \"%V?%V\" non-active",
430 &ctx->wait->uri, &ctx->wait->args);
435 if (ctx->wait->done) {
436 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
437 "http ssi filter wait \"%V?%V\" done",
438 &ctx->wait->uri, &ctx->wait->args);
443 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
444 "http ssi filter wait \"%V?%V\"",
445 &ctx->wait->uri, &ctx->wait->args);
447 return ngx_http_next_body_filter(r, NULL);
451 slcf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
453 while (ctx->in || ctx->buf) {
455 if (ctx->buf == NULL) {
456 ctx->buf = ctx->in->buf;
457 ctx->in = ctx->in->next;
458 ctx->pos = ctx->buf->pos;
461 if (ctx->state == ssi_start_state) {
462 ctx->copy_start = ctx->pos;
463 ctx->copy_end = ctx->pos;
468 while (ctx->pos < ctx->buf->last) {
470 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
471 "saved: %uz state: %ui", ctx->saved, ctx->state);
473 rc = ngx_http_ssi_parse(r, ctx);
475 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
476 "parse: %i, looked: %uz %p-%p",
477 rc, ctx->looked, ctx->copy_start, ctx->copy_end);
479 if (rc == NGX_ERROR) {
483 if (ctx->copy_start != ctx->copy_end) {
487 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
488 "saved: %uz", ctx->saved);
494 ctx->free = ctx->free->next;
496 ngx_memzero(b, sizeof(ngx_buf_t));
499 b = ngx_calloc_buf(r->pool);
504 cl = ngx_alloc_chain_link(r->pool);
513 b->pos = ngx_http_ssi_string;
514 b->last = ngx_http_ssi_string + ctx->saved;
517 ctx->last_out = &cl->next;
524 ctx->free = ctx->free->next;
528 b = ngx_alloc_buf(r->pool);
533 cl = ngx_alloc_chain_link(r->pool);
541 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
543 b->pos = ctx->copy_start;
544 b->last = ctx->copy_end;
550 if (slcf->min_file_chunk < (size_t) (b->last - b->pos))
552 b->file_last = b->file_pos
553 + (b->last - ctx->buf->pos);
554 b->file_pos += b->pos - ctx->buf->pos;
563 ctx->last_out = &cl->next;
567 && ctx->saved + (ctx->copy_end - ctx->copy_start))
569 b = ngx_create_temp_buf(r->pool,
570 ctx->saved + (ctx->copy_end - ctx->copy_start));
577 b->last = ngx_cpymem(b->pos, ngx_http_ssi_string,
581 b->last = ngx_cpymem(b->last, ctx->copy_start,
582 ctx->copy_end - ctx->copy_start);
584 cl = ngx_alloc_chain_link(r->pool);
594 mctx = ngx_http_get_module_ctx(r->main,
595 ngx_http_ssi_filter_module);
596 bl = mctx->blocks->elts;
597 for (ll = &bl[mctx->blocks->nelts - 1].bufs;
611 if (ctx->state == ssi_start_state) {
612 ctx->copy_start = ctx->pos;
613 ctx->copy_end = ctx->pos;
616 ctx->copy_start = NULL;
617 ctx->copy_end = NULL;
620 if (rc == NGX_AGAIN) {
629 smcf = ngx_http_get_module_main_conf(r,
630 ngx_http_ssi_filter_module);
632 cmd = ngx_hash_find(&smcf->hash, ctx->key, ctx->command.data,
637 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
638 "invalid SSI command: \"%V\"",
646 if (!ctx->output && !cmd->block) {
650 /* reconstruct the SSI command text */
652 len = 5 + ctx->command.len + 4;
654 param = ctx->params.elts;
655 for (i = 0; i < ctx->params.nelts; i++) {
656 len += 1 + param[i].key.len + 2
657 + param[i].value.len + 1;
660 b = ngx_create_temp_buf(r->pool, len);
666 cl = ngx_alloc_chain_link(r->pool);
680 b->last = ngx_cpymem(b->last, ctx->command.data,
683 for (i = 0; i < ctx->params.nelts; i++) {
685 b->last = ngx_cpymem(b->last, param[i].key.data,
689 b->last = ngx_cpymem(b->last, param[i].value.data,
699 mctx = ngx_http_get_module_ctx(r->main,
700 ngx_http_ssi_filter_module);
701 bl = mctx->blocks->elts;
702 for (ll = &bl[mctx->blocks->nelts - 1].bufs;
716 if (cmd->conditional == 0) {
722 && (ctx->conditional == 0
723 || ctx->conditional > cmd->conditional))
725 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
726 "invalid context of SSI command: \"%V\"",
731 if (ctx->params.nelts > NGX_HTTP_SSI_MAX_PARAMS) {
732 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
733 "too many SSI command parameters: \"%V\"",
739 (NGX_HTTP_SSI_MAX_PARAMS + 1) * sizeof(ngx_str_t *));
741 param = ctx->params.elts;
743 for (i = 0; i < ctx->params.nelts; i++) {
745 for (prm = cmd->params; prm->name.len; prm++) {
747 if (param[i].key.len != prm->name.len
748 || ngx_strncmp(param[i].key.data, prm->name.data,
754 if (!prm->multiple) {
755 if (params[prm->index]) {
756 ngx_log_error(NGX_LOG_ERR,
757 r->connection->log, 0,
758 "duplicate \"%V\" parameter "
759 "in \"%V\" SSI command",
760 ¶m[i].key, &ctx->command);
765 params[prm->index] = ¶m[i].value;
770 for (index = prm->index; params[index]; index++) {
774 params[index] = ¶m[i].value;
779 if (prm->name.len == 0) {
780 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
781 "invalid parameter name: \"%V\" "
782 "in \"%V\" SSI command",
783 ¶m[i].key, &ctx->command);
789 for (prm = cmd->params; prm->name.len; prm++) {
790 if (prm->mandatory && params[prm->index] == 0) {
791 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
792 "mandatory \"%V\" parameter is absent "
793 "in \"%V\" SSI command",
794 &prm->name, &ctx->command);
800 if (cmd->flush && ctx->out) {
802 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
805 if (ngx_http_ssi_output(r, ctx) == NGX_ERROR) {
810 rc = cmd->handler(r, ctx, params);
816 if (rc == NGX_DONE || rc == NGX_AGAIN || rc == NGX_ERROR) {
817 ngx_http_ssi_buffered(r, ctx);
823 /* rc == NGX_HTTP_SSI_ERROR */
827 if (slcf->silent_errors) {
833 ctx->free = ctx->free->next;
835 ngx_memzero(b, sizeof(ngx_buf_t));
838 b = ngx_calloc_buf(r->pool);
843 cl = ngx_alloc_chain_link(r->pool);
852 b->pos = ctx->errmsg.data;
853 b->last = ctx->errmsg.data + ctx->errmsg.len;
857 ctx->last_out = &cl->next;
862 if (ctx->buf->last_buf || ngx_buf_in_memory(ctx->buf)) {
866 ctx->free = ctx->free->next;
868 ngx_memzero(b, sizeof(ngx_buf_t));
871 b = ngx_calloc_buf(r->pool);
876 cl = ngx_alloc_chain_link(r->pool);
888 ctx->last_out = &cl->next;
891 b->last_buf = ctx->buf->last_buf;
892 b->shadow = ctx->buf;
894 if (slcf->ignore_recycled_buffers == 0) {
895 b->recycled = ctx->buf->recycled;
901 ctx->saved = ctx->looked;
904 if (ctx->out == NULL && ctx->busy == NULL) {
908 return ngx_http_ssi_output(r, ctx);
913 ngx_http_ssi_output(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
921 for (cl = ctx->out; cl; cl = cl->next) {
922 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
923 "ssi out: %p %p", cl->buf, cl->buf->pos);
925 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
926 "the same buf was used in ssi");
934 rc = ngx_http_next_body_filter(r, ctx->out);
936 if (ctx->busy == NULL) {
937 ctx->busy = ctx->out;
940 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
945 ctx->last_out = &ctx->out;
952 if (ngx_buf_size(b) != 0) {
957 b->shadow->pos = b->shadow->last;
960 ctx->busy = cl->next;
962 if (ngx_buf_in_memory(b) || b->in_file) {
963 /* add data bufs only to the free buf chain */
965 cl->next = ctx->free;
970 ngx_http_ssi_buffered(r, ctx);
977 ngx_http_ssi_buffered(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
979 if (ctx->in || ctx->buf) {
980 r->buffered |= NGX_HTTP_SSI_BUFFERED;
983 r->buffered &= ~NGX_HTTP_SSI_BUFFERED;
989 ngx_http_ssi_parse(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
991 u_char *p, *value, *last, *copy_end, ch;
993 ngx_http_ssi_state_e state;
996 looked = ctx->looked;
997 last = ctx->buf->last;
998 copy_end = ctx->copy_end;
1000 for (p = ctx->pos; p < last; p++) {
1004 if (state == ssi_start_state) {
1006 /* the tight loop */
1012 state = ssi_tag_state;
1026 ctx->looked = looked;
1029 if (ctx->copy_start == NULL) {
1030 ctx->copy_start = ctx->buf->pos;
1042 case ssi_start_state:
1050 state = ssi_comment0_state;
1060 state = ssi_start_state;
1066 case ssi_comment0_state:
1070 state = ssi_comment1_state;
1076 state = ssi_tag_state;
1082 state = ssi_start_state;
1088 case ssi_comment1_state:
1092 state = ssi_sharp_state;
1098 state = ssi_tag_state;
1104 state = ssi_start_state;
1110 case ssi_sharp_state:
1113 if (p - ctx->pos < 4) {
1117 state = ssi_precommand_state;
1123 state = ssi_tag_state;
1129 state = ssi_start_state;
1135 case ssi_precommand_state:
1144 ctx->command.len = 1;
1145 ctx->command.data = ngx_pnalloc(r->pool,
1146 NGX_HTTP_SSI_COMMAND_LEN);
1147 if (ctx->command.data == NULL) {
1151 ctx->command.data[0] = ch;
1154 ctx->key = ngx_hash(ctx->key, ch);
1156 ctx->params.nelts = 0;
1158 state = ssi_command_state;
1164 case ssi_command_state:
1170 state = ssi_preparam_state;
1174 state = ssi_comment_end0_state;
1178 if (ctx->command.len == NGX_HTTP_SSI_COMMAND_LEN) {
1179 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1180 "the \"%V%c...\" SSI command is too long",
1183 state = ssi_error_state;
1187 ctx->command.data[ctx->command.len++] = ch;
1188 ctx->key = ngx_hash(ctx->key, ch);
1193 case ssi_preparam_state:
1202 state = ssi_comment_end0_state;
1206 ctx->param = ngx_array_push(&ctx->params);
1207 if (ctx->param == NULL) {
1211 ctx->param->key.len = 1;
1212 ctx->param->key.data = ngx_pnalloc(r->pool,
1213 NGX_HTTP_SSI_PARAM_LEN);
1214 if (ctx->param->key.data == NULL) {
1218 ctx->param->key.data[0] = ch;
1220 ctx->param->value.len = 0;
1222 if (ctx->value_buf == NULL) {
1223 ctx->param->value.data = ngx_pnalloc(r->pool,
1224 ctx->value_len + 1);
1225 if (ctx->param->value.data == NULL) {
1230 ctx->param->value.data = ctx->value_buf;
1233 state = ssi_param_state;
1239 case ssi_param_state:
1245 state = ssi_preequal_state;
1249 state = ssi_prevalue_state;
1253 state = ssi_error_end0_state;
1255 ctx->param->key.data[ctx->param->key.len++] = ch;
1256 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1257 "invalid \"%V\" parameter in \"%V\" SSI command",
1258 &ctx->param->key, &ctx->command);
1262 if (ctx->param->key.len == NGX_HTTP_SSI_PARAM_LEN) {
1263 state = ssi_error_state;
1264 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1265 "too long \"%V%c...\" parameter in "
1266 "\"%V\" SSI command",
1267 &ctx->param->key, ch, &ctx->command);
1271 ctx->param->key.data[ctx->param->key.len++] = ch;
1276 case ssi_preequal_state:
1285 state = ssi_prevalue_state;
1290 state = ssi_error_end0_state;
1292 state = ssi_error_state;
1295 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1296 "unexpected \"%c\" symbol after \"%V\" "
1297 "parameter in \"%V\" SSI command",
1298 ch, &ctx->param->key, &ctx->command);
1304 case ssi_prevalue_state:
1313 state = ssi_double_quoted_value_state;
1317 state = ssi_quoted_value_state;
1322 state = ssi_error_end0_state;
1324 state = ssi_error_state;
1327 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1328 "unexpected \"%c\" symbol before value of "
1329 "\"%V\" parameter in \"%V\" SSI command",
1330 ch, &ctx->param->key, &ctx->command);
1336 case ssi_double_quoted_value_state:
1339 state = ssi_postparam_state;
1343 ctx->saved_state = ssi_double_quoted_value_state;
1344 state = ssi_quoted_symbol_state;
1349 if (ctx->param->value.len == ctx->value_len) {
1350 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1351 "too long \"%V%c...\" value of \"%V\" "
1352 "parameter in \"%V\" SSI command",
1353 &ctx->param->value, ch, &ctx->param->key,
1355 state = ssi_error_state;
1359 ctx->param->value.data[ctx->param->value.len++] = ch;
1364 case ssi_quoted_value_state:
1367 state = ssi_postparam_state;
1371 ctx->saved_state = ssi_quoted_value_state;
1372 state = ssi_quoted_symbol_state;
1377 if (ctx->param->value.len == ctx->value_len) {
1378 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1379 "too long \"%V%c...\" value of \"%V\" "
1380 "parameter in \"%V\" SSI command",
1381 &ctx->param->value, ch, &ctx->param->key,
1383 state = ssi_error_state;
1387 ctx->param->value.data[ctx->param->value.len++] = ch;
1392 case ssi_quoted_symbol_state:
1393 state = ctx->saved_state;
1395 if (ctx->param->value.len == ctx->value_len) {
1396 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1397 "too long \"%V%c...\" value of \"%V\" "
1398 "parameter in \"%V\" SSI command",
1399 &ctx->param->value, ch, &ctx->param->key,
1401 state = ssi_error_state;
1405 ctx->param->value.data[ctx->param->value.len++] = ch;
1409 case ssi_postparam_state:
1411 if (ctx->param->value.len + 1 < ctx->value_len / 2) {
1412 value = ngx_pnalloc(r->pool, ctx->param->value.len + 1);
1413 if (value == NULL) {
1417 ngx_memcpy(value, ctx->param->value.data,
1418 ctx->param->value.len);
1420 ctx->value_buf = ctx->param->value.data;
1421 ctx->param->value.data = value;
1424 ctx->value_buf = NULL;
1432 state = ssi_preparam_state;
1436 state = ssi_comment_end0_state;
1440 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1441 "unexpected \"%c\" symbol after \"%V\" value "
1442 "of \"%V\" parameter in \"%V\" SSI command",
1443 ch, &ctx->param->value, &ctx->param->key,
1445 state = ssi_error_state;
1451 case ssi_comment_end0_state:
1454 state = ssi_comment_end1_state;
1458 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1459 "unexpected \"%c\" symbol in \"%V\" SSI command",
1461 state = ssi_error_state;
1467 case ssi_comment_end1_state:
1470 ctx->state = ssi_start_state;
1472 ctx->looked = looked;
1473 ctx->copy_end = copy_end;
1475 if (ctx->copy_start == NULL && copy_end) {
1476 ctx->copy_start = ctx->buf->pos;
1482 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1483 "unexpected \"%c\" symbol in \"%V\" SSI command",
1485 state = ssi_error_state;
1491 case ssi_error_state:
1494 state = ssi_error_end0_state;
1503 case ssi_error_end0_state:
1506 state = ssi_error_end1_state;
1510 state = ssi_error_state;
1516 case ssi_error_end1_state:
1519 ctx->state = ssi_start_state;
1521 ctx->looked = looked;
1522 ctx->copy_end = copy_end;
1524 if (ctx->copy_start == NULL && copy_end) {
1525 ctx->copy_start = ctx->buf->pos;
1528 return NGX_HTTP_SSI_ERROR;
1531 state = ssi_error_state;
1541 ctx->looked = looked;
1543 ctx->copy_end = (state == ssi_start_state) ? p : copy_end;
1545 if (ctx->copy_start == NULL && ctx->copy_end) {
1546 ctx->copy_start = ctx->buf->pos;
1554 ngx_http_ssi_get_variable(ngx_http_request_t *r, ngx_str_t *name,
1558 ngx_list_part_t *part;
1559 ngx_http_ssi_var_t *var;
1560 ngx_http_ssi_ctx_t *ctx;
1562 ctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
1568 if (key >= '0' && key <= '9') {
1571 if (i < ctx->ncaptures) {
1572 value = ngx_palloc(r->pool, sizeof(ngx_str_t));
1573 if (value == NULL) {
1579 value->data = ctx->captures_data + ctx->captures[i];
1580 value->len = ctx->captures[i + 1] - ctx->captures[i];
1588 if (ctx->variables == NULL) {
1592 part = &ctx->variables->part;
1595 for (i = 0; /* void */ ; i++) {
1597 if (i >= part->nelts) {
1598 if (part->next == NULL) {
1607 if (name->len != var[i].name.len) {
1611 if (key != var[i].key) {
1615 if (ngx_strncmp(name->data, var[i].name.data, name->len) == 0) {
1616 return &var[i].value;
1625 ngx_http_ssi_evaluate_string(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
1626 ngx_str_t *text, ngx_uint_t flags)
1628 u_char ch, *p, **value, *data, *part_data;
1629 size_t *size, len, prefix, part_len;
1630 ngx_str_t var, *val;
1632 ngx_uint_t i, n, bracket, quoted;
1633 ngx_array_t lengths, values;
1634 ngx_http_variable_value_t *vv;
1636 n = ngx_http_script_variables_count(text);
1643 if ((flags & NGX_HTTP_SSI_ADD_PREFIX) && text->data[0] != '/') {
1645 for (prefix = r->uri.len; prefix; prefix--) {
1646 if (r->uri.data[prefix - 1] == '/') {
1652 len = prefix + text->len;
1654 data = ngx_pnalloc(r->pool, len);
1659 p = ngx_copy(data, r->uri.data, prefix);
1665 for (i = 0; i < text->len; i++) {
1678 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
1686 text->len = p - data;
1692 if (ngx_array_init(&lengths, r->pool, 8, sizeof(size_t *)) != NGX_OK) {
1696 if (ngx_array_init(&values, r->pool, 8, sizeof(u_char *)) != NGX_OK) {
1703 while (i < text->len) {
1705 if (text->data[i] == '$') {
1709 if (++i == text->len) {
1710 goto invalid_variable;
1713 if (text->data[i] == '{') {
1716 if (++i == text->len) {
1717 goto invalid_variable;
1720 var.data = &text->data[i];
1724 var.data = &text->data[i];
1727 for ( /* void */ ; i < text->len; i++, var.len++) {
1730 if (ch == '}' && bracket) {
1736 if ((ch >= 'A' && ch <= 'Z')
1737 || (ch >= 'a' && ch <= 'z')
1738 || (ch >= '0' && ch <= '9')
1748 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1749 "the closing bracket in \"%V\" "
1750 "variable is missing", &var);
1751 return NGX_HTTP_SSI_ERROR;
1755 goto invalid_variable;
1758 key = ngx_hash_strlow(var.data, var.data, var.len);
1760 val = ngx_http_ssi_get_variable(r, &var, key);
1763 vv = ngx_http_get_variable(r, &var, key);
1768 if (vv->not_found) {
1772 part_data = vv->data;
1776 part_data = val->data;
1777 part_len = val->len;
1781 part_data = &text->data[i];
1784 for (p = part_data; i < text->len; i++) {
1801 if (ch != '\\' && ch != '\'' && ch != '"' && ch != '$') {
1809 part_len = p - part_data;
1814 size = ngx_array_push(&lengths);
1821 value = ngx_array_push(&values);
1822 if (value == NULL) {
1831 size = lengths.elts;
1832 value = values.elts;
1834 if (flags & NGX_HTTP_SSI_ADD_PREFIX) {
1835 for (i = 0; i < values.nelts; i++) {
1837 if (*value[i] != '/') {
1838 for (prefix = r->uri.len; prefix; prefix--) {
1839 if (r->uri.data[prefix - 1] == '/') {
1851 p = ngx_pnalloc(r->pool, len + ((flags & NGX_HTTP_SSI_ADD_ZERO) ? 1 : 0));
1859 p = ngx_copy(p, r->uri.data, prefix);
1861 for (i = 0; i < values.nelts; i++) {
1862 p = ngx_copy(p, value[i], size[i]);
1869 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1870 "invalid variable name in \"%V\"", text);
1872 return NGX_HTTP_SSI_ERROR;
1877 ngx_http_ssi_regex_match(ngx_http_request_t *r, ngx_str_t *pattern,
1882 u_char *p, errstr[NGX_MAX_CONF_ERRSTR];
1885 ngx_str_t *vv, name, value;
1887 ngx_http_ssi_ctx_t *ctx;
1888 ngx_http_ssi_var_t *var;
1889 ngx_regex_compile_t rgc;
1891 ngx_memzero(&rgc, sizeof(ngx_regex_compile_t));
1893 rgc.pattern = *pattern;
1895 rgc.err.len = NGX_MAX_CONF_ERRSTR;
1896 rgc.err.data = errstr;
1898 if (ngx_regex_compile(&rgc) != NGX_OK) {
1899 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%V", &rgc.err);
1900 return NGX_HTTP_SSI_ERROR;
1903 n = (rgc.captures + 1) * 3;
1905 captures = ngx_palloc(r->pool, n * sizeof(int));
1906 if (captures == NULL) {
1910 rc = ngx_regex_exec(rgc.regex, str, captures, n);
1912 if (rc < NGX_REGEX_NO_MATCHED) {
1913 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1914 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
1916 return NGX_HTTP_SSI_ERROR;
1919 if (rc == NGX_REGEX_NO_MATCHED) {
1920 return NGX_DECLINED;
1923 ctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
1925 ctx->ncaptures = rc;
1926 ctx->captures = captures;
1927 ctx->captures_data = str->data;
1929 if (rgc.named_captures > 0) {
1931 if (ctx->variables == NULL) {
1932 ctx->variables = ngx_list_create(r->pool, 4,
1933 sizeof(ngx_http_ssi_var_t));
1934 if (ctx->variables == NULL) {
1939 size = rgc.name_size;
1942 for (i = 0; i < (ngx_uint_t) rgc.named_captures; i++, p += size) {
1945 name.len = ngx_strlen(name.data);
1947 n = 2 * ((p[0] << 8) + p[1]);
1949 value.data = &str->data[captures[n]];
1950 value.len = captures[n + 1] - captures[n];
1952 key = ngx_hash_strlow(name.data, name.data, name.len);
1954 vv = ngx_http_ssi_get_variable(r, &name, key);
1961 var = ngx_list_push(ctx->variables);
1976 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1977 "the using of the regex \"%V\" in SSI requires PCRE library",
1979 return NGX_HTTP_SSI_ERROR;
1986 ngx_http_ssi_include(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
1990 ngx_str_t *uri, *file, *wait, *set, *stub, args;
1992 ngx_uint_t flags, i;
1993 ngx_chain_t *cl, *tl, **ll, *out;
1994 ngx_http_request_t *sr;
1995 ngx_http_ssi_var_t *var;
1996 ngx_http_ssi_ctx_t *mctx;
1997 ngx_http_ssi_block_t *bl;
1998 ngx_http_post_subrequest_t *psr;
2000 uri = params[NGX_HTTP_SSI_INCLUDE_VIRTUAL];
2001 file = params[NGX_HTTP_SSI_INCLUDE_FILE];
2002 wait = params[NGX_HTTP_SSI_INCLUDE_WAIT];
2003 set = params[NGX_HTTP_SSI_INCLUDE_SET];
2004 stub = params[NGX_HTTP_SSI_INCLUDE_STUB];
2007 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2008 "inclusion may be either virtual=\"%V\" or file=\"%V\"",
2010 return NGX_HTTP_SSI_ERROR;
2013 if (uri == NULL && file == NULL) {
2014 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2015 "no parameter in \"include\" SSI command");
2016 return NGX_HTTP_SSI_ERROR;
2020 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2021 "\"set\" and \"stub\" cannot be used together "
2022 "in \"include\" SSI command");
2023 return NGX_HTTP_SSI_ERROR;
2028 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2029 "\"wait\" cannot be used with file=\"%V\"", file);
2030 return NGX_HTTP_SSI_ERROR;
2034 && ngx_strncasecmp(wait->data, (u_char *) "no", 2) == 0)
2038 } else if (wait->len != 3
2039 || ngx_strncasecmp(wait->data, (u_char *) "yes", 3) != 0)
2041 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2042 "invalid value \"%V\" in the \"wait\" parameter",
2044 return NGX_HTTP_SSI_ERROR;
2050 wait = (ngx_str_t *) -1;
2053 rc = ngx_http_ssi_evaluate_string(r, ctx, uri, NGX_HTTP_SSI_ADD_PREFIX);
2059 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2060 "ssi include: \"%V\"", uri);
2062 ngx_str_null(&args);
2063 flags = NGX_HTTP_LOG_UNSAFE;
2065 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
2066 return NGX_HTTP_SSI_ERROR;
2071 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
2075 bl = mctx->blocks->elts;
2076 for (i = 0; i < mctx->blocks->nelts; i++) {
2077 if (stub->len == bl[i].name.len
2078 && ngx_strncmp(stub->data, bl[i].name.data, stub->len) == 0)
2085 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2086 "\"stub\"=\"%V\" for \"include\" not found", stub);
2087 return NGX_HTTP_SSI_ERROR;
2091 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
2096 psr->handler = ngx_http_ssi_stub_output;
2098 if (bl[i].count++) {
2103 for (tl = bl[i].bufs; tl; tl = tl->next) {
2107 ctx->free = ctx->free->next;
2111 b = ngx_alloc_buf(r->pool);
2116 cl = ngx_alloc_chain_link(r->pool);
2124 ngx_memcpy(b, tl->buf, sizeof(ngx_buf_t));
2136 psr->data = bl[i].bufs;
2141 flags |= NGX_HTTP_SUBREQUEST_WAITED;
2145 key = ngx_hash_strlow(set->data, set->data, set->len);
2147 psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
2152 psr->handler = ngx_http_ssi_set_variable;
2153 psr->data = ngx_http_ssi_get_variable(r, set, key);
2155 if (psr->data == NULL) {
2157 if (mctx->variables == NULL) {
2158 mctx->variables = ngx_list_create(r->pool, 4,
2159 sizeof(ngx_http_ssi_var_t));
2160 if (mctx->variables == NULL) {
2165 var = ngx_list_push(mctx->variables);
2172 var->value = ngx_http_ssi_null_string;
2173 psr->data = &var->value;
2176 flags |= NGX_HTTP_SUBREQUEST_IN_MEMORY|NGX_HTTP_SUBREQUEST_WAITED;
2179 if (ngx_http_subrequest(r, uri, &args, &sr, psr, flags) != NGX_OK) {
2180 return NGX_HTTP_SSI_ERROR;
2183 if (wait == NULL && set == NULL) {
2187 if (ctx->wait == NULL) {
2193 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2194 "can only wait for one subrequest at a time");
2202 ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc)
2206 if (rc == NGX_ERROR || r->connection->error || r->request_output) {
2210 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2211 "ssi stub output: \"%V?%V\"", &r->uri, &r->args);
2215 if (!r->header_sent) {
2216 r->headers_out.content_type_len =
2217 r->parent->headers_out.content_type_len;
2218 r->headers_out.content_type = r->parent->headers_out.content_type;
2220 if (ngx_http_send_header(r) == NGX_ERROR) {
2225 return ngx_http_output_filter(r, out);
2230 ngx_http_ssi_set_variable(ngx_http_request_t *r, void *data, ngx_int_t rc)
2232 ngx_str_t *value = data;
2235 value->len = r->upstream->buffer.last - r->upstream->buffer.pos;
2236 value->data = r->upstream->buffer.pos;
2244 ngx_http_ssi_echo(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2251 ngx_str_t *var, *value, *enc, text;
2253 ngx_http_variable_value_t *vv;
2255 var = params[NGX_HTTP_SSI_ECHO_VAR];
2257 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2258 "ssi echo \"%V\"", var);
2260 key = ngx_hash_strlow(var->data, var->data, var->len);
2262 value = ngx_http_ssi_get_variable(r, var, key);
2264 if (value == NULL) {
2265 vv = ngx_http_get_variable(r, var, key);
2268 return NGX_HTTP_SSI_ERROR;
2271 if (!vv->not_found) {
2272 text.data = vv->data;
2278 if (value == NULL) {
2279 value = params[NGX_HTTP_SSI_ECHO_DEFAULT];
2281 if (value == NULL) {
2282 value = &ngx_http_ssi_none;
2284 } else if (value->len == 0) {
2289 if (value->len == 0) {
2294 enc = params[NGX_HTTP_SSI_ECHO_ENCODING];
2297 if (enc->len == 4 && ngx_strncmp(enc->data, "none", 4) == 0) {
2299 ctx->encoding = NGX_HTTP_SSI_NO_ENCODING;
2301 } else if (enc->len == 3 && ngx_strncmp(enc->data, "url", 3) == 0) {
2303 ctx->encoding = NGX_HTTP_SSI_URL_ENCODING;
2305 } else if (enc->len == 6 && ngx_strncmp(enc->data, "entity", 6) == 0) {
2307 ctx->encoding = NGX_HTTP_SSI_ENTITY_ENCODING;
2310 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2311 "unknown encoding \"%V\" in the \"echo\" command",
2318 switch (ctx->encoding) {
2320 case NGX_HTTP_SSI_URL_ENCODING:
2321 len = 2 * ngx_escape_uri(NULL, value->data, value->len,
2325 p = ngx_pnalloc(r->pool, value->len + len);
2327 return NGX_HTTP_SSI_ERROR;
2330 (void) ngx_escape_uri(p, value->data, value->len, NGX_ESCAPE_HTML);
2336 case NGX_HTTP_SSI_ENTITY_ENCODING:
2337 len = ngx_escape_html(NULL, value->data, value->len);
2340 p = ngx_pnalloc(r->pool, value->len + len);
2342 return NGX_HTTP_SSI_ERROR;
2345 (void) ngx_escape_html(p, value->data, value->len);
2351 default: /* NGX_HTTP_SSI_NO_ENCODING */
2356 b = ngx_calloc_buf(r->pool);
2358 return NGX_HTTP_SSI_ERROR;
2361 cl = ngx_alloc_chain_link(r->pool);
2363 return NGX_HTTP_SSI_ERROR;
2372 *ctx->last_out = cl;
2373 ctx->last_out = &cl->next;
2380 ngx_http_ssi_config(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2385 value = params[NGX_HTTP_SSI_CONFIG_TIMEFMT];
2388 ctx->timefmt.len = value->len;
2389 ctx->timefmt.data = ngx_pnalloc(r->pool, value->len + 1);
2390 if (ctx->timefmt.data == NULL) {
2391 return NGX_HTTP_SSI_ERROR;
2394 ngx_cpystrn(ctx->timefmt.data, value->data, value->len + 1);
2397 value = params[NGX_HTTP_SSI_CONFIG_ERRMSG];
2400 ctx->errmsg = *value;
2408 ngx_http_ssi_set(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2412 ngx_str_t *name, *value, *vv;
2413 ngx_http_ssi_var_t *var;
2414 ngx_http_ssi_ctx_t *mctx;
2416 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
2418 if (mctx->variables == NULL) {
2419 mctx->variables = ngx_list_create(r->pool, 4,
2420 sizeof(ngx_http_ssi_var_t));
2421 if (mctx->variables == NULL) {
2426 name = params[NGX_HTTP_SSI_SET_VAR];
2427 value = params[NGX_HTTP_SSI_SET_VALUE];
2429 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2430 "ssi set \"%V\" \"%V\"", name, value);
2432 rc = ngx_http_ssi_evaluate_string(r, ctx, value, 0);
2438 key = ngx_hash_strlow(name->data, name->data, name->len);
2440 vv = ngx_http_ssi_get_variable(r, name, key);
2447 var = ngx_list_push(mctx->variables);
2454 var->value = *value;
2456 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2457 "set: \"%V\"=\"%V\"", name, value);
2464 ngx_http_ssi_if(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2468 ngx_str_t *expr, left, right;
2470 ngx_uint_t negative, noregex, flags;
2472 if (ctx->command.len == 2) {
2473 if (ctx->conditional) {
2474 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2475 "the \"if\" command inside the \"if\" command");
2476 return NGX_HTTP_SSI_ERROR;
2480 if (ctx->output_chosen) {
2485 expr = params[NGX_HTTP_SSI_IF_EXPR];
2487 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2488 "ssi if expr=\"%V\"", expr);
2490 left.data = expr->data;
2491 last = expr->data + expr->len;
2493 for (p = left.data; p < last; p++) {
2494 if (*p >= 'A' && *p <= 'Z') {
2499 if ((*p >= 'a' && *p <= 'z')
2500 || (*p >= '0' && *p <= '9')
2501 || *p == '$' || *p == '{' || *p == '}' || *p == '_'
2502 || *p == '"' || *p == '\'')
2510 left.len = p - left.data;
2512 while (p < last && *p == ' ') {
2518 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2519 "left: \"%V\"", &left);
2521 rc = ngx_http_ssi_evaluate_string(r, ctx, &left, flags);
2527 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2528 "evaluated left: \"%V\"", &left);
2533 ctx->output_chosen = 1;
2539 ctx->conditional = NGX_HTTP_SSI_COND_IF;
2544 if (p < last && *p == '=') {
2548 } else if (p + 1 < last && *p == '!' && *(p + 1) == '=') {
2553 goto invalid_expression;
2556 while (p < last && *p == ' ') {
2560 if (p < last - 1 && *p == '/') {
2561 if (*(last - 1) != '/') {
2562 goto invalid_expression;
2566 flags = NGX_HTTP_SSI_ADD_ZERO;
2574 if (p < last - 1 && p[0] == '\\' && p[1] == '/') {
2579 right.len = last - p;
2582 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2583 "right: \"%V\"", &right);
2585 rc = ngx_http_ssi_evaluate_string(r, ctx, &right, flags);
2591 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2592 "evaluated right: \"%V\"", &right);
2595 if (left.len != right.len) {
2599 rc = ngx_strncmp(left.data, right.data, right.len);
2603 right.data[right.len] = '\0';
2605 rc = ngx_http_ssi_regex_match(r, &right, &left);
2609 } else if (rc == NGX_DECLINED) {
2616 if ((rc == 0 && !negative) || (rc != 0 && negative)) {
2618 ctx->output_chosen = 1;
2624 ctx->conditional = NGX_HTTP_SSI_COND_IF;
2630 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2631 "invalid expression in \"%V\"", expr);
2633 return NGX_HTTP_SSI_ERROR;
2638 ngx_http_ssi_else(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2641 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2644 if (ctx->output_chosen) {
2650 ctx->conditional = NGX_HTTP_SSI_COND_ELSE;
2657 ngx_http_ssi_endif(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2660 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2664 ctx->output_chosen = 0;
2665 ctx->conditional = 0;
2672 ngx_http_ssi_block(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2675 ngx_http_ssi_ctx_t *mctx;
2676 ngx_http_ssi_block_t *bl;
2678 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2681 mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module);
2683 if (mctx->blocks == NULL) {
2684 mctx->blocks = ngx_array_create(r->pool, 4,
2685 sizeof(ngx_http_ssi_block_t));
2686 if (mctx->blocks == NULL) {
2687 return NGX_HTTP_SSI_ERROR;
2691 bl = ngx_array_push(mctx->blocks);
2693 return NGX_HTTP_SSI_ERROR;
2696 bl->name = *params[NGX_HTTP_SSI_BLOCK_NAME];
2708 ngx_http_ssi_endblock(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
2711 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2722 ngx_http_ssi_date_gmt_local_variable(ngx_http_request_t *r,
2723 ngx_http_variable_value_t *v, uintptr_t gmt)
2726 ngx_http_ssi_ctx_t *ctx;
2729 char buf[NGX_HTTP_SSI_DATE_LEN];
2732 v->no_cacheable = 0;
2737 ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
2739 timefmt = ctx ? &ctx->timefmt : &ngx_http_ssi_timefmt;
2741 if (timefmt->len == sizeof("%s") - 1
2742 && timefmt->data[0] == '%' && timefmt->data[1] == 's')
2744 v->data = ngx_pnalloc(r->pool, NGX_TIME_T_LEN);
2745 if (v->data == NULL) {
2749 v->len = ngx_sprintf(v->data, "%T", now) - v->data;
2755 ngx_libc_gmtime(now, &tm);
2757 ngx_libc_localtime(now, &tm);
2760 v->len = strftime(buf, NGX_HTTP_SSI_DATE_LEN,
2761 (char *) timefmt->data, &tm);
2766 v->data = ngx_pnalloc(r->pool, v->len);
2767 if (v->data == NULL) {
2771 ngx_memcpy(v->data, buf, v->len);
2778 ngx_http_ssi_preconfiguration(ngx_conf_t *cf)
2781 ngx_http_variable_t *var, *v;
2782 ngx_http_ssi_command_t *cmd;
2783 ngx_http_ssi_main_conf_t *smcf;
2785 for (v = ngx_http_ssi_vars; v->name.len; v++) {
2786 var = ngx_http_add_variable(cf, &v->name, v->flags);
2791 var->get_handler = v->get_handler;
2792 var->data = v->data;
2795 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
2797 for (cmd = ngx_http_ssi_commands; cmd->name.len; cmd++) {
2798 rc = ngx_hash_add_key(&smcf->commands, &cmd->name, cmd,
2799 NGX_HASH_READONLY_KEY);
2805 if (rc == NGX_BUSY) {
2806 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2807 "conflicting SSI command \"%V\"", &cmd->name);
2818 ngx_http_ssi_create_main_conf(ngx_conf_t *cf)
2820 ngx_http_ssi_main_conf_t *smcf;
2822 smcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_main_conf_t));
2827 smcf->commands.pool = cf->pool;
2828 smcf->commands.temp_pool = cf->temp_pool;
2830 if (ngx_hash_keys_array_init(&smcf->commands, NGX_HASH_SMALL) != NGX_OK) {
2839 ngx_http_ssi_init_main_conf(ngx_conf_t *cf, void *conf)
2841 ngx_http_ssi_main_conf_t *smcf = conf;
2843 ngx_hash_init_t hash;
2845 hash.hash = &smcf->hash;
2846 hash.key = ngx_hash_key;
2847 hash.max_size = 1024;
2848 hash.bucket_size = ngx_cacheline_size;
2849 hash.name = "ssi_command_hash";
2850 hash.pool = cf->pool;
2851 hash.temp_pool = NULL;
2853 if (ngx_hash_init(&hash, smcf->commands.keys.elts,
2854 smcf->commands.keys.nelts)
2857 return NGX_CONF_ERROR;
2865 ngx_http_ssi_create_loc_conf(ngx_conf_t *cf)
2867 ngx_http_ssi_loc_conf_t *slcf;
2869 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_loc_conf_t));
2875 * set by ngx_pcalloc():
2877 * conf->types = { NULL };
2878 * conf->types_keys = NULL;
2881 slcf->enable = NGX_CONF_UNSET;
2882 slcf->silent_errors = NGX_CONF_UNSET;
2883 slcf->ignore_recycled_buffers = NGX_CONF_UNSET;
2884 slcf->last_modified = NGX_CONF_UNSET;
2886 slcf->min_file_chunk = NGX_CONF_UNSET_SIZE;
2887 slcf->value_len = NGX_CONF_UNSET_SIZE;
2894 ngx_http_ssi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
2896 ngx_http_ssi_loc_conf_t *prev = parent;
2897 ngx_http_ssi_loc_conf_t *conf = child;
2899 ngx_conf_merge_value(conf->enable, prev->enable, 0);
2900 ngx_conf_merge_value(conf->silent_errors, prev->silent_errors, 0);
2901 ngx_conf_merge_value(conf->ignore_recycled_buffers,
2902 prev->ignore_recycled_buffers, 0);
2903 ngx_conf_merge_value(conf->last_modified, prev->last_modified, 0);
2905 ngx_conf_merge_size_value(conf->min_file_chunk, prev->min_file_chunk, 1024);
2906 ngx_conf_merge_size_value(conf->value_len, prev->value_len, 255);
2908 if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
2909 &prev->types_keys, &prev->types,
2910 ngx_http_html_default_types)
2913 return NGX_CONF_ERROR;
2921 ngx_http_ssi_filter_init(ngx_conf_t *cf)
2923 ngx_http_next_header_filter = ngx_http_top_header_filter;
2924 ngx_http_top_header_filter = ngx_http_ssi_header_filter;
2926 ngx_http_next_body_filter = ngx_http_top_body_filter;
2927 ngx_http_top_body_filter = ngx_http_ssi_body_filter;