1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * http_include.c: Handles the server-parsed HTML documents
62 * Original by Rob McCool; substantial fixups by David Robinson;
63 * incorporated into the Apache module framework by rst.
68 #include "apr_strings.h"
69 #include "apr_thread_proc.h"
73 #include "apr_optional.h"
75 #define APR_WANT_STRFUNC
80 #include "ap_config.h"
81 #include "util_filter.h"
83 #include "http_config.h"
84 #include "http_core.h"
85 #include "http_request.h"
86 #include "http_core.h"
87 #include "http_protocol.h"
89 #include "http_main.h"
90 #include "util_script.h"
91 #include "http_core.h"
92 #include "mod_include.h"
93 #include "util_ebcdic.h"
95 module AP_MODULE_DECLARE_DATA include_module;
96 static apr_hash_t *include_hash;
97 static APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *ssi_pfn_register;
99 #define BYTE_COUNT_THRESHOLD AP_MIN_BYTES_TO_WRITE
101 /* ------------------------ Environment function -------------------------- */
103 /* XXX: could use ap_table_overlap here */
104 static void add_include_vars(request_rec *r, char *timefmt)
107 apr_table_t *e = r->subprocess_env;
109 apr_time_t date = r->request_time;
111 apr_table_setn(e, "DATE_LOCAL", ap_ht_time(r->pool, date, timefmt, 0));
112 apr_table_setn(e, "DATE_GMT", ap_ht_time(r->pool, date, timefmt, 1));
113 apr_table_setn(e, "LAST_MODIFIED",
114 ap_ht_time(r->pool, r->finfo.mtime, timefmt, 0));
115 apr_table_setn(e, "DOCUMENT_URI", r->uri);
116 apr_table_setn(e, "DOCUMENT_PATH_INFO", r->path_info);
117 if (apr_get_username(&pwname, r->finfo.user, r->pool) == APR_SUCCESS) {
118 apr_table_setn(e, "USER_NAME", pwname);
121 apr_table_setn(e, "USER_NAME", "<unknown>");
123 if ((t = strrchr(r->filename, '/'))) {
124 apr_table_setn(e, "DOCUMENT_NAME", ++t);
127 apr_table_setn(e, "DOCUMENT_NAME", r->uri);
130 char *arg_copy = apr_pstrdup(r->pool, r->args);
132 ap_unescape_url(arg_copy);
133 apr_table_setn(e, "QUERY_STRING_UNESCAPED",
134 ap_escape_shell_cmd(r->pool, arg_copy));
140 /* --------------------------- Parser functions --------------------------- */
142 /* This function returns either a pointer to the split bucket containing the
143 * first byte of the BEGINNING_SEQUENCE (after finding a complete match) or it
144 * returns NULL if no match found.
146 static apr_bucket *find_start_sequence(apr_bucket *dptr, include_ctx_t *ctx,
147 apr_bucket_brigade *bb, int *do_cleanup)
152 const char *str = STARTING_SEQUENCE;
154 apr_size_t start_index;
159 if (APR_BUCKET_IS_EOS(dptr)) {
162 apr_bucket_read(dptr, &buf, &len, APR_BLOCK_READ);
163 /* XXX handle retcodes */
164 if (len == 0) { /* end of pipe? */
168 while (c < buf + len) {
169 if (ctx->bytes_parsed >= BYTE_COUNT_THRESHOLD) {
170 apr_bucket *start_bucket;
172 if (ctx->head_start_index > 0) {
173 start_index = ctx->head_start_index;
174 start_bucket = ctx->head_start_bucket;
177 start_index = (c - buf);
180 apr_bucket_split(start_bucket, start_index);
181 tmp_bkt = APR_BUCKET_NEXT(start_bucket);
182 if (ctx->head_start_index > 0) {
183 ctx->head_start_index = 0;
184 ctx->head_start_bucket = tmp_bkt;
190 if (*c == str[ctx->parse_pos]) {
191 if (ctx->state == PRE_HEAD) {
192 ctx->state = PARSE_HEAD;
193 ctx->head_start_bucket = dptr;
194 ctx->head_start_index = c - buf;
199 if (str[ctx->parse_pos] == '\0') {
200 /* We want to split the bucket at the '<'. */
202 ctx->state = PARSE_DIRECTIVE;
205 ctx->tag_start_bucket = dptr;
206 ctx->tag_start_index = c - buf;
207 if (ctx->head_start_index > 0) {
208 start_index = (c - buf) - ctx->head_start_index;
209 apr_bucket_split(ctx->head_start_bucket, ctx->head_start_index);
210 tmp_bkt = APR_BUCKET_NEXT(ctx->head_start_bucket);
211 if (dptr == ctx->head_start_bucket) {
212 ctx->tag_start_bucket = tmp_bkt;
213 ctx->tag_start_index = start_index;
215 ctx->head_start_bucket = tmp_bkt;
216 ctx->head_start_index = 0;
218 return ctx->head_start_bucket;
220 else if (ctx->parse_pos != 0) {
221 /* The reason for this, is that we need to make sure
222 * that we catch cases like <<!--#. This makes the
223 * second check after the original check fails.
224 * If parse_pos was already 0 then we already checked this.
229 ctx->state = PARSE_HEAD;
230 ctx->head_start_bucket = dptr;
231 ctx->head_start_index = c - buf;
235 ctx->state = PRE_HEAD;
236 ctx->head_start_bucket = NULL;
237 ctx->head_start_index = 0;
244 dptr = APR_BUCKET_NEXT(dptr);
245 } while (dptr != APR_BRIGADE_SENTINEL(bb));
249 static apr_bucket *find_end_sequence(apr_bucket *dptr, include_ctx_t *ctx, apr_bucket_brigade *bb)
254 const char *str = ENDING_SEQUENCE;
257 if (APR_BUCKET_IS_EOS(dptr)) {
260 apr_bucket_read(dptr, &buf, &len, APR_BLOCK_READ);
261 /* XXX handle retcodes */
262 if (len == 0) { /* end of pipe? */
265 if (dptr == ctx->tag_start_bucket) {
266 c = buf + ctx->tag_start_index;
271 while (c < buf + len) {
272 if (ctx->bytes_parsed >= BYTE_COUNT_THRESHOLD) {
276 if (*c == str[ctx->parse_pos]) {
277 if (ctx->state != PARSE_TAIL) {
278 ctx->state = PARSE_TAIL;
279 ctx->tail_start_bucket = dptr;
280 ctx->tail_start_index = c - buf;
285 if (ctx->state == PARSE_DIRECTIVE) {
286 if (ctx->tag_length == 0) {
287 if (!apr_isspace(*c)) {
288 ctx->tag_start_bucket = dptr;
289 ctx->tag_start_index = c - buf;
291 ctx->directive_length = 1;
295 if (!apr_isspace(*c)) {
296 ctx->directive_length++;
299 ctx->state = PARSE_TAG;
304 else if (ctx->state == PARSE_TAG) {
308 if (str[ctx->parse_pos] == '\0') {
309 apr_bucket *tmp_buck = dptr;
311 /* We want to split the bucket at the '>'. The
312 * end of the END_SEQUENCE is in the current bucket.
313 * The beginning might be in a previous bucket.
318 apr_bucket_split(dptr, c - buf);
319 tmp_buck = APR_BUCKET_NEXT(dptr);
323 else if (ctx->parse_pos != 0) {
324 /* The reason for this, is that we need to make sure
325 * that we catch cases like --->. This makes the
326 * second check after the original check fails.
327 * If parse_pos was already 0 then we already checked this.
329 ctx->tag_length += ctx->parse_pos;
332 ctx->state = PARSE_TAIL;
333 ctx->tail_start_bucket = dptr;
334 ctx->tail_start_index = c - buf;
335 ctx->tag_length += ctx->parse_pos;
339 if (ctx->tag_length > ctx->directive_length) {
340 ctx->state = PARSE_TAG;
343 ctx->state = PARSE_DIRECTIVE;
344 ctx->directive_length += ctx->parse_pos;
346 ctx->tail_start_bucket = NULL;
347 ctx->tail_start_index = 0;
348 ctx->tag_length += ctx->parse_pos;
357 dptr = APR_BUCKET_NEXT(dptr);
358 } while (dptr != APR_BRIGADE_SENTINEL(bb));
362 /* This function culls through the buckets that have been set aside in the
363 * ssi_tag_brigade and copies just the directive part of the SSI tag (none
364 * of the start and end delimiter bytes are copied).
366 static apr_status_t get_combined_directive (include_ctx_t *ctx,
368 apr_bucket_brigade *bb,
370 apr_size_t tmp_buf_size)
374 const char *tmp_from;
375 apr_size_t tmp_from_len;
377 /* If the tag length is longer than the tmp buffer, allocate space. */
378 if (ctx->tag_length > tmp_buf_size-1) {
379 if ((ctx->combined_tag = apr_pcalloc(r->pool, ctx->tag_length + 1)) == NULL) {
382 } /* Else, just use the temp buffer. */
384 ctx->combined_tag = tmp_buf;
387 /* Prime the pump. Start at the beginning of the tag... */
388 dptr = ctx->tag_start_bucket;
389 apr_bucket_read (dptr, &tmp_from, &tmp_from_len, 0); /* Read the bucket... */
391 /* Adjust the pointer to start at the tag within the bucket... */
392 if (dptr == ctx->tail_start_bucket) {
393 tmp_from_len -= (tmp_from_len - ctx->tail_start_index);
395 tmp_from = &tmp_from[ctx->tag_start_index];
396 tmp_from_len -= ctx->tag_start_index;
397 ctx->curr_tag_pos = ctx->combined_tag;
399 /* Loop through the buckets from the tag_start_bucket until before
400 * the tail_start_bucket copying the contents into the buffer.
403 memcpy (ctx->curr_tag_pos, tmp_from, tmp_from_len);
404 ctx->curr_tag_pos += tmp_from_len;
406 if (dptr == ctx->tail_start_bucket) {
410 dptr = APR_BUCKET_NEXT (dptr);
411 apr_bucket_read (dptr, &tmp_from, &tmp_from_len, 0);
412 /* Adjust the count to stop at the beginning of the tail. */
413 if (dptr == ctx->tail_start_bucket) {
414 tmp_from_len -= (tmp_from_len - ctx->tail_start_index);
418 (ctx->curr_tag_pos < ctx->combined_tag + ctx->tag_length));
420 ctx->combined_tag[ctx->tag_length] = '\0';
421 ctx->curr_tag_pos = ctx->combined_tag;
423 return (APR_SUCCESS);
427 * decodes a string containing html entities or numeric character references.
428 * 's' is overwritten with the decoded string.
429 * If 's' is syntatically incorrect, then the followed fixups will be made:
430 * unknown entities will be left undecoded;
431 * references to unused numeric characters will be deleted.
432 * In particular, � will not be decoded, but will be deleted.
437 /* maximum length of any ISO-LATIN-1 HTML entity name. */
438 #define MAXENTLEN (6)
440 /* The following is a shrinking transformation, therefore safe. */
442 static void decodehtml(char *s)
447 static const char * const entlist[MAXENTLEN + 1] =
451 "lt\074gt\076", /* 2 */
452 "amp\046ETH\320eth\360", /* 3 */
453 "quot\042Auml\304Euml\313Iuml\317Ouml\326Uuml\334auml\344euml\353\
454 iuml\357ouml\366uuml\374yuml\377", /* 4 */
455 "Acirc\302Aring\305AElig\306Ecirc\312Icirc\316Ocirc\324Ucirc\333\
456 THORN\336szlig\337acirc\342aring\345aelig\346ecirc\352icirc\356ocirc\364\
457 ucirc\373thorn\376", /* 5 */
458 "Agrave\300Aacute\301Atilde\303Ccedil\307Egrave\310Eacute\311\
459 Igrave\314Iacute\315Ntilde\321Ograve\322Oacute\323Otilde\325Oslash\330\
460 Ugrave\331Uacute\332Yacute\335agrave\340aacute\341atilde\343ccedil\347\
461 egrave\350eacute\351igrave\354iacute\355ntilde\361ograve\362oacute\363\
462 otilde\365oslash\370ugrave\371uacute\372yacute\375" /* 6 */
465 for (; *s != '\0'; s++, p++) {
470 /* find end of entity */
471 for (i = 1; s[i] != ';' && s[i] != '\0'; i++) {
475 if (s[i] == '\0') { /* treat as normal data */
480 /* is it numeric ? */
482 for (j = 2, val = 0; j < i && apr_isdigit(s[j]); j++) {
483 val = val * 10 + s[j] - '0';
486 if (j < i || val <= 8 || (val >= 11 && val <= 31) ||
487 (val >= 127 && val <= 160) || val >= 256) {
488 p--; /* no data to output */
491 *p = RAW_ASCII_CHAR(val);
496 if (j > MAXENTLEN || entlist[j] == NULL) {
499 continue; /* skip it */
501 for (ents = entlist[j]; *ents != '\0'; ents += i) {
502 if (strncmp(s + 1, ents, j) == 0) {
508 *p = '&'; /* unknown */
511 *p = RAW_ASCII_CHAR(((const unsigned char *) ents)[j]);
521 * Extract the next tag name and value.
522 * If there are no more tags, set the tag name to NULL.
523 * The tag value is html decoded if dodecode is non-zero.
524 * The tag value may be NULL if there is no tag value..
526 * [WS]<Tag>[WS]=[WS]['|"]<Value>['|"|WS]
529 #define SKIP_TAG_WHITESPACE(ptr) while ((*ptr != '\0') && (apr_isspace (*ptr))) ptr++
531 static void ap_ssi_get_tag_and_value(include_ctx_t *ctx, char **tag,
532 char **tag_val, int dodecode)
534 char *c = ctx->curr_tag_pos;
539 SKIP_TAG_WHITESPACE(c);
540 *tag = c; /* First non-whitespace character (could be NULL). */
542 while ((*c != '\0') && (*c != '=') && (!apr_isspace(*c))) {
543 *c = apr_tolower(*c); /* find end of tag, lowercasing as we go... */
547 if ((*c == '\0') || (**tag == '=')) {
548 if ((**tag == '\0') || (**tag == '=')) {
551 ctx->curr_tag_pos = c;
552 return; /* We have found the end of the buffer. */
553 } /* We might have a tag, but definitely no value. */
556 *c++ = '\0'; /* Overwrite the '=' with a terminating byte after tag. */
558 else { /* Try skipping WS to find the '='. */
559 *c++ = '\0'; /* Terminate the tag... */
560 SKIP_TAG_WHITESPACE(c);
562 if (*c != '=') { /* There needs to be an equal sign if there's a value. */
563 ctx->curr_tag_pos = c;
564 return; /* There apparently was no value. */
567 c++; /* Skip the equals sign. */
571 SKIP_TAG_WHITESPACE(c);
572 if (*c == '"' || *c == '\'') { /* Allow quoted values for space inclusion. */
573 term = *c++; /* NOTE: This does not pass the quotes on return. */
577 while ((*c != '\0') &&
578 (((term != '\0') && (*c != term)) ||
579 ((term == '\0') && (!apr_isspace(*c))))) {
580 if (*c == '\\') { /* Accept \" and \' as valid char in string. */
582 if (*c == term) { /* Overwrite the "\" during the embedded */
583 shift_val++; /* escape sequence of '\"' or "\'". Shift */
584 } /* bytes from here to next delimiter. */
596 *(c-shift_val) = '\0'; /* Overwrites delimiter (term or WS) with NULL. */
597 ctx->curr_tag_pos = ++c;
599 decodehtml(*tag_val);
607 * Do variable substitution on strings
609 static void ap_ssi_parse_string(request_rec *r, const char *in, char *out,
610 size_t length, int leave_name)
616 /* leave room for nul terminator */
617 end_out = out + length - 1;
619 while ((ch = *in++) != '\0') {
622 if (next == end_out) {
636 const char *start_of_var_name;
637 char *end_of_var_name; /* end of var name + 1 */
638 const char *expansion, *temp_end, *val;
642 /* guess that the expansion won't happen */
646 start_of_var_name = in;
647 in = ap_strchr_c(in, '}');
649 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
650 0, r, "Missing '}' on variable \"%s\"",
656 end_of_var_name = (char *)temp_end;
660 start_of_var_name = in;
661 while (apr_isalnum(*in) || *in == '_') {
665 end_of_var_name = (char *)temp_end;
667 /* what a pain, too bad there's no table_getn where you can
668 * pass a non-nul terminated string */
669 l = end_of_var_name - start_of_var_name;
671 tmp_store = *end_of_var_name;
672 *end_of_var_name = '\0';
673 val = apr_table_get(r->subprocess_env, start_of_var_name);
674 *end_of_var_name = tmp_store;
678 l = strlen(expansion);
680 else if (leave_name) {
684 break; /* no expansion to be done */
688 /* zero-length variable name causes just the $ to be copied */
691 l = ((int)l > end_out - next) ? (end_out - next) : l;
692 memcpy(next, expansion, l);
697 if (next == end_out) {
710 /* --------------------------- Action handlers ---------------------------- */
712 /* ensure that path is relative, and does not contain ".." elements
713 * ensentially ensure that it does not match the regex:
714 * (^/|(^|/)\.\.(/|$))
715 * XXX: Needs to become apr_is_path_relative() test
717 static int is_only_below(const char *path)
719 #ifdef HAVE_DRIVE_LETTERS
724 if (strchr(path, ':')
727 if (path[0] == '/') {
732 while (path[dots] == '.')
735 /* If the name is canonical this is redundant
736 * but in security, redundancy is worthwhile.
737 * Does OS2 belong here (accepts ... for ..)?
739 if (dots > 1 && (!path[dots] || path[dots] == '/'))
742 if (dots == 2 && (!path[dots] || path[dots] == '/'))
746 while (*path && *(path++) != '/')
752 static int handle_include(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
753 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
756 char *tag_val = NULL;
757 apr_bucket *tmp_buck;
758 char parsed_string[MAX_STRING_LEN];
760 *inserted_head = NULL;
761 if (ctx->flags & FLAG_PRINTING) {
763 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
764 if (tag_val == NULL) {
772 if (!strcmp(tag, "file") || !strcmp(tag, "virtual")) {
773 request_rec *rr = NULL;
774 char *error_fmt = NULL;
776 ap_ssi_parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
778 /* be safe; only files in this directory or below allowed */
779 if (!is_only_below(parsed_string)) {
780 error_fmt = "unable to include file \"%s\" "
784 rr = ap_sub_req_lookup_file(parsed_string, r, f->next);
788 rr = ap_sub_req_lookup_uri(parsed_string, r, f->next);
791 if (!error_fmt && rr->status != HTTP_OK) {
792 error_fmt = "unable to include \"%s\" in parsed file %s";
795 if (!error_fmt && (ctx->flags & FLAG_NO_EXEC) && rr->content_type
796 && (strncmp(rr->content_type, "text/", 5))) {
797 error_fmt = "unable to include potential exec \"%s\" "
800 if (error_fmt == NULL) {
801 /* try to avoid recursive includes. We do this by walking
802 * up the r->main list of subrequests, and at each level
803 * walking back through any internal redirects. At each
804 * step, we compare the filenames and the URIs.
806 * The filename comparison catches a recursive include
807 * with an ever-changing URL, eg.
808 * <!--#include virtual=
809 * "$REQUEST_URI/$QUERY_STRING?$QUERY_STRING/x"-->
810 * which, although they would eventually be caught because
811 * we have a limit on the length of files, etc., can
812 * recurse for a while.
814 * The URI comparison catches the case where the filename
815 * is changed while processing the request, so the
816 * current name is never the same as any previous one.
817 * This can happen with "DocumentRoot /foo" when you
818 * request "/" on the server and it includes "/".
819 * This only applies to modules such as mod_dir that
820 * (somewhat improperly) mess with r->filename outside
821 * of a filename translation phase.
825 for (p = r; p != NULL && !founddupe; p = p->main) {
827 for (q = p; q != NULL; q = q->prev) {
828 if ( (strcmp(q->filename, rr->filename) == 0) ||
829 (strcmp(q->uri, rr->uri) == 0) ){
837 error_fmt = "Recursive include of \"%s\" "
842 /* See the Kludge in send_parsed_file for why */
843 /* Basically, it puts a bread crumb in here, then looks */
844 /* for the crumb later to see if its been here. */
846 ap_set_module_config(rr->request_config, &include_module, r);
849 SPLIT_AND_PASS_PRETAG_BUCKETS(*bb, ctx, f->next);
851 if (ap_run_sub_req(rr)) {
852 error_fmt = "unable to include \"%s\" in parsed file %s";
856 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
857 0, r, error_fmt, tag_val, r->filename);
858 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
861 /* destroy the sub request if it's not a nested include (crumb) */
863 && ap_get_module_config(rr->request_config, &include_module)
864 != NESTED_INCLUDE_MAGIC) {
865 ap_destroy_sub_req(rr);
869 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
870 "unknown parameter \"%s\" to tag include in %s",
872 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
880 static int handle_echo(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
881 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
884 char *tag_val = NULL;
885 const char *echo_text = NULL;
886 apr_bucket *tmp_buck;
887 apr_size_t e_len, e_wrt;
888 enum {E_NONE, E_URL, E_ENTITY} encode;
892 *inserted_head = NULL;
893 if (ctx->flags & FLAG_PRINTING) {
895 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
896 if (tag_val == NULL) {
904 if (!strcmp(tag, "var")) {
905 const char *val = apr_table_get(r->subprocess_env, tag_val);
909 case E_NONE: echo_text = val; break;
910 case E_URL: echo_text = ap_escape_uri(r->pool, val); break;
911 case E_ENTITY: echo_text = ap_escape_html(r->pool, val); break;
914 e_len = strlen(echo_text);
915 tmp_buck = apr_bucket_heap_create(echo_text, e_len, 1, &e_wrt);
918 tmp_buck = apr_bucket_immortal_create("(none)", sizeof("(none)")-1);
920 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
921 if (*inserted_head == NULL) {
922 *inserted_head = tmp_buck;
925 else if (!strcmp(tag, "encoding")) {
926 if (!strcasecmp(tag_val, "none")) encode = E_NONE;
927 else if (!strcasecmp(tag_val, "url")) encode = E_URL;
928 else if (!strcasecmp(tag_val, "entity")) encode = E_ENTITY;
930 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
931 "unknown value \"%s\" to parameter \"encoding\" of "
932 "tag echo in %s", tag_val, r->filename);
933 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
937 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
938 "unknown parameter \"%s\" in tag echo of %s",
940 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
948 /* error and tf must point to a string with room for at
949 * least MAX_STRING_LEN characters
951 static int handle_config(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
952 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
955 char *tag_val = NULL;
956 char parsed_string[MAX_STRING_LEN];
957 apr_table_t *env = r->subprocess_env;
959 *inserted_head = NULL;
960 if (ctx->flags & FLAG_PRINTING) {
962 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 0);
963 if (tag_val == NULL) {
965 return 0; /* Reached the end of the string. */
968 return 1; /* tags must have values. */
971 if (!strcmp(tag, "errmsg")) {
972 ap_ssi_parse_string(r, tag_val, ctx->error_str, MAX_STRING_LEN, 0);
973 ctx->error_length = strlen(ctx->error_str);
975 else if (!strcmp(tag, "timefmt")) {
976 apr_time_t date = r->request_time;
978 ap_ssi_parse_string(r, tag_val, ctx->time_str, MAX_STRING_LEN, 0);
979 apr_table_setn(env, "DATE_LOCAL", ap_ht_time(r->pool, date, ctx->time_str, 0));
980 apr_table_setn(env, "DATE_GMT", ap_ht_time(r->pool, date, ctx->time_str, 1));
981 apr_table_setn(env, "LAST_MODIFIED",
982 ap_ht_time(r->pool, r->finfo.mtime, ctx->time_str, 0));
984 else if (!strcmp(tag, "sizefmt")) {
985 ap_ssi_parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
986 decodehtml(parsed_string);
987 if (!strcmp(parsed_string, "bytes")) {
988 ctx->flags |= FLAG_SIZE_IN_BYTES;
990 else if (!strcmp(parsed_string, "abbrev")) {
991 ctx->flags &= FLAG_SIZE_ABBREV;
995 apr_bucket *tmp_buck;
997 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
998 "unknown parameter \"%s\" to tag config in %s",
1000 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
1008 static int find_file(request_rec *r, const char *directive, const char *tag,
1009 char *tag_val, apr_finfo_t *finfo)
1011 char *to_send = tag_val;
1012 request_rec *rr = NULL;
1014 char *error_fmt = NULL;
1015 apr_status_t rv = APR_SUCCESS;
1017 if (!strcmp(tag, "file")) {
1018 /* be safe; only files in this directory or below allowed */
1019 if (!is_only_below(tag_val)) {
1020 error_fmt = "unable to access file \"%s\" "
1021 "in parsed file %s";
1024 ap_getparents(tag_val); /* get rid of any nasties */
1026 /* note: it is okay to pass NULL for the "next filter" since
1027 we never attempt to "run" this sub request. */
1028 rr = ap_sub_req_lookup_file(tag_val, r, NULL);
1030 if (rr->status == HTTP_OK && rr->finfo.filetype != 0) {
1031 to_send = rr->filename;
1032 if ((rv = apr_stat(finfo, to_send, APR_FINFO_GPROT
1033 | APR_FINFO_MIN, rr->pool)) != APR_SUCCESS
1034 && rv != APR_INCOMPLETE) {
1035 error_fmt = "unable to get information about \"%s\" "
1036 "in parsed file %s";
1040 error_fmt = "unable to lookup information about \"%s\" "
1041 "in parsed file %s";
1047 ap_log_rerror(APLOG_MARK, APLOG_ERR | (rv ? 0 : APLOG_NOERRNO),
1048 rv, r, error_fmt, to_send, r->filename);
1051 if (rr) ap_destroy_sub_req(rr);
1055 else if (!strcmp(tag, "virtual")) {
1056 /* note: it is okay to pass NULL for the "next filter" since
1057 we never attempt to "run" this sub request. */
1058 rr = ap_sub_req_lookup_uri(tag_val, r, NULL);
1060 if (rr->status == HTTP_OK && rr->finfo.filetype != 0) {
1061 memcpy((char *) finfo, (const char *) &rr->finfo,
1063 ap_destroy_sub_req(rr);
1067 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1068 "unable to get information about \"%s\" "
1069 "in parsed file %s",
1070 tag_val, r->filename);
1071 ap_destroy_sub_req(rr);
1076 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1077 "unknown parameter \"%s\" to tag %s in %s",
1078 tag, directive, r->filename);
1083 static int handle_fsize(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
1084 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
1087 char *tag_val = NULL;
1089 apr_size_t s_len, s_wrt;
1090 apr_bucket *tmp_buck;
1091 char parsed_string[MAX_STRING_LEN];
1093 *inserted_head = NULL;
1094 if (ctx->flags & FLAG_PRINTING) {
1096 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
1097 if (tag_val == NULL) {
1106 ap_ssi_parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1107 if (!find_file(r, "fsize", tag, parsed_string, &finfo)) {
1110 if (!(ctx->flags & FLAG_SIZE_IN_BYTES)) {
1111 apr_strfsize(finfo.size, buff);
1112 s_len = strlen (buff);
1118 apr_snprintf(tmp_buff, sizeof(tmp_buff), "%" APR_OFF_T_FMT, finfo.size);
1119 l = strlen(tmp_buff); /* grrr */
1120 for (x = 0; x < l; x++) {
1121 if (x && (!((l - x) % 3))) {
1124 buff[pos++] = tmp_buff[x];
1130 tmp_buck = apr_bucket_heap_create(buff, s_len, 1, &s_wrt);
1131 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
1132 if (*inserted_head == NULL) {
1133 *inserted_head = tmp_buck;
1137 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
1145 static int handle_flastmod(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
1146 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
1149 char *tag_val = NULL;
1151 apr_size_t t_len, t_wrt;
1152 apr_bucket *tmp_buck;
1153 char parsed_string[MAX_STRING_LEN];
1155 *inserted_head = NULL;
1156 if (ctx->flags & FLAG_PRINTING) {
1158 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
1159 if (tag_val == NULL) {
1168 ap_ssi_parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1169 if (!find_file(r, "flastmod", tag, parsed_string, &finfo)) {
1172 t_val = ap_ht_time(r->pool, finfo.mtime, ctx->time_str, 0);
1173 t_len = strlen(t_val);
1175 tmp_buck = apr_bucket_heap_create(t_val, t_len, 1, &t_wrt);
1176 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
1177 if (*inserted_head == NULL) {
1178 *inserted_head = tmp_buck;
1182 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
1190 static int re_check(request_rec *r, char *string, char *rexp)
1195 compiled = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_NOSUB);
1196 if (compiled == NULL) {
1197 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1198 "unable to compile pattern \"%s\"", rexp);
1201 regex_error = ap_regexec(compiled, string, 0, (regmatch_t *) NULL, 0);
1202 ap_pregfree(r->pool, compiled);
1203 return (!regex_error);
1208 token_and, token_or, token_not, token_eq, token_ne,
1209 token_rbrace, token_lbrace, token_group,
1210 token_ge, token_le, token_gt, token_lt
1213 enum token_type type;
1214 char value[MAX_STRING_LEN];
1217 /* there is an implicit assumption here that string is at most MAX_STRING_LEN-1
1218 * characters long...
1220 static const char *get_ptoken(request_rec *r, const char *string, struct token *token,
1228 /* Skip leading white space */
1229 if (string == (char *) NULL) {
1230 return (char *) NULL;
1232 while ((ch = *string++)) {
1233 if (!apr_isspace(ch)) {
1238 return (char *) NULL;
1241 token->type = token_string; /* the default type */
1244 token->type = token_lbrace;
1247 token->type = token_rbrace;
1250 token->type = token_eq;
1253 if (*string == '=') {
1254 token->type = token_ne;
1255 return (string + 1);
1258 token->type = token_not;
1262 token->type = token_string;
1266 if (*string == '|') {
1267 token->type = token_or;
1268 return (string + 1);
1272 if (*string == '&') {
1273 token->type = token_and;
1274 return (string + 1);
1278 if (*string == '=') {
1279 token->type = token_ge;
1280 return (string + 1);
1283 token->type = token_gt;
1287 if (*string == '=') {
1288 token->type = token_le;
1289 return (string + 1);
1292 token->type = token_lt;
1296 token->type = token_string;
1299 /* We should only be here if we are in a string */
1301 token->value[next++] = ch;
1305 * Yes I know that goto's are BAD. But, c doesn't allow me to
1306 * exit a loop from a switch statement. Yes, I could use a flag,
1307 * but that is (IMHO) even less readable/maintainable than the goto.
1310 * I used the ++string throughout this section so that string
1311 * ends up pointing to the next token and I can just return it
1313 for (ch = *string; ((ch != '\0') && (!tkn_fnd)); ch = *++string) {
1315 if ((ch = *++string) == '\0') {
1319 token->value[next++] = ch;
1324 if (apr_isspace(ch)) {
1338 if (*(string + 1) == '|') {
1343 if (*(string + 1) == '&') {
1349 token->value[next++] = ch;
1360 token->value[next++] = ch;
1366 /* If qs is still set, I have an unmatched ' */
1371 token->value[next] = '\0';
1377 * Hey I still know that goto's are BAD. I don't think that I've ever
1378 * used two in the same project, let alone the same file before. But,
1379 * I absolutely want to make sure that I clean up the memory in all
1380 * cases. And, without rewriting this completely, the easiest way
1381 * is to just branch to the return code which cleans it up.
1383 /* there is an implicit assumption here that expr is at most MAX_STRING_LEN-1
1384 * characters long...
1386 static int parse_expr(request_rec *r, const char *expr, int *was_error,
1387 int *was_unmatched, char *debug)
1390 struct parse_node *left, *right, *parent;
1393 } *root, *current, *new;
1395 char buffer[MAX_STRING_LEN];
1396 apr_pool_t *expr_pool;
1398 apr_size_t debug_pos = 0;
1400 debug[debug_pos] = '\0';
1403 if ((parse = expr) == (char *) NULL) {
1406 root = current = (struct parse_node *) NULL;
1407 if (apr_pool_create(&expr_pool, r->pool) != APR_SUCCESS)
1410 /* Create Parse Tree */
1412 new = (struct parse_node *) apr_palloc(expr_pool,
1413 sizeof(struct parse_node));
1414 new->parent = new->left = new->right = (struct parse_node *) NULL;
1416 if ((parse = get_ptoken(r, parse, &new->token, was_unmatched)) == (char *) NULL) {
1419 switch (new->token.type) {
1422 #ifdef DEBUG_INCLUDE
1423 debug_pos += sprintf (&debug[debug_pos], " Token: string (%s)\n",
1426 if (current == (struct parse_node *) NULL) {
1427 root = current = new;
1430 switch (current->token.type) {
1432 if (current->token.value[0] != '\0') {
1433 strncat(current->token.value, " ",
1434 sizeof(current->token.value)
1435 - strlen(current->token.value) - 1);
1437 strncat(current->token.value, new->token.value,
1438 sizeof(current->token.value)
1439 - strlen(current->token.value) - 1);
1440 current->token.value[sizeof(current->token.value) - 1] = '\0';
1452 new->parent = current;
1453 current = current->right = new;
1456 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1457 "Invalid expression \"%s\" in file %s",
1466 #ifdef DEBUG_INCLUDE
1467 memcpy (&debug[debug_pos], " Token: and/or\n",
1468 sizeof (" Token: and/or\n"));
1469 debug_pos += sizeof (" Token: and/or\n");
1471 if (current == (struct parse_node *) NULL) {
1472 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1473 "Invalid expression \"%s\" in file %s",
1478 /* Percolate upwards */
1479 while (current != (struct parse_node *) NULL) {
1480 switch (current->token.type) {
1492 current = current->parent;
1497 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1498 "Invalid expression \"%s\" in file %s",
1505 if (current == (struct parse_node *) NULL) {
1507 new->left->parent = new;
1508 new->parent = (struct parse_node *) NULL;
1512 new->left = current->right;
1513 current->right = new;
1514 new->parent = current;
1520 #ifdef DEBUG_INCLUDE
1521 memcpy (&debug[debug_pos], " Token: not\n",
1522 sizeof (" Token: not\n"));
1523 debug_pos += sizeof (" Token: not\n");
1525 if (current == (struct parse_node *) NULL) {
1526 root = current = new;
1529 /* Percolate upwards */
1530 while (current != (struct parse_node *) NULL) {
1531 switch (current->token.type) {
1544 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1545 "Invalid expression \"%s\" in file %s",
1552 if (current == (struct parse_node *) NULL) {
1554 new->left->parent = new;
1555 new->parent = (struct parse_node *) NULL;
1559 new->left = current->right;
1560 current->right = new;
1561 new->parent = current;
1572 #ifdef DEBUG_INCLUDE
1573 memcpy (&debug[debug_pos], " Token: eq/ne/ge/gt/le/lt\n",
1574 sizeof (" Token: eq/ne/ge/gt/le/lt\n"));
1575 debug_pos += sizeof (" Token: eq/ne/ge/gt/le/lt\n");
1577 if (current == (struct parse_node *) NULL) {
1578 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1579 "Invalid expression \"%s\" in file %s",
1584 /* Percolate upwards */
1585 while (current != (struct parse_node *) NULL) {
1586 switch (current->token.type) {
1589 current = current->parent;
1603 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1604 "Invalid expression \"%s\" in file %s",
1611 if (current == (struct parse_node *) NULL) {
1613 new->left->parent = new;
1614 new->parent = (struct parse_node *) NULL;
1618 new->left = current->right;
1619 current->right = new;
1620 new->parent = current;
1626 #ifdef DEBUG_INCLUDE
1627 memcpy (&debug[debug_pos], " Token: rbrace\n",
1628 sizeof (" Token: rbrace\n"));
1629 debug_pos += sizeof (" Token: rbrace\n");
1631 while (current != (struct parse_node *) NULL) {
1632 if (current->token.type == token_lbrace) {
1633 current->token.type = token_group;
1636 current = current->parent;
1638 if (current == (struct parse_node *) NULL) {
1639 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1640 "Unmatched ')' in \"%s\" in file %s",
1648 #ifdef DEBUG_INCLUDE
1649 memcpy (&debug[debug_pos], " Token: lbrace\n",
1650 sizeof (" Token: lbrace\n"));
1651 debug_pos += sizeof (" Token: lbrace\n");
1653 if (current == (struct parse_node *) NULL) {
1654 root = current = new;
1657 /* Percolate upwards */
1658 while (current != (struct parse_node *) NULL) {
1659 switch (current->token.type) {
1674 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1675 "Invalid expression \"%s\" in file %s",
1682 if (current == (struct parse_node *) NULL) {
1684 new->left->parent = new;
1685 new->parent = (struct parse_node *) NULL;
1689 new->left = current->right;
1690 current->right = new;
1691 new->parent = current;
1700 /* Evaluate Parse Tree */
1702 while (current != (struct parse_node *) NULL) {
1703 switch (current->token.type) {
1705 #ifdef DEBUG_INCLUDE
1706 memcpy (&debug[debug_pos], " Evaluate string\n",
1707 sizeof (" Evaluate string\n"));
1708 debug_pos += sizeof (" Evaluate string\n");
1710 ap_ssi_parse_string(r, current->token.value, buffer, sizeof(buffer), 0);
1711 apr_cpystrn(current->token.value, buffer, sizeof(current->token.value));
1712 current->value = (current->token.value[0] != '\0');
1714 current = current->parent;
1719 #ifdef DEBUG_INCLUDE
1720 memcpy (&debug[debug_pos], " Evaluate and/or\n",
1721 sizeof (" Evaluate and/or\n"));
1722 debug_pos += sizeof (" Evaluate and/or\n");
1724 if (current->left == (struct parse_node *) NULL ||
1725 current->right == (struct parse_node *) NULL) {
1726 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1727 "Invalid expression \"%s\" in file %s",
1732 if (!current->left->done) {
1733 switch (current->left->token.type) {
1735 ap_ssi_parse_string(r, current->left->token.value,
1736 buffer, sizeof(buffer), 0);
1737 apr_cpystrn(current->left->token.value, buffer,
1738 sizeof(current->left->token.value));
1739 current->left->value = (current->left->token.value[0] != '\0');
1740 current->left->done = 1;
1743 current = current->left;
1747 if (!current->right->done) {
1748 switch (current->right->token.type) {
1750 ap_ssi_parse_string(r, current->right->token.value,
1751 buffer, sizeof(buffer), 0);
1752 apr_cpystrn(current->right->token.value, buffer,
1753 sizeof(current->right->token.value));
1754 current->right->value = (current->right->token.value[0] != '\0');
1755 current->right->done = 1;
1758 current = current->right;
1762 #ifdef DEBUG_INCLUDE
1763 debug_pos += sprintf (&debug[debug_pos], " Left: %c\n",
1764 current->left->value ? '1' : '0');
1765 debug_pos += sprintf (&debug[debug_pos], " Right: %c\n",
1766 current->right->value ? '1' : '0');
1768 if (current->token.type == token_and) {
1769 current->value = current->left->value && current->right->value;
1772 current->value = current->left->value || current->right->value;
1774 #ifdef DEBUG_INCLUDE
1775 debug_pos += sprintf (&debug[debug_pos], " Returning %c\n",
1776 current->value ? '1' : '0');
1779 current = current->parent;
1784 #ifdef DEBUG_INCLUDE
1785 memcpy (&debug[debug_pos], " Evaluate eq/ne\n",
1786 sizeof (" Evaluate eq/ne\n"));
1787 debug_pos += sizeof (" Evaluate eq/ne\n");
1789 if ((current->left == (struct parse_node *) NULL) ||
1790 (current->right == (struct parse_node *) NULL) ||
1791 (current->left->token.type != token_string) ||
1792 (current->right->token.type != token_string)) {
1793 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1794 "Invalid expression \"%s\" in file %s",
1799 ap_ssi_parse_string(r, current->left->token.value,
1800 buffer, sizeof(buffer), 0);
1801 apr_cpystrn(current->left->token.value, buffer,
1802 sizeof(current->left->token.value));
1803 ap_ssi_parse_string(r, current->right->token.value,
1804 buffer, sizeof(buffer), 0);
1805 apr_cpystrn(current->right->token.value, buffer,
1806 sizeof(current->right->token.value));
1807 if (current->right->token.value[0] == '/') {
1809 len = strlen(current->right->token.value);
1810 if (current->right->token.value[len - 1] == '/') {
1811 current->right->token.value[len - 1] = '\0';
1814 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1815 "Invalid rexp \"%s\" in file %s",
1816 current->right->token.value, r->filename);
1820 #ifdef DEBUG_INCLUDE
1821 debug_pos += sprintf (&debug[debug_pos],
1822 " Re Compare (%s) with /%s/\n",
1823 current->left->token.value,
1824 ¤t->right->token.value[1]);
1827 re_check(r, current->left->token.value,
1828 ¤t->right->token.value[1]);
1831 #ifdef DEBUG_INCLUDE
1832 debug_pos += sprintf (&debug[debug_pos],
1833 " Compare (%s) with (%s)\n",
1834 current->left->token.value,
1835 current->right->token.value);
1838 (strcmp(current->left->token.value,
1839 current->right->token.value) == 0);
1841 if (current->token.type == token_ne) {
1842 current->value = !current->value;
1844 #ifdef DEBUG_INCLUDE
1845 debug_pos += sprintf (&debug[debug_pos], " Returning %c\n",
1846 current->value ? '1' : '0');
1849 current = current->parent;
1855 #ifdef DEBUG_INCLUDE
1856 memcpy (&debug[debug_pos], " Evaluate ge/gt/le/lt\n",
1857 sizeof (" Evaluate ge/gt/le/lt\n"));
1858 debug_pos += sizeof (" Evaluate ge/gt/le/lt\n");
1860 if ((current->left == (struct parse_node *) NULL) ||
1861 (current->right == (struct parse_node *) NULL) ||
1862 (current->left->token.type != token_string) ||
1863 (current->right->token.type != token_string)) {
1864 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1865 "Invalid expression \"%s\" in file %s",
1870 ap_ssi_parse_string(r, current->left->token.value,
1871 buffer, sizeof(buffer), 0);
1872 apr_cpystrn(current->left->token.value, buffer,
1873 sizeof(current->left->token.value));
1874 ap_ssi_parse_string(r, current->right->token.value,
1875 buffer, sizeof(buffer), 0);
1876 apr_cpystrn(current->right->token.value, buffer,
1877 sizeof(current->right->token.value));
1878 #ifdef DEBUG_INCLUDE
1879 debug_pos += sprintf (&debug[debug_pos],
1880 " Compare (%s) with (%s)\n",
1881 current->left->token.value,
1882 current->right->token.value);
1885 strcmp(current->left->token.value,
1886 current->right->token.value);
1887 if (current->token.type == token_ge) {
1888 current->value = current->value >= 0;
1890 else if (current->token.type == token_gt) {
1891 current->value = current->value > 0;
1893 else if (current->token.type == token_le) {
1894 current->value = current->value <= 0;
1896 else if (current->token.type == token_lt) {
1897 current->value = current->value < 0;
1900 current->value = 0; /* Don't return -1 if unknown token */
1902 #ifdef DEBUG_INCLUDE
1903 debug_pos += sprintf (&debug[debug_pos], " Returning %c\n",
1904 current->value ? '1' : '0');
1907 current = current->parent;
1911 if (current->right != (struct parse_node *) NULL) {
1912 if (!current->right->done) {
1913 current = current->right;
1916 current->value = !current->right->value;
1921 #ifdef DEBUG_INCLUDE
1922 debug_pos += sprintf (&debug[debug_pos], " Evaluate !: %c\n",
1923 current->value ? '1' : '0');
1926 current = current->parent;
1930 if (current->right != (struct parse_node *) NULL) {
1931 if (!current->right->done) {
1932 current = current->right;
1935 current->value = current->right->value;
1940 #ifdef DEBUG_INCLUDE
1941 debug_pos += sprintf (&debug[debug_pos], " Evaluate (): %c\n",
1942 current->value ? '1' : '0');
1945 current = current->parent;
1949 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1950 "Unmatched '(' in \"%s\" in file %s",
1956 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1957 "Unmatched ')' in \"%s\" in file %s",
1963 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1970 retval = (root == (struct parse_node *) NULL) ? 0 : root->value;
1972 apr_pool_destroy(expr_pool);
1976 /*-------------------------------------------------------------------------*/
1977 #ifdef DEBUG_INCLUDE
1979 /* XXX overlaying the static string pointed to by cond_txt isn't cool */
1981 #define MAX_DEBUG_SIZE MAX_STRING_LEN
1982 #define LOG_COND_STATUS(cntx, t_buck, h_ptr, ins_head, tag_text) \
1984 char *cond_txt = "**** X conditional_status=\"0\"\n"; \
1987 if (cntx->flags & FLAG_COND_TRUE) { \
1988 cond_txt[31] = '1'; \
1990 memcpy(&cond_txt[5], tag_text, sizeof(tag_text)); \
1991 t_buck = apr_bucket_heap_create(cond_txt, sizeof(cond_txt), 1, &c_wrt); \
1992 APR_BUCKET_INSERT_BEFORE(h_ptr, t_buck); \
1994 if (ins_head == NULL) { \
1995 ins_head = t_buck; \
1998 #define DUMP_PARSE_EXPR_DEBUG(t_buck, h_ptr, d_buf, ins_head) \
2001 if (d_buf[0] != '\0') { \
2002 t_buck = apr_bucket_heap_create(d_buf, strlen(d_buf), 1, &b_wrt); \
2003 APR_BUCKET_INSERT_BEFORE(h_ptr, t_buck); \
2005 if (ins_head == NULL) { \
2006 ins_head = t_buck; \
2012 #define MAX_DEBUG_SIZE 10
2013 #define LOG_COND_STATUS(cntx, t_buck, h_ptr, ins_head, tag_text)
2014 #define DUMP_PARSE_EXPR_DEBUG(t_buck, h_ptr, d_buf, ins_head)
2017 /*-------------------------------------------------------------------------*/
2019 /* pjr - These seem to allow expr="fred" expr="joe" where joe overwrites fred. */
2020 static int handle_if(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
2021 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
2024 char *tag_val = NULL;
2026 int expr_ret, was_error, was_unmatched;
2027 apr_bucket *tmp_buck;
2028 char debug_buf[MAX_DEBUG_SIZE];
2030 *inserted_head = NULL;
2031 if (!ctx->flags & FLAG_PRINTING) {
2032 ctx->if_nesting_level++;
2036 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 0);
2039 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2040 "missing expr in if statement: %s", r->filename);
2041 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2044 expr_ret = parse_expr(r, expr, &was_error, &was_unmatched, debug_buf);
2046 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2049 if (was_unmatched) {
2050 DUMP_PARSE_EXPR_DEBUG(tmp_buck, head_ptr, "\nUnmatched '\n",
2053 DUMP_PARSE_EXPR_DEBUG(tmp_buck, head_ptr, debug_buf, *inserted_head);
2056 ctx->flags |= (FLAG_PRINTING | FLAG_COND_TRUE);
2059 ctx->flags &= FLAG_CLEAR_PRINT_COND;
2061 LOG_COND_STATUS(ctx, tmp_buck, head_ptr, *inserted_head, " if");
2062 ctx->if_nesting_level = 0;
2065 else if (!strcmp(tag, "expr")) {
2067 #ifdef DEBUG_INCLUDE
2069 apr_size_t d_len = 0, d_wrt = 0;
2070 d_len = sprintf(debug_buf, "**** if expr=\"%s\"\n", expr);
2071 tmp_buck = apr_bucket_heap_create(debug_buf, d_len, 1, &d_wrt);
2072 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
2074 if (*inserted_head == NULL) {
2075 *inserted_head = tmp_buck;
2081 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2082 "unknown parameter \"%s\" to tag if in %s", tag, r->filename);
2083 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2091 static int handle_elif(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
2092 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
2095 char *tag_val = NULL;
2097 int expr_ret, was_error, was_unmatched;
2098 apr_bucket *tmp_buck;
2099 char debug_buf[MAX_DEBUG_SIZE];
2101 *inserted_head = NULL;
2102 if (!ctx->if_nesting_level) {
2104 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 0);
2106 LOG_COND_STATUS(ctx, tmp_buck, head_ptr, *inserted_head, " elif");
2108 if (ctx->flags & FLAG_COND_TRUE) {
2109 ctx->flags &= FLAG_CLEAR_PRINTING;
2113 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2114 "missing expr in elif statement: %s", r->filename);
2115 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2118 expr_ret = parse_expr(r, expr, &was_error, &was_unmatched, debug_buf);
2120 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2123 if (was_unmatched) {
2124 DUMP_PARSE_EXPR_DEBUG(tmp_buck, head_ptr, "\nUnmatched '\n",
2127 DUMP_PARSE_EXPR_DEBUG(tmp_buck, head_ptr, debug_buf, *inserted_head);
2130 ctx->flags |= (FLAG_PRINTING | FLAG_COND_TRUE);
2133 ctx->flags &= FLAG_CLEAR_PRINT_COND;
2135 LOG_COND_STATUS(ctx, tmp_buck, head_ptr, *inserted_head, " elif");
2138 else if (!strcmp(tag, "expr")) {
2140 #ifdef DEBUG_INCLUDE
2142 apr_size_t d_len = 0, d_wrt = 0;
2143 d_len = sprintf(debug_buf, "**** elif expr=\"%s\"\n", expr);
2144 tmp_buck = apr_bucket_heap_create(debug_buf, d_len, 1, &d_wrt);
2145 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
2147 if (*inserted_head == NULL) {
2148 *inserted_head = tmp_buck;
2154 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2155 "unknown parameter \"%s\" to tag if in %s", tag, r->filename);
2156 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2163 static int handle_else(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
2164 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
2167 char *tag_val = NULL;
2168 apr_bucket *tmp_buck;
2170 *inserted_head = NULL;
2171 if (!ctx->if_nesting_level) {
2172 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
2173 if ((tag != NULL) || (tag_val != NULL)) {
2174 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2175 "else directive does not take tags in %s", r->filename);
2176 if (ctx->flags & FLAG_PRINTING) {
2177 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2182 LOG_COND_STATUS(ctx, tmp_buck, head_ptr, *inserted_head, " else");
2184 if (ctx->flags & FLAG_COND_TRUE) {
2185 ctx->flags &= FLAG_CLEAR_PRINTING;
2188 ctx->flags |= (FLAG_PRINTING | FLAG_COND_TRUE);
2196 static int handle_endif(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
2197 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
2200 char *tag_val = NULL;
2201 apr_bucket *tmp_buck;
2203 *inserted_head = NULL;
2204 if (!ctx->if_nesting_level) {
2205 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
2206 if ((tag != NULL) || (tag_val != NULL)) {
2207 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2208 "endif directive does not take tags in %s", r->filename);
2209 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2213 LOG_COND_STATUS(ctx, tmp_buck, head_ptr, *inserted_head, "endif");
2214 ctx->flags |= (FLAG_PRINTING | FLAG_COND_TRUE);
2219 ctx->if_nesting_level--;
2224 static int handle_set(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
2225 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
2228 char *tag_val = NULL;
2230 apr_bucket *tmp_buck;
2231 char parsed_string[MAX_STRING_LEN];
2233 *inserted_head = NULL;
2234 if (ctx->flags & FLAG_PRINTING) {
2236 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
2237 if ((tag == NULL) && (tag_val == NULL)) {
2240 else if (tag_val == NULL) {
2243 else if (!strcmp(tag, "var")) {
2246 else if (!strcmp(tag, "value")) {
2247 if (var == (char *) NULL) {
2248 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2249 "variable must precede value in set directive in %s",
2251 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2254 ap_ssi_parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
2255 apr_table_setn(r->subprocess_env, apr_pstrdup(r->pool, var),
2256 apr_pstrdup(r->pool, parsed_string));
2259 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2260 "Invalid tag for set directive in %s", r->filename);
2261 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2269 static int handle_printenv(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
2270 ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
2273 char *tag_val = NULL;
2274 apr_bucket *tmp_buck;
2276 if (ctx->flags & FLAG_PRINTING) {
2277 ap_ssi_get_tag_and_value(ctx, &tag, &tag_val, 1);
2278 if ((tag == NULL) && (tag_val == NULL)) {
2279 apr_array_header_t *arr = apr_table_elts(r->subprocess_env);
2280 apr_table_entry_t *elts = (apr_table_entry_t *)arr->elts;
2282 char *key_text, *val_text;
2283 apr_size_t k_len, v_len, t_wrt;
2285 *inserted_head = NULL;
2286 for (i = 0; i < arr->nelts; ++i) {
2287 key_text = ap_escape_html(r->pool, elts[i].key);
2288 val_text = ap_escape_html(r->pool, elts[i].val);
2289 k_len = strlen(key_text);
2290 v_len = strlen(val_text);
2293 tmp_buck = apr_bucket_heap_create(key_text, k_len, 1, &t_wrt);
2294 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
2295 if (*inserted_head == NULL) {
2296 *inserted_head = tmp_buck;
2299 tmp_buck = apr_bucket_immortal_create("=", 1);
2300 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
2302 tmp_buck = apr_bucket_heap_create(val_text, v_len, 1, &t_wrt);
2303 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
2305 tmp_buck = apr_bucket_immortal_create("\n", 1);
2306 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
2311 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2312 "printenv directive does not take tags in %s", r->filename);
2313 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
2320 /* -------------------------- The main function --------------------------- */
2322 static apr_status_t send_parsed_content(apr_bucket_brigade **bb,
2323 request_rec *r, ap_filter_t *f)
2325 include_ctx_t *ctx = f->ctx;
2326 apr_bucket *dptr = APR_BRIGADE_FIRST(*bb);
2327 apr_bucket *tmp_dptr;
2328 apr_bucket_brigade *tag_and_after;
2332 if (r->args) { /* add QUERY stuff to env cause it ain't yet */
2333 char *arg_copy = apr_pstrdup(r->pool, r->args);
2335 apr_table_setn(r->subprocess_env, "QUERY_STRING", r->args);
2336 ap_unescape_url(arg_copy);
2337 apr_table_setn(r->subprocess_env, "QUERY_STRING_UNESCAPED",
2338 ap_escape_shell_cmd(r->pool, arg_copy));
2341 while (dptr != APR_BRIGADE_SENTINEL(*bb)) {
2342 /* State to check for the STARTING_SEQUENCE. */
2343 if ((ctx->state == PRE_HEAD) || (ctx->state == PARSE_HEAD)) {
2345 apr_size_t cleanup_bytes = ctx->parse_pos;
2347 tmp_dptr = find_start_sequence(dptr, ctx, *bb, &do_cleanup);
2349 /* The few bytes stored in the ssi_tag_brigade turned out not to
2350 * be a tag after all. This can only happen if the starting
2351 * tag actually spans brigades. This should be very rare.
2353 if ((do_cleanup) && (!APR_BRIGADE_EMPTY(ctx->ssi_tag_brigade))) {
2354 apr_bucket *tmp_bkt;
2356 tmp_bkt = apr_bucket_immortal_create(STARTING_SEQUENCE,
2358 APR_BRIGADE_INSERT_HEAD(*bb, tmp_bkt);
2359 apr_brigade_cleanup(ctx->ssi_tag_brigade);
2362 /* If I am inside a conditional (if, elif, else) that is false
2363 * then I need to throw away anything contained in it.
2365 if ((!(ctx->flags & FLAG_PRINTING)) && (tmp_dptr != NULL) &&
2366 (dptr != APR_BRIGADE_SENTINEL(*bb))) {
2367 while ((dptr != APR_BRIGADE_SENTINEL(*bb)) &&
2368 (dptr != tmp_dptr)) {
2369 apr_bucket *free_bucket = dptr;
2371 dptr = APR_BUCKET_NEXT (dptr);
2372 apr_bucket_delete(free_bucket);
2376 /* Adjust the current bucket position based on what was found... */
2377 if ((tmp_dptr != NULL) && (ctx->state == PARSE_DIRECTIVE)) {
2378 if (ctx->tag_start_bucket != NULL) {
2379 dptr = ctx->tag_start_bucket;
2382 dptr = APR_BRIGADE_SENTINEL(*bb);
2385 else if ((tmp_dptr != NULL) && (ctx->bytes_parsed >= BYTE_COUNT_THRESHOLD)) {
2386 /* Send the large chunk of pre-tag bytes... */
2387 tag_and_after = apr_brigade_split(*bb, tmp_dptr);
2388 rv = ap_pass_brigade(f->next, *bb);
2389 if (rv != APR_SUCCESS) {
2392 *bb = tag_and_after;
2394 ctx->bytes_parsed = 0;
2396 else if (tmp_dptr == NULL) { /* There was no possible SSI tag in the */
2397 dptr = APR_BRIGADE_SENTINEL(*bb); /* remainder of this brigade... */
2401 /* State to check for the ENDING_SEQUENCE. */
2402 if (((ctx->state == PARSE_DIRECTIVE) ||
2403 (ctx->state == PARSE_TAG) ||
2404 (ctx->state == PARSE_TAIL)) &&
2405 (dptr != APR_BRIGADE_SENTINEL(*bb))) {
2406 tmp_dptr = find_end_sequence(dptr, ctx, *bb);
2408 if (tmp_dptr != NULL) {
2409 dptr = tmp_dptr; /* Adjust bucket pos... */
2411 /* If some of the tag has already been set aside then set
2412 * aside remainder of tag. Now the full tag is in ssi_tag_brigade.
2413 * If none has yet been set aside, then leave it all where it is.
2414 * In any event after this the entire set of tag buckets will be
2415 * in one place or another.
2417 if (!APR_BRIGADE_EMPTY(ctx->ssi_tag_brigade)) {
2418 tag_and_after = apr_brigade_split(*bb, dptr);
2419 APR_BRIGADE_CONCAT(ctx->ssi_tag_brigade, *bb);
2420 *bb = tag_and_after;
2422 else if (ctx->bytes_parsed >= BYTE_COUNT_THRESHOLD) {
2423 SPLIT_AND_PASS_PRETAG_BUCKETS(*bb, ctx, f->next);
2427 dptr = APR_BRIGADE_SENTINEL(*bb); /* remainder of this brigade... */
2431 /* State to processed the directive... */
2432 if (ctx->state == PARSED) {
2433 apr_bucket *content_head = NULL, *tmp_bkt;
2435 char tmp_buf[TMP_BUF_SIZE];
2436 int (*handle_func)(include_ctx_t *, apr_bucket_brigade **, request_rec *,
2437 ap_filter_t *, apr_bucket *, apr_bucket **);
2439 /* By now the full tag (all buckets) should either be set aside into
2440 * ssi_tag_brigade or contained within the current bb. All tag
2441 * processing from here on can assume that.
2444 /* At this point, everything between ctx->head_start_bucket and
2445 * ctx->tail_start_bucket is an SSI
2446 * directive, we just have to deal with it now.
2448 if (get_combined_directive(ctx, r, *bb, tmp_buf,
2449 TMP_BUF_SIZE) != APR_SUCCESS) {
2450 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2451 "mod_include: error copying directive in %s",
2453 CREATE_ERROR_BUCKET(ctx, tmp_bkt, dptr, content_head);
2455 /* DO CLEANUP HERE!!!!! */
2456 tmp_dptr = ctx->head_start_bucket;
2457 if (!APR_BRIGADE_EMPTY(ctx->ssi_tag_brigade)) {
2458 apr_brigade_cleanup(ctx->ssi_tag_brigade);
2463 tmp_dptr = APR_BUCKET_NEXT (tmp_dptr);
2464 apr_bucket_delete(tmp_bkt);
2465 } while ((tmp_dptr != dptr) &&
2466 (tmp_dptr != APR_BRIGADE_SENTINEL(*bb)));
2472 /* Can't destroy the tag buckets until I'm done processing
2473 * because the combined_tag might just be pointing to
2474 * the contents of a single bucket!
2477 /* Retrieve the handler function to be called for this directive from the
2478 * functions registered in the hash table.
2479 * Need to lower case the directive for proper matching. Also need to have
2480 * it NULL terminated (and include the NULL in the length) for proper
2483 for (tmp_i = 0; tmp_i < ctx->directive_length; tmp_i++) {
2484 ctx->combined_tag[tmp_i] = apr_tolower(ctx->combined_tag[tmp_i]);
2486 ctx->combined_tag[ctx->directive_length] = '\0';
2487 ctx->curr_tag_pos = &ctx->combined_tag[ctx->directive_length+1];
2490 (include_handler_fn_t *)apr_hash_get(include_hash,
2492 ctx->directive_length+1);
2493 if (handle_func != NULL) {
2494 ret = (*handle_func)(ctx, bb, r, f, dptr, &content_head);
2497 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2498 "unknown directive \"%s\" in parsed doc %s",
2499 ctx->combined_tag, r->filename);
2500 CREATE_ERROR_BUCKET(ctx, tmp_bkt, dptr, content_head);
2503 /* This chunk of code starts at the first bucket in the chain
2504 * of tag buckets (assuming that by this point the bucket for
2505 * the STARTING_SEQUENCE has been split) and loops through to
2506 * the end of the tag buckets freeing them all.
2508 * Remember that some part of this may have been set aside
2509 * into the ssi_tag_brigade and the remainder (possibly as
2510 * little as one byte) will be in the current brigade.
2512 * The value of dptr should have been set during the
2513 * PARSE_TAIL state to the first bucket after the
2516 * The value of content_head may have been set during processing
2517 * of the directive. If so, the content was inserted in front
2518 * of the dptr bucket. The inserted buckets should not be thrown
2519 * away here, but they should also not be parsed later.
2521 if (content_head == NULL) {
2522 content_head = dptr;
2524 tmp_dptr = ctx->head_start_bucket;
2525 if (!APR_BRIGADE_EMPTY(ctx->ssi_tag_brigade)) {
2526 apr_brigade_cleanup(ctx->ssi_tag_brigade);
2531 tmp_dptr = APR_BUCKET_NEXT (tmp_dptr);
2532 apr_bucket_delete(tmp_bkt);
2533 } while ((tmp_dptr != content_head) &&
2534 (tmp_dptr != APR_BRIGADE_SENTINEL(*bb)));
2536 if (ctx->combined_tag == tmp_buf) {
2537 memset (ctx->combined_tag, '\0', ctx->tag_length);
2538 ctx->combined_tag = NULL;
2541 /* Don't reset the flags or the nesting level!!! */
2543 ctx->head_start_bucket = NULL;
2544 ctx->head_start_index = 0;
2545 ctx->tag_start_bucket = NULL;
2546 ctx->tag_start_index = 0;
2547 ctx->tail_start_bucket = NULL;
2548 ctx->tail_start_index = 0;
2549 ctx->curr_tag_pos = NULL;
2550 ctx->tag_length = 0;
2551 ctx->directive_length = 0;
2553 if (!APR_BRIGADE_EMPTY(ctx->ssi_tag_brigade)) {
2554 apr_brigade_cleanup(ctx->ssi_tag_brigade);
2557 ctx->state = PRE_HEAD;
2561 /* If I am in the middle of parsing an SSI tag then I need to set aside
2562 * the pertinent trailing buckets and pass on the initial part of the
2563 * brigade. The pertinent parts of the next brigades will be added to
2564 * these set aside buckets to form the whole tag and will be processed
2565 * once the whole tag has been found.
2567 if (ctx->state == PRE_HEAD) {
2568 /* Inside a false conditional (if, elif, else), so toss it all... */
2569 if ((dptr != APR_BRIGADE_SENTINEL(*bb)) &&
2570 (!(ctx->flags & FLAG_PRINTING))) {
2571 apr_bucket *free_bucket;
2574 dptr = APR_BUCKET_NEXT (dptr);
2575 apr_bucket_delete(free_bucket);
2576 } while (dptr != APR_BRIGADE_SENTINEL(*bb));
2578 else { /* Otherwise pass it along... */
2579 rv = ap_pass_brigade(f->next, *bb); /* No SSI tags in this brigade... */
2580 if (rv != APR_SUCCESS) {
2583 ctx->bytes_parsed = 0;
2586 else if (ctx->state == PARSED) { /* Invalid internal condition... */
2587 apr_bucket *content_head = NULL, *tmp_bkt;
2588 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2589 "Invalid mod_include state during file %s", r->filename);
2590 CREATE_ERROR_BUCKET(ctx, tmp_bkt, APR_BRIGADE_FIRST(*bb), content_head);
2592 else { /* Entire brigade is middle chunk of SSI tag... */
2593 if (!APR_BRIGADE_EMPTY(ctx->ssi_tag_brigade)) {
2594 APR_BRIGADE_CONCAT(ctx->ssi_tag_brigade, *bb);
2596 else { /* End of brigade contains part of SSI tag... */
2597 if (ctx->head_start_index > 0) {
2598 apr_bucket_split(ctx->head_start_bucket, ctx->head_start_index);
2599 ctx->head_start_bucket = APR_BUCKET_NEXT(ctx->head_start_bucket);
2600 ctx->head_start_index = 0;
2602 /* Set aside tag, pass pre-tag... */
2603 tag_and_after = apr_brigade_split(*bb, ctx->head_start_bucket);
2604 ap_save_brigade(f, &ctx->ssi_tag_brigade, &tag_and_after, r->pool);
2605 rv = ap_pass_brigade(f->next, *bb);
2606 if (rv != APR_SUCCESS) {
2609 ctx->bytes_parsed = 0;
2615 /*****************************************************************
2617 * XBITHACK. Sigh... NB it's configurable per-directory; the compile-time
2618 * option only changes the default.
2621 module include_module;
2623 xbithack_off, xbithack_on, xbithack_full
2627 char *default_error_msg;
2628 char *default_time_fmt;
2629 enum xbithack *xbithack;
2630 } include_dir_config;
2633 #define DEFAULT_XBITHACK xbithack_full
2635 #define DEFAULT_XBITHACK xbithack_off
2638 static void *create_includes_dir_config(apr_pool_t *p, char *dummy)
2640 include_dir_config *result =
2641 (include_dir_config *)apr_palloc(p, sizeof(include_dir_config));
2642 enum xbithack *xbh = (enum xbithack *) apr_palloc(p, sizeof(enum xbithack));
2643 *xbh = DEFAULT_XBITHACK;
2644 result->default_error_msg = DEFAULT_ERROR_MSG;
2645 result->default_time_fmt = DEFAULT_TIME_FORMAT;
2646 result->xbithack = xbh;
2650 static const char *set_xbithack(cmd_parms *cmd, void *xbp, const char *arg)
2652 include_dir_config *conf = (include_dir_config *)xbp;
2654 if (!strcasecmp(arg, "off")) {
2655 *conf->xbithack = xbithack_off;
2657 else if (!strcasecmp(arg, "on")) {
2658 *conf->xbithack = xbithack_on;
2660 else if (!strcasecmp(arg, "full")) {
2661 *conf->xbithack = xbithack_full;
2664 return "XBitHack must be set to Off, On, or Full";
2670 static apr_status_t includes_filter(ap_filter_t *f, apr_bucket_brigade *b)
2672 request_rec *r = f->r;
2673 include_ctx_t *ctx = f->ctx;
2674 request_rec *parent;
2676 include_dir_config *conf =
2677 (include_dir_config *)ap_get_module_config(r->per_dir_config,
2680 if (!(ap_allow_options(r) & OPT_INCLUDES)) {
2681 return ap_pass_brigade(f->next, b);
2683 r->allowed |= (1 << M_GET);
2684 if (r->method_number != M_GET) {
2685 return ap_pass_brigade(f->next, b);
2689 f->ctx = ctx = apr_pcalloc(f->c->pool, sizeof(*ctx));
2691 ctx->state = PRE_HEAD;
2692 ctx->flags = (FLAG_PRINTING | FLAG_COND_TRUE);
2693 if (ap_allow_options(r) & OPT_INCNOEXEC) {
2694 ctx->flags |= FLAG_NO_EXEC;
2696 ctx->ssi_tag_brigade = apr_brigade_create(f->c->pool);
2698 apr_cpystrn(ctx->error_str, conf->default_error_msg, sizeof(ctx->error_str));
2699 apr_cpystrn(ctx->time_str, conf->default_time_fmt, sizeof(ctx->time_str));
2700 ctx->error_length = strlen(ctx->error_str);
2703 return ap_pass_brigade(f->next, b);
2707 ctx->bytes_parsed = 0;
2710 /* Assure the platform supports Group protections */
2711 if ((*conf->xbithack == xbithack_full)
2712 && (r->finfo.valid & APR_FINFO_GPROT)
2713 && (r->finfo.protection & APR_GEXECUTE)) {
2714 ap_update_mtime(r, r->finfo.mtime);
2715 ap_set_last_modified(r);
2718 if ((parent = ap_get_module_config(r->request_config, &include_module))) {
2719 /* Kludge --- for nested includes, we want to keep the subprocess
2720 * environment of the base document (for compatibility); that means
2721 * torquing our own last_modified date as well so that the
2722 * LAST_MODIFIED variable gets reset to the proper value if the
2723 * nested document resets <!--#config timefmt-->.
2724 * We also insist that the memory for this subrequest not be
2725 * destroyed, that's dealt with in handle_include().
2727 r->subprocess_env = r->main->subprocess_env;
2728 apr_pool_join(r->main->pool, r->pool);
2729 r->finfo.mtime = r->main->finfo.mtime;
2732 /* we're not a nested include, so we create an initial
2734 ap_add_common_vars(r);
2736 add_include_vars(r, conf->default_time_fmt);
2738 /* XXX: this is bogus, at some point we're going to do a subrequest,
2739 * and when we do it we're going to be subjecting code that doesn't
2740 * expect to be signal-ready to SIGALRM. There is no clean way to
2741 * fix this, except to put alarm support into BUFF. -djg
2745 /* Always unset the content-length. There is no way to know if
2746 * the content will be modified at some point by send_parsed_content.
2747 * It is very possible for us to not find any content in the first
2748 * 9k of the file, but still have to modify the content of the file.
2749 * If we are going to pass the file through send_parsed_content, then
2750 * the content-length should just be unset.
2752 apr_table_unset(f->r->headers_out, "Content-Length");
2754 rv = send_parsed_content(&b, r, f);
2757 /* signify that the sub request should not be killed */
2758 ap_set_module_config(r->request_config, &include_module,
2759 NESTED_INCLUDE_MAGIC);
2765 static void ap_register_include_handler(char *tag, include_handler_fn_t *func)
2767 apr_hash_set(include_hash, tag, strlen(tag) + 1, (const void *)func);
2770 static void include_post_config(apr_pool_t *p, apr_pool_t *plog,
2771 apr_pool_t *ptemp, server_rec *s)
2773 include_hash = apr_hash_make(p);
2775 ssi_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_include_handler);
2777 if(ssi_pfn_register) {
2778 ssi_pfn_register("if", handle_if);
2779 ssi_pfn_register("set", handle_set);
2780 ssi_pfn_register("else", handle_else);
2781 ssi_pfn_register("elif", handle_elif);
2782 ssi_pfn_register("echo", handle_echo);
2783 ssi_pfn_register("endif", handle_endif);
2784 ssi_pfn_register("fsize", handle_fsize);
2785 ssi_pfn_register("config", handle_config);
2786 ssi_pfn_register("include", handle_include);
2787 ssi_pfn_register("flastmod", handle_flastmod);
2788 ssi_pfn_register("printenv", handle_printenv);
2792 static const char *set_default_error_msg(cmd_parms *cmd, void *mconfig, const char *msg)
2794 include_dir_config *conf = (include_dir_config *)mconfig;
2795 conf->default_error_msg = apr_pstrdup(cmd->pool, msg);
2799 static const char *set_default_time_fmt(cmd_parms *cmd, void *mconfig, const char *fmt)
2801 include_dir_config *conf = (include_dir_config *)mconfig;
2802 conf->default_time_fmt = apr_pstrdup(cmd->pool, fmt);
2807 * Module definition and configuration data structs...
2809 static const command_rec includes_cmds[] =
2811 AP_INIT_TAKE1("XBitHack", set_xbithack, NULL, OR_OPTIONS,
2812 "Off, On, or Full"),
2813 AP_INIT_TAKE1("SSIErrorMsg", set_default_error_msg, NULL, OR_ALL,
2815 AP_INIT_TAKE1("SSITimeFormat", set_default_time_fmt, NULL, OR_ALL,
2816 "a strftime(3) formatted string"),
2820 static int xbithack_handler(request_rec *r)
2822 #if defined(OS2) || defined(WIN32) || defined(NETWARE)
2823 /* OS/2 dosen't currently support the xbithack. This is being worked on. */
2826 enum xbithack *state;
2828 if (ap_strcmp_match(r->handler, "text/html")) {
2831 if (!(r->finfo.protection & APR_UEXECUTE)) {
2835 state = (enum xbithack *) ap_get_module_config(r->per_dir_config,
2838 if (*state == xbithack_off) {
2841 /* We always return declined, because the default handler will actually
2842 * serve the file. All we have to do is add the filter.
2844 ap_add_output_filter("INCLUDES", NULL, r, r->connection);
2849 static void register_hooks(apr_pool_t *p)
2851 APR_REGISTER_OPTIONAL_FN(ap_ssi_get_tag_and_value);
2852 APR_REGISTER_OPTIONAL_FN(ap_ssi_parse_string);
2853 APR_REGISTER_OPTIONAL_FN(ap_register_include_handler);
2854 ap_hook_post_config(include_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
2855 ap_hook_handler(xbithack_handler, NULL, NULL, APR_HOOK_MIDDLE);
2856 ap_register_output_filter("INCLUDES", includes_filter, AP_FTYPE_CONTENT);
2859 module AP_MODULE_DECLARE_DATA include_module =
2861 STANDARD20_MODULE_STUFF,
2862 create_includes_dir_config, /* dir config creater */
2863 NULL, /* dir merger --- default is to override */
2864 NULL, /* server config */
2865 NULL, /* merge server config */
2866 includes_cmds, /* command apr_table_t */
2867 register_hooks /* register hooks */