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 * 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))
119 * Examine a field value (such as a media-/content-type) string and return
120 * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
122 AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
126 if (intype == NULL) return NULL;
128 semi = ap_strchr_c(intype, ';');
130 return apr_pstrdup(p, intype);
133 while ((semi > intype) && apr_isspace(semi[-1])) {
136 return apr_pstrndup(p, intype, semi - intype);
140 AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt, int gmt)
143 char ts[MAX_STRING_LEN];
144 char tf[MAX_STRING_LEN];
145 apr_exploded_time_t xt;
151 apr_explode_gmt(&xt, t);
152 /* Convert %Z to "GMT" and %z to "+0000";
153 * on hosts that do not have a time zone string in struct tm,
154 * strftime must assume its argument is local time.
156 for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
158 if (*f != '%') continue;
169 case 'z': /* common extension */
183 apr_explode_localtime(&xt, t);
186 /* check return code? */
187 apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
188 ts[MAX_STRING_LEN - 1] = '\0';
189 return apr_pstrdup(p, ts);
192 /* Roy owes Rob beer. */
193 /* Rob owes Roy dinner. */
195 /* These legacy comments would make a lot more sense if Roy hadn't
196 * replaced the old later_than() routine with util_date.c.
198 * Well, okay, they still wouldn't make any sense.
201 /* Match = 0, NoMatch = 1, Abort = -1
202 * Based loosely on sections of wildmat.c by Rich Salz
203 * Hmmm... shouldn't this really go component by component?
205 AP_DECLARE(int) ap_strcmp_match(const char *str, const char *exp)
209 for (x = 0, y = 0; exp[y]; ++y, ++x) {
210 if ((!str[x]) && (exp[y] != '*'))
213 while (exp[++y] == '*');
218 if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
223 else if ((exp[y] != '?') && (str[x] != exp[y]))
226 return (str[x] != '\0');
229 AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *exp)
233 for (x = 0, y = 0; exp[y]; ++y, ++x) {
234 if ((!str[x]) && (exp[y] != '*'))
237 while (exp[++y] == '*');
242 if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
247 else if ((exp[y] != '?') && (apr_tolower(str[x]) != apr_tolower(exp[y])))
250 return (str[x] != '\0');
253 /* We actually compare the canonical root to this root, (but we don't
254 * waste time checking the case), since every use of this function in
255 * httpd-2.0 tests if the path is 'proper', meaning we've already passed
256 * it through apr_filepath_merge, or we haven't.
258 AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
261 const char *ourdir = dir;
262 if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
263 || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
269 AP_DECLARE(int) ap_is_matchexp(const char *str)
273 for (x = 0; str[x]; x++)
274 if ((str[x] == '*') || (str[x] == '?'))
280 * Here's a pool-based interface to POSIX regex's regcomp().
281 * Note that we return regex_t instead of being passed one.
282 * The reason is that if you use an already-used regex_t structure,
283 * the memory that you've already allocated gets forgotten, and
284 * regfree() doesn't clear it. So we don't allow it.
287 static apr_status_t regex_cleanup(void *preg)
289 regfree((regex_t *) preg);
293 AP_DECLARE(regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
296 regex_t *preg = apr_palloc(p, sizeof(regex_t));
298 if (regcomp(preg, pattern, cflags)) {
302 apr_pool_cleanup_register(p, (void *) preg, regex_cleanup, regex_cleanup);
307 AP_DECLARE(void) ap_pregfree(apr_pool_t *p, regex_t * reg)
310 apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
314 * Similar to standard strstr() but we ignore case in this version.
315 * Based on the strstr() implementation further below.
317 AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
325 for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
326 if (*s1 == '\0') return(NULL);
327 /* found first character of s2, see if the rest matches */
330 while (apr_tolower(*++p1) == apr_tolower(*++p2)) {
332 /* both strings ended together */
337 /* second string ended, a match */
340 /* didn't find a match here, try starting at next character in s1 */
347 * Returns an offsetted pointer in bigstring immediately after
348 * prefix. Returns bigstring if bigstring doesn't start with
349 * prefix or if prefix is longer than bigstring while still matching.
350 * NOTE: pointer returned is relative to bigstring, so we
351 * can use standard pointer comparisons in the calling function
352 * (eg: test if ap_stripprefix(a,b) == a)
354 AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
363 while (*p1 && *prefix) {
364 if (*p1++ != *prefix++)
370 /* hit the end of bigstring! */
375 * Apache stub function for the regex libraries regexec() to make sure the
376 * whole regex(3) API is available through the Apache (exported) namespace.
377 * This is especially important for the DSO situations of modules.
378 * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
380 AP_DECLARE(int) ap_regexec(regex_t *preg, const char *string,
381 size_t nmatch, regmatch_t pmatch[], int eflags)
383 return regexec(preg, string, nmatch, pmatch, eflags);
386 AP_DECLARE(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
388 return regerror(errcode, preg, errbuf, errbuf_size);
392 /* This function substitutes for $0-$9, filling in regular expression
393 * submatches. Pass it the same nmatch and pmatch arguments that you
394 * passed ap_regexec(). pmatch should not be greater than the maximum number
395 * of subexpressions - i.e. one more than the re_nsub member of regex_t.
397 * input should be the string with the $-expressions, source should be the
398 * string that was matched against.
400 * It returns the substituted string, or NULL on error.
402 * Parts of this code are based on Henry Spencer's regsub(), from his
403 * AT&T V8 regexp package.
406 AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input, const char *source,
407 size_t nmatch, regmatch_t pmatch[])
409 const char *src = input;
418 return apr_pstrdup(p, src);
420 /* First pass, find the size */
424 while ((c = *src++) != '\0') {
427 else if (c == '$' && apr_isdigit(*src))
432 if (no > 9) { /* Ordinary character. */
433 if (c == '\\' && (*src == '$' || *src == '&'))
437 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
438 len += pmatch[no].rm_eo - pmatch[no].rm_so;
443 dest = dst = apr_pcalloc(p, len + 1);
445 /* Now actually fill in the string */
449 while ((c = *src++) != '\0') {
452 else if (c == '$' && apr_isdigit(*src))
457 if (no > 9) { /* Ordinary character. */
458 if (c == '\\' && (*src == '$' || *src == '&'))
462 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
463 len = pmatch[no].rm_eo - pmatch[no].rm_so;
464 memcpy(dst, source + pmatch[no].rm_so, len);
475 * Parse .. so we don't compromise security
477 AP_DECLARE(void) ap_getparents(char *name)
481 /* Four paseses, as per RFC 1808 */
482 /* a) remove ./ path segments */
484 for (l = 0, w = 0; name[l] != '\0';) {
485 if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
488 name[w++] = name[l++];
491 /* b) remove trailing . path, segment */
492 if (w == 1 && name[0] == '.')
494 else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
498 /* c) remove all xx/../ segments. (including leading ../ and /../) */
501 while (name[l] != '\0') {
502 if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
503 (l == 0 || name[l - 1] == '/')) {
504 register int m = l + 3, n;
508 while (l >= 0 && name[l] != '/')
515 while ((name[n] = name[m]))
522 /* d) remove trailing xx/.. segment. */
523 if (l == 2 && name[0] == '.' && name[1] == '.')
525 else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
528 while (l >= 0 && name[l] != '/')
538 AP_DECLARE(void) ap_no2slash(char *name)
544 #ifdef HAVE_UNC_PATHS
545 /* Check for UNC names. Leave leading two slashes. */
546 if (s[0] == '/' && s[1] == '/')
551 if ((*d++ = *s) == '/') {
565 * copy at most n leading directories of s into d
566 * d should be at least as large as s plus 1 extra byte
568 * the return value is the ever useful pointer to the trailing \0 of d
570 * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
571 * so that if n == 0, "/" is returned in d with n == 1
572 * and s == "e:/test.html", "e:/" is returned in d
573 * *** See also directory_walk in modules/http/http_request.c
576 * /a/b, 0 ==> / (true for all platforms)
585 * c:/a/b 3 ==> c:/a/b
586 * c:/a/b 4 ==> c:/a/b
588 AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
597 if (*s == '\0' || (*s == '/' && (--n) == 0)) {
609 * return the parent directory name including trailing / of the file s
611 AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
613 const char *last_slash = ap_strrchr_c(s, '/');
617 if (last_slash == NULL) {
618 return apr_pstrdup(p, "");
620 l = (last_slash - s) + 1;
621 d = apr_palloc(p, l + 1);
628 AP_DECLARE(int) ap_count_dirs(const char *path)
632 for (x = 0, n = 0; path[x]; x++)
638 AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
640 return ap_getword(atrans, (const char **) line, stop);
643 AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
645 const char *pos = ap_strchr_c(*line, stop);
649 res = apr_pstrdup(atrans, *line);
650 *line += strlen(*line);
654 res = apr_pstrndup(atrans, *line, pos - *line);
656 while (*pos == stop) {
665 AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
667 return ap_getword_white(atrans, (const char **) line);
670 AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
675 for (x = 0; (*line)[x]; x++) {
676 if (apr_isspace((*line)[x])) {
683 res = apr_pstrdup(atrans, *line);
684 *line += strlen(*line);
688 res = apr_palloc(atrans, pos + 1);
689 apr_cpystrn(res, *line, pos + 1);
691 while (apr_isspace((*line)[pos]))
699 AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line, char stop)
701 return ap_getword_nulls(atrans, (const char **) line, stop);
704 AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line, char stop)
706 const char *pos = ap_strchr_c(*line, stop);
710 res = apr_pstrdup(atrans, *line);
711 *line += strlen(*line);
715 res = apr_pstrndup(atrans, *line, pos - *line);
724 /* Get a word, (new) config-file style --- quoted strings and backslashes
728 static char *substring_conf(apr_pool_t *p, const char *start, int len, char quote)
730 char *result = apr_palloc(p, len + 2);
734 for (i = 0; i < len; ++i) {
735 if (start[i] == '\\' && (start[i + 1] == '\\'
736 || (quote && start[i + 1] == quote)))
737 *resp++ = start[++i];
743 #if RESOLVE_ENV_PER_TOKEN
744 return ap_resolve_env(p,result);
750 AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
752 return ap_getword_conf(p, (const char **) line);
755 AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
757 const char *str = *line, *strend;
761 while (*str && apr_isspace(*str))
769 if ((quote = *str) == '"' || quote == '\'') {
771 while (*strend && *strend != quote) {
772 if (*strend == '\\' && strend[1] && strend[1] == quote)
777 res = substring_conf(p, str + 1, strend - str - 1, quote);
779 if (*strend == quote)
784 while (*strend && !apr_isspace(*strend))
787 res = substring_conf(p, str, strend - str, 0);
790 while (*strend && apr_isspace(*strend))
796 /* Check a string for any ${ENV} environment variable
797 * construct and replace each them by the value of
798 * that environment variable, if it exists. If the
799 * environment value does not exist, leave the ${ENV}
800 * construct alone; it means something else.
802 AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
804 char tmp[ MAX_STRING_LEN ];
808 if (!(s=ap_strchr_c(word,'$')))
812 /* XXX - relies on strncat() to add '\0'
814 strncat(tmp,word,s - word);
815 if ((s[1] == '{') && (e=ap_strchr_c(s,'}'))) {
822 strncat(tmp, s, e2-s);
826 /* ignore invalid strings */
830 } while ((s=ap_strchr_c(word,'$')));
833 return apr_pstrdup(p,tmp);
835 AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
838 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
839 "Done with config file %s", cfp->name);
841 return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
844 static apr_status_t cfg_close(void *param)
846 apr_file_t *cfp = (apr_file_t *) param;
847 return (apr_file_close(cfp));
850 static int cfg_getch(void *param)
853 apr_file_t *cfp = (apr_file_t *) param;
854 if (apr_file_getc(&ch, cfp) == APR_SUCCESS)
859 static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
861 apr_file_t *cfp = (apr_file_t *) param;
863 rv = apr_file_gets(buf, bufsiz, cfp);
864 if (rv == APR_SUCCESS || (rv == APR_EOF && strcmp(buf, "")))
869 /* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
870 AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg, apr_pool_t *p, const char *name)
872 ap_configfile_t *new_cfg;
873 apr_file_t *file = NULL;
881 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
882 "Internal error: pcfg_openfile() called with NULL filename");
886 status = apr_file_open(&file, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
888 ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL,
889 "Opening config file %s (%s)",
890 name, (status != APR_SUCCESS) ?
891 apr_strerror(status, buf, sizeof(buf)) : "successful");
893 if (status != APR_SUCCESS)
896 status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
897 if (status != APR_SUCCESS)
900 if (finfo.filetype != APR_REG &&
901 #if defined(WIN32) || defined(OS2) || defined(NETWARE)
902 strcasecmp(apr_filename_of_pathname(name), "nul") != 0) {
904 strcmp(name, "/dev/null") != 0) {
905 #endif /* WIN32 || OS2 */
906 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
907 "Access to file %s denied by server: not a regular file",
909 apr_file_close(file);
913 new_cfg = apr_palloc(p, sizeof(*new_cfg));
914 new_cfg->param = file;
915 new_cfg->name = apr_pstrdup(p, name);
916 new_cfg->getch = (int (*)(void *)) cfg_getch;
917 new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
918 new_cfg->close = (int (*)(void *)) cfg_close;
919 new_cfg->line_number = 0;
925 /* Allocate a ap_configfile_t handle with user defined functions and params */
926 AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(apr_pool_t *p, const char *descr,
928 int(*getch)(void *param),
929 void *(*getstr) (void *buf, size_t bufsiz, void *param),
930 int(*close_func)(void *param))
932 ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
934 ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL, "Opening config handler %s", descr);
936 new_cfg->param = param;
937 new_cfg->name = descr;
938 new_cfg->getch = getch;
939 new_cfg->getstr = getstr;
940 new_cfg->close = close_func;
941 new_cfg->line_number = 0;
945 /* Read one character from a configfile_t */
946 AP_DECLARE(int) ap_cfg_getc(ap_configfile_t *cfp)
948 register int ch = cfp->getch(cfp->param);
954 /* Read one line from open ap_configfile_t, strip LF, increase line number */
955 /* If custom handler does not define a getstr() function, read char by char */
956 AP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, ap_configfile_t *cfp)
958 /* If a "get string" function is defined, use it */
959 if (cfp->getstr != NULL) {
963 size_t cbufsize = bufsize;
967 if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
971 * check for line continuation,
972 * i.e. match [^\\]\\[\r]\n only
975 while (cp < cbuf+cbufsize && *cp != '\0')
977 if (cp > cbuf && cp[-1] == LF) {
979 if (cp > cbuf && cp[-1] == CR)
981 if (cp > cbuf && cp[-1] == '\\') {
983 if (!(cp > cbuf && cp[-1] == '\\')) {
985 * line continuation requested -
986 * then remove backslash and continue
988 cbufsize -= (cp-cbuf);
994 * no real continuation because escaped -
995 * then just remove escape character
997 for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
1006 * Leading and trailing white space is eliminated completely
1009 while (apr_isspace(*src))
1011 /* blast trailing whitespace */
1012 dst = &src[strlen(src)];
1013 while (--dst >= src && apr_isspace(*dst))
1015 /* Zap leading whitespace by shifting */
1017 for (dst = buf; (*dst++ = *src++) != '\0'; )
1020 #ifdef DEBUG_CFG_LINES
1021 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
1025 /* No "get string" function defined; read character by character */
1027 register size_t i = 0;
1030 /* skip leading whitespace */
1032 c = cfp->getch(cfp->param);
1033 } while (c == '\t' || c == ' ');
1039 /* too small, assume caller is crazy */
1044 if ((c == '\t') || (c == ' ')) {
1046 while ((c == '\t') || (c == ' '))
1047 c = cfp->getch(cfp->param);
1050 /* silently ignore CR (_assume_ that a LF follows) */
1051 c = cfp->getch(cfp->param);
1054 /* increase line number and return on LF */
1057 if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
1059 * check for line continuation
1061 if (i > 0 && buf[i-1] == '\\') {
1063 if (!(i > 0 && buf[i-1] == '\\')) {
1064 /* line is continued */
1065 c = cfp->getch(cfp->param);
1068 /* else nothing needs be done because
1069 * then the backslash is escaped and
1070 * we just strip to a single one
1073 /* blast trailing whitespace */
1074 while (i > 0 && apr_isspace(buf[i - 1]))
1077 #ifdef DEBUG_CFG_LINES
1078 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
1084 c = cfp->getch(cfp->param);
1089 /* Size an HTTP header field list item, as separated by a comma.
1090 * The return value is a pointer to the beginning of the non-empty list item
1091 * within the original string (or NULL if there is none) and the address
1092 * of field is shifted to the next non-comma, non-whitespace character.
1093 * len is the length of the item excluding any beginning whitespace.
1095 AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1097 const unsigned char *ptr = (const unsigned char *)*field;
1098 const unsigned char *token;
1099 int in_qpair, in_qstr, in_com;
1101 /* Find first non-comma, non-whitespace byte */
1103 while (*ptr == ',' || apr_isspace(*ptr))
1108 /* Find the end of this item, skipping over dead bits */
1110 for (in_qpair = in_qstr = in_com = 0;
1111 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1119 case '\\': in_qpair = 1; /* quoted-pair */
1121 case '"' : if (!in_com) /* quoted string delim */
1124 case '(' : if (!in_qstr) /* comment (may nest) */
1127 case ')' : if (in_com) /* end comment */
1135 if ((*len = (ptr - token)) == 0) {
1136 *field = (const char *)ptr;
1140 /* Advance field pointer to the next non-comma, non-white byte */
1142 while (*ptr == ',' || apr_isspace(*ptr))
1145 *field = (const char *)ptr;
1146 return (const char *)token;
1149 /* Retrieve an HTTP header field list item, as separated by a comma,
1150 * while stripping insignificant whitespace and lowercasing anything not in
1151 * a quoted string or comment. The return value is a new string containing
1152 * the converted list item (or NULL if none) and the address pointed to by
1153 * field is shifted to the next non-comma, non-whitespace.
1155 AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1157 const char *tok_start;
1158 const unsigned char *ptr;
1161 int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1163 /* Find the beginning and maximum length of the list item so that
1164 * we can allocate a buffer for the new string and reset the field.
1166 if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1169 token = apr_palloc(p, tok_len + 1);
1171 /* Scan the token again, but this time copy only the good bytes.
1172 * We skip extra whitespace and any whitespace around a '=', '/',
1173 * or ';' and lowercase normal characters not within a comment,
1174 * quoted-string or quoted-pair.
1176 for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1177 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1186 case '\\': in_qpair = 1;
1192 case '"' : if (!in_com)
1199 case '(' : if (!in_qstr)
1206 case ')' : if (in_com)
1212 case '\t': if (addspace)
1214 if (in_com || in_qstr)
1221 case ';' : if (!(in_com || in_qstr))
1225 default : if (addspace == 1)
1227 *pos++ = (in_com || in_qstr) ? *ptr
1228 : apr_tolower(*ptr);
1239 /* Find an item in canonical form (lowercase, no extra spaces) within
1240 * an HTTP field value list. Returns 1 if found, 0 if not found.
1241 * This would be much more efficient if we stored header fields as
1242 * an array of list items as they are received instead of a plain string.
1244 AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line, const char *tok)
1246 const unsigned char *pos;
1247 const unsigned char *ptr = (const unsigned char *)line;
1248 int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1253 do { /* loop for each item in line's list */
1255 /* Find first non-comma, non-whitespace byte */
1257 while (*ptr == ',' || apr_isspace(*ptr))
1261 good = 1; /* until proven otherwise for this item */
1263 break; /* no items left and nothing good found */
1265 /* We skip extra whitespace and any whitespace around a '=', '/',
1266 * or ';' and lowercase normal characters not within a comment,
1267 * quoted-string or quoted-pair.
1269 for (pos = (const unsigned char *)tok;
1270 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1276 good = (*pos++ == *ptr);
1280 case '\\': in_qpair = 1;
1282 good = good && (*pos++ == ' ');
1283 good = good && (*pos++ == *ptr);
1286 case '"' : if (!in_com)
1289 good = good && (*pos++ == ' ');
1290 good = good && (*pos++ == *ptr);
1293 case '(' : if (!in_qstr)
1296 good = good && (*pos++ == ' ');
1297 good = good && (*pos++ == *ptr);
1300 case ')' : if (in_com)
1302 good = good && (*pos++ == *ptr);
1306 case '\t': if (addspace || !good)
1308 if (in_com || in_qstr)
1309 good = (*pos++ == *ptr);
1315 case ';' : if (!(in_com || in_qstr))
1317 good = good && (*pos++ == *ptr);
1319 default : if (!good)
1322 good = (*pos++ == ' ');
1323 if (in_com || in_qstr)
1324 good = good && (*pos++ == *ptr);
1326 good = good && (*pos++ == apr_tolower(*ptr));
1333 good = 0; /* not good if only a prefix was matched */
1335 } while (*ptr && !good);
1341 /* Retrieve a token, spacing over it and returning a pointer to
1342 * the first non-white byte afterwards. Note that these tokens
1343 * are delimited by semis and commas; and can also be delimited
1344 * by whitespace at the caller's option.
1347 AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line, int accept_white)
1349 const char *ptr = *accept_line;
1350 const char *tok_start;
1354 /* Find first non-white byte */
1356 while (*ptr && apr_isspace(*ptr))
1361 /* find token end, skipping over quoted strings.
1362 * (comments are already gone).
1365 while (*ptr && (accept_white || !apr_isspace(*ptr))
1366 && *ptr != ';' && *ptr != ',') {
1373 tok_len = ptr - tok_start;
1374 token = apr_pstrndup(p, tok_start, tok_len);
1376 /* Advance accept_line pointer to the next non-white byte */
1378 while (*ptr && apr_isspace(*ptr))
1386 /* find http tokens, see the definition of token from RFC2068 */
1387 AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1389 const unsigned char *start_token;
1390 const unsigned char *s;
1395 s = (const unsigned char *)line;
1397 /* find start of token, skip all stop characters, note NUL
1398 * isn't a token stop, so we don't need to test for it
1400 while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1407 /* find end of the token */
1408 while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1411 if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
1421 AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line, const char *tok)
1423 int llen, tlen, lidx;
1428 llen = strlen(line);
1433 ((lidx > 0) && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1436 return (strncasecmp(&line[lidx], tok, tlen) == 0);
1439 AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1443 const unsigned char *s;
1445 cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1446 d = (unsigned char *)cmd;
1447 s = (const unsigned char *)str;
1450 #if defined(OS2) || defined(WIN32)
1451 /* Don't allow '&' in parameters under OS/2. */
1452 /* This can be used to send commands to the shell. */
1459 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1469 static char x2c(const char *what)
1471 register char digit;
1473 #if !APR_CHARSET_EBCDIC
1474 digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
1476 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
1477 #else /*APR_CHARSET_EBCDIC*/
1484 digit = apr_xlate_conv_byte(ap_hdrs_from_ascii, 0xFF & strtol(xstr, NULL, 16));
1485 #endif /*APR_CHARSET_EBCDIC*/
1491 * Returns 0 on success, non-zero on error
1493 * bad % escape returns HTTP_BAD_REQUEST
1495 * decoding %00 -> \0
1496 * decoding %2f -> / (a special character)
1497 * returns HTTP_NOT_FOUND
1499 AP_DECLARE(int) ap_unescape_url(char *url)
1501 register int badesc, badpath;
1506 /* Initial scan for first '%'. Don't bother writing values before
1508 y = strchr(url, '%');
1512 for (x = y; *y; ++x, ++y) {
1516 if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1523 if (*x == '/' || *x == '\0')
1530 return HTTP_BAD_REQUEST;
1532 return HTTP_NOT_FOUND;
1537 AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1538 apr_port_t port, const request_rec *r)
1540 if (ap_is_default_port(port, r))
1541 return apr_pstrdup(p, hostname);
1543 return apr_psprintf(p, "%s:%u", hostname, port);
1547 /* c2x takes an unsigned, and expects the caller has guaranteed that
1548 * 0 <= what < 256... which usually means that you have to cast to
1549 * unsigned char first, because (unsigned)(char)(x) first goes through
1550 * signed extension to an int before the unsigned cast.
1552 * The reason for this assumption is to assist gcc code generation --
1553 * the unsigned char -> unsigned extension is already done earlier in
1554 * both uses of this code, so there's no need to waste time doing it
1557 static const char c2x_table[] = "0123456789abcdef";
1559 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char *where)
1561 #if APR_CHARSET_EBCDIC
1562 what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1563 #endif /*APR_CHARSET_EBCDIC*/
1565 *where++ = c2x_table[what >> 4];
1566 *where++ = c2x_table[what & 0xf];
1571 * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1572 * routine is (should be) OS independent.
1574 * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1575 * cases if a ':' occurs before the first '/' in the URL, the URL should be
1576 * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1577 * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1578 * efficiency reasons, we don't use escape_path_segment(), which is provided for
1579 * reference. Again, RFC 1808 is where this stuff is defined.
1581 * If partial is set, os_escape_path() assumes that the path will be appended to
1582 * something with a '/' in it (and thus does not prefix "./").
1585 AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1587 char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
1588 const unsigned char *s = (const unsigned char *)segment;
1589 unsigned char *d = (unsigned char *)copy;
1593 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1605 AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1607 char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1608 const unsigned char *s = (const unsigned char *)path;
1609 unsigned char *d = (unsigned char *)copy;
1613 const char *colon = ap_strchr_c(path, ':');
1614 const char *slash = ap_strchr_c(path, '/');
1616 if (colon && (!slash || colon < slash)) {
1622 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1634 /* ap_escape_uri is now a macro for os_escape_path */
1636 AP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
1641 /* first, count the number of extra characters */
1642 for (i = 0, j = 0; s[i] != '\0'; i++)
1643 if (s[i] == '<' || s[i] == '>')
1645 else if (s[i] == '&')
1649 return apr_pstrndup(p, s, i);
1651 x = apr_palloc(p, i + j + 1);
1652 for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1654 memcpy(&x[j], "<", 4);
1657 else if (s[i] == '>') {
1658 memcpy(&x[j], ">", 4);
1661 else if (s[i] == '&') {
1662 memcpy(&x[j], "&", 5);
1672 AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
1676 if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1677 return 0; /* in error condition, just return no */
1679 return (finfo.filetype == APR_DIR);
1682 AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
1686 if (apr_lstat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1687 return 0; /* in error condition, just return no */
1689 return (finfo.filetype == APR_DIR);
1692 AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1695 apr_size_t len1, len2;
1698 len1 = strlen(src1);
1699 len2 = strlen(src2);
1700 path = (char *)apr_palloc(a, len1 + len2 + 2); /* +2 for '/' plus null */
1703 memcpy(path + 1, src2, len2 + 1);
1707 memcpy(path, src1, len1);
1709 if (next[-1] != '/') {
1712 memcpy(next, src2, len2 + 1);
1718 * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1720 AP_DECLARE(int) ap_is_url(const char *u)
1724 for (x = 0; u[x] != ':'; x++) {
1726 ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1727 (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1732 return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
1735 AP_DECLARE(int) ap_ind(const char *s, char c)
1737 const char *p = ap_strchr_c(s, c);
1744 AP_DECLARE(int) ap_rind(const char *s, char c)
1746 const char *p = ap_strrchr_c(s, c);
1753 AP_DECLARE(void) ap_str_tolower(char *str)
1756 *str = apr_tolower(*str);
1761 static char *find_fqdn(apr_pool_t *a, struct hostent *p)
1765 if (!strchr(p->h_name, '.')) {
1766 for (x = 0; p->h_aliases[x]; ++x) {
1767 if (strchr(p->h_aliases[x], '.') &&
1768 (!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
1769 return apr_pstrdup(a, p->h_aliases[x]);
1773 return apr_pstrdup(a, (void *) p->h_name);
1776 char *ap_get_local_host(apr_pool_t *a)
1778 #ifndef MAXHOSTNAMELEN
1779 #define MAXHOSTNAMELEN 256
1781 char str[MAXHOSTNAMELEN + 1];
1782 char *server_hostname = NULL;
1786 if (gethostname(str, sizeof(str) - 1) == 0)
1788 if (gethostname(str, sizeof(str) - 1) != 0)
1791 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
1792 "%s: gethostname() failed to determine ServerName",
1797 str[sizeof(str) - 1] = '\0';
1798 if ((!(p = gethostbyname(str)))
1799 || (!(server_hostname = find_fqdn(a, p)))) {
1800 /* Recovery - return the default servername by IP: */
1801 if (p && p->h_addr_list[0]) {
1802 apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
1803 server_hostname = apr_pstrdup(a, str);
1804 /* We will drop through to report the IP-named server */
1808 /* Since we found a fdqn, return it with no logged message. */
1809 return server_hostname;
1813 if (!server_hostname)
1814 server_hostname = apr_pstrdup(a, "127.0.0.1");
1816 ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO|APLOG_STARTUP, 0, a,
1817 "%s: Could not determine the server's fully qualified "
1818 "domain name, using %s for ServerName",
1819 ap_server_argv0, server_hostname);
1821 return server_hostname;
1824 /* simple 'pool' alloc()ing glue to apr_base64.c
1826 AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
1831 decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
1832 l = apr_base64_decode(decoded, bufcoded);
1833 decoded[l] = '\0'; /* make binary sequence into string */
1838 AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
1841 int l = strlen(string);
1843 encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
1844 l = apr_base64_encode(encoded, string, l);
1845 encoded[l] = '\0'; /* make binary sequence into string */
1850 /* we want to downcase the type/subtype for comparison purposes
1851 * but nothing else because ;parameter=foo values are case sensitive.
1852 * XXX: in truth we want to downcase parameter names... but really,
1853 * apache has never handled parameters and such correctly. You
1854 * also need to compress spaces and such to be able to compare
1857 AP_DECLARE(void) ap_content_type_tolower(char *str)
1861 semi = strchr(str, ';');
1866 *str = apr_tolower(*str);
1875 * Given a string, replace any bare " with \" .
1877 AP_DECLARE(char *) ap_escape_quotes (apr_pool_t *p, const char *instring)
1880 const char *inchr = instring;
1881 char *outchr, *outstring;
1884 * Look through the input string, jogging the length of the output
1885 * string up by an extra byte each time we find an unescaped ".
1887 while (*inchr != '\0') {
1889 if (*inchr == '"') {
1893 * If we find a slosh, and it's not the last byte in the string,
1894 * it's escaping something - advance past both bytes.
1896 if ((*inchr == '\\') && (inchr[1] != '\0')) {
1902 outstring = apr_palloc(p, newlen + 1);
1906 * Now copy the input string to the output string, inserting a slosh
1907 * in front of every " that doesn't already have one.
1909 while (*inchr != '\0') {
1910 if ((*inchr == '\\') && (inchr[1] != '\0')) {
1911 *outchr++ = *inchr++;
1912 *outchr++ = *inchr++;
1914 if (*inchr == '"') {
1917 if (*inchr != '\0') {
1918 *outchr++ = *inchr++;