1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 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 * util.c: string utility things
63 * 1995-96 Many changes by the Apache Software Foundation
68 * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
69 * #define DEBUG_CFG_LINES to trace every line read from the config files
73 #include "apr_strings.h"
76 #define APR_WANT_STDIO
77 #define APR_WANT_STRFUNC
84 #include <netdb.h> /* for gethostbyname() */
89 #include "ap_config.h"
90 #include "apr_base64.h"
92 #include "http_main.h"
94 #include "http_protocol.h"
95 #include "http_config.h"
96 #include "util_ebcdic.h"
105 /* A bunch of functions in util.c scan strings looking for certain characters.
106 * To make that more efficient we encode a lookup table. The test_char_table
107 * is generated automatically by gen_test_char.c.
109 #include "test_char.h"
111 /* we assume the folks using this ensure 0 <= c < 256... which means
112 * you need a cast to (unsigned char) first, you can't just plug a
113 * char in here and get it to work, because if char is signed then it
114 * will first be sign extended.
116 #define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
118 /* Win32/NetWare/OS2 need to check for both forward and back slashes
119 * in ap_getparents() and ap_escape_url.
121 #ifdef CASE_BLIND_FILESYSTEM
122 #define IS_SLASH(s) ((s == '/') || (s == '\\'))
124 #define IS_SLASH(s) (s == '/')
129 * Examine a field value (such as a media-/content-type) string and return
130 * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
132 AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
136 if (intype == NULL) return NULL;
138 semi = ap_strchr_c(intype, ';');
140 return apr_pstrdup(p, intype);
143 while ((semi > intype) && apr_isspace(semi[-1])) {
146 return apr_pstrndup(p, intype, semi - intype);
150 AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
154 char ts[MAX_STRING_LEN];
155 char tf[MAX_STRING_LEN];
162 apr_time_exp_gmt(&xt, t);
163 /* Convert %Z to "GMT" and %z to "+0000";
164 * on hosts that do not have a time zone string in struct tm,
165 * strftime must assume its argument is local time.
167 for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
169 if (*f != '%') continue;
180 case 'z': /* common extension */
194 apr_time_exp_lt(&xt, t);
197 /* check return code? */
198 apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
199 ts[MAX_STRING_LEN - 1] = '\0';
200 return apr_pstrdup(p, ts);
203 /* Roy owes Rob beer. */
204 /* Rob owes Roy dinner. */
206 /* These legacy comments would make a lot more sense if Roy hadn't
207 * replaced the old later_than() routine with util_date.c.
209 * Well, okay, they still wouldn't make any sense.
212 /* Match = 0, NoMatch = 1, Abort = -1
213 * Based loosely on sections of wildmat.c by Rich Salz
214 * Hmmm... shouldn't this really go component by component?
216 AP_DECLARE(int) ap_strcmp_match(const char *str, const char *exp)
220 for (x = 0, y = 0; exp[y]; ++y, ++x) {
221 if ((!str[x]) && (exp[y] != '*'))
224 while (exp[++y] == '*');
229 if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
234 else if ((exp[y] != '?') && (str[x] != exp[y]))
237 return (str[x] != '\0');
240 AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *exp)
244 for (x = 0, y = 0; exp[y]; ++y, ++x) {
245 if (!str[x] && exp[y] != '*')
248 while (exp[++y] == '*');
253 if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
258 else if (exp[y] != '?'
259 && apr_tolower(str[x]) != apr_tolower(exp[y]))
262 return (str[x] != '\0');
265 /* We actually compare the canonical root to this root, (but we don't
266 * waste time checking the case), since every use of this function in
267 * httpd-2.0 tests if the path is 'proper', meaning we've already passed
268 * it through apr_filepath_merge, or we haven't.
270 AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
273 const char *ourdir = dir;
274 if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
275 || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
281 AP_DECLARE(int) ap_is_matchexp(const char *str)
285 for (x = 0; str[x]; x++)
286 if ((str[x] == '*') || (str[x] == '?'))
292 * Here's a pool-based interface to POSIX regex's regcomp().
293 * Note that we return regex_t instead of being passed one.
294 * The reason is that if you use an already-used regex_t structure,
295 * the memory that you've already allocated gets forgotten, and
296 * regfree() doesn't clear it. So we don't allow it.
299 static apr_status_t regex_cleanup(void *preg)
301 regfree((regex_t *) preg);
305 AP_DECLARE(regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
308 regex_t *preg = apr_palloc(p, sizeof(regex_t));
310 if (regcomp(preg, pattern, cflags)) {
314 apr_pool_cleanup_register(p, (void *) preg, regex_cleanup, regex_cleanup);
319 AP_DECLARE(void) ap_pregfree(apr_pool_t *p, regex_t * reg)
322 apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
326 * Similar to standard strstr() but we ignore case in this version.
327 * Based on the strstr() implementation further below.
329 AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
337 for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
341 /* found first character of s2, see if the rest matches */
344 for (++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
346 /* both strings ended together */
351 /* second string ended, a match */
354 /* didn't find a match here, try starting at next character in s1 */
361 * Returns an offsetted pointer in bigstring immediately after
362 * prefix. Returns bigstring if bigstring doesn't start with
363 * prefix or if prefix is longer than bigstring while still matching.
364 * NOTE: pointer returned is relative to bigstring, so we
365 * can use standard pointer comparisons in the calling function
366 * (eg: test if ap_stripprefix(a,b) == a)
368 AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
377 while (*p1 && *prefix) {
378 if (*p1++ != *prefix++)
384 /* hit the end of bigstring! */
389 * Apache stub function for the regex libraries regexec() to make sure the
390 * whole regex(3) API is available through the Apache (exported) namespace.
391 * This is especially important for the DSO situations of modules.
392 * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
394 AP_DECLARE(int) ap_regexec(regex_t *preg, const char *string,
395 size_t nmatch, regmatch_t pmatch[], int eflags)
397 return regexec(preg, string, nmatch, pmatch, eflags);
400 AP_DECLARE(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf,
403 return regerror(errcode, preg, errbuf, errbuf_size);
407 /* This function substitutes for $0-$9, filling in regular expression
408 * submatches. Pass it the same nmatch and pmatch arguments that you
409 * passed ap_regexec(). pmatch should not be greater than the maximum number
410 * of subexpressions - i.e. one more than the re_nsub member of regex_t.
412 * input should be the string with the $-expressions, source should be the
413 * string that was matched against.
415 * It returns the substituted string, or NULL on error.
417 * Parts of this code are based on Henry Spencer's regsub(), from his
418 * AT&T V8 regexp package.
421 AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input,
422 const char *source, size_t nmatch,
425 const char *src = input;
434 return apr_pstrdup(p, src);
436 /* First pass, find the size */
440 while ((c = *src++) != '\0') {
443 else if (c == '$' && apr_isdigit(*src))
448 if (no > 9) { /* Ordinary character. */
449 if (c == '\\' && (*src == '$' || *src == '&'))
453 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
454 len += pmatch[no].rm_eo - pmatch[no].rm_so;
459 dest = dst = apr_pcalloc(p, len + 1);
461 /* Now actually fill in the string */
465 while ((c = *src++) != '\0') {
468 else if (c == '$' && apr_isdigit(*src))
473 if (no > 9) { /* Ordinary character. */
474 if (c == '\\' && (*src == '$' || *src == '&'))
478 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
479 len = pmatch[no].rm_eo - pmatch[no].rm_so;
480 memcpy(dst, source + pmatch[no].rm_so, len);
491 * Parse .. so we don't compromise security
493 AP_DECLARE(void) ap_getparents(char *name)
498 /* Four paseses, as per RFC 1808 */
499 /* a) remove ./ path segments */
500 for (next = name; *next && (*next != '.'); next++) {
503 l = w = first_dot = next - name;
504 while (name[l] != '\0') {
505 if (name[l] == '.' && IS_SLASH(name[l + 1])
506 && (l == 0 || IS_SLASH(name[l - 1])))
509 name[w++] = name[l++];
512 /* b) remove trailing . path, segment */
513 if (w == 1 && name[0] == '.')
515 else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
519 /* c) remove all xx/../ segments. (including leading ../ and /../) */
522 while (name[l] != '\0') {
523 if (name[l] == '.' && name[l + 1] == '.' && IS_SLASH(name[l + 2])
524 && (l == 0 || IS_SLASH(name[l - 1]))) {
525 register int m = l + 3, n;
529 while (l >= 0 && !IS_SLASH(name[l]))
536 while ((name[n] = name[m]))
543 /* d) remove trailing xx/.. segment. */
544 if (l == 2 && name[0] == '.' && name[1] == '.')
546 else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
547 && IS_SLASH(name[l - 3])) {
550 while (l >= 0 && !IS_SLASH(name[l]))
560 AP_DECLARE(void) ap_no2slash(char *name)
566 #ifdef HAVE_UNC_PATHS
567 /* Check for UNC names. Leave leading two slashes. */
568 if (s[0] == '/' && s[1] == '/')
573 if ((*d++ = *s) == '/') {
587 * copy at most n leading directories of s into d
588 * d should be at least as large as s plus 1 extra byte
590 * the return value is the ever useful pointer to the trailing \0 of d
592 * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
593 * so that if n == 0, "/" is returned in d with n == 1
594 * and s == "e:/test.html", "e:/" is returned in d
595 * *** See also directory_walk in modules/http/http_request.c
598 * /a/b, 0 ==> / (true for all platforms)
607 * c:/a/b 3 ==> c:/a/b
608 * c:/a/b 4 ==> c:/a/b
610 AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
619 if (*s == '\0' || (*s == '/' && (--n) == 0)) {
631 * return the parent directory name including trailing / of the file s
633 AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
635 const char *last_slash = ap_strrchr_c(s, '/');
639 if (last_slash == NULL) {
640 return apr_pstrdup(p, "");
642 l = (last_slash - s) + 1;
643 d = apr_palloc(p, l + 1);
650 AP_DECLARE(int) ap_count_dirs(const char *path)
654 for (x = 0, n = 0; path[x]; x++)
660 AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
662 return ap_getword(atrans, (const char **) line, stop);
665 AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
667 const char *pos = *line;
671 while ((*pos != stop) && *pos) {
676 res = (char *)apr_palloc(atrans, len + 1);
677 memcpy(res, *line, len);
681 while (*pos == stop) {
690 AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
692 return ap_getword_white(atrans, (const char **) line);
695 AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
697 const char *pos = *line;
701 while (!apr_isspace(*pos) && *pos) {
706 res = (char *)apr_palloc(atrans, len + 1);
707 memcpy(res, *line, len);
710 while (apr_isspace(*pos)) {
719 AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line,
722 return ap_getword_nulls(atrans, (const char **) line, stop);
725 AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line,
728 const char *pos = ap_strchr_c(*line, stop);
732 res = apr_pstrdup(atrans, *line);
733 *line += strlen(*line);
737 res = apr_pstrndup(atrans, *line, pos - *line);
746 /* Get a word, (new) config-file style --- quoted strings and backslashes
750 static char *substring_conf(apr_pool_t *p, const char *start, int len,
753 char *result = apr_palloc(p, len + 2);
757 for (i = 0; i < len; ++i) {
758 if (start[i] == '\\' && (start[i + 1] == '\\'
759 || (quote && start[i + 1] == quote)))
760 *resp++ = start[++i];
766 #if RESOLVE_ENV_PER_TOKEN
767 return ap_resolve_env(p,result);
773 AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
775 return ap_getword_conf(p, (const char **) line);
778 AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
780 const char *str = *line, *strend;
784 while (*str && apr_isspace(*str))
792 if ((quote = *str) == '"' || quote == '\'') {
794 while (*strend && *strend != quote) {
795 if (*strend == '\\' && strend[1] && strend[1] == quote)
800 res = substring_conf(p, str + 1, strend - str - 1, quote);
802 if (*strend == quote)
807 while (*strend && !apr_isspace(*strend))
810 res = substring_conf(p, str, strend - str, 0);
813 while (*strend && apr_isspace(*strend))
819 /* Check a string for any ${ENV} environment variable
820 * construct and replace each them by the value of
821 * that environment variable, if it exists. If the
822 * environment value does not exist, leave the ${ENV}
823 * construct alone; it means something else.
825 AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
827 char tmp[ MAX_STRING_LEN ];
831 if (!(s=ap_strchr_c(word,'$')))
835 /* XXX - relies on strncat() to add '\0'
837 strncat(tmp,word,s - word);
838 if ((s[1] == '{') && (e=ap_strchr_c(s,'}'))) {
845 strncat(tmp, s, e2-s);
849 /* ignore invalid strings */
853 } while ((s=ap_strchr_c(word,'$')));
856 return apr_pstrdup(p,tmp);
858 AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
861 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
862 "Done with config file %s", cfp->name);
864 return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
867 static apr_status_t cfg_close(void *param)
869 apr_file_t *cfp = (apr_file_t *) param;
870 return (apr_file_close(cfp));
873 static int cfg_getch(void *param)
876 apr_file_t *cfp = (apr_file_t *) param;
877 if (apr_file_getc(&ch, cfp) == APR_SUCCESS)
882 static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
884 apr_file_t *cfp = (apr_file_t *) param;
886 rv = apr_file_gets(buf, bufsiz, cfp);
887 if (rv == APR_SUCCESS || (rv == APR_EOF && strcmp(buf, "")))
892 /* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
893 AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg,
894 apr_pool_t *p, const char *name)
896 ap_configfile_t *new_cfg;
897 apr_file_t *file = NULL;
905 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
906 "Internal error: pcfg_openfile() called with NULL filename");
910 status = apr_file_open(&file, name, APR_READ | APR_BUFFERED,
913 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
914 "Opening config file %s (%s)",
915 name, (status != APR_SUCCESS) ?
916 apr_strerror(status, buf, sizeof(buf)) : "successful");
918 if (status != APR_SUCCESS)
921 status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
922 if (status != APR_SUCCESS)
925 if (finfo.filetype != APR_REG &&
926 #if defined(WIN32) || defined(OS2) || defined(NETWARE)
927 strcasecmp(apr_filename_of_pathname(name), "nul") != 0) {
929 strcmp(name, "/dev/null") != 0) {
930 #endif /* WIN32 || OS2 */
931 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
932 "Access to file %s denied by server: not a regular file",
934 apr_file_close(file);
939 /* Some twisted character [no pun intended] at MS decided that a
940 * zero width joiner as the lead wide character would be ideal for
941 * describing Unicode text files. This was further convoluted to
942 * another MSism that the same character mapped into utf-8, EF BB BF
943 * would signify utf-8 text files.
945 * Since MS configuration files are all protecting utf-8 encoded
946 * Unicode path, file and resource names, we already have the correct
947 * WinNT encoding. But at least eat the stupid three bytes up front.
950 unsigned char buf[4];
952 status = apr_file_read(file, buf, &len);
953 if ((status != APR_SUCCESS) || (len < 3)
954 || memcmp(buf, "\xEF\xBB\xBF", 3) != 0) {
956 apr_file_seek(file, APR_SET, &zero);
961 new_cfg = apr_palloc(p, sizeof(*new_cfg));
962 new_cfg->param = file;
963 new_cfg->name = apr_pstrdup(p, name);
964 new_cfg->getch = (int (*)(void *)) cfg_getch;
965 new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
966 new_cfg->close = (int (*)(void *)) cfg_close;
967 new_cfg->line_number = 0;
973 /* Allocate a ap_configfile_t handle with user defined functions and params */
974 AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(apr_pool_t *p,
977 int(*getch)(void *param),
978 void *(*getstr) (void *buf, size_t bufsiz, void *param),
979 int(*close_func)(void *param))
981 ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
983 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
984 "Opening config handler %s", descr);
986 new_cfg->param = param;
987 new_cfg->name = descr;
988 new_cfg->getch = getch;
989 new_cfg->getstr = getstr;
990 new_cfg->close = close_func;
991 new_cfg->line_number = 0;
995 /* Read one character from a configfile_t */
996 AP_DECLARE(int) ap_cfg_getc(ap_configfile_t *cfp)
998 register int ch = cfp->getch(cfp->param);
1004 /* Read one line from open ap_configfile_t, strip LF, increase line number */
1005 /* If custom handler does not define a getstr() function, read char by char */
1006 AP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, ap_configfile_t *cfp)
1008 /* If a "get string" function is defined, use it */
1009 if (cfp->getstr != NULL) {
1013 size_t cbufsize = bufsize;
1017 if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
1021 * check for line continuation,
1022 * i.e. match [^\\]\\[\r]\n only
1025 while (cp < cbuf+cbufsize && *cp != '\0')
1027 if (cp > cbuf && cp[-1] == LF) {
1029 if (cp > cbuf && cp[-1] == CR)
1031 if (cp > cbuf && cp[-1] == '\\') {
1033 if (!(cp > cbuf && cp[-1] == '\\')) {
1035 * line continuation requested -
1036 * then remove backslash and continue
1038 cbufsize -= (cp-cbuf);
1044 * no real continuation because escaped -
1045 * then just remove escape character
1047 for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
1056 * Leading and trailing white space is eliminated completely
1059 while (apr_isspace(*src))
1061 /* blast trailing whitespace */
1062 dst = &src[strlen(src)];
1063 while (--dst >= src && apr_isspace(*dst))
1065 /* Zap leading whitespace by shifting */
1067 for (dst = buf; (*dst++ = *src++) != '\0'; )
1070 #ifdef DEBUG_CFG_LINES
1071 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, "Read config: %s", buf);
1075 /* No "get string" function defined; read character by character */
1077 register size_t i = 0;
1080 /* skip leading whitespace */
1082 c = cfp->getch(cfp->param);
1083 } while (c == '\t' || c == ' ');
1089 /* too small, assume caller is crazy */
1094 if ((c == '\t') || (c == ' ')) {
1096 while ((c == '\t') || (c == ' '))
1097 c = cfp->getch(cfp->param);
1100 /* silently ignore CR (_assume_ that a LF follows) */
1101 c = cfp->getch(cfp->param);
1104 /* increase line number and return on LF */
1107 if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
1109 * check for line continuation
1111 if (i > 0 && buf[i-1] == '\\') {
1113 if (!(i > 0 && buf[i-1] == '\\')) {
1114 /* line is continued */
1115 c = cfp->getch(cfp->param);
1118 /* else nothing needs be done because
1119 * then the backslash is escaped and
1120 * we just strip to a single one
1123 /* blast trailing whitespace */
1124 while (i > 0 && apr_isspace(buf[i - 1]))
1127 #ifdef DEBUG_CFG_LINES
1128 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
1129 "Read config: %s", buf);
1135 c = cfp->getch(cfp->param);
1140 /* Size an HTTP header field list item, as separated by a comma.
1141 * The return value is a pointer to the beginning of the non-empty list item
1142 * within the original string (or NULL if there is none) and the address
1143 * of field is shifted to the next non-comma, non-whitespace character.
1144 * len is the length of the item excluding any beginning whitespace.
1146 AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1148 const unsigned char *ptr = (const unsigned char *)*field;
1149 const unsigned char *token;
1150 int in_qpair, in_qstr, in_com;
1152 /* Find first non-comma, non-whitespace byte */
1154 while (*ptr == ',' || apr_isspace(*ptr))
1159 /* Find the end of this item, skipping over dead bits */
1161 for (in_qpair = in_qstr = in_com = 0;
1162 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1170 case '\\': in_qpair = 1; /* quoted-pair */
1172 case '"' : if (!in_com) /* quoted string delim */
1175 case '(' : if (!in_qstr) /* comment (may nest) */
1178 case ')' : if (in_com) /* end comment */
1186 if ((*len = (ptr - token)) == 0) {
1187 *field = (const char *)ptr;
1191 /* Advance field pointer to the next non-comma, non-white byte */
1193 while (*ptr == ',' || apr_isspace(*ptr))
1196 *field = (const char *)ptr;
1197 return (const char *)token;
1200 /* Retrieve an HTTP header field list item, as separated by a comma,
1201 * while stripping insignificant whitespace and lowercasing anything not in
1202 * a quoted string or comment. The return value is a new string containing
1203 * the converted list item (or NULL if none) and the address pointed to by
1204 * field is shifted to the next non-comma, non-whitespace.
1206 AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1208 const char *tok_start;
1209 const unsigned char *ptr;
1212 int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1214 /* Find the beginning and maximum length of the list item so that
1215 * we can allocate a buffer for the new string and reset the field.
1217 if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1220 token = apr_palloc(p, tok_len + 1);
1222 /* Scan the token again, but this time copy only the good bytes.
1223 * We skip extra whitespace and any whitespace around a '=', '/',
1224 * or ';' and lowercase normal characters not within a comment,
1225 * quoted-string or quoted-pair.
1227 for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1228 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1237 case '\\': in_qpair = 1;
1243 case '"' : if (!in_com)
1250 case '(' : if (!in_qstr)
1257 case ')' : if (in_com)
1263 case '\t': if (addspace)
1265 if (in_com || in_qstr)
1272 case ';' : if (!(in_com || in_qstr))
1276 default : if (addspace == 1)
1278 *pos++ = (in_com || in_qstr) ? *ptr
1279 : apr_tolower(*ptr);
1290 /* Find an item in canonical form (lowercase, no extra spaces) within
1291 * an HTTP field value list. Returns 1 if found, 0 if not found.
1292 * This would be much more efficient if we stored header fields as
1293 * an array of list items as they are received instead of a plain string.
1295 AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line,
1298 const unsigned char *pos;
1299 const unsigned char *ptr = (const unsigned char *)line;
1300 int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1305 do { /* loop for each item in line's list */
1307 /* Find first non-comma, non-whitespace byte */
1309 while (*ptr == ',' || apr_isspace(*ptr))
1313 good = 1; /* until proven otherwise for this item */
1315 break; /* no items left and nothing good found */
1317 /* We skip extra whitespace and any whitespace around a '=', '/',
1318 * or ';' and lowercase normal characters not within a comment,
1319 * quoted-string or quoted-pair.
1321 for (pos = (const unsigned char *)tok;
1322 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1328 good = (*pos++ == *ptr);
1332 case '\\': in_qpair = 1;
1334 good = good && (*pos++ == ' ');
1335 good = good && (*pos++ == *ptr);
1338 case '"' : if (!in_com)
1341 good = good && (*pos++ == ' ');
1342 good = good && (*pos++ == *ptr);
1345 case '(' : if (!in_qstr)
1348 good = good && (*pos++ == ' ');
1349 good = good && (*pos++ == *ptr);
1352 case ')' : if (in_com)
1354 good = good && (*pos++ == *ptr);
1358 case '\t': if (addspace || !good)
1360 if (in_com || in_qstr)
1361 good = (*pos++ == *ptr);
1367 case ';' : if (!(in_com || in_qstr))
1369 good = good && (*pos++ == *ptr);
1371 default : if (!good)
1374 good = (*pos++ == ' ');
1375 if (in_com || in_qstr)
1376 good = good && (*pos++ == *ptr);
1378 good = good && (*pos++ == apr_tolower(*ptr));
1385 good = 0; /* not good if only a prefix was matched */
1387 } while (*ptr && !good);
1393 /* Retrieve a token, spacing over it and returning a pointer to
1394 * the first non-white byte afterwards. Note that these tokens
1395 * are delimited by semis and commas; and can also be delimited
1396 * by whitespace at the caller's option.
1399 AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line,
1402 const char *ptr = *accept_line;
1403 const char *tok_start;
1407 /* Find first non-white byte */
1409 while (*ptr && apr_isspace(*ptr))
1414 /* find token end, skipping over quoted strings.
1415 * (comments are already gone).
1418 while (*ptr && (accept_white || !apr_isspace(*ptr))
1419 && *ptr != ';' && *ptr != ',') {
1426 tok_len = ptr - tok_start;
1427 token = apr_pstrndup(p, tok_start, tok_len);
1429 /* Advance accept_line pointer to the next non-white byte */
1431 while (*ptr && apr_isspace(*ptr))
1439 /* find http tokens, see the definition of token from RFC2068 */
1440 AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1442 const unsigned char *start_token;
1443 const unsigned char *s;
1448 s = (const unsigned char *)line;
1450 /* find start of token, skip all stop characters, note NUL
1451 * isn't a token stop, so we don't need to test for it
1453 while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1460 /* find end of the token */
1461 while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1464 if (!strncasecmp((const char *)start_token, (const char *)tok,
1475 AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line,
1478 int llen, tlen, lidx;
1483 llen = strlen(line);
1488 (lidx > 0 && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1491 return (strncasecmp(&line[lidx], tok, tlen) == 0);
1494 AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1498 const unsigned char *s;
1500 cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1501 d = (unsigned char *)cmd;
1502 s = (const unsigned char *)str;
1505 #if defined(OS2) || defined(WIN32)
1507 * Newlines to Win32/OS2 CreateProcess() are ill advised.
1508 * Convert them to spaces since they are effectively white
1509 * space to most applications
1511 if (*s == '\r' || *s == '\n') {
1517 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1527 static char x2c(const char *what)
1529 register char digit;
1531 #if !APR_CHARSET_EBCDIC
1532 digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
1535 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
1537 #else /*APR_CHARSET_EBCDIC*/
1544 digit = apr_xlate_conv_byte(ap_hdrs_from_ascii,
1545 0xFF & strtol(xstr, NULL, 16));
1546 #endif /*APR_CHARSET_EBCDIC*/
1552 * Returns 0 on success, non-zero on error
1554 * bad % escape returns HTTP_BAD_REQUEST
1556 * decoding %00 -> \0 (the null character)
1557 * decoding %2f -> / (a special character)
1558 * returns HTTP_NOT_FOUND
1560 AP_DECLARE(int) ap_unescape_url(char *url)
1562 register int badesc, badpath;
1567 /* Initial scan for first '%'. Don't bother writing values before
1569 y = strchr(url, '%');
1573 for (x = y; *y; ++x, ++y) {
1577 if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1584 if (IS_SLASH(*x) || *x == '\0')
1591 return HTTP_BAD_REQUEST;
1593 return HTTP_NOT_FOUND;
1598 AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1599 apr_port_t port, const request_rec *r)
1601 if (ap_is_default_port(port, r)) {
1602 return apr_pstrdup(p, hostname);
1605 return apr_psprintf(p, "%s:%u", hostname, port);
1609 /* c2x takes an unsigned, and expects the caller has guaranteed that
1610 * 0 <= what < 256... which usually means that you have to cast to
1611 * unsigned char first, because (unsigned)(char)(x) first goes through
1612 * signed extension to an int before the unsigned cast.
1614 * The reason for this assumption is to assist gcc code generation --
1615 * the unsigned char -> unsigned extension is already done earlier in
1616 * both uses of this code, so there's no need to waste time doing it
1619 static const char c2x_table[] = "0123456789abcdef";
1621 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char *where)
1623 #if APR_CHARSET_EBCDIC
1624 what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1625 #endif /*APR_CHARSET_EBCDIC*/
1627 *where++ = c2x_table[what >> 4];
1628 *where++ = c2x_table[what & 0xf];
1633 * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1634 * routine is (should be) OS independent.
1636 * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1637 * cases if a ':' occurs before the first '/' in the URL, the URL should be
1638 * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1639 * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1640 * efficiency reasons, we don't use escape_path_segment(), which is provided for
1641 * reference. Again, RFC 1808 is where this stuff is defined.
1643 * If partial is set, os_escape_path() assumes that the path will be appended to
1644 * something with a '/' in it (and thus does not prefix "./").
1647 AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1649 char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
1650 const unsigned char *s = (const unsigned char *)segment;
1651 unsigned char *d = (unsigned char *)copy;
1655 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1667 AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1669 char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1670 const unsigned char *s = (const unsigned char *)path;
1671 unsigned char *d = (unsigned char *)copy;
1675 const char *colon = ap_strchr_c(path, ':');
1676 const char *slash = ap_strchr_c(path, '/');
1678 if (colon && (!slash || colon < slash)) {
1684 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1696 /* ap_escape_uri is now a macro for os_escape_path */
1698 AP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
1703 /* first, count the number of extra characters */
1704 for (i = 0, j = 0; s[i] != '\0'; i++)
1705 if (s[i] == '<' || s[i] == '>')
1707 else if (s[i] == '&')
1711 return apr_pstrmemdup(p, s, i);
1713 x = apr_palloc(p, i + j + 1);
1714 for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1716 memcpy(&x[j], "<", 4);
1719 else if (s[i] == '>') {
1720 memcpy(&x[j], ">", 4);
1723 else if (s[i] == '&') {
1724 memcpy(&x[j], "&", 5);
1734 AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
1738 if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1739 return 0; /* in error condition, just return no */
1741 return (finfo.filetype == APR_DIR);
1744 AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
1748 if (apr_lstat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1749 return 0; /* in error condition, just return no */
1751 return (finfo.filetype == APR_DIR);
1754 AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1757 apr_size_t len1, len2;
1760 len1 = strlen(src1);
1761 len2 = strlen(src2);
1762 /* allocate +3 for '/' delimiter, trailing NULL and overallocate
1763 * one extra byte to allow the caller to add a trailing '/'
1765 path = (char *)apr_palloc(a, len1 + len2 + 3);
1768 memcpy(path + 1, src2, len2 + 1);
1772 memcpy(path, src1, len1);
1774 if (next[-1] != '/') {
1777 memcpy(next, src2, len2 + 1);
1783 * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1785 AP_DECLARE(int) ap_is_url(const char *u)
1789 for (x = 0; u[x] != ':'; x++) {
1791 ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1792 (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1797 return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
1800 AP_DECLARE(int) ap_ind(const char *s, char c)
1802 const char *p = ap_strchr_c(s, c);
1809 AP_DECLARE(int) ap_rind(const char *s, char c)
1811 const char *p = ap_strrchr_c(s, c);
1818 AP_DECLARE(void) ap_str_tolower(char *str)
1821 *str = apr_tolower(*str);
1826 static char *find_fqdn(apr_pool_t *a, struct hostent *p)
1830 if (!strchr(p->h_name, '.')) {
1832 for (x = 0; p->h_aliases[x]; ++x) {
1833 if (strchr(p->h_aliases[x], '.') &&
1834 (!strncasecmp(p->h_aliases[x], p->h_name,
1835 strlen(p->h_name))))
1836 return apr_pstrdup(a, p->h_aliases[x]);
1841 return apr_pstrdup(a, (void *) p->h_name);
1844 char *ap_get_local_host(apr_pool_t *a)
1846 #ifndef MAXHOSTNAMELEN
1847 #define MAXHOSTNAMELEN 256
1849 char str[MAXHOSTNAMELEN + 1];
1850 char *server_hostname = NULL;
1854 if (gethostname(str, sizeof(str) - 1) == 0)
1856 if (gethostname(str, sizeof(str) - 1) != 0)
1859 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
1860 "%s: gethostname() failed to determine ServerName",
1865 str[sizeof(str) - 1] = '\0';
1866 /* TODO: Screaming for APR-ization */
1867 if ((!(p = gethostbyname(str)))
1868 || (!(server_hostname = find_fqdn(a, p)))) {
1869 /* Recovery - return the default servername by IP: */
1870 if (p && p->h_addr_list[0]) {
1871 apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
1872 server_hostname = apr_pstrdup(a, str);
1873 /* We will drop through to report the IP-named server */
1877 /* Since we found a fdqn, return it with no logged message. */
1878 return server_hostname;
1882 if (!server_hostname)
1883 server_hostname = apr_pstrdup(a, "127.0.0.1");
1885 ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, a,
1886 "%s: Could not determine the server's fully qualified "
1887 "domain name, using %s for ServerName",
1888 ap_server_argv0, server_hostname);
1890 return server_hostname;
1893 /* simple 'pool' alloc()ing glue to apr_base64.c
1895 AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
1900 decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
1901 l = apr_base64_decode(decoded, bufcoded);
1902 decoded[l] = '\0'; /* make binary sequence into string */
1907 AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
1910 int l = strlen(string);
1912 encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
1913 l = apr_base64_encode(encoded, string, l);
1914 encoded[l] = '\0'; /* make binary sequence into string */
1919 /* we want to downcase the type/subtype for comparison purposes
1920 * but nothing else because ;parameter=foo values are case sensitive.
1921 * XXX: in truth we want to downcase parameter names... but really,
1922 * apache has never handled parameters and such correctly. You
1923 * also need to compress spaces and such to be able to compare
1926 AP_DECLARE(void) ap_content_type_tolower(char *str)
1930 semi = strchr(str, ';');
1935 *str = apr_tolower(*str);
1944 * Given a string, replace any bare " with \" .
1946 AP_DECLARE(char *) ap_escape_quotes(apr_pool_t *p, const char *instring)
1949 const char *inchr = instring;
1950 char *outchr, *outstring;
1953 * Look through the input string, jogging the length of the output
1954 * string up by an extra byte each time we find an unescaped ".
1956 while (*inchr != '\0') {
1958 if (*inchr == '"') {
1962 * If we find a slosh, and it's not the last byte in the string,
1963 * it's escaping something - advance past both bytes.
1965 if ((*inchr == '\\') && (inchr[1] != '\0')) {
1971 outstring = apr_palloc(p, newlen + 1);
1975 * Now copy the input string to the output string, inserting a slosh
1976 * in front of every " that doesn't already have one.
1978 while (*inchr != '\0') {
1979 if ((*inchr == '\\') && (inchr[1] != '\0')) {
1980 *outchr++ = *inchr++;
1981 *outchr++ = *inchr++;
1983 if (*inchr == '"') {
1986 if (*inchr != '\0') {
1987 *outchr++ = *inchr++;