1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 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
74 #include "ap_config.h"
75 #include "ap_base64.h"
76 #include "apr_strings.h"
78 #include "http_main.h"
80 #include "http_protocol.h"
81 #include "http_config.h"
82 #include "util_ebcdic.h"
90 #ifdef HAVE_NETINET_IN_H
91 #include <netinet/in.h>
93 #ifdef HAVE_SYS_SOCKET_H
94 #include <sys/socket.h>
99 #ifdef HAVE_ARPA_INET_H
100 #include <arpa/inet.h>
108 #ifdef HAVE_STRINGS_H
112 /* A bunch of functions in util.c scan strings looking for certain characters.
113 * To make that more efficient we encode a lookup table. The test_char_table
114 * is generated automatically by gen_test_char.c.
116 #include "test_char.h"
118 /* we assume the folks using this ensure 0 <= c < 256... which means
119 * you need a cast to (unsigned char) first, you can't just plug a
120 * char in here and get it to work, because if char is signed then it
121 * will first be sign extended.
123 #define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
126 * Examine a field value (such as a media-/content-type) string and return
127 * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
129 API_EXPORT(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
133 if (intype == NULL) return NULL;
135 semi = ap_strchr_c(intype, ';');
137 return apr_pstrdup(p, intype);
140 while ((semi > intype) && apr_isspace(semi[-1])) {
143 return apr_pstrndup(p, intype, semi - intype);
147 API_EXPORT(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt, int gmt)
150 char ts[MAX_STRING_LEN];
151 char tf[MAX_STRING_LEN];
152 apr_exploded_time_t xt;
158 apr_explode_gmt(&xt, t);
159 /* Convert %Z to "GMT" and %z to "+0000";
160 * on hosts that do not have a time zone string in struct tm,
161 * strftime must assume its argument is local time.
163 for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
165 if (*f != '%') continue;
176 case 'z': /* common extension */
190 apr_explode_localtime(&xt, t);
193 /* check return code? */
194 apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
195 ts[MAX_STRING_LEN - 1] = '\0';
196 return apr_pstrdup(p, ts);
199 /* Roy owes Rob beer. */
200 /* Rob owes Roy dinner. */
202 /* These legacy comments would make a lot more sense if Roy hadn't
203 * replaced the old later_than() routine with util_date.c.
205 * Well, okay, they still wouldn't make any sense.
208 /* Match = 0, NoMatch = 1, Abort = -1
209 * Based loosely on sections of wildmat.c by Rich Salz
210 * Hmmm... shouldn't this really go component by component?
212 API_EXPORT(int) ap_strcmp_match(const char *str, const char *exp)
216 for (x = 0, y = 0; exp[y]; ++y, ++x) {
217 if ((!str[x]) && (exp[y] != '*'))
220 while (exp[++y] == '*');
225 if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
230 else if ((exp[y] != '?') && (str[x] != exp[y]))
233 return (str[x] != '\0');
236 API_EXPORT(int) ap_strcasecmp_match(const char *str, const char *exp)
240 for (x = 0, y = 0; exp[y]; ++y, ++x) {
241 if ((!str[x]) && (exp[y] != '*'))
244 while (exp[++y] == '*');
249 if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
254 else if ((exp[y] != '?') && (apr_tolower(str[x]) != apr_tolower(exp[y])))
257 return (str[x] != '\0');
260 API_EXPORT(int) ap_is_matchexp(const char *str)
264 for (x = 0; str[x]; x++)
265 if ((str[x] == '*') || (str[x] == '?'))
271 * Here's a pool-based interface to POSIX regex's regcomp().
272 * Note that we return regex_t instead of being passed one.
273 * The reason is that if you use an already-used regex_t structure,
274 * the memory that you've already allocated gets forgotten, and
275 * regfree() doesn't clear it. So we don't allow it.
278 static apr_status_t regex_cleanup(void *preg)
280 regfree((regex_t *) preg);
284 API_EXPORT(regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
287 regex_t *preg = apr_palloc(p, sizeof(regex_t));
289 if (regcomp(preg, pattern, cflags)) {
293 apr_register_cleanup(p, (void *) preg, regex_cleanup, regex_cleanup);
298 API_EXPORT(void) ap_pregfree(apr_pool_t *p, regex_t * reg)
301 apr_kill_cleanup(p, (void *) reg, regex_cleanup);
305 * Similar to standard strstr() but we ignore case in this version.
306 * Based on the strstr() implementation further below.
308 API_EXPORT(char *) ap_strcasestr(const char *s1, const char *s2)
316 for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
317 if (*s1 == '\0') return(NULL);
318 /* found first character of s2, see if the rest matches */
321 while (apr_tolower(*++p1) == apr_tolower(*++p2)) {
323 /* both strings ended together */
328 /* second string ended, a match */
331 /* didn't find a match here, try starting at next character in s1 */
337 * Apache stub function for the regex libraries regexec() to make sure the
338 * whole regex(3) API is available through the Apache (exported) namespace.
339 * This is especially important for the DSO situations of modules.
340 * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
342 API_EXPORT(int) ap_regexec(regex_t *preg, const char *string,
343 size_t nmatch, regmatch_t pmatch[], int eflags)
345 return regexec(preg, string, nmatch, pmatch, eflags);
348 API_EXPORT(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
350 return regerror(errcode, preg, errbuf, errbuf_size);
354 /* This function substitutes for $0-$9, filling in regular expression
355 * submatches. Pass it the same nmatch and pmatch arguments that you
356 * passed ap_regexec(). pmatch should not be greater than the maximum number
357 * of subexpressions - i.e. one more than the re_nsub member of regex_t.
359 * input should be the string with the $-expressions, source should be the
360 * string that was matched against.
362 * It returns the substituted string, or NULL on error.
364 * Parts of this code are based on Henry Spencer's regsub(), from his
365 * AT&T V8 regexp package.
368 API_EXPORT(char *) ap_pregsub(apr_pool_t *p, const char *input, const char *source,
369 size_t nmatch, regmatch_t pmatch[])
371 const char *src = input;
380 return apr_pstrdup(p, src);
382 /* First pass, find the size */
386 while ((c = *src++) != '\0') {
389 else if (c == '$' && apr_isdigit(*src))
394 if (no > 9) { /* Ordinary character. */
395 if (c == '\\' && (*src == '$' || *src == '&'))
399 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
400 len += pmatch[no].rm_eo - pmatch[no].rm_so;
405 dest = dst = apr_pcalloc(p, len + 1);
407 /* Now actually fill in the string */
411 while ((c = *src++) != '\0') {
414 else if (c == '$' && apr_isdigit(*src))
419 if (no > 9) { /* Ordinary character. */
420 if (c == '\\' && (*src == '$' || *src == '&'))
424 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
425 len = pmatch[no].rm_eo - pmatch[no].rm_so;
426 memcpy(dst, source + pmatch[no].rm_so, len);
437 * Parse .. so we don't compromise security
439 API_EXPORT(void) ap_getparents(char *name)
443 /* Four paseses, as per RFC 1808 */
444 /* a) remove ./ path segments */
446 for (l = 0, w = 0; name[l] != '\0';) {
447 if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
450 name[w++] = name[l++];
453 /* b) remove trailing . path, segment */
454 if (w == 1 && name[0] == '.')
456 else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
460 /* c) remove all xx/../ segments. (including leading ../ and /../) */
463 while (name[l] != '\0') {
464 if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
465 (l == 0 || name[l - 1] == '/')) {
466 register int m = l + 3, n;
470 while (l >= 0 && name[l] != '/')
477 while ((name[n] = name[m]))
484 /* d) remove trailing xx/.. segment. */
485 if (l == 2 && name[0] == '.' && name[1] == '.')
487 else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
490 while (l >= 0 && name[l] != '/')
500 API_EXPORT(void) ap_no2slash(char *name)
507 /* Check for UNC names. Leave leading two slashes. */
508 if (s[0] == '/' && s[1] == '/')
513 if ((*d++ = *s) == '/') {
527 * copy at most n leading directories of s into d
528 * d should be at least as large as s plus 1 extra byte
530 * the return value is the ever useful pointer to the trailing \0 of d
538 API_EXPORT(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
541 if (*s == '\0' || (*s == '/' && (--n) == 0)) {
553 * return the parent directory name including trailing / of the file s
555 API_EXPORT(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
557 const char *last_slash = ap_strrchr_c(s, '/');
561 if (last_slash == NULL) {
562 /* XXX: well this is really broken if this happens */
563 return (apr_pstrdup(p, "/"));
565 l = (last_slash - s) + 1;
566 d = apr_palloc(p, l + 1);
573 API_EXPORT(int) ap_count_dirs(const char *path)
577 for (x = 0, n = 0; path[x]; x++)
584 API_EXPORT(void) ap_chdir_file(const char *file)
587 char buf[HUGE_STRING_LEN];
589 x = ap_strrchr_c(file, '/');
593 else if (x - file < sizeof(buf) - 1) {
594 memcpy(buf, file, x - file);
595 buf[x - file] = '\0';
598 /* XXX: well, this is a silly function, no method of reporting an
599 * error... ah well. */
602 API_EXPORT(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
604 return ap_getword(atrans, (const char **) line, stop);
607 API_EXPORT(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
609 const char *pos = ap_strchr_c(*line, stop);
613 res = apr_pstrdup(atrans, *line);
614 *line += strlen(*line);
618 res = apr_pstrndup(atrans, *line, pos - *line);
620 while (*pos == stop) {
629 API_EXPORT(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
631 return ap_getword_white(atrans, (const char **) line);
634 API_EXPORT(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
639 for (x = 0; (*line)[x]; x++) {
640 if (apr_isspace((*line)[x])) {
647 res = apr_pstrdup(atrans, *line);
648 *line += strlen(*line);
652 res = apr_palloc(atrans, pos + 1);
653 apr_cpystrn(res, *line, pos + 1);
655 while (apr_isspace((*line)[pos]))
663 API_EXPORT(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line, char stop)
665 return ap_getword_nulls(atrans, (const char **) line, stop);
668 API_EXPORT(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line, char stop)
670 const char *pos = ap_strchr_c(*line, stop);
674 res = apr_pstrdup(atrans, *line);
675 *line += strlen(*line);
679 res = apr_pstrndup(atrans, *line, pos - *line);
688 /* Get a word, (new) config-file style --- quoted strings and backslashes
692 static char *substring_conf(apr_pool_t *p, const char *start, int len, char quote)
694 char *result = apr_palloc(p, len + 2);
698 for (i = 0; i < len; ++i) {
699 if (start[i] == '\\' && (start[i + 1] == '\\'
700 || (quote && start[i + 1] == quote)))
701 *resp++ = start[++i];
707 #if RESOLVE_ENV_PER_TOKEN
708 return ap_resolve_env(p,result);
714 API_EXPORT(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
716 return ap_getword_conf(p, (const char **) line);
719 API_EXPORT(char *) ap_getword_conf(apr_pool_t *p, const char **line)
721 const char *str = *line, *strend;
725 while (*str && apr_isspace(*str))
733 if ((quote = *str) == '"' || quote == '\'') {
735 while (*strend && *strend != quote) {
736 if (*strend == '\\' && strend[1] && strend[1] == quote)
741 res = substring_conf(p, str + 1, strend - str - 1, quote);
743 if (*strend == quote)
748 while (*strend && !apr_isspace(*strend))
751 res = substring_conf(p, str, strend - str, 0);
754 while (*strend && apr_isspace(*strend))
760 /* Check a string for any ${ENV} environment variable
761 * construct and replace each them by the value of
762 * that environment variable, if it exists. If the
763 * environment value does not exist, leave the ${ENV}
764 * construct alone; it means something else.
766 API_EXPORT(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
768 char tmp[ MAX_STRING_LEN ];
772 if (!(s=ap_strchr_c(word,'$')))
776 /* XXX - relies on strncat() to add '\0'
778 strncat(tmp,word,s - word);
779 if ((s[1] == '{') && (e=ap_strchr_c(s,'}'))) {
786 strncat(tmp, s, e2-s);
790 /* ignore invalid strings */
794 } while ((s=ap_strchr_c(word,'$')));
797 return apr_pstrdup(p,tmp);
799 API_EXPORT(int) ap_cfg_closefile(configfile_t *cfp)
802 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
803 "Done with config file %s", cfp->name);
805 return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
808 static apr_status_t cfg_close(void *param)
810 apr_file_t *cfp = (apr_file_t *) param;
811 return (apr_close(cfp));
814 static int cfg_getch(void *param)
817 apr_file_t *cfp = (apr_file_t *) param;
818 if (apr_getc(&ch, cfp) == APR_SUCCESS)
823 static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
825 apr_file_t *cfp = (apr_file_t *) param;
826 if (apr_fgets(buf, bufsiz, cfp) == APR_SUCCESS)
831 /* Open a configfile_t as FILE, return open configfile_t struct pointer */
832 API_EXPORT(apr_status_t) ap_pcfg_openfile(configfile_t **ret_cfg, apr_pool_t *p, const char *name)
834 configfile_t *new_cfg;
835 apr_file_t *file = NULL;
843 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
844 "Internal error: pcfg_openfile() called with NULL filename");
848 if (!ap_os_is_filename_valid(name)) {
849 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
850 "Access to config file %s denied: not a valid filename",
855 status = apr_open(&file, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
857 ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL,
858 "Opening config file %s (%s)",
859 name, (status != APR_SUCCESS) ?
860 apr_strerror(status, buf, sizeof(buf)) : "successful");
862 if (status != APR_SUCCESS)
865 status = apr_getfileinfo(&finfo, file);
866 if (status != APR_SUCCESS)
869 if (finfo.filetype != APR_REG &&
870 #if defined(WIN32) || defined(OS2)
871 !(strcasecmp(name, "nul") == 0 ||
872 (strlen(name) >= 4 &&
873 strcasecmp(name + strlen(name) - 4, "/nul") == 0))) {
875 strcmp(name, "/dev/null") != 0) {
876 #endif /* WIN32 || OS2 */
877 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
878 "Access to file %s denied by server: not a regular file",
884 new_cfg = apr_palloc(p, sizeof(*new_cfg));
885 new_cfg->param = file;
886 new_cfg->name = apr_pstrdup(p, name);
887 new_cfg->getch = (int (*)(void *)) cfg_getch;
888 new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
889 new_cfg->close = (int (*)(void *)) cfg_close;
890 new_cfg->line_number = 0;
896 /* Allocate a configfile_t handle with user defined functions and params */
897 API_EXPORT(configfile_t *) ap_pcfg_open_custom(apr_pool_t *p, const char *descr,
899 int(*getch)(void *param),
900 void *(*getstr) (void *buf, size_t bufsiz, void *param),
901 int(*close_func)(void *param))
903 configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
905 ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL, "Opening config handler %s", descr);
907 new_cfg->param = param;
908 new_cfg->name = descr;
909 new_cfg->getch = getch;
910 new_cfg->getstr = getstr;
911 new_cfg->close = close_func;
912 new_cfg->line_number = 0;
917 /* Read one character from a configfile_t */
918 API_EXPORT(int) ap_cfg_getc(configfile_t *cfp)
920 register int ch = cfp->getch(cfp->param);
927 /* Read one line from open configfile_t, strip LF, increase line number */
928 /* If custom handler does not define a getstr() function, read char by char */
929 API_EXPORT(int) ap_cfg_getline(char *buf, size_t bufsize, configfile_t *cfp)
931 /* If a "get string" function is defined, use it */
932 if (cfp->getstr != NULL) {
936 size_t cbufsize = bufsize;
940 if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
944 * check for line continuation,
945 * i.e. match [^\\]\\[\r]\n only
948 while (cp < cbuf+cbufsize && *cp != '\0')
950 if (cp > cbuf && cp[-1] == LF) {
952 if (cp > cbuf && cp[-1] == CR)
954 if (cp > cbuf && cp[-1] == '\\') {
956 if (!(cp > cbuf && cp[-1] == '\\')) {
958 * line continuation requested -
959 * then remove backslash and continue
961 cbufsize -= (cp-cbuf);
967 * no real continuation because escaped -
968 * then just remove escape character
970 for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
979 * Leading and trailing white space is eliminated completely
982 while (apr_isspace(*src))
984 /* blast trailing whitespace */
985 dst = &src[strlen(src)];
986 while (--dst >= src && apr_isspace(*dst))
988 /* Zap leading whitespace by shifting */
990 for (dst = buf; (*dst++ = *src++) != '\0'; )
993 #ifdef DEBUG_CFG_LINES
994 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
998 /* No "get string" function defined; read character by character */
1000 register size_t i = 0;
1003 /* skip leading whitespace */
1005 c = cfp->getch(cfp->param);
1006 } while (c == '\t' || c == ' ');
1012 /* too small, assume caller is crazy */
1017 if ((c == '\t') || (c == ' ')) {
1019 while ((c == '\t') || (c == ' '))
1020 c = cfp->getch(cfp->param);
1023 /* silently ignore CR (_assume_ that a LF follows) */
1024 c = cfp->getch(cfp->param);
1027 /* increase line number and return on LF */
1030 if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
1032 * check for line continuation
1034 if (i > 0 && buf[i-1] == '\\') {
1036 if (!(i > 0 && buf[i-1] == '\\')) {
1037 /* line is continued */
1038 c = cfp->getch(cfp->param);
1041 /* else nothing needs be done because
1042 * then the backslash is escaped and
1043 * we just strip to a single one
1046 /* blast trailing whitespace */
1047 while (i > 0 && apr_isspace(buf[i - 1]))
1050 #ifdef DEBUG_CFG_LINES
1051 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
1057 c = cfp->getch(cfp->param);
1062 /* Size an HTTP header field list item, as separated by a comma.
1063 * The return value is a pointer to the beginning of the non-empty list item
1064 * within the original string (or NULL if there is none) and the address
1065 * of field is shifted to the next non-comma, non-whitespace character.
1066 * len is the length of the item excluding any beginning whitespace.
1068 API_EXPORT(const char *) ap_size_list_item(const char **field, int *len)
1070 const unsigned char *ptr = (const unsigned char *)*field;
1071 const unsigned char *token;
1072 int in_qpair, in_qstr, in_com;
1074 /* Find first non-comma, non-whitespace byte */
1076 while (*ptr == ',' || apr_isspace(*ptr))
1081 /* Find the end of this item, skipping over dead bits */
1083 for (in_qpair = in_qstr = in_com = 0;
1084 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1092 case '\\': in_qpair = 1; /* quoted-pair */
1094 case '"' : if (!in_com) /* quoted string delim */
1097 case '(' : if (!in_qstr) /* comment (may nest) */
1100 case ')' : if (in_com) /* end comment */
1108 if ((*len = (ptr - token)) == 0) {
1109 *field = (const char *)ptr;
1113 /* Advance field pointer to the next non-comma, non-white byte */
1115 while (*ptr == ',' || apr_isspace(*ptr))
1118 *field = (const char *)ptr;
1119 return (const char *)token;
1122 /* Retrieve an HTTP header field list item, as separated by a comma,
1123 * while stripping insignificant whitespace and lowercasing anything not in
1124 * a quoted string or comment. The return value is a new string containing
1125 * the converted list item (or NULL if none) and the address pointed to by
1126 * field is shifted to the next non-comma, non-whitespace.
1128 API_EXPORT(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1130 const char *tok_start;
1131 const unsigned char *ptr;
1134 int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1136 /* Find the beginning and maximum length of the list item so that
1137 * we can allocate a buffer for the new string and reset the field.
1139 if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1142 token = apr_palloc(p, tok_len + 1);
1144 /* Scan the token again, but this time copy only the good bytes.
1145 * We skip extra whitespace and any whitespace around a '=', '/',
1146 * or ';' and lowercase normal characters not within a comment,
1147 * quoted-string or quoted-pair.
1149 for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1150 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1159 case '\\': in_qpair = 1;
1165 case '"' : if (!in_com)
1172 case '(' : if (!in_qstr)
1179 case ')' : if (in_com)
1185 case '\t': if (addspace)
1187 if (in_com || in_qstr)
1194 case ';' : if (!(in_com || in_qstr))
1198 default : if (addspace == 1)
1200 *pos++ = (in_com || in_qstr) ? *ptr
1201 : apr_tolower(*ptr);
1212 /* Find an item in canonical form (lowercase, no extra spaces) within
1213 * an HTTP field value list. Returns 1 if found, 0 if not found.
1214 * This would be much more efficient if we stored header fields as
1215 * an array of list items as they are received instead of a plain string.
1217 API_EXPORT(int) ap_find_list_item(apr_pool_t *p, const char *line, const char *tok)
1219 const unsigned char *pos;
1220 const unsigned char *ptr = (const unsigned char *)line;
1221 int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1226 do { /* loop for each item in line's list */
1228 /* Find first non-comma, non-whitespace byte */
1230 while (*ptr == ',' || apr_isspace(*ptr))
1234 good = 1; /* until proven otherwise for this item */
1236 break; /* no items left and nothing good found */
1238 /* We skip extra whitespace and any whitespace around a '=', '/',
1239 * or ';' and lowercase normal characters not within a comment,
1240 * quoted-string or quoted-pair.
1242 for (pos = (const unsigned char *)tok;
1243 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1249 good = (*pos++ == *ptr);
1253 case '\\': in_qpair = 1;
1255 good = good && (*pos++ == ' ');
1256 good = good && (*pos++ == *ptr);
1259 case '"' : if (!in_com)
1262 good = good && (*pos++ == ' ');
1263 good = good && (*pos++ == *ptr);
1266 case '(' : if (!in_qstr)
1269 good = good && (*pos++ == ' ');
1270 good = good && (*pos++ == *ptr);
1273 case ')' : if (in_com)
1275 good = good && (*pos++ == *ptr);
1279 case '\t': if (addspace || !good)
1281 if (in_com || in_qstr)
1282 good = (*pos++ == *ptr);
1288 case ';' : if (!(in_com || in_qstr))
1290 good = good && (*pos++ == *ptr);
1292 default : if (!good)
1295 good = (*pos++ == ' ');
1296 if (in_com || in_qstr)
1297 good = good && (*pos++ == *ptr);
1299 good = good && (*pos++ == apr_tolower(*ptr));
1306 good = 0; /* not good if only a prefix was matched */
1308 } while (*ptr && !good);
1314 /* Retrieve a token, spacing over it and returning a pointer to
1315 * the first non-white byte afterwards. Note that these tokens
1316 * are delimited by semis and commas; and can also be delimited
1317 * by whitespace at the caller's option.
1320 API_EXPORT(char *) ap_get_token(apr_pool_t *p, const char **accept_line, int accept_white)
1322 const char *ptr = *accept_line;
1323 const char *tok_start;
1327 /* Find first non-white byte */
1329 while (*ptr && apr_isspace(*ptr))
1334 /* find token end, skipping over quoted strings.
1335 * (comments are already gone).
1338 while (*ptr && (accept_white || !apr_isspace(*ptr))
1339 && *ptr != ';' && *ptr != ',') {
1346 tok_len = ptr - tok_start;
1347 token = apr_pstrndup(p, tok_start, tok_len);
1349 /* Advance accept_line pointer to the next non-white byte */
1351 while (*ptr && apr_isspace(*ptr))
1359 /* find http tokens, see the definition of token from RFC2068 */
1360 API_EXPORT(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1362 const unsigned char *start_token;
1363 const unsigned char *s;
1368 s = (const unsigned char *)line;
1370 /* find start of token, skip all stop characters, note NUL
1371 * isn't a token stop, so we don't need to test for it
1373 while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1380 /* find end of the token */
1381 while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1384 if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
1394 API_EXPORT(int) ap_find_last_token(apr_pool_t *p, const char *line, const char *tok)
1396 int llen, tlen, lidx;
1401 llen = strlen(line);
1406 ((lidx > 0) && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1409 return (strncasecmp(&line[lidx], tok, tlen) == 0);
1412 API_EXPORT(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1416 const unsigned char *s;
1418 cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1419 d = (unsigned char *)cmd;
1420 s = (const unsigned char *)str;
1423 #if defined(OS2) || defined(WIN32)
1424 /* Don't allow '&' in parameters under OS/2. */
1425 /* This can be used to send commands to the shell. */
1432 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1442 static char x2c(const char *what)
1444 register char digit;
1446 #ifndef CHARSET_EBCDIC
1447 digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
1449 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
1450 #else /*CHARSET_EBCDIC*/
1457 digit = apr_xlate_conv_byte(ap_hdrs_from_ascii, 0xFF & strtol(xstr, NULL, 16));
1458 #endif /*CHARSET_EBCDIC*/
1464 * Returns 0 on success, non-zero on error
1466 * bad % escape returns HTTP_BAD_REQUEST
1468 * decoding %00 -> \0
1469 * decoding %2f -> / (a special character)
1470 * returns HTTP_NOT_FOUND
1472 API_EXPORT(int) ap_unescape_url(char *url)
1474 register int badesc, badpath;
1479 /* Initial scan for first '%'. Don't bother writing values before
1481 y = strchr(url, '%');
1485 for (x = y; *y; ++x, ++y) {
1489 if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1496 if (*x == '/' || *x == '\0')
1503 return HTTP_BAD_REQUEST;
1505 return HTTP_NOT_FOUND;
1510 API_EXPORT(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1511 unsigned port, const request_rec *r)
1513 if (ap_is_default_port(port, r))
1514 return apr_pstrdup(p, hostname);
1516 return apr_psprintf(p, "%s:%u", hostname, port);
1520 /* c2x takes an unsigned, and expects the caller has guaranteed that
1521 * 0 <= what < 256... which usually means that you have to cast to
1522 * unsigned char first, because (unsigned)(char)(x) first goes through
1523 * signed extension to an int before the unsigned cast.
1525 * The reason for this assumption is to assist gcc code generation --
1526 * the unsigned char -> unsigned extension is already done earlier in
1527 * both uses of this code, so there's no need to waste time doing it
1530 static const char c2x_table[] = "0123456789abcdef";
1532 static apr_inline unsigned char *c2x(unsigned what, unsigned char *where)
1534 #ifdef CHARSET_EBCDIC
1535 what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1536 #endif /*CHARSET_EBCDIC*/
1538 *where++ = c2x_table[what >> 4];
1539 *where++ = c2x_table[what & 0xf];
1544 * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1545 * routine is (should be) OS independent.
1547 * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1548 * cases if a ':' occurs before the first '/' in the URL, the URL should be
1549 * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1550 * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1551 * efficiency reasons, we don't use escape_path_segment(), which is provided for
1552 * reference. Again, RFC 1808 is where this stuff is defined.
1554 * If partial is set, os_escape_path() assumes that the path will be appended to
1555 * something with a '/' in it (and thus does not prefix "./").
1558 API_EXPORT(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1560 char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
1561 const unsigned char *s = (const unsigned char *)segment;
1562 unsigned char *d = (unsigned char *)copy;
1566 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1578 API_EXPORT(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1580 char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1581 const unsigned char *s = (const unsigned char *)path;
1582 unsigned char *d = (unsigned char *)copy;
1586 const char *colon = ap_strchr_c(path, ':');
1587 const char *slash = ap_strchr_c(path, '/');
1589 if (colon && (!slash || colon < slash)) {
1595 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1607 /* ap_escape_uri is now a macro for os_escape_path */
1609 API_EXPORT(char *) ap_escape_html(apr_pool_t *p, const char *s)
1614 /* first, count the number of extra characters */
1615 for (i = 0, j = 0; s[i] != '\0'; i++)
1616 if (s[i] == '<' || s[i] == '>')
1618 else if (s[i] == '&')
1622 return apr_pstrndup(p, s, i);
1624 x = apr_palloc(p, i + j + 1);
1625 for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1627 memcpy(&x[j], "<", 4);
1630 else if (s[i] == '>') {
1631 memcpy(&x[j], ">", 4);
1634 else if (s[i] == '&') {
1635 memcpy(&x[j], "&", 5);
1645 API_EXPORT(int) ap_is_directory(const char *path)
1649 if (apr_stat(&finfo, path, NULL) == -1)
1650 return 0; /* in error condition, just return no */
1652 return (finfo.filetype == APR_DIR);
1655 API_EXPORT(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1662 return apr_pstrcat(a, "/", src2, NULL);
1664 if (src1[x - 1] != '/')
1665 return apr_pstrcat(a, src1, "/", src2, NULL);
1667 return apr_pstrcat(a, src1, src2, NULL);
1671 * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1673 API_EXPORT(int) ap_is_url(const char *u)
1677 for (x = 0; u[x] != ':'; x++) {
1679 ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1680 (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1685 return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
1688 #ifndef HAVE_INITGROUPS
1689 int initgroups(const char *name, gid_t basegid)
1691 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32)
1692 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
1694 #else /* ndef QNX */
1695 gid_t groups[NGROUPS_MAX];
1701 groups[index++] = basegid;
1703 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
1704 if (g->gr_gid != basegid) {
1707 for (names = g->gr_mem; *names != NULL; ++names)
1708 if (!strcmp(*names, name))
1709 groups[index++] = g->gr_gid;
1714 return setgroups(index, groups);
1715 #endif /* def QNX */
1717 #endif /* def NEED_INITGROUPS */
1719 API_EXPORT(int) ap_ind(const char *s, char c)
1723 for (x = 0; s[x]; x++)
1730 API_EXPORT(int) ap_rind(const char *s, char c)
1734 for (x = strlen(s) - 1; x != -1; x--)
1741 API_EXPORT(void) ap_str_tolower(char *str)
1744 *str = apr_tolower(*str);
1749 API_EXPORT(uid_t) ap_uname2id(const char *name)
1757 return (atoi(&name[1]));
1759 if (!(ent = getpwnam(name))) {
1760 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
1763 return (ent->pw_uid);
1767 API_EXPORT(gid_t) ap_gname2id(const char *name)
1775 return (atoi(&name[1]));
1777 if (!(ent = getgrnam(name))) {
1778 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name);
1781 return (ent->gr_gid);
1787 * Parses a host of the form <address>[:port]
1788 * :port is permitted if 'port' is not NULL
1790 unsigned long ap_get_virthost_addr(char *w, unsigned short *ports)
1792 struct hostent *hep;
1793 unsigned long my_addr;
1797 if (ports != NULL) {
1799 if (p != NULL && strcmp(p + 1, "*") != 0)
1800 *ports = atoi(p + 1);
1805 if (strcmp(w, "*") == 0) {
1808 return htonl(INADDR_ANY);
1811 my_addr = apr_inet_addr((char *)w);
1812 if (my_addr != INADDR_NONE) {
1818 hep = gethostbyname(w);
1820 if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
1821 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Cannot resolve host name %s --- exiting!", w);
1825 if (hep->h_addr_list[1]) {
1826 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Host %s has multiple addresses ---", w);
1827 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "you must choose one explicitly for use as");
1828 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "a virtual host. Exiting!!!");
1835 return ((struct in_addr *) (hep->h_addr))->s_addr;
1839 static char *find_fqdn(apr_pool_t *a, struct hostent *p)
1843 if (!strchr(p->h_name, '.')) {
1844 for (x = 0; p->h_aliases[x]; ++x) {
1845 if (strchr(p->h_aliases[x], '.') &&
1846 (!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
1847 return apr_pstrdup(a, p->h_aliases[x]);
1851 return apr_pstrdup(a, (void *) p->h_name);
1854 char *ap_get_local_host(apr_pool_t *a)
1856 #ifndef MAXHOSTNAMELEN
1857 #define MAXHOSTNAMELEN 256
1859 char str[MAXHOSTNAMELEN + 1];
1860 char *server_hostname = NULL;
1864 if (gethostname(str, sizeof(str) - 1) == 0)
1866 if (gethostname(str, sizeof(str) - 1) != 0)
1869 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, NULL,
1870 "%s: gethostname() failed to detemine ServerName",
1875 str[sizeof(str) - 1] = '\0';
1876 if ((!(p = gethostbyname(str)))
1877 || (!(server_hostname = find_fqdn(a, p)))) {
1878 /* Recovery - return the default servername by IP: */
1879 if (!str && p->h_addr_list[0]) {
1880 apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
1881 server_hostname = apr_pstrdup(a, str);
1882 /* We will drop through to report the IP-named server */
1886 /* Since we found a fdqn, return it with no logged message. */
1887 return server_hostname;
1891 if (!server_hostname)
1892 server_hostname = apr_pstrdup(a, "127.0.0.1");
1894 ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, NULL,
1895 "%s: Could not find determine the server's fully qualified "
1896 "domain name, using %s for ServerName",
1897 ap_server_argv0, server_hostname);
1899 return server_hostname;
1902 /* simple 'pool' alloc()ing glue to ap_base64.c
1904 API_EXPORT(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
1909 decoded = (char *) apr_palloc(p, 1 + ap_base64decode_len(bufcoded));
1910 l = ap_base64decode(decoded, bufcoded);
1911 decoded[l] = '\0'; /* make binary sequence into string */
1916 API_EXPORT(char *) ap_pbase64encode(apr_pool_t *p, char *string)
1919 int l = strlen(string);
1921 encoded = (char *) apr_palloc(p, 1 + ap_base64encode_len(l));
1922 l = ap_base64encode(encoded, string, l);
1923 encoded[l] = '\0'; /* make binary sequence into string */
1928 /* deprecated names for the above two functions, here for compatibility
1930 API_EXPORT(char *) ap_uudecode(apr_pool_t *p, const char *bufcoded)
1932 return ap_pbase64decode(p, bufcoded);
1935 API_EXPORT(char *) ap_uuencode(apr_pool_t *p, char *string)
1937 return ap_pbase64encode(p, string);
1940 /* we want to downcase the type/subtype for comparison purposes
1941 * but nothing else because ;parameter=foo values are case sensitive.
1942 * XXX: in truth we want to downcase parameter names... but really,
1943 * apache has never handled parameters and such correctly. You
1944 * also need to compress spaces and such to be able to compare
1947 API_EXPORT(void) ap_content_type_tolower(char *str)
1951 semi = strchr(str, ';');
1956 *str = apr_tolower(*str);
1965 * Given a string, replace any bare " with \" .
1967 API_EXPORT(char *) ap_escape_quotes (apr_pool_t *p, const char *instring)
1970 const char *inchr = instring;
1971 char *outchr, *outstring;
1974 * Look through the input string, jogging the length of the output
1975 * string up by an extra byte each time we find an unescaped ".
1977 while (*inchr != '\0') {
1979 if (*inchr == '"') {
1983 * If we find a slosh, and it's not the last byte in the string,
1984 * it's escaping something - advance past both bytes.
1986 if ((*inchr == '\\') && (inchr[1] != '\0')) {
1992 outstring = apr_palloc(p, newlen + 1);
1996 * Now copy the input string to the output string, inserting a slosh
1997 * in front of every " that doesn't already have one.
1999 while (*inchr != '\0') {
2000 if ((*inchr == '\\') && (inchr[1] != '\0')) {
2001 *outchr++ = *inchr++;
2002 *outchr++ = *inchr++;
2004 if (*inchr == '"') {
2007 if (*inchr != '\0') {
2008 *outchr++ = *inchr++;