1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_strings.h"
19 #include "apr_thread_proc.h"
23 #include "apr_optional.h"
25 #define APR_WANT_STRFUNC
26 #define APR_WANT_MEMFUNC
29 #include "ap_config.h"
30 #include "util_filter.h"
32 #include "http_config.h"
33 #include "http_core.h"
34 #include "http_request.h"
35 #include "http_core.h"
36 #include "http_protocol.h"
38 #include "http_main.h"
39 #include "util_script.h"
40 #include "http_core.h"
41 #include "mod_include.h"
44 /* helper for Latin1 <-> entity encoding */
45 #if APR_CHARSET_EBCDIC
46 #include "util_ebcdic.h"
47 #define RAW_ASCII_CHAR(ch) apr_xlate_conv_byte(ap_hdrs_from_ascii, \
49 #else /* APR_CHARSET_EBCDIC */
50 #define RAW_ASCII_CHAR(ch) (ch)
51 #endif /* !APR_CHARSET_EBCDIC */
55 * +-------------------------------------------------------+
57 * | Types and Structures
59 * +-------------------------------------------------------+
62 /* sll used for string expansion */
63 typedef struct result_item {
64 struct result_item *next;
69 /* conditional expression parser stuff */
96 typedef struct parse_node {
97 struct parse_node *parent;
98 struct parse_node *left;
99 struct parse_node *right;
116 const char *default_error_msg;
117 const char *default_time_fmt;
118 const char *undefined_echo;
120 signed char lastmodified;
122 signed char legacy_expr;
123 } include_dir_config;
126 const char *default_start_tag;
127 const char *default_end_tag;
128 } include_server_config;
130 /* main parser states */
135 PARSE_DIRECTIVE_POSTNAME,
136 PARSE_DIRECTIVE_TAIL,
137 PARSE_DIRECTIVE_POSTTAIL,
152 typedef struct arg_item {
153 struct arg_item *next;
157 apr_size_t value_len;
164 ap_regmatch_t match[AP_MAX_REG_MATCH];
171 apr_size_t pattern_len;
174 struct ssi_internal_ctx {
178 char quote; /* quote character value (or \0) */
179 apr_size_t parse_pos; /* parse position of partial matches */
180 apr_size_t bytes_read;
182 apr_bucket_brigade *tmp_bb;
184 const char *start_seq;
185 bndm_t *start_seq_pat;
187 apr_size_t end_seq_len;
188 char *directive; /* name of the current directive */
189 apr_size_t directive_len; /* length of the current directive name */
191 arg_item_t *current_arg; /* currently parsed argument */
192 arg_item_t *argv; /* all arguments */
194 backref_t *re; /* NULL if there wasn't a regex yet */
196 const char *undefined_echo;
197 apr_size_t undefined_echo_len;
199 char legacy_expr; /* use ap_expr or legacy mod_include
200 expression parser? */
202 ap_expr_eval_ctx_t *expr_eval_ctx; /* NULL if there wasn't an ap_expr yet */
203 const char *expr_vary_this; /* for use by ap_expr_eval_ctx */
204 const char *expr_err; /* for use by ap_expr_eval_ctx */
208 apr_bucket_brigade *bb;
215 * +-------------------------------------------------------+
217 * | Debugging Utilities
219 * +-------------------------------------------------------+
224 #define TYPE_TOKEN(token, ttype) do { \
225 (token)->type = ttype; \
226 (token)->s = #ttype; \
229 #define CREATE_NODE(ctx, name) do { \
230 (name) = apr_palloc((ctx)->dpool, sizeof(*(name))); \
231 (name)->parent = (name)->left = (name)->right = NULL; \
233 (name)->dump_done = 0; \
236 static void debug_printf(include_ctx_t *ctx, const char *fmt, ...)
242 debug__str = apr_pvsprintf(ctx->pool, fmt, ap);
245 APR_BRIGADE_INSERT_TAIL(ctx->intern->debug.bb, apr_bucket_pool_create(
246 debug__str, strlen(debug__str), ctx->pool,
247 ctx->intern->debug.f->c->bucket_alloc));
250 #define DUMP__CHILD(ctx, is, node, child) if (1) { \
251 parse_node_t *d__c = node->child; \
253 if (!d__c->dump_done) { \
254 if (d__c->parent != node) { \
255 debug_printf(ctx, "!!! Parse tree is not consistent !!!\n"); \
256 if (!d__c->parent) { \
257 debug_printf(ctx, "Parent of " #child " child node is " \
261 debug_printf(ctx, "Parent of " #child " child node " \
262 "points to another node (of type %s)!\n", \
263 d__c->parent->token.s); \
272 debug_printf(ctx, "%s(missing)\n", is); \
276 static void debug_dump_tree(include_ctx_t *ctx, parse_node_t *root)
278 parse_node_t *current;
282 debug_printf(ctx, " -- Parse Tree empty --\n\n");
286 debug_printf(ctx, " ----- Parse Tree -----\n");
291 switch (current->token.type) {
294 debug_printf(ctx, "%s%s (%s)\n", is, current->token.s,
295 current->token.value);
296 current->dump_done = 1;
297 current = current->parent;
304 if (!current->dump_done) {
305 debug_printf(ctx, "%s%s\n", is, current->token.s);
306 is = apr_pstrcat(ctx->dpool, is, " ", NULL);
307 current->dump_done = 1;
310 DUMP__CHILD(ctx, is, current, right)
312 if (!current->right || current->right->dump_done) {
313 is = apr_pstrmemdup(ctx->dpool, is, strlen(is) - 4);
314 if (current->right) current->right->dump_done = 0;
315 current = current->parent;
320 if (!current->dump_done) {
321 debug_printf(ctx, "%s%s\n", is, current->token.s);
322 is = apr_pstrcat(ctx->dpool, is, " ", NULL);
323 current->dump_done = 1;
326 DUMP__CHILD(ctx, is, current, left)
327 DUMP__CHILD(ctx, is, current, right)
329 if ((!current->left || current->left->dump_done) &&
330 (!current->right || current->right->dump_done)) {
332 is = apr_pstrmemdup(ctx->dpool, is, strlen(is) - 4);
333 if (current->left) current->left->dump_done = 0;
334 if (current->right) current->right->dump_done = 0;
335 current = current->parent;
341 /* it is possible to call this function within the parser loop, to see
342 * how the tree is built. That way, we must cleanup after us to dump
343 * always the whole tree
346 if (root->left) root->left->dump_done = 0;
347 if (root->right) root->right->dump_done = 0;
349 debug_printf(ctx, " --- End Parse Tree ---\n\n");
354 #define DEBUG_INIT(ctx, filter, brigade) do { \
355 (ctx)->intern->debug.f = filter; \
356 (ctx)->intern->debug.bb = brigade; \
359 #define DEBUG_PRINTF(arg) debug_printf arg
361 #define DEBUG_DUMP_TOKEN(ctx, token) do { \
362 token_t *d__t = (token); \
364 if (d__t->type == TOKEN_STRING || d__t->type == TOKEN_RE) { \
365 DEBUG_PRINTF(((ctx), " Found: %s (%s)\n", d__t->s, d__t->value)); \
368 DEBUG_PRINTF((ctx, " Found: %s\n", d__t->s)); \
372 #define DEBUG_DUMP_EVAL(ctx, node) do { \
374 switch ((node)->token.type) { \
376 debug_printf((ctx), " Evaluate: %s (%s) -> %c\n", (node)->token.s,\
377 (node)->token.value, ((node)->value) ? '1':'0'); \
381 debug_printf((ctx), " Evaluate: %s (Left: %s; Right: %s) -> %c\n",\
383 (((node)->left->done) ? ((node)->left->value ?"1":"0") \
384 : "short circuited"), \
385 (((node)->right->done) ? ((node)->right->value?"1":"0") \
386 : "short circuited"), \
387 (node)->value ? '1' : '0'); \
395 if ((node)->right->token.type == TOKEN_RE) c = '/'; \
396 debug_printf((ctx), " Compare: %s (\"%s\" with %c%s%c) -> %c\n", \
398 (node)->left->token.value, \
399 c, (node)->right->token.value, c, \
400 (node)->value ? '1' : '0'); \
403 debug_printf((ctx), " Evaluate: %s -> %c\n", (node)->token.s, \
404 (node)->value ? '1' : '0'); \
409 #define DEBUG_DUMP_UNMATCHED(ctx, unmatched) do { \
411 DEBUG_PRINTF(((ctx), " Unmatched %c\n", (char)(unmatched))); \
415 #define DEBUG_DUMP_COND(ctx, text) \
416 DEBUG_PRINTF(((ctx), "**** %s cond status=\"%c\"\n", (text), \
417 ((ctx)->flags & SSI_FLAG_COND_TRUE) ? '1' : '0'))
419 #define DEBUG_DUMP_TREE(ctx, root) debug_dump_tree(ctx, root)
421 #else /* DEBUG_INCLUDE */
423 #define TYPE_TOKEN(token, ttype) (token)->type = ttype
425 #define CREATE_NODE(ctx, name) do { \
426 (name) = apr_palloc((ctx)->dpool, sizeof(*(name))); \
427 (name)->parent = (name)->left = (name)->right = NULL; \
431 #define DEBUG_INIT(ctx, f, bb)
432 #define DEBUG_PRINTF(arg)
433 #define DEBUG_DUMP_TOKEN(ctx, token)
434 #define DEBUG_DUMP_EVAL(ctx, node)
435 #define DEBUG_DUMP_UNMATCHED(ctx, unmatched)
436 #define DEBUG_DUMP_COND(ctx, text)
437 #define DEBUG_DUMP_TREE(ctx, root)
439 #endif /* !DEBUG_INCLUDE */
443 * +-------------------------------------------------------+
445 * | Static Module Data
447 * +-------------------------------------------------------+
450 /* global module structure */
451 module AP_MODULE_DECLARE_DATA include_module;
453 /* function handlers for include directives */
454 static apr_hash_t *include_handlers;
456 /* forward declaration of handler registry */
457 static APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *ssi_pfn_register;
459 /* Sentinel value to store in subprocess_env for items that
460 * shouldn't be evaluated until/unless they're actually used
462 static const char lazy_eval_sentinel = '\0';
463 #define LAZY_VALUE (&lazy_eval_sentinel)
466 #define DEFAULT_START_SEQUENCE "<!--#"
467 #define DEFAULT_END_SEQUENCE "-->"
468 #define DEFAULT_ERROR_MSG "[an error occurred while processing this directive]"
469 #define DEFAULT_TIME_FORMAT "%A, %d-%b-%Y %H:%M:%S %Z"
470 #define DEFAULT_UNDEFINED_ECHO "(none)"
475 #define DEFAULT_XBITHACK XBITHACK_FULL
477 #define DEFAULT_XBITHACK XBITHACK_OFF
482 * +-------------------------------------------------------+
484 * | Environment/Expansion Functions
486 * +-------------------------------------------------------+
490 * decodes a string containing html entities or numeric character references.
491 * 's' is overwritten with the decoded string.
492 * If 's' is syntatically incorrect, then the followed fixups will be made:
493 * unknown entities will be left undecoded;
494 * references to unused numeric characters will be deleted.
495 * In particular, � will not be decoded, but will be deleted.
498 /* maximum length of any ISO-LATIN-1 HTML entity name. */
499 #define MAXENTLEN (6)
501 /* The following is a shrinking transformation, therefore safe. */
503 static void decodehtml(char *s)
508 static const char * const entlist[MAXENTLEN + 1] =
512 "lt\074gt\076", /* 2 */
513 "amp\046ETH\320eth\360", /* 3 */
514 "quot\042Auml\304Euml\313Iuml\317Ouml\326Uuml\334auml\344euml"
515 "\353iuml\357ouml\366uuml\374yuml\377", /* 4 */
517 "Acirc\302Aring\305AElig\306Ecirc\312Icirc\316Ocirc\324Ucirc"
518 "\333THORN\336szlig\337acirc\342aring\345aelig\346ecirc\352"
519 "icirc\356ocirc\364ucirc\373thorn\376", /* 5 */
521 "Agrave\300Aacute\301Atilde\303Ccedil\307Egrave\310Eacute\311"
522 "Igrave\314Iacute\315Ntilde\321Ograve\322Oacute\323Otilde"
523 "\325Oslash\330Ugrave\331Uacute\332Yacute\335agrave\340"
524 "aacute\341atilde\343ccedil\347egrave\350eacute\351igrave"
525 "\354iacute\355ntilde\361ograve\362oacute\363otilde\365"
526 "oslash\370ugrave\371uacute\372yacute\375" /* 6 */
529 /* Do a fast scan through the string until we find anything
530 * that needs more complicated handling
532 for (; *s != '&'; s++) {
538 for (p = s; *s != '\0'; s++, p++) {
543 /* find end of entity */
544 for (i = 1; s[i] != ';' && s[i] != '\0'; i++) {
548 if (s[i] == '\0') { /* treat as normal data */
553 /* is it numeric ? */
555 for (j = 2, val = 0; j < i && apr_isdigit(s[j]); j++) {
556 val = val * 10 + s[j] - '0';
559 if (j < i || val <= 8 || (val >= 11 && val <= 31) ||
560 (val >= 127 && val <= 160) || val >= 256) {
561 p--; /* no data to output */
564 *p = RAW_ASCII_CHAR(val);
569 if (j > MAXENTLEN || entlist[j] == NULL) {
572 continue; /* skip it */
574 for (ents = entlist[j]; *ents != '\0'; ents += i) {
575 if (strncmp(s + 1, ents, j) == 0) {
581 *p = '&'; /* unknown */
584 *p = RAW_ASCII_CHAR(((const unsigned char *) ents)[j]);
593 static void add_include_vars(request_rec *r)
595 apr_table_t *e = r->subprocess_env;
598 apr_table_setn(e, "DATE_LOCAL", LAZY_VALUE);
599 apr_table_setn(e, "DATE_GMT", LAZY_VALUE);
600 apr_table_setn(e, "LAST_MODIFIED", LAZY_VALUE);
601 apr_table_setn(e, "DOCUMENT_URI", r->uri);
602 if (r->path_info && *r->path_info) {
603 apr_table_setn(e, "DOCUMENT_PATH_INFO", r->path_info);
605 apr_table_setn(e, "USER_NAME", LAZY_VALUE);
606 if (r->filename && (t = strrchr(r->filename, '/'))) {
607 apr_table_setn(e, "DOCUMENT_NAME", ++t);
610 apr_table_setn(e, "DOCUMENT_NAME", r->uri);
613 char *arg_copy = apr_pstrdup(r->pool, r->args);
615 ap_unescape_url(arg_copy);
616 apr_table_setn(e, "QUERY_STRING_UNESCAPED",
617 ap_escape_shell_cmd(r->pool, arg_copy));
621 static const char *add_include_vars_lazy(request_rec *r, const char *var, const char *timefmt)
624 if (!strcasecmp(var, "DATE_LOCAL")) {
625 val = ap_ht_time(r->pool, r->request_time, timefmt, 0);
627 else if (!strcasecmp(var, "DATE_GMT")) {
628 val = ap_ht_time(r->pool, r->request_time, timefmt, 1);
630 else if (!strcasecmp(var, "LAST_MODIFIED")) {
631 val = ap_ht_time(r->pool, r->finfo.mtime, timefmt, 0);
633 else if (!strcasecmp(var, "USER_NAME")) {
634 if (apr_uid_name_get(&val, r->finfo.user, r->pool) != APR_SUCCESS) {
643 apr_table_setn(r->subprocess_env, var, val);
648 static const char *get_include_var(const char *var, include_ctx_t *ctx)
651 request_rec *r = ctx->r;
653 if (apr_isdigit(*var) && !var[1]) {
654 apr_size_t idx = *var - '0';
655 backref_t *re = ctx->intern->re;
657 /* Handle $0 .. $9 from the last regex evaluated.
658 * The choice of returning NULL strings on not-found,
659 * v.s. empty strings on an empty match is deliberate.
661 if (!re || !re->have_match) {
662 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
663 "regex capture $%" APR_SIZE_T_FMT " refers to no regex in %s",
667 else if (re->nsub < idx || idx >= AP_MAX_REG_MATCH) {
668 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
669 "regex capture $%" APR_SIZE_T_FMT
670 " is out of range (last regex was: '%s') in %s",
671 idx, re->rexp, r->filename);
674 else if (re->match[idx].rm_so < 0 || re->match[idx].rm_eo < 0) {
675 /* This particular subpattern was not used by the regex */
679 val = apr_pstrmemdup(ctx->dpool, re->source + re->match[idx].rm_so,
680 re->match[idx].rm_eo - re->match[idx].rm_so);
684 val = apr_table_get(r->subprocess_env, var);
686 if (val == LAZY_VALUE) {
687 val = add_include_vars_lazy(r, var, ctx->time_str);
694 static const char *include_expr_var_fn(ap_expr_eval_ctx_t *eval_ctx,
698 const char *res, *name = data;
699 include_ctx_t *ctx = eval_ctx->data;
700 if (name[0] == 'e') {
701 /* keep legacy "env" semantics */
702 if ((res = apr_table_get(ctx->r->notes, arg)) != NULL)
704 else if ((res = get_include_var(arg, ctx)) != NULL)
710 return get_include_var(arg, ctx);
714 static int include_expr_lookup(ap_expr_lookup_parms *parms)
716 switch (parms->type) {
717 case AP_EXPR_FUNC_STRING:
718 if (strcasecmp(parms->name, "v") == 0 ||
719 strcasecmp(parms->name, "reqenv") == 0 ||
720 strcasecmp(parms->name, "env") == 0) {
721 *parms->func = include_expr_var_fn;
722 *parms->data = parms->name;
727 * We could also make the SSI vars available as %{...} style variables
728 * (AP_EXPR_FUNC_VAR), but this would create problems if we ever want
729 * to cache parsed expressions for performance reasons.
732 return ap_run_expr_lookup(parms);
737 * Do variable substitution on strings
739 * (Note: If out==NULL, this function allocs a buffer for the resulting
740 * string from ctx->pool. The return value is always the parsed string)
742 static char *ap_ssi_parse_string(include_ctx_t *ctx, const char *in, char *out,
743 apr_size_t length, int leave_name)
745 request_rec *r = ctx->r;
746 result_item_t *result = NULL, *current = NULL;
747 apr_size_t outlen = 0, inlen, span;
748 char *ret = NULL, *eout = NULL;
752 /* sanity check, out && !length is not supported */
753 ap_assert(out && length);
756 eout = out + length - 1;
759 span = strcspn(in, "\\$");
765 apr_cpystrn(out, in, length);
768 ret = apr_pstrmemdup(ctx->pool, in, (length && length <= inlen)
769 ? length - 1 : inlen);
775 /* well, actually something to do */
780 memcpy(out, in, (out+span <= eout) ? span : (eout-out));
785 current = result = apr_palloc(ctx->dpool, sizeof(*result));
786 current->next = NULL;
787 current->string = in;
792 /* loop for specials */
794 if ((out && out >= eout) || (length && outlen >= length)) {
798 /* prepare next entry */
799 if (!out && current->len) {
800 current->next = apr_palloc(ctx->dpool, sizeof(*current->next));
801 current = current->next;
802 current->next = NULL;
811 *out++ = (p[1] == '$') ? *++p : *p;
816 current->string = (p[1] == '$') ? ++p : p;
825 else { /* *p == '$' */
826 const char *newp = NULL, *ep, *key = NULL;
829 ep = ap_strchr_c(++p, '}');
831 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Missing '}' on "
832 "variable \"%s\" in %s", p, r->filename);
837 key = apr_pstrmemdup(ctx->dpool, p, ep - p);
844 while (*ep == '_' || apr_isalnum(*ep)) {
849 key = apr_pstrmemdup(ctx->dpool, p, ep - p);
855 /* empty name results in a copy of '$' in the output string */
862 current->string = p++;
867 const char *val = get_include_var(key, ctx);
873 else if (leave_name) {
880 memcpy(out, val, (out+len <= eout) ? len : (eout-out));
885 current->string = val;
894 if ((out && out >= eout) || (length && outlen >= length)) {
898 /* check the remainder */
899 if (*p && (span = strcspn(p, "\\$")) > 0) {
900 if (!out && current->len) {
901 current->next = apr_palloc(ctx->dpool, sizeof(*current->next));
902 current = current->next;
903 current->next = NULL;
907 memcpy(out, p, (out+span <= eout) ? span : (eout-out));
918 } while (p < in+inlen);
920 /* assemble result */
932 if (length && outlen > length) {
936 ret = out = apr_palloc(ctx->pool, outlen + 1);
941 memcpy(out, result->string, (out+result->len <= ep)
942 ? result->len : (ep-out));
945 result = result->next;
946 } while (result && out < ep);
956 * +-------------------------------------------------------+
958 * | Conditional Expression Parser
960 * +-------------------------------------------------------+
963 static APR_INLINE int re_check(include_ctx_t *ctx, const char *string,
966 ap_regex_t *compiled;
967 backref_t *re = ctx->intern->re;
969 compiled = ap_pregcomp(ctx->dpool, rexp, AP_REG_EXTENDED);
971 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "unable to "
972 "compile pattern \"%s\"", rexp);
977 re = ctx->intern->re = apr_palloc(ctx->pool, sizeof(*re));
980 re->source = apr_pstrdup(ctx->pool, string);
981 re->rexp = apr_pstrdup(ctx->pool, rexp);
982 re->nsub = compiled->re_nsub;
983 re->have_match = !ap_regexec(compiled, string, AP_MAX_REG_MATCH,
986 ap_pregfree(ctx->dpool, compiled);
987 return re->have_match;
990 static int get_ptoken(include_ctx_t *ctx, const char **parse, token_t *token, token_t *previous)
1002 /* Skip leading white space */
1003 while (apr_isspace(**parse)) {
1012 TYPE_TOKEN(token, TOKEN_STRING); /* the default type */
1016 switch (*(*parse)++) {
1018 TYPE_TOKEN(token, TOKEN_LBRACE);
1021 TYPE_TOKEN(token, TOKEN_RBRACE);
1024 if (**parse == '=') ++*parse;
1025 TYPE_TOKEN(token, TOKEN_EQ);
1028 if (**parse == '=') {
1029 TYPE_TOKEN(token, TOKEN_NE);
1033 TYPE_TOKEN(token, TOKEN_NOT);
1039 /* if last token was ACCESS, this token is STRING */
1040 if (previous != NULL && TOKEN_ACCESS == previous->type) {
1043 TYPE_TOKEN(token, TOKEN_RE);
1047 if (**parse == '|') {
1048 TYPE_TOKEN(token, TOKEN_OR);
1054 if (**parse == '&') {
1055 TYPE_TOKEN(token, TOKEN_AND);
1061 if (**parse == '=') {
1062 TYPE_TOKEN(token, TOKEN_GE);
1066 TYPE_TOKEN(token, TOKEN_GT);
1069 if (**parse == '=') {
1070 TYPE_TOKEN(token, TOKEN_LE);
1074 TYPE_TOKEN(token, TOKEN_LT);
1077 if (**parse == 'A') {
1078 TYPE_TOKEN(token, TOKEN_ACCESS);
1085 /* It's a string or regex token
1086 * Now search for the next token, which finishes this string
1089 p = *parse = token->value = unmatched ? *parse : p;
1091 for (; **parse; p = ++*parse) {
1092 if (**parse == '\\') {
1102 if (**parse == unmatched) {
1107 } else if (apr_isspace(**parse)) {
1125 if ((*parse)[1] == **parse) {
1139 token->value = apr_pstrdup(ctx->dpool, "");
1142 apr_size_t len = p - token->value - shift;
1143 char *c = apr_palloc(ctx->dpool, len + 1);
1149 const char *e = ap_strchr_c(p, '\\');
1167 static int parse_expr(include_ctx_t *ctx, const char *expr, int *was_error)
1169 parse_node_t *new, *root = NULL, *current = NULL;
1170 request_rec *r = ctx->r;
1171 request_rec *rr = NULL;
1172 const char *error = "Invalid expression \"%s\" in file %s";
1173 const char *parse = expr;
1182 /* Create Parse Tree */
1184 /* uncomment this to see how the tree a built:
1186 * DEBUG_DUMP_TREE(ctx, root);
1188 CREATE_NODE(ctx, new);
1191 #ifdef DEBUG_INCLUDE
1194 get_ptoken(ctx, &parse, &new->token,
1195 (current != NULL ? ¤t->token : NULL));
1199 DEBUG_DUMP_UNMATCHED(ctx, was_unmatched);
1200 DEBUG_DUMP_TOKEN(ctx, &new->token);
1204 switch (new->token.type) {
1209 root = current = new;
1213 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, error, expr,
1220 switch (new->token.type) {
1222 switch (current->token.type) {
1224 current->token.value =
1225 apr_pstrcat(ctx->dpool, current->token.value,
1226 *current->token.value ? " " : "",
1227 new->token.value, NULL);
1236 new->parent = current;
1237 current = current->right = new;
1243 switch (current->token.type) {
1246 new->parent = current;
1247 current = current->right = new;
1258 switch (current->token.type) {
1262 current = current->parent;
1265 switch (current->token.type) {
1272 current = current->parent;
1281 current = root = new;
1285 new->left = current->right;
1286 new->left->parent = new;
1287 new->parent = current;
1288 current = current->right = new;
1302 if (current->token.type == TOKEN_STRING) {
1303 current = current->parent;
1308 current = root = new;
1312 switch (current->token.type) {
1316 new->left = current->right;
1317 new->left->parent = new;
1318 new->parent = current;
1319 current = current->right = new;
1329 while (current && current->token.type != TOKEN_LBRACE) {
1330 current = current->parent;
1334 TYPE_TOKEN(¤t->token, TOKEN_GROUP);
1338 error = "Unmatched ')' in \"%s\" in file %s";
1344 switch (current->token.type) {
1352 current->right = new;
1353 new->parent = current;
1363 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, error, expr, r->filename);
1368 DEBUG_DUMP_TREE(ctx, root);
1370 /* Evaluate Parse Tree */
1374 switch (current->token.type) {
1376 current->token.value =
1377 ap_ssi_parse_string(ctx, current->token.value, NULL, 0,
1378 SSI_EXPAND_DROP_NAME);
1379 current->value = !!*current->token.value;
1384 if (!current->left || !current->right) {
1385 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1386 "Invalid expression \"%s\" in file %s",
1392 if (!current->left->done) {
1393 switch (current->left->token.type) {
1395 current->left->token.value =
1396 ap_ssi_parse_string(ctx, current->left->token.value,
1397 NULL, 0, SSI_EXPAND_DROP_NAME);
1398 current->left->value = !!*current->left->token.value;
1399 DEBUG_DUMP_EVAL(ctx, current->left);
1400 current->left->done = 1;
1404 current = current->left;
1409 /* short circuit evaluation */
1410 if (!current->right->done && !regex &&
1411 ((current->token.type == TOKEN_AND && !current->left->value) ||
1412 (current->token.type == TOKEN_OR && current->left->value))) {
1413 current->value = current->left->value;
1416 if (!current->right->done) {
1417 switch (current->right->token.type) {
1419 current->right->token.value =
1420 ap_ssi_parse_string(ctx,current->right->token.value,
1421 NULL, 0, SSI_EXPAND_DROP_NAME);
1422 current->right->value = !!*current->right->token.value;
1423 DEBUG_DUMP_EVAL(ctx, current->right);
1424 current->right->done = 1;
1428 current = current->right;
1433 if (current->token.type == TOKEN_AND) {
1434 current->value = current->left->value &&
1435 current->right->value;
1438 current->value = current->left->value ||
1439 current->right->value;
1446 if (!current->left || !current->right ||
1447 current->left->token.type != TOKEN_STRING ||
1448 (current->right->token.type != TOKEN_STRING &&
1449 current->right->token.type != TOKEN_RE)) {
1450 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1451 "Invalid expression \"%s\" in file %s",
1456 current->left->token.value =
1457 ap_ssi_parse_string(ctx, current->left->token.value, NULL, 0,
1458 SSI_EXPAND_DROP_NAME);
1459 current->right->token.value =
1460 ap_ssi_parse_string(ctx, current->right->token.value, NULL, 0,
1461 SSI_EXPAND_DROP_NAME);
1463 if (current->right->token.type == TOKEN_RE) {
1464 current->value = re_check(ctx, current->left->token.value,
1465 current->right->token.value);
1469 current->value = !strcmp(current->left->token.value,
1470 current->right->token.value);
1473 if (current->token.type == TOKEN_NE) {
1474 current->value = !current->value;
1482 if (!current->left || !current->right ||
1483 current->left->token.type != TOKEN_STRING ||
1484 current->right->token.type != TOKEN_STRING) {
1485 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1486 "Invalid expression \"%s\" in file %s",
1492 current->left->token.value =
1493 ap_ssi_parse_string(ctx, current->left->token.value, NULL, 0,
1494 SSI_EXPAND_DROP_NAME);
1495 current->right->token.value =
1496 ap_ssi_parse_string(ctx, current->right->token.value, NULL, 0,
1497 SSI_EXPAND_DROP_NAME);
1499 current->value = strcmp(current->left->token.value,
1500 current->right->token.value);
1502 switch (current->token.type) {
1503 case TOKEN_GE: current->value = current->value >= 0; break;
1504 case TOKEN_GT: current->value = current->value > 0; break;
1505 case TOKEN_LE: current->value = current->value <= 0; break;
1506 case TOKEN_LT: current->value = current->value < 0; break;
1507 default: current->value = 0; break; /* should not happen */
1513 if (current->right) {
1514 if (!current->right->done) {
1515 current = current->right;
1518 current->value = current->right->value;
1524 if (current->token.type == TOKEN_NOT) {
1525 current->value = !current->value;
1530 if (current->left || !current->right ||
1531 (current->right->token.type != TOKEN_STRING &&
1532 current->right->token.type != TOKEN_RE)) {
1533 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1534 "Invalid expression \"%s\" in file %s: Token '-A' must be followed by a URI string.",
1539 current->right->token.value =
1540 ap_ssi_parse_string(ctx, current->right->token.value, NULL, 0,
1541 SSI_EXPAND_DROP_NAME);
1542 rr = ap_sub_req_lookup_uri(current->right->token.value, r, NULL);
1543 /* 400 and higher are considered access denied */
1544 if (rr->status < HTTP_BAD_REQUEST) {
1549 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rr->status, r,
1550 "mod_include: The tested "
1551 "subrequest -A \"%s\" returned an error code.",
1552 current->right->token.value);
1554 ap_destroy_sub_req(rr);
1559 error = "No operator before regex in expr \"%s\" in file %s";
1563 error = "Unmatched '(' in \"%s\" in file %s";
1567 error = "internal parser error in \"%s\" in file %s";
1570 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, error, expr,r->filename);
1575 DEBUG_DUMP_EVAL(ctx, current);
1577 current = current->parent;
1580 return (root ? root->value : 0);
1583 /* same as above, but use common ap_expr syntax / API */
1584 static int parse_ap_expr(include_ctx_t *ctx, const char *expr, int *was_error)
1586 ap_expr_info_t expr_info;
1589 backref_t *re = ctx->intern->re;
1590 ap_expr_eval_ctx_t *eval_ctx = ctx->intern->expr_eval_ctx;
1592 expr_info.filename = ctx->r->filename;
1593 expr_info.line_number = 0;
1594 expr_info.module_index = APLOG_MODULE_INDEX;
1595 expr_info.flags = AP_EXPR_FLAG_RESTRICTED;
1596 err = ap_expr_parse(ctx->r->pool, ctx->r->pool, &expr_info, expr,
1597 include_expr_lookup);
1599 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r,
1600 "Could not parse expr \"%s\" in %s: %s", expr,
1601 ctx->r->filename, err);
1607 ctx->intern->re = re = apr_pcalloc(ctx->pool, sizeof(*re));
1610 /* ap_expr_exec_ctx() does not care about re->have_match but only about
1613 if (!re->have_match)
1618 eval_ctx = apr_pcalloc(ctx->pool, sizeof(*eval_ctx));
1619 ctx->intern->expr_eval_ctx = eval_ctx;
1620 eval_ctx->r = ctx->r;
1621 eval_ctx->c = ctx->r->connection;
1622 eval_ctx->s = ctx->r->server;
1623 eval_ctx->p = ctx->r->pool;
1624 eval_ctx->data = ctx;
1625 eval_ctx->err = &ctx->intern->expr_err;
1626 eval_ctx->vary_this = &ctx->intern->expr_vary_this;
1627 eval_ctx->re_nmatch = AP_MAX_REG_MATCH;
1628 eval_ctx->re_pmatch = re->match;
1629 eval_ctx->re_source = &re->source;
1632 eval_ctx->info = &expr_info;
1633 ret = ap_expr_exec_ctx(eval_ctx);
1635 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r,
1636 "Could not evaluate expr \"%s\" in %s: %s", expr,
1637 ctx->r->filename, ctx->intern->expr_err);
1648 * +-------------------------------------------------------+
1652 * +-------------------------------------------------------+
1656 * Extract the next tag name and value.
1657 * If there are no more tags, set the tag name to NULL.
1658 * The tag value is html decoded if dodecode is non-zero.
1659 * The tag value may be NULL if there is no tag value..
1661 static void ap_ssi_get_tag_and_value(include_ctx_t *ctx, char **tag,
1662 char **tag_val, int dodecode)
1664 if (!ctx->intern->argv) {
1671 *tag_val = ctx->intern->argv->value;
1672 *tag = ctx->intern->argv->name;
1674 ctx->intern->argv = ctx->intern->argv->next;
1676 if (dodecode && *tag_val) {
1677 decodehtml(*tag_val);
1683 static int find_file(request_rec *r, const char *directive, const char *tag,
1684 char *tag_val, apr_finfo_t *finfo)
1686 char *to_send = tag_val;
1687 request_rec *rr = NULL;
1689 char *error_fmt = NULL;
1690 apr_status_t rv = APR_SUCCESS;
1692 if (!strcmp(tag, "file")) {
1695 /* be safe; only files in this directory or below allowed */
1696 rv = apr_filepath_merge(&newpath, NULL, tag_val,
1697 APR_FILEPATH_SECUREROOTTEST |
1698 APR_FILEPATH_NOTABSOLUTE, r->pool);
1700 if (rv != APR_SUCCESS) {
1701 error_fmt = "unable to access file \"%s\" "
1702 "in parsed file %s";
1705 /* note: it is okay to pass NULL for the "next filter" since
1706 we never attempt to "run" this sub request. */
1707 rr = ap_sub_req_lookup_file(newpath, r, NULL);
1709 if (rr->status == HTTP_OK && rr->finfo.filetype != APR_NOFILE) {
1710 to_send = rr->filename;
1711 if ((rv = apr_stat(finfo, to_send,
1712 APR_FINFO_GPROT | APR_FINFO_MIN, rr->pool)) != APR_SUCCESS
1713 && rv != APR_INCOMPLETE) {
1714 error_fmt = "unable to get information about \"%s\" "
1715 "in parsed file %s";
1719 error_fmt = "unable to lookup information about \"%s\" "
1720 "in parsed file %s";
1726 ap_log_rerror(APLOG_MARK, APLOG_ERR,
1727 rv, r, error_fmt, to_send, r->filename);
1730 if (rr) ap_destroy_sub_req(rr);
1734 else if (!strcmp(tag, "virtual")) {
1735 /* note: it is okay to pass NULL for the "next filter" since
1736 we never attempt to "run" this sub request. */
1737 rr = ap_sub_req_lookup_uri(tag_val, r, NULL);
1739 if (rr->status == HTTP_OK && rr->finfo.filetype != APR_NOFILE) {
1740 memcpy((char *) finfo, (const char *) &rr->finfo,
1742 ap_destroy_sub_req(rr);
1746 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unable to get "
1747 "information about \"%s\" in parsed file %s",
1748 tag_val, r->filename);
1749 ap_destroy_sub_req(rr);
1754 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter \"%s\" "
1755 "to tag %s in %s", tag, directive, r->filename);
1761 * <!--#include virtual|file="..." [onerror|virtual|file="..."] ... -->
1763 * Output each file/virtual in turn until one of them returns an error.
1764 * On error, ignore all further file/virtual attributes until we reach
1765 * an onerror attribute, where we make an attempt to serve the onerror
1766 * virtual url. If onerror fails, or no onerror is present, the default
1767 * error string is inserted into the stream.
1769 static apr_status_t handle_include(include_ctx_t *ctx, ap_filter_t *f,
1770 apr_bucket_brigade *bb)
1772 request_rec *r = f->r;
1776 ap_log_rerror(APLOG_MARK,
1777 (ctx->flags & SSI_FLAG_PRINTING)
1778 ? APLOG_ERR : APLOG_WARNING,
1779 0, r, "missing argument for include element in %s",
1783 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
1788 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1795 char *tag_val = NULL;
1796 request_rec *rr = NULL;
1797 char *error_fmt = NULL;
1798 char *parsed_string;
1800 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, SSI_VALUE_DECODED);
1801 if (!tag || !tag_val) {
1805 if (strcmp(tag, "virtual") && strcmp(tag, "file") && strcmp(tag,
1807 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter "
1808 "\"%s\" to tag include in %s", tag, r->filename);
1809 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1813 parsed_string = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
1814 SSI_EXPAND_DROP_NAME);
1815 if (tag[0] == 'f') {
1819 /* be safe; only files in this directory or below allowed */
1820 rv = apr_filepath_merge(&newpath, NULL, parsed_string,
1821 APR_FILEPATH_SECUREROOTTEST |
1822 APR_FILEPATH_NOTABSOLUTE, ctx->dpool);
1824 if (rv != APR_SUCCESS) {
1825 error_fmt = "unable to include file \"%s\" in parsed file %s";
1828 rr = ap_sub_req_lookup_file(newpath, r, f->next);
1831 else if ((tag[0] == 'v' && !last_error)
1832 || (tag[0] == 'o' && last_error)) {
1834 rr = ap_sub_req_method_uri(r->method, parsed_string, r, f->next);
1837 rr = ap_sub_req_lookup_uri(parsed_string, r, f->next);
1844 if (!error_fmt && rr->status != HTTP_OK) {
1845 error_fmt = "unable to include \"%s\" in parsed file %s";
1848 if (!error_fmt && (ctx->flags & SSI_FLAG_NO_EXEC) &&
1849 rr->content_type && strncmp(rr->content_type, "text/", 5)) {
1851 error_fmt = "unable to include potential exec \"%s\" in parsed "
1855 /* See the Kludge in includes_filter for why.
1856 * Basically, it puts a bread crumb in here, then looks
1857 * for the crumb later to see if its been here.
1860 ap_set_module_config(rr->request_config, &include_module, r);
1863 if (!error_fmt && ap_run_sub_req(rr)) {
1864 error_fmt = "unable to include \"%s\" in parsed file %s";
1868 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, error_fmt, tag_val,
1871 /* onerror threw an error, give up completely */
1874 last_error = error_fmt;
1880 /* Do *not* destroy the subrequest here; it may have allocated
1881 * variables in this r->subprocess_env in the subrequest's
1882 * r->pool, so that pool must survive as long as this request.
1883 * Yes, this is a memory leak. */
1888 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1895 * <!--#echo [decoding="..."] [encoding="..."] var="..." [decoding="..."]
1896 * [encoding="..."] var="..." ... -->
1898 static apr_status_t handle_echo(include_ctx_t *ctx, ap_filter_t *f,
1899 apr_bucket_brigade *bb)
1901 const char *encoding = "entity", *decoding = "none";
1902 request_rec *r = f->r;
1906 ap_log_rerror(APLOG_MARK,
1907 (ctx->flags & SSI_FLAG_PRINTING)
1908 ? APLOG_ERR : APLOG_WARNING,
1909 0, r, "missing argument for echo element in %s",
1913 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
1918 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1924 char *tag_val = NULL;
1926 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, SSI_VALUE_DECODED);
1927 if (!tag || !tag_val) {
1931 if (!strcmp(tag, "var")) {
1933 const char *echo_text = NULL;
1936 val = get_include_var(ap_ssi_parse_string(ctx, tag_val, NULL,
1937 0, SSI_EXPAND_DROP_NAME),
1942 char *e, *d, *token;
1946 d = apr_pstrdup(ctx->pool, decoding);
1947 token = apr_strtok(d, ", \t", &last);
1950 if (!strcasecmp(token, "none")) {
1953 else if (!strcasecmp(token, "url")) {
1954 char *buf = apr_pstrdup(ctx->pool, echo_text);
1955 ap_unescape_url(buf);
1958 else if (!strcasecmp(token, "urlencoded")) {
1959 char *buf = apr_pstrdup(ctx->pool, echo_text);
1960 ap_unescape_urlencoded(buf);
1963 else if (!strcasecmp(token, "entity")) {
1964 char *buf = apr_pstrdup(ctx->pool, echo_text);
1968 else if (!strcasecmp(token, "base64")) {
1969 echo_text = ap_pbase64decode(ctx->dpool, echo_text);
1972 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown value "
1973 "\"%s\" to parameter \"decoding\" of tag echo in "
1974 "%s", token, r->filename);
1975 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1979 token = apr_strtok(NULL, ", \t", &last);
1982 e = apr_pstrdup(ctx->pool, encoding);
1983 token = apr_strtok(e, ", \t", &last);
1986 if (!strcasecmp(token, "none")) {
1989 else if (!strcasecmp(token, "url")) {
1990 echo_text = ap_escape_uri(ctx->dpool, echo_text);
1992 else if (!strcasecmp(token, "urlencoded")) {
1993 echo_text = ap_escape_urlencoded(ctx->dpool, echo_text);
1995 else if (!strcasecmp(token, "entity")) {
1996 echo_text = ap_escape_html2(ctx->dpool, echo_text, 0);
1998 else if (!strcasecmp(token, "base64")) {
2000 buf = ap_pbase64encode(ctx->dpool, (char *)echo_text);
2004 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown value "
2005 "\"%s\" to parameter \"encoding\" of tag echo in "
2006 "%s", token, r->filename);
2007 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2011 token = apr_strtok(NULL, ", \t", &last);
2014 e_len = strlen(echo_text);
2017 echo_text = ctx->intern->undefined_echo;
2018 e_len = ctx->intern->undefined_echo_len;
2025 APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pool_create(
2026 apr_pmemdup(ctx->pool, echo_text, e_len),
2027 e_len, ctx->pool, f->c->bucket_alloc));
2029 else if (!strcmp(tag, "decoding")) {
2032 else if (!strcmp(tag, "encoding")) {
2036 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter "
2037 "\"%s\" in tag echo of %s", tag, r->filename);
2038 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2047 * <!--#config [timefmt="..."] [sizefmt="..."] [errmsg="..."]
2048 * [echomsg="..."] -->
2050 static apr_status_t handle_config(include_ctx_t *ctx, ap_filter_t *f,
2051 apr_bucket_brigade *bb)
2053 request_rec *r = f->r;
2054 apr_table_t *env = r->subprocess_env;
2057 ap_log_rerror(APLOG_MARK,
2058 (ctx->flags & SSI_FLAG_PRINTING)
2059 ? APLOG_ERR : APLOG_WARNING,
2060 0, r, "missing argument for config element in %s",
2064 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
2069 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2075 char *tag_val = NULL;
2077 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, SSI_VALUE_RAW);
2078 if (!tag || !tag_val) {
2082 if (!strcmp(tag, "errmsg")) {
2083 ctx->error_str = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
2084 SSI_EXPAND_DROP_NAME);
2086 else if (!strcmp(tag, "echomsg")) {
2087 ctx->intern->undefined_echo =
2088 ap_ssi_parse_string(ctx, tag_val, NULL, 0,SSI_EXPAND_DROP_NAME);
2089 ctx->intern->undefined_echo_len=strlen(ctx->intern->undefined_echo);
2091 else if (!strcmp(tag, "timefmt")) {
2092 apr_time_t date = r->request_time;
2094 ctx->time_str = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
2095 SSI_EXPAND_DROP_NAME);
2097 apr_table_setn(env, "DATE_LOCAL", ap_ht_time(r->pool, date,
2099 apr_table_setn(env, "DATE_GMT", ap_ht_time(r->pool, date,
2101 apr_table_setn(env, "LAST_MODIFIED",
2102 ap_ht_time(r->pool, r->finfo.mtime,
2105 else if (!strcmp(tag, "sizefmt")) {
2106 char *parsed_string;
2108 parsed_string = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
2109 SSI_EXPAND_DROP_NAME);
2110 if (!strcmp(parsed_string, "bytes")) {
2111 ctx->flags |= SSI_FLAG_SIZE_IN_BYTES;
2113 else if (!strcmp(parsed_string, "abbrev")) {
2114 ctx->flags &= SSI_FLAG_SIZE_ABBREV;
2117 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown value "
2118 "\"%s\" to parameter \"sizefmt\" of tag config "
2119 "in %s", parsed_string, r->filename);
2120 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2125 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter "
2126 "\"%s\" to tag config in %s", tag, r->filename);
2127 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2136 * <!--#fsize virtual|file="..." [virtual|file="..."] ... -->
2138 static apr_status_t handle_fsize(include_ctx_t *ctx, ap_filter_t *f,
2139 apr_bucket_brigade *bb)
2141 request_rec *r = f->r;
2144 ap_log_rerror(APLOG_MARK,
2145 (ctx->flags & SSI_FLAG_PRINTING)
2146 ? APLOG_ERR : APLOG_WARNING,
2147 0, r, "missing argument for fsize element in %s",
2151 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
2156 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2162 char *tag_val = NULL;
2164 char *parsed_string;
2166 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, SSI_VALUE_DECODED);
2167 if (!tag || !tag_val) {
2171 parsed_string = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
2172 SSI_EXPAND_DROP_NAME);
2174 if (!find_file(r, "fsize", tag, parsed_string, &finfo)) {
2178 if (!(ctx->flags & SSI_FLAG_SIZE_IN_BYTES)) {
2179 buf = apr_strfsize(finfo.size, apr_palloc(ctx->pool, 5));
2180 len = 4; /* omit the \0 terminator */
2183 apr_size_t l, x, pos;
2186 tmp = apr_psprintf(ctx->dpool, "%" APR_OFF_T_FMT, finfo.size);
2187 len = l = strlen(tmp);
2189 for (x = 0; x < l; ++x) {
2190 if (x && !((l - x) % 3)) {
2196 buf = apr_pstrmemdup(ctx->pool, tmp, len);
2199 buf = apr_palloc(ctx->pool, len);
2201 for (pos = x = 0; x < l; ++x) {
2202 if (x && !((l - x) % 3)) {
2205 buf[pos++] = tmp[x];
2210 APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pool_create(buf, len,
2211 ctx->pool, f->c->bucket_alloc));
2214 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2223 * <!--#flastmod virtual|file="..." [virtual|file="..."] ... -->
2225 static apr_status_t handle_flastmod(include_ctx_t *ctx, ap_filter_t *f,
2226 apr_bucket_brigade *bb)
2228 request_rec *r = f->r;
2231 ap_log_rerror(APLOG_MARK,
2232 (ctx->flags & SSI_FLAG_PRINTING)
2233 ? APLOG_ERR : APLOG_WARNING,
2234 0, r, "missing argument for flastmod element in %s",
2238 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
2243 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2249 char *tag_val = NULL;
2251 char *parsed_string;
2253 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, SSI_VALUE_DECODED);
2254 if (!tag || !tag_val) {
2258 parsed_string = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
2259 SSI_EXPAND_DROP_NAME);
2261 if (!find_file(r, "flastmod", tag, parsed_string, &finfo)) {
2265 t_val = ap_ht_time(ctx->pool, finfo.mtime, ctx->time_str, 0);
2266 t_len = strlen(t_val);
2268 APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pool_create(t_val, t_len,
2269 ctx->pool, f->c->bucket_alloc));
2272 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2281 * <!--#if expr="..." -->
2283 static apr_status_t handle_if(include_ctx_t *ctx, ap_filter_t *f,
2284 apr_bucket_brigade *bb)
2288 request_rec *r = f->r;
2289 int expr_ret, was_error;
2291 if (ctx->argc != 1) {
2292 ap_log_rerror(APLOG_MARK,
2293 (ctx->flags & SSI_FLAG_PRINTING)
2294 ? APLOG_ERR : APLOG_WARNING,
2296 ? "too many arguments for if element in %s"
2297 : "missing expr argument for if element in %s",
2301 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
2302 ++(ctx->if_nesting_level);
2306 if (ctx->argc != 1) {
2307 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2311 ap_ssi_get_tag_and_value(ctx, &tag, &expr, SSI_VALUE_RAW);
2313 if (strcmp(tag, "expr")) {
2314 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter \"%s\" "
2315 "to tag if in %s", tag, r->filename);
2316 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2321 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "missing expr value for if "
2322 "element in %s", r->filename);
2323 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2327 DEBUG_PRINTF((ctx, "**** if expr=\"%s\"\n", expr));
2329 if (ctx->intern->legacy_expr)
2330 expr_ret = parse_expr(ctx, expr, &was_error);
2332 expr_ret = parse_ap_expr(ctx, expr, &was_error);
2335 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2340 ctx->flags |= (SSI_FLAG_PRINTING | SSI_FLAG_COND_TRUE);
2343 ctx->flags &= SSI_FLAG_CLEAR_PRINT_COND;
2346 DEBUG_DUMP_COND(ctx, " if");
2348 ctx->if_nesting_level = 0;
2354 * <!--#elif expr="..." -->
2356 static apr_status_t handle_elif(include_ctx_t *ctx, ap_filter_t *f,
2357 apr_bucket_brigade *bb)
2361 request_rec *r = f->r;
2362 int expr_ret, was_error;
2364 if (ctx->argc != 1) {
2365 ap_log_rerror(APLOG_MARK,
2366 (!(ctx->if_nesting_level)) ? APLOG_ERR : APLOG_WARNING,
2368 ? "too many arguments for if element in %s"
2369 : "missing expr argument for if element in %s",
2373 if (ctx->if_nesting_level) {
2377 if (ctx->argc != 1) {
2378 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2382 ap_ssi_get_tag_and_value(ctx, &tag, &expr, SSI_VALUE_RAW);
2384 if (strcmp(tag, "expr")) {
2385 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter \"%s\" "
2386 "to tag if in %s", tag, r->filename);
2387 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2392 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "missing expr in elif "
2393 "statement: %s", r->filename);
2394 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2398 DEBUG_PRINTF((ctx, "**** elif expr=\"%s\"\n", expr));
2399 DEBUG_DUMP_COND(ctx, " elif");
2401 if (ctx->flags & SSI_FLAG_COND_TRUE) {
2402 ctx->flags &= SSI_FLAG_CLEAR_PRINTING;
2406 expr_ret = parse_expr(ctx, expr, &was_error);
2409 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2414 ctx->flags |= (SSI_FLAG_PRINTING | SSI_FLAG_COND_TRUE);
2417 ctx->flags &= SSI_FLAG_CLEAR_PRINT_COND;
2420 DEBUG_DUMP_COND(ctx, " elif");
2428 static apr_status_t handle_else(include_ctx_t *ctx, ap_filter_t *f,
2429 apr_bucket_brigade *bb)
2431 request_rec *r = f->r;
2434 ap_log_rerror(APLOG_MARK,
2435 (!(ctx->if_nesting_level)) ? APLOG_ERR : APLOG_WARNING,
2436 0, r, "else directive does not take tags in %s",
2440 if (ctx->if_nesting_level) {
2445 if (ctx->flags & SSI_FLAG_PRINTING) {
2446 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2452 DEBUG_DUMP_COND(ctx, " else");
2454 if (ctx->flags & SSI_FLAG_COND_TRUE) {
2455 ctx->flags &= SSI_FLAG_CLEAR_PRINTING;
2458 ctx->flags |= (SSI_FLAG_PRINTING | SSI_FLAG_COND_TRUE);
2467 static apr_status_t handle_endif(include_ctx_t *ctx, ap_filter_t *f,
2468 apr_bucket_brigade *bb)
2470 request_rec *r = f->r;
2473 ap_log_rerror(APLOG_MARK,
2474 (!(ctx->if_nesting_level)) ? APLOG_ERR : APLOG_WARNING,
2475 0, r, "endif directive does not take tags in %s",
2479 if (ctx->if_nesting_level) {
2480 --(ctx->if_nesting_level);
2485 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2489 DEBUG_DUMP_COND(ctx, "endif");
2491 ctx->flags |= (SSI_FLAG_PRINTING | SSI_FLAG_COND_TRUE);
2497 * <!--#set var="..." value="..." ... -->
2499 static apr_status_t handle_set(include_ctx_t *ctx, ap_filter_t *f,
2500 apr_bucket_brigade *bb)
2502 const char *encoding = "none", *decoding = "none";
2504 request_rec *r = f->r;
2505 request_rec *sub = r->main;
2506 apr_pool_t *p = r->pool;
2509 if (ctx->argc < 2) {
2510 ap_log_rerror(APLOG_MARK,
2511 (ctx->flags & SSI_FLAG_PRINTING)
2512 ? APLOG_ERR : APLOG_WARNING,
2513 0, r, "missing argument for set element in %s",
2517 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
2521 if (ctx->argc < 2) {
2522 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2526 /* we need to use the 'main' request pool to set notes as that is
2536 char *tag_val = NULL;
2538 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, SSI_VALUE_RAW);
2540 if (!tag || !tag_val) {
2544 if (!strcmp(tag, "var")) {
2545 decodehtml(tag_val);
2546 var = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
2547 SSI_EXPAND_DROP_NAME);
2549 else if (!strcmp(tag, "decoding")) {
2552 else if (!strcmp(tag, "encoding")) {
2555 else if (!strcmp(tag, "value")) {
2556 char *parsed_string;
2559 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "variable must "
2560 "precede value in set directive in %s",
2562 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2566 parsed_string = ap_ssi_parse_string(ctx, tag_val, NULL, 0,
2567 SSI_EXPAND_DROP_NAME);
2569 if (parsed_string) {
2571 char *e, *d, *token;
2573 d = apr_pstrdup(ctx->pool, decoding);
2574 token = apr_strtok(d, ", \t", &last);
2577 if (!strcasecmp(token, "none")) {
2580 else if (!strcasecmp(token, "url")) {
2581 char *buf = apr_pstrdup(ctx->pool, parsed_string);
2582 ap_unescape_url(buf);
2583 parsed_string = buf;
2585 else if (!strcasecmp(token, "urlencoded")) {
2586 char *buf = apr_pstrdup(ctx->pool, parsed_string);
2587 ap_unescape_urlencoded(buf);
2588 parsed_string = buf;
2590 else if (!strcasecmp(token, "entity")) {
2591 char *buf = apr_pstrdup(ctx->pool, parsed_string);
2593 parsed_string = buf;
2595 else if (!strcasecmp(token, "base64")) {
2596 parsed_string = ap_pbase64decode(ctx->dpool, parsed_string);
2599 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown value "
2600 "\"%s\" to parameter \"decoding\" of tag set in "
2601 "%s", token, r->filename);
2602 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2606 token = apr_strtok(NULL, ", \t", &last);
2609 e = apr_pstrdup(ctx->pool, encoding);
2610 token = apr_strtok(e, ", \t", &last);
2613 if (!strcasecmp(token, "none")) {
2616 else if (!strcasecmp(token, "url")) {
2617 parsed_string = ap_escape_uri(ctx->dpool, parsed_string);
2619 else if (!strcasecmp(token, "urlencoded")) {
2620 parsed_string = ap_escape_urlencoded(ctx->dpool, parsed_string);
2622 else if (!strcasecmp(token, "entity")) {
2623 parsed_string = ap_escape_html2(ctx->dpool, parsed_string, 0);
2625 else if (!strcasecmp(token, "base64")) {
2627 buf = ap_pbase64encode(ctx->dpool, (char *)parsed_string);
2628 parsed_string = buf;
2631 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown value "
2632 "\"%s\" to parameter \"encoding\" of tag set in "
2633 "%s", token, r->filename);
2634 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2638 token = apr_strtok(NULL, ", \t", &last);
2647 apr_table_setn(r->subprocess_env, apr_pstrdup(p, var),
2648 apr_pstrdup(p, parsed_string));
2651 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Invalid tag for set "
2652 "directive in %s", r->filename);
2653 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2664 static apr_status_t handle_printenv(include_ctx_t *ctx, ap_filter_t *f,
2665 apr_bucket_brigade *bb)
2667 request_rec *r = f->r;
2668 const apr_array_header_t *arr;
2669 const apr_table_entry_t *elts;
2673 ap_log_rerror(APLOG_MARK,
2674 (ctx->flags & SSI_FLAG_PRINTING)
2675 ? APLOG_ERR : APLOG_WARNING,
2676 0, r, "printenv directive does not take tags in %s",
2680 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
2685 SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
2689 arr = apr_table_elts(r->subprocess_env);
2690 elts = (apr_table_entry_t *)arr->elts;
2692 for (i = 0; i < arr->nelts; ++i) {
2693 const char *key_text, *val_text;
2696 key_text = ap_escape_html(ctx->dpool, elts[i].key);
2699 val_text = elts[i].val;
2700 if (val_text == LAZY_VALUE)
2701 val_text = add_include_vars_lazy(r, elts[i].key, ctx->time_str);
2702 val_text = ap_escape_html(ctx->dpool, val_text);
2704 apr_brigade_putstrs(bb, NULL, NULL, key_text, "=", val_text, "\n",
2714 * +-------------------------------------------------------+
2716 * | Main Includes-Filter Engine
2718 * +-------------------------------------------------------+
2721 /* This is an implementation of the BNDM search algorithm.
2723 * Fast and Flexible String Matching by Combining Bit-parallelism and
2724 * Suffix Automata (2001)
2725 * Gonzalo Navarro, Mathieu Raffinot
2727 * http://www-igm.univ-mlv.fr/~raffinot/ftp/jea2001.ps.gz
2729 * Initial code submitted by Sascha Schumann.
2732 /* Precompile the bndm_t data structure. */
2733 static bndm_t *bndm_compile(apr_pool_t *pool, const char *n, apr_size_t nl)
2736 const char *ne = n + nl;
2737 bndm_t *t = apr_palloc(pool, sizeof(*t));
2739 memset(t->T, 0, sizeof(unsigned int) * 256);
2740 t->pattern_len = nl;
2742 for (x = 1; n < ne; x <<= 1) {
2743 t->T[(unsigned char) *n++] |= x;
2751 /* Implements the BNDM search algorithm (as described above).
2753 * h - the string to look in
2754 * hl - length of the string to look for
2755 * t - precompiled bndm structure against the pattern
2757 * Returns the count of character that is the first match or hl if no
2760 static apr_size_t bndm(bndm_t *t, const char *h, apr_size_t hl)
2763 const char *he, *p, *pi;
2764 unsigned int *T, x, d;
2771 nl = t->pattern_len;
2773 pi = h - 1; /* pi: p initial */
2774 p = pi + nl; /* compare window right to left. point to the first char */
2780 d &= T[(unsigned char) *p--];
2803 * returns the index position of the first byte of start_seq (or the len of
2804 * the buffer as non-match)
2806 static apr_size_t find_start_sequence(include_ctx_t *ctx, const char *data,
2809 struct ssi_internal_ctx *intern = ctx->intern;
2810 apr_size_t slen = intern->start_seq_pat->pattern_len;
2815 p = data; /* try partial match at the end of the buffer (below) */
2818 /* try fast bndm search over the buffer
2819 * (hopefully the whole start sequence can be found in this buffer)
2821 index = bndm(intern->start_seq_pat, data, len);
2823 /* wow, found it. ready. */
2825 intern->state = PARSE_DIRECTIVE;
2829 /* ok, the pattern can't be found as whole in the buffer,
2830 * check the end for a partial match
2832 p = data + len - slen + 1;
2838 while (p < ep && *p != *intern->start_seq) {
2844 /* found a possible start_seq start */
2849 while (p < ep && *p == intern->start_seq[pos]) {
2854 /* partial match found. Store the info for the next round */
2856 intern->state = PARSE_HEAD;
2857 intern->parse_pos = pos;
2862 /* we must try all combinations; consider (e.g.) SSIStartTag "--->"
2863 * and a string data of "--.-" and the end of the buffer
2865 p = data + index + 1;
2873 * returns the first byte *after* the partial (or final) match.
2875 * If we had to trick with the start_seq start, 'release' returns the
2876 * number of chars of the start_seq which appeared not to be part of a
2877 * full tag and may have to be passed down the filter chain.
2879 static apr_size_t find_partial_start_sequence(include_ctx_t *ctx,
2882 apr_size_t *release)
2884 struct ssi_internal_ctx *intern = ctx->intern;
2885 apr_size_t pos, spos = 0;
2886 apr_size_t slen = intern->start_seq_pat->pattern_len;
2889 pos = intern->parse_pos;
2896 while (p < ep && pos < slen && *p == intern->start_seq[pos]) {
2903 intern->state = PARSE_DIRECTIVE;
2907 /* the whole buffer is a partial match */
2909 intern->parse_pos = pos;
2913 /* No match so far, but again:
2914 * We must try all combinations, since the start_seq is a random
2915 * user supplied string
2917 * So: look if the first char of start_seq appears somewhere within
2918 * the current partial match. If it does, try to start a match that
2919 * begins with this offset. (This can happen, if a strange
2920 * start_seq like "---->" spans buffers)
2922 if (spos < intern->parse_pos) {
2926 p = intern->start_seq + spos;
2927 pos = intern->parse_pos - spos;
2929 while (pos && *p != *intern->start_seq) {
2936 /* if a matching beginning char was found, try to match the
2937 * remainder of the old buffer.
2943 while (t < pos && *p == intern->start_seq[t]) {
2949 /* yeah, another partial match found in the *old*
2950 * buffer, now test the *current* buffer for
2964 } while (1); /* work hard to find a match ;-) */
2966 /* no match at all, release all (wrongly) matched chars so far */
2967 *release = intern->parse_pos;
2968 intern->state = PARSE_PRE_HEAD;
2973 * returns the position after the directive
2975 static apr_size_t find_directive(include_ctx_t *ctx, const char *data,
2976 apr_size_t len, char ***store,
2977 apr_size_t **store_len)
2979 struct ssi_internal_ctx *intern = ctx->intern;
2980 const char *p = data;
2981 const char *ep = data + len;
2984 switch (intern->state) {
2985 case PARSE_DIRECTIVE:
2986 while (p < ep && !apr_isspace(*p)) {
2987 /* we have to consider the case of missing space between directive
2988 * and end_seq (be somewhat lenient), e.g. <!--#printenv-->
2990 if (*p == *intern->end_seq) {
2991 intern->state = PARSE_DIRECTIVE_TAIL;
2992 intern->parse_pos = 1;
2999 if (p < ep) { /* found delimiter whitespace */
3000 intern->state = PARSE_DIRECTIVE_POSTNAME;
3001 *store = &intern->directive;
3002 *store_len = &intern->directive_len;
3007 case PARSE_DIRECTIVE_TAIL:
3008 pos = intern->parse_pos;
3010 while (p < ep && pos < intern->end_seq_len &&
3011 *p == intern->end_seq[pos]) {
3016 /* full match, we're done */
3017 if (pos == intern->end_seq_len) {
3018 intern->state = PARSE_DIRECTIVE_POSTTAIL;
3019 *store = &intern->directive;
3020 *store_len = &intern->directive_len;
3024 /* partial match, the buffer is too small to match fully */
3026 intern->parse_pos = pos;
3030 /* no match. continue normal parsing */
3031 intern->state = PARSE_DIRECTIVE;
3034 case PARSE_DIRECTIVE_POSTTAIL:
3035 intern->state = PARSE_EXECUTE;
3036 intern->directive_len -= intern->end_seq_len;
3037 /* continue immediately with the next state */
3039 case PARSE_DIRECTIVE_POSTNAME:
3040 if (PARSE_DIRECTIVE_POSTNAME == intern->state) {
3041 intern->state = PARSE_PRE_ARG;
3044 intern->argv = NULL;
3046 if (!intern->directive_len) {
3048 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "missing "
3049 "directive name in parsed document %s",
3053 char *sp = intern->directive;
3054 char *sep = intern->directive + intern->directive_len;
3056 /* normalize directive name */
3057 for (; sp < sep; ++sp) {
3058 *sp = apr_tolower(*sp);
3065 /* get a rid of a gcc warning about unhandled enumerations */
3073 * find out whether the next token is (a possible) end_seq or an argument
3075 static apr_size_t find_arg_or_tail(include_ctx_t *ctx, const char *data,
3078 struct ssi_internal_ctx *intern = ctx->intern;
3079 const char *p = data;
3080 const char *ep = data + len;
3082 /* skip leading WS */
3083 while (p < ep && apr_isspace(*p)) {
3087 /* buffer doesn't consist of whitespaces only */
3089 intern->state = (*p == *intern->end_seq) ? PARSE_TAIL : PARSE_ARG;
3096 * test the stream for end_seq. If it doesn't match at all, it must be an
3099 static apr_size_t find_tail(include_ctx_t *ctx, const char *data,
3102 struct ssi_internal_ctx *intern = ctx->intern;
3103 const char *p = data;
3104 const char *ep = data + len;
3105 apr_size_t pos = intern->parse_pos;
3107 if (PARSE_TAIL == intern->state) {
3108 intern->state = PARSE_TAIL_SEQ;
3109 pos = intern->parse_pos = 0;
3112 while (p < ep && pos < intern->end_seq_len && *p == intern->end_seq[pos]) {
3117 /* bingo, full match */
3118 if (pos == intern->end_seq_len) {
3119 intern->state = PARSE_EXECUTE;
3123 /* partial match, the buffer is too small to match fully */
3125 intern->parse_pos = pos;
3129 /* no match. It must be an argument string then
3130 * The caller should cleanup and rewind to the reparse point
3132 intern->state = PARSE_ARG;
3137 * extract name=value from the buffer
3138 * A pcre-pattern could look (similar to):
3139 * name\s*(?:=\s*(["'`]?)value\1(?>\s*))?
3141 static apr_size_t find_argument(include_ctx_t *ctx, const char *data,
3142 apr_size_t len, char ***store,
3143 apr_size_t **store_len)
3145 struct ssi_internal_ctx *intern = ctx->intern;
3146 const char *p = data;
3147 const char *ep = data + len;
3149 switch (intern->state) {
3152 * create argument structure and append it to the current list
3154 intern->current_arg = apr_palloc(ctx->dpool,
3155 sizeof(*intern->current_arg));
3156 intern->current_arg->next = NULL;
3159 if (!intern->argv) {
3160 intern->argv = intern->current_arg;
3163 arg_item_t *newarg = intern->argv;
3165 while (newarg->next) {
3166 newarg = newarg->next;
3168 newarg->next = intern->current_arg;
3171 /* check whether it's a valid one. If it begins with a quote, we
3172 * can safely assume, someone forgot the name of the argument
3175 case '"': case '\'': case '`':
3178 intern->state = PARSE_ARG_VAL;
3179 intern->quote = *p++;
3180 intern->current_arg->name = NULL;
3181 intern->current_arg->name_len = 0;
3184 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "missing "
3185 "argument name for value to tag %s in %s",
3186 apr_pstrmemdup(ctx->r->pool, intern->directive,
3187 intern->directive_len),
3193 intern->state = PARSE_ARG_NAME;
3195 /* continue immediately with next state */
3197 case PARSE_ARG_NAME:
3198 while (p < ep && !apr_isspace(*p) && *p != '=') {
3203 intern->state = PARSE_ARG_POSTNAME;
3204 *store = &intern->current_arg->name;
3205 *store_len = &intern->current_arg->name_len;
3210 case PARSE_ARG_POSTNAME:
3211 intern->current_arg->name = apr_pstrmemdup(ctx->dpool,
3212 intern->current_arg->name,
3213 intern->current_arg->name_len);
3214 if (!intern->current_arg->name_len) {
3216 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "missing "
3217 "argument name for value to tag %s in %s",
3218 apr_pstrmemdup(ctx->r->pool, intern->directive,
3219 intern->directive_len),
3223 ap_str_tolower(intern->current_arg->name);
3226 intern->state = PARSE_ARG_EQ;
3227 /* continue with next state immediately */
3232 while (p < ep && apr_isspace(*p)) {
3238 intern->state = PARSE_ARG_PREVAL;
3241 else { /* no value */
3242 intern->current_arg->value = NULL;
3243 intern->state = PARSE_PRE_ARG;
3250 case PARSE_ARG_PREVAL:
3253 while (p < ep && apr_isspace(*p)) {
3257 /* buffer doesn't consist of whitespaces only */
3259 intern->state = PARSE_ARG_VAL;
3261 case '"': case '\'': case '`':
3262 intern->quote = *p++;
3265 intern->quote = '\0';
3273 case PARSE_ARG_VAL_ESC:
3274 if (*p == intern->quote) {
3277 intern->state = PARSE_ARG_VAL;
3278 /* continue with next state immediately */
3281 for (; p < ep; ++p) {
3282 if (intern->quote && *p == '\\') {
3285 intern->state = PARSE_ARG_VAL_ESC;
3289 if (*p != intern->quote) {
3293 else if (intern->quote && *p == intern->quote) {
3295 *store = &intern->current_arg->value;
3296 *store_len = &intern->current_arg->value_len;
3297 intern->state = PARSE_ARG_POSTVAL;
3300 else if (!intern->quote && apr_isspace(*p)) {
3302 *store = &intern->current_arg->value;
3303 *store_len = &intern->current_arg->value_len;
3304 intern->state = PARSE_ARG_POSTVAL;
3311 case PARSE_ARG_POSTVAL:
3313 * The value is still the raw input string. Finally clean it up.
3315 --(intern->current_arg->value_len);
3317 /* strip quote escaping \ from the string */
3318 if (intern->quote) {
3319 apr_size_t shift = 0;
3322 sp = intern->current_arg->value;
3323 ep = intern->current_arg->value + intern->current_arg->value_len;
3324 while (sp < ep && *sp != '\\') {
3327 for (; sp < ep; ++sp) {
3328 if (*sp == '\\' && sp[1] == intern->quote) {
3337 intern->current_arg->value_len -= shift;
3340 intern->current_arg->value[intern->current_arg->value_len] = '\0';
3341 intern->state = PARSE_PRE_ARG;
3346 /* get a rid of a gcc warning about unhandled enumerations */
3350 return len; /* partial match of something */
3354 * This is the main loop over the current bucket brigade.
3356 static apr_status_t send_parsed_content(ap_filter_t *f, apr_bucket_brigade *bb)
3358 include_ctx_t *ctx = f->ctx;
3359 struct ssi_internal_ctx *intern = ctx->intern;
3360 request_rec *r = f->r;
3361 apr_bucket *b = APR_BRIGADE_FIRST(bb);
3362 apr_bucket_brigade *pass_bb;
3363 apr_status_t rv = APR_SUCCESS;
3364 char *magic; /* magic pointer for sentinel use */
3367 if (APR_BRIGADE_EMPTY(bb)) {
3371 /* we may crash, since already cleaned up; hand over the responsibility
3372 * to the next filter;-)
3374 if (intern->seen_eos) {
3375 return ap_pass_brigade(f->next, bb);
3378 /* All stuff passed along has to be put into that brigade */
3379 pass_bb = apr_brigade_create(ctx->pool, f->c->bucket_alloc);
3381 /* initialization for this loop */
3382 intern->bytes_read = 0;
3386 /* loop over the current bucket brigade */
3387 while (b != APR_BRIGADE_SENTINEL(bb)) {
3388 const char *data = NULL;
3389 apr_size_t len, index, release;
3390 apr_bucket *newb = NULL;
3391 char **store = &magic;
3392 apr_size_t *store_len = NULL;
3394 /* handle meta buckets before reading any data */
3395 if (APR_BUCKET_IS_METADATA(b)) {
3396 newb = APR_BUCKET_NEXT(b);
3398 APR_BUCKET_REMOVE(b);
3400 if (APR_BUCKET_IS_EOS(b)) {
3401 intern->seen_eos = 1;
3403 /* Hit end of stream, time for cleanup ... But wait!
3404 * Perhaps we're not ready yet. We may have to loop one or
3405 * two times again to finish our work. In that case, we
3406 * just re-insert the EOS bucket to allow for an extra loop.
3408 * PARSE_EXECUTE means, we've hit a directive just before the
3409 * EOS, which is now waiting for execution.
3411 * PARSE_DIRECTIVE_POSTTAIL means, we've hit a directive with
3412 * no argument and no space between directive and end_seq
3413 * just before the EOS. (consider <!--#printenv--> as last
3414 * or only string within the stream). This state, however,
3415 * just cleans up and turns itself to PARSE_EXECUTE, which
3416 * will be passed through within the next (and actually
3419 if (PARSE_EXECUTE == intern->state ||
3420 PARSE_DIRECTIVE_POSTTAIL == intern->state) {
3421 APR_BUCKET_INSERT_BEFORE(newb, b);
3424 break; /* END OF STREAM */
3428 APR_BRIGADE_INSERT_TAIL(pass_bb, b);
3430 if (APR_BUCKET_IS_FLUSH(b)) {
3439 /* enough is enough ... */
3440 if (ctx->flush_now ||
3441 intern->bytes_read > AP_MIN_BYTES_TO_WRITE) {
3443 if (!APR_BRIGADE_EMPTY(pass_bb)) {
3444 rv = ap_pass_brigade(f->next, pass_bb);
3445 if (rv != APR_SUCCESS) {
3446 apr_brigade_destroy(pass_bb);
3452 intern->bytes_read = 0;
3455 /* read the current bucket data */
3457 if (!intern->seen_eos) {
3458 if (intern->bytes_read > 0) {
3459 rv = apr_bucket_read(b, &data, &len, APR_NONBLOCK_READ);
3460 if (APR_STATUS_IS_EAGAIN(rv)) {
3466 if (!len || rv != APR_SUCCESS) {
3467 rv = apr_bucket_read(b, &data, &len, APR_BLOCK_READ);
3470 if (rv != APR_SUCCESS) {
3471 apr_brigade_destroy(pass_bb);
3475 intern->bytes_read += len;
3478 /* zero length bucket, fetch next one */
3479 if (!len && !intern->seen_eos) {
3480 b = APR_BUCKET_NEXT(b);
3485 * it's actually a data containing bucket, start/continue parsing
3488 switch (intern->state) {
3489 /* no current tag; search for start sequence */
3490 case PARSE_PRE_HEAD:
3491 index = find_start_sequence(ctx, data, len);
3494 apr_bucket_split(b, index);
3497 newb = APR_BUCKET_NEXT(b);
3498 if (ctx->flags & SSI_FLAG_PRINTING) {
3499 APR_BUCKET_REMOVE(b);
3500 APR_BRIGADE_INSERT_TAIL(pass_bb, b);
3503 apr_bucket_delete(b);
3507 /* now delete the start_seq stuff from the remaining bucket */
3508 if (PARSE_DIRECTIVE == intern->state) { /* full match */
3509 apr_bucket_split(newb, intern->start_seq_pat->pattern_len);
3510 ctx->flush_now = 1; /* pass pre-tag stuff */
3513 b = APR_BUCKET_NEXT(newb);
3514 apr_bucket_delete(newb);
3522 /* we're currently looking for the end of the start sequence */
3524 index = find_partial_start_sequence(ctx, data, len, &release);
3526 /* check if we mismatched earlier and have to release some chars */
3527 if (release && (ctx->flags & SSI_FLAG_PRINTING)) {
3528 char *to_release = apr_pmemdup(ctx->pool, intern->start_seq, release);
3530 newb = apr_bucket_pool_create(to_release, release, ctx->pool,
3531 f->c->bucket_alloc);
3532 APR_BRIGADE_INSERT_TAIL(pass_bb, newb);
3535 if (index) { /* any match */
3536 /* now delete the start_seq stuff from the remaining bucket */
3537 if (PARSE_DIRECTIVE == intern->state) { /* final match */
3538 apr_bucket_split(b, index);
3539 ctx->flush_now = 1; /* pass pre-tag stuff */
3541 newb = APR_BUCKET_NEXT(b);
3542 apr_bucket_delete(b);
3548 /* we're currently grabbing the directive name */
3549 case PARSE_DIRECTIVE:
3550 case PARSE_DIRECTIVE_POSTNAME:
3551 case PARSE_DIRECTIVE_TAIL:
3552 case PARSE_DIRECTIVE_POSTTAIL:
3553 index = find_directive(ctx, data, len, &store, &store_len);
3556 apr_bucket_split(b, index);
3557 newb = APR_BUCKET_NEXT(b);
3562 APR_BUCKET_REMOVE(b);
3563 apr_bucket_setaside(b, r->pool);
3564 APR_BRIGADE_INSERT_TAIL(intern->tmp_bb, b);
3568 /* time for cleanup? */
3569 if (store != &magic) {
3570 apr_brigade_pflatten(intern->tmp_bb, store, store_len,
3572 apr_brigade_cleanup(intern->tmp_bb);
3576 apr_bucket_delete(b);
3582 /* skip WS and find out what comes next (arg or end_seq) */
3584 index = find_arg_or_tail(ctx, data, len);
3586 if (index) { /* skipped whitespaces */
3588 apr_bucket_split(b, index);
3590 newb = APR_BUCKET_NEXT(b);
3591 apr_bucket_delete(b);
3597 /* currently parsing name[=val] */
3599 case PARSE_ARG_NAME:
3600 case PARSE_ARG_POSTNAME:
3602 case PARSE_ARG_PREVAL:
3604 case PARSE_ARG_VAL_ESC:
3605 case PARSE_ARG_POSTVAL:
3606 index = find_argument(ctx, data, len, &store, &store_len);
3609 apr_bucket_split(b, index);
3610 newb = APR_BUCKET_NEXT(b);
3615 APR_BUCKET_REMOVE(b);
3616 apr_bucket_setaside(b, r->pool);
3617 APR_BRIGADE_INSERT_TAIL(intern->tmp_bb, b);
3621 /* time for cleanup? */
3622 if (store != &magic) {
3623 apr_brigade_pflatten(intern->tmp_bb, store, store_len,
3625 apr_brigade_cleanup(intern->tmp_bb);
3629 apr_bucket_delete(b);
3635 /* try to match end_seq at current pos. */
3637 case PARSE_TAIL_SEQ:
3638 index = find_tail(ctx, data, len);
3640 switch (intern->state) {
3641 case PARSE_EXECUTE: /* full match */
3642 apr_bucket_split(b, index);
3643 newb = APR_BUCKET_NEXT(b);
3644 apr_bucket_delete(b);
3648 case PARSE_ARG: /* no match */
3649 /* PARSE_ARG must reparse at the beginning */
3650 APR_BRIGADE_PREPEND(bb, intern->tmp_bb);
3651 b = APR_BRIGADE_FIRST(bb);
3654 default: /* partial match */
3655 newb = APR_BUCKET_NEXT(b);
3656 APR_BUCKET_REMOVE(b);
3657 apr_bucket_setaside(b, r->pool);
3658 APR_BRIGADE_INSERT_TAIL(intern->tmp_bb, b);
3665 /* now execute the parsed directive, cleanup the space and
3666 * start again with PARSE_PRE_HEAD
3669 /* if there was an error, it was already logged; just stop here */
3670 if (intern->error) {
3671 if (ctx->flags & SSI_FLAG_PRINTING) {
3672 SSI_CREATE_ERROR_BUCKET(ctx, f, pass_bb);
3677 include_handler_fn_t *handle_func;
3680 (include_handler_fn_t *)apr_hash_get(include_handlers, intern->directive,
3681 intern->directive_len);
3684 DEBUG_INIT(ctx, f, pass_bb);
3685 rv = handle_func(ctx, f, pass_bb);
3686 if (rv != APR_SUCCESS) {
3687 apr_brigade_destroy(pass_bb);
3692 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3693 "unknown directive \"%s\" in parsed doc %s",
3694 apr_pstrmemdup(r->pool, intern->directive,
3695 intern->directive_len),
3697 if (ctx->flags & SSI_FLAG_PRINTING) {
3698 SSI_CREATE_ERROR_BUCKET(ctx, f, pass_bb);
3704 apr_pool_clear(ctx->dpool);
3705 apr_brigade_cleanup(intern->tmp_bb);
3707 /* Oooof. Done here, start next round */
3708 intern->state = PARSE_PRE_HEAD;
3711 } /* switch(ctx->state) */
3713 } /* while(brigade) */
3715 /* End of stream. Final cleanup */
3716 if (intern->seen_eos) {
3717 if (PARSE_HEAD == intern->state) {
3718 if (ctx->flags & SSI_FLAG_PRINTING) {
3719 char *to_release = apr_pmemdup(ctx->pool, intern->start_seq,
3722 APR_BRIGADE_INSERT_TAIL(pass_bb,
3723 apr_bucket_pool_create(to_release,
3724 intern->parse_pos, ctx->pool,
3725 f->c->bucket_alloc));
3728 else if (PARSE_PRE_HEAD != intern->state) {
3729 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3730 "SSI directive was not properly finished at the end "
3731 "of parsed document %s", r->filename);
3732 if (ctx->flags & SSI_FLAG_PRINTING) {
3733 SSI_CREATE_ERROR_BUCKET(ctx, f, pass_bb);
3737 if (!(ctx->flags & SSI_FLAG_PRINTING)) {
3738 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
3739 "missing closing endif directive in parsed document"
3740 " %s", r->filename);
3743 /* cleanup our temporary memory */
3744 apr_brigade_destroy(intern->tmp_bb);
3745 apr_pool_destroy(ctx->dpool);
3747 /* don't forget to finally insert the EOS bucket */
3748 APR_BRIGADE_INSERT_TAIL(pass_bb, b);
3751 /* if something's left over, pass it along */
3752 if (!APR_BRIGADE_EMPTY(pass_bb)) {
3753 rv = ap_pass_brigade(f->next, pass_bb);
3757 apr_brigade_destroy(pass_bb);
3764 * +-------------------------------------------------------+
3768 * +-------------------------------------------------------+
3771 static int includes_setup(ap_filter_t *f)
3773 include_dir_config *conf = ap_get_module_config(f->r->per_dir_config,
3776 /* When our xbithack value isn't set to full or our platform isn't
3777 * providing group-level protection bits or our group-level bits do not
3778 * have group-execite on, we will set the no_local_copy value to 1 so
3779 * that we will not send 304s.
3781 if ((conf->xbithack != XBITHACK_FULL)
3782 || !(f->r->finfo.valid & APR_FINFO_GPROT)
3783 || !(f->r->finfo.protection & APR_GEXECUTE)) {
3784 f->r->no_local_copy = 1;
3787 /* Don't allow ETag headers to be generated - see RFC2616 - 13.3.4.
3788 * We don't know if we are going to be including a file or executing
3789 * a program - in either case a strong ETag header will likely be invalid.
3791 if (conf->etag <= 0) {
3792 apr_table_setn(f->r->notes, "no-etag", "");
3798 static apr_status_t includes_filter(ap_filter_t *f, apr_bucket_brigade *b)
3800 request_rec *r = f->r;
3801 include_ctx_t *ctx = f->ctx;
3802 request_rec *parent;
3803 include_dir_config *conf = ap_get_module_config(r->per_dir_config,
3806 include_server_config *sconf= ap_get_module_config(r->server->module_config,
3809 if (!(ap_allow_options(r) & OPT_INCLUDES)) {
3810 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
3811 "mod_include: Options +Includes (or IncludesNoExec) "
3812 "wasn't set, INCLUDES filter removed");
3813 ap_remove_output_filter(f);
3814 return ap_pass_brigade(f->next, b);
3818 struct ssi_internal_ctx *intern;
3820 /* create context for this filter */
3821 f->ctx = ctx = apr_palloc(r->pool, sizeof(*ctx));
3823 ctx->intern = intern = apr_palloc(r->pool, sizeof(*ctx->intern));
3824 ctx->pool = r->pool;
3825 apr_pool_create(&ctx->dpool, ctx->pool);
3828 intern->tmp_bb = apr_brigade_create(ctx->pool, f->c->bucket_alloc);
3829 intern->seen_eos = 0;
3830 intern->state = PARSE_PRE_HEAD;
3831 ctx->flags = (SSI_FLAG_PRINTING | SSI_FLAG_COND_TRUE);
3832 if ((ap_allow_options(r) & OPT_INC_WITH_EXEC) == 0) {
3833 ctx->flags |= SSI_FLAG_NO_EXEC;
3835 intern->legacy_expr = (conf->legacy_expr > 0);
3836 intern->expr_eval_ctx = NULL;
3837 intern->expr_err = NULL;
3838 intern->expr_vary_this = NULL;
3840 ctx->if_nesting_level = 0;
3843 ctx->error_str = conf->default_error_msg ? conf->default_error_msg :
3845 ctx->time_str = conf->default_time_fmt ? conf->default_time_fmt :
3846 DEFAULT_TIME_FORMAT;
3847 intern->start_seq = sconf->default_start_tag;
3848 intern->start_seq_pat = bndm_compile(ctx->pool, intern->start_seq,
3849 strlen(intern->start_seq));
3850 intern->end_seq = sconf->default_end_tag;
3851 intern->end_seq_len = strlen(intern->end_seq);
3852 intern->undefined_echo = conf->undefined_echo ? conf->undefined_echo :
3853 DEFAULT_UNDEFINED_ECHO;
3854 intern->undefined_echo_len = strlen(intern->undefined_echo);
3857 if ((parent = ap_get_module_config(r->request_config, &include_module))) {
3858 /* Kludge --- for nested includes, we want to keep the subprocess
3859 * environment of the base document (for compatibility); that means
3860 * torquing our own last_modified date as well so that the
3861 * LAST_MODIFIED variable gets reset to the proper value if the
3862 * nested document resets <!--#config timefmt -->.
3864 r->subprocess_env = r->main->subprocess_env;
3865 apr_pool_join(r->main->pool, r->pool);
3866 r->finfo.mtime = r->main->finfo.mtime;
3869 /* we're not a nested include, so we create an initial
3871 ap_add_common_vars(r);
3873 add_include_vars(r);
3875 /* Always unset the content-length. There is no way to know if
3876 * the content will be modified at some point by send_parsed_content.
3877 * It is very possible for us to not find any content in the first
3878 * 9k of the file, but still have to modify the content of the file.
3879 * If we are going to pass the file through send_parsed_content, then
3880 * the content-length should just be unset.
3882 apr_table_unset(f->r->headers_out, "Content-Length");
3884 /* Always unset the Last-Modified field - see RFC2616 - 13.3.4.
3885 * We don't know if we are going to be including a file or executing
3886 * a program which may change the Last-Modified header or make the
3887 * content completely dynamic. Therefore, we can't support these
3890 * Exception: XBitHack full means we *should* set the
3891 * Last-Modified field.
3893 * SSILastModified on means we *should* set the Last-Modified field
3894 * if not present, or respect an existing value if present.
3897 /* Must we respect the last modified header? By default, no */
3898 if (conf->lastmodified > 0) {
3900 /* update the last modified if we have a valid time, and only if
3901 * we don't already have a valid last modified.
3903 if (r->finfo.valid & APR_FINFO_MTIME
3904 && !apr_table_get(f->r->headers_out, "Last-Modified")) {
3905 ap_update_mtime(r, r->finfo.mtime);
3906 ap_set_last_modified(r);
3911 /* Assure the platform supports Group protections */
3912 else if (((conf->xbithack == XBITHACK_FULL ||
3913 (conf->xbithack == XBITHACK_UNSET &&
3914 DEFAULT_XBITHACK == XBITHACK_FULL))
3915 && (r->finfo.valid & APR_FINFO_GPROT)
3916 && (r->finfo.protection & APR_GEXECUTE))) {
3917 ap_update_mtime(r, r->finfo.mtime);
3918 ap_set_last_modified(r);
3921 apr_table_unset(f->r->headers_out, "Last-Modified");
3924 /* add QUERY stuff to env cause it ain't yet */
3926 char *arg_copy = apr_pstrdup(r->pool, r->args);
3928 apr_table_setn(r->subprocess_env, "QUERY_STRING", r->args);
3929 ap_unescape_url(arg_copy);
3930 apr_table_setn(r->subprocess_env, "QUERY_STRING_UNESCAPED",
3931 ap_escape_shell_cmd(r->pool, arg_copy));
3934 return send_parsed_content(f, b);
3937 static int include_fixup(request_rec *r)
3939 if (r->handler && (strcmp(r->handler, "server-parsed") == 0))
3941 if (!r->content_type || !*r->content_type) {
3942 ap_set_content_type(r, "text/html");
3944 r->handler = "default-handler";
3947 #if defined(OS2) || defined(WIN32) || defined(NETWARE)
3948 /* These OS's don't support xbithack. This is being worked on. */
3954 include_dir_config *conf = ap_get_module_config(r->per_dir_config,
3957 if (conf->xbithack == XBITHACK_OFF ||
3958 (DEFAULT_XBITHACK == XBITHACK_OFF &&
3959 conf->xbithack == XBITHACK_UNSET))
3964 if (!(r->finfo.protection & APR_UEXECUTE)) {
3968 if (!r->content_type || strncmp(r->content_type, "text/html", 9)) {
3974 /* We always return declined, because the default handler actually
3975 * serves the file. All we have to do is add the filter.
3977 ap_add_output_filter("INCLUDES", NULL, r, r->connection);
3983 * +-------------------------------------------------------+
3985 * | Configuration Handling
3987 * +-------------------------------------------------------+
3990 static void *create_includes_dir_config(apr_pool_t *p, char *dummy)
3992 include_dir_config *result = apr_pcalloc(p, sizeof(include_dir_config));
3994 result->xbithack = XBITHACK_UNSET;
3995 result->lastmodified = UNSET;
3996 result->etag = UNSET;
3997 result->legacy_expr = UNSET;
4002 #define MERGE(b,o,n,val,unset) n->val = o->val != unset ? o->val : b->val
4003 static void *merge_includes_dir_config(apr_pool_t *p, void *basev, void *overridesv)
4005 include_dir_config *base = (include_dir_config *)basev,
4006 *over = (include_dir_config *)overridesv,
4007 *new = apr_palloc(p, sizeof(include_dir_config));
4008 MERGE(base, over, new, default_error_msg, NULL);
4009 MERGE(base, over, new, default_time_fmt, NULL);
4010 MERGE(base, over, new, undefined_echo, NULL);
4011 MERGE(base, over, new, xbithack, XBITHACK_UNSET);
4012 MERGE(base, over, new, lastmodified, UNSET);
4013 MERGE(base, over, new, etag, UNSET);
4014 MERGE(base, over, new, legacy_expr, UNSET);
4018 static void *create_includes_server_config(apr_pool_t *p, server_rec *server)
4020 include_server_config *result;
4022 result = apr_palloc(p, sizeof(include_server_config));
4023 result->default_end_tag = DEFAULT_END_SEQUENCE;
4024 result->default_start_tag = DEFAULT_START_SEQUENCE;
4029 static const char *set_xbithack(cmd_parms *cmd, void *mconfig, const char *arg)
4031 include_dir_config *conf = mconfig;
4033 if (!strcasecmp(arg, "off")) {
4034 conf->xbithack = XBITHACK_OFF;
4036 else if (!strcasecmp(arg, "on")) {
4037 conf->xbithack = XBITHACK_ON;
4039 else if (!strcasecmp(arg, "full")) {
4040 conf->xbithack = XBITHACK_FULL;
4043 return "XBitHack must be set to Off, On, or Full";
4049 static const char *set_default_start_tag(cmd_parms *cmd, void *mconfig,
4052 include_server_config *conf;
4053 const char *p = tag;
4055 /* be consistent. (See below in set_default_end_tag) */
4057 if (apr_isspace(*p)) {
4058 return "SSIStartTag may not contain any whitespaces";
4063 conf= ap_get_module_config(cmd->server->module_config , &include_module);
4064 conf->default_start_tag = tag;
4069 static const char *set_default_end_tag(cmd_parms *cmd, void *mconfig,
4072 include_server_config *conf;
4073 const char *p = tag;
4075 /* sanity check. The parser may fail otherwise */
4077 if (apr_isspace(*p)) {
4078 return "SSIEndTag may not contain any whitespaces";
4083 conf= ap_get_module_config(cmd->server->module_config , &include_module);
4084 conf->default_end_tag = tag;
4089 static const char *set_undefined_echo(cmd_parms *cmd, void *mconfig,
4092 include_dir_config *conf = mconfig;
4093 conf->undefined_echo = msg;
4098 static const char *set_default_error_msg(cmd_parms *cmd, void *mconfig,
4101 include_dir_config *conf = mconfig;
4102 conf->default_error_msg = msg;
4107 static const char *set_default_time_fmt(cmd_parms *cmd, void *mconfig,
4110 include_dir_config *conf = mconfig;
4111 conf->default_time_fmt = fmt;
4118 * +-------------------------------------------------------+
4120 * | Module Initialization and Configuration
4122 * +-------------------------------------------------------+
4125 static int include_post_config(apr_pool_t *p, apr_pool_t *plog,
4126 apr_pool_t *ptemp, server_rec *s)
4128 include_handlers = apr_hash_make(p);
4130 ssi_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_include_handler);
4132 if(ssi_pfn_register) {
4133 ssi_pfn_register("if", handle_if);
4134 ssi_pfn_register("set", handle_set);
4135 ssi_pfn_register("else", handle_else);
4136 ssi_pfn_register("elif", handle_elif);
4137 ssi_pfn_register("echo", handle_echo);
4138 ssi_pfn_register("endif", handle_endif);
4139 ssi_pfn_register("fsize", handle_fsize);
4140 ssi_pfn_register("config", handle_config);
4141 ssi_pfn_register("include", handle_include);
4142 ssi_pfn_register("flastmod", handle_flastmod);
4143 ssi_pfn_register("printenv", handle_printenv);
4149 static const command_rec includes_cmds[] =
4151 AP_INIT_TAKE1("XBitHack", set_xbithack, NULL, OR_OPTIONS,
4152 "Off, On, or Full"),
4153 AP_INIT_TAKE1("SSIErrorMsg", set_default_error_msg, NULL, OR_ALL,
4155 AP_INIT_TAKE1("SSITimeFormat", set_default_time_fmt, NULL, OR_ALL,
4156 "a strftime(3) formatted string"),
4157 AP_INIT_TAKE1("SSIStartTag", set_default_start_tag, NULL, RSRC_CONF,
4158 "SSI Start String Tag"),
4159 AP_INIT_TAKE1("SSIEndTag", set_default_end_tag, NULL, RSRC_CONF,
4160 "SSI End String Tag"),
4161 AP_INIT_TAKE1("SSIUndefinedEcho", set_undefined_echo, NULL, OR_ALL,
4162 "String to be displayed if an echoed variable is undefined"),
4163 AP_INIT_FLAG("SSILegacyExprParser", ap_set_flag_slot_char,
4164 (void *)APR_OFFSETOF(include_dir_config, legacy_expr),
4166 "Whether to use the legacy expression parser compatible "
4167 "with <= 2.2.x. Limited to 'on' or 'off'"),
4168 AP_INIT_FLAG("SSILastModified", ap_set_flag_slot_char,
4169 (void *)APR_OFFSETOF(include_dir_config, lastmodified),
4170 OR_LIMIT, "Whether to set the last modified header or respect "
4171 "an existing header. Limited to 'on' or 'off'"),
4172 AP_INIT_FLAG("SSIEtag", ap_set_flag_slot_char,
4173 (void *)APR_OFFSETOF(include_dir_config, etag),
4174 OR_LIMIT, "Whether to allow the generation of ETags within the server. "
4175 "Existing ETags will be preserved. Limited to 'on' or 'off'"),
4179 static void ap_register_include_handler(char *tag, include_handler_fn_t *func)
4181 apr_hash_set(include_handlers, tag, strlen(tag), (const void *)func);
4184 static void register_hooks(apr_pool_t *p)
4186 APR_REGISTER_OPTIONAL_FN(ap_ssi_get_tag_and_value);
4187 APR_REGISTER_OPTIONAL_FN(ap_ssi_parse_string);
4188 APR_REGISTER_OPTIONAL_FN(ap_register_include_handler);
4189 ap_hook_post_config(include_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
4190 ap_hook_fixups(include_fixup, NULL, NULL, APR_HOOK_LAST);
4191 ap_register_output_filter("INCLUDES", includes_filter, includes_setup,
4195 AP_DECLARE_MODULE(include) =
4197 STANDARD20_MODULE_STUFF,
4198 create_includes_dir_config, /* dir config creater */
4199 merge_includes_dir_config, /* dir config merger */
4200 create_includes_server_config,/* server config */
4201 NULL, /* merge server config */
4202 includes_cmds, /* command apr_table_t */
4203 register_hooks /* register hooks */